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