minor fixes
[ia32rtools.git] / tools / translate.c
1 /*
2  * ia32rtools
3  * (C) notaz, 2013-2015
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  * recognized asm hint comments:
9  * sctattr - function attributes (see code)
10  * sctend  - force end of function/chunk
11  * sctpatch: <p> - replace current asm line with <p>
12  * sctproto: <p> - prototype of ref'd function or struct
13  * sctref  - variable is referenced, make global
14  * sctskip_start - start of skipped code chunk (inclusive)
15  * sctskip_end   - end of skipped code chunk (inclusive)
16  */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <math.h>
24 #include <errno.h>
25
26 #include "my_assert.h"
27 #include "my_str.h"
28 #include "common.h"
29
30 #include "protoparse.h"
31
32 static const char *asmfn;
33 static int asmln;
34 static FILE *g_fhdr;
35
36 #define anote(fmt, ...) \
37         printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
38 #define awarn(fmt, ...) \
39         printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
40 #define aerr(fmt, ...) do { \
41         printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
42   fcloseall(); \
43         exit(1); \
44 } while (0)
45
46 #include "masm_tools.h"
47
48 enum op_flags {
49   OPF_RMD    = (1 << 0), /* removed from code generation */
50   OPF_DATA   = (1 << 1), /* data processing - writes to dst opr */
51   OPF_FLAGS  = (1 << 2), /* sets flags */
52   OPF_JMP    = (1 << 3), /* branch, call */
53   OPF_CJMP   = (1 << 4), /* cond. branch (cc or jecxz/loop) */
54   OPF_CC     = (1 << 5), /* uses flags */
55   OPF_TAIL   = (1 << 6), /* ret or tail call */
56   OPF_RSAVE  = (1 << 7), /* push/pop is local reg save/load */
57   OPF_REP    = (1 << 8), /* prefixed by rep */
58   OPF_REPZ   = (1 << 9), /* rep is repe/repz */
59   OPF_REPNZ  = (1 << 10), /* rep is repne/repnz */
60   OPF_FARG   = (1 << 11), /* push collected as func arg */
61   OPF_FARGNR = (1 << 12), /* push collected as func arg (no reuse) */
62   OPF_EBP_S  = (1 << 13), /* ebp used as scratch here, not BP */
63   OPF_DF     = (1 << 14), /* DF flag set */
64   OPF_ATAIL  = (1 << 15), /* tail call with reused arg frame */
65   OPF_32BIT  = (1 << 16), /* enough to do 32bit for this op */
66   OPF_LOCK   = (1 << 17), /* op has lock prefix */
67   OPF_VAPUSH = (1 << 18), /* vararg ptr push (as call arg) */
68   OPF_DONE   = (1 << 19), /* already fully handled by analysis */
69   OPF_PPUSH  = (1 << 20), /* part of complex push-pop graph */
70   OPF_NOREGS = (1 << 21), /* don't track regs of this op */
71   OPF_FPUSH  = (1 << 22), /* pushes x87 stack */
72   OPF_FPOP   = (1 << 23), /* pops x87 stack */
73   OPF_FPOPP  = (1 << 24), /* pops x87 stack twice */
74   OPF_FSHIFT = (1 << 25), /* x87 stack shift is actually needed */
75   OPF_FINT   = (1 << 26), /* integer float op arg */
76 };
77
78 enum op_op {
79         OP_INVAL,
80         OP_NOP,
81         OP_PUSH,
82         OP_POP,
83         OP_PUSHA,
84         OP_POPA,
85         OP_LEAVE,
86         OP_MOV,
87         OP_LEA,
88         OP_MOVZX,
89         OP_MOVSX,
90         OP_XCHG,
91         OP_NOT,
92         OP_XLAT,
93         OP_CDQ,
94         OP_BSWAP,
95         OP_LODS,
96         OP_STOS,
97         OP_MOVS,
98         OP_CMPS,
99         OP_SCAS,
100         OP_RDTSC,
101         OP_CPUID,
102         OP_STD,
103         OP_CLD,
104         OP_RET,
105         OP_ADD,
106         OP_SUB,
107         OP_AND,
108         OP_OR,
109         OP_XOR,
110         OP_SHL,
111         OP_SHR,
112         OP_SAR,
113         OP_SHLD,
114         OP_SHRD,
115         OP_ROL,
116         OP_ROR,
117         OP_RCL,
118         OP_RCR,
119         OP_ADC,
120         OP_SBB,
121         OP_BSF,
122         OP_BSR,
123         OP_INC,
124         OP_DEC,
125         OP_NEG,
126         OP_MUL,
127         OP_IMUL,
128         OP_DIV,
129         OP_IDIV,
130         OP_TEST,
131         OP_CMP,
132         OP_CALL,
133         OP_JMP,
134         OP_JECXZ,
135         OP_LOOP,
136         OP_JCC,
137         OP_SCC,
138   // x87
139   OP_FLD,
140   OP_FILD,
141   OP_FLDc,
142   OP_FST,
143   OP_FIST,
144   OP_FABS,
145   OP_FADD,
146   OP_FDIV,
147   OP_FMUL,
148   OP_FSUB,
149   OP_FDIVR,
150   OP_FSUBR,
151   OP_FIADD,
152   OP_FIDIV,
153   OP_FIMUL,
154   OP_FISUB,
155   OP_FIDIVR,
156   OP_FISUBR,
157   OP_FCOM,
158   OP_FNSTSW,
159   OP_FCHS,
160   OP_FCOS,
161   OP_FPATAN,
162   OP_FPTAN,
163   OP_FSIN,
164   OP_FSQRT,
165   OP_FXCH,
166   OP_FYL2X,
167   // mmx
168   OP_EMMS,
169   // pseudo-ops for lib calls
170   OPP_ALLSHL,
171   OPP_ALLSHR,
172   OPP_FTOL,
173   OPP_CIPOW,
174   OPP_ABORT,
175   // undefined
176   OP_UD2,
177 };
178
179 enum opr_type {
180   OPT_UNSPEC,
181   OPT_REG,
182   OPT_REGMEM,
183   OPT_LABEL,
184   OPT_OFFSET,
185   OPT_CONST,
186 };
187
188 // must be sorted (larger len must be further in enum)
189 enum opr_lenmod {
190         OPLM_UNSPEC,
191         OPLM_BYTE,
192         OPLM_WORD,
193         OPLM_DWORD,
194         OPLM_QWORD,
195 };
196
197 #define MAX_EXITS 128
198
199 #define MAX_OPERANDS 3
200 #define NAMELEN 112
201
202 #define OPR_INIT(type_, lmod_, reg_) \
203   { type_, lmod_, reg_, }
204
205 struct parsed_opr {
206   enum opr_type type;
207   enum opr_lenmod lmod;
208   int reg;
209   unsigned int is_ptr:1;   // pointer in C
210   unsigned int is_array:1; // array in C
211   unsigned int type_from_var:1; // .. in header, sometimes wrong
212   unsigned int size_mismatch:1; // type override differs from C
213   unsigned int size_lt:1;  // type override is larger than C
214   unsigned int segment:7;  // had segment override (enum segment)
215   const struct parsed_proto *pp; // for OPT_LABEL
216   unsigned int val;
217   char name[NAMELEN];
218 };
219
220 struct parsed_op {
221   enum op_op op;
222   struct parsed_opr operand[MAX_OPERANDS];
223   unsigned int flags;
224   unsigned char pfo;
225   unsigned char pfo_inv;
226   unsigned char operand_cnt;
227   unsigned char p_argnum; // arg push: call's saved arg #
228   unsigned char p_arggrp; // arg push: arg group # for above
229   unsigned char p_argpass;// arg push: arg of host func
230   short pad;
231   int regmask_src;        // all referensed regs
232   int regmask_dst;
233   int pfomask;            // flagop: parsed_flag_op that can't be delayed
234   int cc_scratch;         // scratch storage during analysis
235   int bt_i;               // branch target for branches
236   struct parsed_data *btj;// branch targets for jumptables
237   struct parsed_proto *pp;// parsed_proto for OP_CALL
238   void *datap;
239   int asmln;
240 };
241
242 // datap:
243 // on start:  function/data type hint (sctproto)
244 // after analysis:
245 // (OPF_CC) - points to one of (OPF_FLAGS) that affects cc op
246 // OP_PUSH  - points to OP_POP in complex push/pop graph
247 // OP_POP   - points to OP_PUSH in simple push/pop pair
248 // OP_FCOM  - needed_status_word_bits | (is_z_check << 16)
249
250 struct parsed_equ {
251   char name[64];
252   enum opr_lenmod lmod;
253   int offset;
254 };
255
256 struct parsed_data {
257   char label[256];
258   enum opr_type type;
259   enum opr_lenmod lmod;
260   int count;
261   int count_alloc;
262   struct {
263     union {
264       char *label;
265       unsigned int val;
266     } u;
267     int bt_i;
268   } *d;
269 };
270
271 struct label_ref {
272   int i;
273   struct label_ref *next;
274 };
275
276 enum ida_func_attr {
277   IDAFA_BP_FRAME = (1 << 0),
278   IDAFA_LIB_FUNC = (1 << 1),
279   IDAFA_STATIC   = (1 << 2),
280   IDAFA_NORETURN = (1 << 3),
281   IDAFA_THUNK    = (1 << 4),
282   IDAFA_FPD      = (1 << 5),
283 };
284
285 // sctattr
286 enum sct_func_attr {
287   SCTFA_CLEAR_SF   = (1 << 0), // clear stack frame
288   SCTFA_CLEAR_REGS = (1 << 1), // clear registers (mask)
289   SCTFA_RM_REGS    = (1 << 2), // don't emit regs (mask)
290   SCTFA_NOWARN     = (1 << 3), // don't try to detect problems
291   SCTFA_ARGFRAME   = (1 << 4), // copy all args to a struct, in order
292   SCTFA_UA_FLOAT   = (1 << 5), // emit float i/o helpers for alignemnt
293 };
294
295 enum x87_const {
296   X87_CONST_1 = 1,
297   X87_CONST_L2T,
298   X87_CONST_L2E,
299   X87_CONST_PI,
300   X87_CONST_LG2,
301   X87_CONST_LN2,
302   X87_CONST_Z,
303 };
304
305 enum segment {
306   SEG_CS = 1,
307   SEG_DS,
308   SEG_SS,
309   SEG_ES,
310   SEG_FS,
311   SEG_GS,
312 };
313
314 #define MAX_OPS     4096
315 #define MAX_ARG_GRP 2
316
317 static struct parsed_op ops[MAX_OPS];
318 static struct parsed_equ *g_eqs;
319 static int g_eqcnt;
320 static char *g_labels[MAX_OPS];
321 static struct label_ref g_label_refs[MAX_OPS];
322 static const struct parsed_proto *g_func_pp;
323 static struct parsed_data *g_func_pd;
324 static int g_func_pd_cnt;
325 static int g_func_lmods;
326 static char g_func[256];
327 static char g_comment[256];
328 static int g_bp_frame;
329 static int g_sp_frame;
330 static int g_stack_frame_used;
331 static int g_stack_fsz;
332 static int g_seh_found;
333 static int g_seh_size;
334 static int g_ida_func_attr;
335 static int g_sct_func_attr;
336 static int g_stack_clear_start; // in dwords
337 static int g_stack_clear_len;
338 static int g_regmask_init;
339 static int g_regmask_rm;
340 static int g_skip_func;
341 static int g_allow_regfunc;
342 static int g_allow_user_icall;
343 static int g_nowarn_reguse;
344 static int g_quiet_pp;
345 static int g_header_mode;
346
347 #define ferr(op_, fmt, ...) do { \
348   printf("%s:%d: error %u: [%s] '%s': " fmt, asmfn, (op_)->asmln, \
349     __LINE__, g_func, dump_op(op_), ##__VA_ARGS__); \
350   fcloseall(); \
351   exit(1); \
352 } while (0)
353 #define fnote(op_, fmt, ...) \
354   printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
355     dump_op(op_), ##__VA_ARGS__)
356
357 #define ferr_assert(op_, cond) do { \
358   if (!(cond)) ferr(op_, "assertion '%s' failed\n", #cond); \
359 } while (0)
360
361 #define IS_OP_INDIRECT_CALL(op_) \
362   ((op_)->op == OP_CALL && (op_)->operand[0].type != OPT_LABEL)
363
364 const char *regs_r32[] = {
365   "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
366   // not r32, but list here for easy parsing and printing
367   "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
368   "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)"
369 };
370 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
371 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
372 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
373
374 enum x86_regs {
375   xUNSPEC = -1,
376   xAX, xBX, xCX, xDX,
377   xSI, xDI, xBP, xSP,
378   xMM0, xMM1, xMM2, xMM3, // mmx
379   xMM4, xMM5, xMM6, xMM7,
380   xST0, xST1, xST2, xST3, // x87
381   xST4, xST5, xST6, xST7,
382 };
383
384 #define mxAX     (1 << xAX)
385 #define mxBX     (1 << xBX)
386 #define mxCX     (1 << xCX)
387 #define mxDX     (1 << xDX)
388 #define mxSP     (1 << xSP)
389 #define mxST0    (1 << xST0)
390 #define mxST1    (1 << xST1)
391 #define mxST1_0  (mxST1 | mxST0)
392 #define mxST7_2  (0xfc << xST0)
393 #define mxSTa    (0xff << xST0)
394
395 // possible basic comparison types (without inversion)
396 enum parsed_flag_op {
397   PFO_O,  // 0 OF=1
398   PFO_C,  // 2 CF=1
399   PFO_Z,  // 4 ZF=1
400   PFO_BE, // 6 CF=1||ZF=1
401   PFO_S,  // 8 SF=1
402   PFO_P,  // a PF=1
403   PFO_L,  // c SF!=OF
404   PFO_LE, // e ZF=1||SF!=OF
405 };
406
407 #define PFOB_O   (1 << PFO_O)
408 #define PFOB_C   (1 << PFO_C)
409 #define PFOB_Z   (1 << PFO_Z)
410 #define PFOB_S   (1 << PFO_S)
411
412 static const char *parsed_flag_op_names[] = {
413   "o", "c", "z", "be", "s", "p", "l", "le"
414 };
415
416 static int char_array_i(const char *array[], size_t len, const char *s)
417 {
418   int i;
419
420   for (i = 0; i < len; i++)
421     if (IS(s, array[i]))
422       return i;
423
424   return -1;
425 }
426
427 static void printf_number(char *buf, size_t buf_size,
428   unsigned long number)
429 {
430   // output in C-friendly form
431   snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
432 }
433
434 static int check_segment_prefix(const char *s)
435 {
436   if (s[0] == 0 || s[1] != 's' || s[2] != ':')
437     return 0;
438
439   switch (s[0]) {
440   case 'c': return SEG_CS;
441   case 'd': return SEG_DS;
442   case 's': return SEG_SS;
443   case 'e': return SEG_ES;
444   case 'f': return SEG_FS;
445   case 'g': return SEG_GS;
446   default:  return 0;
447   }
448 }
449
450 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
451 {
452   int reg;
453
454   reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
455   if (reg >= 8) {
456     *reg_lmod = OPLM_QWORD;
457     return reg;
458   }
459   if (reg >= 0) {
460     *reg_lmod = OPLM_DWORD;
461     return reg;
462   }
463   reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
464   if (reg >= 0) {
465     *reg_lmod = OPLM_WORD;
466     return reg;
467   }
468   reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
469   if (reg >= 0) {
470     *reg_lmod = OPLM_BYTE;
471     return reg;
472   }
473   reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
474   if (reg >= 0) {
475     *reg_lmod = OPLM_BYTE;
476     return reg;
477   }
478
479   return -1;
480 }
481
482 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
483 {
484   enum opr_lenmod lmod;
485   char cvtbuf[256];
486   char *d = cvtbuf;
487   char *s = name;
488   char w[64];
489   long number;
490   int reg;
491   int c = 0;
492
493   *d = 0;
494
495   while (*s != 0) {
496     d += strlen(d);
497     while (my_isblank(*s))
498       s++;
499     for (; my_issep(*s); d++, s++)
500       *d = *s;
501     while (my_isblank(*s))
502       s++;
503     *d = 0;
504
505     // skip '?s:' prefixes
506     if (check_segment_prefix(s))
507       s += 3;
508
509     s = next_idt(w, sizeof(w), s);
510     if (w[0] == 0)
511       break;
512     c++;
513
514     reg = parse_reg(&lmod, w);
515     if (reg >= 0) {
516       *regmask |= 1 << reg;
517       goto pass;
518     }
519
520     if ('0' <= w[0] && w[0] <= '9') {
521       number = parse_number(w, 0);
522       printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
523       continue;
524     }
525
526     // probably some label/identifier - pass
527
528 pass:
529     snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
530   }
531
532   if (need_c_cvt)
533     strcpy(name, cvtbuf);
534
535   return c;
536 }
537
538 static int is_reg_in_str(const char *s)
539 {
540   int i;
541
542   if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
543     return 0;
544
545   for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
546     if (!strncmp(s, regs_r32[i], 3))
547       return 1;
548
549   return 0;
550 }
551
552 static const char *parse_stack_el(const char *name, char *extra_reg,
553   int *base_val, int early_try)
554 {
555   const char *p, *p2, *s;
556   char *endp = NULL;
557   char buf[32];
558   long val = -1;
559   int len;
560
561   if (g_bp_frame || early_try)
562   {
563     p = name;
564     if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
565       p += 4;
566       if (extra_reg != NULL) {
567         strncpy(extra_reg, name, 3);
568         extra_reg[4] = 0;
569       }
570     }
571
572     if (IS_START(p, "ebp+")) {
573       p += 4;
574
575       p2 = strchr(p, '+');
576       if (p2 != NULL && is_reg_in_str(p)) {
577         if (extra_reg != NULL) {
578           strncpy(extra_reg, p, p2 - p);
579           extra_reg[p2 - p] = 0;
580         }
581         p = p2 + 1;
582       }
583
584       if (!('0' <= *p && *p <= '9'))
585         return p;
586
587       return NULL;
588     }
589   }
590
591   if (!IS_START(name, "esp+"))
592     return NULL;
593
594   s = name + 4;
595   p = strchr(s, '+');
596   if (p) {
597     if (is_reg_in_str(s)) {
598       if (extra_reg != NULL) {
599         strncpy(extra_reg, s, p - s);
600         extra_reg[p - s] = 0;
601       }
602       s = p + 1;
603       p = strchr(s, '+');
604       if (p == NULL)
605         aerr("%s IDA stackvar not set?\n", __func__);
606     }
607     if ('0' <= *s && *s <= '9') {
608       if (s[0] == '0' && s[1] == 'x')
609         s += 2;
610       len = p - s;
611       if (len < sizeof(buf) - 1) {
612         strncpy(buf, s, len);
613         buf[len] = 0;
614         errno = 0;
615         val = strtol(buf, &endp, 16);
616         if (val == 0 || *endp != 0 || errno != 0) {
617           aerr("%s num parse fail for '%s'\n", __func__, buf);
618           return NULL;
619         }
620       }
621       p++;
622     }
623     else {
624       // probably something like [esp+arg_4+2]
625       p = s;
626       val = 0;
627     }
628   }
629   else
630     p = name + 4;
631
632   if ('0' <= *p && *p <= '9')
633     return NULL;
634
635   if (base_val != NULL)
636     *base_val = val;
637   return p;
638 }
639
640 static int guess_lmod_from_name(struct parsed_opr *opr)
641 {
642   if (IS_START(opr->name, "dword_") || IS_START(opr->name, "off_")) {
643     opr->lmod = OPLM_DWORD;
644     return 1;
645   }
646   if (IS_START(opr->name, "word_")) {
647     opr->lmod = OPLM_WORD;
648     return 1;
649   }
650   if (IS_START(opr->name, "byte_")) {
651     opr->lmod = OPLM_BYTE;
652     return 1;
653   }
654   if (IS_START(opr->name, "qword_")) {
655     opr->lmod = OPLM_QWORD;
656     return 1;
657   }
658   return 0;
659 }
660
661 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
662   const struct parsed_type *c_type)
663 {
664   static const char *qword_types[] = {
665     "uint64_t", "int64_t", "__int64",
666   };
667   static const char *dword_types[] = {
668     "uint32_t", "int", "_DWORD", "UINT_PTR", "DWORD",
669     "WPARAM", "LPARAM", "UINT", "__int32",
670     "LONG", "HIMC", "BOOL", "size_t",
671     "float",
672   };
673   static const char *word_types[] = {
674     "uint16_t", "int16_t", "_WORD", "WORD",
675     "unsigned __int16", "__int16",
676   };
677   static const char *byte_types[] = {
678     "uint8_t", "int8_t", "char",
679     "unsigned __int8", "__int8", "BYTE", "_BYTE",
680     "CHAR", "_UNKNOWN",
681     // structures.. deal the same as with _UNKNOWN for now
682     "CRITICAL_SECTION",
683   };
684   const char *n;
685   int i;
686
687   if (c_type->is_ptr) {
688     *lmod = OPLM_DWORD;
689     return 1;
690   }
691
692   n = skip_type_mod(c_type->name);
693
694   for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
695     if (IS(n, dword_types[i])) {
696       *lmod = OPLM_DWORD;
697       return 1;
698     }
699   }
700
701   for (i = 0; i < ARRAY_SIZE(word_types); i++) {
702     if (IS(n, word_types[i])) {
703       *lmod = OPLM_WORD;
704       return 1;
705     }
706   }
707
708   for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
709     if (IS(n, byte_types[i])) {
710       *lmod = OPLM_BYTE;
711       return 1;
712     }
713   }
714
715   for (i = 0; i < ARRAY_SIZE(qword_types); i++) {
716     if (IS(n, qword_types[i])) {
717       *lmod = OPLM_QWORD;
718       return 1;
719     }
720   }
721
722   return 0;
723 }
724
725 static char *default_cast_to(char *buf, size_t buf_size,
726   struct parsed_opr *opr)
727 {
728   buf[0] = 0;
729
730   if (!opr->is_ptr || strchr(opr->name, '['))
731     return buf;
732   if (opr->pp == NULL || opr->pp->type.name == NULL
733     || opr->pp->is_fptr)
734   {
735     snprintf(buf, buf_size, "%s", "(void *)");
736     return buf;
737   }
738
739   snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
740   return buf;
741 }
742
743 static enum opr_type lmod_from_directive(const char *d)
744 {
745   if (IS(d, "dd"))
746     return OPLM_DWORD;
747   else if (IS(d, "dw"))
748     return OPLM_WORD;
749   else if (IS(d, "db"))
750     return OPLM_BYTE;
751
752   aerr("unhandled directive: '%s'\n", d);
753   return OPLM_UNSPEC;
754 }
755
756 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
757   int *regmask)
758 {
759   opr->type = OPT_REG;
760   opr->reg = reg;
761   opr->lmod = lmod;
762   *regmask |= 1 << reg;
763 }
764
765 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
766   int *extra_offs);
767
768 static int parse_operand(struct parsed_opr *opr,
769   int *regmask, int *regmask_indirect,
770   char words[16][256], int wordc, int w, unsigned int op_flags)
771 {
772   const struct parsed_proto *pp = NULL;
773   enum opr_lenmod tmplmod;
774   unsigned long number;
775   char buf[256];
776   int ret, len;
777   int wordc_in;
778   char *p;
779   int i;
780
781   if (w >= wordc)
782     aerr("parse_operand w %d, wordc %d\n", w, wordc);
783
784   opr->reg = xUNSPEC;
785
786   for (i = w; i < wordc; i++) {
787     len = strlen(words[i]);
788     if (words[i][len - 1] == ',') {
789       words[i][len - 1] = 0;
790       wordc = i + 1;
791       break;
792     }
793   }
794
795   wordc_in = wordc - w;
796
797   if ((op_flags & OPF_JMP) && wordc_in > 0
798       && !('0' <= words[w][0] && words[w][0] <= '9'))
799   {
800     const char *label = NULL;
801
802     if (wordc_in == 3 && !strncmp(words[w], "near", 4)
803      && IS(words[w + 1], "ptr"))
804       label = words[w + 2];
805     else if (wordc_in == 2 && IS(words[w], "short"))
806       label = words[w + 1];
807     else if (wordc_in == 1
808           && strchr(words[w], '[') == NULL
809           && parse_reg(&tmplmod, words[w]) < 0)
810       label = words[w];
811
812     if (label != NULL) {
813       opr->type = OPT_LABEL;
814       ret = check_segment_prefix(label);
815       if (ret != 0) {
816         opr->segment = ret;
817         label += 3;
818       }
819       strcpy(opr->name, label);
820       return wordc;
821     }
822   }
823
824   if (wordc_in >= 3) {
825     if (IS(words[w + 1], "ptr")) {
826       if (IS(words[w], "dword"))
827         opr->lmod = OPLM_DWORD;
828       else if (IS(words[w], "word"))
829         opr->lmod = OPLM_WORD;
830       else if (IS(words[w], "byte"))
831         opr->lmod = OPLM_BYTE;
832       else if (IS(words[w], "qword"))
833         opr->lmod = OPLM_QWORD;
834       else
835         aerr("type parsing failed\n");
836       w += 2;
837       wordc_in = wordc - w;
838     }
839   }
840
841   if (wordc_in == 2) {
842     if (IS(words[w], "offset")) {
843       opr->type = OPT_OFFSET;
844       opr->lmod = OPLM_DWORD;
845       strcpy(opr->name, words[w + 1]);
846       pp = proto_parse(g_fhdr, opr->name, 1);
847       goto do_label;
848     }
849     if (IS(words[w], "(offset")) {
850       p = strchr(words[w + 1], ')');
851       if (p == NULL)
852         aerr("parse of bracketed offset failed\n");
853       *p = 0;
854       opr->type = OPT_OFFSET;
855       strcpy(opr->name, words[w + 1]);
856       return wordc;
857     }
858   }
859
860   if (wordc_in != 1)
861     aerr("parse_operand 1 word expected\n");
862
863   ret = check_segment_prefix(words[w]);
864   if (ret != 0) {
865     opr->segment = ret;
866     memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
867     if (ret == SEG_FS && IS(words[w], "0"))
868       g_seh_found = 1;
869   }
870   strcpy(opr->name, words[w]);
871
872   if (words[w][0] == '[') {
873     opr->type = OPT_REGMEM;
874     ret = sscanf(words[w], "[%[^]]]", opr->name);
875     if (ret != 1)
876       aerr("[] parse failure\n");
877
878     parse_indmode(opr->name, regmask_indirect, 1);
879     if (opr->lmod == OPLM_UNSPEC
880       && parse_stack_el(opr->name, NULL, NULL, 1))
881     {
882       // might be an equ
883       struct parsed_equ *eq =
884         equ_find(NULL, parse_stack_el(opr->name, NULL, NULL, 1), &i);
885       if (eq)
886         opr->lmod = eq->lmod;
887
888       // might be unaligned access
889       g_func_lmods |= 1 << OPLM_BYTE;
890     }
891     return wordc;
892   }
893   else if (strchr(words[w], '[')) {
894     // label[reg] form
895     p = strchr(words[w], '[');
896     opr->type = OPT_REGMEM;
897     parse_indmode(p, regmask_indirect, 0);
898     strncpy(buf, words[w], p - words[w]);
899     buf[p - words[w]] = 0;
900     pp = proto_parse(g_fhdr, buf, 1);
901     goto do_label;
902   }
903   else if (('0' <= words[w][0] && words[w][0] <= '9')
904     || words[w][0] == '-')
905   {
906     number = parse_number(words[w], 0);
907     opr->type = OPT_CONST;
908     opr->val = number;
909     printf_number(opr->name, sizeof(opr->name), number);
910     return wordc;
911   }
912
913   ret = parse_reg(&tmplmod, opr->name);
914   if (ret >= 0) {
915     setup_reg_opr(opr, ret, tmplmod, regmask);
916     return wordc;
917   }
918
919   // most likely var in data segment
920   opr->type = OPT_LABEL;
921   pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
922
923 do_label:
924   if (pp != NULL) {
925     if (pp->is_fptr || pp->is_func) {
926       opr->lmod = OPLM_DWORD;
927       opr->is_ptr = 1;
928     }
929     else {
930       tmplmod = OPLM_UNSPEC;
931       if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
932         anote("unhandled C type '%s' for '%s'\n",
933           pp->type.name, opr->name);
934       
935       if (opr->lmod == OPLM_UNSPEC) {
936         opr->lmod = tmplmod;
937         opr->type_from_var = 1;
938       }
939       else if (opr->lmod != tmplmod) {
940         opr->size_mismatch = 1;
941         if (tmplmod < opr->lmod)
942           opr->size_lt = 1;
943       }
944       opr->is_ptr = pp->type.is_ptr;
945     }
946     opr->is_array = pp->type.is_array;
947   }
948   opr->pp = pp;
949
950   if (opr->lmod == OPLM_UNSPEC)
951     guess_lmod_from_name(opr);
952   return wordc;
953 }
954
955 static const struct {
956   const char *name;
957   unsigned int flags;
958 } pref_table[] = {
959   { "rep",    OPF_REP },
960   { "repe",   OPF_REP|OPF_REPZ },
961   { "repz",   OPF_REP|OPF_REPZ },
962   { "repne",  OPF_REP|OPF_REPNZ },
963   { "repnz",  OPF_REP|OPF_REPNZ },
964   { "lock",   OPF_LOCK },
965 };
966
967 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
968
969 static const struct {
970   const char *name;
971   enum op_op op;
972   unsigned short minopr;
973   unsigned short maxopr;
974   unsigned int flags;
975   unsigned char pfo;
976   unsigned char pfo_inv;
977 } op_table[] = {
978   { "nop",  OP_NOP,    0, 0, 0 },
979   { "push", OP_PUSH,   1, 1, 0 },
980   { "pop",  OP_POP,    1, 1, OPF_DATA },
981   { "pusha",OP_PUSHA,  0, 0, 0 },
982   { "popa", OP_POPA,   0, 0, OPF_DATA },
983   { "leave",OP_LEAVE,  0, 0, OPF_DATA },
984   { "mov" , OP_MOV,    2, 2, OPF_DATA },
985   { "lea",  OP_LEA,    2, 2, OPF_DATA },
986   { "movzx",OP_MOVZX,  2, 2, OPF_DATA },
987   { "movsx",OP_MOVSX,  2, 2, OPF_DATA },
988   { "xchg", OP_XCHG,   2, 2, OPF_DATA },
989   { "not",  OP_NOT,    1, 1, OPF_DATA },
990   { "xlat", OP_XLAT,   0, 0, OPF_DATA },
991   { "cdq",  OP_CDQ,    0, 0, OPF_DATA },
992   { "bswap",OP_BSWAP,  1, 1, OPF_DATA },
993   { "lodsb",OP_LODS,   0, 0, OPF_DATA },
994   { "lodsw",OP_LODS,   0, 0, OPF_DATA },
995   { "lodsd",OP_LODS,   0, 0, OPF_DATA },
996   { "stosb",OP_STOS,   0, 0, OPF_DATA },
997   { "stosw",OP_STOS,   0, 0, OPF_DATA },
998   { "stosd",OP_STOS,   0, 0, OPF_DATA },
999   { "movsb",OP_MOVS,   0, 0, OPF_DATA },
1000   { "movsw",OP_MOVS,   0, 0, OPF_DATA },
1001   { "movsd",OP_MOVS,   0, 0, OPF_DATA },
1002   { "cmpsb",OP_CMPS,   0, 0, OPF_DATA|OPF_FLAGS },
1003   { "cmpsw",OP_CMPS,   0, 0, OPF_DATA|OPF_FLAGS },
1004   { "cmpsd",OP_CMPS,   0, 0, OPF_DATA|OPF_FLAGS },
1005   { "scasb",OP_SCAS,   0, 0, OPF_DATA|OPF_FLAGS },
1006   { "scasw",OP_SCAS,   0, 0, OPF_DATA|OPF_FLAGS },
1007   { "scasd",OP_SCAS,   0, 0, OPF_DATA|OPF_FLAGS },
1008   { "rdtsc",OP_RDTSC,  0, 0, OPF_DATA },
1009   { "cpuid",OP_CPUID,  0, 0, OPF_DATA },
1010   { "std",  OP_STD,    0, 0, OPF_DATA }, // special flag
1011   { "cld",  OP_CLD,    0, 0, OPF_DATA },
1012   { "add",  OP_ADD,    2, 2, OPF_DATA|OPF_FLAGS },
1013   { "sub",  OP_SUB,    2, 2, OPF_DATA|OPF_FLAGS },
1014   { "and",  OP_AND,    2, 2, OPF_DATA|OPF_FLAGS },
1015   { "or",   OP_OR,     2, 2, OPF_DATA|OPF_FLAGS },
1016   { "xor",  OP_XOR,    2, 2, OPF_DATA|OPF_FLAGS },
1017   { "shl",  OP_SHL,    2, 2, OPF_DATA|OPF_FLAGS },
1018   { "shr",  OP_SHR,    2, 2, OPF_DATA|OPF_FLAGS },
1019   { "sal",  OP_SHL,    2, 2, OPF_DATA|OPF_FLAGS },
1020   { "sar",  OP_SAR,    2, 2, OPF_DATA|OPF_FLAGS },
1021   { "shld", OP_SHLD,   3, 3, OPF_DATA|OPF_FLAGS },
1022   { "shrd", OP_SHRD,   3, 3, OPF_DATA|OPF_FLAGS },
1023   { "rol",  OP_ROL,    2, 2, OPF_DATA|OPF_FLAGS },
1024   { "ror",  OP_ROR,    2, 2, OPF_DATA|OPF_FLAGS },
1025   { "rcl",  OP_RCL,    2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
1026   { "rcr",  OP_RCR,    2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
1027   { "adc",  OP_ADC,    2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
1028   { "sbb",  OP_SBB,    2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
1029   { "bsf",  OP_BSF,    2, 2, OPF_DATA|OPF_FLAGS },
1030   { "bsr",  OP_BSR,    2, 2, OPF_DATA|OPF_FLAGS },
1031   { "inc",  OP_INC,    1, 1, OPF_DATA|OPF_FLAGS },
1032   { "dec",  OP_DEC,    1, 1, OPF_DATA|OPF_FLAGS },
1033   { "neg",  OP_NEG,    1, 1, OPF_DATA|OPF_FLAGS },
1034   { "mul",  OP_MUL,    1, 1, OPF_DATA|OPF_FLAGS },
1035   { "imul", OP_IMUL,   1, 3, OPF_DATA|OPF_FLAGS },
1036   { "div",  OP_DIV,    1, 1, OPF_DATA|OPF_FLAGS },
1037   { "idiv", OP_IDIV,   1, 1, OPF_DATA|OPF_FLAGS },
1038   { "test", OP_TEST,   2, 2, OPF_FLAGS },
1039   { "cmp",  OP_CMP,    2, 2, OPF_FLAGS },
1040   { "retn", OP_RET,    0, 1, OPF_TAIL },
1041   { "call", OP_CALL,   1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
1042   { "jmp",  OP_JMP,    1, 1, OPF_JMP },
1043   { "jecxz",OP_JECXZ,  1, 1, OPF_JMP|OPF_CJMP },
1044   { "loop", OP_LOOP,   1, 1, OPF_JMP|OPF_CJMP|OPF_DATA },
1045   { "jo",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_O,  0 }, // 70 OF=1
1046   { "jno",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_O,  1 }, // 71 OF=0
1047   { "jc",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_C,  0 }, // 72 CF=1
1048   { "jb",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_C,  0 }, // 72
1049   { "jnc",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_C,  1 }, // 73 CF=0
1050   { "jnb",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_C,  1 }, // 73
1051   { "jae",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_C,  1 }, // 73
1052   { "jz",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_Z,  0 }, // 74 ZF=1
1053   { "je",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_Z,  0 }, // 74
1054   { "jnz",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_Z,  1 }, // 75 ZF=0
1055   { "jne",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_Z,  1 }, // 75
1056   { "jbe",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
1057   { "jna",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
1058   { "ja",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
1059   { "jnbe", OP_JCC,    1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
1060   { "js",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_S,  0 }, // 78 SF=1
1061   { "jns",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_S,  1 }, // 79 SF=0
1062   { "jp",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_P,  0 }, // 7a PF=1
1063   { "jpe",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_P,  0 }, // 7a
1064   { "jnp",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_P,  1 }, // 7b PF=0
1065   { "jpo",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_P,  1 }, // 7b
1066   { "jl",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_L,  0 }, // 7c SF!=OF
1067   { "jnge", OP_JCC,    1, 1, OPF_CJMP_CC, PFO_L,  0 }, // 7c
1068   { "jge",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_L,  1 }, // 7d SF=OF
1069   { "jnl",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_L,  1 }, // 7d
1070   { "jle",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
1071   { "jng",  OP_JCC,    1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
1072   { "jg",   OP_JCC,    1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
1073   { "jnle", OP_JCC,    1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
1074   { "seto",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_O,  0 },
1075   { "setno",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_O,  1 },
1076   { "setc",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_C,  0 },
1077   { "setb",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_C,  0 },
1078   { "setnc",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_C,  1 },
1079   { "setae",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_C,  1 },
1080   { "setnb",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_C,  1 },
1081   { "setz",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_Z,  0 },
1082   { "sete",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_Z,  0 },
1083   { "setnz",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_Z,  1 },
1084   { "setne",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_Z,  1 },
1085   { "setbe",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
1086   { "setna",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
1087   { "seta",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
1088   { "setnbe", OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
1089   { "sets",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_S,  0 },
1090   { "setns",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_S,  1 },
1091   { "setp",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_P,  0 },
1092   { "setpe",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_P,  0 },
1093   { "setnp",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_P,  1 },
1094   { "setpo",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_P,  1 },
1095   { "setl",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_L,  0 },
1096   { "setnge", OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_L,  0 },
1097   { "setge",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_L,  1 },
1098   { "setnl",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_L,  1 },
1099   { "setle",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
1100   { "setng",  OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
1101   { "setg",   OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
1102   { "setnle", OP_SCC,  1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
1103   // x87
1104   { "fld",    OP_FLD,    1, 1, OPF_FPUSH },
1105   { "fild",   OP_FILD,   1, 1, OPF_FPUSH|OPF_FINT },
1106   { "fld1",   OP_FLDc,   0, 0, OPF_FPUSH },
1107   { "fldl2t", OP_FLDc,   0, 0, OPF_FPUSH },
1108   { "fldl2e", OP_FLDc,   0, 0, OPF_FPUSH },
1109   { "fldpi",  OP_FLDc,   0, 0, OPF_FPUSH },
1110   { "fldlg2", OP_FLDc,   0, 0, OPF_FPUSH },
1111   { "fldln2", OP_FLDc,   0, 0, OPF_FPUSH },
1112   { "fldz",   OP_FLDc,   0, 0, OPF_FPUSH },
1113   { "fst",    OP_FST,    1, 1, 0 },
1114   { "fstp",   OP_FST,    1, 1, OPF_FPOP },
1115   { "fist",   OP_FIST,   1, 1, OPF_FINT },
1116   { "fistp",  OP_FIST,   1, 1, OPF_FPOP|OPF_FINT },
1117   { "fabs",   OP_FABS,   0, 0, 0 },
1118   { "fadd",   OP_FADD,   0, 2, 0 },
1119   { "faddp",  OP_FADD,   0, 2, OPF_FPOP },
1120   { "fdiv",   OP_FDIV,   0, 2, 0 },
1121   { "fdivp",  OP_FDIV,   0, 2, OPF_FPOP },
1122   { "fmul",   OP_FMUL,   0, 2, 0 },
1123   { "fmulp",  OP_FMUL,   0, 2, OPF_FPOP },
1124   { "fsub",   OP_FSUB,   0, 2, 0 },
1125   { "fsubp",  OP_FSUB,   0, 2, OPF_FPOP },
1126   { "fdivr",  OP_FDIVR,  0, 2, 0 },
1127   { "fdivrp", OP_FDIVR,  0, 2, OPF_FPOP },
1128   { "fsubr",  OP_FSUBR,  0, 2, 0 },
1129   { "fsubrp", OP_FSUBR,  0, 2, OPF_FPOP },
1130   { "fiadd",  OP_FIADD,  1, 1, OPF_FINT },
1131   { "fidiv",  OP_FIDIV,  1, 1, OPF_FINT },
1132   { "fimul",  OP_FIMUL,  1, 1, OPF_FINT },
1133   { "fisub",  OP_FISUB,  1, 1, OPF_FINT },
1134   { "fidivr", OP_FIDIVR, 1, 1, OPF_FINT },
1135   { "fisubr", OP_FISUBR, 1, 1, OPF_FINT },
1136   { "fcom",   OP_FCOM,   0, 1, 0 },
1137   { "fcomp",  OP_FCOM,   0, 1, OPF_FPOP },
1138   { "fcompp", OP_FCOM,   0, 0, OPF_FPOPP },
1139   { "fucom",  OP_FCOM,   0, 1, 0 },
1140   { "fucomp", OP_FCOM,   0, 1, OPF_FPOP },
1141   { "fucompp",OP_FCOM,   0, 0, OPF_FPOPP },
1142   { "fnstsw", OP_FNSTSW, 1, 1, OPF_DATA },
1143   { "fchs",   OP_FCHS,   0, 0, 0 },
1144   { "fcos",   OP_FCOS,   0, 0, 0 },
1145   { "fpatan", OP_FPATAN, 0, 0, OPF_FPOP },
1146   { "fptan",  OP_FPTAN,  0, 0, OPF_FPUSH },
1147   { "fsin",   OP_FSIN,   0, 0, 0 },
1148   { "fsqrt",  OP_FSQRT,  0, 0, 0 },
1149   { "fxch",   OP_FXCH,   1, 1, 0 },
1150   { "fyl2x",  OP_FYL2X,  0, 0, OPF_FPOP },
1151   // mmx
1152   { "emms",   OP_EMMS,   0, 0, OPF_DATA },
1153   { "movq",   OP_MOV,    2, 2, OPF_DATA },
1154   // pseudo-ops for lib calls
1155   { "_allshl",OPP_ALLSHL },
1156   { "_allshr",OPP_ALLSHR },
1157   { "_ftol",  OPP_FTOL },
1158   { "_CIpow", OPP_CIPOW },
1159   { "abort",  OPP_ABORT },
1160   // must be last
1161   { "ud2",    OP_UD2 },
1162 };
1163
1164 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
1165 {
1166   enum opr_lenmod lmod = OPLM_UNSPEC;
1167   int prefix_flags = 0;
1168   int regmask_ind;
1169   int regmask;
1170   int op_w = 0;
1171   int opr = 0;
1172   int w = 0;
1173   int i, j;
1174
1175   for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
1176     if (IS(words[w], pref_table[i].name)) {
1177       prefix_flags = pref_table[i].flags;
1178       break;
1179     }
1180   }
1181
1182   if (prefix_flags) {
1183     if (wordc <= 1)
1184       aerr("lone prefix: '%s'\n", words[0]);
1185     w++;
1186   }
1187
1188   op_w = w;
1189   for (i = 0; i < ARRAY_SIZE(op_table); i++) {
1190     if (IS(words[w], op_table[i].name))
1191       break;
1192   }
1193
1194   if (i == ARRAY_SIZE(op_table)) {
1195     if (!g_skip_func)
1196       aerr("unhandled op: '%s'\n", words[0]);
1197     i--; // OP_UD2
1198   }
1199   w++;
1200
1201   op->op = op_table[i].op;
1202   op->flags = op_table[i].flags | prefix_flags;
1203   op->pfo = op_table[i].pfo;
1204   op->pfo_inv = op_table[i].pfo_inv;
1205   op->regmask_src = op->regmask_dst = 0;
1206   op->asmln = asmln;
1207
1208   if (op->op == OP_UD2)
1209     return;
1210
1211   for (opr = 0; opr < op_table[i].maxopr; opr++) {
1212     if (opr >= op_table[i].minopr && w >= wordc)
1213       break;
1214
1215     regmask = regmask_ind = 0;
1216     w = parse_operand(&op->operand[opr], &regmask, &regmask_ind,
1217       words, wordc, w, op->flags);
1218
1219     if (opr == 0 && (op->flags & OPF_DATA))
1220       op->regmask_dst = regmask;
1221     else
1222       op->regmask_src |= regmask;
1223     op->regmask_src |= regmask_ind;
1224
1225     if (op->operand[opr].lmod != OPLM_UNSPEC)
1226       g_func_lmods |= 1 << op->operand[opr].lmod;
1227   }
1228
1229   if (w < wordc)
1230     aerr("parse_op %s incomplete: %d/%d\n",
1231       words[0], w, wordc);
1232
1233   // special cases
1234   op->operand_cnt = opr;
1235   if (!strncmp(op_table[i].name, "set", 3))
1236     op->operand[0].lmod = OPLM_BYTE;
1237
1238   switch (op->op) {
1239   // first operand is not dst
1240   case OP_CMP:
1241   case OP_TEST:
1242     op->regmask_src |= op->regmask_dst;
1243     op->regmask_dst = 0;
1244     break;
1245
1246   // first operand is src too
1247   case OP_NOT:
1248   case OP_ADD:
1249   case OP_AND:
1250   case OP_OR:
1251   case OP_RCL:
1252   case OP_RCR:
1253   case OP_ADC:
1254   case OP_INC:
1255   case OP_DEC:
1256   case OP_NEG:
1257   case OP_BSWAP:
1258   // more below..
1259     op->regmask_src |= op->regmask_dst;
1260     break;
1261
1262   // special
1263   case OP_XCHG:
1264     op->regmask_src |= op->regmask_dst;
1265     op->regmask_dst |= op->regmask_src;
1266     goto check_align;
1267
1268   case OP_SUB:
1269   case OP_SBB:
1270   case OP_XOR:
1271     if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1272      && op->operand[0].lmod == op->operand[1].lmod
1273      && op->operand[0].reg == op->operand[1].reg
1274      && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1275     {
1276       op->regmask_src = 0;
1277     }
1278     else
1279       op->regmask_src |= op->regmask_dst;
1280     break;
1281
1282   // ops with implicit argumets
1283   case OP_XLAT:
1284     op->operand_cnt = 2;
1285     setup_reg_opr(&op->operand[0], xAX, OPLM_BYTE, &op->regmask_src);
1286     op->regmask_dst = op->regmask_src;
1287     setup_reg_opr(&op->operand[1], xBX, OPLM_DWORD, &op->regmask_src);
1288     break;
1289
1290   case OP_CDQ:
1291     op->operand_cnt = 2;
1292     setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
1293     setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
1294     break;
1295
1296   case OP_LODS:
1297   case OP_STOS:
1298   case OP_SCAS:
1299     if      (words[op_w][4] == 'b')
1300       lmod = OPLM_BYTE;
1301     else if (words[op_w][4] == 'w')
1302       lmod = OPLM_WORD;
1303     else if (words[op_w][4] == 'd')
1304       lmod = OPLM_DWORD;
1305     j = 0;
1306     op->regmask_src = 0;
1307     setup_reg_opr(&op->operand[j++], op->op == OP_LODS ? xSI : xDI,
1308       OPLM_DWORD, &op->regmask_src);
1309     op->regmask_dst = op->regmask_src;
1310     setup_reg_opr(&op->operand[j++], xAX, lmod,
1311       op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
1312     if (op->flags & OPF_REP) {
1313       setup_reg_opr(&op->operand[j++], xCX, OPLM_DWORD, &op->regmask_src);
1314       op->regmask_dst |= 1 << xCX;
1315     }
1316     op->operand_cnt = j;
1317     break;
1318
1319   case OP_MOVS:
1320   case OP_CMPS:
1321     if      (words[op_w][4] == 'b')
1322       lmod = OPLM_BYTE;
1323     else if (words[op_w][4] == 'w')
1324       lmod = OPLM_WORD;
1325     else if (words[op_w][4] == 'd')
1326       lmod = OPLM_DWORD;
1327     j = 0;
1328     op->regmask_src = 0;
1329     // note: lmod is not correct, don't have where to place it
1330     setup_reg_opr(&op->operand[j++], xDI, lmod, &op->regmask_src);
1331     setup_reg_opr(&op->operand[j++], xSI, OPLM_DWORD, &op->regmask_src);
1332     if (op->flags & OPF_REP)
1333       setup_reg_opr(&op->operand[j++], xCX, OPLM_DWORD, &op->regmask_src);
1334     op->operand_cnt = j;
1335     op->regmask_dst = op->regmask_src;
1336     break;
1337
1338   case OP_RDTSC:
1339     op->regmask_dst = mxAX | mxDX;
1340     break;
1341
1342   case OP_CPUID:
1343     // for now, ignore ecx dep for eax={4,7,b,d}
1344     op->regmask_src = mxAX;
1345     op->regmask_dst = mxAX | mxBX | mxCX | mxDX;
1346     break;
1347
1348   case OP_LOOP:
1349     op->regmask_dst = 1 << xCX;
1350     // fallthrough
1351   case OP_JECXZ:
1352     op->operand_cnt = 2;
1353     op->regmask_src = 1 << xCX;
1354     op->operand[1].type = OPT_REG;
1355     op->operand[1].reg = xCX;
1356     op->operand[1].lmod = OPLM_DWORD;
1357     break;
1358
1359   case OP_IMUL:
1360     if (op->operand_cnt == 2) {
1361       if (op->operand[0].type != OPT_REG)
1362         aerr("reg expected\n");
1363       op->regmask_src |= 1 << op->operand[0].reg;
1364     }
1365     if (op->operand_cnt != 1)
1366       break;
1367     // fallthrough
1368   case OP_MUL:
1369     // singleop mul
1370     if (op->operand[0].lmod == OPLM_UNSPEC)
1371       op->operand[0].lmod = OPLM_DWORD;
1372     op->regmask_src = mxAX | op->regmask_dst;
1373     op->regmask_dst = mxAX;
1374     if (op->operand[0].lmod != OPLM_BYTE)
1375       op->regmask_dst |= mxDX;
1376     break;
1377
1378   case OP_DIV:
1379   case OP_IDIV:
1380     // we could set up operands for edx:eax, but there is no real need to
1381     // (see is_opr_modified())
1382     if (op->operand[0].lmod == OPLM_UNSPEC)
1383       op->operand[0].lmod = OPLM_DWORD;
1384     op->regmask_src = mxAX | op->regmask_dst;
1385     op->regmask_dst = mxAX;
1386     if (op->operand[0].lmod != OPLM_BYTE) {
1387       op->regmask_src |= mxDX;
1388       op->regmask_dst |= mxDX;
1389     }
1390     break;
1391
1392   case OP_SHL:
1393   case OP_SHR:
1394   case OP_SAR:
1395   case OP_ROL:
1396   case OP_ROR:
1397     op->regmask_src |= op->regmask_dst;
1398     if (op->operand[1].lmod == OPLM_UNSPEC)
1399       op->operand[1].lmod = OPLM_BYTE;
1400     break;
1401
1402   case OP_SHLD:
1403   case OP_SHRD:
1404     op->regmask_src |= op->regmask_dst;
1405     if (op->operand[2].lmod == OPLM_UNSPEC)
1406       op->operand[2].lmod = OPLM_BYTE;
1407     break;
1408
1409   case OP_PUSH:
1410     op->regmask_src |= op->regmask_dst;
1411     op->regmask_dst = 0;
1412     if (op->operand[0].lmod == OPLM_UNSPEC
1413         && (op->operand[0].type == OPT_CONST
1414          || op->operand[0].type == OPT_OFFSET
1415          || op->operand[0].type == OPT_LABEL))
1416       op->operand[0].lmod = OPLM_DWORD;
1417     break;
1418
1419   // alignment
1420   case OP_MOV:
1421   check_align:
1422     if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1423      && op->operand[0].lmod == op->operand[1].lmod
1424      && op->operand[0].reg == op->operand[1].reg
1425      && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1426     {
1427       op->flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
1428       op->regmask_src = op->regmask_dst = 0;
1429     }
1430     break;
1431
1432   case OP_LEA:
1433     if (op->operand[0].type == OPT_REG
1434      && op->operand[1].type == OPT_REGMEM)
1435     {
1436       char buf[16];
1437       snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1438       if (IS(buf, op->operand[1].name))
1439         op->flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
1440     }
1441     break;
1442
1443   case OP_CALL:
1444     // needed because of OPF_DATA
1445     op->regmask_src |= op->regmask_dst;
1446     // trashed regs must be explicitly detected later
1447     op->regmask_dst = 0;
1448     break;
1449
1450   case OP_LEAVE:
1451     op->regmask_dst = (1 << xBP) | (1 << xSP);
1452     op->regmask_src =  1 << xBP;
1453     break;
1454
1455   case OP_FLD:
1456   case OP_FILD:
1457     op->regmask_dst |= mxST0;
1458     break;
1459
1460   case OP_FLDc:
1461     op->regmask_dst |= mxST0;
1462     if      (IS(words[op_w] + 3, "1"))
1463       op->operand[0].val = X87_CONST_1;
1464     else if (IS(words[op_w] + 3, "l2t"))
1465       op->operand[0].val = X87_CONST_L2T;
1466     else if (IS(words[op_w] + 3, "l2e"))
1467       op->operand[0].val = X87_CONST_L2E;
1468     else if (IS(words[op_w] + 3, "pi"))
1469       op->operand[0].val = X87_CONST_PI;
1470     else if (IS(words[op_w] + 3, "lg2"))
1471       op->operand[0].val = X87_CONST_LG2;
1472     else if (IS(words[op_w] + 3, "ln2"))
1473       op->operand[0].val = X87_CONST_LN2;
1474     else if (IS(words[op_w] + 3, "z"))
1475       op->operand[0].val = X87_CONST_Z;
1476     else
1477       aerr("fld what?\n");
1478     break;
1479
1480   case OP_FST:
1481   case OP_FIST:
1482     op->regmask_src |= mxST0;
1483     break;
1484
1485   case OP_FADD:
1486   case OP_FDIV:
1487   case OP_FMUL:
1488   case OP_FSUB:
1489   case OP_FDIVR:
1490   case OP_FSUBR:
1491     op->regmask_src |= mxST0;
1492     if (op->operand_cnt == 2)
1493       op->regmask_src |= op->regmask_dst;
1494     else if (op->operand_cnt == 1) {
1495       memcpy(&op->operand[1], &op->operand[0], sizeof(op->operand[1]));
1496       op->operand[0].type = OPT_REG;
1497       op->operand[0].lmod = OPLM_QWORD;
1498       op->operand[0].reg = xST0;
1499       op->regmask_dst |= mxST0;
1500     }
1501     else
1502       // IDA doesn't use this
1503       aerr("no operands?\n");
1504     break;
1505
1506   case OP_FIADD:
1507   case OP_FIDIV:
1508   case OP_FIMUL:
1509   case OP_FISUB:
1510   case OP_FIDIVR:
1511   case OP_FISUBR:
1512   case OP_FABS:
1513   case OP_FCHS:
1514   case OP_FCOS:
1515   case OP_FSIN:
1516   case OP_FSQRT:
1517   case OP_FXCH:
1518     op->regmask_src |= mxST0;
1519     op->regmask_dst |= mxST0;
1520     break;
1521
1522   case OP_FPATAN:
1523   case OP_FYL2X:
1524     op->regmask_src |= mxST0 | mxST1;
1525     op->regmask_dst |= mxST0;
1526     break;
1527
1528   case OP_FPTAN:
1529     aerr("TODO\n");
1530     break;
1531
1532   case OP_FCOM:
1533     op->regmask_src |= mxST0;
1534     if (op->operand_cnt == 0) {
1535       op->operand_cnt = 1;
1536       op->operand[0].type = OPT_REG;
1537       op->operand[0].lmod = OPLM_QWORD;
1538       op->operand[0].reg = xST1;
1539       op->regmask_src |= mxST1;
1540     }
1541     break;
1542
1543   default:
1544     break;
1545   }
1546
1547   if (op->operand[0].type == OPT_REG
1548    && op->operand[1].type == OPT_CONST)
1549   {
1550     struct parsed_opr *op1 = &op->operand[1];
1551     if ((op->op == OP_AND && op1->val == 0)
1552      || (op->op == OP_OR
1553       && (op1->val == ~0
1554        || (op->operand[0].lmod == OPLM_WORD && op1->val == 0xffff)
1555        || (op->operand[0].lmod == OPLM_BYTE && op1->val == 0xff))))
1556     {
1557       op->regmask_src = 0;
1558     }
1559   }
1560 }
1561
1562 static const char *op_name(struct parsed_op *po)
1563 {
1564   static char buf[16];
1565   char *p;
1566   int i;
1567
1568   if (po->op == OP_JCC || po->op == OP_SCC) {
1569     p = buf;
1570     *p++ = (po->op == OP_JCC) ? 'j' : 's';
1571     if (po->pfo_inv)
1572       *p++ = 'n';
1573     strcpy(p, parsed_flag_op_names[po->pfo]);
1574     return buf;
1575   }
1576
1577   for (i = 0; i < ARRAY_SIZE(op_table); i++)
1578     if (op_table[i].op == po->op)
1579       return op_table[i].name;
1580
1581   return "???";
1582 }
1583
1584 // debug
1585 static const char *dump_op(struct parsed_op *po)
1586 {
1587   static char out[128];
1588   char *p = out;
1589   int i;
1590
1591   if (po == NULL)
1592     return "???";
1593
1594   snprintf(out, sizeof(out), "%s", op_name(po));
1595   for (i = 0; i < po->operand_cnt; i++) {
1596     p += strlen(p);
1597     if (i > 0)
1598       *p++ = ',';
1599     snprintf(p, sizeof(out) - (p - out),
1600       po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1601       po->operand[i].name);
1602   }
1603
1604   return out;
1605 }
1606
1607 static const char *lmod_type_u(struct parsed_op *po,
1608   enum opr_lenmod lmod)
1609 {
1610   switch (lmod) {
1611   case OPLM_QWORD:
1612     return "u64";
1613   case OPLM_DWORD:
1614     return "u32";
1615   case OPLM_WORD:
1616     return "u16";
1617   case OPLM_BYTE:
1618     return "u8";
1619   default:
1620     ferr(po, "invalid lmod: %d\n", lmod);
1621     return "(_invalid_)";
1622   }
1623 }
1624
1625 static const char *lmod_cast_u(struct parsed_op *po,
1626   enum opr_lenmod lmod)
1627 {
1628   switch (lmod) {
1629   case OPLM_QWORD:
1630     return "";
1631   case OPLM_DWORD:
1632     return "";
1633   case OPLM_WORD:
1634     return "(u16)";
1635   case OPLM_BYTE:
1636     return "(u8)";
1637   default:
1638     ferr(po, "invalid lmod: %d\n", lmod);
1639     return "(_invalid_)";
1640   }
1641 }
1642
1643 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1644   enum opr_lenmod lmod)
1645 {
1646   switch (lmod) {
1647   case OPLM_QWORD:
1648     return "*(u64 *)";
1649   case OPLM_DWORD:
1650     return "*(u32 *)";
1651   case OPLM_WORD:
1652     return "*(u16 *)";
1653   case OPLM_BYTE:
1654     return "*(u8 *)";
1655   default:
1656     ferr(po, "invalid lmod: %d\n", lmod);
1657     return "(_invalid_)";
1658   }
1659 }
1660
1661 static const char *lmod_cast_s(struct parsed_op *po,
1662   enum opr_lenmod lmod)
1663 {
1664   switch (lmod) {
1665   case OPLM_QWORD:
1666     return "(s64)";
1667   case OPLM_DWORD:
1668     return "(s32)";
1669   case OPLM_WORD:
1670     return "(s16)";
1671   case OPLM_BYTE:
1672     return "(s8)";
1673   default:
1674     ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1675     return "(_invalid_)";
1676   }
1677 }
1678
1679 static const char *lmod_cast(struct parsed_op *po,
1680   enum opr_lenmod lmod, int is_signed)
1681 {
1682   return is_signed ?
1683     lmod_cast_s(po, lmod) :
1684     lmod_cast_u(po, lmod);
1685 }
1686
1687 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1688 {
1689   switch (lmod) {
1690   case OPLM_QWORD:
1691     return 8;
1692   case OPLM_DWORD:
1693     return 4;
1694   case OPLM_WORD:
1695     return 2;
1696   case OPLM_BYTE:
1697     return 1;
1698   default:
1699     ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1700     return 0;
1701   }
1702 }
1703
1704 static const char *opr_name(struct parsed_op *po, int opr_num)
1705 {
1706   if (opr_num >= po->operand_cnt)
1707     ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1708   return po->operand[opr_num].name;
1709 }
1710
1711 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1712 {
1713   if (opr_num >= po->operand_cnt)
1714     ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1715   if (po->operand[opr_num].type != OPT_CONST)
1716     ferr(po, "opr %d: const expected\n", opr_num);
1717   return po->operand[opr_num].val;
1718 }
1719
1720 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1721 {
1722   if ((unsigned int)popr->reg >= ARRAY_SIZE(regs_r32))
1723     ferr(po, "invalid reg: %d\n", popr->reg);
1724   return regs_r32[popr->reg];
1725 }
1726
1727 static int check_simple_cast(const char *cast, int *bits, int *is_signed)
1728 {
1729   if      (IS_START(cast, "(s8)") || IS_START(cast, "(u8)"))
1730     *bits = 8;
1731   else if (IS_START(cast, "(s16)") || IS_START(cast, "(u16)"))
1732     *bits = 16;
1733   else if (IS_START(cast, "(s32)") || IS_START(cast, "(u32)"))
1734     *bits = 32;
1735   else if (IS_START(cast, "(s64)") || IS_START(cast, "(u64)"))
1736     *bits = 64;
1737   else
1738     return -1;
1739
1740   *is_signed = cast[1] == 's' ? 1 : 0;
1741   return 0;
1742 }
1743
1744 static int check_deref_cast(const char *cast, int *bits)
1745 {
1746   if      (IS_START(cast, "*(u8 *)"))
1747     *bits = 8;
1748   else if (IS_START(cast, "*(u16 *)"))
1749     *bits = 16;
1750   else if (IS_START(cast, "*(u32 *)"))
1751     *bits = 32;
1752   else if (IS_START(cast, "*(u64 *)"))
1753     *bits = 64;
1754   else
1755     return -1;
1756
1757   return 0;
1758 }
1759
1760 // cast1 is the "final" cast
1761 static const char *simplify_cast(const char *cast1, const char *cast2)
1762 {
1763   static char buf[256];
1764   int bits1, bits2;
1765   int s1, s2;
1766
1767   if (cast1[0] == 0)
1768     return cast2;
1769   if (cast2[0] == 0)
1770     return cast1;
1771   if (IS(cast1, cast2))
1772     return cast1;
1773
1774   if (check_simple_cast(cast1, &bits1, &s1) == 0
1775     && check_simple_cast(cast2, &bits2, &s2) == 0)
1776   {
1777     if (bits1 <= bits2)
1778       return cast1;
1779   }
1780   if (check_simple_cast(cast1, &bits1, &s1) == 0
1781     && check_deref_cast(cast2, &bits2) == 0)
1782   {
1783     if (bits1 == bits2) {
1784       snprintf(buf, sizeof(buf), "*(%c%d *)", s1 ? 's' : 'u', bits1);
1785       return buf;
1786     }
1787   }
1788
1789   if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1790     return cast1;
1791
1792   snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1793   return buf;
1794 }
1795
1796 static const char *simplify_cast_num(const char *cast, unsigned int val)
1797 {
1798   if (IS(cast, "(u8)") && val < 0x100)
1799     return "";
1800   if (IS(cast, "(s8)") && val < 0x80)
1801     return "";
1802   if (IS(cast, "(u16)") && val < 0x10000)
1803     return "";
1804   if (IS(cast, "(s16)") && val < 0x8000)
1805     return "";
1806   if (IS(cast, "(s32)") && val < 0x80000000)
1807     return "";
1808
1809   return cast;
1810 }
1811
1812 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1813   int *extra_offs)
1814 {
1815   const char *p;
1816   char *endp;
1817   int namelen;
1818   int i;
1819
1820   *extra_offs = 0;
1821   namelen = strlen(name);
1822
1823   p = strpbrk(name, "+-");
1824   if (p != NULL) {
1825     namelen = p - name;
1826     if (namelen <= 0)
1827       ferr(po, "equ parse failed for '%s'\n", name);
1828
1829     errno = 0;
1830     *extra_offs = strtol(p, &endp, 16);
1831     if (*endp != 0 || errno != 0)
1832       ferr(po, "equ parse failed for '%s'\n", name);
1833   }
1834
1835   for (i = 0; i < g_eqcnt; i++)
1836     if (strncmp(g_eqs[i].name, name, namelen) == 0
1837      && g_eqs[i].name[namelen] == 0)
1838       break;
1839   if (i >= g_eqcnt) {
1840     if (po != NULL)
1841       ferr(po, "unresolved equ name: '%s'\n", name);
1842     return NULL;
1843   }
1844
1845   return &g_eqs[i];
1846 }
1847
1848 static int is_stack_access(struct parsed_op *po,
1849   const struct parsed_opr *popr)
1850 {
1851   return (parse_stack_el(popr->name, NULL, NULL, 0)
1852     || (g_bp_frame && !(po->flags & OPF_EBP_S)
1853         && IS_START(popr->name, "ebp")));
1854 }
1855
1856 static void parse_stack_access(struct parsed_op *po,
1857   const char *name, char *ofs_reg, int *offset_out,
1858   int *stack_ra_out, const char **bp_arg_out, int is_lea)
1859 {
1860   const char *bp_arg = "";
1861   const char *p = NULL;
1862   struct parsed_equ *eq;
1863   char *endp = NULL;
1864   int stack_ra = 0;
1865   int offset = 0;
1866
1867   ofs_reg[0] = 0;
1868
1869   if (IS_START(name, "ebp-")
1870    || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1871   {
1872     p = name + 4;
1873     if (IS_START(p, "0x"))
1874       p += 2;
1875     errno = 0;
1876     offset = strtoul(p, &endp, 16);
1877     if (name[3] == '-')
1878       offset = -offset;
1879     if (*endp != 0 || errno != 0)
1880       ferr(po, "ebp- parse of '%s' failed\n", name);
1881   }
1882   else {
1883     bp_arg = parse_stack_el(name, ofs_reg, NULL, 0);
1884     eq = equ_find(po, bp_arg, &offset);
1885     if (eq == NULL)
1886       ferr(po, "detected but missing eq\n");
1887     offset += eq->offset;
1888   }
1889
1890   if (!strncmp(name, "ebp", 3))
1891     stack_ra = 4;
1892
1893   // yes it sometimes LEAs ra for compares..
1894   if (!is_lea && ofs_reg[0] == 0
1895     && stack_ra <= offset && offset < stack_ra + 4)
1896   {
1897     ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1898   }
1899
1900   *offset_out = offset;
1901   if (stack_ra_out)
1902     *stack_ra_out = stack_ra;
1903   if (bp_arg_out)
1904     *bp_arg_out = bp_arg;
1905 }
1906
1907 static int parse_stack_esp_offset(struct parsed_op *po,
1908   const char *name, int *offset_out)
1909 {
1910   char ofs_reg[16] = { 0, };
1911   struct parsed_equ *eq;
1912   const char *bp_arg;
1913   char *endp = NULL;
1914   int base_val = 0;
1915   int offset = 0;
1916
1917   if (strstr(name, "esp") == NULL)
1918     return -1;
1919   bp_arg = parse_stack_el(name, ofs_reg, &base_val, 0);
1920   if (bp_arg == NULL) {
1921     // just plain offset?
1922     if (!IS_START(name, "esp+"))
1923       return -1;
1924     errno = 0;
1925     offset = strtol(name + 4, &endp, 0);
1926     if (endp == NULL || *endp != 0 || errno != 0)
1927       return -1;
1928     *offset_out = offset;
1929     return 0;
1930   }
1931
1932   if (ofs_reg[0] != 0)
1933     return -1;
1934   eq = equ_find(po, bp_arg, &offset);
1935   if (eq == NULL)
1936     ferr(po, "detected but missing eq\n");
1937   offset += eq->offset;
1938   *offset_out = base_val + offset;
1939   return 0;
1940 }
1941
1942 // returns g_func_pp arg number if arg is accessed
1943 // -1 otherwise (stack vars, va_list)
1944 // note: 'popr' must be from 'po', not some other op
1945 static int stack_frame_access(struct parsed_op *po,
1946   struct parsed_opr *popr, char *buf, size_t buf_size,
1947   const char *name, const char *cast, int is_src, int is_lea)
1948 {
1949   enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1950   const char *prefix = "";
1951   const char *bp_arg = NULL;
1952   char ofs_reg[16] = { 0, };
1953   char argname[8], buf2[32];
1954   int i, arg_i, arg_s;
1955   int unaligned = 0;
1956   int stack_ra = 0;
1957   int offset = 0;
1958   int retval = -1;
1959   int sf_ofs;
1960
1961   if (g_bp_frame && (po->flags & OPF_EBP_S)
1962       && !(po->regmask_src & mxSP))
1963     ferr(po, "stack_frame_access while ebp is scratch\n");
1964
1965   parse_stack_access(po, name, ofs_reg, &offset,
1966     &stack_ra, &bp_arg, is_lea);
1967
1968   snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1969
1970   if (offset > stack_ra)
1971   {
1972     arg_i = (offset - stack_ra - 4) / 4;
1973     if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1974     {
1975       if (g_func_pp->is_vararg && arg_i >= g_func_pp->argc_stack) {
1976         // vararg access - messy and non-portable,
1977         // but works with gcc on both x86 and ARM
1978         if (arg_i == g_func_pp->argc_stack)
1979           // should be va_list
1980           snprintf(buf2, sizeof(buf2), "*(u32 *)&ap");
1981         else
1982           snprintf(buf2, sizeof(buf2), "(*(u32 *)&ap + %u)",
1983             (arg_i - g_func_pp->argc_stack) * 4);
1984
1985         if (is_lea)
1986           snprintf(buf, buf_size, "%s%s", cast, buf2);
1987         else
1988           snprintf(buf, buf_size, "%s*(u32 *)%s", cast, buf2);
1989         return -1;
1990       }
1991       ferr(po, "offset 0x%x (%s,%d) doesn't map to any arg\n",
1992         offset, bp_arg, arg_i);
1993     }
1994     if (ofs_reg[0] != 0)
1995       ferr(po, "offset reg on arg access?\n");
1996
1997     for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1998       if (g_func_pp->arg[i].reg != NULL)
1999         continue;
2000       if (arg_s == arg_i)
2001         break;
2002       arg_s++;
2003     }
2004     if (i == g_func_pp->argc)
2005       ferr(po, "arg %d not in prototype?\n", arg_i);
2006
2007     popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
2008     retval = i;
2009
2010     snprintf(argname, sizeof(argname), "%sa%d",
2011       g_sct_func_attr & SCTFA_ARGFRAME ? "af." : "", i + 1);
2012
2013     switch (popr->lmod)
2014     {
2015     case OPLM_BYTE:
2016       if (is_lea)
2017         ferr(po, "lea/byte to arg?\n");
2018       if (is_src && (offset & 3) == 0)
2019         snprintf(buf, buf_size, "%s%s",
2020           simplify_cast(cast, "(u8)"), argname);
2021       else
2022         snprintf(buf, buf_size, "%sBYTE%d(%s)",
2023           cast, offset & 3, argname);
2024       break;
2025
2026     case OPLM_WORD:
2027       if (is_lea)
2028         ferr(po, "lea/word to arg?\n");
2029       if (offset & 1) {
2030         unaligned = 1;
2031         if (!is_src) {
2032           if (offset & 2)
2033             ferr(po, "problematic arg store\n");
2034           snprintf(buf, buf_size, "%s((char *)&%s + 1)",
2035             simplify_cast(cast, "*(u16 *)"), argname);
2036         }
2037         else
2038           ferr(po, "unaligned arg word load\n");
2039       }
2040       else if (is_src && (offset & 2) == 0)
2041         snprintf(buf, buf_size, "%s%s",
2042           simplify_cast(cast, "(u16)"), argname);
2043       else
2044         snprintf(buf, buf_size, "%s%sWORD(%s)",
2045           cast, (offset & 2) ? "HI" : "LO", argname);
2046       break;
2047
2048     case OPLM_DWORD:
2049       if (cast[0])
2050         prefix = cast;
2051       else if (is_src)
2052         prefix = "(u32)";
2053
2054       if (offset & 3) {
2055         unaligned = 1;
2056         if (is_lea)
2057           snprintf(buf, buf_size, "(u32)&%s + %d",
2058             argname, offset & 3);
2059         else if (!is_src)
2060           ferr(po, "unaligned arg store\n");
2061         else {
2062           // mov edx, [ebp+arg_4+2]; movsx ecx, dx
2063           snprintf(buf, buf_size, "%s(%s >> %d)",
2064             prefix, argname, (offset & 3) * 8);
2065         }
2066       }
2067       else {
2068         snprintf(buf, buf_size, "%s%s%s",
2069           prefix, is_lea ? "&" : "", argname);
2070       }
2071       break;
2072
2073     case OPLM_QWORD:
2074       ferr_assert(po, !(offset & 7));
2075       if (cast[0])
2076         prefix = cast;
2077       snprintf(buf, buf_size, "%s%s%s",
2078         prefix, is_lea ? "&" : "", argname);
2079       break;
2080
2081     default:
2082       ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
2083     }
2084
2085     if (unaligned)
2086       strcat(g_comment, " unaligned");
2087
2088     // common problem
2089     guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
2090     if (tmp_lmod != OPLM_DWORD
2091       && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod)
2092                          < lmod_bytes(po, popr->lmod) + (offset & 3))))
2093     {
2094       ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
2095         i + 1, offset, g_func_pp->arg[i].type.name);
2096     }
2097     // can't check this because msvc likes to reuse
2098     // arg space for scratch..
2099     //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
2100     //  ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
2101   }
2102   else
2103   {
2104     if (g_stack_fsz == 0)
2105       ferr(po, "stack var access without stackframe\n");
2106     g_stack_frame_used = 1;
2107
2108     sf_ofs = g_stack_fsz + offset;
2109     if (ofs_reg[0] == 0 && (offset > 0 || sf_ofs < 0))
2110       ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
2111
2112     if (is_lea)
2113       prefix = "(u32)&";
2114     else
2115       prefix = cast;
2116
2117     switch (popr->lmod)
2118     {
2119     case OPLM_BYTE:
2120       snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
2121         prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
2122       break;
2123
2124     case OPLM_WORD:
2125       if ((sf_ofs & 1) || ofs_reg[0] != 0) {
2126         // known unaligned or possibly unaligned
2127         strcat(g_comment, " unaligned");
2128         if (prefix[0] == 0)
2129           prefix = "*(u16 *)&";
2130         snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
2131           prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
2132         break;
2133       }
2134       snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
2135       break;
2136
2137     case OPLM_DWORD:
2138       if ((sf_ofs & 3) || ofs_reg[0] != 0) {
2139         // known unaligned or possibly unaligned
2140         strcat(g_comment, " unaligned");
2141         if (prefix[0] == 0)
2142           prefix = "*(u32 *)&";
2143         snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
2144           prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
2145         break;
2146       }
2147       snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
2148       break;
2149
2150     case OPLM_QWORD:
2151       ferr_assert(po, !(sf_ofs & 7));
2152       ferr_assert(po, ofs_reg[0] == 0);
2153       // only used for x87 int64/float, float sets is_lea
2154       if (!is_lea && (po->flags & OPF_FINT))
2155         prefix = "*(s64 *)&";
2156       snprintf(buf, buf_size, "%ssf.q[%d]", prefix, sf_ofs / 8);
2157       break;
2158
2159     default:
2160       ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
2161     }
2162   }
2163
2164   return retval;
2165 }
2166
2167 static void check_func_pp(struct parsed_op *po,
2168   const struct parsed_proto *pp, const char *pfx)
2169 {
2170   enum opr_lenmod tmp_lmod;
2171   char buf[256];
2172   int ret, i;
2173
2174   if (pp->argc_reg != 0) {
2175     if (!g_allow_user_icall && !pp->is_fastcall) {
2176       pp_print(buf, sizeof(buf), pp);
2177       ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
2178     }
2179     if (pp->argc_stack > 0 && pp->argc_reg != 2)
2180       ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
2181         pfx, pp->argc_reg, pp->argc_stack);
2182   }
2183
2184   // fptrs must use 32bit args, callsite might have no information and
2185   // lack a cast to smaller types, which results in incorrectly masked
2186   // args passed (callee may assume masked args, it does on ARM)
2187   if (!pp->is_osinc) {
2188     for (i = 0; i < pp->argc; i++) {
2189       ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
2190       if (ret && tmp_lmod != OPLM_DWORD)
2191         ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
2192           i + 1, pp->arg[i].type.name);
2193     }
2194   }
2195 }
2196
2197 static const char *check_label_read_ref(struct parsed_op *po,
2198   const char *name, int *is_import)
2199 {
2200   const struct parsed_proto *pp;
2201
2202   pp = proto_parse(g_fhdr, name, 0);
2203   if (pp == NULL)
2204     ferr(po, "proto_parse failed for ref '%s'\n", name);
2205
2206   if (pp->is_func)
2207     check_func_pp(po, pp, "ref");
2208
2209   if (is_import != NULL)
2210     *is_import = pp->is_import;
2211
2212   return pp->name;
2213 }
2214
2215 static void check_opr(struct parsed_op *po, struct parsed_opr *popr)
2216 {
2217   if (popr->segment == SEG_FS)
2218     ferr(po, "fs: used\n");
2219   if (popr->segment == SEG_GS)
2220     ferr(po, "gs: used\n");
2221 }
2222
2223 static char *out_src_opr(char *buf, size_t buf_size,
2224   struct parsed_op *po, struct parsed_opr *popr, const char *cast,
2225   int is_lea)
2226 {
2227   char tmp1[256], tmp2[256];
2228   char expr[256];
2229   const char *name;
2230   int is_import = 0;
2231   char *p;
2232   int ret;
2233
2234   check_opr(po, popr);
2235
2236   if (cast == NULL)
2237     cast = "";
2238
2239   switch (popr->type) {
2240   case OPT_REG:
2241     if (is_lea)
2242       ferr(po, "lea from reg?\n");
2243
2244     switch (popr->lmod) {
2245     case OPLM_QWORD:
2246       snprintf(buf, buf_size, "%s%s.q", cast, opr_reg_p(po, popr));
2247       break;
2248     case OPLM_DWORD:
2249       snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
2250       break;
2251     case OPLM_WORD:
2252       snprintf(buf, buf_size, "%s%s",
2253         simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
2254       break;
2255     case OPLM_BYTE:
2256       if (popr->name[1] == 'h') // XXX..
2257         snprintf(buf, buf_size, "%s(%s >> 8)",
2258           simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
2259       else
2260         snprintf(buf, buf_size, "%s%s",
2261           simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
2262       break;
2263     default:
2264       ferr(po, "invalid src lmod: %d\n", popr->lmod);
2265     }
2266     break;
2267
2268   case OPT_REGMEM:
2269     if (is_stack_access(po, popr)) {
2270       stack_frame_access(po, popr, buf, buf_size,
2271         popr->name, cast, 1, is_lea);
2272       break;
2273     }
2274
2275     strcpy(expr, popr->name);
2276     if (strchr(expr, '[')) {
2277       // special case: '[' can only be left for label[reg] form
2278       ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
2279       if (ret != 2)
2280         ferr(po, "parse failure for '%s'\n", expr);
2281       if (tmp1[0] == '(') {
2282         // (off_4FFF50+3)[eax]
2283         p = strchr(tmp1 + 1, ')');
2284         if (p == NULL || p[1] != 0)
2285           ferr(po, "parse failure (2) for '%s'\n", expr);
2286         *p = 0;
2287         memmove(tmp1, tmp1 + 1, strlen(tmp1));
2288       }
2289       snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
2290     }
2291
2292     // XXX: do we need more parsing?
2293     if (is_lea) {
2294       snprintf(buf, buf_size, "%s", expr);
2295       break;
2296     }
2297
2298     snprintf(buf, buf_size, "%s(%s)",
2299       simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
2300     break;
2301
2302   case OPT_LABEL:
2303     name = check_label_read_ref(po, popr->name, &is_import);
2304     if (is_import)
2305       // for imported data, asm is loading the offset
2306       goto do_offset;
2307
2308     if (cast[0] == 0 && popr->is_ptr)
2309       cast = "(u32)";
2310
2311     if (is_lea)
2312       snprintf(buf, buf_size, "(u32)&%s", name);
2313     else if (popr->size_lt)
2314       snprintf(buf, buf_size, "%s%s%s%s", cast,
2315         lmod_cast_u_ptr(po, popr->lmod),
2316         popr->is_array ? "" : "&", name);
2317     else
2318       snprintf(buf, buf_size, "%s%s%s", cast, name,
2319         popr->is_array ? "[0]" : "");
2320     break;
2321
2322   case OPT_OFFSET:
2323   do_offset:
2324     name = check_label_read_ref(po, popr->name, NULL);
2325     if (cast[0] == 0)
2326       cast = "(u32)";
2327     if (is_lea)
2328       ferr(po, "lea an offset?\n");
2329     snprintf(buf, buf_size, "%s&%s", cast, name);
2330     break;
2331
2332   case OPT_CONST:
2333     if (is_lea)
2334       ferr(po, "lea from const?\n");
2335
2336     printf_number(tmp1, sizeof(tmp1), popr->val);
2337     if (popr->val == 0 && strchr(cast, '*'))
2338       snprintf(buf, buf_size, "NULL");
2339     else
2340       snprintf(buf, buf_size, "%s%s",
2341         simplify_cast_num(cast, popr->val), tmp1);
2342     break;
2343
2344   default:
2345     ferr(po, "invalid src type: %d\n", popr->type);
2346   }
2347
2348   return buf;
2349 }
2350
2351 // note: may set is_ptr (we find that out late for ebp frame..)
2352 static char *out_dst_opr(char *buf, size_t buf_size,
2353         struct parsed_op *po, struct parsed_opr *popr)
2354 {
2355   check_opr(po, popr);
2356
2357   switch (popr->type) {
2358   case OPT_REG:
2359     switch (popr->lmod) {
2360     case OPLM_QWORD:
2361       snprintf(buf, buf_size, "%s.q", opr_reg_p(po, popr));
2362       break;
2363     case OPLM_DWORD:
2364       snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
2365       break;
2366     case OPLM_WORD:
2367       // ugh..
2368       snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
2369       break;
2370     case OPLM_BYTE:
2371       // ugh..
2372       if (popr->name[1] == 'h') // XXX..
2373         snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
2374       else
2375         snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
2376       break;
2377     default:
2378       ferr(po, "invalid dst lmod: %d\n", popr->lmod);
2379     }
2380     break;
2381
2382   case OPT_REGMEM:
2383     if (is_stack_access(po, popr)) {
2384       stack_frame_access(po, popr, buf, buf_size,
2385         popr->name, "", 0, 0);
2386       break;
2387     }
2388
2389     return out_src_opr(buf, buf_size, po, popr, NULL, 0);
2390
2391   case OPT_LABEL:
2392     if (popr->size_mismatch)
2393       snprintf(buf, buf_size, "%s%s%s",
2394         lmod_cast_u_ptr(po, popr->lmod),
2395         popr->is_array ? "" : "&", popr->name);
2396     else
2397       snprintf(buf, buf_size, "%s%s", popr->name,
2398         popr->is_array ? "[0]" : "");
2399     break;
2400
2401   default:
2402     ferr(po, "invalid dst type: %d\n", popr->type);
2403   }
2404
2405   return buf;
2406 }
2407
2408 static char *out_src_opr_u32(char *buf, size_t buf_size,
2409         struct parsed_op *po, struct parsed_opr *popr)
2410 {
2411   return out_src_opr(buf, buf_size, po, popr, NULL, 0);
2412 }
2413
2414 // do we need a helper func to perform a float i/o?
2415 static int float_opr_needs_helper(struct parsed_op *po,
2416   struct parsed_opr *popr)
2417 {
2418   if (!(g_sct_func_attr & SCTFA_UA_FLOAT))
2419     return 0;
2420   if (popr->type != OPT_REGMEM)
2421     return 0;
2422   if (is_stack_access(po, popr))
2423     return 0;
2424
2425   return 1;
2426 }
2427
2428 static char *out_opr_float(char *buf, size_t buf_size,
2429   struct parsed_op *po, struct parsed_opr *popr, int is_src,
2430   int need_float_stack)
2431 {
2432   const char *cast = NULL;
2433   char tmp[256];
2434   union {
2435     float f;
2436     int i;
2437   } u;
2438
2439   switch (popr->type) {
2440   case OPT_REG:
2441     if (popr->reg < xST0 || popr->reg > xST7) {
2442       // func arg
2443       ferr_assert(po, po->op == OP_PUSH);
2444       ferr_assert(po, popr->lmod == OPLM_DWORD);
2445       snprintf(buf, buf_size, "*(float *)&%s", opr_reg_p(po, popr));
2446       break;
2447     }
2448
2449     if (need_float_stack) {
2450       if (popr->reg == xST0)
2451         snprintf(buf, buf_size, "f_st[f_stp & 7]");
2452       else
2453         snprintf(buf, buf_size, "f_st[(f_stp + %d) & 7]",
2454           popr->reg - xST0);
2455     }
2456     else
2457       snprintf(buf, buf_size, "f_st%d", popr->reg - xST0);
2458     break;
2459
2460   case OPT_REGMEM:
2461     if (popr->lmod == OPLM_QWORD && is_stack_access(po, popr)) {
2462       stack_frame_access(po, popr, buf, buf_size,
2463         popr->name, "", is_src, 0);
2464       break;
2465     }
2466     // fallthrough
2467   case OPT_LABEL:
2468   case OPT_OFFSET:
2469     switch (popr->lmod) {
2470     case OPLM_QWORD:
2471       cast = "double";
2472       break;
2473     case OPLM_DWORD:
2474       cast = "float";
2475       break;
2476     default:
2477       ferr(po, "unhandled lmod: %d\n", popr->lmod);
2478       break;
2479     }
2480     out_src_opr(tmp, sizeof(tmp), po, popr, "", 1);
2481     if (is_src && float_opr_needs_helper(po, popr))
2482       snprintf(buf, buf_size, "%s_load(%s)", cast, tmp);
2483     else
2484       snprintf(buf, buf_size, "*(%s *)(%s)", cast, tmp);
2485     break;
2486
2487   case OPT_CONST:
2488     // only for func float args pushes
2489     ferr_assert(po, po->op == OP_PUSH);
2490     u.i = po->operand[0].val;
2491     if (ceilf(u.f) == u.f)
2492       snprintf(buf, buf_size, "%.1ff", u.f);
2493     else
2494       snprintf(buf, buf_size, "%.8ff", u.f);
2495     break;
2496
2497   default:
2498     ferr(po, "invalid float type: %d\n", popr->type);
2499   }
2500
2501   return buf;
2502 }
2503
2504 static char *out_src_opr_float(char *buf, size_t buf_size,
2505   struct parsed_op *po, struct parsed_opr *popr, int need_float_stack)
2506 {
2507   return out_opr_float(buf, buf_size, po, popr, 1, need_float_stack);
2508 }
2509
2510 static char *out_dst_opr_float(char *buf, size_t buf_size,
2511   struct parsed_op *po, struct parsed_opr *popr, int need_float_stack)
2512 {
2513   return out_opr_float(buf, buf_size, po, popr, 0, need_float_stack);
2514 }
2515
2516 static void out_test_for_cc(char *buf, size_t buf_size,
2517   struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
2518   enum opr_lenmod lmod, const char *expr)
2519 {
2520   const char *cast, *scast;
2521
2522   cast = lmod_cast_u(po, lmod);
2523   scast = lmod_cast_s(po, lmod);
2524
2525   switch (pfo) {
2526   case PFO_Z:
2527   case PFO_BE: // CF==1||ZF==1; CF=0
2528     snprintf(buf, buf_size, "(%s%s %s 0)",
2529       cast, expr, is_inv ? "!=" : "==");
2530     break;
2531
2532   case PFO_S:
2533   case PFO_L: // SF!=OF; OF=0
2534     snprintf(buf, buf_size, "(%s%s %s 0)",
2535       scast, expr, is_inv ? ">=" : "<");
2536     break;
2537
2538   case PFO_LE: // ZF==1||SF!=OF; OF=0
2539     snprintf(buf, buf_size, "(%s%s %s 0)",
2540       scast, expr, is_inv ? ">" : "<=");
2541     break;
2542
2543   case PFO_C: // CF=0
2544   case PFO_O: // OF=0
2545     snprintf(buf, buf_size, "(%d)", !!is_inv);
2546     break;
2547
2548   case PFO_P: // PF==1
2549     snprintf(buf, buf_size, "(%sdo_parity(%s))",
2550       is_inv ? "!" : "", expr);
2551     break;
2552
2553   default:
2554     ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
2555   }
2556 }
2557
2558 static void out_cmp_for_cc(char *buf, size_t buf_size,
2559   struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
2560   int is_neg)
2561 {
2562   const char *cast, *scast, *cast_use;
2563   char buf1[256], buf2[256];
2564   enum opr_lenmod lmod;
2565
2566   if (po->op != OP_DEC && po->operand[0].lmod != po->operand[1].lmod)
2567     ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
2568       po->operand[0].lmod, po->operand[1].lmod);
2569   lmod = po->operand[0].lmod;
2570
2571   cast = lmod_cast_u(po, lmod);
2572   scast = lmod_cast_s(po, lmod);
2573
2574   switch (pfo) {
2575   case PFO_C:
2576   case PFO_Z:
2577   case PFO_BE: // !a
2578     cast_use = cast;
2579     break;
2580
2581   case PFO_S:
2582   case PFO_L: // !ge
2583   case PFO_LE:
2584     cast_use = scast;
2585     break;
2586
2587   default:
2588     ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
2589   }
2590
2591   out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
2592   if (po->op == OP_DEC)
2593     snprintf(buf2, sizeof(buf2), "1");
2594   else {
2595     char cast_op2[64];
2596     snprintf(cast_op2, sizeof(cast_op2) - 1, "%s", cast_use);
2597     if (is_neg)
2598       strcat(cast_op2, "-");
2599     out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_op2, 0);
2600   }
2601
2602   switch (pfo) {
2603   case PFO_C:
2604     // note: must be unsigned compare
2605     snprintf(buf, buf_size, "(%s %s %s)",
2606       buf1, is_inv ? ">=" : "<", buf2);
2607     break;
2608
2609   case PFO_Z:
2610     snprintf(buf, buf_size, "(%s %s %s)",
2611       buf1, is_inv ? "!=" : "==", buf2);
2612     break;
2613
2614   case PFO_BE: // !a
2615     // note: must be unsigned compare
2616     snprintf(buf, buf_size, "(%s %s %s)",
2617       buf1, is_inv ? ">" : "<=", buf2);
2618
2619     // annoying case
2620     if (is_inv && lmod == OPLM_BYTE
2621       && po->operand[1].type == OPT_CONST
2622       && po->operand[1].val == 0xff)
2623     {
2624       snprintf(g_comment, sizeof(g_comment), "if %s", buf);
2625       snprintf(buf, buf_size, "(0)");
2626     }
2627     break;
2628
2629   // note: must be signed compare
2630   case PFO_S:
2631     snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
2632       scast, buf1, buf2, is_inv ? ">=" : "<");
2633     break;
2634
2635   case PFO_L: // !ge
2636     snprintf(buf, buf_size, "(%s %s %s)",
2637       buf1, is_inv ? ">=" : "<", buf2);
2638     break;
2639
2640   case PFO_LE: // !g
2641     snprintf(buf, buf_size, "(%s %s %s)",
2642       buf1, is_inv ? ">" : "<=", buf2);
2643     break;
2644
2645   default:
2646     break;
2647   }
2648 }
2649
2650 static void out_cmp_test(char *buf, size_t buf_size,
2651   struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
2652 {
2653   char buf1[256], buf2[256], buf3[256];
2654
2655   if (po->op == OP_TEST) {
2656     if (IS(opr_name(po, 0), opr_name(po, 1))) {
2657       out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
2658     }
2659     else {
2660       out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
2661       out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
2662       snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
2663     }
2664     out_test_for_cc(buf, buf_size, po, pfo, is_inv,
2665       po->operand[0].lmod, buf3);
2666   }
2667   else if (po->op == OP_CMP) {
2668     out_cmp_for_cc(buf, buf_size, po, pfo, is_inv, 0);
2669   }
2670   else
2671     ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
2672 }
2673
2674 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
2675         struct parsed_opr *popr2)
2676 {
2677   if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
2678     ferr(po, "missing lmod for both operands\n");
2679
2680   if (popr1->lmod == OPLM_UNSPEC)
2681     popr1->lmod = popr2->lmod;
2682   else if (popr2->lmod == OPLM_UNSPEC)
2683     popr2->lmod = popr1->lmod;
2684   else if (popr1->lmod != popr2->lmod) {
2685     if (popr1->type_from_var) {
2686       popr1->size_mismatch = 1;
2687       if (popr1->lmod < popr2->lmod)
2688         popr1->size_lt = 1;
2689       popr1->lmod = popr2->lmod;
2690     }
2691     else if (popr2->type_from_var) {
2692       popr2->size_mismatch = 1;
2693       if (popr2->lmod < popr1->lmod)
2694         popr2->size_lt = 1;
2695       popr2->lmod = popr1->lmod;
2696     }
2697     else
2698       ferr(po, "conflicting lmods: %d vs %d\n",
2699         popr1->lmod, popr2->lmod);
2700   }
2701 }
2702
2703 static const char *op_to_c(struct parsed_op *po)
2704 {
2705   switch (po->op)
2706   {
2707     case OP_ADD:
2708     case OP_ADC:
2709       return "+";
2710     case OP_SUB:
2711     case OP_SBB:
2712       return "-";
2713     case OP_AND:
2714       return "&";
2715     case OP_OR:
2716       return "|";
2717     case OP_XOR:
2718       return "^";
2719     case OP_SHL:
2720       return "<<";
2721     case OP_SHR:
2722       return ">>";
2723     case OP_MUL:
2724     case OP_IMUL:
2725       return "*";
2726     default:
2727       ferr(po, "op_to_c was supplied with %d\n", po->op);
2728   }
2729 }
2730
2731 // last op in stream - unconditional branch or ret
2732 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
2733   || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
2734       && ops[_i].op != OP_CALL))
2735
2736 #define check_i(po, i) \
2737   if ((i) < 0) \
2738     ferr(po, "bad " #i ": %d\n", i)
2739
2740 // note: this skips over calls and rm'd stuff assuming they're handled
2741 // so it's intended to use at one of final passes
2742 // exception: doesn't skip OPF_RSAVE stuff
2743 static int scan_for_pop(int i, int opcnt, int magic, int reg,
2744   int depth, int seen_noreturn, int save_level, int flags_set)
2745 {
2746   struct parsed_op *po;
2747   int relevant;
2748   int ret = 0;
2749   int j;
2750
2751   for (; i < opcnt; i++) {
2752     po = &ops[i];
2753     if (po->cc_scratch == magic)
2754       return ret; // already checked
2755     po->cc_scratch = magic;
2756
2757     if (po->flags & OPF_TAIL) {
2758       if (po->op == OP_CALL && po->pp != NULL && po->pp->is_noreturn) {
2759         // msvc sometimes generates stack cleanup code after
2760         // noreturn, set a flag and continue
2761         seen_noreturn = 1;
2762
2763         // ... but stop if there is another path to next insn -
2764         // if msvc skipped something stack tracking may mess up
2765         if (i + 1 < opcnt && g_labels[i + 1] != NULL)
2766           goto out;
2767       }
2768       else
2769         goto out;
2770     }
2771
2772     if (po->flags & OPF_FARG)
2773       continue;
2774     if (po->flags & (OPF_RMD|OPF_DONE)) {
2775       if (!(po->flags & OPF_RSAVE))
2776         continue;
2777       // reprocess, there might be another push in some "parallel"
2778       // path that took a pop what we should also take
2779     }
2780
2781     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2782       if (po->btj != NULL) {
2783         // jumptable
2784         for (j = 0; j < po->btj->count; j++) {
2785           check_i(po, po->btj->d[j].bt_i);
2786           ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, magic, reg,
2787                    depth, seen_noreturn, save_level, flags_set);
2788           if (ret < 0)
2789             return ret; // dead end
2790         }
2791         return ret;
2792       }
2793
2794       check_i(po, po->bt_i);
2795       if (po->flags & OPF_CJMP) {
2796         ret |= scan_for_pop(po->bt_i, opcnt, magic, reg,
2797                  depth, seen_noreturn, save_level, flags_set);
2798         if (ret < 0)
2799           return ret; // dead end
2800       }
2801       else {
2802         i = po->bt_i - 1;
2803       }
2804       continue;
2805     }
2806
2807     relevant = 0;
2808     if ((po->op == OP_POP || po->op == OP_PUSH)
2809       && po->operand[0].type == OPT_REG && po->operand[0].reg == reg)
2810     {
2811       relevant = 1;
2812     }
2813
2814     if (po->op == OP_PUSH) {
2815       depth++;
2816     }
2817     else if (po->op == OP_POP) {
2818       if (relevant && depth == 0) {
2819         if (flags_set == 0 && save_level > 0) {
2820           ret = scan_for_pop(i + 1, opcnt, magic, reg,
2821                   depth, seen_noreturn, save_level - 1, flags_set);
2822           if (ret != 1)
2823             // no pop for other levels, current one must be false
2824             return -1;
2825         }
2826         po->flags |= flags_set;
2827         return 1;
2828       }
2829       depth--;
2830     }
2831   }
2832
2833 out:
2834   // for noreturn, assume msvc skipped stack cleanup
2835   return seen_noreturn ? 1 : -1;
2836 }
2837
2838 // scan for 'reg' pop backwards starting from i
2839 // intended to use for register restore search, so other reg
2840 // references are considered an error
2841 static int scan_for_rsave_pop_reg(int i, int magic, int reg, int set_flags)
2842 {
2843   struct parsed_op *po;
2844   struct label_ref *lr;
2845   int ret = 0;
2846
2847   ops[i].cc_scratch = magic;
2848
2849   while (1)
2850   {
2851     if (g_labels[i] != NULL) {
2852       lr = &g_label_refs[i];
2853       for (; lr != NULL; lr = lr->next) {
2854         check_i(&ops[i], lr->i);
2855         ret |= scan_for_rsave_pop_reg(lr->i, magic, reg, set_flags);
2856         if (ret < 0)
2857           return ret;
2858       }
2859       if (i > 0 && LAST_OP(i - 1))
2860         return ret;
2861     }
2862
2863     i--;
2864     if (i < 0)
2865       break;
2866
2867     if (ops[i].cc_scratch == magic)
2868       return ret;
2869     ops[i].cc_scratch = magic;
2870
2871     po = &ops[i];
2872     if (po->op == OP_POP && po->operand[0].reg == reg) {
2873       if (po->flags & (OPF_RMD|OPF_DONE))
2874         return -1;
2875
2876       po->flags |= set_flags;
2877       return 1;
2878     }
2879
2880     // this also covers the case where we reach corresponding push
2881     if ((po->regmask_dst | po->regmask_src) & (1 << reg))
2882       return -1;
2883   }
2884
2885   // nothing interesting on this path,
2886   // still return ret for something recursive calls could find
2887   return ret;
2888 }
2889
2890 static void find_reachable_exits(int i, int opcnt, int magic,
2891   int *exits, int *exit_count)
2892 {
2893   struct parsed_op *po;
2894   int j;
2895
2896   for (; i < opcnt; i++)
2897   {
2898     po = &ops[i];
2899     if (po->cc_scratch == magic)
2900       return;
2901     po->cc_scratch = magic;
2902
2903     if (po->flags & OPF_TAIL) {
2904       ferr_assert(po, *exit_count < MAX_EXITS);
2905       exits[*exit_count] = i;
2906       (*exit_count)++;
2907       return;
2908     }
2909
2910     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2911       if (po->flags & OPF_RMD)
2912         continue;
2913
2914       if (po->btj != NULL) {
2915         for (j = 0; j < po->btj->count; j++) {
2916           check_i(po, po->btj->d[j].bt_i);
2917           find_reachable_exits(po->btj->d[j].bt_i, opcnt, magic,
2918                   exits, exit_count);
2919         }
2920         return;
2921       }
2922
2923       check_i(po, po->bt_i);
2924       if (po->flags & OPF_CJMP)
2925         find_reachable_exits(po->bt_i, opcnt, magic, exits, exit_count);
2926       else
2927         i = po->bt_i - 1;
2928       continue;
2929     }
2930   }
2931 }
2932
2933 // scan for 'reg' pop backwards starting from exits (all paths)
2934 static int scan_for_pop_ret(int i, int opcnt, int reg, int set_flags)
2935 {
2936   static int exits[MAX_EXITS];
2937   static int exit_count;
2938   int found = 0;
2939   int e, j, ret;
2940
2941   if (!set_flags) {
2942     exit_count = 0;
2943     find_reachable_exits(i, opcnt, i + opcnt * 15, exits,
2944       &exit_count);
2945     ferr_assert(&ops[i], exit_count > 0);
2946   }
2947
2948   for (j = 0; j < exit_count; j++) {
2949     e = exits[j];
2950     ret = scan_for_rsave_pop_reg(e, i + opcnt * 16 + set_flags,
2951             reg, set_flags);
2952     if (ret != -1) {
2953       found |= ret;
2954       continue;
2955     }
2956     if (ops[e].op == OP_CALL && ops[e].pp != NULL
2957       && ops[e].pp->is_noreturn)
2958     {
2959       // assume stack cleanup was skipped
2960       continue;
2961     }
2962     return -1;
2963   }
2964
2965   return found;
2966 }
2967
2968 // scan for one or more pop of push <const>
2969 static int scan_for_pop_const_r(int i, int opcnt, int magic,
2970   int push_i, int is_probe)
2971 {
2972   struct parsed_op *po;
2973   struct label_ref *lr;
2974   int ret = 0;
2975   int j;
2976
2977   for (; i < opcnt; i++)
2978   {
2979     po = &ops[i];
2980     if (po->cc_scratch == magic)
2981       return ret; // already checked
2982     po->cc_scratch = magic;
2983
2984     if (po->flags & OPF_JMP) {
2985       if (po->flags & OPF_RMD)
2986         continue;
2987       if (po->op == OP_CALL)
2988         return -1;
2989
2990       if (po->btj != NULL) {
2991         for (j = 0; j < po->btj->count; j++) {
2992           check_i(po, po->btj->d[j].bt_i);
2993           ret |= scan_for_pop_const_r(po->btj->d[j].bt_i, opcnt, magic,
2994                   push_i, is_probe);
2995           if (ret < 0)
2996             return ret;
2997         }
2998         return ret;
2999       }
3000
3001       check_i(po, po->bt_i);
3002       if (po->flags & OPF_CJMP) {
3003         ret |= scan_for_pop_const_r(po->bt_i, opcnt, magic, push_i,
3004                  is_probe);
3005         if (ret < 0)
3006           return ret;
3007       }
3008       else {
3009         i = po->bt_i - 1;
3010       }
3011       continue;
3012     }
3013
3014     if ((po->flags & (OPF_TAIL|OPF_RSAVE)) || po->op == OP_PUSH)
3015       return -1;
3016
3017     if (g_labels[i] != NULL) {
3018       // all refs must be visited
3019       lr = &g_label_refs[i];
3020       for (; lr != NULL; lr = lr->next) {
3021         check_i(po, lr->i);
3022         if (ops[lr->i].cc_scratch != magic)
3023           return -1;
3024       }
3025       if (i > 0 && !LAST_OP(i - 1) && ops[i - 1].cc_scratch != magic)
3026         return -1;
3027     }
3028
3029     if (po->op == OP_POP)
3030     {
3031       if (po->flags & (OPF_RMD|OPF_DONE))
3032         return -1;
3033
3034       if (!is_probe) {
3035         po->flags |= OPF_DONE;
3036         po->datap = &ops[push_i];
3037       }
3038       return 1;
3039     }
3040   }
3041
3042   return -1;
3043 }
3044
3045 static void scan_for_pop_const(int i, int opcnt, int magic)
3046 {
3047   int ret;
3048
3049   ret = scan_for_pop_const_r(i + 1, opcnt, magic, i, 1);
3050   if (ret == 1) {
3051     ops[i].flags |= OPF_RMD | OPF_DONE;
3052     scan_for_pop_const_r(i + 1, opcnt, magic + 1, i, 0);
3053   }
3054 }
3055
3056 // check if all branch targets within a marked path are also marked
3057 // note: the path checked must not be empty or end with a branch
3058 static int check_path_branches(int opcnt, int magic)
3059 {
3060   struct parsed_op *po;
3061   int i, j;
3062
3063   for (i = 0; i < opcnt; i++) {
3064     po = &ops[i];
3065     if (po->cc_scratch != magic)
3066       continue;
3067
3068     if (po->flags & OPF_JMP) {
3069       if ((po->flags & OPF_RMD) || po->op == OP_CALL)
3070         continue;
3071
3072       if (po->btj != NULL) {
3073         for (j = 0; j < po->btj->count; j++) {
3074           check_i(po, po->btj->d[j].bt_i);
3075           if (ops[po->btj->d[j].bt_i].cc_scratch != magic)
3076             return 0;
3077         }
3078       }
3079
3080       check_i(po, po->bt_i);
3081       if (ops[po->bt_i].cc_scratch != magic)
3082         return 0;
3083       if ((po->flags & OPF_CJMP) && ops[i + 1].cc_scratch != magic)
3084         return 0;
3085     }
3086   }
3087
3088   return 1;
3089 }
3090
3091 // scan for multiple pushes for given pop
3092 static int scan_pushes_for_pop_r(int i, int magic, int pop_i,
3093   int is_probe)
3094 {
3095   int reg = ops[pop_i].operand[0].reg;
3096   struct parsed_op *po;
3097   struct label_ref *lr;
3098   int ret = 0;
3099
3100   ops[i].cc_scratch = magic;
3101
3102   while (1)
3103   {
3104     if (g_labels[i] != NULL) {
3105       lr = &g_label_refs[i];
3106       for (; lr != NULL; lr = lr->next) {
3107         check_i(&ops[i], lr->i);
3108         ret |= scan_pushes_for_pop_r(lr->i, magic, pop_i, is_probe);
3109         if (ret < 0)
3110           return ret;
3111       }
3112       if (i > 0 && LAST_OP(i - 1))
3113         return ret;
3114     }
3115
3116     i--;
3117     if (i < 0)
3118       break;
3119
3120     if (ops[i].cc_scratch == magic)
3121       return ret;
3122     ops[i].cc_scratch = magic;
3123
3124     po = &ops[i];
3125     if (po->op == OP_CALL)
3126       return -1;
3127     if ((po->flags & (OPF_TAIL|OPF_RSAVE)) || po->op == OP_POP)
3128       return -1;
3129
3130     if (po->op == OP_PUSH)
3131     {
3132       if (po->datap != NULL)
3133         return -1;
3134       if (po->operand[0].type == OPT_REG && po->operand[0].reg == reg)
3135         // leave this case for reg save/restore handlers
3136         return -1;
3137
3138       if (!is_probe) {
3139         po->flags |= OPF_PPUSH | OPF_DONE;
3140         po->datap = &ops[pop_i];
3141       }
3142       return 1;
3143     }
3144   }
3145
3146   return -1;
3147 }
3148
3149 static void scan_pushes_for_pop(int i, int opcnt, int *regmask_pp)
3150 {
3151   int magic = i + opcnt * 14;
3152   int ret;
3153
3154   ret = scan_pushes_for_pop_r(i, magic, i, 1);
3155   if (ret == 1) {
3156     ret = check_path_branches(opcnt, magic);
3157     if (ret == 1) {
3158       ops[i].flags |= OPF_PPUSH | OPF_DONE;
3159       *regmask_pp |= 1 << ops[i].operand[0].reg;
3160       scan_pushes_for_pop_r(i, magic + 1, i, 0);
3161     }
3162   }
3163 }
3164
3165 static void scan_propagate_df(int i, int opcnt)
3166 {
3167   struct parsed_op *po = &ops[i];
3168   int j;
3169
3170   for (; i < opcnt; i++) {
3171     po = &ops[i];
3172     if (po->flags & OPF_DF)
3173       return; // already resolved
3174     po->flags |= OPF_DF;
3175
3176     if (po->op == OP_CALL)
3177       ferr(po, "call with DF set?\n");
3178
3179     if (po->flags & OPF_JMP) {
3180       if (po->btj != NULL) {
3181         // jumptable
3182         for (j = 0; j < po->btj->count; j++) {
3183           check_i(po, po->btj->d[j].bt_i);
3184           scan_propagate_df(po->btj->d[j].bt_i, opcnt);
3185         }
3186         return;
3187       }
3188
3189       if (po->flags & OPF_RMD)
3190         continue;
3191       check_i(po, po->bt_i);
3192       if (po->flags & OPF_CJMP)
3193         scan_propagate_df(po->bt_i, opcnt);
3194       else
3195         i = po->bt_i - 1;
3196       continue;
3197     }
3198
3199     if (po->flags & OPF_TAIL)
3200       break;
3201
3202     if (po->op == OP_CLD) {
3203       po->flags |= OPF_RMD | OPF_DONE;
3204       return;
3205     }
3206   }
3207
3208   ferr(po, "missing DF clear?\n");
3209 }
3210
3211 // is operand 'opr' referenced by parsed_op 'po'?
3212 static int is_opr_referenced(const struct parsed_opr *opr,
3213   const struct parsed_op *po)
3214 {
3215   int i, mask;
3216
3217   if (opr->type == OPT_REG) {
3218     mask = po->regmask_dst | po->regmask_src;
3219     if (po->op == OP_CALL)
3220       mask |= (1 << xAX) | (1 << xCX) | (1 << xDX);
3221     if ((1 << opr->reg) & mask)
3222       return 1;
3223     else
3224       return 0;
3225   }
3226
3227   for (i = 0; i < po->operand_cnt; i++)
3228     if (IS(po->operand[0].name, opr->name))
3229       return 1;
3230
3231   return 0;
3232 }
3233
3234 // is operand 'opr' read by parsed_op 'po'?
3235 static int is_opr_read(const struct parsed_opr *opr,
3236   const struct parsed_op *po)
3237 {
3238   if (opr->type == OPT_REG) {
3239     if (po->regmask_src & (1 << opr->reg))
3240       return 1;
3241     else
3242       return 0;
3243   }
3244
3245   // yes I'm lazy
3246   return 0;
3247 }
3248
3249 // is operand 'opr' modified by parsed_op 'po'?
3250 static int is_opr_modified(const struct parsed_opr *opr,
3251   const struct parsed_op *po)
3252 {
3253   int mask;
3254
3255   if (opr->type == OPT_REG) {
3256     if (po->op == OP_CALL) {
3257       mask = po->regmask_dst;
3258       mask |= (1 << xAX) | (1 << xCX) | (1 << xDX); // ?
3259       if (mask & (1 << opr->reg))
3260         return 1;
3261       else
3262         return 0;
3263     }
3264
3265     if (po->regmask_dst & (1 << opr->reg))
3266       return 1;
3267     else
3268       return 0;
3269   }
3270
3271   return IS(po->operand[0].name, opr->name);
3272 }
3273
3274 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
3275 static int is_any_opr_modified(const struct parsed_op *po_test,
3276   const struct parsed_op *po, int c_mode)
3277 {
3278   int mask;
3279   int i;
3280
3281   if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
3282     return 0;
3283
3284   if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
3285     return 0;
3286
3287   if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
3288     return 1;
3289
3290   // in reality, it can wreck any register, but in decompiled C
3291   // version it can only overwrite eax or edx:eax
3292   mask = (1 << xAX) | (1 << xDX);
3293   if (!c_mode)
3294     mask |= 1 << xCX;
3295
3296   if (po->op == OP_CALL
3297    && ((po_test->regmask_src | po_test->regmask_dst) & mask))
3298     return 1;
3299
3300   for (i = 0; i < po_test->operand_cnt; i++)
3301     if (IS(po_test->operand[i].name, po->operand[0].name))
3302       return 1;
3303
3304   return 0;
3305 }
3306
3307 // scan for any po_test operand modification in range given
3308 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
3309   int c_mode)
3310 {
3311   if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
3312     return -1;
3313
3314   for (; i < opcnt; i++) {
3315     if (is_any_opr_modified(po_test, &ops[i], c_mode))
3316       return i;
3317   }
3318
3319   return -1;
3320 }
3321
3322 // scan for po_test operand[0] modification in range given
3323 static int scan_for_mod_opr0(struct parsed_op *po_test,
3324   int i, int opcnt)
3325 {
3326   for (; i < opcnt; i++) {
3327     if (is_opr_modified(&po_test->operand[0], &ops[i]))
3328       return i;
3329   }
3330
3331   return -1;
3332 }
3333
3334 static int try_resolve_const(int i, const struct parsed_opr *opr,
3335   int magic, unsigned int *val);
3336
3337 static int scan_for_flag_set(int i, int opcnt, int magic,
3338   int *branched, int *setters, int *setter_cnt)
3339 {
3340   struct label_ref *lr;
3341   int ret;
3342
3343   while (i >= 0) {
3344     if (ops[i].cc_scratch == magic) {
3345       // is this a problem?
3346       //ferr(&ops[i], "%s looped\n", __func__);
3347       return 0;
3348     }
3349     ops[i].cc_scratch = magic;
3350
3351     if (g_labels[i] != NULL) {
3352       *branched = 1;
3353
3354       lr = &g_label_refs[i];
3355       for (; lr->next; lr = lr->next) {
3356         check_i(&ops[i], lr->i);
3357         ret = scan_for_flag_set(lr->i, opcnt, magic,
3358                 branched, setters, setter_cnt);
3359         if (ret < 0)
3360           return ret;
3361       }
3362
3363       check_i(&ops[i], lr->i);
3364       if (i > 0 && LAST_OP(i - 1)) {
3365         i = lr->i;
3366         continue;
3367       }
3368       ret = scan_for_flag_set(lr->i, opcnt, magic,
3369               branched, setters, setter_cnt);
3370       if (ret < 0)
3371         return ret;
3372     }
3373     i--;
3374
3375     if (ops[i].flags & OPF_FLAGS) {
3376       setters[*setter_cnt] = i;
3377       (*setter_cnt)++;
3378
3379       if (ops[i].flags & OPF_REP) {
3380         struct parsed_opr opr = OPR_INIT(OPT_REG, OPLM_DWORD, xCX);
3381         unsigned int uval;
3382
3383         ret = try_resolve_const(i, &opr, i + opcnt * 7, &uval);
3384         if (ret != 1 || uval == 0) {
3385           // can't treat it as full setter because of ecx=0 case,
3386           // also disallow delayed compare
3387           *branched = 1;
3388           continue;
3389         }
3390       }
3391
3392       return 0;
3393     }
3394
3395     if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
3396       return -1;
3397   }
3398
3399   return -1;
3400 }
3401
3402 // scan back for cdq, if anything modifies edx, fail
3403 static int scan_for_cdq_edx(int i)
3404 {
3405   while (i >= 0) {
3406     if (g_labels[i] != NULL) {
3407       if (g_label_refs[i].next != NULL)
3408         return -1;
3409       if (i > 0 && LAST_OP(i - 1)) {
3410         i = g_label_refs[i].i;
3411         continue;
3412       }
3413       return -1;
3414     }
3415     i--;
3416
3417     if (ops[i].op == OP_CDQ)
3418       return i;
3419
3420     if (ops[i].regmask_dst & (1 << xDX))
3421       return -1;
3422   }
3423
3424   return -1;
3425 }
3426
3427 static int scan_for_reg_clear(int i, int reg)
3428 {
3429   while (i >= 0) {
3430     if (g_labels[i] != NULL) {
3431       if (g_label_refs[i].next != NULL)
3432         return -1;
3433       if (i > 0 && LAST_OP(i - 1)) {
3434         i = g_label_refs[i].i;
3435         continue;
3436       }
3437       return -1;
3438     }
3439     i--;
3440
3441     if (ops[i].op == OP_XOR
3442      && ops[i].operand[0].lmod == OPLM_DWORD
3443      && ops[i].operand[0].reg == ops[i].operand[1].reg
3444      && ops[i].operand[0].reg == reg)
3445       return i;
3446
3447     if (ops[i].regmask_dst & (1 << reg))
3448       return -1;
3449   }
3450
3451   return -1;
3452 }
3453
3454 static void patch_esp_adjust(struct parsed_op *po, int adj)
3455 {
3456   ferr_assert(po, po->op == OP_ADD);
3457   ferr_assert(po, IS(opr_name(po, 0), "esp"));
3458   ferr_assert(po, po->operand[1].type == OPT_CONST);
3459
3460   // this is a bit of a hack, but deals with use of
3461   // single adj for multiple calls
3462   po->operand[1].val -= adj;
3463   po->flags |= OPF_RMD;
3464   if (po->operand[1].val == 0)
3465     po->flags |= OPF_DONE;
3466   ferr_assert(po, (int)po->operand[1].val >= 0);
3467 }
3468
3469 // scan for positive, constant esp adjust
3470 // multipath case is preliminary
3471 static int scan_for_esp_adjust(int i, int opcnt,
3472   int adj_expect, int *adj, int *is_multipath, int do_update)
3473 {
3474   int adj_expect_unknown = 0;
3475   struct parsed_op *po;
3476   int first_pop = -1;
3477   int adj_best = 0;
3478
3479   *adj = *is_multipath = 0;
3480   if (adj_expect < 0) {
3481     adj_expect_unknown = 1;
3482     adj_expect = 32 * 4; // enough?
3483   }
3484
3485   for (; i < opcnt && *adj < adj_expect; i++) {
3486     if (g_labels[i] != NULL)
3487       *is_multipath = 1;
3488
3489     po = &ops[i];
3490     if (po->flags & OPF_DONE)
3491       continue;
3492
3493     if (po->op == OP_ADD && po->operand[0].reg == xSP) {
3494       if (po->operand[1].type != OPT_CONST)
3495         ferr(&ops[i], "non-const esp adjust?\n");
3496       *adj += po->operand[1].val;
3497       if (*adj & 3)
3498         ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
3499       if (do_update) {
3500         if (!*is_multipath)
3501           patch_esp_adjust(po, adj_expect);
3502         else
3503           po->flags |= OPF_RMD;
3504       }
3505       return i;
3506     }
3507     else if (po->op == OP_PUSH) {
3508       //if (first_pop == -1)
3509       //  first_pop = -2; // none
3510       *adj -= lmod_bytes(po, po->operand[0].lmod);
3511     }
3512     else if (po->op == OP_POP) {
3513       if (!(po->flags & OPF_DONE)) {
3514         // seems like msvc only uses 'pop ecx' for stack realignment..
3515         if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
3516           break;
3517         if (first_pop == -1 && *adj >= 0)
3518           first_pop = i;
3519       }
3520       if (do_update && *adj >= 0) {
3521         po->flags |= OPF_RMD;
3522         if (!*is_multipath)
3523           po->flags |= OPF_DONE | OPF_NOREGS;
3524       }
3525
3526       *adj += lmod_bytes(po, po->operand[0].lmod);
3527       if (*adj > adj_best)
3528         adj_best = *adj;
3529     }
3530     else if (po->flags & (OPF_JMP|OPF_TAIL)) {
3531       if (po->op == OP_JMP && po->btj == NULL) {
3532         if (po->bt_i <= i)
3533           break;
3534         i = po->bt_i - 1;
3535         continue;
3536       }
3537       if (po->op != OP_CALL)
3538         break;
3539       if (po->operand[0].type != OPT_LABEL)
3540         break;
3541       if (po->pp != NULL && po->pp->is_stdcall)
3542         break;
3543       if (adj_expect_unknown && first_pop >= 0)
3544         break;
3545       // assume it's another cdecl call
3546     }
3547   }
3548
3549   if (first_pop >= 0) {
3550     // probably only 'pop ecx' was used
3551     *adj = adj_best;
3552     return first_pop;
3553   }
3554
3555   return -1;
3556 }
3557
3558 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
3559 {
3560   struct parsed_op *po;
3561   int j;
3562
3563   if (i < 0)
3564     ferr(ops, "%s: followed bad branch?\n", __func__);
3565
3566   for (; i < opcnt; i++) {
3567     po = &ops[i];
3568     if (po->cc_scratch == magic)
3569       return;
3570     po->cc_scratch = magic;
3571     po->flags |= flags;
3572
3573     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
3574       if (po->btj != NULL) {
3575         // jumptable
3576         for (j = 0; j < po->btj->count; j++)
3577           scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
3578         return;
3579       }
3580
3581       scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
3582       if (!(po->flags & OPF_CJMP))
3583         return;
3584     }
3585     if (po->flags & OPF_TAIL)
3586       return;
3587   }
3588 }
3589
3590 static const struct parsed_proto *try_recover_pp(
3591   struct parsed_op *po, const struct parsed_opr *opr,
3592   int is_call, int *search_instead)
3593 {
3594   const struct parsed_proto *pp = NULL;
3595   char buf[256];
3596   char *p;
3597
3598   if (po->pp != NULL && (po->flags & OPF_DATA)) {
3599     // hint given in asm
3600     return po->pp;
3601   }
3602
3603   // maybe an arg of g_func?
3604   if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
3605   {
3606     char ofs_reg[16] = { 0, };
3607     int arg, arg_s, arg_i;
3608     int stack_ra = 0;
3609     int offset = 0;
3610
3611     if (g_header_mode)
3612       return NULL;
3613
3614     parse_stack_access(po, opr->name, ofs_reg,
3615       &offset, &stack_ra, NULL, 0);
3616     if (ofs_reg[0] != 0)
3617       ferr(po, "offset reg on arg access?\n");
3618     if (offset <= stack_ra) {
3619       // search who set the stack var instead
3620       if (search_instead != NULL)
3621         *search_instead = 1;
3622       return NULL;
3623     }
3624
3625     arg_i = (offset - stack_ra - 4) / 4;
3626     for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
3627       if (g_func_pp->arg[arg].reg != NULL)
3628         continue;
3629       if (arg_s == arg_i)
3630         break;
3631       arg_s++;
3632     }
3633     if (arg == g_func_pp->argc)
3634       ferr(po, "stack arg %d not in prototype?\n", arg_i);
3635
3636     pp = g_func_pp->arg[arg].pp;
3637     if (is_call) {
3638       if (pp == NULL)
3639         ferr(po, "icall arg: arg%d has no pp\n", arg + 1);
3640       check_func_pp(po, pp, "icall arg");
3641     }
3642   }
3643   else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
3644     // label[index]
3645     p = strchr(opr->name + 1, '[');
3646     memcpy(buf, opr->name, p - opr->name);
3647     buf[p - opr->name] = 0;
3648     pp = proto_parse(g_fhdr, buf, g_quiet_pp);
3649   }
3650   else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
3651     pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
3652     if (pp == NULL) {
3653       if (!g_header_mode)
3654         ferr(po, "proto_parse failed for icall to '%s'\n", opr->name);
3655     }
3656     else
3657       check_func_pp(po, pp, "reg-fptr ref");
3658   }
3659
3660   return pp;
3661 }
3662
3663 static void scan_for_call_type(int i, const struct parsed_opr *opr,
3664   int magic, int is_call_op, const struct parsed_proto **pp_found,
3665   int *pp_i, int *multi)
3666 {
3667   const struct parsed_proto *pp = NULL;
3668   struct parsed_op *po;
3669   struct label_ref *lr;
3670
3671   ops[i].cc_scratch = magic;
3672
3673   while (1) {
3674     if (g_labels[i] != NULL) {
3675       lr = &g_label_refs[i];
3676       for (; lr != NULL; lr = lr->next) {
3677         check_i(&ops[i], lr->i);
3678         scan_for_call_type(lr->i, opr, magic, is_call_op,
3679           pp_found, pp_i, multi);
3680       }
3681       if (i > 0 && LAST_OP(i - 1))
3682         return;
3683     }
3684
3685     i--;
3686     if (i < 0)
3687       break;
3688
3689     if (ops[i].cc_scratch == magic)
3690       return;
3691     ops[i].cc_scratch = magic;
3692
3693     if (!(ops[i].flags & OPF_DATA))
3694       continue;
3695     if (!is_opr_modified(opr, &ops[i]))
3696       continue;
3697     if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
3698       // most probably trashed by some processing
3699       *pp_found = NULL;
3700       return;
3701     }
3702
3703     opr = &ops[i].operand[1];
3704     if (opr->type != OPT_REG)
3705       break;
3706   }
3707
3708   po = (i >= 0) ? &ops[i] : ops;
3709
3710   if (i < 0) {
3711     // reached the top - can only be an arg-reg
3712     if (opr->type != OPT_REG || g_func_pp == NULL)
3713       return;
3714
3715     for (i = 0; i < g_func_pp->argc; i++) {
3716       if (g_func_pp->arg[i].reg == NULL)
3717         continue;
3718       if (IS(opr->name, g_func_pp->arg[i].reg))
3719         break;
3720     }
3721     if (i == g_func_pp->argc)
3722       return;
3723     pp = g_func_pp->arg[i].pp;
3724     if (pp == NULL) {
3725       if (is_call_op)
3726         ferr(po, "icall: arg%d (%s) is not a fptr?\n",
3727           i + 1, g_func_pp->arg[i].reg);
3728       return;
3729     }
3730     check_func_pp(po, pp, "icall reg-arg");
3731   }
3732   else
3733     pp = try_recover_pp(po, opr, is_call_op, NULL);
3734
3735   if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
3736     if (pp_cmp_func(*pp_found, pp)) {
3737       if (pp_i != NULL && *pp_i != -1)
3738         fnote(&ops[*pp_i], "(other ref)\n");
3739       ferr(po, "icall: parsed_proto mismatch\n");
3740     }
3741     if (multi != NULL)
3742       *multi = 1;
3743   }
3744   if (pp != NULL) {
3745     *pp_found = pp;
3746     if (pp_i != NULL)
3747       *pp_i = po - ops;
3748   }
3749 }
3750
3751 static void add_label_ref(struct label_ref *lr, int op_i)
3752 {
3753   struct label_ref *lr_new;
3754
3755   if (lr->i == -1) {
3756     lr->i = op_i;
3757     return;
3758   }
3759
3760   lr_new = calloc(1, sizeof(*lr_new));
3761   lr_new->i = op_i;
3762   lr_new->next = lr->next;
3763   lr->next = lr_new;
3764 }
3765
3766 static struct parsed_data *try_resolve_jumptab(int i, int opcnt)
3767 {
3768   struct parsed_op *po = &ops[i];
3769   struct parsed_data *pd;
3770   char label[NAMELEN], *p;
3771   int len, j, l;
3772
3773   p = strchr(po->operand[0].name, '[');
3774   if (p == NULL)
3775     return NULL;
3776
3777   len = p - po->operand[0].name;
3778   strncpy(label, po->operand[0].name, len);
3779   label[len] = 0;
3780
3781   for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3782     if (IS(g_func_pd[j].label, label)) {
3783       pd = &g_func_pd[j];
3784       break;
3785     }
3786   }
3787   if (pd == NULL)
3788     //ferr(po, "label '%s' not parsed?\n", label);
3789     return NULL;
3790
3791   if (pd->type != OPT_OFFSET)
3792     ferr(po, "label '%s' with non-offset data?\n", label);
3793
3794   // find all labels, link
3795   for (j = 0; j < pd->count; j++) {
3796     for (l = 0; l < opcnt; l++) {
3797       if (g_labels[l] != NULL && IS(g_labels[l], pd->d[j].u.label)) {
3798         add_label_ref(&g_label_refs[l], i);
3799         pd->d[j].bt_i = l;
3800         break;
3801       }
3802     }
3803   }
3804
3805   return pd;
3806 }
3807
3808 static void clear_labels(int count)
3809 {
3810   int i;
3811
3812   for (i = 0; i < count; i++) {
3813     if (g_labels[i] != NULL) {
3814       free(g_labels[i]);
3815       g_labels[i] = NULL;
3816     }
3817   }
3818 }
3819
3820 static int get_pp_arg_regmask_src(const struct parsed_proto *pp)
3821 {
3822   int regmask = 0;
3823   int i, reg;
3824
3825   for (i = 0; i < pp->argc; i++) {
3826     if (pp->arg[i].reg != NULL) {
3827       reg = char_array_i(regs_r32,
3828               ARRAY_SIZE(regs_r32), pp->arg[i].reg);
3829       if (reg < 0)
3830         ferr(ops, "arg '%s' of func '%s' is not a reg?\n",
3831           pp->arg[i].reg, pp->name);
3832       regmask |= 1 << reg;
3833     }
3834   }
3835
3836   return regmask;
3837 }
3838
3839 static int get_pp_arg_regmask_dst(const struct parsed_proto *pp)
3840 {
3841   int regmask = 0;
3842   int i, reg;
3843
3844   if (pp->has_retreg) {
3845     for (i = 0; i < pp->argc; i++) {
3846       if (pp->arg[i].type.is_retreg) {
3847         reg = char_array_i(regs_r32,
3848                 ARRAY_SIZE(regs_r32), pp->arg[i].reg);
3849         ferr_assert(ops, reg >= 0);
3850         regmask |= 1 << reg;
3851       }
3852     }
3853   }
3854
3855   if (strstr(pp->ret_type.name, "int64"))
3856     return regmask | (1 << xAX) | (1 << xDX);
3857   if (IS(pp->ret_type.name, "float")
3858    || IS(pp->ret_type.name, "double"))
3859   {
3860     return regmask | mxST0;
3861   }
3862   if (strcasecmp(pp->ret_type.name, "void") == 0)
3863     return regmask;
3864
3865   return regmask | mxAX;
3866 }
3867
3868 static int are_ops_same(struct parsed_op *po1, struct parsed_op *po2)
3869 {
3870   return po1->op == po2->op && po1->operand_cnt == po2->operand_cnt
3871     && memcmp(po1->operand, po2->operand,
3872               sizeof(po1->operand[0]) * po1->operand_cnt) == 0;
3873 }
3874
3875 static void resolve_branches_parse_calls(int opcnt)
3876 {
3877   static const struct {
3878     const char *name;
3879     enum op_op op;
3880     unsigned int flags;
3881     unsigned int regmask_src;
3882     unsigned int regmask_dst;
3883   } pseudo_ops[] = {
3884     { "__allshl", OPP_ALLSHL, OPF_DATA, mxAX|mxDX|mxCX, mxAX|mxDX },
3885     { "__allshr", OPP_ALLSHR, OPF_DATA, mxAX|mxDX|mxCX, mxAX|mxDX },
3886     { "__ftol",   OPP_FTOL,   OPF_FPOP, mxST0, mxAX | mxDX },
3887     // more precise? Wine gets away with just __ftol handler
3888     { "__ftol2",  OPP_FTOL,   OPF_FPOP, mxST0, mxAX | mxDX },
3889     { "__CIpow",  OPP_CIPOW,  OPF_FPOP, mxST0|mxST1, mxST0 },
3890   };
3891   const struct parsed_proto *pp_c;
3892   struct parsed_proto *pp;
3893   struct parsed_data *pd;
3894   struct parsed_op *po;
3895   const char *tmpname;
3896   enum op_op prev_op;
3897   int i, l;
3898   int ret;
3899
3900   for (i = 0; i < opcnt; i++)
3901   {
3902     po = &ops[i];
3903     po->bt_i = -1;
3904     po->btj = NULL;
3905
3906     if (po->datap != NULL) {
3907       pp = calloc(1, sizeof(*pp));
3908       my_assert_not(pp, NULL);
3909
3910       ret = parse_protostr(po->datap, pp);
3911       if (ret < 0)
3912         ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3913       free(po->datap);
3914       po->datap = NULL;
3915       po->pp = pp;
3916     }
3917
3918     if (po->op == OP_CALL) {
3919       pp = NULL;
3920
3921       if (po->pp != NULL)
3922         pp = po->pp;
3923       else if (po->operand[0].type == OPT_LABEL)
3924       {
3925         tmpname = opr_name(po, 0);
3926         if (IS_START(tmpname, "loc_")) {
3927           if (!g_seh_found)
3928             ferr(po, "call to loc_*\n");
3929           // eliminate_seh() must take care of it
3930           continue;
3931         }
3932         if (IS(tmpname, "__alloca_probe"))
3933           continue;
3934         if (IS(tmpname, "__SEH_prolog")) {
3935           ferr_assert(po, g_seh_found == 0);
3936           g_seh_found = 2;
3937           continue;
3938         }
3939         if (IS(tmpname, "__SEH_epilog"))
3940           continue;
3941
3942         // convert some calls to pseudo-ops
3943         for (l = 0; l < ARRAY_SIZE(pseudo_ops); l++) {
3944           if (!IS(tmpname, pseudo_ops[l].name))
3945             continue;
3946
3947           po->op = pseudo_ops[l].op;
3948           po->operand_cnt = 0;
3949           po->regmask_src = pseudo_ops[l].regmask_src;
3950           po->regmask_dst = pseudo_ops[l].regmask_dst;
3951           po->flags &= OPF_TAIL;
3952           po->flags |= pseudo_ops[l].flags;
3953           po->flags |= po->regmask_dst ? OPF_DATA : 0;
3954           break;
3955         }
3956         if (l < ARRAY_SIZE(pseudo_ops))
3957           continue;
3958
3959         pp_c = proto_parse(g_fhdr, tmpname, g_header_mode);
3960         if (!g_header_mode && pp_c == NULL)
3961           ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3962
3963         if (pp_c != NULL) {
3964           pp = proto_clone(pp_c);
3965           my_assert_not(pp, NULL);
3966         }
3967       }
3968
3969       if (pp != NULL) {
3970         if (pp->is_fptr)
3971           check_func_pp(po, pp, "fptr var call");
3972         if (pp->is_noreturn) {
3973           po->flags |= OPF_TAIL;
3974           po->flags &= ~OPF_ATAIL; // most likely...
3975         }
3976       }
3977       po->pp = pp;
3978       continue;
3979     }
3980
3981     if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3982       continue;
3983
3984     if (po->operand[0].type == OPT_REGMEM) {
3985       pd = try_resolve_jumptab(i, opcnt);
3986       if (pd == NULL)
3987         goto tailcall;
3988
3989       po->btj = pd;
3990       continue;
3991     }
3992
3993     for (l = 0; l < opcnt; l++) {
3994       if (g_labels[l] != NULL
3995           && IS(po->operand[0].name, g_labels[l]))
3996       {
3997         if (l == i + 1 && po->op == OP_JMP) {
3998           // yet another alignment type...
3999           po->flags |= OPF_RMD | OPF_DONE;
4000           po->flags &= ~OPF_JMP;
4001           po->op = OP_NOP;
4002           break;
4003         }
4004         add_label_ref(&g_label_refs[l], i);
4005         po->bt_i = l;
4006         break;
4007       }
4008     }
4009
4010     if (po->bt_i != -1 || (po->flags & OPF_RMD))
4011       continue;
4012
4013     if (po->operand[0].type == OPT_LABEL
4014         || po->operand[0].type == OPT_REG)
4015       // assume tail call
4016       goto tailcall;
4017
4018     ferr(po, "unhandled branch\n");
4019
4020 tailcall:
4021     po->op = OP_CALL;
4022     po->flags |= OPF_TAIL;
4023     prev_op = i > 0 ? ops[i - 1].op : OP_UD2;
4024     if (prev_op == OP_POP)
4025       po->flags |= OPF_ATAIL;
4026     if (g_stack_fsz + g_bp_frame == 0 && prev_op != OP_PUSH
4027       && (g_func_pp == NULL || g_func_pp->argc_stack > 0))
4028     {
4029       po->flags |= OPF_ATAIL;
4030     }
4031     i--; // reprocess
4032   }
4033 }
4034
4035 static int resolve_origin(int i, const struct parsed_opr *opr,
4036   int magic, int *op_i, int *is_caller);
4037 static void set_label(int i, const char *name);
4038
4039 static void eliminate_seh_writes(int opcnt)
4040 {
4041   const struct parsed_opr *opr;
4042   char ofs_reg[16];
4043   int offset;
4044   int i;
4045
4046   // assume all sf writes above g_seh_size to be seh related
4047   // (probably unsafe but oh well)
4048   for (i = 0; i < opcnt; i++) {
4049     if (ops[i].op != OP_MOV)
4050       continue;
4051     opr = &ops[i].operand[0];
4052     if (opr->type != OPT_REGMEM)
4053       continue;
4054     if (!is_stack_access(&ops[i], opr))
4055       continue;
4056
4057     offset = 0;
4058     parse_stack_access(&ops[i], opr->name, ofs_reg, &offset,
4059       NULL, NULL, 0);
4060     if (offset < 0 && offset >= -g_seh_size)
4061       ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4062   }
4063 }
4064
4065 static void eliminate_seh_finally(int opcnt)
4066 {
4067   const char *target_name = NULL;
4068   const char *return_name = NULL;
4069   int exits[MAX_EXITS];
4070   int exit_count = 0;
4071   int call_i = -1;
4072   int target_i = -1;
4073   int return_i = -1;
4074   int tgend_i = -1;
4075   int i;
4076
4077   for (i = 0; i < opcnt; i++) {
4078     if (ops[i].op != OP_CALL)
4079       continue;
4080     if (!IS_START(opr_name(&ops[i], 0), "loc_"))
4081       continue;
4082     if (target_name != NULL)
4083       ferr(&ops[i], "multiple finally calls? (last was %s)\n",
4084         target_name);
4085     target_name = opr_name(&ops[i], 0);
4086     call_i = i;
4087
4088     if (g_labels[i + 1] == NULL)
4089       set_label(i + 1, "seh_fin_done");
4090     return_name = g_labels[i + 1];
4091     return_i = i + 1;
4092   }
4093
4094   if (call_i == -1)
4095     // no finally block
4096     return;
4097
4098   // find finally code (bt_i is not set because it's call)
4099   for (i = 0; i < opcnt; i++) {
4100     if (g_labels[i] == NULL)
4101       continue;
4102     if (!IS(g_labels[i], target_name))
4103       continue;
4104
4105     ferr_assert(&ops[i], target_i == -1);
4106     target_i = i;
4107   }
4108   ferr_assert(&ops[0], target_i != -1);
4109
4110   find_reachable_exits(target_i, opcnt, target_i + opcnt * 24,
4111     exits, &exit_count);
4112   ferr_assert(&ops[target_i], exit_count == 1);
4113   ferr_assert(&ops[target_i], ops[exits[0]].op == OP_RET);
4114   tgend_i = exits[0];
4115
4116   // convert to jumps, link
4117   ops[call_i].op = OP_JMP;
4118   ops[call_i].bt_i = target_i;
4119   add_label_ref(&g_label_refs[target_i], call_i);
4120
4121   ops[tgend_i].op = OP_JMP;
4122   ops[tgend_i].flags &= ~OPF_TAIL;
4123   ops[tgend_i].flags |= OPF_JMP;
4124   ops[tgend_i].bt_i = return_i;
4125   ops[tgend_i].operand_cnt = 1;
4126   ops[tgend_i].operand[0].type = OPT_LABEL;
4127   snprintf(ops[tgend_i].operand[0].name, NAMELEN, "%s", return_name);
4128   add_label_ref(&g_label_refs[return_i], tgend_i);
4129
4130   // rm seh finally entry code
4131   for (i = target_i - 1; i >= 0; i--) {
4132     if (g_labels[i] != NULL && g_label_refs[i].i != -1)
4133       return;
4134     if (ops[i].flags & OPF_CJMP)
4135       return;
4136     if (ops[i].flags & (OPF_JMP | OPF_TAIL))
4137       break;
4138   }
4139   for (i = target_i - 1; i >= 0; i--) {
4140     if (ops[i].flags & (OPF_JMP | OPF_TAIL))
4141       break;
4142     ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4143   }
4144 }
4145
4146 static void eliminate_seh(int opcnt)
4147 {
4148   int i, j, k, ret;
4149
4150   for (i = 0; i < opcnt; i++) {
4151     if (ops[i].op != OP_MOV)
4152       continue;
4153     if (ops[i].operand[0].segment != SEG_FS)
4154       continue;
4155     if (!IS(opr_name(&ops[i], 0), "0"))
4156       continue;
4157
4158     ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4159     if (ops[i].operand[1].reg == xSP) {
4160       for (j = i - 1; j >= 0; j--) {
4161         if (ops[j].op != OP_PUSH)
4162           continue;
4163         ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4164         g_seh_size += 4;
4165         if (ops[j].operand[0].val == ~0)
4166           break;
4167         if (ops[j].operand[0].type == OPT_REG) {
4168           k = -1;
4169           ret = resolve_origin(j, &ops[j].operand[0],
4170                   j + opcnt * 22, &k, NULL);
4171           if (ret == 1)
4172             ops[k].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4173         }
4174       }
4175       if (j < 0)
4176         ferr(ops, "missing seh terminator\n");
4177     }
4178     else {
4179       k = -1;
4180       ret = resolve_origin(i, &ops[i].operand[1],
4181               i + opcnt * 23, &k, NULL);
4182       if (ret == 1)
4183         ops[k].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4184     }
4185   }
4186
4187   eliminate_seh_writes(opcnt);
4188   eliminate_seh_finally(opcnt);
4189 }
4190
4191 static void eliminate_seh_calls(int opcnt)
4192 {
4193   int epilog_found = 0;
4194   int i;
4195
4196   g_bp_frame = 1;
4197   g_seh_size = 0x10;
4198
4199   i = 0;
4200   ferr_assert(&ops[i], ops[i].op == OP_PUSH
4201                && ops[i].operand[0].type == OPT_CONST);
4202   g_stack_fsz = g_seh_size + ops[i].operand[0].val;
4203   ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4204
4205   i++;
4206   ferr_assert(&ops[i], ops[i].op == OP_PUSH
4207                && ops[i].operand[0].type == OPT_OFFSET);
4208   ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4209
4210   i++;
4211   ferr_assert(&ops[i], ops[i].op == OP_CALL
4212                && IS(opr_name(&ops[i], 0), "__SEH_prolog"));
4213   ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4214
4215   for (i++; i < opcnt; i++) {
4216     if (ops[i].op != OP_CALL)
4217       continue;
4218     if (!IS(opr_name(&ops[i], 0), "__SEH_epilog"))
4219       continue;
4220
4221     ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4222     epilog_found = 1;
4223   }
4224   ferr_assert(ops, epilog_found);
4225
4226   eliminate_seh_writes(opcnt);
4227   eliminate_seh_finally(opcnt);
4228 }
4229
4230 // check for prologue of many pushes and epilogue with pops
4231 static void check_simple_sequence(int opcnt, int *fsz)
4232 {
4233   int found = 0;
4234   int seq_len;
4235   int seq_p;
4236   int seq[4];
4237   int reg;
4238   int i, j;
4239
4240   for (i = 0; i < opcnt && i < ARRAY_SIZE(seq); i++) {
4241     if (ops[i].op != OP_PUSH || ops[i].operand[0].type != OPT_REG)
4242       break;
4243     reg = ops[i].operand[0].reg;
4244     if (reg != xBX && reg != xSI && reg != xDI && reg != xBP)
4245       break;
4246     for (j = 0; j < i; j++)
4247       if (seq[j] == reg)
4248         break;
4249     if (j != i)
4250       // probably something else is going on here
4251       break;
4252     seq[i] = reg;
4253   }
4254   seq_len = i;
4255   if (seq_len == 0)
4256     return;
4257
4258   for (; i < opcnt && seq_len > 0; i++) {
4259     if (!(ops[i].flags & OPF_TAIL))
4260       continue;
4261
4262     for (j = i - 1, seq_p = 0; j >= 0 && seq_p < seq_len; j--) {
4263       if (ops[j].op != OP_POP || ops[j].operand[0].type != OPT_REG)
4264         break;
4265       if (ops[j].operand[0].reg != seq[seq_p])
4266         break;
4267       seq_p++;
4268     }
4269     found = seq_len = seq_p;
4270   }
4271   if (!found)
4272     return;
4273
4274   for (i = 0; i < seq_len; i++)
4275     ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4276
4277   for (; i < opcnt && seq_len > 0; i++) {
4278     if (!(ops[i].flags & OPF_TAIL))
4279       continue;
4280
4281     for (j = i - 1, seq_p = 0; j >= 0 && seq_p < seq_len; j--) {
4282       ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4283       seq_p++;
4284     }
4285   }
4286
4287   // unlike pushes after sub esp,
4288   // IDA treats pushes like this as part of var area
4289   *fsz += seq_len * 4;
4290 }
4291
4292 static int scan_prologue_ecx(int i, int opcnt, int flags_set,
4293   int limit, int *ecx_push_out)
4294 {
4295   const struct parsed_proto *pp;
4296   int ecx_push = 0, other_push = 0;
4297   int ret;
4298
4299   while (limit > 0 && ops[i].op == OP_PUSH
4300          && IS(opr_name(&ops[i], 0), "ecx"))
4301   {
4302     ops[i].flags |= flags_set;
4303     ecx_push++;
4304     i++;
4305     limit--;
4306   }
4307
4308   ret = i;
4309   if (ecx_push == 0 || flags_set != 0)
4310     goto out;
4311
4312   // check if some of the pushes aren't really call args
4313   for (; i < opcnt; i++) {
4314     if (i > 0 && g_labels[i] != NULL)
4315       break;
4316     if (ops[i].flags & (OPF_JMP|OPF_TAIL))
4317       break;
4318     if (ops[i].op == OP_PUSH)
4319       other_push++;
4320   }
4321
4322   if (ops[i].op != OP_CALL)
4323     goto out;
4324
4325   pp = ops[i].pp;
4326   if (pp == NULL && ops[i].operand[0].type == OPT_LABEL)
4327     pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
4328   if (pp == NULL)
4329     goto out;
4330
4331   ferr_assert(&ops[i], ecx_push + other_push >= pp->argc_stack);
4332   if (other_push < pp->argc_stack)
4333     ecx_push -= pp->argc_stack - other_push;
4334
4335 out:
4336   if (ecx_push_out != NULL)
4337     *ecx_push_out = ecx_push;
4338   return ret;
4339 }
4340
4341 static int scan_prologue(int i, int opcnt, int *ecx_push, int *esp_sub)
4342 {
4343   const char *name;
4344   int j, len, ret;
4345   int ecx_tmp = 0;
4346
4347   for (; i < opcnt; i++)
4348     if (!(ops[i].flags & OPF_DONE))
4349       break;
4350
4351   ret = scan_prologue_ecx(i, opcnt, 0, 4, &ecx_tmp);
4352   if (ecx_tmp > 0) {
4353     scan_prologue_ecx(i, opcnt, OPF_RMD | OPF_DONE | OPF_NOREGS,
4354       ecx_tmp, NULL);
4355     g_stack_fsz += 4 * ecx_tmp;
4356     *ecx_push += ecx_tmp;
4357     i = ret;
4358   }
4359
4360   for (; i < opcnt; i++) {
4361     if (i > 0 && g_labels[i] != NULL)
4362       break;
4363     if (ops[i].flags & (OPF_JMP|OPF_TAIL))
4364       break;
4365     if (ops[i].flags & OPF_DONE)
4366       continue;
4367     if (ops[i].op == OP_PUSH)
4368       break;
4369     if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
4370       && ops[i].operand[1].type == OPT_CONST)
4371     {
4372       g_stack_fsz += opr_const(&ops[i], 1);
4373       ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4374       i++;
4375       *esp_sub = 1;
4376       break;
4377     }
4378     if (ops[i].op == OP_LEA && ops[i].operand[0].reg == xSP
4379       && ops[i].operand[1].type == OPT_REGMEM
4380       && IS_START(ops[i].operand[1].name, "esp-"))
4381     {
4382       name = ops[i].operand[1].name;
4383       ret = sscanf(name, "esp-%x%n", &j, &len);
4384       ferr_assert(&ops[i], ret == 1 && len == strlen(name));
4385       g_stack_fsz += j;
4386       ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4387       i++;
4388       *esp_sub = 1;
4389       break;
4390     }
4391     if (ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
4392         && ops[i].operand[1].type == OPT_CONST)
4393     {
4394       for (j = i + 1; j < opcnt; j++)
4395         if (!(ops[j].flags & OPF_DONE))
4396           break;
4397       if (ops[j].op == OP_CALL
4398         && IS(opr_name(&ops[j], 0), "__alloca_probe"))
4399       {
4400         g_stack_fsz += opr_const(&ops[i], 1);
4401         ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4402         ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4403         i = j + 1;
4404         *esp_sub = 1;
4405         break;
4406       }
4407     }
4408   }
4409
4410   return i;
4411 }
4412
4413 static void scan_prologue_epilogue(int opcnt, int *stack_align)
4414 {
4415   int ecx_push = 0, esp_sub = 0, pusha = 0;
4416   int sandard_epilogue;
4417   int found, ret, len;
4418   int push_fsz = 0;
4419   int i, j, l;
4420
4421   if (g_seh_found == 2) {
4422     eliminate_seh_calls(opcnt);
4423     return;
4424   }
4425   if (g_seh_found) {
4426     eliminate_seh(opcnt);
4427     // ida treats seh as part of sf
4428     g_stack_fsz = g_seh_size;
4429     esp_sub = 1;
4430   }
4431
4432   if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
4433       && ops[1].op == OP_MOV
4434       && IS(opr_name(&ops[1], 0), "ebp")
4435       && IS(opr_name(&ops[1], 1), "esp"))
4436   {
4437     g_bp_frame = 1;
4438     ops[0].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4439     ops[1].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4440
4441     for (i = 2; i < opcnt; i++)
4442       if (!(ops[i].flags & OPF_DONE))
4443         break;
4444
4445     if (ops[i].op == OP_PUSHA) {
4446       ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4447       pusha = 1;
4448       i++;
4449     }
4450
4451     if (ops[i].op == OP_AND && ops[i].operand[0].reg == xSP
4452         && ops[i].operand[1].type == OPT_CONST)
4453     {
4454       l = ops[i].operand[1].val;
4455       j = ffs(l) - 1;
4456       if (j == -1 || (l >> j) != -1)
4457         ferr(&ops[i], "unhandled esp align: %x\n", l);
4458       if (stack_align != NULL)
4459         *stack_align = 1 << j;
4460       ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4461       i++;
4462     }
4463
4464     i = scan_prologue(i, opcnt, &ecx_push, &esp_sub);
4465
4466     found = 0;
4467     do {
4468       for (; i < opcnt; i++)
4469         if (ops[i].flags & OPF_TAIL)
4470           break;
4471       j = i - 1;
4472       if (i == opcnt && (ops[j].flags & OPF_JMP)) {
4473         if (ops[j].bt_i != -1 || ops[j].btj != NULL)
4474           break;
4475         i--;
4476         j--;
4477       }
4478
4479       sandard_epilogue = 0;
4480       if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
4481       {
4482         ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4483         // the standard epilogue is sometimes even used without a sf
4484         if (ops[j - 1].op == OP_MOV
4485             && IS(opr_name(&ops[j - 1], 0), "esp")
4486             && IS(opr_name(&ops[j - 1], 1), "ebp"))
4487           sandard_epilogue = 1;
4488       }
4489       else if (ops[j].op == OP_LEAVE)
4490       {
4491         ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4492         sandard_epilogue = 1;
4493       }
4494       else if (ops[i].op == OP_CALL && ops[i].pp != NULL
4495         && ops[i].pp->is_noreturn)
4496       {
4497         // on noreturn, msvc sometimes cleans stack, sometimes not
4498         i++;
4499         found = 1;
4500         continue;
4501       }
4502       else if (!(g_ida_func_attr & IDAFA_NORETURN))
4503         ferr(&ops[j], "'pop ebp' expected\n");
4504
4505       if (g_stack_fsz != 0 || sandard_epilogue) {
4506         if (ops[j].op == OP_LEAVE)
4507           j--;
4508         else if (sandard_epilogue) // mov esp, ebp
4509         {
4510           ops[j - 1].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4511           j -= 2;
4512         }
4513         else if (!(g_ida_func_attr & IDAFA_NORETURN))
4514         {
4515           ferr(&ops[j], "esp restore expected\n");
4516         }
4517
4518         if (ecx_push && j >= 0 && ops[j].op == OP_POP
4519           && IS(opr_name(&ops[j], 0), "ecx"))
4520         {
4521           ferr(&ops[j], "unexpected ecx pop\n");
4522         }
4523       }
4524
4525       if (pusha) {
4526         if (ops[j].op == OP_POPA)
4527           ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4528         else
4529           ferr(&ops[j], "popa expected\n");
4530       }
4531
4532       found = 1;
4533       i++;
4534     } while (i < opcnt);
4535
4536     if (!found)
4537       ferr(ops, "missing ebp epilogue\n");
4538     return;
4539   }
4540
4541   // non-bp frame
4542   check_simple_sequence(opcnt, &push_fsz);
4543   i = scan_prologue(0, opcnt, &ecx_push, &esp_sub);
4544
4545   found = 0;
4546   if (ecx_push || esp_sub)
4547   {
4548     g_sp_frame = 1;
4549
4550     do {
4551       for (; i < opcnt; i++)
4552         if (ops[i].flags & OPF_TAIL)
4553           break;
4554
4555       j = i - 1;
4556       if (i == opcnt && (ops[j].flags & OPF_JMP)) {
4557         if (ops[j].bt_i != -1 || ops[j].btj != NULL)
4558           break;
4559         i--;
4560         j--;
4561       }
4562       else if (i < opcnt && (ops[i].flags & OPF_ATAIL)) {
4563         // skip arg updates for arg-reuse tailcall
4564         for (; j >= 0; j--) {
4565           if (ops[j].op != OP_MOV)
4566             break;
4567           if (ops[j].operand[0].type == OPT_REGMEM
4568               && strstr(ops[j].operand[0].name, "arg_") != NULL)
4569             continue;
4570           if (ops[j].operand[0].type == OPT_REG)
4571             continue; // assume arg-reg mov
4572           break;
4573         }
4574       }
4575
4576       for (; j >= 0; j--) {
4577         if ((ops[j].flags & (OPF_RMD | OPF_DONE | OPF_NOREGS)) !=
4578             (OPF_RMD | OPF_DONE | OPF_NOREGS))
4579           break;
4580       }
4581
4582       if (ecx_push > 0 && !esp_sub) {
4583         for (l = 0; l < ecx_push && j >= 0; l++) {
4584           if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ecx"))
4585             /* pop ecx */;
4586           else if (ops[j].op == OP_ADD
4587                    && IS(opr_name(&ops[j], 0), "esp")
4588                    && ops[j].operand[1].type == OPT_CONST)
4589           {
4590             /* add esp, N */
4591             l += ops[j].operand[1].val / 4 - 1;
4592           }
4593           else
4594             break;
4595
4596           ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4597           j--;
4598         }
4599         if (l != ecx_push) {
4600           if (i < opcnt && ops[i].op == OP_CALL
4601             && ops[i].pp != NULL && ops[i].pp->is_noreturn)
4602           {
4603             // noreturn tailcall with no epilogue
4604             i++;
4605             found = 1;
4606             continue;
4607           }
4608           ferr(&ops[j], "epilogue scan failed\n");
4609         }
4610
4611         found = 1;
4612       }
4613
4614       if (esp_sub) {
4615         if (ops[j].op == OP_ADD
4616             && IS(opr_name(&ops[j], 0), "esp")
4617             && ops[j].operand[1].type == OPT_CONST)
4618         {
4619           if (ops[j].operand[1].val < g_stack_fsz)
4620             ferr(&ops[j], "esp adj is too low (need %d)\n", g_stack_fsz);
4621
4622           ops[j].operand[1].val -= g_stack_fsz; // for stack arg scanner
4623           if (ops[j].operand[1].val == 0)
4624             ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4625           found = 1;
4626         }
4627         else if (ops[j].op == OP_LEA && ops[j].operand[0].reg == xSP
4628           && ops[j].operand[1].type == OPT_REGMEM
4629           && IS_START(ops[j].operand[1].name, "esp+"))
4630         {
4631           const char *name = ops[j].operand[1].name;
4632           ret = sscanf(name, "esp+%x%n", &l, &len);
4633           ferr_assert(&ops[j], ret == 1 && len == strlen(name));
4634           ferr_assert(&ops[j], l <= g_stack_fsz);
4635           ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
4636           found = 1;
4637         }
4638         else if (i < opcnt && ops[i].op == OP_CALL
4639           && ops[i].pp != NULL && ops[i].pp->is_noreturn)
4640         {
4641           // noreturn tailcall with no epilogue
4642           found = 1;
4643         }
4644         else
4645           ferr(&ops[j], "'add esp' expected\n");
4646       }
4647
4648       i++;
4649     } while (i < opcnt);
4650
4651     if (!found)
4652       ferr(ops, "missing esp epilogue\n");
4653   }
4654
4655   if (g_stack_fsz != 0)
4656     // see check_simple_sequence
4657     g_stack_fsz += push_fsz;
4658 }
4659
4660 // find an instruction that changed opr before i op
4661 // *op_i must be set to -1 by the caller
4662 // *is_caller is set to 1 if one source is determined to be g_func arg
4663 // returns 1 if found, *op_i is then set to origin
4664 // returns -1 if multiple origins are found
4665 static int resolve_origin(int i, const struct parsed_opr *opr,
4666   int magic, int *op_i, int *is_caller)
4667 {
4668   struct label_ref *lr;
4669   int ret = 0;
4670
4671   while (1) {
4672     if (g_labels[i] != NULL) {
4673       lr = &g_label_refs[i];
4674       for (; lr != NULL; lr = lr->next) {
4675         check_i(&ops[i], lr->i);
4676         ret |= resolve_origin(lr->i, opr, magic, op_i, is_caller);
4677       }
4678       if (i > 0 && LAST_OP(i - 1))
4679         return ret;
4680     }
4681
4682     i--;
4683     if (i < 0) {
4684       if (is_caller != NULL)
4685         *is_caller = 1;
4686       return -1;
4687     }
4688
4689     if (ops[i].cc_scratch == magic)
4690       return ret;
4691     ops[i].cc_scratch = magic;
4692
4693     if (!(ops[i].flags & OPF_DATA))
4694       continue;
4695     if (!is_opr_modified(opr, &ops[i]))
4696       continue;
4697
4698     if (*op_i >= 0) {
4699       if (*op_i == i || are_ops_same(&ops[*op_i], &ops[i]))
4700         return ret | 1;
4701
4702       return -1;
4703     }
4704
4705     *op_i = i;
4706     return ret | 1;
4707   }
4708 }
4709
4710 static int resolve_origin_reg(int i, int reg, int magic, int *op_i,
4711   int *is_caller)
4712 {
4713   struct parsed_opr opr = OPR_INIT(OPT_REG, OPLM_DWORD, reg);
4714
4715   *op_i = -1;
4716   if (is_caller != NULL)
4717     *is_caller = 0;
4718   return resolve_origin(i, &opr, magic, op_i, is_caller);
4719 }
4720
4721 // find an instruction that previously referenced opr
4722 // if multiple results are found - fail
4723 // *op_i must be set to -1 by the caller
4724 // returns 1 if found, *op_i is then set to referencer insn
4725 static int resolve_last_ref(int i, const struct parsed_opr *opr,
4726   int magic, int *op_i)
4727 {
4728   struct label_ref *lr;
4729   int ret = 0;
4730
4731   while (1) {
4732     if (g_labels[i] != NULL) {
4733       lr = &g_label_refs[i];
4734       for (; lr != NULL; lr = lr->next) {
4735         check_i(&ops[i], lr->i);
4736         ret |= resolve_last_ref(lr->i, opr, magic, op_i);
4737       }
4738       if (i > 0 && LAST_OP(i - 1))
4739         return ret;
4740     }
4741
4742     i--;
4743     if (i < 0)
4744       return -1;
4745
4746     if (ops[i].cc_scratch == magic)
4747       return 0;
4748     ops[i].cc_scratch = magic;
4749
4750     if (!is_opr_referenced(opr, &ops[i]))
4751       continue;
4752
4753     if (*op_i >= 0)
4754       return -1;
4755
4756     *op_i = i;
4757     return 1;
4758   }
4759 }
4760
4761 // adjust datap of all reachable 'op' insns when moving back
4762 // returns  1 if at least 1 op was found
4763 // returns -1 if path without an op was found
4764 static int adjust_prev_op(int i, enum op_op op, int magic, void *datap)
4765 {
4766   struct label_ref *lr;
4767   int ret = 0;
4768
4769   if (ops[i].cc_scratch == magic)
4770     return 0;
4771   ops[i].cc_scratch = magic;
4772
4773   while (1) {
4774     if (g_labels[i] != NULL) {
4775       lr = &g_label_refs[i];
4776       for (; lr != NULL; lr = lr->next) {
4777         check_i(&ops[i], lr->i);
4778         ret |= adjust_prev_op(lr->i, op, magic, datap);
4779       }
4780       if (i > 0 && LAST_OP(i - 1))
4781         return ret;
4782     }
4783
4784     i--;
4785     if (i < 0)
4786       return -1;
4787
4788     if (ops[i].cc_scratch == magic)
4789       return 0;
4790     ops[i].cc_scratch = magic;
4791
4792     if (ops[i].op != op)
4793       continue;
4794
4795     ops[i].datap = datap;
4796     return 1;
4797   }
4798 }
4799
4800 // find next instruction that reads opr
4801 // *op_i must be set to -1 by the caller
4802 // on return, *op_i is set to first referencer insn
4803 // returns 1 if exactly 1 referencer is found
4804 static int find_next_read(int i, int opcnt,
4805   const struct parsed_opr *opr, int magic, int *op_i)
4806 {
4807   struct parsed_op *po;
4808   int j, ret = 0;
4809
4810   for (; i < opcnt; i++)
4811   {
4812     if (ops[i].cc_scratch == magic)
4813       return ret;
4814     ops[i].cc_scratch = magic;
4815
4816     po = &ops[i];
4817     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
4818       if (po->btj != NULL) {
4819         // jumptable
4820         for (j = 0; j < po->btj->count; j++) {
4821           check_i(po, po->btj->d[j].bt_i);
4822           ret |= find_next_read(po->btj->d[j].bt_i, opcnt, opr,
4823                    magic, op_i);
4824         }
4825         return ret;
4826       }
4827
4828       if (po->flags & OPF_RMD)
4829         continue;
4830       check_i(po, po->bt_i);
4831       if (po->flags & OPF_CJMP) {
4832         ret |= find_next_read(po->bt_i, opcnt, opr, magic, op_i);
4833         if (ret < 0)
4834           return ret;
4835       }
4836       else
4837         i = po->bt_i - 1;
4838       continue;
4839     }
4840
4841     if (!is_opr_read(opr, po)) {
4842       int full_opr = 1;
4843       if (opr->type == OPT_REG && po->operand[0].type == OPT_REG
4844           && opr->reg == po->operand[0].reg && (po->flags & OPF_DATA))
4845       {
4846         full_opr = po->operand[0].lmod >= opr->lmod;
4847       }
4848       if (is_opr_modified(opr, po) && full_opr) {
4849         // it's overwritten
4850         return ret;
4851       }
4852       if (po->flags & OPF_TAIL)
4853         return ret;
4854       continue;
4855     }
4856
4857     if (*op_i >= 0)
4858       return -1;
4859
4860     *op_i = i;
4861     return 1;
4862   }
4863
4864   return 0;
4865 }
4866
4867 static int find_next_read_reg(int i, int opcnt, int reg,
4868   enum opr_lenmod lmod, int magic, int *op_i)
4869 {
4870   struct parsed_opr opr = OPR_INIT(OPT_REG, lmod, reg);
4871
4872   *op_i = -1;
4873   return find_next_read(i, opcnt, &opr, magic, op_i);
4874 }
4875
4876 // find next instruction that reads opr
4877 // *op_i must be set to -1 by the caller
4878 // on return, *op_i is set to first flag user insn
4879 // returns 1 if exactly 1 flag user is found
4880 static int find_next_flag_use(int i, int opcnt, int magic, int *op_i)
4881 {
4882   struct parsed_op *po;
4883   int j, ret = 0;
4884
4885   for (; i < opcnt; i++)
4886   {
4887     if (ops[i].cc_scratch == magic)
4888       return ret;
4889     ops[i].cc_scratch = magic;
4890
4891     po = &ops[i];
4892     if (po->op == OP_CALL)
4893       return -1;
4894     if (po->flags & OPF_JMP) {
4895       if (po->btj != NULL) {
4896         // jumptable
4897         for (j = 0; j < po->btj->count; j++) {
4898           check_i(po, po->btj->d[j].bt_i);
4899           ret |= find_next_flag_use(po->btj->d[j].bt_i, opcnt,
4900                    magic, op_i);
4901         }
4902         return ret;
4903       }
4904
4905       if (po->flags & OPF_RMD)
4906         continue;
4907       check_i(po, po->bt_i);
4908       if (po->flags & OPF_CJMP)
4909         goto found;
4910       else
4911         i = po->bt_i - 1;
4912       continue;
4913     }
4914
4915     if (!(po->flags & OPF_CC)) {
4916       if (po->flags & OPF_FLAGS)
4917         // flags changed
4918         return ret;
4919       if (po->flags & OPF_TAIL)
4920         return ret;
4921       continue;
4922     }
4923
4924 found:
4925     if (*op_i >= 0)
4926       return -1;
4927
4928     *op_i = i;
4929     return 1;
4930   }
4931
4932   return 0;
4933 }
4934
4935 static int try_resolve_const(int i, const struct parsed_opr *opr,
4936   int magic, unsigned int *val)
4937 {
4938   int s_i = -1;
4939   int ret;
4940
4941   ret = resolve_origin(i, opr, magic, &s_i, NULL);
4942   if (ret == 1) {
4943     i = s_i;
4944     if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
4945       return -1;
4946
4947     *val = ops[i].operand[1].val;
4948     return 1;
4949   }
4950
4951   return -1;
4952 }
4953
4954 static int resolve_used_bits(int i, int opcnt, int reg,
4955   int *mask, int *is_z_check)
4956 {
4957   struct parsed_opr opr = OPR_INIT(OPT_REG, OPLM_WORD, reg);
4958   int j = -1, k = -1;
4959   int ret;
4960
4961   ret = find_next_read(i, opcnt, &opr, i + opcnt * 20, &j);
4962   if (ret != 1)
4963     return -1;
4964
4965   find_next_read(j + 1, opcnt, &opr, i + opcnt * 20 + 1, &k);
4966   if (k != -1) {
4967     fnote(&ops[j], "(first read)\n");
4968     ferr(&ops[k], "TODO: bit resolve: multiple readers\n");
4969   }
4970
4971   if (ops[j].op != OP_TEST || ops[j].operand[1].type != OPT_CONST)
4972     ferr(&ops[j], "TODO: bit resolve: not a const test\n");
4973
4974   ferr_assert(&ops[j], ops[j].operand[0].type == OPT_REG);
4975   ferr_assert(&ops[j], ops[j].operand[0].reg == reg);
4976
4977   *mask = ops[j].operand[1].val;
4978   if (ops[j].operand[0].lmod == OPLM_BYTE
4979     && ops[j].operand[0].name[1] == 'h')
4980   {
4981     *mask <<= 8;
4982   }
4983   ferr_assert(&ops[j], (*mask & ~0xffff) == 0);
4984
4985   *is_z_check = 0;
4986   ret = find_next_flag_use(j + 1, opcnt, i + opcnt * 20 + 2, &k);
4987   if (ret == 1)
4988     *is_z_check = ops[k].pfo == PFO_Z;
4989
4990   return 0;
4991 }
4992
4993 static const struct parsed_proto *resolve_deref(int i, int magic,
4994   const struct parsed_opr *opr, int level)
4995 {
4996   const struct parsed_proto *pp = NULL;
4997   int from_caller = 0;
4998   char s_reg[4];
4999   int offset = 0;
5000   int len = 0;
5001   int j = -1;
5002   int k = -1;
5003   int reg;
5004   int ret;
5005
5006   ret = sscanf(opr->name, "%3s+%x%n", s_reg, &offset, &len);
5007   if (ret != 2 || len != strlen(opr->name)) {
5008     ret = sscanf(opr->name, "%3s%n", s_reg, &len);
5009     if (ret != 1 || len != strlen(opr->name))
5010       return NULL;
5011   }
5012
5013   reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s_reg);
5014   if (reg < 0)
5015     return NULL;
5016
5017   ret = resolve_origin_reg(i, reg, i + magic, &j, NULL);
5018   if (ret != 1)
5019     return NULL;
5020
5021   if (ops[j].op == OP_MOV && ops[j].operand[1].type == OPT_REGMEM
5022     && strlen(ops[j].operand[1].name) == 3
5023     && ops[j].operand[0].lmod == OPLM_DWORD
5024     && ops[j].pp == NULL // no hint
5025     && level == 0)
5026   {
5027     // allow one simple dereference (com/directx)
5028     reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32),
5029             ops[j].operand[1].name);
5030     if (reg < 0)
5031       return NULL;
5032     ret = resolve_origin_reg(j, reg, j + magic, &k, NULL);
5033     if (ret != 1)
5034       return NULL;
5035     j = k;
5036   }
5037   if (ops[j].op != OP_MOV || ops[j].operand[0].lmod != OPLM_DWORD)
5038     return NULL;
5039
5040   if (ops[j].pp != NULL) {
5041     // type hint in asm
5042     pp = ops[j].pp;
5043   }
5044   else if (ops[j].operand[1].type == OPT_REGMEM) {
5045     pp = try_recover_pp(&ops[j], &ops[j].operand[1], 0, NULL);
5046     if (pp == NULL) {
5047       // maybe structure ptr in structure
5048       pp = resolve_deref(j, magic, &ops[j].operand[1], level + 1);
5049     }
5050   }
5051   else if (ops[j].operand[1].type == OPT_LABEL)
5052     pp = proto_parse(g_fhdr, ops[j].operand[1].name, g_quiet_pp);
5053   else if (ops[j].operand[1].type == OPT_REG) {
5054     // maybe arg reg?
5055     k = -1;
5056     ret = resolve_origin(j, &ops[j].operand[1], i + magic,
5057             &k, &from_caller);
5058     if (ret != 1 && from_caller && k == -1 && g_func_pp != NULL) {
5059       for (k = 0; k < g_func_pp->argc; k++) {
5060         if (g_func_pp->arg[k].reg == NULL)
5061           continue;
5062         if (IS(g_func_pp->arg[k].reg, ops[j].operand[1].name)) {
5063           pp = g_func_pp->arg[k].pp;
5064           break;
5065         }
5066       }
5067     }
5068   }
5069
5070   if (pp == NULL)
5071     return NULL;
5072   if (pp->is_func || pp->is_fptr || !pp->type.is_struct) {
5073     if (offset != 0)
5074       ferr(&ops[j], "expected struct, got '%s %s'\n",
5075            pp->type.name, pp->name);
5076     return NULL;
5077   }
5078
5079   return proto_lookup_struct(g_fhdr, pp->type.name, offset);
5080 }
5081
5082 static const struct parsed_proto *resolve_func_ptr(int i, int opcnt,
5083   int is_call_op, const struct parsed_opr *opr,
5084   int *pp_i, int *multi_src)
5085 {
5086   const struct parsed_proto *pp = NULL;
5087   int search_advice = 0;
5088
5089   if (multi_src != NULL)
5090     *multi_src = 0;
5091   if (pp_i != NULL)
5092     *pp_i = -1;
5093
5094   switch (opr->type) {
5095   case OPT_REGMEM:
5096     // try to resolve struct member calls
5097     pp = resolve_deref(i, i + opcnt * 19, opr, 0);
5098     if (pp != NULL)
5099       break;
5100     // fallthrough
5101   case OPT_LABEL:
5102   case OPT_OFFSET:
5103     pp = try_recover_pp(&ops[i], opr, is_call_op, &search_advice);
5104     if (!search_advice)
5105       break;
5106     // fallthrough
5107   default:
5108     scan_for_call_type(i, opr, i + opcnt * 9, is_call_op,
5109       &pp, pp_i, multi_src);
5110     break;
5111   }
5112
5113   return pp;
5114 }
5115
5116 static struct parsed_proto *process_call_early(int i, int opcnt,
5117   int *adj_i)
5118 {
5119   struct parsed_op *po = &ops[i];
5120   struct parsed_proto *pp;
5121   int multipath = 0;
5122   int adj = 0;
5123   int j, ret;
5124
5125   pp = po->pp;
5126   if (pp == NULL || pp->is_vararg || pp->argc_reg != 0)
5127     // leave for later
5128     return NULL;
5129
5130   // look for and make use of esp adjust
5131   *adj_i = ret = -1;
5132   if (!pp->is_stdcall && pp->argc_stack > 0)
5133     ret = scan_for_esp_adjust(i + 1, opcnt,
5134             pp->argc_stack * 4, &adj, &multipath, 0);
5135   if (ret >= 0) {
5136     if (pp->argc_stack > adj / 4)
5137       return NULL;
5138     if (multipath)
5139       return NULL;
5140     if (ops[ret].op == OP_POP) {
5141       for (j = 1; j < adj / 4; j++) {
5142         if (ops[ret + j].op != OP_POP
5143           || ops[ret + j].operand[0].reg != xCX)
5144         {
5145           return NULL;
5146         }
5147       }
5148     }
5149   }
5150
5151   *adj_i = ret;
5152   return pp;
5153 }
5154
5155 static struct parsed_proto *process_call(int i, int opcnt)
5156 {
5157   struct parsed_op *po = &ops[i];
5158   const struct parsed_proto *pp_c;
5159   struct parsed_proto *pp;
5160   const char *tmpname;
5161   int call_i = -1, ref_i = -1;
5162   int adj = 0, multipath = 0;
5163   int ret, arg;
5164
5165   tmpname = opr_name(po, 0);
5166   pp = po->pp;
5167   if (pp == NULL)
5168   {
5169     // indirect call
5170     pp_c = resolve_func_ptr(i, opcnt, 1, &ops[i].operand[0],
5171              &call_i, &multipath);
5172     if (pp_c != NULL) {
5173       if (!pp_c->is_func && !pp_c->is_fptr)
5174         ferr(po, "call to non-func: %s\n", pp_c->name);
5175       pp = proto_clone(pp_c);
5176       my_assert_not(pp, NULL);
5177       if (multipath)
5178         // not resolved just to single func
5179         pp->is_fptr = 1;
5180
5181       switch (po->operand[0].type) {
5182       case OPT_REG:
5183         // we resolved this call and no longer need the register
5184         po->regmask_src &= ~(1 << po->operand[0].reg);
5185
5186         if (!multipath && i != call_i && ops[call_i].op == OP_MOV
5187           && ops[call_i].operand[1].type == OPT_LABEL)
5188         {
5189           // no other source users?
5190           ret = resolve_last_ref(i, &po->operand[0], i + opcnt * 10,
5191                   &ref_i);
5192           if (ret == 1 && call_i == ref_i) {
5193             // and nothing uses it after us?
5194             ref_i = -1;
5195             find_next_read(i + 1, opcnt, &po->operand[0],
5196               i + opcnt * 11, &ref_i);
5197             if (ref_i == -1)
5198               // then also don't need the source mov
5199               ops[call_i].flags |= OPF_RMD | OPF_NOREGS;
5200           }
5201         }
5202         break;
5203       case OPT_REGMEM:
5204         pp->is_fptr = 1;
5205         break;
5206       default:
5207         break;
5208       }
5209     }
5210     if (pp == NULL) {
5211       pp = calloc(1, sizeof(*pp));
5212       my_assert_not(pp, NULL);
5213
5214       pp->is_fptr = 1;
5215       ret = scan_for_esp_adjust(i + 1, opcnt,
5216               -1, &adj, &multipath, 0);
5217       if (ret < 0 || adj < 0) {
5218         if (!g_allow_regfunc)
5219           ferr(po, "non-__cdecl indirect call unhandled yet\n");
5220         pp->is_unresolved = 1;
5221         adj = 0;
5222       }
5223       adj /= 4;
5224       if (adj > ARRAY_SIZE(pp->arg))
5225         ferr(po, "esp adjust too large: %d\n", adj);
5226       pp->ret_type.name = strdup("int");
5227       pp->argc = pp->argc_stack = adj;
5228       for (arg = 0; arg < pp->argc; arg++)
5229         pp->arg[arg].type.name = strdup("int");
5230     }
5231     po->pp = pp;
5232   }
5233
5234   // look for and make use of esp adjust
5235   multipath = 0;
5236   ret = -1;
5237   if (!pp->is_stdcall && pp->argc_stack > 0) {
5238     int adj_expect = pp->is_vararg ? -1 : pp->argc_stack * 4;
5239     ret = scan_for_esp_adjust(i + 1, opcnt,
5240             adj_expect, &adj, &multipath, 0);
5241   }
5242   if (ret >= 0) {
5243     if (pp->is_vararg) {
5244       if (adj / 4 < pp->argc_stack) {
5245         fnote(po, "(this call)\n");
5246         ferr(&ops[ret], "esp adjust is too small: %x < %x\n",
5247           adj, pp->argc_stack * 4);
5248       }
5249       // modify pp to make it have varargs as normal args
5250       arg = pp->argc;
5251       pp->argc += adj / 4 - pp->argc_stack;
5252       for (; arg < pp->argc; arg++) {
5253         pp->arg[arg].type.name = strdup("int");
5254         pp->argc_stack++;
5255       }
5256       if (pp->argc > ARRAY_SIZE(pp->arg))
5257         ferr(po, "too many args for '%s'\n", tmpname);
5258     }
5259     if (pp->argc_stack > adj / 4) {
5260       if (pp->is_noreturn)
5261         // assume no stack adjust was emited
5262         goto out;
5263       fnote(po, "(this call)\n");
5264       ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n",
5265         tmpname, pp->argc_stack * 4, adj);
5266     }
5267
5268     scan_for_esp_adjust(i + 1, opcnt,
5269       pp->argc_stack * 4, &adj, &multipath, 1);
5270   }
5271   else if (pp->is_vararg)
5272     ferr(po, "missing esp_adjust for vararg func '%s'\n",
5273       pp->name);
5274
5275 out:
5276   return pp;
5277 }
5278
5279 static void check_fptr_args(int i, int opcnt, struct parsed_proto *pp)
5280 {
5281   struct parsed_opr s_opr = OPR_INIT(OPT_REG, OPLM_DWORD, 0);
5282   const struct parsed_proto *pp_arg, *pp_cmp;
5283   const struct parsed_op *po_a;
5284   const char *s_reg;
5285   int pp_cmp_i;
5286   int arg, reg;
5287   int bad = 0;
5288   int j;
5289
5290   for (arg = 0; arg < pp->argc; arg++) {
5291     pp_cmp = NULL;
5292     pp_cmp_i = -1;
5293
5294     pp_arg = pp->arg[arg].pp;
5295     if (pp_arg == NULL || !pp_arg->is_func)
5296       continue;
5297
5298     s_reg = pp->arg[arg].reg;
5299     if (s_reg != NULL) {
5300       reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s_reg);
5301       ferr_assert(&ops[i], reg >= 0);
5302       s_opr.reg = reg;
5303       scan_for_call_type(i, &s_opr, i + arg + opcnt * 28, 0,
5304         &pp_cmp, &pp_cmp_i, NULL);
5305       if (pp_cmp != NULL && !pp_compatible_func(pp_arg, pp_cmp)) {
5306         bad = 1;
5307         if (pp_cmp_i >= 0)
5308           fnote(&ops[pp_cmp_i], "(referenced here)\n");
5309       }
5310     }
5311     else {
5312       for (j = 0; j < pp->arg[arg].push_ref_cnt; j++) {
5313         po_a = pp->arg[arg].push_refs[j];
5314         if (po_a == NULL || po_a->op != OP_PUSH)
5315           continue;
5316         pp_cmp = resolve_func_ptr(po_a - ops, opcnt, 0,
5317                    &po_a->operand[0], &pp_cmp_i, NULL);
5318         if (pp_cmp != NULL && !pp_compatible_func(pp_arg, pp_cmp)) {
5319           bad = 1;
5320           if (pp_cmp_i < 0)
5321             pp_cmp_i = po_a - ops;
5322           if (pp_cmp_i >= 0)
5323             fnote(&ops[pp_cmp_i], "(referenced here)\n");
5324         }
5325       }
5326     }
5327
5328     if (bad)
5329       ferr(&ops[i], "incompatible fptr arg %d\n", arg + 1);
5330   }
5331 }
5332
5333 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
5334 {
5335   int i;
5336
5337   for (i = 0; i < pp->argc; i++)
5338     if (pp->arg[i].reg == NULL)
5339       break;
5340
5341   if (pp->argc_stack)
5342     memmove(&pp->arg[i + 1], &pp->arg[i],
5343       sizeof(pp->arg[0]) * pp->argc_stack);
5344   memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
5345   pp->arg[i].reg = strdup(reg);
5346   pp->arg[i].type.name = strdup("int");
5347   pp->argc++;
5348   pp->argc_reg++;
5349 }
5350
5351 static void pp_insert_stack_args(struct parsed_proto *pp, int count)
5352 {
5353   int a;
5354
5355   pp->argc += count;
5356   pp->argc_stack += count;
5357
5358   for (a = 0; a < pp->argc; a++)
5359     if (pp->arg[a].type.name == NULL)
5360       pp->arg[a].type.name = strdup("int");
5361 }
5362
5363 static void pp_add_push_ref(struct parsed_proto *pp,
5364   int arg, struct parsed_op *po)
5365 {
5366   pp->arg[arg].push_refs = realloc(pp->arg[arg].push_refs,
5367                              (pp->arg[arg].push_ref_cnt + 1)
5368                               * sizeof(pp->arg[arg].push_refs[0]));
5369   ferr_assert(po, pp->arg[arg].push_refs != NULL);
5370   pp->arg[arg].push_refs[pp->arg[arg].push_ref_cnt++] = po;
5371 }
5372
5373 static void mark_float_arg(struct parsed_op *po,
5374   struct parsed_proto *pp, int arg, int *regmask_ffca)
5375 {
5376   ferr_assert(po, pp->arg[arg].push_ref_cnt == 0);
5377   pp_add_push_ref(pp, arg, po);
5378
5379   po->p_argnum = arg + 1;
5380   po->flags |= OPF_DONE | OPF_FARGNR | OPF_FARG;
5381   if (regmask_ffca != NULL)
5382     *regmask_ffca |= 1 << arg;
5383 }
5384
5385 static int check_for_stp(int i, int i_to)
5386 {
5387   struct parsed_op *po;
5388
5389   for (; i < i_to; i++) {
5390     po = &ops[i];
5391     if (po->op == OP_FST)
5392       return i;
5393     if (g_labels[i] != NULL || (po->flags & OPF_JMP))
5394       return -1;
5395     if (po->op == OP_CALL || po->op == OP_PUSH || po->op == OP_POP)
5396       return -1;
5397     if (po->op == OP_ADD && po->operand[0].reg == xSP)
5398       return -1;
5399   }
5400
5401   return -1;
5402 }
5403
5404 static int collect_call_args_no_push(int i, struct parsed_proto *pp,
5405   int *regmask_ffca)
5406 {
5407   struct parsed_op *po;
5408   int offset = 0;
5409   int base_arg;
5410   int j, arg;
5411   int ret;
5412
5413   for (base_arg = 0; base_arg < pp->argc; base_arg++)
5414     if (pp->arg[base_arg].reg == NULL)
5415       break;
5416
5417   for (j = i; j > 0; )
5418   {
5419     ferr_assert(&ops[j], g_labels[j] == NULL);
5420     j--;
5421
5422     po = &ops[j];
5423     ferr_assert(po, po->op != OP_PUSH);
5424     if (po->op == OP_FST)
5425     {
5426       if (po->operand[0].type != OPT_REGMEM)
5427         continue;
5428       ret = parse_stack_esp_offset(po, po->operand[0].name, &offset);
5429       if (ret != 0)
5430         continue;
5431       if (offset < 0 || offset >= pp->argc_stack * 4 || (offset & 3)) {
5432         //ferr(po, "offset %d, %d args\n", offset, pp->argc_stack);
5433         continue;
5434       }
5435
5436       arg = base_arg + offset / 4;
5437       mark_float_arg(po, pp, arg, regmask_ffca);
5438     }
5439     else if (po->op == OP_SUB && po->operand[0].reg == xSP
5440       && po->operand[1].type == OPT_CONST)
5441     {
5442       po->flags |= OPF_RMD | OPF_DONE | OPF_FARGNR | OPF_FARG;
5443       break;
5444     }
5445   }
5446
5447   for (arg = base_arg; arg < pp->argc; arg++) {
5448     ferr_assert(&ops[i], pp->arg[arg].reg == NULL);
5449     if (pp->arg[arg].push_ref_cnt != 1)
5450       ferr(&ops[i], "arg %d/%d not found or bad\n", arg, pp->argc);
5451     po = pp->arg[arg].push_refs[0];
5452     if (po->operand[0].lmod == OPLM_QWORD)
5453       arg++;
5454   }
5455
5456   return 0;
5457 }
5458
5459 static int collect_call_args_early(int i, int opcnt,
5460   struct parsed_proto *pp, int *regmask, int *regmask_ffca)
5461 {
5462   struct parsed_op *po;
5463   int arg, ret;
5464   int offset;
5465   int j, k;
5466
5467   for (arg = 0; arg < pp->argc; arg++)
5468     if (pp->arg[arg].reg == NULL)
5469       break;
5470
5471   // first see if it can be easily done
5472   for (j = i; j > 0 && arg < pp->argc; )
5473   {
5474     if (g_labels[j] != NULL)
5475       return -1;
5476     j--;
5477
5478     po = &ops[j];
5479     if (po->op == OP_CALL)
5480       return -1;
5481     else if (po->op == OP_ADD && po->operand[0].reg == xSP)
5482       return -1;
5483     else if (po->op == OP_POP)
5484       return -1;
5485     else if (po->flags & OPF_CJMP)
5486       return -1;
5487     else if (po->op == OP_PUSH) {
5488       if (po->flags & (OPF_FARG|OPF_FARGNR))
5489         return -1;
5490       if (!g_header_mode) {
5491         ret = scan_for_mod(po, j + 1, i, 1);
5492         if (ret >= 0)
5493           return -1;
5494       }
5495
5496       if (pp->arg[arg].type.is_va_list)
5497         return -1;
5498
5499       // next arg
5500       for (arg++; arg < pp->argc; arg++)
5501         if (pp->arg[arg].reg == NULL)
5502           break;
5503     }
5504     else if (po->op == OP_SUB && po->operand[0].reg == xSP
5505       && po->operand[1].type == OPT_CONST)
5506     {
5507       if (po->flags & (OPF_RMD|OPF_DONE))
5508         return -1;
5509       if (po->operand[1].val != pp->argc_stack * 4)
5510         ferr(po, "unexpected esp adjust: %d\n",
5511              po->operand[1].val * 4);
5512       ferr_assert(po, pp->argc - arg == pp->argc_stack);
5513       return collect_call_args_no_push(i, pp, regmask_ffca);
5514     }
5515   }
5516
5517   if (arg < pp->argc)
5518     return -1;
5519
5520   // now do it
5521   for (arg = 0; arg < pp->argc; arg++)
5522     if (pp->arg[arg].reg == NULL)
5523       break;
5524
5525   for (j = i; j > 0 && arg < pp->argc; )
5526   {
5527     j--;
5528
5529     if (ops[j].op == OP_PUSH)
5530     {
5531       int ref_handled = 0;
5532
5533       k = check_for_stp(j + 1, i);
5534       if (k != -1) {
5535         // push ecx; fstp dword ptr [esp]
5536         ret = parse_stack_esp_offset(&ops[k],
5537                 ops[k].operand[0].name, &offset);
5538         if (ret == 0 && offset == 0) {
5539           if (!pp->arg[arg].type.is_float)
5540             ferr(&ops[i], "arg %d should be float\n", arg + 1);
5541           mark_float_arg(&ops[k], pp, arg, regmask_ffca);
5542           ref_handled = 1;
5543         }
5544       }
5545
5546       if (!ref_handled) {
5547         ferr_assert(&ops[j], pp->arg[arg].push_ref_cnt == 0);
5548         pp_add_push_ref(pp, arg, &ops[j]);
5549       }
5550
5551       if (regmask != NULL && ops[j].operand[0].type == OPT_REG)
5552         *regmask |= 1 << ops[j].operand[0].reg;
5553
5554       ops[j].flags |= OPF_RMD | OPF_DONE | OPF_FARGNR | OPF_FARG;
5555       ops[j].flags &= ~OPF_RSAVE;
5556
5557       // next arg
5558       for (arg++; arg < pp->argc; arg++)
5559         if (pp->arg[arg].reg == NULL)
5560           break;
5561     }
5562   }
5563
5564   if (!g_header_mode)
5565     check_fptr_args(i, opcnt, pp);
5566
5567   return 0;
5568 }
5569
5570 // ensure all s_a* numbers match for a given func arg in all branches
5571 // returns 1 if any changes were made, 0 if not
5572 static int sync_argnum(struct parsed_proto *pp, int arg,
5573   int *argnum, int *arggrp)
5574 {
5575   struct parsed_op *po_tmp;
5576   int changed = 0;
5577   int i;
5578
5579   // see if other branches don't have higher argnum
5580   for (i = 0; i < pp->arg[arg].push_ref_cnt; i++) {
5581     po_tmp = pp->arg[arg].push_refs[i];
5582     if (*argnum < po_tmp->p_argnum)
5583       *argnum = po_tmp->p_argnum;
5584     if (*arggrp < po_tmp->p_arggrp)
5585       *arggrp = po_tmp->p_arggrp;
5586   }
5587
5588   // make all argnums consistent
5589   for (i = 0; i < pp->arg[arg].push_ref_cnt; i++) {
5590     po_tmp = pp->arg[arg].push_refs[i];
5591     if (po_tmp->p_argnum == 0)
5592       continue;
5593     if (po_tmp->p_argnum != *argnum || po_tmp->p_arggrp != *arggrp) {
5594       po_tmp->p_argnum = *argnum;
5595       po_tmp->p_arggrp = *arggrp;
5596       changed = 1;
5597     }
5598   }
5599
5600   return changed;
5601 }
5602
5603 static int collect_call_args_r(struct parsed_op *po, int i,
5604   struct parsed_proto *pp, int *regmask,
5605   int arg, int argnum, int magic,
5606   int skip, int need_op_saving, int may_reuse)
5607 {
5608   struct parsed_proto *pp_tmp;
5609   struct label_ref *lr;
5610   int need_to_save_current;
5611   int arg_grp_current = 0;
5612   int save_args_seen = 0;
5613   int dummy = 0;
5614   int ret = 0;
5615   int reg;
5616   char buf[32];
5617   int j, k;
5618
5619   if (i < 0) {
5620     ferr(po, "dead label encountered\n");
5621     return -1;
5622   }
5623
5624   for (; arg < pp->argc; arg++, argnum++)
5625     if (pp->arg[arg].reg == NULL)
5626       break;
5627   magic = (magic & 0xffffff) | (arg << 24);
5628
5629   for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
5630   {
5631     if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
5632       if (ops[j].cc_scratch != magic) {
5633         ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
5634            pp->name);
5635         return -1;
5636       }
5637       // ok: have already been here
5638       return 0;
5639     }
5640     ops[j].cc_scratch = magic;
5641
5642     if (g_labels[j] != NULL && g_label_refs[j].i != -1) {
5643       lr = &g_label_refs[j];
5644       if (lr->next != NULL)
5645         need_op_saving = 1;
5646       for (; lr->next; lr = lr->next) {
5647         check_i(&ops[j], lr->i);
5648         if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
5649           may_reuse = 1;
5650         ret = collect_call_args_r(po, lr->i, pp, regmask,
5651                 arg, argnum, magic, skip, need_op_saving, may_reuse);
5652         if (ret < 0)
5653           return ret;
5654       }
5655
5656       check_i(&ops[j], lr->i);
5657       if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
5658         may_reuse = 1;
5659       if (j > 0 && LAST_OP(j - 1)) {
5660         // follow last branch in reverse
5661         j = lr->i;
5662         continue;
5663       }
5664       need_op_saving = 1;
5665       ret = collect_call_args_r(po, lr->i, pp, regmask,
5666               arg, argnum, magic, skip, need_op_saving, may_reuse);
5667       if (ret < 0)
5668         return ret;
5669     }
5670     j--;
5671
5672     if (ops[j].op == OP_CALL)
5673     {
5674       if (pp->is_unresolved)
5675         break;
5676
5677       pp_tmp = ops[j].pp;
5678       if (pp_tmp == NULL)
5679         ferr(po, "arg collect %d/%d hit unparsed call '%s'\n",
5680           arg, pp->argc, ops[j].operand[0].name);
5681       if (may_reuse && pp_tmp->argc_stack > 0)
5682         ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
5683           arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
5684       if (!pp_tmp->is_unresolved)
5685         skip = pp_tmp->argc_stack;
5686     }
5687     // esp adjust of 0 means we collected it before
5688     else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP
5689       && (ops[j].operand[1].type != OPT_CONST
5690           || ops[j].operand[1].val != 0))
5691     {
5692       if (pp->is_unresolved)
5693         break;
5694
5695       fnote(po, "(this call)\n");
5696       ferr(&ops[j], "arg collect %d/%d hit esp adjust of %d\n",
5697         arg, pp->argc, ops[j].operand[1].val);
5698     }
5699     else if (ops[j].op == OP_POP && !(ops[j].flags & OPF_DONE))
5700     {
5701       if (pp->is_unresolved)
5702         break;
5703
5704       fnote(po, "(this call)\n");
5705       ferr(&ops[j], "arg collect %d/%d hit pop\n", arg, pp->argc);
5706     }
5707     else if (ops[j].flags & OPF_CJMP)
5708     {
5709       if (pp->is_unresolved)
5710         break;
5711
5712       may_reuse = 1;
5713     }
5714     else if (ops[j].op == OP_PUSH && skip > 0) {
5715       // XXX: might want to rm OPF_FARGNR and only use this
5716       skip--;
5717     }
5718     else if (ops[j].op == OP_PUSH
5719       && !(ops[j].flags & (OPF_FARGNR|OPF_DONE)))
5720     {
5721       if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
5722         break;
5723
5724       pp_add_push_ref(pp, arg, &ops[j]);
5725
5726       sync_argnum(pp, arg, &argnum, &dummy);
5727
5728       need_to_save_current = 0;
5729       reg = -1;
5730       if (ops[j].operand[0].type == OPT_REG)
5731         reg = ops[j].operand[0].reg;
5732
5733       if (!need_op_saving) {
5734         ret = scan_for_mod(&ops[j], j + 1, i, 1);
5735         need_to_save_current = (ret >= 0);
5736       }
5737       if (need_op_saving || need_to_save_current) {
5738         // mark this arg as one that needs operand saving
5739         pp->arg[arg].is_saved = 1;
5740
5741         if (save_args_seen & (1 << (argnum - 1))) {
5742           save_args_seen = 0;
5743           arg_grp_current++;
5744           if (arg_grp_current >= MAX_ARG_GRP)
5745             ferr(&ops[j], "out of arg groups (arg%d), f %s\n",
5746               argnum, pp->name);
5747         }
5748       }
5749       else if (ops[j].p_argnum == 0)
5750         ops[j].flags |= OPF_RMD;
5751
5752       // some PUSHes are reused by different calls on other branches,
5753       // but that can't happen if we didn't branch, so they
5754       // can be removed from future searches (handles nested calls)
5755       if (!may_reuse)
5756         ops[j].flags |= OPF_FARGNR;
5757
5758       ops[j].flags |= OPF_FARG;
5759       ops[j].flags &= ~OPF_RSAVE;
5760
5761       // check for __VALIST
5762       if (!pp->is_unresolved && g_func_pp != NULL
5763         && pp->arg[arg].type.is_va_list)
5764       {
5765         k = -1;
5766         ret = resolve_origin(j, &ops[j].operand[0],
5767                 magic + 1, &k, NULL);
5768         if (ret == 1 && k >= 0)
5769         {
5770           if (ops[k].op == OP_LEA) {
5771             if (!g_func_pp->is_vararg)
5772               ferr(&ops[k], "lea <arg> used, but %s is not vararg?\n",
5773                    g_func_pp->name);
5774
5775             snprintf(buf, sizeof(buf), "arg_%X",
5776               g_func_pp->argc_stack * 4);
5777             if (strstr(ops[k].operand[1].name, buf)
5778              || strstr(ops[k].operand[1].name, "arglist"))
5779             {
5780               ops[k].flags |= OPF_RMD | OPF_NOREGS | OPF_DONE;
5781               ops[j].flags |= OPF_RMD | OPF_NOREGS | OPF_VAPUSH;
5782               pp->arg[arg].is_saved = 0;
5783               reg = -1;
5784             }
5785             else
5786               ferr(&ops[k], "va_list arg detection failed\n");
5787           }
5788           // check for va_list from g_func_pp arg too
5789           else if (ops[k].op == OP_MOV
5790             && is_stack_access(&ops[k], &ops[k].operand[1]))
5791           {
5792             ret = stack_frame_access(&ops[k], &ops[k].operand[1],
5793               buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0);
5794             if (ret >= 0) {
5795               ops[k].flags |= OPF_RMD | OPF_DONE;
5796               ops[j].flags |= OPF_RMD;
5797               ops[j].p_argpass = ret + 1;
5798               pp->arg[arg].is_saved = 0;
5799               reg = -1;
5800             }
5801           }
5802         }
5803       }
5804
5805       if (pp->arg[arg].is_saved) {
5806         ops[j].flags &= ~OPF_RMD;
5807         ops[j].p_argnum = argnum;
5808         ops[j].p_arggrp = arg_grp_current;
5809       }
5810
5811       // tracking reg usage
5812       if (reg >= 0)
5813         *regmask |= 1 << reg;
5814
5815       arg++;
5816       argnum++;
5817       if (!pp->is_unresolved) {
5818         // next arg
5819         for (; arg < pp->argc; arg++, argnum++)
5820           if (pp->arg[arg].reg == NULL)
5821             break;
5822       }
5823       magic = (magic & 0xffffff) | (arg << 24);
5824     }
5825
5826     if (ops[j].p_arggrp > arg_grp_current) {
5827       save_args_seen = 0;
5828       arg_grp_current = ops[j].p_arggrp;
5829     }
5830     if (ops[j].p_argnum > 0)
5831       save_args_seen |= 1 << (ops[j].p_argnum - 1);
5832   }
5833
5834   if (arg < pp->argc) {
5835     ferr(po, "arg collect failed for '%s': %d/%d\n",
5836       pp->name, arg, pp->argc);
5837     return -1;
5838   }
5839
5840   return arg;
5841 }
5842
5843 static int collect_call_args(struct parsed_op *po, int i, int opcnt,
5844   struct parsed_proto *pp, int *regmask, int magic)
5845 {
5846   int ret;
5847
5848   ret = collect_call_args_r(po, i, pp, regmask, 0, 1, magic,
5849           0, 0, 0);
5850   if (ret < 0)
5851     return ret;
5852
5853   if (pp->is_unresolved)
5854     pp_insert_stack_args(pp, ret);
5855
5856   // note: p_argnum, p_arggrp will be propagated in a later pass,
5857   // look for sync_argnum() (p_arggrp is for cases when mixed pushes
5858   // for multiple funcs are going on)
5859
5860   if (!g_header_mode)
5861     check_fptr_args(i, opcnt, pp);
5862
5863   return ret;
5864 }
5865
5866 static void reg_use_pass(int i, int opcnt, unsigned char *cbits,
5867   int regmask_now, int *regmask,
5868   int regmask_save_now, int *regmask_save,
5869   int *regmask_init, int regmask_arg)
5870 {
5871   struct parsed_op *po;
5872   int already_saved;
5873   int regmask_new;
5874   int regmask_op;
5875   int flags_set;
5876   int ret, reg;
5877   int j;
5878
5879   for (; i < opcnt; i++)
5880   {
5881     po = &ops[i];
5882     if (cbits[i >> 3] & (1 << (i & 7)))
5883       return;
5884     cbits[i >> 3] |= (1 << (i & 7));
5885
5886     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
5887       if (po->flags & (OPF_RMD|OPF_DONE))
5888         continue;
5889       if (po->btj != NULL) {
5890         for (j = 0; j < po->btj->count; j++) {
5891           check_i(po, po->btj->d[j].bt_i);
5892           reg_use_pass(po->btj->d[j].bt_i, opcnt, cbits,
5893             regmask_now, regmask, regmask_save_now, regmask_save,
5894             regmask_init, regmask_arg);
5895         }
5896         return;
5897       }
5898
5899       check_i(po, po->bt_i);
5900       if (po->flags & OPF_CJMP)
5901         reg_use_pass(po->bt_i, opcnt, cbits,
5902           regmask_now, regmask, regmask_save_now, regmask_save,
5903           regmask_init, regmask_arg);
5904       else
5905         i = po->bt_i - 1;
5906       continue;
5907     }
5908
5909     if (po->op == OP_PUSH && !(po->flags & (OPF_FARG|OPF_DONE))
5910       && !g_func_pp->is_userstack
5911       && po->operand[0].type == OPT_REG)
5912     {
5913       int save_level = 0;
5914
5915       reg = po->operand[0].reg;
5916       ferr_assert(po, reg >= 0);
5917
5918       already_saved = 0;
5919       flags_set = OPF_RSAVE | OPF_RMD | OPF_DONE;
5920       if (regmask_now & (1 << reg)) {
5921         already_saved = regmask_save_now & (1 << reg);
5922         flags_set = OPF_RSAVE | OPF_DONE;
5923         save_level++;
5924       }
5925
5926       ret = scan_for_pop(i + 1, opcnt, i + opcnt * 3,
5927               reg, 0, 0, save_level, 0);
5928       if (ret == 1) {
5929         scan_for_pop(i + 1, opcnt, i + opcnt * 4,
5930           reg, 0, 0, save_level, flags_set);
5931       }
5932       else {
5933         ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].reg, 0);
5934         if (ret == 1) {
5935           scan_for_pop_ret(i + 1, opcnt, po->operand[0].reg,
5936             flags_set);
5937         }
5938       }
5939       if (ret == 1) {
5940         ferr_assert(po, !already_saved);
5941         po->flags |= flags_set;
5942
5943         if (regmask_now & (1 << reg)) {
5944           regmask_save_now |= (1 << reg);
5945           *regmask_save |= regmask_save_now;
5946         }
5947         continue;
5948       }
5949     }
5950     else if (po->op == OP_POP && (po->flags & OPF_RSAVE)) {
5951       reg = po->operand[0].reg;
5952       ferr_assert(po, reg >= 0);
5953
5954       if (regmask_save_now & (1 << reg))
5955         regmask_save_now &= ~(1 << reg);
5956       else
5957         regmask_now &= ~(1 << reg);
5958       continue;
5959     }
5960     else if (po->op == OP_CALL) {
5961       if ((po->regmask_dst & (1 << xAX))
5962         && !(po->regmask_dst & (1 << xDX)))
5963       {
5964         if (po->flags & OPF_TAIL)
5965           // don't need eax, will do "return f();" or "f(); return;"
5966           po->regmask_dst &= ~(1 << xAX);
5967         else {
5968           find_next_read_reg(i + 1, opcnt, xAX, OPLM_DWORD,
5969             i + opcnt * 17, &j);
5970           if (j == -1)
5971             // not used
5972             po->regmask_dst &= ~(1 << xAX);
5973         }
5974       }
5975
5976       // not "full stack" mode and have something in stack
5977       if (!(regmask_now & mxST7_2) && (regmask_now & mxST1_0))
5978         ferr(po, "float stack is not empty on func call\n");
5979     }
5980
5981     if (po->flags & OPF_NOREGS)
5982       continue;
5983
5984     // if incomplete register is used, clear it on init to avoid
5985     // later use of uninitialized upper part in some situations
5986     if ((po->flags & OPF_DATA) && po->operand[0].type == OPT_REG
5987         && po->operand[0].lmod != OPLM_DWORD)
5988     {
5989       reg = po->operand[0].reg;
5990       ferr_assert(po, reg >= 0);
5991
5992       if (!(regmask_now & (1 << reg)))
5993         *regmask_init |= 1 << reg;
5994     }
5995
5996     regmask_op = po->regmask_src | po->regmask_dst;
5997
5998     regmask_new = po->regmask_src & ~regmask_now & ~regmask_arg;
5999     regmask_new &= ~(1 << xSP);
6000     if (g_bp_frame && !(po->flags & OPF_EBP_S))
6001       regmask_new &= ~(1 << xBP);
6002
6003     if (regmask_new != 0)
6004       fnote(po, "uninitialized reg mask: %x\n", regmask_new);
6005
6006     if (regmask_op & (1 << xBP)) {
6007       if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
6008         if (po->regmask_dst & (1 << xBP))
6009           // compiler decided to drop bp frame and use ebp as scratch
6010           scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
6011         else
6012           regmask_op &= ~(1 << xBP);
6013       }
6014     }
6015
6016     if (po->flags & OPF_FPUSH) {
6017       if (regmask_now & mxST1)
6018         regmask_now |= mxSTa; // switch to "full stack" mode
6019       if (regmask_now & mxSTa)
6020         po->flags |= OPF_FSHIFT;
6021       if (!(regmask_now & mxST7_2)) {
6022         regmask_now =
6023           (regmask_now & ~mxST1_0) | ((regmask_now & mxST0) << 1);
6024       }
6025     }
6026
6027     regmask_now |= regmask_op;
6028     *regmask |= regmask_now;
6029
6030     // released regs
6031     if (po->flags & OPF_FPOPP) {
6032       if ((regmask_now & mxSTa) == 0)
6033         ferr(po, "float pop on empty stack?\n");
6034       if (regmask_now & mxST7_2)
6035         po->flags |= OPF_FSHIFT;
6036       if (!(regmask_now & mxST7_2))
6037         regmask_now &= ~mxST1_0;
6038     }
6039     else if (po->flags & OPF_FPOP) {
6040       if ((regmask_now & mxSTa) == 0)
6041         ferr(po, "float pop on empty stack?\n");
6042       if (regmask_now & (mxST7_2 | mxST1))
6043         po->flags |= OPF_FSHIFT;
6044       if (!(regmask_now & mxST7_2)) {
6045         regmask_now =
6046           (regmask_now & ~mxST1_0) | ((regmask_now & mxST1) >> 1);
6047       }
6048     }
6049
6050     if (po->flags & OPF_TAIL) {
6051       if (!(regmask_now & mxST7_2)) {
6052         if (get_pp_arg_regmask_dst(g_func_pp) & mxST0) {
6053           if (!(regmask_now & mxST0))
6054             ferr(po, "no st0 on float return, mask: %x\n",
6055                  regmask_now);
6056         }
6057         else if (regmask_now & mxST1_0)
6058           ferr(po, "float regs on tail: %x\n", regmask_now);
6059       }
6060
6061       // there is support for "conditional tailcall", sort of
6062       if (!(po->flags & OPF_CC))
6063         return;
6064     }
6065   }
6066 }
6067
6068 static void output_std_flag_z(FILE *fout, struct parsed_op *po,
6069   int *pfomask, const char *dst_opr_text)
6070 {
6071   if (*pfomask & (1 << PFO_Z)) {
6072     fprintf(fout, "\n  cond_z = (%s%s == 0);",
6073       lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
6074     *pfomask &= ~(1 << PFO_Z);
6075   }
6076 }
6077
6078 static void output_std_flag_s(FILE *fout, struct parsed_op *po,
6079   int *pfomask, const char *dst_opr_text)
6080 {
6081   if (*pfomask & (1 << PFO_S)) {
6082     fprintf(fout, "\n  cond_s = (%s%s < 0);",
6083       lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
6084     *pfomask &= ~(1 << PFO_S);
6085   }
6086 }
6087
6088 static void output_std_flags(FILE *fout, struct parsed_op *po,
6089   int *pfomask, const char *dst_opr_text)
6090 {
6091   output_std_flag_z(fout, po, pfomask, dst_opr_text);
6092   output_std_flag_s(fout, po, pfomask, dst_opr_text);
6093 }
6094
6095 enum {
6096   OPP_FORCE_NORETURN = (1 << 0),
6097   OPP_SIMPLE_ARGS    = (1 << 1),
6098   OPP_ALIGN          = (1 << 2),
6099 };
6100
6101 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
6102   int flags)
6103 {
6104   const char *cconv = "";
6105
6106   if (pp->is_fastcall)
6107     cconv = "__fastcall ";
6108   else if (pp->is_stdcall && pp->argc_reg == 0)
6109     cconv = "__stdcall ";
6110
6111   fprintf(fout, (flags & OPP_ALIGN) ? "%-16s" : "%s", cconv);
6112
6113   if (pp->is_noreturn || (flags & OPP_FORCE_NORETURN))
6114     fprintf(fout, "noreturn ");
6115 }
6116
6117 static void output_pp(FILE *fout, const struct parsed_proto *pp,
6118   int flags)
6119 {
6120   int i;
6121
6122   fprintf(fout, (flags & OPP_ALIGN) ? "%-5s" : "%s ",
6123     pp->ret_type.name);
6124   if (pp->is_fptr)
6125     fprintf(fout, "(");
6126   output_pp_attrs(fout, pp, flags);
6127   if (pp->is_fptr)
6128     fprintf(fout, "*");
6129   fprintf(fout, "%s", pp->name);
6130   if (pp->is_fptr)
6131     fprintf(fout, ")");
6132
6133   fprintf(fout, "(");
6134   for (i = 0; i < pp->argc; i++) {
6135     if (i > 0)
6136       fprintf(fout, ", ");
6137     if (pp->arg[i].pp != NULL && pp->arg[i].pp->is_func
6138       && !(flags & OPP_SIMPLE_ARGS))
6139     {
6140       // func pointer
6141       output_pp(fout, pp->arg[i].pp, 0);
6142     }
6143     else if (pp->arg[i].type.is_retreg) {
6144       fprintf(fout, "u32 *r_%s", pp->arg[i].reg);
6145     }
6146     else {
6147       fprintf(fout, "%s", pp->arg[i].type.name);
6148       if (!pp->is_fptr)
6149         fprintf(fout, " a%d", i + 1);
6150     }
6151
6152     if (pp->arg[i].type.is_64bit)
6153       i++;
6154   }
6155   if (pp->is_vararg) {
6156     if (i > 0)
6157       fprintf(fout, ", ");
6158     fprintf(fout, "...");
6159   }
6160   fprintf(fout, ")");
6161 }
6162
6163 static char *saved_arg_name(char *buf, size_t buf_size, int grp, int num)
6164 {
6165   char buf1[16];
6166
6167   buf1[0] = 0;
6168   if (grp > 0)
6169     snprintf(buf1, sizeof(buf1), "%d", grp);
6170   snprintf(buf, buf_size, "s%s_a%d", buf1, num);
6171
6172   return buf;
6173 }
6174
6175 static void gen_x_cleanup(int opcnt);
6176
6177 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
6178 {
6179   struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
6180   struct parsed_opr *last_arith_dst = NULL;
6181   char buf1[256], buf2[256], buf3[256], cast[64];
6182   struct parsed_proto *pp, *pp_tmp;
6183   struct parsed_data *pd;
6184   int save_arg_vars[MAX_ARG_GRP] = { 0, };
6185   unsigned char cbits[MAX_OPS / 8];
6186   const char *float_type;
6187   const char *float_st0;
6188   const char *float_st1;
6189   int need_float_stack = 0;
6190   int need_float_sw = 0; // status word
6191   int need_tmp_var = 0;
6192   int need_tmp64 = 0;
6193   int cond_vars = 0;
6194   int had_decl = 0;
6195   int label_pending = 0;
6196   int need_double = 0;
6197   int stack_align = 0;
6198   int stack_fsz_adj = 0;
6199   int lock_handled = 0;
6200   int regmask_save = 0; // used regs saved/restored in this func
6201   int regmask_arg;      // regs from this function args (fastcall, etc)
6202   int regmask_ret;      // regs needed on ret
6203   int regmask_now;      // temp
6204   int regmask_init = 0; // regs that need zero initialization
6205   int regmask_pp = 0;   // regs used in complex push-pop graph
6206   int regmask_ffca = 0; // float function call args
6207   int regmask = 0;      // used regs
6208   int pfomask = 0;
6209   int found = 0;
6210   int dead_dst;
6211   int no_output;
6212   int i, j, l;
6213   int arg;
6214   int reg;
6215   int ret;
6216
6217   g_bp_frame = g_sp_frame = g_stack_fsz = 0;
6218   g_stack_frame_used = 0;
6219   g_seh_size = 0;
6220   if (g_sct_func_attr & SCTFA_CLEAR_REGS)
6221     regmask_init = g_regmask_init;
6222
6223   g_func_pp = proto_parse(fhdr, funcn, 0);
6224   if (g_func_pp == NULL)
6225     ferr(ops, "proto_parse failed for '%s'\n", funcn);
6226
6227   regmask_arg = get_pp_arg_regmask_src(g_func_pp);
6228   regmask_ret = get_pp_arg_regmask_dst(g_func_pp);
6229
6230   // pass1:
6231   // - resolve all branches
6232   // - parse calls with labels
6233   resolve_branches_parse_calls(opcnt);
6234
6235   // pass2:
6236   // - handle ebp/esp frame, remove ops related to it
6237   scan_prologue_epilogue(opcnt, &stack_align);
6238
6239   // handle a case where sf size is unalignment, but is
6240   // placed in a way that elements are still aligned
6241   if (g_stack_fsz & 4) {
6242     for (i = 0; i < g_eqcnt; i++) {
6243       if (g_eqs[i].lmod != OPLM_QWORD)
6244         continue;
6245       if (!(g_eqs[i].offset & 4)) {
6246         g_stack_fsz += 4;
6247         stack_fsz_adj = 4;
6248       }
6249       break;
6250     }
6251   }
6252
6253   // pass3:
6254   // - remove dead labels
6255   // - set regs needed at ret
6256   for (i = 0; i < opcnt; i++)
6257   {
6258     if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
6259       free(g_labels[i]);
6260       g_labels[i] = NULL;
6261     }
6262
6263     if (ops[i].op == OP_RET)
6264       ops[i].regmask_src |= regmask_ret;
6265   }
6266
6267   // pass4:
6268   // - process trivial calls
6269   for (i = 0; i < opcnt; i++)
6270   {
6271     po = &ops[i];
6272     if (po->flags & (OPF_RMD|OPF_DONE))
6273       continue;
6274
6275     if (po->op == OP_CALL)
6276     {
6277       pp = process_call_early(i, opcnt, &j);
6278       if (pp != NULL) {
6279         if (!(po->flags & OPF_ATAIL)) {
6280           // since we know the args, try to collect them
6281           ret = collect_call_args_early(i, opcnt, pp,
6282                   &regmask, &regmask_ffca);
6283           if (ret != 0)
6284             pp = NULL;
6285         }
6286       }
6287
6288       if (pp != NULL) {
6289         if (j >= 0) {
6290           // commit esp adjust
6291           if (ops[j].op != OP_POP)
6292             patch_esp_adjust(&ops[j], pp->argc_stack * 4);
6293           else {
6294             for (l = 0; l < pp->argc_stack; l++)
6295               ops[j + l].flags |= OPF_DONE | OPF_RMD | OPF_NOREGS;
6296           }
6297         }
6298
6299         if (strstr(pp->ret_type.name, "int64"))
6300           need_tmp64 = 1;
6301
6302         po->flags |= OPF_DONE;
6303       }
6304     }
6305   }
6306
6307   // pass5:
6308   // - process calls, stage 2
6309   // - handle some push/pop pairs
6310   // - scan for STD/CLD, propagate DF
6311   // - try to resolve needed x87 status word bits
6312   for (i = 0; i < opcnt; i++)
6313   {
6314     int mask, z_check;
6315
6316     po = &ops[i];
6317     if (po->flags & OPF_RMD)
6318       continue;
6319
6320     if (po->op == OP_CALL)
6321     {
6322       if (!(po->flags & OPF_DONE)) {
6323         pp = process_call(i, opcnt);
6324
6325         if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
6326           // since we know the args, collect them
6327           collect_call_args(po, i, opcnt, pp, &regmask, i + opcnt * 2);
6328         }
6329         // for unresolved, collect after other passes
6330       }
6331
6332       pp = po->pp;
6333       ferr_assert(po, pp != NULL);
6334
6335       po->regmask_src |= get_pp_arg_regmask_src(pp);
6336       po->regmask_dst |= get_pp_arg_regmask_dst(pp);
6337
6338       if (po->regmask_dst & mxST0)
6339         po->flags |= OPF_FPUSH;
6340
6341       if (strstr(pp->ret_type.name, "int64"))
6342         need_tmp64 = 1;
6343
6344       continue;
6345     }
6346
6347     if (po->flags & OPF_DONE)
6348       continue;
6349
6350     switch (po->op) {
6351     case OP_PUSH:
6352       if (!(po->flags & OPF_FARG) && !(po->flags & OPF_RSAVE)
6353         && po->operand[0].type == OPT_CONST)
6354       {
6355         scan_for_pop_const(i, opcnt, i + opcnt * 12);
6356       }
6357       break;
6358
6359     case OP_POP:
6360       scan_pushes_for_pop(i, opcnt, &regmask_pp);
6361       break;
6362
6363     case OP_STD:
6364       po->flags |= OPF_DF | OPF_RMD | OPF_DONE;
6365       scan_propagate_df(i + 1, opcnt);
6366       break;
6367
6368     case OP_FNSTSW:
6369       need_float_sw = 1;
6370       if (po->operand[0].type != OPT_REG || po->operand[0].reg != xAX)
6371         ferr(po, "TODO: fnstsw to mem\n");
6372       ret = resolve_used_bits(i + 1, opcnt, xAX, &mask, &z_check);
6373       if (ret != 0)
6374         ferr(po, "fnstsw resolve failed\n");
6375       ret = adjust_prev_op(i, OP_FCOM, i + opcnt * 21,
6376               (void *)(long)(mask | (z_check << 16)));
6377       if (ret != 1)
6378         ferr(po, "failed to find fcom: %d\n", ret);
6379       break;
6380
6381     default:
6382       break;
6383     }
6384   }
6385
6386   // pass6:
6387   // - find POPs for PUSHes, rm both
6388   // - scan for all used registers
6389   memset(cbits, 0, sizeof(cbits));
6390   reg_use_pass(0, opcnt, cbits, regmask_init, &regmask,
6391     0, &regmask_save, &regmask_init, regmask_arg);
6392
6393   need_float_stack = !!(regmask & mxST7_2);
6394
6395   // pass7:
6396   // - find flag set ops for their users
6397   // - do unresolved calls
6398   // - declare indirect functions
6399   // - other op specific processing
6400   for (i = 0; i < opcnt; i++)
6401   {
6402     po = &ops[i];
6403     if (po->flags & (OPF_RMD|OPF_DONE))
6404       continue;
6405
6406     if (po->flags & OPF_CC)
6407     {
6408       int setters[16], cnt = 0, branched = 0;
6409
6410       ret = scan_for_flag_set(i, opcnt, i + opcnt * 6,
6411               &branched, setters, &cnt);
6412       if (ret < 0 || cnt <= 0)
6413         ferr(po, "unable to trace flag setter(s)\n");
6414       if (cnt > ARRAY_SIZE(setters))
6415         ferr(po, "too many flag setters\n");
6416
6417       for (j = 0; j < cnt; j++)
6418       {
6419         tmp_op = &ops[setters[j]]; // flag setter
6420         pfomask = 0;
6421
6422         // to get nicer code, we try to delay test and cmp;
6423         // if we can't because of operand modification, or if we
6424         // have arith op, or branch, make it calculate flags explicitly
6425         if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
6426         {
6427           if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
6428             pfomask = 1 << po->pfo;
6429         }
6430         else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
6431           pfomask = 1 << po->pfo;
6432         }
6433         else {
6434           // see if we'll be able to handle based on op result
6435           if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
6436                && po->pfo != PFO_Z && po->pfo != PFO_S
6437                && po->pfo != PFO_P)
6438               || branched
6439               || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
6440           {
6441             pfomask = 1 << po->pfo;
6442           }
6443
6444           if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
6445             propagate_lmod(tmp_op, &tmp_op->operand[0],
6446               &tmp_op->operand[1]);
6447             if (tmp_op->operand[0].lmod == OPLM_DWORD)
6448               need_tmp64 = 1;
6449           }
6450         }
6451         if (pfomask) {
6452           tmp_op->pfomask |= pfomask;
6453           cond_vars |= pfomask;
6454         }
6455         // note: may overwrite, currently not a problem
6456         po->datap = tmp_op;
6457       }
6458
6459       if (po->op == OP_RCL || po->op == OP_RCR
6460        || po->op == OP_ADC || po->op == OP_SBB)
6461         cond_vars |= 1 << PFO_C;
6462     }
6463
6464     switch (po->op) {
6465     case OP_CMPS:
6466     case OP_SCAS:
6467       cond_vars |= 1 << PFO_Z;
6468       break;
6469
6470     case OP_MUL:
6471       if (po->operand[0].lmod == OPLM_DWORD)
6472         need_tmp64 = 1;
6473       break;
6474
6475     case OP_IMUL:
6476       if (po->operand_cnt == 1 && po->operand[0].lmod == OPLM_DWORD)
6477         need_tmp64 = 1;
6478       break;
6479
6480     case OP_CALL:
6481       // note: resolved non-reg calls are OPF_DONE already
6482       pp = po->pp;
6483       ferr_assert(po, pp != NULL);
6484
6485       if (pp->is_unresolved) {
6486         int regmask_stack = 0;
6487
6488         if ((po->flags & OPF_TAIL) && g_func_pp->is_stdcall)
6489           pp_insert_stack_args(pp, g_func_pp->argc_stack);
6490         else {
6491           collect_call_args(po, i, opcnt, pp, &regmask, i + opcnt * 2);
6492
6493           // this is pretty rough guess:
6494           // see ecx and edx were pushed (and not their saved versions)
6495           for (arg = 0; arg < pp->argc; arg++) {
6496             if (pp->arg[arg].reg != NULL && !pp->arg[arg].is_saved)
6497               continue;
6498
6499             if (pp->arg[arg].push_ref_cnt == 0)
6500               ferr(po, "parsed_op missing for arg%d\n", arg);
6501             tmp_op = pp->arg[arg].push_refs[0];
6502             if (tmp_op->operand[0].type == OPT_REG)
6503               regmask_stack |= 1 << tmp_op->operand[0].reg;
6504           }
6505         }
6506
6507         // quick dumb check for potential reg-args
6508         for (j = i - 1; j >= 0 && ops[j].op == OP_MOV; j--)
6509           if (ops[j].operand[0].type == OPT_REG)
6510             regmask_stack &= ~(1 << ops[j].operand[0].reg);
6511
6512         if ((regmask_stack & (mxCX|mxDX)) != (mxCX|mxDX)
6513             && ((regmask | regmask_arg) & (mxCX|mxDX)))
6514         {
6515           if (pp->argc_stack != 0
6516               || ((regmask | regmask_arg) & (mxCX|mxDX)))
6517           {
6518             pp_insert_reg_arg(pp, "ecx");
6519             pp->is_fastcall = 1;
6520             regmask_init |= 1 << xCX;
6521             regmask |= 1 << xCX;
6522           }
6523           if (pp->argc_stack != 0
6524               || ((regmask | regmask_arg) & mxDX))
6525           {
6526             pp_insert_reg_arg(pp, "edx");
6527             regmask_init |= 1 << xDX;
6528             regmask |= 1 << xDX;
6529           }
6530         }
6531
6532         // note: __cdecl doesn't fall into is_unresolved category
6533         if (pp->argc_stack > 0)
6534           pp->is_stdcall = 1;
6535       }
6536       if (!(po->flags & OPF_TAIL)
6537           && !(g_sct_func_attr & SCTFA_NOWARN) && !g_nowarn_reguse)
6538       {
6539         // treat al write as overwrite to avoid many false positives
6540         if (IS(pp->ret_type.name, "void") || pp->ret_type.is_float) {
6541           find_next_read_reg(i + 1, opcnt, xAX, OPLM_BYTE,
6542             i + opcnt * 25, &j);
6543           if (j != -1) {
6544             fnote(po, "eax used after void/float ret call\n");
6545             fnote(&ops[j], "(used here)\n");
6546           }
6547         }
6548         if (!strstr(pp->ret_type.name, "int64")) {
6549           find_next_read_reg(i + 1, opcnt, xDX, OPLM_BYTE,
6550             i + opcnt * 26, &j);
6551           // indirect calls are often guessed, don't warn
6552           if (j != -1 && !IS_OP_INDIRECT_CALL(&ops[j])) {
6553             fnote(po, "edx used after 32bit ret call\n");
6554             fnote(&ops[j], "(used here)\n");
6555           }
6556         }
6557         j = 1;
6558         // msvc often relies on callee not modifying 'this'
6559         for (arg = 0; arg < pp->argc; arg++) {
6560           if (pp->arg[arg].reg && IS(pp->arg[arg].reg, "ecx")) {
6561             j = 0;
6562             break;
6563           }
6564         }
6565         if (j != 0) {
6566           find_next_read_reg(i + 1, opcnt, xCX, OPLM_BYTE,
6567             i + opcnt * 27, &j);
6568           if (j != -1 && !IS_OP_INDIRECT_CALL(&ops[j])) {
6569             fnote(po, "ecx used after call\n");
6570             fnote(&ops[j], "(used here)\n");
6571           }
6572         }
6573       }
6574       break;
6575
6576     case OP_MOV:
6577       if (po->operand[0].pp != NULL && po->operand[1].pp != NULL)
6578       {
6579         // <var> = offset <something>
6580         if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
6581           && !IS_START(po->operand[1].name, "off_"))
6582         {
6583           if (!po->operand[0].pp->is_fptr)
6584             ferr(po, "%s not declared as fptr when it should be\n",
6585               po->operand[0].name);
6586           if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
6587             pp_print(buf1, sizeof(buf1), po->operand[0].pp);
6588             pp_print(buf2, sizeof(buf2), po->operand[1].pp);
6589             fnote(po, "var:  %s\n", buf1);
6590             fnote(po, "func: %s\n", buf2);
6591             ferr(po, "^ mismatch\n");
6592           }
6593         }
6594       }
6595       break;
6596
6597     case OP_DIV:
6598     case OP_IDIV:
6599       if (po->operand[0].lmod == OPLM_DWORD) {
6600         // 32bit division is common, look for it
6601         if (po->op == OP_DIV)
6602           ret = scan_for_reg_clear(i, xDX);
6603         else
6604           ret = scan_for_cdq_edx(i);
6605         if (ret >= 0)
6606           po->flags |= OPF_32BIT;
6607         else
6608           need_tmp64 = 1;
6609       }
6610       else
6611         need_tmp_var = 1;
6612       break;
6613
6614     case OP_CLD:
6615       po->flags |= OPF_RMD | OPF_DONE;
6616       break;
6617
6618     case OP_RCL:
6619     case OP_RCR:
6620     case OP_XCHG:
6621       need_tmp_var = 1;
6622       break;
6623
6624     case OP_FLD:
6625       if (po->operand[0].lmod == OPLM_QWORD)
6626         need_double = 1;
6627       break;
6628
6629     case OP_RDTSC:
6630     case OPP_ALLSHL:
6631     case OPP_ALLSHR:
6632       need_tmp64 = 1;
6633       break;
6634
6635     case OPP_FTOL:
6636       find_next_read_reg(i + 1, opcnt, xDX, OPLM_DWORD,
6637         i + opcnt * 18, &j);
6638       if (j == -1)
6639         po->flags |= OPF_32BIT;
6640       break;
6641
6642     default:
6643       break;
6644     }
6645   }
6646
6647   // pass8: sync all push arg numbers
6648   // some calls share args and not all of them
6649   // (there's only partial intersection)
6650   do {
6651     int changed, argnum, arggrp;
6652
6653     found = 0;
6654     for (i = 0; i < opcnt; i++)
6655     {
6656       po = &ops[i];
6657       if ((po->flags & (OPF_RMD|OPF_DONE)) || po->op != OP_CALL)
6658         continue;
6659
6660       pp = po->pp;
6661       arggrp = 0;
6662       do {
6663         changed = 0;
6664         for (arg = argnum = 0; arg < pp->argc; arg++) {
6665           if (pp->arg[arg].reg != NULL)
6666             continue;
6667           if (pp->arg[arg].is_saved)
6668             changed |= sync_argnum(pp, arg, &argnum, &arggrp);
6669           argnum++;
6670         }
6671         found |= changed;
6672       }
6673       while (changed);
6674
6675       if (argnum > 32)
6676         ferr(po, "too many args or looping in graph\n");
6677     }
6678   }
6679   while (found);
6680
6681   // pass9: final adjustments
6682   for (i = 0; i < opcnt; i++)
6683   {
6684     po = &ops[i];
6685     if (po->flags & (OPF_RMD|OPF_DONE))
6686       continue;
6687
6688     if (po->op != OP_FST && po->p_argnum > 0)
6689       save_arg_vars[po->p_arggrp] |= 1 << (po->p_argnum - 1);
6690
6691     // correct for "full stack" mode late enable
6692     if ((po->flags & (OPF_PPUSH|OPF_FPOP|OPF_FPOPP))
6693         && need_float_stack)
6694       po->flags |= OPF_FSHIFT;
6695   }
6696
6697   float_type = need_double ? "double" : "float";
6698   float_st0 = need_float_stack ? "f_st[f_stp & 7]" : "f_st0";
6699   float_st1 = need_float_stack ? "f_st[(f_stp + 1) & 7]" : "f_st1";
6700
6701   // output starts here
6702
6703   if (g_seh_found)
6704     fprintf(fout, "// had SEH\n");
6705
6706   // define userstack size
6707   if (g_func_pp->is_userstack) {
6708     fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
6709     fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
6710     fprintf(fout, "#endif\n");
6711   }
6712
6713   // the function itself
6714   ferr_assert(ops, !g_func_pp->is_fptr);
6715   output_pp(fout, g_func_pp,
6716     (g_ida_func_attr & IDAFA_NORETURN) ? OPP_FORCE_NORETURN : 0);
6717   fprintf(fout, "\n{\n");
6718
6719   // declare indirect functions
6720   for (i = 0; i < opcnt; i++) {
6721     po = &ops[i];
6722     if (po->flags & OPF_RMD)
6723       continue;
6724
6725     if (po->op == OP_CALL) {
6726       pp = po->pp;
6727       if (pp == NULL)
6728         ferr(po, "NULL pp\n");
6729
6730       if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
6731         if (pp->name[0] != 0) {
6732           if (IS_START(pp->name, "guess"))
6733             pp->is_guessed = 1;
6734
6735           memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
6736           memcpy(pp->name, "i_", 2);
6737
6738           // might be declared already
6739           found = 0;
6740           for (j = 0; j < i; j++) {
6741             if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
6742               if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
6743                 found = 1;
6744                 break;
6745               }
6746             }
6747           }
6748           if (found)
6749             continue;
6750         }
6751         else
6752           snprintf(pp->name, sizeof(pp->name), "icall%d", i);
6753
6754         fprintf(fout, "  ");
6755         output_pp(fout, pp, OPP_SIMPLE_ARGS);
6756         fprintf(fout, ";\n");
6757       }
6758     }
6759   }
6760
6761   // output LUTs/jumptables
6762   for (i = 0; i < g_func_pd_cnt; i++) {
6763     pd = &g_func_pd[i];
6764     fprintf(fout, "  static const ");
6765     if (pd->type == OPT_OFFSET) {
6766       fprintf(fout, "void *jt_%s[] =\n    { ", pd->label);
6767
6768       for (j = 0; j < pd->count; j++) {
6769         if (j > 0)
6770           fprintf(fout, ", ");
6771         fprintf(fout, "&&%s", pd->d[j].u.label);
6772       }
6773     }
6774     else {
6775       fprintf(fout, "%s %s[] =\n    { ",
6776         lmod_type_u(ops, pd->lmod), pd->label);
6777
6778       for (j = 0; j < pd->count; j++) {
6779         if (j > 0)
6780           fprintf(fout, ", ");
6781         fprintf(fout, "%u", pd->d[j].u.val);
6782       }
6783     }
6784     fprintf(fout, " };\n");
6785     had_decl = 1;
6786   }
6787
6788   // declare stack frame, va_arg
6789   if (g_stack_fsz) {
6790     if (stack_fsz_adj)
6791       fprintf(fout, "  // stack_fsz_adj %d\n", stack_fsz_adj);
6792
6793     fprintf(fout, "  union { u32 d[%d];", (g_stack_fsz + 3) / 4);
6794     if (g_func_lmods & (1 << OPLM_WORD))
6795       fprintf(fout, " u16 w[%d];", (g_stack_fsz + 1) / 2);
6796     if (g_func_lmods & (1 << OPLM_BYTE))
6797       fprintf(fout, " u8 b[%d];", g_stack_fsz);
6798     if (g_func_lmods & (1 << OPLM_QWORD))
6799       fprintf(fout, " double q[%d];", (g_stack_fsz + 7) / 8);
6800
6801     if (stack_align > 8)
6802       ferr(ops, "unhandled stack align of %d\n", stack_align);
6803     else if (stack_align == 8)
6804       fprintf(fout, " u64 align;");
6805     fprintf(fout, " } sf;\n");
6806     had_decl = 1;
6807   }
6808
6809   if ((g_sct_func_attr & SCTFA_ARGFRAME) && g_func_pp->argc_stack) {
6810     fprintf(fout, "  struct { u32 ");
6811     for (i = j = 0; i < g_func_pp->argc; i++) {
6812       if (g_func_pp->arg[i].reg != NULL)
6813         continue;
6814       if (j++ != 0)
6815         fprintf(fout, ", ");
6816       fprintf(fout, "a%d", i + 1);
6817     }
6818     fprintf(fout, "; } af = {\n    ");
6819     for (i = j = 0; i < g_func_pp->argc; i++) {
6820       if (g_func_pp->arg[i].reg != NULL)
6821         continue;
6822       if (j++ != 0)
6823         fprintf(fout, ", ");
6824         if (g_func_pp->arg[i].type.is_ptr)
6825           fprintf(fout, "(u32)");
6826       fprintf(fout, "a%d", i + 1);
6827     }
6828     fprintf(fout, "\n  };\n");
6829   }
6830
6831   if (g_func_pp->is_userstack) {
6832     fprintf(fout, "  u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
6833     fprintf(fout, "  u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
6834     had_decl = 1;
6835   }
6836
6837   if (g_func_pp->is_vararg) {
6838     fprintf(fout, "  va_list ap;\n");
6839     had_decl = 1;
6840   }
6841
6842   // declare arg-registers
6843   for (i = 0; i < g_func_pp->argc; i++) {
6844     if (g_func_pp->arg[i].reg != NULL) {
6845       reg = char_array_i(regs_r32,
6846               ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
6847       if (regmask & (1 << reg)) {
6848         if (g_func_pp->arg[i].type.is_retreg)
6849           fprintf(fout, "  u32 %s = *r_%s;\n",
6850             g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
6851         else
6852           fprintf(fout, "  u32 %s = (u32)a%d;\n",
6853             g_func_pp->arg[i].reg, i + 1);
6854       }
6855       else {
6856         if (g_func_pp->arg[i].type.is_retreg)
6857           ferr(ops, "retreg '%s' is unused?\n",
6858             g_func_pp->arg[i].reg);
6859         fprintf(fout, "  // %s = a%d; // unused\n",
6860           g_func_pp->arg[i].reg, i + 1);
6861       }
6862       had_decl = 1;
6863     }
6864   }
6865
6866   // declare normal registers
6867   regmask_now = regmask & ~regmask_arg & ~g_regmask_rm;
6868   regmask_now &= ~(1 << xSP);
6869   if (regmask_now & 0x00ff) {
6870     for (reg = 0; reg < 8; reg++) {
6871       if (regmask_now & (1 << reg)) {
6872         fprintf(fout, "  u32 %s", regs_r32[reg]);
6873         if (regmask_init & (1 << reg))
6874           fprintf(fout, " = 0");
6875         fprintf(fout, ";\n");
6876         had_decl = 1;
6877       }
6878     }
6879   }
6880   // ... mmx
6881   if (regmask_now & 0xff00) {
6882     for (reg = 8; reg < 16; reg++) {
6883       if (regmask_now & (1 << reg)) {
6884         fprintf(fout, "  mmxr %s", regs_r32[reg]);
6885         if (regmask_init & (1 << reg))
6886           fprintf(fout, " = { 0, }");
6887         fprintf(fout, ";\n");
6888         had_decl = 1;
6889       }
6890     }
6891   }
6892   // ... x87
6893   if (need_float_stack) {
6894     fprintf(fout, "  %s f_st[8];\n", float_type);
6895     fprintf(fout, "  int f_stp = 0;\n");
6896     had_decl = 1;
6897   }
6898   else {
6899     if (regmask_now & 0xff0000) {
6900       for (reg = 16; reg < 24; reg++) {
6901         if (regmask_now & (1 << reg)) {
6902           fprintf(fout, "  %s f_st%d", float_type, reg - 16);
6903           if (regmask_init & (1 << reg))
6904             fprintf(fout, " = 0");
6905           fprintf(fout, ";\n");
6906           had_decl = 1;
6907         }
6908       }
6909     }
6910   }
6911
6912   if (need_float_sw) {
6913     fprintf(fout, "  u16 f_sw;\n");
6914     had_decl = 1;
6915   }
6916
6917   if (regmask_save) {
6918     for (reg = 0; reg < 8; reg++) {
6919       if (regmask_save & (1 << reg)) {
6920         fprintf(fout, "  u32 s_%s;\n", regs_r32[reg]);
6921         had_decl = 1;
6922       }
6923     }
6924   }
6925
6926   for (i = 0; i < ARRAY_SIZE(save_arg_vars); i++) {
6927     if (save_arg_vars[i] == 0)
6928       continue;
6929     for (reg = 0; reg < 32; reg++) {
6930       if (save_arg_vars[i] & (1 << reg)) {
6931         fprintf(fout, "  u32 %s;\n",
6932           saved_arg_name(buf1, sizeof(buf1), i, reg + 1));
6933         had_decl = 1;
6934       }
6935     }
6936   }
6937
6938   if (regmask_ffca) {
6939     for (reg = 0; reg < 32; reg++) {
6940       if (regmask_ffca & (1 << reg)) {
6941         fprintf(fout, "  %s fs_%d;\n", float_type, reg + 1);
6942         had_decl = 1;
6943       }
6944     }
6945   }
6946
6947   // declare push-pop temporaries
6948   if (regmask_pp) {
6949     for (reg = 0; reg < 8; reg++) {
6950       if (regmask_pp & (1 << reg)) {
6951         fprintf(fout, "  u32 pp_%s;\n", regs_r32[reg]);
6952         had_decl = 1;
6953       }
6954     }
6955   }
6956
6957   if (cond_vars) {
6958     for (i = 0; i < 8; i++) {
6959       if (cond_vars & (1 << i)) {
6960         fprintf(fout, "  u32 cond_%s;\n", parsed_flag_op_names[i]);
6961         had_decl = 1;
6962       }
6963     }
6964   }
6965
6966   if (need_tmp_var) {
6967     fprintf(fout, "  u32 tmp;\n");
6968     had_decl = 1;
6969   }
6970
6971   if (need_tmp64) {
6972     fprintf(fout, "  u64 tmp64;\n");
6973     had_decl = 1;
6974   }
6975
6976   if (had_decl)
6977     fprintf(fout, "\n");
6978
6979   // do stack clear, if needed
6980   if (g_sct_func_attr & SCTFA_CLEAR_SF) {
6981     fprintf(fout, "  ");
6982     if (g_stack_clear_len != 0) {
6983       if (g_stack_clear_len <= 4) {
6984         for (i = 0; i < g_stack_clear_len; i++)
6985           fprintf(fout, "sf.d[%d] = ", g_stack_clear_start + i);
6986         fprintf(fout, "0;\n");
6987       }
6988       else {
6989         fprintf(fout, "memset(&sf[%d], 0, %d);\n",
6990           g_stack_clear_start, g_stack_clear_len * 4);
6991       }
6992     }
6993     else
6994       fprintf(fout, "memset(&sf, 0, sizeof(sf));\n");
6995   }
6996
6997   if (g_func_pp->is_vararg) {
6998     if (g_func_pp->argc_stack == 0)
6999       ferr(ops, "vararg func without stack args?\n");
7000     fprintf(fout, "  va_start(ap, a%d);\n", g_func_pp->argc);
7001   }
7002
7003   // output ops
7004   for (i = 0; i < opcnt; i++)
7005   {
7006     if (g_labels[i] != NULL) {
7007       fprintf(fout, "\n%s:\n", g_labels[i]);
7008       label_pending = 1;
7009
7010       delayed_flag_op = NULL;
7011       last_arith_dst = NULL;
7012     }
7013
7014     po = &ops[i];
7015     if (po->flags & OPF_RMD)
7016       continue;
7017
7018     lock_handled = 0;
7019     no_output = 0;
7020
7021     #define assert_operand_cnt(n_) \
7022       if (po->operand_cnt != n_) \
7023         ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
7024
7025     // conditional/flag using op?
7026     if (po->flags & OPF_CC)
7027     {
7028       int is_delayed = 0;
7029
7030       tmp_op = po->datap;
7031
7032       // we go through all this trouble to avoid using parsed_flag_op,
7033       // which makes generated code much nicer
7034       if (delayed_flag_op != NULL)
7035       {
7036         out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
7037           po->pfo, po->pfo_inv);
7038         is_delayed = 1;
7039       }
7040       else if (last_arith_dst != NULL
7041         && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
7042            || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
7043            ))
7044       {
7045         struct parsed_op *po_arith = (void *)((char *)last_arith_dst
7046           - offsetof(struct parsed_op, operand[0]));
7047         ferr_assert(po, &ops[po_arith - ops] == po_arith);
7048         out_src_opr_u32(buf3, sizeof(buf3), po_arith, last_arith_dst);
7049         out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
7050           last_arith_dst->lmod, buf3);
7051         is_delayed = 1;
7052       }
7053       else if (tmp_op != NULL) {
7054         // use preprocessed flag calc results
7055         if (!(tmp_op->pfomask & (1 << po->pfo)))
7056           ferr(po, "not prepared for pfo %d\n", po->pfo);
7057
7058         // note: pfo_inv was not yet applied
7059         snprintf(buf1, sizeof(buf1), "(%scond_%s)",
7060           po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
7061       }
7062       else {
7063         ferr(po, "all methods of finding comparison failed\n");
7064       }
7065  
7066       if (po->flags & OPF_JMP) {
7067         fprintf(fout, "  if %s", buf1);
7068       }
7069       else if (po->op == OP_RCL || po->op == OP_RCR
7070                || po->op == OP_ADC || po->op == OP_SBB)
7071       {
7072         if (is_delayed)
7073           fprintf(fout, "  cond_%s = %s;\n",
7074             parsed_flag_op_names[po->pfo], buf1);
7075       }
7076       else if (po->flags & OPF_DATA) { // SETcc
7077         out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
7078         fprintf(fout, "  %s = %s;", buf2, buf1);
7079       }
7080       else {
7081         ferr(po, "unhandled conditional op\n");
7082       }
7083     }
7084
7085     pfomask = po->pfomask;
7086
7087     switch (po->op)
7088     {
7089       case OP_MOV:
7090         assert_operand_cnt(2);
7091         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7092         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7093         default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
7094         fprintf(fout, "  %s = %s;", buf1,
7095             out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
7096               buf3, 0));
7097         break;
7098
7099       case OP_LEA:
7100         assert_operand_cnt(2);
7101         po->operand[1].lmod = OPLM_DWORD; // always
7102         fprintf(fout, "  %s = %s;",
7103             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7104             out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
7105               NULL, 1));
7106         break;
7107
7108       case OP_MOVZX:
7109         assert_operand_cnt(2);
7110         fprintf(fout, "  %s = %s;",
7111             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7112             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
7113         break;
7114
7115       case OP_MOVSX:
7116         assert_operand_cnt(2);
7117         switch (po->operand[1].lmod) {
7118         case OPLM_BYTE:
7119           strcpy(buf3, "(s8)");
7120           break;
7121         case OPLM_WORD:
7122           strcpy(buf3, "(s16)");
7123           break;
7124         default:
7125           ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
7126         }
7127         fprintf(fout, "  %s = %s;",
7128             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7129             out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
7130               buf3, 0));
7131         break;
7132
7133       case OP_XCHG:
7134         assert_operand_cnt(2);
7135         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7136         fprintf(fout, "  tmp = %s;",
7137           out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
7138         fprintf(fout, " %s = %s;",
7139           out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7140           out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
7141             default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
7142         fprintf(fout, " %s = %stmp;",
7143           out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
7144           default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
7145         snprintf(g_comment, sizeof(g_comment), "xchg");
7146         break;
7147
7148       case OP_NOT:
7149         assert_operand_cnt(1);
7150         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7151         fprintf(fout, "  %s = ~%s;", buf1, buf1);
7152         break;
7153
7154       case OP_XLAT:
7155         assert_operand_cnt(2);
7156         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7157         out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
7158         fprintf(fout, "  %s = *(u8 *)(%s + %s);", buf1, buf2, buf1);
7159         strcpy(g_comment, "xlat");
7160         break;
7161
7162       case OP_CDQ:
7163         assert_operand_cnt(2);
7164         fprintf(fout, "  %s = (s32)%s >> 31;",
7165             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7166             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
7167         strcpy(g_comment, "cdq");
7168         break;
7169
7170       case OP_BSWAP:
7171         assert_operand_cnt(1);
7172         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7173         fprintf(fout, "  %s = __builtin_bswap32(%s);", buf1, buf1);
7174         break;
7175
7176       case OP_LODS:
7177         if (po->flags & OPF_REP) {
7178           assert_operand_cnt(3);
7179           // hmh..
7180           ferr(po, "TODO\n");
7181         }
7182         else {
7183           assert_operand_cnt(2);
7184           fprintf(fout, "  %s = %sesi; esi %c= %d;",
7185             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
7186             lmod_cast_u_ptr(po, po->operand[1].lmod),
7187             (po->flags & OPF_DF) ? '-' : '+',
7188             lmod_bytes(po, po->operand[1].lmod));
7189           strcpy(g_comment, "lods");
7190         }
7191         break;
7192
7193       case OP_STOS:
7194         if (po->flags & OPF_REP) {
7195           assert_operand_cnt(3);
7196           fprintf(fout, "  for (; ecx != 0; ecx--, edi %c= %d)\n",
7197             (po->flags & OPF_DF) ? '-' : '+',
7198             lmod_bytes(po, po->operand[1].lmod));
7199           fprintf(fout, "    %sedi = eax;\n",
7200             lmod_cast_u_ptr(po, po->operand[1].lmod));
7201           fprintf(fout, "  barrier();");
7202           strcpy(g_comment, "^ rep stos");
7203         }
7204         else {
7205           assert_operand_cnt(2);
7206           fprintf(fout, "  %sedi = eax; edi %c= %d;",
7207             lmod_cast_u_ptr(po, po->operand[1].lmod),
7208             (po->flags & OPF_DF) ? '-' : '+',
7209             lmod_bytes(po, po->operand[1].lmod));
7210           strcpy(g_comment, "stos");
7211         }
7212         break;
7213
7214       case OP_MOVS:
7215         j = lmod_bytes(po, po->operand[0].lmod);
7216         strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
7217         l = (po->flags & OPF_DF) ? '-' : '+';
7218         if (po->flags & OPF_REP) {
7219           assert_operand_cnt(3);
7220           fprintf(fout,
7221             "  for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
7222             l, j, l, j);
7223           fprintf(fout,
7224             "    %sedi = %sesi;\n", buf1, buf1);
7225           // this can overwrite many variables
7226           fprintf(fout, "  barrier();");
7227           strcpy(g_comment, "^ rep movs");
7228         }
7229         else {
7230           assert_operand_cnt(2);
7231           fprintf(fout, "  %sedi = %sesi; edi %c= %d; esi %c= %d;",
7232             buf1, buf1, l, j, l, j);
7233           strcpy(g_comment, "movs");
7234         }
7235         break;
7236
7237       case OP_CMPS:
7238         // repe ~ repeat while ZF=1
7239         j = lmod_bytes(po, po->operand[0].lmod);
7240         strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
7241         l = (po->flags & OPF_DF) ? '-' : '+';
7242         if (po->flags & OPF_REP) {
7243           assert_operand_cnt(3);
7244           fprintf(fout,
7245             "  while (ecx != 0) {\n");
7246           if (pfomask & (1 << PFO_C)) {
7247             // ugh..
7248             fprintf(fout,
7249             "    cond_c = %sesi < %sedi;\n", buf1, buf1);
7250             pfomask &= ~(1 << PFO_C);
7251           }
7252           fprintf(fout,
7253             "    cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
7254               buf1, buf1, l, j, l, j);
7255           fprintf(fout,
7256             "    ecx--;\n"
7257             "    if (cond_z %s 0) break;\n",
7258               (po->flags & OPF_REPZ) ? "==" : "!=");
7259           fprintf(fout,
7260             "  }");
7261           snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
7262             (po->flags & OPF_REPZ) ? "e" : "ne");
7263         }
7264         else {
7265           assert_operand_cnt(2);
7266           fprintf(fout,
7267             "  cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
7268             buf1, buf1, l, j, l, j);
7269           strcpy(g_comment, "cmps");
7270         }
7271         pfomask &= ~(1 << PFO_Z);
7272         last_arith_dst = NULL;
7273         delayed_flag_op = NULL;
7274         break;
7275
7276       case OP_SCAS:
7277         // only does ZF (for now)
7278         // repe ~ repeat while ZF=1
7279         j = lmod_bytes(po, po->operand[1].lmod);
7280         l = (po->flags & OPF_DF) ? '-' : '+';
7281         if (po->flags & OPF_REP) {
7282           assert_operand_cnt(3);
7283           fprintf(fout,
7284             "  while (ecx != 0) {\n");
7285           fprintf(fout,
7286             "    cond_z = (%seax == %sedi); edi %c= %d;\n",
7287               lmod_cast_u(po, po->operand[1].lmod),
7288               lmod_cast_u_ptr(po, po->operand[1].lmod), l, j);
7289           fprintf(fout,
7290             "    ecx--;\n"
7291             "    if (cond_z %s 0) break;\n",
7292               (po->flags & OPF_REPZ) ? "==" : "!=");
7293           fprintf(fout,
7294             "  }");
7295           snprintf(g_comment, sizeof(g_comment), "rep%s scas",
7296             (po->flags & OPF_REPZ) ? "e" : "ne");
7297         }
7298         else {
7299           assert_operand_cnt(2);
7300           fprintf(fout, "  cond_z = (%seax == %sedi); edi %c= %d;",
7301               lmod_cast_u(po, po->operand[1].lmod),
7302               lmod_cast_u_ptr(po, po->operand[1].lmod), l, j);
7303           strcpy(g_comment, "scas");
7304         }
7305         pfomask &= ~(1 << PFO_Z);
7306         last_arith_dst = NULL;
7307         delayed_flag_op = NULL;
7308         break;
7309
7310       case OP_RDTSC:
7311         fprintf(fout, "  tmp64 = ext_rdtsc();\n");
7312         fprintf(fout, "  edx = tmp64 >> 32;\n");
7313         fprintf(fout, "  eax = tmp64;");
7314         break;
7315
7316       case OP_CPUID:
7317         fprintf(fout, "  ext_cpuid(&eax, &ebx, &ecx, &edx);");
7318         break;
7319
7320       // arithmetic w/flags
7321       case OP_AND:
7322         if (po->operand[1].type == OPT_CONST && !po->operand[1].val)
7323           goto dualop_arith_const;
7324         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7325         goto dualop_arith;
7326
7327       case OP_OR:
7328         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7329         if (po->operand[1].type == OPT_CONST) {
7330           j = lmod_bytes(po, po->operand[0].lmod);
7331           if (((1ull << j * 8) - 1) == po->operand[1].val)
7332             goto dualop_arith_const;
7333         }
7334         goto dualop_arith;
7335
7336       dualop_arith:
7337         assert_operand_cnt(2);
7338         fprintf(fout, "  %s %s= %s;",
7339             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7340             op_to_c(po),
7341             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
7342         output_std_flags(fout, po, &pfomask, buf1);
7343         last_arith_dst = &po->operand[0];
7344         delayed_flag_op = NULL;
7345         break;
7346
7347       dualop_arith_const:
7348         // and 0, or ~0 used instead mov
7349         assert_operand_cnt(2);
7350         fprintf(fout, "  %s = %s;",
7351           out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7352           out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
7353            default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
7354         output_std_flags(fout, po, &pfomask, buf1);
7355         last_arith_dst = &po->operand[0];
7356         delayed_flag_op = NULL;
7357         break;
7358
7359       case OP_SHL:
7360       case OP_SHR:
7361         assert_operand_cnt(2);
7362         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7363         if (pfomask & (1 << PFO_C)) {
7364           if (po->operand[1].type == OPT_CONST) {
7365             l = lmod_bytes(po, po->operand[0].lmod) * 8;
7366             j = po->operand[1].val;
7367             j %= l;
7368             if (j != 0) {
7369               if (po->op == OP_SHL)
7370                 j = l - j;
7371               else
7372                 j -= 1;
7373               fprintf(fout, "  cond_c = (%s >> %d) & 1;\n",
7374                 buf1, j);
7375             }
7376             else
7377               ferr(po, "zero shift?\n");
7378           }
7379           else
7380             ferr(po, "TODO\n");
7381           pfomask &= ~(1 << PFO_C);
7382         }
7383         fprintf(fout, "  %s %s= %s", buf1, op_to_c(po),
7384             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
7385         if (po->operand[1].type != OPT_CONST)
7386           fprintf(fout, " & 0x1f");
7387         fprintf(fout, ";");
7388         output_std_flags(fout, po, &pfomask, buf1);
7389         last_arith_dst = &po->operand[0];
7390         delayed_flag_op = NULL;
7391         break;
7392
7393       case OP_SAR:
7394         assert_operand_cnt(2);
7395         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7396         fprintf(fout, "  %s = %s%s >> %s;", buf1,
7397           lmod_cast_s(po, po->operand[0].lmod), buf1,
7398           out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
7399         output_std_flags(fout, po, &pfomask, buf1);
7400         last_arith_dst = &po->operand[0];
7401         delayed_flag_op = NULL;
7402         break;
7403
7404       case OP_SHLD:
7405       case OP_SHRD:
7406         assert_operand_cnt(3);
7407         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7408         l = lmod_bytes(po, po->operand[0].lmod) * 8;
7409         out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]);
7410         if (po->operand[2].type != OPT_CONST) {
7411           // no handling for "undefined" case, hopefully not needed
7412           snprintf(buf2, sizeof(buf2), "(%s & 0x1f)", buf3);
7413           strcpy(buf3, buf2);
7414         }
7415         out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
7416         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7417         if (po->op == OP_SHLD) {
7418           fprintf(fout, "  %s <<= %s; %s |= %s >> (%d - %s);",
7419             buf1, buf3, buf1, buf2, l, buf3);
7420           strcpy(g_comment, "shld");
7421         }
7422         else {
7423           fprintf(fout, "  %s >>= %s; %s |= %s << (%d - %s);",
7424             buf1, buf3, buf1, buf2, l, buf3);
7425           strcpy(g_comment, "shrd");
7426         }
7427         output_std_flags(fout, po, &pfomask, buf1);
7428         last_arith_dst = &po->operand[0];
7429         delayed_flag_op = NULL;
7430         break;
7431
7432       case OP_ROL:
7433       case OP_ROR:
7434         assert_operand_cnt(2);
7435         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7436         if (po->operand[1].type == OPT_CONST) {
7437           j = po->operand[1].val;
7438           j %= lmod_bytes(po, po->operand[0].lmod) * 8;
7439           fprintf(fout, po->op == OP_ROL ?
7440             "  %s = (%s << %d) | (%s >> %d);" :
7441             "  %s = (%s >> %d) | (%s << %d);",
7442             buf1, buf1, j, buf1,
7443             lmod_bytes(po, po->operand[0].lmod) * 8 - j);
7444         }
7445         else
7446           ferr(po, "TODO\n");
7447         output_std_flags(fout, po, &pfomask, buf1);
7448         last_arith_dst = &po->operand[0];
7449         delayed_flag_op = NULL;
7450         break;
7451
7452       case OP_RCL:
7453       case OP_RCR:
7454         assert_operand_cnt(2);
7455         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7456         l = lmod_bytes(po, po->operand[0].lmod) * 8;
7457         if (po->operand[1].type == OPT_CONST) {
7458           j = po->operand[1].val % l;
7459           if (j == 0)
7460             ferr(po, "zero rotate\n");
7461           fprintf(fout, "  tmp = (%s >> %d) & 1;\n",
7462             buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
7463           if (po->op == OP_RCL) {
7464             fprintf(fout,
7465               "  %s = (%s << %d) | (cond_c << %d)",
7466               buf1, buf1, j, j - 1);
7467             if (j != 1)
7468               fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
7469           }
7470           else {
7471             fprintf(fout,
7472               "  %s = (%s >> %d) | (cond_c << %d)",
7473               buf1, buf1, j, l - j);
7474             if (j != 1)
7475               fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
7476           }
7477           fprintf(fout, ";\n");
7478           fprintf(fout, "  cond_c = tmp;");
7479         }
7480         else
7481           ferr(po, "TODO\n");
7482         strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
7483         output_std_flags(fout, po, &pfomask, buf1);
7484         last_arith_dst = &po->operand[0];
7485         delayed_flag_op = NULL;
7486         break;
7487
7488       case OP_XOR:
7489         assert_operand_cnt(2);
7490         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7491         if (IS(opr_name(po, 0), opr_name(po, 1))) {
7492           // special case for XOR
7493           int z = PFOB_O | PFOB_C | PFOB_S | (1 << PFO_L);
7494           for (j = 0; j <= PFO_LE; j++) {
7495             if (pfomask & (1 << j)) {
7496               fprintf(fout, "  cond_%s = %d;\n",
7497                 parsed_flag_op_names[j], (1 << j) & z ? 0 : 1);
7498               pfomask &= ~(1 << j);
7499             }
7500           }
7501           fprintf(fout, "  %s = 0;",
7502             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
7503           last_arith_dst = &po->operand[0];
7504           delayed_flag_op = NULL;
7505           break;
7506         }
7507         goto dualop_arith;
7508
7509       case OP_ADD:
7510         assert_operand_cnt(2);
7511         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7512         if (pfomask & (1 << PFO_C)) {
7513           out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
7514           out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
7515           if (po->operand[0].lmod == OPLM_DWORD) {
7516             fprintf(fout, "  tmp64 = (u64)%s + %s;\n", buf1, buf2);
7517             fprintf(fout, "  cond_c = tmp64 >> 32;\n");
7518             fprintf(fout, "  %s = (u32)tmp64;",
7519               out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
7520             strcat(g_comment, " add64");
7521           }
7522           else {
7523             fprintf(fout, "  cond_c = ((u32)%s + %s) >> %d;\n",
7524               buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
7525             fprintf(fout, "  %s += %s;",
7526               out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
7527               buf2);
7528           }
7529           pfomask &= ~(1 << PFO_C);
7530           output_std_flags(fout, po, &pfomask, buf1);
7531           last_arith_dst = &po->operand[0];
7532           delayed_flag_op = NULL;
7533           break;
7534         }
7535         if (pfomask & (1 << PFO_LE)) {
7536           out_cmp_for_cc(buf1, sizeof(buf1), po, PFO_LE, 0, 1);
7537           fprintf(fout, "  cond_%s = %s;\n",
7538             parsed_flag_op_names[PFO_LE], buf1);
7539           pfomask &= ~(1 << PFO_LE);
7540         }
7541         goto dualop_arith;
7542
7543       case OP_SUB:
7544         assert_operand_cnt(2);
7545         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7546         if (pfomask & ~((1 << PFO_Z) | (1 << PFO_S))) {
7547           for (j = 0; j <= PFO_LE; j++) {
7548             if (!(pfomask & (1 << j)))
7549               continue;
7550             if (j == PFO_Z || j == PFO_S)
7551               continue;
7552
7553             out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0, 0);
7554             fprintf(fout, "  cond_%s = %s;\n",
7555               parsed_flag_op_names[j], buf1);
7556             pfomask &= ~(1 << j);
7557           }
7558         }
7559         goto dualop_arith;
7560
7561       case OP_ADC:
7562       case OP_SBB:
7563         assert_operand_cnt(2);
7564         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7565         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7566         if (po->op == OP_SBB
7567           && IS(po->operand[0].name, po->operand[1].name))
7568         {
7569           // avoid use of unitialized var
7570           fprintf(fout, "  %s = -cond_c;", buf1);
7571           // carry remains what it was
7572           pfomask &= ~(1 << PFO_C);
7573         }
7574         else {
7575           fprintf(fout, "  %s %s= %s + cond_c;", buf1, op_to_c(po),
7576             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
7577         }
7578         output_std_flags(fout, po, &pfomask, buf1);
7579         last_arith_dst = &po->operand[0];
7580         delayed_flag_op = NULL;
7581         break;
7582
7583       case OP_BSF:
7584       case OP_BSR:
7585         // on SKL, if src is 0, dst is left unchanged
7586         assert_operand_cnt(2);
7587         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7588         out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
7589         output_std_flag_z(fout, po, &pfomask, buf2);
7590         if (po->op == OP_BSF)
7591           snprintf(buf3, sizeof(buf3), "__builtin_ffs(%s) - 1", buf2);
7592         else
7593           snprintf(buf3, sizeof(buf3), "31 - __builtin_clz(%s)", buf2);
7594         fprintf(fout, "  if (%s) %s = %s;", buf2, buf1, buf3);
7595         last_arith_dst = &po->operand[0];
7596         delayed_flag_op = NULL;
7597         strcat(g_comment, po->op == OP_BSF ? " bsf" : " bsr");
7598         break;
7599
7600       case OP_DEC:
7601         if (pfomask & ~(PFOB_S | PFOB_S | PFOB_C)) {
7602           for (j = 0; j <= PFO_LE; j++) {
7603             if (!(pfomask & (1 << j)))
7604               continue;
7605             if (j == PFO_Z || j == PFO_S || j == PFO_C)
7606               continue;
7607
7608             out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0, 0);
7609             fprintf(fout, "  cond_%s = %s;\n",
7610               parsed_flag_op_names[j], buf1);
7611             pfomask &= ~(1 << j);
7612           }
7613         }
7614         // fallthrough
7615
7616       case OP_INC:
7617         if (pfomask & (1 << PFO_C))
7618           // carry is unaffected by inc/dec.. wtf?
7619           ferr(po, "carry propagation needed\n");
7620
7621         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7622         if (po->operand[0].type == OPT_REG) {
7623           ferr_assert(po, !(po->flags & OPF_LOCK));
7624           strcpy(buf2, po->op == OP_INC ? "++" : "--");
7625           fprintf(fout, "  %s%s;", buf1, buf2);
7626         }
7627         else if (po->flags & OPF_LOCK) {
7628           out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], "", 1);
7629           fprintf(fout, "  __sync_fetch_and_%s((%s *)(%s), 1);",
7630             po->op == OP_INC ? "add" : "sub",
7631             lmod_type_u(po, po->operand[0].lmod), buf2);
7632           strcat(g_comment, " lock");
7633           lock_handled = 1;
7634         }
7635         else {
7636           strcpy(buf2, po->op == OP_INC ? "+" : "-");
7637           fprintf(fout, "  %s %s= 1;", buf1, buf2);
7638         }
7639         output_std_flags(fout, po, &pfomask, buf1);
7640         last_arith_dst = &po->operand[0];
7641         delayed_flag_op = NULL;
7642         break;
7643
7644       case OP_NEG:
7645         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
7646         out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
7647         fprintf(fout, "  %s = -%s%s;", buf1,
7648           lmod_cast_s(po, po->operand[0].lmod), buf2);
7649         last_arith_dst = &po->operand[0];
7650         delayed_flag_op = NULL;
7651         if (pfomask & PFOB_C) {
7652           fprintf(fout, "\n  cond_c = (%s != 0);", buf1);
7653           pfomask &= ~PFOB_C;
7654         }
7655         output_std_flags(fout, po, &pfomask, buf1);
7656         break;
7657
7658       case OP_IMUL:
7659         if (po->operand_cnt == 2) {
7660           propagate_lmod(po, &po->operand[0], &po->operand[1]);
7661           goto dualop_arith;
7662         }
7663         if (po->operand_cnt == 3)
7664           ferr(po, "TODO imul3\n");
7665         // fallthrough
7666       case OP_MUL:
7667         assert_operand_cnt(1);
7668         switch (po->operand[0].lmod) {
7669         case OPLM_DWORD:
7670           strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
7671           fprintf(fout, "  tmp64 = %seax * %s%s;\n", buf1, buf1,
7672             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
7673           fprintf(fout, "  edx = tmp64 >> 32;\n");
7674           fprintf(fout, "  eax = tmp64;");
7675           break;
7676         case OPLM_BYTE:
7677           strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
7678           fprintf(fout, "  LOWORD(eax) = %seax * %s;", buf1,
7679             out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
7680               buf1, 0));
7681           break;
7682         default:
7683           ferr(po, "TODO: unhandled mul type\n");
7684           break;
7685         }
7686         last_arith_dst = NULL;
7687         delayed_flag_op = NULL;
7688         break;
7689
7690       case OP_DIV:
7691       case OP_IDIV:
7692         assert_operand_cnt(1);
7693         out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
7694         strcpy(cast, lmod_cast(po, po->operand[0].lmod,
7695           po->op == OP_IDIV));
7696         switch (po->operand[0].lmod) {
7697         case OPLM_DWORD:
7698           if (po->flags & OPF_32BIT)
7699             snprintf(buf2, sizeof(buf2), "%seax", cast);
7700           else {
7701             fprintf(fout, "  tmp64 = ((u64)edx << 32) | eax;\n");
7702             snprintf(buf2, sizeof(buf2), "%stmp64",
7703               (po->op == OP_IDIV) ? "(s64)" : "");
7704           }
7705           if (po->operand[0].type == OPT_REG
7706             && po->operand[0].reg == xDX)
7707           {
7708             fprintf(fout, "  eax = %s / %s%s;\n", buf2, cast, buf1);
7709             fprintf(fout, "  edx = %s %% %s%s;", buf2, cast, buf1);
7710           }
7711           else {
7712             fprintf(fout, "  edx = %s %% %s%s;\n", buf2, cast, buf1);
7713             fprintf(fout, "  eax = %s / %s%s;", buf2, cast, buf1);
7714           }
7715           break;
7716         case OPLM_WORD:
7717           fprintf(fout, "  tmp = (edx << 16) | (eax & 0xffff);\n");
7718           snprintf(buf2, sizeof(buf2), "%stmp",
7719             (po->op == OP_IDIV) ? "(s32)" : "");
7720           if (po->operand[0].type == OPT_REG
7721             && po->operand[0].reg == xDX)
7722           {
7723             fprintf(fout, "  LOWORD(eax) = %s / %s%s;\n",
7724               buf2, cast, buf1);
7725             fprintf(fout, "  LOWORD(edx) = %s %% %s%s;",
7726               buf2, cast, buf1);
7727           }
7728           else {
7729             fprintf(fout, "  LOWORD(edx) = %s %% %s%s;\n",
7730               buf2, cast, buf1);
7731             fprintf(fout, "  LOWORD(eax) = %s / %s%s;",
7732               buf2, cast, buf1);
7733           }
7734           strcat(g_comment, " div16");
7735           break;
7736         default:
7737           ferr(po, "unhandled div lmod %d\n", po->operand[0].lmod);
7738         }
7739         last_arith_dst = NULL;
7740         delayed_flag_op = NULL;
7741         break;
7742
7743       case OP_TEST:
7744       case OP_CMP:
7745         propagate_lmod(po, &po->operand[0], &po->operand[1]);
7746         if (pfomask != 0) {
7747           for (j = 0; j < 8; j++) {
7748             if (pfomask & (1 << j)) {
7749               out_cmp_test(buf1, sizeof(buf1), po, j, 0);
7750               fprintf(fout, "  cond_%s = %s;",
7751                 parsed_flag_op_names[j], buf1);
7752             }
7753           }
7754           pfomask = 0;
7755         }
7756         else
7757           no_output = 1;
7758         last_arith_dst = NULL;
7759         delayed_flag_op = po;
7760         break;
7761
7762       case OP_SCC:
7763         // SETcc - should already be handled
7764         break;
7765
7766       // note: we reuse OP_Jcc for SETcc, only flags differ
7767       case OP_JCC:
7768         fprintf(fout, "\n    goto %s;", po->operand[0].name);
7769         break;
7770
7771       case OP_JECXZ:
7772         fprintf(fout, "  if (ecx == 0)\n");
7773         fprintf(fout, "    goto %s;", po->operand[0].name);
7774         strcat(g_comment, " jecxz");
7775         break;
7776
7777       case OP_LOOP:
7778         fprintf(fout, "  if (--ecx != 0)\n");
7779         fprintf(fout, "    goto %s;", po->operand[0].name);
7780         strcat(g_comment, " loop");
7781         break;
7782
7783       case OP_JMP:
7784         assert_operand_cnt(1);
7785         last_arith_dst = NULL;
7786         delayed_flag_op = NULL;
7787
7788         if (po->operand[0].type == OPT_REGMEM) {
7789           ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
7790                   buf1, buf2);
7791           if (ret != 2)
7792             ferr(po, "parse failure for jmp '%s'\n",
7793               po->operand[0].name);
7794           fprintf(fout, "  goto *jt_%s[%s];", buf1, buf2);
7795           break;
7796         }
7797         else if (po->operand[0].type != OPT_LABEL)
7798           ferr(po, "unhandled jmp type\n");
7799
7800         fprintf(fout, "  goto %s;", po->operand[0].name);
7801         break;
7802
7803       case OP_CALL:
7804         assert_operand_cnt(1);
7805         pp = po->pp;
7806         my_assert_not(pp, NULL);
7807
7808         strcpy(buf3, "  ");
7809         if (po->flags & OPF_CC) {
7810           // we treat conditional branch to another func
7811           // (yes such code exists..) as conditional tailcall
7812           strcat(buf3, "  ");
7813           fprintf(fout, " {\n");
7814         }
7815
7816         if (pp->is_fptr && !pp->is_arg) {
7817           fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
7818             out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
7819               "(void *)", 0));
7820         }
7821         if (pp->is_fptr && (pp->is_unresolved || pp->is_guessed)) {
7822           fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
7823             buf3, asmfn, po->asmln, pp->name);
7824         }
7825
7826         fprintf(fout, "%s", buf3);
7827         if (strstr(pp->ret_type.name, "int64")) {
7828           if (po->flags & OPF_TAIL)
7829             ferr(po, "int64 and tail?\n");
7830           fprintf(fout, "tmp64 = ");
7831         }
7832         else if (!IS(pp->ret_type.name, "void")) {
7833           if (po->flags & OPF_TAIL) {
7834             if (regmask_ret & mxAX) {
7835               fprintf(fout, "return ");
7836               if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
7837                 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
7838             }
7839             else if (regmask_ret & mxST0)
7840               ferr(po, "float tailcall\n");
7841           }
7842           else if (po->regmask_dst & mxAX) {
7843             fprintf(fout, "eax = ");
7844             if (pp->ret_type.is_ptr)
7845               fprintf(fout, "(u32)");
7846           }
7847           else if (po->regmask_dst & mxST0) {
7848             ferr_assert(po, po->flags & OPF_FPUSH);
7849             if (need_float_stack)
7850               fprintf(fout, "f_st[--f_stp & 7] = ");
7851             else
7852               fprintf(fout, "f_st0 = ");
7853           }
7854         }
7855
7856         if (pp->name[0] == 0)
7857           ferr(po, "missing pp->name\n");
7858         fprintf(fout, "%s%s(", pp->name,
7859           pp->has_structarg ? "_sa" : "");
7860
7861         if (po->flags & OPF_ATAIL) {
7862           int check_compat =
7863             g_func_pp->is_stdcall && g_func_pp->argc_stack > 0;
7864           check_compat |= pp->argc_stack > 0;
7865           if (check_compat
7866            && (pp->argc_stack != g_func_pp->argc_stack
7867                || pp->is_stdcall != g_func_pp->is_stdcall))
7868             ferr(po, "incompatible arg-reuse tailcall\n");
7869           if (g_func_pp->has_retreg)
7870             ferr(po, "TODO: retreg+tailcall\n");
7871
7872           for (arg = j = 0; arg < pp->argc; arg++) {
7873             if (arg > 0)
7874               fprintf(fout, ", ");
7875
7876             cast[0] = 0;
7877             if (pp->arg[arg].type.is_ptr)
7878               snprintf(cast, sizeof(cast), "(%s)",
7879                 pp->arg[arg].type.name);
7880
7881             if (pp->arg[arg].reg != NULL) {
7882               fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
7883               continue;
7884             }
7885             // stack arg
7886             for (; j < g_func_pp->argc; j++)
7887               if (g_func_pp->arg[j].reg == NULL)
7888                 break;
7889             fprintf(fout, "%sa%d", cast, j + 1);
7890             j++;
7891           }
7892         }
7893         else {
7894           for (arg = 0; arg < pp->argc; arg++) {
7895             if (arg > 0)
7896               fprintf(fout, ", ");
7897
7898             cast[0] = 0;
7899             if (pp->arg[arg].type.is_ptr)
7900               snprintf(cast, sizeof(cast), "(%s)",
7901                 pp->arg[arg].type.name);
7902
7903             if (pp->arg[arg].reg != NULL) {
7904               if (pp->arg[arg].type.is_retreg)
7905                 fprintf(fout, "&%s", pp->arg[arg].reg);
7906               else if (IS(pp->arg[arg].reg, "ebp")
7907                     && g_bp_frame && !(po->flags & OPF_EBP_S))
7908               {
7909                 // rare special case
7910                 fprintf(fout, "%s(u32)&sf.b[sizeof(sf)]", cast);
7911                 strcat(g_comment, " bp_ref");
7912               }
7913               else
7914                 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
7915               continue;
7916             }
7917
7918             // stack arg
7919             if (pp->arg[arg].push_ref_cnt == 0)
7920               ferr(po, "parsed_op missing for arg%d\n", arg);
7921             if (pp->arg[arg].push_ref_cnt > 1)
7922               ferr_assert(po, pp->arg[arg].is_saved);
7923             tmp_op = pp->arg[arg].push_refs[0];
7924             ferr_assert(po, tmp_op != NULL);
7925
7926             if (tmp_op->flags & OPF_VAPUSH) {
7927               fprintf(fout, "ap");
7928             }
7929             else if (tmp_op->op == OP_FST) {
7930               fprintf(fout, "fs_%d", tmp_op->p_argnum);
7931               if (tmp_op->operand[0].lmod == OPLM_QWORD)
7932                 arg++;
7933             }
7934             else if (pp->arg[arg].type.is_64bit) {
7935               ferr_assert(po, tmp_op->p_argpass == 0);
7936               ferr_assert(po, !pp->arg[arg].is_saved);
7937               ferr_assert(po, !pp->arg[arg].type.is_float);
7938               ferr_assert(po, cast[0] == 0);
7939               out_src_opr(buf1, sizeof(buf1),
7940                 tmp_op, &tmp_op->operand[0], cast, 0);
7941               arg++;
7942               ferr_assert(po, pp->arg[arg].push_ref_cnt == 1);
7943               tmp_op = pp->arg[arg].push_refs[0];
7944               ferr_assert(po, tmp_op != NULL);
7945               out_src_opr(buf2, sizeof(buf2),
7946                 tmp_op, &tmp_op->operand[0], cast, 0);
7947               fprintf(fout, "((u64)(%s) << 32) | (%s)",
7948                 buf2, buf1);
7949             }
7950             else if (tmp_op->p_argpass != 0) {
7951               ferr_assert(po, !pp->arg[arg].type.is_float);
7952               fprintf(fout, "a%d", tmp_op->p_argpass);
7953             }
7954             else if (pp->arg[arg].is_saved) {
7955               ferr_assert(po, tmp_op->p_argnum > 0);
7956               ferr_assert(po, !pp->arg[arg].type.is_float);
7957               fprintf(fout, "%s%s", cast,
7958                 saved_arg_name(buf1, sizeof(buf1),
7959                   tmp_op->p_arggrp, tmp_op->p_argnum));
7960             }
7961             else if (pp->arg[arg].type.is_float) {
7962               ferr_assert(po, !pp->arg[arg].type.is_64bit);
7963               fprintf(fout, "%s",
7964                 out_src_opr_float(buf1, sizeof(buf1),
7965                   tmp_op, &tmp_op->operand[0], need_float_stack));
7966             }
7967             else {
7968               fprintf(fout, "%s",
7969                 out_src_opr(buf1, sizeof(buf1),
7970                   tmp_op, &tmp_op->operand[0], cast, 0));
7971             }
7972           }
7973         }
7974         fprintf(fout, ");");
7975
7976         if (strstr(pp->ret_type.name, "int64")) {
7977           fprintf(fout, "\n");
7978           fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
7979           fprintf(fout, "%seax = tmp64;", buf3);
7980         }
7981
7982         if (pp->is_unresolved) {
7983           snprintf(buf2, sizeof(buf2), " unresolved %dreg",
7984             pp->argc_reg);
7985           strcat(g_comment, buf2);
7986         }
7987
7988         if (po->flags & OPF_TAIL) {
7989           ret = 0;
7990           if (i == opcnt - 1 || pp->is_noreturn)
7991             ret = 0;
7992           else if (IS(pp->ret_type.name, "void"))
7993             ret = 1;
7994           else if (!(regmask_ret & (1 << xAX)))
7995             ret = 1;
7996           // else already handled as 'return f()'
7997
7998           if (ret) {
7999             fprintf(fout, "\n%sreturn;", buf3);
8000             strcat(g_comment, " ^ tailcall");
8001           }
8002           else
8003             strcat(g_comment, " tailcall");
8004
8005           if ((regmask_ret & (1 << xAX))
8006             && IS(pp->ret_type.name, "void") && !pp->is_noreturn)
8007           {
8008             ferr(po, "int func -> void func tailcall?\n");
8009           }
8010         }
8011         if (pp->is_noreturn)
8012           strcat(g_comment, " noreturn");
8013         if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
8014           strcat(g_comment, " argframe");
8015         if (po->flags & OPF_CC)
8016           strcat(g_comment, " cond");
8017
8018         if (po->flags & OPF_CC)
8019           fprintf(fout, "\n  }");
8020
8021         delayed_flag_op = NULL;
8022         last_arith_dst = NULL;
8023         break;
8024
8025       case OP_RET:
8026       do_tail:
8027         if (g_func_pp->is_vararg)
8028           fprintf(fout, "  va_end(ap);\n");
8029         if (g_func_pp->has_retreg) {
8030           for (arg = 0; arg < g_func_pp->argc; arg++)
8031             if (g_func_pp->arg[arg].type.is_retreg)
8032               fprintf(fout, "  *r_%s = %s;\n",
8033                 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
8034         }
8035  
8036         if (regmask_ret & mxST0) {
8037           fprintf(fout, "  return %s;", float_st0);
8038         }
8039         else if (!(regmask_ret & mxAX)) {
8040           if (i != opcnt - 1 || label_pending)
8041             fprintf(fout, "  return;");
8042         }
8043         else if (g_func_pp->ret_type.is_ptr) {
8044           fprintf(fout, "  return (%s)eax;",
8045             g_func_pp->ret_type.name);
8046         }
8047         else if (IS(g_func_pp->ret_type.name, "__int64"))
8048           fprintf(fout, "  return ((u64)edx << 32) | eax;");
8049         else
8050           fprintf(fout, "  return eax;");
8051
8052         last_arith_dst = NULL;
8053         delayed_flag_op = NULL;
8054         break;
8055
8056       case OP_PUSH:
8057         out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
8058         if (po->p_argnum != 0) {
8059           // special case - saved func arg
8060           fprintf(fout, "  %s = %s;",
8061             saved_arg_name(buf2, sizeof(buf2),
8062               po->p_arggrp, po->p_argnum), buf1);
8063           break;
8064         }
8065         else if (po->flags & OPF_RSAVE) {
8066           fprintf(fout, "  s_%s = %s;", buf1, buf1);
8067           break;
8068         }
8069         else if (po->flags & OPF_PPUSH) {
8070           tmp_op = po->datap;
8071           ferr_assert(po, tmp_op != NULL);
8072           out_dst_opr(buf2, sizeof(buf2), po, &tmp_op->operand[0]);
8073           fprintf(fout, "  pp_%s = %s;", buf2, buf1);
8074           break;
8075         }
8076         else if (g_func_pp->is_userstack) {
8077           fprintf(fout, "  *(--esp) = %s;", buf1);
8078           break;
8079         }
8080         if (!(g_ida_func_attr & IDAFA_NORETURN))
8081           ferr(po, "stray push encountered\n");
8082         no_output = 1;
8083         break;
8084
8085       case OP_POP:
8086         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
8087         if (po->flags & OPF_RSAVE) {
8088           fprintf(fout, "  %s = s_%s;", buf1, buf1);
8089           break;
8090         }
8091         else if (po->flags & OPF_PPUSH) {
8092           // push/pop graph / non-const
8093           ferr_assert(po, po->datap == NULL);
8094           fprintf(fout, "  %s = pp_%s;", buf1, buf1);
8095           break;
8096         }
8097         else if (po->datap != NULL) {
8098           // push/pop pair
8099           tmp_op = po->datap;
8100           fprintf(fout, "  %s = %s;", buf1,
8101             out_src_opr(buf2, sizeof(buf2),
8102               tmp_op, &tmp_op->operand[0],
8103               default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
8104           break;
8105         }
8106         else if (g_func_pp->is_userstack) {
8107           fprintf(fout, "  %s = *esp++;", buf1);
8108           break;
8109         }
8110         else
8111           ferr(po, "stray pop encountered\n");
8112         break;
8113
8114       case OP_NOP:
8115         no_output = 1;
8116         break;
8117
8118       // pseudo ops
8119       case OPP_ALLSHL:
8120       case OPP_ALLSHR:
8121         fprintf(fout, "  tmp64 = ((u64)edx << 32) | eax;\n");
8122         fprintf(fout, "  tmp64 = (s64)tmp64 %s LOBYTE(ecx);\n",
8123           po->op == OPP_ALLSHL ? "<<" : ">>");
8124         fprintf(fout, "  edx = tmp64 >> 32; eax = tmp64;");
8125         strcat(g_comment, po->op == OPP_ALLSHL
8126           ? " allshl" : " allshr");
8127         break;
8128
8129       // x87
8130       case OP_FLD:
8131         if (need_float_stack) {
8132           out_src_opr_float(buf1, sizeof(buf1),
8133             po, &po->operand[0], 1);
8134           if (po->regmask_src & mxSTa) {
8135             fprintf(fout, "  f_st[(f_stp - 1) & 7] = %s; f_stp--;",
8136               buf1);
8137           }
8138           else
8139             fprintf(fout, "  f_st[--f_stp & 7] = %s;", buf1);
8140         }
8141         else {
8142           if (po->flags & OPF_FSHIFT)
8143             fprintf(fout, "  f_st1 = f_st0;");
8144           if (po->operand[0].type == OPT_REG
8145             && po->operand[0].reg == xST0)
8146           {
8147             strcat(g_comment, " fld st");
8148             break;
8149           }
8150           fprintf(fout, "  f_st0 = %s;",
8151             out_src_opr_float(buf1, sizeof(buf1),
8152               po, &po->operand[0], 0));
8153         }
8154         strcat(g_comment, " fld");
8155         break;
8156
8157       case OP_FILD:
8158         out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
8159           lmod_cast(po, po->operand[0].lmod, 1), 0);
8160         snprintf(buf2, sizeof(buf2), "(%s)%s", float_type, buf1);
8161         if (need_float_stack) {
8162           fprintf(fout, "  f_st[--f_stp & 7] = %s;", buf2);
8163         }
8164         else {
8165           if (po->flags & OPF_FSHIFT)
8166             fprintf(fout, "  f_st1 = f_st0;");
8167           fprintf(fout, "  f_st0 = %s;", buf2);
8168         }
8169         strcat(g_comment, " fild");
8170         break;
8171
8172       case OP_FLDc:
8173         if (need_float_stack)
8174           fprintf(fout, "  f_st[--f_stp & 7] = ");
8175         else {
8176           if (po->flags & OPF_FSHIFT)
8177             fprintf(fout, "  f_st1 = f_st0;");
8178           fprintf(fout, "  f_st0 = ");
8179         }
8180         switch (po->operand[0].val) {
8181         case X87_CONST_1:   fprintf(fout, "1.0;"); break;
8182         case X87_CONST_L2T: fprintf(fout, "3.321928094887362;"); break;
8183         case X87_CONST_L2E: fprintf(fout, "M_LOG2E;"); break;
8184         case X87_CONST_PI:  fprintf(fout, "M_PI;"); break;
8185         case X87_CONST_LG2: fprintf(fout, "0.301029995663981;"); break;
8186         case X87_CONST_LN2: fprintf(fout, "M_LN2;"); break;
8187         case X87_CONST_Z:   fprintf(fout, "0.0;"); break;
8188         default: ferr_assert(po, 0); break;
8189         }
8190         break;
8191
8192       case OP_FST:
8193         dead_dst = 0;
8194         if (po->flags & OPF_FARG) {
8195           // store to stack as func arg
8196           fprintf(fout, "  fs_%d = %s;", po->p_argnum, float_st0);
8197         }
8198         else if (po->operand[0].type == OPT_REG
8199                  && po->operand[0].reg == xST0)
8200         {
8201           dead_dst = 1;
8202         }
8203         else if (float_opr_needs_helper(po, &po->operand[0])) {
8204           out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 1);
8205           fprintf(fout, "  %s_store(%s, %s);",
8206             po->operand[0].lmod == OPLM_QWORD ? "double" : "float",
8207             float_st0, buf1);
8208         }
8209         else {
8210           out_dst_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
8211             need_float_stack);
8212           fprintf(fout, "  %s = %s;", buf1, float_st0);
8213         }
8214         if (po->flags & OPF_FSHIFT) {
8215           if (need_float_stack)
8216             fprintf(fout, "  f_stp++;");
8217           else
8218             fprintf(fout, "  f_st0 = f_st1;");
8219         }
8220         if (dead_dst && !(po->flags & OPF_FSHIFT))
8221           no_output = 1;
8222         else
8223           strcat(g_comment, " fst");
8224         break;
8225
8226       case OP_FIST:
8227         fprintf(fout, "  %s = %s%s;",
8228           out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
8229             lmod_cast(po, po->operand[0].lmod, 1), float_st0);
8230         if (po->flags & OPF_FSHIFT) {
8231           if (need_float_stack)
8232             fprintf(fout, "  f_stp++;");
8233           else
8234             fprintf(fout, "  f_st0 = f_st1;");
8235         }
8236         strcat(g_comment, " fist");
8237         break;
8238
8239       case OP_FABS:
8240         fprintf(fout, "  %s = fabs%s(%s);", float_st0,
8241           need_double ? "" : "f", float_st0);
8242         break;
8243
8244       case OP_FADD:
8245       case OP_FDIV:
8246       case OP_FMUL:
8247       case OP_FSUB:
8248         out_dst_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
8249           need_float_stack);
8250         out_src_opr_float(buf2, sizeof(buf2), po, &po->operand[1],
8251           need_float_stack);
8252         dead_dst = (po->flags & OPF_FPOP)
8253           && po->operand[0].type == OPT_REG
8254           && po->operand[0].reg == xST0;
8255         switch (po->op) {
8256         case OP_FADD: j = '+'; break;
8257         case OP_FDIV: j = '/'; break;
8258         case OP_FMUL: j = '*'; break;
8259         case OP_FSUB: j = '-'; break;
8260         default: j = 'x'; break;
8261         }
8262         if (need_float_stack) {
8263           if (!dead_dst)
8264             fprintf(fout, "  %s %c= %s;", buf1, j, buf2);
8265           if (po->flags & OPF_FSHIFT)
8266             fprintf(fout, "  f_stp++;");
8267         }
8268         else {
8269           if (po->flags & OPF_FSHIFT) {
8270             // note: assumes only 2 regs handled
8271             if (!dead_dst)
8272               fprintf(fout, "  f_st0 = f_st1 %c f_st0;", j);
8273             else
8274               fprintf(fout, "  f_st0 = f_st1;");
8275           }
8276           else if (!dead_dst)
8277             fprintf(fout, "  %s %c= %s;", buf1, j, buf2);
8278         }
8279         no_output = (dead_dst && !(po->flags & OPF_FSHIFT));
8280         break;
8281
8282       case OP_FDIVR:
8283       case OP_FSUBR:
8284         out_dst_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
8285           need_float_stack);
8286         out_src_opr_float(buf2, sizeof(buf2), po, &po->operand[1],
8287           need_float_stack);
8288         out_src_opr_float(buf3, sizeof(buf3), po, &po->operand[0],
8289           need_float_stack);
8290         dead_dst = (po->flags & OPF_FPOP)
8291           && po->operand[0].type == OPT_REG
8292           && po->operand[0].reg == xST0;
8293         j = po->op == OP_FDIVR ? '/' : '-';
8294         if (need_float_stack) {
8295           if (!dead_dst)
8296             fprintf(fout, "  %s = %s %c %s;", buf1, buf2, j, buf3);
8297           if (po->flags & OPF_FSHIFT)
8298             fprintf(fout, "  f_stp++;");
8299         }
8300         else {
8301           if (po->flags & OPF_FSHIFT) {
8302             if (!dead_dst)
8303               fprintf(fout, "  f_st0 = f_st0 %c f_st1;", j);
8304             else
8305               fprintf(fout, "  f_st0 = f_st1;");
8306           }
8307           else if (!dead_dst)
8308             fprintf(fout, "  %s = %s %c %s;", buf1, buf2, j, buf3);
8309         }
8310         no_output = (dead_dst && !(po->flags & OPF_FSHIFT));
8311         break;
8312
8313       case OP_FIADD:
8314       case OP_FIDIV:
8315       case OP_FIMUL:
8316       case OP_FISUB:
8317         switch (po->op) {
8318         case OP_FIADD: j = '+'; break;
8319         case OP_FIDIV: j = '/'; break;
8320         case OP_FIMUL: j = '*'; break;
8321         case OP_FISUB: j = '-'; break;
8322         default: j = 'x'; break;
8323         }
8324         fprintf(fout, "  %s %c= (%s)%s;", float_st0,
8325           j, float_type,
8326           out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
8327             lmod_cast(po, po->operand[0].lmod, 1), 0));
8328         break;
8329
8330       case OP_FIDIVR:
8331       case OP_FISUBR:
8332         fprintf(fout, "  %s = %s %c %s;", float_st0,
8333           out_src_opr_float(buf2, sizeof(buf2), po, &po->operand[1],
8334             need_float_stack),
8335           po->op == OP_FIDIVR ? '/' : '-', float_st0);
8336         break;
8337
8338       case OP_FCOM: {
8339         int mask, z_check;
8340         ferr_assert(po, po->datap != NULL);
8341         mask = (long)po->datap & 0xffff;
8342         z_check = ((long)po->datap >> 16) & 1;
8343         out_src_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
8344           need_float_stack);
8345         if (mask == 0x0100 || mask == 0x0500) { // C0 -> <
8346           fprintf(fout, "  f_sw = %s < %s ? 0x0100 : 0;",
8347             float_st0, buf1);
8348         }
8349         else if (mask == 0x4000 || mask == 0x4400) { // C3 -> =
8350           fprintf(fout, "  f_sw = %s == %s ? 0x4000 : 0;",
8351             float_st0, buf1);
8352         }
8353         else if (mask == 0x4100) { // C3, C0
8354           if (z_check) {
8355             fprintf(fout, "  f_sw = %s <= %s ? 0x4100 : 0;",
8356               float_st0, buf1);
8357             strcat(g_comment, " z_chk_det");
8358           }
8359           else {
8360             fprintf(fout, "  f_sw = %s == %s ? 0x4000 : "
8361                           "(%s < %s ? 0x0100 : 0);",
8362               float_st0, buf1, float_st0, buf1);
8363           }
8364         }
8365         else
8366           ferr(po, "unhandled sw mask: %x\n", mask);
8367         if (po->flags & OPF_FSHIFT) {
8368           if (need_float_stack) {
8369             if (po->flags & OPF_FPOPP)
8370               fprintf(fout, " f_stp += 2;");
8371             else
8372               fprintf(fout, " f_stp++;");
8373           }
8374           else {
8375             ferr_assert(po, !(po->flags & OPF_FPOPP));
8376             fprintf(fout, " f_st0 = f_st1;");
8377           }
8378         }
8379         break;
8380       }
8381
8382       case OP_FNSTSW:
8383         fprintf(fout, "  %s = f_sw;",
8384           out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
8385         break;
8386
8387       case OP_FCHS:
8388         fprintf(fout, "  %s = -%s;", float_st0, float_st0);
8389         break;
8390
8391       case OP_FCOS:
8392         fprintf(fout, "  %s = cos%s(%s);", float_st0,
8393           need_double ? "" : "f", float_st0);
8394         break;
8395
8396       case OP_FPATAN:
8397         if (need_float_stack) {
8398           fprintf(fout, "  %s = atan%s(%s / %s);", float_st1,
8399             need_double ? "" : "f", float_st1, float_st0);
8400           fprintf(fout, " f_stp++;");
8401         }
8402         else {
8403           fprintf(fout, "  f_st0 = atan%s(f_st1 / f_st0);",
8404             need_double ? "" : "f");
8405         }
8406         break;
8407
8408       case OP_FYL2X:
8409         if (need_float_stack) {
8410           fprintf(fout, "  %s = %s * log2%s(%s);", float_st1,
8411             float_st1, need_double ? "" : "f", float_st0);
8412           fprintf(fout, " f_stp++;");
8413         }
8414         else {
8415           fprintf(fout, "  f_st0 = f_st1 * log2%s(f_st0);",
8416             need_double ? "" : "f");
8417         }
8418         strcat(g_comment, " fyl2x");
8419         break;
8420
8421       case OP_FSIN:
8422         fprintf(fout, "  %s = sin%s(%s);", float_st0,
8423           need_double ? "" : "f", float_st0);
8424         break;
8425
8426       case OP_FSQRT:
8427         fprintf(fout, "  %s = sqrt%s(%s);", float_st0,
8428           need_double ? "" : "f", float_st0);
8429         break;
8430
8431       case OP_FXCH:
8432         dead_dst = po->operand[0].type == OPT_REG
8433           && po->operand[0].reg == xST0;
8434         if (!dead_dst) {
8435           out_src_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
8436             need_float_stack);
8437           fprintf(fout, "  { %s t = %s; %s = %s; %s = t; }", float_type,
8438             float_st0, float_st0, buf1, buf1);
8439           strcat(g_comment, " fxch");
8440         }
8441         else
8442           no_output = 1;
8443         break;
8444
8445       case OPP_FTOL:
8446         ferr_assert(po, po->flags & OPF_32BIT);
8447         fprintf(fout, "  eax = (s32)%s;", float_st0);
8448         if (po->flags & OPF_FSHIFT) {
8449           if (need_float_stack)
8450             fprintf(fout, " f_stp++;");
8451           else
8452             fprintf(fout, " f_st0 = f_st1;");
8453         }
8454         strcat(g_comment, " ftol");
8455         goto tail_check;
8456
8457       case OPP_CIPOW:
8458         if (need_float_stack) {
8459           fprintf(fout, "  %s = pow%s(%s, %s);", float_st1,
8460             need_double ? "" : "f", float_st1, float_st0);
8461           fprintf(fout, " f_stp++;");
8462         }
8463         else {
8464           fprintf(fout, "  f_st0 = pow%s(f_st1, f_st0);",
8465             need_double ? "" : "f");
8466         }
8467         strcat(g_comment, " CIpow");
8468         goto tail_check;
8469
8470       case OPP_ABORT:
8471         fprintf(fout, "  do_skip_code_abort();");
8472         break;
8473
8474       // mmx
8475       case OP_EMMS:
8476         fprintf(fout, "  do_emms();");
8477         break;
8478
8479       tail_check:
8480         if (po->flags & OPF_TAIL) {
8481           fprintf(fout, "\n");
8482           strcat(g_comment, " tail");
8483           goto do_tail;
8484         }
8485         break;
8486
8487       default:
8488         no_output = 1;
8489         ferr(po, "unhandled op type %d, flags %x\n",
8490           po->op, po->flags);
8491         break;
8492     }
8493
8494     if (g_comment[0] != 0) {
8495       char *p = g_comment;
8496       while (my_isblank(*p))
8497         p++;
8498       fprintf(fout, "  // %s", p);
8499       g_comment[0] = 0;
8500       no_output = 0;
8501     }
8502     if (!no_output)
8503       fprintf(fout, "\n");
8504
8505     // some sanity checking
8506     if (po->flags & OPF_REP) {
8507       if (po->op != OP_STOS && po->op != OP_MOVS
8508           && po->op != OP_CMPS && po->op != OP_SCAS)
8509         ferr(po, "unexpected rep\n");
8510       if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
8511           && (po->op == OP_CMPS || po->op == OP_SCAS))
8512         ferr(po, "cmps/scas with plain rep\n");
8513     }
8514     if ((po->flags & (OPF_REPZ|OPF_REPNZ))
8515         && po->op != OP_CMPS && po->op != OP_SCAS)
8516       ferr(po, "unexpected repz/repnz\n");
8517
8518     if (pfomask != 0)
8519       ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
8520
8521     if ((po->flags & OPF_LOCK) && !lock_handled)
8522       ferr(po, "unhandled lock\n");
8523
8524     // see is delayed flag stuff is still valid
8525     if (delayed_flag_op != NULL && delayed_flag_op != po) {
8526       if (is_any_opr_modified(delayed_flag_op, po, 0))
8527         delayed_flag_op = NULL;
8528     }
8529
8530     if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
8531       if (is_opr_modified(last_arith_dst, po))
8532         last_arith_dst = NULL;
8533     }
8534
8535     if (!no_output)
8536       label_pending = 0;
8537   }
8538
8539   if (g_stack_fsz && !g_stack_frame_used)
8540     fprintf(fout, "  (void)sf;\n");
8541
8542   fprintf(fout, "}\n\n");
8543
8544   gen_x_cleanup(opcnt);
8545 }
8546
8547 static void gen_x_cleanup(int opcnt)
8548 {
8549   int i;
8550
8551   for (i = 0; i < opcnt; i++) {
8552     struct label_ref *lr, *lr_del;
8553
8554     lr = g_label_refs[i].next;
8555     while (lr != NULL) {
8556       lr_del = lr;
8557       lr = lr->next;
8558       free(lr_del);
8559     }
8560     g_label_refs[i].i = -1;
8561     g_label_refs[i].next = NULL;
8562
8563     if (ops[i].op == OP_CALL) {
8564       if (ops[i].pp)
8565         proto_release(ops[i].pp);
8566     }
8567   }
8568   g_func_pp = NULL;
8569 }
8570
8571 struct func_proto_dep;
8572
8573 struct func_prototype {
8574   char name[NAMELEN];
8575   int id;
8576   int argc_stack;
8577   int regmask_dep;               // likely register args
8578   int regmask_use;               // used registers
8579   int has_ret:3;                 // -1, 0, 1: unresolved, no, yes
8580   unsigned int has_ret64:1;
8581   unsigned int dep_resolved:1;
8582   unsigned int is_stdcall:1;
8583   unsigned int eax_pass:1;       // returns without touching eax
8584   unsigned int ptr_taken:1;      // pointer taken of this func
8585   struct func_proto_dep *dep_func;
8586   int dep_func_cnt;
8587   const struct parsed_proto *pp; // seed pp, if any
8588 };
8589
8590 struct func_proto_dep {
8591   char *name;
8592   struct func_prototype *proto;
8593   int regmask_live;             // .. at the time of call
8594   unsigned int ret_dep:1;       // return from this is caller's return
8595   unsigned int has_ret:1;       // found from eax use after return
8596   unsigned int has_ret64:1;
8597   unsigned int ptr_taken:1;     // pointer taken, not a call
8598 };
8599
8600 static struct func_prototype *hg_fp;
8601 static int hg_fp_cnt;
8602
8603 static struct scanned_var {
8604   char name[NAMELEN];
8605   enum opr_lenmod lmod;
8606   unsigned int is_seeded:1;
8607   unsigned int is_c_str:1;
8608   const struct parsed_proto *pp; // seed pp, if any
8609 } *hg_vars;
8610 static int hg_var_cnt;
8611
8612 static char **hg_refs;
8613 static int hg_ref_cnt;
8614
8615 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
8616   int count);
8617
8618 static struct func_prototype *hg_fp_add(const char *funcn)
8619 {
8620   struct func_prototype *fp;
8621
8622   if ((hg_fp_cnt & 0xff) == 0) {
8623     hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100));
8624     my_assert_not(hg_fp, NULL);
8625     memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100);
8626   }
8627
8628   fp = &hg_fp[hg_fp_cnt];
8629   snprintf(fp->name, sizeof(fp->name), "%s", funcn);
8630   fp->id = hg_fp_cnt;
8631   fp->argc_stack = -1;
8632   hg_fp_cnt++;
8633
8634   return fp;
8635 }
8636
8637 static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp,
8638   const char *name)
8639 {
8640   int i;
8641
8642   for (i = 0; i < fp->dep_func_cnt; i++)
8643     if (IS(fp->dep_func[i].name, name))
8644       return &fp->dep_func[i];
8645
8646   return NULL;
8647 }
8648
8649 static void hg_fp_add_dep(struct func_prototype *fp, const char *name,
8650   unsigned int ptr_taken)
8651 {
8652   struct func_proto_dep * dep;
8653
8654   // is it a dupe?
8655   dep = hg_fp_find_dep(fp, name);
8656   if (dep != NULL && dep->ptr_taken == ptr_taken)
8657     return;
8658
8659   if ((fp->dep_func_cnt & 0xff) == 0) {
8660     fp->dep_func = realloc(fp->dep_func,
8661       sizeof(fp->dep_func[0]) * (fp->dep_func_cnt + 0x100));
8662     my_assert_not(fp->dep_func, NULL);
8663     memset(&fp->dep_func[fp->dep_func_cnt], 0,
8664       sizeof(fp->dep_func[0]) * 0x100);
8665   }
8666   fp->dep_func[fp->dep_func_cnt].name = strdup(name);
8667   fp->dep_func[fp->dep_func_cnt].ptr_taken = ptr_taken;
8668   fp->dep_func_cnt++;
8669 }
8670
8671 static int hg_fp_cmp_name(const void *p1_, const void *p2_)
8672 {
8673   const struct func_prototype *p1 = p1_, *p2 = p2_;
8674   return strcmp(p1->name, p2->name);
8675 }
8676
8677 #if 0
8678 static int hg_fp_cmp_id(const void *p1_, const void *p2_)
8679 {
8680   const struct func_prototype *p1 = p1_, *p2 = p2_;
8681   return p1->id - p2->id;
8682 }
8683 #endif
8684
8685 static void hg_ref_add(const char *name)
8686 {
8687   if ((hg_ref_cnt & 0xff) == 0) {
8688     hg_refs = realloc(hg_refs, sizeof(hg_refs[0]) * (hg_ref_cnt + 0x100));
8689     my_assert_not(hg_refs, NULL);
8690     memset(hg_refs + hg_ref_cnt, 0, sizeof(hg_refs[0]) * 0x100);
8691   }
8692
8693   hg_refs[hg_ref_cnt] = strdup(name);
8694   my_assert_not(hg_refs[hg_ref_cnt], NULL);
8695   hg_ref_cnt++;
8696 }
8697
8698 // recursive register dep pass
8699 // - track saved regs (part 2)
8700 // - try to figure out arg-regs
8701 // - calculate reg deps
8702 static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits,
8703   struct func_prototype *fp, int regmask_save, int regmask_dst,
8704   int *regmask_dep, int *regmask_use, int *has_ret)
8705 {
8706   struct func_proto_dep *dep;
8707   struct parsed_op *po;
8708   int from_caller = 0;
8709   int j, l;
8710   int reg;
8711   int ret;
8712
8713   for (; i < opcnt; i++)
8714   {
8715     if (cbits[i >> 3] & (1 << (i & 7)))
8716       return;
8717     cbits[i >> 3] |= (1 << (i & 7));
8718
8719     po = &ops[i];
8720
8721     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
8722       if (po->flags & OPF_RMD)
8723         continue;
8724
8725       if (po->btj != NULL) {
8726         // jumptable
8727         for (j = 0; j < po->btj->count; j++) {
8728           check_i(po, po->btj->d[j].bt_i);
8729           gen_hdr_dep_pass(po->btj->d[j].bt_i, opcnt, cbits, fp,
8730             regmask_save, regmask_dst, regmask_dep, regmask_use,
8731             has_ret);
8732         }
8733         return;
8734       }
8735
8736       check_i(po, po->bt_i);
8737       if (po->flags & OPF_CJMP) {
8738         gen_hdr_dep_pass(po->bt_i, opcnt, cbits, fp,
8739           regmask_save, regmask_dst, regmask_dep, regmask_use,
8740           has_ret);
8741       }
8742       else {
8743         i = po->bt_i - 1;
8744       }
8745       continue;
8746     }
8747
8748     if (po->flags & OPF_FARG)
8749       /* (just calculate register deps) */;
8750     else if (po->op == OP_PUSH && po->operand[0].type == OPT_REG)
8751     {
8752       reg = po->operand[0].reg;
8753       ferr_assert(po, reg >= 0);
8754
8755       if (po->flags & OPF_RSAVE) {
8756         regmask_save |= 1 << reg;
8757         continue;
8758       }
8759       if (po->flags & OPF_DONE)
8760         continue;
8761
8762       ret = scan_for_pop(i + 1, opcnt, i + opcnt * 2,
8763               reg, 0, 0, 0, 0);
8764       if (ret == 1) {
8765         regmask_save |= 1 << reg;
8766         po->flags |= OPF_RMD;
8767         scan_for_pop(i + 1, opcnt, i + opcnt * 3,
8768           reg, 0, 0, 0, OPF_RMD);
8769         continue;
8770       }
8771     }
8772     else if (po->flags & OPF_RMD)
8773       continue;
8774     else if (po->op == OP_CALL) {
8775       po->regmask_dst |= 1 << xAX;
8776
8777       dep = hg_fp_find_dep(fp, po->operand[0].name);
8778       if (dep != NULL) {
8779         dep->regmask_live = regmask_save | regmask_dst;
8780         if (g_bp_frame && !(po->flags & OPF_EBP_S))
8781           dep->regmask_live |= 1 << xBP;
8782       }
8783       if ((po->flags & OPF_TAIL) && po->pp != NULL
8784           && po->pp->is_stdcall)
8785         fp->is_stdcall = 1;
8786     }
8787     else if (po->op == OP_RET) {
8788       if (po->operand_cnt > 0) {
8789         fp->is_stdcall = 1;
8790         if (fp->argc_stack >= 0
8791             && fp->argc_stack != po->operand[0].val / 4)
8792           ferr(po, "ret mismatch? (%d)\n", fp->argc_stack * 4);
8793         fp->argc_stack = po->operand[0].val / 4;
8794       }
8795     }
8796
8797     if (!fp->eax_pass && (po->flags & OPF_TAIL)) {
8798       if (po->op == OP_CALL) {
8799         j = i;
8800         ret = 1;
8801       }
8802       else {
8803         j = -1;
8804         from_caller = 0;
8805         ret = resolve_origin_reg(i, xAX, i + opcnt * 4, &j, &from_caller);
8806       }
8807
8808       if (ret != 1 && from_caller) {
8809         // unresolved eax - probably void func
8810         *has_ret = 0;
8811         fp->eax_pass = 1;
8812       }
8813       else {
8814         if (j >= 0 && ops[j].op == OP_CALL) {
8815           if (ops[j].pp != NULL && !ops[j].pp->is_unresolved) {
8816             int call_has_ret = !IS(ops[j].pp->ret_type.name, "void");
8817             if (ops[j].pp->is_noreturn) {
8818               // could be some fail path
8819               if (*has_ret == -1)
8820                 *has_ret = call_has_ret;
8821             }
8822             else
8823               *has_ret = call_has_ret;
8824           }
8825           else {
8826             dep = hg_fp_find_dep(fp, ops[j].operand[0].name);
8827             if (dep != NULL)
8828               dep->ret_dep = 1;
8829             else
8830               *has_ret = 1;
8831           }
8832         }
8833         else
8834           *has_ret = 1;
8835       }
8836     }
8837
8838     l = regmask_save | regmask_dst;
8839     if (g_bp_frame && !(po->flags & OPF_EBP_S))
8840       l |= 1 << xBP;
8841
8842     l = po->regmask_src & ~l;
8843 #if 0
8844     if (l)
8845       fnote(po, "dep |= %04x, dst %04x, save %04x (f %x)\n",
8846         l, regmask_dst, regmask_save, po->flags);
8847 #endif
8848     *regmask_dep |= l;
8849     *regmask_use |= (po->regmask_src | po->regmask_dst)
8850                   & ~regmask_save;
8851     regmask_dst |= po->regmask_dst;
8852
8853     if (po->flags & OPF_TAIL) {
8854       if (!(po->flags & OPF_CC)) // not cond. tailcall
8855         return;
8856     }
8857   }
8858 }
8859
8860 static void gen_hdr(const char *funcn, int opcnt)
8861 {
8862   unsigned char cbits[MAX_OPS / 8];
8863   const struct parsed_proto *pp_c;
8864   struct parsed_proto *pp;
8865   struct func_prototype *fp;
8866   struct func_proto_dep *dep;
8867   struct parsed_op *po;
8868   const char *tmpname;
8869   int regmask_dummy = 0;
8870   int regmask_dep;
8871   int regmask_use;
8872   int max_bp_offset = 0;
8873   int has_ret;
8874   int i, j, l;
8875   int ret;
8876
8877   pp_c = proto_parse(g_fhdr, funcn, 1);
8878   if (pp_c != NULL)
8879     // already in seed, will add to hg_fp later
8880     return;
8881
8882   fp = hg_fp_add(funcn);
8883
8884   g_bp_frame = g_sp_frame = g_stack_fsz = 0;
8885   g_stack_frame_used = 0;
8886   g_seh_size = 0;
8887
8888   // pass1:
8889   // - resolve all branches
8890   // - parse calls with labels
8891   resolve_branches_parse_calls(opcnt);
8892
8893   // pass2:
8894   // - handle ebp/esp frame, remove ops related to it
8895   scan_prologue_epilogue(opcnt, NULL);
8896
8897   // pass3:
8898   // - remove dead labels
8899   // - collect calls
8900   // - collect function ptr refs
8901   for (i = 0; i < opcnt; i++)
8902   {
8903     if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
8904       free(g_labels[i]);
8905       g_labels[i] = NULL;
8906     }
8907
8908     po = &ops[i];
8909     if (po->flags & (OPF_RMD|OPF_DONE))
8910       continue;
8911
8912     if (po->op == OP_CALL) {
8913       if (po->operand[0].type == OPT_LABEL)
8914         hg_fp_add_dep(fp, opr_name(po, 0), 0);
8915       else if (po->pp != NULL)
8916         hg_fp_add_dep(fp, po->pp->name, 0);
8917     }
8918     else if (po->op == OP_MOV && po->operand[1].type == OPT_OFFSET) {
8919       tmpname = opr_name(po, 1);
8920       if (IS_START(tmpname, "p_") || IS_START(tmpname, "sub_"))
8921         hg_fp_add_dep(fp, tmpname, 1);
8922     }
8923     else if (po->op == OP_PUSH && po->operand[0].type == OPT_OFFSET) {
8924       tmpname = opr_name(po, 0);
8925       if (IS_START(tmpname, "p_") || IS_START(tmpname, "sub_"))
8926         hg_fp_add_dep(fp, tmpname, 1);
8927     }
8928   }
8929
8930   // pass4:
8931   // - handle push <const>/pop pairs
8932   for (i = 0; i < opcnt; i++)
8933   {
8934     po = &ops[i];
8935     if (po->flags & (OPF_RMD|OPF_DONE))
8936       continue;
8937
8938     if (po->op == OP_PUSH && po->operand[0].type == OPT_CONST)
8939       scan_for_pop_const(i, opcnt, i + opcnt * 13);
8940   }
8941
8942   // pass5:
8943   // - process trivial calls
8944   for (i = 0; i < opcnt; i++)
8945   {
8946     po = &ops[i];
8947     if (po->flags & (OPF_RMD|OPF_DONE))
8948       continue;
8949
8950     if (po->op == OP_CALL)
8951     {
8952       pp = process_call_early(i, opcnt, &j);
8953       if (pp != NULL) {
8954         if (!(po->flags & OPF_ATAIL))
8955           // since we know the args, try to collect them
8956           if (collect_call_args_early(i, opcnt, pp, NULL, NULL) != 0)
8957             pp = NULL;
8958       }
8959
8960       if (pp != NULL) {
8961         if (j >= 0) {
8962           // commit esp adjust
8963           if (ops[j].op != OP_POP)
8964             patch_esp_adjust(&ops[j], pp->argc_stack * 4);
8965           else {
8966             for (l = 0; l < pp->argc_stack; l++)
8967               ops[j + l].flags |= OPF_DONE | OPF_RMD | OPF_NOREGS;
8968           }
8969         }
8970
8971         po->flags |= OPF_DONE;
8972       }
8973     }
8974   }
8975
8976   // pass6:
8977   // - track saved regs (simple)
8978   // - process calls
8979   for (i = 0; i < opcnt; i++)
8980   {
8981     po = &ops[i];
8982     if (po->flags & (OPF_RMD|OPF_DONE))
8983       continue;
8984
8985     if (po->op == OP_PUSH && po->operand[0].type == OPT_REG
8986       && po->operand[0].reg != xCX)
8987     {
8988       ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].reg, 0);
8989       if (ret == 1) {
8990         // regmask_save |= 1 << po->operand[0].reg; // do it later
8991         po->flags |= OPF_RSAVE | OPF_RMD | OPF_DONE;
8992         scan_for_pop_ret(i + 1, opcnt, po->operand[0].reg, OPF_RMD);
8993       }
8994     }
8995     else if (po->op == OP_CALL)
8996     {
8997       pp = process_call(i, opcnt);
8998
8999       if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
9000         // since we know the args, collect them
9001         ret = collect_call_args(po, i, opcnt, pp, &regmask_dummy,
9002                 i + opcnt * 1);
9003       }
9004       if (!(po->flags & OPF_TAIL)
9005           && po->operand[0].type == OPT_LABEL)
9006       {
9007         dep = hg_fp_find_dep(fp, opr_name(po, 0));
9008         ferr_assert(po, dep != NULL);
9009         // treat al write as overwrite to avoid many false positives
9010         find_next_read_reg(i + 1, opcnt, xAX, OPLM_BYTE,
9011           i + opcnt * 25, &j);
9012         if (j != -1)
9013           dep->has_ret = 1;
9014         find_next_read_reg(i + 1, opcnt, xDX, OPLM_BYTE,
9015           i + opcnt * 26, &j);
9016         if (j != -1 && !IS_OP_INDIRECT_CALL(&ops[j]))
9017           dep->has_ret64 = 1;
9018       }
9019     }
9020   }
9021
9022   // pass7
9023   memset(cbits, 0, (opcnt + 7) / 8);
9024   regmask_dep = regmask_use = 0;
9025   has_ret = -1;
9026
9027   gen_hdr_dep_pass(0, opcnt, cbits, fp, 0, 0,
9028     &regmask_dep, &regmask_use, &has_ret);
9029
9030   // find unreachable code - must be fixed in IDA
9031   for (i = 0; i < opcnt; i++)
9032   {
9033     if (cbits[i >> 3] & (1 << (i & 7)))
9034       continue;
9035
9036     if (g_labels[i] == NULL && i > 0 && ops[i - 1].op == OP_CALL
9037       && ops[i - 1].pp != NULL && ops[i - 1].pp->is_osinc)
9038     {
9039       // the compiler sometimes still generates code after
9040       // noreturn OS functions
9041       break;
9042     }
9043     if (!(ops[i].flags & OPF_RMD)
9044         && ops[i].op != OP_NOP && ops[i].op != OPP_ABORT)
9045     {
9046       ferr(&ops[i], "unreachable code\n");
9047     }
9048   }
9049
9050   for (i = 0; i < g_eqcnt; i++) {
9051     if (g_eqs[i].offset > max_bp_offset && g_eqs[i].offset < 4*32)
9052       max_bp_offset = g_eqs[i].offset;
9053   }
9054
9055   if (fp->argc_stack < 0) {
9056     max_bp_offset = (max_bp_offset + 3) & ~3;
9057     fp->argc_stack = max_bp_offset / 4;
9058     if ((g_ida_func_attr & IDAFA_BP_FRAME) && fp->argc_stack > 0)
9059       fp->argc_stack--;
9060   }
9061
9062   fp->regmask_dep = regmask_dep & ~((1 << xSP) | mxSTa);
9063   fp->regmask_use = regmask_use;
9064   fp->has_ret = has_ret;
9065 #if 0
9066   printf("// has_ret %d, regmask_dep %x\n",
9067     fp->has_ret, fp->regmask_dep);
9068   output_hdr_fp(stdout, fp, 1);
9069   if (IS(funcn, "sub_10007F72")) exit(1);
9070 #endif
9071
9072   gen_x_cleanup(opcnt);
9073 }
9074
9075 static void hg_fp_resolve_deps(struct func_prototype *fp)
9076 {
9077   struct func_prototype fp_s;
9078   struct func_proto_dep *dep;
9079   int regmask_dep;
9080   int i;
9081
9082   // this thing is recursive, so mark first..
9083   fp->dep_resolved = 1;
9084
9085   for (i = 0; i < fp->dep_func_cnt; i++) {
9086     dep = &fp->dep_func[i];
9087
9088     strcpy(fp_s.name, dep->name);
9089     dep->proto = bsearch(&fp_s, hg_fp, hg_fp_cnt,
9090       sizeof(hg_fp[0]), hg_fp_cmp_name);
9091     if (dep->proto != NULL) {
9092       if (dep->ptr_taken) {
9093         dep->proto->ptr_taken = 1;
9094         continue;
9095       }
9096
9097       if (!dep->proto->dep_resolved)
9098         hg_fp_resolve_deps(dep->proto);
9099
9100       regmask_dep = ~dep->regmask_live
9101                    & dep->proto->regmask_dep;
9102       fp->regmask_dep |= regmask_dep;
9103       // printf("dep %s %s |= %x\n", fp->name,
9104       //   fp->dep_func[i].name, regmask_dep);
9105
9106       if (dep->has_ret && (dep->proto->regmask_use & mxAX))
9107         dep->proto->has_ret = 1;
9108       if (dep->has_ret64 && (dep->proto->regmask_use & mxDX))
9109         dep->proto->has_ret64 = 1;
9110       if (fp->has_ret == -1 && dep->ret_dep)
9111         fp->has_ret = dep->proto->has_ret;
9112     }
9113   }
9114 }
9115
9116 // make all thiscall/edx arg functions referenced from .data fastcall
9117 static void do_func_refs_from_data(void)
9118 {
9119   struct func_prototype *fp, fp_s;
9120   int i;
9121
9122   for (i = 0; i < hg_ref_cnt; i++) {
9123     strcpy(fp_s.name, hg_refs[i]);
9124     fp = bsearch(&fp_s, hg_fp, hg_fp_cnt,
9125       sizeof(hg_fp[0]), hg_fp_cmp_name);
9126     if (fp != NULL)
9127       fp->ptr_taken = 1;
9128   }
9129 }
9130
9131 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
9132   int count)
9133 {
9134   const struct parsed_proto *pp;
9135   char *p, namebuf[NAMELEN];
9136   const char *name;
9137   int regmask_dep;
9138   int argc_normal;
9139   int j, arg;
9140
9141   for (; count > 0; count--, fp++) {
9142     if (fp->has_ret == -1)
9143       fprintf(fout, "// ret unresolved\n");
9144 #if 0
9145     fprintf(fout, "// dep:");
9146     for (j = 0; j < fp->dep_func_cnt; j++) {
9147       fprintf(fout, " %s/", fp->dep_func[j].name);
9148       if (fp->dep_func[j].proto != NULL)
9149         fprintf(fout, "%04x/%d", fp->dep_func[j].proto->regmask_dep,
9150           fp->dep_func[j].proto->has_ret);
9151     }
9152     fprintf(fout, "\n");
9153 #endif
9154
9155     p = strchr(fp->name, '@');
9156     if (p != NULL) {
9157       memcpy(namebuf, fp->name, p - fp->name);
9158       namebuf[p - fp->name] = 0;
9159       name = namebuf;
9160     }
9161     else
9162       name = fp->name;
9163     if (name[0] == '_')
9164       name++;
9165
9166     pp = proto_parse(g_fhdr, name, 1);
9167     if (pp != NULL && pp->is_include)
9168       continue;
9169
9170     if (fp->pp != NULL) {
9171       // part of seed, output later
9172       continue;
9173     }
9174
9175     regmask_dep = fp->regmask_dep;
9176     argc_normal = fp->argc_stack;
9177     if (fp->ptr_taken && regmask_dep
9178         && (regmask_dep & ~(mxCX|mxDX)) == 0)
9179     {
9180       if ((regmask_dep & mxDX) || fp->argc_stack > 0)
9181         regmask_dep |= mxCX | mxDX;
9182     }
9183
9184     fprintf(fout, "%-5s",
9185       fp->pp ? fp->pp->ret_type.name :
9186       fp->has_ret64 ? "__int64" :
9187       fp->has_ret ? "int" : "void");
9188     if (regmask_dep == mxCX && fp->is_stdcall && fp->argc_stack > 0) {
9189       fprintf(fout, "/*__thiscall*/  ");
9190       argc_normal++;
9191       regmask_dep = 0;
9192     }
9193     else if ((regmask_dep == (mxCX | mxDX)
9194               && (fp->is_stdcall || fp->argc_stack == 0))
9195       || (regmask_dep == mxCX && fp->argc_stack == 0))
9196     {
9197       fprintf(fout, "  __fastcall    ");
9198       if (!(regmask_dep & (1 << xDX)) && fp->argc_stack == 0)
9199         argc_normal = 1;
9200       else
9201         argc_normal += 2;
9202       regmask_dep = 0;
9203     }
9204     else if (regmask_dep && !fp->is_stdcall) {
9205       fprintf(fout, "/*__usercall*/  ");
9206     }
9207     else if (regmask_dep) {
9208       fprintf(fout, "/*__userpurge*/ ");
9209     }
9210     else if (fp->is_stdcall)
9211       fprintf(fout, "  __stdcall     ");
9212     else
9213       fprintf(fout, "  __cdecl       ");
9214
9215     fprintf(fout, "%s(", name);
9216
9217     arg = 0;
9218     for (j = 0; j < xSP; j++) {
9219       if (regmask_dep & (1 << j)) {
9220         arg++;
9221         if (arg != 1)
9222           fprintf(fout, ", ");
9223         if (fp->pp != NULL)
9224           fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name);
9225         else
9226           fprintf(fout, "int");
9227         fprintf(fout, " a%d/*<%s>*/", arg, regs_r32[j]);
9228       }
9229     }
9230
9231     for (j = 0; j < argc_normal; j++) {
9232       arg++;
9233       if (arg != 1)
9234         fprintf(fout, ", ");
9235       if (fp->pp != NULL) {
9236         fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name);
9237         if (!fp->pp->arg[arg - 1].type.is_ptr)
9238           fprintf(fout, " ");
9239       }
9240       else
9241         fprintf(fout, "int ");
9242       fprintf(fout, "a%d", arg);
9243     }
9244
9245     fprintf(fout, ");\n");
9246   }
9247 }
9248
9249 static void output_hdr(FILE *fout)
9250 {
9251   static const char *lmod_c_names[] = {
9252     [OPLM_UNSPEC] = "???",
9253     [OPLM_BYTE]  = "uint8_t",
9254     [OPLM_WORD]  = "uint16_t",
9255     [OPLM_DWORD] = "uint32_t",
9256     [OPLM_QWORD] = "uint64_t",
9257   };
9258   const struct scanned_var *var;
9259   struct func_prototype *fp;
9260   char line[256] = { 0, };
9261   char name[256];
9262   int i;
9263
9264   // add stuff from headers
9265   for (i = 0; i < pp_cache_size; i++) {
9266     if (pp_cache[i].is_cinc && !pp_cache[i].is_stdcall)
9267       snprintf(name, sizeof(name), "_%s", pp_cache[i].name);
9268     else
9269       snprintf(name, sizeof(name), "%s", pp_cache[i].name);
9270     fp = hg_fp_add(name);
9271     fp->pp = &pp_cache[i];
9272     fp->argc_stack = fp->pp->argc_stack;
9273     fp->is_stdcall = fp->pp->is_stdcall;
9274     fp->regmask_dep = get_pp_arg_regmask_src(fp->pp);
9275     fp->has_ret = !IS(fp->pp->ret_type.name, "void");
9276   }
9277
9278   // resolve deps
9279   qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_name);
9280   for (i = 0; i < hg_fp_cnt; i++)
9281     hg_fp_resolve_deps(&hg_fp[i]);
9282
9283   // adjust functions referenced from data segment
9284   do_func_refs_from_data();
9285
9286   // final adjustments
9287   for (i = 0; i < hg_fp_cnt; i++) {
9288     if (hg_fp[i].eax_pass && (hg_fp[i].regmask_dep & mxAX))
9289       hg_fp[i].has_ret = 1;
9290   }
9291
9292   // note: messes up .proto ptr, don't use
9293   //qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_id);
9294
9295   // output variables
9296   for (i = 0; i < hg_var_cnt; i++) {
9297     var = &hg_vars[i];
9298
9299     if (var->pp != NULL)
9300       // part of seed
9301       continue;
9302     else if (var->is_c_str)
9303       fprintf(fout, "extern %-8s %s[];", "char", var->name);
9304     else
9305       fprintf(fout, "extern %-8s %s;",
9306         lmod_c_names[var->lmod], var->name);
9307
9308     if (var->is_seeded)
9309       fprintf(fout, " // seeded");
9310     fprintf(fout, "\n");
9311   }
9312
9313   fprintf(fout, "\n");
9314
9315   // output function prototypes
9316   output_hdr_fp(fout, hg_fp, hg_fp_cnt);
9317
9318   // seed passthrough
9319   fprintf(fout, "\n// - seed -\n");
9320
9321   rewind(g_fhdr);
9322   while (fgets(line, sizeof(line), g_fhdr))
9323     fwrite(line, 1, strlen(line), fout);
9324 }
9325
9326 // '=' needs special treatment
9327 // also ' quote
9328 static char *next_word_s(char *w, size_t wsize, char *s)
9329 {
9330   size_t i;
9331
9332   s = sskip(s);
9333
9334   i = 0;
9335   if (*s == '\'' && s[1] != '\r' && s[1] != '\n') {
9336     w[0] = s[0];
9337     for (i = 1; i < wsize - 1; i++) {
9338       if (s[i] == 0) {
9339         printf("warning: missing closing quote: \"%s\"\n", s);
9340         break;
9341       }
9342       if (s[i] == '\'')
9343         break;
9344       w[i] = s[i];
9345     }
9346   }
9347
9348   for (; i < wsize - 1; i++) {
9349     if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
9350       break;
9351     w[i] = s[i];
9352   }
9353   w[i] = 0;
9354
9355   if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
9356     printf("warning: '%s' truncated\n", w);
9357
9358   return s + i;
9359 }
9360
9361 static int cmpstringp(const void *p1, const void *p2)
9362 {
9363   return strcmp(*(char * const *)p1, *(char * const *)p2);
9364 }
9365
9366 static int is_xref_needed(char *p, char **rlist, int rlist_len)
9367 {
9368   char *p2;
9369
9370   p = sskip(p);
9371   if (strstr(p, "..."))
9372     // unable to determine, assume needed
9373     return 1;
9374
9375   if (*p == '.') // .text, .data, ...
9376     // ref from other data or non-function -> no
9377     return 0;
9378
9379   p2 = strpbrk(p, "+:\r\n\x18");
9380   if (p2 != NULL)
9381     *p2 = 0;
9382   if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
9383     // referenced from removed code
9384     return 0;
9385
9386   return 1;
9387 }
9388
9389 static int ida_xrefs_show_need(FILE *fasm, char *p,
9390   char **rlist, int rlist_len)
9391 {
9392   int found_need = 0;
9393   char line[256];
9394   long pos;
9395
9396   p = strrchr(p, ';');
9397   if (p != NULL && *p == ';') {
9398     if (IS_START(p + 2, "sctref"))
9399       return 1;
9400     if (IS_START(p + 2, "DATA XREF: ")) {
9401       p += 13;
9402       if (is_xref_needed(p, rlist, rlist_len))
9403         return 1;
9404     }
9405   }
9406
9407   pos = ftell(fasm);
9408   while (1)
9409   {
9410     if (!my_fgets(line, sizeof(line), fasm))
9411       break;
9412     // non-first line is always indented
9413     if (!my_isblank(line[0]))
9414       break;
9415
9416     // should be no content, just comment
9417     p = sskip(line);
9418     if (*p != ';')
9419       break;
9420
9421     p = strrchr(p, ';');
9422     p += 2;
9423
9424     if (IS_START(p, "sctref")) {
9425       found_need = 1;
9426       break;
9427     }
9428
9429     // it's printed once, but no harm to check again
9430     if (IS_START(p, "DATA XREF: "))
9431       p += 11;
9432
9433     if (is_xref_needed(p, rlist, rlist_len)) {
9434       found_need = 1;
9435       break;
9436     }
9437   }
9438   fseek(fasm, pos, SEEK_SET);
9439   return found_need;
9440 }
9441
9442 static void scan_variables(FILE *fasm, char **rlist, int rlist_len)
9443 {
9444   struct scanned_var *var;
9445   char line[256] = { 0, };
9446   char words[4][256];
9447   int no_identifier;
9448   char *p = NULL;
9449   int wordc;
9450   int l;
9451
9452   while (!feof(fasm))
9453   {
9454     // skip to next data section
9455     while (my_fgets(line, sizeof(line), fasm))
9456     {
9457       asmln++;
9458
9459       p = sskip(line);
9460       if (*p == 0 || *p == ';')
9461         continue;
9462
9463       p = sskip(next_word_s(words[0], sizeof(words[0]), p));
9464       if (*p == 0 || *p == ';')
9465         continue;
9466
9467       if (*p != 's' || !IS_START(p, "segment para public"))
9468         continue;
9469
9470       break;
9471     }
9472
9473     if (p == NULL || !IS_START(p, "segment para public"))
9474       break;
9475     p = sskip(p + 19);
9476
9477     if (!IS_START(p, "'DATA'"))
9478       continue;
9479
9480     // now process it
9481     while (my_fgets(line, sizeof(line), fasm))
9482     {
9483       asmln++;
9484
9485       p = line;
9486       no_identifier = my_isblank(*p);
9487
9488       p = sskip(p);
9489       if (*p == 0 || *p == ';')
9490         continue;
9491
9492       for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
9493         words[wordc][0] = 0;
9494         p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
9495         if (*p == 0 || *p == ';') {
9496           wordc++;
9497           break;
9498         }
9499       }
9500
9501       if (wordc == 2 && IS(words[1], "ends"))
9502         break;
9503       if (wordc < 2)
9504         continue;
9505
9506       if (no_identifier) {
9507         if (wordc >= 3 && IS(words[0], "dd") && IS(words[1], "offset"))
9508           hg_ref_add(words[2]);
9509         continue;
9510       }
9511
9512       if (IS_START(words[0], "__IMPORT_DESCRIPTOR_")) {
9513         // when this starts, we don't need anything from this section
9514         break;
9515       }
9516
9517       // check refs comment(s)
9518       if (!ida_xrefs_show_need(fasm, p, rlist, rlist_len))
9519         continue;
9520
9521       if ((hg_var_cnt & 0xff) == 0) {
9522         hg_vars = realloc(hg_vars, sizeof(hg_vars[0])
9523                    * (hg_var_cnt + 0x100));
9524         my_assert_not(hg_vars, NULL);
9525         memset(hg_vars + hg_var_cnt, 0, sizeof(hg_vars[0]) * 0x100);
9526       }
9527
9528       var = &hg_vars[hg_var_cnt++];
9529       snprintf(var->name, sizeof(var->name), "%s", words[0]);
9530
9531       // maybe already in seed header?
9532       var->pp = proto_parse(g_fhdr, var->name, 1);
9533       if (var->pp != NULL) {
9534         if (var->pp->is_fptr) {
9535           var->lmod = OPLM_DWORD;
9536           //var->is_ptr = 1;
9537         }
9538         else if (var->pp->is_func)
9539           aerr("func?\n");
9540         else if (!guess_lmod_from_c_type(&var->lmod, &var->pp->type))
9541           aerr("unhandled C type '%s' for '%s'\n",
9542             var->pp->type.name, var->name);
9543
9544         var->is_seeded = 1;
9545         continue;
9546       }
9547
9548       if      (IS(words[1], "dd")) {
9549         var->lmod = OPLM_DWORD;
9550         if (wordc >= 4 && IS(words[2], "offset"))
9551           hg_ref_add(words[3]);
9552       }
9553       else if (IS(words[1], "dw"))
9554         var->lmod = OPLM_WORD;
9555       else if (IS(words[1], "db")) {
9556         var->lmod = OPLM_BYTE;
9557         if (wordc >= 3 && (l = strlen(words[2])) > 4) {
9558           if (words[2][0] == '\'' && IS(words[2] + l - 2, ",0"))
9559             var->is_c_str = 1;
9560         }
9561       }
9562       else if (IS(words[1], "dq"))
9563         var->lmod = OPLM_QWORD;
9564       //else if (IS(words[1], "dt"))
9565       else
9566         aerr("type '%s' not known\n", words[1]);
9567     }
9568   }
9569
9570   rewind(fasm);
9571   asmln = 0;
9572 }
9573
9574 static void set_label(int i, const char *name)
9575 {
9576   const char *p;
9577   int len;
9578
9579   len = strlen(name);
9580   p = strchr(name, ':');
9581   if (p != NULL)
9582     len = p - name;
9583
9584   if (g_labels[i] != NULL && !IS_START(g_labels[i], "algn_"))
9585     aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
9586   g_labels[i] = realloc(g_labels[i], len + 1);
9587   my_assert_not(g_labels[i], NULL);
9588   memcpy(g_labels[i], name, len);
9589   g_labels[i][len] = 0;
9590 }
9591
9592 struct chunk_item {
9593   char *name;
9594   long fptr;
9595   int asmln;
9596 };
9597
9598 static struct chunk_item *func_chunks;
9599 static int func_chunk_cnt;
9600 static int func_chunk_alloc;
9601
9602 static void add_func_chunk(FILE *fasm, const char *name, int line)
9603 {
9604   if (func_chunk_cnt >= func_chunk_alloc) {
9605     func_chunk_alloc *= 2;
9606     func_chunks = realloc(func_chunks,
9607       func_chunk_alloc * sizeof(func_chunks[0]));
9608     my_assert_not(func_chunks, NULL);
9609   }
9610   func_chunks[func_chunk_cnt].fptr = ftell(fasm);
9611   func_chunks[func_chunk_cnt].name = strdup(name);
9612   func_chunks[func_chunk_cnt].asmln = line;
9613   func_chunk_cnt++;
9614 }
9615
9616 static int cmp_chunks(const void *p1, const void *p2)
9617 {
9618   const struct chunk_item *c1 = p1, *c2 = p2;
9619   return strcmp(c1->name, c2->name);
9620 }
9621
9622 static void scan_ahead_for_chunks(FILE *fasm)
9623 {
9624   char words[2][256];
9625   char line[256];
9626   long oldpos;
9627   int oldasmln;
9628   int wordc;
9629   char *p;
9630   int i;
9631
9632   oldpos = ftell(fasm);
9633   oldasmln = asmln;
9634
9635   while (my_fgets(line, sizeof(line), fasm))
9636   {
9637     wordc = 0;
9638     asmln++;
9639
9640     p = sskip(line);
9641     if (*p == 0)
9642       continue;
9643
9644     if (*p == ';')
9645     {
9646       // get rid of random tabs
9647       for (i = 0; line[i] != 0; i++)
9648         if (line[i] == '\t')
9649           line[i] = ' ';
9650
9651       if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
9652       {
9653         p += 30;
9654         next_word(words[0], sizeof(words[0]), p);
9655         if (words[0][0] == 0)
9656           aerr("missing name for func chunk?\n");
9657
9658         add_func_chunk(fasm, words[0], asmln);
9659       }
9660       else if (IS_START(p, "; sctend"))
9661         break;
9662
9663       continue;
9664     } // *p == ';'
9665
9666     for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
9667       words[wordc][0] = 0;
9668       p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
9669       if (*p == 0 || *p == ';') {
9670         wordc++;
9671         break;
9672       }
9673     }
9674
9675     if (wordc == 2 && IS(words[1], "ends"))
9676       break;
9677   }
9678
9679   fseek(fasm, oldpos, SEEK_SET);
9680   asmln = oldasmln;
9681 }
9682
9683 int main(int argc, char *argv[])
9684 {
9685   FILE *fout, *fasm, *frlist;
9686   struct parsed_data *pd = NULL;
9687   int pd_alloc = 0;
9688   char **rlist = NULL;
9689   int rlist_len = 0;
9690   int rlist_alloc = 0;
9691   int func_chunks_used = 0;
9692   int func_chunks_sorted = 0;
9693   int func_chunk_i = -1;
9694   long func_chunk_ret = 0;
9695   int func_chunk_ret_ln = 0;
9696   int scanned_ahead = 0;
9697   char line[256];
9698   char words[20][256];
9699   enum opr_lenmod lmod;
9700   char *sctproto = NULL;
9701   int in_func = 0;
9702   int pending_endp = 0;
9703   int skip_code = 0;
9704   int skip_code_end = 0;
9705   int skip_warned = 0;
9706   int eq_alloc;
9707   int verbose = 0;
9708   int multi_seg = 0;
9709   int end = 0;
9710   int arg_out;
9711   int arg;
9712   int pi = 0;
9713   int i, j;
9714   int ret, len;
9715   char *p, *p2;
9716   int wordc;
9717
9718   for (arg = 1; arg < argc; arg++) {
9719     if (IS(argv[arg], "-v"))
9720       verbose = 1;
9721     else if (IS(argv[arg], "-rf"))
9722       g_allow_regfunc = 1;
9723     else if (IS(argv[arg], "-uc"))
9724       g_allow_user_icall = 1;
9725     else if (IS(argv[arg], "-wu"))
9726       g_nowarn_reguse = 1;
9727     else if (IS(argv[arg], "-m"))
9728       multi_seg = 1;
9729     else if (IS(argv[arg], "-hdr"))
9730       g_header_mode = g_quiet_pp = g_allow_regfunc = 1;
9731     else
9732       break;
9733   }
9734
9735   if (argc < arg + 3) {
9736     printf("usage:\n%s [options] <.c> <.asm> <hdr.h> [rlist]*\n"
9737            "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n"
9738            "options:\n"
9739            "  -hdr - header generation mode\n"
9740            "  -rf  - allow unannotated indirect calls\n"
9741            "  -uc  - allow ind. calls/refs to __usercall\n"
9742            "  -m   - allow multiple .text sections\n"
9743            "  -wu  - don't warn about bad reg use\n"
9744            "[rlist] is a file with function names to skip,"
9745            " one per line\n",
9746       argv[0], argv[0]);
9747     return 1;
9748   }
9749
9750   arg_out = arg++;
9751
9752   asmfn = argv[arg++];
9753   fasm = fopen(asmfn, "r");
9754   my_assert_not(fasm, NULL);
9755
9756   hdrfn = argv[arg++];
9757   g_fhdr = fopen(hdrfn, "r");
9758   my_assert_not(g_fhdr, NULL);
9759
9760   rlist_alloc = 64;
9761   rlist = malloc(rlist_alloc * sizeof(rlist[0]));
9762   my_assert_not(rlist, NULL);
9763   // needs special handling..
9764   rlist[rlist_len++] = "__alloca_probe";
9765
9766   func_chunk_alloc = 32;
9767   func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
9768   my_assert_not(func_chunks, NULL);
9769
9770   memset(words, 0, sizeof(words));
9771
9772   for (; arg < argc; arg++) {
9773     int skip_func = 0;
9774
9775     frlist = fopen(argv[arg], "r");
9776     my_assert_not(frlist, NULL);
9777
9778     while (my_fgets(line, sizeof(line), frlist)) {
9779       p = sskip(line);
9780       if (*p == 0 || *p == ';')
9781         continue;
9782       if (*p == '#') {
9783         if (IS_START(p, "#if 0")
9784          || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
9785         {
9786           skip_func = 1;
9787         }
9788         else if (IS_START(p, "#endif"))
9789           skip_func = 0;
9790         continue;
9791       }
9792       if (skip_func)
9793         continue;
9794
9795       p = next_word(words[0], sizeof(words[0]), p);
9796       if (words[0][0] == 0)
9797         continue;
9798
9799       if (rlist_len >= rlist_alloc) {
9800         rlist_alloc = rlist_alloc * 2 + 64;
9801         rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
9802         my_assert_not(rlist, NULL);
9803       }
9804       rlist[rlist_len++] = strdup(words[0]);
9805     }
9806
9807     fclose(frlist);
9808     frlist = NULL;
9809   }
9810
9811   if (rlist_len > 0)
9812     qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
9813
9814   fout = fopen(argv[arg_out], "w");
9815   my_assert_not(fout, NULL);
9816
9817   eq_alloc = 128;
9818   g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
9819   my_assert_not(g_eqs, NULL);
9820
9821   for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
9822     g_label_refs[i].i = -1;
9823     g_label_refs[i].next = NULL;
9824   }
9825
9826   if (g_header_mode)
9827     scan_variables(fasm, rlist, rlist_len);
9828
9829   while (my_fgets(line, sizeof(line), fasm))
9830   {
9831     wordc = 0;
9832     asmln++;
9833
9834     p = sskip(line);
9835     if (*p == 0)
9836       continue;
9837
9838     // get rid of random tabs
9839     for (i = 0; line[i] != 0; i++)
9840       if (line[i] == '\t')
9841         line[i] = ' ';
9842
9843     if (*p == ';')
9844     {
9845       if (p[2] == '=' && IS_START(p, "; =============== S U B"))
9846         goto do_pending_endp; // eww..
9847
9848       if (p[2] == 'A' && IS_START(p, "; Attributes:"))
9849       {
9850         static const char *attrs[] = {
9851           "bp-based frame",
9852           "library function",
9853           "static",
9854           "noreturn",
9855           "thunk",
9856           "fpd=",
9857         };
9858
9859         // parse IDA's attribute-list comment
9860         g_ida_func_attr = 0;
9861         p = sskip(p + 13);
9862
9863         for (; *p != 0; p = sskip(p)) {
9864           for (i = 0; i < ARRAY_SIZE(attrs); i++) {
9865             if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
9866               g_ida_func_attr |= 1 << i;
9867               p += strlen(attrs[i]);
9868               break;
9869             }
9870           }
9871           if (i == ARRAY_SIZE(attrs)) {
9872             anote("unparsed IDA attr: %s\n", p);
9873             break;
9874           }
9875           if (IS(attrs[i], "fpd=")) {
9876             p = next_word(words[0], sizeof(words[0]), p);
9877             // ignore for now..
9878           }
9879         }
9880       }
9881       else if (p[2] == 's' && IS_START(p, "; sctattr:"))
9882       {
9883         static const char *attrs[] = {
9884           "clear_sf",
9885           "clear_regmask",
9886           "rm_regmask",
9887           "nowarn",
9888           "argframe",
9889           "align_float",
9890         };
9891
9892         // parse manual attribute-list comment
9893         g_sct_func_attr = 0;
9894         p = sskip(p + 10);
9895
9896         for (; *p != 0; p = sskip(p)) {
9897           for (i = 0; i < ARRAY_SIZE(attrs); i++) {
9898             if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
9899               g_sct_func_attr |= 1 << i;
9900               p += strlen(attrs[i]);
9901               break;
9902             }
9903           }
9904           if (*p == '=') {
9905             j = ret = 0;
9906             if (i == 0)
9907               // clear_sf=start,len (in dwords)
9908               ret = sscanf(p, "=%d,%d%n", &g_stack_clear_start,
9909                       &g_stack_clear_len, &j);
9910             else if (i == 1)
9911               // clear_regmask=<mask>
9912               ret = sscanf(p, "=%x%n", &g_regmask_init, &j) + 1;
9913             else if (i == 2)
9914               // rm_regmask=<mask>
9915               ret = sscanf(p, "=%x%n", &g_regmask_rm, &j) + 1;
9916             if (ret < 2) {
9917               anote("unparsed attr value: %s\n", p);
9918               break;
9919             }
9920             p += j;
9921           }
9922           else if (i == ARRAY_SIZE(attrs)) {
9923             anote("unparsed sct attr: %s\n", p);
9924             break;
9925           }
9926         }
9927       }
9928       else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
9929       {
9930         p += 30;
9931         next_word(words[0], sizeof(words[0]), p);
9932         if (words[0][0] == 0)
9933           aerr("missing name for func chunk?\n");
9934
9935         if (!scanned_ahead) {
9936           add_func_chunk(fasm, words[0], asmln);
9937           func_chunks_sorted = 0;
9938         }
9939       }
9940       else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
9941       {
9942         if (func_chunk_i >= 0) {
9943           if (func_chunk_i < func_chunk_cnt
9944             && IS(func_chunks[func_chunk_i].name, g_func))
9945           {
9946             // move on to next chunk
9947             ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
9948             if (ret)
9949               aerr("seek failed for '%s' chunk #%d\n",
9950                 g_func, func_chunk_i);
9951             asmln = func_chunks[func_chunk_i].asmln;
9952             func_chunk_i++;
9953           }
9954           else {
9955             if (func_chunk_ret == 0)
9956               aerr("no return from chunk?\n");
9957             fseek(fasm, func_chunk_ret, SEEK_SET);
9958             asmln = func_chunk_ret_ln;
9959             func_chunk_ret = 0;
9960             pending_endp = 1;
9961           }
9962         }
9963       }
9964       else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
9965         func_chunks_used = 1;
9966         p += 20;
9967         if (IS_START(g_func, "sub_")) {
9968           unsigned long addr = strtoul(p, NULL, 16);
9969           unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
9970           if (addr > f_addr && !scanned_ahead) {
9971             //anote("scan_ahead caused by '%s', addr %lx\n",
9972             //  g_func, addr);
9973             scan_ahead_for_chunks(fasm);
9974             scanned_ahead = 1;
9975             func_chunks_sorted = 0;
9976           }
9977         }
9978       }
9979       continue;
9980     } // *p == ';'
9981
9982 parse_words:
9983     for (i = wordc; i < ARRAY_SIZE(words); i++)
9984       words[i][0] = 0;
9985     for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
9986       p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
9987       if (*p == 0 || *p == ';') {
9988         wordc++;
9989         break;
9990       }
9991     }
9992     if (*p != 0 && *p != ';')
9993       aerr("too many words\n");
9994
9995     if (skip_code_end) {
9996       skip_code_end = 0;
9997       skip_code = 0;
9998     }
9999
10000     // allow asm patches in comments
10001     if (*p == ';') {
10002       // skip IDA's forced non-removable comment
10003       if (!IS_START(p, "; sct") && (p2 = strchr(p + 1, ';')))
10004         p = p2;
10005     }
10006     if (*p == ';' && IS_START(p, "; sct")) {
10007       if (IS_START(p, "; sctpatch:")) {
10008         p = sskip(p + 11);
10009         if (*p == 0 || *p == ';')
10010           continue;
10011         goto parse_words; // lame
10012       }
10013       else if (IS_START(p, "; sctend")) {
10014         end = 1;
10015         if (!pending_endp)
10016           break;
10017       }
10018       else if (g_skip_func)
10019         /* ignore remaining attrs */;
10020       else if (IS_START(p, "; sctproto:")) {
10021         sctproto = strdup(p + 11);
10022       }
10023       else if (IS_START(p, "; sctskip_start")) {
10024         if (in_func) {
10025           if (!skip_code) {
10026             ops[pi].op = OPP_ABORT;
10027             ops[pi].asmln = asmln;
10028             pi++;
10029           }
10030           skip_code = 1;
10031         }
10032       }
10033       else if (IS_START(p, "; sctskip_end")) {
10034         if (skip_code)
10035           skip_code_end = 1;
10036       }
10037     }
10038
10039     if (wordc == 0) {
10040       // shouldn't happen
10041       awarn("wordc == 0?\n");
10042       continue;
10043     }
10044
10045     // don't care about this:
10046     if (words[0][0] == '.'
10047         || IS(words[0], "include")
10048         || IS(words[0], "assume") || IS(words[1], "segment")
10049         || IS(words[0], "align"))
10050     {
10051       continue;
10052     }
10053
10054 do_pending_endp:
10055     // do delayed endp processing to collect switch jumptables
10056     if (pending_endp) {
10057       if (in_func && !g_skip_func && !end && wordc >= 2
10058           && ((words[0][0] == 'd' && words[0][2] == 0)
10059               || (words[1][0] == 'd' && words[1][2] == 0)))
10060       {
10061         i = 1;
10062         if (words[1][0] == 'd' && words[1][2] == 0) {
10063           // label
10064           if (g_func_pd_cnt >= pd_alloc) {
10065             pd_alloc = pd_alloc * 2 + 16;
10066             g_func_pd = realloc(g_func_pd,
10067               sizeof(g_func_pd[0]) * pd_alloc);
10068             my_assert_not(g_func_pd, NULL);
10069           }
10070           pd = &g_func_pd[g_func_pd_cnt];
10071           g_func_pd_cnt++;
10072           memset(pd, 0, sizeof(*pd));
10073           strcpy(pd->label, words[0]);
10074           pd->type = OPT_CONST;
10075           pd->lmod = lmod_from_directive(words[1]);
10076           i = 2;
10077         }
10078         else {
10079           if (pd == NULL) {
10080             if (verbose)
10081               anote("skipping alignment byte?\n");
10082             continue;
10083           }
10084           lmod = lmod_from_directive(words[0]);
10085           if (lmod != pd->lmod)
10086             aerr("lmod change? %d->%d\n", pd->lmod, lmod);
10087         }
10088
10089         if (pd->count_alloc < pd->count + wordc) {
10090           pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
10091           pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
10092           my_assert_not(pd->d, NULL);
10093         }
10094         for (; i < wordc; i++) {
10095           if (IS(words[i], "offset")) {
10096             pd->type = OPT_OFFSET;
10097             i++;
10098           }
10099           p = strchr(words[i], ',');
10100           if (p != NULL)
10101             *p = 0;
10102           if (pd->type == OPT_OFFSET)
10103             pd->d[pd->count].u.label = strdup(words[i]);
10104           else
10105             pd->d[pd->count].u.val = parse_number(words[i], 0);
10106           pd->d[pd->count].bt_i = -1;
10107           pd->count++;
10108         }
10109         continue;
10110       }
10111
10112       if (in_func && !g_skip_func) {
10113         if (g_header_mode)
10114           gen_hdr(g_func, pi);
10115         else
10116           gen_func(fout, g_fhdr, g_func, pi);
10117       }
10118
10119       pending_endp = 0;
10120       in_func = 0;
10121       g_ida_func_attr = 0;
10122       g_sct_func_attr = 0;
10123       g_stack_clear_start = 0;
10124       g_stack_clear_len = 0;
10125       g_regmask_init = 0;
10126       g_regmask_rm = 0;
10127       skip_warned = 0;
10128       g_skip_func = 0;
10129       g_func[0] = 0;
10130       g_seh_found = 0;
10131       func_chunks_used = 0;
10132       func_chunk_i = -1;
10133       if (pi != 0) {
10134         memset(&ops, 0, pi * sizeof(ops[0]));
10135         clear_labels(pi);
10136         pi = 0;
10137       }
10138       g_eqcnt = 0;
10139       for (i = 0; i < g_func_pd_cnt; i++) {
10140         pd = &g_func_pd[i];
10141         if (pd->type == OPT_OFFSET) {
10142           for (j = 0; j < pd->count; j++)
10143             free(pd->d[j].u.label);
10144         }
10145         free(pd->d);
10146         pd->d = NULL;
10147       }
10148       g_func_pd_cnt = 0;
10149       g_func_lmods = 0;
10150       pd = NULL;
10151
10152       if (end)
10153         break;
10154       if (wordc == 0)
10155         continue;
10156     }
10157
10158     if (IS(words[1], "proc")) {
10159       if (in_func)
10160         aerr("proc '%s' while in_func '%s'?\n",
10161           words[0], g_func);
10162       p = words[0];
10163       if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
10164         g_skip_func = 1;
10165       strcpy(g_func, words[0]);
10166       set_label(0, words[0]);
10167       in_func = 1;
10168       continue;
10169     }
10170
10171     if (IS(words[1], "endp"))
10172     {
10173       if (!in_func)
10174         aerr("endp '%s' while not in_func?\n", words[0]);
10175       if (!IS(g_func, words[0]))
10176         aerr("endp '%s' while in_func '%s'?\n",
10177           words[0], g_func);
10178       if (skip_code)
10179         aerr("endp '%s' while skipping code\n", words[0]);
10180
10181       if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
10182         && ops[0].op == OP_JMP && ops[0].operand[0].segment)
10183       {
10184         // import jump
10185         g_skip_func = 1;
10186       }
10187
10188       if (!g_skip_func && func_chunks_used) {
10189         // start processing chunks
10190         struct chunk_item *ci, key = { g_func, 0 };
10191
10192         func_chunk_ret = ftell(fasm);
10193         func_chunk_ret_ln = asmln;
10194         if (!func_chunks_sorted) {
10195           qsort(func_chunks, func_chunk_cnt,
10196             sizeof(func_chunks[0]), cmp_chunks);
10197           func_chunks_sorted = 1;
10198         }
10199         ci = bsearch(&key, func_chunks, func_chunk_cnt,
10200                sizeof(func_chunks[0]), cmp_chunks);
10201         if (ci == NULL)
10202           aerr("'%s' needs chunks, but none found\n", g_func);
10203         func_chunk_i = ci - func_chunks;
10204         for (; func_chunk_i > 0; func_chunk_i--)
10205           if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
10206             break;
10207
10208         ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
10209         if (ret)
10210           aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
10211         asmln = func_chunks[func_chunk_i].asmln;
10212         func_chunk_i++;
10213         continue;
10214       }
10215       pending_endp = 1;
10216       continue;
10217     }
10218
10219     if (wordc == 2 && IS(words[1], "ends")) {
10220       if (!multi_seg) {
10221         end = 1;
10222         if (pending_endp)
10223           goto do_pending_endp;
10224         break;
10225       }
10226
10227       // scan for next text segment
10228       while (my_fgets(line, sizeof(line), fasm)) {
10229         asmln++;
10230         p = sskip(line);
10231         if (*p == 0 || *p == ';')
10232           continue;
10233
10234         if (strstr(p, "segment para public 'CODE' use32"))
10235           break;
10236       }
10237
10238       continue;
10239     }
10240
10241     p = strchr(words[0], ':');
10242     if (p != NULL) {
10243       set_label(pi, words[0]);
10244       continue;
10245     }
10246
10247     if (!in_func || g_skip_func || skip_code) {
10248       if (!skip_warned && !g_skip_func && g_labels[pi] != NULL) {
10249         if (verbose)
10250           anote("skipping from '%s'\n", g_labels[pi]);
10251         skip_warned = 1;
10252       }
10253       free(g_labels[pi]);
10254       g_labels[pi] = NULL;
10255       continue;
10256     }
10257
10258     if (wordc > 1 && IS(words[1], "="))
10259     {
10260       if (wordc != 5)
10261         aerr("unhandled equ, wc=%d\n", wordc);
10262       if (g_eqcnt >= eq_alloc) {
10263         eq_alloc *= 2;
10264         g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
10265         my_assert_not(g_eqs, NULL);
10266       }
10267
10268       len = strlen(words[0]);
10269       if (len > sizeof(g_eqs[0].name) - 1)
10270         aerr("equ name too long: %d\n", len);
10271       strcpy(g_eqs[g_eqcnt].name, words[0]);
10272
10273       if (!IS(words[3], "ptr"))
10274         aerr("unhandled equ\n");
10275       if (IS(words[2], "dword"))
10276         g_eqs[g_eqcnt].lmod = OPLM_DWORD;
10277       else if (IS(words[2], "word"))
10278         g_eqs[g_eqcnt].lmod = OPLM_WORD;
10279       else if (IS(words[2], "byte"))
10280         g_eqs[g_eqcnt].lmod = OPLM_BYTE;
10281       else if (IS(words[2], "qword"))
10282         g_eqs[g_eqcnt].lmod = OPLM_QWORD;
10283       else
10284         aerr("bad lmod: '%s'\n", words[2]);
10285
10286       g_eqs[g_eqcnt].offset = parse_number(words[4], 0);
10287       g_eqcnt++;
10288       continue;
10289     }
10290
10291     if (pi >= ARRAY_SIZE(ops))
10292       aerr("too many ops\n");
10293
10294     parse_op(&ops[pi], words, wordc);
10295
10296     ops[pi].datap = sctproto;
10297     sctproto = NULL;
10298     pi++;
10299   }
10300
10301   if (g_header_mode)
10302     output_hdr(fout);
10303
10304   fclose(fout);
10305   fclose(fasm);
10306   fclose(g_fhdr);
10307
10308   return 0;
10309 }
10310
10311 // vim:ts=2:shiftwidth=2:expandtab