From f74c23796966deccf00bf832036b32101dcf09c6 Mon Sep 17 00:00:00 2001 From: Akuli Date: Fri, 10 Jan 2025 20:00:33 +0200 Subject: [PATCH 1/3] print_llvm_ir --- compiler/main.jou | 15 +++++++++++++++ compiler/print.jou | 16 ---------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/compiler/main.jou b/compiler/main.jou index 3e7a1393..6e1eca77 100644 --- a/compiler/main.jou +++ b/compiler/main.jou @@ -180,6 +180,21 @@ def statement_conflicts_with_an_import(stmt: AstStatement*, importsym: ExportSym assert False +def print_llvm_ir(module: LLVMModule*, is_optimized: bool) -> None: + if is_optimized: + opt_or_unopt = "Optimized" + else: + opt_or_unopt = "Unoptimized" + + len = 0L + filename = LLVMGetSourceFileName(module, &len) + printf("===== %s LLVM IR for file \"%.*s\" =====\n", opt_or_unopt, len as int, filename) + + s = LLVMPrintModuleToString(module) + puts(s) + LLVMDisposeMessage(s) + + class FileState: path: byte* # owned ast: AstFile diff --git a/compiler/print.jou b/compiler/print.jou index 097d2daf..689beea0 100644 --- a/compiler/print.jou +++ b/compiler/print.jou @@ -1,6 +1,5 @@ import "stdlib/io.jou" -import "./llvm.jou" import "./structs.jou" @@ -46,18 +45,3 @@ def print_constant(c: Constant*) -> None: print_string(c->str, -1) else: assert False - - -def print_llvm_ir(module: LLVMModule*, is_optimized: bool) -> None: - if is_optimized: - opt_or_unopt = "Optimized" - else: - opt_or_unopt = "Unoptimized" - - len = 0L - filename = LLVMGetSourceFileName(module, &len) - printf("===== %s LLVM IR for file \"%.*s\" =====\n", opt_or_unopt, len as int, filename) - - s = LLVMPrintModuleToString(module) - puts(s) - LLVMDisposeMessage(s) From 5c1d238f2c98bfd694cecfc01d87a6d74bda1a61 Mon Sep 17 00:00:00 2001 From: Akuli Date: Fri, 10 Jan 2025 20:10:07 +0200 Subject: [PATCH 2/3] print_constant --- compiler/ast.jou | 17 +++++----------- compiler/cf_graph.jou | 4 ++-- compiler/main.jou | 1 - compiler/print.jou | 47 ------------------------------------------- compiler/structs.jou | 34 +++++++++++++++++++++++++++++++ compiler/utils.jou | 14 +++++++++++++ 6 files changed, 55 insertions(+), 62 deletions(-) delete mode 100644 compiler/print.jou create mode 100644 compiler/utils.jou diff --git a/compiler/ast.jou b/compiler/ast.jou index ba27104b..84510750 100644 --- a/compiler/ast.jou +++ b/compiler/ast.jou @@ -8,10 +8,9 @@ import "stdlib/io.jou" import "stdlib/str.jou" import "stdlib/mem.jou" -import "./errors_and_warnings.jou" -# TODO: move to stdlib -declare isprint(b: int) -> int +import "./errors_and_warnings.jou" +import "./utils.jou" enum AstTypeKind: @@ -202,15 +201,9 @@ class AstExpression: def print_with_tree_printer(self, tp: TreePrinter) -> None: printf("[line %d] ", self->location.lineno) if self->kind == AstExpressionKind::String: - printf("string \"") - for s = self->string; *s != 0; s++: - if isprint(*s) != 0: - putchar(*s) - elif *s == '\n': - printf("\\n") - else: - printf("\\x%02x", *s) - printf("\"\n") + printf("string ") + print_string(self->string, strlen(self->string)) + printf("\n") elif self->kind == AstExpressionKind::Short: printf("short %hd\n", self->short_value) elif self->kind == AstExpressionKind::Int: diff --git a/compiler/cf_graph.jou b/compiler/cf_graph.jou index fc30b96f..98545a4e 100644 --- a/compiler/cf_graph.jou +++ b/compiler/cf_graph.jou @@ -5,9 +5,9 @@ import "stdlib/mem.jou" import "stdlib/io.jou" import "./errors_and_warnings.jou" +import "./utils.jou" import "./structs.jou" import "./typecheck/common.jou" -import "./print.jou" import "./free.jou" import "./types.jou" @@ -129,7 +129,7 @@ class CfInstruction: self->operands[0]->print() printf(" from 64-bit integer to pointer") elif self->kind == CfInstructionKind::Constant: - print_constant(&self->constant) + self->constant.print() elif self->kind == CfInstructionKind::SpecialConstant: printf("special constant \"%s\"", self->scname) elif self->kind == CfInstructionKind::StringArray: diff --git a/compiler/main.jou b/compiler/main.jou index 6e1eca77..289d411c 100644 --- a/compiler/main.jou +++ b/compiler/main.jou @@ -9,7 +9,6 @@ import "./build_cf_graph.jou" import "./evaluate.jou" import "./run.jou" import "./codegen.jou" -import "./print.jou" import "./llvm.jou" import "./output.jou" import "./typecheck/common.jou" diff --git a/compiler/print.jou b/compiler/print.jou deleted file mode 100644 index 689beea0..00000000 --- a/compiler/print.jou +++ /dev/null @@ -1,47 +0,0 @@ -import "stdlib/io.jou" - -import "./structs.jou" - - -def print_string(s: byte*, len: int) -> None: - putchar('"') - for i = 0; i < len or (len == -1 and s[i] != '\0'); i++: - if 32 <= s[i] and s[i] <= 126: - # printable ascii character - putchar(s[i]) - elif s[i] == '\n': - printf("\\n") - else: - printf("\\x%02x", s[i]) # TODO: \x is not yet recognized by the tokenizer - putchar('"') - - -def print_constant(c: Constant*) -> None: - if c->kind == ConstantKind::EnumMember: - printf("enum member %d of %s", c->enum_member.memberidx, c->enum_member.enumtype->name) - elif c->kind == ConstantKind::Bool: - if c->boolean: - printf("True") - else: - printf("False") - elif c->kind == ConstantKind::Float: - printf("float %s", c->double_or_float_text) - elif c->kind == ConstantKind::Double: - printf("double %s", c->double_or_float_text) - elif c->kind == ConstantKind::Integer: - if c->integer.is_signed: - signed_or_unsigned = "signed" - else: - signed_or_unsigned = "unsigned" - printf( - "%lld (%d-bit %s)", - c->integer.value, - c->integer.size_in_bits, - signed_or_unsigned, - ) - elif c->kind == ConstantKind::Null: - printf("NULL") - elif c->kind == ConstantKind::String: - print_string(c->str, -1) - else: - assert False diff --git a/compiler/structs.jou b/compiler/structs.jou index 9a36e662..6cc46e61 100644 --- a/compiler/structs.jou +++ b/compiler/structs.jou @@ -1,7 +1,9 @@ # TODO: delete this file, merge into others import "stdlib/str.jou" +import "stdlib/io.jou" +import "./utils.jou" import "./types.jou" @@ -48,6 +50,38 @@ class Constant: boolean: bool enum_member: EnumMemberConstant + def print(self) -> None: + if self->kind == ConstantKind::EnumMember: + printf("enum member %d of %s", self->enum_member.memberidx, self->enum_member.enumtype->name) + elif self->kind == ConstantKind::Bool: + if self->boolean: + printf("True") + else: + printf("False") + elif self->kind == ConstantKind::Float: + printf("float %s", self->double_or_float_text) + elif self->kind == ConstantKind::Double: + printf("double %s", self->double_or_float_text) + elif self->kind == ConstantKind::Integer: + if self->integer.is_signed: + signed_or_unsigned = "signed" + else: + signed_or_unsigned = "unsigned" + printf( + "%lld (%d-bit %s)", + self->integer.value, + self->integer.size_in_bits, + signed_or_unsigned, + ) + elif self->kind == ConstantKind::Null: + printf("NULL") + elif self->kind == ConstantKind::String: + printf("string ") + print_string(self->str, strlen(self->str)) + printf("\n") + else: + assert False + def get_type(self) -> Type*: if self->kind == ConstantKind::EnumMember: return self->enum_member.enumtype diff --git a/compiler/utils.jou b/compiler/utils.jou new file mode 100644 index 00000000..5669812c --- /dev/null +++ b/compiler/utils.jou @@ -0,0 +1,14 @@ +import "stdlib/io.jou" + + +def print_string(s: byte*, len: long) -> None: + putchar('"') + for i = 0; i < len; i++: + if 32 <= s[i] and s[i] <= 126: + # printable ascii character + putchar(s[i]) + elif s[i] == '\n': + printf("\\n") + else: + printf("\\x%02x", s[i]) # TODO: \x is not yet recognized by the tokenizer + putchar('"') From 2a835078068ed7a7e0103c67b3ad4258a8dbb163 Mon Sep 17 00:00:00 2001 From: Akuli Date: Fri, 10 Jan 2025 20:37:09 +0200 Subject: [PATCH 3/3] fixed? --- compiler/structs.jou | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/structs.jou b/compiler/structs.jou index e3e151fd..e01d0f92 100644 --- a/compiler/structs.jou +++ b/compiler/structs.jou @@ -1,6 +1,8 @@ # TODO: delete this file, merge into others import "stdlib/str.jou" +import "stdlib/io.jou" +import "stdlib/mem.jou" import "./utils.jou" import "./types.jou" @@ -81,7 +83,7 @@ class Constant: else: assert False - def free(self) -> None: + def free(self) -> None: if self->kind == ConstantKind::String: free(self->str)