Skip to content

Commit e9324df

Browse files
committed
updates
1 parent fd0b623 commit e9324df

File tree

7 files changed

+96
-31
lines changed

7 files changed

+96
-31
lines changed

interp_Cfun.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def interp_stmts(self, ss, env):
3838
if len(ss) == 0:
3939
raise Exception('interp_stmts function ended without return')
4040
match ss[0]:
41-
case Return(value):
42-
return self.interp_exp(value, env)
41+
case TailCall(func, args):
42+
return self.interp_exp(Call(func, args), env)
4343
case _:
4444
return super().interp_stmts(ss, env)
4545

@@ -50,8 +50,7 @@ def interp(self, p):
5050
for d in defs:
5151
match d:
5252
case FunctionDef(name, params, blocks, dl, returns, comment):
53-
env[name] = Function(name, [p.arg for p in params.args],
54-
blocks, env)
53+
env[name] = Function(name, [x for (x,t) in params], blocks, env)
5554
self.blocks = {}
5655
self.apply_fun(env['main'], [], None)
5756
case _:

interp_Lfun.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ def interp(self, p):
5353
for d in defs:
5454
match d:
5555
case FunctionDef(name, params, bod, dl, returns, comment):
56-
env[name] = Function(name, [p.arg for p in params.args],
57-
bod, env)
56+
if isinstance(params, ast.arguments):
57+
ps = [p.arg for p in params.args]
58+
else:
59+
ps = [x for (x,t) in params]
60+
env[name] = Function(name, ps, bod, env)
5861
self.apply_fun(env['main'], [], None)
5962
case _:
6063
raise Exception('interp: unexpected ' + repr(p))

type_check_Cfun.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def type_check_def(self, d, env):
6868
case _:
6969
raise Exception('type_check_def: unexpected ' + repr(d))
7070

71-
def type_check_stmts(self, ss, env):
72-
if len(ss) == 0:
73-
return
74-
match ss[0]:
71+
def type_check_stmt(self, s, env):
72+
match s:
7573
case Return(value):
76-
return self.type_check_exp(value, env)
74+
self.type_check_exp(value, env)
75+
case TailCall(func, args):
76+
self.type_check_exp(Call(func, args), env)
7777
case _:
78-
return super().type_check_stmts(ss, env)
78+
super().type_check_stmt(s, env)
7979

8080
def type_check(self, p):
8181
match p:

type_check_Cif.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def type_check_exp(self, e, env):
7070
case _:
7171
raise Exception('error in type_check_exp, unexpected ' + repr(e))
7272

73+
def type_check_stmts(self, ss, env):
74+
for s in ss:
75+
self.type_check_stmt(s, env)
76+
7377
def type_check_stmt(self, s, env):
7478
match s:
7579
case Assign([lhs], value):
@@ -96,10 +100,6 @@ def type_check_stmt(self, s, env):
96100
case _:
97101
raise Exception('error in type_check_stmt, unexpected' + repr(s))
98102

99-
def type_check_stmts(self, ss, env):
100-
for s in ss:
101-
self.type_check_stmt(s, env)
102-
103103
def type_check(self, p):
104104
match p:
105105
case CProgram(body):

type_check_Lfun.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ast
12
from ast import *
23
from type_check_Lvar import check_type_equal
34
from type_check_Ltup import TypeCheckLtup
@@ -25,6 +26,8 @@ def parse_type_annot(self, annot):
2526
self.parse_type_annot(rt))
2627
case Subscript(Name('tuple'), Tuple(ts)):
2728
return TupleType([self.parse_type_annot(t) for t in ts])
29+
case t if t == int or t == bool:
30+
return annot
2831
case _:
2932
raise Exception('parse_type_annot: unexpected ' + repr(annot))
3033

@@ -54,10 +57,18 @@ def type_check_stmts(self, ss, env):
5457
match ss[0]:
5558
case FunctionDef(name, params, body, dl, returns, comment):
5659
new_env = {x: t for (x,t) in env.items()}
57-
for p in params.args:
58-
new_env[p.arg] = self.parse_type_annot(p.annotation)
60+
if isinstance(params, ast.arguments):
61+
new_params = [(p.arg, self.parse_type_annot(p.annotation)) for p in params.args]
62+
ss[0].args = new_params
63+
new_returns = self.parse_type_annot(returns)
64+
ss[0].returns = new_returns
65+
else:
66+
new_params = params
67+
new_returns = returns
68+
for (x,t) in new_params:
69+
new_env[x] = t
5970
rt = self.type_check_stmts(body, new_env)
60-
check_type_equal(self.parse_type_annot(returns), rt, ss[0])
71+
check_type_equal(new_returns, rt, ss[0])
6172
return self.type_check_stmts(ss[1:], env)
6273
case Return(value):
6374
return self.type_check_exp(value, env)
@@ -71,10 +82,12 @@ def type_check(self, p):
7182
for s in body:
7283
match s:
7384
case FunctionDef(name, params, bod, dl, returns, comment):
74-
params_t = [self. parse_type_annot(p.annotation) \
75-
for p in params.args]
76-
env[name] = FunctionType(params_t,
77-
self.parse_type_annot(returns))
85+
if isinstance(params, ast.arguments):
86+
params_t = [self.parse_type_annot(p.annotation) \
87+
for p in params.args]
88+
else:
89+
params_t = [t for (x,t) in params]
90+
env[name] = FunctionType(params_t, self.parse_type_annot(returns))
7891
self.type_check_stmts(body, env)
7992
case _:
8093
raise Exception('type_check: unexpected ' + repr(p))

utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
indent_amount = 0
1212

13-
sed = 'sed'
13+
sed = 'gsed'
1414

1515
def indent_stmt():
1616
return " " * indent_amount
@@ -436,6 +436,17 @@ def __str__(self):
436436
return self.name + '(%rip)'
437437
def __repr__(self):
438438
return 'FunRef(' + self.name + ')'
439+
440+
class TailCall:
441+
__match_args__ = ("func","args")
442+
def __init__(self, func, args):
443+
self.func = func
444+
self.args = args
445+
def __str__(self):
446+
return str(self.func) + '(' + ','.join([str(e) for e in self.args]) + ')'
447+
def __repr__(self):
448+
return 'TailCall(' + repr(self.func) + ',' + repr(self.args) + ')'
449+
439450

440451
################################################################################
441452
# Miscellaneous Auxiliary Functions

x86_ast.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,31 @@ def __str__(self):
1111
for (l,ss) in self.body.items():
1212
if l == label_name('main'):
1313
result += '\t.globl ' + label_name('main') + '\n'
14+
result += '\t.align 16\n'
1415
result += l + ':\n'
1516
indent()
16-
result += '\n'.join([str(s) for s in ss]) + '\n\n'
17+
result += ''.join([str(s) for s in ss]) + '\n'
1718
dedent()
1819
else:
1920
result += '\t.globl ' + label_name('main') + '\n' + \
2021
label_name('main') + ':\n'
2122
indent()
22-
result += '\n'.join([str(s) for s in self.body])
23+
result += ''.join([str(s) for s in self.body])
2324
dedent()
2425
result += '\n'
2526
return result
2627
def __repr__(self):
2728
return 'X86Program(' + repr(self.body) + ')'
28-
29+
30+
class X86ProgramDefs:
31+
__match_args__ = ("defs",)
32+
def __init__(self, defs):
33+
self.defs = defs
34+
def __str__(self):
35+
return '\n'.join([str(d) for d in self.defs])
36+
def __repr__(self):
37+
return 'X86ProgramDefs(' + repr(self.defs) + ')'
38+
2939
class instr: ...
3040
class arg: ...
3141
class location(arg): ...
@@ -43,7 +53,7 @@ def source(self):
4353
def target(self):
4454
return self.args[-1]
4555
def __str__(self):
46-
return indent_stmt() + self.instr + ' ' + ', '.join(str(a) for a in self.args)
56+
return indent_stmt() + self.instr + ' ' + ', '.join(str(a) for a in self.args) + '\n'
4757
def __repr__(self):
4858
return 'Instr(' + repr(self.instr) + ', ' + repr(self.args) + ')'
4959

@@ -53,9 +63,19 @@ def __init__(self, func, num_args):
5363
self.func = func
5464
self.num_args = num_args
5565
def __str__(self):
56-
return indent_stmt() + 'callq' + ' ' + self.func
66+
return indent_stmt() + 'callq' + ' ' + self.func + '\n'
5767
def __repr__(self):
5868
return 'Callq(' + repr(self.func) + ', ' + repr(self.num_args) + ')'
69+
70+
class IndirectCallq(instr):
71+
__match_args__ = ("func", "num_args")
72+
def __init__(self, func, num_args):
73+
self.func = func
74+
self.num_args = num_args
75+
def __str__(self):
76+
return indent_stmt() + 'callq' + ' *' + str(self.func) + '\n'
77+
def __repr__(self):
78+
return 'IndirectCallq(' + repr(self.func) + ', ' + repr(self.num_args) + ')'
5979

6080
class JumpIf(instr):
6181
cc: str
@@ -66,7 +86,7 @@ def __init__(self, cc, label):
6686
self.cc = cc
6787
self.label = label
6888
def __str__(self):
69-
return indent_stmt() + 'j' + self.cc + ' ' + self.label
89+
return indent_stmt() + 'j' + self.cc + ' ' + self.label + '\n'
7090
def __repr__(self):
7191
return 'JumpIf(' + repr(self.cc) + ', ' + repr(self.label) + ')'
7292

@@ -77,10 +97,29 @@ class Jump(instr):
7797
def __init__(self, label):
7898
self.label = label
7999
def __str__(self):
80-
return indent_stmt() + 'jmp ' + self.label
100+
return indent_stmt() + 'jmp ' + self.label + '\n'
81101
def __repr__(self):
82102
return 'Jump(' + repr(self.label) + ')'
83103

104+
class IndirectJump(instr):
105+
__match_args__ = ("target",)
106+
def __init__(self, target):
107+
self.target = target
108+
def __str__(self):
109+
return indent_stmt() + 'jmp *' + str(self.target) + '\n'
110+
def __repr__(self):
111+
return 'IndirectJump(' + repr(self.target) + ')'
112+
113+
class TailJump(instr):
114+
__match_args__ = ("func","arity")
115+
def __init__(self, func, arity):
116+
self.func = func
117+
self.arity = arity
118+
def __str__(self):
119+
return indent_stmt() + 'tailjmp ' + str(self.func) + '\n'
120+
def __repr__(self):
121+
return 'TailJump(' + repr(self.func) + ',' + repr(self.arity) + ')'
122+
84123
class Variable(location):
85124
__match_args__ = ("id",)
86125
def __init__(self, id):

0 commit comments

Comments
 (0)