-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgalanguage.cpp
More file actions
325 lines (287 loc) · 9.15 KB
/
galanguage.cpp
File metadata and controls
325 lines (287 loc) · 9.15 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
#include<QDebug>
#include "galanguage.h"
#include "gainstruction.h"
#include "goodasm.h"
#include "gamnemonic.h"
#include "gaparser.h"
GALanguage::~GALanguage(){
foreach (auto m, mnemonics) {
delete m;
}
}
//Raw encode function, used by parameters but not opcodes.
void GALanguage::rawencode(uint64_t adr, QByteArray &bytes,
GAParserOperand op,
int inslen,
GAParameter *param,
int64_t val
){
uint8_t buf[GAMAXLEN];
/* For big endian, we count bytes down from the last
* byte while populating with the least bits. Little
* endian is the reverse, counting forward from the first
* byte.
*/
if(endian==GALanguage::BIG)
for(int i=inslen-1; i>=0; i--){
int mb=param->mask[i];
buf[i]=0;
for(int j=0; j<8; j++){
if(mb & (1<<j)){
if(val&1)
buf[i]|=(1<<j);
val>>=1;
}
}
}
else //Little endian.
for(int i=0; i<inslen; i++){
int mb=param->mask[i];
buf[i]=0;
for(int j=0; j<8; j++){
if(mb & (1<<j)){
if(val&1)
buf[i]|=(1<<j);
val>>=1;
}
}
}
for(int i=0; i<bytes.length() && i<inslen; i++){
//Invert bits of just this parameter from the invert mask.
buf[i]^=(param->invertmask[i]¶m->mask[i]);
//Apply those to the output.
bytes[i]=bytes[i]|buf[i];
}
}
//Raw decode function, used by parameters but not by opcodes.
int64_t GALanguage::rawdecode(GAParameter *param, uint64_t adr,
const char *bytes, int inslen){
int64_t val=0; // Value to return.
/* For little endian, we count bytes down from the last
* byte while populating with the least bits. Big
* endian is the reverse, counting forward from the first
* byte.
*/
if(endian==GALanguage::LITTLE)
for(int i=inslen-1; i>=0; i--){
int mb=param->mask[i];
for(int j=7; j>=0; j--){
if(mb & (1<<j)){
val<<=1;
if((bytes[i]^param->invertmask[i])&(1<<j)){
val|=1;
}
}
}
}
else //Big endian.
for(int i=0; i<inslen; i++){
int mb=param->mask[i];
for(int j=7; j>=0; j--){
if(mb & (1<<j)){
val<<=1;
if((bytes[i]^param->invertmask[i])&(1<<j)){
val|=1;
}
}
}
}
if(param->isSigned){ //Sign extend if necessary.
int count=param->rawbitcount(inslen); //Count of bits after decoding.
if(val&(1<<(count-1))){ //If the highest bit is set,
val^=(1<<count)-1; // first flip the used bits.
val^=-1; // then flip all bits.
}
}
return val;
}
uint64_t GALanguage::find_reg_by_name(QString name){
/* By default, every valid register name needs
* to begin with the letter R and then an integer
* number. r0 and r1 on 8051, for example.
*
* Override these functions if you need to do something
* more complex.
*/
assert(name.length()>=2);
assert(name[0]==QString("r"));
QString sub=name.mid(1,name.length()-1);
bool okay;
int regnum=sub.toInt(&okay);
if(!okay)
qDebug()<<"Error parsing register name: "<<sub;
return regnum;
}
uint64_t GALanguage::find_bit_by_name(QString name){
/* Bit names come two ways:
* bit1, bit2, bit3, etc
* 1, 2, 4, 8.
*
* Override these functions if you need to do something
* more complex.
*/
if(name.length()>=4 && name.startsWith("bit")){
QString sub=name.mid(3,name.length()-3);
bool okay;
int bitnum=sub.toInt(&okay);
if(!okay)
qDebug()<<"Error parsing bitfield name: "<<sub;
return bitnum;
}else{
bool okay;
int i=name.toInt(&okay);
if(!okay)
qDebug()<<"Error parsing bitfield mask "<<name;
switch(i){
case 1: return 0;
case 2: return 1;
case 4: return 2;
case 8: return 3;
case 0x10: return 4;
case 0x20: return 5;
case 0x40: return 6;
case 0x80: return 7;
}
qDebug()<<"Bitfield mask is out of range: "<<name;
return 0;
}
}
QString GALanguage::find_regname(uint64_t num){
QString name="";
name.setNum(num);
return "r"+name;
}
void GALanguage::setGoodASM(GoodASM* gasm){
assert(gasm);
this->goodasm=gasm;
}
//Disassemble an instruction from an address.
GAInstruction GALanguage::dis(uint64_t adr){
assert(goodasm);
/* By default, we just output db directives.
* You can add your own implementation as you subclass this.
*/
uint32_t len; //throwaway, passed by reference.
GAInstruction ins=decode(adr, len);
ins.adr=adr;
return ins;
}
//Returns a GAInstruction from a verb and operands.
GAInstruction GALanguage::match(QString verb, QList<GAParserOperand> ops,
uint64_t adr){
assert(goodasm);
GAInstruction ins(goodasm);
GAMnemonic *matched=0;
int count=0;
QString dups="";
if(goodasm->verbose)
qDebug()<<"Matching"<<verb<<"with"<<ops.count()<<"parameters.";
foreach (GAMnemonic *m, mnemonics) {
if(m->match(ins, adr, verb, ops)){
if(!matched){
matched=m;
}
dups+=m->examplestr+";";
count++;
}
}
//Handy to highlight collisions if we aren't separating them right.
if(goodasm->verbose && count>1){
qDebug()<<"Duplicates:"<<dups;
}
if(matched){
matched->match(ins, adr, verb, ops);
return ins;
}
//Empty instruction if we don't know what it is.
//qDebug()<<"ERROR: unmatched instruction on line"<<goodasm->line;
goodasm->error("Unmatched instruction.");
return GAInstruction(goodasm);
}
//Insert a mnemonic into the language.
GAMnemonic* GALanguage::insert(GAMnemonic* mnemonic){
mnemonic->lang=this;
mnemonics.append(mnemonic);
assert(name!="nameless");
return mnemonic;
}
//Convert a native address to a byte address.
uint64_t GALanguage::byteAdr(uint64_t adr){
return adr*epsilon;
}
//Convert a byte address to a native address.
uint64_t GALanguage::nativeAdr(uint64_t adr){
return adr/epsilon;
}
//Cheat sheet of mnemonic names.
QString GALanguage::cheatSheet(){
QString cheat="";
GAMnemonic* mnem=0;
for(QList<GAMnemonic*>::iterator i = mnemonics.begin(),
end = mnemonics.end();
i != end;
++i){
mnem=((GAMnemonic*)*i);
if(mnem->examplestr.length()>0){
cheat+=goodasm->formatSource("", mnem->examplestr, "; "+mnem->helpstr);
}else{
cheat+=goodasm->formatSource(mnem->name, "", "MISSING");
}
}
return cheat;
}
//Matches a known mnemonic to decode, or db for default.
GAInstruction GALanguage::decode(uint64_t adr, char *bytes, uint32_t &len){
//Default decoding is a DB directive of a single byte.
GAInstruction ins(goodasm, bytes[0]);
//Count matches, panicking if there's a collision.
int count=0;
//Duplicate matches
QString dupes="";
QString lastexample="";
GAMnemonic *mnem=0, *bestmnem=0;
for(QList<GAMnemonic*>::iterator i = mnemonics.begin(),
end = mnemonics.end();
i != end;
++i){
mnem=((GAMnemonic*)*i);
//We want the match with the highest priority.
if(mnem->match(ins, adr, len, bytes)){
if(bestmnem && bestmnem->priority>mnem->priority){
//We already have a better match, so do nothing.
}else if(bestmnem && bestmnem->priority==mnem->priority){
//Priority is steady; we have a collision!
if(!count)
dupes+=bestmnem->name+" ";
dupes+=mnem->name+" ";
count++;
lastexample=mnem->examplestr;
}else{
//We have a new best match.
bestmnem=mnem;
//And the old collisions are no longer a problem.
count=0;
dupes="";
}
}
}
if(count>=1){
goodasm->duplicates++;
qDebug()<<"Colliding matches: "<<dupes;
qDebug()<<"1: "<<bestmnem->examplestr;
qDebug()<<"2: "<<lastexample;
}
//Reapply the best match.
if(bestmnem)
bestmnem->match(ins,adr,len,bytes);
return ins;
}
//Matches a known mnemonic to decode, or db for default.
GAInstruction GALanguage::decode(uint64_t adr, uint32_t &len){
//Here we populate a few bytes to decode.
assert(goodasm->lang->maxbytes<=GAMAXLEN);
char bytes[GAMAXLEN];
for(int i=0; i<GAMAXLEN; i++)
bytes[i]=goodasm->byteAt(adr+i);
return decode(adr, bytes, len);
}