Mercurial > repos > tabletprog
annotate modules/x86.tp @ 180:270d31c6c4cd
Add support for jmps and labels in x86 module
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 24 Aug 2013 15:08:00 -0700 |
parents | 75aca5f87969 |
children | f188723c15b4 |
rev | line source |
---|---|
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 { |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
2 regnames <- #["rax" "rcx" "rdx" "rbx" "rsp" "rbp" "rsi" "rdi" "r8" "r9" "r10" "r11" "r12" "r13" "r14" "r15"] |
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
3 uppernames <- #["ah" "ch" "dh" "bh"] |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 ireg <- :regnum { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #{ |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 num <- { regnum } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 reg <- { regnum and 7u8} |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
8 string <- { regnames get: regnum } |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 rm <- :tail { reg or 0xC0u8 | tail } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 validforSize? <- :size { true } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 isInteger? <- { false } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 register? <- { true } |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
13 localLabel? <- { false } |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 upper? <- { true } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 needsRex? <- { regnum >= 8u8 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 rexBitReg <- { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 if: needsRex? { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 4u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 } else: { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 0u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 rexBitRM <- { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 if: needsRex? { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 1u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 } else: { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 0u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 = <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 (not: (other isInteger?)) && (other register?) && (not: (other upper?)) && regnum = (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 upper <- :regnum { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 #{ |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 num <- { regnum } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 reg <- { regnum } |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
40 string <- { uppernames get: regnum - 4 } |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 rm <- :tail { regnum or 0xC0u8 | tail } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 validforSize? <- :size { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 size = byte |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 isInteger? <- { false } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 register? <- { true } |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
47 localLabel? <- { false } |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 upper? <- { true } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 needsRex? <- { false } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 = <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 (not: (other isInteger?)) && (other register?) && (other upper?) && regnum = (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 fakesrc <- #{ |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 needsRex? <- { false } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 rexBitReg <- { 0u8 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 rexBitRM <- { 0u8 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 size <- :s { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 #{ |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 num <- { s } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 = <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 s = (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 > <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 s > (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 >= <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 s >= (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 < <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 s < (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 <= <- :other { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 s <= (other num) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 needsRex? <- { s = 3 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 rexBit <- { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 if: needsRex? { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 0x08u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 } else: { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 0u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 byte <- size: 0 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 word <- size: 1 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 dword <- size: 2 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 qword <- size: 3 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 size_bit <- :opcode size { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 if: size = byte { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 opcode |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 } else: { |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
97 opcode or 1u8 |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 opex <- :val { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 #{ |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 reg <- { val } |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
103 string <- { "opex " . val} |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 mod_rm:withTail <- :register regmem :end { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 l <- regmem rm: end |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
109 (l value) or ( lshift: (register reg) by: 3u8) | (l tail) |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 mod_rm <- :reg rm { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 mod_rm: reg rm withTail: [] |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
116 int_op:withTail <- :value size :tail { |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 if: size >= dword { |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
118 tail <- (uint8: (rshift: value by: 16u64)) | (uint8: (rshift: value by: 24u64)) | tail |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 if: size >= word { |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
121 tail <- (uint8: (rshift: value by: 8u64)) | tail |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 (uint8: value) | tail |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 } |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
125 int_op <- :value size { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
126 int_op: value size withTail: [] |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
127 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
128 //used for mov instructions that support 64-bit immediate operands/offsets |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
129 int_op64 <- :value size { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
130 tail <- [] |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
131 if: size = qword { |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
132 tail <- (uint8: (rshift: value by: 32u64)) | (uint8: (rshift: value by: 40u64)) | (uint8: (rshift: value by: 48u64)) | (uint8: (rshift: value by: 56u64)) | tail |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
133 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
134 int_op: value size withTail: tail |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
135 } |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 prefix:withInstruction <- :reg rm size :inst { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 if: size = word { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 inst <- 0x66u8 | inst |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 if: (size needsRex?) || (reg needsRex?) || (rm needsRex?) { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 rex <- 0x40u8 or (size rexBit) or (reg rexBitReg) or (rm rexBitRM) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 inst <- rex | inst |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 inst |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 _rax <- ireg: 0u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 _rcx <- ireg: 1u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 _rdx <- ireg: 2u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 _rbx <- ireg: 3u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 _rsp <- ireg: 4u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 _rbp <- ireg: 5u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 _rsi <- ireg: 6u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 _rdi <- ireg: 7u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 _r8 <- ireg: 8u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 _r9 <- ireg: 9u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 _r10 <- ireg: 10u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 _r11 <- ireg: 11u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 _r12 <- ireg: 12u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 _r13 <- ireg: 13u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 _r14 <- ireg: 14u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 _r15 <- ireg: 15u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 _ah <- upper: 4u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 _ch <- upper: 5u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 _dh <- upper: 6u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 _bh <- upper: 7u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
169 inst <- :ilist { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
170 #{ |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
171 length <- { ilist length } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
172 flattenTo:at <- :dest :idx { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
173 ilist fold: idx with: :idx byte { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
174 dest set: idx byte |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
175 idx + 1 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
176 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
177 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
178 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
179 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
180 |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
181 op:withCode:withImmed:withOpEx <- :src dst size :normal :immed :myopex { |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 reg <- src |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 rm <- dst |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 base <- if: (src isInteger?) { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 reg <- fakesrc |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
186 (size_bit: immed size) | (mod_rm: (opex: myopex) dst withTail: (int_op: src size)) |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 } else: { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 if: (src register?) { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 (size_bit: normal size) | (mod_rm: src dst) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 } else: { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 reg <- dst |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 rm <- src |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 (size_bit: normal or 0x02u8 size) | (mod_rm: dst src) |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 } |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
196 inst: (prefix: reg rm size withInstruction: base) |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
199 op:withCode:withImmed:withImmedRax:withOpEx:withByteExtend <- :src dst size :normal :immed :immedRax :myopex :byteExt { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
200 reg <- src |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
201 rm <- dst |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
202 if: (src isInteger?) { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
203 reg <- fakesrc |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
204 base <- if: size > byte && (((src signed?) && src < 128 && src >= -128) || ((not: (src signed?)) && src < 256)) { |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
205 byteExt | (mod_rm: (opex: myopex) dst withTail: [(uint8: src)]) |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
206 } else: { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
207 if: dst = _rax { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
208 (size_bit: immedRax size) | (int_op: src size) |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
209 } else: { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
210 (size_bit: immed size) | (mod_rm: (opex: myopex) dst withTail: (int_op: src size)) |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
211 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
212 } |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
213 inst: (prefix: reg rm size withInstruction: base) |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
214 } else: { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
215 op: src dst size withCode: normal withImmed: immed withOpEx: myopex |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
216 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
217 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
218 |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 #{ |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 rax <- { _rax } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 rcx <- { _rcx } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 rdx <- { _rdx } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 rbx <- { _rbx } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
224 rsp <- { _rsp } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
225 rbp <- { _rbp } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
226 rsi <- { _rsi } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
227 rdi <- { _rdi } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
228 r8 <- { _r8 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
229 r9 <- { _r9 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
230 r10 <- { _r10 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
231 r11 <- { _r11 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
232 r12 <- { _r12 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
233 r13 <- { _r13 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
234 r14 <- { _r14 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
235 r15 <- { _r15 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
236 ah <- { _ah } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
237 ch <- { _ch } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
238 dh <- { _dh } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 bh <- { _bh } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
240 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
241 b <- { byte } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
242 w <- { word } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
243 d <- { dword } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
244 q <- { qword } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
245 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
246 add <- :src dst size { |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
247 op: src dst size withCode: 0u8 withImmed: 0x80u8 withImmedRax: 0x04u8 withOpEx: 0u8 withByteExtend: 0x83u8 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
248 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
249 |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
250 sub <- :src dst size { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
251 op: src dst size withCode: 0x28u8 withImmed: 0x80u8 withImmedRax: 0x2Cu8 withOpEx: 5u8 withByteExtend: 0x83u8 |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
252 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
253 |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
254 mov <- :src dst size { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
255 reg <- src |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
256 rm <- dst |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
257 if: (src isInteger?) && (dst register?) { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
258 opval <- if: size = byte { 0xB0u8 } else: { 0xB8u8 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
259 base <- opval | (int_op64: src size) |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
260 inst: (prefix: fakesrc rm size withInstruction: base) |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
261 } else: { |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
262 op: src dst size withCode: 0x88u8 withImmed: 0xC6u8 withOpEx: 0u8 |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
263 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
264 } |
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
265 |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
266 ret <- { inst: [ 0xC3u8 ] } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
267 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
268 label <- { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
269 _offset <- -1 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
270 _forwardRefs <- #[] |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
271 #{ |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
272 length <- { 0 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
273 hasOffset? <- { _offset >= 0 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
274 offset <- { _offset } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
275 register? <- { false } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
276 localLabel? <- { true } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
277 flattenTo:at <- :dest :idx { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
278 if: (not: hasOffset?) { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
279 _offset <- idx |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
280 foreach: _forwardRefs :idx fun { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
281 fun: _offset |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
282 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
283 _forwardRefs <- #[] |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
284 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
285 idx |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
286 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
287 withOffset:else <- :fun :elsefun { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
288 if: hasOffset? { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
289 fun: _offset |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
290 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
291 _forwardRefs append: fun |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
292 elsefun: |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
293 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
294 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
295 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
296 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
297 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
298 jmp <- :jmpDest { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
299 if: (jmpDest localLabel?) { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
300 _size <- -1 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
301 #{ |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
302 length <- { if: _size < 0 { 5 } else: { _size } } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
303 flattenTo:at <- :dest :idx { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
304 jmpDest withOffset: :off { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
305 if: _size < 0 { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
306 rel <- off - (idx + 2) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
307 if: rel < 128 && rel >= -128 { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
308 _size <- 2 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
309 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
310 rel <- rel - 2 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
311 if: rel < 32768 && rel >= -32768 { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
312 _size <- 4 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
313 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
314 _size <- 5 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
315 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
316 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
317 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
318 rel <- off - (idx + _size) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
319 if: _size = 2 { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
320 dest set: idx 0xEBu8 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
321 dest set: (idx + 1) (uint8: rel) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
322 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
323 if: _size = 4 { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
324 dest set: idx 0x66u8 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
325 dest set: (idx + 1) 0xE9u8 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
326 dest set: (idx + 2) (uint8: rel) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
327 dest set: (idx + 3) (uint8: (rshift: rel by: 8)) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
328 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
329 dest set: idx 0xE9u8 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
330 dest set: (idx + 1) (uint8: rel) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
331 dest set: (idx + 2) (uint8: (rshift: rel by: 8)) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
332 dest set: (idx + 3) (uint8: (rshift: rel by: 16)) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
333 dest set: (idx + 4) (uint8: (rshift: rel by: 24)) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
334 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
335 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
336 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
337 _size <- 5 |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
338 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
339 idx + _size |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
340 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
341 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
342 } else: { |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
343 inst: 0xFFu8 | (mod_rm: (opex: 5u8) jmpDest) |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
344 } |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
345 } |
175
20b6041a8b23
Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents:
174
diff
changeset
|
346 |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
347 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
348 main <- { |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
349 foo <- label: |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
350 bar <- label: |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
351 prog <- #[ |
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
352 mov: rdi rax q |
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
353 sub: 1 rdi q |
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
354 add: rdi rax q |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
355 jmp: bar |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
356 foo |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
357 ret: |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
358 bar |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
359 sub: 13 rax q |
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
360 jmp: foo |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
361 ] |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
179
diff
changeset
|
362 |
179
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
363 ba <- bytearray executableFromBytes: prog |
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
364 res <- ba runWithArg: 24u64 |
75aca5f87969
A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents:
175
diff
changeset
|
365 print: (string: res) . "\n" |
174
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
366 0 |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
367 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
368 } |
8b5829372ad1
Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
369 } |