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