-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdisassembler.zig
More file actions
765 lines (670 loc) · 24.6 KB
/
Copy pathdisassembler.zig
File metadata and controls
765 lines (670 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
const std = @import("std");
const io = @import("io.zig");
const print = io.print;
const Chunk = @import("Chunk.zig");
const Value = @import("value.zig").Value;
const obj = @import("obj.zig");
const _vm = @import("vm.zig");
const global_allocator = @import("buzz_api.zig").allocator;
const builtin = @import("builtin");
const is_wasm = builtin.cpu.arch.isWasm();
const VM = _vm.VM;
const OpCode = Chunk.OpCode;
pub fn disassembleChunk(chunk: *Chunk, name: []const u8) void {
print("\u{001b}[2m", .{}); // Dimmed
print("=== {s} ===\n", .{name});
var offset: usize = 0;
while (offset < chunk.code.items.len) {
offset = disassembleInstruction(chunk, offset);
}
print("\u{001b}[0m", .{});
}
fn namedInvokeInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const constant: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
const arg_count: u8 = @intCast(chunk.code.items[offset + 1] >> 24);
const catch_count: u24 = @intCast(0x00ffffff & chunk.code.items[offset + 1]);
var value_str = chunk.constants.items[constant].toStringAlloc(global_allocator) catch @panic("Out of memory");
defer global_allocator.free(value_str);
print(
"{s}\t{s}({} args, {} catches)",
.{
@tagName(code),
value_str[0..@min(value_str.len, 100)],
arg_count,
catch_count,
},
);
return offset + 2;
}
fn invokeInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const method_idx: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
const arg_count: u8 = @intCast(chunk.code.items[offset + 1] >> 24);
const catch_count: u24 = @intCast(0x00ffffff & chunk.code.items[offset + 1]);
print(
"{s}\t{}({} args, {} catches)",
.{
@tagName(code),
method_idx,
arg_count,
catch_count,
},
);
return offset + 1;
}
fn simpleInstruction(code: OpCode, offset: usize) usize {
print("{s}\t", .{@tagName(code)});
return offset + 1;
}
fn byteInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const slot: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
print(
"{s}\t{}",
.{
@tagName(code),
slot,
},
);
return offset + 1;
}
fn bytesInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const a: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
const b: u24 = @intCast(chunk.code.items[offset + 1]);
print(
"{s}\t{} {}",
.{
@tagName(code),
a,
b,
},
);
return offset + 2;
}
fn triInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const full_instruction: u32 = chunk.code.items[offset];
const a: u8 = @intCast((0x00ffffff & full_instruction) >> 16);
const b: u16 = @intCast(0x0000ffff & full_instruction);
print(
"{s}\t{} {}",
.{
@tagName(code),
a,
b,
},
);
return offset + 1;
}
fn constantInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const constant: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
var value_str = chunk.constants.items[constant].toStringAlloc(global_allocator) catch @panic("Out of memory");
defer global_allocator.free(value_str);
print(
"{s}\t{} {s}",
.{
@tagName(code),
constant,
value_str[0..@min(value_str.len, 100)],
},
);
return offset + 1;
}
fn jumpInstruction(code: OpCode, chunk: *Chunk, direction: bool, offset: usize) usize {
const jump: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
if (direction) {
print(
"{s}\t{} + {} -> {}",
.{
@tagName(code),
offset,
jump,
offset + 1 + 1 * jump,
},
);
} else {
print(
"{s}\t{} - {} -> {}",
.{
@tagName(code),
offset,
jump,
offset + 1 - 1 * jump,
},
);
}
return offset + 1;
}
fn tryInstruction(code: OpCode, chunk: *Chunk, offset: usize) usize {
const jump: u24 = @intCast(0x00ffffff & chunk.code.items[offset]);
print(
"{s}\t{} -> {}",
.{
@tagName(code),
offset,
jump,
},
);
return offset + 1;
}
pub fn dumpStack(vm: *VM) void {
const vm_io = if (is_wasm) {} else vm.process.io;
print(vm_io, "\u{001b}[2m", .{}); // Dimmed
print(vm_io, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", .{});
var value: [*]Value = @ptrCast(vm.current_fiber.stack[0..]);
var count: usize = 0;
while (@intFromPtr(value) < @intFromPtr(vm.current_fiber.stack_top) and count < vm.current_fiber.stack.len) : (count += 1) {
var value_str = value[0].toStringAlloc(global_allocator) catch unreachable;
defer global_allocator.free(value_str);
if (vm.currentFrame().?.slots == value) {
print(
vm_io,
"{} {} {s} frame\n ",
.{
@intFromPtr(value),
value[0].val,
value_str[0..@min(value_str.len, 100)],
},
);
} else {
print(
vm_io,
"{} {} {s}\n ",
.{
@intFromPtr(value),
value[0].val,
value_str[0..@min(value_str.len, 100)],
},
);
}
value += 1;
}
print(vm_io, "{} top\n", .{@intFromPtr(vm.current_fiber.stack_top)});
print(vm_io, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n\n", .{});
print(vm_io, "\u{001b}[0m", .{});
}
pub fn disassembleInstruction(chunk: *Chunk, offset: usize) usize {
const vm_io = if (is_wasm) {} else std.Options.debug_io;
print(vm_io, "\n{:0>3} ", .{offset});
const lines = chunk.ast.tokens.items(.line);
if (offset > 0 and lines[chunk.locations.items[offset]] == lines[chunk.locations.items[offset - 1]]) {
print(vm_io, "| ", .{});
} else {
print(vm_io, "{:0>3} ", .{lines[chunk.locations.items[offset]]});
}
const full_instruction: u32 = chunk.code.items[offset];
const instruction: OpCode = @enumFromInt(@as(u8, @intCast(full_instruction >> 24)));
const arg: u24 = @intCast(0x00ffffff & full_instruction);
return switch (instruction) {
.OP_NULL,
.OP_VOID,
.OP_TRUE,
.OP_FALSE,
.OP_POP,
.OP_EQUAL,
.OP_IS,
.OP_GREATER,
.OP_LESS,
.OP_ADD_F,
.OP_ADD_I,
.OP_ADD_STRING,
.OP_ADD_LIST,
.OP_ADD_MAP,
.OP_SUBTRACT_I,
.OP_SUBTRACT_F,
.OP_MULTIPLY_I,
.OP_MULTIPLY_F,
.OP_DIVIDE_I,
.OP_DIVIDE_F,
.OP_NOT,
.OP_NEGATE_I,
.OP_NEGATE_F,
.OP_BAND,
.OP_BOR,
.OP_XOR,
.OP_BNOT,
.OP_SHL,
.OP_SHR,
.OP_MOD_I,
.OP_MOD_F,
.OP_UNWRAP,
.OP_GET_ENUM_CASE_VALUE,
.OP_SET_LIST_SUBSCRIPT,
.OP_SET_MAP_SUBSCRIPT,
.OP_THROW,
.OP_IMPORT,
.OP_TO_STRING,
.OP_INSTANCE,
.OP_FCONTAINER_INSTANCE,
.OP_STRING_FOREACH,
.OP_LIST_FOREACH,
.OP_ENUM_FOREACH,
.OP_MAP_FOREACH,
.OP_FIBER_FOREACH,
.OP_RANGE_FOREACH,
.OP_RESUME,
.OP_YIELD,
.OP_RESOLVE,
.OP_TRY_END,
.OP_GET_ENUM_CASE_FROM_VALUE,
.OP_TYPEOF,
.OP_HOTSPOT_CALL,
.OP_CLONE,
.OP_CLOSE_UPVALUE,
.OP_RETURN,
.OP_RANGE,
.OP_FIBER,
=> simpleInstruction(instruction, offset),
.OP_SWAP => bytesInstruction(instruction, chunk, offset),
.OP_DEFINE_GLOBAL,
.OP_GET_GLOBAL,
.OP_SET_GLOBAL,
.OP_GET_LOCAL,
.OP_SET_LOCAL,
.OP_GET_UPVALUE,
.OP_SET_UPVALUE,
.OP_GET_ENUM_CASE,
.OP_EXPORT,
.OP_LIST_APPEND,
.OP_SET_MAP,
.OP_PROPERTY,
.OP_OBJECT_DEFAULT,
.OP_SET_OBJECT_PROPERTY,
.OP_SET_INSTANCE_PROPERTY,
.OP_GET_INSTANCE_PROPERTY,
.OP_GET_INSTANCE_METHOD,
.OP_GET_LIST_PROPERTY,
.OP_GET_MAP_PROPERTY,
.OP_GET_FIBER_PROPERTY,
.OP_GET_RANGE_PROPERTY,
.OP_GET_STRING_PROPERTY,
.OP_GET_PATTERN_PROPERTY,
.OP_GET_OBJECT_PROPERTY,
.OP_GET_LIST_SUBSCRIPT,
.OP_GET_STRING_SUBSCRIPT,
.OP_GET_MAP_SUBSCRIPT,
.OP_GET_FCONTAINER_INSTANCE_PROPERTY,
.OP_SET_FCONTAINER_INSTANCE_PROPERTY,
.OP_COPY,
.OP_DBG_LOCAL_EXIT,
=> byteInstruction(instruction, chunk, offset),
.OP_OBJECT,
.OP_LIST,
.OP_MAP,
.OP_GET_PROTOCOL_METHOD,
.OP_CONSTANT,
=> constantInstruction(instruction, chunk, offset),
.OP_JUMP,
.OP_JUMP_IF_FALSE,
.OP_JUMP_IF_NOT_NULL,
.OP_HOTSPOT,
.OP_TRY,
=> jumpInstruction(instruction, chunk, true, offset),
.OP_LOOP => jumpInstruction(instruction, chunk, false, offset),
.OP_CALL_INSTANCE_PROPERTY,
.OP_TAIL_CALL_INSTANCE_PROPERTY,
.OP_INSTANCE_INVOKE,
.OP_INSTANCE_TAIL_INVOKE,
.OP_STRING_INVOKE,
.OP_PATTERN_INVOKE,
.OP_FIBER_INVOKE,
.OP_LIST_INVOKE,
.OP_MAP_INVOKE,
.OP_RANGE_INVOKE,
=> invokeInstruction(instruction, chunk, offset),
.OP_PROTOCOL_INVOKE,
.OP_PROTOCOL_TAIL_INVOKE,
=> namedInvokeInstruction(instruction, chunk, offset),
.OP_CALL,
.OP_TAIL_CALL,
=> triInstruction(instruction, chunk, offset),
.OP_DBG_GLOBAL_DEFINE => {
const slot = chunk.code.items[offset + 1];
const constant: u24 = @intCast(chunk.code.items[offset + 2]);
const value_str = chunk.constants.items[constant].toStringAlloc(global_allocator) catch @panic("Out of memory");
defer global_allocator.free(value_str);
print(
vm_io,
"OP_DBG_GLOBAL_DEFINE\t{} {} {s}",
.{
slot,
constant,
value_str,
},
);
return offset + 3;
},
.OP_DBG_LOCAL_ENTER => {
const arg_instruction = chunk.code.items[offset + 1];
const slot: u8 = @intCast(arg_instruction >> 24);
const constant: u24 = @intCast(0x00ffffff & arg_instruction);
const value_str = chunk.constants.items[constant].toStringAlloc(global_allocator) catch @panic("Out of memory");
defer global_allocator.free(value_str);
print(
vm_io,
"OP_DBG_LOCAL_ENTER\t{} {} {s}",
.{
slot,
constant,
value_str,
},
);
return offset + 2;
},
.OP_CLOSURE => closure: {
const constant: u24 = arg;
var off_offset: usize = offset + 1;
var value_str = chunk.constants.items[constant].toStringAlloc(global_allocator) catch @panic("Out of memory");
defer global_allocator.free(value_str);
print(
vm_io,
"{s}\t{} {s}",
.{
@tagName(instruction),
constant,
value_str[0..@min(value_str.len, 100)],
},
);
const function: *obj.ObjFunction = obj.ObjFunction.cast(chunk.constants.items[constant].obj()).?;
var i: u8 = 0;
while (i < function.upvalue_count) : (i += 1) {
const is_local: bool = chunk.code.items[off_offset] == 1;
off_offset += 1;
const index: u8 = @intCast(chunk.code.items[off_offset]);
off_offset += 1;
print(
vm_io,
"\n{:0>3} | \t{s} {}\n",
.{
off_offset - 2,
if (is_local) "local " else "upvalue",
index,
},
);
}
break :closure off_offset;
},
};
}
pub const DumpState = struct {
const Self = @This();
vm: *VM,
seen: std.AutoHashMapUnmanaged(*obj.Obj, void) = .empty,
depth: usize = 0,
tab: usize = 0,
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
self.seen.deinit(allocator);
}
fn writeIndent(state: *DumpState, out: anytype) void {
for (0..state.tab) |_| {
out.writeAll(" ") catch unreachable;
}
}
pub fn valueDump(state: *DumpState, value: Value, out: anytype, same_line: bool) void {
if (state.depth > 50) {
out.print("...", .{}) catch unreachable;
return;
}
state.depth += 1;
if (!same_line) {
state.writeIndent(out);
}
if (value.isNull()) {
out.print("null", .{}) catch unreachable;
} else if (!value.isObj() or state.seen.get(value.obj()) != null) {
const string = value.toStringAlloc(state.vm.gc.allocator) catch unreachable;
defer state.vm.gc.allocator.free(string);
out.print("{s}", .{string}) catch unreachable;
} else {
state.seen.put(state.vm.gc.allocator, value.obj(), {}) catch unreachable;
switch (value.obj().obj_type) {
.Type,
.Closure,
.Function,
.Bound,
.Native,
.UserData,
.Fiber,
.EnumInstance,
=> {
const string = value.toStringAlloc(state.vm.gc.allocator) catch unreachable;
defer state.vm.gc.allocator.free(string);
out.print("{s}", .{string}) catch unreachable;
},
.UpValue => {
const upvalue = obj.ObjUpValue.cast(value.obj()).?;
state.valueDump(
if (upvalue.closed != null)
upvalue.closed.?
else
upvalue.location.*,
out,
false,
);
},
.String => {
const string = obj.ObjString.cast(value.obj()).?;
out.print("\"{s}\"", .{string.string}) catch unreachable;
},
.Pattern => {
const pattern = obj.ObjPattern.cast(value.obj()).?;
out.print("$\"{s}\"", .{pattern.source}) catch unreachable;
},
.Range => {
const range = obj.ObjRange.cast(value.obj()).?;
out.print("{}..{}", .{ range.low, range.high }) catch unreachable;
},
.List => {
const list = obj.ObjList.cast(value.obj()).?;
const has_items = list.items.items.len > 0;
out.print(
"{s}[{s}",
.{
if (list.type_def.resolved_type.?.List.mutable)
"mut "
else
"",
if (has_items)
"\n"
else
"",
},
) catch unreachable;
state.tab += 1;
for (list.items.items) |item| {
state.valueDump(
item,
out,
false,
);
out.print(",\n", .{}) catch unreachable;
}
state.tab -= 1;
if (has_items) {
state.writeIndent(out);
}
out.print("]", .{}) catch unreachable;
},
.Map => {
const map = obj.ObjMap.cast(value.obj()).?;
const has_items = map.map.count() > 0;
out.print(
"{s}{{{s}",
.{
if (map.type_def.resolved_type.?.Map.mutable)
"mut "
else
"",
if (has_items)
"\n"
else
"",
},
) catch unreachable;
state.tab += 1;
var it = map.map.iterator();
while (it.next()) |kv| {
const key = kv.key_ptr.*;
state.valueDump(
key,
out,
false,
);
out.writeAll(": ") catch unreachable;
state.valueDump(
kv.value_ptr.*,
out,
true,
);
out.writeAll(",\n") catch unreachable;
}
state.tab -= 1;
if (has_items) {
state.writeIndent(out);
}
out.print("}}", .{}) catch unreachable;
},
.Enum => {
const enumeration = obj.ObjEnum.cast(value.obj()).?;
const enum_type_def = enumeration.type_def.resolved_type.?.Enum;
const enum_value_type_def = enumeration.type_def.resolved_type.?.Enum.enum_type.toStringAlloc(state.vm.gc.allocator, true) catch unreachable;
defer state.vm.gc.allocator.free(enum_value_type_def);
out.print(
"enum<{s}> {s} {{\n",
.{
enum_value_type_def,
enum_type_def.name.string,
},
) catch unreachable;
state.tab += 1;
for (enum_type_def.cases, 0..) |case, i| {
out.print(" {s} = ", .{case}) catch unreachable;
state.valueDump(
enumeration.cases[i],
out,
true,
);
out.writeAll(",\n") catch unreachable;
}
state.tab -= 1;
out.print("}}", .{}) catch unreachable;
},
.Object => {
const object = obj.ObjObject.cast(value.obj()).?;
const object_def = object.type_def.resolved_type.?.Object;
out.print("object", .{}) catch unreachable;
if (object_def.conforms_to.count() > 0) {
out.print("<", .{}) catch unreachable;
var it = object_def.conforms_to.iterator();
while (it.next()) |kv| {
out.print("{s}, ", .{kv.key_ptr.*.resolved_type.?.Protocol.name.string}) catch unreachable;
}
out.print(">", .{}) catch unreachable;
}
out.print(" {s} {{\n", .{object_def.name.string}) catch unreachable;
state.tab += 1;
var it = object_def.fields.iterator();
while (it.next()) |kv| {
const field = kv.value_ptr.*;
const field_type_str = field.type_def.toStringAlloc(state.vm.gc.allocator, true) catch unreachable;
defer state.vm.gc.allocator.free(field_type_str);
if (!field.method) {
out.print(
" {s}{s}{s}: {s}",
.{
if (kv.value_ptr.*.static) "static " else "",
if (kv.value_ptr.*.final) "final " else "",
kv.key_ptr.*,
field_type_str,
},
) catch unreachable;
if (if (field.static)
object.fields[field.index]
else if (field.has_default)
object.defaults[field.index]
else
null) |v|
{
out.print(" = ", .{}) catch unreachable;
state.valueDump(
v,
out,
true,
);
}
out.print(",\n", .{}) catch unreachable;
} else {
out.print(
" {s}{s}{s}\n",
.{
if (field.static) "static " else "",
if (field.mutable) "mut " else "",
field_type_str,
},
) catch unreachable;
}
}
state.tab -= 1;
out.print("}}", .{}) catch unreachable;
},
.ObjectInstance => {
const object_instance = obj.ObjObjectInstance.cast(value.obj()).?;
const fields = object_instance.type_def.resolved_type.?.ObjectInstance.of
.resolved_type.?.Object
.fields;
out.print(
"{s}{s}{{\n",
.{
if (object_instance.type_def.resolved_type.?.ObjectInstance.mutable)
"mut "
else
"",
if (object_instance.object) |object|
object.type_def.resolved_type.?.Object.name.string
else
".",
},
) catch unreachable;
state.tab += 1;
for (object_instance.fields, 0..) |val, idx| {
out.print(
" {s} = ",
.{
fields.keys()[idx],
},
) catch unreachable;
state.valueDump(
val,
out,
true,
);
out.print(",\n", .{}) catch unreachable;
}
state.tab -= 1;
out.print("}}", .{}) catch unreachable;
},
.ForeignContainer => {
const foreign = obj.ObjForeignContainer.cast(value.obj()).?;
const foreign_def = foreign.type_def.resolved_type.?.ForeignContainer;
out.print(
"{s}{{\n",
.{foreign_def.name.string},
) catch unreachable;
var it = foreign_def.fields.iterator();
while (it.next()) |kv| {
out.print(" {s} = ", .{kv.key_ptr.*}) catch unreachable;
state.valueDump(
kv.value_ptr.*.getter(
state.vm,
foreign.data.ptr,
),
out,
true,
);
out.print(",\n", .{}) catch unreachable;
}
out.print("}}", .{}) catch unreachable;
},
}
_ = state.seen.remove(value.obj());
}
state.depth -= 1;
}
};