Skip to content

Commit

Permalink
add fun
Browse files Browse the repository at this point in the history
  • Loading branch information
jiamo committed Mar 15, 2022
1 parent 2458305 commit 1d8b192
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 161 deletions.
380 changes: 226 additions & 154 deletions compiler.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions interp_Cfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def interp_exp(self, e, env):
f = self.interp_exp(func, env)
vs = [self.interp_exp(arg, env) for arg in args]
return self.apply_fun(f, vs, e)
case TailCall(func, args):
f = self.interp_exp(func, env)
vs = [self.interp_exp(arg, env) for arg in args]
return self.apply_fun(f, vs, e)
case FunRef(id, arity):
return env[id]
case _:
Expand Down
2 changes: 2 additions & 0 deletions interp_x86/convert_x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def convert_instr(instr):
return Tree('indirect_callq', [convert_arg(func)])
case Jump(label):
return Tree('jmp', [label])
case TailJump(fun, arg):
return Tree('indirect_jmp', [convert_arg(fun)])
case JumpIf(cc, label):
return Tree('j' + cc, [label])
case _:
Expand Down
6 changes: 5 additions & 1 deletion interp_x86/eval_x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def eval_instrs(self, instrs, blocks, output):
if self.logging:
print(self.print_state())

elif target == 'initialize':
elif target == 'initialize' or target == '_initialize':
self.log(f'CALL TO initialize: {self.registers["rdi"]}, {self.registers["rsi"]}')
rootstack_size = self.registers['rdi']
heap_size = self.registers['rsi']
Expand Down Expand Up @@ -422,12 +422,16 @@ def eval_instrs(self, instrs, blocks, output):
elif instr.data == 'indirect_callq':
# trace("&&&& {} {}".format(instr, instr.children[0]))
v = self.eval_arg(instr.children[0])
# trace('{} {}'.format(blocks.keys(), v))

assert isinstance(v, FunPointer)
target = v.fun_name
self.eval_instrs(blocks[target], blocks, output)

elif instr.data == 'indirect_jmp':

v = self.eval_arg(instr.children[0])
# trace("&&&& {} {}".format(instr, instr.children[0], v))
assert isinstance(v, FunPointer)
target = v.fun_name
self.eval_instrs(blocks[target], blocks, output)
Expand Down
12 changes: 10 additions & 2 deletions tests/func/add.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
t = (40, True, (2,))
print( t[0] + t[2][0] if t[1] else 44 )
g = 3 + 4
def add(x:int,y:int,x1:int,y1:int, x2:int,y2:int, x3:int,y3:int)-> int:
if x > 0:
return x + y + x1 + y1 + x2 + y2 + x3 + y3
else:
return add(-40, 82, 0, 0, 0, 0, 0, 0)


print(add(40, 2, 0, 0, 0, 0,0, 0))
x = 4
4 changes: 2 additions & 2 deletions tests/func/add1.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
g = 3 + 4
def add(x:int,y:int,x1:int,y1:int, x2:int,y2:int, x3:int,y3:int)-> int:
if x > 0:
if x < 0:
return x + y + x1 + y1 + x2 + y2 + x3 + y3
else:
return add(40, 2, 0, 0, 0, 0, 0, 0)
return add(-40, 82, 0, 0, 0, 0, 0, 0)


print(add(40, 2, 0, 0, 0, 0,0, 0))
Expand Down
13 changes: 13 additions & 0 deletions type_check_Cfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ def type_check_exp(self, e, env):
case _:
raise Exception('type_check_exp: in call, unexpected ' + \
repr(func_t))
case TailCall(func, args):
func_t = self.type_check_exp(func, env)
args_t = [self.type_check_exp(arg, env) for arg in args]
match func_t:
case FunctionType(params_t, return_t):
for (arg_t, param_t) in zip(args_t, params_t):
self.check_type_equal(param_t, arg_t, e)
return return_t
case Bottom():
return Bottom()
case _:
raise Exception('type_check_exp: in call, unexpected ' + \
repr(func_t))
case _:
return super().type_check_exp(e, env)

Expand Down
6 changes: 4 additions & 2 deletions x86_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, func, num_args):
self.func = func
self.num_args = num_args
def __str__(self):
return indent_stmt() + 'callq' + ' ' + self.func + '\n'
return indent_stmt() + 'callq' + ' ' + str(self.func) + '\n'
def __repr__(self):
return 'Callq(' + repr(self.func) + ', ' + repr(self.num_args) + ')'

Expand Down Expand Up @@ -116,8 +116,10 @@ class TailJump(instr):
def __init__(self, func, arity):
self.func = func
self.arity = arity
# def __str__(self):
# return indent_stmt() + 'tailjmp ' + str(self.func) + '\n'
def __str__(self):
return indent_stmt() + 'tailjmp ' + str(self.func) + '\n'
return indent_stmt() + 'jmp *' + str(self.func) + '\n'
def __repr__(self):
return 'TailJump(' + repr(self.func) + ',' + repr(self.arity) + ')'

Expand Down

0 comments on commit 1d8b192

Please sign in to comment.