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