Exemple #1
0
    def test_str(self):
        """
        Test the string representation of types.
        """
        self.assertEqual(str(int1), 'i1')
        self.assertEqual(str(ir.IntType(29)), 'i29')
        self.assertEqual(str(flt), 'float')
        self.assertEqual(str(dbl), 'double')
        self.assertEqual(str(ir.VoidType()), 'void')
        self.assertEqual(str(ir.FunctionType(int1, ())), 'i1 ()')
        self.assertEqual(str(ir.FunctionType(int1, (flt,))), 'i1 (float)')
        self.assertEqual(str(ir.FunctionType(int1, (flt, dbl))),
                         'i1 (float, double)')
        self.assertEqual(str(ir.FunctionType(int1, (), var_arg=True)),
                         'i1 (...)')
        self.assertEqual(str(ir.FunctionType(int1, (flt,), var_arg=True)),
                         'i1 (float, ...)')
        self.assertEqual(str(ir.FunctionType(int1, (flt, dbl), var_arg=True)),
                         'i1 (float, double, ...)')
        self.assertEqual(str(ir.PointerType(int32)), 'i32*')
        self.assertEqual(str(ir.PointerType(ir.PointerType(int32))), 'i32**')
        self.assertEqual(str(ir.ArrayType(int1, 5)), '[5 x i1]')
        self.assertEqual(str(ir.ArrayType(ir.PointerType(int1), 5)), '[5 x i1*]')
        self.assertEqual(str(ir.PointerType(ir.ArrayType(int1, 5))), '[5 x i1]*')
        self.assertEqual(str(ir.LiteralStructType((int1,))), '{i1}')
        self.assertEqual(str(ir.LiteralStructType((int1, flt))), '{i1, float}')
        self.assertEqual(str(ir.LiteralStructType((
            ir.PointerType(int1), ir.LiteralStructType((int32, int8))))),
            '{i1*, {i32, i8}}')

        # Avoid polluting the namespace
        context = ir.Context()
        mytype = context.get_identified_type("MyType")
        self.assertEqual(str(mytype), "%MyType")
Exemple #2
0
    def test_gep(self):
        def check_constant(tp, i, expected):
            actual = tp.gep(ir.Constant(int32, i))
            self.assertEqual(actual, expected)

        def check_index_type(tp):
            index = ir.Constant(dbl, 1.0)
            with self.assertRaises(TypeError):
                tp.gep(index)

        tp = ir.PointerType(dbl)
        for i in range(5):
            check_constant(tp, i, dbl)
        check_index_type(tp)

        tp = ir.ArrayType(int1, 3)
        for i in range(3):
            check_constant(tp, i, int1)
        check_index_type(tp)

        tp = ir.LiteralStructType((dbl, ir.LiteralStructType((int1, int8))))
        check_constant(tp, 0, dbl)
        check_constant(tp, 1, ir.LiteralStructType((int1, int8)))
        with self.assertRaises(IndexError):
            tp.gep(ir.Constant(int32, 2))
        check_index_type(tp)

        context = ir.Context()
        tp = ir.IdentifiedStructType(context, "MyType")
        tp.set_body(dbl, ir.LiteralStructType((int1, int8)))
        check_constant(tp, 0, dbl)
        check_constant(tp, 1, ir.LiteralStructType((int1, int8)))
        with self.assertRaises(IndexError):
            tp.gep(ir.Constant(int32, 2))
        check_index_type(tp)
Exemple #3
0
    def __init__(self):
        # TODO: come up with a less naive way of handling the symtab and types
        self.classes = None
        self.symtab = {}
        self.typetab = {}
        self.is_break = False
        self.current_class = None

        self.loop_end_blocks = []
        self.loop_cond_blocks = []
        context = ir.Context()
        self.module = Module(name='opal-lang', context=context)
        self.blocks = []
        self.scope = {}

        self._add_builtins()

        func_ty = ir.FunctionType(ir.VoidType(), [])
        func = Function(self.module, func_ty, 'main')

        self.current_function = func
        entry_block = self.add_block('entry')
        exit_block = self.add_block('exit')

        self.function_stack = [func]
        self.builder = Builder(entry_block)
        self.exit_blocks = [exit_block]
        self.block_stack = [entry_block]
Exemple #4
0
 def _config_llvm(self):
     self.module = ir.Module(name='main_module', context=ir.Context())
     self.module.triple = self.binding.get_default_triple()
     func_type = ir.FunctionType(ir.IntType(64), [], False)
     main_func = ir.Function(self.module, func_type, name='main')
     block = main_func.append_basic_block(name='entry')
     self.builder = ir.IRBuilder(block)
Exemple #5
0
 def test_target_data_non_default_context(self):
     context = ir.Context()
     mytype = context.get_identified_type("MyType")
     mytype.elements = [ir.IntType(32)]
     module = ir.Module(context=context)
     td = llvm.create_target_data("e-m:e-i64:64-f80:128-n8:16:32:64-S128")
     self.assertEqual(mytype.get_abi_size(td, context=context), 4)
Exemple #6
0
 def __init__(self, name: str):
     # create llvm module
     self.__llvm_module = ir.Module(name, context=ir.Context())
     self.__llvm_module.triple = binding.Target.from_default_triple().triple
     self.__llvm_ref = None
     self.__is_normalize = True
     self.__llvm_types = LazyDictionary[Type, ir.Type](constructor=self.declare_type, initializer=self.emit_type)
     self.__llvm_functions = LazyDictionary[Function, ir.Function](constructor=self.declare_function)
Exemple #7
0
 def test_identified_struct(self):
     context = ir.Context()
     mytype = context.get_identified_type("MyType")
     module = ir.Module(context=context)
     self.assertTrue(mytype.is_opaque)
     self.assert_valid_ir(module)
     oldstr = str(module)
     mytype.set_body(ir.IntType(32), ir.IntType(64), ir.FloatType())
     self.assertFalse(mytype.is_opaque)
     self.assert_valid_ir(module)
     self.assertNotEqual(oldstr, str(module))
Exemple #8
0
#/usr/bin/env python3

import llvmlite.ir as ll
import llvmlite.binding as llvm

llvm.initialize()
llvm.initialize_native_target()

context = ll.Context()

module = ll.Module(context=context)
module.triple = llvm.get_default_triple()

my_type2 = context.get_identified_type("MyType2")
a_type = ll.LiteralStructType((ll.FloatType(), ll.IntType(1)), packed=True)

my_type2.set_body(ll.FloatType(),
                  ll.IntType(32))  # setting %"MyType2" = type {float, i32}

# module.add_global(my_type2)

print(module)
Exemple #9
0
 def __init__(self):
     self.llcontext = ll.Context()