diff --git a/src/cleaf.c b/src/cleaf.c index 8d7a889..d16c45c 100644 --- a/src/cleaf.c +++ b/src/cleaf.c @@ -8,6 +8,7 @@ #define LOG_LIB_IMPLEMENTATION #include "thirdparty/log.h" +#include "thirdparty/assertion.h" #include "frontend/ast_definition.h" #include "frontend/ast.h" #include "frontend/ast_printer.h" @@ -75,11 +76,7 @@ int main(int argc, char** argv) build_context_t build_ctx = {0}; if (is_build_mode) { build_ctx.registry = calloc(1, sizeof(hashmap_t)); - if (!build_ctx.registry) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - compiler_resources_free(res); - return 1; - } + CLEAF_ASSERT(build_ctx.registry != NULL, "out of memory"); } da_foreach(char*, it, &res->files) { @@ -214,12 +211,7 @@ int main(int argc, char** argv) } res->hir_program = calloc(1, sizeof(IR_function_array)); - if (!res->hir_program) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - build_context_free(&build_ctx); - compiler_resources_free(res); - return 1; - } + CLEAF_ASSERT(res->hir_program != NULL, "out of memory"); rand_t chunk_rng; rand_init(&chunk_rng); @@ -289,7 +281,8 @@ int main(int argc, char** argv) if (log_is_dump()) { log_section_begin("HIR"); for (size_t i = hir_before; i < res->hir_program->count; ++i) { - char* hir_text = IR_generate_string_program(res->hir_program->items[i]); + char* hir_text = + IR_generate_string_program(res->hir_program->items[i]); fprintf(stderr, "%s", hir_text); free(hir_text); } @@ -305,14 +298,13 @@ int main(int argc, char** argv) if (decl->type != DECLARATION_FUNC) continue; char* mangled = - IR_mangle_function_name(unit->module_name, decl->func.name); - if (!mangled) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - had_errors = 1; - continue; - } + IR_mangle_function_name( + unit->module_name, decl->func.name); + + CLEAF_ASSERT(mangled != NULL, "out of memory"); - if (!decl->func.is_internal || strcmp(mangled, "start") == 0) + if (!decl->func.is_internal || + strcmp(mangled, "start") == 0) target->emit_global(&module_sb, mangled); free(mangled); @@ -373,13 +365,7 @@ int main(int argc, char** argv) } char* base = build_object_basename(unit); - if (!base) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - had_errors = 1; - da_free(&module_sb); - semantic_free_program_definition(&analyzer); - continue; - } + CLEAF_ASSERT(base != NULL, "out of memory"); char asm_path[512]; snprintf(asm_path, sizeof(asm_path), "build/%s.asm", base); diff --git a/src/compiler/build/dep_graph.c b/src/compiler/build/dep_graph.c index f62d3de..ff53247 100644 --- a/src/compiler/build/dep_graph.c +++ b/src/compiler/build/dep_graph.c @@ -1,4 +1,5 @@ #include "compiler/build/dep_graph.h" +#include "thirdparty/assertion.h" static void dep_graph_free(dep_graph_t* graph) { @@ -45,10 +46,7 @@ bool build_dep_graph(build_context_t* ctx) dep_graph_t graph = {0}; graph.index = calloc(1, sizeof(hashmap_t)); - if (!graph.index) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return false; - } + CLEAF_ASSERT(graph.index != NULL, "out of memory"); for (size_t i = 0; i < HASH_SIZE; ++i) { hashmap_entry_t* e = ctx->registry->buckets[i]; diff --git a/src/compiler/build/export_table.c b/src/compiler/build/export_table.c index aa262ab..87b2330 100644 --- a/src/compiler/build/export_table.c +++ b/src/compiler/build/export_table.c @@ -1,14 +1,12 @@ #include "export_table.h" #include "frontend/symbols.h" #include "thirdparty/error.h" +#include "thirdparty/assertion.h" bool semantic_build_export_table(module_unit_t* unit) { unit->export_funcs = calloc(1, sizeof(hashmap_t)); - if (!unit->export_funcs) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return false; - } + CLEAF_ASSERT(unit->export_funcs != NULL, "out of memory"); da_foreach(declaration_t*, it, &unit->program) { declaration_t* decl = *it; @@ -16,10 +14,7 @@ bool semantic_build_export_table(module_unit_t* unit) if (decl->func.is_internal) continue; function_symbol_t* fs = calloc(1, sizeof(function_symbol_t)); - if (!fs) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return false; - } + CLEAF_ASSERT(fs != NULL, "out of memory"); fs->return_type.kind = decl->func.return_type.kind; fs->params_count = decl->func.params.count; @@ -29,13 +24,10 @@ bool semantic_build_export_table(module_unit_t* unit) fs->params_type = calloc(fs->params_count, sizeof(variable_symbol_t)); - if (!fs->params_name || !fs->params_type) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free(fs->params_name); - free(fs->params_type); - free(fs); - return false; - } + + CLEAF_ASSERT(fs->params_name != NULL, "out of memory"); + CLEAF_ASSERT(fs->params_type != NULL, "out of memory"); + for (size_t i = 0; i < fs->params_count; ++i) { fs->params_name[i] = decl->func.params.items[i].ident_name; fs->params_type[i].type = decl->func.params.items[i].type; diff --git a/src/compiler/build/import_resolver.c b/src/compiler/build/import_resolver.c index 920743e..98324aa 100644 --- a/src/compiler/build/import_resolver.c +++ b/src/compiler/build/import_resolver.c @@ -1,6 +1,7 @@ #include "import_resolver.h" #include "frontend/symbols.h" #include "thirdparty/error.h" +#include "thirdparty/assertion.h" #include #include @@ -62,10 +63,7 @@ bool semantic_resolve_imports( semantic_analyzer_t* analyzer) { analyzer->imported_functions = calloc(1, sizeof(hashmap_t)); - if (!analyzer->imported_functions) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return false; - } + CLEAF_ASSERT(analyzer->imported_functions != NULL, "out of memory"); da_foreach(declaration_t*, it, &unit->program) { declaration_t* decl = *it; @@ -82,10 +80,7 @@ bool semantic_resolve_imports( const char* qualifier = path->items[path->count - 2]; char* module_name = join_path_segments(path->items, path->count - 1); - if (!module_name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return false; - } + CLEAF_ASSERT(module_name != NULL, "out of memory"); module_unit_array* target_units = (module_unit_array*) hashmap_get(ctx->registry, module_name); @@ -111,11 +106,7 @@ bool semantic_resolve_imports( } imported_symbol_t* isym = calloc(1, sizeof(imported_symbol_t)); - if (!isym) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free(module_name); - return false; - } + CLEAF_ASSERT(isym != NULL, "out of memory"); isym->fs = fs; isym->module_name = module_name; diff --git a/src/frontend/ast.c b/src/frontend/ast.c index 4a5cafb..d64d1ea 100644 --- a/src/frontend/ast.c +++ b/src/frontend/ast.c @@ -2,6 +2,7 @@ #define DA_LIB_IMPLEMENTATION #include "../thirdparty/da.h" +#include "thirdparty/assertion.h" #include "error.h" void free_expression(expression_t* e) @@ -319,10 +320,7 @@ expression_t* ast_parse_expr_int_lit(parser_t* p) { expression_t* e = (expression_t*) malloc(sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); memset(e, 0, sizeof(expression_t)); e->type = EXPRESSION_INT_LIT; @@ -337,10 +335,9 @@ expression_t* ast_parse_expr_int_lit(parser_t* p) expression_t* ast_parse_expr_var(parser_t* p) { expression_t* e = (expression_t*) malloc(sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + + CLEAF_ASSERT(e != NULL, "out of memory"); + memset(e, 0, sizeof(expression_t)); e->type = EXPRESSION_VAR; @@ -376,10 +373,7 @@ expression_t* ast_parse_expr_var(parser_t* p) expression_t* ast_parse_expr_array_composite_literal(parser_t* p) { expression_t* e = calloc(1, sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); e->type = EXPRESSION_COMPOSITE_LITERAL; e->source_pos = peek(p)->source_pos; @@ -426,10 +420,7 @@ expression_t* ast_parse_expr_array_composite_literal(parser_t* p) expression_t* ast_parse_expr_composite_literal(parser_t* p) { expression_t* e = calloc(1, sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); e->type = EXPRESSION_COMPOSITE_LITERAL; e->source_pos = peek(p)->source_pos; @@ -473,10 +464,8 @@ expression_t* ast_parse_expr_composite_literal(parser_t* p) expression_t* ast_parse_expr_assign(parser_t* p) { expression_t* e = (expression_t*) malloc(sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); + memset(e, 0, sizeof(expression_t)); e->type = EXPRESSION_ASSIGN; @@ -484,11 +473,8 @@ expression_t* ast_parse_expr_assign(parser_t* p) // We compute lhs here otherwise we fallback in infinit loop 'id =' expression_t* lhs = (expression_t*) malloc(sizeof(expression_t)); - if (!lhs) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_expression(e); - return NULL; - } + CLEAF_ASSERT(lhs != NULL, "out of memory"); + memset(lhs, 0, sizeof(expression_t)); lhs->type = EXPRESSION_VAR; @@ -505,12 +491,7 @@ expression_t* ast_parse_expr_assign(parser_t* p) } lhs->var.ident.ident_name = strdup(var_tok->string_value); - if (!lhs->var.ident.ident_name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_expression(e); - free_expression(lhs); - return NULL; - } + CLEAF_ASSERT(lhs->var.ident.ident_name != NULL, "out of memory"); e->assign.lhs = lhs; @@ -566,11 +547,8 @@ expression_t* ast_parse_expr_binary(parser_t* p, int min_bp) if (lbp < min_bp) break; expression_t* e = (expression_t*) calloc(1, sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_expression(expr); - return NULL; - } + + CLEAF_ASSERT(e != NULL, "out of memory"); advance(p); @@ -640,10 +618,7 @@ expression_t* ast_parse_expr_binary(parser_t* p, int min_bp) expression_t* ast_parse_expr_index(parser_t* p) { expression_t* e = calloc(1, sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); e->type = EXPRESSION_INDEX; e->source_pos = peek(p)->source_pos; @@ -671,10 +646,7 @@ expression_t* ast_parse_expr_index(parser_t* p) expression_t* ast_parse_expr_call(parser_t* p) { expression_t* e = calloc(1, sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); e->type = EXPRESSION_CALL; e->source_pos = peek(p)->source_pos; @@ -689,11 +661,7 @@ expression_t* ast_parse_expr_call(parser_t* p) } e->call.qualifier = strdup(qualifier_tok->string_value); - if (!e->call.qualifier) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_expression(e); - return NULL; - } + CLEAF_ASSERT(e->call.qualifier != NULL, "out of memory"); // consume '::' advance(p); @@ -708,11 +676,7 @@ expression_t* ast_parse_expr_call(parser_t* p) return NULL; } e->call.callee = strdup(name_tok->string_value); - if (!e->call.callee) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_expression(e); - return NULL; - } + CLEAF_ASSERT(e->call.callee != NULL, "out of memory"); // consume '(' advance(p); @@ -772,10 +736,7 @@ expression_t* ast_parse_expr_call(parser_t* p) expression_t* ast_parse_expr_unary(parser_t* p) { expression_t* e = (expression_t*) malloc(sizeof(expression_t)); - if (!e) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(e != NULL, "out of memory"); memset(e, 0, sizeof(expression_t)); e->type = EXPRESSION_UNARY; @@ -873,10 +834,7 @@ expression_t* parse_primary(parser_t* p) expression_t* ast_parse_expr_char_lit(parser_t* p) { expression_t* expr = calloc(1, sizeof(expression_t)); - if (!expr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(expr != NULL, "out of memory"); expr->type = EXPRESSION_CHAR_LIT; token_t* char_tok = advance(p); @@ -909,18 +867,14 @@ expression_t* parse_expression(parser_t* p) advance(p); expression_t* rhs = parse_expression(p); if (!rhs) { - error_report_at_token(p->error_ctx, peek(p), ERROR_SEVERITY_ERROR, - "expected expression on right side of assignment"); + error_report_at_token( + p->error_ctx, peek(p), ERROR_SEVERITY_ERROR, + "expected expression on right side of assignment"); free_expression(expr); return NULL; } expression_t* assign = calloc(1, sizeof(expression_t)); - if (!assign) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_expression(expr); - free_expression(rhs); - return NULL; - } + CLEAF_ASSERT(assign != NULL, "out of memory"); assign->type = EXPRESSION_ASSIGN; assign->source_pos = expr->source_pos; assign->assign.lhs = expr; @@ -933,11 +887,9 @@ expression_t* parse_expression(parser_t* p) declaration_t* ast_parse_function(parser_t* p) { - declaration_t* decl = (declaration_t*) malloc(sizeof(declaration_t)); - if (!decl) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + declaration_t* decl = + (declaration_t*) malloc(sizeof(declaration_t)); + CLEAF_ASSERT(decl != NULL, "out of memory"); memset(decl, 0, sizeof(declaration_t)); decl->type = DECLARATION_FUNC; @@ -958,11 +910,7 @@ declaration_t* ast_parse_function(parser_t* p) token_t * name_tok = advance(p); if (name_tok->string_value) { decl->func.name = strdup(name_tok->string_value); - if (!decl->func.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(decl->func.name != NULL, "out of memory"); } } else { token_t* tok = peek(p); @@ -1054,11 +1002,7 @@ declaration_t* ast_parse_function(parser_t* p) } param.ident_name = strdup(name_tok->string_value); - if (!param.ident_name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(param.ident_name != NULL, "out of memory"); param.source_pos = name_tok->source_pos; @@ -1115,11 +1059,7 @@ declaration_t* ast_parse_function(parser_t* p) statement_t* s; statement_block_t* sb = (statement_block_t*) malloc(sizeof(statement_block_t)); - if (!sb) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(sb != NULL, "out of memory"); memset(sb, 0, sizeof(statement_block_t)); while ((s = parse_statement(p)) != NULL) da_append(sb, s); @@ -1137,10 +1077,7 @@ declaration_t* ast_parse_function(parser_t* p) declaration_t* ast_parse_var_decl(parser_t* p) { declaration_t* d = (declaration_t*) malloc(sizeof(declaration_t)); - if (!d) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(d != NULL, "out of memory"); memset(d, 0, sizeof(declaration_t)); d->type = DECLARATION_VAR; @@ -1223,11 +1160,8 @@ declaration_t* ast_parse_var_decl(parser_t* p) } d->var_decl.ident.ident_name = strdup(name_tok->string_value); - if (!d->var_decl.ident.ident_name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(d); - return NULL; - } + CLEAF_ASSERT( + d->var_decl.ident.ident_name != NULL, "out of memory"); d->var_decl.ident.source_pos = name_tok->source_pos; @@ -1265,10 +1199,7 @@ declaration_t* ast_parse_var_decl(parser_t* p) declaration_t* ast_parse_untype_var_decl(parser_t* p) { declaration_t* d = (declaration_t*) malloc(sizeof(declaration_t)); - if (!d) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(d != NULL, "out of memory"); memset(d, 0, sizeof(declaration_t)); d->type = DECLARATION_VAR; @@ -1295,11 +1226,8 @@ declaration_t* ast_parse_untype_var_decl(parser_t* p) } d->var_decl.ident.type.name = strdup(name_tok->string_value); - if (!d->var_decl.ident.type.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(d); - return NULL; - } + CLEAF_ASSERT( + d->var_decl.ident.type.name != NULL, "out of memory"); d->var_decl.ident.source_pos = name_tok->source_pos; @@ -1336,10 +1264,8 @@ declaration_t* ast_parse_untype_var_decl(parser_t* p) declaration_t* ast_parse_import_decl(parser_t* p) { declaration_t* decl = calloc(1, sizeof(declaration_t)); - if (!decl) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(decl != NULL, "out of memory"); + decl->type = DECLARATION_IMPORT; decl->source_pos = peek(p)->source_pos; @@ -1365,11 +1291,7 @@ declaration_t* ast_parse_import_decl(parser_t* p) } char* n = strdup(name_tok->string_value); - if (!n) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(n != NULL, "out of memory"); da_append(&decl->import.path, n); if (!check(p, LEXER_token_coloncolon)) @@ -1394,11 +1316,7 @@ declaration_t* ast_parse_import_decl(parser_t* p) } char* alias = strdup(name_tok->string_value); - if (!alias) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(alias != NULL, "out of memory"); decl->import.alias = alias; } @@ -1409,10 +1327,7 @@ declaration_t* ast_parse_import_decl(parser_t* p) declaration_t* ast_parse_module_decl(parser_t* p) { declaration_t* decl = calloc(1, sizeof(declaration_t)); - if (!decl) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(decl != NULL, "out of memory"); decl->type = DECLARATION_MODULE; decl->source_pos = peek(p)->source_pos; @@ -1438,11 +1353,7 @@ declaration_t* ast_parse_module_decl(parser_t* p) } char* m = strdup(name_tok->string_value); - if (!m) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(m != NULL, "out of memory"); da_append(&(decl->module.path), m); if (!check(p, LEXER_token_coloncolon)) @@ -1459,10 +1370,7 @@ declaration_t* ast_parse_struct_decl(parser_t* p) { size_t total_struct_size = 0; declaration_t* decl = calloc(1, sizeof(declaration_t)); - if (!decl) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(decl != NULL, "out of memory"); decl->type = DECLARATION_STRUCT; decl->source_pos = advance(p)->source_pos; @@ -1471,12 +1379,7 @@ declaration_t* ast_parse_struct_decl(parser_t* p) token_t* name_tok = advance(p); if (name_tok->string_value) { decl->struc.name = strdup(name_tok->string_value); - if (!decl->struc.name) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(decl->struc.name != NULL, "out of memory"); } } else { token_t* tok = peek(p); @@ -1570,11 +1473,7 @@ declaration_t* ast_parse_struct_decl(parser_t* p) } member.ident_name= strdup(name_tok->string_value); - if (!member.ident_name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_declaration(decl); - return NULL; - } + CLEAF_ASSERT(member.ident_name != NULL, "out of memory"); member.source_pos = name_tok->source_pos; total_struct_size += type_info->size; @@ -1655,10 +1554,7 @@ declaration_t* parse_declaration(parser_t* p) statement_t* ast_parse_return_stmt(parser_t* p) { statement_t* s = (statement_t*) malloc(sizeof(statement_t)); - if (s == NULL) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(s != NULL, "out of memory"); memset(s, 0, sizeof(statement_t)); s->type = STATEMENT_RETURN; @@ -1680,10 +1576,7 @@ statement_t* ast_parse_return_stmt(parser_t* p) statement_t* ast_parse_decl_stmt(parser_t* p) { statement_t* s = (statement_t*) malloc(sizeof(statement_t)); - if (s == NULL) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(s != NULL, "out of memory"); memset(s, 0, sizeof(statement_t)); s->type = STATEMENT_DECL; @@ -1702,10 +1595,7 @@ statement_t* ast_parse_decl_stmt(parser_t* p) statement_t* ast_parse_expr_stmt(parser_t* p) { statement_t* s = (statement_t*) malloc(sizeof(statement_t)); - if (!s) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(s != NULL, "out of memory"); memset(s, 0, sizeof(statement_t)); s->type = STATEMENT_EXPR; @@ -1729,10 +1619,7 @@ statement_t* ast_parse_expr_stmt(parser_t* p) statement_t* ast_parse_if_stmt(parser_t* p) { statement_t* s = (statement_t*) malloc(sizeof(statement_t)); - if (!s) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(s != NULL, "out of memory"); memset(s, 0, sizeof(statement_t)); s->type = STATEMENT_IF; @@ -1747,8 +1634,9 @@ statement_t* ast_parse_if_stmt(parser_t* p) } s->if_stmt.condition = parse_expression(p); if (!s->if_stmt.condition) { - error_report_at_token(p->error_ctx, peek(p), ERROR_SEVERITY_ERROR, - "expected condition"); + error_report_at_token( + p->error_ctx, peek(p), ERROR_SEVERITY_ERROR, + "expected condition"); free_statement(s); return NULL; } @@ -1763,12 +1651,9 @@ statement_t* ast_parse_if_stmt(parser_t* p) return NULL; } - statement_block_t* then_sb = (statement_block_t*) malloc(sizeof(statement_block_t)); - if (!then_sb) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_statement(s); - return NULL; - } + statement_block_t* then_sb = + (statement_block_t*) malloc(sizeof(statement_block_t)); + CLEAF_ASSERT(then_sb != NULL, "out of memory"); memset(then_sb, 0, sizeof(statement_block_t)); s->if_stmt.then_branch = then_sb; @@ -1788,7 +1673,8 @@ statement_t* ast_parse_if_stmt(parser_t* p) return NULL; } - if (check(p, LEXER_token_id) && strcmp(peek(p)->string_value, "else") == 0) { + if (check(p, LEXER_token_id) && + strcmp(peek(p)->string_value, "else") == 0) { advance(p); if (!expect(p, '{', "expected '{' after else stmt")) { @@ -1796,22 +1682,15 @@ statement_t* ast_parse_if_stmt(parser_t* p) return NULL; } - statement_block_t* else_sb = (statement_block_t*) malloc(sizeof(statement_block_t)); - if (!else_sb) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_statement(s); - return NULL; - } + statement_block_t* else_sb = + (statement_block_t*) malloc(sizeof(statement_block_t)); + CLEAF_ASSERT(else_sb != NULL, "out of memory"); memset(else_sb, 0, sizeof(statement_block_t)); s->if_stmt.else_branch = else_sb; while (!check(p, '}')) { statement_t* stmt = parse_statement(p); - if (!stmt) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_statement(s); - return NULL; - } + CLEAF_ASSERT(stmt != NULL, "out of memory"); da_append(else_sb, stmt); } @@ -1827,10 +1706,7 @@ statement_t* ast_parse_if_stmt(parser_t* p) statement_t* ast_parse_while_stmt(parser_t* p) { statement_t* s = (statement_t*) malloc(sizeof(statement_t)); - if (!s) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(s != NULL, "out of memory"); memset(s, 0, sizeof(statement_t)); s->type = STATEMENT_WHILE; @@ -1865,11 +1741,7 @@ statement_t* ast_parse_while_stmt(parser_t* p) } statement_block_t* block = (statement_block_t*) malloc(sizeof(statement_block_t)); - if (!block) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_statement(s); - return NULL; - } + CLEAF_ASSERT(block != NULL, "out of memory"); memset(block, 0, sizeof(statement_block_t)); s->while_stmt.body = block; @@ -1895,10 +1767,7 @@ statement_t* ast_parse_while_stmt(parser_t* p) statement_t* ast_parse_for_stmt(parser_t* p) { statement_t* s = (statement_t*) malloc(sizeof(statement_t)); - if (!s) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(s != NULL, "out of memory"); memset(s, 0, sizeof(statement_t)); s->type = STATEMENT_FOR; @@ -1972,19 +1841,17 @@ statement_t* ast_parse_for_stmt(parser_t* p) return NULL; } - statement_block_t* body = (statement_block_t*) malloc(sizeof(statement_block_t)); - if (!body) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free_statement(s); - return NULL; - } + statement_block_t* body = + (statement_block_t*) malloc(sizeof(statement_block_t)); + CLEAF_ASSERT(body != NULL, "out of memory"); memset(body, 0, sizeof(statement_block_t)); s->for_stmt.body = body; while(!check(p, '}')) { statement_t* stmt = parse_statement(p); if (!stmt) { - error_report_at_token(p->error_ctx, peek(p), ERROR_SEVERITY_ERROR, + error_report_at_token( + p->error_ctx, peek(p), ERROR_SEVERITY_ERROR, "expected statement"); free_statement(s); return NULL; @@ -2003,10 +1870,7 @@ statement_t* ast_parse_for_stmt(parser_t* p) statement_t* ast_parse_asm_stmt(parser_t* p) { statement_t* stmt = calloc(1, sizeof(statement_t)); - if (!stmt) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(stmt != NULL, "out of memory"); stmt->type = STATEMENT_ASM; stmt->source_pos = peek(p)->source_pos; @@ -2045,10 +1909,7 @@ statement_t* ast_parse_asm_stmt(parser_t* p) if (check(p, LEXER_token_dqstring)) { stmt->asm_stmt.instr[stmt->asm_stmt.instr_count++] = strdup(peek(p)->string_value); - if (!stmt->asm_stmt.instr[stmt->asm_stmt.instr_count - 1]) { - error_report_general( - ERROR_SEVERITY_ERROR, "out of memory"); - } + CLEAF_ASSERT(stmt->asm_stmt.instr[stmt->asm_stmt.instr_count - 1] != NULL, "out of memory"); advance(p); } else { @@ -2072,10 +1933,7 @@ statement_t* ast_parse_asm_stmt(parser_t* p) statement_t* ast_parse_free_stmt(parser_t* p) { statement_t* stmt = calloc(1, sizeof(statement_t)); - if (!stmt) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return NULL; - } + CLEAF_ASSERT(stmt != NULL, "out of memory"); stmt->type = STATEMENT_FREE; // consume 'free' diff --git a/src/frontend/semantic.c b/src/frontend/semantic.c index 0dbd3e7..cbf2910 100644 --- a/src/frontend/semantic.c +++ b/src/frontend/semantic.c @@ -1,4 +1,5 @@ #include "semantic.h" +#include "thirdparty/assertion.h" static void semantic_resolve_type_size( semantic_analyzer_t* analyzer, known_type_t* t) @@ -165,10 +166,7 @@ int analyze_declaration(semantic_analyzer_t* analyzer, size_t total_found = 0; char** founds = calloc(struc_sym->members_count, sizeof(char*)); - if (!founds) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 0; - } + CLEAF_ASSERT(founds != NULL, "out of memory"); for (size_t j = 0; j < e->composite_literal.count; ++j) { expression_t* assign = e->composite_literal.values[j]; @@ -852,10 +850,7 @@ void semantic_check_scope(semantic_analyzer_t* analyzer, scope_t* scope) { scope_t* local_scope = scope_enter(scope); - if (!local_scope) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return; - } + CLEAF_ASSERT(local_scope != NULL, "out of memory"); da_foreach(statement_t*, it, body) { statement_t* stmt = *it; @@ -944,16 +939,11 @@ void semantic_load_program_definition(semantic_analyzer_t* analyzer) { // TODO: return some sort of status code to make this stop the compiler hashmap_t* func_sym = (hashmap_t*) malloc(sizeof(hashmap_t)); - if (!func_sym) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return; - } + CLEAF_ASSERT(func_sym != NULL, "out of memory"); memset(func_sym, 0, sizeof(hashmap_t)); + hashmap_t* struct_sym = calloc(1, sizeof(hashmap_t)); - if (!struct_sym) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return; - } + CLEAF_ASSERT(struct_sym != NULL, "out of memory"); da_foreach(declaration_t*, it, analyzer->ast) { if ((*it)->type == DECLARATION_FUNC) { @@ -973,13 +963,7 @@ void semantic_load_program_definition(semantic_analyzer_t* analyzer) function_symbol_t* value = (function_symbol_t*) malloc(sizeof(function_symbol_t)); - if (!value) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - hashmap_free(func_sym, 1); - hashmap_free(struct_sym, 1); - return; - } + CLEAF_ASSERT(value != NULL, "out of memory"); memset(value, 0, sizeof(function_symbol_t)); value->return_type.kind = (*it)->func.return_type.kind; @@ -991,23 +975,12 @@ void semantic_load_program_definition(semantic_analyzer_t* analyzer) size_t actual_count = 0; value->params_name = calloc((*it)->func.params.count, sizeof(char*)); - if (!value->params_name) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - hashmap_free(func_sym, 1); - hashmap_free(struct_sym, 1); - return ; - } + CLEAF_ASSERT(value->params_name != NULL, "out of memory"); value->params_type = calloc((*it)->func.params.count, sizeof(variable_symbol_t)); - if (!value->params_type) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - hashmap_free(func_sym, 1); - hashmap_free(struct_sym, 1); - return; - } + CLEAF_ASSERT(value->params_type != NULL, "out of memory"); + value->params_count = (*it)->func.params.count; for (size_t i = 0; i < (*it)->func.params.count; ++i) { @@ -1045,13 +1018,7 @@ void semantic_load_program_definition(semantic_analyzer_t* analyzer) } struct_symbol_t* value = calloc(1, sizeof(struct_symbol_t)); - if (!value) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - hashmap_free(func_sym, 1); - hashmap_free(struct_sym, 1); - return; - } + CLEAF_ASSERT(value != NULL, "out of memory"); // we still store the struct for redeclaration error but with dumie values if ((*it)->struc.members.count <= 0) { @@ -1063,23 +1030,11 @@ void semantic_load_program_definition(semantic_analyzer_t* analyzer) value->members_count = (*it)->struc.members.count; value->members_name = calloc(value->members_count, sizeof(char*)); - if (!value->members_name) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - hashmap_free(func_sym, 1); - hashmap_free(struct_sym, 1); - return ; - } + CLEAF_ASSERT(value->members_name != NULL, "out of memory"); value->members_type = calloc(value->members_count, sizeof(variable_symbol_t)); - if (!value->members_type) { - error_report_general(ERROR_SEVERITY_ERROR, - "out of memory"); - hashmap_free(func_sym, 1); - hashmap_free(struct_sym, 1); - return; - } + CLEAF_ASSERT(value->members_type != NULL, "out of memory"); size_t actual_count = 0; for (size_t i = 0; i < (*it)->struc.members.count; ++i) { diff --git a/src/middleend/hir.c b/src/middleend/hir.c index d18773a..7671bd3 100644 --- a/src/middleend/hir.c +++ b/src/middleend/hir.c @@ -1,4 +1,5 @@ #include "hir.h" +#include "thirdparty/assertion.h" static size_t min(size_t a, size_t b) { return a < b ? a : b; @@ -102,27 +103,18 @@ int IR_lower_declaration( } IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); instr->kind = IR_STORE_VAR; instr->var.name = strdup(decl->var_decl.ident.ident_name); instr->src.size = decl->var_decl.ident.type.element_size; - if (!instr->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr->var.name != NULL, "out of memory"); if (decl->var_decl.ident.type.kind == TYPE_CUSTOM) { IR_instruction_t* alloc = calloc(1, sizeof(IR_instruction_t)); - if (!alloc) { - error_report_general( - ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(alloc != NULL, "out of memory"); + alloc->kind = IR_ALLOC; alloc->alloc_size = decl->var_decl.ident.type.element_size; @@ -143,10 +135,8 @@ int IR_lower_declaration( else if (decl->var_decl.ident.type.array_len > 0) { IR_instruction_t* alloc = calloc(1, sizeof(IR_instruction_t)); - if (!alloc) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(alloc != NULL, "out of memory"); + alloc->kind = IR_ALLOC; alloc->alloc_size = decl->var_decl.ident.type.size; da_append(func->code, alloc); @@ -187,20 +177,15 @@ int IR_lower_composite_literal_expression( IR_function_t* func) { IR_instruction_t* load = calloc(1, sizeof(IR_instruction_t)); - if (!load) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(load != NULL, "out of memory"); + load->kind = IR_LOAD_VAR; load->dest.id = func->next_temp_id; // we store a pointer (64bits) so we can hardcode the size here load->dest.size = 8; load->var.is_init = 1; load->var.name = strdup(decl->var_decl.ident.ident_name); - if (!load->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(load->var.name != NULL, "out of memory"); da_append(func->code, load); @@ -221,10 +206,8 @@ int IR_lower_composite_literal_expression( size_t computed_place = 0; IR_instruction_t* mov_offset = calloc(1, sizeof(IR_instruction_t)); - if (!mov_offset) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(mov_offset != NULL, "out of memory"); + mov_offset->kind = IR_MOV_OFFSET; mov_offset->offset.timing = IR_PRE_OFFSET; mov_offset->dest.id = save; @@ -276,26 +259,18 @@ int IR_lower_unary_expression(HIR_parser_t* hir, if (expr->unary.op == UNARY_POST_INC || expr->unary.op == UNARY_POST_DEC) { IR_instruction_t* load = calloc(1, sizeof(IR_instruction_t)); - if (!load) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(load != NULL, "out of memory"); load->kind = IR_LOAD_VAR; load->var.name = strdup(expr->unary.operand->var.ident.ident_name); load->dest.id = ++(func->next_temp_id); load->dest.size = operand_size; - if (!load->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(load->var.name != NULL, "out of memory"); da_append(func->code, load); IR_instruction_t* mov = calloc(1, sizeof(IR_instruction_t)); - if (!mov) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(mov != NULL, "out of memory"); + mov->kind = IR_MOV; mov->dest.id = func->next_temp_id + 1; mov->dest.size = operand_size; @@ -304,10 +279,8 @@ int IR_lower_unary_expression(HIR_parser_t* hir, da_append(func->code, mov); IR_instruction_t* op = calloc(1, sizeof(IR_instruction_t)); - if (!op) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(op != NULL, "out of memory"); + switch (expr->unary.op) { case UNARY_POST_INC: op->kind = IR_INC; break; case UNARY_POST_DEC: op->kind = IR_DEC; break; @@ -319,19 +292,14 @@ int IR_lower_unary_expression(HIR_parser_t* hir, da_append(func->code, op); IR_instruction_t* str = calloc(1, sizeof(IR_instruction_t)); - if (!str) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(str != NULL, "out of memory"); + str->kind = IR_STORE_VAR; str->src.id = func->next_temp_id++; str->src.size = operand_size; str->var.name = strdup(expr->unary.operand->var.ident.ident_name); str->var.is_init = 1; - if (!str->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(str->var.name != NULL, "out of memory"); da_append(func->code, str); return 0; } @@ -339,25 +307,20 @@ int IR_lower_unary_expression(HIR_parser_t* hir, if (expr->unary.op == UNARY_PRE_INC || expr->unary.op == UNARY_PRE_DEC) { IR_instruction_t* load = calloc(1, sizeof(IR_instruction_t)); - if (!load) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(load != NULL, "out of memory"); + load->kind = IR_LOAD_VAR; load->dest.id = ++(func->next_temp_id); load->dest.size = operand_size; - load->var.name = strdup(expr->unary.operand->var.ident.ident_name); - if (!load->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + load->var.name = + strdup(expr->unary.operand->var.ident.ident_name); + + CLEAF_ASSERT(load->var.name != NULL, "out of memory"); da_append(func->code, load); IR_instruction_t* op = calloc(1, sizeof(IR_instruction_t)); - if (!op) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(op != NULL, "out of memory"); + switch (expr->unary.op) { case UNARY_PRE_INC: op->kind = IR_INC; break; case UNARY_PRE_DEC: op->kind = IR_DEC; break; @@ -368,18 +331,16 @@ int IR_lower_unary_expression(HIR_parser_t* hir, da_append(func->code, op); IR_instruction_t* str = calloc(1, sizeof(IR_instruction_t)); - if (!str) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(str != NULL, "out of memory"); + str->kind = IR_STORE_VAR; str->src.id = func->next_temp_id; str->src.size = operand_size; - str->var.name = strdup(expr->unary.operand->var.ident.ident_name); - if (!str->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + str->var.name = + strdup(expr->unary.operand->var.ident.ident_name); + + CLEAF_ASSERT(str->var.name != NULL, "out of memory"); + str->var.is_init = 1; da_append(func->code, str); return 0; @@ -415,10 +376,8 @@ int IR_lower_lvalue(HIR_parser_t* hir, int idx_id = func->next_temp_id; IR_instruction_t* mul = calloc(1, sizeof(IR_instruction_t)); - if (!mul) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(mul != NULL, "out of memory"); + mul->kind = IR_DIRECT_MUL; mul->dest.id = func->next_temp_id; mul->dest.size = elem_size; @@ -445,10 +404,8 @@ int IR_lower_index_expression( return 1; IR_instruction_t* load = calloc(1, sizeof(IR_instruction_t)); - if (!load) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(load != NULL, "out of memory"); + load->kind = IR_LOAD_ELEM; load->src.id = lv.base_id; load->src.size = 8; @@ -466,10 +423,8 @@ int IR_lower_call_expression( { for (int i = 0; i < (int) expr->call.arg_count; ++i) { IR_instruction_t* set_arg = calloc(1, sizeof(IR_instruction_t)); - if (!set_arg) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(set_arg != NULL, "out of memory"); + set_arg->kind = IR_MOV; set_arg->dest.id = -i - 1; @@ -482,26 +437,21 @@ int IR_lower_call_expression( } IR_instruction_t* call = calloc(1, sizeof(IR_instruction_t)); - if (!call) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(call != NULL, "out of memory"); + call->kind = IR_CALL; const char* call_module = expr->call.resolved_module ? expr->call.resolved_module : hir->current_module; call->func_name = IR_mangle_function_name(call_module, expr->call.callee); - if (!call->func_name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + + CLEAF_ASSERT(call->func_name != NULL, "out of memory"); + da_append(func->code, call); IR_instruction_t* result = calloc(1, sizeof(IR_instruction_t)); - if (!result) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(result != NULL, "out of memory"); + result->kind = IR_MOV; result->dest.id = ++(func->next_temp_id); result->src.id = -1; @@ -563,10 +513,8 @@ int IR_lower_expr_int_lit(HIR_parser_t* hir, { (void)hir; IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); + instr->kind = IR_INT_CONST; instr->dest.id = ++(func->next_temp_id); instr->int_value = expr->int_lit.value; @@ -580,10 +528,8 @@ int IR_lower_expr_char_lit(HIR_parser_t* hir, { (void)hir; IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); + instr->kind = IR_INT_CONST; instr->dest.id = ++(func->next_temp_id); instr->int_value = expr->char_lit.value; @@ -596,10 +542,8 @@ int IR_lower_expr_var(HIR_parser_t* hir, IR_function_t* func) { IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); + instr->kind = IR_LOAD_VAR; instr->dest.id = ++(func->next_temp_id); @@ -611,18 +555,15 @@ int IR_lower_expr_var(HIR_parser_t* hir, } instr->var.name = strdup(expr->var.ident.ident_name); - if (!instr->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr->var.name != NULL, "out of memory"); da_append(func->code, instr); while (expr->var.member) { - IR_instruction_t* offset_instr = calloc(1, sizeof(IR_instruction_t)); - if (!offset_instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + IR_instruction_t* offset_instr = + calloc(1, sizeof(IR_instruction_t)); + + CLEAF_ASSERT(offset_instr != NULL, "out of memory"); + offset_instr->kind = IR_MOV_OFFSET; offset_instr->offset.timing = IR_POST_OFFSET; @@ -669,10 +610,7 @@ int IR_lower_expr_assign(HIR_parser_t* hir, return 1; IR_instruction_t* store = calloc(1, sizeof(IR_instruction_t)); - if (!store) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(store != NULL, "out of memory"); if (lv.kind == LVALUE_VAR) { store->kind = IR_STORE_VAR; @@ -680,11 +618,7 @@ int IR_lower_expr_assign(HIR_parser_t* hir, store->src.size = lv.elem_size; store->var.name = strdup(lv.var_name); store->var.is_init = 1; - if (!store->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - free(store); - return 1; - } + CLEAF_ASSERT(store->var.name != NULL, "out of memory"); } else { store->kind = IR_STORE_ELEM; store->src.id = rhs_temp; @@ -714,10 +648,8 @@ int IR_lower_expression(HIR_parser_t* hir, if (expr->type == EXPRESSION_BINARY) { IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); + if (IR_lower_binary_expression(expr, hir, instr, func) != 0) { error_report_at_position(hir->error_ctx, expr->source_pos, @@ -751,10 +683,7 @@ int IR_lower_free_statement( IR_function_t* func) { IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out if memory"); - return 1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); instr->kind = IR_DEALLOC; @@ -778,10 +707,8 @@ int IR_lower_asm_statement( IR_temp_id* temps = NULL; if (stmt->asm_stmt.arg_count > 0) { temps = calloc(stmt->asm_stmt.arg_count, sizeof(IR_temp_id)); - if (!temps) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(temps!= NULL, "out of memory"); + for (size_t i = 0; i < stmt->asm_stmt.arg_count; i++) { if (IR_lower_expression( hir, stmt->asm_stmt.args[i], func) != 0) { @@ -795,20 +722,11 @@ int IR_lower_asm_statement( } IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - free(temps); - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); instr->asm_data.strings = calloc(stmt->asm_stmt.instr_count, sizeof(char*)); - if (!instr->asm_data.strings) { - free(temps); - free(instr); - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(instr->asm_data.strings != NULL, "out of memory"); instr->kind = IR_ASM; instr->asm_data.string_count = stmt->asm_stmt.instr_count; @@ -817,15 +735,8 @@ int IR_lower_asm_statement( for (size_t i = 0; i < stmt->asm_stmt.instr_count; i++) { instr->asm_data.strings[i] = strdup(stmt->asm_stmt.instr[i]); - if (!instr->asm_data.strings[i]) { - for (size_t j = 0; j < i; j++) - free(instr->asm_data.strings[j]); - free(instr->asm_data.strings); - free(temps); - free(instr); - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT( + instr->asm_data.strings[i] != NULL, "out of memory"); } da_append(func->code, instr); @@ -846,17 +757,14 @@ int IR_lower_for_statement( return 1; char* main_chunk = calloc(2 + RAND_CHUNK_LEN, sizeof(char)); - if (!main_chunk) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(main_chunk != NULL, "out of memory"); + hir->gen_chunk(hir->chunk_ctx, main_chunk); - IR_instruction_t* main_label = calloc(1, sizeof(IR_instruction_t)); - if (!main_label) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* main_label = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(main_label != NULL, "out of memory"); + main_label->kind = IR_CHUNK; main_label->chunk_name = strdup(main_chunk); da_append(func->code, main_label); @@ -874,10 +782,8 @@ int IR_lower_for_statement( return 1; IR_instruction_t* jump = calloc(1, sizeof(IR_instruction_t)); - if (!jump) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(jump != NULL, "out of memory"); + jump->chunk_name = strdup(main_chunk); switch (stmt->for_stmt.condition->binary.op) { case BINARY_EQ: @@ -914,17 +820,13 @@ int IR_lower_while_statement( IR_function_t* func) { char* condition_chunk = calloc(2 + RAND_CHUNK_LEN, sizeof(char)); - if (!condition_chunk) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(condition_chunk != NULL, "out of memory"); hir->gen_chunk(hir->chunk_ctx, condition_chunk); - IR_instruction_t* condition_label = calloc(1, sizeof(IR_instruction_t)); - if (!condition_label) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* condition_label = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(condition_label != NULL, "out of memory"); + condition_label->kind = IR_CHUNK; condition_label->chunk_name = strdup(condition_chunk); da_append(func->code, condition_label); @@ -933,17 +835,13 @@ int IR_lower_while_statement( return 1; char* next_chunk = calloc(2 + RAND_CHUNK_LEN, sizeof(char)); - if (!next_chunk) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(next_chunk != NULL, "out of memory"); + hir->gen_chunk(hir->chunk_ctx, next_chunk); IR_instruction_t* jump = calloc(1, sizeof(IR_instruction_t)); - if (!jump) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(jump != NULL, "out of memory"); + jump->chunk_name = strdup(next_chunk); switch (stmt->while_stmt.condition->binary.op) { case BINARY_EQ: @@ -976,20 +874,18 @@ int IR_lower_while_statement( return 1; } - IR_instruction_t* jump_back = calloc(1, sizeof(IR_instruction_t)); - if (!jump_back) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* jump_back = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(jump_back != NULL, "out of memory"); + jump_back->kind = IR_JMP; jump_back->chunk_name = strdup(condition_chunk); da_append(func->code, jump_back); - IR_instruction_t* next_label = calloc(1, sizeof(IR_instruction_t)); - if (!next_label) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* next_label = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(next_label != NULL, "out of memory"); + next_label->kind = IR_CHUNK; next_label->chunk_name = strdup(next_chunk); da_append(func->code, next_label); @@ -1010,18 +906,13 @@ int IR_lower_if_statement(HIR_parser_t* hir, return 1; char* chunk = calloc(RAND_CHUNK_LEN + 2, sizeof(char)); - if (!chunk) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(chunk != NULL, "out of memory"); + hir->gen_chunk(hir->chunk_ctx, chunk); char* else_chunk = calloc(RAND_CHUNK_LEN + 2, sizeof(char)); - if (!else_chunk) { - free(chunk); - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(else_chunk != NULL, "out of memory"); + // TODO: this is src.id bit ugly but whatever for now if (stmt->if_stmt.else_branch) { hir->gen_chunk(hir->chunk_ctx, else_chunk); @@ -1032,10 +923,8 @@ int IR_lower_if_statement(HIR_parser_t* hir, } IR_instruction_t* jump = calloc(1, sizeof(IR_instruction_t)); - if (!jump) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + CLEAF_ASSERT(jump != NULL, "out of memory"); + if (else_chunk) jump->chunk_name = strdup(else_chunk); else @@ -1073,21 +962,17 @@ int IR_lower_if_statement(HIR_parser_t* hir, } if (else_chunk) { - IR_instruction_t* jump_else = calloc(1, sizeof(IR_instruction_t)); - if (!jump_else) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* jump_else = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(jump_else != NULL, "out of memory"); jump_else->kind = IR_JMP; jump_else->chunk_name = strdup(chunk); da_append(func->code, jump_else); - IR_instruction_t* chunk_else_label = calloc(1, sizeof(IR_instruction_t)); - if (!chunk_else_label) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* chunk_else_label = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(chunk_else_label != NULL, "out of memory"); chunk_else_label->kind = IR_CHUNK; chunk_else_label->chunk_name = else_chunk; @@ -1100,11 +985,9 @@ int IR_lower_if_statement(HIR_parser_t* hir, } } - IR_instruction_t* chunk_label = calloc(1, sizeof(IR_instruction_t)); - if (!chunk_label) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* chunk_label = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(chunk_label != NULL, "out of memory"); chunk_label->kind = IR_CHUNK; chunk_label->chunk_name = strdup(chunk); @@ -1124,23 +1007,20 @@ int IR_lower_return_statement(HIR_parser_t* hir, return -1; IR_instruction_t* instr = calloc(1, sizeof(IR_instruction_t)); - if (!instr) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(instr != NULL, "out of memory"); - if (strcmp(func->name, "main") == 0 || strcmp(func->name, "start") == 0) { + if (strcmp(func->name, "main") == 0 || + strcmp(func->name, "start") == 0) { instr->kind = IR_EXIT; instr->dest.id = func->next_temp_id; instr->dest.size = func->code->items[func->code->count - 1]->dest.size; } else { // TODO: what append if we return void ? - IR_instruction_t* return_var = calloc(1, sizeof(IR_instruction_t)); - if (!return_var) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return 1; - } + IR_instruction_t* return_var = + calloc(1, sizeof(IR_instruction_t)); + CLEAF_ASSERT(return_var != NULL, "out of memory"); + return_var->kind = IR_MOV; return_var->dest.id = -1; return_var->src.id = func->next_temp_id; @@ -1199,10 +1079,8 @@ static int IR_lower_function_params( { for (int i = 0; i < (int) function->func.params.count; ++i) { IR_instruction_t* mov = calloc(1, sizeof(IR_instruction_t)); - if (!mov) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(mov != NULL, "out of memory"); + mov->kind = IR_MOV; mov->dest.id = func->next_temp_id; mov->dest.size = function->func.params.items[i].type.size; @@ -1221,16 +1099,12 @@ static int IR_lower_function_params( da_append(func->code, mov); IR_instruction_t* str = calloc(1, sizeof(IR_instruction_t)); - if (!str) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(str != NULL, "out of memory"); + str->kind = IR_STORE_VAR; str->var.name = strdup(function->func.params.items[i].ident_name); - if (!str->var.name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(str->var.name != NULL, "out of memory"); + str->var.is_init = 1; str->src.id = func->next_temp_id++; str->src.size = function->func.params.items[i].type.element_size; @@ -1250,25 +1124,18 @@ int IR_lower_function(HIR_parser_t* hir, return 0; IR_function_t* func = calloc(1, sizeof(IR_function_t)); - if (!func) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(func != NULL, "out of memory"); - func->name = IR_mangle_function_name(hir->current_module, function->func.name); - if (!func->name) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + func->name = IR_mangle_function_name( + hir->current_module, function->func.name); + + CLEAF_ASSERT(func->name != NULL, "out of memory"); func->next_temp_id = 0; func->stack_reserve_size = 0; func->code = calloc(1, sizeof(IR_instruction_block)); - if (!func->code) { - error_report_general(ERROR_SEVERITY_ERROR, "out of memory"); - return -1; - } + CLEAF_ASSERT(func->code != NULL, "out of memory"); // TODO: behavior is different for _start but whatever for now if (IR_lower_function_params(func, function) != 0) diff --git a/src/thirdparty/assertion.h b/src/thirdparty/assertion.h new file mode 100644 index 0000000..3b583e5 --- /dev/null +++ b/src/thirdparty/assertion.h @@ -0,0 +1,17 @@ +#ifndef ASSERTION_H +#define ASSERTION_H + +#define CLEAF_ASSERT(expr, msg) \ + do { \ + if (!(expr)) { \ + fprintf(stderr, \ + "Assertion failed: %s\n" \ + "Reason: %s\n" \ + " File: %s\n" \ + " Line: %d\n", \ + #expr, msg, __FILE__, __LINE__); \ + abort(); \ + } \ + } while(0) \ + +#endif // ASSERTION_H