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()
# entrypoint function
#[nomangle,noreturn,entry]
fun _start() {
main()
_exit(0 as i32)
os::exit(0 as i32)
}

View File

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

View File

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

View File

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

View File

@ -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, ':');
}
}
}

View File

@ -6,11 +6,11 @@
#include <mem/cache.h>
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);

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) {
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(&currentNode->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;

View File

@ -77,6 +77,7 @@
%type <node_ptr> program
%type <node_ptr> storage_expr
%type <node_ptr> returnstmt
%type <node_ptr> 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"); };