Skip to content

Commit 0c32d03

Browse files
committed
Improved mangling of names in codegeneratio. Fixed bug with codegeneration for arrays
1 parent bbbb77e commit 0c32d03

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

src/codegen/codegen.cpp

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../codegen.hpp"
99
#include "codegen.hpp"
1010
#include "../utilities.hpp"
11+
#include "../semantic/intrinsics.hpp"
1112

1213
// Print LLVM IR
1314
// -------------
@@ -508,19 +509,88 @@ codegen::Context::Binding codegen::Context::get_binding(std::string identifier)
508509

509510
// Name mangling
510511
std::string codegen::Context::get_mangled_type_name(std::filesystem::path module, std::string identifier) {
511-
return module.string() + "::" + identifier;
512+
std::string name = "";
513+
auto path = module;
514+
if (path == this->ast.module_path) {
515+
name = identifier;
516+
}
517+
else if (std_libs.contains(module)) {
518+
name = identifier;
519+
}
520+
else {
521+
name = std::filesystem::relative(module, this->ast.module_path.parent_path()).string() + "::" + identifier;
522+
}
523+
524+
return name;
512525
}
513526

514527
std::string codegen::Context::get_mangled_function_name(std::filesystem::path module, std::string identifier, std::vector<ast::Type> args, ast::Type return_type, bool is_extern) {
515528
if (is_extern) {
516529
return identifier;
517530
}
518531

519-
std::string name = module.string() + "::" + identifier;
520-
for (size_t i = 0; i < args.size(); i++) {
521-
name += "_" + args[i].to_str();
532+
std::string name = "";
533+
auto path = module;
534+
if (path == this->ast.module_path) {
535+
name = identifier;
536+
}
537+
else if (std_libs.contains(module)) {
538+
name = identifier;
539+
}
540+
else {
541+
name = std::filesystem::relative(module, this->ast.module_path.parent_path()).string() + "::" + identifier;
542+
}
543+
544+
auto binding = this->scopes.functions_and_types_scopes.get_binding(identifier);
545+
assert(binding);
546+
if (binding->index() == ast::Interface) {
547+
auto interface = std::get<ast::InterfaceNode>(*binding);
548+
name += "[";
549+
bool founded = false;
550+
for (size_t i = 0; i < interface.args.size(); i++) {
551+
if (interface.args[i]->type == interface.type_parameters[0].type) {
552+
name += args[i].to_str();
553+
founded = true;
554+
break;
555+
}
556+
}
557+
if (!founded) {
558+
name += return_type.to_str();
559+
}
560+
name += "]";
561+
return name;
562+
563+
}
564+
else if (binding->index() == ast::Function
565+
&& std::get<ast::FunctionNode>(*binding).type_parameters.size() > 0) {
566+
auto function = std::get<ast::FunctionNode>(*binding);
567+
name += "[";
568+
for (size_t j = 0; j < function.type_parameters.size(); j++) {
569+
bool founded = false;
570+
for (size_t i = 0; i < function.args.size(); i++) {
571+
if (function.type_parameters[j].type == function.args[i]->type) {
572+
name += args[i].to_str();
573+
founded = true;
574+
break;
575+
}
576+
}
577+
578+
if (!founded) {
579+
if (function.return_type == function.type_parameters[j].type) {
580+
name += return_type.to_str();
581+
}
582+
}
583+
584+
if (j + 1 != function.type_parameters.size()) {
585+
name += ", ";
586+
}
587+
}
588+
name += "]";
589+
return name;
590+
}
591+
else {
592+
return name;
522593
}
523-
return name + "_" + return_type.to_str();
524594
}
525595

526596

@@ -825,7 +895,11 @@ void codegen::Context::store_array_elements(ast::Node* expression, llvm::Value*
825895
}
826896
else if (expression->index() == ast::Identifier) {
827897
auto& identifier = std::get<ast::IdentifierNode>(*expression);
828-
this->builder->CreateMemCpy(array_allocation, llvm::MaybeAlign(), this->get_binding(identifier.value).pointer, llvm::MaybeAlign(), this->get_type_size(array_type));
898+
llvm::Value* array_pointer = this->get_binding(identifier.value).pointer;
899+
if (((llvm::AllocaInst*) array_pointer)->getAllocatedType()->isPointerTy()) {
900+
array_pointer = this->builder->CreateLoad(array_pointer->getType(), array_pointer);
901+
}
902+
this->builder->CreateMemCpy(array_allocation, llvm::MaybeAlign(), array_pointer, llvm::MaybeAlign(), this->get_type_size(array_type));
829903
}
830904
else {
831905
assert(false);
@@ -873,7 +947,7 @@ llvm::Value* codegen::Context::get_index_access_pointer(ast::CallNode& node) {
873947
return this->builder->CreateGEP(array_type, array_ptr, {llvm::ConstantInt::get(*(this->context), llvm::APInt(64, 0, true)), index}, "", true);
874948
}
875949
else {
876-
llvm::Type* wrapper_type = this->as_llvm_type(ast::get_concrete_type(node.args[0]->expression, this->type_bindings));
950+
llvm::Type* wrapper_type = llvm::StructType::getTypeByName(*this->context, "arrayWrapper");
877951
llvm::Value* wrapper_ptr = this->get_binding(std::get<ast::IdentifierNode>(*node.args[0]->expression).value).pointer;
878952
if (((llvm::AllocaInst*) wrapper_ptr)->getAllocatedType()->isPointerTy()) {
879953
wrapper_ptr = this->builder->CreateLoad(
@@ -1026,7 +1100,12 @@ void codegen::Context::codegen(ast::Ast& ast) {
10261100

10271101
// Codegen functions
10281102
for (auto it = ast.modules.begin(); it != ast.modules.end(); it++) {
1103+
this->current_module = it->first;
1104+
this->add_scope(*it->second);
10291105
this->codegen_function_prototypes(it->second->functions);
1106+
this->scopes.variable_scopes = {};
1107+
this->scopes.functions_and_types_scopes.scopes = {};
1108+
this->current_module = ast.module_path;
10301109
}
10311110
for (auto it = ast.modules.begin(); it != ast.modules.end(); it++) {
10321111
this->current_module = it->first;

std/std.dmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function printWithoutLineEnding[t](value: Array[t]): None
114114
_ = printf("[")
115115
i = 1
116116
while i <= size(value)
117-
printWithoutLineEnding(i)
117+
printWithoutLineEnding(value[i])
118118
if not i == size(value)
119119
_ = printf(", ")
120120
i := i + 1

test/arrays/arrays13.dmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ a = [10, 20, 30]
22
print(a)
33

44
--- Output
5-
[1, 2, 3]
5+
[10, 20, 30]
66
---

0 commit comments

Comments
 (0)