From 42fbda9f01cdd8ea68ef0ed17dfb1d3fc99aca7a Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 10 Oct 2024 11:31:38 +0200 Subject: [PATCH] feat: introduced mangled modules for function names --- lib/src/libc/bootstrap.gsc | 3 ++- lib/src/libc/os.gsc | 6 ++++++ src/ast/ast.c | 1 + src/ast/ast.h | 1 + src/cfg/opt.c | 1 + src/lex/lexer.l | 9 +++++---- src/set/set.c | 35 ++++++++++++++++++++++++++++------- src/yacc/parser.y | 13 +++++++++---- 8 files changed, 53 insertions(+), 16 deletions(-) diff --git a/lib/src/libc/bootstrap.gsc b/lib/src/libc/bootstrap.gsc index aaeeeaf..799f16b 100644 --- a/lib/src/libc/bootstrap.gsc +++ b/lib/src/libc/bootstrap.gsc @@ -8,7 +8,8 @@ include "os" fun main() # entrypoint function +#[nomangle,noreturn,entry] fun _start() { main() - _exit(0 as i32) + os::exit(0 as i32) } diff --git a/lib/src/libc/os.gsc b/lib/src/libc/os.gsc index bb12636..954a374 100644 --- a/lib/src/libc/os.gsc +++ b/lib/src/libc/os.gsc @@ -2,4 +2,10 @@ include "types" # from unistd.h +#[nomangle,preserve,noreturn] fun _exit(in i32: code) + +#[noreturn] +fun exit(in i32: code) { + os::_exit(code) +} diff --git a/src/ast/ast.c b/src/ast/ast.c index 67f841a..012581f 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -93,6 +93,7 @@ void AST_init() { lookup_table[AST_Dereference] = "deref"; lookup_table[AST_Reference] = "ref"; lookup_table[AST_Return] = "ret"; + lookup_table[AST_ModuleRef] = "modref"; } const char* AST_node_to_string(const struct AST_Node_t* node) { diff --git a/src/ast/ast.h b/src/ast/ast.h index d4b8b8d..adb71a5 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -85,6 +85,7 @@ enum AST_SyntaxElement_t { AST_Include, AST_Char, AST_Return, + AST_ModuleRef, AST_ELEMENT_COUNT }; diff --git a/src/cfg/opt.c b/src/cfg/opt.c index 559cc66..97c837a 100644 --- a/src/cfg/opt.c +++ b/src/cfg/opt.c @@ -180,6 +180,7 @@ char* module_ref_to_str(ModuleRef* ref) { if (n + 1 < ref->module_path->len) { g_string_append_unichar(string, ':'); + g_string_append_unichar(string, ':'); } } } diff --git a/src/lex/lexer.l b/src/lex/lexer.l index 9902ea4..9f28ba3 100644 --- a/src/lex/lexer.l +++ b/src/lex/lexer.l @@ -6,11 +6,11 @@ #include int yyLineNumber = 1; - + int yylex(); extern int yyerror(const char* s); - + #define YY_USER_ACTION beginToken(yytext); #define YY_INPUT(buf,result,max_size) {\ @@ -30,6 +30,7 @@ #.* ; +"::" {return(ModSep);}; ":" {DEBUG("\"%s\" tokenized with \':\'", yytext); return(':');}; "=" {DEBUG("\"%s\" tokenized with \'=\'", yytext); return('=');}; "+" {DEBUG("\"%s\" tokenized with \'+\'", yytext); return('+');}; @@ -57,7 +58,7 @@ "short" {DEBUG("\"%s\" tokenized with \'KeyShort\'", yytext); return(KeyShort);}; "long" {DEBUG("\"%s\" tokenized with \'KeyLong\'", yytext); return(KeyLong);}; "half" {DEBUG("\"%s\" tokenized with \'KeyHalf\'", yytext); return(KeyHalf);}; -"double" {DEBUG("\"%s\" tokenized with \'KeyDouble\'", yytext); return(KeyDouble);}; +"double" {DEBUG("\"%s\" tokenized with \'KeyDouble\'", yytext); return(KeyDouble);}; "signed" {DEBUG("\"%s\" tokenized with \'KeySigned\'", yytext); return(KeySigned);}; "unsigned" {DEBUG("\"%s\" tokenized with \'KeyUnsigned\'", yytext); return(KeyUnsigned);}; "ref" {DEBUG("\"%s\" tokenized with \'KeyRef\'", yytext); return(KeyRef);}; @@ -106,7 +107,7 @@ \"([^\"\n])*\" { yytext = yytext +1; yytext[yyleng - 2] = 0; - + DEBUG("\"%s\" tokenized with \'ValStr\'", yytext); yylval.string = collapse_escape_sequences(yytext); return(ValStr); diff --git a/src/set/set.c b/src/set/set.c index c2714e3..8cf746c 100644 --- a/src/set/set.c +++ b/src/set/set.c @@ -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) { assert(currentNode != NULL); 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 nameNode = AST_get_node(currentNode, 0); + char* function_name = module_ref_to_string(nameNode); + Function* fun = NULL; - if (nameNode->kind == AST_Ident) { - int result = getFunction(nameNode->value, &fun); + if (nameNode->kind == AST_ModuleRef || nameNode->kind == AST_Ident) { + int result = getFunction(function_name, &fun); if (result == SEMANTIC_ERROR) { print_diagnostic(¤tNode->location, Error, - "Unknown function: `%s`", nameNode->value); + "Unknown function: `%s`", function_name); return SEMANTIC_ERROR; } } @@ -2107,10 +2130,8 @@ int createfuncall(FunctionCall* funcall, AST_NODE_PTR currentNode) { // unique const char* boxName = 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); if (result) { @@ -2629,7 +2650,7 @@ int createFunction(Function* function, AST_NODE_PTR currentNode) { // compose function name by appending parent modules 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); function->name = composed_name; diff --git a/src/yacc/parser.y b/src/yacc/parser.y index 46f4477..cf83f51 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -77,6 +77,7 @@ %type program %type storage_expr %type returnstmt +%type moduleref %token KeyInt @@ -128,6 +129,7 @@ %token FunExtsupport %token Invalid %token KeyReturn +%token ModSep /* Operator associativity */ /* Operators at lower line number have lower precedence */ @@ -162,7 +164,11 @@ programbody: moduleimport {$$ = $1;} | decl{$$ = $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);} | 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"); }; -funcall: Ident 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, ident); +funcall: moduleref argumentlist {AST_NODE_PTR funcall = AST_new_node(new_loc(), AST_Call, NULL); + AST_push_node(funcall, $1); AST_push_node(funcall, $2); $$ = funcall; DEBUG("Function call"); };