Commit a08947c4 authored by dbarbera's avatar dbarbera
Browse files

Refactor code generation for procedure calls/outputs

parent 41fead9e
...@@ -36,6 +36,7 @@ class Context(): ...@@ -36,6 +36,7 @@ class Context():
self.module = core.Module.new(self.name) self.module = core.Module.new(self.name)
self.target_data = ee.TargetData.new(self.module.data_layout) self.target_data = ee.TargetData.new(self.module.data_layout)
self.dataview = process.dataview self.dataview = process.dataview
self.procedures = process.procedures
self.scope = Scope(self) self.scope = Scope(self)
self.global_scope = self.scope self.global_scope = self.scope
...@@ -548,26 +549,12 @@ def generate_input_signal(signal, inputs, ctx): ...@@ -548,26 +549,12 @@ def generate_input_signal(signal, inputs, ctx):
@generate.register(ogAST.Output) @generate.register(ogAST.Output)
@generate.register(ogAST.ProcedureCall) def _output(output, ctx):
def _call_external_function(output, ctx): ''' Generate the IR for an output '''
''' Generate the IR for an output or procedure call '''
for out in output.output: for out in output.output:
name = out['outputName'].lower() name = out['outputName'].lower()
args = out.get('params', []) args = out.get('params', [])
if name == 'write' or name == 'writeln':
arg_vals = [expression(a, ctx) for a in args]
arg_asn1tys = [a.exprType for a in args]
sdl_write(arg_vals, arg_asn1tys, ctx, name == 'writeln')
continue
elif name == 'reset_timer':
sdl_reset_timer(args[0].value[0], ctx)
continue
elif name == 'set_timer':
timer_expr, timer_id = args
sdl_set_timer(timer_id.value[0], expression(timer_expr, ctx), ctx)
continue
arg_vals = [] arg_vals = []
for arg in args: for arg in args:
arg_val = expression(arg, ctx) arg_val = expression(arg, ctx)
...@@ -582,6 +569,52 @@ def _call_external_function(output, ctx): ...@@ -582,6 +569,52 @@ def _call_external_function(output, ctx):
sdl_call(str(name).lower(), arg_vals, ctx) sdl_call(str(name).lower(), arg_vals, ctx)
@generate.register(ogAST.ProcedureCall)
def _proc_call(proc_call, ctx):
''' Generate the IR for a procedure call '''
output = proc_call.output[0]
name = output['outputName'].lower()
args = output.get('params', [])
if name == 'write' or name == 'writeln':
arg_vals = [expression(a, ctx) for a in args]
arg_asn1tys = [a.exprType for a in args]
sdl_write(arg_vals, arg_asn1tys, ctx, name == 'writeln')
return
elif name == 'reset_timer':
sdl_reset_timer(args[0].value[0], ctx)
return
elif name == 'set_timer':
timer_expr, timer_id = args
sdl_set_timer(timer_id.value[0], expression(timer_expr, ctx), ctx)
return
proc = None
for p in ctx.procedures:
if p.inputString.lower() == name:
proc = p
break
else:
raise CompileError('Procedure "%s" not found' % name)
arg_vals = []
for arg, param in zip(args, proc.fpar):
if param['direction'] == 'out':
arg_vals.append(reference(arg, ctx))
else:
arg_val = expression(arg, ctx)
# Pass by reference
if arg_val.type.kind != core.TYPE_POINTER:
arg_var = ctx.builder.alloca(arg_val.type, None)
ctx.builder.store(arg_val, arg_var)
arg_vals.append(arg_var)
else:
arg_vals.append(arg_val)
sdl_call(str(name).lower(), arg_vals, ctx)
@generate.register(ogAST.TaskAssign) @generate.register(ogAST.TaskAssign)
def _task_assign(task, ctx): def _task_assign(task, ctx):
''' Generate the IR for a list of assignments ''' ''' Generate the IR for a list of assignments '''
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment