| @@ -5,6 +5,7 @@ import sys · 4 unmodified lines |
| 5 | import dis | 5 | import dis |
| 6 | import opcodes | 6 | import opcodes |
| 7 | from opcodes import Imm, LOfp, LOmp, NoOp, SOfp, SOmp, SOSOfp, SOSOmp | 7 | from opcodes import Imm, LOfp, LOmp, NoOp, SOfp, SOmp, SOSOfp, SOSOmp |
| | 8 | from utils import hash_signature |
| 8 | | 9 | |
| 9 | if __name__ == "__main__": | 10 | if __name__ == "__main__": |
| 10 | | 11 | |
| @@ -19,28 +20,53 @@ if __name__ == "__main__": · 8 unmodified lines |
| 19 | d.entry_pc = 0 | 20 | d.entry_pc = 0 |
| 20 | d.entry_type = 2 # refers to Type 2 | 21 | d.entry_type = 2 # refers to Type 2 |
| 21 | | 22 | |
| | 23 | # implement count; |
| | 24 | # |
| | 25 | # include "sys.m"; |
| | 26 | # include "draw.m"; |
| | 27 | # |
| | 28 | # count: module |
| | 29 | # { |
| | 30 | # init: fn(ctxt: ref Draw->Context, args: list of string); |
| | 31 | # }; |
| | 32 | # |
| | 33 | # init(ctxt: ref Draw->Context, args: list of string) |
| | 34 | # { |
| | 35 | # sys := load Sys Sys->PATH; |
| | 36 | # sys->print("Counting...\n"); |
| | 37 | # for (i := 0; i < 20; i++) |
| | 38 | # sys->print("%d\n", i); |
| | 39 | # } |
| | 40 | |
| 22 | d.code = [ | 41 | d.code = [ |
| 23 | opcodes.load(LOmp(0), Imm(0), LOfp(44)), | 42 | opcodes.load(LOmp(0), Imm(0), LOfp(44)), # "$Sys", ldt[0] -> module |
| 24 | opcodes.frame(Imm(1), LOfp(52)), | 43 | |
| 25 | opcodes.movp(LOmp(8), SOSOfp(52, 32)), | 44 | opcodes.frame(Imm(1), LOfp(52)), # types[1] -> new_frame1 (ptr) |
| 26 | opcodes.lea(LOfp(48), SOSOfp(52, 16)), | 45 | opcodes.movp(LOmp(8), SOSOfp(52, 32)), # "Counting..." -> |
| 27 | opcodes.mcall(LOfp(52), Imm(0), LOfp(44)), | 46 | opcodes.lea(LOfp(48), SOSOfp(52, 16)), # |
| 28 | opcodes.movw(Imm(0), LOfp(40)), | 47 | opcodes.mcall(LOfp(52), Imm(0), LOfp(44)), # new_frame1, ldt[0], module |
| 29 | opcodes.blew(Imm(0x14), SOfp(40), Imm(0xe)), | 48 | |
| 30 | opcodes.frame(Imm(1), LOfp(48)), | 49 | opcodes.movw(Imm(0), LOfp(40)), # 0 -> i (int) |
| 31 | opcodes.movp(LOmp(4), SOSOfp(48, 32)), | 50 | opcodes.blew(Imm(0x14), SOfp(40), Imm(0xe)),# 0x6: if 20 <= i branch to 0xe |
| | 51 | |
| | 52 | opcodes.frame(Imm(1), LOfp(48)), # types[1] -> new_frame2 (ptr) |
| | 53 | opcodes.movp(LOmp(4), SOSOfp(48, 32)), # "%d\n" -> |
| 32 | opcodes.movw(LOfp(40), SOSOfp(48, 36)), | 54 | opcodes.movw(LOfp(40), SOSOfp(48, 36)), |
| 33 | opcodes.lea(LOfp(52), SOSOfp(48, 16)), | 55 | opcodes.lea(LOfp(52), SOSOfp(48, 16)), # new_frame1 -> |
| 34 | opcodes.mcall(LOfp(48), Imm(0), LOfp(44)), | 56 | opcodes.mcall(LOfp(48), Imm(0), LOfp(44)), # new_frame2, ldt[0] |
| 35 | opcodes.addw(Imm(1), NoOp(), LOfp(40)), | 57 | |
| 36 | opcodes.jmp(Imm(6)), | 58 | opcodes.addw(Imm(1), NoOp(), LOfp(40)), # 1 + [i] -> i |
| 37 | opcodes.ret() | 59 | opcodes.jmp(Imm(6)), # jump to 0x6 |
| | 60 | opcodes.ret() # 0xe: return |
| 38 | ] | 61 | ] |
| 39 | | 62 | |
| 40 | d.types = [ | 63 | d.types = [ |
| 41 | dis.Type(0, 16, 1, "\xf0"), | 64 | dis.Type(0, 16, 1, "\xf0"), |
| 42 | dis.Type(1, 40, 2, "\x00\x80"), | 65 | dis.Type(1, 40, 2, "\x00\x80"), |
| 43 | dis.Type(2, 56, 2, "\x00\xd0") # stack frame type, entry type | 66 | # Type 2 is the stack frame type (see below), entry type (see above), |
| | 67 | # 56 bytes (14 words) in size: |
| | 68 | # pointer bitmap: 0 0 0 0 0 0 0 0 | 1 1 0 1 0 0 [0 0] |
| | 69 | dis.Type(2, 56, 2, "\x00\xd0") |
| 44 | ] | 70 | ] |
| 45 | | 71 | |
| 46 | d.data = { | 72 | d.data = { |
| @@ -53,7 +79,7 @@ if __name__ == "__main__": · 6 unmodified lines |
| 53 | d.module_name = "count" | 79 | d.module_name = "count" |
| 54 | d.link = [dis.Link(0, 2, 0x4244b354, "init")] # refers to Type 2 | 80 | d.link = [dis.Link(0, 2, 0x4244b354, "init")] # refers to Type 2 |
| 55 | d.initialised_globals = 1 | 81 | d.initialised_globals = 1 |
| 56 | d.ldt = [[dis.LDT(0xac849033, "print")]] | 82 | d.ldt = [[dis.LDT(hash_signature("f*(s)i"), "print")]] |
| 57 | d.path = sys.argv[0] | 83 | d.path = sys.argv[0] |
| 58 | | 84 | |
| 59 | d.write(open(sys.argv[1], "w")) | 85 | d.write(open(sys.argv[1], "w")) |