annotate modules/x86.tp @ 377:93c28eee141e default tip

Merge
author Michael Pavone <pavone@retrodev.com>
date Sat, 15 Aug 2015 22:45:33 -0700
parents e44f65abaf0e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
12 isString? <- { false }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 register? <- { true }
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
14 label? <- { false }
198
3606a7cb3999 Fix ireg upper, regSource returnAll and regSource needSaveForCall in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 195
diff changeset
15 upper? <- { false }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 needsRex? <- { regnum >= 8u8 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 rexBitReg <- {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 if: needsRex? {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 4u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 } else: {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 0u8
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 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 rexBitRM <- {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 if: needsRex? {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 1u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 } else: {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 0u8
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 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 = <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 (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
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
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 upper <- :regnum {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 #{
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 num <- { regnum }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 reg <- { regnum }
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
41 string <- { uppernames get: regnum - 4 }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 rm <- :tail { regnum or 0xC0u8 | tail }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 validforSize? <- :size {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 size = byte
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 isInteger? <- { false }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 register? <- { true }
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
48 label? <- { false }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 upper? <- { true }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 needsRex? <- { false }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 = <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 (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
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 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 fakesrc <- #{
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 needsRex? <- { false }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 rexBitReg <- { 0u8 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 rexBitRM <- { 0u8 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
61 _size <- :s {
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 #{
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 num <- { s }
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
64 bytes <- { lshift: 1 by: s }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 = <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 s = (other num)
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 > <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 s > (other num)
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 >= <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 s >= (other num)
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 < <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 s < (other num)
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 <= <- :other {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 s <= (other num)
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 needsRex? <- { s = 3 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 rexBit <- {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 if: needsRex? {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 0x08u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 } else: {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 0u8
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 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
90 byte <- _size: 0
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
91 word <- _size: 1
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
92 dword <- _size: 2
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
93 qword <- _size: 3
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
95 condition <- :num {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
96 #{
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
97 cc <- { num }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
98 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
99 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
100 _o <- condition: 0u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
101 _no <- condition: 1u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
102 _c <- condition: 2u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
103 _nc <- condition: 3u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
104 _z <- condition: 4u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
105 _nz <- condition: 5u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
106 _be <- condition: 6u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
107 _nbe <- condition: 7u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
108 _s <- condition: 8u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
109 _ns <- condition: 9u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
110 _p <- condition: 10u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
111 _np <- condition: 11u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
112 _l <- condition: 12u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
113 _nl <- condition: 13u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
114 _le <- condition: 14u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
115 _nle <- condition: 15u8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
116
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
117
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 size_bit <- :opcode size {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 if: size = byte {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 opcode
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 } else: {
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
122 opcode or 1u8
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 opex <- :val {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 #{
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 reg <- { val }
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
128 string <- { "opex " . val}
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 mod_rm:withTail <- :register regmem :end {
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
133 list <- regmem rm: end
359
023c29e1f595 Remove extraneous and from x86 module
Michael Pavone <pavone@retrodev.com>
parents: 357
diff changeset
134 (list value) or ( lshift: (register reg) by: 3u8) | (list tail)
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 }
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 mod_rm <- :reg rm {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 mod_rm: reg rm withTail: []
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
141 int_op:withTail <- :value size :tail {
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
142 if: (not: (value isInteger?)) {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
143 //label
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
144 //FIXME: Needs implementation
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
145 value <- 0
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
146 }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 if: size >= dword {
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
148 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
149 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 if: size >= word {
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
151 tail <- (uint8: (rshift: value by: 8u64)) | tail
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 (uint8: value) | tail
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 }
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
155 int_op <- :value size {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
156 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
157 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
158 //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
159 int_op64 <- :value size {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
160 tail <- []
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
161 value <- uint64: value
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
162 if: size = qword {
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
163 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
164 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
165 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
166 }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 prefix:withInstruction <- :reg rm size :inst {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 if: size = word {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 inst <- 0x66u8 | inst
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 if: (size needsRex?) || (reg needsRex?) || (rm needsRex?) {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 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
174 inst <- rex | inst
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 inst
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 _rax <- ireg: 0u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 _rcx <- ireg: 1u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 _rdx <- ireg: 2u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 _rbx <- ireg: 3u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 _rsp <- ireg: 4u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
184 _rbp <- ireg: 5u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 _rsi <- ireg: 6u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 _rdi <- ireg: 7u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187 _r8 <- ireg: 8u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 _r9 <- ireg: 9u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 _r10 <- ireg: 10u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 _r11 <- ireg: 11u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 _r12 <- ireg: 12u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 _r13 <- ireg: 13u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 _r14 <- ireg: 14u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 _r15 <- ireg: 15u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 _ah <- upper: 4u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 _ch <- upper: 5u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 _dh <- upper: 6u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 _bh <- upper: 7u8
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
200 //AMD64 convention
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
201 _argregs <- #[
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
202 _rdi
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
203 _rsi
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
204 _rdx
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
205 _rcx
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
206 _r8
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
207 _r9
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
208 ]
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
209 _calleesave <- #[
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
210 _rbx
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
211 _rbp
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
212 _r12
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
213 _r13
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
214 _r14
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
215 _r15
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
216 ]
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
217 _tempregs <- #[
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
218 _r10
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
219 _r11
194
30bed95cbb18 Apply register assignments in il module
Mike Pavone <pavone@retrodev.com>
parents: 193
diff changeset
220 //TODO: Add rax back in once there's logic in il to properly
30bed95cbb18 Apply register assignments in il module
Mike Pavone <pavone@retrodev.com>
parents: 193
diff changeset
221 //allocate it for the instances in which it's live
30bed95cbb18 Apply register assignments in il module
Mike Pavone <pavone@retrodev.com>
parents: 193
diff changeset
222 //_rax
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
223 ]
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
224
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
225
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
226 inst <- :ilist {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
227 #{
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
228 length <- { ilist length }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
229 flattenTo:at <- :dest :idx {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
230 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
231 dest set: idx byte
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
232 idx + 1
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
233 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
234 }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
235 string <- { (ilist map: :el { hex: el}) join: " "}
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
236 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
237 }
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
238
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
239 labelinst <- :prefix label size {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
240 _length <- (prefix length) + (size bytes)
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
241 #{
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
242 length <- { _length }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
243 flattenTo:at <- :dest :idx {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
244 label withOffset: :off {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
245 writeBytes <- :idx byte {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
246 dest set: idx byte
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
247 idx + 1
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
248 }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
249 idx <- prefix fold: idx with: writeBytes
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
250 (int_op64: ((dest _buf_ptr) address) + (off uint64) size) fold: idx with: writeBytes
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
251 } else: {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
252 idx + _length
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
253 }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
254 }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
255 }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
256 }
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
257 multiInst <- :instarr {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
258 #{
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
259 length <- {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
260 instarr fold: 0 with: :acc inst {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
261 acc + (inst length)
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
262 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
263 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
264 flattenTo:at <- :dest :idx {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
265 instarr fold: idx with: :idx inst {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
266 inst flattenTo: dest at: idx
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
267 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
268 }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
269 string <- {
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
270 (instarr map: :inst {
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
271 (inst map: :el { hex: el}) join: " "
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
272 }) join: "\n"
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
273 }
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
274 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
275 }
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
276
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
277 data <- :bytes {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
278 #{
362
7101ad443081 Add null terminator to string literals in x86 module and fix a bug that interfered with use of function name labels in call instructions
Michael Pavone <pavone@retrodev.com>
parents: 361
diff changeset
279 length <- { (bytes byte_length) + 1 }
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
280 flattenTo:at <- :dest :idx {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
281 foreach: (range from: 0 to: length) :_ cidx {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
282 dest set: idx + cidx (bytes byte: cidx)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
283 }
362
7101ad443081 Add null terminator to string literals in x86 module and fix a bug that interfered with use of function name labels in call instructions
Michael Pavone <pavone@retrodev.com>
parents: 361
diff changeset
284 //NULL terminator
7101ad443081 Add null terminator to string literals in x86 module and fix a bug that interfered with use of function name labels in call instructions
Michael Pavone <pavone@retrodev.com>
parents: 361
diff changeset
285 dest set: idx + (bytes byte_length) 0u8
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
286 idx + length
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
287 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
288 string <- {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
289 "data: " . bytes
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
290 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
291 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
292 }
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
293
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
294 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
295 reg <- src
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
296 rm <- dst
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
297 if: (not: (src isInteger?)) && (src label?) {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
298 //most instructions only support 32-bit immediates
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
299 if: size = qword {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
300 size <- dword
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
301 }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
302 labelinst: (prefix: reg rm size withInstruction: (size_bit: immed size) | (mod_rm: (opex: myopex) dst)) src size
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
303 } else: {
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
304 base <- if: (src isInteger?) {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
305 reg <- fakesrc
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
306 (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
307 } else: {
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
308 if: (src register?) {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
309 (size_bit: normal size) | (mod_rm: src dst)
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
310 } else: {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
311 reg <- dst
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
312 rm <- src
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
313 (size_bit: normal or 0x02u8 size) | (mod_rm: dst src)
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
314 }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
315 }
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
316 inst: (prefix: reg rm size withInstruction: base)
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
317 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
318 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
319
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
320 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
321 reg <- src
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
322 rm <- dst
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
323 if: (src isInteger?) {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
324 reg <- fakesrc
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
325 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
326 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
327 } else: {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
328 if: dst = _rax {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
329 (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
330 } else: {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
331 (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
332 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
333 }
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
334 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
335 } else: {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
336 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
337 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
338 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
339
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
340 shiftRot:withOpEx <- :amount dst size :myopex {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
341 opcode <- 0u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
342 tail <- []
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
343 pre <- #[]
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
344 post <- #[]
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
345 base <- if: (amount isInteger?) {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
346 if: amount = 1 {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
347 opcode <- 0xD0u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
348 } else: {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
349 opcode <- 0xC0u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
350 tail <- [uint8: amount]
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
351 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
352 } else: {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
353 opcode <- 0xD2u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
354 if: (not: _rcx = amount) {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
355 pre <- #[
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
356 x86 push: _rcx
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
357 x86 mov: amount _rcx byte
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
358 ]
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
359 post <- #[
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
360 x86 pop: _rcx
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
361 ]
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
362 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
363 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
364 bytes <- prefix: fakesrc dst withInstruction: (size_bit: 0xC0u8 size) | (mod_rm: (opex: myopex) dst withTail: tail)
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
365 myinst <- inst: bytes
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
366 if: (pre length) > 0 {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
367 pre append: myinst
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
368 foreach: post :_ inst {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
369 pre append: inst
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
370 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
371 multiInst: pre
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
372 } else: {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
373 myinst
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
374 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
375 }
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
376 _dlHandle <- option none
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
377 _getDLHandle <- {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
378 _dlHandle value: :handle { handle } none: {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
379 handle <- dl open: "" withFlags: (dl NOW)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
380 _dlHandle <- option value: handle
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
381 handle
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
382 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
383 }
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
384
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
385 _jmprel <- :op jmpDest {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
386 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
387
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
388 #{
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
389 rax <- { _rax }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
390 rcx <- { _rcx }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
391 rdx <- { _rdx }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
392 rbx <- { _rbx }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
393 rsp <- { _rsp }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
394 rbp <- { _rbp }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
395 rsi <- { _rsi }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
396 rdi <- { _rdi }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
397 r8 <- { _r8 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
398 r9 <- { _r9 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
399 r10 <- { _r10 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
400 r11 <- { _r11 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
401 r12 <- { _r12 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
402 r13 <- { _r13 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
403 r14 <- { _r14 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
404 r15 <- { _r15 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
405 ah <- { _ah }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
406 ch <- { _ch }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
407 dh <- { _dh }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
408 bh <- { _bh }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
409
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
410 b <- { byte }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
411 w <- { word }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
412 d <- { dword }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
413 q <- { qword }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
414
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
415 o <- { _o }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
416 no <- { _no }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
417 c <- { _c }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
418 nc <- { _nc }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
419 ae <- { _nc }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
420 z <- { _z }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
421 e <- { _z }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
422 nz <- { _nz }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
423 ne <- { _nz }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
424 be <- { _be }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
425 nbe <- { _nbe }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
426 a <- { _nbe }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
427 s <- { _s }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
428 ns <- { _ns }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
429 p <- { _p }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
430 pe <- { _p }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
431 np <- { _np }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
432 po <- { _np }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
433 l <- { _l }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
434 nl <- { _nl }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
435 ge <- { _nl }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
436 le <- { _le }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
437 nle <- { _nle }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
438 g <- { _nle }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
439
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
440 add <- :src dst size {
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
441 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
442 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
443
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
444 sub <- :src dst size {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
445 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
446 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
447
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
448 cmp <- :src dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
449 op: src dst size withCode: 0x38u8 withImmed: 0x80u8 withImmedRax: 0x3Cu8 withOpEx: 7u8 withByteExtend: 0x83u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
450 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
451
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
452 and <- :src dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
453 op: src dst size withCode: 0x20u8 withImmed: 0x80u8 withImmedRax: 0x24u8 withOpEx: 4u8 withByteExtend: 0x83u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
454 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
455
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
456 or <- :src dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
457 op: src dst size withCode: 0x08u8 withImmed: 0x80u8 withImmedRax: 0x0Cu8 withOpEx: 1u8 withByteExtend: 0x83u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
458 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
459
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
460 xor <- :src dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
461 op: src dst size withCode: 0x30u8 withImmed: 0x80u8 withImmedRax: 0x34u8 withOpEx: 6u8 withByteExtend: 0x83u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
462 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
463
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
464 mov <- :src dst size {
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
465 rm <- dst
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
466 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
467 opval <- if: size = byte { 0xB0u8 } else: { 0xB8u8 }
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
468 base <- opval or (dst reg) | (int_op64: src size)
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
469 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
470 } else: {
364
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
471 if: (src label?) && (dst register?) {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
472 opval <- if: size = byte { 0xB0u8 } else: { 0xB8u8 }
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
473 labelinst: (prefix: fakesrc rm size withInstruction: [opval or (dst reg)]) src size
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
474 } else: {
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
475 op: src dst size withCode: 0x88u8 withImmed: 0xC6u8 withOpEx: 0u8
e44f65abaf0e Support labels in non-branch instructions. String literals now work and so does the llhello sample
Michael Pavone <pavone@retrodev.com>
parents: 363
diff changeset
476 }
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
477 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
478 }
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
479
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
480 shl <- :shift dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
481 shiftRot: shift dst size withOpEx: 4u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
482 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
483
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
484 shr <- :shift dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
485 shiftRot: shift dst size withOpEx: 5u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
486 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
487
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
488 sar <- :shift dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
489 shiftRot: shift dst size withOpEx: 7u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
490 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
491
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
492 rol <- :shift dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
493 shiftRot: shift dst size withOpEx: 0u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
494 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
495
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
496 ror <- :shift dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
497 shiftRot: shift dst size withOpEx: 1u8
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
498 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
499
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
500 ret <- { inst: [ 0xC3u8 ] }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
501
363
d949fe826e04 Unify il and backend labels
Michael Pavone <pavone@retrodev.com>
parents: 362
diff changeset
502 label <- { il label }
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
503
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
504 jmp <- :jmpDest {
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
505 if: (jmpDest label?) {
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
506 _size <- -1
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
507 #{
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
508 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
509 flattenTo:at <- :dest :idx {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
510 jmpDest withOffset: :off {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
511 if: _size < 0 {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
512 rel <- off - (idx + 2)
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
513 if: rel < 128 && rel >= -128 {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
514 _size <- 2
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
515 } else: {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
516 rel <- rel - 2
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
517 if: rel < 32768 && rel >= -32768 {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
518 _size <- 4
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
519 } else: {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
520 _size <- 5
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
521 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
522 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
523 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
524 rel <- off - (idx + _size)
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
525 if: _size = 2 {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
526 dest set: idx 0xEBu8
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
527 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
528 } else: {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
529 if: _size = 4 {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
530 dest set: idx 0x66u8
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
531 dest set: (idx + 1) 0xE9u8
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
532 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
533 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
534 } else: {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
535 dest set: idx 0xE9u8
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
536 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
537 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
538 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
539 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
540 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
541 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
542 } else: {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
543 _size <- 5
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
544 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
545 idx + _size
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
546 }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
547 string <- {
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
548 "jmp " . jmpDest
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
549 }
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
550 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
551 } else: {
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
552 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
553 }
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
554 }
175
20b6041a8b23 Small refactor in x86 module. Added a few more instructions.
Mike Pavone <pavone@retrodev.com>
parents: 174
diff changeset
555
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
556 jcc <- :cond jmpDest {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
557 _size <- -1
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
558 #{
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
559 length <- { if: _size < 0 { 5 } else: { _size } }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
560 flattenTo:at <- :dest :idx {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
561 jmpDest withOffset: :off {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
562 if: _size < 0 {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
563 rel <- off - (idx + 2)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
564 if: rel < 128 && rel >= -128 {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
565 _size <- 2
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
566 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
567 _size <- 6
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
568 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
569 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
570 rel <- off - (idx + _size)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
571 if: _size = 2 {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
572 dest set: idx 0x70u8 or (cond cc)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
573 dest set: (idx + 1) (uint8: rel)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
574 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
575 dest set: idx 0x0Fu8
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
576 dest set: (idx + 1) 0x80u8 or (cond cc)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
577 dest set: (idx + 2) (uint8: rel)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
578 dest set: (idx + 3) (uint8: (rshift: rel by: 8))
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
579 dest set: (idx + 4) (uint8: (rshift: rel by: 16))
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
580 dest set: (idx + 5) (uint8: (rshift: rel by: 24))
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
581 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
582 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
583 _size <- 6
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
584 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
585 idx + _size
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
586 }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
587 string <- {
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
588 "jcc " . jmpDest
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
589 }
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
590 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
591 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
592
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
593 call <- :callDest {
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
594 if: (callDest isInteger?) {
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
595 #{
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
596 length <- { 12 } //worst case
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
597 flattenTo:at <- :dest :idx {
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
598 base <- (dest _buf_ptr) address
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
599 rel <- (callDest uint64) - (base + (idx uint64) + 5u64)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
600 if: rel < 0x80000000u64 || rel >= 0xFFFFFFFF80000000u64 {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
601 rel <- rel int64
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
602 dest set: idx 0xE8u8
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
603 dest set: (idx + 1) (uint8: rel)
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
604 dest set: (idx + 2) (uint8: (rshift: rel by: 8))
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
605 dest set: (idx + 3) (uint8: (rshift: rel by: 16))
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
606 dest set: (idx + 4) (uint8: (rshift: rel by: 24))
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
607 idx + 5
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
608 } else: {
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
609 dst <- callDest uint64
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
610 dest set: idx 0x48u8 //REX size=quad
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
611 dest set: idx + 1 0xB8 //mov immed rax
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
612 dest set: idx + 2 (uint8: dst)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
613 dest set: idx + 3 (uint8: (rshift: dst by: 8))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
614 dest set: idx + 4 (uint8: (rshift: dst by: 16))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
615 dest set: idx + 5 (uint8: (rshift: dst by: 24))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
616 dest set: idx + 6 (uint8: (rshift: dst by: 32))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
617 dest set: idx + 7 (uint8: (rshift: dst by: 40))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
618 dest set: idx + 8 (uint8: (rshift: dst by: 48))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
619 dest set: idx + 9 (uint8: (rshift: dst by: 56))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
620 dest set: idx + 10 0xFFu8 //single EA op
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
621 dest set: idx + 11 0xd0u8 //call reg direct
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
622 idx + 12
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
623 }
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
624
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
625 }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
626 string <- {
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
627 "call " . callDest
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
628 }
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
629 }
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
630 } else: {
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
631 if: (callDest label?) {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
632 #{
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
633 length <- { 5 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
634 flattenTo:at <- :dest :idx {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
635 dest set: idx 0xE8u8
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
636 callDest withOffset: :off {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
637 rel <- off - (idx + 5)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
638 dest set: (idx + 1) (uint8: rel)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
639 dest set: (idx + 2) (uint8: (rshift: rel by: 8))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
640 dest set: (idx + 3) (uint8: (rshift: rel by: 16))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
641 dest set: (idx + 4) (uint8: (rshift: rel by: 24))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
642 } else: {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
643 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
644 idx + 5
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
645 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
646 string <- {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
647 "call " . callDest
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
648 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
649 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
650 } else: {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
651 inst: 0xFFu8 | (mod_rm: (opex: 2u8) callDest)
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
652 }
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
653 }
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
654 }
355
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
655
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
656 setcc <- :cond dst {
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
657 src <- opex: 0u8
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
658 base <- 0x0Fu8 | (0x90u8 or (cond cc)) | (mod_rm: (opex: 0u8) dst)
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
659
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
660 inst: (prefix: fakesrc dst b withInstruction: base)
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
661 }
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
662
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
663 push <- :src {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
664 if: (src isInteger?) {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
665 if: src < 128 && src > -128 {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
666 inst: 0x6Au8 | (uint8: src)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
667 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
668 inst: 0x68u8 | (uint8: src) | (uint8: (rshift: src by: 8)) | (uint8: (rshift: src by: 16)) | (uint8: (rshift: src by: 24))
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
669 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
670 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
671 base <- if: (src register?) {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
672 [0x50u8 or (src reg)]
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
673 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
674 0xFFu8 | (mod_rm: (opex: 6u8) src)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
675 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
676 inst: (prefix: fakesrc src d withInstruction: base)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
677 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
678 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
679
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
680 pop <- :dst {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
681 base <- if: (dst register?) {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
682 [0x58u8 or (dst reg)]
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
683 } else: {
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
684 0x8Fu8 | (mod_rm: (opex: 0u8) dst)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
685 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
686 inst: (prefix: fakesrc dst d withInstruction: base)
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
687 }
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
688
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
689 bnot <- :dst size {
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
690 base <- (size_bit: 0xF6u8 size) | (mod_rm: (opex: 2u8) dst)
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
691 inst: (prefix: fakesrc dst size withInstruction: base)
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
692 }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
693
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
694 //TODO: support multiple calling conventions
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
695 regSource <- {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
696 _used <- 0
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
697 _usedAllTime <- 0
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
698 _nextStackOff <- 0
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
699 _findUnused <- :size reglists{
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
700 found <- -1
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
701 foundlist <- -1
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
702 curlist <- 0
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
703 ll <- reglists length
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
704 while: { found < 0 && curlist < ll } do: {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
705 cur <- 0
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
706 regs <- reglists get: curlist
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
707 len <- regs length
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
708 while: { found < 0 && cur < len } do: {
195
7856f0916549 Add save il instruction to save callee saved registers in function prolog
Mike Pavone <pavone@retrodev.com>
parents: 194
diff changeset
709 bit <- lshift: 1 by: ((regs get: cur) num)
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
710 if: (_used and bit) = 0 {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
711 found <- cur
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
712 foundlist <- regs
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
713 _used <- _used or bit
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
714 _usedAllTime <- _usedAllTime or bit
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
715 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
716 cur <- cur + 1
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
717 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
718 curlist <- curlist + 1
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
719 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
720 if: found >= 0 {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
721 foundlist get: found
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
722 } else: {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
723 myoff <- _nextStackOff
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
724 _nextStackOff <- _nextStackOff + size
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
725 il base: _rsp offset: myoff
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
726 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
727 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
728 #{
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
729 alloc <- :size {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
730 _findUnused: size #[
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
731 _calleesave
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
732 _tempregs
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
733 _argregs
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
734 ]
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
735 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
736 //used to allocate a register
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
737 //that will be returned before a call
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
738 allocTemp <- :size {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
739 _findUnused: size #[
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
740 _tempregs
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
741 _argregs
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
742 _calleesave
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
743 ]
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
744 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
745 //allocated the return register
194
30bed95cbb18 Apply register assignments in il module
Mike Pavone <pavone@retrodev.com>
parents: 193
diff changeset
746 allocRet <- {
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
747 bit <- (lshift: 1 by: (_rax num))
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
748 _used <- _used or bit
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
749 _usedAllTime <- _usedAllTime or bit
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
750 _rax
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
751 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
752 allocArg <- :argnum {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
753 if: argnum < (_argregs length) {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
754 reg <- _argregs get: argnum
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
755 bit <- (lshift: 1 by: (reg num))
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
756 _used <- _used or bit
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
757 _usedAllTime <- _usedAllTime or bit
194
30bed95cbb18 Apply register assignments in il module
Mike Pavone <pavone@retrodev.com>
parents: 193
diff changeset
758 reg
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
759 } else: {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
760 il base: _rsp offset: _nextStackOff + 8 * (argnum - (_argregs length))
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
761 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
762 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
763 allocSpecific <- :reg {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
764 if: (reg register?) {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
765 bit <- (lshift: 1 by: (reg num))
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
766 _used <- _used or bit
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
767 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
768 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
769 stackSize <- { _nextStackOff }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
770 return <- :reg {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
771 _used <- _used and (0xF xor (lshift: 1 by: (reg num)))
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
772 }
198
3606a7cb3999 Fix ireg upper, regSource returnAll and regSource needSaveForCall in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 195
diff changeset
773 returnAll <- { _used <- 0 }
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
774 needSaveProlog <- {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
775 retval <- #[]
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
776 foreach: _calleesave :idx reg {
195
7856f0916549 Add save il instruction to save callee saved registers in function prolog
Mike Pavone <pavone@retrodev.com>
parents: 194
diff changeset
777 bit <- lshift: 1 by: (reg num)
7856f0916549 Add save il instruction to save callee saved registers in function prolog
Mike Pavone <pavone@retrodev.com>
parents: 194
diff changeset
778 if: (_usedAllTime and bit) != 0 {
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
779 retval append: reg
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
780 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
781 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
782 retval
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
783 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
784 needSaveForCall <- {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
785 retval <- #[]
198
3606a7cb3999 Fix ireg upper, regSource returnAll and regSource needSaveForCall in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 195
diff changeset
786 foreach: #[(_tempregs) (_argregs)] :_ regs {
193
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
787 foreach: regs :_ reg {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
788 if: (_used and (lshift: 1 by: (reg num))) != 0 {
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
789 retval append: reg
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
790 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
791 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
792 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
793 retval
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
794 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
795 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
796 }
4293c725394c Mostly complete register allocation in il module with a register source in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 183
diff changeset
797
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
798 adjustIL <- :ilfun {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
799 il to2Op: (il allocRegs: ilfun withSource: regSource)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
800 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
801
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
802 convertIL:to:withLabels:withSaved <- :inst :outarr :labels :saved {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
803 mapSize <- :ilsize {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
804 if: (ilsize bytes) > 2 {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
805 if: (ilsize bytes) = 8 { q } else: { d }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
806 } else: {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
807 if: (ilsize bytes) = 1 { b } else: { w }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
808 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
809 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
810 mapcond <- :ilcond {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
811 ccmap <- #[
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
812 e
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
813 ne
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
814 ge
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
815 le
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
816 g
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
817 l
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
818 ae
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
819 be
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
820 a
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
821 c
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
822 ]
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
823 ccmap get: (ilcond cc)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
824 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
825 opmap <- #[
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
826 { outarr append: (add: (inst in) (inst out) (mapSize: (inst size))) }
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
827 { outarr append: (and: (inst in) (inst out) (mapSize: (inst size))) }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
828 { outarr append: (or: (inst in) (inst out) (mapSize: (inst size))) }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
829 { outarr append: (xor: (inst in) (inst out) (mapSize: (inst size))) }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
830 { } //muls
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
831 { } //mulu
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
832 { } //divs
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
833 { } //divu
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
834 { outarr append: (sub: (inst in) (inst out) (mapSize: (inst size))) }
204
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
835 { outarr append: (cmp: (inst in) (inst out) (mapSize: (inst size))) }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
836 { outarr append: (bnot: (inst arg) (mapSize: (inst size))) }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
837 { outarr append: (shl: (inst in) (inst out) (mapSize: (inst size))) } //sl
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
838 { outarr append: (sar: (inst in) (inst out) (mapSize: (inst size))) } //asr
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
839 { outarr append: (shr: (inst in) (inst out) (mapSize: (inst size))) } //lsr
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
840 { outarr append: (rol: (inst in) (inst out) (mapSize: (inst size))) }
a8dffa4d4b54 Add support for the rest of the instructions currently defined in the il module in the x86 module
Mike Pavone <pavone@retrodev.com>
parents: 203
diff changeset
841 { outarr append: (ror: (inst in) (inst out) (mapSize: (inst size))) }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
842 { outarr append: (mov: (inst in) (inst out) (mapSize: (inst size))) }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
843 {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
844 //call
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
845 arguments <- inst args
350
a3b06d53bcb9 Make il and x86 modules cope with dict hash instead of dict linear for the program definition
Michael Pavone <pavone@retrodev.com>
parents: 348
diff changeset
846 arguments foldr: (arguments length) - 1 with: :cur src {
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
847 if: cur < (_argregs length) {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
848 dst <- _argregs get: cur
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
849 if: (not: dst = src) {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
850 //TODO: Handle edge case in which src is a caller saved
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
851 //reg that has been pusehd onto the stack to preserve
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
852 //it across this call
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
853 outarr append: (mov: src dst q)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
854 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
855 } else: {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
856 outarr append: (push: src)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
857 }
350
a3b06d53bcb9 Make il and x86 modules cope with dict hash instead of dict linear for the program definition
Michael Pavone <pavone@retrodev.com>
parents: 348
diff changeset
858 cur - 1
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
859 }
350
a3b06d53bcb9 Make il and x86 modules cope with dict hash instead of dict linear for the program definition
Michael Pavone <pavone@retrodev.com>
parents: 348
diff changeset
860
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
861 toCall <- inst target
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
862 if: (toCall isString?) {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
863 //TODO: Handle call to undefined label
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
864 toCall <- labels get: toCall else: {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
865 handle <- _getDLHandle:
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
866 address <- dl sym: toCall from: handle
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
867 if: address != 0u64 {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
868 address
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
869 } else: {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
870 print: "Could not find label " . toCall . "\nDefined labels:\n"
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
871 foreach: labels :key _ {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
872 print: "\t" . key . "\n"
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
873 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
874 false
350
a3b06d53bcb9 Make il and x86 modules cope with dict hash instead of dict linear for the program definition
Michael Pavone <pavone@retrodev.com>
parents: 348
diff changeset
875 }
a3b06d53bcb9 Make il and x86 modules cope with dict hash instead of dict linear for the program definition
Michael Pavone <pavone@retrodev.com>
parents: 348
diff changeset
876 }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
877 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
878 outarr append: (call: toCall)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
879 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
880 {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
881 //return
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
882 if: (not: _rax = (inst arg)) {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
883 outarr append: (mov: (inst arg) _rax q)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
884 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
885 foreach: saved :_ reg {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
886 outarr append: (pop: reg)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
887 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
888 outarr append: (ret: )
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
889 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
890 {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
891 //skipIf
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
892 endlab <- label:
363
d949fe826e04 Unify il and backend labels
Michael Pavone <pavone@retrodev.com>
parents: 362
diff changeset
893 outarr append: (jcc: (mapcond: (inst cond)) (endlab reference))
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
894 foreach: (inst toskip) :_ inst {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
895 convertIL: inst to: outarr withLabels: labels withSaved: saved
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
896 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
897 outarr append: endlab
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
898 }
315
f987bb2a1911 WIP native compiler work
Michael Pavone <pavone@retrodev.com>
parents: 204
diff changeset
899 //skipIf:else
355
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
900 {
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
901 endlab <- label:
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
902 elselab <- label:
363
d949fe826e04 Unify il and backend labels
Michael Pavone <pavone@retrodev.com>
parents: 362
diff changeset
903 outarr append: (jcc: (mapcond: (inst cond)) (elselab reference))
355
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
904 foreach: (inst toskip) :_ inst {
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
905 convertIL: inst to: outarr withLabels: labels withSaved: saved
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
906 }
363
d949fe826e04 Unify il and backend labels
Michael Pavone <pavone@retrodev.com>
parents: 362
diff changeset
907 outarr append: (jmp: (endlab reference))
355
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
908 outarr append: elselab
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
909 foreach: (inst else) :_ inst {
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
910 convertIL: inst to: outarr withLabels: labels withSaved: saved
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
911 }
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
912 outarr append: endlab
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
913 }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
914 {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
915 //save
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
916 newsave <- []
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
917 foreach: (inst tosave) :_ reg {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
918 outarr append: (push: reg)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
919 newsave <- reg | newsave
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
920 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
921 foreach: (inst scope) :_ inst {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
922 convertIL: inst to: outarr withLabels: labels withSaved: newsave
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
923 }
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
924 if: ((inst scope) length) = 0 || (((inst scope) get: ((inst scope) length) - 1) opcode) != 18 {
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
925 foreach: newsave :_ reg {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
926 outarr append: (pop: reg)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
927 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
928 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
929 }
315
f987bb2a1911 WIP native compiler work
Michael Pavone <pavone@retrodev.com>
parents: 204
diff changeset
930 //bool
355
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
931 {
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
932 outarr append: (setcc: (mapcond: (inst cond)) (inst out))
0b4d4f06bf91 Add support for setcc in x86 module. Add support for translating il skipIf:else and il bool instructions in x86 module.
Michael Pavone <pavone@retrodev.com>
parents: 350
diff changeset
933 }
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
934 //label
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
935 {
363
d949fe826e04 Unify il and backend labels
Michael Pavone <pavone@retrodev.com>
parents: 362
diff changeset
936 outarr append: inst
361
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
937 }
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
938 //data
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
939 {
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
940 outarr append: (data: (inst bytes))
06dceff348ea llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents: 359
diff changeset
941 }
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
942 ]
348
a840e9a068a2 Get sample builtin to il module working again
Michael Pavone <pavone@retrodev.com>
parents: 315
diff changeset
943 print: "Opcode: " . (inst opcode) . "\n"
203
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
944 fun <- opmap get: (inst opcode)
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
945 fun:
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
946 outarr
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
947 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
948
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
949 convertIL:to:withLabels <- :inst :outarr :labels {
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
950 convertIL: inst to: outarr withLabels: labels withSaved: []
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
951 }
56b2100d9fff Add code for converting IL into x86 machine code
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
952
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
953 main <- {
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
954 fib <- label:
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
955 notbase <- label:
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
956 prog <- #[
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
957 fib
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
958 sub: 2 rdi q
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
959 jcc: ge notbase
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
960 mov: 1 rax q
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
961 ret:
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
962
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
963 notbase
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
964 push: rdi
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
965 call: fib
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
966 pop: rdi
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
967 push: rax
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
968 add: 1 rdi q
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
969 call: fib
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
970 pop: rdi
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
971 add: rdi rax q
181
f188723c15b4 Add call instruction to x86 module
Mike Pavone <pavone@retrodev.com>
parents: 180
diff changeset
972 ret:
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
973 ]
180
270d31c6c4cd Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents: 179
diff changeset
974
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
975 ba <- bytearray executableFromBytes: prog
183
97f107b9e8d3 Fix a few bugs in the x86 module and add jcc, push and pop instructions
Mike Pavone <pavone@retrodev.com>
parents: 181
diff changeset
976 res <- ba runWithArg: 30u64
179
75aca5f87969 A bunch of fixes in x86 instruction encoding
Mike Pavone <pavone@retrodev.com>
parents: 175
diff changeset
977 print: (string: res) . "\n"
174
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
978 0
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
979 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
980 }
8b5829372ad1 Initial work on x86 instruction encoding module
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
981 }