Skip to content

Commit c49201f

Browse files
committed
merge
2 parents 78678fd + 00bbb87 commit c49201f

File tree

8 files changed

+48
-45
lines changed

8 files changed

+48
-45
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# python-student-support-code
2-
Support for for students (Python)
2+
3+
Support code for students (Python version).
4+
5+
The `runtime.c` file needs to be compiled by doing the following
6+
```
7+
gcc -c -g -std=c99 runtime.c
8+
```
9+
This will produce a file named `runtime.o`. The -g flag is to tell the
10+
compiler to produce debug information that you may need to use
11+
the gdb (or lldb) debugger.

compiler.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ def patch_instructions(self, p: X86Program) -> X86Program:
8383
# YOUR CODE HERE
8484
pass
8585

86-
############################################################################
87-
# Generate Main Function
88-
############################################################################
89-
90-
def generate_main(self, p: X86Program) -> X86Program:
91-
# YOUR CODE HERE
92-
pass
93-
9486
############################################################################
9587
# Prelude & Conclusion
9688
############################################################################
-36 Bytes
Binary file not shown.
-40 Bytes
Binary file not shown.
-2 Bytes
Binary file not shown.

interp_x86/eval_x86.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def eval_program(self, p):
7272

7373
def eval_instructions(self, s):
7474
import pandas as pd
75-
75+
7676
p = x86_parser_instrs.parse(s)
7777

7878
assert p.data == 'instrs'

utils.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,6 @@ def is_python_extension(filename):
202202
# expected "golden" output.
203203
def test_pass(passname, interp, program_root, ast,
204204
compiler_name):
205-
if not interp:
206-
return 0
207205
input_file = program_root + '.in'
208206
output_file = program_root + '.out'
209207
stdin = sys.stdin
@@ -230,11 +228,7 @@ def compile_and_test(compiler, compiler_name, type_check_P, interp_P, interp_C,
230228
program_filename):
231229
total_passes = 0
232230
successful_passes = 0
233-
emulate_x86 = True
234-
if emulate_x86:
235-
from interp_x86.eval_x86 import interp_x86
236-
else:
237-
interp_x86 = None
231+
from interp_x86.eval_x86 import interp_x86
238232

239233
program_root = program_filename.split('.')[0]
240234
with open(program_filename) as source:
@@ -297,16 +291,17 @@ def compile_and_test(compiler, compiler_name, type_check_P, interp_P, interp_C,
297291
test_pass('patch instructions', interp_x86, program_root, x86,
298292
compiler_name)
299293

300-
trace('\n**********\n print x86 \n**********\n')
294+
trace('\n**********\n prelude and conclusion \n**********\n')
295+
x86 = compiler.prelude_and_conclusion(x86)
296+
301297
x86_filename = program_root + ".s"
302298
with open(x86_filename, "w") as dest:
303-
dest.write(compiler.print_x86(x86))
304-
305-
x86 = compiler.generate_main(x86)
299+
dest.write(repr(x86))
306300

307301
total_passes += 1
308302

309-
# Run the x86 program
303+
# Run the final x86 program
304+
emulate_x86 = False
310305
if emulate_x86:
311306
stdin = sys.stdin
312307
stdout = sys.stdout

x86_ast.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import List
2+
from utils import label_name
23

34
class X86Program:
45
__match_args__ = ("body",)
@@ -8,10 +9,15 @@ def __repr__(self):
89
result = ''
910
if type(self.body) == dict:
1011
for (l,ss) in self.body.items():
12+
if l == label_name('main'):
13+
result += '\t.globl ' + label_name('main') + '\n'
1114
result += l + ':\n'
1215
result += '\n'.join([repr(s) for s in ss]) + '\n\n'
1316
else:
14-
result = '\n'.join([repr(s) for s in self.body])
17+
result += '\t.globl ' + label_name('main') + '\n' + \
18+
label_name('main') + ':\n'
19+
result += '\n'.join([repr(s) for s in self.body])
20+
result += '\n'
1521
return result
1622

1723
class instr: ...
@@ -31,16 +37,36 @@ def source(self):
3137
def target(self):
3238
return self.args[-1]
3339
def __repr__(self):
34-
return self.instr + ' ' + ', '.join(repr(a) for a in self.args)
40+
return '\t' + self.instr + ' ' + ', '.join(repr(a) for a in self.args)
3541

3642
class Callq(instr):
3743
__match_args__ = ("func", "num_args")
3844
def __init__(self, func, num_args):
3945
self.func = func
4046
self.num_args = num_args
4147
def __repr__(self):
42-
return 'callq' + ' ' + self.func
48+
return '\tcallq' + ' ' + self.func
4349

50+
class JumpIf(instr):
51+
cc: str
52+
label: str
53+
54+
__match_args__ = ("cc", "label")
55+
def __init__(self, cc, label):
56+
self.cc = cc
57+
self.label = label
58+
def __repr__(self):
59+
return '\tj' + self.cc + ' ' + self.label
60+
61+
class Jump(instr):
62+
label: str
63+
64+
__match_args__ = ("label",)
65+
def __init__(self, label):
66+
self.label = label
67+
def __repr__(self):
68+
return '\tjmp ' + self.label
69+
4470
class Variable(location):
4571
__match_args__ = ("id",)
4672
def __init__(self, id):
@@ -60,7 +86,7 @@ class Immediate(arg):
6086
def __init__(self, value):
6187
self.value = value
6288
def __repr__(self):
63-
return repr(self.value)
89+
return '$' + repr(self.value)
6490
def __eq__(self, other):
6591
if isinstance(other, Immediate):
6692
return self.value == other.value
@@ -112,22 +138,3 @@ def __eq__(self, other):
112138
def __hash__(self):
113139
return hash((self.reg, self.offset))
114140

115-
class JumpIf(instr):
116-
cc: str
117-
label: str
118-
119-
__match_args__ = ("cc", "label")
120-
def __init__(self, cc, label):
121-
self.cc = cc
122-
self.label = label
123-
def __repr__(self):
124-
return 'j' + self.cc + ' ' + self.label
125-
126-
class Jump(instr):
127-
label: str
128-
129-
__match_args__ = ("label",)
130-
def __init__(self, label):
131-
self.label = label
132-
def __repr__(self):
133-
return 'jmp ' + self.label

0 commit comments

Comments
 (0)