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