forked from M-Labs/artiq
1
0
Fork 0

compiler/ir_ast_body,ir_infer_types: support syscalls

This commit is contained in:
Sebastien Bourdeauducq 2014-08-18 14:33:21 +08:00
parent 4b0788d92c
commit 65566ec710
2 changed files with 16 additions and 7 deletions

View File

@ -6,7 +6,8 @@ from llvm import core as lc
from artiq.compiler import ir_values from artiq.compiler import ir_values
class Visitor: class Visitor:
def __init__(self, ns, builder=None): def __init__(self, env, ns, builder=None):
self.env = env
self.ns = ns self.ns = ns
self.builder = builder self.builder = builder
@ -112,7 +113,15 @@ class Visitor:
"round": ir_values.operators.round, "round": ir_values.operators.round,
"round64": ir_values.operators.round64, "round64": ir_values.operators.round64,
} }
return ast_unfuns[node.func.id](self.visit_expression(node.args[0]), self.builder) fn = node.func.id
if fn in ast_unfuns:
return ast_unfuns[fn](self.visit_expression(node.args[0]), self.builder)
elif fn == "syscall":
return self.env.syscall(node.args[0].s,
[self.visit_expression(expr) for expr in node.args[1:]],
self.builder)
else:
raise NameError("Function '{}' is not defined".format(fn))
def visit_statements(self, stmts): def visit_statements(self, stmts):
for node in stmts: for node in stmts:

View File

@ -5,8 +5,8 @@ from copy import deepcopy
from artiq.compiler.ir_ast_body import Visitor from artiq.compiler.ir_ast_body import Visitor
class _TypeScanner(ast.NodeVisitor): class _TypeScanner(ast.NodeVisitor):
def __init__(self, ns): def __init__(self, env, ns):
self.exprv = Visitor(ns) self.exprv = Visitor(env, ns)
def visit_Assign(self, node): def visit_Assign(self, node):
val = self.exprv.visit_expression(node.value) val = self.exprv.visit_expression(node.value)
@ -32,11 +32,11 @@ class _TypeScanner(ast.NodeVisitor):
else: else:
raise NotImplementedError raise NotImplementedError
def infer_types(node): def infer_types(env, node):
ns = dict() ns = dict()
while True: while True:
prev_ns = deepcopy(ns) prev_ns = deepcopy(ns)
ts = _TypeScanner(ns) ts = _TypeScanner(env, ns)
ts.visit(node) ts.visit(node)
if prev_ns and all(v.same_type(prev_ns[k]) for k, v in ns.items()): if prev_ns and all(v.same_type(prev_ns[k]) for k, v in ns.items()):
# no more promotions - completed # no more promotions - completed
@ -53,6 +53,6 @@ a += x # promotes a to int64
foo = True foo = True
bar = None bar = None
""" """
ns = infer_types(ast.parse(testcode)) ns = infer_types(None, ast.parse(testcode))
for k, v in sorted(ns.items(), key=itemgetter(0)): for k, v in sorted(ns.items(), key=itemgetter(0)):
print("{:10}--> {}".format(k, str(v))) print("{:10}--> {}".format(k, str(v)))