diff --git a/include/ravi_api.h b/include/ravi_api.h index 1b46f2d..94c4d82 100644 --- a/include/ravi_api.h +++ b/include/ravi_api.h @@ -28,7 +28,14 @@ #include -struct Ravi_CompilerInterface { +typedef struct Ravi_MemoryAllocatorInterface { + void *arena; + void *(*realloc)(void *arena, void* mem, size_t newsize); + void *(*calloc)(void *arena, size_t n_elements, size_t elem_size); + void (*free)(void *arena, void *p); +} Ravi_MemoryAllocatorInterface; + +typedef struct Ravi_CompilerInterface { /* ------------------------ Inputs ------------------------------ */ void *context; /* Ravi supplied context */ @@ -39,13 +46,15 @@ struct Ravi_CompilerInterface { char main_func_name[31]; /* Name of the generated function that when called will set up the Lua closure */ + Ravi_MemoryAllocatorInterface *memory_allocator; + /* ------------------------- Outputs ------------------------------ */ const char* generated_code; /* Output of the compiler, must be freed by caller. */ /* ------------------------ Debugging and error handling ----------------------------------------- */ void (*debug_message)(void *context, const char *filename, long long line, const char *message); void (*error_message)(void *context, const char *message); -}; +} Ravi_CompilerInterface; /** * This is the API exposed by the Compiler itself. This function is invoked by @@ -53,7 +62,7 @@ struct Ravi_CompilerInterface { * @param compiler_interface The interface expected by the compiler must be setup * @return 0 for success, non-zero for failure */ -RAVICOMP_EXPORT int raviX_compile(struct Ravi_CompilerInterface *compiler_interface); -RAVICOMP_EXPORT void raviX_release(struct Ravi_CompilerInterface *compiler_interface); +RAVICOMP_EXPORT int raviX_compile(Ravi_CompilerInterface *compiler_interface); +RAVICOMP_EXPORT void raviX_release(Ravi_CompilerInterface *compiler_interface); #endif diff --git a/src/chibicc/chibicc.h b/src/chibicc/chibicc.h index ef674c9..178caf2 100644 --- a/src/chibicc/chibicc.h +++ b/src/chibicc/chibicc.h @@ -64,6 +64,15 @@ typedef struct C_Member C_Member; typedef struct C_Relocation C_Relocation; typedef struct C_Hideset C_Hideset; typedef struct C_Parser C_Parser; +typedef struct C_MemoryAllocator C_MemoryAllocator; + +struct C_MemoryAllocator { + void *arena; + void *(*realloc)(void *arena, void* mem, size_t newsize); + void *(*calloc)(void *arena, size_t n_elements, size_t elem_size); + void (*free)(void *arena, void *p); +}; + // // strings.c @@ -75,7 +84,7 @@ typedef struct { int len; } StringArray; -void strarray_push(mspace arena, StringArray *arr, char *s); +void strarray_push(C_MemoryAllocator *allocator, StringArray *arr, char *s); char *format(char *fmt, ...) __attribute__((format(printf, 1, 2))); // @@ -322,7 +331,7 @@ typedef struct { HashEntry *buckets; int capacity; int used; - mspace arena; + C_MemoryAllocator *allocator; } HashMap; // Represents a block scope. @@ -387,7 +396,7 @@ struct C_Parser { HashMap keywords; // used by tokenizer HashMap typewords; // used by parser - mspace arena; // pointer to memory arena handle + C_MemoryAllocator *memory_allocator; jmp_buf env; /* For error handling */ @@ -399,7 +408,7 @@ struct C_Parser { }; -void C_parser_init(C_Parser *parser); +void C_parser_init(C_Parser *parser, C_MemoryAllocator *memory_allocator); C_Scope *C_global_scope(C_Parser *parser); C_Node *C_new_cast(C_Parser *parser, C_Node *expr, C_Type *ty); int64_t C_const_expr(C_Parser *parser, C_Token **rest, C_Token *tok); diff --git a/src/chibicc/chibicc_hashmap.c b/src/chibicc/chibicc_hashmap.c index 84f7914..8d76587 100644 --- a/src/chibicc/chibicc_hashmap.c +++ b/src/chibicc/chibicc_hashmap.c @@ -65,8 +65,8 @@ static void rehash(HashMap *map) { // Create a new hashmap and copy all key-values. HashMap map2 = {0}; - map2.arena = map->arena; - map2.buckets = mspace_calloc(map->arena, cap, sizeof(HashEntry)); + map2.allocator = map->allocator; + map2.buckets = map->allocator->calloc(map->allocator->arena, cap, sizeof(HashEntry)); map2.capacity = cap; for (int i = 0; i < map->capacity; i++) { @@ -76,7 +76,7 @@ static void rehash(HashMap *map) { } assert(map2.used == nkeys); - mspace_free(map->arena, map->buckets); + map->allocator->free(map->allocator->arena, map->buckets); *map = map2; } @@ -105,8 +105,8 @@ static HashEntry *get_entry(HashMap *map, char *key, int keylen) { static HashEntry *get_or_insert_entry(HashMap *map, char *key, int keylen) { if (!map->buckets) { - assert(map->arena != NULL); - map->buckets = mspace_calloc(map->arena, INIT_SIZE, sizeof(HashEntry)); + assert(map->allocator != NULL); + map->buckets = map->allocator->calloc(map->allocator->arena, INIT_SIZE, sizeof(HashEntry)); map->capacity = INIT_SIZE; } else if ((map->used * 100) / map->capacity >= HIGH_WATERMARK) { rehash(map); diff --git a/src/chibicc/chibicc_parse.c b/src/chibicc/chibicc_parse.c index 530b05e..2e2d045 100644 --- a/src/chibicc/chibicc_parse.c +++ b/src/chibicc/chibicc_parse.c @@ -142,24 +142,24 @@ static int align_down(int n, int align) { return C_align_to(n - align + 1, align); } -static char *str_dup(mspace arena, const char *temp, size_t len) { - char *p = (char *) mspace_calloc(arena, 1, len+1); +static char *str_dup(struct C_MemoryAllocator *allocator, const char *temp, size_t len) { + char *p = (char *) allocator->calloc(allocator->arena, 1, len+1); memcpy(p, temp, len); p[len] = 0; return p; } C_Scope *C_global_scope(C_Parser *parser) { - C_Scope *sc = mspace_calloc(parser->arena, 1, sizeof(C_Scope)); - sc->vars.arena = parser->arena; - sc->tags.arena = parser->arena; + C_Scope *sc = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Scope)); + sc->vars.allocator = parser->memory_allocator; + sc->tags.allocator = parser->memory_allocator; return sc; } static void enter_scope(C_Parser *parser) { - C_Scope *sc = mspace_calloc(parser->arena, 1, sizeof(C_Scope)); - sc->vars.arena = parser->arena; - sc->tags.arena = parser->arena; + C_Scope *sc = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Scope)); + sc->vars.allocator = parser->memory_allocator; + sc->tags.allocator = parser->memory_allocator; sc->next = parser->scope; parser->scope = sc; } @@ -188,7 +188,7 @@ static C_Type *find_tag(C_Parser *parser, C_Token *tok) { } static C_Node *new_node(C_Parser *parser, C_NodeKind kind, C_Token *tok) { - C_Node *node = mspace_calloc(parser->arena,1, sizeof(C_Node)); + C_Node *node = parser->memory_allocator->calloc(parser->memory_allocator->arena,1, sizeof(C_Node)); node->kind = kind; node->tok = tok; return node; @@ -242,7 +242,7 @@ static C_Node *new_vla_ptr(C_Parser *parser, C_Obj *var, C_Token *tok) { C_Node *C_new_cast(C_Parser *parser, C_Node *expr, C_Type *ty) { C_add_type(parser, expr); - C_Node *node = mspace_calloc( parser->arena,1, sizeof(C_Node)); + C_Node *node = parser->memory_allocator->calloc(parser->memory_allocator->arena,1, sizeof(C_Node)); node->kind = ND_CAST; node->tok = expr->tok; node->lhs = expr; @@ -251,13 +251,13 @@ C_Node *C_new_cast(C_Parser *parser, C_Node *expr, C_Type *ty) { } static C_VarScope *push_scope(C_Parser *parser, char *name) { - C_VarScope *sc = mspace_calloc(parser->arena,1, sizeof(C_VarScope)); + C_VarScope *sc = parser->memory_allocator->calloc(parser->memory_allocator->arena,1, sizeof(C_VarScope)); hashmap_put(&parser->scope->vars, name, sc); return sc; } static Initializer *new_initializer(C_Parser *parser, C_Type *ty, bool is_flexible) { - Initializer *init = mspace_calloc(parser->arena,1, sizeof(Initializer)); + Initializer *init = parser->memory_allocator->calloc(parser->memory_allocator->arena,1, sizeof(Initializer)); init->ty = ty; if (ty->kind == TY_ARRAY) { @@ -266,7 +266,7 @@ static Initializer *new_initializer(C_Parser *parser, C_Type *ty, bool is_flexib return init; } - init->children = mspace_calloc(parser->arena, ty->array_len, sizeof(Initializer *)); + init->children = parser->memory_allocator->calloc(parser->memory_allocator->arena, ty->array_len, sizeof(Initializer *)); for (int i = 0; i < ty->array_len; i++) init->children[i] = new_initializer(parser, ty->base, false); return init; @@ -278,11 +278,11 @@ static Initializer *new_initializer(C_Parser *parser, C_Type *ty, bool is_flexib for (C_Member *mem = ty->members; mem; mem = mem->next) len++; - init->children = mspace_calloc(parser->arena, len, sizeof(Initializer *)); + init->children = parser->memory_allocator->calloc(parser->memory_allocator->arena, len, sizeof(Initializer *)); for (C_Member *mem = ty->members; mem; mem = mem->next) { if (is_flexible && ty->is_flexible && !mem->next) { - Initializer *child = mspace_calloc(parser->arena, 1, sizeof(Initializer)); + Initializer *child = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(Initializer)); child->ty = mem->ty; child->is_flexible = true; init->children[mem->idx] = child; @@ -297,7 +297,7 @@ static Initializer *new_initializer(C_Parser *parser, C_Type *ty, bool is_flexib } static C_Obj *new_var(C_Parser *parser, char *name, C_Type *ty) { - C_Obj *var = mspace_calloc(parser->arena, 1, sizeof(C_Obj)); + C_Obj *var = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Obj)); var->name = name; var->ty = ty; var->align = ty->align; @@ -331,7 +331,7 @@ static char *new_unique_name(mspace arena) { } static C_Obj *new_anon_gvar(C_Parser *parser, C_Type *ty) { - return new_gvar(parser, new_unique_name(parser->arena), ty); + return new_gvar(parser, new_unique_name(parser->memory_allocator), ty); } static C_Obj *new_string_literal(C_Parser *parser, char *p, C_Type *ty) { @@ -343,7 +343,7 @@ static C_Obj *new_string_literal(C_Parser *parser, char *p, C_Type *ty) { static char *get_ident(C_Parser *parser, C_Token *tok) { if (tok->kind != TK_IDENT) C_error_tok(parser, tok, "expected an identifier"); - return str_dup(parser->arena, tok->loc, tok->len); + return str_dup(parser->memory_allocator, tok->loc, tok->len); } static C_Type *find_typedef(C_Parser *parser, C_Token *tok) { @@ -1283,7 +1283,7 @@ static C_Type *copy_struct_type(C_Parser *parser, C_Type *ty) { C_Member head = {0}; C_Member *cur = &head; for (C_Member *mem = ty->members; mem; mem = mem->next) { - C_Member *m = mspace_calloc(parser->arena, 1, sizeof(C_Member)); + C_Member *m = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Member)); *m = *mem; cur = cur->next = m; } @@ -1471,7 +1471,7 @@ write_gvar_data(C_Parser *parser, C_Relocation *cur, Initializer *init, C_Type * return cur; } - C_Relocation *rel = mspace_calloc(parser->arena, 1, sizeof(C_Relocation)); + C_Relocation *rel = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Relocation)); rel->offset = offset; rel->label = label; rel->addend = val; @@ -1487,7 +1487,7 @@ static void gvar_initializer(C_Parser *parser, C_Token **rest, C_Token *tok, C_O Initializer *init = initializer(parser, rest, tok, var->ty, &var->ty); C_Relocation head = {0}; - char *buf = mspace_calloc(parser->arena, 1, var->ty->size); + char *buf = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, var->ty->size); write_gvar_data(parser, &head, init, var->ty, buf, 0); var->init_data = buf; var->rel = head.next; @@ -1581,7 +1581,7 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { parser->current_switch = node; char *brk = parser->brk_label; - parser->brk_label = node->brk_label = new_unique_name(parser->arena); + parser->brk_label = node->brk_label = new_unique_name(parser->memory_allocator); node->then = stmt(parser, rest, tok); @@ -1608,7 +1608,7 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { } tok = C_skip(parser, tok, ":"); - node->label = new_unique_name(parser->arena); + node->label = new_unique_name(parser->memory_allocator); node->lhs = stmt(parser, rest, tok); node->begin = begin; node->end = end; @@ -1623,7 +1623,7 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { C_Node *node = new_node(parser, ND_CASE, tok); tok = C_skip(parser, tok->next, ":"); - node->label = new_unique_name(parser->arena); + node->label = new_unique_name(parser->memory_allocator); node->lhs = stmt(parser, rest, tok); parser->current_switch->default_case = node; return node; @@ -1637,8 +1637,8 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { char *brk = parser->brk_label; char *cont = parser->cont_label; - parser->brk_label = node->brk_label = new_unique_name(parser->arena); - parser->cont_label = node->cont_label = new_unique_name(parser->arena); + parser->brk_label = node->brk_label = new_unique_name(parser->memory_allocator); + parser->cont_label = node->cont_label = new_unique_name(parser->memory_allocator); if (is_typename(parser, tok)) { C_Type *basety = declspec(parser, &tok, tok, NULL); @@ -1671,8 +1671,8 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { char *brk = parser->brk_label; char *cont = parser->cont_label; - parser->brk_label = node->brk_label = new_unique_name(parser->arena); - parser->cont_label = node->cont_label = new_unique_name(parser->arena); + parser->brk_label = node->brk_label = new_unique_name(parser->memory_allocator); + parser->cont_label = node->cont_label = new_unique_name(parser->memory_allocator); node->then = stmt(parser, rest, tok); @@ -1686,8 +1686,8 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { char *brk = parser->brk_label; char *cont = parser->cont_label; - parser->brk_label = node->brk_label = new_unique_name(parser->arena); - parser->cont_label = node->cont_label = new_unique_name(parser->arena); + parser->brk_label = node->brk_label = new_unique_name(parser->memory_allocator); + parser->cont_label = node->cont_label = new_unique_name(parser->memory_allocator); node->then = stmt(parser, &tok, tok->next); @@ -1742,8 +1742,8 @@ static C_Node *stmt(C_Parser *parser, C_Token **rest, C_Token *tok) { if (tok->kind == TK_IDENT && C_equal(tok->next, ":")) { C_Node *node = new_node(parser, ND_LABEL, tok); - node->label = str_dup(parser->arena, tok->loc, tok->len); - node->unique_label = new_unique_name(parser->arena); + node->label = str_dup(parser->memory_allocator, tok->loc, tok->len); + node->unique_label = new_unique_name(parser->memory_allocator); node->lhs = stmt(parser, rest, tok->next->next); node->goto_next = parser->labels; parser->labels = node; @@ -2092,8 +2092,8 @@ static C_Node *to_assign(C_Parser *parser, C_Node *binary) { tok); C_Node *loop = new_node(parser, ND_DO, tok); - loop->brk_label = new_unique_name(parser->arena); - loop->cont_label = new_unique_name(parser->arena); + loop->brk_label = new_unique_name(parser->memory_allocator); + loop->cont_label = new_unique_name(parser->memory_allocator); C_Node *body = new_binary(parser, ND_ASSIGN, new_var_node(parser, new, tok), @@ -2552,7 +2552,7 @@ static void struct_members(C_Parser *parser, C_Token **rest, C_Token *tok, C_Typ // Anonymous struct member if ((basety->kind == TY_STRUCT || basety->kind == TY_UNION) && C_consume(&tok, tok, ";")) { - C_Member *mem = mspace_calloc(parser->arena, 1, sizeof(C_Member)); + C_Member *mem = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Member)); mem->ty = basety; mem->idx = idx++; mem->align = attr.align ? attr.align : mem->ty->align; @@ -2566,7 +2566,7 @@ static void struct_members(C_Parser *parser, C_Token **rest, C_Token *tok, C_Typ tok = C_skip(parser, tok, ","); first = false; - C_Member *mem = mspace_calloc(parser->arena, 1, sizeof(C_Member)); + C_Member *mem = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Member)); mem->ty = declarator(parser, &tok, tok, basety); mem->name = mem->ty->name; mem->idx = idx++; @@ -3087,7 +3087,7 @@ static C_Node *primary(C_Parser *parser, C_Token **rest, C_Token *tok) { // For "static inline" function if (sc && sc->var && sc->var->is_function) { if (parser->current_fn) - strarray_push(parser->arena, &parser->current_fn->refs, sc->var->name); + strarray_push(parser->memory_allocator, &parser->current_fn->refs, sc->var->name); else sc->var->is_root = true; } @@ -3399,15 +3399,13 @@ C_Obj *C_parse(C_Scope *globalScope, C_Parser *parser, C_Token *tok) { return parser->globals; } -void C_parser_init(C_Parser *parser) { +void C_parser_init(C_Parser *parser, C_MemoryAllocator *allocator) { memset(parser, 0, sizeof *parser); - parser->arena = create_mspace(0, 0); - parser->keywords.arena = parser->arena; - parser->typewords.arena = parser->arena; + parser->memory_allocator = allocator; + parser->keywords.allocator = allocator; + parser->typewords.allocator = allocator; } void C_parser_destroy(C_Parser *parser) { - //mspace_malloc_stats(parser->arena); - destroy_mspace(parser->arena); - parser->arena = NULL; + parser->memory_allocator = NULL; } \ No newline at end of file diff --git a/src/chibicc/chibicc_strings.c b/src/chibicc/chibicc_strings.c index a780538..fc166f1 100644 --- a/src/chibicc/chibicc_strings.c +++ b/src/chibicc/chibicc_strings.c @@ -27,14 +27,14 @@ SOFTWARE. #include "chibicc.h" -void strarray_push(mspace arena, StringArray *arr, char *s) { +void strarray_push(C_MemoryAllocator *allocator, StringArray *arr, char *s) { if (!arr->data) { - arr->data = mspace_calloc(arena, 8, sizeof(char *)); + arr->data = allocator->calloc(allocator->arena, 8, sizeof(char *)); arr->capacity = 8; } if (arr->capacity == arr->len) { - arr->data = mspace_realloc(arena, arr->data, sizeof(char *) * arr->capacity * 2); + arr->data = allocator->realloc(allocator->arena, arr->data, sizeof(char *) * arr->capacity * 2); arr->capacity *= 2; for (int i = arr->len; i < arr->capacity; i++) arr->data[i] = NULL; diff --git a/src/chibicc/chibicc_tokenize.c b/src/chibicc/chibicc_tokenize.c index 2248258..e5a0089 100644 --- a/src/chibicc/chibicc_tokenize.c +++ b/src/chibicc/chibicc_tokenize.c @@ -33,7 +33,7 @@ static int error_sprintf(C_Parser *parser, const char *fmt, ...) { int estimated_size = 128; int n = 0; for (int i = 0; i < 2; i++) { - parser->error_message = mspace_realloc(parser->arena, parser->error_message, pos + estimated_size); // ensure we have at least estimated_size free space + parser->error_message = parser->memory_allocator->realloc(parser->memory_allocator->arena, parser->error_message, pos + estimated_size); // ensure we have at least estimated_size free space va_start(args, fmt); n = vsnprintf(parser->error_message + pos, estimated_size, fmt, args); va_end(args); @@ -55,7 +55,7 @@ static int error_vsprintf(C_Parser *parser, const char *fmt, va_list args) { int pos = parser->error_message ? (int)strlen(parser->error_message) : 0; int n = 0; for (int i = 0; i < 2; i++) { - parser->error_message = mspace_realloc(parser->arena, parser->error_message, pos + estimated_size); // ensure we have at least estimated_size free space + parser->error_message = parser->memory_allocator->realloc(parser->memory_allocator->arena, parser->error_message, pos + estimated_size); // ensure we have at least estimated_size free space n = vsnprintf(parser->error_message + pos, estimated_size, fmt, args); if (n >= estimated_size) { estimated_size = n + 1; // allow for 0 byte @@ -157,7 +157,7 @@ bool C_consume(C_Token **rest, C_Token *tok, char *str) { // Create a new token. static C_Token *new_token(C_Parser *tokenizer, C_TokenKind kind, char *start, char *end) { - C_Token *tok = mspace_calloc(tokenizer->arena, 1, sizeof(C_Token)); + C_Token *tok = tokenizer->memory_allocator->calloc(tokenizer->memory_allocator->arena, 1, sizeof(C_Token)); tok->kind = kind; tok->loc = start; tok->len = end - start; @@ -301,7 +301,7 @@ static char *string_literal_end(C_Parser *tokenizer, char *p) { static C_Token *read_string_literal(C_Parser *tokenizer, char *start, char *quote) { char *end = string_literal_end(tokenizer,quote + 1); - char *buf = mspace_calloc(tokenizer->arena, 1, end - quote); + char *buf = tokenizer->memory_allocator->calloc(tokenizer->memory_allocator->arena, 1, end - quote); int len = 0; for (char *p = quote + 1; p < end;) { @@ -326,7 +326,7 @@ static C_Token *read_string_literal(C_Parser *tokenizer, char *start, char *quot // is called a "surrogate pair". static C_Token *read_utf16_string_literal(C_Parser *tokenizer, char *start, char *quote) { char *end = string_literal_end(tokenizer, quote + 1); - uint16_t *buf = mspace_calloc(tokenizer->arena, 2, end - start); + uint16_t *buf = tokenizer->memory_allocator->calloc(tokenizer->memory_allocator->arena, 2, end - start); int len = 0; for (char *p = quote + 1; p < end;) { @@ -359,7 +359,7 @@ static C_Token *read_utf16_string_literal(C_Parser *tokenizer, char *start, char // encoded in 4 bytes. static C_Token *read_utf32_string_literal(C_Parser *tokenizer, char *start, char *quote, C_Type *ty) { char *end = string_literal_end(tokenizer, quote + 1); - uint32_t *buf = mspace_calloc(tokenizer->arena, 4, end - quote); + uint32_t *buf = tokenizer->memory_allocator->calloc(tokenizer->memory_allocator->arena, 4, end - quote); int len = 0; for (char *p = quote + 1; p < end;) { @@ -739,7 +739,7 @@ C_File **get_input_files(C_Parser *tokenizer) { C_File *C_new_file(C_Parser *tokenizer, char *name, int file_no, char *contents) { - C_File *file = mspace_calloc(tokenizer->arena, 1, sizeof(C_File)); + C_File *file = tokenizer->memory_allocator->calloc(tokenizer->memory_allocator->arena, 1, sizeof(C_File)); file->name = name; file->display_name = name; file->file_no = file_no; @@ -868,7 +868,7 @@ C_Token *C_tokenize_buffer(C_Parser *tokenizer, char *p) { C_File *file = C_new_file(tokenizer, "", file_no + 1, p); // Save the filename for assembler .file directive. - tokenizer->input_files = mspace_realloc(tokenizer->arena, tokenizer->input_files, sizeof(char *) * (file_no + 2)); + tokenizer->input_files = tokenizer->memory_allocator->realloc(tokenizer->memory_allocator->arena, tokenizer->input_files, sizeof(char *) * (file_no + 2)); tokenizer->input_files[file_no] = file; tokenizer->input_files[file_no + 1] = NULL; file_no++; diff --git a/src/chibicc/chibicc_type.c b/src/chibicc/chibicc_type.c index f79616c..e4594fe 100644 --- a/src/chibicc/chibicc_type.c +++ b/src/chibicc/chibicc_type.c @@ -44,7 +44,7 @@ C_Type *C_ty_double = &(C_Type){TY_DOUBLE, 8, 8}; C_Type *C_ty_ldouble = &(C_Type){TY_LDOUBLE, 16, 16}; static C_Type *new_type(C_Parser *parser, TypeKind kind, int size, int align) { - C_Type *ty = mspace_calloc(parser->arena, 1, sizeof(C_Type)); + C_Type *ty = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Type)); ty->kind = kind; ty->size = size; ty->align = align; @@ -114,7 +114,7 @@ bool C_is_compatible(C_Type *t1, C_Type *t2) { } C_Type *C_copy_type(C_Parser *parser, C_Type *ty) { - C_Type *ret = mspace_calloc(parser->arena, 1, sizeof(C_Type)); + C_Type *ret = parser->memory_allocator->calloc(parser->memory_allocator->arena, 1, sizeof(C_Type)); *ret = *ty; ret->origin = ty; return ret; diff --git a/src/codegen.c b/src/codegen.c index 3ed3a00..e22088e 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -2764,9 +2764,16 @@ static int analyze_C_code(Function *fn, TextBuffer *user_code) if (fn->C_local_declarations.buf) /* declarations of temp integer and float vars */ raviX_buffer_add_string(&canned_code, fn->C_local_declarations.buf); + C_MemoryAllocator allocator; + allocator.arena = create_mspace(0, 0); + allocator.realloc = mspace_realloc; + allocator.calloc = mspace_calloc; + allocator.free = mspace_free; + C_Code_Analysis analysis = {0}; C_Parser parser; - C_parser_init(&parser); + + C_parser_init(&parser, &allocator); C_Scope *global_scope = C_global_scope(&parser); C_Token *tok = C_tokenize_buffer(&parser, canned_code.buf); if (tok == NULL) { @@ -2801,6 +2808,7 @@ static int analyze_C_code(Function *fn, TextBuffer *user_code) fn->api->error_message(fn->api->context, parser.error_message); } C_parser_destroy(&parser); + destroy_mspace(allocator.arena); raviX_buffer_free(&canned_code); return analysis.status; @@ -2969,8 +2977,15 @@ static int emit_op_C__new(Function *fn, Instruction *insn) raviX_buffer_add_string(&code, linearizer->C_declarations.buf); int status = -1; + + C_MemoryAllocator allocator; + allocator.arena = create_mspace(0, 0); + allocator.realloc = mspace_realloc; + allocator.calloc = mspace_calloc; + allocator.free = mspace_free; + C_Parser parser; - C_parser_init(&parser); + C_parser_init(&parser, &allocator); C_Scope *global_scope = C_global_scope(&parser); C_Token *tok = C_tokenize_buffer(&parser, code.buf); if (tok == NULL) { @@ -3025,6 +3040,7 @@ static int emit_op_C__new(Function *fn, Instruction *insn) Lexit: C_parser_destroy(&parser); + destroy_mspace(allocator.arena); raviX_buffer_free(&code); return status; @@ -3534,8 +3550,14 @@ static int emit_C__decl(LinearizerState *linearizer, struct Ravi_CompilerInterfa raviX_buffer_add_string(&code, Embedded_C_header); raviX_buffer_add_string(&code, linearizer->C_declarations.buf); + C_MemoryAllocator allocator; + allocator.arena = create_mspace(0, 0); + allocator.realloc = mspace_realloc; + allocator.calloc = mspace_calloc; + allocator.free = mspace_free; + C_Parser parser; - C_parser_init(&parser); + C_parser_init(&parser, &allocator); C_Scope *global_scope = C_global_scope(&parser); C_Decl_Analysis analysis = {&parser, global_scope, 0, 0, api}; @@ -3566,6 +3588,7 @@ static int emit_C__decl(LinearizerState *linearizer, struct Ravi_CompilerInterfa api->error_message(api->context, parser.error_message); } C_parser_destroy(&parser); + destroy_mspace(allocator.arena); raviX_buffer_free(&code); return analysis.status; diff --git a/tests/tchibicc.c b/tests/tchibicc.c index 0284745..5cbf096 100644 --- a/tests/tchibicc.c +++ b/tests/tchibicc.c @@ -17,13 +17,18 @@ int main(int argc, const char *argv[]) " int len;\n" \ "} Str;\n"; strncpy(buffer, code, sizeof buffer); + C_MemoryAllocator allocator; + allocator.arena = create_mspace(0, 0); + allocator.free = mspace_free; + allocator.calloc = mspace_calloc; + allocator.realloc = mspace_realloc; C_Parser parser = {0}; - C_parser_init(&parser); + C_parser_init(&parser, &allocator); C_Token *tok = C_tokenize_buffer(&parser, buffer); C_convert_pp_tokens(&parser, tok); C_Scope scope = {0}; - scope.vars.arena = parser.arena; - scope.tags.arena = parser.arena; + scope.vars.allocator = parser.memory_allocator; + scope.tags.allocator = parser.memory_allocator; C_Obj *obj = C_parse(&scope, &parser, tok); hashmap_foreach(&scope.vars, printout, NULL); C_create_function(&scope, &parser, "dummy");