-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoodasm.cpp
674 lines (564 loc) · 17 KB
/
goodasm.cpp
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include <QDebug>
#include <QFile>
#include <QRandomGenerator>
#include <iostream>
#include "goodasm.h"
#include "galanguage.h"
#include "gainstruction.h"
#include "galexer.h"
#include "gaparser.h"
//Languages
#include "galang8086.h"
#include "galangucom43.h"
#include "galangtlcs47.h"
#include "galangs2000.h"
#include "galangpic16c5x.h"
#include "galangmarc4.h"
#include "galang6502.h"
#include "galang6805.h"
#include "galang8051.h"
#include "galangsm83.h"
#include "galangchip8.h"
#include "galangz80.h"
#include "galangfcard.h"
//Listings
#include "galistingdefault.h"
#include "galistingnasm.h"
#include "galistingc.h"
#include "galistinggo.h"
#include "galistingyarax.h"
#include "galistingmarkdown.h"
#include "galistinghex.h"
#include "galistingga.h"
//Symbol table.
#include "gasymboltable.h"
#ifdef HASREADLINE
#include<readline/readline.h>
#else
void rl_forced_update_display(){
qDebug()<<"I should be updating the readline display but can't on Windows.";
}
#endif
void GoodASM::setLanguage(QString language){
if(languages.empty()){
//List of other languages, in case we need them.
languages.append(new GALang8086());
languages.append(new GALangUcom43());
languages.append(new GALangTLCS47());
languages.append(new GALangS2000()); //EMZ1001
languages.append(new GALangPIC16C5x());
languages.append(new GALangMARC4());
languages.append(new GALang6502());
languages.append(new GALang6805());
languages.append(new GALang8051());
languages.append(new GALangSM83()); //GameBoy.
languages.append(new GALangChip8());
languages.append(new GALangZ80());
languages.append(new GALangFCard());
}
foreach (auto lang, languages) {
if(lang->name==language)
setLanguage(lang);
}
//Empty language if we need one.
if(!lang) setLanguage(0);
}
//List of supported language names.
QVector<QString> GoodASM::languageNames(){
QVector<QString> v;
if(languages.empty())
setLanguage("");
foreach(auto l, languages){
v.append(l->name);
}
return v;
}
void GoodASM::setListing(QString style){
if(listings.empty()){
//Listing styles, for formatting text output.
listings.append(new GAListingDefault());
listings.append(new GAListingNasm());
listings.append(new GAListingC());
listings.append(new GAListingGo());
listings.append(new GAListingYaraX());
listings.append(new GAListingMarkdown());
listings.append(new GAListingHex());
listings.append(new GAListingGA());
}
listing=0;
foreach(auto l, listings){
if(l->name==style)
setListing(l);
}
//You really need a name to your listing.
assert(listing);
}
GoodASM::GoodASM(QString language){
setLanguage(language);
setListing("default");
assert(lang);
symbols.lang=lang;
}
GoodASM::GoodASM(GALanguage *language) {
//Parameter is language, or maybe null.
setLanguage(language);
setListing("default");
assert(lang);
symbols.lang=lang;
}
QString GoodASM::opcodeTable(){
/* Here, i is the high nybble and j is the low nybble.
* We are disassembling a short buffer with the first
* byte set to IJ and then including its verb name
* in an ASCII table for the user's convenience.
*/
QString toret="";
char nyb[]="0123456789abcdef";
//Top row header.
toret+=" \t";
for(int j=0; j<0x10; j++){
toret+="0";
toret+=QChar(nyb[j]);
toret+="\t";
}
toret+="\n";
for(int i=0; i<0x10; i++){
toret+=QChar(nyb[i]);
toret+="0\t";
for(int j=0; j<0x10; j++){
QByteArray a;
QString name="?";
a.append(((i<<4)|j)&0xff);
a.append((char) 0);
a.append((char) 0);
a.append((char) 0);
clear();
load(a);
if(at(0).mnem)
name=at(0).mnem->name;
toret+=name+"\t";
}
toret+="\n";
}
return toret;
}
GoodASM::~GoodASM(){
//Free the selected language if it's not in the list.
if(lang && !languages.contains(lang))
delete lang;
//Free the potential languages.
foreach(auto l, languages)
delete l;
}
//Returns filename and line number.
QString GoodASM::addr2line(int64_t adr){
return QString("%1:%2").arg(filename).arg(line);
}
//Logs an error at current file and line.
void GoodASM::error(QString message){
errors.append(addr2line()+"\t"+message);
}
//Prints all errors from the latest run.
int GoodASM::printErrors(){
for(int i=0; i<errors.count(); i++){
std::cout<<errors[i].toStdString()<<"\n";
}
return errors.count();
}
//Clears errrors. Call this between runs.
void GoodASM::clearErrors(){
errors.clear();
}
//Formats a source line for printing.
QString GoodASM::formatSource(QString label, QString code,
QString comment, QString comment2){
assert(listing);
return listing->formatSource(label, code, comment, comment2);
}
//Returns an array of tab completions for the REPL mode.
char** GoodASM::readline_completions(const char *fragment, const char *line,
int start, int end){
/* This function returns an array for GNU ReadLine's autocompletion.
* The first element replaces the selected fragment, while the latter
* lines are presented in a list for the user.
*
* At the very least we need to complete verbs, but it's also handy to
* show the usage of commands that might be completed. Every element and
* the array itself will be freed after usage, and the array ends with
* the first null pointer.
*/
//No word to complete, so we complete the whole sentence.
if(start==end){
//FIXME: Replace this shotgun parser with the real parser.
QString l(line);
QStringList words=l.split(" ");
if(words.length()>0){
QString v=words[0];
std::cout<<"\n";
foreach(auto m, lang->mnemonics){
if(m->name==v)
std::cout<<m->examplestr.toStdString()<<"\n";
}
}
rl_forced_update_display();
return 0; //No suggestions for Readline.
}
/* Here we do have a word, but we don't know what part of speech it
* is. For now, we ignore the part of speech and return completions
* for all known mnemonic verbs and symbols that match.
*/
QStringList list;
assert(lang);
assert(lang->mnemonics.size()>0);
foreach(auto m, lang->mnemonics)
if(m->name.startsWith(fragment))
list.append(m->name);
list.append(symbols.completions(fragment));
list.removeDuplicates();
list.sort();
//Empty result when there are no matches.
if(list.size()==0)
return 0;
//Result is a structure.
char** result=(char**) malloc(sizeof(char*)*list.size()+2*sizeof(char*));
//Skip ahead if there's exactly one unique match.
if(list.size()==1){
result[0]=strdup(list[0].toStdString().c_str());
result[1]=0;
result[2]=0;
return result;
}
//By here, we know that we have more than one match to choose from.
//First element replaces whatever was being completed.
result[0]=strdup(fragment);
//Further elements are potential completion words.
int i=1;
foreach(QString s, list)
result[i++]=strdup(s.toStdString().c_str());
result[i]=0; //Null terminator.
return result;
}
void GoodASM::setLanguage(GALanguage *language){
lang=language;
if(!lang) //Apply if it's an empty language.
lang=new GALanguage();
lang->setGoodASM(this);
assert(lang);
}
void GoodASM::setListing(GAListing *style){
listing=style;
if(!listing) //Apply if empty language.
listing=new GAListingDefault();
assert(listing);
}
// Text input or disassembly.
QString GoodASM::source(){
assert(listing);
listing->render(this); //First pass sets column widths.
return(listing->render(this)); //Second is real.
}
// All the self testing.
bool GoodASM::selftest_all(){
return selftest_examples()
&& selftest_collisions()
&& selftest_overlap()
&& selftest_length();
}
// Are the mask strings null-terminated?
bool GoodASM::selftest_length(){
foreach(auto m, lang->mnemonics){
uint64_t l=m->length;
if(m->opcodemask[l]){
qDebug()<<"Mask length is suspicious: "<<m->examplestr;
return false;
}
}
return true;
}
// Do disassemblies ever violate assertions?
bool GoodASM::selftest_fuzz(){
qDebug()<<"Fuzz testing the disassembler.";
assert(lang);
QRandomGenerator *rnd=QRandomGenerator::system();
for(int i=0; i<0x10000; i++){
uint32_t val=rnd->generate();
QByteArray a;
a.append(val&0xff);
a.append((val>> 8)&0xff);
a.append((val>>16)&0xff);
a.append((val>>24)&0xff);
clear();
load(a);
source();
if(duplicates>0){
qDebug()<<"Duplicates found in fuzzing.";
qDebug()<<hexdump();
return false;
}
}
return true; //Haven't crashed yet!
}
// Do the examples line up?
bool GoodASM::selftest_examples(){
uint64_t passes=0;
uint64_t fails=0;
qDebug()<<"Testing each example in the language.";
assert(lang);
foreach(auto m, lang->mnemonics){
QString lineinput=m->examplestr;
clear(true);
load(lineinput);
if(instructions.length()>0
&& instructions[0].mnem
&& instructions[0].mnem==m
){
qDebug()<<"TEST: "<<lineinput;
passes++;
QByteArray a=bytes;
load(a);
source();
if(instructions.length()>0
&& instructions[0].mnem
&& instructions[0].mnem==m){
}else{
qDebug()<<"Reflection failed.";
}
if(duplicates>0){
qDebug()<<"DUPE: "<<lineinput;
fails++;
}
if(!symbols.complete()){
qDebug()<<"Undefined symbols: "<<symbols.missingSymbols();
fails++;
}
}else{
qDebug()<<"FAIL: "
<<(lineinput!=""?lineinput:m->name);
fails++;
}
clear();
}
if(!fails)
return true;
return false;
}
// Are there collisions?
bool GoodASM::selftest_collisions(){
qDebug()<<"Testing for mask collisions.";
assert(lang);
foreach(auto m, lang->mnemonics){
foreach(auto n, lang->mnemonics){
if(m!=n){
//TODO: Check the collision of masks here.
}
}
}
return true;
}
// Does a parameter's mask overlap with the opcode?
bool GoodASM::selftest_overlap(){
qDebug()<<"Testing for mask overlaps.";
assert(lang);
foreach(auto m, lang->mnemonics){
//Working mask, to ensure all bits are used.
uint8_t wmask[GAMAXLEN];
memcpy(wmask, m->opcodemask, m->length);
foreach(auto p, m->params){
for(int i=0; i<m->length; i++){
wmask[i]|=p->mask[i];
if(m->opcodemask[i] & p->mask[i]){
qDebug()<<"Mask collision: "<<m->examplestr;
return false;
}
}
}
for(int i=0; i<m->length; i++){
if(wmask[i]&m->dcmask[i]){
qDebug()<<"Consumed don't-care bit: "<<m->examplestr;
qDebug()<<"Byte"<<i<<": "<<Qt::hex<<(uint8_t) (wmask[i]&m->dcmask[i]);
return false;
}
if((wmask[i]|m->dcmask[i])!=0xff){
qDebug()<<"Empty mask bit: "<<m->examplestr;
qDebug()<<"Byte"<<i<<": "<<Qt::hex<<(uint8_t) wmask[i];
return false;
}
}
}
return true;
}
// Clears all code or data.
void GoodASM::clear(bool symbols){
instructions.clear();
bytes.clear();
clearErrors();
if(symbols) this->symbols.clear();
workingadr=baseaddress;
}
// Binary input or assembly.
QByteArray GoodASM::code(){
//Disassembly is easy, just send back the source bytes.
if(type==DISASSEMBLY)
return this->bytes;
//Assembly is harder. Walk the instructions and append them.
if(type==ASSEMBLY){
QByteArray code;
foreach(GAInstruction ins, instructions)
code.append(ins.code());
return code;
}
//Empty output when undefined.
return QByteArray();
}
// Code in hex.
QString GoodASM::hexdump(){
QByteArray bytes=code();
QString hex="";
int i;
for(i=0; i<bytes.length(); i++){
hex.append(QString::asprintf("%02x ",0x00FF & bytes[i]));
if(i%32==15)
hex.append(" ");
if(i%32==31)
hex.append("\n");
}
if(i%32!=31)
hex.append("\n");
return hex;
}
// Load a binary for disassembly.
void GoodASM::load(QByteArray bytes){
clear(true); //Clear code and symbols.
//Load the binary.
type=DISASSEMBLY;
this->bytes=bytes;
duplicates=0;
workingadr=baseaddress;
GAInstruction ins=at(baseaddress);
for(int i=0; ins.adr<baseaddress+bytes.length(); i++){
append(ins);
ins=ins.next();
}
}
// Disassemble bytes and append them to the working project.
void GoodASM::loadFragment(QString label, QByteArray bytes, QString comment){
char buf[GAMAXLEN];
uint64_t base=0; //Offset into the buffer that we are adding.
uint32_t len; //Length of the working instruction.
while(base<bytes.length()){
//Load bytes, or zeroes.
for(int i=0; i<lang->maxbytes; i++)
buf[i]=(i+base<bytes.length()?bytes.at(i+base):0);
GAInstruction ins=lang->decode(workingadr, buf, len);
ins.comment=comment;
if(label.length()>0){
ins.label=label;
label="";
}
if(base+len>bytes.length()){
GAInstruction data=GAInstruction(this, buf[0]);
data.comment=comment+" (data)";
if(label.length()>0){
data.label=label;
label="";
}
base+=1;
append(data);
}else{
base+=len;
append(ins);
}
}
}
// Load source for assembly.
void GoodASM::load(QString source){
type=ASSEMBLY;
/* Here we keep parsing the source and abandoning the response
* until the hash of the symbol table stops changing.
*/
QByteArray last, next;
int count=0;
do{
last=symbols.symbolhash(); //Has the last symbols.
clear(false); //Clear code but not symbols.
GALexer lex(source);
GAParser p(&lex, this);
p.parse();
next=symbols.symbolhash();
}while(count++<10 && last!=next);
if(!symbols.complete())
error("Symbols incomplete after last pass.");
if(count>5)
error(QString("After %1 passes, symbols disagree.").arg(count));
}
//Loads a filename for assembly.
void GoodASM::loadFile(QString file){
QByteArray bytes;
QFile input(file);
if(file=="-")
input.open(stdin, QIODevice::ReadOnly);
else
input.open(QFile::ReadOnly);
bytes=input.readAll();
if(bytes.length()==0)
exit(1);
pushFilename(file);
load(QString(bytes));
popFilename();
}
//Pushes the filename before including another.
void GoodASM::pushFilename(QString name){
filenames.push(filename);
filenamelines.push(line);
filename=name;
line=0;
}
//Pops the filename after including another.
QString GoodASM::popFilename(){
assert(filenames.count()>0);
assert(filenamelines.count()>0);
//As an exception, we keep the last filename after popping it.
QString newfn=filenames.pop();
if(newfn.length()>0)
filename=newfn;
line=filenamelines.pop();
return filename;
}
// Insert the next instruction.
void GoodASM::append(GAInstruction ins){
ins.adr=workingadr;
instructions.append(ins);
if(type==ASSEMBLY){
//Only append code if it's not already there.
bytes.append(ins.code());
address(); //recalculate working address.
}else{
workingadr+=ins.len;
}
}
// Grab an instruction from an address.
GAInstruction GoodASM::at(uint64_t adr){
GAInstruction ins=lang->dis(adr);
return ins;
}
// Confusing when epsilon!=1.
uint8_t GoodASM::byteAt(uint64_t adr){
/* On architectures like PIC16C5x, we swap the bytes
* as we load them so that our code more closely matches
* the datasheet.
*/
if(lang->swapwordonload)
adr^=1;
//Important to adjust for the base address.
adr-=baseaddress;
if(adr<bytes.length())
return bytes[(int) adr];
if(verbose)
qDebug()<<"Illegal fetch from adr"<<adr;
return 0;
}
//Calculated current working address.
uint64_t GoodASM::address(){
workingadr=baseaddress+bytes.length();
return workingadr;
}