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