Exemplo n.º 1
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)
Exemplo n.º 2
0
def insert_type(module: ir.Module, scope_stack: list, builder: ir.IRBuilder,
                type, name):
    assert name not in type_map
    try:
        type_map[name] = type_map[type]
    except (KeyError, TypeError):
        if type['type'] == 'struct':
            ir.IdentifiedStructType(module.context, name)
            struct = module.context.get_identified_type(name)
            type_map[name] = struct
            struct_list = type['list']
            elems = []
            struct_map[name] = {}
            index = 0
            for item in struct_list:
                item_type = type_map[item[0]]
                for id in item[1]:
                    if id[0] == 'id':
                        elems.append(item_type)
                        struct_map[name][id[1]] = {
                            'type': 'val',
                            'index': int_type(index)
                        }
                    elif id[0] == 'array':
                        array_type = ir.ArrayType(item_type, id[2].constant)
                        elems.append(array_type)
                        struct_map[name][id[1][1]] = {
                            'type': 'array',
                            'index': int_type(index)
                        }
                    index += 1
            struct.set_body(*elems)
    return type_map[name]