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