From 1b6834ce3102d7d30594d8f0ea9628efe758b95c Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Tue, 3 Dec 2024 22:14:34 +0800 Subject: [PATCH] :sparkles: optimize generated size method --- README.md | 1 + bootstrap/sir/context.cpp | 21 +++++++++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a31422f..b62357d 100644 --- a/README.md +++ b/README.md @@ -86,3 +86,4 @@ Learn more about [Self-host compiler](./src/README.md) - syntax like `var a: [i32; 128] = [1, 2, 3];` - syntax like `var a: [i8*; 128] = ["foo", "bar", tmp];` 5. switch statement should be mapped to `switch` in llvm +6. `default` for match statement diff --git a/bootstrap/sir/context.cpp b/bootstrap/sir/context.cpp index 62c7e30..b5ca93e 100644 --- a/bootstrap/sir/context.cpp +++ b/bootstrap/sir/context.cpp @@ -93,16 +93,13 @@ void sir_context::dump_struct_size_method(std::ostream& out) const { .loc_file = st->get_file() }; const auto st_name = mangle(st_type.full_path_name()); - const auto st_real_name = "%struct." + st_name; + const auto st_real_name = quoted_name("%struct." + st_name); const auto size_func_name = quoted_name(st_name + ".__size__"); - out << "define i64 @" << size_func_name; - out << "() alwaysinline {\n"; + out << "define i64 @" << size_func_name << "() alwaysinline {\n"; out << "label.entry:\n"; - out << " %0 = getelementptr " << quoted_name(st_real_name); - out << ", " << quoted_name(st_real_name) << "* null, i64 1\n"; - out << " %1 = ptrtoint " << quoted_name(st_real_name) << "* "; - out << "%0 to i64\n"; - out << " ret i64 %1\n"; + out << " ret i64 ptrtoint (" << st_real_name; + out << "* getelementptr (" << st_real_name << ", "; + out << st_real_name << "* null, i64 1) to i64)\n"; out << "}\n"; } } @@ -114,17 +111,17 @@ void sir_context::dump_struct_alloc_method(std::ostream& out) const { .loc_file = st->get_file() }; const auto st_name = mangle(st_type.full_path_name()); - const auto st_real_name = "%struct." + st_name; + const auto st_real_name = quoted_name("%struct." + st_name); const auto alloc_func_name = quoted_name(st_name + ".__alloc__"); const auto size_func_name = quoted_name(st_name + ".__size__"); - out << "define " << quoted_name(st_real_name) << "* "; + out << "define " << st_real_name << "* "; out << "@" << alloc_func_name; out << "() alwaysinline {\n"; out << "label.entry:\n"; out << " %0 = call i64 @" << size_func_name << "()\n"; out << " %1 = call i8* @malloc(i64 %0)\n"; - out << " %2 = bitcast i8* %1 to " << quoted_name(st_real_name) << "*\n"; - out << " ret " << quoted_name(st_real_name) << "* %2\n"; + out << " %2 = bitcast i8* %1 to " << st_real_name << "*\n"; + out << " ret " << st_real_name << "* %2\n"; out << "}\n"; } }