optimize generated size method

This commit is contained in:
ValKmjolnir 2024-12-03 22:14:34 +08:00
parent 51be7d6fda
commit 1b6834ce31
2 changed files with 10 additions and 12 deletions

View File

@ -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

View File

@ -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";
}
}