fix edx arg-reg corruption in fromasm
[ia32rtools.git] / tools / translate.c
CommitLineData
c36e914d 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "my_assert.h"
6#include "my_str.h"
7
8#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9#define IS(w, y) !strcmp(w, y)
10
11#include "protoparse.h"
12
13const char *asmfn;
14static int asmln;
15
16#define awarn(fmt, ...) \
17 printf("warning:%s:%d: " fmt, asmfn, asmln, ##__VA_ARGS__)
18#define aerr(fmt, ...) do { \
19 printf("error:%s:%d: " fmt, asmfn, asmln, ##__VA_ARGS__); \
20 exit(1); \
21} while (0)
22
23enum op_class {
24 OPC_UNSPEC,
91977a1c 25 OPC_RMD, /* removed or optimized out */
26 OPC_DATA, /* data processing */
27 OPC_DATA_FLAGS, /* data processing + sets flags */
28 OPC_JMP, /* .. and call */
29 OPC_JCC, /* conditional jump */
30 OPC_SCC, /* conditionel set */
c36e914d 31};
32
33enum op_op {
34 OP_INVAL,
35 OP_PUSH,
36 OP_POP,
37 OP_MOV,
850c9265 38 OP_LEA,
39 OP_MOVZX,
40 OP_MOVSX,
41 OP_NOT,
c36e914d 42 OP_RET,
43 OP_ADD,
91977a1c 44 OP_SUB,
850c9265 45 OP_AND,
46 OP_OR,
47 OP_XOR,
48 OP_SHL,
49 OP_SHR,
50 OP_SAR,
51 OP_SBB,
52 OP_INC,
53 OP_DEC,
54 OP_MUL,
55 OP_IMUL,
c36e914d 56 OP_TEST,
57 OP_CMP,
58 OP_CALL,
59 OP_JMP,
60 OP_JO,
61 OP_JNO,
62 OP_JC,
63 OP_JNC,
64 OP_JZ,
65 OP_JNZ,
66 OP_JBE,
67 OP_JA,
68 OP_JS,
69 OP_JNS,
70 OP_JP,
71 OP_JNP,
72 OP_JL,
73 OP_JGE,
74 OP_JLE,
75 OP_JG,
76};
77
78enum opr_type {
79 OPT_UNSPEC,
80 OPT_REG,
81 OPT_REGMEM,
82 OPT_LABEL,
850c9265 83 OPT_OFFSET,
c36e914d 84 OPT_CONST,
85};
86
87enum opr_lenmod {
91977a1c 88 OPLM_UNSPEC,
89 OPLM_BYTE,
90 OPLM_WORD,
91 OPLM_DWORD,
c36e914d 92};
93
850c9265 94#define MAX_OPERANDS 3
c36e914d 95
96struct parsed_opr {
91977a1c 97 enum opr_type type;
98 enum opr_lenmod lmod;
99 int reg;
100 unsigned int val;
101 char name[256];
c36e914d 102};
103
104struct parsed_op {
91977a1c 105 enum op_class cls;
106 enum op_op op;
107 struct parsed_opr operand[MAX_OPERANDS];
108 int operand_cnt;
109 int regmask; // all referensed regs
110 void *datap;
111};
112
113struct parsed_equ {
114 char name[64];
115 enum opr_lenmod lmod;
116 int offset;
c36e914d 117};
118
119#define MAX_OPS 1024
120
121static struct parsed_op ops[MAX_OPS];
91977a1c 122static struct parsed_equ *g_eqs;
123static int g_eqcnt;
124static char g_labels[MAX_OPS][32];
125static struct parsed_proto g_func_pp;
126static char g_func[256];
127static char g_comment[256];
128static int g_bp_frame;
129static int g_bp_stack;
130#define ferr(op_, fmt, ...) do { \
850c9265 131 printf("error:%s:#%ld: '%s': " fmt, g_func, (op_) - ops, \
132 dump_op(op_), ##__VA_ARGS__); \
91977a1c 133 exit(1); \
134} while (0)
135
136#define MAX_REGS 8
137
138const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
139const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
140const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
141const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
142
143enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
144
145static int char_array_i(const char *array[], size_t len, const char *s)
146{
147 int i;
c36e914d 148
91977a1c 149 for (i = 0; i < len; i++)
150 if (IS(s, array[i]))
151 return i;
c36e914d 152
91977a1c 153 return -1;
154}
155
850c9265 156static int parse_reg(int *reg_out, enum opr_lenmod *reg_lmod,
157 int *regmask, char *s)
91977a1c 158{
159 char w[16];
160 int reg = xUNSPEC;
161 int c = 0;
162
163 while (*s != 0) {
164 while (my_isblank(*s) || my_issep(*s))
165 s++;
166 s = next_idt(w, sizeof(w), s);
167 if (w[0] == 0)
168 break;
169 c++;
170 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), w);
171 if (reg >= 0) {
850c9265 172 *reg_lmod = OPLM_DWORD;
91977a1c 173 *regmask |= 1 << reg;
174 continue;
175 }
176 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), w);
177 if (reg >= 0) {
850c9265 178 *reg_lmod = OPLM_WORD;
91977a1c 179 *regmask |= 1 << reg;
180 continue;
181 }
182 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), w);
183 if (reg >= 0) {
850c9265 184 *reg_lmod = OPLM_BYTE;
91977a1c 185 *regmask |= 1 << reg;
186 continue;
187 }
188 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), w);
189 if (reg >= 0) {
850c9265 190 *reg_lmod = OPLM_BYTE;
91977a1c 191 *regmask |= 1 << reg;
192 continue;
193 }
194
850c9265 195 return -1;
91977a1c 196 }
197
850c9265 198 if (c == 1) {
199 *reg_out = reg;
200 return 0;
201 }
202
203 return -1;
91977a1c 204}
205
206static long parse_number(const char *number)
207{
208 int len = strlen(number);
209 const char *p = number;
210 char *endp = NULL;
211 int neg = 0;
212 int bad;
213 long ret;
214
215 if (*p == '-') {
216 neg = 1;
217 p++;
218 }
219 if (len > 1 && *p == '0')
220 p++;
221 if (number[len - 1] == 'h') {
222 ret = strtol(p, &endp, 16);
223 bad = (*endp != 'h');
224 }
225 else {
226 ret = strtol(p, &endp, 10);
227 bad = (*endp != 0);
228 }
229 if (bad)
230 aerr("number parsing failed\n");
231 if (neg)
232 ret = -ret;
233 return ret;
234}
235
850c9265 236static int guess_lmod_from_name(struct parsed_opr *opr)
237{
238 if (!strncmp(opr->name, "dword_", 6)) {
239 opr->lmod = OPLM_DWORD;
240 return 1;
241 }
242 if (!strncmp(opr->name, "word_", 5)) {
243 opr->lmod = OPLM_WORD;
244 return 1;
245 }
246 if (!strncmp(opr->name, "byte_", 5)) {
247 opr->lmod = OPLM_BYTE;
248 return 1;
249 }
250 return 0;
251}
252
91977a1c 253static int parse_operand(struct parsed_opr *opr, int *regmask,
c36e914d 254 char words[16][256], int wordc, int w, enum op_class cls)
255{
850c9265 256 enum opr_lenmod tmplmod;
257 int tmpreg;
258 int ret, len;
259 int i;
c36e914d 260
261 if (w >= wordc)
262 aerr("parse_operand w %d, wordc %d\n", w, wordc);
263
91977a1c 264 opr->reg = xUNSPEC;
265
c36e914d 266 for (i = w; i < wordc; i++) {
267 len = strlen(words[i]);
268 if (words[i][len - 1] == ',') {
269 words[i][len - 1] = 0;
270 wordc = i + 1;
271 break;
272 }
273 }
274
275 if (cls == OPC_JMP || cls == OPC_JCC) {
276 const char *label;
277
278 if (wordc - w == 3 && IS(words[w + 1], "ptr"))
279 label = words[w + 2];
280 else if (wordc - w == 2 && IS(words[w], "short"))
281 label = words[w + 1];
282 else if (wordc - w == 1)
283 label = words[w];
284 else
285 aerr("jump parse error");
286
287 opr->type = OPT_LABEL;
288 strcpy(opr->name, label);
289 return wordc;
290 }
291
292 if (wordc - w >= 3) {
293 if (IS(words[w + 1], "ptr")) {
294 if (IS(words[w], "dword"))
91977a1c 295 opr->lmod = OPLM_DWORD;
c36e914d 296 else if (IS(words[w], "word"))
91977a1c 297 opr->lmod = OPLM_WORD;
c36e914d 298 else if (IS(words[w], "byte"))
91977a1c 299 opr->lmod = OPLM_BYTE;
c36e914d 300 else
301 aerr("type parsing failed\n");
302 w += 2;
303 }
304 }
305
306 if (wordc - w == 2 && IS(words[w], "offset")) {
850c9265 307 opr->type = OPT_OFFSET;
c36e914d 308 strcpy(opr->name, words[w + 1]);
309 return wordc;
310 }
311
850c9265 312 if (wordc - w != 1)
313 aerr("parse_operand 1 word expected\n");
c36e914d 314
850c9265 315 strcpy(opr->name, words[w]);
c36e914d 316
850c9265 317 if (words[w][0] == '[') {
318 opr->type = OPT_REGMEM;
319 ret = sscanf(words[w], "[%[^]]]", opr->name);
320 if (ret != 1)
321 aerr("[] parse failure\n");
322 // only need the regmask
323 parse_reg(&tmpreg, &tmplmod, regmask, opr->name);
324 return wordc;
325 }
326 else if (strchr(words[w], '[')) {
327 // label[reg] form
328 opr->type = OPT_REGMEM;
329 if (opr->lmod == OPLM_UNSPEC)
330 guess_lmod_from_name(opr);
331 parse_reg(&tmpreg, &tmplmod, regmask, strchr(words[w], '['));
332 return wordc;
333 }
334 else if (('0' <= words[w][0] && words[w][0] <= '9')
335 || words[w][0] == '-')
336 {
91977a1c 337 opr->type = OPT_CONST;
338 opr->val = (unsigned int)parse_number(words[w]);
339 return wordc;
850c9265 340 }
c36e914d 341
850c9265 342 ret = parse_reg(&opr->reg, &tmplmod, regmask, opr->name);
343 if (ret == 0) {
344 opr->type = OPT_REG;
345 opr->lmod = tmplmod;
346 return wordc;
347 }
348
349 // most likely var in data segment
350 opr->type = OPT_LABEL;
351 if (opr->lmod == OPLM_UNSPEC)
352 guess_lmod_from_name(opr);
353 if (opr->lmod != OPLM_UNSPEC)
354 return wordc;
91977a1c 355
850c9265 356 // TODO: scan data seg to determine type?
357 return wordc;
c36e914d 358}
359
360static const struct {
850c9265 361 const char *name;
362 enum op_op op;
363 enum op_class cls;
364 int minopr;
365 int maxopr;
c36e914d 366} op_table[] = {
850c9265 367 { "push", OP_PUSH, OPC_DATA, 1, 1 },
368 { "pop", OP_POP, OPC_DATA, 1, 1 },
369 { "mov" , OP_MOV, OPC_DATA, 2, 2 },
370 { "lea", OP_LEA, OPC_DATA, 2, 2 },
371 { "movzx",OP_MOVZX, OPC_DATA, 2, 2 },
372 { "movsx",OP_MOVSX, OPC_DATA, 2, 2 },
373 { "not", OP_NOT, OPC_DATA, 1, 1 },
374 { "add", OP_ADD, OPC_DATA_FLAGS, 2, 2 },
375 { "sub", OP_SUB, OPC_DATA_FLAGS, 2, 2 },
376 { "and", OP_AND, OPC_DATA_FLAGS, 2, 2 },
377 { "or", OP_OR, OPC_DATA_FLAGS, 2, 2 },
378 { "xor", OP_XOR, OPC_DATA_FLAGS, 2, 2 },
379 { "shl", OP_SHL, OPC_DATA_FLAGS, 2, 2 },
380 { "shr", OP_SHR, OPC_DATA_FLAGS, 2, 2 },
381 { "sal", OP_SHL, OPC_DATA_FLAGS, 2, 2 },
382 { "sar", OP_SAR, OPC_DATA_FLAGS, 2, 2 },
383 { "sbb", OP_SBB, OPC_DATA_FLAGS, 2, 2 },
384 { "inc", OP_INC, OPC_DATA_FLAGS, 1, 1 },
385 { "dec", OP_DEC, OPC_DATA_FLAGS, 1, 1 },
386 { "mul", OP_MUL, OPC_DATA_FLAGS, 1, 1 },
387 { "imul", OP_IMUL, OPC_DATA_FLAGS, 1, 3 },
388 { "test", OP_TEST, OPC_DATA_FLAGS, 2, 2 },
389 { "cmp", OP_CMP, OPC_DATA_FLAGS, 2, 2 },
390 { "retn", OP_RET, OPC_JMP, 0, 1 },
391 { "call", OP_CALL, OPC_JMP, 1, 1 },
392 { "jmp", OP_JMP, OPC_JMP, 1, 1 },
393 { "jo", OP_JO, OPC_JCC, 1, 1 }, // 70 OF=1
394 { "jno", OP_JNO, OPC_JCC, 1, 1 }, // 71 OF=0
395 { "jc", OP_JC, OPC_JCC, 1, 1 }, // 72 CF=1
396 { "jb", OP_JC, OPC_JCC, 1, 1 }, // 72
397 { "jnc", OP_JNC, OPC_JCC, 1, 1 }, // 73 CF=0
398 { "jae", OP_JNC, OPC_JCC, 1, 1 }, // 73
399 { "jz", OP_JZ, OPC_JCC, 1, 1 }, // 74 ZF=1
400 { "je", OP_JZ, OPC_JCC, 1, 1 }, // 74
401 { "jnz", OP_JNZ, OPC_JCC, 1, 1 }, // 75 ZF=0
402 { "jne", OP_JNZ, OPC_JCC, 1, 1 }, // 75
403 { "jbe", OP_JBE, OPC_JCC, 1, 1 }, // 76 CF=1 || ZF=1
404 { "jna", OP_JBE, OPC_JCC, 1, 1 }, // 76
405 { "ja", OP_JA, OPC_JCC, 1, 1 }, // 77 CF=0 && ZF=0
406 { "jnbe", OP_JA, OPC_JCC, 1, 1 }, // 77
407 { "js", OP_JS, OPC_JCC, 1, 1 }, // 78 SF=1
408 { "jns", OP_JNS, OPC_JCC, 1, 1 }, // 79 SF=0
409 { "jp", OP_JP, OPC_JCC, 1, 1 }, // 7a PF=1
410 { "jpe", OP_JP, OPC_JCC, 1, 1 }, // 7a
411 { "jnp", OP_JNP, OPC_JCC, 1, 1 }, // 7b PF=0
412 { "jpo", OP_JNP, OPC_JCC, 1, 1 }, // 7b
413 { "jl", OP_JL, OPC_JCC, 1, 1 }, // 7c SF!=OF
414 { "jnge", OP_JL, OPC_JCC, 1, 1 }, // 7c
415 { "jge", OP_JGE, OPC_JCC, 1, 1 }, // 7d SF=OF
416 { "jnl", OP_JGE, OPC_JCC, 1, 1 }, // 7d
417 { "jle", OP_JLE, OPC_JCC, 1, 1 }, // 7e ZF=1 || SF!=OF
418 { "jng", OP_JLE, OPC_JCC, 1, 1 }, // 7e
419 { "jg", OP_JG, OPC_JCC, 1, 1 }, // 7f ZF=0 && SF=OF
420 { "jnle", OP_JG, OPC_JCC, 1, 1 }, // 7f
421 };
c36e914d 422
423static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
424{
91977a1c 425 int opr = 0;
426 int w = 1;
427 int i;
c36e914d 428
91977a1c 429 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
430 if (!IS(words[0], op_table[i].name))
431 continue;
c36e914d 432
91977a1c 433 op->regmask = 0;
c36e914d 434
91977a1c 435 for (opr = 0; opr < op_table[i].minopr; opr++) {
436 w = parse_operand(&op->operand[opr], &op->regmask,
437 words, wordc, w, op_table[i].cls);
438 }
c36e914d 439
91977a1c 440 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
441 w = parse_operand(&op->operand[opr], &op->regmask,
442 words, wordc, w, op_table[i].cls);
443 }
c36e914d 444
91977a1c 445 goto done;
446 }
c36e914d 447
91977a1c 448 aerr("unhandled op: '%s'\n", words[0]);
c36e914d 449
91977a1c 450done:
451 if (w < wordc)
452 aerr("parse_op %s incomplete: %d/%d\n",
453 words[0], w, wordc);
454
455 op->cls = op_table[i].cls;
456 op->op = op_table[i].op;
457 op->operand_cnt = opr;
458 return;
c36e914d 459}
460
850c9265 461static const char *op_name(enum op_op op)
462{
463 int i;
464
465 for (i = 0; i < ARRAY_SIZE(op_table); i++)
466 if (op_table[i].op == op)
467 return op_table[i].name;
468
469 return "???";
470}
471
472// debug
473static const char *dump_op(struct parsed_op *po)
474{
475 static char out[128];
476 char *p = out;
477 int i;
478
479 snprintf(out, sizeof(out), "%s", op_name(po->op));
480 for (i = 0; i < po->operand_cnt; i++) {
481 p += strlen(p);
482 if (i > 0)
483 *p++ = ',';
484 snprintf(p, sizeof(out) - (p - out),
485 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
486 po->operand[i].name);
487 }
488
489 return out;
490}
491
91977a1c 492static const char *opr_name(struct parsed_op *po, int opr_num)
c36e914d 493{
91977a1c 494 if (opr_num >= po->operand_cnt)
495 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
496 return po->operand[opr_num].name;
c36e914d 497}
498
91977a1c 499static unsigned int opr_const(struct parsed_op *po, int opr_num)
c36e914d 500{
91977a1c 501 if (opr_num >= po->operand_cnt)
502 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
503 if (po->operand[opr_num].type != OPT_CONST)
504 ferr(po, "opr %d: const expected\n", opr_num);
505 return po->operand[opr_num].val;
506}
c36e914d 507
91977a1c 508static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
509{
510 if ((unsigned int)popr->reg >= MAX_REGS)
511 ferr(po, "invalid reg: %d\n", popr->reg);
512 return regs_r32[popr->reg];
513}
c36e914d 514
850c9265 515static struct parsed_equ *equ_find(struct parsed_op *po, const char *name)
91977a1c 516{
850c9265 517 int i;
518
519 for (i = 0; i < g_eqcnt; i++)
520 if (IS(g_eqs[i].name, name))
521 break;
522 if (i >= g_eqcnt)
523 ferr(po, "unresolved equ name: '%s'\n", name);
524
525 return &g_eqs[i];
526}
527
528static void bg_frame_access(struct parsed_op *po, enum opr_lenmod lmod,
529 char *buf, size_t buf_size, const char *bp_arg,
530 int is_src, int is_lea)
531{
532 const char *prefix = "";
91977a1c 533 struct parsed_equ *eq;
534 int i, arg_i, arg_s;
850c9265 535 int sf_ofs;
91977a1c 536
537 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
538
850c9265 539 eq = equ_find(po, bp_arg);
91977a1c 540
541 if (eq->offset >= 0) {
542 arg_i = eq->offset / 4 - 2;
543 if (arg_i < 0 || arg_i >= g_func_pp.argc_stack)
544 ferr(po, "offset %d doesn't map to any arg\n", eq->offset);
545
546 for (i = arg_s = 0; i < g_func_pp.argc; i++) {
547 if (g_func_pp.arg[i].reg != NULL)
548 continue;
549 if (arg_s == arg_i)
550 break;
551 arg_s++;
552 }
553 if (i == g_func_pp.argc)
554 ferr(po, "arg %d not in prototype?\n", arg_i);
850c9265 555 if (is_lea)
556 ferr(po, "lea to arg?\n");
557
91977a1c 558 snprintf(buf, buf_size, "%sa%d", is_src ? "(u32)" : "", i + 1);
559 }
560 else {
561 if (g_bp_stack == 0)
562 ferr(po, "bp_stack access after it was not detected\n");
850c9265 563
564 sf_ofs = g_bp_stack + eq->offset;
565 if (sf_ofs < 0)
566 ferr(po, "bp_stack offset %d/%d\n", eq->offset, g_bp_stack);
567
568 if (is_lea)
569 prefix = "&";
570
571 switch (lmod)
572 {
573 case OPLM_BYTE:
574 snprintf(buf, buf_size, "%ssf.b[%d]", prefix, sf_ofs);
575 break;
576 case OPLM_WORD:
577 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
578 break;
579 case OPLM_DWORD:
580 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
581 break;
582 default:
583 ferr(po, "bp_stack bad lmod: %d\n", lmod);
584 }
91977a1c 585 }
586}
c36e914d 587
91977a1c 588static char *out_src_opr(char *buf, size_t buf_size,
850c9265 589 struct parsed_op *po, struct parsed_opr *popr, int is_lea)
91977a1c 590{
850c9265 591 const char *cast = "";
592 char tmp1[256], tmp2[256];
593 char expr[256];
594 int ret;
595
91977a1c 596 switch (popr->type) {
597 case OPT_REG:
850c9265 598 if (is_lea)
599 ferr(po, "lea from reg?\n");
600
91977a1c 601 switch (popr->lmod) {
602 case OPLM_DWORD:
603 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
604 break;
850c9265 605 case OPLM_WORD:
606 snprintf(buf, buf_size, "(u16)%s", opr_reg_p(po, popr));
607 break;
608 case OPLM_BYTE:
609 snprintf(buf, buf_size, "(u8)%s", opr_reg_p(po, popr));
610 break;
91977a1c 611 default:
612 ferr(po, "invalid src lmod: %d\n", popr->lmod);
613 }
614 break;
850c9265 615
91977a1c 616 case OPT_REGMEM:
617 if (g_bp_frame && !strncmp(popr->name, "ebp+", 4)) {
850c9265 618 bg_frame_access(po, popr->lmod, buf, buf_size,
619 popr->name + 4, 1, is_lea);
91977a1c 620 break;
621 }
850c9265 622
623 strcpy(expr, popr->name);
624 if (strchr(expr, '[')) {
625 // special case: '[' can only be left for label[reg] form
626 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
627 if (ret != 2)
628 ferr(po, "parse failure for '%s'\n", expr);
629 snprintf(expr, sizeof(expr), "(u32)%s + %s", tmp1, tmp2);
630 }
631
632 // XXX: do we need more parsing?
633 if (is_lea) {
634 snprintf(buf, buf_size, "%s", expr);
635 break;
636 }
637
638 switch (popr->lmod) {
639 case OPLM_DWORD:
640 cast = "*(u32 *)";
641 break;
642 case OPLM_WORD:
643 cast = "*(u16 *)";
644 break;
645 case OPLM_BYTE:
646 cast = "*(u8 *)";
647 break;
648 default:
649 ferr(po, "invalid lmod: %d\n", popr->lmod);
650 }
651 snprintf(buf, buf_size, "%s(%s)", cast, expr);
91977a1c 652 break;
850c9265 653
91977a1c 654 case OPT_LABEL:
850c9265 655 if (is_lea)
656 snprintf(buf, buf_size, "(u32)&%s", popr->name);
657 else
658 snprintf(buf, buf_size, "%s", popr->name);
659 break;
660
661 case OPT_OFFSET:
662 if (is_lea)
663 ferr(po, "lea an offset?\n");
664 snprintf(buf, buf_size, "(u32)&%s", popr->name);
91977a1c 665 break;
850c9265 666
91977a1c 667 case OPT_CONST:
850c9265 668 if (is_lea)
669 ferr(po, "lea from const?\n");
670
91977a1c 671 snprintf(buf, buf_size, popr->val < 10 ? "%u" : "0x%02x", popr->val);
672 break;
850c9265 673
91977a1c 674 default:
675 ferr(po, "invalid src type: %d\n", popr->type);
676 }
677
678 return buf;
679}
c36e914d 680
91977a1c 681static char *out_dst_opr(char *buf, size_t buf_size,
682 struct parsed_op *po, struct parsed_opr *popr)
683{
684 switch (popr->type) {
685 case OPT_REG:
686 switch (popr->lmod) {
687 case OPLM_DWORD:
688 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
689 break;
850c9265 690 case OPLM_WORD:
691 // ugh..
692 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
693 break;
694 case OPLM_BYTE:
695 // ugh..
696 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
697 break;
91977a1c 698 default:
699 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
700 }
701 break;
850c9265 702
703 case OPT_REGMEM:
704 if (g_bp_frame && !strncmp(popr->name, "ebp+", 4)) {
705 bg_frame_access(po, popr->lmod, buf, buf_size,
706 popr->name + 4, 0, 0);
707 break;
708 }
709
710 return out_src_opr(buf, buf_size, po, popr, 0);
711
91977a1c 712 default:
713 ferr(po, "invalid dst type: %d\n", popr->type);
714 }
715
716 return buf;
717}
c36e914d 718
91977a1c 719static void split_cond(struct parsed_op *po, enum op_op *op, int *is_neg)
720{
721 *is_neg = 0;
722
723 switch (*op) {
724 case OP_JNO:
725 *op = OP_JO;
726 *is_neg = 1;
727 break;
728 case OP_JNC:
729 *op = OP_JC;
730 *is_neg = 1;
731 break;
732 case OP_JNZ:
733 *op = OP_JZ;
734 *is_neg = 1;
735 break;
736 case OP_JNS:
737 *op = OP_JS;
738 *is_neg = 1;
739 break;
740 case OP_JNP:
741 *op = OP_JP;
742 *is_neg = 1;
743 break;
850c9265 744 case OP_JLE:
745 *op = OP_JG;
746 *is_neg = 1;
747 break;
748 case OP_JGE:
749 *op = OP_JL;
750 *is_neg = 1;
751 break;
752 case OP_JA:
753 *op = OP_JBE;
754 *is_neg = 1;
755 break;
91977a1c 756 case OP_JO:
757 case OP_JC:
758 case OP_JZ:
759 case OP_JS:
760 case OP_JP:
91977a1c 761 case OP_JG:
850c9265 762 case OP_JL:
763 case OP_JBE:
91977a1c 764 break;
850c9265 765 //break;
91977a1c 766 default:
767 ferr(po, "split_cond: bad op %d\n", *op);
768 break;
769 }
770}
c36e914d 771
91977a1c 772static void out_test_for_cc(char *buf, size_t buf_size,
850c9265 773 struct parsed_op *po, enum op_op op, enum opr_lenmod lmod,
774 const char *expr)
91977a1c 775{
850c9265 776 const char *cast = "";
91977a1c 777 int is_neg = 0;
778
779 split_cond(po, &op, &is_neg);
780 switch (op) {
781 case OP_JZ:
782 switch (lmod) {
783 case OPLM_DWORD:
850c9265 784 break;
785 case OPLM_WORD:
786 cast = "(u16)";
787 break;
788 case OPLM_BYTE:
789 cast = "(u8)";
91977a1c 790 break;
791 default:
850c9265 792 ferr(po, "%s: invalid lmod for JZ: %d\n", __func__, lmod);
91977a1c 793 }
850c9265 794 snprintf(buf, buf_size, "(%s%s %s 0)",
795 cast, expr, is_neg ? "!=" : "==");
91977a1c 796 break;
850c9265 797
798 case OP_JG: // ZF=0 && SF=OF; OF=0 after test
799 switch (lmod) {
800 case OPLM_DWORD:
801 snprintf(buf, buf_size, "((int)%s %s 0)", expr, is_neg ? "<=" : ">");
802 break;
803 default:
804 ferr(po, "%s: unhandled lmod for JG: %d\n", __func__, lmod);
805 }
806 break;
807
91977a1c 808 default:
809 ferr(po, "%s: unhandled op: %d\n", __func__, op);
810 }
811}
c36e914d 812
850c9265 813static void out_cmp_for_cc(char *buf, size_t buf_size,
814 struct parsed_op *po, enum op_op op, enum opr_lenmod lmod,
815 const char *expr1, const char *expr2)
816{
817 const char *cast = "";
818 const char *scast = "";
819 int is_neg = 0;
820
821 switch (lmod) {
822 case OPLM_DWORD:
823 scast = "(s32)";
824 break;
825 case OPLM_WORD:
826 cast = "(u16)";
827 scast = "(s16)";
828 break;
829 case OPLM_BYTE:
830 cast = "(u8)";
831 scast = "(s8)";
832 break;
833 default:
834 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
835 }
836
837 split_cond(po, &op, &is_neg);
838 switch (op) {
839 case OP_JZ:
840 snprintf(buf, buf_size, "(%s%s %s %s%s)",
841 cast, expr1, is_neg ? "!=" : "==", cast, expr2);
842 break;
843
844 case OP_JC:
845 // note: must be unsigned compare
846 snprintf(buf, buf_size, "(%s%s %s %s%s)",
847 cast, expr1, is_neg ? ">=" : "<", cast, expr2);
848 break;
849
850 case OP_JL:
851 // note: must be unsigned compare
852 snprintf(buf, buf_size, "(%s%s %s %s%s)",
853 scast, expr1, is_neg ? ">=" : "<", scast, expr2);
854 break;
855
856 default:
857 ferr(po, "%s: unhandled op: %d\n", __func__, op);
858 }
859}
860
861static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
91977a1c 862 struct parsed_opr *popr2)
863{
850c9265 864 struct parsed_equ *eq;
865
866 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC) {
867 // lmod could be specified by equ..
868 if (!strncmp(popr1->name, "ebp+", 4)) {
869 eq = equ_find(po, popr1->name + 4);
870 popr1->lmod = eq->lmod;
871 }
872 if (!strncmp(popr2->name, "ebp+", 4)) {
873 eq = equ_find(po, popr2->name + 4);
874 popr2->lmod = eq->lmod;
875 }
876 }
877
91977a1c 878 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
879 ferr(po, "missing lmod for both operands\n");
880
881 if (popr1->lmod == OPLM_UNSPEC)
882 popr1->lmod = popr2->lmod;
883 else if (popr2->lmod == OPLM_UNSPEC)
884 popr2->lmod = popr1->lmod;
885 else if (popr1->lmod != popr2->lmod)
886 ferr(po, "conflicting lmods: %d vs %d\n", popr1->lmod, popr2->lmod);
887}
c36e914d 888
850c9265 889static const char *op_to_c(struct parsed_op *po)
890{
891 switch (po->op)
892 {
893 case OP_ADD:
894 return "+";
895 case OP_SUB:
896 return "-";
897 case OP_AND:
898 return "&";
899 case OP_OR:
900 return "|";
901 case OP_XOR:
902 return "^";
903 case OP_SHL:
904 return "<<";
905 case OP_SHR:
906 return ">>";
907 case OP_MUL:
908 case OP_IMUL:
909 return "*";
910 default:
911 ferr(po, "op_to_c was supplied with %d\n", po->op);
912 }
913}
914
915static int scan_for_pop(int i, int opcnt, const char *reg)
916{
917 for (; i < opcnt; i++) {
918 if (ops[i].cls == OPC_RMD)
919 continue;
920
921 if (ops[i].cls == OPC_JMP || ops[i].cls == OPC_JCC
922 || g_labels[i][0] != 0)
923 return -1;
924
925 if (ops[i].op == OP_POP && ops[i].operand[0].type == OPT_REG
926 && IS(ops[i].operand[0].name, reg))
927 return i;
928 }
929
930 return -1;
931}
932
933// scan for pop starting from ret
934static int scan_for_pop_ret(int i, int opcnt, const char *reg, int do_patch)
935{
936 int found = 0;
937 int j;
938
939 for (; i < opcnt; i++) {
940 if (ops[i].op != OP_RET)
941 continue;
942
943 for (j = i - 1; j >= 0; j--) {
944 if (ops[j].cls == OPC_JMP || ops[j].cls == OPC_JCC)
945 return -1;
946
947 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
948 && IS(ops[j].operand[0].name, reg))
949 {
950 found = 1;
951 if (do_patch)
952 ops[j].cls = OPC_RMD;
953 break;
954 }
955
956 if (g_labels[j][0] != 0)
957 return -1;
958 }
959 }
960
961 return found ? 0 : -1;
962}
963
91977a1c 964static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
965{
850c9265 966 struct parsed_op *po, *delayed_op = NULL, *tmp_op;
967 struct parsed_opr *last_arith_dst = NULL;
91977a1c 968 char buf1[256], buf2[256], buf3[256];
969 struct parsed_proto *pp;
970 const char *tmpname;
971 int had_decl = 0;
972 int regmask_arg = 0;
973 int regmask = 0;
850c9265 974 int special_sbb;
91977a1c 975 int no_output;
976 int arg;
977 int i, j;
978 int reg;
979 int ret;
980
981 g_bp_frame = g_bp_stack = 0;
982
983 ret = proto_parse(fhdr, funcn, &g_func_pp);
984 if (ret)
985 ferr(ops, "proto_parse failed for '%s'\n", funcn);
986
987 fprintf(fout, "%s %s(", g_func_pp.ret_type, funcn);
988 for (i = 0; i < g_func_pp.argc; i++) {
989 if (i > 0)
990 fprintf(fout, ", ");
991 fprintf(fout, "%s a%d", g_func_pp.arg[i].type, i + 1);
992 }
993 fprintf(fout, ")\n{\n");
994
995 // pass1:
996 // - handle ebp frame, remove ops related to it
997 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
998 && ops[1].op == OP_MOV
999 && IS(opr_name(&ops[1], 0), "ebp")
1000 && IS(opr_name(&ops[1], 1), "esp"))
1001 {
1002 g_bp_frame = 1;
1003 ops[0].cls = OPC_RMD;
1004 ops[1].cls = OPC_RMD;
1005
1006 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
1007 g_bp_stack = opr_const(&ops[2], 1);
1008 ops[2].cls = OPC_RMD;
1009 }
1010
1011 i = 2;
1012 do {
1013 for (; i < opcnt; i++)
1014 if (ops[i].op == OP_RET)
1015 break;
1016 if (ops[i - 1].op != OP_POP || !IS(opr_name(&ops[i - 1], 0), "ebp"))
1017 ferr(&ops[i - 1], "'pop ebp' expected\n");
1018 ops[i - 1].cls = OPC_RMD;
1019
1020 if (g_bp_stack != 0) {
1021 if (ops[i - 2].op != OP_MOV
1022 || !IS(opr_name(&ops[i - 2], 0), "esp")
1023 || !IS(opr_name(&ops[i - 2], 1), "ebp"))
1024 {
1025 ferr(&ops[i - 2], "esp restore expected\n");
1026 }
1027 ops[i - 2].cls = OPC_RMD;
1028 }
1029 i++;
1030 } while (i < opcnt);
1031 }
1032
1033 // pass2:
1034 // - scan for all used registers
1035 // - process calls
850c9265 1036 // - find POPs for PUSHes, rm both
91977a1c 1037 for (i = 0; i < opcnt; i++) {
1038 if (ops[i].cls == OPC_RMD)
1039 continue;
850c9265 1040
1041 if (ops[i].op == OP_PUSH && ops[i].operand[0].type == OPT_REG) {
1042 if (ops[i].operand[0].reg < 0)
1043 ferr(&ops[i], "reg not set for push?\n");
1044 if (!(regmask & (1 << ops[i].operand[0].reg))) { // reg save
1045 ret = scan_for_pop(i + 1, opcnt, ops[i].operand[0].name);
1046 if (ret >= 0) {
1047 ops[i].cls = ops[ret].cls = OPC_RMD;
1048 continue;
1049 }
1050 ret = scan_for_pop_ret(i + 1, opcnt, ops[i].operand[0].name, 0);
1051 if (ret == 0) {
1052 ops[i].cls = OPC_RMD;
1053 scan_for_pop_ret(i + 1, opcnt, ops[i].operand[0].name, 1);
1054 continue;
1055 }
1056 }
1057 }
1058
91977a1c 1059 regmask |= ops[i].regmask;
1060
1061 if (ops[i].op == OP_CALL) {
1062 pp = malloc(sizeof(*pp));
1063 my_assert_not(pp, NULL);
1064 tmpname = opr_name(&ops[i], 0);
1065 ret = proto_parse(fhdr, tmpname, pp);
1066 if (ret)
1067 ferr(&ops[i], "proto_parse failed for '%s'\n", tmpname);
1068
1069 for (arg = 0; arg < pp->argc; arg++)
1070 if (pp->arg[arg].reg == NULL)
1071 break;
1072
1073 for (j = i - 1; j >= 0 && arg < pp->argc; j--) {
1074 if (ops[j].cls == OPC_RMD)
1075 continue;
1076 if (ops[j].op != OP_PUSH)
1077 continue;
1078
1079 pp->arg[arg].datap = &ops[j];
1080 ops[j].cls = OPC_RMD;
1081 for (arg++; arg < pp->argc; arg++)
1082 if (pp->arg[arg].reg == NULL)
1083 break;
1084 }
1085 if (arg < pp->argc)
1086 ferr(&ops[i], "arg collect failed for '%s'\n", tmpname);
1087 ops[i].datap = pp;
1088 }
1089 }
1090
850c9265 1091 // declare stack frame
1092 if (g_bp_stack)
1093 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
1094 (g_bp_stack + 3) / 4, (g_bp_stack + 1) / 2, g_bp_stack);
1095
91977a1c 1096 // instantiate arg-registers
1097 for (i = 0; i < g_func_pp.argc; i++) {
1098 if (g_func_pp.arg[i].reg != NULL) {
1099 reg = char_array_i(regs_r32,
1100 ARRAY_SIZE(regs_r32), g_func_pp.arg[i].reg);
1101 if (reg < 0)
1102 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp.arg[i].reg);
1103
1104 regmask_arg |= 1 << reg;
1105 fprintf(fout, " u32 %s = (u32)a%d;\n",
850c9265 1106 g_func_pp.arg[i].reg, i + 1);
91977a1c 1107 had_decl = 1;
1108 }
1109 }
1110
1111 // instantiate other regs - special case for eax
1112 if (!((regmask | regmask_arg) & 1) && !IS(g_func_pp.ret_type, "void")) {
1113 fprintf(fout, " u32 eax = 0;\n");
1114 had_decl = 1;
1115 }
1116
1117 regmask &= ~regmask_arg;
1118 if (g_bp_frame)
1119 regmask &= ~(1 << xBP);
1120 if (regmask) {
1121 for (reg = 0; reg < 8; reg++) {
1122 if (regmask & (1 << reg)) {
1123 fprintf(fout, " u32 %s;\n", regs_r32[reg]);
1124 had_decl = 1;
1125 }
1126 }
1127 }
1128
1129 if (had_decl)
1130 fprintf(fout, "\n");
1131
1132 // output ops
1133 for (i = 0; i < opcnt; i++) {
1134 if (g_labels[i][0] != 0)
1135 fprintf(fout, "\n%s:\n", g_labels[i]);
1136
1137 if (ops[i].cls == OPC_RMD)
1138 continue;
1139
850c9265 1140 po = &ops[i];
91977a1c 1141 no_output = 0;
1142
91977a1c 1143 #define assert_operand_cnt(n_) \
850c9265 1144 if (po->operand_cnt != n_) \
1145 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
1146
1147 if (last_arith_dst != NULL && po->operand_cnt > 0
1148 && po->operand[0].type == OPT_REG)
1149 {
1150 if (IS(last_arith_dst->name, po->operand[0].name))
1151 last_arith_dst = NULL;
1152 }
91977a1c 1153
1154 // conditional op?
850c9265 1155 special_sbb = 0;
1156 if (po->op == OP_SBB && IS(opr_name(po, 0), opr_name(po, 1)))
1157 special_sbb = 1;
1158
1159 if (po->cls == OPC_JCC || po->cls == OPC_SCC || special_sbb)
1160 {
1161 enum op_op flag_op = po->op;
1162 if (special_sbb)
1163 flag_op = OP_JC;
1164
1165 if (delayed_op != NULL)
1166 {
1167 if (delayed_op->op == OP_TEST) {
1168 if (IS(opr_name(delayed_op, 0), opr_name(delayed_op, 1))) {
1169 out_src_opr(buf3, sizeof(buf3), delayed_op,
1170 &delayed_op->operand[0], 0);
1171 }
1172 else {
1173 out_src_opr(buf1, sizeof(buf1), delayed_op,
1174 &delayed_op->operand[0], 0);
1175 out_src_opr(buf2, sizeof(buf2), delayed_op,
1176 &delayed_op->operand[1], 0);
1177 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1178 }
1179 out_test_for_cc(buf1, sizeof(buf1), po, flag_op,
1180 delayed_op->operand[0].lmod, buf3);
91977a1c 1181 }
850c9265 1182 else if (delayed_op->op == OP_CMP) {
91977a1c 1183 out_src_opr(buf2, sizeof(buf2), delayed_op,
850c9265 1184 &delayed_op->operand[0], 0);
1185 out_src_opr(buf3, sizeof(buf3), delayed_op,
1186 &delayed_op->operand[1], 0);
1187 out_cmp_for_cc(buf1, sizeof(buf1), po, flag_op,
1188 delayed_op->operand[0].lmod, buf2, buf3);
91977a1c 1189 }
1190 }
850c9265 1191 else if (last_arith_dst != NULL
1192 && (flag_op == OP_JZ || flag_op == OP_JNZ))
1193 {
1194 out_src_opr(buf3, sizeof(buf3), po, last_arith_dst, 0);
1195 out_test_for_cc(buf1, sizeof(buf1), po, flag_op,
1196 last_arith_dst->lmod, buf3);
1197 }
1198 else
1199 ferr(po, "no delayed_op or last arith result before cond op\n");
1200
1201 if (po->cls == OPC_JCC) {
1202 fprintf(fout, " if %s\n", buf1);
1203 }
1204 else if (special_sbb) {
1205 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
1206 fprintf(fout, " %s = %s * -1;", buf2, buf1);
1207 }
91977a1c 1208 else {
850c9265 1209 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
1210 fprintf(fout, " %s = %s;", buf2, buf1);
91977a1c 1211 }
1212 }
1213
850c9265 1214 switch (po->op)
91977a1c 1215 {
1216 case OP_MOV:
1217 assert_operand_cnt(2);
850c9265 1218 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1219 fprintf(fout, " %s = %s;",
1220 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1221 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1222 break;
1223
1224 case OP_LEA:
1225 assert_operand_cnt(2);
1226 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1227 fprintf(fout, " %s = %s;",
1228 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1229 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 1));
1230 break;
1231
1232 case OP_MOVZX:
1233 assert_operand_cnt(2);
91977a1c 1234 fprintf(fout, " %s = %s;",
850c9265 1235 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1236 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1237 break;
1238
1239 case OP_MOVSX:
1240 assert_operand_cnt(2);
1241 switch (po->operand[1].lmod) {
1242 case OPLM_BYTE:
1243 strcpy(buf3, "(s8)");
1244 break;
1245 case OPLM_WORD:
1246 strcpy(buf3, "(s16)");
1247 break;
1248 default:
1249 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
1250 }
1251 fprintf(fout, " %s = %s%s;",
1252 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1253 buf3,
1254 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1255 break;
1256
1257 case OP_NOT:
1258 assert_operand_cnt(1);
1259 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1260 fprintf(fout, " %s = ~%s;", buf1, buf1);
1261 break;
1262
1263 // arithmetic w/flags
1264 case OP_ADD:
1265 case OP_SUB:
1266 case OP_AND:
1267 case OP_OR:
1268 case OP_XOR:
1269 case OP_SHL:
1270 case OP_SHR:
1271 dualop_arith:
1272 assert_operand_cnt(2);
1273 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1274 fprintf(fout, " %s %s= %s;",
1275 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1276 op_to_c(po),
1277 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1278 last_arith_dst = &po->operand[0];
1279 delayed_op = NULL;
1280 break;
1281
1282 case OP_SAR:
1283 assert_operand_cnt(2);
1284 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1285 switch (po->operand[0].lmod) {
1286 case OPLM_BYTE:
1287 strcpy(buf3, "(s8)");
1288 break;
1289 case OPLM_WORD:
1290 strcpy(buf3, "(s16)");
1291 break;
1292 case OPLM_DWORD:
1293 strcpy(buf3, "(s32)");
1294 break;
1295 default:
1296 ferr(po, "invalid dst lmod: %d\n", po->operand[0].lmod);
1297 }
1298 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1299 fprintf(fout, " %s = %s%s >> %s;", buf1, buf3, buf1,
1300 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1301 last_arith_dst = &po->operand[0];
1302 delayed_op = NULL;
1303 break;
1304
1305 case OP_SBB:
1306 if (!special_sbb)
1307 ferr(po, "TODO\n");
1308 break;
1309
1310 case OP_INC:
1311 case OP_DEC:
1312 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1313 strcpy(buf2, po->op == OP_INC ? "++" : "--");
1314 switch (po->operand[0].lmod) {
1315 case OPLM_BYTE:
1316 fprintf(fout, " LOBYTE(%s)%s;", buf1, buf2);
1317 break;
1318 case OPLM_WORD:
1319 fprintf(fout, " LOWORD(%s)%s;", buf1, buf2);
1320 break;
1321 case OPLM_DWORD:
1322 fprintf(fout, " %s%s;", buf1, buf2);
1323 break;
1324 default:
1325 ferr(po, "invalid dst lmod: %d\n", po->operand[0].lmod);
1326 }
1327 last_arith_dst = &po->operand[0];
1328 delayed_op = NULL;
1329 break;
1330
1331 case OP_IMUL:
1332 if (po->operand_cnt == 2)
1333 goto dualop_arith;
1334 ferr(po, "TODO imul\n");
1335 last_arith_dst = &po->operand[0];
1336 delayed_op = NULL;
91977a1c 1337 break;
1338
1339 case OP_TEST:
1340 case OP_CMP:
850c9265 1341 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1342 delayed_op = po;
91977a1c 1343 no_output = 1;
1344 break;
1345
1346 // note: we reuse OP_Jcc for SETcc, only cls differs
1347 case OP_JO ... OP_JG:
850c9265 1348 if (po->cls == OPC_JCC)
1349 fprintf(fout, " goto %s;", po->operand[0].name);
1350 break;
1351
1352 case OP_JMP:
1353 fprintf(fout, " goto %s;", po->operand[0].name);
91977a1c 1354 break;
1355
1356 case OP_CALL:
850c9265 1357 pp = po->datap;
91977a1c 1358 if (pp == NULL)
850c9265 1359 ferr(po, "NULL pp\n");
91977a1c 1360
1361 fprintf(fout, " ");
1362 if (!IS(pp->ret_type, "void")) {
1363 fprintf(fout, "eax = ");
1364 if (strchr(pp->ret_type, '*'))
1365 fprintf(fout, "(u32)");
1366 }
850c9265 1367 fprintf(fout, "%s(", opr_name(po, 0));
91977a1c 1368 for (arg = 0; arg < pp->argc; arg++) {
1369 if (arg > 0)
1370 fprintf(fout, ", ");
1371 if (pp->arg[arg].reg != NULL) {
850c9265 1372 fprintf(fout, "%s", pp->arg[arg].reg);
91977a1c 1373 continue;
1374 }
1375
1376 // stack arg
1377 tmp_op = pp->arg[arg].datap;
1378 if (tmp_op == NULL)
850c9265 1379 ferr(po, "parsed_op missing for arg%d\n", arg);
91977a1c 1380 fprintf(fout, "%s",
850c9265 1381 out_src_opr(buf1, sizeof(buf1), tmp_op, &tmp_op->operand[0], 0));
91977a1c 1382 }
1383 fprintf(fout, ");");
1384 break;
1385
1386 case OP_RET:
1387 if (IS(g_func_pp.ret_type, "void"))
1388 fprintf(fout, " return;");
1389 else
1390 fprintf(fout, " return eax;");
1391 break;
1392
1393 case OP_PUSH:
850c9265 1394 ferr(po, "push encountered\n");
91977a1c 1395 break;
1396
1397 case OP_POP:
850c9265 1398 ferr(po, "pop encountered\n");
91977a1c 1399 break;
1400
1401 default:
1402 no_output = 1;
850c9265 1403 ferr(po, "unhandled op type %d, cls %d\n",
1404 po->op, po->cls);
91977a1c 1405 break;
1406 }
1407
1408 if (g_comment[0] != 0) {
1409 fprintf(fout, " // %s", g_comment);
1410 g_comment[0] = 0;
1411 no_output = 0;
1412 }
1413 if (!no_output)
1414 fprintf(fout, "\n");
1415 }
1416
1417 fprintf(fout, "}\n\n");
1418
1419 // cleanup
1420 for (i = 0; i < opcnt; i++) {
1421 if (ops[i].op == OP_CALL) {
1422 pp = ops[i].datap;
1423 if (pp) {
1424 proto_release(pp);
1425 free(pp);
1426 }
1427 }
1428 }
1429 proto_release(&g_func_pp);
1430}
c36e914d 1431
91977a1c 1432int main(int argc, char *argv[])
1433{
1434 FILE *fout, *fasm, *fhdr;
1435 char line[256];
1436 char words[16][256];
1437 int in_func = 0;
1438 int eq_alloc;
1439 int pi = 0;
1440 int len;
1441 char *p;
1442 int wordc;
1443
1444 if (argc != 4) {
1445 printf("usage:\n%s <.c> <.asm> <hdrf>\n",
1446 argv[0]);
1447 return 1;
1448 }
1449
1450 hdrfn = argv[3];
1451 fhdr = fopen(hdrfn, "r");
1452 my_assert_not(fhdr, NULL);
1453
1454 asmfn = argv[2];
1455 fasm = fopen(asmfn, "r");
1456 my_assert_not(fasm, NULL);
1457
1458 fout = fopen(argv[1], "w");
1459 my_assert_not(fout, NULL);
1460
1461 eq_alloc = 128;
1462 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
1463 my_assert_not(g_eqs, NULL);
1464
1465 while (fgets(line, sizeof(line), fasm))
1466 {
1467 asmln++;
1468
1469 p = sskip(line);
1470 if (*p == 0 || *p == ';')
1471 continue;
1472
1473 memset(words, 0, sizeof(words));
1474 for (wordc = 0; wordc < 16; wordc++) {
1475 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
1476 if (*p == 0 || *p == ';') {
1477 wordc++;
1478 break;
1479 }
1480 }
1481
1482 if (wordc == 0) {
1483 // shouldn't happen
1484 awarn("wordc == 0?\n");
1485 continue;
1486 }
1487
1488 // don't care about this:
1489 if (words[0][0] == '.'
1490 || IS(words[0], "include")
1491 || IS(words[0], "assume") || IS(words[1], "segment")
1492 || IS(words[0], "align"))
1493 {
1494 continue;
1495 }
1496
1497 if (IS(words[1], "proc")) {
1498 if (in_func)
1499 aerr("proc '%s' while in_func '%s'?\n",
1500 words[0], g_func);
1501 strcpy(g_func, words[0]);
1502 in_func = 1;
1503 continue;
1504 }
1505
1506 if (IS(words[1], "endp")) {
1507 if (!in_func)
1508 aerr("endp '%s' while not in_func?\n", words[0]);
1509 if (!IS(g_func, words[0]))
1510 aerr("endp '%s' while in_func '%s'?\n",
1511 words[0], g_func);
1512 gen_func(fout, fhdr, g_func, pi);
1513 in_func = 0;
1514 g_func[0] = 0;
1515 if (pi != 0) {
1516 memset(&ops, 0, pi * sizeof(ops[0]));
1517 memset(g_labels, 0, pi * sizeof(g_labels[0]));
1518 pi = 0;
1519 }
1520 g_eqcnt = 0;
91977a1c 1521 continue;
1522 }
1523
1524 if (IS(words[1], "=")) {
1525 if (wordc != 5)
1526 aerr("unhandled equ, wc=%d\n", wordc);
1527 if (g_eqcnt >= eq_alloc) {
1528 eq_alloc *= 2;
1529 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
1530 my_assert_not(g_eqs, NULL);
1531 }
1532
1533 len = strlen(words[0]);
1534 if (len > sizeof(g_eqs[0].name) - 1)
1535 aerr("equ name too long: %d\n", len);
1536 strcpy(g_eqs[g_eqcnt].name, words[0]);
1537
1538 if (!IS(words[3], "ptr"))
1539 aerr("unhandled equ\n");
1540 if (IS(words[2], "dword"))
1541 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
1542 else if (IS(words[2], "word"))
1543 g_eqs[g_eqcnt].lmod = OPLM_WORD;
1544 else if (IS(words[2], "byte"))
1545 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
1546 else
1547 aerr("bad lmod: '%s'\n", words[2]);
1548
1549 g_eqs[g_eqcnt].offset = parse_number(words[4]);
1550 g_eqcnt++;
1551 continue;
1552 }
1553
1554 if (pi >= ARRAY_SIZE(ops))
1555 aerr("too many ops\n");
1556
1557 p = strchr(words[0], ':');
1558 if (p != NULL) {
1559 len = p - words[0];
1560 if (len > sizeof(g_labels[0]) - 1)
1561 aerr("label too long: %d\n", len);
1562 if (g_labels[pi][0] != 0)
1563 aerr("dupe label?\n");
1564 memcpy(g_labels[pi], words[0], len);
1565 g_labels[pi][len] = 0;
1566 continue;
1567 }
1568
1569 parse_op(&ops[pi], words, wordc);
1570 pi++;
1571
1572 (void)proto_parse;
1573 }
1574
1575 fclose(fout);
1576 fclose(fasm);
1577 fclose(fhdr);
1578
1579 return 0;
c36e914d 1580}
91977a1c 1581
1582// vim:ts=2:shiftwidth=2:expandtab