improve call arg and flag tracking, fixes
[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
940e8e66 16#define anote(fmt, ...) \
17 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
c36e914d 18#define awarn(fmt, ...) \
940e8e66 19 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
c36e914d 20#define aerr(fmt, ...) do { \
940e8e66 21 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
c36e914d 22 exit(1); \
23} while (0)
24
69a3cdfc 25enum op_flags {
87bf6cec 26 OPF_RMD = (1 << 0), /* removed or optimized out */
27 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
28 OPF_FLAGS = (1 << 2), /* sets flags */
29 OPF_JMP = (1 << 3), /* branches, ret and call */
30 OPF_CC = (1 << 4), /* uses flags */
31 OPF_TAIL = (1 << 5), /* ret or tail call */
c36e914d 32};
33
34enum op_op {
35 OP_INVAL,
36 OP_PUSH,
37 OP_POP,
38 OP_MOV,
850c9265 39 OP_LEA,
40 OP_MOVZX,
41 OP_MOVSX,
42 OP_NOT,
5101a5f9 43 OP_CDQ,
c36e914d 44 OP_RET,
45 OP_ADD,
91977a1c 46 OP_SUB,
850c9265 47 OP_AND,
48 OP_OR,
49 OP_XOR,
50 OP_SHL,
51 OP_SHR,
52 OP_SAR,
69a3cdfc 53 OP_ADC,
850c9265 54 OP_SBB,
55 OP_INC,
56 OP_DEC,
5101a5f9 57 OP_NEG,
850c9265 58 OP_MUL,
59 OP_IMUL,
5101a5f9 60 OP_DIV,
61 OP_IDIV,
c36e914d 62 OP_TEST,
63 OP_CMP,
64 OP_CALL,
65 OP_JMP,
66 OP_JO,
67 OP_JNO,
68 OP_JC,
69 OP_JNC,
70 OP_JZ,
71 OP_JNZ,
72 OP_JBE,
73 OP_JA,
74 OP_JS,
75 OP_JNS,
76 OP_JP,
77 OP_JNP,
78 OP_JL,
79 OP_JGE,
80 OP_JLE,
81 OP_JG,
82};
83
84enum opr_type {
87bf6cec 85 OPT_UNSPEC,
86 OPT_REG,
87 OPT_REGMEM,
88 OPT_LABEL,
850c9265 89 OPT_OFFSET,
87bf6cec 90 OPT_CONST,
c36e914d 91};
92
93enum opr_lenmod {
91977a1c 94 OPLM_UNSPEC,
95 OPLM_BYTE,
96 OPLM_WORD,
97 OPLM_DWORD,
c36e914d 98};
99
850c9265 100#define MAX_OPERANDS 3
c36e914d 101
102struct parsed_opr {
91977a1c 103 enum opr_type type;
104 enum opr_lenmod lmod;
105 int reg;
106 unsigned int val;
107 char name[256];
c36e914d 108};
109
110struct parsed_op {
91977a1c 111 enum op_op op;
112 struct parsed_opr operand[MAX_OPERANDS];
69a3cdfc 113 unsigned int flags;
91977a1c 114 int operand_cnt;
69a3cdfc 115 int regmask_src; // all referensed regs
116 int regmask_dst;
940e8e66 117 int pfomask; // flagop: parsed_flag_op that can't be delayed
118 int argmask; // push: args that are altered before call
119 int cc_scratch; // scratch storage during analysis
87bf6cec 120 int bt_i; // branch target (for branches)
940e8e66 121 struct parsed_op *lrl; // label reference list entry
91977a1c 122 void *datap;
123};
124
69a3cdfc 125// datap:
69a3cdfc 126// OP_CALL - ptr to parsed_proto
127// (OPF_CC) - point to corresponding (OPF_FLAGS)
128
91977a1c 129struct parsed_equ {
130 char name[64];
131 enum opr_lenmod lmod;
132 int offset;
c36e914d 133};
134
135#define MAX_OPS 1024
136
137static struct parsed_op ops[MAX_OPS];
91977a1c 138static struct parsed_equ *g_eqs;
139static int g_eqcnt;
140static char g_labels[MAX_OPS][32];
940e8e66 141static struct parsed_op *g_label_refs[MAX_OPS];
91977a1c 142static struct parsed_proto g_func_pp;
143static char g_func[256];
144static char g_comment[256];
145static int g_bp_frame;
146static int g_bp_stack;
147#define ferr(op_, fmt, ...) do { \
850c9265 148 printf("error:%s:#%ld: '%s': " fmt, g_func, (op_) - ops, \
149 dump_op(op_), ##__VA_ARGS__); \
91977a1c 150 exit(1); \
151} while (0)
152
153#define MAX_REGS 8
154
155const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
156const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
157const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
158const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
159
160enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
161
69a3cdfc 162// possible basic comparison types (without inversion)
163enum parsed_flag_op {
164 PFO_O, // 0 OF=1
165 PFO_C, // 2 CF=1
166 PFO_Z, // 4 ZF=1
167 PFO_BE, // 6 CF=1||ZF=1
168 PFO_S, // 8 SF=1
169 PFO_P, // a PF=1
170 PFO_L, // c SF!=OF
171 PFO_LE, // e ZF=1||SF!=OF
172};
173
174static const char *parsed_flag_op_names[] = {
175 "o", "c", "z", "be", "s", "p", "l", "le"
176};
177
91977a1c 178static int char_array_i(const char *array[], size_t len, const char *s)
179{
180 int i;
c36e914d 181
91977a1c 182 for (i = 0; i < len; i++)
183 if (IS(s, array[i]))
184 return i;
c36e914d 185
91977a1c 186 return -1;
187}
188
5101a5f9 189static void printf_number(char *buf, size_t buf_size, long number)
91977a1c 190{
5101a5f9 191 // output in C-friendly form
192 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
193}
91977a1c 194
5101a5f9 195static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
196{
197 int reg;
91977a1c 198
5101a5f9 199 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
200 if (reg >= 0) {
201 *reg_lmod = OPLM_DWORD;
202 return reg;
91977a1c 203 }
5101a5f9 204 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
205 if (reg >= 0) {
206 *reg_lmod = OPLM_WORD;
207 return reg;
208 }
209 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
210 if (reg >= 0) {
211 *reg_lmod = OPLM_BYTE;
212 return reg;
213 }
214 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
215 if (reg >= 0) {
216 *reg_lmod = OPLM_BYTE;
217 return reg;
850c9265 218 }
219
220 return -1;
91977a1c 221}
222
223static long parse_number(const char *number)
224{
225 int len = strlen(number);
226 const char *p = number;
227 char *endp = NULL;
228 int neg = 0;
229 int bad;
230 long ret;
231
232 if (*p == '-') {
233 neg = 1;
234 p++;
235 }
236 if (len > 1 && *p == '0')
237 p++;
238 if (number[len - 1] == 'h') {
239 ret = strtol(p, &endp, 16);
240 bad = (*endp != 'h');
241 }
242 else {
243 ret = strtol(p, &endp, 10);
244 bad = (*endp != 0);
245 }
246 if (bad)
247 aerr("number parsing failed\n");
248 if (neg)
249 ret = -ret;
250 return ret;
251}
252
5101a5f9 253static int parse_indmode(char *name, int *regmask, int need_c_cvt)
254{
255 enum opr_lenmod lmod;
256 char cvtbuf[256];
257 char *d = cvtbuf;
258 char *s = name;
259 char w[64];
260 long number;
261 int reg;
262 int c = 0;
263
264 *d = 0;
265
266 while (*s != 0) {
267 d += strlen(d);
268 while (my_isblank(*s))
269 s++;
270 for (; my_issep(*s); d++, s++)
271 *d = *s;
272 while (my_isblank(*s))
273 s++;
274 *d = 0;
275
87bf6cec 276 // skip 'ds:' prefix
277 if (!strncmp(s, "ds:", 3))
278 s += 3;
279
5101a5f9 280 s = next_idt(w, sizeof(w), s);
281 if (w[0] == 0)
282 break;
283 c++;
284
285 reg = parse_reg(&lmod, w);
286 if (reg >= 0) {
287 *regmask |= 1 << reg;
288 goto pass;
289 }
290
291 if ('0' <= w[0] && w[0] <= '9') {
292 number = parse_number(w);
293 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
294 continue;
295 }
296
297 // probably some label/identifier - pass
298
299pass:
300 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
301 }
302
303 if (need_c_cvt)
304 strcpy(name, cvtbuf);
305
306 return c;
307}
308
850c9265 309static int guess_lmod_from_name(struct parsed_opr *opr)
310{
311 if (!strncmp(opr->name, "dword_", 6)) {
312 opr->lmod = OPLM_DWORD;
313 return 1;
314 }
315 if (!strncmp(opr->name, "word_", 5)) {
316 opr->lmod = OPLM_WORD;
317 return 1;
318 }
319 if (!strncmp(opr->name, "byte_", 5)) {
320 opr->lmod = OPLM_BYTE;
321 return 1;
322 }
323 return 0;
324}
325
5101a5f9 326static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
327 int *regmask)
328{
329 opr->type = OPT_REG;
330 opr->reg = reg;
331 opr->lmod = lmod;
332 *regmask |= 1 << reg;
333}
334
87bf6cec 335static struct parsed_equ *equ_find(struct parsed_op *po, const char *name);
336
69a3cdfc 337static int parse_operand(struct parsed_opr *opr,
338 int *regmask, int *regmask_indirect,
339 char words[16][256], int wordc, int w, unsigned int op_flags)
c36e914d 340{
850c9265 341 enum opr_lenmod tmplmod;
850c9265 342 int ret, len;
5101a5f9 343 long number;
850c9265 344 int i;
c36e914d 345
346 if (w >= wordc)
347 aerr("parse_operand w %d, wordc %d\n", w, wordc);
348
91977a1c 349 opr->reg = xUNSPEC;
350
c36e914d 351 for (i = w; i < wordc; i++) {
352 len = strlen(words[i]);
353 if (words[i][len - 1] == ',') {
354 words[i][len - 1] = 0;
355 wordc = i + 1;
356 break;
357 }
358 }
359
69a3cdfc 360 if (op_flags & OPF_JMP) {
c36e914d 361 const char *label;
362
363 if (wordc - w == 3 && IS(words[w + 1], "ptr"))
364 label = words[w + 2];
365 else if (wordc - w == 2 && IS(words[w], "short"))
366 label = words[w + 1];
367 else if (wordc - w == 1)
368 label = words[w];
369 else
370 aerr("jump parse error");
371
372 opr->type = OPT_LABEL;
373 strcpy(opr->name, label);
374 return wordc;
375 }
376
377 if (wordc - w >= 3) {
378 if (IS(words[w + 1], "ptr")) {
379 if (IS(words[w], "dword"))
91977a1c 380 opr->lmod = OPLM_DWORD;
c36e914d 381 else if (IS(words[w], "word"))
91977a1c 382 opr->lmod = OPLM_WORD;
c36e914d 383 else if (IS(words[w], "byte"))
91977a1c 384 opr->lmod = OPLM_BYTE;
c36e914d 385 else
386 aerr("type parsing failed\n");
387 w += 2;
388 }
389 }
390
391 if (wordc - w == 2 && IS(words[w], "offset")) {
850c9265 392 opr->type = OPT_OFFSET;
c36e914d 393 strcpy(opr->name, words[w + 1]);
394 return wordc;
395 }
396
850c9265 397 if (wordc - w != 1)
398 aerr("parse_operand 1 word expected\n");
c36e914d 399
850c9265 400 strcpy(opr->name, words[w]);
c36e914d 401
850c9265 402 if (words[w][0] == '[') {
403 opr->type = OPT_REGMEM;
404 ret = sscanf(words[w], "[%[^]]]", opr->name);
405 if (ret != 1)
406 aerr("[] parse failure\n");
87bf6cec 407
5101a5f9 408 parse_indmode(opr->name, regmask_indirect, 1);
87bf6cec 409 if (opr->lmod == OPLM_UNSPEC && !strncmp(opr->name, "ebp+", 4)) {
410 // might be an equ
411 struct parsed_equ *eq = equ_find(NULL, opr->name + 4);
412 if (eq)
413 opr->lmod = eq->lmod;
414 }
850c9265 415 return wordc;
416 }
417 else if (strchr(words[w], '[')) {
418 // label[reg] form
419 opr->type = OPT_REGMEM;
420 if (opr->lmod == OPLM_UNSPEC)
421 guess_lmod_from_name(opr);
5101a5f9 422 parse_indmode(strchr(words[w], '['), regmask_indirect, 0);
850c9265 423 return wordc;
424 }
425 else if (('0' <= words[w][0] && words[w][0] <= '9')
426 || words[w][0] == '-')
427 {
5101a5f9 428 number = parse_number(words[w]);
91977a1c 429 opr->type = OPT_CONST;
5101a5f9 430 opr->val = number;
431 printf_number(opr->name, sizeof(opr->name), number);
91977a1c 432 return wordc;
850c9265 433 }
c36e914d 434
5101a5f9 435 ret = parse_reg(&tmplmod, opr->name);
436 if (ret >= 0) {
437 setup_reg_opr(opr, ret, tmplmod, regmask);
850c9265 438 return wordc;
439 }
440
441 // most likely var in data segment
442 opr->type = OPT_LABEL;
443 if (opr->lmod == OPLM_UNSPEC)
444 guess_lmod_from_name(opr);
445 if (opr->lmod != OPLM_UNSPEC)
446 return wordc;
91977a1c 447
850c9265 448 // TODO: scan data seg to determine type?
449 return wordc;
c36e914d 450}
451
452static const struct {
850c9265 453 const char *name;
454 enum op_op op;
69a3cdfc 455 unsigned int minopr;
456 unsigned int maxopr;
457 unsigned int flags;
c36e914d 458} op_table[] = {
69a3cdfc 459 { "push", OP_PUSH, 1, 1, 0 },
460 { "pop", OP_POP, 1, 1, OPF_DATA },
461 { "mov" , OP_MOV, 2, 2, OPF_DATA },
462 { "lea", OP_LEA, 2, 2, OPF_DATA },
463 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
464 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
465 { "not", OP_NOT, 1, 1, OPF_DATA },
5101a5f9 466 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
69a3cdfc 467 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
468 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
469 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
470 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
471 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
472 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
473 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
474 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
475 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
5101a5f9 476 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
69a3cdfc 477 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
478 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
479 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
5101a5f9 480 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
481 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
69a3cdfc 482 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
5101a5f9 483 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
484 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
69a3cdfc 485 { "test", OP_TEST, 2, 2, OPF_FLAGS },
486 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
87bf6cec 487 { "retn", OP_RET, 0, 1, OPF_JMP|OPF_TAIL },
488 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_FLAGS },
69a3cdfc 489 { "jmp", OP_JMP, 1, 1, OPF_JMP },
490 { "jo", OP_JO, 1, 1, OPF_JMP|OPF_CC }, // 70 OF=1
491 { "jno", OP_JNO, 1, 1, OPF_JMP|OPF_CC }, // 71 OF=0
492 { "jc", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72 CF=1
493 { "jb", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72
494 { "jnc", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 CF=0
495 { "jae", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73
496 { "jz", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74 ZF=1
497 { "je", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74
498 { "jnz", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75 ZF=0
499 { "jne", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75
500 { "jbe", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76 CF=1 || ZF=1
501 { "jna", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76
502 { "ja", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77 CF=0 && ZF=0
503 { "jnbe", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77
504 { "js", OP_JS, 1, 1, OPF_JMP|OPF_CC }, // 78 SF=1
505 { "jns", OP_JNS, 1, 1, OPF_JMP|OPF_CC }, // 79 SF=0
506 { "jp", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a PF=1
507 { "jpe", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a
508 { "jnp", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b PF=0
509 { "jpo", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b
510 { "jl", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c SF!=OF
511 { "jnge", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c
512 { "jge", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d SF=OF
513 { "jnl", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d
514 { "jle", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e ZF=1 || SF!=OF
515 { "jng", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e
516 { "jg", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f ZF=0 && SF=OF
517 { "jnle", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f
5101a5f9 518 { "seto", OP_JO, 1, 1, OPF_DATA|OPF_CC },
519 { "setno", OP_JNO, 1, 1, OPF_DATA|OPF_CC },
520 { "setc", OP_JC, 1, 1, OPF_DATA|OPF_CC },
521 { "setb", OP_JC, 1, 1, OPF_DATA|OPF_CC },
522 { "setnc", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
523 { "setae", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
524 { "setz", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
525 { "sete", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
526 { "setnz", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
527 { "setne", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
528 { "setbe", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
529 { "setna", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
530 { "seta", OP_JA, 1, 1, OPF_DATA|OPF_CC },
531 { "setnbe", OP_JA, 1, 1, OPF_DATA|OPF_CC },
532 { "sets", OP_JS, 1, 1, OPF_DATA|OPF_CC },
533 { "setns", OP_JNS, 1, 1, OPF_DATA|OPF_CC },
534 { "setp", OP_JP, 1, 1, OPF_DATA|OPF_CC },
535 { "setpe", OP_JP, 1, 1, OPF_DATA|OPF_CC },
536 { "setnp", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
537 { "setpo", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
538 { "setl", OP_JL, 1, 1, OPF_DATA|OPF_CC },
539 { "setnge", OP_JL, 1, 1, OPF_DATA|OPF_CC },
540 { "setge", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
541 { "setnl", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
542 { "setle", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
543 { "setng", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
544 { "setg", OP_JG, 1, 1, OPF_DATA|OPF_CC },
545 { "setnle", OP_JG, 1, 1, OPF_DATA|OPF_CC },
69a3cdfc 546};
c36e914d 547
548static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
549{
69a3cdfc 550 int regmask_ind;
551 int regmask;
91977a1c 552 int opr = 0;
553 int w = 1;
554 int i;
c36e914d 555
91977a1c 556 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
69a3cdfc 557 if (IS(words[0], op_table[i].name))
558 break;
559 }
c36e914d 560
69a3cdfc 561 if (i == ARRAY_SIZE(op_table))
562 aerr("unhandled op: '%s'\n", words[0]);
c36e914d 563
69a3cdfc 564 op->op = op_table[i].op;
565 op->flags = op_table[i].flags;
566 op->regmask_src = op->regmask_dst = 0;
567
568 for (opr = 0; opr < op_table[i].minopr; opr++) {
569 regmask = regmask_ind = 0;
570 w = parse_operand(&op->operand[opr], &regmask, &regmask_ind,
571 words, wordc, w, op->flags);
572
573 if (opr == 0 && (op->flags & OPF_DATA))
574 op->regmask_dst = regmask;
575 // for now, mark dst as src too
576 op->regmask_src |= regmask | regmask_ind;
577 }
c36e914d 578
69a3cdfc 579 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
580 w = parse_operand(&op->operand[opr],
581 &op->regmask_src, &op->regmask_src,
582 words, wordc, w, op->flags);
91977a1c 583 }
c36e914d 584
91977a1c 585 if (w < wordc)
586 aerr("parse_op %s incomplete: %d/%d\n",
587 words[0], w, wordc);
5101a5f9 588
589 // special cases
590 op->operand_cnt = opr;
591 if (!strncmp(op_table[i].name, "set", 3))
592 op->operand[0].lmod = OPLM_BYTE;
593
594 // ops with implicit argumets
595 switch (op->op) {
596 case OP_CDQ:
597 op->operand_cnt = 2;
598 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
599 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
600 break;
601
602 case OP_IMUL:
603 if (op->operand_cnt != 1)
604 break;
605 // fallthrough
606 case OP_MUL:
607 // singleop mul
608 op->regmask_dst = (1 << xDX) | (1 << xAX);
609 op->regmask_src |= (1 << xAX);
610 if (op->operand[0].lmod == OPLM_UNSPEC)
611 op->operand[0].lmod = OPLM_DWORD;
612 break;
613
614 case OP_DIV:
615 case OP_IDIV:
616 // we could set up operands for edx:eax, but there is no real need to
617 // (see is_opr_modified())
618 regmask = (1 << xDX) | (1 << xAX);
619 op->regmask_dst = regmask;
620 op->regmask_src |= regmask;
621 if (op->operand[0].lmod == OPLM_UNSPEC)
622 op->operand[0].lmod = OPLM_DWORD;
623 break;
624
625 case OP_SHL:
626 case OP_SHR:
627 case OP_SAR:
628 if (op->operand[1].lmod == OPLM_UNSPEC)
629 op->operand[1].lmod = OPLM_BYTE;
630 break;
631
632 default:
633 break;
634 }
c36e914d 635}
636
850c9265 637static const char *op_name(enum op_op op)
638{
639 int i;
640
641 for (i = 0; i < ARRAY_SIZE(op_table); i++)
642 if (op_table[i].op == op)
643 return op_table[i].name;
644
645 return "???";
646}
647
648// debug
649static const char *dump_op(struct parsed_op *po)
650{
651 static char out[128];
652 char *p = out;
653 int i;
654
655 snprintf(out, sizeof(out), "%s", op_name(po->op));
656 for (i = 0; i < po->operand_cnt; i++) {
657 p += strlen(p);
658 if (i > 0)
659 *p++ = ',';
660 snprintf(p, sizeof(out) - (p - out),
661 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
662 po->operand[i].name);
663 }
664
665 return out;
666}
667
91977a1c 668static const char *opr_name(struct parsed_op *po, int opr_num)
c36e914d 669{
91977a1c 670 if (opr_num >= po->operand_cnt)
671 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
672 return po->operand[opr_num].name;
c36e914d 673}
674
91977a1c 675static unsigned int opr_const(struct parsed_op *po, int opr_num)
c36e914d 676{
91977a1c 677 if (opr_num >= po->operand_cnt)
678 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
679 if (po->operand[opr_num].type != OPT_CONST)
680 ferr(po, "opr %d: const expected\n", opr_num);
681 return po->operand[opr_num].val;
682}
c36e914d 683
91977a1c 684static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
685{
686 if ((unsigned int)popr->reg >= MAX_REGS)
687 ferr(po, "invalid reg: %d\n", popr->reg);
688 return regs_r32[popr->reg];
689}
c36e914d 690
850c9265 691static struct parsed_equ *equ_find(struct parsed_op *po, const char *name)
91977a1c 692{
850c9265 693 int i;
694
695 for (i = 0; i < g_eqcnt; i++)
696 if (IS(g_eqs[i].name, name))
697 break;
87bf6cec 698 if (i >= g_eqcnt) {
699 if (po != NULL)
700 ferr(po, "unresolved equ name: '%s'\n", name);
701 return NULL;
702 }
850c9265 703
704 return &g_eqs[i];
705}
706
707static void bg_frame_access(struct parsed_op *po, enum opr_lenmod lmod,
708 char *buf, size_t buf_size, const char *bp_arg,
709 int is_src, int is_lea)
710{
711 const char *prefix = "";
91977a1c 712 struct parsed_equ *eq;
713 int i, arg_i, arg_s;
850c9265 714 int sf_ofs;
91977a1c 715
716 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
717
850c9265 718 eq = equ_find(po, bp_arg);
91977a1c 719
720 if (eq->offset >= 0) {
721 arg_i = eq->offset / 4 - 2;
722 if (arg_i < 0 || arg_i >= g_func_pp.argc_stack)
723 ferr(po, "offset %d doesn't map to any arg\n", eq->offset);
724
725 for (i = arg_s = 0; i < g_func_pp.argc; i++) {
726 if (g_func_pp.arg[i].reg != NULL)
727 continue;
728 if (arg_s == arg_i)
729 break;
730 arg_s++;
731 }
732 if (i == g_func_pp.argc)
733 ferr(po, "arg %d not in prototype?\n", arg_i);
850c9265 734 if (is_lea)
735 ferr(po, "lea to arg?\n");
736
91977a1c 737 snprintf(buf, buf_size, "%sa%d", is_src ? "(u32)" : "", i + 1);
738 }
739 else {
740 if (g_bp_stack == 0)
741 ferr(po, "bp_stack access after it was not detected\n");
850c9265 742
743 sf_ofs = g_bp_stack + eq->offset;
744 if (sf_ofs < 0)
745 ferr(po, "bp_stack offset %d/%d\n", eq->offset, g_bp_stack);
746
747 if (is_lea)
748 prefix = "&";
749
750 switch (lmod)
751 {
752 case OPLM_BYTE:
753 snprintf(buf, buf_size, "%ssf.b[%d]", prefix, sf_ofs);
754 break;
755 case OPLM_WORD:
756 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
757 break;
758 case OPLM_DWORD:
759 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
760 break;
761 default:
762 ferr(po, "bp_stack bad lmod: %d\n", lmod);
763 }
91977a1c 764 }
765}
c36e914d 766
91977a1c 767static char *out_src_opr(char *buf, size_t buf_size,
850c9265 768 struct parsed_op *po, struct parsed_opr *popr, int is_lea)
91977a1c 769{
850c9265 770 const char *cast = "";
771 char tmp1[256], tmp2[256];
772 char expr[256];
773 int ret;
774
91977a1c 775 switch (popr->type) {
776 case OPT_REG:
850c9265 777 if (is_lea)
778 ferr(po, "lea from reg?\n");
779
91977a1c 780 switch (popr->lmod) {
781 case OPLM_DWORD:
782 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
783 break;
850c9265 784 case OPLM_WORD:
785 snprintf(buf, buf_size, "(u16)%s", opr_reg_p(po, popr));
786 break;
787 case OPLM_BYTE:
5101a5f9 788 if (popr->name[1] == 'h') // XXX..
789 snprintf(buf, buf_size, "(u8)(%s >> 8)", opr_reg_p(po, popr));
790 else
791 snprintf(buf, buf_size, "(u8)%s", opr_reg_p(po, popr));
850c9265 792 break;
91977a1c 793 default:
794 ferr(po, "invalid src lmod: %d\n", popr->lmod);
795 }
796 break;
850c9265 797
91977a1c 798 case OPT_REGMEM:
799 if (g_bp_frame && !strncmp(popr->name, "ebp+", 4)) {
850c9265 800 bg_frame_access(po, popr->lmod, buf, buf_size,
801 popr->name + 4, 1, is_lea);
91977a1c 802 break;
803 }
850c9265 804
805 strcpy(expr, popr->name);
806 if (strchr(expr, '[')) {
807 // special case: '[' can only be left for label[reg] form
808 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
809 if (ret != 2)
810 ferr(po, "parse failure for '%s'\n", expr);
811 snprintf(expr, sizeof(expr), "(u32)%s + %s", tmp1, tmp2);
812 }
813
814 // XXX: do we need more parsing?
815 if (is_lea) {
816 snprintf(buf, buf_size, "%s", expr);
817 break;
818 }
819
820 switch (popr->lmod) {
821 case OPLM_DWORD:
822 cast = "*(u32 *)";
823 break;
824 case OPLM_WORD:
825 cast = "*(u16 *)";
826 break;
827 case OPLM_BYTE:
828 cast = "*(u8 *)";
829 break;
830 default:
831 ferr(po, "invalid lmod: %d\n", popr->lmod);
832 }
833 snprintf(buf, buf_size, "%s(%s)", cast, expr);
91977a1c 834 break;
850c9265 835
91977a1c 836 case OPT_LABEL:
850c9265 837 if (is_lea)
838 snprintf(buf, buf_size, "(u32)&%s", popr->name);
839 else
87bf6cec 840 snprintf(buf, buf_size, "(u32)%s", popr->name);
850c9265 841 break;
842
843 case OPT_OFFSET:
844 if (is_lea)
845 ferr(po, "lea an offset?\n");
846 snprintf(buf, buf_size, "(u32)&%s", popr->name);
91977a1c 847 break;
850c9265 848
91977a1c 849 case OPT_CONST:
850c9265 850 if (is_lea)
851 ferr(po, "lea from const?\n");
852
5101a5f9 853 printf_number(buf, buf_size, popr->val);
91977a1c 854 break;
850c9265 855
91977a1c 856 default:
857 ferr(po, "invalid src type: %d\n", popr->type);
858 }
859
860 return buf;
861}
c36e914d 862
91977a1c 863static char *out_dst_opr(char *buf, size_t buf_size,
864 struct parsed_op *po, struct parsed_opr *popr)
865{
866 switch (popr->type) {
867 case OPT_REG:
868 switch (popr->lmod) {
869 case OPLM_DWORD:
870 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
871 break;
850c9265 872 case OPLM_WORD:
873 // ugh..
874 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
875 break;
876 case OPLM_BYTE:
877 // ugh..
5101a5f9 878 if (popr->name[1] == 'h') // XXX..
879 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
880 else
881 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
850c9265 882 break;
91977a1c 883 default:
884 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
885 }
886 break;
850c9265 887
888 case OPT_REGMEM:
889 if (g_bp_frame && !strncmp(popr->name, "ebp+", 4)) {
890 bg_frame_access(po, popr->lmod, buf, buf_size,
891 popr->name + 4, 0, 0);
892 break;
893 }
894
895 return out_src_opr(buf, buf_size, po, popr, 0);
896
91977a1c 897 default:
898 ferr(po, "invalid dst type: %d\n", popr->type);
899 }
900
901 return buf;
902}
c36e914d 903
69a3cdfc 904static const char *lmod_cast_u(struct parsed_op *po,
905 enum opr_lenmod lmod)
906{
907 switch (lmod) {
908 case OPLM_DWORD:
909 return "";
910 case OPLM_WORD:
911 return "(u16)";
912 case OPLM_BYTE:
913 return "(u8)";
914 default:
915 ferr(po, "invalid lmod: %d\n", lmod);
916 return "(_invalid_)";
917 }
918}
919
920static const char *lmod_cast_s(struct parsed_op *po,
921 enum opr_lenmod lmod)
922{
923 switch (lmod) {
924 case OPLM_DWORD:
925 return "(s32)";
926 case OPLM_WORD:
927 return "(s16)";
928 case OPLM_BYTE:
929 return "(s8)";
930 default:
931 ferr(po, "invalid lmod: %d\n", lmod);
932 return "(_invalid_)";
933 }
934}
935
5101a5f9 936static const char *lmod_cast(struct parsed_op *po,
937 enum opr_lenmod lmod, int is_signed)
938{
939 return is_signed ?
940 lmod_cast_s(po, lmod) :
941 lmod_cast_u(po, lmod);
942}
943
69a3cdfc 944static enum parsed_flag_op split_cond(struct parsed_op *po,
940e8e66 945 enum op_op op, int *is_inv)
91977a1c 946{
940e8e66 947 *is_inv = 0;
91977a1c 948
69a3cdfc 949 switch (op) {
950 case OP_JO:
951 return PFO_O;
952 case OP_JC:
953 return PFO_C;
954 case OP_JZ:
955 return PFO_Z;
956 case OP_JBE:
957 return PFO_BE;
958 case OP_JS:
959 return PFO_S;
960 case OP_JP:
961 return PFO_P;
962 case OP_JL:
963 return PFO_L;
964 case OP_JLE:
965 return PFO_LE;
966
91977a1c 967 case OP_JNO:
940e8e66 968 *is_inv = 1;
69a3cdfc 969 return PFO_O;
91977a1c 970 case OP_JNC:
940e8e66 971 *is_inv = 1;
69a3cdfc 972 return PFO_C;
91977a1c 973 case OP_JNZ:
940e8e66 974 *is_inv = 1;
69a3cdfc 975 return PFO_Z;
976 case OP_JA:
940e8e66 977 *is_inv = 1;
69a3cdfc 978 return PFO_BE;
91977a1c 979 case OP_JNS:
940e8e66 980 *is_inv = 1;
69a3cdfc 981 return PFO_S;
91977a1c 982 case OP_JNP:
940e8e66 983 *is_inv = 1;
69a3cdfc 984 return PFO_P;
850c9265 985 case OP_JGE:
940e8e66 986 *is_inv = 1;
69a3cdfc 987 return PFO_L;
91977a1c 988 case OP_JG:
940e8e66 989 *is_inv = 1;
69a3cdfc 990 return PFO_LE;
991
992 case OP_ADC:
993 case OP_SBB:
994 return PFO_C;
995
91977a1c 996 default:
69a3cdfc 997 ferr(po, "split_cond: bad op %d\n", op);
998 return -1;
91977a1c 999 }
1000}
c36e914d 1001
91977a1c 1002static void out_test_for_cc(char *buf, size_t buf_size,
940e8e66 1003 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
69a3cdfc 1004 enum opr_lenmod lmod, const char *expr)
91977a1c 1005{
69a3cdfc 1006 const char *cast, *scast;
91977a1c 1007
69a3cdfc 1008 cast = lmod_cast_u(po, lmod);
1009 scast = lmod_cast_s(po, lmod);
1010
1011 switch (pfo) {
1012 case PFO_Z:
87bf6cec 1013 case PFO_BE: // CF=1||ZF=1; CF=0
850c9265 1014 snprintf(buf, buf_size, "(%s%s %s 0)",
940e8e66 1015 cast, expr, is_inv ? "!=" : "==");
91977a1c 1016 break;
850c9265 1017
5101a5f9 1018 case PFO_S:
1019 case PFO_L: // SF!=OF; OF=0
1020 snprintf(buf, buf_size, "(%s%s %s 0)",
940e8e66 1021 scast, expr, is_inv ? ">=" : "<");
5101a5f9 1022 break;
1023
87bf6cec 1024 case PFO_LE: // ZF=1||SF!=OF; OF=0
69a3cdfc 1025 snprintf(buf, buf_size, "(%s%s %s 0)",
940e8e66 1026 scast, expr, is_inv ? ">" : "<=");
850c9265 1027 break;
1028
91977a1c 1029 default:
69a3cdfc 1030 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
91977a1c 1031 }
1032}
c36e914d 1033
850c9265 1034static void out_cmp_for_cc(char *buf, size_t buf_size,
940e8e66 1035 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
69a3cdfc 1036 enum opr_lenmod lmod, const char *expr1, const char *expr2)
850c9265 1037{
69a3cdfc 1038 const char *cast, *scast;
850c9265 1039
69a3cdfc 1040 cast = lmod_cast_u(po, lmod);
1041 scast = lmod_cast_s(po, lmod);
850c9265 1042
69a3cdfc 1043 switch (pfo) {
5101a5f9 1044 case PFO_C:
1045 // note: must be unsigned compare
1046 snprintf(buf, buf_size, "(%s%s %s %s%s)",
940e8e66 1047 cast, expr1, is_inv ? ">=" : "<", cast, expr2);
5101a5f9 1048 break;
1049
69a3cdfc 1050 case PFO_Z:
850c9265 1051 snprintf(buf, buf_size, "(%s%s %s %s%s)",
940e8e66 1052 cast, expr1, is_inv ? "!=" : "==", cast, expr2);
850c9265 1053 break;
1054
5101a5f9 1055 case PFO_BE: // !a
850c9265 1056 // note: must be unsigned compare
1057 snprintf(buf, buf_size, "(%s%s %s %s%s)",
940e8e66 1058 cast, expr1, is_inv ? ">" : "<=", cast, expr2);
5101a5f9 1059 break;
1060
1061 // note: must be signed compare
1062 case PFO_S:
1063 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
940e8e66 1064 scast, expr1, expr2, is_inv ? ">=" : "<");
850c9265 1065 break;
1066
5101a5f9 1067 case PFO_L: // !ge
850c9265 1068 snprintf(buf, buf_size, "(%s%s %s %s%s)",
940e8e66 1069 scast, expr1, is_inv ? ">=" : "<", scast, expr2);
850c9265 1070 break;
1071
5101a5f9 1072 case PFO_LE:
1073 snprintf(buf, buf_size, "(%s%s %s %s%s)",
940e8e66 1074 scast, expr1, is_inv ? ">" : "<=", scast, expr2);
5101a5f9 1075 break;
1076
850c9265 1077 default:
69a3cdfc 1078 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
850c9265 1079 }
1080}
1081
69a3cdfc 1082static void out_cmp_test(char *buf, size_t buf_size,
940e8e66 1083 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
69a3cdfc 1084{
1085 char buf1[256], buf2[256], buf3[256];
1086
1087 if (po->op == OP_TEST) {
1088 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1089 out_src_opr(buf3, sizeof(buf3), po, &po->operand[0], 0);
1090 }
1091 else {
1092 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], 0);
1093 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0);
1094 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1095 }
940e8e66 1096 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
69a3cdfc 1097 po->operand[0].lmod, buf3);
1098 }
1099 else if (po->op == OP_CMP) {
1100 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], 0);
1101 out_src_opr(buf3, sizeof(buf3), po, &po->operand[1], 0);
940e8e66 1102 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv,
69a3cdfc 1103 po->operand[0].lmod, buf2, buf3);
1104 }
1105 else
1106 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1107}
1108
850c9265 1109static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
91977a1c 1110 struct parsed_opr *popr2)
1111{
1112 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1113 ferr(po, "missing lmod for both operands\n");
1114
1115 if (popr1->lmod == OPLM_UNSPEC)
1116 popr1->lmod = popr2->lmod;
1117 else if (popr2->lmod == OPLM_UNSPEC)
1118 popr2->lmod = popr1->lmod;
1119 else if (popr1->lmod != popr2->lmod)
1120 ferr(po, "conflicting lmods: %d vs %d\n", popr1->lmod, popr2->lmod);
1121}
c36e914d 1122
850c9265 1123static const char *op_to_c(struct parsed_op *po)
1124{
1125 switch (po->op)
1126 {
1127 case OP_ADD:
5101a5f9 1128 case OP_ADC:
850c9265 1129 return "+";
1130 case OP_SUB:
5101a5f9 1131 case OP_SBB:
850c9265 1132 return "-";
1133 case OP_AND:
1134 return "&";
1135 case OP_OR:
1136 return "|";
1137 case OP_XOR:
1138 return "^";
1139 case OP_SHL:
1140 return "<<";
1141 case OP_SHR:
1142 return ">>";
1143 case OP_MUL:
1144 case OP_IMUL:
1145 return "*";
1146 default:
1147 ferr(po, "op_to_c was supplied with %d\n", po->op);
1148 }
1149}
1150
87bf6cec 1151static int scan_for_pop(int i, int opcnt, const char *reg,
1152 int magic, int do_patch)
850c9265 1153{
87bf6cec 1154 struct parsed_op *po;
1155 int ret = 0;
1156
850c9265 1157 for (; i < opcnt; i++) {
87bf6cec 1158 po = &ops[i];
1159 if (po->cc_scratch == magic)
1160 break; // already checked
1161 po->cc_scratch = magic;
1162
1163 if (po->flags & OPF_TAIL)
1164 return -1; // deadend
1165
1166 if (po->flags & OPF_RMD)
850c9265 1167 continue;
1168
87bf6cec 1169 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
1170 if (po->bt_i < 0) {
1171 ferr(po, "dead branch\n");
1172 return -1;
1173 }
850c9265 1174
87bf6cec 1175 if (po->flags & OPF_CC) {
1176 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic, do_patch);
1177 if (ret < 0)
1178 return ret; // dead end
1179 }
1180 else {
1181 i = po->bt_i - 1;
1182 }
1183 continue;
1184 }
1185
1186 if (po->op == OP_POP && po->operand[0].type == OPT_REG
1187 && IS(po->operand[0].name, reg))
1188 {
1189 if (do_patch)
1190 po->flags |= OPF_RMD;
1191 return 1;
1192 }
850c9265 1193 }
1194
87bf6cec 1195 return ret;
850c9265 1196}
1197
69a3cdfc 1198// scan for pop starting from 'ret' op (all paths)
850c9265 1199static int scan_for_pop_ret(int i, int opcnt, const char *reg, int do_patch)
1200{
1201 int found = 0;
1202 int j;
1203
1204 for (; i < opcnt; i++) {
87bf6cec 1205 if (!(ops[i].flags & OPF_TAIL))
850c9265 1206 continue;
1207
1208 for (j = i - 1; j >= 0; j--) {
69a3cdfc 1209 if (ops[j].flags & OPF_RMD)
1210 continue;
1211 if (ops[j].flags & OPF_JMP)
850c9265 1212 return -1;
1213
1214 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
1215 && IS(ops[j].operand[0].name, reg))
1216 {
1217 found = 1;
1218 if (do_patch)
69a3cdfc 1219 ops[j].flags |= OPF_RMD;
850c9265 1220 break;
1221 }
1222
1223 if (g_labels[j][0] != 0)
1224 return -1;
1225 }
1226 }
1227
1228 return found ? 0 : -1;
1229}
1230
5101a5f9 1231// is operand 'opr modified' by parsed_op 'po'?
1232static int is_opr_modified(const struct parsed_opr *opr,
69a3cdfc 1233 const struct parsed_op *po)
1234{
1235 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
1236 return 0;
1237
1238 if (opr->type == OPT_REG && po->operand[0].type == OPT_REG) {
1239 if (po->regmask_dst & (1 << opr->reg))
1240 return 1;
1241 else
1242 return 0;
1243 }
1244
1245 return IS(po->operand[0].name, opr->name);
1246}
1247
5101a5f9 1248// is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
1249static int is_any_opr_modified(const struct parsed_op *po_test,
1250 const struct parsed_op *po)
1251{
1252 int i;
1253
1254 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
1255 return 0;
1256
1257 if (po_test->regmask_src & po->regmask_dst)
1258 return 1;
1259
1260 for (i = 0; i < po_test->operand_cnt; i++)
1261 if (IS(po_test->operand[i].name, po->operand[0].name))
1262 return 1;
1263
1264 return 0;
1265}
1266
940e8e66 1267// scan for any po_test operand modification in range given
5101a5f9 1268static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt)
69a3cdfc 1269{
1270 for (; i < opcnt; i++) {
5101a5f9 1271 if (is_any_opr_modified(po_test, &ops[i]))
69a3cdfc 1272 return i;
1273 }
1274
1275 return -1;
1276}
1277
940e8e66 1278// scan for po_test operand[0] modification in range given
1279static int scan_for_mod_opr0(struct parsed_op *po_test,
1280 int i, int opcnt)
1281{
1282 for (; i < opcnt; i++) {
1283 if (is_opr_modified(&po_test->operand[0], &ops[i]))
1284 return i;
1285 }
1286
1287 return -1;
1288}
1289
5101a5f9 1290static int scan_for_flag_set(int i)
69a3cdfc 1291{
1292 for (; i >= 0; i--) {
1293 if (ops[i].flags & OPF_FLAGS)
1294 return i;
1295
1296 if ((ops[i].flags & OPF_JMP) && !(ops[i].flags & OPF_CC))
1297 return -1;
1298 if (g_labels[i][0] != 0)
1299 return -1;
1300 }
1301
1302 return -1;
1303}
1304
5101a5f9 1305// scan back for cdq, if anything modifies edx, fail
1306static int scan_for_cdq_edx(int i)
1307{
1308 for (; i >= 0; i--) {
1309 if (ops[i].op == OP_CDQ)
1310 return i;
1311
1312 if (ops[i].regmask_dst & (1 << xDX))
1313 return -1;
1314 if (g_labels[i][0] != 0)
1315 return -1;
1316 }
1317
1318 return -1;
1319}
1320
91977a1c 1321static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
1322{
69a3cdfc 1323 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
850c9265 1324 struct parsed_opr *last_arith_dst = NULL;
91977a1c 1325 char buf1[256], buf2[256], buf3[256];
940e8e66 1326 struct parsed_proto *pp, *pp_tmp;
91977a1c 1327 const char *tmpname;
940e8e66 1328 enum parsed_flag_op pfo;
69a3cdfc 1329 int save_arg_vars = 0;
1330 int cmp_result_vars = 0;
87bf6cec 1331 int need_mul_var = 0;
91977a1c 1332 int had_decl = 0;
1333 int regmask_arg = 0;
1334 int regmask = 0;
940e8e66 1335 int pfomask = 0;
91977a1c 1336 int no_output;
69a3cdfc 1337 int dummy;
91977a1c 1338 int arg;
1339 int i, j;
1340 int reg;
1341 int ret;
1342
1343 g_bp_frame = g_bp_stack = 0;
1344
1345 ret = proto_parse(fhdr, funcn, &g_func_pp);
1346 if (ret)
1347 ferr(ops, "proto_parse failed for '%s'\n", funcn);
1348
1349 fprintf(fout, "%s %s(", g_func_pp.ret_type, funcn);
1350 for (i = 0; i < g_func_pp.argc; i++) {
1351 if (i > 0)
1352 fprintf(fout, ", ");
1353 fprintf(fout, "%s a%d", g_func_pp.arg[i].type, i + 1);
1354 }
1355 fprintf(fout, ")\n{\n");
1356
1357 // pass1:
1358 // - handle ebp frame, remove ops related to it
1359 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
1360 && ops[1].op == OP_MOV
1361 && IS(opr_name(&ops[1], 0), "ebp")
1362 && IS(opr_name(&ops[1], 1), "esp"))
1363 {
87bf6cec 1364 int ecx_push = 0;
1365
91977a1c 1366 g_bp_frame = 1;
69a3cdfc 1367 ops[0].flags |= OPF_RMD;
1368 ops[1].flags |= OPF_RMD;
91977a1c 1369
1370 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
1371 g_bp_stack = opr_const(&ops[2], 1);
69a3cdfc 1372 ops[2].flags |= OPF_RMD;
91977a1c 1373 }
87bf6cec 1374 else {
1375 // another way msvc builds stack frame..
1376 i = 2;
1377 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
1378 g_bp_stack += 4;
1379 ops[i].flags |= OPF_RMD;
1380 ecx_push++;
1381 i++;
1382 }
1383 }
91977a1c 1384
1385 i = 2;
1386 do {
1387 for (; i < opcnt; i++)
1388 if (ops[i].op == OP_RET)
1389 break;
1390 if (ops[i - 1].op != OP_POP || !IS(opr_name(&ops[i - 1], 0), "ebp"))
1391 ferr(&ops[i - 1], "'pop ebp' expected\n");
69a3cdfc 1392 ops[i - 1].flags |= OPF_RMD;
91977a1c 1393
1394 if (g_bp_stack != 0) {
1395 if (ops[i - 2].op != OP_MOV
1396 || !IS(opr_name(&ops[i - 2], 0), "esp")
1397 || !IS(opr_name(&ops[i - 2], 1), "ebp"))
1398 {
1399 ferr(&ops[i - 2], "esp restore expected\n");
1400 }
69a3cdfc 1401 ops[i - 2].flags |= OPF_RMD;
87bf6cec 1402
1403 if (ecx_push && ops[i - 3].op == OP_POP
1404 && IS(opr_name(&ops[i - 3], 0), "ecx"))
1405 {
1406 ferr(&ops[i - 3], "unexpected ecx pop\n");
1407 }
91977a1c 1408 }
87bf6cec 1409
91977a1c 1410 i++;
1411 } while (i < opcnt);
1412 }
1413
1414 // pass2:
87bf6cec 1415 // - resolve all branches
1416 for (i = 0; i < opcnt; i++) {
1417 po = &ops[i];
1418 po->bt_i = -1;
1419
1420 if ((po->flags & OPF_RMD) || !(po->flags & OPF_JMP)
1421 || po->op == OP_CALL || po->op == OP_RET)
1422 continue;
1423
1424 for (j = 0; j < opcnt; j++) {
1425 if (g_labels[j][0] && IS(po->operand[0].name, g_labels[j])) {
1426 po->bt_i = j;
940e8e66 1427 po->lrl = g_label_refs[j];
1428 g_label_refs[j] = po;
87bf6cec 1429 break;
1430 }
1431 }
1432
1433 if (po->bt_i == -1) {
1434 // assume tail call
1435 po->op = OP_CALL;
1436 po->flags |= OPF_TAIL;
1437 }
1438 }
1439
1440 // pass3:
69a3cdfc 1441 // - find POPs for PUSHes, rm both
91977a1c 1442 // - scan for all used registers
69a3cdfc 1443 // - find flag set ops for their users
91977a1c 1444 // - process calls
1445 for (i = 0; i < opcnt; i++) {
69a3cdfc 1446 po = &ops[i];
1447 if (po->flags & OPF_RMD)
91977a1c 1448 continue;
850c9265 1449
69a3cdfc 1450 if (po->op == OP_PUSH && po->operand[0].type == OPT_REG) {
1451 if (po->operand[0].reg < 0)
1452 ferr(po, "reg not set for push?\n");
1453 if (!(regmask & (1 << po->operand[0].reg))) { // reg save
87bf6cec 1454 ret = scan_for_pop(i + 1, opcnt, po->operand[0].name, i + 1, 0);
1455 if (ret == 1) {
69a3cdfc 1456 po->flags |= OPF_RMD;
87bf6cec 1457 scan_for_pop(i + 1, opcnt, po->operand[0].name, i + 2, 1);
850c9265 1458 continue;
1459 }
69a3cdfc 1460 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
850c9265 1461 if (ret == 0) {
69a3cdfc 1462 po->flags |= OPF_RMD;
1463 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 1);
850c9265 1464 continue;
1465 }
1466 }
1467 }
1468
69a3cdfc 1469 regmask |= po->regmask_src | po->regmask_dst;
91977a1c 1470
69a3cdfc 1471 if (po->flags & OPF_CC)
1472 {
5101a5f9 1473 ret = scan_for_flag_set(i - 1);
69a3cdfc 1474 if (ret < 0)
1475 ferr(po, "unable to trace flag setter\n");
1476
1477 tmp_op = &ops[ret]; // flag setter
940e8e66 1478 pfo = split_cond(po, po->op, &dummy);
1479 pfomask = 0;
1480
1481 // to get nicer code, we try to delay test and cmp;
1482 // if we can't because of operand modification, or if we
1483 // have math op, make it calculate flags explicitly
1484 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP) {
1485 if (scan_for_mod(tmp_op, ret + 1, i) >= 0)
1486 pfomask = 1 << pfo;
1487 }
1488 else {
1489 if ((pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P)
1490 || scan_for_mod_opr0(tmp_op, ret + 1, i) >= 0)
1491 pfomask = 1 << pfo;
1492 }
1493 if (pfomask) {
1494 tmp_op->pfomask |= pfomask;
1495 cmp_result_vars |= pfomask;
5101a5f9 1496 po->datap = tmp_op;
69a3cdfc 1497 }
5101a5f9 1498
1499 if (po->op == OP_ADC || po->op == OP_SBB)
1500 cmp_result_vars |= 1 << PFO_C;
69a3cdfc 1501 }
1502 else if (po->op == OP_CALL)
1503 {
91977a1c 1504 pp = malloc(sizeof(*pp));
1505 my_assert_not(pp, NULL);
1506 tmpname = opr_name(&ops[i], 0);
1507 ret = proto_parse(fhdr, tmpname, pp);
1508 if (ret)
69a3cdfc 1509 ferr(po, "proto_parse failed for '%s'\n", tmpname);
91977a1c 1510
1511 for (arg = 0; arg < pp->argc; arg++)
1512 if (pp->arg[arg].reg == NULL)
1513 break;
1514
1515 for (j = i - 1; j >= 0 && arg < pp->argc; j--) {
940e8e66 1516 if (ops[j].op == OP_CALL) {
1517 pp_tmp = ops[j].datap;
1518 if (pp_tmp == NULL)
1519 ferr(po, "arg collect hit unparsed call\n");
1520 if (pp_tmp->argc_stack > 0)
1521 ferr(po, "arg collect hit '%s' with %d stack args\n",
1522 opr_name(&ops[j], 0), pp_tmp->argc_stack);
1523 }
1524 else if ((ops[j].flags & OPF_TAIL)
1525 || (ops[j].flags & (OPF_JMP|OPF_CC)) == OPF_JMP)
1526 {
1527 break;
69a3cdfc 1528 }
69a3cdfc 1529
940e8e66 1530 if (ops[j].op == OP_PUSH) {
1531 pp->arg[arg].datap = &ops[j];
1532 ret = scan_for_mod(&ops[j], j + 1, i);
1533 if (ret >= 0) {
1534 // mark this push as one that needs operand saving
1535 ops[j].argmask |= 1 << arg;
1536 save_arg_vars |= 1 << arg;
1537 }
1538 else
1539 ops[j].flags |= OPF_RMD;
1540
1541 // next arg
1542 for (arg++; arg < pp->argc; arg++)
1543 if (pp->arg[arg].reg == NULL)
1544 break;
1545 }
1546
1547 if (g_labels[j][0] != 0) {
1548 if (j > 0 && ((ops[j - 1].flags & OPF_TAIL)
1549 || (ops[j - 1].flags & (OPF_JMP|OPF_CC)) == OPF_JMP))
1550 {
1551 // follow the branch in reverse
1552 if (g_label_refs[j] == NULL)
1553 ferr(po, "no refs for '%s'?\n", g_labels[j]);
1554 if (g_label_refs[j]->lrl != NULL)
1555 ferr(po, "unhandled multiple fefs to '%s'\n", g_labels[j]);
1556 j = (g_label_refs[j] - ops) + 1;
1557 continue;
1558 }
1559 break;
1560 }
91977a1c 1561 }
1562 if (arg < pp->argc)
69a3cdfc 1563 ferr(po, "arg collect failed for '%s'\n", tmpname);
1564 po->datap = pp;
91977a1c 1565 }
87bf6cec 1566 else if (po->op == OP_MUL
1567 || (po->op == OP_IMUL && po->operand_cnt == 1))
1568 {
1569 need_mul_var = 1;
1570 }
91977a1c 1571 }
1572
850c9265 1573 // declare stack frame
1574 if (g_bp_stack)
1575 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
1576 (g_bp_stack + 3) / 4, (g_bp_stack + 1) / 2, g_bp_stack);
1577
940e8e66 1578 // declare arg-registers
91977a1c 1579 for (i = 0; i < g_func_pp.argc; i++) {
1580 if (g_func_pp.arg[i].reg != NULL) {
1581 reg = char_array_i(regs_r32,
1582 ARRAY_SIZE(regs_r32), g_func_pp.arg[i].reg);
1583 if (reg < 0)
1584 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp.arg[i].reg);
1585
1586 regmask_arg |= 1 << reg;
1587 fprintf(fout, " u32 %s = (u32)a%d;\n",
850c9265 1588 g_func_pp.arg[i].reg, i + 1);
91977a1c 1589 had_decl = 1;
1590 }
1591 }
1592
940e8e66 1593 // declare other regs - special case for eax
91977a1c 1594 if (!((regmask | regmask_arg) & 1) && !IS(g_func_pp.ret_type, "void")) {
1595 fprintf(fout, " u32 eax = 0;\n");
1596 had_decl = 1;
1597 }
1598
1599 regmask &= ~regmask_arg;
1600 if (g_bp_frame)
1601 regmask &= ~(1 << xBP);
1602 if (regmask) {
1603 for (reg = 0; reg < 8; reg++) {
1604 if (regmask & (1 << reg)) {
1605 fprintf(fout, " u32 %s;\n", regs_r32[reg]);
1606 had_decl = 1;
1607 }
1608 }
1609 }
1610
69a3cdfc 1611 if (save_arg_vars) {
1612 for (reg = 0; reg < 32; reg++) {
1613 if (save_arg_vars & (1 << reg)) {
1614 fprintf(fout, " u32 s_a%d;\n", reg + 1);
1615 had_decl = 1;
1616 }
1617 }
1618 }
1619
1620 if (cmp_result_vars) {
1621 for (i = 0; i < 8; i++) {
1622 if (cmp_result_vars & (1 << i)) {
1623 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
1624 had_decl = 1;
1625 }
1626 }
1627 }
1628
87bf6cec 1629 if (need_mul_var) {
1630 fprintf(fout, " u64 mul_tmp;\n");
1631 had_decl = 1;
1632 }
1633
91977a1c 1634 if (had_decl)
1635 fprintf(fout, "\n");
1636
1637 // output ops
69a3cdfc 1638 for (i = 0; i < opcnt; i++)
1639 {
91977a1c 1640 if (g_labels[i][0] != 0)
1641 fprintf(fout, "\n%s:\n", g_labels[i]);
1642
69a3cdfc 1643 po = &ops[i];
1644 if (po->flags & OPF_RMD)
91977a1c 1645 continue;
1646
1647 no_output = 0;
1648
91977a1c 1649 #define assert_operand_cnt(n_) \
850c9265 1650 if (po->operand_cnt != n_) \
1651 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
1652
69a3cdfc 1653 // conditional/flag using op?
1654 if (po->flags & OPF_CC)
850c9265 1655 {
940e8e66 1656 int is_delayed = 0;
1657 int is_inv = 0;
69a3cdfc 1658
940e8e66 1659 pfo = split_cond(po, po->op, &is_inv);
850c9265 1660
69a3cdfc 1661 // we go through all this trouble to avoid using parsed_flag_op,
1662 // which makes generated code much nicer
1663 if (delayed_flag_op != NULL)
850c9265 1664 {
940e8e66 1665 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op, pfo, is_inv);
1666 is_delayed = 1;
91977a1c 1667 }
850c9265 1668 else if (last_arith_dst != NULL
69a3cdfc 1669 && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P))
850c9265 1670 {
1671 out_src_opr(buf3, sizeof(buf3), po, last_arith_dst, 0);
940e8e66 1672 out_test_for_cc(buf1, sizeof(buf1), po, pfo, is_inv,
850c9265 1673 last_arith_dst->lmod, buf3);
940e8e66 1674 is_delayed = 1;
850c9265 1675 }
69a3cdfc 1676 else if (po->datap != NULL) {
1677 // use preprocessed results
1678 tmp_op = po->datap;
1679 if (!tmp_op || !(tmp_op->pfomask & (1 << pfo)))
1680 ferr(po, "not prepared for pfo %d\n", pfo);
1681
940e8e66 1682 // note: is_inv was not yet applied
69a3cdfc 1683 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
940e8e66 1684 is_inv ? "!" : "", parsed_flag_op_names[pfo]);
69a3cdfc 1685 }
1686 else {
1687 ferr(po, "all methods of finding comparison failed\n");
1688 }
850c9265 1689
69a3cdfc 1690 if (po->flags & OPF_JMP) {
850c9265 1691 fprintf(fout, " if %s\n", buf1);
1692 }
5101a5f9 1693 else if (po->op == OP_ADC || po->op == OP_SBB) {
940e8e66 1694 if (is_delayed)
1695 fprintf(fout, " cond_%s = %s;\n",
1696 parsed_flag_op_names[pfo], buf1);
850c9265 1697 }
5101a5f9 1698 else if (po->flags & OPF_DATA) { // SETcc
850c9265 1699 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
1700 fprintf(fout, " %s = %s;", buf2, buf1);
91977a1c 1701 }
69a3cdfc 1702 else {
1703 ferr(po, "unhandled conditional op\n");
1704 }
91977a1c 1705 }
1706
940e8e66 1707 pfomask = po->pfomask;
1708
850c9265 1709 switch (po->op)
91977a1c 1710 {
1711 case OP_MOV:
1712 assert_operand_cnt(2);
850c9265 1713 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1714 fprintf(fout, " %s = %s;",
1715 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1716 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1717 break;
1718
1719 case OP_LEA:
1720 assert_operand_cnt(2);
87bf6cec 1721 po->operand[1].lmod = OPLM_DWORD; // always
850c9265 1722 fprintf(fout, " %s = %s;",
1723 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1724 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 1));
1725 break;
1726
1727 case OP_MOVZX:
1728 assert_operand_cnt(2);
91977a1c 1729 fprintf(fout, " %s = %s;",
850c9265 1730 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1731 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1732 break;
1733
1734 case OP_MOVSX:
1735 assert_operand_cnt(2);
1736 switch (po->operand[1].lmod) {
1737 case OPLM_BYTE:
1738 strcpy(buf3, "(s8)");
1739 break;
1740 case OPLM_WORD:
1741 strcpy(buf3, "(s16)");
1742 break;
1743 default:
1744 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
1745 }
1746 fprintf(fout, " %s = %s%s;",
1747 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1748 buf3,
1749 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1750 break;
1751
1752 case OP_NOT:
1753 assert_operand_cnt(1);
1754 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1755 fprintf(fout, " %s = ~%s;", buf1, buf1);
1756 break;
1757
5101a5f9 1758 case OP_CDQ:
1759 assert_operand_cnt(2);
1760 fprintf(fout, " %s = (s32)%s >> 31;",
1761 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1762 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1763 strcpy(g_comment, "cdq");
1764 break;
1765
850c9265 1766 // arithmetic w/flags
1767 case OP_ADD:
1768 case OP_SUB:
1769 case OP_AND:
1770 case OP_OR:
5101a5f9 1771 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1772 // fallthrough
850c9265 1773 case OP_SHL:
1774 case OP_SHR:
1775 dualop_arith:
1776 assert_operand_cnt(2);
850c9265 1777 fprintf(fout, " %s %s= %s;",
1778 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1779 op_to_c(po),
1780 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1781 last_arith_dst = &po->operand[0];
69a3cdfc 1782 delayed_flag_op = NULL;
850c9265 1783 break;
1784
5101a5f9 1785 case OP_XOR:
850c9265 1786 assert_operand_cnt(2);
1787 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5101a5f9 1788 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1789 // special case for XOR
1790 fprintf(fout, " %s = 0;",
1791 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
1792 last_arith_dst = &po->operand[0];
1793 delayed_flag_op = NULL;
850c9265 1794 break;
850c9265 1795 }
5101a5f9 1796 goto dualop_arith;
1797
1798 case OP_SAR:
1799 assert_operand_cnt(2);
850c9265 1800 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5101a5f9 1801 fprintf(fout, " %s = %s%s >> %s;", buf1,
1802 lmod_cast_s(po, po->operand[0].lmod), buf1,
1803 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
850c9265 1804 last_arith_dst = &po->operand[0];
69a3cdfc 1805 delayed_flag_op = NULL;
850c9265 1806 break;
1807
5101a5f9 1808 case OP_ADC:
850c9265 1809 case OP_SBB:
5101a5f9 1810 assert_operand_cnt(2);
1811 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1812 fprintf(fout, " %s %s= %s + cond_c;",
1813 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1814 op_to_c(po),
1815 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1816 last_arith_dst = &po->operand[0];
1817 delayed_flag_op = NULL;
850c9265 1818 break;
1819
1820 case OP_INC:
1821 case OP_DEC:
1822 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1823 strcpy(buf2, po->op == OP_INC ? "++" : "--");
5101a5f9 1824 fprintf(fout, " %s%s;", buf1, buf2);
1825 last_arith_dst = &po->operand[0];
1826 delayed_flag_op = NULL;
1827 break;
1828
1829 case OP_NEG:
1830 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1831 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], 0);
1832 fprintf(fout, " %s = -%s%s;", buf1,
1833 lmod_cast_s(po, po->operand[0].lmod), buf2);
850c9265 1834 last_arith_dst = &po->operand[0];
69a3cdfc 1835 delayed_flag_op = NULL;
940e8e66 1836 if (pfomask & (1 << PFO_C)) {
1837 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
1838 pfomask &= ~(1 << PFO_C);
1839 }
850c9265 1840 break;
1841
1842 case OP_IMUL:
1843 if (po->operand_cnt == 2)
1844 goto dualop_arith;
87bf6cec 1845 if (po->operand_cnt == 3)
1846 ferr(po, "TODO imul3\n");
1847 // fallthrough
1848 case OP_MUL:
1849 assert_operand_cnt(1);
1850 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
1851 fprintf(fout, " mul_tmp = %seax * %s%s;\n", buf1, buf1,
1852 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], 0));
1853 fprintf(fout, " edx = mul_tmp >> 32;\n");
1854 fprintf(fout, " eax = mul_tmp;");
1855 last_arith_dst = NULL;
69a3cdfc 1856 delayed_flag_op = NULL;
91977a1c 1857 break;
1858
5101a5f9 1859 case OP_DIV:
1860 case OP_IDIV:
1861 assert_operand_cnt(1);
1862 if (po->operand[0].lmod != OPLM_DWORD)
1863 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
1864
1865 // 32bit division is common, look for it
1866 if (scan_for_cdq_edx(i - 1) >= 0) {
1867 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], 0);
1868 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
1869 po->op == OP_IDIV));
1870 fprintf(fout, " edx = %seax %% %s%s;\n", buf2, buf2, buf1);
1871 fprintf(fout, " eax = %seax / %s%s;", buf2, buf2, buf1);
1872 }
1873 else
1874 ferr(po, "TODO 64bit divident\n");
87bf6cec 1875 last_arith_dst = NULL;
1876 delayed_flag_op = NULL;
5101a5f9 1877 break;
1878
91977a1c 1879 case OP_TEST:
1880 case OP_CMP:
850c9265 1881 propagate_lmod(po, &po->operand[0], &po->operand[1]);
940e8e66 1882 if (pfomask != 0) {
69a3cdfc 1883 for (j = 0; j < 8; j++) {
940e8e66 1884 if (pfomask & (1 << j)) {
69a3cdfc 1885 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
1886 fprintf(fout, " cond_%s = %s;",
1887 parsed_flag_op_names[j], buf1);
1888 }
1889 }
940e8e66 1890 pfomask = 0;
69a3cdfc 1891 }
1892 else
1893 no_output = 1;
1894 delayed_flag_op = po;
91977a1c 1895 break;
1896
69a3cdfc 1897 // note: we reuse OP_Jcc for SETcc, only flags differ
91977a1c 1898 case OP_JO ... OP_JG:
5101a5f9 1899 if (po->flags & OPF_JMP)
850c9265 1900 fprintf(fout, " goto %s;", po->operand[0].name);
5101a5f9 1901 // else SETcc - should already be handled
850c9265 1902 break;
1903
1904 case OP_JMP:
87bf6cec 1905 assert_operand_cnt(1);
1906 if (po->operand[0].type != OPT_LABEL)
1907 ferr(po, "unhandled call type\n");
1908
850c9265 1909 fprintf(fout, " goto %s;", po->operand[0].name);
91977a1c 1910 break;
1911
1912 case OP_CALL:
5101a5f9 1913 assert_operand_cnt(1);
1914 if (po->operand[0].type != OPT_LABEL)
1915 ferr(po, "unhandled call type\n");
1916
850c9265 1917 pp = po->datap;
91977a1c 1918 if (pp == NULL)
850c9265 1919 ferr(po, "NULL pp\n");
91977a1c 1920
1921 fprintf(fout, " ");
1922 if (!IS(pp->ret_type, "void")) {
87bf6cec 1923 if (po->flags & OPF_TAIL)
1924 fprintf(fout, "return ");
1925 else
1926 fprintf(fout, "eax = ");
91977a1c 1927 if (strchr(pp->ret_type, '*'))
1928 fprintf(fout, "(u32)");
1929 }
87bf6cec 1930
850c9265 1931 fprintf(fout, "%s(", opr_name(po, 0));
91977a1c 1932 for (arg = 0; arg < pp->argc; arg++) {
1933 if (arg > 0)
1934 fprintf(fout, ", ");
87bf6cec 1935
1936 if (strchr(pp->arg[arg].type, '*'))
1937 fprintf(fout, "(%s)", pp->arg[arg].type);
1938
91977a1c 1939 if (pp->arg[arg].reg != NULL) {
850c9265 1940 fprintf(fout, "%s", pp->arg[arg].reg);
91977a1c 1941 continue;
1942 }
1943
1944 // stack arg
1945 tmp_op = pp->arg[arg].datap;
1946 if (tmp_op == NULL)
850c9265 1947 ferr(po, "parsed_op missing for arg%d\n", arg);
940e8e66 1948 if (tmp_op->argmask) {
1949 fprintf(fout, "s_a%d", arg + 1);
69a3cdfc 1950 }
1951 else {
1952 fprintf(fout, "%s",
1953 out_src_opr(buf1, sizeof(buf1),
1954 tmp_op, &tmp_op->operand[0], 0));
1955 }
91977a1c 1956 }
1957 fprintf(fout, ");");
87bf6cec 1958
1959 if (po->flags & OPF_TAIL) {
1960 strcpy(g_comment, "tailcall");
1961 if (IS(pp->ret_type, "void"))
1962 fprintf(fout, "\n return;");
1963 }
1964 delayed_flag_op = NULL;
1965 last_arith_dst = NULL;
91977a1c 1966 break;
1967
1968 case OP_RET:
1969 if (IS(g_func_pp.ret_type, "void"))
1970 fprintf(fout, " return;");
1971 else
1972 fprintf(fout, " return eax;");
1973 break;
1974
1975 case OP_PUSH:
940e8e66 1976 if (po->argmask) {
69a3cdfc 1977 // special case - saved func arg
940e8e66 1978 for (j = 0; j < 32; j++) {
1979 if (po->argmask & (1 << j)) {
1980 fprintf(fout, " s_a%d = %s;", j + 1,
1981 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], 0));
1982 }
1983 }
69a3cdfc 1984 break;
1985 }
850c9265 1986 ferr(po, "push encountered\n");
91977a1c 1987 break;
1988
1989 case OP_POP:
850c9265 1990 ferr(po, "pop encountered\n");
91977a1c 1991 break;
1992
1993 default:
1994 no_output = 1;
69a3cdfc 1995 ferr(po, "unhandled op type %d, flags %x\n",
1996 po->op, po->flags);
91977a1c 1997 break;
1998 }
1999
2000 if (g_comment[0] != 0) {
2001 fprintf(fout, " // %s", g_comment);
2002 g_comment[0] = 0;
2003 no_output = 0;
2004 }
2005 if (!no_output)
2006 fprintf(fout, "\n");
5101a5f9 2007
940e8e66 2008 if (pfomask != 0)
2009 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
2010
5101a5f9 2011 // see is delayed flag stuff is still valid
2012 if (delayed_flag_op != NULL && delayed_flag_op != po) {
2013 if (is_any_opr_modified(delayed_flag_op, po))
2014 delayed_flag_op = NULL;
2015 }
2016
2017 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
2018 if (is_opr_modified(last_arith_dst, po))
2019 last_arith_dst = NULL;
2020 }
91977a1c 2021 }
2022
2023 fprintf(fout, "}\n\n");
2024
2025 // cleanup
2026 for (i = 0; i < opcnt; i++) {
2027 if (ops[i].op == OP_CALL) {
2028 pp = ops[i].datap;
2029 if (pp) {
2030 proto_release(pp);
2031 free(pp);
2032 }
2033 }
2034 }
2035 proto_release(&g_func_pp);
2036}
c36e914d 2037
91977a1c 2038int main(int argc, char *argv[])
2039{
2040 FILE *fout, *fasm, *fhdr;
2041 char line[256];
2042 char words[16][256];
2043 int in_func = 0;
940e8e66 2044 int skip_warned = 0;
91977a1c 2045 int eq_alloc;
2046 int pi = 0;
2047 int len;
2048 char *p;
2049 int wordc;
2050
2051 if (argc != 4) {
2052 printf("usage:\n%s <.c> <.asm> <hdrf>\n",
2053 argv[0]);
2054 return 1;
2055 }
2056
2057 hdrfn = argv[3];
2058 fhdr = fopen(hdrfn, "r");
2059 my_assert_not(fhdr, NULL);
2060
2061 asmfn = argv[2];
2062 fasm = fopen(asmfn, "r");
2063 my_assert_not(fasm, NULL);
2064
2065 fout = fopen(argv[1], "w");
2066 my_assert_not(fout, NULL);
2067
2068 eq_alloc = 128;
2069 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
2070 my_assert_not(g_eqs, NULL);
2071
2072 while (fgets(line, sizeof(line), fasm))
2073 {
2074 asmln++;
2075
2076 p = sskip(line);
2077 if (*p == 0 || *p == ';')
2078 continue;
2079
2080 memset(words, 0, sizeof(words));
2081 for (wordc = 0; wordc < 16; wordc++) {
2082 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
2083 if (*p == 0 || *p == ';') {
2084 wordc++;
2085 break;
2086 }
2087 }
2088
2089 if (wordc == 0) {
2090 // shouldn't happen
2091 awarn("wordc == 0?\n");
2092 continue;
2093 }
2094
2095 // don't care about this:
2096 if (words[0][0] == '.'
2097 || IS(words[0], "include")
2098 || IS(words[0], "assume") || IS(words[1], "segment")
2099 || IS(words[0], "align"))
2100 {
2101 continue;
2102 }
2103
2104 if (IS(words[1], "proc")) {
2105 if (in_func)
2106 aerr("proc '%s' while in_func '%s'?\n",
2107 words[0], g_func);
2108 strcpy(g_func, words[0]);
2109 in_func = 1;
2110 continue;
2111 }
2112
2113 if (IS(words[1], "endp")) {
2114 if (!in_func)
2115 aerr("endp '%s' while not in_func?\n", words[0]);
2116 if (!IS(g_func, words[0]))
2117 aerr("endp '%s' while in_func '%s'?\n",
2118 words[0], g_func);
2119 gen_func(fout, fhdr, g_func, pi);
2120 in_func = 0;
940e8e66 2121 skip_warned = 0;
91977a1c 2122 g_func[0] = 0;
2123 if (pi != 0) {
2124 memset(&ops, 0, pi * sizeof(ops[0]));
2125 memset(g_labels, 0, pi * sizeof(g_labels[0]));
940e8e66 2126 memset(g_label_refs, 0, pi * sizeof(g_label_refs[0]));
91977a1c 2127 pi = 0;
2128 }
2129 g_eqcnt = 0;
91977a1c 2130 continue;
2131 }
2132
2133 if (IS(words[1], "=")) {
2134 if (wordc != 5)
2135 aerr("unhandled equ, wc=%d\n", wordc);
2136 if (g_eqcnt >= eq_alloc) {
2137 eq_alloc *= 2;
2138 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
2139 my_assert_not(g_eqs, NULL);
2140 }
2141
2142 len = strlen(words[0]);
2143 if (len > sizeof(g_eqs[0].name) - 1)
2144 aerr("equ name too long: %d\n", len);
2145 strcpy(g_eqs[g_eqcnt].name, words[0]);
2146
2147 if (!IS(words[3], "ptr"))
2148 aerr("unhandled equ\n");
2149 if (IS(words[2], "dword"))
2150 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
2151 else if (IS(words[2], "word"))
2152 g_eqs[g_eqcnt].lmod = OPLM_WORD;
2153 else if (IS(words[2], "byte"))
2154 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
2155 else
2156 aerr("bad lmod: '%s'\n", words[2]);
2157
2158 g_eqs[g_eqcnt].offset = parse_number(words[4]);
2159 g_eqcnt++;
2160 continue;
2161 }
2162
2163 if (pi >= ARRAY_SIZE(ops))
2164 aerr("too many ops\n");
2165
2166 p = strchr(words[0], ':');
2167 if (p != NULL) {
2168 len = p - words[0];
2169 if (len > sizeof(g_labels[0]) - 1)
2170 aerr("label too long: %d\n", len);
2171 if (g_labels[pi][0] != 0)
2172 aerr("dupe label?\n");
2173 memcpy(g_labels[pi], words[0], len);
2174 g_labels[pi][len] = 0;
2175 continue;
2176 }
2177
940e8e66 2178 if (!in_func) {
2179 if (!skip_warned && g_labels[pi][0] != 0) {
2180 anote("skipping from '%s'\n", g_labels[pi]);
2181 skip_warned = 1;
2182 }
2183 g_labels[pi][0] = 0;
2184 continue;
2185 }
2186
91977a1c 2187 parse_op(&ops[pi], words, wordc);
2188 pi++;
91977a1c 2189 }
2190
2191 fclose(fout);
2192 fclose(fasm);
2193 fclose(fhdr);
2194
2195 return 0;
c36e914d 2196}
91977a1c 2197
2198// vim:ts=2:shiftwidth=2:expandtab