-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.sh
More file actions
executable file
·80 lines (69 loc) · 2 KB
/
driver.sh
File metadata and controls
executable file
·80 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
# According to the book, if no option is specified, the
# code should be compiled. This script sets the mode to
# --compile if none of the ones in the book are used.
MODE="--compile"
if [ "$1" = "--lex" ]; then
MODE="--lex"
shift
elif [ "$1" = "--parse" ]; then
MODE="--parse"
shift
elif [ "$1" = "--codegen" ]; then
MODE="--codegen"
shift
elif [ "$1" = "--tacky" ]; then
MODE="--tacky"
shift
elif [ "$1" = "-S" ]; then
MODE="-S"
shift
elif [ "$1" = "--compile" ]; then
MODE="--compile"
shift
fi
SOURCE_FILE=$1
# Find out what operating system this is
UNAME=`uname`
# Extract the filename from the source file path
BASE=${SOURCE_FILE%.*}
# PREPROCESSED is what we call the file after it has been
# run through the C preprocessor
PREPROCESSED=$BASE".i"
# ASSEMBLED is what we call the assembly file if there is one
ASSEMBLED=$BASE".s"
# ROOT_DIR is the directory where driver.sh is executing from, this
# is handy for figuring out where your compiler executable is
ROOT_DIR=`dirname "$0"`
# Run the C preprocessor on the file
# If you want CPP to include file/line markers so you can track
# the original source file and line, remove the -P option
gcc -P -E $SOURCE_FILE -o $PREPROCESSED
# Exit if there is an error
ERR=$?
if [ $ERR -ne 0 ]; then
rm $PREPROCESSED
exit $ERR
fi
# Execute my C compiler, change this line to execute yours
# Here is an example of one where the Ocaml compiler puts
# the executable main.exe in _build/default:
# $ROOT_DIR/_build/default/bin/main.exe $MODE $PREPROCESSED
# Exit if there was an error
ERR=$?
rm $PREPROCESSED
if [ $ERR -ne 0 ]; then
exit $ERR
fi
if [ "$MODE" = "--compile" ]; then
# If this is running on an Arm Mac, then it is probably using
# clang, so we need to modify the generated asm a little bit
if [ "$UNAME" = "Darwin" ]; then
mv $ASSEMBLED "__astemp"
sed -e "/@progbits/d" < __astemp | sed -e "s/.globl main/.globl _main/" | sed -e "s/^main:/_main:/" > $ASSEMBLED
fi
gcc $ASSEMBLED -o $BASE
ERR=$?
rm $ASSEMBLED
exit $ERR
fi