Working on simplifying configurable allocation scheme

This commit is contained in:
Krzosa Karol
2023-01-01 12:40:58 +01:00
parent 8c0a8bf72b
commit c5539276ae
18 changed files with 169 additions and 347 deletions

View File

@@ -34,7 +34,7 @@ gen_last_line(){
}
CORE_Static String
string_scope_name(Arena *a, Ast_Scope *scope){
string_scope_name(Allocator *a, Ast_Scope *scope){
String string = {};
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
assert_message(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id");
@@ -44,17 +44,17 @@ string_scope_name(Arena *a, Ast_Scope *scope){
CORE_Static void
gen_scope_name(Ast_Scope *scope){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String string = string_scope_name(scratch, scope);
gen("%.*s", (int)string.len, string.str);
}
CORE_Static String
unique_name(Arena *allocator, Ast *ast){
Scratch scratch(allocator);
unique_name_scratch(Allocator *scratch, Ast *ast){
String result = string_scope_name(scratch, ast->parent_scope);
assert(result.len);
result = string_fmt(allocator, "%Q%d", result, ast->pos->line);
result = string_fmt(scratch, "%Q%d", result, ast->pos->line);
return result;
}
@@ -87,7 +87,7 @@ get_ctype_name_for_type(Ast_Type *type){
}
CORE_Static String
string_simple_decl_prefix(Arena *a, Ast_Type *ast){
string_simple_decl_prefix(Allocator *a, Ast_Type *ast){
switch(ast->kind){
case TYPE_POINTER:{
String string = string_simple_decl_prefix(a, ast->base);
@@ -124,7 +124,7 @@ string_simple_decl_prefix(Arena *a, Ast_Type *ast){
}
CORE_Static String
string_simple_decl_postfix(Arena *a, Ast_Type *ast){
string_simple_decl_postfix(Allocator *a, Ast_Type *ast){
switch(ast->kind){
case TYPE_POINTER:
return string_simple_decl_postfix(a, ast->base);
@@ -143,7 +143,7 @@ string_simple_decl_postfix(Arena *a, Ast_Type *ast){
}
CORE_Static String
string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}){
if(ast->kind == TYPE_LAMBDA) {
String prefix = string_simple_decl_prefix(a, ast->func.ret);
String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
@@ -169,19 +169,13 @@ string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){
CORE_Static void
gen_simple_decl(Ast_Type *ast, Intern_String name = {}){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String string = string_simple_decl(scratch, ast, name);
gen("%.*s", (int)string.len, string.str);
}
CORE_Static String
gen_string_simple_decl(Arena *a, Ast_Type *ast, String name){
Scratch scratch;
String string = string_simple_decl(scratch, ast, pctx->intern(name));
String result = string_copy(a, string);
return result;
}
CORE_Static String
get_type_postfix(Ast_Type *type){
switch(type->kind) {
@@ -211,7 +205,9 @@ gen_value(Token *pos, Value a){
switch(type->kind){
CASE_INT: {
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String postfix = get_type_postfix(type);
const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10);
gen("%s%Q", string, postfix);
@@ -535,8 +531,10 @@ gen_ast(Ast *ast){
CASE(RETURN, Return){
if(is_tuple(node->resolved_type)) {
Scratch scratch;
Intern_String var_name = pctx->intern(unique_name(scratch, node));
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node));
gen_simple_decl(node->resolved_type, var_name);
gen(";");
@@ -744,8 +742,10 @@ gen_ast(Ast *ast){
For(node->vars)
gen_ast(it);
Scratch scratch;
Intern_String var_name = pctx->intern(unique_name(scratch, node));
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node));
gen_simple_decl(node->resolved_type, var_name);
gen(" = ");
gen_expr(node->expr);
@@ -845,7 +845,9 @@ typedef struct String{
// Generate slice and tuple types
Iter(&pctx->all_types){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Ast_Type *type = it.item[0];
if(type->kind == TYPE_SLICE){