-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgaparameter.cpp
More file actions
566 lines (467 loc) · 16 KB
/
gaparameter.cpp
File metadata and controls
566 lines (467 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include <QList>
#include <QDebug>
#include "galanguage.h"
#include "gaparameter.h"
GAParameter::GAParameter(const char* mask){
setMask(mask);
}
void GAParameter::setMask(const char* mask){
memcpy(this->mask, mask, GAMAXLEN);
}
GAParameter::~GAParameter(){
for(int i=0; i<params.size(); i++){
delete params[i];
}
}
//Does a parsed parameter match this parameter?
int GAParameter::match(GAParserOperand *op, int len){
/* This implementation is for numeric parameters.
* We need to be careful to return failure if
* the parameter cannot fit, so as to allow overloaded
* opcodes like "ldx @u8" and "ldx @u16" on 6502.
*/
if(len==0){
qDebug()<<"Len="<<len<<"and"<<op->value;
}
//Prefix must match.
if(op->prefix!=prefix
//Odd exception for negative immediates.
&& ( op->prefix!=prefix+"-" && !isSigned)
)
return 0;
//Suffix must match.
if(op->suffix!=suffix)
return 0;
/* This is a little tricky, in that the value needs to fit in
* range but we don't necessarily know the value of the symbol
* yet on this pass.
*
* Supposing the value is absolute, we might get away with guessing
* zero for unknown symbols on the first pass, then repeating
* passes until the hash of all symbols remains the same for two passes.
*/
if(!isSigned){
int64_t max=
(((uint64_t) 1)<<rawbitcount(len));
uint64_t val=op->uint64(false)-offset;
//Signed value illegal in unsigned parameter.
if(val<0) return 0;
if(val >= max) return 0;
}else{
int64_t val=op->int64(false)-offset;
//Positive values must fit range.
if(val >= ~(((int64_t) -1)& ~((((int64_t) 1)<<(rawbitcount(len)-1))))) return 0;
//Negative values must also fit range.
if(val<0 && (val| ~(((int64_t) 1)<<(rawbitcount(len)-1)))!=-1) return 0;
}
//We're good if we get here.
return 1;
}
int GAParameterRelative::match(GAParserOperand *op, int len){
//FIXME: We should do some sort of a match here.2
//It's lazy to just return success.
if(op->prefix=="-" && this->prefix=="")
return 1; //Negative value, but a match.
if(op->prefix!=this->prefix)
return 0;
return 1;
}
int GAParameterAddress::match(GAParserOperand *op, int len){
//FIXME: We should do some sort of a match here.
//It's lazy to just return success.
if(op->prefix!=this->prefix) return 0;
//Check that value fits in the field.
//For now, we allow either offset or unoffset to be generous.
if(
(op->uint64(false)>=(1<<rawbitcount(len)))
&& (op->uint64(false)+offset>=(1<<rawbitcount(len)))
)
return 0;
return 1;
}
/* Numbered registers are a little tricky to match, in that
* they need to be a number in the instruction but they might
* also be a name in the decoding. */
int GAParameterRegNum::match(GAParserOperand *op, int len){
if(op->prefix!=this->prefix)
return 0; //Wrong type.
if(!op->value.startsWith("r"))
return 0; //Not a register number.
return 1; //Success!
}
/* Numbered bit, used in bit setting and clearing instructions. */
int GAParameterBitIndex::match(GAParserOperand *op, int len){
if(op->prefix!=this->prefix)
return 0; //Bits rarely have prefixes.
//FIXME: Check the bit range.
return 1; //Success!
}
//Absolute or immediate.
QString GAParameterRegNum::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen) {
uint64_t p=rawdecode(lang,adr,bytes,inslen);
QString rendering=prefix
+QString::asprintf("r%d",(unsigned int) p)
+suffix;
return rendering;
}
//Encode a parser parameter to bytes, which are OR'ed into target.
void GAParameterRegNum::encode(GALanguage *lang, uint64_t adr,
QByteArray &bytes,
GAParserOperand op,
int inslen){
uint64_t val=lang->find_reg_by_name(op.value);
rawencode(lang,adr,bytes,op,inslen,val);
}
QString GAParameterBitIndex::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen){
uint64_t p=rawdecode(lang,adr,bytes,inslen);
QString rendering=prefix
+QString::asprintf("bit%d",(unsigned int) p)
+suffix;
return rendering;
}
void GAParameterBitIndex::encode(GALanguage *lang,
uint64_t adr, QByteArray &bytes,
GAParserOperand op,
int inslen
){
uint64_t val=lang->find_bit_by_name(op.value);
rawencode(lang,adr,bytes,op,inslen,val);
}
//Group value.
int GAParameterGroup::match(GAParserOperand *op, int len){
assert(len>0);
//Fail to match on other grouping symbols.
if(op->prefix!=groupstart)
return 0;
//Every child must match. This allows for nested groups.
if(op->children.count()==this->params.count()){
int i=0;
foreach (auto param, this->params){
if(!param->match(&op->children[i], len))
return 0;
i++;
}
return 1;
}
return 0;
}
//Explicit register name, must exactly match.
int GAParameterRegName::match(GAParserOperand *op, int len){
assert(len>0);
return
op->value==name
&& op->prefix==prefix
&& op->suffix==suffix;
;
}
//Absolute or immediate.
QString GAParameter::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen) {
//Right value for positives.
int64_t p=rawdecode(lang,adr,bytes,inslen);
p+=offset;
QString rendering;
if(!isSigned){
/* Most parameters are unsigned, and the trick there is just to keep
* the rendering short enough that the zeroes don't feel weird.
*/
rendering=prefix
+QString::asprintf("0x%04x",(unsigned int) p)
+suffix;
}else{
/* Signed parameters are a little harder. Here, we need to make sure
* that the decoded string encodes back as an encoded string.
*/
rendering=prefix
+(p<0?"-":"")
+QString::asprintf("0x%04x",
p>=0?(unsigned int) p:(unsigned int) -p)
+suffix;
}
return rendering;
}
//How many bits are in the mask?
uint64_t GAParameter::rawbitcount(int inslen){
int count=0;
for(int i=0; i<inslen; i++){
for(int j=0; j<8; j++){
if(mask[i] & (1<<j))
count++;
}
}
return count;
}
//Raw decode function, intentionally not virtual.
int64_t GAParameter::rawdecode(GALanguage *lang, uint64_t adr,
const char *bytes, int inslen,
bool sign){
return lang->rawdecode(this, adr, bytes, inslen);
}
//Raw encode function, also intentionally not virtual.
void GAParameter::rawencode(GALanguage *lang,
uint64_t adr, QByteArray &bytes,
GAParserOperand op,
int inslen,
int64_t val
){
//If you need to override this, do it in GALanguage.
lang->rawencode(adr, bytes, op, inslen, this, val);
}
//Encode a parser parameter to bytes, which are OR'ed into target.
void GAParameter::encode(GALanguage *lang, uint64_t adr,
QByteArray &bytes,
GAParserOperand op,
int inslen){
rawencode(lang, adr, bytes, op, inslen,
op.int64(true)-offset);
}
//Encode a parser parameter to bytes, which are OR'ed into target.
void GAParameterGroup::encode(GALanguage *lang,
uint64_t adr, QByteArray &bytes,
GAParserOperand op,
int inslen){
assert(inslen>0);
if(op.children.count()==this->params.count()){
int i=0;
foreach (auto param, this->params){
//FIXME: bytes.length() is not set yet.
assert(param->match(&op.children[i], inslen));
param->encode(lang,adr,bytes,op.children[i], inslen);
i++;
}
}
}
//PC-relative
QString GAParameterRelative::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen){
int64_t p=rawdecode(lang,adr,bytes, inslen, true);
uint64_t tadr=adr+offset+p*multiple;
QString rendering=prefix
+QString::asprintf("0x%04x",(unsigned int) tadr)
+suffix;
return rendering;
}
//Encode a parser parameter to bytes, which are OR'ed into target.
void GAParameterRelative::encode(GALanguage *lang, uint64_t adr,
QByteArray &bytes,
GAParserOperand op,
int inslen){
uint8_t buf[10];
for(int i=0; i<lang->maxbytes && i<inslen; i++)
buf[i]=0;
int64_t val=op.int64(true);
//qDebug()<<"Encoding relative jump to "<<val<<"from "<<adr;
val-=adr;
val-=offset;
val/=multiple;
rawencode(lang, adr, bytes, op, inslen, val);
}
//Symbol address.
QString GAParameterAddress::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen){
uint64_t p=rawdecode(lang,adr,bytes,inslen);
QString rendering=prefix
+QString::asprintf("0x%04x",(unsigned int) (p-offset))
+suffix;
return rendering;
}
//High part of address is fixed, low part provided in the instruction.
QString GAParameterPageAddress::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen){
uint64_t p=rawdecode(lang,adr,bytes,inslen);
//FIXME: Apply the page here.
QString rendering=prefix
+QString::asprintf("0x%04x",(unsigned int) p)
+suffix;
return rendering;
}
QString GAParameterGroup::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen){
QString rendering=QString()+groupstart;
for(auto i=params.constBegin(); i!=params.constEnd(); i++){
rendering+=(*i)->decode(lang, adr, bytes, inslen);
if(i+1!=params.constEnd())
rendering+=", ";
}
rendering+=groupend;
//qDebug()<<"Rendered"<<rendering;
return rendering;
}
GAParameter::GAParameter(){
//this->mask="\x00\x00\x00";
setMask("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
}
//Immediate values are direct numbers.
GAParameterImmediate::GAParameterImmediate(const char* mask, uint64_t offset){
setMask(mask);
this->offset=offset;
prefix="#";
}
//Absolute values are addresses.
GAParameterAbsolute::GAParameterAbsolute(const char* mask, uint64_t offset){
setMask(mask);
this->offset=offset;
prefix="@";
}
//Ports are an extra memory space.
GAParameterPort::GAParameterPort(const char* mask){
setMask(mask);
prefix="%";
}
//PC-relative addressing with some offset.
GAParameterRelative::GAParameterRelative(const char* mask, int offset, int multiple){
setMask(mask);
this->offset=offset;
this->multiple=multiple;
this->prefix="";
this->isSigned=true;
}
//An address itself, rather than the value at that address.
GAParameterAddress::GAParameterAddress(const char* mask, int offset){
setMask(mask);
prefix="";
this->offset=offset;
}
//Offset into the current page.
GAParameterPageAddress::GAParameterPageAddress(const char* mask, uint32_t pagemask){
setMask(mask);
this->pagemask=pagemask;
prefix="";
}
//Register numbers are counted from a bitfield.
GAParameterRegNum::GAParameterRegNum(const char* mask){
setMask(mask);
}
//3 bits representing a bit choice.
GAParameterBitIndex::GAParameterBitIndex(const char* mask){
setMask(mask);
}
GAParameterGroup::GAParameterGroup(){
}
GAParameterGroup::GAParameterGroup(const char symbol){
groupstart=symbol;
switch(symbol){
case '(':
groupend=')';
break;
case '[':
groupend=']';
break;
}
//Crash here if the group end is unsupported.
assert(groupend!='_');
}
GAParameterRegName::GAParameterRegName(const QString name){
this->name=name;
}
QString GAParameterRegName::decode(GALanguage *lang, uint64_t adr, const char *bytes, int inslen){
//A register name must be a register name.
if(!lang->regnames.contains(name)){
qDebug()<<"Names missing"<<name;
}
assert(lang->regnames.contains(name));
return prefix+name+suffix;
}
//Insert parameters into the group.
GAParameterGroup* GAParameterGroup::insert(GAParameter* p){
memcpy(p->opcodemask, opcodemask, GAMAXLEN);
memcpy(p->invertmask, invertmask, GAMAXLEN);
params.append(p);
for(int i=0; i<GAMAXLEN; i++){
mask[i]|=p->mask[i];
}
return this;
}
//Insert parameters into the group.
GAParameterGroup* GAParameterGroup::operator<<(GAParameter* p){
insert(p);
return this;
}
//Registers an immediate argument, given a mask of the original size.
GAParameterGroup* GAParameterGroup::imm(const char *mask, uint64_t offset){
//Prefixed by #
insert(new GAParameterImmediate(mask, offset));
return this;
}
//Same but the immediate is signed.
GAParameterGroup* GAParameterGroup::simm(const char *mask, uint64_t offset){
//Prefixed by #
GAParameterImmediate *p= new GAParameterImmediate(mask, offset);
p->isSigned=true;
insert(p);
return this;
}
//Same for an absolute argument, prefixed by @.
GAParameterGroup* GAParameterGroup::abs(const char *mask, uint64_t offset){
insert(new GAParameterAbsolute(mask, offset));
return this;
}
//Adds a port number by just its mask, preceeded by %.
GAParameterGroup* GAParameterGroup::port(const char *mask){
insert(new GAParameterPort(mask));
return this;
}
//PC-relative address.
GAParameterGroup* GAParameterGroup::rel(const char *mask, int offset, int multiple){
insert(new GAParameterRelative(mask, offset, multiple));
return this;
}
//Absolute address is the same as relative from zero.
GAParameterGroup* GAParameterGroup::adr(const char *mask, int offset){
insert(new GAParameterAddress(mask, offset));
return this;
}
// Page-offset address, common on 4-bit chips.
GAParameterGroup* GAParameterGroup::pageadr(const char *mask, uint32_t pagemask){
//qDebug()<<"FIXME: Faking a page address. https://github.com/travisgoodspeed/goodasm/issues/55";
insert(new GAParameterPageAddress(mask, pagemask));
return this;
}
//Absolute indexed by X.
GAParameterGroup* GAParameterGroup::absx(const char *mask){
abs(mask);
insert(new GAParameterRegName("x"));
return this;
}
//Absolute indexed by Y.
GAParameterGroup* GAParameterGroup::absy(const char *mask){
abs(mask);
insert(new GAParameterRegName("y"));
return this;
}
//Explicit register by name.
GAParameterGroup* GAParameterGroup::regname(const QString name,
const QString prefix,
const QString suffix){
GAParameterRegName *reg=new GAParameterRegName(name);
reg->prefix=prefix;
reg->suffix=suffix;
insert(reg);
return this;
}
//Explicit register by name, the indirected.
GAParameterGroup* GAParameterGroup::regnameind(const QString name){
GAParameterRegName *reg=new GAParameterRegName(name);
reg->prefix="@";
reg->suffix="";
insert(reg);
return this;
}
//Numbered register with a mask, indirect access.
GAParameterGroup* GAParameterGroup::regind(const char *mask){
GAParameterRegNum *reg=new GAParameterRegNum(mask);
reg->prefix="@";
insert(reg);
return this;
}
//Numbered register with a mask, direct access.
GAParameterGroup* GAParameterGroup::regdir(const char *mask){
insert(new GAParameterRegNum(mask));
return this;
}
//Group of parameters.
GAParameterGroup* GAParameterGroup::group(const char g){
GAParameterGroup *pg=new GAParameterGroup(g);
insert(pg);
return pg;
}
//Bit index.
GAParameterGroup* GAParameterGroup::bit3(const char *mask){
insert(new GAParameterBitIndex(mask));
return this;
}