-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathx86_lexer.cpp
444 lines (392 loc) · 11.7 KB
/
x86_lexer.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
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <sstream>
#include "x86_lexer.h"
using namespace std;
void reportRuntimeError(const char *format, ...);
#define RETURN_TOKEN(tk) \
do { \
tokenInfo.set(tkText, currentLine); \
return tk; \
} while (0)
#define APPEND_SEQUENCE(cond, sb) \
do { \
while ( (cond) ) { \
sb += (char)ch;\
ch = nextChar();\
} \
ungetChar(ch); \
} while (0)
struct XKeyword {
const char *name;
int tokenKind;
};
static XKeyword kw[] = {
{"hexadecimal", XCKW_HEX },
{"hex", XCKW_HEX },
{"decimal", XCKW_DEC },
{"signed", XCKW_SIGNED },
{"unsigned", XCKW_UNSIGNED },
{"binary", XCKW_BIN },
{"octal", XCKW_OCT },
{"ascii", XCKW_ASCII},
{"mov", XKW_MOV},
{"movzx", XKW_MOVZX},
{"movsx", XKW_MOVSX},
{"push", XKW_PUSH},
{"pop", XKW_POP},
{"lea", XKW_LEA},
{"add", XKW_ADD},
{"sub", XKW_SUB},
{"cmp", XKW_CMP},
{"test", XKW_TEST},
{"inc", XKW_INC},
{"dec", XKW_DEC},
{"imul", XKW_IMUL},
{"idiv", XKW_IDIV},
{"mul", XKW_MUL},
{"div", XKW_DIV},
{"and", XKW_AND},
{"or", XKW_OR},
{"xor", XKW_XOR},
{"not", XKW_NOT},
{"neg", XKW_NEG},
{"shl", XKW_SHL},
{"shr", XKW_SHR},
{"jmp", XKW_JMP},
{"je", XKW_JE},
{"jne", XKW_JNE},
{"jz", XKW_JZ},
{"jnz", XKW_JNZ},
{"jg", XKW_JG},
{"jge", XKW_JGE},
{"jl", XKW_JL},
{"jle", XKW_JLE},
{"jb", XKW_JB},
{"jnae", XKW_JNAE},
{"jc", XKW_JC},
{"jnb", XKW_JNB},
{"jae", XKW_JAE},
{"jnc", XKW_JNC},
{"jbe", XKW_JBE},
{"jna", XKW_JNA},
{"ja", XKW_JA},
{"jnbe", XKW_JNBE},
{"cmp", XKW_CMP},
{"call", XKW_CALL},
{"ret", XKW_RET},
{"seta", XKW_SETA},
{"setae", XKW_SETAE},
{"setb", XKW_SETB},
{"setbe", XKW_SETBE},
{"setc", XKW_SETC},
{"sete", XKW_SETE},
{"setg", XKW_SETG},
{"setge", XKW_SETGE},
{"setl", XKW_SETL},
{"setle", XKW_SETLE},
{"setna", XKW_SETNA},
{"setnae", XKW_SETNAE},
{"setnb", XKW_SETNB},
{"setnbe", XKW_SETNBE},
{"setnc", XKW_SETNC},
{"setne", XKW_SETNE},
{"setng", XKW_SETNG},
{"setnge", XKW_SETNGE},
{"setnl", XKW_SETNL},
{"setnle", XKW_SETNLE},
{"setno", XKW_SETNO},
{"setnp", XKW_SETNP},
{"setns", XKW_SETNS},
{"setnz", XKW_SETNZ},
{"seto", XKW_SETO},
{"setp", XKW_SETP},
{"setpe", XKW_SETPE},
{"setpo", XKW_SETPO},
{"sets", XKW_SETS},
{"setz", XKW_SETZ},
{"cdq", XKW_CDQ},
{"leave", XKW_LEAVE},
{"byte", XKW_BYTE},
{"word", XKW_WORD},
{"dword", XKW_DWORD},
{"ptr", XKW_PTR},
{"eax", TK_EAX}, {"ebx", TK_EBX}, {"ecx", TK_ECX}, {"edx", TK_EDX}, {"esi", TK_ESI}, {"edi", TK_EDI}, {"esp", TK_ESP}, {"ebp", TK_EBP},
{"eflags", TK_EFLAGS},
{"ax", TK_AX}, {"bx", TK_BX}, {"cx", TK_CX}, {"dx", TK_DX},
{"al", TK_AL}, {"ah", TK_AH}, {"bl", TK_BL}, {"bh", TK_BH}, {"cl", TK_CL}, {"ch", TK_CH}, {"dl", TK_DX}, {"dh", TK_DX},
};
const int KWCount = sizeof(kw)/sizeof(XKeyword);
static XKeyword x_commands[] = {
{"show", XCKW_SHOW },
{"set", XCKW_SET },
{"exec", XCKW_EXEC },
{"debug", XCKW_DEBUG},
{"stop", XCKW_STOP },
{"paddr", XCKW_PADDR},
};
const int KWCmdCount = sizeof(x_commands)/sizeof(XKeyword);
#if defined(_MSC_VER)
#define strcasecmp _stricmp
#endif
static int lookUpWord(XKeyword kw[], int size, string str)
{
for (int i=0; i<size; i++) {
if (strcasecmp(str.c_str(), kw[i].name) == 0)
return kw[i].tokenKind;
}
return XTK_ID;
}
X86Lexer::X86Lexer(istream *in)
{
this->in = in;
currentLine = 1;
}
int X86Lexer::getNextToken()
{
int ch;
string tkText;
tkText.reserve(MAX_TOKEN_LENGTH);
tkText = "";
while (1) {
ch = nextChar();
if (ch == ' ' || ch == '\t')
continue;
tkText += (char)ch;
switch (ch) {
case EOF: RETURN_TOKEN(XTK_EOF);
case '[': RETURN_TOKEN(XTK_LBRACKET);
case ']': RETURN_TOKEN(XTK_RBRACKET);
case '(': RETURN_TOKEN(XTK_LPAREN);
case ')': RETURN_TOKEN(XTK_RPAREN);
case ',': RETURN_TOKEN(XTK_COMMA);
case ':': RETURN_TOKEN(XTK_COLON);
case '-': RETURN_TOKEN(XTK_OP_MINUS);
case '+': RETURN_TOKEN(XTK_OP_PLUS);
case '*': RETURN_TOKEN(XTK_OP_MULT);
case '=': RETURN_TOKEN(XTK_OP_EQUAL);
case '@': RETURN_TOKEN(XTK_AT);
case '.': RETURN_TOKEN(XTK_DOT);
case ';': {
ch = nextChar();
while (ch != '\n' && ch != EOF) {
ch = nextChar();
}
currentLine ++;
RETURN_TOKEN(XTK_EOL);
}
case '\r': {
ch = nextChar();
if (ch != '\n')
ungetChar(ch);
currentLine ++;
RETURN_TOKEN(XTK_EOL);
}
case '\n': {
currentLine ++;
RETURN_TOKEN(XTK_EOL);
}
case '"': {
tkText.clear();
ch = nextChar();
APPEND_SEQUENCE((ch != '"') && (ch != EOF), tkText);
ch = nextChar();
if (ch == EOF) {
tokenInfo.set(string("unterminated string \"") + tkText + string("\""), currentLine);
return XTK_ERROR;
}
tokenInfo.set(tkText, currentLine);
return XSTR_LITERAL;
}
case '\'': {
tkText.clear();
ch = nextChar();
APPEND_SEQUENCE((ch != '\'') && (ch != EOF), tkText);
ch = nextChar();
if (ch == EOF) {
tokenInfo.set(string("unterminated character constant '") + tkText + string("'"), currentLine);
return XTK_ERROR;
}
if (tkText.length() != 1) {
tokenInfo.set("character constant '" + tkText + "'", currentLine);
return XTK_ERROR;
}
tokenInfo.set(tkText, currentLine);
tokenInfo.intValue = (int)tkText[0];
return XTK_CHAR_CONSTANT;
}
case '#': {
tkText.clear();
ch = nextChar();
APPEND_SEQUENCE(isalpha(ch), tkText);
tokenInfo.set(tkText, currentLine);
return lookUpWord(x_commands, KWCmdCount, tkText);
}
default: {
if (isdigit(ch)) {
char prevCh = ch;
ch = nextChar();
if (prevCh == '0' && (ch == 'b' || ch == 'B')) {
tkText.clear();
ch = nextChar();
APPEND_SEQUENCE((ch=='0') || (ch=='1'), tkText);
if (tkText.empty()) {
tokenInfo.set("binary constant '0b'", currentLine);
return XTK_ERROR;
}
tokenInfo.set(tkText, currentLine);
tokenInfo.intValue = strtoul(tkText.c_str(), NULL, 2);
return XTK_BIN_CONSTANT;
} else if (prevCh == '0' && (ch == 'x' || ch == 'X')) {
tkText.clear();
ch = nextChar();
APPEND_SEQUENCE(isxdigit(ch), tkText);
if (tkText.empty()) {
tokenInfo.set("hexadecimal constant '0x'", currentLine);
return XTK_ERROR;
}
tokenInfo.set(tkText, currentLine);
tokenInfo.intValue = strtoul(tkText.c_str(), NULL, 16);
return XTK_HEX_CONSTANT;
} else if (isdigit(ch)) {
APPEND_SEQUENCE(isdigit(ch), tkText);
tokenInfo.set(tkText, currentLine);
tokenInfo.intValue = atol(tkText.c_str());
return XTK_DEC_CONSTANT;
} else {
ungetChar(ch);
tokenInfo.set(tkText, currentLine);
tokenInfo.intValue = prevCh - '0';
return XTK_DEC_CONSTANT;
}
} else if (isalpha(ch) || ch == '_') {
ch = nextChar();
APPEND_SEQUENCE( (isalnum(ch) || ch == '_') && (ch != EOF), tkText );
tokenInfo.set(tkText, currentLine);
return lookUpWord(kw, KWCount, tkText);
} else {
tokenInfo.set(string("symbol '") + ((char)ch) + string("'"), currentLine);
return XTK_ERROR;
}
}
}
}
}
string X86Lexer::getTokenString(int token, TokenInfo *info)
{
string tokenName;
switch (token) {
case XTK_EOF: tokenName = "end of input"; break;
case XTK_EOL: tokenName = "end of line"; break;
case XTK_ERROR: tokenName = ""; break;
case XCKW_EXEC:
case XCKW_SET:
case XCKW_SHOW:
case XCKW_PADDR:
case XCKW_HEX:
case XCKW_SIGNED:
case XCKW_UNSIGNED:
case XCKW_BIN:
case XCKW_OCT:
case XCKW_DEC:
case XKW_DEC:
case XKW_MOV:
case XKW_MOVSX:
case XKW_MOVZX:
case XKW_PUSH:
case XKW_POP:
case XKW_LEA:
case XKW_ADD:
case XKW_SUB:
case XKW_CMP:
case XKW_TEST:
case XKW_INC:
case XKW_IMUL:
case XKW_IDIV:
case XKW_AND:
case XKW_OR:
case XKW_XOR:
case XKW_NOT:
case XKW_NEG:
case XKW_SHL:
case XKW_SHR:
case XKW_JMP:
case XKW_JE:
case XKW_JNE:
case XKW_JZ:
case XKW_JNZ:
case XKW_JG:
case XKW_JGE:
case XKW_JL:
case XKW_JLE:
case XKW_CALL:
case XKW_RET:
case XKW_BYTE:
case XKW_WORD:
case XKW_DWORD:
case XKW_PTR:
tokenName = "keyword";
break;
case TK_AL:
case TK_AH:
case TK_BL:
case TK_BH:
case TK_CL:
case TK_CH:
case TK_DL:
case TK_DH:
case TK_AX:
case TK_BX:
case TK_CX:
case TK_DX:
case TK_EAX:
case TK_EBX:
case TK_ECX:
case TK_EDX:
case TK_ESI:
case TK_EDI:
case TK_ESP:
case TK_EBP:
case TK_EFLAGS:
tokenName = "register";
break;
case XTK_COMMA:
case XTK_LPAREN:
case XTK_RPAREN:
case XTK_AT:
case XTK_DOT:
case XTK_LBRACKET:
case XTK_RBRACKET:
tokenName = "symbol";
break;
case XTK_OP_PLUS:
case XTK_OP_MINUS:
case XTK_OP_MULT:
tokenName = "operator";
break;
case XTK_ID: tokenName = "identifier"; break;
case XTK_DEC_CONSTANT: tokenName = "decimal constant"; break;
case XTK_HEX_CONSTANT: tokenName = "hexadecimal constant"; break;
case XTK_BIN_CONSTANT: tokenName = "binary constant"; break;
case XTK_OCT_CONSTANT: tokenName = "octal constant"; break;
default:
tokenName = ::convertToString(token);
}
string result = tokenName;
if (info != NULL) {
switch (token) {
case XTK_ERROR:
result += info->tokenLexeme;
break;
case XTK_EOL:
case XTK_EOF:
break;
default:
result += " '" + info->tokenLexeme + "'";
}
}
return result;
}