def insert_pointer(module: ir.Module, scope_stack: list, builder: ir.IRBuilder, type_list: list, val_name: tuple, val_value=None): val_type = type_map[type_list[-1]] for pointer in val_name[1]: if pointer == '*': val_type = val_type.as_pointer() if builder is None: val = ir.GlobalVariable(module, val_type, module.get_unique_name(val_name[2][1])) if val_value: val.initializer = val_value else: val = builder.alloca(val_type, name=module.get_unique_name(val_name[2][1])) if val_value: opt_store(builder, val_value, val) scope_stack[-1][val_name[2][1]] = { 'type': 'struct_ptr' if isinstance(type_map[type_list[-1]], ir.IdentifiedStructType) else 'val_ptr', 'val_type': type_list[-1], 'value': val } return val
def insert_array(module: ir.Module, scope_stack: list, builder: ir.IRBuilder, type: list, num, val_name: tuple, val_value=None): array_type = ir.ArrayType(type_map[type[-1]], num.constant) if builder is None: val = ir.GlobalVariable(module, array_type, module.get_unique_name(val_name[1])) if val_value: val.initializer = val_value else: val = builder.alloca(array_type, name=module.get_unique_name(val_name[1])) # if val_value: # opt_store(builder, val_value, val) scope_stack[-1][val_name[1]] = {'type': 'array', 'value': val} return val
def insert_string(module: ir.Module, scope_stack: list, builder: ir.IRBuilder, raw_data: str, prt=False): try: global_string = scope_stack[0][raw_data] except KeyError: data = eval('%s' % raw_data) data += '\00' data = data.encode() str_type = ir.ArrayType(char_type, len(data)) const_string = ir.Constant(str_type, bytearray(data)) global_string = ir.GlobalVariable(module, str_type, module.get_unique_name(raw_data)) global_string.initializer = const_string scope_stack[0][raw_data] = global_string if prt: return builder.gep(global_string, [int_type(0), int_type(0)], True) else: return global_string