-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyCompiler.sh
More file actions
executable file
·147 lines (118 loc) · 3.3 KB
/
myCompiler.sh
File metadata and controls
executable file
·147 lines (118 loc) · 3.3 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# ================================
# JCC – Java Compiler Chain
# ================================
# Usage:
# ./myCompiler.sh input.java [output.s]
#
# Folder layout :
# pass1/ -> P1.l, P2.y (flex/bison)
# pass2/ -> Java type checker
# pass3/ -> Java IR1 generator
# pass4/ -> Java IR2 lowering
# pass5/ -> Java IR3 lowering
# pass6/ -> Java MIPS generator
# ================================
set -e # exit on first error
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <source-file> [output-file]"
exit 1
fi
INPUT="$1"
OUTPUT="${2:-final.s}"
# ============== CONFIG: directories ==============
PASS1_DIR="Lab-1"
PASS2_DIR="Lab-2"
PASS3_DIR="Lab-3"
PASS4_DIR="Lab-4"
PASS5_DIR="Lab-5"
PASS6_DIR="Lab-6"
# ============== CONFIG: Java main classes ========
# These are the classes that have `public static void main(String[] args)`
# inside their respective directories.
PASS2_MAIN="P2"
PASS3_MAIN="P3"
PASS4_MAIN="P4"
PASS5_MAIN="P5"
PASS6_MAIN="P6"
# ============== Tools check ======================
for tool in flex bison gcc javac java; do
if ! command -v "$tool" &> /dev/null; then
echo "[ERROR] $tool is not installed or not in PATH"
exit 1
fi
done
# ============== Temp file ========================
TMP_JAVA="$(mktemp /tmp/macrofree-XXXXXX.java)"
cleanup() {
rm -f "$TMP_JAVA"
}
trap cleanup EXIT
# =================================================
# 1. COMPILES & RUNS PASS 1 (flex/bison) in pass1/
# =================================================
echo "[myCompiler] Compiling Pass 1 (flex + bison) in '$PASS1_DIR'..."
(
cd "$PASS1_DIR"
flex -o P1.lex.c P1.l
bison -d -o P1.tab.c P1.y
g++ P1.lex.c P1.tab.c -o P1.out
)
echo "[myCompiler] Running Pass 1 (macro removal)..."
# Runs the generated executable from pass1/ directory
"$PASS1_DIR/P1.out" < "$INPUT" > "$TMP_JAVA"
# =================================================
# 2. COMPILES JAVA PASSES 2–6 in their own folders
# =================================================
echo "[myCompiler] Compiling Java passes..."
(
cd "$PASS2_DIR"
javac P2.java
)
(
cd "$PASS3_DIR"
javac P3.java
)
(
cd "$PASS4_DIR"
javac P4.java
)
(
cd "$PASS5_DIR"
javac P5.java
)
(
cd "$PASS6_DIR"
javac P6.java
)
echo "[myCompiler] Java passes compiled successfully."
# =================================================
# 3. RUNS PASS 2 (type checker) from pass2/
# =================================================
echo "[myCompiler] Running Pass 2 (type checking)..."
(
cd "$PASS2_DIR"
# Note: we pass the *path* to the macro-free file.
java "$PASS2_MAIN" < "$TMP_JAVA"
)
# If we got here, type checking succeeded (exit code 0).
# =================================================
# 4. RUNS PASS 3 → 6 as a pipeline, each in its folder
# =================================================
echo "[myCompiler] Running Passes 3 → 6 (IR + lowering + MIPS)..."
# Each subshell `cd`s into the pass's directory and runs its main.
# All of them read from stdin and write to stdout, so we can pipe them.
(
cd "$PASS3_DIR"
java "$PASS3_MAIN" < "$TMP_JAVA"
) | (
cd "$PASS4_DIR"
java "$PASS4_MAIN"
) | (
cd "$PASS5_DIR"
java "$PASS5_MAIN"
) | (
cd "$PASS6_DIR"
java "$PASS6_MAIN"
) > "$OUTPUT"
echo "[myCompiler] Compilation successful → $OUTPUT"