From: Brendan Hansen Date: Tue, 21 Feb 2023 17:37:28 +0000 (-0600) Subject: bugfix with overflowing integers X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=47dc68e783082ef4e0e2cfc2e4d4fe6e3afa394e;p=onyx.git bugfix with overflowing integers --- diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 7a47f718..410ac3b6 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -800,6 +800,14 @@ static AstTyped* parse_factor(OnyxParser* parser) { retval = (AstTyped *) this_package; break; } + else if (parse_possible_directive(parser, "Self")) { + if (parser->injection_point == NULL) { + onyx_report_error((parser->curr - 2)->pos, Error_Critical, "#Self is only allowed in an #inject block."); + } + + retval = parser->injection_point; + break; + } onyx_report_error(parser->curr->pos, Error_Critical, "Invalid directive in expression."); return NULL; @@ -1968,6 +1976,18 @@ static AstType* parse_type(OnyxParser* parser) { break; } + case '#': { + if (parse_possible_directive(parser, "Self")) { + if (parser->injection_point == NULL) { + onyx_report_error((parser->curr - 2)->pos, Error_Critical, "#Self is only allowed in an #inject block."); + } + + *next_insertion = (AstType *) parser->injection_point; + next_insertion = NULL; + break; + } + } + default: onyx_report_error(parser->curr->pos, Error_Critical, "unexpected token '%b'.", parser->curr->text, parser->curr->length); consume_token(parser); diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index c0d3e9df..27fc5d2e 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -302,6 +302,10 @@ static void debug_set_position(OnyxWasmModule *mod, OnyxToken *token) { // - REP // - SET, REP 0 static void debug_emit_instruction(OnyxWasmModule *mod, OnyxToken *token) { + if (!context.options->debug_enabled) { + return; + } + DebugContext *ctx = mod->debug_context; assert(ctx && ctx->last_token); diff --git a/compiler/src/wasm_output.h b/compiler/src/wasm_output.h index c2557827..2692febb 100644 --- a/compiler/src/wasm_output.h +++ b/compiler/src/wasm_output.h @@ -1064,7 +1064,9 @@ void onyx_wasm_module_write_to_buffer(OnyxWasmModule* module, bh_buffer* buffer) bh_buffer_append(buffer, WASM_VERSION, 4); #ifdef ENABLE_DEBUG_INFO - output_ovm_debug_sections(module, buffer); + if (context.options->debug_enabled) { + output_ovm_debug_sections(module, buffer); + } #endif output_typesection(module, buffer); output_importsection(module, buffer); diff --git a/core/onyx/cptr.onyx b/core/onyx/cptr.onyx index 80fb096c..ae0afe6b 100644 --- a/core/onyx/cptr.onyx +++ b/core/onyx/cptr.onyx @@ -29,10 +29,16 @@ cptr :: struct (T: type_expr) { // // Extract the data out of a C pointer into a buffer in the Onyx memory. - read :: (this: cptr($T)) -> T { - buf: [sizeof T] u8; - __cptr_read(this.data, ~~buf, sizeof T); - return *cast(^T) buf; + read :: #match { + // + // Special, error-inducing case for cptr(void) + (this: cptr(void)) -> void { }, + + (this: cptr($T)) -> T { + buf: [sizeof T] u8; + __cptr_read(this.data, ~~buf, sizeof T); + return *cast(^T) buf; + } } // @@ -69,10 +75,19 @@ cptr :: struct (T: type_expr) { // to the memory address 0, not the base address for the WASM // memory. mem_base_ptr := __cptr_make(cast(rawptr) 1); - assert(mem_base_ptr <= this.data + 1 && this.data + 1 <= mem_base_ptr + ~~(wasm.memory_size() << 16), "Invalid conversion from cptr to rawptr: pointer value out of Onyx memory range."); + assert(mem_base_ptr <= this.data + 1 && (this.data + 1) >> 16 <= mem_base_ptr + ~~(wasm.memory_size()), "Invalid conversion from cptr to rawptr: pointer value out of Onyx memory range."); return ~~(this.data - mem_base_ptr + 1); } + as :: (this: cptr($T), $new_type: type_expr) -> cptr(new_type) { + return .{ this.data }; + } + + at :: (this: cptr($T), index: i32) -> T { + elem := this + index; + return elem->read(); + } + format :: (output: ^conv.Format_Output, format: ^conv.Format, p: ^cptr($T)) { conv.format(output, "cptr({})[0x{b16}]", T, p.data); } diff --git a/interpreter/src/vm/vm_instrs.h b/interpreter/src/vm/vm_instrs.h index c1ecfdea..c1916cec 100644 --- a/interpreter/src/vm/vm_instrs.h +++ b/interpreter/src/vm/vm_instrs.h @@ -467,7 +467,7 @@ CMPXCHG(OVM_TYPE_I64, i64) // OVMI_INSTR_EXEC(mem_size) { - VAL(instr->r).u32 = (u32) state->engine->memory_size / 65536; + VAL(instr->r).u32 = (u32) (state->engine->memory_size / 65536); VAL(instr->r).type = OVM_TYPE_I32; NEXT_OP; } @@ -475,7 +475,7 @@ OVMI_INSTR_EXEC(mem_size) { OVMI_INSTR_EXEC(mem_grow) { ovm_assert(VAL(instr->a).type == OVM_TYPE_I32); VAL(instr->r).type = OVM_TYPE_I32; - VAL(instr->r).u32 = (u32) state->engine->memory_size / 65536; + VAL(instr->r).u32 = (u32) (state->engine->memory_size / 65536); if (!ovm_engine_memory_ensure_capacity(state->engine, state->engine->memory_size + VAL(instr->a).u32 * 65536)) {