some func ptr arg type checking
[ia32rtools.git] / tools / translate.c
CommitLineData
7637b6cc 1/*
2 * ia32rtools
3 * (C) notaz, 2013,2014
4 *
5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
7 */
8
33c35af6 9#define _GNU_SOURCE
c36e914d 10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#include "my_assert.h"
15#include "my_str.h"
16
17#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
18#define IS(w, y) !strcmp(w, y)
39b168b8 19#define IS_START(w, y) !strncmp(w, y, strlen(y))
c36e914d 20
21#include "protoparse.h"
22
06c5d854 23static const char *asmfn;
c36e914d 24static int asmln;
06c5d854 25static FILE *g_fhdr;
c36e914d 26
940e8e66 27#define anote(fmt, ...) \
28 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
c36e914d 29#define awarn(fmt, ...) \
940e8e66 30 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
c36e914d 31#define aerr(fmt, ...) do { \
940e8e66 32 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
33c35af6 33 fcloseall(); \
c36e914d 34 exit(1); \
35} while (0)
36
054f95b2 37#include "masm_tools.h"
38
69a3cdfc 39enum op_flags {
87bf6cec 40 OPF_RMD = (1 << 0), /* removed or optimized out */
41 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
42 OPF_FLAGS = (1 << 2), /* sets flags */
5c024ef7 43 OPF_JMP = (1 << 3), /* branch, call */
44 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */
45 OPF_CC = (1 << 5), /* uses flags */
46 OPF_TAIL = (1 << 6), /* ret or tail call */
47 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
48 OPF_REP = (1 << 8), /* prefixed by rep */
49 OPF_REPZ = (1 << 9), /* rep is repe/repz */
50 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
51 OPF_FARG = (1 << 11), /* push collected as func arg (no reuse) */
52 OPF_EBP_S = (1 << 12), /* ebp used as scratch here, not BP */
53 OPF_DF = (1 << 13), /* DF flag set */
2fe80fdb 54 OPF_ATAIL = (1 << 14), /* tail call with reused arg frame */
cb090db0 55 OPF_32BIT = (1 << 15), /* 32bit division */
037f4971 56 OPF_LOCK = (1 << 16), /* op has lock prefix */
23fd0b11 57 OPF_VAPUSH = (1 << 17), /* vararg ptr push (as call arg) */
c36e914d 58};
59
60enum op_op {
61 OP_INVAL,
33c35af6 62 OP_NOP,
c36e914d 63 OP_PUSH,
64 OP_POP,
591721d7 65 OP_LEAVE,
c36e914d 66 OP_MOV,
850c9265 67 OP_LEA,
68 OP_MOVZX,
69 OP_MOVSX,
108e9fe3 70 OP_XCHG,
850c9265 71 OP_NOT,
5101a5f9 72 OP_CDQ,
092f64e1 73 OP_LODS,
33c35af6 74 OP_STOS,
d4e3b5db 75 OP_MOVS,
7ba45c34 76 OP_CMPS,
591721d7 77 OP_SCAS,
78 OP_STD,
79 OP_CLD,
c36e914d 80 OP_RET,
81 OP_ADD,
91977a1c 82 OP_SUB,
850c9265 83 OP_AND,
84 OP_OR,
85 OP_XOR,
86 OP_SHL,
87 OP_SHR,
88 OP_SAR,
d4e3b5db 89 OP_ROL,
90 OP_ROR,
cb090db0 91 OP_RCL,
92 OP_RCR,
69a3cdfc 93 OP_ADC,
850c9265 94 OP_SBB,
1f84f6b3 95 OP_BSF,
850c9265 96 OP_INC,
97 OP_DEC,
5101a5f9 98 OP_NEG,
850c9265 99 OP_MUL,
100 OP_IMUL,
5101a5f9 101 OP_DIV,
102 OP_IDIV,
c36e914d 103 OP_TEST,
104 OP_CMP,
105 OP_CALL,
106 OP_JMP,
5c024ef7 107 OP_JECXZ,
092f64e1 108 OP_JCC,
109 OP_SCC,
c36e914d 110};
111
112enum opr_type {
87bf6cec 113 OPT_UNSPEC,
114 OPT_REG,
115 OPT_REGMEM,
116 OPT_LABEL,
850c9265 117 OPT_OFFSET,
87bf6cec 118 OPT_CONST,
c36e914d 119};
120
2b43685d 121// must be sorted (larger len must be further in enum)
c36e914d 122enum opr_lenmod {
91977a1c 123 OPLM_UNSPEC,
124 OPLM_BYTE,
125 OPLM_WORD,
126 OPLM_DWORD,
c36e914d 127};
128
850c9265 129#define MAX_OPERANDS 3
c36e914d 130
131struct parsed_opr {
91977a1c 132 enum opr_type type;
133 enum opr_lenmod lmod;
7ba45c34 134 unsigned int is_ptr:1; // pointer in C
135 unsigned int is_array:1; // array in C
a3684be1 136 unsigned int type_from_var:1; // .. in header, sometimes wrong
2b43685d 137 unsigned int size_mismatch:1; // type override differs from C
138 unsigned int size_lt:1; // type override is larger than C
ddaf8bd7 139 unsigned int had_ds:1; // had ds: prefix
c7ed83dd 140 const struct parsed_proto *pp; // for OPT_LABEL
91977a1c 141 int reg;
142 unsigned int val;
c7ed83dd 143 char name[112];
c36e914d 144};
145
146struct parsed_op {
91977a1c 147 enum op_op op;
148 struct parsed_opr operand[MAX_OPERANDS];
69a3cdfc 149 unsigned int flags;
092f64e1 150 unsigned char pfo;
151 unsigned char pfo_inv;
152 unsigned char operand_cnt;
153 unsigned char pad;
69a3cdfc 154 int regmask_src; // all referensed regs
155 int regmask_dst;
940e8e66 156 int pfomask; // flagop: parsed_flag_op that can't be delayed
de50b98b 157 int argnum; // push: altered before call arg #
940e8e66 158 int cc_scratch; // scratch storage during analysis
4c45fa73 159 int bt_i; // branch target for branches
160 struct parsed_data *btj;// branch targets for jumptables
c7ed83dd 161 struct parsed_proto *pp;// parsed_proto for OP_CALL
91977a1c 162 void *datap;
8eb12e72 163 int asmln;
91977a1c 164};
165
69a3cdfc 166// datap:
092f64e1 167// OP_CALL - parser proto hint (str)
2b43685d 168// (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op
5c024ef7 169// OP_POP - point to OP_PUSH in push/pop pair
69a3cdfc 170
91977a1c 171struct parsed_equ {
172 char name[64];
173 enum opr_lenmod lmod;
174 int offset;
c36e914d 175};
176
4c45fa73 177struct parsed_data {
178 char label[256];
179 enum opr_type type;
180 enum opr_lenmod lmod;
181 int count;
182 int count_alloc;
183 struct {
184 union {
185 char *label;
63df67be 186 unsigned int val;
4c45fa73 187 } u;
188 int bt_i;
189 } *d;
190};
191
192struct label_ref {
193 int i;
194 struct label_ref *next;
195};
196
1bafb621 197enum ida_func_attr {
198 IDAFA_BP_FRAME = (1 << 0),
199 IDAFA_LIB_FUNC = (1 << 1),
200 IDAFA_STATIC = (1 << 2),
201 IDAFA_NORETURN = (1 << 3),
202 IDAFA_THUNK = (1 << 4),
203 IDAFA_FPD = (1 << 5),
204};
205
206#define MAX_OPS 4096
c36e914d 207
208static struct parsed_op ops[MAX_OPS];
91977a1c 209static struct parsed_equ *g_eqs;
210static int g_eqcnt;
037f4971 211static char g_labels[MAX_OPS][48];
4c45fa73 212static struct label_ref g_label_refs[MAX_OPS];
bd96f656 213static const struct parsed_proto *g_func_pp;
4c45fa73 214static struct parsed_data *g_func_pd;
215static int g_func_pd_cnt;
91977a1c 216static char g_func[256];
217static char g_comment[256];
218static int g_bp_frame;
1bafb621 219static int g_sp_frame;
a2c1d768 220static int g_stack_frame_used;
1bafb621 221static int g_stack_fsz;
4c45fa73 222static int g_ida_func_attr;
89ff3147 223static int g_allow_regfunc;
91977a1c 224#define ferr(op_, fmt, ...) do { \
8eb12e72 225 printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
850c9265 226 dump_op(op_), ##__VA_ARGS__); \
33c35af6 227 fcloseall(); \
91977a1c 228 exit(1); \
229} while (0)
de50b98b 230#define fnote(op_, fmt, ...) \
8eb12e72 231 printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
de50b98b 232 dump_op(op_), ##__VA_ARGS__)
91977a1c 233
234#define MAX_REGS 8
235
236const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
237const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
238const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
239const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
240
241enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
242
69a3cdfc 243// possible basic comparison types (without inversion)
244enum parsed_flag_op {
245 PFO_O, // 0 OF=1
246 PFO_C, // 2 CF=1
247 PFO_Z, // 4 ZF=1
248 PFO_BE, // 6 CF=1||ZF=1
249 PFO_S, // 8 SF=1
250 PFO_P, // a PF=1
251 PFO_L, // c SF!=OF
252 PFO_LE, // e ZF=1||SF!=OF
253};
254
255static const char *parsed_flag_op_names[] = {
256 "o", "c", "z", "be", "s", "p", "l", "le"
257};
258
91977a1c 259static int char_array_i(const char *array[], size_t len, const char *s)
260{
261 int i;
c36e914d 262
91977a1c 263 for (i = 0; i < len; i++)
264 if (IS(s, array[i]))
265 return i;
c36e914d 266
91977a1c 267 return -1;
268}
269
63df67be 270static void printf_number(char *buf, size_t buf_size,
271 unsigned long number)
91977a1c 272{
5101a5f9 273 // output in C-friendly form
274 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
275}
91977a1c 276
5101a5f9 277static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
278{
279 int reg;
91977a1c 280
5101a5f9 281 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
282 if (reg >= 0) {
283 *reg_lmod = OPLM_DWORD;
284 return reg;
91977a1c 285 }
5101a5f9 286 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
287 if (reg >= 0) {
288 *reg_lmod = OPLM_WORD;
289 return reg;
290 }
291 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
292 if (reg >= 0) {
293 *reg_lmod = OPLM_BYTE;
294 return reg;
295 }
296 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
297 if (reg >= 0) {
298 *reg_lmod = OPLM_BYTE;
299 return reg;
850c9265 300 }
301
302 return -1;
91977a1c 303}
304
5101a5f9 305static int parse_indmode(char *name, int *regmask, int need_c_cvt)
306{
307 enum opr_lenmod lmod;
308 char cvtbuf[256];
309 char *d = cvtbuf;
310 char *s = name;
311 char w[64];
312 long number;
313 int reg;
314 int c = 0;
315
316 *d = 0;
317
318 while (*s != 0) {
319 d += strlen(d);
320 while (my_isblank(*s))
321 s++;
322 for (; my_issep(*s); d++, s++)
323 *d = *s;
324 while (my_isblank(*s))
325 s++;
326 *d = 0;
327
87bf6cec 328 // skip 'ds:' prefix
39b168b8 329 if (IS_START(s, "ds:"))
87bf6cec 330 s += 3;
331
5101a5f9 332 s = next_idt(w, sizeof(w), s);
333 if (w[0] == 0)
334 break;
335 c++;
336
337 reg = parse_reg(&lmod, w);
338 if (reg >= 0) {
339 *regmask |= 1 << reg;
340 goto pass;
341 }
342
343 if ('0' <= w[0] && w[0] <= '9') {
344 number = parse_number(w);
345 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
346 continue;
347 }
348
349 // probably some label/identifier - pass
350
351pass:
352 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
353 }
354
355 if (need_c_cvt)
356 strcpy(name, cvtbuf);
357
358 return c;
359}
360
4c45fa73 361static int is_reg_in_str(const char *s)
362{
363 int i;
364
365 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
366 return 0;
367
368 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
369 if (!strncmp(s, regs_r32[i], 3))
370 return 1;
371
372 return 0;
373}
374
375static const char *parse_stack_el(const char *name, char *extra_reg)
d4e3b5db 376{
4c45fa73 377 const char *p, *p2, *s;
1bafb621 378 char *endp = NULL;
d4e3b5db 379 char buf[32];
1bafb621 380 long val;
d4e3b5db 381 int len;
382
4c45fa73 383 p = name;
384 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
385 p += 4;
386 if (extra_reg != NULL) {
387 strncpy(extra_reg, name, 3);
388 extra_reg[4] = 0;
389 }
d4e3b5db 390 }
4c45fa73 391
392 if (IS_START(p, "ebp+")) {
393 p += 4;
394
395 p2 = strchr(p, '+');
396 if (p2 != NULL && is_reg_in_str(p)) {
397 if (extra_reg != NULL) {
398 strncpy(extra_reg, p, p2 - p);
399 extra_reg[p2 - p] = 0;
400 }
401 p = p2 + 1;
402 }
403
404 if (!('0' <= *p && *p <= '9'))
405 return p;
406
407 return NULL;
408 }
409
39b168b8 410 if (!IS_START(name, "esp+"))
d4e3b5db 411 return NULL;
412
037f4971 413 s = name + 4;
414 p = strchr(s, '+');
d4e3b5db 415 if (p) {
037f4971 416 if (is_reg_in_str(s)) {
417 if (extra_reg != NULL) {
418 strncpy(extra_reg, s, p - s);
419 extra_reg[p - s] = 0;
420 }
421 s = p + 1;
422 p = strchr(s, '+');
423 if (p == NULL)
424 aerr("%s IDA stackvar not set?\n", __func__);
425 }
1bafb621 426 if (!('0' <= *s && *s <= '9')) {
037f4971 427 aerr("%s IDA stackvar offset not set?\n", __func__);
d4e3b5db 428 return NULL;
1bafb621 429 }
430 if (s[0] == '0' && s[1] == 'x')
431 s += 2;
432 len = p - s;
d4e3b5db 433 if (len < sizeof(buf) - 1) {
1bafb621 434 strncpy(buf, s, len);
d4e3b5db 435 buf[len] = 0;
1bafb621 436 val = strtol(buf, &endp, 16);
437 if (val == 0 || *endp != 0) {
438 aerr("%s num parse fail for '%s'\n", __func__, buf);
439 return NULL;
440 }
d4e3b5db 441 }
442 p++;
443 }
444 else
445 p = name + 4;
446
447 if ('0' <= *p && *p <= '9')
448 return NULL;
449
450 return p;
451}
452
850c9265 453static int guess_lmod_from_name(struct parsed_opr *opr)
454{
455 if (!strncmp(opr->name, "dword_", 6)) {
456 opr->lmod = OPLM_DWORD;
457 return 1;
458 }
459 if (!strncmp(opr->name, "word_", 5)) {
460 opr->lmod = OPLM_WORD;
461 return 1;
462 }
463 if (!strncmp(opr->name, "byte_", 5)) {
464 opr->lmod = OPLM_BYTE;
465 return 1;
466 }
467 return 0;
468}
469
3ebea2cf 470static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
471 const struct parsed_type *c_type)
06c5d854 472{
06c5d854 473 static const char *dword_types[] = {
da87ae38 474 "int", "_DWORD", "UINT_PTR", "DWORD",
4741fdfe 475 "WPARAM", "LPARAM", "UINT", "__int32",
179b79a9 476 "LONG", "HIMC", "BOOL",
06c5d854 477 };
478 static const char *word_types[] = {
fe18709a 479 "uint16_t", "int16_t", "_WORD", "WORD",
2b43685d 480 "unsigned __int16", "__int16",
06c5d854 481 };
482 static const char *byte_types[] = {
2b43685d 483 "uint8_t", "int8_t", "char",
89ff3147 484 "unsigned __int8", "__int8", "BYTE", "_BYTE",
4741fdfe 485 "CHAR", "_UNKNOWN",
b4878d2b 486 // structures.. deal the same as with _UNKNOWN for now
487 "CRITICAL_SECTION",
06c5d854 488 };
3ebea2cf 489 const char *n;
06c5d854 490 int i;
491
3ebea2cf 492 if (c_type->is_ptr) {
493 *lmod = OPLM_DWORD;
06c5d854 494 return 1;
495 }
496
3ebea2cf 497 n = skip_type_mod(c_type->name);
06c5d854 498
3ebea2cf 499 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
500 if (IS(n, dword_types[i])) {
501 *lmod = OPLM_DWORD;
06c5d854 502 return 1;
503 }
504 }
505
506 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
3ebea2cf 507 if (IS(n, word_types[i])) {
508 *lmod = OPLM_WORD;
06c5d854 509 return 1;
510 }
511 }
512
513 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
3ebea2cf 514 if (IS(n, byte_types[i])) {
515 *lmod = OPLM_BYTE;
06c5d854 516 return 1;
517 }
518 }
519
06c5d854 520 return 0;
521}
522
c7ed83dd 523static char *default_cast_to(char *buf, size_t buf_size,
524 struct parsed_opr *opr)
525{
526 buf[0] = 0;
527
528 if (!opr->is_ptr)
529 return buf;
530 if (opr->pp == NULL || opr->pp->type.name == NULL
531 || opr->pp->is_fptr)
532 {
533 snprintf(buf, buf_size, "%s", "(void *)");
534 return buf;
535 }
536
537 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
538 return buf;
539}
540
4c45fa73 541static enum opr_type lmod_from_directive(const char *d)
542{
543 if (IS(d, "dd"))
544 return OPLM_DWORD;
545 else if (IS(d, "dw"))
546 return OPLM_WORD;
547 else if (IS(d, "db"))
548 return OPLM_BYTE;
549
550 aerr("unhandled directive: '%s'\n", d);
551 return OPLM_UNSPEC;
552}
553
5101a5f9 554static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
555 int *regmask)
556{
557 opr->type = OPT_REG;
558 opr->reg = reg;
559 opr->lmod = lmod;
560 *regmask |= 1 << reg;
561}
562
39b168b8 563static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
564 int *extra_offs);
87bf6cec 565
69a3cdfc 566static int parse_operand(struct parsed_opr *opr,
567 int *regmask, int *regmask_indirect,
1bafb621 568 char words[16][256], int wordc, int w, unsigned int op_flags)
c36e914d 569{
bd96f656 570 const struct parsed_proto *pp;
850c9265 571 enum opr_lenmod tmplmod;
63df67be 572 unsigned long number;
89ff3147 573 char buf[256];
850c9265 574 int ret, len;
1bafb621 575 int wordc_in;
89ff3147 576 char *p;
850c9265 577 int i;
c36e914d 578
1bafb621 579 if (w >= wordc)
580 aerr("parse_operand w %d, wordc %d\n", w, wordc);
c36e914d 581
1bafb621 582 opr->reg = xUNSPEC;
91977a1c 583
1bafb621 584 for (i = w; i < wordc; i++) {
585 len = strlen(words[i]);
586 if (words[i][len - 1] == ',') {
587 words[i][len - 1] = 0;
588 wordc = i + 1;
589 break;
590 }
591 }
c36e914d 592
1bafb621 593 wordc_in = wordc - w;
c36e914d 594
1bafb621 595 if ((op_flags & OPF_JMP) && wordc_in > 0
596 && !('0' <= words[w][0] && words[w][0] <= '9'))
597 {
598 const char *label = NULL;
599
600 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
601 && IS(words[w + 1], "ptr"))
602 label = words[w + 2];
603 else if (wordc_in == 2 && IS(words[w], "short"))
604 label = words[w + 1];
605 else if (wordc_in == 1
606 && strchr(words[w], '[') == NULL
607 && parse_reg(&tmplmod, words[w]) < 0)
608 label = words[w];
609
610 if (label != NULL) {
611 opr->type = OPT_LABEL;
ddaf8bd7 612 if (IS_START(label, "ds:")) {
613 opr->had_ds = 1;
39b168b8 614 label += 3;
ddaf8bd7 615 }
1bafb621 616 strcpy(opr->name, label);
617 return wordc;
618 }
619 }
c36e914d 620
1bafb621 621 if (wordc_in >= 3) {
622 if (IS(words[w + 1], "ptr")) {
623 if (IS(words[w], "dword"))
624 opr->lmod = OPLM_DWORD;
625 else if (IS(words[w], "word"))
626 opr->lmod = OPLM_WORD;
627 else if (IS(words[w], "byte"))
628 opr->lmod = OPLM_BYTE;
629 else
630 aerr("type parsing failed\n");
631 w += 2;
632 wordc_in = wordc - w;
633 }
634 }
635
39b168b8 636 if (wordc_in == 2) {
637 if (IS(words[w], "offset")) {
638 opr->type = OPT_OFFSET;
27ebfaed 639 opr->lmod = OPLM_DWORD;
39b168b8 640 strcpy(opr->name, words[w + 1]);
27ebfaed 641 pp = proto_parse(g_fhdr, opr->name, 1);
642 goto do_label;
39b168b8 643 }
644 if (IS(words[w], "(offset")) {
89ff3147 645 p = strchr(words[w + 1], ')');
39b168b8 646 if (p == NULL)
647 aerr("parse of bracketed offset failed\n");
648 *p = 0;
649 opr->type = OPT_OFFSET;
650 strcpy(opr->name, words[w + 1]);
651 return wordc;
652 }
1bafb621 653 }
c36e914d 654
1bafb621 655 if (wordc_in != 1)
850c9265 656 aerr("parse_operand 1 word expected\n");
c36e914d 657
ddaf8bd7 658 if (IS_START(words[w], "ds:")) {
659 opr->had_ds = 1;
89ff3147 660 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
ddaf8bd7 661 }
89ff3147 662 strcpy(opr->name, words[w]);
c36e914d 663
850c9265 664 if (words[w][0] == '[') {
665 opr->type = OPT_REGMEM;
666 ret = sscanf(words[w], "[%[^]]]", opr->name);
667 if (ret != 1)
668 aerr("[] parse failure\n");
87bf6cec 669
5101a5f9 670 parse_indmode(opr->name, regmask_indirect, 1);
4c45fa73 671 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL)) {
87bf6cec 672 // might be an equ
d4e3b5db 673 struct parsed_equ *eq =
4c45fa73 674 equ_find(NULL, parse_stack_el(opr->name, NULL), &i);
87bf6cec 675 if (eq)
676 opr->lmod = eq->lmod;
677 }
850c9265 678 return wordc;
679 }
680 else if (strchr(words[w], '[')) {
681 // label[reg] form
89ff3147 682 p = strchr(words[w], '[');
850c9265 683 opr->type = OPT_REGMEM;
89ff3147 684 parse_indmode(p, regmask_indirect, 0);
685 strncpy(buf, words[w], p - words[w]);
686 buf[p - words[w]] = 0;
687 pp = proto_parse(g_fhdr, buf, 1);
688 goto do_label;
850c9265 689 }
690 else if (('0' <= words[w][0] && words[w][0] <= '9')
691 || words[w][0] == '-')
692 {
5101a5f9 693 number = parse_number(words[w]);
91977a1c 694 opr->type = OPT_CONST;
5101a5f9 695 opr->val = number;
696 printf_number(opr->name, sizeof(opr->name), number);
91977a1c 697 return wordc;
850c9265 698 }
c36e914d 699
5101a5f9 700 ret = parse_reg(&tmplmod, opr->name);
701 if (ret >= 0) {
702 setup_reg_opr(opr, ret, tmplmod, regmask);
850c9265 703 return wordc;
704 }
705
706 // most likely var in data segment
707 opr->type = OPT_LABEL;
36595fd2 708 pp = proto_parse(g_fhdr, opr->name, 0);
89ff3147 709
710do_label:
bd96f656 711 if (pp != NULL) {
1cd4a663 712 if (pp->is_fptr || pp->is_func) {
06c5d854 713 opr->lmod = OPLM_DWORD;
714 opr->is_ptr = 1;
715 }
2b43685d 716 else {
840257f6 717 tmplmod = OPLM_UNSPEC;
bd96f656 718 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
2b43685d 719 anote("unhandled C type '%s' for '%s'\n",
bd96f656 720 pp->type.name, opr->name);
2b43685d 721
a3684be1 722 if (opr->lmod == OPLM_UNSPEC) {
2b43685d 723 opr->lmod = tmplmod;
a3684be1 724 opr->type_from_var = 1;
725 }
2b43685d 726 else if (opr->lmod != tmplmod) {
727 opr->size_mismatch = 1;
728 if (tmplmod < opr->lmod)
729 opr->size_lt = 1;
730 }
a652aa9f 731 opr->is_ptr = pp->type.is_ptr;
3ebea2cf 732 }
bd96f656 733 opr->is_array = pp->type.is_array;
06c5d854 734 }
c7ed83dd 735 opr->pp = pp;
06c5d854 736
850c9265 737 if (opr->lmod == OPLM_UNSPEC)
738 guess_lmod_from_name(opr);
850c9265 739 return wordc;
c36e914d 740}
741
33c35af6 742static const struct {
743 const char *name;
744 unsigned int flags;
745} pref_table[] = {
746 { "rep", OPF_REP },
7ba45c34 747 { "repe", OPF_REP|OPF_REPZ },
748 { "repz", OPF_REP|OPF_REPZ },
749 { "repne", OPF_REP|OPF_REPNZ },
750 { "repnz", OPF_REP|OPF_REPNZ },
037f4971 751 { "lock", OPF_LOCK }, // ignored for now..
33c35af6 752};
753
5c024ef7 754#define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
755
c36e914d 756static const struct {
850c9265 757 const char *name;
758 enum op_op op;
092f64e1 759 unsigned short minopr;
760 unsigned short maxopr;
69a3cdfc 761 unsigned int flags;
092f64e1 762 unsigned char pfo;
763 unsigned char pfo_inv;
c36e914d 764} op_table[] = {
33c35af6 765 { "nop", OP_NOP, 0, 0, 0 },
69a3cdfc 766 { "push", OP_PUSH, 1, 1, 0 },
767 { "pop", OP_POP, 1, 1, OPF_DATA },
591721d7 768 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
69a3cdfc 769 { "mov" , OP_MOV, 2, 2, OPF_DATA },
770 { "lea", OP_LEA, 2, 2, OPF_DATA },
771 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
772 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
108e9fe3 773 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
69a3cdfc 774 { "not", OP_NOT, 1, 1, OPF_DATA },
5101a5f9 775 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
092f64e1 776 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
777 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
778 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
33c35af6 779 { "stosb",OP_STOS, 0, 0, OPF_DATA },
780 { "stosw",OP_STOS, 0, 0, OPF_DATA },
781 { "stosd",OP_STOS, 0, 0, OPF_DATA },
d4e3b5db 782 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
783 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
784 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
7ba45c34 785 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
786 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
787 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
591721d7 788 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
789 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
790 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
791 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
792 { "cld", OP_CLD, 0, 0, OPF_DATA },
69a3cdfc 793 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
794 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
795 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
796 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
797 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
798 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
799 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
800 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
801 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
d4e3b5db 802 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
803 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
092f64e1 804 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
805 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
806 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
807 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
1f84f6b3 808 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
69a3cdfc 809 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
810 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
5101a5f9 811 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
812 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
69a3cdfc 813 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
5101a5f9 814 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
815 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
69a3cdfc 816 { "test", OP_TEST, 2, 2, OPF_FLAGS },
817 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
a3684be1 818 { "retn", OP_RET, 0, 1, OPF_TAIL },
de50b98b 819 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
69a3cdfc 820 { "jmp", OP_JMP, 1, 1, OPF_JMP },
5c024ef7 821 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
092f64e1 822 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
823 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
824 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
825 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
826 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
827 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
828 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
829 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
830 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
831 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
832 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
833 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
834 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
835 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
836 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
837 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
838 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
839 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
840 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
841 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
842 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
843 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
844 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
845 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
846 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
847 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
848 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
849 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
850 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
851 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
852 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
853 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
854 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
855 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
856 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
857 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
858 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
859 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
860 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
861 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
862 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
863 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
864 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
865 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
866 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
867 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
868 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
869 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
870 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
871 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
872 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
873 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
874 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
875 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
876 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
877 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
878 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
879 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
69a3cdfc 880};
c36e914d 881
882static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
883{
bfa4a6ee 884 enum opr_lenmod lmod = OPLM_UNSPEC;
33c35af6 885 int prefix_flags = 0;
69a3cdfc 886 int regmask_ind;
887 int regmask;
33c35af6 888 int op_w = 0;
91977a1c 889 int opr = 0;
33c35af6 890 int w = 0;
91977a1c 891 int i;
c36e914d 892
33c35af6 893 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
894 if (IS(words[w], pref_table[i].name)) {
895 prefix_flags = pref_table[i].flags;
896 break;
897 }
898 }
899
900 if (prefix_flags) {
901 if (wordc <= 1)
902 aerr("lone prefix: '%s'\n", words[0]);
903 w++;
904 }
905
906 op_w = w;
91977a1c 907 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
33c35af6 908 if (IS(words[w], op_table[i].name))
69a3cdfc 909 break;
910 }
c36e914d 911
69a3cdfc 912 if (i == ARRAY_SIZE(op_table))
913 aerr("unhandled op: '%s'\n", words[0]);
33c35af6 914 w++;
c36e914d 915
69a3cdfc 916 op->op = op_table[i].op;
33c35af6 917 op->flags = op_table[i].flags | prefix_flags;
092f64e1 918 op->pfo = op_table[i].pfo;
919 op->pfo_inv = op_table[i].pfo_inv;
69a3cdfc 920 op->regmask_src = op->regmask_dst = 0;
8eb12e72 921 op->asmln = asmln;
69a3cdfc 922
923 for (opr = 0; opr < op_table[i].minopr; opr++) {
924 regmask = regmask_ind = 0;
925 w = parse_operand(&op->operand[opr], &regmask, &regmask_ind,
926 words, wordc, w, op->flags);
927
928 if (opr == 0 && (op->flags & OPF_DATA))
929 op->regmask_dst = regmask;
930 // for now, mark dst as src too
931 op->regmask_src |= regmask | regmask_ind;
932 }
c36e914d 933
69a3cdfc 934 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
935 w = parse_operand(&op->operand[opr],
936 &op->regmask_src, &op->regmask_src,
937 words, wordc, w, op->flags);
91977a1c 938 }
c36e914d 939
91977a1c 940 if (w < wordc)
941 aerr("parse_op %s incomplete: %d/%d\n",
942 words[0], w, wordc);
5101a5f9 943
944 // special cases
945 op->operand_cnt = opr;
946 if (!strncmp(op_table[i].name, "set", 3))
947 op->operand[0].lmod = OPLM_BYTE;
948
949 // ops with implicit argumets
950 switch (op->op) {
951 case OP_CDQ:
952 op->operand_cnt = 2;
953 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
954 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
955 break;
956
092f64e1 957 case OP_LODS:
33c35af6 958 case OP_STOS:
591721d7 959 case OP_SCAS:
33c35af6 960 if (op->operand_cnt != 0)
961 break;
591721d7 962 if (words[op_w][4] == 'b')
33c35af6 963 lmod = OPLM_BYTE;
591721d7 964 else if (words[op_w][4] == 'w')
33c35af6 965 lmod = OPLM_WORD;
591721d7 966 else if (words[op_w][4] == 'd')
33c35af6 967 lmod = OPLM_DWORD;
968 op->operand_cnt = 3;
092f64e1 969 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
970 lmod, &op->regmask_src);
d4e3b5db 971 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
972 op->regmask_dst = op->regmask_src;
092f64e1 973 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
974 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
33c35af6 975 break;
976
d4e3b5db 977 case OP_MOVS:
7ba45c34 978 case OP_CMPS:
d4e3b5db 979 if (op->operand_cnt != 0)
980 break;
7ba45c34 981 if (words[op_w][4] == 'b')
d4e3b5db 982 lmod = OPLM_BYTE;
7ba45c34 983 else if (words[op_w][4] == 'w')
d4e3b5db 984 lmod = OPLM_WORD;
7ba45c34 985 else if (words[op_w][4] == 'd')
d4e3b5db 986 lmod = OPLM_DWORD;
987 op->operand_cnt = 3;
988 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
989 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
990 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
991 op->regmask_dst = op->regmask_src;
992 break;
993
108e9fe3 994 case OP_XCHG:
995 op->regmask_src |= op->regmask_dst;
996 op->regmask_dst |= op->regmask_src;
092f64e1 997 goto check_align;
108e9fe3 998
5c024ef7 999 case OP_JECXZ:
1000 op->operand_cnt = 1;
1001 op->regmask_src = 1 << xCX;
1002 op->operand[0].type = OPT_REG;
1003 op->operand[0].reg = xCX;
1004 op->operand[0].lmod = OPLM_DWORD;
1005 break;
1006
5101a5f9 1007 case OP_IMUL:
1008 if (op->operand_cnt != 1)
1009 break;
1010 // fallthrough
1011 case OP_MUL:
1012 // singleop mul
1013 op->regmask_dst = (1 << xDX) | (1 << xAX);
1014 op->regmask_src |= (1 << xAX);
1015 if (op->operand[0].lmod == OPLM_UNSPEC)
1016 op->operand[0].lmod = OPLM_DWORD;
1017 break;
1018
1019 case OP_DIV:
1020 case OP_IDIV:
1021 // we could set up operands for edx:eax, but there is no real need to
1022 // (see is_opr_modified())
1023 regmask = (1 << xDX) | (1 << xAX);
1024 op->regmask_dst = regmask;
1025 op->regmask_src |= regmask;
1026 if (op->operand[0].lmod == OPLM_UNSPEC)
1027 op->operand[0].lmod = OPLM_DWORD;
1028 break;
1029
1030 case OP_SHL:
1031 case OP_SHR:
1032 case OP_SAR:
d4e3b5db 1033 case OP_ROL:
1034 case OP_ROR:
5101a5f9 1035 if (op->operand[1].lmod == OPLM_UNSPEC)
1036 op->operand[1].lmod = OPLM_BYTE;
1037 break;
1038
7ba45c34 1039 case OP_PUSH:
1040 if (op->operand[0].lmod == OPLM_UNSPEC
1041 && (op->operand[0].type == OPT_CONST
1042 || op->operand[0].type == OPT_OFFSET
1043 || op->operand[0].type == OPT_LABEL))
1044 op->operand[0].lmod = OPLM_DWORD;
1045 break;
1046
3ebea2cf 1047 // alignment
1048 case OP_MOV:
092f64e1 1049 check_align:
3ebea2cf 1050 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
092f64e1 1051 && op->operand[0].lmod == op->operand[1].lmod
1052 && op->operand[0].reg == op->operand[1].reg
1053 && IS(op->operand[0].name, op->operand[1].name)) // ah, al..
3ebea2cf 1054 {
1055 op->flags |= OPF_RMD;
092f64e1 1056 op->regmask_src = op->regmask_dst = 0;
3ebea2cf 1057 }
1058 break;
1059
de50b98b 1060 case OP_LEA:
1061 if (op->operand[0].type == OPT_REG
1062 && op->operand[1].type == OPT_REGMEM)
1063 {
1064 char buf[16];
1065 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1066 if (IS(buf, op->operand[1].name))
1067 op->flags |= OPF_RMD;
1068 }
1069 break;
1070
89ff3147 1071 case OP_CALL:
1072 // trashed regs must be explicitly detected later
1073 op->regmask_dst = 0;
1074 break;
1075
5101a5f9 1076 default:
1077 break;
1078 }
c36e914d 1079}
1080
092f64e1 1081static const char *op_name(struct parsed_op *po)
850c9265 1082{
092f64e1 1083 static char buf[16];
1084 char *p;
850c9265 1085 int i;
1086
092f64e1 1087 if (po->op == OP_JCC || po->op == OP_SCC) {
1088 p = buf;
1089 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1090 if (po->pfo_inv)
1091 *p++ = 'n';
1092 strcpy(p, parsed_flag_op_names[po->pfo]);
1093 return buf;
1094 }
1095
850c9265 1096 for (i = 0; i < ARRAY_SIZE(op_table); i++)
092f64e1 1097 if (op_table[i].op == po->op)
850c9265 1098 return op_table[i].name;
1099
1100 return "???";
1101}
1102
1103// debug
1104static const char *dump_op(struct parsed_op *po)
1105{
1106 static char out[128];
1107 char *p = out;
1108 int i;
1109
4c45fa73 1110 if (po == NULL)
1111 return "???";
1112
092f64e1 1113 snprintf(out, sizeof(out), "%s", op_name(po));
850c9265 1114 for (i = 0; i < po->operand_cnt; i++) {
1115 p += strlen(p);
1116 if (i > 0)
1117 *p++ = ',';
1118 snprintf(p, sizeof(out) - (p - out),
1119 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1120 po->operand[i].name);
1121 }
1122
1123 return out;
1124}
1125
4c45fa73 1126static const char *lmod_type_u(struct parsed_op *po,
1127 enum opr_lenmod lmod)
1128{
1129 switch (lmod) {
1130 case OPLM_DWORD:
1131 return "u32";
1132 case OPLM_WORD:
1133 return "u16";
1134 case OPLM_BYTE:
1135 return "u8";
1136 default:
1137 ferr(po, "invalid lmod: %d\n", lmod);
1138 return "(_invalid_)";
1139 }
1140}
1141
d4e3b5db 1142static const char *lmod_cast_u(struct parsed_op *po,
1143 enum opr_lenmod lmod)
1144{
1145 switch (lmod) {
1146 case OPLM_DWORD:
1147 return "";
1148 case OPLM_WORD:
1149 return "(u16)";
1150 case OPLM_BYTE:
1151 return "(u8)";
1152 default:
1153 ferr(po, "invalid lmod: %d\n", lmod);
1154 return "(_invalid_)";
1155 }
1156}
1157
1158static const char *lmod_cast_u_ptr(struct parsed_op *po,
1159 enum opr_lenmod lmod)
1160{
1161 switch (lmod) {
1162 case OPLM_DWORD:
1163 return "*(u32 *)";
1164 case OPLM_WORD:
1165 return "*(u16 *)";
1166 case OPLM_BYTE:
1167 return "*(u8 *)";
1168 default:
1169 ferr(po, "invalid lmod: %d\n", lmod);
1170 return "(_invalid_)";
1171 }
1172}
1173
1174static const char *lmod_cast_s(struct parsed_op *po,
1175 enum opr_lenmod lmod)
1176{
1177 switch (lmod) {
1178 case OPLM_DWORD:
1179 return "(s32)";
1180 case OPLM_WORD:
1181 return "(s16)";
1182 case OPLM_BYTE:
1183 return "(s8)";
1184 default:
1185 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1186 return "(_invalid_)";
1187 }
1188}
1189
1190static const char *lmod_cast(struct parsed_op *po,
1191 enum opr_lenmod lmod, int is_signed)
1192{
1193 return is_signed ?
1194 lmod_cast_s(po, lmod) :
1195 lmod_cast_u(po, lmod);
1196}
1197
1198static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1199{
1200 switch (lmod) {
1201 case OPLM_DWORD:
1202 return 4;
1203 case OPLM_WORD:
1204 return 2;
1205 case OPLM_BYTE:
1206 return 1;
1207 default:
1208 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1209 return 0;
1210 }
1211}
1212
91977a1c 1213static const char *opr_name(struct parsed_op *po, int opr_num)
c36e914d 1214{
91977a1c 1215 if (opr_num >= po->operand_cnt)
1216 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1217 return po->operand[opr_num].name;
c36e914d 1218}
1219
91977a1c 1220static unsigned int opr_const(struct parsed_op *po, int opr_num)
c36e914d 1221{
91977a1c 1222 if (opr_num >= po->operand_cnt)
1223 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1224 if (po->operand[opr_num].type != OPT_CONST)
1225 ferr(po, "opr %d: const expected\n", opr_num);
1226 return po->operand[opr_num].val;
1227}
c36e914d 1228
91977a1c 1229static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1230{
1231 if ((unsigned int)popr->reg >= MAX_REGS)
1232 ferr(po, "invalid reg: %d\n", popr->reg);
1233 return regs_r32[popr->reg];
1234}
c36e914d 1235
a2c1d768 1236// cast1 is the "final" cast
1237static const char *simplify_cast(const char *cast1, const char *cast2)
1238{
1239 static char buf[256];
1240
1241 if (cast1[0] == 0)
1242 return cast2;
1243 if (cast2[0] == 0)
1244 return cast1;
1245 if (IS(cast1, cast2))
1246 return cast1;
1247 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1248 return cast1;
1249 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1250 return cast1;
1251 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1252 return cast2;
1253 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1254 return cast2;
1cd4a663 1255 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1256 return cast1;
a2c1d768 1257
1258 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1259 return buf;
1260}
1261
1262static const char *simplify_cast_num(const char *cast, unsigned int val)
1263{
1264 if (IS(cast, "(u8)") && val < 0x100)
1265 return "";
1266 if (IS(cast, "(s8)") && val < 0x80)
1267 return "";
1268 if (IS(cast, "(u16)") && val < 0x10000)
1269 return "";
1270 if (IS(cast, "(s16)") && val < 0x8000)
1271 return "";
1272 if (IS(cast, "(s32)") && val < 0x80000000)
1273 return "";
1274
1275 return cast;
1276}
1277
39b168b8 1278static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1279 int *extra_offs)
91977a1c 1280{
39b168b8 1281 const char *p;
1282 char *endp;
1283 int namelen;
850c9265 1284 int i;
1285
39b168b8 1286 *extra_offs = 0;
1287 namelen = strlen(name);
1288
1289 p = strchr(name, '+');
1290 if (p != NULL) {
1291 namelen = p - name;
1292 if (namelen <= 0)
1293 ferr(po, "equ parse failed for '%s'\n", name);
1294
1295 if (IS_START(p, "0x"))
1296 p += 2;
1297 *extra_offs = strtol(p, &endp, 16);
1298 if (*endp != 0)
1299 ferr(po, "equ parse failed for '%s'\n", name);
1300 }
1301
850c9265 1302 for (i = 0; i < g_eqcnt; i++)
39b168b8 1303 if (strncmp(g_eqs[i].name, name, namelen) == 0
1304 && g_eqs[i].name[namelen] == 0)
850c9265 1305 break;
87bf6cec 1306 if (i >= g_eqcnt) {
1307 if (po != NULL)
1308 ferr(po, "unresolved equ name: '%s'\n", name);
1309 return NULL;
1310 }
850c9265 1311
1312 return &g_eqs[i];
1313}
1314
1cd4a663 1315static int is_stack_access(struct parsed_op *po,
1316 const struct parsed_opr *popr)
850c9265 1317{
1cd4a663 1318 return (parse_stack_el(popr->name, NULL)
1319 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1320 && IS_START(popr->name, "ebp")));
1321}
1322
1323static void parse_stack_access(struct parsed_op *po,
1324 const char *name, char *ofs_reg, int *offset_out,
037f4971 1325 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1cd4a663 1326{
1327 const char *bp_arg = "";
1328 const char *p = NULL;
91977a1c 1329 struct parsed_equ *eq;
4f12f671 1330 char *endp = NULL;
d4e3b5db 1331 int stack_ra = 0;
39b168b8 1332 int offset = 0;
91977a1c 1333
1cd4a663 1334 ofs_reg[0] = 0;
a3684be1 1335
1336 if (IS_START(name, "ebp-")
1337 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1338 {
1339 p = name + 4;
1340 if (IS_START(p, "0x"))
1341 p += 2;
1342 offset = strtoul(p, &endp, 16);
1343 if (name[3] == '-')
1344 offset = -offset;
1345 if (*endp != 0)
1346 ferr(po, "ebp- parse of '%s' failed\n", name);
1347 }
1348 else {
4f12f671 1349 bp_arg = parse_stack_el(name, ofs_reg);
1350 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1351 eq = equ_find(po, bp_arg, &offset);
1352 if (eq == NULL)
1353 ferr(po, "detected but missing eq\n");
1354 offset += eq->offset;
1355 }
39b168b8 1356
d4e3b5db 1357 if (!strncmp(name, "ebp", 3))
1358 stack_ra = 4;
1359
037f4971 1360 // yes it sometimes LEAs ra for compares..
1361 if (!is_lea && ofs_reg[0] == 0
1362 && stack_ra <= offset && offset < stack_ra + 4)
1363 {
39b168b8 1364 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
037f4971 1365 }
d4e3b5db 1366
1cd4a663 1367 *offset_out = offset;
1368 *stack_ra_out = stack_ra;
1369 if (bp_arg_out)
1370 *bp_arg_out = bp_arg;
1371}
1372
1373static void stack_frame_access(struct parsed_op *po,
1374 struct parsed_opr *popr, char *buf, size_t buf_size,
1375 const char *name, const char *cast, int is_src, int is_lea)
1376{
1377 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1378 const char *prefix = "";
1379 const char *bp_arg = NULL;
1380 char ofs_reg[16] = { 0, };
1381 int i, arg_i, arg_s;
1382 int unaligned = 0;
1383 int stack_ra = 0;
1384 int offset = 0;
1385 int sf_ofs;
1386 int lim;
1387
1388 if (po->flags & OPF_EBP_S)
1389 ferr(po, "stack_frame_access while ebp is scratch\n");
1390
037f4971 1391 parse_stack_access(po, name, ofs_reg, &offset,
1392 &stack_ra, &bp_arg, is_lea);
1cd4a663 1393
4f12f671 1394 if (offset > stack_ra)
1395 {
39b168b8 1396 arg_i = (offset - stack_ra - 4) / 4;
bd96f656 1397 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
4f12f671 1398 {
bd96f656 1399 if (g_func_pp->is_vararg
1400 && arg_i == g_func_pp->argc_stack && is_lea)
1401 {
4f12f671 1402 // should be va_list
1403 if (cast[0] == 0)
1404 cast = "(u32)";
1405 snprintf(buf, buf_size, "%sap", cast);
1406 return;
1407 }
1408 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1409 offset, bp_arg, arg_i);
1410 }
4c45fa73 1411 if (ofs_reg[0] != 0)
7ba45c34 1412 ferr(po, "offset reg on arg access?\n");
91977a1c 1413
bd96f656 1414 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1415 if (g_func_pp->arg[i].reg != NULL)
91977a1c 1416 continue;
1417 if (arg_s == arg_i)
1418 break;
1419 arg_s++;
1420 }
bd96f656 1421 if (i == g_func_pp->argc)
91977a1c 1422 ferr(po, "arg %d not in prototype?\n", arg_i);
850c9265 1423
bd96f656 1424 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
de50b98b 1425
1426 switch (popr->lmod)
7ba45c34 1427 {
1428 case OPLM_BYTE:
4f12f671 1429 if (is_lea)
1430 ferr(po, "lea/byte to arg?\n");
7ba45c34 1431 if (is_src && (offset & 3) == 0)
a2c1d768 1432 snprintf(buf, buf_size, "%sa%d",
1433 simplify_cast(cast, "(u8)"), i + 1);
7ba45c34 1434 else
a2c1d768 1435 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1436 cast, offset & 3, i + 1);
7ba45c34 1437 break;
1438
1439 case OPLM_WORD:
4f12f671 1440 if (is_lea)
1441 ferr(po, "lea/word to arg?\n");
a3684be1 1442 if (offset & 1) {
1443 unaligned = 1;
1444 if (!is_src) {
1445 if (offset & 2)
1446 ferr(po, "problematic arg store\n");
a2c1d768 1447 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1448 simplify_cast(cast, "*(u16 *)"), i + 1);
a3684be1 1449 }
1450 else
1451 ferr(po, "unaligned arg word load\n");
1452 }
1453 else if (is_src && (offset & 2) == 0)
a2c1d768 1454 snprintf(buf, buf_size, "%sa%d",
1455 simplify_cast(cast, "(u16)"), i + 1);
7ba45c34 1456 else
a2c1d768 1457 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1458 cast, (offset & 2) ? "HI" : "LO", i + 1);
7ba45c34 1459 break;
1460
1461 case OPLM_DWORD:
3ebea2cf 1462 if (cast[0])
1463 prefix = cast;
1464 else if (is_src)
1465 prefix = "(u32)";
a3684be1 1466
de50b98b 1467 if (offset & 3) {
a3684be1 1468 unaligned = 1;
2b43685d 1469 if (is_lea)
1470 snprintf(buf, buf_size, "(u32)&a%d + %d",
1471 i + 1, offset & 3);
a3684be1 1472 else if (!is_src)
1473 ferr(po, "unaligned arg store\n");
1474 else {
1475 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
2b43685d 1476 snprintf(buf, buf_size, "%s(a%d >> %d)",
1477 prefix, i + 1, (offset & 3) * 8);
a3684be1 1478 }
de50b98b 1479 }
1480 else {
1481 snprintf(buf, buf_size, "%s%sa%d",
1482 prefix, is_lea ? "&" : "", i + 1);
1483 }
7ba45c34 1484 break;
1485
1486 default:
de50b98b 1487 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
7ba45c34 1488 }
1489
a3684be1 1490 if (unaligned)
1491 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1492
7ba45c34 1493 // common problem
bd96f656 1494 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
a2c1d768 1495 if (tmp_lmod != OPLM_DWORD
1496 && (unaligned || (!is_src && tmp_lmod < popr->lmod)))
1497 {
1498 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1499 i + 1, offset, g_func_pp->arg[i].type.name);
1500 }
4741fdfe 1501 // can't check this because msvc likes to reuse
1502 // arg space for scratch..
1503 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1504 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
91977a1c 1505 }
4f12f671 1506 else
1507 {
1bafb621 1508 if (g_stack_fsz == 0)
1509 ferr(po, "stack var access without stackframe\n");
a2c1d768 1510 g_stack_frame_used = 1;
850c9265 1511
39b168b8 1512 sf_ofs = g_stack_fsz + offset;
de50b98b 1513 lim = (ofs_reg[0] != 0) ? -4 : 0;
1514 if (offset > 0 || sf_ofs < lim)
39b168b8 1515 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
850c9265 1516
1517 if (is_lea)
33c35af6 1518 prefix = "(u32)&";
3ebea2cf 1519 else
1520 prefix = cast;
850c9265 1521
de50b98b 1522 switch (popr->lmod)
850c9265 1523 {
1524 case OPLM_BYTE:
7ba45c34 1525 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1526 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
850c9265 1527 break;
7ba45c34 1528
850c9265 1529 case OPLM_WORD:
7ba45c34 1530 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1531 // known unaligned or possibly unaligned
1532 strcat(g_comment, " unaligned");
1533 if (prefix[0] == 0)
1534 prefix = "*(u16 *)&";
1535 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1536 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1537 break;
1538 }
1539 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
850c9265 1540 break;
7ba45c34 1541
850c9265 1542 case OPLM_DWORD:
7ba45c34 1543 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1544 // known unaligned or possibly unaligned
1545 strcat(g_comment, " unaligned");
1546 if (prefix[0] == 0)
1547 prefix = "*(u32 *)&";
1548 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1549 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1550 break;
1551 }
1552 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
850c9265 1553 break;
7ba45c34 1554
850c9265 1555 default:
de50b98b 1556 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
850c9265 1557 }
91977a1c 1558 }
1559}
c36e914d 1560
89ff3147 1561static void check_func_pp(struct parsed_op *po,
1562 const struct parsed_proto *pp, const char *pfx)
1563{
179b79a9 1564 enum opr_lenmod tmp_lmod;
b74c31e3 1565 char buf[256];
179b79a9 1566 int ret, i;
b74c31e3 1567
89ff3147 1568 if (pp->argc_reg != 0) {
b74c31e3 1569 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1570 pp_print(buf, sizeof(buf), pp);
1571 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1572 }
89ff3147 1573 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1574 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1575 pfx, pp->argc_reg, pp->argc_stack);
1576 }
179b79a9 1577
1578 // fptrs must use 32bit args, callsite might have no information and
1579 // lack a cast to smaller types, which results in incorrectly masked
1580 // args passed (callee may assume masked args, it does on ARM)
1581 if (!pp->is_oslib) {
1582 for (i = 0; i < pp->argc; i++) {
1583 ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
1584 if (ret && tmp_lmod != OPLM_DWORD)
1585 ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
1586 i + 1, pp->arg[i].type.name);
1587 }
1588 }
89ff3147 1589}
1590
7aca4698 1591static const char *check_label_read_ref(struct parsed_op *po,
1592 const char *name)
3ebea2cf 1593{
840257f6 1594 const struct parsed_proto *pp;
1595
36595fd2 1596 pp = proto_parse(g_fhdr, name, 0);
840257f6 1597 if (pp == NULL)
1598 ferr(po, "proto_parse failed for ref '%s'\n", name);
1599
89ff3147 1600 if (pp->is_func)
1601 check_func_pp(po, pp, "ref");
7aca4698 1602
1603 return pp->name;
3ebea2cf 1604}
1605
91977a1c 1606static char *out_src_opr(char *buf, size_t buf_size,
591721d7 1607 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
3ebea2cf 1608 int is_lea)
91977a1c 1609{
850c9265 1610 char tmp1[256], tmp2[256];
1611 char expr[256];
7aca4698 1612 const char *name;
a2c1d768 1613 char *p;
850c9265 1614 int ret;
1615
3ebea2cf 1616 if (cast == NULL)
1617 cast = "";
1618
91977a1c 1619 switch (popr->type) {
1620 case OPT_REG:
850c9265 1621 if (is_lea)
1622 ferr(po, "lea from reg?\n");
1623
91977a1c 1624 switch (popr->lmod) {
1625 case OPLM_DWORD:
3ebea2cf 1626 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
91977a1c 1627 break;
850c9265 1628 case OPLM_WORD:
a2c1d768 1629 snprintf(buf, buf_size, "%s%s",
1630 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
850c9265 1631 break;
1632 case OPLM_BYTE:
5101a5f9 1633 if (popr->name[1] == 'h') // XXX..
a2c1d768 1634 snprintf(buf, buf_size, "%s(%s >> 8)",
1635 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
5101a5f9 1636 else
a2c1d768 1637 snprintf(buf, buf_size, "%s%s",
1638 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
850c9265 1639 break;
91977a1c 1640 default:
1641 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1642 }
1643 break;
850c9265 1644
91977a1c 1645 case OPT_REGMEM:
1cd4a663 1646 if (is_stack_access(po, popr)) {
de50b98b 1647 stack_frame_access(po, popr, buf, buf_size,
3ebea2cf 1648 popr->name, cast, 1, is_lea);
91977a1c 1649 break;
1650 }
850c9265 1651
1652 strcpy(expr, popr->name);
1653 if (strchr(expr, '[')) {
1654 // special case: '[' can only be left for label[reg] form
1655 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1656 if (ret != 2)
1657 ferr(po, "parse failure for '%s'\n", expr);
a2c1d768 1658 if (tmp1[0] == '(') {
1659 // (off_4FFF50+3)[eax]
1660 p = strchr(tmp1 + 1, ')');
1661 if (p == NULL || p[1] != 0)
1662 ferr(po, "parse failure (2) for '%s'\n", expr);
1663 *p = 0;
1664 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1665 }
33c35af6 1666 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
850c9265 1667 }
1668
1669 // XXX: do we need more parsing?
1670 if (is_lea) {
1671 snprintf(buf, buf_size, "%s", expr);
1672 break;
1673 }
1674
a2c1d768 1675 snprintf(buf, buf_size, "%s(%s)",
1676 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
91977a1c 1677 break;
850c9265 1678
91977a1c 1679 case OPT_LABEL:
7aca4698 1680 name = check_label_read_ref(po, popr->name);
3ebea2cf 1681 if (cast[0] == 0 && popr->is_ptr)
1682 cast = "(u32)";
2b43685d 1683
850c9265 1684 if (is_lea)
7aca4698 1685 snprintf(buf, buf_size, "(u32)&%s", name);
2b43685d 1686 else if (popr->size_lt)
1687 snprintf(buf, buf_size, "%s%s%s%s", cast,
1688 lmod_cast_u_ptr(po, popr->lmod),
7aca4698 1689 popr->is_array ? "" : "&", name);
850c9265 1690 else
7aca4698 1691 snprintf(buf, buf_size, "%s%s%s", cast, name,
7ba45c34 1692 popr->is_array ? "[0]" : "");
850c9265 1693 break;
1694
1695 case OPT_OFFSET:
7aca4698 1696 name = check_label_read_ref(po, popr->name);
3ebea2cf 1697 if (cast[0] == 0)
1698 cast = "(u32)";
850c9265 1699 if (is_lea)
1700 ferr(po, "lea an offset?\n");
7aca4698 1701 snprintf(buf, buf_size, "%s&%s", cast, name);
91977a1c 1702 break;
850c9265 1703
91977a1c 1704 case OPT_CONST:
850c9265 1705 if (is_lea)
1706 ferr(po, "lea from const?\n");
1707
a2c1d768 1708 printf_number(tmp1, sizeof(tmp1), popr->val);
ddaf8bd7 1709 if (popr->val == 0 && strchr(cast, '*'))
1710 snprintf(buf, buf_size, "NULL");
1711 else
1712 snprintf(buf, buf_size, "%s%s",
1713 simplify_cast_num(cast, popr->val), tmp1);
91977a1c 1714 break;
850c9265 1715
91977a1c 1716 default:
1717 ferr(po, "invalid src type: %d\n", popr->type);
1718 }
1719
1720 return buf;
1721}
c36e914d 1722
de50b98b 1723// note: may set is_ptr (we find that out late for ebp frame..)
91977a1c 1724static char *out_dst_opr(char *buf, size_t buf_size,
1725 struct parsed_op *po, struct parsed_opr *popr)
1726{
1727 switch (popr->type) {
1728 case OPT_REG:
1729 switch (popr->lmod) {
1730 case OPLM_DWORD:
1731 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1732 break;
850c9265 1733 case OPLM_WORD:
1734 // ugh..
1735 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1736 break;
1737 case OPLM_BYTE:
1738 // ugh..
5101a5f9 1739 if (popr->name[1] == 'h') // XXX..
1740 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1741 else
1742 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
850c9265 1743 break;
91977a1c 1744 default:
1745 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1746 }
1747 break;
850c9265 1748
1749 case OPT_REGMEM:
1cd4a663 1750 if (is_stack_access(po, popr)) {
de50b98b 1751 stack_frame_access(po, popr, buf, buf_size,
3ebea2cf 1752 popr->name, "", 0, 0);
850c9265 1753 break;
1754 }
1755
3ebea2cf 1756 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
850c9265 1757
bfa4a6ee 1758 case OPT_LABEL:
2b43685d 1759 if (popr->size_mismatch)
1760 snprintf(buf, buf_size, "%s%s%s",
1761 lmod_cast_u_ptr(po, popr->lmod),
1762 popr->is_array ? "" : "&", popr->name);
1763 else
1764 snprintf(buf, buf_size, "%s%s", popr->name,
1765 popr->is_array ? "[0]" : "");
bfa4a6ee 1766 break;
1767
91977a1c 1768 default:
1769 ferr(po, "invalid dst type: %d\n", popr->type);
1770 }
1771
1772 return buf;
1773}
c36e914d 1774
3ebea2cf 1775static char *out_src_opr_u32(char *buf, size_t buf_size,
1776 struct parsed_op *po, struct parsed_opr *popr)
1777{
1778 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1779}
1780
91977a1c 1781static void out_test_for_cc(char *buf, size_t buf_size,
940e8e66 1782 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
69a3cdfc 1783 enum opr_lenmod lmod, const char *expr)
91977a1c 1784{
69a3cdfc 1785 const char *cast, *scast;
91977a1c 1786
69a3cdfc 1787 cast = lmod_cast_u(po, lmod);
1788 scast = lmod_cast_s(po, lmod);
1789
1790 switch (pfo) {
1791 case PFO_Z:
87bf6cec 1792 case PFO_BE: // CF=1||ZF=1; CF=0
850c9265 1793 snprintf(buf, buf_size, "(%s%s %s 0)",
940e8e66 1794 cast, expr, is_inv ? "!=" : "==");
91977a1c 1795 break;
850c9265 1796
5101a5f9 1797 case PFO_S:
1798 case PFO_L: // SF!=OF; OF=0
1799 snprintf(buf, buf_size, "(%s%s %s 0)",
940e8e66 1800 scast, expr, is_inv ? ">=" : "<");
5101a5f9 1801 break;
1802
87bf6cec 1803 case PFO_LE: // ZF=1||SF!=OF; OF=0
69a3cdfc 1804 snprintf(buf, buf_size, "(%s%s %s 0)",
940e8e66 1805 scast, expr, is_inv ? ">" : "<=");
850c9265 1806 break;
1807
91977a1c 1808 default:
69a3cdfc 1809 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
91977a1c 1810 }
1811}
c36e914d 1812
850c9265 1813static void out_cmp_for_cc(char *buf, size_t buf_size,
a2c1d768 1814 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
850c9265 1815{
a2c1d768 1816 const char *cast, *scast, *cast_use;
1817 char buf1[256], buf2[256];
1818 enum opr_lenmod lmod;
1819
1820 if (po->operand[0].lmod != po->operand[1].lmod)
1821 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1822 po->operand[0].lmod, po->operand[1].lmod);
1823 lmod = po->operand[0].lmod;
850c9265 1824
69a3cdfc 1825 cast = lmod_cast_u(po, lmod);
1826 scast = lmod_cast_s(po, lmod);
850c9265 1827
a2c1d768 1828 switch (pfo) {
1829 case PFO_C:
1830 case PFO_Z:
1831 case PFO_BE: // !a
1832 cast_use = cast;
1833 break;
1834
1835 case PFO_S:
1836 case PFO_L: // !ge
1837 case PFO_LE:
1838 cast_use = scast;
1839 break;
1840
1841 default:
1842 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1843 }
1844
1845 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1846 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1847
69a3cdfc 1848 switch (pfo) {
5101a5f9 1849 case PFO_C:
1850 // note: must be unsigned compare
a2c1d768 1851 snprintf(buf, buf_size, "(%s %s %s)",
1852 buf1, is_inv ? ">=" : "<", buf2);
5101a5f9 1853 break;
1854
69a3cdfc 1855 case PFO_Z:
a2c1d768 1856 snprintf(buf, buf_size, "(%s %s %s)",
1857 buf1, is_inv ? "!=" : "==", buf2);
850c9265 1858 break;
1859
5101a5f9 1860 case PFO_BE: // !a
850c9265 1861 // note: must be unsigned compare
a2c1d768 1862 snprintf(buf, buf_size, "(%s %s %s)",
1863 buf1, is_inv ? ">" : "<=", buf2);
1864
1865 // annoying case
1866 if (is_inv && lmod == OPLM_BYTE
1867 && po->operand[1].type == OPT_CONST
1868 && po->operand[1].val == 0xff)
1869 {
1870 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1871 snprintf(buf, buf_size, "(0)");
1872 }
5101a5f9 1873 break;
1874
1875 // note: must be signed compare
1876 case PFO_S:
1877 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
a2c1d768 1878 scast, buf1, buf2, is_inv ? ">=" : "<");
850c9265 1879 break;
1880
5101a5f9 1881 case PFO_L: // !ge
a2c1d768 1882 snprintf(buf, buf_size, "(%s %s %s)",
1883 buf1, is_inv ? ">=" : "<", buf2);
850c9265 1884 break;
1885
5101a5f9 1886 case PFO_LE:
a2c1d768 1887 snprintf(buf, buf_size, "(%s %s %s)",
1888 buf1, is_inv ? ">" : "<=", buf2);
5101a5f9 1889 break;
1890
850c9265 1891 default:
a2c1d768 1892 break;
850c9265 1893 }
1894}
1895
69a3cdfc 1896static void out_cmp_test(char *buf, size_t buf_size,
940e8e66 1897 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
69a3cdfc 1898{
1899 char buf1[256], buf2[256], buf3[256];
1900
1901 if (po->op == OP_TEST) {
1902 if (IS(opr_name(po, 0), opr_name(po, 1))) {
3ebea2cf 1903 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
69a3cdfc 1904 }
1905 else {
3ebea2cf 1906 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1907 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
69a3cdfc 1908 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1909 }
940e8e66 1910 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
69a3cdfc 1911 po->operand[0].lmod, buf3);
1912 }
1913 else if (po->op == OP_CMP) {
a2c1d768 1914 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
69a3cdfc 1915 }
1916 else
1917 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1918}
1919
850c9265 1920static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
91977a1c 1921 struct parsed_opr *popr2)
1922{
1923 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1924 ferr(po, "missing lmod for both operands\n");
1925
1926 if (popr1->lmod == OPLM_UNSPEC)
1927 popr1->lmod = popr2->lmod;
1928 else if (popr2->lmod == OPLM_UNSPEC)
1929 popr2->lmod = popr1->lmod;
a3684be1 1930 else if (popr1->lmod != popr2->lmod) {
1931 if (popr1->type_from_var) {
1932 popr1->size_mismatch = 1;
1933 if (popr1->lmod < popr2->lmod)
1934 popr1->size_lt = 1;
1935 popr1->lmod = popr2->lmod;
1936 }
1937 else if (popr2->type_from_var) {
1938 popr2->size_mismatch = 1;
1939 if (popr2->lmod < popr1->lmod)
1940 popr2->size_lt = 1;
1941 popr2->lmod = popr1->lmod;
1942 }
1943 else
1944 ferr(po, "conflicting lmods: %d vs %d\n",
1945 popr1->lmod, popr2->lmod);
1946 }
91977a1c 1947}
c36e914d 1948
850c9265 1949static const char *op_to_c(struct parsed_op *po)
1950{
1951 switch (po->op)
1952 {
1953 case OP_ADD:
5101a5f9 1954 case OP_ADC:
850c9265 1955 return "+";
1956 case OP_SUB:
5101a5f9 1957 case OP_SBB:
850c9265 1958 return "-";
1959 case OP_AND:
1960 return "&";
1961 case OP_OR:
1962 return "|";
1963 case OP_XOR:
1964 return "^";
1965 case OP_SHL:
1966 return "<<";
1967 case OP_SHR:
1968 return ">>";
1969 case OP_MUL:
1970 case OP_IMUL:
1971 return "*";
1972 default:
1973 ferr(po, "op_to_c was supplied with %d\n", po->op);
1974 }
1975}
1976
7ae48d73 1977static void op_set_clear_flag(struct parsed_op *po,
1978 enum op_flags flag_set, enum op_flags flag_clear)
d4e3b5db 1979{
7ae48d73 1980 po->flags |= flag_set;
1981 po->flags &= ~flag_clear;
d4e3b5db 1982}
1983
de50b98b 1984// last op in stream - unconditional branch or ret
1985#define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
092f64e1 1986 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
037f4971 1987 && ops[_i].op != OP_CALL))
de50b98b 1988
87bf6cec 1989static int scan_for_pop(int i, int opcnt, const char *reg,
d4e3b5db 1990 int magic, int depth, int *maxdepth, int do_flags)
850c9265 1991{
89ff3147 1992 const struct parsed_proto *pp;
87bf6cec 1993 struct parsed_op *po;
1994 int ret = 0;
4c45fa73 1995 int j;
87bf6cec 1996
850c9265 1997 for (; i < opcnt; i++) {
87bf6cec 1998 po = &ops[i];
1999 if (po->cc_scratch == magic)
2000 break; // already checked
2001 po->cc_scratch = magic;
2002
89ff3147 2003 if (po->flags & OPF_TAIL) {
2004 if (po->op == OP_CALL) {
2005 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
2006 if (pp != NULL && pp->is_noreturn)
2007 // no stack cleanup for noreturn
2008 return ret;
2009 }
87bf6cec 2010 return -1; // deadend
89ff3147 2011 }
87bf6cec 2012
1bafb621 2013 if ((po->flags & OPF_RMD)
de50b98b 2014 || (po->op == OP_PUSH && po->argnum != 0)) // arg push
850c9265 2015 continue;
2016
87bf6cec 2017 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
4c45fa73 2018 if (po->btj != NULL) {
2019 // jumptable
da87ae38 2020 for (j = 0; j < po->btj->count; j++) {
4c45fa73 2021 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
2022 depth, maxdepth, do_flags);
2023 if (ret < 0)
2024 return ret; // dead end
2025 }
da87ae38 2026 return ret;
4c45fa73 2027 }
2028
87bf6cec 2029 if (po->bt_i < 0) {
2030 ferr(po, "dead branch\n");
2031 return -1;
2032 }
850c9265 2033
5c024ef7 2034 if (po->flags & OPF_CJMP) {
d4e3b5db 2035 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2036 depth, maxdepth, do_flags);
87bf6cec 2037 if (ret < 0)
2038 return ret; // dead end
2039 }
2040 else {
2041 i = po->bt_i - 1;
2042 }
2043 continue;
2044 }
2045
d4e3b5db 2046 if ((po->op == OP_POP || po->op == OP_PUSH)
2047 && po->operand[0].type == OPT_REG
87bf6cec 2048 && IS(po->operand[0].name, reg))
2049 {
da87ae38 2050 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)) {
d4e3b5db 2051 depth++;
2052 if (depth > *maxdepth)
2053 *maxdepth = depth;
2054 if (do_flags)
7ae48d73 2055 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
d4e3b5db 2056 }
da87ae38 2057 else if (po->op == OP_POP) {
2058 if (depth == 0) {
2059 if (do_flags)
2060 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2061 return 1;
2062 }
2063 else {
2064 depth--;
2065 if (depth < 0) // should not happen
2066 ferr(po, "fail with depth\n");
2067 if (do_flags)
2068 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2069 }
d4e3b5db 2070 }
87bf6cec 2071 }
850c9265 2072 }
2073
87bf6cec 2074 return ret;
850c9265 2075}
2076
69a3cdfc 2077// scan for pop starting from 'ret' op (all paths)
d4e3b5db 2078static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2079 int flag_set)
850c9265 2080{
2081 int found = 0;
2082 int j;
2083
2084 for (; i < opcnt; i++) {
87bf6cec 2085 if (!(ops[i].flags & OPF_TAIL))
850c9265 2086 continue;
2087
2088 for (j = i - 1; j >= 0; j--) {
69a3cdfc 2089 if (ops[j].flags & OPF_RMD)
2090 continue;
2091 if (ops[j].flags & OPF_JMP)
850c9265 2092 return -1;
2093
2094 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2095 && IS(ops[j].operand[0].name, reg))
2096 {
2097 found = 1;
d4e3b5db 2098 ops[j].flags |= flag_set;
850c9265 2099 break;
2100 }
2101
2102 if (g_labels[j][0] != 0)
2103 return -1;
2104 }
2105 }
2106
2107 return found ? 0 : -1;
2108}
2109
591721d7 2110static void scan_propagate_df(int i, int opcnt)
2111{
2112 struct parsed_op *po = &ops[i];
2113 int j;
2114
2115 for (; i < opcnt; i++) {
2116 po = &ops[i];
2117 if (po->flags & OPF_DF)
2118 return; // already resolved
2119 po->flags |= OPF_DF;
2120
2121 if (po->op == OP_CALL)
2122 ferr(po, "call with DF set?\n");
2123
2124 if (po->flags & OPF_JMP) {
2125 if (po->btj != NULL) {
2126 // jumptable
2127 for (j = 0; j < po->btj->count; j++)
2128 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2129 return;
2130 }
2131
2132 if (po->bt_i < 0) {
2133 ferr(po, "dead branch\n");
2134 return;
2135 }
2136
5c024ef7 2137 if (po->flags & OPF_CJMP)
591721d7 2138 scan_propagate_df(po->bt_i, opcnt);
2139 else
2140 i = po->bt_i - 1;
2141 continue;
2142 }
2143
2144 if (po->flags & OPF_TAIL)
2145 break;
2146
2147 if (po->op == OP_CLD) {
2148 po->flags |= OPF_RMD;
2149 return;
2150 }
2151 }
2152
2153 ferr(po, "missing DF clear?\n");
2154}
2155
1cd4a663 2156// is operand 'opr' modified by parsed_op 'po'?
5101a5f9 2157static int is_opr_modified(const struct parsed_opr *opr,
69a3cdfc 2158 const struct parsed_op *po)
2159{
89ff3147 2160 int mask;
2161
69a3cdfc 2162 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2163 return 0;
2164
89ff3147 2165 if (opr->type == OPT_REG) {
2166 if (po->op == OP_CALL) {
2167 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2168 if ((1 << opr->reg) & mask)
2169 return 1;
2170 else
2171 return 0;
2172 }
2173
2174 if (po->operand[0].type == OPT_REG) {
2175 if (po->regmask_dst & (1 << opr->reg))
2176 return 1;
2177 else
2178 return 0;
2179 }
69a3cdfc 2180 }
2181
2182 return IS(po->operand[0].name, opr->name);
2183}
2184
5101a5f9 2185// is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2186static int is_any_opr_modified(const struct parsed_op *po_test,
89ff3147 2187 const struct parsed_op *po, int c_mode)
5101a5f9 2188{
89ff3147 2189 int mask;
5101a5f9 2190 int i;
2191
2192 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2193 return 0;
2194
de50b98b 2195 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2196 return 0;
2197
2198 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2199 return 1;
2200
2201 // in reality, it can wreck any register, but in decompiled C
2b43685d 2202 // version it can only overwrite eax or edx:eax
89ff3147 2203 mask = (1 << xAX) | (1 << xDX);
2204 if (!c_mode)
2205 mask |= 1 << xCX;
2206
de50b98b 2207 if (po->op == OP_CALL
89ff3147 2208 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
5101a5f9 2209 return 1;
2210
2211 for (i = 0; i < po_test->operand_cnt; i++)
2212 if (IS(po_test->operand[i].name, po->operand[0].name))
2213 return 1;
2214
2215 return 0;
2216}
2217
940e8e66 2218// scan for any po_test operand modification in range given
89ff3147 2219static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2220 int c_mode)
69a3cdfc 2221{
2b43685d 2222 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2223 return -1;
2224
69a3cdfc 2225 for (; i < opcnt; i++) {
89ff3147 2226 if (is_any_opr_modified(po_test, &ops[i], c_mode))
69a3cdfc 2227 return i;
2228 }
2229
2230 return -1;
2231}
2232
940e8e66 2233// scan for po_test operand[0] modification in range given
2234static int scan_for_mod_opr0(struct parsed_op *po_test,
2235 int i, int opcnt)
2236{
2237 for (; i < opcnt; i++) {
2238 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2239 return i;
2240 }
2241
2242 return -1;
2243}
2244
04f8a628 2245static int scan_for_flag_set(int i, int magic, int *branched,
2246 int *setters, int *setter_cnt)
69a3cdfc 2247{
04f8a628 2248 struct label_ref *lr;
2b43685d 2249 int ret;
de50b98b 2250
2251 while (i >= 0) {
04f8a628 2252 if (ops[i].cc_scratch == magic) {
2253 ferr(&ops[i], "%s looped\n", __func__);
2254 return -1;
2255 }
2256 ops[i].cc_scratch = magic;
2257
de50b98b 2258 if (g_labels[i][0] != 0) {
2b43685d 2259 *branched = 1;
04f8a628 2260
2261 lr = &g_label_refs[i];
2262 for (; lr->next; lr = lr->next) {
2263 ret = scan_for_flag_set(lr->i, magic,
2264 branched, setters, setter_cnt);
2265 if (ret < 0)
2266 return ret;
2267 }
2268
de50b98b 2269 if (i > 0 && LAST_OP(i - 1)) {
94d447fb 2270 i = lr->i;
de50b98b 2271 continue;
2272 }
04f8a628 2273 ret = scan_for_flag_set(lr->i, magic,
2274 branched, setters, setter_cnt);
2b43685d 2275 if (ret < 0)
2276 return ret;
de50b98b 2277 }
2278 i--;
2279
2b43685d 2280 if (ops[i].flags & OPF_FLAGS) {
2281 setters[*setter_cnt] = i;
2282 (*setter_cnt)++;
2283 return 0;
2284 }
69a3cdfc 2285
5c024ef7 2286 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
69a3cdfc 2287 return -1;
69a3cdfc 2288 }
2289
2290 return -1;
2291}
2292
5101a5f9 2293// scan back for cdq, if anything modifies edx, fail
2294static int scan_for_cdq_edx(int i)
2295{
cdfaeed7 2296 while (i >= 0) {
2297 if (g_labels[i][0] != 0) {
2298 if (g_label_refs[i].next != NULL)
2299 return -1;
2300 if (i > 0 && LAST_OP(i - 1)) {
2301 i = g_label_refs[i].i;
2302 continue;
2303 }
2304 return -1;
2305 }
2306 i--;
2307
5101a5f9 2308 if (ops[i].op == OP_CDQ)
2309 return i;
2310
2311 if (ops[i].regmask_dst & (1 << xDX))
2312 return -1;
5101a5f9 2313 }
2314
2315 return -1;
2316}
2317
64c59faf 2318static int scan_for_reg_clear(int i, int reg)
2319{
cdfaeed7 2320 while (i >= 0) {
2321 if (g_labels[i][0] != 0) {
2322 if (g_label_refs[i].next != NULL)
2323 return -1;
2324 if (i > 0 && LAST_OP(i - 1)) {
2325 i = g_label_refs[i].i;
2326 continue;
2327 }
2328 return -1;
2329 }
2330 i--;
2331
64c59faf 2332 if (ops[i].op == OP_XOR
2333 && ops[i].operand[0].lmod == OPLM_DWORD
2334 && ops[i].operand[0].reg == ops[i].operand[1].reg
2335 && ops[i].operand[0].reg == reg)
2336 return i;
2337
2338 if (ops[i].regmask_dst & (1 << reg))
2339 return -1;
64c59faf 2340 }
2341
2342 return -1;
2343}
2344
1bafb621 2345// scan for positive, constant esp adjust
4741fdfe 2346static int scan_for_esp_adjust(int i, int opcnt, int *adj,
2347 int *multipath)
1bafb621 2348{
7ba45c34 2349 struct parsed_op *po;
46411e6c 2350 int first_pop = -1;
4741fdfe 2351
2352 *adj = *multipath = 0;
7ba45c34 2353
1bafb621 2354 for (; i < opcnt; i++) {
7ba45c34 2355 po = &ops[i];
2356
a2c1d768 2357 if (g_labels[i][0] != 0)
4741fdfe 2358 *multipath = 1;
a2c1d768 2359
7ba45c34 2360 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2361 if (po->operand[1].type != OPT_CONST)
1bafb621 2362 ferr(&ops[i], "non-const esp adjust?\n");
7ba45c34 2363 *adj += po->operand[1].val;
1bafb621 2364 if (*adj & 3)
2365 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2366 return i;
2367 }
108e9fe3 2368 else if (po->op == OP_PUSH && !(po->flags & OPF_RMD)) {
5c024ef7 2369 //if (first_pop == -1)
2370 // first_pop = -2; // none
7ba45c34 2371 *adj -= lmod_bytes(po, po->operand[0].lmod);
46411e6c 2372 }
5c024ef7 2373 else if (po->op == OP_POP && !(po->flags & OPF_RMD)) {
108e9fe3 2374 // seems like msvc only uses 'pop ecx' for stack realignment..
2375 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2376 break;
5c024ef7 2377 if (first_pop == -1 && *adj >= 0)
46411e6c 2378 first_pop = i;
7ba45c34 2379 *adj += lmod_bytes(po, po->operand[0].lmod);
46411e6c 2380 }
e56ab892 2381 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
4741fdfe 2382 if (po->op == OP_JMP && po->btj == NULL) {
2383 i = po->bt_i - 1;
2384 continue;
2385 }
e56ab892 2386 if (po->op != OP_CALL)
a2c1d768 2387 break;
e56ab892 2388 if (po->operand[0].type != OPT_LABEL)
a2c1d768 2389 break;
092f64e1 2390 if (po->pp != NULL && po->pp->is_stdcall)
89ff3147 2391 break;
e56ab892 2392 }
a2c1d768 2393 }
7ba45c34 2394
108e9fe3 2395 if (first_pop >= 0) {
a2c1d768 2396 // probably 'pop ecx' was used..
46411e6c 2397 return first_pop;
1bafb621 2398 }
2399
2400 return -1;
2401}
2402
a3684be1 2403static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2404{
2405 struct parsed_op *po;
2406 int j;
2407
2408 if (i < 0)
2409 ferr(ops, "%s: followed bad branch?\n", __func__);
2410
2411 for (; i < opcnt; i++) {
2412 po = &ops[i];
2413 if (po->cc_scratch == magic)
2414 return;
2415 po->cc_scratch = magic;
2416 po->flags |= flags;
2417
2418 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2419 if (po->btj != NULL) {
2420 // jumptable
2421 for (j = 0; j < po->btj->count; j++)
2422 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2423 return;
2424 }
2425
2426 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
5c024ef7 2427 if (!(po->flags & OPF_CJMP))
a3684be1 2428 return;
2429 }
2430 if (po->flags & OPF_TAIL)
2431 return;
2432 }
2433}
2434
1cd4a663 2435static const struct parsed_proto *try_recover_pp(
da87ae38 2436 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
1cd4a663 2437{
2438 const struct parsed_proto *pp = NULL;
89ff3147 2439 char buf[256];
2440 char *p;
1cd4a663 2441
2442 // maybe an arg of g_func?
2443 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2444 {
2445 char ofs_reg[16] = { 0, };
2446 int arg, arg_s, arg_i;
2447 int stack_ra = 0;
2448 int offset = 0;
2449
2450 parse_stack_access(po, opr->name, ofs_reg,
037f4971 2451 &offset, &stack_ra, NULL, 0);
1cd4a663 2452 if (ofs_reg[0] != 0)
2453 ferr(po, "offset reg on arg access?\n");
da87ae38 2454 if (offset <= stack_ra) {
2455 // search who set the stack var instead
2456 if (search_instead != NULL)
2457 *search_instead = 1;
2458 return NULL;
2459 }
1cd4a663 2460
2461 arg_i = (offset - stack_ra - 4) / 4;
2462 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2463 if (g_func_pp->arg[arg].reg != NULL)
2464 continue;
2465 if (arg_s == arg_i)
2466 break;
2467 arg_s++;
2468 }
2469 if (arg == g_func_pp->argc)
2470 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2471
2472 pp = g_func_pp->arg[arg].fptr;
2473 if (pp == NULL)
46411e6c 2474 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
89ff3147 2475 check_func_pp(po, pp, "icall arg");
2476 }
2477 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2478 // label[index]
2479 p = strchr(opr->name + 1, '[');
2480 memcpy(buf, opr->name, p - opr->name);
2481 buf[p - opr->name] = 0;
2482 pp = proto_parse(g_fhdr, buf, 0);
1cd4a663 2483 }
2484 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
36595fd2 2485 pp = proto_parse(g_fhdr, opr->name, 0);
1cd4a663 2486 if (pp == NULL)
2487 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
89ff3147 2488 check_func_pp(po, pp, "reg-fptr ref");
1cd4a663 2489 }
2490
2491 return pp;
2492}
2493
da87ae38 2494static void scan_for_call_type(int i, const struct parsed_opr *opr,
89ff3147 2495 int magic, const struct parsed_proto **pp_found, int *multi)
1cd4a663 2496{
2497 const struct parsed_proto *pp = NULL;
2498 struct parsed_op *po;
2499 struct label_ref *lr;
2500
037f4971 2501 ops[i].cc_scratch = magic;
1cd4a663 2502
037f4971 2503 while (1) {
1cd4a663 2504 if (g_labels[i][0] != 0) {
2505 lr = &g_label_refs[i];
2506 for (; lr != NULL; lr = lr->next)
89ff3147 2507 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
46411e6c 2508 if (i > 0 && LAST_OP(i - 1))
2509 return;
1cd4a663 2510 }
037f4971 2511
1cd4a663 2512 i--;
037f4971 2513 if (i < 0)
2514 break;
2515
2516 if (ops[i].cc_scratch == magic)
2517 return;
2518 ops[i].cc_scratch = magic;
1cd4a663 2519
2520 if (!(ops[i].flags & OPF_DATA))
2521 continue;
2522 if (!is_opr_modified(opr, &ops[i]))
2523 continue;
2524 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2525 // most probably trashed by some processing
2526 *pp_found = NULL;
2527 return;
2528 }
2529
2530 opr = &ops[i].operand[1];
2531 if (opr->type != OPT_REG)
2532 break;
2533 }
2534
2535 po = (i >= 0) ? &ops[i] : ops;
2536
2537 if (i < 0) {
2538 // reached the top - can only be an arg-reg
2539 if (opr->type != OPT_REG)
2540 return;
2541
2542 for (i = 0; i < g_func_pp->argc; i++) {
2543 if (g_func_pp->arg[i].reg == NULL)
2544 continue;
2545 if (IS(opr->name, g_func_pp->arg[i].reg))
2546 break;
2547 }
2548 if (i == g_func_pp->argc)
2549 return;
2550 pp = g_func_pp->arg[i].fptr;
2551 if (pp == NULL)
46411e6c 2552 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2553 i + 1, g_func_pp->arg[i].reg);
89ff3147 2554 check_func_pp(po, pp, "icall reg-arg");
1cd4a663 2555 }
2556 else
da87ae38 2557 pp = try_recover_pp(po, opr, NULL);
1cd4a663 2558
89ff3147 2559 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
1cd4a663 2560 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2561 || (*pp_found)->is_stdcall != pp->is_stdcall
89ff3147 2562 || (*pp_found)->is_fptr != pp->is_fptr
1cd4a663 2563 || (*pp_found)->argc != pp->argc
2564 || (*pp_found)->argc_reg != pp->argc_reg
2565 || (*pp_found)->argc_stack != pp->argc_stack)
2566 {
2567 ferr(po, "icall: parsed_proto mismatch\n");
2568 }
89ff3147 2569 *multi = 1;
1cd4a663 2570 }
2571 if (pp != NULL)
2572 *pp_found = pp;
2573}
2574
89ff3147 2575static const struct parsed_proto *resolve_icall(int i, int opcnt,
2576 int *multi_src)
1cd4a663 2577{
2578 const struct parsed_proto *pp = NULL;
da87ae38 2579 int search_advice = 0;
1cd4a663 2580
89ff3147 2581 *multi_src = 0;
2582
1cd4a663 2583 switch (ops[i].operand[0].type) {
2584 case OPT_REGMEM:
2585 case OPT_LABEL:
2586 case OPT_OFFSET:
da87ae38 2587 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2588 if (!search_advice)
2589 break;
2590 // fallthrough
1cd4a663 2591 default:
89ff3147 2592 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2593 multi_src);
1cd4a663 2594 break;
2595 }
2596
2597 return pp;
2598}
2599
23fd0b11 2600// find an instruction that changed opr before i op
2601// *op_i must be set to -1
2602static int resolve_origin(int i, const struct parsed_opr *opr,
2603 int magic, int *op_i)
1f84f6b3 2604{
2605 struct label_ref *lr;
2606 int ret = 0;
2607
2608 ops[i].cc_scratch = magic;
2609
2610 while (1) {
2611 if (g_labels[i][0] != 0) {
2612 lr = &g_label_refs[i];
2613 for (; lr != NULL; lr = lr->next)
23fd0b11 2614 ret |= resolve_origin(lr->i, opr, magic, op_i);
1f84f6b3 2615 if (i > 0 && LAST_OP(i - 1))
2616 return ret;
2617 }
2618
2619 i--;
2620 if (i < 0)
2621 return -1;
2622
2623 if (ops[i].cc_scratch == magic)
2624 return 0;
2625 ops[i].cc_scratch = magic;
2626
2627 if (!(ops[i].flags & OPF_DATA))
2628 continue;
2629 if (!is_opr_modified(opr, &ops[i]))
2630 continue;
23fd0b11 2631
2632 if (*op_i >= 0) {
2633 if (*op_i == i)
2634 return 1;
2635 // XXX: could check if the other op does the same
2636 return -1;
2637 }
2638
2639 *op_i = i;
2640 return 1;
2641 }
2642}
2643
2644static int try_resolve_const(int i, const struct parsed_opr *opr,
2645 int magic, unsigned int *val)
2646{
2647 int s_i = -1;
2648 int ret = 0;
2649
2650 ret = resolve_origin(i, opr, magic, &s_i);
2651 if (ret == 1) {
2652 i = s_i;
1f84f6b3 2653 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
2654 return -1;
2655
2656 *val = ops[i].operand[1].val;
2657 return 1;
2658 }
23fd0b11 2659
2660 return -1;
1f84f6b3 2661}
2662
89ff3147 2663static int collect_call_args_r(struct parsed_op *po, int i,
75ad0378 2664 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
a3684be1 2665 int magic, int need_op_saving, int may_reuse)
e56ab892 2666{
2667 struct parsed_proto *pp_tmp;
2668 struct label_ref *lr;
2b43685d 2669 int need_to_save_current;
23fd0b11 2670 int save_args;
e56ab892 2671 int ret = 0;
23fd0b11 2672 char buf[32];
2673 int j, k;
e56ab892 2674
a3684be1 2675 if (i < 0) {
a2c1d768 2676 ferr(po, "dead label encountered\n");
a3684be1 2677 return -1;
2678 }
e56ab892 2679
de50b98b 2680 for (; arg < pp->argc; arg++)
e56ab892 2681 if (pp->arg[arg].reg == NULL)
2682 break;
a3684be1 2683 magic = (magic & 0xffffff) | (arg << 24);
e56ab892 2684
89ff3147 2685 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
e56ab892 2686 {
a3684be1 2687 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2688 if (ops[j].cc_scratch != magic) {
2689 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2690 pp->name);
2691 return -1;
2692 }
2693 // ok: have already been here
2694 return 0;
2695 }
2696 ops[j].cc_scratch = magic;
2697
a2c1d768 2698 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
e56ab892 2699 lr = &g_label_refs[j];
2700 if (lr->next != NULL)
2701 need_op_saving = 1;
a652aa9f 2702 for (; lr->next; lr = lr->next) {
5c024ef7 2703 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
a652aa9f 2704 may_reuse = 1;
89ff3147 2705 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
a3684be1 2706 arg, magic, need_op_saving, may_reuse);
2707 if (ret < 0)
2708 return ret;
a652aa9f 2709 }
e56ab892 2710
5c024ef7 2711 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
a652aa9f 2712 may_reuse = 1;
de50b98b 2713 if (j > 0 && LAST_OP(j - 1)) {
e56ab892 2714 // follow last branch in reverse
2715 j = lr->i;
2716 continue;
2717 }
2718 need_op_saving = 1;
89ff3147 2719 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
a3684be1 2720 arg, magic, need_op_saving, may_reuse);
2721 if (ret < 0)
2722 return ret;
e56ab892 2723 }
2724 j--;
2725
2726 if (ops[j].op == OP_CALL)
2727 {
89ff3147 2728 if (pp->is_unresolved)
2729 break;
2730
092f64e1 2731 pp_tmp = ops[j].pp;
e56ab892 2732 if (pp_tmp == NULL)
7ae48d73 2733 ferr(po, "arg collect hit unparsed call '%s'\n",
2734 ops[j].operand[0].name);
a652aa9f 2735 if (may_reuse && pp_tmp->argc_stack > 0)
de50b98b 2736 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2737 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
e56ab892 2738 }
de50b98b 2739 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) {
89ff3147 2740 if (pp->is_unresolved)
2741 break;
2742
de50b98b 2743 ferr(po, "arg collect %d/%d hit esp adjust\n",
2744 arg, pp->argc);
2745 }
2746 else if (ops[j].op == OP_POP) {
89ff3147 2747 if (pp->is_unresolved)
2748 break;
2749
de50b98b 2750 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2751 }
5c024ef7 2752 else if (ops[j].flags & OPF_CJMP)
de50b98b 2753 {
89ff3147 2754 if (pp->is_unresolved)
2755 break;
2756
a652aa9f 2757 may_reuse = 1;
de50b98b 2758 }
2759 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
e56ab892 2760 {
89ff3147 2761 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2762 break;
2763
e56ab892 2764 pp->arg[arg].datap = &ops[j];
2b43685d 2765 need_to_save_current = 0;
23fd0b11 2766 save_args = 0;
e56ab892 2767 if (!need_op_saving) {
89ff3147 2768 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2b43685d 2769 need_to_save_current = (ret >= 0);
e56ab892 2770 }
2b43685d 2771 if (need_op_saving || need_to_save_current) {
e56ab892 2772 // mark this push as one that needs operand saving
2773 ops[j].flags &= ~OPF_RMD;
de50b98b 2774 if (ops[j].argnum == 0) {
2775 ops[j].argnum = arg + 1;
23fd0b11 2776 save_args |= 1 << arg;
de50b98b 2777 }
2778 else if (ops[j].argnum < arg + 1)
2779 ferr(&ops[j], "argnum conflict (%d<%d) for '%s'\n",
2780 ops[j].argnum, arg + 1, pp->name);
e56ab892 2781 }
de50b98b 2782 else if (ops[j].argnum == 0)
e56ab892 2783 ops[j].flags |= OPF_RMD;
2784
a652aa9f 2785 // some PUSHes are reused by different calls on other branches,
de50b98b 2786 // but that can't happen if we didn't branch, so they
2787 // can be removed from future searches (handles nested calls)
a652aa9f 2788 if (!may_reuse)
de50b98b 2789 ops[j].flags |= OPF_FARG;
2790
da87ae38 2791 ops[j].flags &= ~OPF_RSAVE;
2792
23fd0b11 2793 // check for __VALIST
2794 if (!pp->is_unresolved && g_func_pp->is_vararg
2795 && IS(pp->arg[arg].type.name, "__VALIST"))
2796 {
2797 snprintf(buf, sizeof(buf), "arg_%X",
2798 g_func_pp->argc_stack * 4);
2799 k = -1;
2800 ret = resolve_origin(j, &ops[j].operand[0], magic + 1, &k);
2801 if (ret == 1 && k >= 0 && ops[k].op == OP_LEA
2802 && strstr(ops[k].operand[1].name, buf))
2803 {
2804 ops[k].flags |= OPF_RMD;
2805 ops[j].flags |= OPF_RMD | OPF_VAPUSH;
2806 save_args &= ~(1 << arg);
2807 }
2808 }
2809
2810 *save_arg_vars |= save_args;
2811
2812 // tracking reg usage
2813 if (!(ops[j].flags & OPF_VAPUSH)
2814 && ops[j].operand[0].type == OPT_REG)
2815 {
2816 *regmask |= 1 << ops[j].operand[0].reg;
2817 }
2818
89ff3147 2819 arg++;
2820 if (!pp->is_unresolved) {
2821 // next arg
2822 for (; arg < pp->argc; arg++)
2823 if (pp->arg[arg].reg == NULL)
2824 break;
2825 }
a3684be1 2826 magic = (magic & 0xffffff) | (arg << 24);
e56ab892 2827 }
2828 }
2829
2830 if (arg < pp->argc) {
2831 ferr(po, "arg collect failed for '%s': %d/%d\n",
2832 pp->name, arg, pp->argc);
89ff3147 2833 return -1;
e56ab892 2834 }
89ff3147 2835
2836 return arg;
2837}
2838
2839static int collect_call_args(struct parsed_op *po, int i,
2840 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2841 int magic)
2842{
2843 int ret;
2844 int a;
2845
2846 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2847 0, magic, 0, 0);
2848 if (ret < 0)
2849 return ret;
2850
2851 if (pp->is_unresolved) {
2852 pp->argc += ret;
2853 pp->argc_stack += ret;
2854 for (a = 0; a < pp->argc; a++)
2855 if (pp->arg[a].type.name == NULL)
2856 pp->arg[a].type.name = strdup("int");
2857 }
2858
e56ab892 2859 return ret;
2860}
2861
037f4971 2862// early check for tail call or branch back
2863static int is_like_tailjmp(int j)
2864{
2865 if (!(ops[j].flags & OPF_JMP))
2866 return 0;
2867
2868 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2869 // probably local branch back..
2870 return 1;
2871 if (ops[j].op == OP_CALL)
2872 // probably noreturn call..
2873 return 1;
2874
2875 return 0;
2876}
2877
89ff3147 2878static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2879{
2880 int i;
2881
2882 for (i = 0; i < pp->argc; i++)
2883 if (pp->arg[i].reg == NULL)
2884 break;
2885
2886 if (pp->argc_stack)
2887 memmove(&pp->arg[i + 1], &pp->arg[i],
2888 sizeof(pp->arg[0]) * pp->argc_stack);
2889 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2890 pp->arg[i].reg = strdup(reg);
2891 pp->arg[i].type.name = strdup("int");
2892 pp->argc++;
2893 pp->argc_reg++;
2894}
2895
4c45fa73 2896static void add_label_ref(struct label_ref *lr, int op_i)
2897{
2898 struct label_ref *lr_new;
2899
2900 if (lr->i == -1) {
2901 lr->i = op_i;
2902 return;
2903 }
2904
2905 lr_new = calloc(1, sizeof(*lr_new));
2906 lr_new->i = op_i;
a652aa9f 2907 lr_new->next = lr->next;
4c45fa73 2908 lr->next = lr_new;
2909}
2910
04f8a628 2911static void output_std_flags(FILE *fout, struct parsed_op *po,
2912 int *pfomask, const char *dst_opr_text)
2913{
2914 if (*pfomask & (1 << PFO_Z)) {
2915 fprintf(fout, "\n cond_z = (%s%s == 0);",
2916 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2917 *pfomask &= ~(1 << PFO_Z);
2918 }
2919 if (*pfomask & (1 << PFO_S)) {
2920 fprintf(fout, "\n cond_s = (%s%s < 0);",
2921 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2922 *pfomask &= ~(1 << PFO_S);
2923 }
2924}
2925
c0050df6 2926static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
2927 int is_noreturn)
2928{
2929 if (pp->is_fastcall)
2930 fprintf(fout, "__fastcall ");
2931 else if (pp->is_stdcall && pp->argc_reg == 0)
2932 fprintf(fout, "__stdcall ");
2933 if (pp->is_noreturn || is_noreturn)
2934 fprintf(fout, "noreturn ");
2935}
2936
91977a1c 2937static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
2938{
69a3cdfc 2939 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
850c9265 2940 struct parsed_opr *last_arith_dst = NULL;
3ebea2cf 2941 char buf1[256], buf2[256], buf3[256], cast[64];
bd96f656 2942 const struct parsed_proto *pp_c;
ddaf8bd7 2943 struct parsed_proto *pp, *pp_tmp;
4c45fa73 2944 struct parsed_data *pd;
91977a1c 2945 const char *tmpname;
1f84f6b3 2946 unsigned int uval;
69a3cdfc 2947 int save_arg_vars = 0;
cb090db0 2948 int cond_vars = 0;
108e9fe3 2949 int need_tmp_var = 0;
2fe80fdb 2950 int need_tmp64 = 0;
91977a1c 2951 int had_decl = 0;
3ebea2cf 2952 int label_pending = 0;
d4e3b5db 2953 int regmask_save = 0;
91977a1c 2954 int regmask_arg = 0;
a3684be1 2955 int regmask_now = 0;
ddaf8bd7 2956 int regmask_init = 0;
91977a1c 2957 int regmask = 0;
940e8e66 2958 int pfomask = 0;
64c59faf 2959 int found = 0;
d4e3b5db 2960 int depth = 0;
91977a1c 2961 int no_output;
4c45fa73 2962 int i, j, l;
91977a1c 2963 int arg;
91977a1c 2964 int reg;
2965 int ret;
2966
1bafb621 2967 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
a2c1d768 2968 g_stack_frame_used = 0;
91977a1c 2969
36595fd2 2970 g_func_pp = proto_parse(fhdr, funcn, 0);
bd96f656 2971 if (g_func_pp == NULL)
91977a1c 2972 ferr(ops, "proto_parse failed for '%s'\n", funcn);
2973
bd96f656 2974 for (i = 0; i < g_func_pp->argc; i++) {
89ff3147 2975 if (g_func_pp->arg[i].reg != NULL) {
2976 reg = char_array_i(regs_r32,
2977 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
2978 if (reg < 0)
2979 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
2980 regmask_arg |= 1 << reg;
2981 }
91977a1c 2982 }
91977a1c 2983
2984 // pass1:
1bafb621 2985 // - handle ebp/esp frame, remove ops related to it
91977a1c 2986 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2987 && ops[1].op == OP_MOV
2988 && IS(opr_name(&ops[1], 0), "ebp")
2989 && IS(opr_name(&ops[1], 1), "esp"))
2990 {
87bf6cec 2991 int ecx_push = 0;
2992
91977a1c 2993 g_bp_frame = 1;
69a3cdfc 2994 ops[0].flags |= OPF_RMD;
2995 ops[1].flags |= OPF_RMD;
4c45fa73 2996 i = 2;
91977a1c 2997
2998 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
1bafb621 2999 g_stack_fsz = opr_const(&ops[2], 1);
69a3cdfc 3000 ops[2].flags |= OPF_RMD;
4c45fa73 3001 i++;
91977a1c 3002 }
87bf6cec 3003 else {
3004 // another way msvc builds stack frame..
3005 i = 2;
3006 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
1bafb621 3007 g_stack_fsz += 4;
87bf6cec 3008 ops[i].flags |= OPF_RMD;
3009 ecx_push++;
3010 i++;
3011 }
4c45fa73 3012 // and another way..
3013 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
3014 && ops[i].operand[1].type == OPT_CONST
3015 && ops[i + 1].op == OP_CALL
3016 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
3017 {
3018 g_stack_fsz += ops[i].operand[1].val;
3019 ops[i].flags |= OPF_RMD;
3020 i++;
3021 ops[i].flags |= OPF_RMD;
3022 i++;
3023 }
87bf6cec 3024 }
91977a1c 3025
64c59faf 3026 found = 0;
91977a1c 3027 do {
3028 for (; i < opcnt; i++)
3029 if (ops[i].op == OP_RET)
3030 break;
2fe80fdb 3031 j = i - 1;
3032 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
037f4971 3033 if (found && is_like_tailjmp(j))
2fe80fdb 3034 break;
2fe80fdb 3035 j--;
3036 }
64c59faf 3037
2fe80fdb 3038 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
3039 || ops[j].op == OP_LEAVE)
591721d7 3040 {
2fe80fdb 3041 ops[j].flags |= OPF_RMD;
591721d7 3042 }
e56ab892 3043 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2fe80fdb 3044 ferr(&ops[j], "'pop ebp' expected\n");
91977a1c 3045
1bafb621 3046 if (g_stack_fsz != 0) {
2fe80fdb 3047 if (ops[j - 1].op == OP_MOV
3048 && IS(opr_name(&ops[j - 1], 0), "esp")
3049 && IS(opr_name(&ops[j - 1], 1), "ebp"))
91977a1c 3050 {
2fe80fdb 3051 ops[j - 1].flags |= OPF_RMD;
91977a1c 3052 }
2fe80fdb 3053 else if (ops[j].op != OP_LEAVE
591721d7 3054 && !(g_ida_func_attr & IDAFA_NORETURN))
3055 {
2fe80fdb 3056 ferr(&ops[j - 1], "esp restore expected\n");
591721d7 3057 }
87bf6cec 3058
2fe80fdb 3059 if (ecx_push && ops[j - 2].op == OP_POP
3060 && IS(opr_name(&ops[j - 2], 0), "ecx"))
87bf6cec 3061 {
2fe80fdb 3062 ferr(&ops[j - 2], "unexpected ecx pop\n");
87bf6cec 3063 }
91977a1c 3064 }
87bf6cec 3065
64c59faf 3066 found = 1;
91977a1c 3067 i++;
3068 } while (i < opcnt);
3069 }
1bafb621 3070 else {
3071 for (i = 0; i < opcnt; i++) {
3072 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
3073 break;
3074 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
3075 && ops[i].operand[1].type == OPT_CONST)
3076 {
3077 g_sp_frame = 1;
3078 break;
3079 }
3080 }
3081
037f4971 3082 found = 0;
1bafb621 3083 if (g_sp_frame)
3084 {
3085 g_stack_fsz = ops[i].operand[1].val;
3086 ops[i].flags |= OPF_RMD;
3087
3088 i++;
3089 do {
3090 for (; i < opcnt; i++)
3091 if (ops[i].op == OP_RET)
3092 break;
037f4971 3093 j = i - 1;
3094 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3095 if (found && is_like_tailjmp(j))
3096 break;
3097 j--;
3098 }
1bafb621 3099
037f4971 3100 if (ops[j].op != OP_ADD
3101 || !IS(opr_name(&ops[j], 0), "esp")
3102 || ops[j].operand[1].type != OPT_CONST
3103 || ops[j].operand[1].val != g_stack_fsz)
3104 ferr(&ops[j], "'add esp' expected\n");
3105 ops[j].flags |= OPF_RMD;
3106
3107 found = 1;
1bafb621 3108 i++;
3109 } while (i < opcnt);
3110 }
3111 }
91977a1c 3112
3113 // pass2:
7ae48d73 3114 // - parse calls with labels
87bf6cec 3115 // - resolve all branches
de50b98b 3116 for (i = 0; i < opcnt; i++)
3117 {
87bf6cec 3118 po = &ops[i];
3119 po->bt_i = -1;
4c45fa73 3120 po->btj = NULL;
87bf6cec 3121
7ae48d73 3122 if (po->flags & OPF_RMD)
3123 continue;
3124
3125 if (po->op == OP_CALL) {
ddaf8bd7 3126 pp = NULL;
3127
7ae48d73 3128 if (po->operand[0].type == OPT_LABEL) {
3129 tmpname = opr_name(po, 0);
46411e6c 3130 if (IS_START(tmpname, "loc_"))
3131 ferr(po, "call to loc_*\n");
36595fd2 3132 pp_c = proto_parse(fhdr, tmpname, 0);
7ae48d73 3133 if (pp_c == NULL)
3134 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
da87ae38 3135
7ae48d73 3136 pp = proto_clone(pp_c);
3137 my_assert_not(pp, NULL);
ddaf8bd7 3138 }
3139 else if (po->datap != NULL) {
3140 pp = calloc(1, sizeof(*pp));
3141 my_assert_not(pp, NULL);
3142
3143 ret = parse_protostr(po->datap, pp);
3144 if (ret < 0)
3145 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3146 free(po->datap);
3147 po->datap = NULL;
3148 }
3149
3150 if (pp != NULL) {
3151 if (pp->is_fptr)
3152 check_func_pp(po, pp, "fptr var call");
3153 if (pp->is_noreturn)
3154 po->flags |= OPF_TAIL;
7ae48d73 3155 }
092f64e1 3156 po->pp = pp;
7ae48d73 3157 continue;
3158 }
3159
3160 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
87bf6cec 3161 continue;
3162
4c45fa73 3163 if (po->operand[0].type == OPT_REGMEM) {
3164 char *p = strchr(po->operand[0].name, '[');
3165 if (p == NULL)
89ff3147 3166 goto tailcall;
4c45fa73 3167 ret = p - po->operand[0].name;
3168 strncpy(buf1, po->operand[0].name, ret);
3169 buf1[ret] = 0;
3170
3171 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3172 if (IS(g_func_pd[j].label, buf1)) {
3173 pd = &g_func_pd[j];
3174 break;
3175 }
3176 }
3177 if (pd == NULL)
840257f6 3178 //ferr(po, "label '%s' not parsed?\n", buf1);
3179 goto tailcall;
4c45fa73 3180 if (pd->type != OPT_OFFSET)
3181 ferr(po, "label '%s' with non-offset data?\n", buf1);
3182
3183 // find all labels, link
3184 for (j = 0; j < pd->count; j++) {
3185 for (l = 0; l < opcnt; l++) {
3186 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
3187 add_label_ref(&g_label_refs[l], i);
3188 pd->d[j].bt_i = l;
3189 break;
3190 }
3191 }
3192 }
3193
3194 po->btj = pd;
3195 continue;
3196 }
3197
3198 for (l = 0; l < opcnt; l++) {
3199 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
092f64e1 3200 if (l == i + 1 && po->op == OP_JMP) {
3201 // yet another alignment type..
3202 po->flags |= OPF_RMD;
3203 break;
3204 }
4c45fa73 3205 add_label_ref(&g_label_refs[l], i);
3206 po->bt_i = l;
87bf6cec 3207 break;
3208 }
3209 }
3210
092f64e1 3211 if (po->bt_i != -1 || (po->flags & OPF_RMD))
4c45fa73 3212 continue;
3213
840257f6 3214 if (po->operand[0].type == OPT_LABEL)
87bf6cec 3215 // assume tail call
840257f6 3216 goto tailcall;
4c45fa73 3217
3218 ferr(po, "unhandled branch\n");
840257f6 3219
3220tailcall:
3221 po->op = OP_CALL;
3222 po->flags |= OPF_TAIL;
2fe80fdb 3223 if (i > 0 && ops[i - 1].op == OP_POP)
3224 po->flags |= OPF_ATAIL;
7ae48d73 3225 i--; // reprocess
87bf6cec 3226 }
3227
3228 // pass3:
a2c1d768 3229 // - remove dead labels
91977a1c 3230 // - process calls
1bafb621 3231 for (i = 0; i < opcnt; i++)
3232 {
a2c1d768 3233 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
3234 g_labels[i][0] = 0;
3235
69a3cdfc 3236 po = &ops[i];
3237 if (po->flags & OPF_RMD)
91977a1c 3238 continue;
850c9265 3239
d4e3b5db 3240 if (po->op == OP_CALL)
69a3cdfc 3241 {
1bafb621 3242 tmpname = opr_name(po, 0);
092f64e1 3243 pp = po->pp;
7ae48d73 3244 if (pp == NULL)
1bafb621 3245 {
7ae48d73 3246 // indirect call
89ff3147 3247 pp_c = resolve_icall(i, opcnt, &l);
3248 if (pp_c != NULL) {
fe18709a 3249 if (!pp_c->is_func && !pp_c->is_fptr)
3250 ferr(po, "call to non-func: %s\n", pp_c->name);
1cd4a663 3251 pp = proto_clone(pp_c);
89ff3147 3252 my_assert_not(pp, NULL);
3253 if (l)
3254 // not resolved just to single func
3255 pp->is_fptr = 1;
1f84f6b3 3256
3257 switch (po->operand[0].type) {
3258 case OPT_REG:
037f4971 3259 // we resolved this call and no longer need the register
3260 po->regmask_src &= ~(1 << po->operand[0].reg);
1f84f6b3 3261 break;
3262 case OPT_REGMEM:
3263 pp->is_fptr = 1;
3264 break;
3265 default:
3266 break;
3267 }
89ff3147 3268 }
1cd4a663 3269 if (pp == NULL) {
3270 pp = calloc(1, sizeof(*pp));
3271 my_assert_not(pp, NULL);
89ff3147 3272 pp->is_fptr = 1;
4741fdfe 3273 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
89ff3147 3274 if (ret < 0) {
3275 if (!g_allow_regfunc)
3276 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3277 pp->is_unresolved = 1;
3278 j = 0;
3279 }
1cd4a663 3280 j /= 4;
3281 if (j > ARRAY_SIZE(pp->arg))
89ff3147 3282 ferr(po, "esp adjust too large: %d\n", j);
1cd4a663 3283 pp->ret_type.name = strdup("int");
3284 pp->argc = pp->argc_stack = j;
3285 for (arg = 0; arg < pp->argc; arg++)
3286 pp->arg[arg].type.name = strdup("int");
3287 }
092f64e1 3288 po->pp = pp;
1bafb621 3289 }
3290
7ba45c34 3291 // look for and make use of esp adjust
3292 ret = -1;
e56ab892 3293 if (!pp->is_stdcall && pp->argc_stack > 0)
4741fdfe 3294 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
1bafb621 3295 if (ret >= 0) {
7ba45c34 3296 if (pp->is_vararg) {
3297 if (j / 4 < pp->argc_stack)
3298 ferr(po, "esp adjust is too small: %x < %x\n",
3299 j, pp->argc_stack * 4);
3300 // modify pp to make it have varargs as normal args
3301 arg = pp->argc;
3302 pp->argc += j / 4 - pp->argc_stack;
3303 for (; arg < pp->argc; arg++) {
3ebea2cf 3304 pp->arg[arg].type.name = strdup("int");
7ba45c34 3305 pp->argc_stack++;
3306 }
3307 if (pp->argc > ARRAY_SIZE(pp->arg))
e56ab892 3308 ferr(po, "too many args for '%s'\n", tmpname);
7ba45c34 3309 }
1bafb621 3310 if (pp->argc_stack != j / 4)
e56ab892 3311 ferr(po, "stack tracking failed for '%s': %x %x\n",
3312 tmpname, pp->argc_stack * 4, j);
7ba45c34 3313
1bafb621 3314 ops[ret].flags |= OPF_RMD;
591721d7 3315 if (ops[ret].op == OP_POP && j > 4) {
3316 // deal with multi-pop stack adjust
3317 j = pp->argc_stack;
3318 while (ops[ret].op == OP_POP && j > 0 && ret < opcnt) {
3319 ops[ret].flags |= OPF_RMD;
3320 j--;
3321 ret++;
3322 }
3323 }
4741fdfe 3324 else if (!l) {
591721d7 3325 // a bit of a hack, but deals with use of
3326 // single adj for multiple calls
3327 ops[ret].operand[1].val -= j;
3328 }
1bafb621 3329 }
7ba45c34 3330 else if (pp->is_vararg)
3331 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3332 pp->name);
91977a1c 3333
2fe80fdb 3334 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
89ff3147 3335 // since we know the args, collect them
3336 collect_call_args(po, i, pp, &regmask, &save_arg_vars,
3337 i + opcnt * 2);
3338 }
2b43685d 3339
3340 if (strstr(pp->ret_type.name, "int64"))
2fe80fdb 3341 need_tmp64 = 1;
91977a1c 3342 }
d4e3b5db 3343 }
3344
3345 // pass4:
3346 // - find POPs for PUSHes, rm both
591721d7 3347 // - scan for STD/CLD, propagate DF
d4e3b5db 3348 // - scan for all used registers
3349 // - find flag set ops for their users
89ff3147 3350 // - do unreselved calls
1bafb621 3351 // - declare indirect functions
d4e3b5db 3352 for (i = 0; i < opcnt; i++) {
3353 po = &ops[i];
3354 if (po->flags & OPF_RMD)
3355 continue;
3356
7ae48d73 3357 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3358 reg = po->operand[0].reg;
3359 if (!(regmask & (1 << reg)))
3360 // not a reg save after all, rerun scan_for_pop
3361 po->flags &= ~OPF_RSAVE;
3362 else
3363 regmask_save |= 1 << reg;
3364 }
3365
5c024ef7 3366 if (po->op == OP_PUSH && po->argnum == 0
1f84f6b3 3367 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
d4e3b5db 3368 {
5c024ef7 3369 if (po->operand[0].type == OPT_REG)
3370 {
3371 reg = po->operand[0].reg;
3372 if (reg < 0)
3373 ferr(po, "reg not set for push?\n");
3374
3375 depth = 0;
3376 ret = scan_for_pop(i + 1, opcnt,
3377 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3378 if (ret == 1) {
3379 if (depth > 1)
3380 ferr(po, "too much depth: %d\n", depth);
3381
3382 po->flags |= OPF_RMD;
3383 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3384 i + opcnt * 4, 0, &depth, 1);
3385 continue;
3386 }
3387 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3388 if (ret == 0) {
3389 arg = OPF_RMD;
3390 if (regmask & (1 << reg)) {
3391 if (regmask_save & (1 << reg))
3392 ferr(po, "%s already saved?\n", po->operand[0].name);
3393 arg = OPF_RSAVE;
3394 }
3395 po->flags |= arg;
3396 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3397 continue;
3398 }
d4e3b5db 3399 }
5c024ef7 3400 else if (po->operand[0].type == OPT_CONST) {
3401 for (j = i + 1; j < opcnt; j++) {
3402 if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
3403 || ops[j].op == OP_PUSH || g_labels[i][0] != 0)
3404 {
3405 break;
3406 }
3407
3408 if (!(ops[j].flags & OPF_RMD) && ops[j].op == OP_POP)
3409 {
3410 po->flags |= OPF_RMD;
3411 ops[j].datap = po;
3412 break;
3413 }
d4e3b5db 3414 }
d4e3b5db 3415 }
3416 }
3417
591721d7 3418 if (po->op == OP_STD) {
3419 po->flags |= OPF_DF | OPF_RMD;
3420 scan_propagate_df(i + 1, opcnt);
3421 }
3422
a3684be1 3423 regmask_now = po->regmask_src | po->regmask_dst;
3424 if (regmask_now & (1 << xBP)) {
3425 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3426 if (po->regmask_dst & (1 << xBP))
3427 // compiler decided to drop bp frame and use ebp as scratch
037f4971 3428 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
a3684be1 3429 else
3430 regmask_now &= ~(1 << xBP);
3431 }
3432 }
3433
3434 regmask |= regmask_now;
d4e3b5db 3435
3436 if (po->flags & OPF_CC)
3437 {
2b43685d 3438 int setters[16], cnt = 0, branched = 0;
3439
04f8a628 3440 ret = scan_for_flag_set(i, i + opcnt * 6,
3441 &branched, setters, &cnt);
2b43685d 3442 if (ret < 0 || cnt <= 0)
3443 ferr(po, "unable to trace flag setter(s)\n");
3444 if (cnt > ARRAY_SIZE(setters))
3445 ferr(po, "too many flag setters\n");
d4e3b5db 3446
2b43685d 3447 for (j = 0; j < cnt; j++)
3448 {
3449 tmp_op = &ops[setters[j]]; // flag setter
3450 pfomask = 0;
3451
3452 // to get nicer code, we try to delay test and cmp;
3453 // if we can't because of operand modification, or if we
591721d7 3454 // have arith op, or branch, make it calculate flags explicitly
3455 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
3456 {
89ff3147 3457 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
092f64e1 3458 pfomask = 1 << po->pfo;
2b43685d 3459 }
4741fdfe 3460 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
092f64e1 3461 pfomask = 1 << po->pfo;
591721d7 3462 }
2b43685d 3463 else {
04f8a628 3464 // see if we'll be able to handle based on op result
3465 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
092f64e1 3466 && po->pfo != PFO_Z && po->pfo != PFO_S
3467 && po->pfo != PFO_P)
04f8a628 3468 || branched
2b43685d 3469 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
092f64e1 3470 {
3471 pfomask = 1 << po->pfo;
3472 }
2fe80fdb 3473
c8dbc5be 3474 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
3475 propagate_lmod(tmp_op, &tmp_op->operand[0],
3476 &tmp_op->operand[1]);
3477 if (tmp_op->operand[0].lmod == OPLM_DWORD)
3478 need_tmp64 = 1;
3479 }
2b43685d 3480 }
3481 if (pfomask) {
3482 tmp_op->pfomask |= pfomask;
cb090db0 3483 cond_vars |= pfomask;
2b43685d 3484 }
04f8a628 3485 // note: may overwrite, currently not a problem
3486 po->datap = tmp_op;
d4e3b5db 3487 }
3488
cb090db0 3489 if (po->op == OP_RCL || po->op == OP_RCR
3490 || po->op == OP_ADC || po->op == OP_SBB)
3491 cond_vars |= 1 << PFO_C;
d4e3b5db 3492 }
092f64e1 3493
3494 if (po->op == OP_CMPS || po->op == OP_SCAS) {
cb090db0 3495 cond_vars |= 1 << PFO_Z;
591721d7 3496 }
87bf6cec 3497 else if (po->op == OP_MUL
3498 || (po->op == OP_IMUL && po->operand_cnt == 1))
3499 {
c8dbc5be 3500 if (po->operand[0].lmod == OPLM_DWORD)
3501 need_tmp64 = 1;
87bf6cec 3502 }
89ff3147 3503 else if (po->op == OP_CALL) {
092f64e1 3504 pp = po->pp;
89ff3147 3505 if (pp == NULL)
3506 ferr(po, "NULL pp\n");
3507
3508 if (pp->is_unresolved) {
ddaf8bd7 3509 int regmask_stack = 0;
b74c31e3 3510 collect_call_args(po, i, pp, &regmask, &save_arg_vars,
89ff3147 3511 i + opcnt * 2);
3512
b74c31e3 3513 // this is pretty rough guess:
3514 // see ecx and edx were pushed (and not their saved versions)
3515 for (arg = 0; arg < pp->argc; arg++) {
3516 if (pp->arg[arg].reg != NULL)
3517 continue;
3518
3519 tmp_op = pp->arg[arg].datap;
3520 if (tmp_op == NULL)
3521 ferr(po, "parsed_op missing for arg%d\n", arg);
3522 if (tmp_op->argnum == 0 && tmp_op->operand[0].type == OPT_REG)
3523 regmask_stack |= 1 << tmp_op->operand[0].reg;
3524 }
3525
ddaf8bd7 3526 if (!((regmask_stack & (1 << xCX))
3527 && (regmask_stack & (1 << xDX))))
89ff3147 3528 {
3529 if (pp->argc_stack != 0
c0050df6 3530 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
89ff3147 3531 {
3532 pp_insert_reg_arg(pp, "ecx");
c0050df6 3533 pp->is_fastcall = 1;
ddaf8bd7 3534 regmask_init |= 1 << xCX;
89ff3147 3535 regmask |= 1 << xCX;
3536 }
3537 if (pp->argc_stack != 0
3538 || ((regmask | regmask_arg) & (1 << xDX)))
3539 {
3540 pp_insert_reg_arg(pp, "edx");
ddaf8bd7 3541 regmask_init |= 1 << xDX;
89ff3147 3542 regmask |= 1 << xDX;
3543 }
3544 }
c0050df6 3545
3546 // note: __cdecl doesn't fall into is_unresolved category
3547 if (pp->argc_stack > 0)
3548 pp->is_stdcall = 1;
ddaf8bd7 3549 }
3550
3551 for (arg = 0; arg < pp->argc; arg++) {
3552 if (pp->arg[arg].reg != NULL) {
3553 reg = char_array_i(regs_r32,
3554 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3555 if (reg < 0)
3556 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3557 if (!(regmask & (1 << reg))) {
3558 regmask_init |= 1 << reg;
3559 regmask |= 1 << reg;
3560 }
3561 }
89ff3147 3562 }
1bafb621 3563 }
27ebfaed 3564 else if (po->op == OP_MOV && po->operand[0].pp != NULL
3565 && po->operand[1].pp != NULL)
3566 {
3567 // <var> = offset <something>
3568 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
3569 && !IS_START(po->operand[1].name, "off_"))
3570 {
3571 if (!po->operand[0].pp->is_fptr)
3572 ferr(po, "%s not declared as fptr when it should be\n",
3573 po->operand[0].name);
3574 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
3575 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
3576 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
3577 fnote(po, "var: %s\n", buf1);
3578 fnote(po, "func: %s\n", buf2);
3579 ferr(po, "^ mismatch\n");
3580 }
3581 }
3582 }
75ad0378 3583 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3584 regmask |= 1 << xAX;
cb090db0 3585 else if (po->op == OP_DIV || po->op == OP_IDIV) {
3586 // 32bit division is common, look for it
3587 if (po->op == OP_DIV)
3588 ret = scan_for_reg_clear(i, xDX);
3589 else
3590 ret = scan_for_cdq_edx(i);
3591 if (ret >= 0)
3592 po->flags |= OPF_32BIT;
3593 else
3594 need_tmp64 = 1;
3595 }
037f4971 3596 else if (po->op == OP_CLD)
3597 po->flags |= OPF_RMD;
cb090db0 3598
3599 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
3600 need_tmp_var = 1;
3601 }
91977a1c 3602 }
3603
da87ae38 3604 // pass4:
3605 // - confirm regmask_save, it might have been reduced
3606 if (regmask_save != 0)
3607 {
3608 regmask_save = 0;
3609 for (i = 0; i < opcnt; i++) {
3610 po = &ops[i];
3611 if (po->flags & OPF_RMD)
3612 continue;
3613
3614 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
3615 regmask_save |= 1 << po->operand[0].reg;
3616 }
3617 }
3618
60fe410c 3619 // output starts here
3620
3621 // define userstack size
3622 if (g_func_pp->is_userstack) {
3623 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
3624 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
3625 fprintf(fout, "#endif\n");
3626 }
3627
3628 // the function itself
3629 fprintf(fout, "%s ", g_func_pp->ret_type.name);
3630 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
3631 fprintf(fout, "%s(", g_func_pp->name);
3632
3633 for (i = 0; i < g_func_pp->argc; i++) {
3634 if (i > 0)
3635 fprintf(fout, ", ");
3636 if (g_func_pp->arg[i].fptr != NULL) {
3637 // func pointer..
3638 pp = g_func_pp->arg[i].fptr;
3639 fprintf(fout, "%s (", pp->ret_type.name);
3640 output_pp_attrs(fout, pp, 0);
3641 fprintf(fout, "*a%d)(", i + 1);
3642 for (j = 0; j < pp->argc; j++) {
3643 if (j > 0)
3644 fprintf(fout, ", ");
3645 if (pp->arg[j].fptr)
3646 ferr(ops, "nested fptr\n");
3647 fprintf(fout, "%s", pp->arg[j].type.name);
3648 }
3649 if (pp->is_vararg) {
3650 if (j > 0)
3651 fprintf(fout, ", ");
3652 fprintf(fout, "...");
3653 }
3654 fprintf(fout, ")");
3655 }
3656 else if (g_func_pp->arg[i].type.is_retreg) {
3657 fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg);
3658 }
3659 else {
3660 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
3661 }
3662 }
3663 if (g_func_pp->is_vararg) {
3664 if (i > 0)
3665 fprintf(fout, ", ");
3666 fprintf(fout, "...");
3667 }
3668
3669 fprintf(fout, ")\n{\n");
3670
3671 // declare indirect functions
3672 for (i = 0; i < opcnt; i++) {
3673 po = &ops[i];
3674 if (po->flags & OPF_RMD)
3675 continue;
3676
3677 if (po->op == OP_CALL) {
3678 pp = po->pp;
3679 if (pp == NULL)
3680 ferr(po, "NULL pp\n");
3681
3682 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
3683 if (pp->name[0] != 0) {
3684 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
3685 memcpy(pp->name, "i_", 2);
3686
3687 // might be declared already
3688 found = 0;
3689 for (j = 0; j < i; j++) {
3690 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
3691 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
3692 found = 1;
3693 break;
3694 }
3695 }
3696 }
3697 if (found)
3698 continue;
3699 }
3700 else
3701 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
3702
3703 fprintf(fout, " %s (", pp->ret_type.name);
3704 output_pp_attrs(fout, pp, 0);
3705 fprintf(fout, "*%s)(", pp->name);
3706 for (j = 0; j < pp->argc; j++) {
3707 if (j > 0)
3708 fprintf(fout, ", ");
3709 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3710 }
3711 fprintf(fout, ");\n");
3712 }
3713 }
3714 }
da87ae38 3715
4c45fa73 3716 // output LUTs/jumptables
3717 for (i = 0; i < g_func_pd_cnt; i++) {
3718 pd = &g_func_pd[i];
3719 fprintf(fout, " static const ");
3720 if (pd->type == OPT_OFFSET) {
3721 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3722
3723 for (j = 0; j < pd->count; j++) {
3724 if (j > 0)
3725 fprintf(fout, ", ");
3726 fprintf(fout, "&&%s", pd->d[j].u.label);
3727 }
3728 }
3729 else {
3730 fprintf(fout, "%s %s[] =\n { ",
3731 lmod_type_u(ops, pd->lmod), pd->label);
3732
3733 for (j = 0; j < pd->count; j++) {
3734 if (j > 0)
3735 fprintf(fout, ", ");
3736 fprintf(fout, "%u", pd->d[j].u.val);
3737 }
3738 }
3739 fprintf(fout, " };\n");
1f84f6b3 3740 had_decl = 1;
4c45fa73 3741 }
3742
4f12f671 3743 // declare stack frame, va_arg
1f84f6b3 3744 if (g_stack_fsz) {
850c9265 3745 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
1bafb621 3746 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
1f84f6b3 3747 had_decl = 1;
3748 }
3749
3750 if (g_func_pp->is_userstack) {
60fe410c 3751 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
3752 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
1f84f6b3 3753 had_decl = 1;
3754 }
850c9265 3755
1f84f6b3 3756 if (g_func_pp->is_vararg) {
4f12f671 3757 fprintf(fout, " va_list ap;\n");
1f84f6b3 3758 had_decl = 1;
3759 }
4f12f671 3760
940e8e66 3761 // declare arg-registers
bd96f656 3762 for (i = 0; i < g_func_pp->argc; i++) {
3763 if (g_func_pp->arg[i].reg != NULL) {
91977a1c 3764 reg = char_array_i(regs_r32,
bd96f656 3765 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
75ad0378 3766 if (regmask & (1 << reg)) {
1f84f6b3 3767 if (g_func_pp->arg[i].type.is_retreg)
3768 fprintf(fout, " u32 %s = *r_%s;\n",
3769 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
3770 else
3771 fprintf(fout, " u32 %s = (u32)a%d;\n",
3772 g_func_pp->arg[i].reg, i + 1);
75ad0378 3773 }
1f84f6b3 3774 else {
3775 if (g_func_pp->arg[i].type.is_retreg)
3776 ferr(ops, "retreg '%s' is unused?\n",
3777 g_func_pp->arg[i].reg);
75ad0378 3778 fprintf(fout, " // %s = a%d; // unused\n",
3779 g_func_pp->arg[i].reg, i + 1);
1f84f6b3 3780 }
91977a1c 3781 had_decl = 1;
3782 }
3783 }
3784
75ad0378 3785 regmask_now = regmask & ~regmask_arg;
3786 regmask_now &= ~(1 << xSP);
3787 if (regmask_now) {
91977a1c 3788 for (reg = 0; reg < 8; reg++) {
75ad0378 3789 if (regmask_now & (1 << reg)) {
ddaf8bd7 3790 fprintf(fout, " u32 %s", regs_r32[reg]);
3791 if (regmask_init & (1 << reg))
3792 fprintf(fout, " = 0");
3793 fprintf(fout, ";\n");
91977a1c 3794 had_decl = 1;
3795 }
3796 }
3797 }
3798
d4e3b5db 3799 if (regmask_save) {
3800 for (reg = 0; reg < 8; reg++) {
3801 if (regmask_save & (1 << reg)) {
3802 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3803 had_decl = 1;
3804 }
3805 }
3806 }
3807
69a3cdfc 3808 if (save_arg_vars) {
3809 for (reg = 0; reg < 32; reg++) {
3810 if (save_arg_vars & (1 << reg)) {
3811 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3812 had_decl = 1;
3813 }
3814 }
3815 }
3816
cb090db0 3817 if (cond_vars) {
69a3cdfc 3818 for (i = 0; i < 8; i++) {
cb090db0 3819 if (cond_vars & (1 << i)) {
69a3cdfc 3820 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3821 had_decl = 1;
3822 }
3823 }
3824 }
3825
108e9fe3 3826 if (need_tmp_var) {
3827 fprintf(fout, " u32 tmp;\n");
3828 had_decl = 1;
3829 }
3830
2fe80fdb 3831 if (need_tmp64) {
3832 fprintf(fout, " u64 tmp64;\n");
87bf6cec 3833 had_decl = 1;
3834 }
3835
91977a1c 3836 if (had_decl)
3837 fprintf(fout, "\n");
3838
bd96f656 3839 if (g_func_pp->is_vararg) {
3840 if (g_func_pp->argc_stack == 0)
4f12f671 3841 ferr(ops, "vararg func without stack args?\n");
bd96f656 3842 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
4f12f671 3843 }
3844
91977a1c 3845 // output ops
69a3cdfc 3846 for (i = 0; i < opcnt; i++)
3847 {
a2c1d768 3848 if (g_labels[i][0] != 0) {
91977a1c 3849 fprintf(fout, "\n%s:\n", g_labels[i]);
3ebea2cf 3850 label_pending = 1;
2b43685d 3851
3852 delayed_flag_op = NULL;
3853 last_arith_dst = NULL;
3ebea2cf 3854 }
91977a1c 3855
69a3cdfc 3856 po = &ops[i];
3857 if (po->flags & OPF_RMD)
91977a1c 3858 continue;
3859
3860 no_output = 0;
3861
91977a1c 3862 #define assert_operand_cnt(n_) \
850c9265 3863 if (po->operand_cnt != n_) \
3864 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3865
69a3cdfc 3866 // conditional/flag using op?
3867 if (po->flags & OPF_CC)
850c9265 3868 {
940e8e66 3869 int is_delayed = 0;
69a3cdfc 3870
04f8a628 3871 tmp_op = po->datap;
850c9265 3872
69a3cdfc 3873 // we go through all this trouble to avoid using parsed_flag_op,
3874 // which makes generated code much nicer
3875 if (delayed_flag_op != NULL)
850c9265 3876 {
092f64e1 3877 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
3878 po->pfo, po->pfo_inv);
940e8e66 3879 is_delayed = 1;
91977a1c 3880 }
850c9265 3881 else if (last_arith_dst != NULL
092f64e1 3882 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
04f8a628 3883 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3884 ))
850c9265 3885 {
3ebea2cf 3886 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
092f64e1 3887 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
850c9265 3888 last_arith_dst->lmod, buf3);
940e8e66 3889 is_delayed = 1;
850c9265 3890 }
04f8a628 3891 else if (tmp_op != NULL) {
7ba45c34 3892 // use preprocessed flag calc results
092f64e1 3893 if (!(tmp_op->pfomask & (1 << po->pfo)))
3894 ferr(po, "not prepared for pfo %d\n", po->pfo);
69a3cdfc 3895
092f64e1 3896 // note: pfo_inv was not yet applied
69a3cdfc 3897 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
092f64e1 3898 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
69a3cdfc 3899 }
3900 else {
3901 ferr(po, "all methods of finding comparison failed\n");
3902 }
850c9265 3903
69a3cdfc 3904 if (po->flags & OPF_JMP) {
092f64e1 3905 fprintf(fout, " if %s", buf1);
850c9265 3906 }
cb090db0 3907 else if (po->op == OP_RCL || po->op == OP_RCR
3908 || po->op == OP_ADC || po->op == OP_SBB)
3909 {
940e8e66 3910 if (is_delayed)
3911 fprintf(fout, " cond_%s = %s;\n",
092f64e1 3912 parsed_flag_op_names[po->pfo], buf1);
850c9265 3913 }
5101a5f9 3914 else if (po->flags & OPF_DATA) { // SETcc
850c9265 3915 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
3916 fprintf(fout, " %s = %s;", buf2, buf1);
91977a1c 3917 }
69a3cdfc 3918 else {
3919 ferr(po, "unhandled conditional op\n");
3920 }
91977a1c 3921 }
3922
940e8e66 3923 pfomask = po->pfomask;
3924
4741fdfe 3925 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
1f84f6b3 3926 struct parsed_opr opr = {0,};
3927 opr.type = OPT_REG;
3928 opr.reg = xCX;
3929 opr.lmod = OPLM_DWORD;
3930 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
3931
3932 if (ret != 1 || uval == 0) {
3933 // we need initial flags for ecx=0 case..
3934 if (i > 0 && ops[i - 1].op == OP_XOR
3935 && IS(ops[i - 1].operand[0].name,
3936 ops[i - 1].operand[1].name))
3937 {
3938 fprintf(fout, " cond_z = ");
3939 if (pfomask & (1 << PFO_C))
3940 fprintf(fout, "cond_c = ");
3941 fprintf(fout, "0;\n");
3942 }
3943 else if (last_arith_dst != NULL) {
3944 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3945 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
3946 last_arith_dst->lmod, buf3);
3947 fprintf(fout, " cond_z = %s;\n", buf1);
3948 }
3949 else
3950 ferr(po, "missing initial ZF\n");
4741fdfe 3951 }
4741fdfe 3952 }
3953
850c9265 3954 switch (po->op)
91977a1c 3955 {
3956 case OP_MOV:
3957 assert_operand_cnt(2);
850c9265 3958 propagate_lmod(po, &po->operand[0], &po->operand[1]);
de50b98b 3959 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
c7ed83dd 3960 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
de50b98b 3961 fprintf(fout, " %s = %s;", buf1,
3ebea2cf 3962 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
c7ed83dd 3963 buf3, 0));
850c9265 3964 break;
3965
3966 case OP_LEA:
3967 assert_operand_cnt(2);
87bf6cec 3968 po->operand[1].lmod = OPLM_DWORD; // always
850c9265 3969 fprintf(fout, " %s = %s;",
3970 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3ebea2cf 3971 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3972 NULL, 1));
850c9265 3973 break;
3974
3975 case OP_MOVZX:
3976 assert_operand_cnt(2);
91977a1c 3977 fprintf(fout, " %s = %s;",
850c9265 3978 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3ebea2cf 3979 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
850c9265 3980 break;
3981
3982 case OP_MOVSX:
3983 assert_operand_cnt(2);
3984 switch (po->operand[1].lmod) {
3985 case OPLM_BYTE:
3986 strcpy(buf3, "(s8)");
3987 break;
3988 case OPLM_WORD:
3989 strcpy(buf3, "(s16)");
3990 break;
3991 default:
3992 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
3993 }
a2c1d768 3994 fprintf(fout, " %s = %s;",
850c9265 3995 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
a2c1d768 3996 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3997 buf3, 0));
850c9265 3998 break;
3999
108e9fe3 4000 case OP_XCHG:
4001 assert_operand_cnt(2);
4002 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4003 fprintf(fout, " tmp = %s;",
4004 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
4005 fprintf(fout, " %s = %s;",
4006 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
c7ed83dd 4007 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4008 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4009 fprintf(fout, " %s = %stmp;",
4010 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
4011 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
108e9fe3 4012 snprintf(g_comment, sizeof(g_comment), "xchg");
4013 break;
4014
850c9265 4015 case OP_NOT:
4016 assert_operand_cnt(1);
4017 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4018 fprintf(fout, " %s = ~%s;", buf1, buf1);
4019 break;
4020
5101a5f9 4021 case OP_CDQ:
4022 assert_operand_cnt(2);
4023 fprintf(fout, " %s = (s32)%s >> 31;",
4024 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3ebea2cf 4025 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5101a5f9 4026 strcpy(g_comment, "cdq");
4027 break;
4028
092f64e1 4029 case OP_LODS:
4030 assert_operand_cnt(3);
4031 if (po->flags & OPF_REP) {
4032 // hmh..
4033 ferr(po, "TODO\n");
4034 }
4035 else {
05381e4a 4036 fprintf(fout, " eax = %sesi; esi %c= %d;",
092f64e1 4037 lmod_cast_u_ptr(po, po->operand[0].lmod),
4038 (po->flags & OPF_DF) ? '-' : '+',
4039 lmod_bytes(po, po->operand[0].lmod));
4040 strcpy(g_comment, "lods");
4041 }
4042 break;
4043
33c35af6 4044 case OP_STOS:
33c35af6 4045 assert_operand_cnt(3);
4046 if (po->flags & OPF_REP) {
591721d7 4047 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
4048 (po->flags & OPF_DF) ? '-' : '+',
33c35af6 4049 lmod_bytes(po, po->operand[0].lmod));
d4e3b5db 4050 fprintf(fout, " %sedi = eax;",
4051 lmod_cast_u_ptr(po, po->operand[0].lmod));
33c35af6 4052 strcpy(g_comment, "rep stos");
4053 }
4054 else {
092f64e1 4055 fprintf(fout, " %sedi = eax; edi %c= %d;",
d4e3b5db 4056 lmod_cast_u_ptr(po, po->operand[0].lmod),
591721d7 4057 (po->flags & OPF_DF) ? '-' : '+',
33c35af6 4058 lmod_bytes(po, po->operand[0].lmod));
4059 strcpy(g_comment, "stos");
4060 }
4061 break;
4062
d4e3b5db 4063 case OP_MOVS:
d4e3b5db 4064 assert_operand_cnt(3);
4065 j = lmod_bytes(po, po->operand[0].lmod);
4066 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
591721d7 4067 l = (po->flags & OPF_DF) ? '-' : '+';
d4e3b5db 4068 if (po->flags & OPF_REP) {
4069 fprintf(fout,
591721d7 4070 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
4071 l, j, l, j);
d4e3b5db 4072 fprintf(fout,
4073 " %sedi = %sesi;", buf1, buf1);
4074 strcpy(g_comment, "rep movs");
4075 }
4076 else {
092f64e1 4077 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
591721d7 4078 buf1, buf1, l, j, l, j);
d4e3b5db 4079 strcpy(g_comment, "movs");
4080 }
4081 break;
4082
7ba45c34 4083 case OP_CMPS:
7ba45c34 4084 // repe ~ repeat while ZF=1
4085 assert_operand_cnt(3);
4086 j = lmod_bytes(po, po->operand[0].lmod);
4087 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
591721d7 4088 l = (po->flags & OPF_DF) ? '-' : '+';
7ba45c34 4089 if (po->flags & OPF_REP) {
4090 fprintf(fout,
1f84f6b3 4091 " for (; ecx != 0; ecx--) {\n");
4741fdfe 4092 if (pfomask & (1 << PFO_C)) {
4093 // ugh..
4094 fprintf(fout,
05381e4a 4095 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4741fdfe 4096 pfomask &= ~(1 << PFO_C);
4097 }
7ba45c34 4098 fprintf(fout,
05381e4a 4099 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
1f84f6b3 4100 buf1, buf1, l, j, l, j);
4101 fprintf(fout,
4102 " if (cond_z %s 0) break;\n",
4103 (po->flags & OPF_REPZ) ? "==" : "!=");
7ba45c34 4104 fprintf(fout,
4741fdfe 4105 " }");
7ba45c34 4106 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
4107 (po->flags & OPF_REPZ) ? "e" : "ne");
4108 }
4109 else {
4110 fprintf(fout,
05381e4a 4111 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
591721d7 4112 buf1, buf1, l, j, l, j);
7ba45c34 4113 strcpy(g_comment, "cmps");
4114 }
4115 pfomask &= ~(1 << PFO_Z);
4116 last_arith_dst = NULL;
4117 delayed_flag_op = NULL;
4118 break;
4119
591721d7 4120 case OP_SCAS:
4121 // only does ZF (for now)
4122 // repe ~ repeat while ZF=1
4123 assert_operand_cnt(3);
4124 j = lmod_bytes(po, po->operand[0].lmod);
4125 l = (po->flags & OPF_DF) ? '-' : '+';
4126 if (po->flags & OPF_REP) {
4127 fprintf(fout,
1f84f6b3 4128 " for (; ecx != 0; ecx--) {\n");
591721d7 4129 fprintf(fout,
1f84f6b3 4130 " cond_z = (%seax == %sedi); edi %c= %d;\n",
591721d7 4131 lmod_cast_u(po, po->operand[0].lmod),
1f84f6b3 4132 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4133 fprintf(fout,
4134 " if (cond_z %s 0) break;\n",
591721d7 4135 (po->flags & OPF_REPZ) ? "==" : "!=");
4136 fprintf(fout,
1f84f6b3 4137 " }");
591721d7 4138 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4139 (po->flags & OPF_REPZ) ? "e" : "ne");
4140 }
4141 else {
05381e4a 4142 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
591721d7 4143 lmod_cast_u(po, po->operand[0].lmod),
4144 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4145 strcpy(g_comment, "scas");
4146 }
4147 pfomask &= ~(1 << PFO_Z);
4148 last_arith_dst = NULL;
4149 delayed_flag_op = NULL;
4150 break;
4151
850c9265 4152 // arithmetic w/flags
850c9265 4153 case OP_AND:
4154 case OP_OR:
5101a5f9 4155 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4156 // fallthrough
850c9265 4157 dualop_arith:
4158 assert_operand_cnt(2);
850c9265 4159 fprintf(fout, " %s %s= %s;",
4160 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4161 op_to_c(po),
3ebea2cf 4162 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
04f8a628 4163 output_std_flags(fout, po, &pfomask, buf1);
4164 last_arith_dst = &po->operand[0];
4165 delayed_flag_op = NULL;
4166 break;
4167
4168 case OP_SHL:
4169 case OP_SHR:
4170 assert_operand_cnt(2);
4171 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4172 if (pfomask & (1 << PFO_C)) {
4173 if (po->operand[1].type == OPT_CONST) {
4174 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4175 j = po->operand[1].val;
4176 j %= l;
4177 if (j != 0) {
4178 if (po->op == OP_SHL)
4179 j = l - j;
4180 else
4181 j -= 1;
cb090db0 4182 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
4183 buf1, j);
04f8a628 4184 }
4185 else
4186 ferr(po, "zero shift?\n");
4187 }
4188 else
4189 ferr(po, "TODO\n");
4190 pfomask &= ~(1 << PFO_C);
840257f6 4191 }
04f8a628 4192 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
4193 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4194 output_std_flags(fout, po, &pfomask, buf1);
850c9265 4195 last_arith_dst = &po->operand[0];
69a3cdfc 4196 delayed_flag_op = NULL;
850c9265 4197 break;
4198
d4e3b5db 4199 case OP_SAR:
4200 assert_operand_cnt(2);
4201 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4202 fprintf(fout, " %s = %s%s >> %s;", buf1,
4203 lmod_cast_s(po, po->operand[0].lmod), buf1,
3ebea2cf 4204 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
04f8a628 4205 output_std_flags(fout, po, &pfomask, buf1);
d4e3b5db 4206 last_arith_dst = &po->operand[0];
4207 delayed_flag_op = NULL;
4208 break;
4209
4210 case OP_ROL:
4211 case OP_ROR:
4212 assert_operand_cnt(2);
4213 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4214 if (po->operand[1].type == OPT_CONST) {
4215 j = po->operand[1].val;
4216 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
4217 fprintf(fout, po->op == OP_ROL ?
4218 " %s = (%s << %d) | (%s >> %d);" :
4219 " %s = (%s >> %d) | (%s << %d);",
4220 buf1, buf1, j, buf1,
4221 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
4222 }
4223 else
4224 ferr(po, "TODO\n");
04f8a628 4225 output_std_flags(fout, po, &pfomask, buf1);
d4e3b5db 4226 last_arith_dst = &po->operand[0];
4227 delayed_flag_op = NULL;
4228 break;
4229
cb090db0 4230 case OP_RCL:
4231 case OP_RCR:
4232 assert_operand_cnt(2);
4233 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4234 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4235 if (po->operand[1].type == OPT_CONST) {
4236 j = po->operand[1].val % l;
4237 if (j == 0)
4238 ferr(po, "zero rotate\n");
4239 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
4240 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
4241 if (po->op == OP_RCL) {
4242 fprintf(fout,
4243 " %s = (%s << %d) | (cond_c << %d)",
4244 buf1, buf1, j, j - 1);
4245 if (j != 1)
4246 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
4247 }
4248 else {
4249 fprintf(fout,
4250 " %s = (%s >> %d) | (cond_c << %d)",
4251 buf1, buf1, j, l - j);
4252 if (j != 1)
4253 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
4254 }
4255 fprintf(fout, ";\n");
4256 fprintf(fout, " cond_c = tmp;");
4257 }
4258 else
4259 ferr(po, "TODO\n");
4260 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
4261 output_std_flags(fout, po, &pfomask, buf1);
4262 last_arith_dst = &po->operand[0];
4263 delayed_flag_op = NULL;
4264 break;
4265
5101a5f9 4266 case OP_XOR:
850c9265 4267 assert_operand_cnt(2);
4268 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5101a5f9 4269 if (IS(opr_name(po, 0), opr_name(po, 1))) {
4270 // special case for XOR
2fe80fdb 4271 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
4272 fprintf(fout, " cond_be = 1;\n");
4273 pfomask &= ~(1 << PFO_BE);
4274 }
5101a5f9 4275 fprintf(fout, " %s = 0;",
4276 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4277 last_arith_dst = &po->operand[0];
4278 delayed_flag_op = NULL;
850c9265 4279 break;
850c9265 4280 }
5101a5f9 4281 goto dualop_arith;
4282
2fe80fdb 4283 case OP_ADD:
4284 assert_operand_cnt(2);
4285 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4286 if (pfomask & (1 << PFO_C)) {
c8dbc5be 4287 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4288 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4289 if (po->operand[0].lmod == OPLM_DWORD) {
4290 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
4291 fprintf(fout, " cond_c = tmp64 >> 32;\n");
4292 fprintf(fout, " %s = (u32)tmp64;",
4293 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4294 strcat(g_comment, "add64");
4295 }
4296 else {
4297 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
4298 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
4299 fprintf(fout, " %s += %s;",
4300 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4301 buf2);
4302 }
2fe80fdb 4303 pfomask &= ~(1 << PFO_C);
4304 output_std_flags(fout, po, &pfomask, buf1);
4305 last_arith_dst = &po->operand[0];
4306 delayed_flag_op = NULL;
4307 break;
4308 }
4309 goto dualop_arith;
4310
4311 case OP_SUB:
4312 assert_operand_cnt(2);
4313 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4314 if (pfomask & (1 << PFO_C)) {
4315 fprintf(fout, " cond_c = %s < %s;\n",
4316 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]),
4317 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4318 pfomask &= ~(1 << PFO_C);
4319 }
4320 goto dualop_arith;
4321
5101a5f9 4322 case OP_ADC:
850c9265 4323 case OP_SBB:
5101a5f9 4324 assert_operand_cnt(2);
4325 propagate_lmod(po, &po->operand[0], &po->operand[1]);
a2c1d768 4326 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
840257f6 4327 if (po->op == OP_SBB
4328 && IS(po->operand[0].name, po->operand[1].name))
4329 {
4330 // avoid use of unitialized var
a2c1d768 4331 fprintf(fout, " %s = -cond_c;", buf1);
94d447fb 4332 // carry remains what it was
4333 pfomask &= ~(1 << PFO_C);
840257f6 4334 }
4335 else {
a2c1d768 4336 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
3ebea2cf 4337 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
840257f6 4338 }
a2c1d768 4339 output_std_flags(fout, po, &pfomask, buf1);
5101a5f9 4340 last_arith_dst = &po->operand[0];
4341 delayed_flag_op = NULL;
850c9265 4342 break;
4343
1f84f6b3 4344 case OP_BSF:
4345 assert_operand_cnt(2);
4346 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4347 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
4348 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4349 buf2, buf2);
4350 output_std_flags(fout, po, &pfomask, buf1);
4351 last_arith_dst = &po->operand[0];
4352 delayed_flag_op = NULL;
4353 strcat(g_comment, "bsf");
4354 break;
4355
850c9265 4356 case OP_INC:
4357 case OP_DEC:
4358 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1bafb621 4359 if (po->operand[0].type == OPT_REG) {
4360 strcpy(buf2, po->op == OP_INC ? "++" : "--");
4361 fprintf(fout, " %s%s;", buf1, buf2);
4362 }
4363 else {
4364 strcpy(buf2, po->op == OP_INC ? "+" : "-");
4365 fprintf(fout, " %s %s= 1;", buf1, buf2);
4366 }
a2c1d768 4367 output_std_flags(fout, po, &pfomask, buf1);
5101a5f9 4368 last_arith_dst = &po->operand[0];
4369 delayed_flag_op = NULL;
4370 break;
4371
4372 case OP_NEG:
4373 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3ebea2cf 4374 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
5101a5f9 4375 fprintf(fout, " %s = -%s%s;", buf1,
4376 lmod_cast_s(po, po->operand[0].lmod), buf2);
850c9265 4377 last_arith_dst = &po->operand[0];
69a3cdfc 4378 delayed_flag_op = NULL;
940e8e66 4379 if (pfomask & (1 << PFO_C)) {
4380 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
4381 pfomask &= ~(1 << PFO_C);
4382 }
850c9265 4383 break;
4384
4385 case OP_IMUL:
de50b98b 4386 if (po->operand_cnt == 2) {
4387 propagate_lmod(po, &po->operand[0], &po->operand[1]);
850c9265 4388 goto dualop_arith;
de50b98b 4389 }
87bf6cec 4390 if (po->operand_cnt == 3)
4391 ferr(po, "TODO imul3\n");
4392 // fallthrough
4393 case OP_MUL:
4394 assert_operand_cnt(1);
c8dbc5be 4395 switch (po->operand[0].lmod) {
4396 case OPLM_DWORD:
4397 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
4398 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
4399 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
4400 fprintf(fout, " edx = tmp64 >> 32;\n");
4401 fprintf(fout, " eax = tmp64;");
4402 break;
4403 case OPLM_BYTE:
4404 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
4405 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
4406 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
4407 buf1, 0));
4408 break;
4409 default:
4410 ferr(po, "TODO: unhandled mul type\n");
4411 break;
4412 }
87bf6cec 4413 last_arith_dst = NULL;
69a3cdfc 4414 delayed_flag_op = NULL;
91977a1c 4415 break;
4416
5101a5f9 4417 case OP_DIV:
4418 case OP_IDIV:
4419 assert_operand_cnt(1);
4420 if (po->operand[0].lmod != OPLM_DWORD)
4421 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
4422
cb090db0 4423 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4424 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
4425 po->op == OP_IDIV));
4426 switch (po->operand[0].lmod) {
4427 case OPLM_DWORD:
4428 if (po->flags & OPF_32BIT)
4429 snprintf(buf3, sizeof(buf3), "%seax", buf2);
4430 else {
4431 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
4432 snprintf(buf3, sizeof(buf3), "%stmp64",
4433 (po->op == OP_IDIV) ? "(s64)" : "");
4434 }
4435 if (po->operand[0].type == OPT_REG
4436 && po->operand[0].reg == xDX)
4437 {
4438 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4439 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4440 }
4441 else {
4442 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4443 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4444 }
4445 break;
4446 default:
4447 ferr(po, "unhandled division type\n");
5101a5f9 4448 }
87bf6cec 4449 last_arith_dst = NULL;
4450 delayed_flag_op = NULL;
5101a5f9 4451 break;
4452
91977a1c 4453 case OP_TEST:
4454 case OP_CMP:
850c9265 4455 propagate_lmod(po, &po->operand[0], &po->operand[1]);
940e8e66 4456 if (pfomask != 0) {
69a3cdfc 4457 for (j = 0; j < 8; j++) {
940e8e66 4458 if (pfomask & (1 << j)) {
69a3cdfc 4459 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
4460 fprintf(fout, " cond_%s = %s;",
4461 parsed_flag_op_names[j], buf1);
4462 }
4463 }
940e8e66 4464 pfomask = 0;
69a3cdfc 4465 }
4466 else
4467 no_output = 1;
7ba45c34 4468 last_arith_dst = NULL;
69a3cdfc 4469 delayed_flag_op = po;
91977a1c 4470 break;
4471
092f64e1 4472 case OP_SCC:
4473 // SETcc - should already be handled
4474 break;
4475
69a3cdfc 4476 // note: we reuse OP_Jcc for SETcc, only flags differ
092f64e1 4477 case OP_JCC:
4478 fprintf(fout, "\n goto %s;", po->operand[0].name);
850c9265 4479 break;
4480
5c024ef7 4481 case OP_JECXZ:
4482 fprintf(fout, " if (ecx == 0)\n");
4483 fprintf(fout, " goto %s;", po->operand[0].name);
4484 strcat(g_comment, "jecxz");
4485 break;
4486
850c9265 4487 case OP_JMP:
87bf6cec 4488 assert_operand_cnt(1);
de50b98b 4489 last_arith_dst = NULL;
4490 delayed_flag_op = NULL;
4491
4c45fa73 4492 if (po->operand[0].type == OPT_REGMEM) {
4493 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
4494 buf1, buf2);
4495 if (ret != 2)
4496 ferr(po, "parse failure for jmp '%s'\n",
4497 po->operand[0].name);
4498 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
4499 break;
4500 }
4501 else if (po->operand[0].type != OPT_LABEL)
4502 ferr(po, "unhandled jmp type\n");
87bf6cec 4503
850c9265 4504 fprintf(fout, " goto %s;", po->operand[0].name);
91977a1c 4505 break;
4506
4507 case OP_CALL:
5101a5f9 4508 assert_operand_cnt(1);
092f64e1 4509 pp = po->pp;
89ff3147 4510 my_assert_not(pp, NULL);
91977a1c 4511
092f64e1 4512 strcpy(buf3, " ");
4513 if (po->flags & OPF_CC) {
4514 // we treat conditional branch to another func
4515 // (yes such code exists..) as conditional tailcall
4516 strcat(buf3, " ");
4517 fprintf(fout, " {\n");
4518 }
4519
8eb12e72 4520 if (pp->is_fptr && !pp->is_arg) {
092f64e1 4521 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
1cd4a663 4522 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
4523 "(void *)", 0));
8eb12e72 4524 if (pp->is_unresolved)
4525 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
4526 buf3, asmfn, po->asmln, pp->name);
4527 }
1bafb621 4528
092f64e1 4529 fprintf(fout, "%s", buf3);
2b43685d 4530 if (strstr(pp->ret_type.name, "int64")) {
87bf6cec 4531 if (po->flags & OPF_TAIL)
2b43685d 4532 ferr(po, "int64 and tail?\n");
2fe80fdb 4533 fprintf(fout, "tmp64 = ");
2b43685d 4534 }
4535 else if (!IS(pp->ret_type.name, "void")) {
4536 if (po->flags & OPF_TAIL) {
840257f6 4537 if (!IS(g_func_pp->ret_type.name, "void")) {
4538 fprintf(fout, "return ");
4539 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
4540 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
4541 }
2b43685d 4542 }
75ad0378 4543 else if (regmask & (1 << xAX)) {
87bf6cec 4544 fprintf(fout, "eax = ");
2b43685d 4545 if (pp->ret_type.is_ptr)
4546 fprintf(fout, "(u32)");
4547 }
91977a1c 4548 }
87bf6cec 4549
ddaf8bd7 4550 if (pp->name[0] == 0)
4551 ferr(po, "missing pp->name\n");
4552 fprintf(fout, "%s%s(", pp->name,
4553 pp->has_structarg ? "_sa" : "");
39b168b8 4554
2fe80fdb 4555 if (po->flags & OPF_ATAIL) {
4556 if (pp->argc_stack != g_func_pp->argc_stack
4557 || (pp->argc_stack > 0
4558 && pp->is_stdcall != g_func_pp->is_stdcall))
4559 ferr(po, "incompatible tailcall\n");
1f84f6b3 4560 if (g_func_pp->has_retreg)
4561 ferr(po, "TODO: retreg+tailcall\n");
87bf6cec 4562
2fe80fdb 4563 for (arg = j = 0; arg < pp->argc; arg++) {
4564 if (arg > 0)
4565 fprintf(fout, ", ");
87bf6cec 4566
2fe80fdb 4567 cast[0] = 0;
4568 if (pp->arg[arg].type.is_ptr)
4569 snprintf(cast, sizeof(cast), "(%s)",
4570 pp->arg[arg].type.name);
91977a1c 4571
2fe80fdb 4572 if (pp->arg[arg].reg != NULL) {
4573 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4574 continue;
4575 }
4576 // stack arg
4577 for (; j < g_func_pp->argc; j++)
4578 if (g_func_pp->arg[j].reg == NULL)
4579 break;
4580 fprintf(fout, "%sa%d", cast, j + 1);
4581 j++;
69a3cdfc 4582 }
2fe80fdb 4583 }
4584 else {
4585 for (arg = 0; arg < pp->argc; arg++) {
4586 if (arg > 0)
4587 fprintf(fout, ", ");
4588
4589 cast[0] = 0;
4590 if (pp->arg[arg].type.is_ptr)
4591 snprintf(cast, sizeof(cast), "(%s)",
4592 pp->arg[arg].type.name);
4593
4594 if (pp->arg[arg].reg != NULL) {
1f84f6b3 4595 if (pp->arg[arg].type.is_retreg)
4596 fprintf(fout, "&%s", pp->arg[arg].reg);
4597 else
4598 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
2fe80fdb 4599 continue;
4600 }
4601
4602 // stack arg
4603 tmp_op = pp->arg[arg].datap;
4604 if (tmp_op == NULL)
4605 ferr(po, "parsed_op missing for arg%d\n", arg);
23fd0b11 4606
4607 if (tmp_op->flags & OPF_VAPUSH) {
4608 fprintf(fout, "ap");
4609 }
4610 else if (tmp_op->argnum != 0) {
2fe80fdb 4611 fprintf(fout, "%ss_a%d", cast, tmp_op->argnum);
4612 }
4613 else {
4614 fprintf(fout, "%s",
4615 out_src_opr(buf1, sizeof(buf1),
4616 tmp_op, &tmp_op->operand[0], cast, 0));
4617 }
69a3cdfc 4618 }
91977a1c 4619 }
4620 fprintf(fout, ");");
87bf6cec 4621
2b43685d 4622 if (strstr(pp->ret_type.name, "int64")) {
4623 fprintf(fout, "\n");
092f64e1 4624 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
4625 fprintf(fout, "%seax = tmp64;", buf3);
2b43685d 4626 }
4627
89ff3147 4628 if (pp->is_unresolved) {
8eb12e72 4629 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
89ff3147 4630 pp->argc_reg);
092f64e1 4631 strcat(g_comment, buf2);
89ff3147 4632 }
4633
87bf6cec 4634 if (po->flags & OPF_TAIL) {
840257f6 4635 ret = 0;
ddaf8bd7 4636 if (i == opcnt - 1 || pp->is_noreturn)
840257f6 4637 ret = 0;
4638 else if (IS(pp->ret_type.name, "void"))
4639 ret = 1;
4640 else if (IS(g_func_pp->ret_type.name, "void"))
4641 ret = 1;
4642 // else already handled as 'return f()'
4643
4644 if (ret) {
da87ae38 4645 if (!IS(g_func_pp->ret_type.name, "void")) {
ddaf8bd7 4646 ferr(po, "int func -> void func tailcall?\n");
da87ae38 4647 }
4648 else {
092f64e1 4649 fprintf(fout, "\n%sreturn;", buf3);
ddaf8bd7 4650 strcat(g_comment, " ^ tailcall");
da87ae38 4651 }
3ebea2cf 4652 }
89ff3147 4653 else
ddaf8bd7 4654 strcat(g_comment, " tailcall");
87bf6cec 4655 }
ddaf8bd7 4656 if (pp->is_noreturn)
4657 strcat(g_comment, " noreturn");
2fe80fdb 4658 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
4659 strcat(g_comment, " argframe");
092f64e1 4660 if (po->flags & OPF_CC)
4661 strcat(g_comment, " cond");
4662
4663 if (po->flags & OPF_CC)
4664 fprintf(fout, "\n }");
4665
87bf6cec 4666 delayed_flag_op = NULL;
4667 last_arith_dst = NULL;
91977a1c 4668 break;
4669
4670 case OP_RET:
bd96f656 4671 if (g_func_pp->is_vararg)
4f12f671 4672 fprintf(fout, " va_end(ap);\n");
1f84f6b3 4673 if (g_func_pp->has_retreg) {
4674 for (arg = 0; arg < g_func_pp->argc; arg++)
4675 if (g_func_pp->arg[arg].type.is_retreg)
4676 fprintf(fout, " *r_%s = %s;\n",
4677 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
4678 }
4f12f671 4679
bd96f656 4680 if (IS(g_func_pp->ret_type.name, "void")) {
3ebea2cf 4681 if (i != opcnt - 1 || label_pending)
4682 fprintf(fout, " return;");
4683 }
bd96f656 4684 else if (g_func_pp->ret_type.is_ptr) {
d4e3b5db 4685 fprintf(fout, " return (%s)eax;",
bd96f656 4686 g_func_pp->ret_type.name);
3ebea2cf 4687 }
2fe80fdb 4688 else if (IS(g_func_pp->ret_type.name, "__int64"))
4689 fprintf(fout, " return ((u64)edx << 32) | eax;");
91977a1c 4690 else
4691 fprintf(fout, " return eax;");
de50b98b 4692
4693 last_arith_dst = NULL;
4694 delayed_flag_op = NULL;
91977a1c 4695 break;
4696
4697 case OP_PUSH:
1f84f6b3 4698 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
de50b98b 4699 if (po->argnum != 0) {
69a3cdfc 4700 // special case - saved func arg
de50b98b 4701 fprintf(fout, " s_a%d = %s;", po->argnum, buf1);
69a3cdfc 4702 break;
4703 }
d4e3b5db 4704 else if (po->flags & OPF_RSAVE) {
d4e3b5db 4705 fprintf(fout, " s_%s = %s;", buf1, buf1);
4706 break;
4707 }
1f84f6b3 4708 else if (g_func_pp->is_userstack) {
4709 fprintf(fout, " *(--esp) = %s;", buf1);
4710 break;
4711 }
e56ab892 4712 if (!(g_ida_func_attr & IDAFA_NORETURN))
4713 ferr(po, "stray push encountered\n");
4714 no_output = 1;
91977a1c 4715 break;
4716
4717 case OP_POP:
d4e3b5db 4718 if (po->flags & OPF_RSAVE) {
3ebea2cf 4719 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
d4e3b5db 4720 fprintf(fout, " %s = s_%s;", buf1, buf1);
4721 break;
4722 }
5c024ef7 4723 else if (po->datap != NULL) {
4724 // push/pop pair
4725 tmp_op = po->datap;
4726 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4727 fprintf(fout, " %s = %s;", buf1,
4728 out_src_opr(buf2, sizeof(buf2),
4729 tmp_op, &tmp_op->operand[0],
c7ed83dd 4730 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
5c024ef7 4731 break;
4732 }
1f84f6b3 4733 else if (g_func_pp->is_userstack) {
4734 fprintf(fout, " %s = *esp++;",
4735 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4736 break;
4737 }
4738 else
4739 ferr(po, "stray pop encountered\n");
91977a1c 4740 break;
4741
33c35af6 4742 case OP_NOP:
2b43685d 4743 no_output = 1;
33c35af6 4744 break;
4745
91977a1c 4746 default:
4747 no_output = 1;
69a3cdfc 4748 ferr(po, "unhandled op type %d, flags %x\n",
4749 po->op, po->flags);
91977a1c 4750 break;
4751 }
4752
4753 if (g_comment[0] != 0) {
ddaf8bd7 4754 char *p = g_comment;
4755 while (my_isblank(*p))
4756 p++;
4757 fprintf(fout, " // %s", p);
91977a1c 4758 g_comment[0] = 0;
4759 no_output = 0;
4760 }
4761 if (!no_output)
4762 fprintf(fout, "\n");
5101a5f9 4763
2b43685d 4764 // some sanity checking
591721d7 4765 if (po->flags & OPF_REP) {
4766 if (po->op != OP_STOS && po->op != OP_MOVS
4767 && po->op != OP_CMPS && po->op != OP_SCAS)
4768 ferr(po, "unexpected rep\n");
4769 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
4770 && (po->op == OP_CMPS || po->op == OP_SCAS))
4771 ferr(po, "cmps/scas with plain rep\n");
4772 }
4773 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
4774 && po->op != OP_CMPS && po->op != OP_SCAS)
2b43685d 4775 ferr(po, "unexpected repz/repnz\n");
4776
940e8e66 4777 if (pfomask != 0)
7ba45c34 4778 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
940e8e66 4779
5101a5f9 4780 // see is delayed flag stuff is still valid
4781 if (delayed_flag_op != NULL && delayed_flag_op != po) {
89ff3147 4782 if (is_any_opr_modified(delayed_flag_op, po, 0))
5101a5f9 4783 delayed_flag_op = NULL;
4784 }
4785
4786 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
4787 if (is_opr_modified(last_arith_dst, po))
4788 last_arith_dst = NULL;
4789 }
3ebea2cf 4790
4791 label_pending = 0;
91977a1c 4792 }
4793
a2c1d768 4794 if (g_stack_fsz && !g_stack_frame_used)
4795 fprintf(fout, " (void)sf;\n");
4796
91977a1c 4797 fprintf(fout, "}\n\n");
4798
4799 // cleanup
4800 for (i = 0; i < opcnt; i++) {
4c45fa73 4801 struct label_ref *lr, *lr_del;
4802
4803 lr = g_label_refs[i].next;
4804 while (lr != NULL) {
4805 lr_del = lr;
4806 lr = lr->next;
4807 free(lr_del);
4808 }
4809 g_label_refs[i].i = -1;
4810 g_label_refs[i].next = NULL;
4811
91977a1c 4812 if (ops[i].op == OP_CALL) {
092f64e1 4813 if (ops[i].pp)
4814 proto_release(ops[i].pp);
91977a1c 4815 }
4816 }
bd96f656 4817 g_func_pp = NULL;
91977a1c 4818}
c36e914d 4819
d4e3b5db 4820static void set_label(int i, const char *name)
4821{
4822 const char *p;
4823 int len;
4824
4825 len = strlen(name);
4826 p = strchr(name, ':');
4827 if (p != NULL)
4828 len = p - name;
4829
4830 if (len > sizeof(g_labels[0]) - 1)
4831 aerr("label '%s' too long: %d\n", name, len);
e56ab892 4832 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4833 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
d4e3b5db 4834 memcpy(g_labels[i], name, len);
4835 g_labels[i][len] = 0;
4836}
4837
bfa4a6ee 4838// '=' needs special treatment..
4839static char *next_word_s(char *w, size_t wsize, char *s)
4840{
4841 size_t i;
4842
4843 s = sskip(s);
4844
4845 for (i = 0; i < wsize - 1; i++) {
4846 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4847 break;
4848 w[i] = s[i];
4849 }
4850 w[i] = 0;
4851
4852 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4853 printf("warning: '%s' truncated\n", w);
4854
4855 return s + i;
4856}
4857
e56ab892 4858struct chunk_item {
4859 char *name;
4860 long fptr;
de50b98b 4861 int asmln;
e56ab892 4862};
4863
cdfaeed7 4864static struct chunk_item *func_chunks;
4865static int func_chunk_cnt;
4866static int func_chunk_alloc;
4867
4868static void add_func_chunk(FILE *fasm, const char *name, int line)
4869{
4870 if (func_chunk_cnt >= func_chunk_alloc) {
4871 func_chunk_alloc *= 2;
4872 func_chunks = realloc(func_chunks,
4873 func_chunk_alloc * sizeof(func_chunks[0]));
4874 my_assert_not(func_chunks, NULL);
4875 }
4876 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4877 func_chunks[func_chunk_cnt].name = strdup(name);
4878 func_chunks[func_chunk_cnt].asmln = line;
4879 func_chunk_cnt++;
4880}
4881
e56ab892 4882static int cmp_chunks(const void *p1, const void *p2)
4883{
4884 const struct chunk_item *c1 = p1, *c2 = p2;
4885 return strcmp(c1->name, c2->name);
4886}
4887
bfa4a6ee 4888static int cmpstringp(const void *p1, const void *p2)
4889{
4890 return strcmp(*(char * const *)p1, *(char * const *)p2);
4891}
4892
cdfaeed7 4893static void scan_ahead(FILE *fasm)
4894{
4895 char words[2][256];
4896 char line[256];
4897 long oldpos;
4898 int oldasmln;
4899 int wordc;
4900 char *p;
4901 int i;
4902
4903 oldpos = ftell(fasm);
4904 oldasmln = asmln;
4905
4906 while (fgets(line, sizeof(line), fasm))
4907 {
4908 wordc = 0;
4909 asmln++;
4910
4911 p = sskip(line);
4912 if (*p == 0)
4913 continue;
4914
4915 if (*p == ';')
4916 {
4917 // get rid of random tabs
4918 for (i = 0; line[i] != 0; i++)
4919 if (line[i] == '\t')
4920 line[i] = ' ';
4921
4922 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4923 {
4924 p += 30;
4925 next_word(words[0], sizeof(words[0]), p);
4926 if (words[0][0] == 0)
4927 aerr("missing name for func chunk?\n");
4928
4929 add_func_chunk(fasm, words[0], asmln);
4930 }
46b388c2 4931 else if (IS_START(p, "; sctend"))
4932 break;
4933
cdfaeed7 4934 continue;
4935 } // *p == ';'
4936
4937 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4938 words[wordc][0] = 0;
4939 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4940 if (*p == 0 || *p == ';') {
4941 wordc++;
4942 break;
4943 }
4944 }
4945
4946 if (wordc == 2 && IS(words[1], "ends"))
4947 break;
4948 }
4949
4950 fseek(fasm, oldpos, SEEK_SET);
4951 asmln = oldasmln;
4952}
4953
91977a1c 4954int main(int argc, char *argv[])
4955{
06c5d854 4956 FILE *fout, *fasm, *frlist;
4c45fa73 4957 struct parsed_data *pd = NULL;
4958 int pd_alloc = 0;
4959 char **rlist = NULL;
4960 int rlist_len = 0;
4961 int rlist_alloc = 0;
e56ab892 4962 int func_chunks_used = 0;
4963 int func_chunks_sorted = 0;
e56ab892 4964 int func_chunk_i = -1;
4965 long func_chunk_ret = 0;
de50b98b 4966 int func_chunk_ret_ln = 0;
cdfaeed7 4967 int scanned_ahead = 0;
91977a1c 4968 char line[256];
a2c1d768 4969 char words[20][256];
4c45fa73 4970 enum opr_lenmod lmod;
ddaf8bd7 4971 char *sctproto = NULL;
91977a1c 4972 int in_func = 0;
4c45fa73 4973 int pending_endp = 0;
bfa4a6ee 4974 int skip_func = 0;
940e8e66 4975 int skip_warned = 0;
91977a1c 4976 int eq_alloc;
bfa4a6ee 4977 int verbose = 0;
1f84f6b3 4978 int multi_seg = 0;
46b388c2 4979 int end = 0;
bfa4a6ee 4980 int arg_out;
89ff3147 4981 int arg;
91977a1c 4982 int pi = 0;
e56ab892 4983 int i, j;
4984 int ret, len;
91977a1c 4985 char *p;
4986 int wordc;
4987
89ff3147 4988 for (arg = 1; arg < argc; arg++) {
4989 if (IS(argv[arg], "-v"))
4990 verbose = 1;
4991 else if (IS(argv[arg], "-rf"))
4992 g_allow_regfunc = 1;
1f84f6b3 4993 else if (IS(argv[arg], "-m"))
4994 multi_seg = 1;
89ff3147 4995 else
4996 break;
bfa4a6ee 4997 }
4998
4999 if (argc < arg + 3) {
1f84f6b3 5000 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdrf> [rlist]*\n",
91977a1c 5001 argv[0]);
5002 return 1;
5003 }
5004
bfa4a6ee 5005 arg_out = arg++;
91977a1c 5006
bfa4a6ee 5007 asmfn = argv[arg++];
91977a1c 5008 fasm = fopen(asmfn, "r");
5009 my_assert_not(fasm, NULL);
5010
bfa4a6ee 5011 hdrfn = argv[arg++];
06c5d854 5012 g_fhdr = fopen(hdrfn, "r");
5013 my_assert_not(g_fhdr, NULL);
bfa4a6ee 5014
5015 rlist_alloc = 64;
5016 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
5017 my_assert_not(rlist, NULL);
5018 // needs special handling..
5019 rlist[rlist_len++] = "__alloca_probe";
5020
e56ab892 5021 func_chunk_alloc = 32;
5022 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
5023 my_assert_not(func_chunks, NULL);
5024
a2c1d768 5025 memset(words, 0, sizeof(words));
5026
bfa4a6ee 5027 for (; arg < argc; arg++) {
5028 frlist = fopen(argv[arg], "r");
5029 my_assert_not(frlist, NULL);
5030
5031 while (fgets(line, sizeof(line), frlist)) {
5032 p = sskip(line);
1cd4a663 5033 if (*p == 0 || *p == ';')
5034 continue;
5035 if (*p == '#') {
89ff3147 5036 if (IS_START(p, "#if 0")
5037 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
5038 {
1cd4a663 5039 skip_func = 1;
89ff3147 5040 }
1cd4a663 5041 else if (IS_START(p, "#endif"))
5042 skip_func = 0;
5043 continue;
5044 }
5045 if (skip_func)
bfa4a6ee 5046 continue;
5047
5048 p = next_word(words[0], sizeof(words[0]), p);
5049 if (words[0][0] == 0)
5050 continue;
5051
5052 if (rlist_len >= rlist_alloc) {
5053 rlist_alloc = rlist_alloc * 2 + 64;
5054 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
5055 my_assert_not(rlist, NULL);
5056 }
5057 rlist[rlist_len++] = strdup(words[0]);
5058 }
1cd4a663 5059 skip_func = 0;
bfa4a6ee 5060
5061 fclose(frlist);
5062 frlist = NULL;
5063 }
5064
5065 if (rlist_len > 0)
5066 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
5067
5068 fout = fopen(argv[arg_out], "w");
91977a1c 5069 my_assert_not(fout, NULL);
5070
5071 eq_alloc = 128;
5072 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
5073 my_assert_not(g_eqs, NULL);
5074
4c45fa73 5075 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
5076 g_label_refs[i].i = -1;
5077 g_label_refs[i].next = NULL;
5078 }
5079
91977a1c 5080 while (fgets(line, sizeof(line), fasm))
5081 {
4c45fa73 5082 wordc = 0;
91977a1c 5083 asmln++;
5084
5085 p = sskip(line);
1bafb621 5086 if (*p == 0)
91977a1c 5087 continue;
5088
de50b98b 5089 // get rid of random tabs
5090 for (i = 0; line[i] != 0; i++)
5091 if (line[i] == '\t')
5092 line[i] = ' ';
5093
e56ab892 5094 if (*p == ';')
5095 {
e56ab892 5096 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
5097 goto do_pending_endp; // eww..
5098
5099 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
5100 {
5101 static const char *attrs[] = {
5102 "bp-based frame",
5103 "library function",
5104 "static",
5105 "noreturn",
5106 "thunk",
5107 "fpd=",
5108 };
5109
5110 // parse IDA's attribute-list comment
5111 g_ida_func_attr = 0;
5112 p = sskip(p + 13);
5113
5114 for (; *p != 0; p = sskip(p)) {
5115 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
5116 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
5117 g_ida_func_attr |= 1 << i;
5118 p += strlen(attrs[i]);
5119 break;
5120 }
5121 }
5122 if (i == ARRAY_SIZE(attrs)) {
5123 anote("unparsed IDA attr: %s\n", p);
1bafb621 5124 break;
5125 }
e56ab892 5126 if (IS(attrs[i], "fpd=")) {
5127 p = next_word(words[0], sizeof(words[0]), p);
5128 // ignore for now..
5129 }
1bafb621 5130 }
e56ab892 5131 }
5132 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5133 {
5134 p += 30;
5135 next_word(words[0], sizeof(words[0]), p);
5136 if (words[0][0] == 0)
cdfaeed7 5137 aerr("missing name for func chunk?\n");
5138
5139 if (!scanned_ahead) {
5140 add_func_chunk(fasm, words[0], asmln);
5141 func_chunks_sorted = 0;
e56ab892 5142 }
e56ab892 5143 }
5144 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
5145 {
5146 if (func_chunk_i >= 0) {
5147 if (func_chunk_i < func_chunk_cnt
5148 && IS(func_chunks[func_chunk_i].name, g_func))
5149 {
5150 // move on to next chunk
5151 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5152 if (ret)
5153 aerr("seek failed for '%s' chunk #%d\n",
5154 g_func, func_chunk_i);
de50b98b 5155 asmln = func_chunks[func_chunk_i].asmln;
e56ab892 5156 func_chunk_i++;
5157 }
5158 else {
5159 if (func_chunk_ret == 0)
5160 aerr("no return from chunk?\n");
5161 fseek(fasm, func_chunk_ret, SEEK_SET);
de50b98b 5162 asmln = func_chunk_ret_ln;
e56ab892 5163 func_chunk_ret = 0;
5164 pending_endp = 1;
5165 }
1bafb621 5166 }
e56ab892 5167 }
5168 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
5169 func_chunks_used = 1;
5170 p += 20;
5171 if (IS_START(g_func, "sub_")) {
5172 unsigned long addr = strtoul(p, NULL, 16);
5173 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
cdfaeed7 5174 if (addr > f_addr && !scanned_ahead) {
94d447fb 5175 anote("scan_ahead caused by '%s', addr %lx\n",
5176 g_func, addr);
cdfaeed7 5177 scan_ahead(fasm);
5178 scanned_ahead = 1;
5179 func_chunks_sorted = 0;
5180 }
1bafb621 5181 }
5182 }
5183 continue;
e56ab892 5184 } // *p == ';'
1bafb621 5185
06c5d854 5186parse_words:
a2c1d768 5187 for (i = wordc; i < ARRAY_SIZE(words); i++)
5188 words[i][0] = 0;
cdfaeed7 5189 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
bfa4a6ee 5190 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
91977a1c 5191 if (*p == 0 || *p == ';') {
5192 wordc++;
5193 break;
5194 }
5195 }
a2c1d768 5196 if (*p != 0 && *p != ';')
5197 aerr("too many words\n");
91977a1c 5198
06c5d854 5199 // alow asm patches in comments
ddaf8bd7 5200 if (*p == ';') {
5201 if (IS_START(p, "; sctpatch:")) {
5202 p = sskip(p + 11);
5203 if (*p == 0 || *p == ';')
5204 continue;
5205 goto parse_words; // lame
5206 }
5207 if (IS_START(p, "; sctproto:")) {
5208 sctproto = strdup(p + 11);
5209 }
46b388c2 5210 else if (IS_START(p, "; sctend")) {
5211 end = 1;
5212 if (!pending_endp)
5213 break;
5214 }
06c5d854 5215 }
5216
91977a1c 5217 if (wordc == 0) {
5218 // shouldn't happen
5219 awarn("wordc == 0?\n");
5220 continue;
5221 }
5222
5223 // don't care about this:
5224 if (words[0][0] == '.'
5225 || IS(words[0], "include")
5226 || IS(words[0], "assume") || IS(words[1], "segment")
5227 || IS(words[0], "align"))
5228 {
5229 continue;
5230 }
5231
4c45fa73 5232do_pending_endp:
5233 // do delayed endp processing to collect switch jumptables
5234 if (pending_endp) {
46b388c2 5235 if (in_func && !skip_func && !end && wordc >= 2
4c45fa73 5236 && ((words[0][0] == 'd' && words[0][2] == 0)
5237 || (words[1][0] == 'd' && words[1][2] == 0)))
5238 {
5239 i = 1;
5240 if (words[1][0] == 'd' && words[1][2] == 0) {
5241 // label
5242 if (g_func_pd_cnt >= pd_alloc) {
5243 pd_alloc = pd_alloc * 2 + 16;
5244 g_func_pd = realloc(g_func_pd,
5245 sizeof(g_func_pd[0]) * pd_alloc);
5246 my_assert_not(g_func_pd, NULL);
5247 }
5248 pd = &g_func_pd[g_func_pd_cnt];
5249 g_func_pd_cnt++;
5250 memset(pd, 0, sizeof(*pd));
5251 strcpy(pd->label, words[0]);
5252 pd->type = OPT_CONST;
5253 pd->lmod = lmod_from_directive(words[1]);
5254 i = 2;
5255 }
5256 else {
da87ae38 5257 if (pd == NULL) {
5258 if (verbose)
5259 anote("skipping alignment byte?\n");
5260 continue;
5261 }
4c45fa73 5262 lmod = lmod_from_directive(words[0]);
5263 if (lmod != pd->lmod)
5264 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
5265 }
5266
5267 if (pd->count_alloc < pd->count + wordc) {
5268 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
5269 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
5270 my_assert_not(pd->d, NULL);
5271 }
5272 for (; i < wordc; i++) {
5273 if (IS(words[i], "offset")) {
5274 pd->type = OPT_OFFSET;
5275 i++;
5276 }
5277 p = strchr(words[i], ',');
5278 if (p != NULL)
5279 *p = 0;
5280 if (pd->type == OPT_OFFSET)
5281 pd->d[pd->count].u.label = strdup(words[i]);
5282 else
5283 pd->d[pd->count].u.val = parse_number(words[i]);
5284 pd->d[pd->count].bt_i = -1;
5285 pd->count++;
5286 }
5287 continue;
5288 }
5289
5290 if (in_func && !skip_func)
5291 gen_func(fout, g_fhdr, g_func, pi);
5292
5293 pending_endp = 0;
5294 in_func = 0;
5295 g_ida_func_attr = 0;
5296 skip_warned = 0;
5297 skip_func = 0;
5298 g_func[0] = 0;
e56ab892 5299 func_chunks_used = 0;
5300 func_chunk_i = -1;
4c45fa73 5301 if (pi != 0) {
5302 memset(&ops, 0, pi * sizeof(ops[0]));
5303 memset(g_labels, 0, pi * sizeof(g_labels[0]));
5304 pi = 0;
5305 }
5306 g_eqcnt = 0;
5307 for (i = 0; i < g_func_pd_cnt; i++) {
5308 pd = &g_func_pd[i];
5309 if (pd->type == OPT_OFFSET) {
5310 for (j = 0; j < pd->count; j++)
5311 free(pd->d[j].u.label);
5312 }
5313 free(pd->d);
5314 pd->d = NULL;
5315 }
5316 g_func_pd_cnt = 0;
5317 pd = NULL;
46b388c2 5318
5319 if (end)
5320 break;
4c45fa73 5321 if (wordc == 0)
5322 continue;
5323 }
5324
91977a1c 5325 if (IS(words[1], "proc")) {
5326 if (in_func)
5327 aerr("proc '%s' while in_func '%s'?\n",
5328 words[0], g_func);
bfa4a6ee 5329 p = words[0];
ddaf8bd7 5330 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
bfa4a6ee 5331 skip_func = 1;
91977a1c 5332 strcpy(g_func, words[0]);
d4e3b5db 5333 set_label(0, words[0]);
91977a1c 5334 in_func = 1;
5335 continue;
5336 }
5337
e56ab892 5338 if (IS(words[1], "endp"))
5339 {
91977a1c 5340 if (!in_func)
5341 aerr("endp '%s' while not in_func?\n", words[0]);
5342 if (!IS(g_func, words[0]))
5343 aerr("endp '%s' while in_func '%s'?\n",
5344 words[0], g_func);
bfa4a6ee 5345
ddaf8bd7 5346 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
5347 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
5348 {
5349 // import jump
5350 skip_func = 1;
5351 }
5352
e56ab892 5353 if (!skip_func && func_chunks_used) {
5354 // start processing chunks
5355 struct chunk_item *ci, key = { g_func, 0 };
5356
5357 func_chunk_ret = ftell(fasm);
de50b98b 5358 func_chunk_ret_ln = asmln;
e56ab892 5359 if (!func_chunks_sorted) {
5360 qsort(func_chunks, func_chunk_cnt,
5361 sizeof(func_chunks[0]), cmp_chunks);
5362 func_chunks_sorted = 1;
5363 }
5364 ci = bsearch(&key, func_chunks, func_chunk_cnt,
5365 sizeof(func_chunks[0]), cmp_chunks);
5366 if (ci == NULL)
5367 aerr("'%s' needs chunks, but none found\n", g_func);
5368 func_chunk_i = ci - func_chunks;
5369 for (; func_chunk_i > 0; func_chunk_i--)
5370 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
5371 break;
5372
5373 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5374 if (ret)
5375 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
de50b98b 5376 asmln = func_chunks[func_chunk_i].asmln;
e56ab892 5377 func_chunk_i++;
5378 continue;
5379 }
4c45fa73 5380 pending_endp = 1;
91977a1c 5381 continue;
5382 }
5383
1f84f6b3 5384 if (wordc == 2 && IS(words[1], "ends")) {
46b388c2 5385 if (!multi_seg) {
5386 end = 1;
5387 if (pending_endp)
5388 goto do_pending_endp;
1f84f6b3 5389 break;
46b388c2 5390 }
1f84f6b3 5391
5392 // scan for next text segment
5393 while (fgets(line, sizeof(line), fasm)) {
5394 asmln++;
5395 p = sskip(line);
5396 if (*p == 0 || *p == ';')
5397 continue;
5398
5399 if (strstr(p, "segment para public 'CODE' use32"))
5400 break;
5401 }
5402
5403 continue;
5404 }
a2c1d768 5405
bfa4a6ee 5406 p = strchr(words[0], ':');
5407 if (p != NULL) {
d4e3b5db 5408 set_label(pi, words[0]);
bfa4a6ee 5409 continue;
5410 }
5411
5412 if (!in_func || skip_func) {
5413 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
5414 if (verbose)
5415 anote("skipping from '%s'\n", g_labels[pi]);
5416 skip_warned = 1;
5417 }
5418 g_labels[pi][0] = 0;
5419 continue;
5420 }
5421
ddaf8bd7 5422 if (wordc > 1 && IS(words[1], "="))
5423 {
91977a1c 5424 if (wordc != 5)
5425 aerr("unhandled equ, wc=%d\n", wordc);
5426 if (g_eqcnt >= eq_alloc) {
5427 eq_alloc *= 2;
5428 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
5429 my_assert_not(g_eqs, NULL);
5430 }
5431
5432 len = strlen(words[0]);
5433 if (len > sizeof(g_eqs[0].name) - 1)
5434 aerr("equ name too long: %d\n", len);
5435 strcpy(g_eqs[g_eqcnt].name, words[0]);
5436
5437 if (!IS(words[3], "ptr"))
5438 aerr("unhandled equ\n");
5439 if (IS(words[2], "dword"))
5440 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
5441 else if (IS(words[2], "word"))
5442 g_eqs[g_eqcnt].lmod = OPLM_WORD;
5443 else if (IS(words[2], "byte"))
5444 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
5445 else
5446 aerr("bad lmod: '%s'\n", words[2]);
5447
5448 g_eqs[g_eqcnt].offset = parse_number(words[4]);
5449 g_eqcnt++;
5450 continue;
5451 }
5452
5453 if (pi >= ARRAY_SIZE(ops))
5454 aerr("too many ops\n");
5455
91977a1c 5456 parse_op(&ops[pi], words, wordc);
ddaf8bd7 5457
5458 if (sctproto != NULL) {
8eb12e72 5459 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
ddaf8bd7 5460 ops[pi].datap = sctproto;
5461 sctproto = NULL;
5462 }
91977a1c 5463 pi++;
91977a1c 5464 }
5465
5466 fclose(fout);
5467 fclose(fasm);
06c5d854 5468 fclose(g_fhdr);
91977a1c 5469
5470 return 0;
c36e914d 5471}
91977a1c 5472
5473// vim:ts=2:shiftwidth=2:expandtab