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