def test_array_repr(): int8 = IntType(8) tp = ArrayType(int8, 3) int8_1 = Constant(int8, 1) tp1 = tp.gep(int8_1) print(tp1, type(tp1)) values = [Constant(int8, x) for x in (5, 10, -15)] c = Constant(tp, values) print(c) assert str(c) == "[3 x i8] [i8 5, i8 10, i8 -15]" c = Constant(tp, bytearray(b"\x01\x02\x03")) print(c) assert str(c) == '[3 x i8] c"\\01\\02\\03"'
def test_array_repr(self): tp = ArrayType(int8, 3) values = [Constant(int8, x) for x in (5, 10, -15)] c = Constant(tp, values) self.assertEqual(str(c), "[3 x i8] [i8 5, i8 10, i8 -15]") c = Constant(tp, bytearray(b"\x01\x02\x03")) self.assertEqual(str(c), '[3 x i8] c"\\01\\02\\03"')
def visit_Writeln(self, node: Writeln) -> None: """Converts the contents of the command writeln to LLVM ir code and adds the print call to the operating system. Args: node (Writeln): content passed in the command writeln """ self.printf_counter += 1 output_operation_type = "%s" if isinstance(node.content[0], BinaryOperator): self._create_instruct("BinaryOperator", is_printf=True) writeln_content = self.visit(node.content[0]) if isinstance(writeln_content, VarSymbol): content = self.GLOBAL_MEMORY[writeln_content.name] else: content = writeln_content content_type = type(content.type).__name__ if self.builder.block.is_terminated: self._create_instruct(typ=content_type, is_printf=True) if isinstance(content.type, DoubleType): output_operation_type = "%f" output_format = f"{output_operation_type}\n\0" printf_format = Constant( ArrayType(IntType(8), len(output_format)), bytearray(output_format.encode("utf8")), ) fstr = GlobalVariable(self.module, printf_format.type, name=f"fstr_{self.printf_counter}") fstr.linkage = "internal" fstr.global_constant = True fstr.initializer = printf_format writeln_type = FunctionType(IntType(32), [], var_arg=True) writeln = Function( self.module, writeln_type, name=f"printf_{content_type}_{self.printf_counter}", ) body = self.builder.alloca(content.type) temp_loaded = self.builder.load(body) self.builder.store(temp_loaded, body) void_pointer_type = IntType(8).as_pointer() casted_arg = self.builder.bitcast(fstr, void_pointer_type) self.builder.call(writeln, [casted_arg, body]) self.builder.ret_void()
def visit_String(self, node: String) -> Constant: """Converts the literal string to an array of characters. Args: node (String): a token represeting a string (literal) Returns: Constant: a constant containing a array of characters """ content = node.value return Constant(ArrayType(IntType(8), len(content)), bytearray(content.encode("utf8")))