feat: introduced mangled modules for function names

This commit is contained in:
Sven Vogel 2024-10-10 11:31:38 +02:00
parent d133a8a0f0
commit 42fbda9f01
8 changed files with 53 additions and 16 deletions

View File

@ -8,7 +8,8 @@ include "os"
fun main() fun main()
# entrypoint function # entrypoint function
#[nomangle,noreturn,entry]
fun _start() { fun _start() {
main() main()
_exit(0 as i32) os::exit(0 as i32)
} }

View File

@ -2,4 +2,10 @@
include "types" include "types"
# from unistd.h # from unistd.h
#[nomangle,preserve,noreturn]
fun _exit(in i32: code) fun _exit(in i32: code)
#[noreturn]
fun exit(in i32: code) {
os::_exit(code)
}

View File

@ -93,6 +93,7 @@ void AST_init() {
lookup_table[AST_Dereference] = "deref"; lookup_table[AST_Dereference] = "deref";
lookup_table[AST_Reference] = "ref"; lookup_table[AST_Reference] = "ref";
lookup_table[AST_Return] = "ret"; lookup_table[AST_Return] = "ret";
lookup_table[AST_ModuleRef] = "modref";
} }
const char* AST_node_to_string(const struct AST_Node_t* node) { const char* AST_node_to_string(const struct AST_Node_t* node) {

View File

@ -85,6 +85,7 @@ enum AST_SyntaxElement_t {
AST_Include, AST_Include,
AST_Char, AST_Char,
AST_Return, AST_Return,
AST_ModuleRef,
AST_ELEMENT_COUNT AST_ELEMENT_COUNT
}; };

View File

@ -180,6 +180,7 @@ char* module_ref_to_str(ModuleRef* ref) {
if (n + 1 < ref->module_path->len) { if (n + 1 < ref->module_path->len) {
g_string_append_unichar(string, ':'); g_string_append_unichar(string, ':');
g_string_append_unichar(string, ':');
} }
} }
} }

View File

@ -30,6 +30,7 @@
#.* ; #.* ;
"::" {return(ModSep);};
":" {DEBUG("\"%s\" tokenized with \':\'", yytext); return(':');}; ":" {DEBUG("\"%s\" tokenized with \':\'", yytext); return(':');};
"=" {DEBUG("\"%s\" tokenized with \'=\'", yytext); return('=');}; "=" {DEBUG("\"%s\" tokenized with \'=\'", yytext); return('=');};
"+" {DEBUG("\"%s\" tokenized with \'+\'", yytext); return('+');}; "+" {DEBUG("\"%s\" tokenized with \'+\'", yytext); return('+');};

View File

@ -2083,6 +2083,27 @@ Parameter get_param_from_func(Function* func, size_t index) {
} }
} }
char* module_ref_to_string(AST_NODE_PTR node) {
switch (node->kind) {
case AST_Ident:
return mem_strdup(MemoryNamespaceSet, node->value);
case AST_ModuleRef:
char* submodule = module_ref_to_string(AST_get_last_node(node));
char* curmodule = module_ref_to_string(AST_get_node(node, 0));
char* composed = g_strjoin("::", curmodule, submodule, NULL);
char* cached_composed = mem_strdup(MemoryNamespaceSet, composed);
mem_free(composed);
mem_free(curmodule);
mem_free(submodule);
return cached_composed;
default:
return NULL;
}
}
int createfuncall(FunctionCall* funcall, AST_NODE_PTR currentNode) { int createfuncall(FunctionCall* funcall, AST_NODE_PTR currentNode) {
assert(currentNode != NULL); assert(currentNode != NULL);
assert(currentNode->children->len == 2); assert(currentNode->children->len == 2);
@ -2090,12 +2111,14 @@ int createfuncall(FunctionCall* funcall, AST_NODE_PTR currentNode) {
AST_NODE_PTR argsListNode = AST_get_node(currentNode, 1); AST_NODE_PTR argsListNode = AST_get_node(currentNode, 1);
AST_NODE_PTR nameNode = AST_get_node(currentNode, 0); AST_NODE_PTR nameNode = AST_get_node(currentNode, 0);
char* function_name = module_ref_to_string(nameNode);
Function* fun = NULL; Function* fun = NULL;
if (nameNode->kind == AST_Ident) { if (nameNode->kind == AST_ModuleRef || nameNode->kind == AST_Ident) {
int result = getFunction(nameNode->value, &fun); int result = getFunction(function_name, &fun);
if (result == SEMANTIC_ERROR) { if (result == SEMANTIC_ERROR) {
print_diagnostic(&currentNode->location, Error, print_diagnostic(&currentNode->location, Error,
"Unknown function: `%s`", nameNode->value); "Unknown function: `%s`", function_name);
return SEMANTIC_ERROR; return SEMANTIC_ERROR;
} }
} }
@ -2107,10 +2130,8 @@ int createfuncall(FunctionCall* funcall, AST_NODE_PTR currentNode) {
// unique // unique
const char* boxName = const char* boxName =
AST_get_node(nameNode, (nameNode->children->len - 2))->value; AST_get_node(nameNode, (nameNode->children->len - 2))->value;
const char* funName =
AST_get_node(nameNode, (nameNode->children->len - 1))->value;
const char* name = g_strjoin("", boxName, ".", funName, NULL); const char* name = g_strjoin("", boxName, ".", function_name, NULL);
int result = getFunction(name, &fun); int result = getFunction(name, &fun);
if (result) { if (result) {
@ -2629,7 +2650,7 @@ int createFunction(Function* function, AST_NODE_PTR currentNode) {
// compose function name by appending parent modules // compose function name by appending parent modules
char* modules = module_ref_to_str(currentNode->location.module_ref); char* modules = module_ref_to_str(currentNode->location.module_ref);
char* composed_name = g_strjoin("", modules, ":", function->name, NULL); char* composed_name = g_strjoin("", modules, "::", function->name, NULL);
g_free(modules); g_free(modules);
function->name = composed_name; function->name = composed_name;

View File

@ -77,6 +77,7 @@
%type <node_ptr> program %type <node_ptr> program
%type <node_ptr> storage_expr %type <node_ptr> storage_expr
%type <node_ptr> returnstmt %type <node_ptr> returnstmt
%type <node_ptr> moduleref
%token KeyInt %token KeyInt
@ -128,6 +129,7 @@
%token FunExtsupport %token FunExtsupport
%token Invalid %token Invalid
%token KeyReturn %token KeyReturn
%token ModSep
/* Operator associativity */ /* Operator associativity */
/* Operators at lower line number have lower precedence */ /* Operators at lower line number have lower precedence */
@ -162,7 +164,11 @@ programbody: moduleimport {$$ = $1;}
| decl{$$ = $1;} | decl{$$ = $1;}
| typedef{$$ = $1;}; | typedef{$$ = $1;};
moduleref: Ident {$$ = AST_new_node(new_loc(), AST_Ident, $1);}
| Ident ModSep moduleref {AST_NODE_PTR modref = AST_new_node(new_loc(), AST_ModuleRef, NULL);
AST_push_node(modref, AST_new_node(new_loc(), AST_Ident, $1));
AST_push_node(modref, $3);
$$ = modref;};
expr: ValFloat {$$ = AST_new_node(new_loc(), AST_Float, $1);} expr: ValFloat {$$ = AST_new_node(new_loc(), AST_Float, $1);}
| ValInt {$$ = AST_new_node(new_loc(), AST_Int, $1);} | ValInt {$$ = AST_new_node(new_loc(), AST_Int, $1);}
@ -352,9 +358,8 @@ reinterpretcast: expr KeyTo type %prec KeyTo { AST_NODE_PTR cast = AST_new_node(
DEBUG("Reinterpret-Cast"); }; DEBUG("Reinterpret-Cast"); };
funcall: Ident argumentlist {AST_NODE_PTR funcall = AST_new_node(new_loc(), AST_Call, NULL); funcall: moduleref argumentlist {AST_NODE_PTR funcall = AST_new_node(new_loc(), AST_Call, NULL);
AST_NODE_PTR ident = AST_new_node(new_loc(), AST_Ident, $1); AST_push_node(funcall, $1);
AST_push_node(funcall, ident);
AST_push_node(funcall, $2); AST_push_node(funcall, $2);
$$ = funcall; $$ = funcall;
DEBUG("Function call"); }; DEBUG("Function call"); };