def test_assign_undeclared_variable(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.Assign(pt.Var(x), pt.Int32(500)), pt.Return(pt.Int32(2)), ]), )
program = pt.Program("module") n = pt.Symbol(pt.Int32_t, "n") program.add_func( "collatz", (n, ), pt.Int32_t, pt.Block([ pt.If( pt.Eq(pt.Var(n), pt.Int32(1)), pt.Block([pt.Return(pt.Int32(0))]), pt.Block([]), ), pt.If( pt.Eq(pt.Mod(pt.Var(n), pt.Int32(2)), pt.Int32(0)), pt.Block([pt.Assign(pt.Var(n), pt.Div(pt.Var(n), pt.Int32(2)))]), pt.Block([ pt.Assign( pt.Var(n), pt.Add(pt.Mul(pt.Int32(3), pt.Var(n)), pt.Int32(1)), ), ]), ), pt.Return(pt.Add(pt.Int32(1), pt.Call("collatz", [pt.Var(n)]))), ]), ) program.save_object("collatz.py.o")
from ctypes import CFUNCTYPE, c_int32 program = pt.Program("module") My_Array = pt.ArrayType(pt.Int32_t, 3) array_var = pt.Symbol(My_Array, "array_var") program.add_func( "array_set_get_values", (), pt.Int32_t, pt.Block( [ pt.DefineVar(array_var), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(1), 0) ), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(2), 1) ), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(3), 2) ), pt.Return( pt.Add( pt.GetElement(pt.Var(array_var), 0), pt.Add( pt.GetElement(pt.Var(array_var), 1), pt.GetElement(pt.Var(array_var), 2), ), )
), legion_logical_region_t, pt.Block([ pt.DefineVar( point1d, pt.Call("legion_domain_point_get_point_1d", [pt.Deref(pt.Var(point_ptr))])), pt.DefineVar( point1d_x, pt.GetElement(pt.Var(point1d), name="x"), ), pt.DefineVar(x, pt.GetElement(pt.Var(point1d_x), idx=0)), pt.DefineVar(x_plus_1, pt.Add(pt.Var(x), pt.Int64(1))), pt.DefineVar(point1d_x_plus_1_x), pt.Assign( pt.Var(point1d_x_plus_1_x), pt.SetElement(pt.Var(point1d_x_plus_1_x), pt.Var(x_plus_1), 0), ), pt.DefineVar(point1d_x_plus_1), pt.Assign( pt.Var(point1d_x_plus_1), pt.SetElement(pt.Var(point1d_x_plus_1), pt.Var(point1d_x_plus_1_x), name="x"), ), pt.DefineVar( domain_point_x_plus_1, pt.Call("legion_domain_point_from_point_1d", [pt.Var(point1d_x_plus_1)]), ), pt.DefineVar( result,
pt.Block([ pt.DefineVar(ptr, pt.Call("malloc", [pt.Int64(8)])), pt.Call("free", [pt.Var(ptr)]), pt.Return(pt.Int32(1)), ]), ) program.add_func( "malloc_memset", (), pt.Int64_t, pt.Block([ pt.DefineVar(ptr, pt.Call("malloc", [pt.Int64(8)])), pt.Assign( pt.Var(ptr), pt.Call("memset", [pt.Var(ptr), pt.Int32(0x3A), pt.Int64(8)]), ), pt.DefineVar(val, pt.Deref(pt.Var(ptr))), pt.Call("free", [pt.Var(ptr)]), pt.Return(pt.Var(val)), ]), ) class PointersTestCase(unittest.TestCase): def setUp(self) -> None: self.engine = program.compile() malloc_free = self.engine.get_function_address("malloc_free") self.malloc_free = cast(Callable[[], int],
pt.Return(pt.Int32(0)), ]), ) x = pt.Symbol(pt.Int32_t, "x") program.add_func( "while_then", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int32(0)), pt.While( pt.Lt(pt.Var(x), pt.Int32(10)), pt.Block([ pt.Assign( pt.Var(x), pt.Add(pt.Var(x), pt.Int32(1)), ), ]), ), pt.Return(pt.Var(x)), ]), ) class ConditionalsTestCase(unittest.TestCase): def setUp(self) -> None: self.engine = program.compile() # If statement if_else_true = self.engine.get_function_address("if_else_true")