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