コード例 #1
0
ファイル: test_vars.py プロジェクト: ritabt/petra
 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)),
             ]),
         )
コード例 #2
0
 def test_mismatch_type_deref(self) -> None:
     with self.assertRaises(pt.TypeCheckError):
         pt.Program("module").add_func(
             "foo",
             (),
             pt.Int32_t,
             pt.Block([pt.Return(pt.Deref(pt.Int64(123)))]),
         )
     with self.assertRaises(pt.TypeCheckError):
         pt.Program("module").add_func(
             "foo", (ptr, ), (),
             pt.Block([
                 pt.DefineVar(v32, pt.Deref(pt.Var(ptr))),
             ]))
コード例 #3
0
 "add_i8",
 (),
 pt.Int8_t,
 pt.Block([
     pt.DefineVar(a, pt.Add(pt.Int8(-11), pt.Int8(-4))),
     pt.DefineVar(b, pt.Add(pt.Int8(-11), pt.Int8(0))),
     pt.DefineVar(c, pt.Add(pt.Int8(-11), pt.Int8(7))),
     pt.DefineVar(d, pt.Add(pt.Int8(0), pt.Int8(-5))),
     pt.DefineVar(e, pt.Add(pt.Int8(0), pt.Int8(0))),
     pt.DefineVar(f, pt.Add(pt.Int8(0), pt.Int8(3))),
     pt.DefineVar(g, pt.Add(pt.Int8(7), pt.Int8(-8))),
     pt.DefineVar(h, pt.Add(pt.Int8(7), pt.Int8(0))),
     pt.DefineVar(i, pt.Add(pt.Int8(7), pt.Int8(8))),
     pt.Return(
         pt.Add(
             pt.Var(a),
             pt.Add(
                 pt.Var(b),
                 pt.Add(
                     pt.Var(c),
                     pt.Add(
                         pt.Var(d),
                         pt.Add(
                             pt.Var(e),
                             pt.Add(
                                 pt.Var(f),
                                 pt.Add(
                                     pt.Var(g),
                                     pt.Add(pt.Var(h), pt.Var(i)),
                                 ),
                             ),
コード例 #4
0
import petra as pt

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")
コード例 #5
0
ファイル: test_arrays.py プロジェクト: ritabt/petra
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),
                    ),
                )
コード例 #6
0
ファイル: test_truth.py プロジェクト: ritabt/petra
import petra as pt
import unittest

from ctypes import CFUNCTYPE, c_bool, c_int8

program = pt.Program("module")

x, y = pt.Symbol(pt.Int8_t, "x"), pt.Symbol(pt.Int8_t, "y")
z, w = pt.Symbol(pt.Bool_t, "z"), pt.Symbol(pt.Bool_t, "w")

program.add_func(
    "lt",
    (x, y),
    pt.Bool_t,
    pt.Block([pt.Return(pt.Lt(pt.Var(x), pt.Var(y)))]),
)

program.add_func(
    "lte",
    (x, y),
    pt.Bool_t,
    pt.Block([pt.Return(pt.Lte(pt.Var(x), pt.Var(y)))]),
)

program.add_func(
    "gt",
    (x, y),
    pt.Bool_t,
    pt.Block([pt.Return(pt.Gt(pt.Var(x), pt.Var(y)))]),
)
コード例 #7
0
ファイル: test_vars.py プロジェクト: ritabt/petra
import unittest

from ctypes import CFUNCTYPE, c_int32

program = pt.Program("module")

x = pt.Symbol(pt.Int32_t, "x")
y = pt.Symbol(pt.Int32_t, "y")
z = pt.Symbol(pt.Int8_t, "z")

program.add_func(
    "return_temp",
    (),
    pt.Int32_t,
    pt.Block([pt.DefineVar(x, pt.Int32(2)),
              pt.Return(pt.Var(x))]),
)

program.add_func(
    "return_shuffle_temp",
    (),
    pt.Int32_t,
    pt.Block([
        pt.DefineVar(x, pt.Int32(2)),
        pt.DefineVar(y, pt.Var(x)),
        pt.Return(pt.Var(y)),
    ]),
)

program.add_func(
    "temp_unused",
コード例 #8
0
                               "point1d_x_plus_1_x")

program.add_func(
    "proj_functor",
    (
        runtime_ptr,
        parent_ptr,
        point_ptr,
        domain_ptr,
    ),
    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),
コード例 #9
0
ファイル: test_functions.py プロジェクト: ritabt/petra
program.add_func("call_return_2", (), pt.Int32_t,
                 pt.Block([pt.Return(pt.Call("return_2", []))]))

program.add_func(
    "call_return_2_discard",
    (),
    pt.Int32_t,
    pt.Block([pt.Call("return_2", []),
              pt.Return(pt.Int32(3))]),
)

program.add_func(
    "iden",
    (x, ),
    pt.Int32_t,
    pt.Block([pt.Return(pt.Var(x))]),
)

program.add_func(
    "sum",
    (x, y),
    pt.Int32_t,
    pt.Block([pt.Return(pt.Add(pt.Var(x), pt.Var(y)))]),
)


class FunctionsTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.engine = program.compile()

        return_2 = self.engine.get_function_address("return_2")
コード例 #10
0
program.add_func_decl("free", (Pointer_Int64_t, ), ())
# void* memset ( void* ptr, int value, size_t num );
program.add_func_decl("memset", (Pointer_Int64_t, pt.Int32_t, pt.Int64_t),
                      Pointer_Int64_t)

ptr = pt.Symbol(Pointer_Int64_t, "ptr")
val = pt.Symbol(pt.Int64_t, "val")
v32 = pt.Symbol(pt.Int32_t, "val")

program.add_func(
    "malloc_free",
    (),
    pt.Int32_t,
    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)]),
コード例 #11
0
import petra as pt

program = pt.Program("module")

program.add_func_decl("sqrtf", (pt.Float32_t, ), pt.Float32_t)

x = pt.Symbol(pt.Float32_t, "x")

program.add_func(
    "call_sqrtf",
    (x, ),
    pt.Float32_t,
    pt.Block([pt.Return(pt.Call("sqrtf", [pt.Var(x)]))]),
)

program.save_object("sqrt.py.o")
コード例 #12
0
ファイル: test_conditionals.py プロジェクト: ritabt/petra
            ]),
            pt.Block([pt.Return(pt.Int32(2))]),
        ),
        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()