bugfix with overflowing integers
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Feb 2023 17:37:28 +0000 (11:37 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Feb 2023 17:37:28 +0000 (11:37 -0600)
compiler/src/parser.c
compiler/src/wasm_emit.c
compiler/src/wasm_output.h
core/onyx/cptr.onyx
interpreter/src/vm/vm_instrs.h

index 7a47f71827fbf0d35579da60dba0aec07e29d0d7..410ac3b66e8a21edf20c034c767ca3aa64b074f2 100644 (file)
@@ -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);
index c0d3e9dfa49a27f66c9032da7376167eb9b4af3b..27fc5d2e50adee0502ad2d97fb09474492e557b5 100644 (file)
@@ -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);
 
index c25578273fe5a14f395e70cf6ebf355b9fc6f853..2692febbbd34b85c231bcc1b4503dfa690ffc993 100644 (file)
@@ -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);
index 80fb096cd752c884943decb92eb30b96cdeefba9..ae0afe6b356d015c71f24f3a5269922d6b386eda 100644 (file)
@@ -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);
     }
index c1ecfdea4ac4c59bb7328caa6f9ed4a4ecbd2cec..c1916ceceaa8a4a15195311c50a02142c258d94d 100644 (file)
@@ -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)) {