Line |
Branch |
Exec |
Source |
1 |
|
|
#include "asar.h" |
2 |
|
|
#include "assembleblock.h" |
3 |
|
|
#include "asar_math.h" |
4 |
|
|
#include <cassert> |
5 |
|
|
#include <initializer_list> |
6 |
|
|
|
7 |
|
|
// A bit of terminology i just invented: |
8 |
|
|
// "mnemonic" refers to the name of an instruction, e.g. LDA or JMP. |
9 |
|
|
// "address mode" is the form of an instruction's argument, with a specific |
10 |
|
|
// width, e.g. [$12] or $1234,x. determining the exact mode requires knowing |
11 |
|
|
// the width of the instruction. |
12 |
|
|
// "address kind" is like an address mode, but without a specific width. it |
13 |
|
|
// is determined purely from the syntax of the argument. |
14 |
|
|
// "modifier" is the length suffix (the .w in LDA.w), but it's called modifier |
15 |
|
|
// because it's a bit more general (also sets branch targeting mode). |
16 |
|
|
|
17 |
|
|
enum class addr_kind { |
18 |
|
|
abs, // $00 |
19 |
|
|
x, // $00,x |
20 |
|
|
y, // $00,y |
21 |
|
|
ind, // ($00) "indirect" |
22 |
|
|
lind, // [$00] "long indirect" |
23 |
|
|
xind, // ($00,x) |
24 |
|
|
indy, // ($00),y |
25 |
|
|
lindy, // [$00],y |
26 |
|
|
s, // $00,s |
27 |
|
|
sy, // ($00,s),y |
28 |
|
|
imp, // implied (no argument) |
29 |
|
|
a, // 'A' as argument |
30 |
|
|
imm, // #$00 "immediate" |
31 |
|
|
mvn, // special for mvn/mvp |
32 |
|
|
num_addr_kinds, |
33 |
|
|
}; |
34 |
|
|
// used as bitmasks |
35 |
|
|
static_assert((int)addr_kind::num_addr_kinds < 32); |
36 |
|
|
|
37 |
|
|
// various flags that affect parsing of specific instructions. |
38 |
|
|
// these are stored per mnemonic+kind, which is required for some of them, and |
39 |
|
|
// the rest usually have only 1 kind/width allowed anyways so it doesn't hurt. |
40 |
|
|
// this is not an `enum class` because those are annoying to use as bitmasks. |
41 |
|
|
enum mnemonic_flag { |
42 |
|
|
// for brk,cop,wdm: implied mode = immediate with arg 0 |
43 |
|
|
flag_imm_implied_0 = 1 << 0, |
44 |
|
|
// imm should be size 1 or 2 depending on accum size. |
45 |
|
|
// currently all this does is allow both 1- and 2-wide immediates, |
46 |
|
|
// but could be useful for tracking rep/sep state in asar in the future. |
47 |
|
|
flag_imm_width_a = 1 << 1, |
48 |
|
|
// imm should be size 1 or 2 depending on x/y size |
49 |
|
|
flag_imm_width_xy = 1 << 2, |
50 |
|
|
// branch opcodes, have weird target computation rules |
51 |
|
|
flag_branch = 1 << 3, |
52 |
|
|
// for jmp/jsr, sets optimizeforbank to K |
53 |
|
|
flag_bank_k = 1 << 4, |
54 |
|
|
// sets optimizeforbank to 0 (as some jumps always read from bank 0) |
55 |
|
|
flag_bank_0 = 1 << 5, |
56 |
|
|
// additionally allow `imm` addressing which is interpreted as repetition |
57 |
|
|
flag_implied_rep = 1 << 6, |
58 |
|
|
}; |
59 |
|
|
|
60 |
|
|
struct mnem_kind_info { |
61 |
|
|
uint8_t allowed_widths; // bitmask, low 4 bits represent widths 0 to 3 |
62 |
|
|
uint8_t opcode_for_width[4]; |
63 |
|
|
uint32_t flags; // combination of `mnemonic_flag`s |
64 |
|
|
}; |
65 |
|
|
struct mnemonicinfo { |
66 |
|
|
// kind of a waste to have this array which most of the time only has 1 entry filled |
67 |
|
|
mnem_kind_info types[(int)addr_kind::num_addr_kinds]; |
68 |
|
|
uint32_t allowed_kinds_mask; |
69 |
|
|
}; |
70 |
|
|
|
71 |
|
|
struct mnemonic_lookup_t { |
72 |
|
|
std::unordered_map<string, mnemonicinfo> the_map; |
73 |
|
|
|
74 |
|
|
// helper struct for the initializer_list |
75 |
|
|
struct opcode { |
76 |
|
|
uint8_t byte; |
77 |
|
|
const char *mnem; |
78 |
|
|
addr_kind akind; |
79 |
|
|
int width; // width of argument, not entire insn |
80 |
|
|
uint32_t flags; |
81 |
|
1799 |
opcode(uint8_t byte_, const char *mnem_, addr_kind akind_, int width_, uint32_t flags_ = 0) |
82 |
|
1799 |
: byte(byte_), mnem(mnem_), akind(akind_), width(width_), |
83 |
|
1799 |
flags(flags_) {} |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
2023 |
static void add_one(mnemonicinfo& mnem, addr_kind kind, int width, uint8_t byte, uint32_t flags) { |
87 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1734 times.
|
2023 |
mnem.allowed_kinds_mask |= 1 << (int)kind; |
88 |
|
2023 |
auto& this_part = mnem.types[(int)kind]; |
89 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2023 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1734 times.
|
2023 |
assert((this_part.allowed_widths & (1 << width)) == 0); |
90 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1734 times.
|
2023 |
this_part.allowed_widths |= 1 << width; |
91 |
|
2023 |
this_part.opcode_for_width[width] = byte; |
92 |
|
2023 |
this_part.flags |= flags; |
93 |
|
2023 |
} |
94 |
|
|
|
95 |
|
7 |
mnemonic_lookup_t(std::initializer_list<opcode> l) { |
96 |
2/2
✓ Branch 0 taken 1799 times.
✓ Branch 1 taken 7 times.
|
1806 |
for(const auto& x : l) { |
97 |
2/4
✓ Branch 0 taken 1799 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1799 times.
✗ Branch 3 not taken.
|
1799 |
auto& this_mnem = the_map[x.mnem]; |
98 |
1/2
✓ Branch 0 taken 257 times.
✗ Branch 1 not taken.
|
1799 |
add_one(this_mnem, x.akind, x.width, x.byte, x.flags); |
99 |
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1778 times.
|
1799 |
if(x.flags & flag_imm_implied_0) { |
100 |
3/6
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
21 |
assert(x.akind == addr_kind::imm && x.width == 1); |
101 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
21 |
add_one(this_mnem, addr_kind::imp, 0, x.byte, x.flags); |
102 |
|
|
} |
103 |
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1757 times.
|
1799 |
if(x.akind == addr_kind::a) { |
104 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
42 |
add_one(this_mnem, addr_kind::imp, 0, x.byte, x.flags); |
105 |
|
|
} |
106 |
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 1722 times.
|
1799 |
if(x.flags & flag_implied_rep) { |
107 |
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
77 |
add_one(this_mnem, addr_kind::imm, 0, x.byte, x.flags); |
108 |
|
|
} |
109 |
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1715 times.
|
1799 |
if(x.flags & (flag_imm_width_a | flag_imm_width_xy)) { |
110 |
3/6
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
84 |
assert(x.akind == addr_kind::imm && x.width == 1); |
111 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
84 |
add_one(this_mnem, x.akind, 2, x.byte, x.flags); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
7 |
} |
115 |
|
|
}; |
116 |
|
|
|
117 |
|
|
static mnemonic_lookup_t mnemonic_lookup = { |
118 |
|
|
{ 0x00, "brk", addr_kind::imm , 1, flag_imm_implied_0 }, |
119 |
|
|
{ 0x01, "ora", addr_kind::xind , 1 }, |
120 |
|
|
{ 0x02, "cop", addr_kind::imm , 1, flag_imm_implied_0 }, |
121 |
|
|
{ 0x03, "ora", addr_kind::s , 1 }, |
122 |
|
|
{ 0x04, "tsb", addr_kind::abs , 1 }, |
123 |
|
|
{ 0x05, "ora", addr_kind::abs , 1 }, |
124 |
|
|
{ 0x06, "asl", addr_kind::abs , 1 }, |
125 |
|
|
{ 0x07, "ora", addr_kind::lind , 1 }, |
126 |
|
|
{ 0x08, "php", addr_kind::imp , 0 }, |
127 |
|
|
{ 0x09, "ora", addr_kind::imm , 1, flag_imm_width_a }, |
128 |
|
|
{ 0x0a, "asl", addr_kind::a , 0, flag_implied_rep }, |
129 |
|
|
{ 0x0b, "phd", addr_kind::imp , 0 }, |
130 |
|
|
{ 0x0c, "tsb", addr_kind::abs , 2 }, |
131 |
|
|
{ 0x0d, "ora", addr_kind::abs , 2 }, |
132 |
|
|
{ 0x0e, "asl", addr_kind::abs , 2 }, |
133 |
|
|
{ 0x0f, "ora", addr_kind::abs , 3 }, |
134 |
|
|
{ 0x10, "bpl", addr_kind::abs , 1, flag_branch }, |
135 |
|
|
{ 0x11, "ora", addr_kind::indy , 1 }, |
136 |
|
|
{ 0x12, "ora", addr_kind::ind , 1 }, |
137 |
|
|
{ 0x13, "ora", addr_kind::sy , 1 }, |
138 |
|
|
{ 0x14, "trb", addr_kind::abs , 1 }, |
139 |
|
|
{ 0x15, "ora", addr_kind::x , 1 }, |
140 |
|
|
{ 0x16, "asl", addr_kind::x , 1 }, |
141 |
|
|
{ 0x17, "ora", addr_kind::lindy , 1 }, |
142 |
|
|
{ 0x18, "clc", addr_kind::imp , 0 }, |
143 |
|
|
{ 0x19, "ora", addr_kind::y , 2 }, |
144 |
|
|
{ 0x1a, "inc", addr_kind::a , 0, flag_implied_rep }, |
145 |
|
|
{ 0x1b, "tcs", addr_kind::imp , 0 }, |
146 |
|
|
{ 0x1c, "trb", addr_kind::abs , 2 }, |
147 |
|
|
{ 0x1d, "ora", addr_kind::x , 2 }, |
148 |
|
|
{ 0x1e, "asl", addr_kind::x , 2 }, |
149 |
|
|
{ 0x1f, "ora", addr_kind::x , 3 }, |
150 |
|
|
{ 0x20, "jsr", addr_kind::abs , 2, flag_bank_k }, |
151 |
|
|
{ 0x21, "and", addr_kind::xind , 1 }, |
152 |
|
|
{ 0x22, "jsl", addr_kind::abs , 3 }, |
153 |
|
|
{ 0x23, "and", addr_kind::s , 1 }, |
154 |
|
|
{ 0x24, "bit", addr_kind::abs , 1 }, |
155 |
|
|
{ 0x25, "and", addr_kind::abs , 1 }, |
156 |
|
|
{ 0x26, "rol", addr_kind::abs , 1 }, |
157 |
|
|
{ 0x27, "and", addr_kind::lind , 1 }, |
158 |
|
|
{ 0x28, "plp", addr_kind::imp , 0 }, |
159 |
|
|
{ 0x29, "and", addr_kind::imm , 1, flag_imm_width_a }, |
160 |
|
|
{ 0x2a, "rol", addr_kind::a , 0, flag_implied_rep }, |
161 |
|
|
{ 0x2b, "pld", addr_kind::imp , 0 }, |
162 |
|
|
{ 0x2c, "bit", addr_kind::abs , 2 }, |
163 |
|
|
{ 0x2d, "and", addr_kind::abs , 2 }, |
164 |
|
|
{ 0x2e, "rol", addr_kind::abs , 2 }, |
165 |
|
|
{ 0x2f, "and", addr_kind::abs , 3 }, |
166 |
|
|
{ 0x30, "bmi", addr_kind::abs , 1, flag_branch }, |
167 |
|
|
{ 0x31, "and", addr_kind::indy , 1 }, |
168 |
|
|
{ 0x32, "and", addr_kind::ind , 1 }, |
169 |
|
|
{ 0x33, "and", addr_kind::sy , 1 }, |
170 |
|
|
{ 0x34, "bit", addr_kind::x , 1 }, |
171 |
|
|
{ 0x35, "and", addr_kind::x , 1 }, |
172 |
|
|
{ 0x36, "rol", addr_kind::x , 1 }, |
173 |
|
|
{ 0x37, "and", addr_kind::lindy , 1 }, |
174 |
|
|
{ 0x38, "sec", addr_kind::imp , 0 }, |
175 |
|
|
{ 0x39, "and", addr_kind::y , 2 }, |
176 |
|
|
{ 0x3a, "dec", addr_kind::a , 0, flag_implied_rep }, |
177 |
|
|
{ 0x3b, "tsc", addr_kind::imp , 0 }, |
178 |
|
|
{ 0x3c, "bit", addr_kind::x , 2 }, |
179 |
|
|
{ 0x3d, "and", addr_kind::x , 2 }, |
180 |
|
|
{ 0x3e, "rol", addr_kind::x , 2 }, |
181 |
|
|
{ 0x3f, "and", addr_kind::x , 3 }, |
182 |
|
|
{ 0x40, "rti", addr_kind::imp , 0 }, |
183 |
|
|
{ 0x41, "eor", addr_kind::xind , 1 }, |
184 |
|
|
{ 0x42, "wdm", addr_kind::imm , 1, flag_imm_implied_0 }, |
185 |
|
|
{ 0x43, "eor", addr_kind::s , 1 }, |
186 |
|
|
{ 0x44, "mvp", addr_kind::mvn , 2 }, |
187 |
|
|
{ 0x45, "eor", addr_kind::abs , 1 }, |
188 |
|
|
{ 0x46, "lsr", addr_kind::abs , 1 }, |
189 |
|
|
{ 0x47, "eor", addr_kind::lind , 1 }, |
190 |
|
|
{ 0x48, "pha", addr_kind::imp , 0 }, |
191 |
|
|
{ 0x49, "eor", addr_kind::imm , 1, flag_imm_width_a }, |
192 |
|
|
{ 0x4a, "lsr", addr_kind::a , 0, flag_implied_rep }, |
193 |
|
|
{ 0x4b, "phk", addr_kind::imp , 0 }, |
194 |
|
|
{ 0x4c, "jmp", addr_kind::abs , 2, flag_bank_k }, |
195 |
|
|
{ 0x4d, "eor", addr_kind::abs , 2 }, |
196 |
|
|
{ 0x4e, "lsr", addr_kind::abs , 2 }, |
197 |
|
|
{ 0x4f, "eor", addr_kind::abs , 3 }, |
198 |
|
|
{ 0x50, "bvc", addr_kind::abs , 1, flag_branch }, |
199 |
|
|
{ 0x51, "eor", addr_kind::indy , 1 }, |
200 |
|
|
{ 0x52, "eor", addr_kind::ind , 1 }, |
201 |
|
|
{ 0x53, "eor", addr_kind::sy , 1 }, |
202 |
|
|
{ 0x54, "mvn", addr_kind::mvn , 2 }, |
203 |
|
|
{ 0x55, "eor", addr_kind::x , 1 }, |
204 |
|
|
{ 0x56, "lsr", addr_kind::x , 1 }, |
205 |
|
|
{ 0x57, "eor", addr_kind::lindy , 1 }, |
206 |
|
|
{ 0x58, "cli", addr_kind::imp , 0 }, |
207 |
|
|
{ 0x59, "eor", addr_kind::y , 2 }, |
208 |
|
|
{ 0x5a, "phy", addr_kind::imp , 0 }, |
209 |
|
|
{ 0x5b, "tcd", addr_kind::imp , 0 }, |
210 |
|
|
{ 0x5c, "jml", addr_kind::abs , 3 }, |
211 |
|
|
{ 0x5d, "eor", addr_kind::x , 2 }, |
212 |
|
|
{ 0x5e, "lsr", addr_kind::x , 2 }, |
213 |
|
|
{ 0x5f, "eor", addr_kind::x , 3 }, |
214 |
|
|
{ 0x60, "rts", addr_kind::imp , 0 }, |
215 |
|
|
{ 0x61, "adc", addr_kind::xind , 1 }, |
216 |
|
|
{ 0x62, "per", addr_kind::abs , 2, flag_branch }, |
217 |
|
|
{ 0x63, "adc", addr_kind::s , 1 }, |
218 |
|
|
{ 0x64, "stz", addr_kind::abs , 1 }, |
219 |
|
|
{ 0x65, "adc", addr_kind::abs , 1 }, |
220 |
|
|
{ 0x66, "ror", addr_kind::abs , 1 }, |
221 |
|
|
{ 0x67, "adc", addr_kind::lind , 1 }, |
222 |
|
|
{ 0x68, "pla", addr_kind::imp , 0 }, |
223 |
|
|
{ 0x69, "adc", addr_kind::imm , 1, flag_imm_width_a }, |
224 |
|
|
{ 0x6a, "ror", addr_kind::a , 0, flag_implied_rep }, |
225 |
|
|
{ 0x6b, "rtl", addr_kind::imp , 0 }, |
226 |
|
|
{ 0x6c, "jmp", addr_kind::ind , 2, flag_bank_0 }, |
227 |
|
|
{ 0x6d, "adc", addr_kind::abs , 2 }, |
228 |
|
|
{ 0x6e, "ror", addr_kind::abs , 2 }, |
229 |
|
|
{ 0x6f, "adc", addr_kind::abs , 3 }, |
230 |
|
|
{ 0x70, "bvs", addr_kind::abs , 1, flag_branch }, |
231 |
|
|
{ 0x71, "adc", addr_kind::indy , 1 }, |
232 |
|
|
{ 0x72, "adc", addr_kind::ind , 1 }, |
233 |
|
|
{ 0x73, "adc", addr_kind::sy , 1 }, |
234 |
|
|
{ 0x74, "stz", addr_kind::x , 1 }, |
235 |
|
|
{ 0x75, "adc", addr_kind::x , 1 }, |
236 |
|
|
{ 0x76, "ror", addr_kind::x , 1 }, |
237 |
|
|
{ 0x77, "adc", addr_kind::lindy , 1 }, |
238 |
|
|
{ 0x78, "sei", addr_kind::imp , 0 }, |
239 |
|
|
{ 0x79, "adc", addr_kind::y , 2 }, |
240 |
|
|
{ 0x7a, "ply", addr_kind::imp , 0 }, |
241 |
|
|
{ 0x7b, "tdc", addr_kind::imp , 0 }, |
242 |
|
|
{ 0x7c, "jmp", addr_kind::xind , 2, flag_bank_k }, |
243 |
|
|
{ 0x7d, "adc", addr_kind::x , 2 }, |
244 |
|
|
{ 0x7e, "ror", addr_kind::x , 2 }, |
245 |
|
|
{ 0x7f, "adc", addr_kind::x , 3 }, |
246 |
|
|
{ 0x80, "bra", addr_kind::abs , 1, flag_branch }, |
247 |
|
|
{ 0x81, "sta", addr_kind::xind , 1 }, |
248 |
|
|
{ 0x82, "brl", addr_kind::abs , 2, flag_branch }, |
249 |
|
|
{ 0x83, "sta", addr_kind::s , 1 }, |
250 |
|
|
{ 0x84, "sty", addr_kind::abs , 1 }, |
251 |
|
|
{ 0x85, "sta", addr_kind::abs , 1 }, |
252 |
|
|
{ 0x86, "stx", addr_kind::abs , 1 }, |
253 |
|
|
{ 0x87, "sta", addr_kind::lind , 1 }, |
254 |
|
|
{ 0x88, "dey", addr_kind::imp , 0, flag_implied_rep }, |
255 |
|
|
{ 0x89, "bit", addr_kind::imm , 1, flag_imm_width_a }, |
256 |
|
|
{ 0x8a, "txa", addr_kind::imp , 0 }, |
257 |
|
|
{ 0x8b, "phb", addr_kind::imp , 0 }, |
258 |
|
|
{ 0x8c, "sty", addr_kind::abs , 2 }, |
259 |
|
|
{ 0x8d, "sta", addr_kind::abs , 2 }, |
260 |
|
|
{ 0x8e, "stx", addr_kind::abs , 2 }, |
261 |
|
|
{ 0x8f, "sta", addr_kind::abs , 3 }, |
262 |
|
|
{ 0x90, "bcc", addr_kind::abs , 1, flag_branch }, |
263 |
|
|
{ 0x91, "sta", addr_kind::indy , 1 }, |
264 |
|
|
{ 0x92, "sta", addr_kind::ind , 1 }, |
265 |
|
|
{ 0x93, "sta", addr_kind::sy , 1 }, |
266 |
|
|
{ 0x94, "sty", addr_kind::x , 1 }, |
267 |
|
|
{ 0x95, "sta", addr_kind::x , 1 }, |
268 |
|
|
{ 0x96, "stx", addr_kind::y , 1 }, |
269 |
|
|
{ 0x97, "sta", addr_kind::lindy , 1 }, |
270 |
|
|
{ 0x98, "tya", addr_kind::imp , 0 }, |
271 |
|
|
{ 0x99, "sta", addr_kind::y , 2 }, |
272 |
|
|
{ 0x9a, "txs", addr_kind::imp , 0 }, |
273 |
|
|
{ 0x9b, "txy", addr_kind::imp , 0 }, |
274 |
|
|
{ 0x9c, "stz", addr_kind::abs , 2 }, |
275 |
|
|
{ 0x9d, "sta", addr_kind::x , 2 }, |
276 |
|
|
{ 0x9e, "stz", addr_kind::x , 2 }, |
277 |
|
|
{ 0x9f, "sta", addr_kind::x , 3 }, |
278 |
|
|
{ 0xa0, "ldy", addr_kind::imm , 1, flag_imm_width_xy }, |
279 |
|
|
{ 0xa1, "lda", addr_kind::xind , 1 }, |
280 |
|
|
{ 0xa2, "ldx", addr_kind::imm , 1, flag_imm_width_xy }, |
281 |
|
|
{ 0xa3, "lda", addr_kind::s , 1 }, |
282 |
|
|
{ 0xa4, "ldy", addr_kind::abs , 1 }, |
283 |
|
|
{ 0xa5, "lda", addr_kind::abs , 1 }, |
284 |
|
|
{ 0xa6, "ldx", addr_kind::abs , 1 }, |
285 |
|
|
{ 0xa7, "lda", addr_kind::lind , 1 }, |
286 |
|
|
{ 0xa8, "tay", addr_kind::imp , 0 }, |
287 |
|
|
{ 0xa9, "lda", addr_kind::imm , 1, flag_imm_width_a }, |
288 |
|
|
{ 0xaa, "tax", addr_kind::imp , 0 }, |
289 |
|
|
{ 0xab, "plb", addr_kind::imp , 0 }, |
290 |
|
|
{ 0xac, "ldy", addr_kind::abs , 2 }, |
291 |
|
|
{ 0xad, "lda", addr_kind::abs , 2 }, |
292 |
|
|
{ 0xae, "ldx", addr_kind::abs , 2 }, |
293 |
|
|
{ 0xaf, "lda", addr_kind::abs , 3 }, |
294 |
|
|
{ 0xb0, "bcs", addr_kind::abs , 1, flag_branch }, |
295 |
|
|
{ 0xb1, "lda", addr_kind::indy , 1 }, |
296 |
|
|
{ 0xb2, "lda", addr_kind::ind , 1 }, |
297 |
|
|
{ 0xb3, "lda", addr_kind::sy , 1 }, |
298 |
|
|
{ 0xb4, "ldy", addr_kind::x , 1 }, |
299 |
|
|
{ 0xb5, "lda", addr_kind::x , 1 }, |
300 |
|
|
{ 0xb6, "ldx", addr_kind::y , 1 }, |
301 |
|
|
{ 0xb7, "lda", addr_kind::lindy , 1 }, |
302 |
|
|
{ 0xb8, "clv", addr_kind::imp , 0 }, |
303 |
|
|
{ 0xb9, "lda", addr_kind::y , 2 }, |
304 |
|
|
{ 0xba, "tsx", addr_kind::imp , 0 }, |
305 |
|
|
{ 0xbb, "tyx", addr_kind::imp , 0 }, |
306 |
|
|
{ 0xbc, "ldy", addr_kind::x , 2 }, |
307 |
|
|
{ 0xbd, "lda", addr_kind::x , 2 }, |
308 |
|
|
{ 0xbe, "ldx", addr_kind::y , 2 }, |
309 |
|
|
{ 0xbf, "lda", addr_kind::x , 3 }, |
310 |
|
|
{ 0xc0, "cpy", addr_kind::imm , 1, flag_imm_width_xy }, |
311 |
|
|
{ 0xc1, "cmp", addr_kind::xind , 1 }, |
312 |
|
|
{ 0xc2, "rep", addr_kind::imm , 1 }, |
313 |
|
|
{ 0xc3, "cmp", addr_kind::s , 1 }, |
314 |
|
|
{ 0xc4, "cpy", addr_kind::abs , 1 }, |
315 |
|
|
{ 0xc5, "cmp", addr_kind::abs , 1 }, |
316 |
|
|
{ 0xc6, "dec", addr_kind::abs , 1 }, |
317 |
|
|
{ 0xc7, "cmp", addr_kind::lind , 1 }, |
318 |
|
|
{ 0xc8, "iny", addr_kind::imp , 0, flag_implied_rep }, |
319 |
|
|
{ 0xc9, "cmp", addr_kind::imm , 1, flag_imm_width_a }, |
320 |
|
|
{ 0xca, "dex", addr_kind::imp , 0, flag_implied_rep }, |
321 |
|
|
{ 0xcb, "wai", addr_kind::imp , 0 }, |
322 |
|
|
{ 0xcc, "cpy", addr_kind::abs , 2 }, |
323 |
|
|
{ 0xcd, "cmp", addr_kind::abs , 2 }, |
324 |
|
|
{ 0xce, "dec", addr_kind::abs , 2 }, |
325 |
|
|
{ 0xcf, "cmp", addr_kind::abs , 3 }, |
326 |
|
|
{ 0xd0, "bne", addr_kind::abs , 1, flag_branch }, |
327 |
|
|
{ 0xd1, "cmp", addr_kind::indy , 1 }, |
328 |
|
|
{ 0xd2, "cmp", addr_kind::ind , 1 }, |
329 |
|
|
{ 0xd3, "cmp", addr_kind::sy , 1 }, |
330 |
|
|
{ 0xd4, "pei", addr_kind::ind , 1 }, |
331 |
|
|
{ 0xd5, "cmp", addr_kind::x , 1 }, |
332 |
|
|
{ 0xd6, "dec", addr_kind::x , 1 }, |
333 |
|
|
{ 0xd7, "cmp", addr_kind::lindy , 1 }, |
334 |
|
|
{ 0xd8, "cld", addr_kind::imp , 0 }, |
335 |
|
|
{ 0xd9, "cmp", addr_kind::y , 2 }, |
336 |
|
|
{ 0xda, "phx", addr_kind::imp , 0 }, |
337 |
|
|
{ 0xdb, "stp", addr_kind::imp , 0 }, |
338 |
|
|
{ 0xdc, "jmp", addr_kind::lind , 2, flag_bank_0 }, |
339 |
|
|
{ 0xdc, "jml", addr_kind::lind , 2, flag_bank_0 }, |
340 |
|
|
{ 0xdd, "cmp", addr_kind::x , 2 }, |
341 |
|
|
{ 0xde, "dec", addr_kind::x , 2 }, |
342 |
|
|
{ 0xdf, "cmp", addr_kind::x , 3 }, |
343 |
|
|
{ 0xe0, "cpx", addr_kind::imm , 1, flag_imm_width_xy }, |
344 |
|
|
{ 0xe1, "sbc", addr_kind::xind , 1 }, |
345 |
|
|
{ 0xe2, "sep", addr_kind::imm , 1 }, |
346 |
|
|
{ 0xe3, "sbc", addr_kind::s , 1 }, |
347 |
|
|
{ 0xe4, "cpx", addr_kind::abs , 1 }, |
348 |
|
|
{ 0xe5, "sbc", addr_kind::abs , 1 }, |
349 |
|
|
{ 0xe6, "inc", addr_kind::abs , 1 }, |
350 |
|
|
{ 0xe7, "sbc", addr_kind::lind , 1 }, |
351 |
|
|
{ 0xe8, "inx", addr_kind::imp , 0, flag_implied_rep }, |
352 |
|
|
{ 0xe9, "sbc", addr_kind::imm , 1, flag_imm_width_a }, |
353 |
|
|
{ 0xea, "nop", addr_kind::imp , 0, flag_implied_rep }, |
354 |
|
|
{ 0xeb, "xba", addr_kind::imp , 0 }, |
355 |
|
|
{ 0xec, "cpx", addr_kind::abs , 2 }, |
356 |
|
|
{ 0xed, "sbc", addr_kind::abs , 2 }, |
357 |
|
|
{ 0xee, "inc", addr_kind::abs , 2 }, |
358 |
|
|
{ 0xef, "sbc", addr_kind::abs , 3 }, |
359 |
|
|
{ 0xf0, "beq", addr_kind::abs , 1, flag_branch }, |
360 |
|
|
{ 0xf1, "sbc", addr_kind::indy , 1 }, |
361 |
|
|
{ 0xf2, "sbc", addr_kind::ind , 1 }, |
362 |
|
|
{ 0xf3, "sbc", addr_kind::sy , 1 }, |
363 |
|
|
{ 0xf4, "pea", addr_kind::abs , 2 }, |
364 |
|
|
{ 0xf5, "sbc", addr_kind::x , 1 }, |
365 |
|
|
{ 0xf6, "inc", addr_kind::x , 1 }, |
366 |
|
|
{ 0xf7, "sbc", addr_kind::lindy , 1 }, |
367 |
|
|
{ 0xf8, "sed", addr_kind::imp , 0 }, |
368 |
|
|
{ 0xf9, "sbc", addr_kind::y , 2 }, |
369 |
|
|
{ 0xfa, "plx", addr_kind::imp , 0 }, |
370 |
|
|
{ 0xfb, "xce", addr_kind::imp , 0 }, |
371 |
|
|
{ 0xfc, "jsr", addr_kind::xind , 2, flag_bank_k }, |
372 |
|
|
{ 0xfd, "sbc", addr_kind::x , 2 }, |
373 |
|
|
{ 0xfe, "inc", addr_kind::x , 2 }, |
374 |
|
|
{ 0xff, "sbc", addr_kind::x , 3 }, |
375 |
|
|
}; |
376 |
|
|
|
377 |
|
|
struct parse_result { |
378 |
|
|
addr_kind kind; |
379 |
|
|
string arg; |
380 |
|
|
}; |
381 |
|
|
|
382 |
|
|
// checks for matching characters at the start of haystack, ignoring spaces. |
383 |
|
|
// returns index into haystack right after the match |
384 |
|
|
template<char... chars> |
385 |
|
22158 |
static int64_t startmatch(const string& haystack) { |
386 |
|
|
static const char needle[] = {chars...}; |
387 |
|
22158 |
size_t haystack_i = 0; |
388 |
2/2
✓ Branch 0 taken 11079 times.
✓ Branch 1 taken 906 times.
|
23970 |
for(size_t i = 0; i < sizeof...(chars); i++) { |
389 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11079 times.
|
22158 |
while(haystack[haystack_i] == ' ') haystack_i++; |
390 |
2/2
✓ Branch 0 taken 10173 times.
✓ Branch 1 taken 906 times.
|
22158 |
if(needle[i] != to_lower(haystack[haystack_i++])) return -1; |
391 |
|
|
} |
392 |
|
1812 |
return haystack_i; |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
// checks for matching characters at the end of haystack, ignoring spaces. |
396 |
|
|
// returns index into haystack right before the match |
397 |
|
|
template<char... chars> |
398 |
|
18654 |
static int64_t endmatch(const string& haystack) { |
399 |
|
|
static const char needle[] = {chars...}; |
400 |
|
18654 |
int64_t haystack_i = haystack.length()-1; |
401 |
2/2
✓ Branch 0 taken 10707 times.
✓ Branch 1 taken 990 times.
|
23394 |
for(int64_t i = sizeof...(chars)-1; i >= 0; i--) { |
402 |
4/6
✓ Branch 0 taken 6969 times.
✓ Branch 1 taken 3738 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6969 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10707 times.
|
21414 |
while(haystack_i >= 0 && haystack[haystack_i] == ' ') haystack_i--; |
403 |
8/8
✓ Branch 0 taken 6969 times.
✓ Branch 1 taken 3738 times.
✓ Branch 2 taken 2286 times.
✓ Branch 3 taken 1248 times.
✓ Branch 4 taken 6468 times.
✓ Branch 5 taken 2370 times.
✓ Branch 6 taken 4182 times.
✓ Branch 7 taken 1122 times.
|
21414 |
if(haystack_i < 0 || needle[i] != to_lower(haystack[haystack_i--])) return -1; |
404 |
|
|
} |
405 |
|
1980 |
return haystack_i+1; |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
/* |
409 |
|
|
* Parses the address kind from an argument, given a list of allowed address kinds. |
410 |
|
|
* Throws "invalid address mode" if the argument does not match any kinds. |
411 |
|
|
*/ |
412 |
|
|
// i still don't like this function... |
413 |
|
4149 |
static parse_result parse_addr_kind(const string& arg, uint32_t allowed_kinds_mask) { |
414 |
|
4149 |
int64_t start_i = 0, end_i = arg.length(); |
415 |
|
|
// If this addressing kind is allowed, return it, along with a trimmed version of the string. |
416 |
|
|
#define RETURN_IF_ALLOWED(kind) \ |
417 |
|
|
if (allowed_kinds_mask & 1<<(uint32_t)addr_kind::kind) { \ |
418 |
|
|
string out(arg.data() + start_i, end_i - start_i); \ |
419 |
|
|
return parse_result{addr_kind::kind, out}; \ |
420 |
|
|
} |
421 |
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 3573 times.
|
4149 |
if((start_i = startmatch<'#'>(arg)) >= 0) { |
422 |
2/4
✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 576 times.
✗ Branch 3 not taken.
|
1152 |
RETURN_IF_ALLOWED(imm); |
423 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
424 |
|
|
} |
425 |
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 3357 times.
|
3573 |
if((start_i = startmatch<'('>(arg)) >= 0) { |
426 |
|
|
// TODO check if the end of this paren is where it should be |
427 |
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 168 times.
|
216 |
if((end_i = endmatch<',', 's', ')', ',', 'y'>(arg)) >= 0) { |
428 |
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
96 |
RETURN_IF_ALLOWED(sy); |
429 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
430 |
|
|
} |
431 |
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 120 times.
|
168 |
if((end_i = endmatch<')', ',', 'y'>(arg)) >= 0) { |
432 |
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
96 |
RETURN_IF_ALLOWED(indy); |
433 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
434 |
|
|
} |
435 |
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 60 times.
|
120 |
if((end_i = endmatch<',', 'x', ')'>(arg)) >= 0) { |
436 |
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
|
120 |
RETURN_IF_ALLOWED(xind); |
437 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
438 |
|
|
} |
439 |
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 |
if((end_i = endmatch<')'>(arg)) >= 0) { |
440 |
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
|
120 |
RETURN_IF_ALLOWED(ind); |
441 |
|
✗ |
asar_throw_warning(1, warning_id_assuming_address_mode, "($00)", "$00", " (if this was intentional, add a +0 after the parentheses.)"); |
442 |
|
|
} |
443 |
|
|
} |
444 |
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 3243 times.
|
3357 |
if((start_i = startmatch<'['>(arg)) >= 0) { |
445 |
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 66 times.
|
114 |
if((end_i = endmatch<']', ',', 'y'>(arg)) >= 0) { |
446 |
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
96 |
RETURN_IF_ALLOWED(lindy); |
447 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
448 |
|
|
} |
449 |
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 |
if((end_i = endmatch<']'>(arg)) >= 0) { |
450 |
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
|
132 |
RETURN_IF_ALLOWED(lind); |
451 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
452 |
|
|
} |
453 |
|
|
} |
454 |
|
3243 |
start_i = 0; |
455 |
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 2709 times.
|
3243 |
if((end_i = endmatch<',', 'x'>(arg)) >= 0) { |
456 |
2/4
✓ Branch 0 taken 534 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 534 times.
✗ Branch 3 not taken.
|
1068 |
RETURN_IF_ALLOWED(x); |
457 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
458 |
|
|
} |
459 |
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 2631 times.
|
2709 |
if((end_i = endmatch<',', 'y'>(arg)) >= 0) { |
460 |
2/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
156 |
RETURN_IF_ALLOWED(y); |
461 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
462 |
|
|
} |
463 |
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 2583 times.
|
2631 |
if((end_i = endmatch<',', 's'>(arg)) >= 0) { |
464 |
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
96 |
RETURN_IF_ALLOWED(s); |
465 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
466 |
|
|
} |
467 |
|
2583 |
end_i = arg.length(); |
468 |
|
|
// hoping that by now, something would have stripped whitespace lol |
469 |
2/2
✓ Branch 0 taken 1242 times.
✓ Branch 1 taken 1341 times.
|
2583 |
if(arg.length() == 0) { |
470 |
2/4
✓ Branch 0 taken 1242 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
✗ Branch 3 not taken.
|
2484 |
RETURN_IF_ALLOWED(imp); |
471 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
472 |
|
|
} |
473 |
6/6
✓ Branch 0 taken 1335 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 1299 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 1299 times.
|
1341 |
if(arg == "a" || arg == "A") { |
474 |
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
78 |
RETURN_IF_ALLOWED(a); |
475 |
|
|
// todo: some hint for "don't name your label "a" "? |
476 |
|
6 |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
477 |
|
|
} |
478 |
2/4
✓ Branch 0 taken 1299 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1299 times.
✗ Branch 3 not taken.
|
2598 |
RETURN_IF_ALLOWED(abs); |
479 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
480 |
|
|
#undef RETURN_IF_ALLOWED |
481 |
13/26
✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 60 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 60 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 48 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 66 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 534 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 78 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 48 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1242 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 36 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 1299 times.
✗ Branch 25 not taken.
|
4143 |
} |
482 |
|
|
|
483 |
|
✗ |
static const char* format_valid_widths(int min, int max) { |
484 |
|
✗ |
if(min == 1) { |
485 |
|
✗ |
if(max == 1) return "only 8-bit"; |
486 |
|
✗ |
if(max == 2) return "only 8-bit or 16-bit"; |
487 |
|
✗ |
if(max == 3) return "any width"; // shouldn't actually happen lol |
488 |
|
✗ |
} else if(min == 2) { |
489 |
|
✗ |
if(max == 2) return "only 16-bit"; |
490 |
|
✗ |
if(max == 3) return "only 16-bit or 24-bit"; |
491 |
|
✗ |
} else if(min == 3) { |
492 |
|
✗ |
return "only 24-bit"; |
493 |
|
|
} |
494 |
|
✗ |
return "???"; |
495 |
|
|
} |
496 |
|
|
|
497 |
|
2724 |
static int get_real_len(int min_len, int max_len, char modifier, const parse_result& parsed) { |
498 |
|
|
// we can theoretically give min_len to getlen now :o |
499 |
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 6 times.
|
2724 |
int arg_min_len = getlen(parsed.arg, parsed.kind == addr_kind::imm); |
500 |
|
|
int out_len; |
501 |
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 1938 times.
|
2718 |
if(modifier != 0) { |
502 |
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 6 times.
|
780 |
out_len = getlenfromchar(modifier); |
503 |
2/4
✓ Branch 0 taken 774 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 774 times.
|
774 |
if(out_len < min_len || out_len > max_len) |
504 |
|
✗ |
asar_throw_error(2, error_type_block, error_id_bad_access_width, format_valid_widths(min_len, max_len), out_len*8); |
505 |
|
|
} else { |
506 |
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 1623 times.
|
1938 |
if(parsed.kind == addr_kind::imm) { |
507 |
3/4
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✓ Branch 3 taken 216 times.
|
315 |
if(!is_hex_constant(parsed.arg.data())) |
508 |
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
|
99 |
asar_throw_warning(2, warning_id_implicitly_sized_immediate); |
509 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
315 |
if(arg_min_len == 3 && max_len == 2) { |
510 |
|
|
// lda #label |
511 |
|
|
// todo: throw pedantic warning |
512 |
|
✗ |
return max_len; |
513 |
|
|
} |
514 |
|
|
} |
515 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1938 times.
|
1938 |
if(arg_min_len > max_len) { |
516 |
|
|
// we might get here on pass 0 if getlen is wrong about the width, |
517 |
|
|
// which can happen with forward labels and namespaces. |
518 |
|
|
// in that case return some valid width to silence the error. |
519 |
|
✗ |
if(pass == 0) return max_len; |
520 |
|
|
|
521 |
|
✗ |
asar_throw_error(2, error_type_block, error_id_bad_access_width, format_valid_widths(min_len, max_len), arg_min_len*8); |
522 |
|
|
} |
523 |
|
|
// todo warn about widening when dpbase != 0 |
524 |
|
1938 |
out_len = std::max(arg_min_len, min_len); |
525 |
|
|
} |
526 |
|
2712 |
return out_len; |
527 |
|
|
} |
528 |
|
|
|
529 |
|
22 |
static int64_t get_branch_value(parse_result& parsed, char modifier, int width) { |
530 |
|
22 |
int64_t num = 0; |
531 |
|
22 |
num = getnum(parsed.arg); |
532 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
22 |
bool target_is_abs = foundlabel; |
533 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 |
if(modifier != 0) { |
534 |
|
✗ |
if(to_lower(modifier) == 'a') target_is_abs = true; |
535 |
|
✗ |
else if(to_lower(modifier) == 'r') target_is_abs = false; |
536 |
|
|
// TODO: better error message |
537 |
|
✗ |
else asar_throw_error(2, error_type_block, error_id_invalid_opcode_length); |
538 |
|
|
} |
539 |
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
|
22 |
if(target_is_abs) { |
540 |
|
|
// cast delta to signed 16-bit, this makes it possible to handle bank-border-wrapping automatically |
541 |
|
20 |
int16_t delta = num - (snespos + width + 1); |
542 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 |
if((num & ~0xffff) != (snespos & ~0xffff)) { |
543 |
|
|
// todo: should throw error "can't branch to different bank" |
544 |
|
✗ |
asar_throw_error(2, error_type_block, error_id_relative_branch_out_of_bounds, dec(delta).data()); |
545 |
|
|
} |
546 |
4/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
|
20 |
if(width==1 && (delta < -128 || delta > 127)) { |
547 |
|
✗ |
asar_throw_error(2, error_type_block, error_id_relative_branch_out_of_bounds, dec(delta).data()); |
548 |
|
|
} |
549 |
|
20 |
return delta; |
550 |
|
|
} else { |
551 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 |
if(num & ~(width==2 ? 0xffff : 0xff)) { |
552 |
|
✗ |
asar_throw_error(2, error_type_block, error_id_relative_branch_out_of_bounds, dec(num).data()); |
553 |
|
|
} |
554 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
return (int16_t)(width==2 ? num : (int8_t)num); |
555 |
|
|
} |
556 |
|
|
} |
557 |
|
|
|
558 |
|
20529 |
bool asblock_65816(char** word, int numwords) |
559 |
|
|
{ |
560 |
|
|
// first find the mnemonic from the first word |
561 |
|
20529 |
int word_i = 0; |
562 |
|
20529 |
bool autoclean = false; |
563 |
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 20367 times.
|
20529 |
if(!stricmpwithlower(word[0], "autoclean")) { |
564 |
|
162 |
word_i++; |
565 |
|
162 |
autoclean = true; |
566 |
|
|
} |
567 |
1/2
✓ Branch 0 taken 20529 times.
✗ Branch 1 not taken.
|
20529 |
string mnem = word[word_i++]; |
568 |
2/2
✓ Branch 0 taken 100725 times.
✓ Branch 1 taken 20529 times.
|
121254 |
for(int i = 0; i < mnem.length(); i++) mnem.raw()[i] = to_lower(mnem[i]); |
569 |
|
|
|
570 |
|
20529 |
char modifier = 0; |
571 |
6/6
✓ Branch 0 taken 20433 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 780 times.
✓ Branch 3 taken 19653 times.
✓ Branch 4 taken 780 times.
✓ Branch 5 taken 19749 times.
|
20529 |
if(mnem.length() >= 2 && mnem[mnem.length()-2] == '.') { |
572 |
|
780 |
modifier = mnem[mnem.length()-1]; |
573 |
1/2
✓ Branch 0 taken 780 times.
✗ Branch 1 not taken.
|
780 |
mnem.truncate(mnem.length()-2); |
574 |
|
|
} |
575 |
|
|
|
576 |
1/2
✓ Branch 0 taken 20529 times.
✗ Branch 1 not taken.
|
20529 |
auto it = mnemonic_lookup.the_map.find(mnem); |
577 |
2/2
✓ Branch 0 taken 16368 times.
✓ Branch 1 taken 4161 times.
|
20529 |
if(it == mnemonic_lookup.the_map.end()) return false; |
578 |
|
4161 |
mnemonicinfo& mnem_info = it->second; |
579 |
|
|
|
580 |
|
|
// join together all the other arguments |
581 |
|
4161 |
string par; |
582 |
2/2
✓ Branch 0 taken 2991 times.
✓ Branch 1 taken 4161 times.
|
7152 |
for(int i = word_i; i < numwords; i++){ |
583 |
3/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 2919 times.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
2991 |
if(i > word_i) par += " "; |
584 |
1/2
✓ Branch 0 taken 2991 times.
✗ Branch 1 not taken.
|
2991 |
par += word[i]; |
585 |
|
|
} |
586 |
|
|
|
587 |
|
|
// check if we're doing mvn/mvp, and if yes, parse completely differently |
588 |
|
|
// this is a little hacky, sorry... |
589 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4149 times.
|
4161 |
if(mnem_info.allowed_kinds_mask & 1<<(uint32_t)addr_kind::mvn) { |
590 |
|
12 |
int count = 0; |
591 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
autoptr<char**> parts = qpsplit(par.raw(), ',', &count); |
592 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 |
if(count != 2) asar_throw_error(2, error_type_block, error_id_bad_addr_mode); |
593 |
|
12 |
uint8_t opcode = mnem_info.types[(int)addr_kind::mvn].opcode_for_width[2]; |
594 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
write1(opcode); |
595 |
|
12 |
int64_t num1 = 0, num2 = 0; |
596 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
|
12 |
if(pass == 2) { |
597 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
num1 = getnum(parts[0]); |
598 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
num2 = getnum(parts[1]); |
599 |
|
|
} |
600 |
2/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 |
if(num1 < 0 || num1 > 255) asar_throw_error(2, error_type_block, error_id_bad_access_width, format_valid_widths(1, 1), num1 > 65535 ? 24 : 16); |
601 |
2/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 |
if(num2 < 0 || num2 > 255) asar_throw_error(2, error_type_block, error_id_bad_access_width, format_valid_widths(1, 1), num2 > 65535 ? 24 : 16); |
602 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
write1(num1); |
603 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
write1(num2); |
604 |
|
|
// a bit hacky to check this here, but we kinda do need to early-return here |
605 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 |
if(autoclean) asar_throw_error(2, error_type_block, error_id_broken_autoclean); |
606 |
|
12 |
return true; |
607 |
|
12 |
} |
608 |
2/2
✓ Branch 0 taken 4143 times.
✓ Branch 1 taken 6 times.
|
4149 |
parse_result parse_res = parse_addr_kind(par, mnem_info.allowed_kinds_mask); |
609 |
|
4143 |
mnem_kind_info& kind_info = mnem_info.types[(uint32_t)parse_res.kind]; |
610 |
|
|
|
611 |
|
|
// figure out the minimum/maximum argument width for this mnemonic+addr_kind combo |
612 |
|
|
// todo this is mildly jank |
613 |
|
|
// we also kinda implicitly assume that no insn has "gaps" in its allowed widths |
614 |
|
4143 |
int min_w = 99; |
615 |
|
4143 |
int max_w = 0; |
616 |
2/2
✓ Branch 0 taken 33144 times.
✓ Branch 1 taken 4143 times.
|
37287 |
for(int i = 0; i < 8; i++) { |
617 |
4/4
✓ Branch 0 taken 3516 times.
✓ Branch 1 taken 29628 times.
✓ Branch 2 taken 3540 times.
✓ Branch 3 taken 13092 times.
|
33144 |
if(kind_info.allowed_widths & 1<<i) { |
618 |
|
7056 |
min_w = std::min(min_w, i); |
619 |
|
7056 |
max_w = std::max(max_w, i); |
620 |
|
|
} |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
// compute argument value |
624 |
4/4
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 3567 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 501 times.
|
4143 |
bool is_implied_rep = parse_res.kind == addr_kind::imm && (kind_info.flags & flag_implied_rep); |
625 |
|
|
int arg_len; |
626 |
|
|
int64_t arg_value; |
627 |
|
4143 |
int64_t rep_count = 0; |
628 |
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 4077 times.
|
4143 |
if(kind_info.flags & flag_branch) { |
629 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
66 |
assert(min_w == max_w); // can't call get_real_len, but there should be only one width anyways |
630 |
|
66 |
arg_len = min_w; |
631 |
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
66 |
arg_value = pass == 2 ? get_branch_value(parse_res, modifier, arg_len) : 0; |
632 |
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 4002 times.
|
4077 |
} else if(is_implied_rep) { |
633 |
|
75 |
arg_len = 0; |
634 |
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
|
75 |
rep_count = getnum(parse_res.arg); |
635 |
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
75 |
if(foundlabel && !foundlabel_static) asar_throw_error(0, error_type_block, error_id_no_labels_here); |
636 |
4/4
✓ Branch 0 taken 2760 times.
✓ Branch 1 taken 1242 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 2724 times.
|
4002 |
} else if(parse_res.kind == addr_kind::imp || parse_res.kind == addr_kind::a) { |
637 |
|
1278 |
arg_len = 0; |
638 |
|
|
} else { |
639 |
|
2724 |
int old_optimize = optimizeforbank; |
640 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2700 times.
|
2724 |
if(kind_info.flags & flag_bank_0) optimizeforbank = 0; |
641 |
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2634 times.
|
2700 |
else if(kind_info.flags & flag_bank_k) optimizeforbank = -1; |
642 |
2/2
✓ Branch 0 taken 2712 times.
✓ Branch 1 taken 12 times.
|
2724 |
arg_len = get_real_len(min_w, max_w, modifier, parse_res); |
643 |
|
2712 |
optimizeforbank = old_optimize; |
644 |
4/4
✓ Branch 0 taken 904 times.
✓ Branch 1 taken 1808 times.
✓ Branch 2 taken 902 times.
✓ Branch 3 taken 2 times.
|
2712 |
arg_value = pass == 2 ? getnum(parse_res.arg) : 0; |
645 |
|
|
} |
646 |
|
|
|
647 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4129 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2072 times.
|
4129 |
assert((kind_info.allowed_widths & 1<<arg_len) != 0); |
648 |
|
4129 |
uint8_t opcode = kind_info.opcode_for_width[arg_len]; |
649 |
|
|
|
650 |
4/4
✓ Branch 0 taken 1353 times.
✓ Branch 1 taken 2776 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1335 times.
|
4129 |
if(arg_len == 0 && (kind_info.flags & flag_imm_implied_0)) { |
651 |
|
18 |
arg_len = 1; |
652 |
|
18 |
arg_value = 0; |
653 |
|
|
} |
654 |
|
|
|
655 |
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 4054 times.
|
4129 |
if(is_implied_rep) { |
656 |
3/4
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 174 times.
✓ Branch 3 taken 75 times.
|
249 |
for(int64_t i = 0; i < rep_count; i++) write1(opcode); |
657 |
|
|
} else { |
658 |
1/2
✓ Branch 0 taken 4054 times.
✗ Branch 1 not taken.
|
4054 |
write1(opcode); |
659 |
2/2
✓ Branch 0 taken 2794 times.
✓ Branch 1 taken 1260 times.
|
4054 |
if(arg_len == 0); |
660 |
3/4
✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 1573 times.
✓ Branch 2 taken 1221 times.
✗ Branch 3 not taken.
|
2794 |
else if(arg_len == 1) write1(arg_value); |
661 |
3/4
✓ Branch 0 taken 1063 times.
✓ Branch 1 taken 510 times.
✓ Branch 2 taken 1063 times.
✗ Branch 3 not taken.
|
1573 |
else if(arg_len == 2) write2(arg_value); |
662 |
2/4
✓ Branch 0 taken 510 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 510 times.
✗ Branch 3 not taken.
|
510 |
else if(arg_len == 3) write3(arg_value); |
663 |
|
|
} |
664 |
|
|
|
665 |
4/4
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 3985 times.
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 48 times.
|
4129 |
if(autoclean && pass > 0) { |
666 |
|
|
// should be changed to "can't use autoclean on this instruction"? |
667 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 |
if(arg_len != 3) asar_throw_error(2, error_type_block, error_id_broken_autoclean); |
668 |
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 |
handle_autoclean(parse_res.arg, opcode, snespos - 4); |
669 |
|
|
} |
670 |
|
4129 |
return true; |
671 |
|
20563 |
} |
672 |
|
|
|