Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 104 additions & 24 deletions src/codegen/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../codegen.hpp"
#include "codegen.hpp"
#include "../utilities.hpp"
#include "../semantic/intrinsics.hpp"

// Print LLVM IR
// -------------
Expand Down Expand Up @@ -160,10 +161,10 @@ static void link(std::string executable_name, std::string object_file_name, std:
"lld",
object_file_name,
name,
utilities::get_folder_of_executable() + "/deps/musl/libc.a",
utilities::get_folder_of_executable() + "/deps/musl/crt1.o",
utilities::get_folder_of_executable() + "/deps/musl/crti.o",
utilities::get_folder_of_executable() + "/deps/musl/crtn.o"
utilities::get_folder_of_executable().string() + "/deps/musl/libc.a",
utilities::get_folder_of_executable().string() + "/deps/musl/crt1.o",
utilities::get_folder_of_executable().string() + "/deps/musl/crti.o",
utilities::get_folder_of_executable().string() + "/deps/musl/crtn.o"
};

std::string output = "";
Expand Down Expand Up @@ -353,10 +354,11 @@ static void link(std::string executable_name, std::string object_file_name, std:
// Constructor
codegen::Context::Context(ast::Ast& ast) : ast(ast) {
this->current_module = ast.module_path;
auto filename = ast.module_path.filename().string();

// Create llvm context
this->context = new llvm::LLVMContext();
this->module = new llvm::Module("My cool jit", *(this->context));
this->module = new llvm::Module(filename, *(this->context));
this->builder = new llvm::IRBuilder(*(this->context));

// Add function pass optimizations
Expand Down Expand Up @@ -507,19 +509,88 @@ codegen::Context::Binding codegen::Context::get_binding(std::string identifier)

// Name mangling
std::string codegen::Context::get_mangled_type_name(std::filesystem::path module, std::string identifier) {
return module.string() + "::" + identifier;
std::string name = "";
auto path = module;
if (path == this->ast.module_path) {
name = identifier;
}
else if (std_libs.contains(module)) {
name = identifier;
}
else {
name = std::filesystem::relative(module, this->ast.module_path.parent_path()).string() + "::" + identifier;
}

return name;
}

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) {
if (is_extern) {
return identifier;
}

std::string name = module.string() + "::" + identifier;
for (size_t i = 0; i < args.size(); i++) {
name += "_" + args[i].to_str();
std::string name = "";
auto path = module;
if (path == this->ast.module_path) {
name = identifier;
}
else if (std_libs.contains(module)) {
name = identifier;
}
else {
name = std::filesystem::relative(module, this->ast.module_path.parent_path()).string() + "::" + identifier;
}

auto binding = this->scopes.functions_and_types_scopes.get_binding(identifier);
assert(binding);
if (binding->index() == ast::Interface) {
auto interface = std::get<ast::InterfaceNode>(*binding);
name += "[";
bool founded = false;
for (size_t i = 0; i < interface.args.size(); i++) {
if (interface.args[i]->type == interface.type_parameters[0].type) {
name += args[i].to_str();
founded = true;
break;
}
}
if (!founded) {
name += return_type.to_str();
}
name += "]";
return name;

}
else if (binding->index() == ast::Function
&& std::get<ast::FunctionNode>(*binding).type_parameters.size() > 0) {
auto function = std::get<ast::FunctionNode>(*binding);
name += "[";
for (size_t j = 0; j < function.type_parameters.size(); j++) {
bool founded = false;
for (size_t i = 0; i < function.args.size(); i++) {
if (function.type_parameters[j].type == function.args[i]->type) {
name += args[i].to_str();
founded = true;
break;
}
}

if (!founded) {
if (function.return_type == function.type_parameters[j].type) {
name += return_type.to_str();
}
}

if (j + 1 != function.type_parameters.size()) {
name += ", ";
}
}
name += "]";
return name;
}
else {
return name;
}
return name + "_" + return_type.to_str();
}


Expand Down Expand Up @@ -824,7 +895,11 @@ void codegen::Context::store_array_elements(ast::Node* expression, llvm::Value*
}
else if (expression->index() == ast::Identifier) {
auto& identifier = std::get<ast::IdentifierNode>(*expression);
this->builder->CreateMemCpy(array_allocation, llvm::MaybeAlign(), this->get_binding(identifier.value).pointer, llvm::MaybeAlign(), this->get_type_size(array_type));
llvm::Value* array_pointer = this->get_binding(identifier.value).pointer;
if (((llvm::AllocaInst*) array_pointer)->getAllocatedType()->isPointerTy()) {
array_pointer = this->builder->CreateLoad(array_pointer->getType(), array_pointer);
}
this->builder->CreateMemCpy(array_allocation, llvm::MaybeAlign(), array_pointer, llvm::MaybeAlign(), this->get_type_size(array_type));
}
else {
assert(false);
Expand Down Expand Up @@ -872,7 +947,7 @@ llvm::Value* codegen::Context::get_index_access_pointer(ast::CallNode& node) {
return this->builder->CreateGEP(array_type, array_ptr, {llvm::ConstantInt::get(*(this->context), llvm::APInt(64, 0, true)), index}, "", true);
}
else {
llvm::Type* wrapper_type = this->as_llvm_type(ast::get_concrete_type(node.args[0]->expression, this->type_bindings));
llvm::Type* wrapper_type = llvm::StructType::getTypeByName(*this->context, "arrayWrapper");
llvm::Value* wrapper_ptr = this->get_binding(std::get<ast::IdentifierNode>(*node.args[0]->expression).value).pointer;
if (((llvm::AllocaInst*) wrapper_ptr)->getAllocatedType()->isPointerTy()) {
wrapper_ptr = this->builder->CreateLoad(
Expand Down Expand Up @@ -1025,7 +1100,12 @@ void codegen::Context::codegen(ast::Ast& ast) {

// Codegen functions
for (auto it = ast.modules.begin(); it != ast.modules.end(); it++) {
this->current_module = it->first;
this->add_scope(*it->second);
this->codegen_function_prototypes(it->second->functions);
this->scopes.variable_scopes = {};
this->scopes.functions_and_types_scopes.scopes = {};
this->current_module = ast.module_path;
}
for (auto it = ast.modules.begin(); it != ast.modules.end(); it++) {
this->current_module = it->first;
Expand Down Expand Up @@ -1747,7 +1827,7 @@ llvm::Value* codegen::Context::codegen_print_struct_function(ast::Type arg_type,
);
}
else {
std::string print_function_name = this->get_mangled_function_name(utilities::get_folder_of_executable() + "/std/std" + ".dmd", "printWithoutLineEnding", {struct_type.as_nominal_type().type_definition->fields[i]->type}, ast::Type("None"), false);
std::string print_function_name = this->get_mangled_function_name(utilities::get_folder_of_executable().string() + "/std/std" + ".dmd", "printWithoutLineEnding", {struct_type.as_nominal_type().type_definition->fields[i]->type}, ast::Type("None"), false);
llvm::Function* llvm_function = this->module->getFunction(print_function_name);
assert(llvm_function);

Expand Down Expand Up @@ -1800,74 +1880,74 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l

// Intrinsics
if (node.args.size() == 2) {
if (node.identifier->value == "add") {
if (node.identifier->value == "+") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFAdd(args[0], args[1], "addtmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateAdd(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "subtract") {
if (node.identifier->value == "-") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFSub(args[0], args[1], "subtmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateSub(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "multiply") {
if (node.identifier->value == "*") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFMul(args[0], args[1], "multmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateMul(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "divide") {
if (node.identifier->value == "/") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFDiv(args[0], args[1], "divtmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateSDiv(args[0], args[1], "divtmp");
}
}
if (node.identifier->value == "modulo") {
if (node.identifier->value == "%") {
return this->builder->CreateSRem(args[0], args[1], "remtmp");
}
if (node.identifier->value == "less") {
if (node.identifier->value == "<") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFCmpULT(args[0], args[1], "cmptmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateICmpULT(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "lessEqual") {
if (node.identifier->value == "<=") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFCmpULE(args[0], args[1], "cmptmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateICmpULE(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "greater") {
if (node.identifier->value == ">") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFCmpUGT(args[0], args[1], "cmptmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateICmpUGT(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "greaterEqual") {
if (node.identifier->value == ">=") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFCmpUGE(args[0], args[1], "cmptmp");
}
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
return this->builder->CreateICmpUGE(args[0], args[1], "addtmp");
}
}
if (node.identifier->value == "equal") {
if (node.identifier->value == "==") {
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
return this->builder->CreateFCmpUEQ(args[0], args[1], "eqtmp");
}
Expand All @@ -1883,7 +1963,7 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l
}
}
if (node.args.size() == 1) {
if (node.identifier->value == "negate") {
if (node.identifier->value == "-[negation]") {
if (args[0]->getType()->isDoubleTy()) {
return this->builder->CreateFNeg(args[0], "negation");
}
Expand Down
3 changes: 2 additions & 1 deletion src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ std::string errors::usage() {
" --ast-with-types\n"
" --ast-with-concrete-types\n"
" --llvm-ir\n"
" --obj\n";
" --assembly\n"
" --object-code\n";
}

std::string errors::generic_error(Location location, std::string message) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void check_usage(int argc, char *argv[]) {
if (argv[1] == std::string("run") && argc < 3) {
print_usage_and_exit();
}
if (argv[1] == std::string("emit") && (argc < 4 || !(argv[2] == std::string("--llvm-ir") || argv[2] == std::string("--ast") || argv[2] == std::string("--ast-with-types") || argv[2] == std::string("--ast-with-concrete-types") || argv[2] == std::string("--tokens") || argv[2] == std::string("--obj") || argv[2] == std::string("--asm")))) {
if (argv[1] == std::string("emit") && (argc < 4 || !(argv[2] == std::string("--llvm-ir") || argv[2] == std::string("--ast") || argv[2] == std::string("--ast-with-types") || argv[2] == std::string("--ast-with-concrete-types") || argv[2] == std::string("--tokens") || argv[2] == std::string("--object-code") || argv[2] == std::string("--assembly")))) {
print_usage_and_exit();
}
};
Expand Down Expand Up @@ -189,14 +189,14 @@ void emit(Command command) {
}

// Emit asm
if (command.options[0] == std::string("--asm")) {
if (command.options[0] == std::string("--assembly")) {
codegen::print_assembly(ast, program_name);
ast.free();
return;
}

// Emit object code
if (command.options[0] == std::string("--obj")) {
if (command.options[0] == std::string("--object-code")) {
codegen::generate_object_code(ast, program_name);
ast.free();
return;
Expand Down
Loading