| 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 |
|
33410 |
opcode(uint8_t byte_, const char *mnem_, addr_kind akind_, int width_, uint32_t flags_ = 0) |
| 82 |
|
33410 |
: byte(byte_), mnem(mnem_), akind(akind_), width(width_), |
| 83 |
|
33410 |
flags(flags_) {} |
| 84 |
|
|
}; |
| 85 |
|
|
|
| 86 |
|
73984 |
static void add_one(mnemonicinfo& mnem, addr_kind kind, int width, uint8_t byte, uint32_t flags) { |
| 87 |
|
73984 |
mnem.allowed_kinds_mask |= 1 << (int)kind; |
| 88 |
|
37570 |
auto& this_part = mnem.types[(int)kind]; |
| 89 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37570 times.
|
37570 |
assert((this_part.allowed_widths & (1 << width)) == 0); |
| 90 |
|
73984 |
this_part.allowed_widths |= 1 << width; |
| 91 |
|
73984 |
this_part.opcode_for_width[width] = byte; |
| 92 |
|
73984 |
this_part.flags |= flags; |
| 93 |
|
73984 |
} |
| 94 |
|
|
|
| 95 |
|
256 |
mnemonic_lookup_t(std::initializer_list<opcode> l) { |
| 96 |
2/2
✓ Branch 0 taken 65792 times.
✓ Branch 1 taken 256 times.
|
66048 |
for(const auto& x : l) { |
| 97 |
2/4
✓ Branch 0 taken 65792 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33410 times.
✗ Branch 3 not taken.
|
98174 |
auto& this_mnem = the_map[x.mnem]; |
| 98 |
|
65792 |
add_one(this_mnem, x.akind, x.width, x.byte, x.flags); |
| 99 |
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 65024 times.
|
65792 |
if(x.flags & flag_imm_implied_0) { |
| 100 |
2/4
✓ Branch 0 taken 390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 390 times.
✗ Branch 3 not taken.
|
390 |
assert(x.akind == addr_kind::imm && x.width == 1); |
| 101 |
|
768 |
add_one(this_mnem, addr_kind::imp, 0, x.byte, x.flags); |
| 102 |
|
|
} |
| 103 |
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 64256 times.
|
65792 |
if(x.akind == addr_kind::a) { |
| 104 |
|
1536 |
add_one(this_mnem, addr_kind::imp, 0, x.byte, x.flags); |
| 105 |
|
|
} |
| 106 |
2/2
✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 62976 times.
|
65792 |
if(x.flags & flag_implied_rep) { |
| 107 |
|
2816 |
add_one(this_mnem, addr_kind::imm, 0, x.byte, x.flags); |
| 108 |
|
|
} |
| 109 |
2/2
✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 62720 times.
|
65792 |
if(x.flags & (flag_imm_width_a | flag_imm_width_xy)) { |
| 110 |
2/4
✓ Branch 0 taken 1560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1560 times.
✗ Branch 3 not taken.
|
1560 |
assert(x.akind == addr_kind::imm && x.width == 1); |
| 111 |
|
3072 |
add_one(this_mnem, x.akind, 2, x.byte, x.flags); |
| 112 |
|
|
} |
| 113 |
|
|
} |
| 114 |
|
256 |
} |
| 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 |
|
6030 |
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 |
|
65550 |
static int64_t startmatch(const string& haystack) { |
| 386 |
|
|
static const char needle[] = {chars...}; |
| 387 |
|
32808 |
size_t haystack_i = 0; |
| 388 |
2/2
✓ Branch 0 taken 32775 times.
✓ Branch 1 taken 2418 times.
|
70386 |
for(size_t i = 0; i < sizeof...(chars); i++) { |
| 389 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32775 times.
|
65550 |
while(haystack[haystack_i] == ' ') haystack_i++; |
| 390 |
2/2
✓ Branch 0 taken 16398 times.
✓ Branch 1 taken 16377 times.
|
65550 |
if(needle[i] != to_lower(haystack[haystack_i++])) return -1; |
| 391 |
|
|
} |
| 392 |
|
4836 |
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 |
|
55782 |
static int64_t endmatch(const string& haystack) { |
| 399 |
|
|
static const char needle[] = {chars...}; |
| 400 |
|
55782 |
int64_t haystack_i = haystack.length()-1; |
| 401 |
2/2
✓ Branch 0 taken 31995 times.
✓ Branch 1 taken 2934 times.
|
69858 |
for(int64_t i = sizeof...(chars)-1; i >= 0; i--) { |
| 402 |
4/6
✓ Branch 0 taken 20781 times.
✓ Branch 1 taken 11214 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20781 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16011 times.
|
63990 |
while(haystack_i >= 0 && haystack[haystack_i] == ' ') haystack_i--; |
| 403 |
6/6
✓ Branch 0 taken 20781 times.
✓ Branch 1 taken 11214 times.
✓ Branch 2 taken 10404 times.
✓ Branch 3 taken 10377 times.
✓ Branch 4 taken 12492 times.
✓ Branch 5 taken 3519 times.
|
63990 |
if(haystack_i < 0 || needle[i] != to_lower(haystack[haystack_i--])) return -1; |
| 404 |
|
|
} |
| 405 |
|
5868 |
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 |
2/2
✓ Branch 0 taken 711 times.
✓ Branch 1 taken 5328 times.
|
12093 |
static parse_result parse_addr_kind(const string& arg, uint32_t allowed_kinds_mask) { |
| 414 |
|
6054 |
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 1428 times.
✓ Branch 1 taken 10665 times.
|
12093 |
if((start_i = startmatch<'#'>(arg)) >= 0) { |
| 422 |
2/4
✓ Branch 0 taken 1428 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 717 times.
✗ Branch 3 not taken.
|
2856 |
RETURN_IF_ALLOWED(imm); |
| 423 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 424 |
|
|
} |
| 425 |
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 10017 times.
|
10665 |
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 144 times.
✓ Branch 1 taken 504 times.
|
648 |
if((end_i = endmatch<',', 's', ')', ',', 'y'>(arg)) >= 0) { |
| 428 |
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
288 |
RETURN_IF_ALLOWED(sy); |
| 429 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 430 |
|
|
} |
| 431 |
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 360 times.
|
504 |
if((end_i = endmatch<')', ',', 'y'>(arg)) >= 0) { |
| 432 |
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
288 |
RETURN_IF_ALLOWED(indy); |
| 433 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 434 |
|
|
} |
| 435 |
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 180 times.
|
360 |
if((end_i = endmatch<',', 'x', ')'>(arg)) >= 0) { |
| 436 |
2/4
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
|
360 |
RETURN_IF_ALLOWED(xind); |
| 437 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 438 |
|
|
} |
| 439 |
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 |
if((end_i = endmatch<')'>(arg)) >= 0) { |
| 440 |
2/4
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
|
360 |
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 342 times.
✓ Branch 1 taken 9675 times.
|
10017 |
if((start_i = startmatch<'['>(arg)) >= 0) { |
| 445 |
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 198 times.
|
342 |
if((end_i = endmatch<']', ',', 'y'>(arg)) >= 0) { |
| 446 |
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
288 |
RETURN_IF_ALLOWED(lindy); |
| 447 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 448 |
|
|
} |
| 449 |
1/2
✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 |
if((end_i = endmatch<']'>(arg)) >= 0) { |
| 450 |
2/4
✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
|
396 |
RETURN_IF_ALLOWED(lind); |
| 451 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 452 |
|
|
} |
| 453 |
|
|
} |
| 454 |
|
4842 |
start_i = 0; |
| 455 |
2/2
✓ Branch 0 taken 1566 times.
✓ Branch 1 taken 8109 times.
|
9675 |
if((end_i = endmatch<',', 'x'>(arg)) >= 0) { |
| 456 |
2/4
✓ Branch 0 taken 1566 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 783 times.
✗ Branch 3 not taken.
|
3132 |
RETURN_IF_ALLOWED(x); |
| 457 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 458 |
|
|
} |
| 459 |
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 7875 times.
|
8109 |
if((end_i = endmatch<',', 'y'>(arg)) >= 0) { |
| 460 |
2/4
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
|
468 |
RETURN_IF_ALLOWED(y); |
| 461 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 462 |
|
|
} |
| 463 |
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 7731 times.
|
7875 |
if((end_i = endmatch<',', 's'>(arg)) >= 0) { |
| 464 |
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
288 |
RETURN_IF_ALLOWED(s); |
| 465 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 466 |
|
|
} |
| 467 |
|
3870 |
end_i = arg.length(); |
| 468 |
|
|
// hoping that by now, something would have stripped whitespace lol |
| 469 |
2/2
✓ Branch 0 taken 3726 times.
✓ Branch 1 taken 4005 times.
|
7731 |
if(arg.length() == 0) { |
| 470 |
2/4
✓ Branch 0 taken 3726 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1863 times.
✗ Branch 3 not taken.
|
7452 |
RETURN_IF_ALLOWED(imp); |
| 471 |
|
✗ |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 472 |
|
|
} |
| 473 |
6/6
✓ Branch 0 taken 3987 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 3879 times.
✓ Branch 4 taken 63 times.
✓ Branch 5 taken 1944 times.
|
4005 |
if(arg == "a" || arg == "A") { |
| 474 |
3/4
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
|
234 |
RETURN_IF_ALLOWED(a); |
| 475 |
|
|
// todo: some hint for "don't name your label "a" "? |
| 476 |
|
18 |
asar_throw_error(1, error_type_block, error_id_bad_addr_mode); |
| 477 |
|
|
} |
| 478 |
2/4
✓ Branch 0 taken 3879 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1944 times.
✗ Branch 3 not taken.
|
7758 |
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 1428 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 180 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 180 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 144 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 198 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1566 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 234 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 144 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 3726 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 108 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 3879 times.
✗ Branch 25 not taken.
|
12075 |
} |
| 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 |
|
7824 |
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 |
1/2
✓ Branch 0 taken 3918 times.
✗ Branch 1 not taken.
|
7824 |
int arg_min_len = getlen(parsed.arg, parsed.kind == addr_kind::imm); |
| 500 |
|
|
int out_len; |
| 501 |
2/2
✓ Branch 0 taken 2340 times.
✓ Branch 1 taken 5484 times.
|
7824 |
if(modifier != 0) { |
| 502 |
2/2
✓ Branch 0 taken 1161 times.
✓ Branch 1 taken 9 times.
|
2340 |
out_len = getlenfromchar(modifier); |
| 503 |
2/4
✓ Branch 0 taken 2322 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2322 times.
|
2322 |
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 669 times.
✓ Branch 1 taken 4815 times.
|
5484 |
if(parsed.kind == addr_kind::imm) { |
| 507 |
4/4
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 288 times.
|
669 |
if(!is_hex_constant(parsed.arg.data())) |
| 508 |
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
93 |
asar_throw_warning(2, warning_id_implicitly_sized_immediate); |
| 509 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
669 |
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 5484 times.
|
5484 |
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 |
|
5484 |
out_len = std::max(arg_min_len, min_len); |
| 525 |
|
|
} |
| 526 |
|
3909 |
return out_len; |
| 527 |
|
|
} |
| 528 |
|
|
|
| 529 |
|
66 |
static int64_t get_branch_value(parse_result& parsed, char modifier, int width) { |
| 530 |
|
33 |
int64_t num = 0; |
| 531 |
|
66 |
num = getnum(parsed.arg); |
| 532 |
|
66 |
bool target_is_abs = foundlabel; |
| 533 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 |
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 60 times.
✓ Branch 1 taken 6 times.
|
66 |
if(target_is_abs) { |
| 540 |
|
|
// cast delta to signed 16-bit, this makes it possible to handle bank-border-wrapping automatically |
| 541 |
|
60 |
int16_t delta = num - (snespos + width + 1); |
| 542 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 |
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 |
5/6
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
|
60 |
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 |
|
60 |
return delta; |
| 550 |
|
|
} else { |
| 551 |
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 |
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 6 times.
|
6 |
return (int16_t)(width==2 ? num : (int8_t)num); |
| 555 |
|
|
} |
| 556 |
|
|
} |
| 557 |
|
|
|
| 558 |
|
70563 |
bool asblock_65816(char** word, int numwords) |
| 559 |
|
|
{ |
| 560 |
|
|
// first find the mnemonic from the first word |
| 561 |
|
32835 |
int word_i = 0; |
| 562 |
|
32835 |
bool autoclean = false; |
| 563 |
2/2
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 70077 times.
|
70563 |
if(!stricmpwithlower(word[0], "autoclean")) { |
| 564 |
|
243 |
word_i++; |
| 565 |
|
243 |
autoclean = true; |
| 566 |
|
|
} |
| 567 |
1/2
✓ Branch 0 taken 32835 times.
✗ Branch 1 not taken.
|
70563 |
string mnem = word[word_i++]; |
| 568 |
2/2
✓ Branch 0 taken 357858 times.
✓ Branch 1 taken 70563 times.
|
428421 |
for(int i = 0; i < mnem.length(); i++) mnem.raw()[i] = to_lower(mnem[i]); |
| 569 |
|
|
|
| 570 |
|
32835 |
char modifier = 0; |
| 571 |
6/6
✓ Branch 0 taken 70275 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 2340 times.
✓ Branch 3 taken 67935 times.
✓ Branch 4 taken 1170 times.
✓ Branch 5 taken 31665 times.
|
70563 |
if(mnem.length() >= 2 && mnem[mnem.length()-2] == '.') { |
| 572 |
|
2340 |
modifier = mnem[mnem.length()-1]; |
| 573 |
1/2
✓ Branch 0 taken 2340 times.
✗ Branch 1 not taken.
|
2340 |
mnem.truncate(mnem.length()-2); |
| 574 |
|
|
} |
| 575 |
|
|
|
| 576 |
1/2
✓ Branch 0 taken 32835 times.
✗ Branch 1 not taken.
|
32835 |
auto it = mnemonic_lookup.the_map.find(mnem); |
| 577 |
2/2
✓ Branch 0 taken 32820 times.
✓ Branch 1 taken 37743 times.
|
70563 |
if(it == mnemonic_lookup.the_map.end()) return false; |
| 578 |
|
6072 |
mnemonicinfo& mnem_info = it->second; |
| 579 |
|
|
|
| 580 |
|
|
// join together all the other arguments |
| 581 |
|
6072 |
string par; |
| 582 |
2/2
✓ Branch 0 taken 8403 times.
✓ Branch 1 taken 12129 times.
|
20532 |
for(int i = word_i; i < numwords; i++){ |
| 583 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8403 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8403 |
if(i > word_i) par += " "; |
| 584 |
1/2
✓ Branch 0 taken 8403 times.
✗ Branch 1 not taken.
|
8403 |
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 36 times.
✓ Branch 1 taken 12093 times.
|
12129 |
if(mnem_info.allowed_kinds_mask & 1<<(uint32_t)addr_kind::mvn) { |
| 590 |
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
36 |
int count = 0; |
| 591 |
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 |
autoptr<char**> parts = qpsplit(par.raw(), ',', &count); |
| 592 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 |
if(count != 2) asar_throw_error(2, error_type_block, error_id_bad_addr_mode); |
| 593 |
|
36 |
uint8_t opcode = mnem_info.types[(int)addr_kind::mvn].opcode_for_width[2]; |
| 594 |
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 |
write1(opcode); |
| 595 |
|
18 |
int64_t num1 = 0, num2 = 0; |
| 596 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
|
36 |
if(pass == 2) { |
| 597 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
num1 = getnum(parts[0]); |
| 598 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
num2 = getnum(parts[1]); |
| 599 |
|
|
} |
| 600 |
3/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
36 |
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 |
3/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
36 |
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 36 times.
✗ Branch 1 not taken.
|
36 |
write1(num1); |
| 603 |
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 |
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 36 times.
|
36 |
if(autoclean) asar_throw_error(2, error_type_block, error_id_broken_autoclean); |
| 606 |
|
18 |
return true; |
| 607 |
|
36 |
} |
| 608 |
2/2
✓ Branch 0 taken 12075 times.
✓ Branch 1 taken 18 times.
|
12093 |
parse_result parse_res = parse_addr_kind(par, mnem_info.allowed_kinds_mask); |
| 609 |
|
12075 |
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 |
|
12075 |
int min_w = 99; |
| 615 |
|
12075 |
int max_w = 0; |
| 616 |
2/2
✓ Branch 0 taken 96600 times.
✓ Branch 1 taken 12075 times.
|
108675 |
for(int i = 0; i < 8; i++) { |
| 617 |
2/2
✓ Branch 0 taken 20436 times.
✓ Branch 1 taken 76164 times.
|
96600 |
if(kind_info.allowed_widths & 1<<i) { |
| 618 |
2/2
✓ Branch 0 taken 8181 times.
✓ Branch 1 taken 2025 times.
|
20436 |
min_w = std::min(min_w, i); |
| 619 |
|
20436 |
max_w = std::max(max_w, i); |
| 620 |
|
|
} |
| 621 |
|
|
} |
| 622 |
|
|
|
| 623 |
|
|
// compute argument value |
| 624 |
4/4
✓ Branch 0 taken 1428 times.
✓ Branch 1 taken 10647 times.
✓ Branch 2 taken 714 times.
✓ Branch 3 taken 714 times.
|
12075 |
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 |
|
6045 |
int64_t rep_count = 0; |
| 628 |
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 11877 times.
|
12075 |
if(kind_info.flags & flag_branch) { |
| 629 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
|
99 |
assert(min_w == max_w); // can't call get_real_len, but there should be only one width anyways |
| 630 |
|
198 |
arg_len = min_w; |
| 631 |
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
|
198 |
arg_value = pass == 2 ? get_branch_value(parse_res, modifier, arg_len) : 0; |
| 632 |
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 11658 times.
|
11877 |
} else if(is_implied_rep) { |
| 633 |
|
111 |
arg_len = 0; |
| 634 |
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 |
rep_count = getnum(parse_res.arg); |
| 635 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
|
219 |
if(foundlabel) asar_throw_error(0, error_type_block, error_id_no_labels_here); |
| 636 |
4/4
✓ Branch 0 taken 7878 times.
✓ Branch 1 taken 3780 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 3918 times.
|
11658 |
} else if(parse_res.kind == addr_kind::imp || parse_res.kind == addr_kind::a) { |
| 637 |
|
1917 |
arg_len = 0; |
| 638 |
|
|
} else { |
| 639 |
|
7824 |
int old_optimize = optimizeforbank; |
| 640 |
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 7752 times.
|
7824 |
if(kind_info.flags & flag_bank_0) optimizeforbank = 0; |
| 641 |
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 7566 times.
|
7752 |
else if(kind_info.flags & flag_bank_k) optimizeforbank = -1; |
| 642 |
2/2
✓ Branch 0 taken 7806 times.
✓ Branch 1 taken 18 times.
|
7824 |
arg_len = get_real_len(min_w, max_w, modifier, parse_res); |
| 643 |
|
7806 |
optimizeforbank = old_optimize; |
| 644 |
4/4
✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 5204 times.
✓ Branch 2 taken 2590 times.
✓ Branch 3 taken 12 times.
|
7806 |
arg_value = pass == 2 ? getnum(parse_res.arg) : 0; |
| 645 |
|
|
} |
| 646 |
|
|
|
| 647 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6030 times.
|
6030 |
assert((kind_info.allowed_widths & 1<<arg_len) != 0); |
| 648 |
|
12045 |
uint8_t opcode = kind_info.opcode_for_width[arg_len]; |
| 649 |
|
|
|
| 650 |
4/4
✓ Branch 0 taken 4053 times.
✓ Branch 1 taken 7992 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 3999 times.
|
12045 |
if(arg_len == 0 && (kind_info.flags & flag_imm_implied_0)) { |
| 651 |
|
27 |
arg_len = 1; |
| 652 |
|
27 |
arg_value = 0; |
| 653 |
|
|
} |
| 654 |
|
|
|
| 655 |
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 11826 times.
|
12045 |
if(is_implied_rep) { |
| 656 |
3/4
✓ Branch 0 taken 510 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 510 times.
✓ Branch 3 taken 219 times.
|
729 |
for(int64_t i = 0; i < rep_count; i++) write1(opcode); |
| 657 |
|
|
} else { |
| 658 |
1/2
✓ Branch 0 taken 11826 times.
✗ Branch 1 not taken.
|
11826 |
write1(opcode); |
| 659 |
2/2
✓ Branch 0 taken 8046 times.
✓ Branch 1 taken 3780 times.
|
11826 |
if(arg_len == 0); |
| 660 |
3/4
✓ Branch 0 taken 3441 times.
✓ Branch 1 taken 4605 times.
✓ Branch 2 taken 3441 times.
✗ Branch 3 not taken.
|
8046 |
else if(arg_len == 1) write1(arg_value); |
| 661 |
3/4
✓ Branch 0 taken 3075 times.
✓ Branch 1 taken 1530 times.
✓ Branch 2 taken 3075 times.
✗ Branch 3 not taken.
|
4605 |
else if(arg_len == 2) write2(arg_value); |
| 662 |
2/4
✓ Branch 0 taken 1530 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1530 times.
✗ Branch 3 not taken.
|
1530 |
else if(arg_len == 3) write3(arg_value); |
| 663 |
|
|
} |
| 664 |
|
|
|
| 665 |
4/4
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 11613 times.
✓ Branch 2 taken 288 times.
✓ Branch 3 taken 144 times.
|
12045 |
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 288 times.
|
288 |
if(arg_len != 3) asar_throw_error(2, error_type_block, error_id_broken_autoclean); |
| 668 |
1/2
✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
|
288 |
handle_autoclean(parse_res.arg, opcode, snespos - 4); |
| 669 |
|
|
} |
| 670 |
|
6030 |
return true; |
| 671 |
|
70626 |
} |
| 672 |
|
|
|