Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Constant expr and bool_type #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions example_const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from numpile import autojit


@autojit
def test_const():
return 114514


result = test_const()
print(result)
38 changes: 33 additions & 5 deletions numpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def __init__(self, val, ix):
self.val = val
self.ix = ix

class Const(object):
_fields = ["val"]

def __init__(self, val):
self.val = val

class Noop(ast.AST):
_fields = []

Expand Down Expand Up @@ -188,6 +194,7 @@ def is_array(ty):
int64 = TCon("Int64")
float32 = TCon("Float")
double64 = TCon("Double")
boolean = TCon("Bool")
void = TCon("Void")
array = lambda t: TApp(TCon("Array"), t)

Expand Down Expand Up @@ -221,6 +228,17 @@ def visit(self, node):
else:
return self.generic_visit(node)

def visit_Const(self, node):
if isinstance(node.val, int):
ty = int64
elif isinstance(node.val, float):
ty = double64
elif isinstance(node.val, bool):
ty = boolean
else:
raise NotImplementedError(node.value)
return ty

def visit_Fun(self, node):
self.argtys = [self.fresh() for v in node.args]
self.retty = TVar("$retty")
Expand Down Expand Up @@ -495,6 +513,9 @@ def visit_AugAssign(self, node):
else:
raise NotImplementedError

def visit_Constant(self, node):
return Const(node.value)

def generic_visit(self, node):
raise NotImplementedError(ast.dump(node))

Expand Down Expand Up @@ -533,7 +554,7 @@ def dump(node):
int_type = ir.IntType(32)
float_type = ir.FloatType()
double_type = ir.DoubleType()
bool_type = ir.IntType(1)
bool_type = ir.IntType(32)
void_type = ir.VoidType()
void_ptr = pointer(ir.IntType(8))

Expand Down Expand Up @@ -563,7 +584,8 @@ def array_type(elt_type):
double64 : double_type,
array_int32 : int32_array,
array_int64 : int64_array,
array_double64 : double_array
array_double64 : double_array,
boolean : bool_type,
}

def to_lltype(ptype):
Expand Down Expand Up @@ -621,18 +643,21 @@ def specialize(self, val):
return val.type

def const(self, val):
if isinstance(val, int):
if isinstance(val, bool):
return ir.Constant(bool_type, int(val))
elif isinstance(val, int):
return ir.Constant(int_type, val)
elif isinstance(val, float):
return ir.Constant(double_type, val)
elif isinstance(val, bool):
return ir.Constant(bool_type, int(val))
elif isinstance(val, str):
raise NotImplementedError
#return Constant.stringz(val)
else:
raise NotImplementedError

def visit_Const(self, node):
return self.const(node.val)

def visit_LitInt(self, node):
ty = self.specialize(node)
if ty is double_type:
Expand Down Expand Up @@ -796,6 +821,9 @@ def visit(self, node):
return getattr(self, name)(node)
else:
return self.generic_visit(node)

def generic_visit(self, node):
raise NotImplementedError(ast.dump(node))

### == Type Mapping ==

Expand Down