From 406d2ab6ba8a5906df634fc5f00c40b6983a0c39 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 1 May 2021 17:42:59 +0100 Subject: [PATCH] Simplify/Fix the state_flag behaviour for code generation --- src/llvm_backend.cpp | 42 +++++++++++++----------------------------- src/llvm_backend.hpp | 3 +-- src/parser.cpp | 8 ++++++++ 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index f215f12a4..de202658a 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -184,7 +184,7 @@ void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index, lbValue le if (build_context.no_bounds_check) { return; } - if ((p->module->state_flags & StateFlag_no_bounds_check) != 0) { + if ((p->state_flags & StateFlag_no_bounds_check) != 0) { return; } @@ -209,7 +209,7 @@ void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValu if (build_context.no_bounds_check) { return; } - if ((p->module->state_flags & StateFlag_no_bounds_check) != 0) { + if ((p->state_flags & StateFlag_no_bounds_check) != 0) { return; } @@ -3117,19 +3117,6 @@ void lb_begin_procedure_body(lbProcedure *p) { } } - if (p->tags != 0) { - u64 in = p->tags; - u64 out = p->module->state_flags; - if (in & ProcTag_bounds_check) { - out |= StateFlag_bounds_check; - out &= ~StateFlag_no_bounds_check; - } else if (in & ProcTag_no_bounds_check) { - out |= StateFlag_no_bounds_check; - out &= ~StateFlag_bounds_check; - } - p->module->state_flags = out; - } - p->builder = LLVMCreateBuilder(); p->decl_block = lb_create_block(p, "decls", true); @@ -3299,7 +3286,7 @@ void lb_end_procedure_body(lbProcedure *p) { } p->curr_block = nullptr; - p->module->state_flags = 0; + p->state_flags = 0; } void lb_end_procedure(lbProcedure *p) { LLVMDisposeBuilder(p->builder); @@ -4821,12 +4808,12 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location); }); - u64 prev_state_flags = p->module->state_flags; - defer (p->module->state_flags = prev_state_flags); + u16 prev_state_flags = p->state_flags; + defer (p->state_flags = prev_state_flags); if (node->state_flags != 0) { - u64 in = node->state_flags; - u64 out = p->module->state_flags; + u16 in = node->state_flags; + u16 out = p->state_flags; if (in & StateFlag_bounds_check) { out |= StateFlag_bounds_check; @@ -4836,7 +4823,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { out &= ~StateFlag_bounds_check; } - p->module->state_flags = out; + p->state_flags = out; } switch (node->kind) { @@ -11014,12 +11001,12 @@ lbValue lb_emit_any_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos lbValue lb_build_expr(lbProcedure *p, Ast *expr) { lbModule *m = p->module; - u64 prev_state_flags = p->module->state_flags; - defer (p->module->state_flags = prev_state_flags); + u16 prev_state_flags = p->state_flags; + defer (p->state_flags = prev_state_flags); if (expr->state_flags != 0) { - u64 in = expr->state_flags; - u64 out = p->module->state_flags; + u16 in = expr->state_flags; + u16 out = p->state_flags; if (in & StateFlag_bounds_check) { out |= StateFlag_bounds_check; @@ -11029,7 +11016,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { out &= ~StateFlag_bounds_check; } - p->module->state_flags = out; + p->state_flags = out; } expr = unparen_expr(expr); @@ -12844,9 +12831,6 @@ void lb_init_module(lbModule *m, Checker *c) { m->debug_builder = LLVMCreateDIBuilder(m->mod); } - m->state_flags = 0; - m->state_flags |= StateFlag_bounds_check; - gb_mutex_init(&m->mutex); gbAllocator a = heap_allocator(); map_init(&m->types, a); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index cd6a99c20..aed555aaf 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -85,8 +85,6 @@ struct lbModule { LLVMModuleRef mod; LLVMContextRef ctx; - u64 state_flags; - CheckerInfo *info; gbMutex mutex; @@ -210,6 +208,7 @@ enum lbProcedureFlag : u32 { struct lbProcedure { u32 flags; + u16 state_flags; lbProcedure *parent; Array children; diff --git a/src/parser.cpp b/src/parser.cpp index a6764de08..c9275c7f2 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -2170,6 +2170,14 @@ Ast *parse_operand(AstFile *f, bool lhs) { body = parse_body(f); f->curr_proc = curr_proc; + // Apply the tags directly to the body rather than the type + if (tags & ProcTag_no_bounds_check) { + body->state_flags |= StateFlag_no_bounds_check; + } + if (tags & ProcTag_bounds_check) { + body->state_flags |= StateFlag_bounds_check; + } + return ast_proc_lit(f, type, body, tags, where_token, where_clauses); } else if (allow_token(f, Token_do)) { Ast *curr_proc = f->curr_proc;