fixes with disassembly view
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 4 Jan 2023 03:29:28 +0000 (21:29 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 4 Jan 2023 03:29:28 +0000 (21:29 -0600)
core/memory/memory.onyx
interpreter/src/debug/debug_thread.c
interpreter/src/vm/code_builder.c
interpreter/src/vm/disasm.c
interpreter/src/wasm/module.c
misc/vscode/onyx-0.1.2.vsix [new file with mode: 0644]
misc/vscode/onyx-0.1.3.vsix [new file with mode: 0644]
misc/vscode/out/ovmDebug.js
misc/vscode/ovmDebug.ts
misc/vscode/package.json
shared/lib/linux_x86_64/lib/libovmwasm.so

index 1b23c8b1996e27d510d685a1e080f7ee3f04838c..a3ca6e70444f5e9c67f011b7352b73e5786f1ff0 100644 (file)
@@ -59,7 +59,7 @@ resize_slice :: (sl: [] $T, new_size: i32, allocator := context.allocator) -> []
 #overload
 builtin.__make_overload :: macro (_: ^[] $T, count: u32, allocator := context.allocator) -> [] T {
     ret := (package core.memory).make_slice(T, count, allocator);
-    memory.set(ret.data, 0, sizeof T * count);
+    (package core.memory).set(ret.data, 0, sizeof T * count);
     return ret;
 }
 
index bd3994a56e9e135f91b86ec2d2f50f03c112db52..02d34633baf79a69e9300c5642ca5a04cb39cf94 100644 (file)
@@ -489,6 +489,9 @@ static DEBUG_COMMAND_HANDLER(debug_command_disassmble) {
     // through the first thread.
     ovm_program_t *prog = debug->threads[0]->ovm_state->program;
 
+    debug_loc_info_t loc_info;
+    u32 last_file_id = 0xffffffff;
+
     while (addr < bh_arr_length(prog->code) && count--) {
         send_int(debug, 0);
 
@@ -497,6 +500,18 @@ static DEBUG_COMMAND_HANDLER(debug_command_disassmble) {
         send_bytes(debug, instr_buf.data, instr_buf.length);
         bh_buffer_clear(&instr_buf);
 
+        debug_info_lookup_location(debug->info, addr, &loc_info);
+        send_int(debug, loc_info.line);
+
+        if (loc_info.file_id != last_file_id) {
+            send_bool(debug, true);
+            send_string(debug, debug->info->files[loc_info.file_id].name);
+
+            last_file_id = loc_info.file_id;
+        } else {
+            send_bool(debug, false);
+        }
+
         addr += 1;
     }
 
index bb4429a255034258fdd18c28c041ea6e07a978cd..a00e416d40fb0ed7b559fb7d8ac62b47471a6b27 100644 (file)
 
 #define PUSH_VALUE(b, r) (bh_arr_push((b)->execution_stack, r))
 
+#define LAST_VALUE(b) bh_arr_last((b)->execution_stack)
+
+#define IS_TEMPORARY_VALUE(b, r) (r >= (b->param_count + b->local_count))
+
 static inline int NEXT_VALUE(ovm_code_builder_t *b) {
 #if defined(BUILDER_DEBUG)
     b->highest_value_number += 1;
@@ -400,6 +404,21 @@ void ovm_code_builder_add_local_get(ovm_code_builder_t *builder, i32 local_idx)
 }
 
 void ovm_code_builder_add_local_set(ovm_code_builder_t *builder, i32 local_idx) {
+    // :PrimitiveOptimization
+    // Perform a simple optimization that an immediate temporary moved to
+    // a local can be optimized as an immediate loaded directly to a local.
+    {
+        ovm_instr_t *last_instr = &bh_arr_last(builder->program->code);
+        if (OVM_INSTR_INSTR(*last_instr) == OVMI_IMM) {
+            if (IS_TEMPORARY_VALUE(builder, last_instr->r)
+                && last_instr->r == LAST_VALUE(builder)) {
+                last_instr->r = local_idx;
+                POP_VALUE(builder);
+                return;
+            }
+        }
+    }
+
     ovm_instr_t instr = {0};
     instr.full_instr = OVM_TYPED_INSTR(OVMI_MOV, OVM_TYPE_NONE);
     instr.r = local_idx; // This makes the assumption that the params will be in
index c23cc192b7662aeabf6d08b7af40b2bd2f6751c2..4292876a25593d4d8df6e790d342167e1b3e4646 100644 (file)
@@ -165,8 +165,8 @@ void ovm_disassemble(ovm_program_t *program, u32 instr_addr, bh_buffer *instr_te
 
         case instr_format_idx_arr: formatted = snprintf(buf, 255, "%%%d, __global_arr_%d[%%%d]", instr->r, instr->a, instr->b); break;
 
-        case instr_format_br:       formatted = snprintf(buf, 255, "%d", instr_addr + instr->a); break;
-        case instr_format_br_cond:  formatted = snprintf(buf, 255, "%d, %%%d", instr_addr + instr->a, instr->b); break;
+        case instr_format_br:       formatted = snprintf(buf, 255, "%d", instr_addr + instr->a + 1); break;
+        case instr_format_br_cond:  formatted = snprintf(buf, 255, "%d, %%%d", instr_addr + instr->a + 1, instr->b); break;
         case instr_format_bri:      formatted = snprintf(buf, 255, "ip + %%%d", instr->a); break;
         case instr_format_bri_cond: formatted = snprintf(buf, 255, "ip + %%%d, %%%d", instr->a, instr->b); break;
 
index 8e469a674b5e9668f835ec1915285b3c65095ea5..0003ea89430edb061b9d12c4260c0d9b55d18e41 100644 (file)
@@ -29,6 +29,10 @@ static bool module_build(wasm_module_t *module, const wasm_byte_vec_t *binary) {
     // But Onyx does not do this, so I don't care at the moment.
     module->program->register_count = module->globaltypes.size;
 
+    #if 0
+        printf("Program instruction count: %d\n", bh_arr_length(module->program->code));
+    #endif
+
     return true;
 }
 
diff --git a/misc/vscode/onyx-0.1.2.vsix b/misc/vscode/onyx-0.1.2.vsix
new file mode 100644 (file)
index 0000000..a40e2d7
Binary files /dev/null and b/misc/vscode/onyx-0.1.2.vsix differ
diff --git a/misc/vscode/onyx-0.1.3.vsix b/misc/vscode/onyx-0.1.3.vsix
new file mode 100644 (file)
index 0000000..1db3ed0
Binary files /dev/null and b/misc/vscode/onyx-0.1.3.vsix differ
index efe9d3ec25baa420d9d7af1a12e23bb3c36892e5..440bd4566b050c00005241bd46cd75cd10aeb50f 100644 (file)
@@ -148,11 +148,7 @@ class OVMDebugSession extends debugadapter_1.LoggingDebugSession {
             this._variableReferences.reset();
             response.body = {
                 stackFrames: frames.map((f, i) => {
-                    let source = new debugadapter_1.Source(this.fileNameToShortName(f.filename), this.convertDebuggerPathToClient(f.filename), undefined, undefined, "ovm-debug-src");
-                    if (!this._loadedSources.has(source.name)) {
-                        this._loadedSources.set(source.name, source);
-                        this.sendEvent(new debugadapter_1.LoadedSourceEvent("new", source));
-                    }
+                    let source = this.loadSource(f.filename);
                     let frameRef = this._frameReferences.create({
                         threadId: args.threadId,
                         frameIndex: i
@@ -270,14 +266,19 @@ class OVMDebugSession extends debugadapter_1.LoggingDebugSession {
     }
     disassembleRequest(response, args, request) {
         return __awaiter(this, void 0, void 0, function* () {
-            console.log(args);
             let addr = parseInt(args.memoryReference) + args.offset;
             let instrs = yield this.debugger.disassemble(addr, args.instructionCount);
             response.body = {
                 instructions: instrs.map((i, index) => {
+                    let source;
+                    if (i.newSource != null) {
+                        source = this.loadSource(i.newSource);
+                    }
                     return {
                         address: (index + addr).toString(),
-                        instruction: i.instr
+                        instruction: i.instr,
+                        line: i.line,
+                        location: source,
                     };
                 })
             };
@@ -309,6 +310,14 @@ class OVMDebugSession extends debugadapter_1.LoggingDebugSession {
     fileNameToShortName(filename) {
         return filename.substring(filename.lastIndexOf("/") + 1);
     }
+    loadSource(filename) {
+        let source = new debugadapter_1.Source(this.fileNameToShortName(filename), this.convertDebuggerPathToClient(filename), undefined, undefined, "ovm-debug-src");
+        if (!this._loadedSources.has(source.name)) {
+            this._loadedSources.set(source.name, source);
+            this.sendEvent(new debugadapter_1.LoadedSourceEvent("new", source));
+        }
+        return source;
+    }
 }
 exports.OVMDebugSession = OVMDebugSession;
 var OVMCommand;
@@ -644,7 +653,12 @@ class OVMDebugger extends EventEmitter {
                 let result = new Array();
                 while (parser.parseInt32() == 0) {
                     let instr = parser.parseString();
-                    result.push({ instr });
+                    let line = parser.parseUint32();
+                    let newSource = null;
+                    if (parser.parseBool()) {
+                        newSource = parser.parseString();
+                    }
+                    result.push({ instr, line, newSource });
                 }
                 this.resolvePromise(msg_id, result);
                 break;
index 940fb4f41a5a38f2d050850837a1f79377eae53c..d09127580df3dc911c4ec2a4e205948cc53ace82 100644 (file)
@@ -220,18 +220,8 @@ export class OVMDebugSession extends LoggingDebugSession {
 
                response.body = {
                        stackFrames: frames.map((f, i) => {
-                               let source = new Source(
-                                       this.fileNameToShortName(f.filename),
-                                       this.convertDebuggerPathToClient(f.filename),
-                                       undefined, undefined, "ovm-debug-src"
-                               );
-
-                               if (!this._loadedSources.has(source.name)) {
-                                       this._loadedSources.set(source.name, source);
-       
-                                       this.sendEvent(new LoadedSourceEvent("new", source));
-                               }
-                               
+                               let source = this.loadSource(f.filename);
+
                                let frameRef = this._frameReferences.create({
                                        threadId: args.threadId,
                                        frameIndex: i
@@ -381,9 +371,16 @@ export class OVMDebugSession extends LoggingDebugSession {
 
                response.body = {
                        instructions: instrs.map((i, index) => {
+                               let source: DebugProtocol.Source | null;
+                               if (i.newSource != null) {
+                                       source = this.loadSource(i.newSource);
+                               }
+
                                return {
-                                       address: (index + addr - 1).toString(),
+                                       address: (index + addr).toString(),
                                        instruction: i.instr,
+                                       line: i.line,
+                                       location: source,
                                };
                        })
                };
@@ -421,6 +418,22 @@ export class OVMDebugSession extends LoggingDebugSession {
        private fileNameToShortName(filename: string): string {
                return filename.substring(filename.lastIndexOf("/") + 1);
        }
+
+       private loadSource(filename: string): Source {
+               let source = new Source(
+                       this.fileNameToShortName(filename),
+                       this.convertDebuggerPathToClient(filename),
+                       undefined, undefined, "ovm-debug-src"
+               );
+
+               if (!this._loadedSources.has(source.name)) {
+                       this._loadedSources.set(source.name, source);
+
+                       this.sendEvent(new LoadedSourceEvent("new", source));
+               }
+                               
+               return source;
+       }
 }
 
 interface IFileLocation {
@@ -456,6 +469,8 @@ interface IReadMemory {
 
 interface IDisassembledInstruction {
        instr: string;
+       line: number;
+       newSource?: string;
 }
 
 enum OVMCommand {
@@ -887,7 +902,14 @@ class OVMDebugger extends EventEmitter {
 
                                while (parser.parseInt32() == 0) {
                                        let instr = parser.parseString();
-                                       result.push({ instr });
+                                       let line  = parser.parseUint32();
+
+                                       let newSource: string | null = null;
+                                       if (parser.parseBool()) {
+                                               newSource = parser.parseString();
+                                       }
+
+                                       result.push({ instr, line, newSource });
                                }
 
                                this.resolvePromise(msg_id, result);
index ba240183404a3c0e05acab8d130e1327fb731ae1..6dbb9104b9cabcd5affdcacfa41d526450646fbe 100644 (file)
@@ -2,7 +2,7 @@
     "name": "onyx",
     "displayName": "Onyx",
     "description": "Onyx syntax highlighting.",
-    "version": "0.1.1",
+    "version": "0.1.3",
     "publisher": "brendanfh",
     "license": "BSD-2-Clause",
     "engines": {
index 3e670e1482725e7a3c53eef326c34a6cdd9224a9..6e2ffbb9e34ffa5f8a5198e6820ebf9adfd9a2d9 100755 (executable)
Binary files a/shared/lib/linux_x86_64/lib/libovmwasm.so and b/shared/lib/linux_x86_64/lib/libovmwasm.so differ