Browse Source

Fixes to libdisasm, also use it a bit more

Artur K 7 years ago
parent
commit
d5e1fc733f

+ 24 - 19
3rd_party/libdisasm/ia32_operand.cpp

@@ -109,17 +109,15 @@ size_t Ia32_Decoder::decode_operand_value( unsigned char *buf, size_t buf_len,
             op->type = op_absolute;
 
             /* segment:offset address used in far calls */
-            x86_imm_sized( buf, buf_len,
-                           &op->data.absolute.segment, 2 );
             if ( m_decoded->addr_size == 4 ) {
-                x86_imm_sized( buf, buf_len,
-                               &op->data.absolute.offset.off32, 4 );
-                size = 6;
-            } else {
-                x86_imm_sized( buf, buf_len,
-                               &op->data.absolute.offset.off16, 2 );
+                x86_imm_sized( buf, buf_len, &op->data.absolute.offset.off32, 4 );
                 size = 4;
+            } else {
+                x86_imm_sized( buf, buf_len,&op->data.absolute.offset.off16, 2 );
+                size = 2;
             }
+            x86_imm_sized( buf+size, buf_len-size, &op->data.absolute.segment, 2 );
+            size+=2;
 
             break;
         case ADDRMETH_I:	/* Immediate val */
@@ -140,17 +138,24 @@ size_t Ia32_Decoder::decode_operand_value( unsigned char *buf, size_t buf_len,
                            op->data.far_offset depending on the size of
                            the operand */
             op->flags.op_signed = true;
-            if ( op_size == 1 ) {
-                /* one-byte near offset */
-                op->type = op_relative_near;
-                x86_imm_signsized(buf, buf_len, &op->data.relative_near, 1);
-            } else {
-                /* far offset...is this truly signed? */
-                op->type = op_relative_far;
-                x86_imm_signsized(buf, buf_len,
-                                  &op->data.relative_far, op_size );
+            switch(op_size)
+            {
+                case 1:
+                    /* one-byte near offset */
+                    op->type = op_relative_near;
+                    size = x86_imm_signsized(buf, buf_len, &op->data.relative_near, 1);
+                    break;
+                case 2:
+                    op->type = op_relative_far;
+                    int16_t offset_val;
+                    size = x86_imm_signsized(buf, buf_len,&offset_val, 2);
+                    op->data.relative_far=offset_val;
+                    break;
+                default:
+                    assert(false);
+                    size=0;
+
             }
-            size = op_size;
             break;
         case ADDRMETH_O:	/* No ModR/M; op is word/dword offset */
             /* NOTE: these are actually RVAs not offsets to seg!! */
@@ -258,7 +263,7 @@ size_t Ia32_Decoder::decode_operand_size( unsigned int op_type, x86_op_t *op ) {
                          * value s a 16:16 pointer or a 16:32 pointer, where
                          * the first '16' is a segment */
             size = (m_decoded->addr_size == 4) ? 6 : 4;
-            op->datatype = (size == 4) ? op_descr32 : op_descr16;
+            op->datatype = (size == 6) ? op_descr32 : op_descr16;
             break;
         case OPTYPE_b:	/* byte, ignore op-size */
             size = 1;

+ 372 - 349
3rd_party/libdisasm/libdis.h

@@ -6,17 +6,17 @@
 #endif
 #include <cstring>
 #include <cstdlib>
+#include <cassert>
 #include <stdint.h>
-
 /* 'NEW" types
  * __________________________________________________________________________*/
 #ifndef LIBDISASM_QWORD_H       /* do not interfere with qword.h */
-        #define LIBDISASM_QWORD_H
-        #ifdef _MSC_VER
-                typedef __int64         qword_t;
-        #else
-                typedef int64_t         qword_t;
-        #endif
+#define LIBDISASM_QWORD_H
+#ifdef _MSC_VER
+typedef __int64         qword_t;
+#else
+typedef int64_t         qword_t;
+#endif
 #endif
 
 #include <sys/types.h>
@@ -36,7 +36,7 @@
  *      this allows the report to recover from errors, or just log them.
  */
 enum x86_report_codes {
-        report_disasm_bounds,   /* RVA OUT OF BOUNDS : The disassembler could
+    report_disasm_bounds,   /* RVA OUT OF BOUNDS : The disassembler could
                                    not disassemble the supplied RVA as it is
                                    out of the range of the buffer. The
                                    application should store the address and
@@ -44,21 +44,21 @@ enum x86_report_codes {
                                    binary it is in, then disassemble the
                                    address from the bytes in that section.
                                         data: uint32_t rva */
-        report_insn_bounds,     /* INSTRUCTION OUT OF BOUNDS: The disassembler
+    report_insn_bounds,     /* INSTRUCTION OUT OF BOUNDS: The disassembler
                                    could not disassemble the instruction as
                                    the instruction would require bytes beyond
                                    the end of the current buffer. This usually
                                    indicated garbage bytes at the end of a
                                    buffer, or an incorrectly-sized buffer.
                                         data: uint32_t rva */
-        report_invalid_insn,    /* INVALID INSTRUCTION: The disassembler could
+    report_invalid_insn,    /* INVALID INSTRUCTION: The disassembler could
                                    not disassemble the instruction as it has an
                                    invalid combination of opcodes and operands.
                                    This will stop automated disassembly; the
                                    application can restart the disassembly
                                    after the invalid instruction.
                                         data: uint32_t rva */
-        report_unknown
+    report_unknown
 };
 /* Disassembly formats:
  *      AT&T is standard AS/GAS-style: "mnemonic\tsrc, dest, imm"
@@ -68,12 +68,12 @@ enum x86_report_codes {
  *      Raw is addr|offset|size|bytes|prefix... see libdisasm_formats.7
  */
 enum x86_asm_format {
-        unknown_syntax = 0,		/* never use! */
-        native_syntax, 			/* header: 35 bytes */
-        intel_syntax, 			/* header: 23 bytes */
-        att_syntax,  			/* header: 23 bytes */
-        xml_syntax,			/* header: 679 bytes */
-        raw_syntax			/* header: 172 bytes */
+    unknown_syntax = 0,		/* never use! */
+    native_syntax, 			/* header: 35 bytes */
+    intel_syntax, 			/* header: 23 bytes */
+    att_syntax,  			/* header: 23 bytes */
+    xml_syntax,			/* header: 679 bytes */
+    raw_syntax			/* header: 172 bytes */
 };
 
 /* 'arg' is optional arbitrary data provided by the code passing the
@@ -86,10 +86,10 @@ typedef void (*DISASM_REPORTER)( enum x86_report_codes code,
 
 /* ========================================= Libdisasm Management Routines */
 enum x86_options {		/* these can be ORed together */
-        opt_none= 0,
-        opt_ignore_nulls=1,     /* ignore sequences of > 4 NULL bytes */
-        opt_16_bit=2,           /* 16-bit/DOS disassembly */
-        opt_att_mnemonics=4    /* use AT&T syntax names for alternate opcode mnemonics */
+    opt_none= 0,
+    opt_ignore_nulls=1,     /* ignore sequences of > 4 NULL bytes */
+    opt_16_bit=2,           /* 16-bit/DOS disassembly */
+    opt_att_mnemonics=4    /* use AT&T syntax names for alternate opcode mnemonics */
 };
 
 /* ========================================= Instruction Representation */
@@ -110,35 +110,35 @@ enum x86_options {		/* these can be ORed together */
 #define MAX_INSN_XML_STRING 4096   /* 2 * 8 * MAX_OP_XML_STRING */
 
 enum x86_reg_type {     /* NOTE: these may be ORed together */
-        reg_undef       = 0x00000,      // used only in ia32_reg_table initializater
-        reg_gen         = 0x00001,      /* general purpose */
-        reg_in          = 0x00002,      /* incoming args, ala RISC */
-        reg_out         = 0x00004,      /* args to calls, ala RISC */
-        reg_local       = 0x00008,      /* local vars, ala RISC */
-        reg_fpu         = 0x00010,      /* FPU data register */
-        reg_seg         = 0x00020,      /* segment register */
-        reg_simd        = 0x00040,      /* SIMD/MMX reg */
-        reg_sys         = 0x00080,      /* restricted/system register */
-        reg_sp          = 0x00100,      /* stack pointer */
-        reg_fp          = 0x00200,      /* frame pointer */
-        reg_pc          = 0x00400,      /* program counter */
-        reg_retaddr     = 0x00800,      /* return addr for func */
-        reg_cond        = 0x01000,      /* condition code / flags */
-        reg_zero        = 0x02000,      /* zero register, ala RISC */
-        reg_ret         = 0x04000,      /* return value */
-        reg_src         = 0x10000,      /* array/rep source */
-        reg_dest        = 0x20000,      /* array/rep destination */
-        reg_count       = 0x40000       /* array/rep/loop counter */
+    reg_undef       = 0x00000,      // used only in ia32_reg_table initializater
+    reg_gen         = 0x00001,      /* general purpose */
+    reg_in          = 0x00002,      /* incoming args, ala RISC */
+    reg_out         = 0x00004,      /* args to calls, ala RISC */
+    reg_local       = 0x00008,      /* local vars, ala RISC */
+    reg_fpu         = 0x00010,      /* FPU data register */
+    reg_seg         = 0x00020,      /* segment register */
+    reg_simd        = 0x00040,      /* SIMD/MMX reg */
+    reg_sys         = 0x00080,      /* restricted/system register */
+    reg_sp          = 0x00100,      /* stack pointer */
+    reg_fp          = 0x00200,      /* frame pointer */
+    reg_pc          = 0x00400,      /* program counter */
+    reg_retaddr     = 0x00800,      /* return addr for func */
+    reg_cond        = 0x01000,      /* condition code / flags */
+    reg_zero        = 0x02000,      /* zero register, ala RISC */
+    reg_ret         = 0x04000,      /* return value */
+    reg_src         = 0x10000,      /* array/rep source */
+    reg_dest        = 0x20000,      /* array/rep destination */
+    reg_count       = 0x40000       /* array/rep/loop counter */
 };
 
 /* x86_reg_t : an X86 CPU register */
 struct x86_reg_t {
-        char name[MAX_REGNAME];
-        enum x86_reg_type type;         /* what register is used for */
-        unsigned int size;              /* size of register in bytes */
-        unsigned int id;                /* register ID #, for quick compares */
-        unsigned int alias;		/* ID of reg this is an alias for */
-        unsigned int shift;		/* amount to shift aliased reg by */
+    char name[MAX_REGNAME];
+    enum x86_reg_type type;         /* what register is used for */
+    unsigned int size;              /* size of register in bytes */
+    unsigned int id;                /* register ID #, for quick compares */
+    unsigned int alias;		/* ID of reg this is an alias for */
+    unsigned int shift;		/* amount to shift aliased reg by */
     x86_reg_t * aliased_reg( ) {
         x86_reg_t * reg = (x86_reg_t * )calloc( sizeof(x86_reg_t), 1 );
         reg->x86_reg_from_id( id );
@@ -149,92 +149,92 @@ struct x86_reg_t {
 
 /* x86_ea_t : an X86 effective address (address expression) */
 typedef struct {
-        unsigned int     scale;         /* scale factor */
-        x86_reg_t        index, base;   /* index, base registers */
-        int32_t          disp;          /* displacement */
-        char             disp_sign;     /* is negative? 1/0 */
-        char             disp_size;     /* 0, 1, 2, 4 */
+    unsigned int     scale;         /* scale factor */
+    x86_reg_t        index, base;   /* index, base registers */
+    int32_t          disp;          /* displacement */
+    char             disp_sign;     /* is negative? 1/0 */
+    char             disp_size;     /* 0, 1, 2, 4 */
 } x86_ea_t;
 
 /* x86_absolute_t : an X86 segment:offset address (descriptor) */
 typedef struct {
-        unsigned short	segment;	/* loaded directly into CS */
-        union {
-                unsigned short	off16;	/* loaded directly into IP */
-                uint32_t		off32;	/* loaded directly into EIP */
-        } offset;
+    unsigned short	segment;	/* loaded directly into CS */
+    union {
+        unsigned short	off16;	/* loaded directly into IP */
+        uint32_t		off32;	/* loaded directly into EIP */
+    } offset;
 } x86_absolute_t;
 
 enum x86_op_type {      /* mutually exclusive */
-        op_unused = 0,          /* empty/unused operand: should never occur */
-        op_register = 1,        /* CPU register */
-        op_immediate = 2,       /* Immediate Value */
-        op_relative_near = 3,   /* Relative offset from IP */
-        op_relative_far = 4,    /* Relative offset from IP */
-        op_absolute = 5,        /* Absolute address (ptr16:32) */
-        op_expression = 6,      /* Address expression (scale/index/base/disp) */
-        op_offset = 7,          /* Offset from start of segment (m32) */
-        op_unknown
+    op_unused = 0,          /* empty/unused operand: should never occur */
+    op_register = 1,        /* CPU register */
+    op_immediate = 2,       /* Immediate Value */
+    op_relative_near = 3,   /* Relative offset from IP */
+    op_relative_far = 4,    /* Relative offset from IP */
+    op_absolute = 5,        /* Absolute address (ptr16:32) */
+    op_expression = 6,      /* Address expression (scale/index/base/disp) */
+    op_offset = 7,          /* Offset from start of segment (m32) */
+    op_unknown
 };
 
 #define x86_optype_is_address( optype ) \
-        ( optype == op_absolute || optype == op_offset )
+    ( optype == op_absolute || optype == op_offset )
 #define x86_optype_is_relative( optype ) \
-        ( optype == op_relative_near || optype == op_relative_far )
+    ( optype == op_relative_near || optype == op_relative_far )
 #define x86_optype_is_memory( optype ) \
-        ( optype > op_immediate && optype < op_unknown )
+    ( optype > op_immediate && optype < op_unknown )
 
 enum x86_op_datatype {          /* these use Intel's lame terminology */
-        op_byte = 1,            /* 1 byte integer */
-        op_word = 2,            /* 2 byte integer */
-        op_dword = 3,           /* 4 byte integer */
-        op_qword = 4,           /* 8 byte integer */
-        op_dqword = 5,          /* 16 byte integer */
-        op_sreal = 6,           /* 4 byte real (single real) */
-        op_dreal = 7,           /* 8 byte real (double real) */
-        op_extreal = 8,         /* 10 byte real (extended real) */
-        op_bcd = 9,             /* 10 byte binary-coded decimal */
-        op_ssimd = 10,          /* 16 byte : 4 packed single FP (SIMD, MMX) */
-        op_dsimd = 11,          /* 16 byte : 2 packed double FP (SIMD, MMX) */
-        op_sssimd = 12,         /* 4 byte : scalar single FP (SIMD, MMX) */
-        op_sdsimd = 13,         /* 8 byte : scalar double FP (SIMD, MMX) */
-        op_descr32 = 14,	/* 6 byte Intel descriptor 2:4 */
-        op_descr16 = 15,	/* 4 byte Intel descriptor 2:2 */
-        op_pdescr32 = 16,	/* 6 byte Intel pseudo-descriptor 32:16 */
-        op_pdescr16 = 17,	/* 6 byte Intel pseudo-descriptor 8:24:16 */
-        op_bounds16 = 18,	/* signed 16:16 lower:upper bounds */
-        op_bounds32 = 19,	/* signed 32:32 lower:upper bounds */
-        op_fpuenv16 = 20,	/* 14 byte FPU control/environment data */
-        op_fpuenv32 = 21,	/* 28 byte FPU control/environment data */
-        op_fpustate16 = 22,	/* 94 byte FPU state (env & reg stack) */
-        op_fpustate32 = 23,	/* 108 byte FPU state (env & reg stack) */
-        op_fpregset = 24,	/* 512 bytes: register set */
-        op_fpreg = 25,		/* FPU register */
-        op_none = 0xFF     /* operand without a datatype (INVLPG) */
+    op_byte = 1,            /* 1 byte integer */
+    op_word = 2,            /* 2 byte integer */
+    op_dword = 3,           /* 4 byte integer */
+    op_qword = 4,           /* 8 byte integer */
+    op_dqword = 5,          /* 16 byte integer */
+    op_sreal = 6,           /* 4 byte real (single real) */
+    op_dreal = 7,           /* 8 byte real (double real) */
+    op_extreal = 8,         /* 10 byte real (extended real) */
+    op_bcd = 9,             /* 10 byte binary-coded decimal */
+    op_ssimd = 10,          /* 16 byte : 4 packed single FP (SIMD, MMX) */
+    op_dsimd = 11,          /* 16 byte : 2 packed double FP (SIMD, MMX) */
+    op_sssimd = 12,         /* 4 byte : scalar single FP (SIMD, MMX) */
+    op_sdsimd = 13,         /* 8 byte : scalar double FP (SIMD, MMX) */
+    op_descr32 = 14,	/* 6 byte Intel descriptor 2:4 */
+    op_descr16 = 15,	/* 4 byte Intel descriptor 2:2 */
+    op_pdescr32 = 16,	/* 6 byte Intel pseudo-descriptor 32:16 */
+    op_pdescr16 = 17,	/* 6 byte Intel pseudo-descriptor 8:24:16 */
+    op_bounds16 = 18,	/* signed 16:16 lower:upper bounds */
+    op_bounds32 = 19,	/* signed 32:32 lower:upper bounds */
+    op_fpuenv16 = 20,	/* 14 byte FPU control/environment data */
+    op_fpuenv32 = 21,	/* 28 byte FPU control/environment data */
+    op_fpustate16 = 22,	/* 94 byte FPU state (env & reg stack) */
+    op_fpustate32 = 23,	/* 108 byte FPU state (env & reg stack) */
+    op_fpregset = 24,	/* 512 bytes: register set */
+    op_fpreg = 25,		/* FPU register */
+    op_none = 0xFF     /* operand without a datatype (INVLPG) */
 };
 
 enum x86_op_access {    /* ORed together */
-        op_read = 1,
-        op_write = 2,
-        op_execute = 4
+    op_read = 1,
+    op_write = 2,
+    op_execute = 4
 };
 
 struct x86_op_flags {     /* ORed together, but segs are mutually exclusive */
     union {
         unsigned int op_signed:1,          /* signed integer */
-                     op_string:1,// = 2,          /* possible string or array */
-                    op_constant:1,// = 4,        /* symbolic constant */
-                    op_pointer:1,// = 8,         /* operand points to a memory address */
-                    op_sysref:1,// = 0x010,	/* operand is a syscall number */
-                    op_implied:1,// = 0x020,	/* operand is implicit in the insn */
-                    op_hardcode:1,// = 0x40,	/* operand is hardcoded in insn definition */
-        /* NOTE: an 'implied' operand is one which can be considered a side
-         * effect of the insn, e.g. %esp being modified by PUSH or POP. A
-         * 'hard-coded' operand is one which is specified in the instruction
-         * definition, e.g. %es:%edi in MOVSB or 1 in ROL Eb, 1. The difference
-         * is that hard-coded operands are printed by disassemblers and are
-         * required to re-assemble, while implicit operands are invisible. */
-                    op_seg : 3;
+            op_string:1,// = 2,          /* possible string or array */
+            op_constant:1,// = 4,        /* symbolic constant */
+            op_pointer:1,// = 8,         /* operand points to a memory address */
+            op_sysref:1,// = 0x010,	/* operand is a syscall number */
+            op_implied:1,// = 0x020,	/* operand is implicit in the insn */
+            op_hardcode:1,// = 0x40,	/* operand is hardcoded in insn definition */
+            /* NOTE: an 'implied' operand is one which can be considered a side
+                 * effect of the insn, e.g. %esp being modified by PUSH or POP. A
+                 * 'hard-coded' operand is one which is specified in the instruction
+                 * definition, e.g. %es:%edi in MOVSB or 1 in ROL Eb, 1. The difference
+                 * is that hard-coded operands are printed by disassemblers and are
+                 * required to re-assemble, while implicit operands are invisible. */
+            op_seg : 3;
         unsigned int whole;
     };
     enum {
@@ -250,43 +250,43 @@ struct x86_op_flags {     /* ORed together, but segs are mutually exclusive */
 /* x86_op_t : an X86 instruction operand */
 struct x86_op_t{
     friend struct x86_insn_t;
-        enum x86_op_type        type;           /* operand type */
-        enum x86_op_datatype    datatype;       /* operand size */
-        enum x86_op_access      access;         /* operand access [RWX] */
-        x86_op_flags       flags;          /* misc flags */
-        union {
-                /* sizeof will have to work on these union members! */
-                /* immediate values */
-                char            sbyte;
-                short           sword;
-                int32_t         sdword;
-                qword_t         sqword;
-                unsigned char   byte;
-                unsigned short  word;
-                uint32_t        dword;
-                qword_t         qword;
-                float           sreal;
-                double          dreal;
-                /* misc large/non-native types */
-                unsigned char   extreal[10];
-                unsigned char   bcd[10];
-                qword_t         dqword[2];
-                unsigned char   simd[16];
-                unsigned char   fpuenv[28];
-                /* offset from segment */
-                uint32_t        offset;
-                /* ID of CPU register */
-                x86_reg_t       reg;
-                /* offsets from current insn */
-                char            relative_near;
-                int32_t         relative_far;
-                /* segment:offset */
-                x86_absolute_t	absolute;
-                /* effective address [expression] */
-                x86_ea_t        expression;
-        } data;
-        /* this is needed to make formatting operands more sane */
-        void * insn;		/* pointer to x86_insn_t owning operand */
+    enum x86_op_type        type;           /* operand type */
+    enum x86_op_datatype    datatype;       /* operand size */
+    enum x86_op_access      access;         /* operand access [RWX] */
+    x86_op_flags       flags;          /* misc flags */
+    union {
+        /* sizeof will have to work on these union members! */
+        /* immediate values */
+        char            sbyte;
+        short           sword;
+        int32_t         sdword;
+        qword_t         sqword;
+        unsigned char   byte;
+        unsigned short  word;
+        uint32_t        dword;
+        qword_t         qword;
+        float           sreal;
+        double          dreal;
+        /* misc large/non-native types */
+        unsigned char   extreal[10];
+        unsigned char   bcd[10];
+        qword_t         dqword[2];
+        unsigned char   simd[16];
+        unsigned char   fpuenv[28];
+        /* offset from segment */
+        uint32_t        offset;
+        /* ID of CPU register */
+        x86_reg_t       reg;
+        /* offsets from current insn */
+        char            relative_near;
+        int32_t         relative_far;
+        /* segment:offset */
+        x86_absolute_t	absolute;
+        /* effective address [expression] */
+        x86_ea_t        expression;
+    } data;
+    /* this is needed to make formatting operands more sane */
+    void * insn;		/* pointer to x86_insn_t owning operand */
     size_t size()
     {
         return operand_size();
@@ -301,6 +301,29 @@ struct x86_op_t{
     bool is_relative( ) {
         return ( type == op_relative_near || type == op_relative_far );
     }
+    int32_t getAddress()
+    {
+        assert(is_address()||is_relative());
+        switch(type)
+        {
+            case op_absolute:
+            {
+                if(datatype==op_descr16)
+                    return (int32_t(data.absolute.segment)<<4) + data.absolute.offset.off16;
+                else
+                    return (int32_t(data.absolute.segment)<<4) + data.absolute.offset.off32;
+            }
+            case op_offset:
+                return data.offset;
+            case op_relative_near:
+                return data.relative_near;
+            case op_relative_far:
+                return data.relative_far;
+            default:
+                assert(false);
+                return ~0;
+        }
+    }
     char * format( enum x86_asm_format format );
     x86_op_t * copy()
     {
@@ -319,119 +342,119 @@ private:
  * list in an insn. Users wishing to add operands to this list, e.g. to add
  * implicit operands, should use x86_operand_new in x86_operand_list.h */
 struct  x86_oplist_t {
-        x86_op_t op;
-        struct  x86_oplist_t *next;
+    x86_op_t op;
+    struct  x86_oplist_t *next;
 };
 
 enum x86_insn_type {
-        insn_invalid = 0,	/* invalid instruction */
-        /* insn_controlflow */
-        insn_jmp = 0x1001,
-        insn_jcc = 0x1002,
-        insn_call = 0x1003,
-        insn_callcc = 0x1004,
-        insn_return = 0x1005,
-        /* insn_arithmetic */
-        insn_add = 0x2001,
-        insn_sub = 0x2002,
-        insn_mul = 0x2003,
-        insn_div = 0x2004,
-        insn_inc = 0x2005,
-        insn_dec = 0x2006,
-        insn_shl = 0x2007,
-        insn_shr = 0x2008,
-        insn_rol = 0x2009,
-        insn_ror = 0x200A,
-        /* insn_logic */
-        insn_and = 0x3001,
-        insn_or = 0x3002,
-        insn_xor = 0x3003,
-        insn_not = 0x3004,
-        insn_neg = 0x3005,
-        /* insn_stack */
-        insn_push = 0x4001,
-        insn_pop = 0x4002,
-        insn_pushregs = 0x4003,
-        insn_popregs = 0x4004,
-        insn_pushflags = 0x4005,
-        insn_popflags = 0x4006,
-        insn_enter = 0x4007,
-        insn_leave = 0x4008,
-        /* insn_comparison */
-        insn_test = 0x5001,
-        insn_cmp = 0x5002,
-        /* insn_move */
-        insn_mov = 0x6001,      /* move */
-        insn_movcc = 0x6002,    /* conditional move */
-        insn_xchg = 0x6003,     /* exchange */
-        insn_xchgcc = 0x6004,   /* conditional exchange */
-        /* insn_string */
-        insn_strcmp = 0x7001,
-        insn_strload = 0x7002,
-        insn_strmov = 0x7003,
-        insn_strstore = 0x7004,
-        insn_translate = 0x7005,        /* xlat */
-        /* insn_bit_manip */
-        insn_bittest = 0x8001,
-        insn_bitset = 0x8002,
-        insn_bitclear = 0x8003,
-        /* insn_flag_manip */
-        insn_clear_carry = 0x9001,
-        insn_clear_zero = 0x9002,
-        insn_clear_oflow = 0x9003,
-        insn_clear_dir = 0x9004,
-        insn_clear_sign = 0x9005,
-        insn_clear_parity = 0x9006,
-        insn_set_carry = 0x9007,
-        insn_set_zero = 0x9008,
-        insn_set_oflow = 0x9009,
-        insn_set_dir = 0x900A,
-        insn_set_sign = 0x900B,
-        insn_set_parity = 0x900C,
-        insn_tog_carry = 0x9010,
-        insn_tog_zero = 0x9020,
-        insn_tog_oflow = 0x9030,
-        insn_tog_dir = 0x9040,
-        insn_tog_sign = 0x9050,
-        insn_tog_parity = 0x9060,
-        /* insn_fpu */
-        insn_fmov = 0xA001,
-        insn_fmovcc = 0xA002,
-        insn_fneg = 0xA003,
-        insn_fabs = 0xA004,
-        insn_fadd = 0xA005,
-        insn_fsub = 0xA006,
-        insn_fmul = 0xA007,
-        insn_fdiv = 0xA008,
-        insn_fsqrt = 0xA009,
-        insn_fcmp = 0xA00A,
-        insn_fcos = 0xA00C,
-        insn_fldpi = 0xA00D,
-        insn_fldz = 0xA00E,
-        insn_ftan = 0xA00F,
-        insn_fsine = 0xA010,
-        insn_fsys = 0xA020,
-        /* insn_interrupt */
-        insn_int = 0xD001,
-        insn_intcc = 0xD002,    /* not present in x86 ISA */
-        insn_iret = 0xD003,
-        insn_bound = 0xD004,
-        insn_debug = 0xD005,
-        insn_trace = 0xD006,
-        insn_invalid_op = 0xD007,
-        insn_oflow = 0xD008,
-        /* insn_system */
-        insn_halt = 0xE001,
-        insn_in = 0xE002,       /* input from port/bus */
-        insn_out = 0xE003,      /* output to port/bus */
-        insn_cpuid = 0xE004,
-        insn_lmsw = 0xE005,
-        insn_smsw = 0xE006,
-        insn_clts = 0xE007,
-        /* insn_other */
-        insn_nop = 0xF001,
-        insn_bcdconv = 0xF002,  /* convert to or from BCD */
-        insn_szconv = 0xF003    /* change size of operand */
+    insn_invalid = 0,	/* invalid instruction */
+    /* insn_controlflow */
+    insn_jmp = 0x1001,
+    insn_jcc = 0x1002,
+    insn_call = 0x1003,
+    insn_callcc = 0x1004,
+    insn_return = 0x1005,
+    /* insn_arithmetic */
+    insn_add = 0x2001,
+    insn_sub = 0x2002,
+    insn_mul = 0x2003,
+    insn_div = 0x2004,
+    insn_inc = 0x2005,
+    insn_dec = 0x2006,
+    insn_shl = 0x2007,
+    insn_shr = 0x2008,
+    insn_rol = 0x2009,
+    insn_ror = 0x200A,
+    /* insn_logic */
+    insn_and = 0x3001,
+    insn_or = 0x3002,
+    insn_xor = 0x3003,
+    insn_not = 0x3004,
+    insn_neg = 0x3005,
+    /* insn_stack */
+    insn_push = 0x4001,
+    insn_pop = 0x4002,
+    insn_pushregs = 0x4003,
+    insn_popregs = 0x4004,
+    insn_pushflags = 0x4005,
+    insn_popflags = 0x4006,
+    insn_enter = 0x4007,
+    insn_leave = 0x4008,
+    /* insn_comparison */
+    insn_test = 0x5001,
+    insn_cmp = 0x5002,
+    /* insn_move */
+    insn_mov = 0x6001,      /* move */
+    insn_movcc = 0x6002,    /* conditional move */
+    insn_xchg = 0x6003,     /* exchange */
+    insn_xchgcc = 0x6004,   /* conditional exchange */
+    /* insn_string */
+    insn_strcmp = 0x7001,
+    insn_strload = 0x7002,
+    insn_strmov = 0x7003,
+    insn_strstore = 0x7004,
+    insn_translate = 0x7005,        /* xlat */
+    /* insn_bit_manip */
+    insn_bittest = 0x8001,
+    insn_bitset = 0x8002,
+    insn_bitclear = 0x8003,
+    /* insn_flag_manip */
+    insn_clear_carry = 0x9001,
+    insn_clear_zero = 0x9002,
+    insn_clear_oflow = 0x9003,
+    insn_clear_dir = 0x9004,
+    insn_clear_sign = 0x9005,
+    insn_clear_parity = 0x9006,
+    insn_set_carry = 0x9007,
+    insn_set_zero = 0x9008,
+    insn_set_oflow = 0x9009,
+    insn_set_dir = 0x900A,
+    insn_set_sign = 0x900B,
+    insn_set_parity = 0x900C,
+    insn_tog_carry = 0x9010,
+    insn_tog_zero = 0x9020,
+    insn_tog_oflow = 0x9030,
+    insn_tog_dir = 0x9040,
+    insn_tog_sign = 0x9050,
+    insn_tog_parity = 0x9060,
+    /* insn_fpu */
+    insn_fmov = 0xA001,
+    insn_fmovcc = 0xA002,
+    insn_fneg = 0xA003,
+    insn_fabs = 0xA004,
+    insn_fadd = 0xA005,
+    insn_fsub = 0xA006,
+    insn_fmul = 0xA007,
+    insn_fdiv = 0xA008,
+    insn_fsqrt = 0xA009,
+    insn_fcmp = 0xA00A,
+    insn_fcos = 0xA00C,
+    insn_fldpi = 0xA00D,
+    insn_fldz = 0xA00E,
+    insn_ftan = 0xA00F,
+    insn_fsine = 0xA010,
+    insn_fsys = 0xA020,
+    /* insn_interrupt */
+    insn_int = 0xD001,
+    insn_intcc = 0xD002,    /* not present in x86 ISA */
+    insn_iret = 0xD003,
+    insn_bound = 0xD004,
+    insn_debug = 0xD005,
+    insn_trace = 0xD006,
+    insn_invalid_op = 0xD007,
+    insn_oflow = 0xD008,
+    /* insn_system */
+    insn_halt = 0xE001,
+    insn_in = 0xE002,       /* input from port/bus */
+    insn_out = 0xE003,      /* output to port/bus */
+    insn_cpuid = 0xE004,
+    insn_lmsw = 0xE005,
+    insn_smsw = 0xE006,
+    insn_clts = 0xE007,
+    /* insn_other */
+    insn_nop = 0xF001,
+    insn_bcdconv = 0xF002,  /* convert to or from BCD */
+    insn_szconv = 0xF003    /* change size of operand */
 };
 
 /* These flags specify special characteristics of the instruction, such as
@@ -440,11 +463,11 @@ enum x86_insn_type {
  * NOTE : These may not be accurate for all instructions; updates to the
  * opcode tables have not been completed. */
 enum x86_insn_note {
-        insn_note_ring0		= 1,	/* Only available in ring 0 */
-        insn_note_smm		= 2,	/* "" in System Management Mode */
-        insn_note_serial	= 4,	/* Serializing instruction */
-        insn_note_nonswap	= 8,	/* Does not swap arguments in att-style formatting */
-        insn_note_nosuffix  = 16	/* Does not have size suffix in att-style formatting */
+    insn_note_ring0		= 1,	/* Only available in ring 0 */
+    insn_note_smm		= 2,	/* "" in System Management Mode */
+    insn_note_serial	= 4,	/* Serializing instruction */
+    insn_note_nonswap	= 8,	/* Does not swap arguments in att-style formatting */
+    insn_note_nosuffix  = 16	/* Does not have size suffix in att-style formatting */
 };
 
 /* This specifies what effects the instruction has on the %eflags register */
@@ -458,22 +481,22 @@ enum x86_eflags
     insn_eflag_parity
 };
 enum x86_flag_status {
-        insn_carry_set = 0x1,			/* CF */
-        insn_zero_set = 0x2,			/* ZF */
-        insn_oflow_set = 0x4,			/* OF */
-        insn_dir_set = 0x8,			/* DF */
-        insn_sign_set = 0x10,			/* SF */
-        insn_parity_set = 0x20,			/* PF */
-        insn_carry_or_zero_set = 0x40,
-        insn_zero_set_or_sign_ne_oflow = 0x80,
-        insn_carry_clear = 0x100,
-        insn_zero_clear = 0x200,
-        insn_oflow_clear = 0x400,
-        insn_dir_clear = 0x800,
-        insn_sign_clear = 0x1000,
-        insn_parity_clear = 0x2000,
-        insn_sign_eq_oflow = 0x4000,
-        insn_sign_ne_oflow = 0x8000
+    insn_carry_set = 0x1,			/* CF */
+    insn_zero_set = 0x2,			/* ZF */
+    insn_oflow_set = 0x4,			/* OF */
+    insn_dir_set = 0x8,			/* DF */
+    insn_sign_set = 0x10,			/* SF */
+    insn_parity_set = 0x20,			/* PF */
+    insn_carry_or_zero_set = 0x40,
+    insn_zero_set_or_sign_ne_oflow = 0x80,
+    insn_carry_clear = 0x100,
+    insn_zero_clear = 0x200,
+    insn_oflow_clear = 0x400,
+    insn_dir_clear = 0x800,
+    insn_sign_clear = 0x1000,
+    insn_parity_clear = 0x2000,
+    insn_sign_eq_oflow = 0x4000,
+    insn_sign_ne_oflow = 0x8000
 };
 
 /* The CPU model in which the insturction first appeared; this can be used
@@ -482,19 +505,19 @@ enum x86_flag_status {
  * NOTE : These may not be accurate for all instructions; updates to the
  * opcode tables have not been completed. */
 enum x86_insn_cpu {
-        cpu_8086 	= 1,	/* Intel */
-        cpu_80286	= 2,
-        cpu_80386	= 3,
-        cpu_80387	= 4,
-        cpu_80486	= 5,
-        cpu_pentium	= 6,
-        cpu_pentiumpro	= 7,
-        cpu_pentium2	= 8,
-        cpu_pentium3	= 9,
-        cpu_pentium4	= 10,
-        cpu_k6		= 16,	/* AMD */
-        cpu_k7		= 32,
-        cpu_athlon	= 48
+    cpu_8086 	= 1,	/* Intel */
+    cpu_80286	= 2,
+    cpu_80386	= 3,
+    cpu_80387	= 4,
+    cpu_80486	= 5,
+    cpu_pentium	= 6,
+    cpu_pentiumpro	= 7,
+    cpu_pentium2	= 8,
+    cpu_pentium3	= 9,
+    cpu_pentium4	= 10,
+    cpu_k6		= 16,	/* AMD */
+    cpu_k7		= 32,
+    cpu_athlon	= 48
 };
 
 /* CPU ISA subsets: These are derived from the Instruction Groups in
@@ -505,22 +528,22 @@ enum x86_insn_cpu {
  * NOTE : These may not be accurate for all instructions; updates to the
  * opcode tables have not been completed. */
 enum x86_insn_isa {
-        isa_gp		= 1,	/* general purpose */
-        isa_fp		= 2,	/* floating point */
-        isa_fpumgt	= 3,	/* FPU/SIMD management */
-        isa_mmx		= 4,	/* Intel MMX */
-        isa_sse1	= 5,	/* Intel SSE SIMD */
-        isa_sse2	= 6,	/* Intel SSE2 SIMD */
-        isa_sse3	= 7,	/* Intel SSE3 SIMD */
-        isa_3dnow	= 8,	/* AMD 3DNow! SIMD */
-        isa_sys		= 9	/* system instructions */
+    isa_gp		= 1,	/* general purpose */
+    isa_fp		= 2,	/* floating point */
+    isa_fpumgt	= 3,	/* FPU/SIMD management */
+    isa_mmx		= 4,	/* Intel MMX */
+    isa_sse1	= 5,	/* Intel SSE SIMD */
+    isa_sse2	= 6,	/* Intel SSE2 SIMD */
+    isa_sse3	= 7,	/* Intel SSE3 SIMD */
+    isa_3dnow	= 8,	/* AMD 3DNow! SIMD */
+    isa_sys		= 9	/* system instructions */
 };
 
 enum x86_insn_prefix {
-        insn_no_prefix = 0,
-        insn_rep_zero = 1,	/* REPZ and REPE */
-        insn_rep_notzero = 2,	/* REPNZ and REPNZ */
-        insn_lock = 4		/* LOCK: */
+    insn_no_prefix = 0,
+    insn_rep_zero = 1,	/* REPZ and REPE */
+    insn_rep_notzero = 2,	/* REPNZ and REPNZ */
+    insn_lock = 4		/* LOCK: */
 };
 
 
@@ -535,15 +558,15 @@ enum x86_insn_prefix {
  * The "type" (implicit or explicit) and the access method can
  * be ORed together, e.g. op_wo | op_explicit */
 enum x86_op_foreach_type {
-        op_any 	= 0,		/* ALL operands (explicit, implicit, rwx) */
-        op_dest = 1,		/* operands with Write access */
-        op_src 	= 2,		/* operands with Read access */
-        op_ro 	= 3,		/* operands with Read but not Write access */
-        op_wo 	= 4,		/* operands with Write but not Read access */
-        op_xo 	= 5,		/* operands with Execute access */
-        op_rw 	= 6,		/* operands with Read AND Write access */
-        op_implicit = 0x10,	/* operands that are implied by the opcode */
-        op_explicit = 0x20	/* operands that are not side-effects */
+    op_any 	= 0,		/* ALL operands (explicit, implicit, rwx) */
+    op_dest = 1,		/* operands with Write access */
+    op_src 	= 2,		/* operands with Read access */
+    op_ro 	= 3,		/* operands with Read but not Write access */
+    op_wo 	= 4,		/* operands with Write but not Read access */
+    op_xo 	= 5,		/* operands with Execute access */
+    op_rw 	= 6,		/* operands with Read AND Write access */
+    op_implicit = 0x10,	/* operands that are implied by the opcode */
+    op_explicit = 0x20	/* operands that are not side-effects */
 };
 
 /* Operand FOREACH callback: 'arg' is an abritrary parameter passed to the
@@ -684,9 +707,9 @@ public:
 *        (buf, buf_len, buf_rva, offset, len, insn, func, arg, resolve_func)
  *      ...but of course all of these are not used at the same time.
  */
- class X86_Disasm
- {
- public:
+class X86_Disasm
+{
+public:
     /* Function prototype for caller-supplied callback routine
      *      These callbacks are intended to process 'insn' further, e.g. by
      *      adding it to a linked list, database, etc */
@@ -701,28 +724,28 @@ public:
      *      should return -1; in all other cases the RVA to be disassembled next
      *      should be returned. */
     typedef int32_t (*DISASM_RESOLVER)( x86_op_t *op, x86_insn_t * current_insn,
-                                     void *arg );
- protected:
-     DISASM_REPORTER __x86_reporter_func;
-     void * __x86_reporter_arg;
-     Ia32_Decoder m_decoder;
-
- public:
-     X86_Disasm( x86_options options=opt_none,DISASM_REPORTER reporter=0, void *arg=0 ) :
-                __x86_reporter_func(reporter),
-                __x86_reporter_arg(arg) {
-         x86_init( options,reporter,arg);
-     }
-     /* management routines */
-     /* 'arg' is caller-specific data which is passed as the first argument
+                                        void *arg );
+protected:
+    DISASM_REPORTER __x86_reporter_func;
+    void * __x86_reporter_arg;
+    Ia32_Decoder m_decoder;
+
+public:
+    X86_Disasm( x86_options options=opt_none,DISASM_REPORTER reporter=0, void *arg=0 ) :
+        __x86_reporter_func(reporter),
+        __x86_reporter_arg(arg) {
+        x86_init( options,reporter,arg);
+    }
+    /* management routines */
+    /* 'arg' is caller-specific data which is passed as the first argument
      * to the reporter callback routine */
-     int x86_init( x86_options options, DISASM_REPORTER reporter, void *arg);
-     void x86_set_reporter( DISASM_REPORTER reporter, void *arg);
-     void x86_set_options( x86_options options );
-     x86_options x86_get_options( void );
-     int x86_cleanup(void);
+    int x86_init( x86_options options, DISASM_REPORTER reporter, void *arg);
+    void x86_set_reporter( DISASM_REPORTER reporter, void *arg);
+    void x86_set_options( x86_options options );
+    x86_options x86_get_options( void );
+    int x86_cleanup(void);
 
-     /* x86_disasm: Disassemble a single instruction from a buffer of bytes.
+    /* x86_disasm: Disassemble a single instruction from a buffer of bytes.
      *             Returns size of instruction in bytes.
      *             Caller is responsible for calling x86_oplist_free() on
      *             a reused "insn" to avoid leaking memory when calling this
@@ -733,9 +756,9 @@ public:
      *      offset  : Offset in buffer to disassemble
      *      insn    : Structure to fill with disassembled instruction
      */
-    unsigned int x86_disasm( unsigned char *buf, unsigned int buf_len,
-                             uint32_t buf_rva, unsigned int offset,
-                             x86_insn_t * insn );
+    unsigned int x86_disasm(const unsigned char *buf, unsigned int buf_len,
+                            uint32_t buf_rva, unsigned int offset,
+                            x86_insn_t * insn );
     /* x86_disasm_range: Sequential disassembly of a range of bytes in a buffer,
      *                   invoking a callback function each time an instruction
      *                   is successfully disassembled. The 'range' refers to the
@@ -794,7 +817,7 @@ public:
     unsigned int x86_ip_reg(void);
     unsigned int x86_flag_reg(void);
 
- };
+};
 
 /* Instruction operands: these are stored as a list of explicit and
  * implicit operands. It is recommended that the 'foreach' routines
@@ -843,24 +866,24 @@ public:
 #define X86_WILDCARD_BYTE 0xF4
 
 struct x86_invariant_op_t{
-        enum x86_op_type        type;           /* operand type */
-        enum x86_op_datatype    datatype;       /* operand size */
-        enum x86_op_access      access;         /* operand access [RWX] */
-        x86_op_flags       flags;          /* misc flags */
+    enum x86_op_type        type;           /* operand type */
+    enum x86_op_datatype    datatype;       /* operand size */
+    enum x86_op_access      access;         /* operand access [RWX] */
+    x86_op_flags       flags;          /* misc flags */
 };
 
 struct x86_invariant_t {
-        unsigned char bytes[64];	/* invariant representation */
-        unsigned int  size;		/* number of bytes in insn */
+    unsigned char bytes[64];	/* invariant representation */
+    unsigned int  size;		/* number of bytes in insn */
     x86_insn_t::x86_insn_group group;      /* meta-type, e.g. INS_EXEC */
-        enum x86_insn_type type;        /* type, e.g. INS_BRANCH */
-        x86_invariant_op_t operands[3];	/* operands: dest, src, imm */
+    enum x86_insn_type type;        /* type, e.g. INS_BRANCH */
+    x86_invariant_op_t operands[3];	/* operands: dest, src, imm */
 } ;
 
 
 /* return a version of the instruction with the variant bytes masked out */
 size_t x86_invariant_disasm( unsigned char *buf, int buf_len,
-                          x86_invariant_t *inv );
+                             x86_invariant_t *inv );
 /* return the size in bytes of the intruction pointed to by 'buf';
  * this used x86_invariant_disasm since it faster than x86_disasm */
 size_t x86_size_disasm( unsigned char *buf, unsigned int buf_len );

+ 1 - 1
3rd_party/libdisasm/x86_disasm.cpp

@@ -21,7 +21,7 @@ void x86_insn_t::make_invalid(unsigned char *buf)
     type = insn_invalid;
     memcpy( bytes, buf, 1 );
 }
-unsigned int X86_Disasm::x86_disasm( unsigned char *buf, unsigned int buf_len,
+unsigned int X86_Disasm::x86_disasm( const unsigned char *buf, unsigned int buf_len,
                                      uint32_t buf_rva, unsigned int offset,
                                      x86_insn_t *insn ){
     int len, size;

+ 2 - 1
CMakeLists.txt

@@ -25,7 +25,7 @@ ENDIF()
 
 ADD_SUBDIRECTORY(3rd_party)
 
-llvm_map_components_to_libraries(REQ_LLVM_LIBRARIES jit native mc support)
+llvm_map_components_to_libraries(REQ_LLVM_LIBRARIES jit native mc support tablegen)
 INCLUDE_DIRECTORIES(
     3rd_party/libdisasm
     include
@@ -50,6 +50,7 @@ set(dcc_LIB_SOURCES
     src/hltype.cpp
     src/machine_x86.cpp
     src/icode.cpp
+    src/RegisterNode
     src/idioms.cpp
     src/idioms/idiom1.cpp
     src/idioms/arith_idioms.cpp

+ 1 - 1
base_regression.sh

@@ -3,4 +3,4 @@
 #make -j5
 #cd ..
 ./test_use_base.sh
-./regression_tester.rb ./dcc_original -s -c 2>stderr >stdout; diff tests/prev/ tests/outputs/
+./regression_tester.rb ./dcc_original -s -c 2>stderr >stdout; diff -wB tests/prev/ tests/outputs/

+ 1 - 1
full_regression.sh

@@ -1,3 +1,3 @@
 #!/bin/bash
 ./test_use_all.sh
-./regression_tester.rb ./dcc_original -s -c 2>stderr >stdout; diff tests/prev/ tests/outputs/
+./regression_tester.rb ./dcc_original -s -c 2>stderr >stdout; diff -wB tests/prev/ tests/outputs/

+ 2 - 1
include/BinaryImage.h

@@ -15,6 +15,7 @@ struct PROG /* Loaded program image parameters  */
     uint16_t    segMain;    /* The segment of the main() proc   */
     bool        bSigs;		/* True if signatures loaded		*/
     int         cbImage;    /* Length of image in bytes         */
-    uint8_t *   Image;      /* Allocated by loader to hold entire program image */
+    const uint8_t *image() const {return Imagez;}
+    uint8_t *   Imagez;      /* Allocated by loader to hold entire program image */
 };
 

+ 1 - 0
include/Enums.h

@@ -8,6 +8,7 @@ enum regType
 };
 enum condId
 {
+    UNDEF=0,
     GLOB_VAR,       /* global variable  */
     REGISTER,       /* register         */
     LOCAL_VAR,      /* negative disp    */

+ 11 - 14
include/IdentType.h

@@ -2,32 +2,29 @@
 #include "ast.h"
 #include "types.h"
 #include "machine_x86.h"
-
+struct GlobalVariable;
+struct AstIdent;
 struct IDENTTYPE
 {
+    friend struct GlobalVariable;
+    friend struct Constant;
+    friend struct AstIdent;
+protected:
     condId           idType;
-    regType          regiType;  /* for REGISTER only                */
+public:
+    condId           type() {return idType;}
+    void             type(condId t) {idType=t;}
     union _idNode {
-        int          regiIdx;   /* index into localId, REGISTER		*/
-        int          globIdx;   /* index into symtab for GLOB_VAR   */
         int          localIdx;  /* idx into localId,  LOCAL_VAR		*/
         int          paramIdx;  /* idx into args symtab, PARAMS     */
-        int			 idxGlbIdx;	/* idx into localId, GLOB_VAR_IDX   */
-        struct _kte
-        {			/* for CONSTANT only					*/
-            uint32_t   kte;   	/*   value of the constant			*/
-            uint8_t    size;       /*   #bytes size constant	 		*/
-        } kte;
         uint32_t        strIdx;	/* idx into image, for STRING	 	*/
         int             longIdx;	/* idx into LOCAL_ID table, LONG_VAR*/
-        struct _call {			/* for FUNCTION only				*/
-            Function     *proc;
-            STKFRAME *args;
-        }			 call;
         struct {                /* for OTHER; tmp struct            */
             eReg     seg;       /*   segment                        */
             eReg     regi;      /*   index mode                     */
             int16_t    off;       /*   offset                         */
         }            other;
     }                idNode;
+    IDENTTYPE() : idType(UNDEF)
+    {}
 };

+ 6 - 4
include/Procedure.h

@@ -9,7 +9,7 @@
 #include "StackFrame.h"
 /* PROCEDURE NODE */
 struct CALL_GRAPH;
-struct COND_EXPR;
+struct Expr;
 struct Disassembler;
 struct Function;
 struct CALL_GRAPH;
@@ -74,8 +74,8 @@ struct FunctionType
 };
 struct Assignment
 {
-    COND_EXPR *lhs;
-    COND_EXPR *rhs;
+    Expr *lhs;
+    Expr *rhs;
 };
 struct JumpTable
 {
@@ -184,9 +184,11 @@ public:
 
     void displayCFG();
     void displayStats();
-    void processHliCall(COND_EXPR *exp, iICODE picode);
+    void processHliCall(Expr *exp, iICODE picode);
 
     void preprocessReturnDU(LivenessSet &_liveOut);
+    Expr * adjustActArgType(Expr *_exp, hlType forType);
+    std::string writeCall(Function *tproc, STKFRAME &args, int *numLoc);
 protected:
     void extractJumpTableRange(ICODE& pIcode, STATE *pstate, JumpTable &table);
     bool followAllTableEntries(JumpTable &table, uint32_t cs, ICODE &pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);

+ 144 - 60
include/ast.h

@@ -5,6 +5,7 @@
  * (C) Cristina Cifuentes
  */
 #pragma once
+#include <stdint.h>
 #include <cstring>
 #include <list>
 #include <boost/range/iterator_range.hpp>
@@ -34,37 +35,37 @@ typedef boost::iterator_range<iICODE> rICODE;
 #include "IdentType.h"
 
 /* Expression data type */
-struct COND_EXPR
+struct Expr
 {
 public:
     condNodeType            m_type;     /* Conditional Expression Node Type */
 public:
-    static bool         insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR *&tree, int longIdx);
-    static bool         insertSubTreeReg(COND_EXPR *&tree, COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
-    static bool         insertSubTreeReg(AstIdent *&tree, COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
+    static bool         insertSubTreeLongReg(Expr *exp, Expr *&tree, int longIdx);
+    static bool         insertSubTreeReg(Expr *&tree, Expr *_expr, eReg regi, const LOCAL_ID *locsym);
+    static bool         insertSubTreeReg(AstIdent *&tree, Expr *_expr, eReg regi, const LOCAL_ID *locsym);
 public:
-    virtual COND_EXPR *clone() const;
-    void release();
-    COND_EXPR(condNodeType t=UNKNOWN_OP) : m_type(t)
+
+    virtual Expr *clone() const=0;  //!< Makes a deep copy of the given expression
+    Expr(condNodeType t=UNKNOWN_OP) : m_type(t)
     {
 
     }
-    virtual ~COND_EXPR();
+    virtual ~Expr();
 public:
     virtual std::string walkCondExpr (Function * pProc, int* numLoc) const=0;
-    virtual COND_EXPR *inverse() const=0; // return new COND_EXPR that is invarse of this
+    virtual Expr *inverse() const=0; // return new COND_EXPR that is invarse of this
     virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId)=0;
-    virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym)=0;
-    virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)=0;
-    virtual hlType expType(Function *pproc) const;
+    virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)=0;
+    virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx)=0;
+    virtual hlType expType(Function *pproc) const=0;
     virtual int hlTypeSize(Function *pproc) const=0;
-    virtual void performLongRemoval(eReg regi, LOCAL_ID *locId) {}
+    virtual Expr * performLongRemoval(eReg regi, LOCAL_ID *locId) { return this; }
 };
-struct UnaryOperator : public COND_EXPR
+struct UnaryOperator : public Expr
 {
-    UnaryOperator(condNodeType t=UNKNOWN_OP) : COND_EXPR(t),unaryExp(nullptr) {}
-    COND_EXPR *unaryExp;
-    virtual COND_EXPR *inverse() const
+    UnaryOperator(condNodeType t=UNKNOWN_OP) : Expr(t),unaryExp(nullptr) {}
+    Expr *unaryExp;
+    virtual Expr *inverse() const
     {
         if (m_type == NEGATION) //TODO: memleak here
         {
@@ -72,14 +73,14 @@ struct UnaryOperator : public COND_EXPR
         }
         return this->clone();
     }
-    virtual COND_EXPR *clone() const
+    virtual Expr *clone() const
     {
         UnaryOperator *newExp = new UnaryOperator(*this);
         newExp->unaryExp = unaryExp->clone();
         return newExp;
     }
     virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locs);
-    static UnaryOperator *Create(condNodeType t, COND_EXPR *sub_expr)
+    static UnaryOperator *Create(condNodeType t, Expr *sub_expr)
     {
         UnaryOperator *newExp = new UnaryOperator();
         newExp->m_type = t;
@@ -94,22 +95,22 @@ struct UnaryOperator : public COND_EXPR
 public:
     int hlTypeSize(Function *pproc) const;
     virtual std::string walkCondExpr(Function *pProc, int *numLoc) const;
-    virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
+    virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
     virtual hlType expType(Function *pproc) const;
-    virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx);
+    virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
 };
 
-struct BinaryOperator : public COND_EXPR
+struct BinaryOperator : public Expr
 {
     condOp      m_op;
-    COND_EXPR *m_lhs;
-    COND_EXPR *m_rhs;
-    BinaryOperator(condOp o) : COND_EXPR(BOOLEAN_OP)
+    Expr *m_lhs;
+    Expr *m_rhs;
+    BinaryOperator(condOp o) : Expr(BOOLEAN_OP)
     {
         m_op = o;
         m_lhs=m_rhs=nullptr;
     }
-    BinaryOperator(condOp o,COND_EXPR *l,COND_EXPR *r) : COND_EXPR(BOOLEAN_OP)
+    BinaryOperator(condOp o,Expr *l,Expr *r) : Expr(BOOLEAN_OP)
     {
         m_op = o;
         m_lhs=l;
@@ -121,51 +122,51 @@ struct BinaryOperator : public COND_EXPR
         delete m_lhs;
         delete m_rhs;
     }
-    static BinaryOperator *Create(condOp o,COND_EXPR *l,COND_EXPR *r)
+    static BinaryOperator *Create(condOp o,Expr *l,Expr *r)
     {
         BinaryOperator *res = new BinaryOperator(o);
         res->m_lhs = l;
         res->m_rhs = r;
         return res;
     }
-    static BinaryOperator *LogicAnd(COND_EXPR *l,COND_EXPR *r)
+    static BinaryOperator *LogicAnd(Expr *l,Expr *r)
     {
         return new BinaryOperator(DBL_AND,l,r);
     }
-    static BinaryOperator *And(COND_EXPR *l,COND_EXPR *r)
+    static BinaryOperator *And(Expr *l,Expr *r)
     {
         return new BinaryOperator(AND,l,r);
     }
-    static BinaryOperator *Or(COND_EXPR *l,COND_EXPR *r)
+    static BinaryOperator *Or(Expr *l,Expr *r)
     {
         return new BinaryOperator(OR,l,r);
     }
-    static BinaryOperator *LogicOr(COND_EXPR *l,COND_EXPR *r)
+    static BinaryOperator *LogicOr(Expr *l,Expr *r)
     {
         return new BinaryOperator(DBL_OR,l,r);
     }
-    static BinaryOperator *CreateAdd(COND_EXPR *l,COND_EXPR *r);
+    static BinaryOperator *CreateAdd(Expr *l,Expr *r);
     void changeBoolOp(condOp newOp);
-    virtual COND_EXPR *inverse() const;
-    virtual COND_EXPR *clone() const;
+    virtual Expr *inverse() const;
+    virtual Expr *clone() const;
     virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locs);
-    virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
-    virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx);
-    const COND_EXPR *lhs() const
+    virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
+    virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
+    const Expr *lhs() const
     {
-        return const_cast<const COND_EXPR *>(const_cast<BinaryOperator *>(this)->lhs());
+        return const_cast<const Expr *>(const_cast<BinaryOperator *>(this)->lhs());
     }
-    const COND_EXPR *rhs() const
+    const Expr *rhs() const
     {
-        return const_cast<const COND_EXPR *>(const_cast<BinaryOperator *>(this)->rhs());
+        return const_cast<const Expr *>(const_cast<BinaryOperator *>(this)->rhs());
     }
 
-    COND_EXPR *lhs()
+    Expr *lhs()
     {
         assert(m_type==BOOLEAN_OP);
         return m_lhs;
     }
-    COND_EXPR *rhs()
+    Expr *rhs()
     {
         assert(m_type==BOOLEAN_OP);
         return m_rhs;
@@ -182,31 +183,27 @@ struct AstIdent : public UnaryOperator
 {
     AstIdent() : UnaryOperator(IDENTIFIER)
     {
-        memset(&ident,0,sizeof(ident));
-    }
-    virtual COND_EXPR *clone() const
-    {
-        return new AstIdent(*this);
     }
     IDENTTYPE   ident;              /* for IDENTIFIER                   */
-    static AstIdent *  RegIdx(int idx, regType reg_type);
-    static AstIdent *  Kte(uint32_t kte, uint8_t size);
     static AstIdent *  Loc(int off, LOCAL_ID *localId);
-    static AstIdent *  Reg(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym);
     static AstIdent *  LongIdx(int idx);
+    static AstIdent *  String(uint32_t idx);
     static AstIdent *  Other(eReg seg, eReg regi, int16_t off);
-    static AstIdent *  idParam(int off, const STKFRAME *argSymtab);
-    static AstIdent *  idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
-    static AstIdent *  idFunc(Function *pproc, STKFRAME *args);
+    static AstIdent *  Param(int off, const STKFRAME *argSymtab);
+    static AstIdent *  Long(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
     static AstIdent *  idID(const ID *retVal, LOCAL_ID *locsym, iICODE ix_);
-    static COND_EXPR * id(const LLInst &ll_insn, opLoc sd, Function *pProc, iICODE ix_, ICODE &duIcode, operDu du);
+    static Expr * id(const LLInst &ll_insn, opLoc sd, Function *pProc, iICODE ix_, ICODE &duIcode, operDu du);
 
+    virtual Expr *clone() const
+    {
+        return new AstIdent(*this);
+    }
     virtual int hlTypeSize(Function *pproc) const;
     virtual hlType expType(Function *pproc) const;
-    virtual void performLongRemoval(eReg regi, LOCAL_ID *locId);
+    virtual Expr * performLongRemoval(eReg regi, LOCAL_ID *locId);
     virtual std::string walkCondExpr(Function *pProc, int *numLoc) const;
-    virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
-    virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx);
+    virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
+    virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
     virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);
 protected:
     eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl);
@@ -214,7 +211,94 @@ protected:
 };
 struct GlobalVariable : public AstIdent
 {
-    static AstIdent *Create(int16_t segValue, int16_t off);
+    bool valid;
+    int globIdx;
+    virtual Expr *clone() const
+    {
+        return new GlobalVariable(*this);
+    }
+    GlobalVariable(int16_t segValue, int16_t off);
+    std::string walkCondExpr(Function *pProc, int *numLoc) const;
+    int hlTypeSize(Function *pproc) const;
+    hlType expType(Function *pproc) const;
+};
+struct GlobalVariableIdx : public AstIdent
+{
+    bool valid;
+    int idxGlbIdx;	/* idx into localId, GLOB_VAR_IDX   */
+
+    virtual Expr *clone() const
+    {
+        return new GlobalVariableIdx(*this);
+    }
+    GlobalVariableIdx(int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym);
+    std::string walkCondExpr(Function *pProc, int *numLoc) const;
+    int hlTypeSize(Function *pproc) const;
+    hlType expType(Function *pproc) const;
+};
+struct Constant : public AstIdent
+{
+    struct _kte
+    {			/* for CONSTANT only					*/
+        uint32_t   kte;   	/*   value of the constant			*/
+        uint8_t    size;       /*   #bytes size constant	 		*/
+    } kte;
+
+    Constant(uint32_t _kte, uint8_t size)
+    {
+        ident.idType = CONSTANT;
+        kte.kte = _kte;
+        kte.size = size;
+    }
+    virtual Expr *clone() const
+    {
+        return new Constant(*this);
+    }
+    std::string walkCondExpr(Function *pProc, int *numLoc) const;
+    int hlTypeSize(Function *pproc) const;
+    hlType expType(Function *pproc) const;
+};
+struct FuncNode : public AstIdent
+{
+    struct _call {			/* for FUNCTION only				*/
+        Function     *proc;
+        STKFRAME *args;
+    } call;
+
+    FuncNode(Function *pproc, STKFRAME *args)
+    {
+        call.proc = pproc;
+        call.args = args;
+    }
+    virtual Expr *clone() const
+    {
+        return new FuncNode(*this);
+    }
+    std::string walkCondExpr(Function *pProc, int *numLoc) const;
+    int hlTypeSize(Function *pproc) const;
+    hlType expType(Function *pproc) const;
+};
+struct RegisterNode : public AstIdent
+{
+    regType     regiType;  /* for REGISTER only                */
+    int         regiIdx;   /* index into localId, REGISTER		*/
+
+    virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
+
+    RegisterNode(int idx, regType reg_type)
+    {
+        ident.type(REGISTER);
+        regiType = reg_type;
+        regiIdx = idx;
+    }
+
+    RegisterNode(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym);
+    virtual Expr *clone() const
+    {
+        return new RegisterNode(*this);
+    }
+    std::string walkCondExpr(Function *pProc, int *numLoc) const;
+    int hlTypeSize(Function *) const;
+    hlType expType(Function *pproc) const;
+    bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);
 };
-struct Constant : public COND_EXPR
-{};

+ 1 - 1
include/bundle.h

@@ -33,7 +33,7 @@ public:
     int current_indent;
 };
 
-
+extern bundle cCode;
 #define lineSize	360		/* 3 lines in the mean time */
 
 //void    newBundle (bundle *procCode);

+ 2 - 7
include/dcc.h

@@ -37,9 +37,6 @@ public:
         bool insertCallGraph(Function *caller, ilFunction callee);
         void insertArc(ilFunction newProc);
 };
-//#define NUM_PROCS_DELTA		5		/* delta # procs a proc invokes		 	*/
-//extern std::list<Function> pProcList;
-//extern FunctionListType pProcList;
 //extern CALL_GRAPH * callGraph;	/* Pointer to the head of the call graph     */
 extern bundle cCode;			/* Output C procedure's declaration and code */
 
@@ -110,11 +107,11 @@ void    udm(void);                                          /* udm.c        */
 void    freeCFG(BB * cfg);                                  /* graph.c      */
 BB *    newBB(BB *, int, int, uint8_t, int, Function *);    /* graph.c      */
 void    BackEnd(char *filename, CALL_GRAPH *);              /* backend.c    */
-char   *cChar(uint8_t c);                                   /* backend.c    */
+extern char   *cChar(uint8_t c);                            /* backend.c    */
 eErrorId scan(uint32_t ip, ICODE &p);                       /* scanner.c    */
 void    parse (CALL_GRAPH * *);                             /* parser.c     */
 
-int     strSize (uint8_t *, char);                          /* parser.c     */
+extern int     strSize (const uint8_t *, char);             /* parser.c     */
 //void    disassem(int pass, Function * pProc);             /* disassem.c   */
 void    interactDis(Function *, int initIC);       /* disassem.c   */
 bool    JmpInst(llIcode opcode);                            /* idioms.c     */
@@ -126,10 +123,8 @@ bool    LibCheck(Function &p);                              /* chklib.c     */
 
 /* Exported functions from procs.c */
 boolT	insertCallGraph (CALL_GRAPH *, ilFunction, ilFunction);
-void	adjustActArgType (COND_EXPR *, hlType, Function *);
 
 /* Exported functions from hlicode.c */
-std::string writeCall (Function *, STKFRAME &, Function *, int *);
 char 	*writeJcond (const HLTYPE &, Function *, int *);
 char 	*writeJcondInv (HLTYPE, Function *, int *);
 

+ 5 - 2
include/error.h

@@ -1,7 +1,10 @@
-/*****************************************************************************
+/*
+
+****************************************************************************
  * Error codes
  * (C) Cristina Cifuentes
- ****************************************************************************/
+***************************************************************************
+*/
 #pragma once
 
 /* These definitions refer to errorMessage in error.c */

+ 53 - 40
include/icode.h

@@ -65,6 +65,8 @@ struct LivenessSet : public std::bitset<32>
     {
         return test(r-rAX);
     }
+public:
+    LivenessSet &clrReg(int r);
 private:
     void postProcessCompositeRegs();
 };
@@ -83,15 +85,16 @@ struct DU
 #define MAX_REGS_DEF	4		/* 2 regs def'd for long-reg vars */
 
 
-struct COND_EXPR;
+struct Expr;
 struct AstIdent;
+struct UnaryOperator;
 struct HlTypeSupport
 {
     //hlIcode              opcode;    /* hlIcode opcode           */
     virtual bool        removeRegFromLong(eReg regi, LOCAL_ID *locId)=0;
     virtual std::string writeOut(Function *pProc, int *numLoc) const=0;
 protected:
-    void performLongRemoval (eReg regi, LOCAL_ID *locId, COND_EXPR *tree);
+    Expr * performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree);
 };
 
 struct CallType : public HlTypeSupport
@@ -100,9 +103,9 @@ struct CallType : public HlTypeSupport
     Function *      proc;
     STKFRAME *      args;   // actual arguments
     void allocStkArgs (int num);
-    bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc);
-    void placeStkArg(COND_EXPR *exp, int pos);
-    virtual COND_EXPR * toId();
+    bool newStkArg(Expr *exp, llIcode opcode, Function *pproc);
+    void placeStkArg(Expr *exp, int pos);
+    virtual Expr * toAst();
 public:
     bool removeRegFromLong(eReg /*regi*/, LOCAL_ID */*locId*/)
     {
@@ -114,20 +117,24 @@ public:
 struct AssignType : public HlTypeSupport
 {
     /* for HLI_ASSIGN */
-    COND_EXPR    *lhs;
-    COND_EXPR    *rhs;
-    AssignType() : lhs(0),rhs(0) {}
+protected:
+public:
+    Expr    *m_lhs;
+    Expr    *rhs;
+    AssignType() {}
+    Expr *lhs() const {return m_lhs;}
+    void lhs(Expr *l);
     bool removeRegFromLong(eReg regi, LOCAL_ID *locId);
     std::string writeOut(Function *pProc, int *numLoc) const;
 };
 struct ExpType : public HlTypeSupport
 {
     /* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
-    COND_EXPR    *v;
+    Expr    *v;
     ExpType() : v(0) {}
     bool removeRegFromLong(eReg regi, LOCAL_ID *locId)
     {
-        performLongRemoval(regi,locId,v);
+        v=performLongRemoval(regi,locId,v);
         return true;
     }
     std::string writeOut(Function *pProc, int *numLoc) const;
@@ -147,15 +154,21 @@ public:
         return const_cast<const HlTypeSupport *>(const_cast<HLTYPE*>(this)->get());
     }
 
-    void expr(COND_EXPR *e)
+    void expr(Expr *e)
     {
         assert(e);
         exp.v=e;
     }
-    void replaceExpr(COND_EXPR *e);
-    COND_EXPR * expr() { return exp.v;}
-    const COND_EXPR * expr() const  { return exp.v;}
-    void set(hlIcode i,COND_EXPR *e)
+    Expr *getMyExpr()
+    {
+        if(opcode==HLI_CALL)
+            return call.toAst();
+        return expr();
+    }
+    void replaceExpr(Expr *e);
+    Expr * expr() { return exp.v;}
+    const Expr * expr() const  { return exp.v;}
+    void set(hlIcode i,Expr *e)
     {
         if(i!=HLI_RET)
             assert(e);
@@ -163,7 +176,7 @@ public:
         opcode=i;
         exp.v=e;
     }
-    void set(COND_EXPR *l,COND_EXPR *r);
+    void set(Expr *l,Expr *r);
     void setCall(Function *proc);
     HLTYPE(hlIcode op=HLI_INVALID) : opcode(op)
     {}
@@ -179,7 +192,7 @@ public:
     }
 public:
     std::string write1HlIcode(Function *pProc, int *numLoc) const;
-    void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs);
+    void setAsgn(Expr *lhs, Expr *rhs);
 } ;
 /* LOW_LEVEL icode operand record */
 struct LLOperand
@@ -201,6 +214,16 @@ struct LLOperand
         proc.proc=0;
         proc.cb=0;
     }
+    bool operator==(const LLOperand &with) const
+    {
+        return (seg==with.seg) &&
+                (segOver==with.segOver) &&
+                (segValue==with.segValue) &&
+                (regi == with.regi) &&
+                (off == with.off) &&
+                (opz==with.opz) &&
+                (proc.proc==with.proc.proc);
+    }
     int64_t getImm2() const {return opz;}
     void SetImmediateOp(uint32_t dw)
     {
@@ -219,8 +242,6 @@ struct LLOperand
     static LLOperand CreateReg2(unsigned Val)
     {
         LLOperand Op;
-//        Op.Kind = kRegister;
-//        Op.RegVal = Reg;
         Op.regi = (eReg)Val;
         return Op;
     }
@@ -229,20 +250,15 @@ struct LLOperand
 struct LLInst : public llvm::MCInst //: public llvm::ilist_node<LLInst>
 {
 protected:
-    uint32_t     flg;            /* icode flags                  */
-//    LLOperand &get(int idx)
-//    {
-//        assert(idx<size());
-//        return getOperand(idx);
-//    }
-    LLOperand    m_src;            /* source operand               */
+    uint32_t        flg;            /* icode flags                  */
+    LLOperand       m_src;            /* source operand               */
 public:
-    int          codeIdx;    	/* Index into cCode.code            */
-    uint8_t      numBytes;       /* Number of bytes this instr   */
-    uint32_t     label;          /* offset in image (20-bit adr) */
-    LLOperand    dst;            /* destination operand          */
-    DU           flagDU;         /* def/use of flags				*/
-        int caseEntry;
+    int             codeIdx;    	/* Index into cCode.code            */
+    uint8_t         numBytes;       /* Number of bytes this instr   */
+    uint32_t        label;          /* offset in image (20-bit adr) */
+    LLOperand       dst;            /* destination operand          */
+    DU              flagDU;         /* def/use of flags				*/
+    int             caseEntry;
     std::vector<uint32_t> caseTbl2;
     int         hllLabNum;      /* label # for hll codegen      */
     bool conditionalJump()
@@ -260,9 +276,7 @@ public:
         flg &= ~flag;
     }
     uint32_t getFlag() const {return flg;}
-    //llIcode getOpcode() const { return opcode; }
-
-    uint32_t  GetLlLabel() const { return label;}
+    uint32_t GetLlLabel() const { return label;}
 
     void SetImmediateOp(uint32_t dw) {m_src.SetImmediateOp(dw);}
 
@@ -308,7 +322,6 @@ public:
 
     void flops(std::ostringstream &out);
     bool isJmpInst();
-    //HLTYPE toHighLevel(COND_EXPR *lhs, COND_EXPR *rhs, Function *func);
     HLTYPE createCall();
     LLInst(ICODE *container) : flg(0),codeIdx(0),numBytes(0),m_link(container)
     {
@@ -465,13 +478,13 @@ public:
     condId idType(opLoc sd);
     // HLL setting functions
     // set this icode to be an assign
-    void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs)
+    void setAsgn(Expr *lhs, Expr *rhs)
     {
         type=HIGH_LEVEL;
         hlU()->setAsgn(lhs,rhs);
     }
-    void setUnary(hlIcode op, COND_EXPR *_exp);
-    void setJCond(COND_EXPR *cexp);
+    void setUnary(hlIcode op, Expr *_exp);
+    void setJCond(Expr *cexp);
 
     void emitGotoLabel(int indLevel);
     void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
@@ -480,7 +493,7 @@ public:
 public:
     bool removeDefRegi(eReg regi, int thisDefIdx, LOCAL_ID *locId);
     void checkHlCall();
-    bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc)
+    bool newStkArg(Expr *exp, llIcode opcode, Function *pproc)
     {
         return hlU()->call.newStkArg(exp,opcode,pproc);
     }

+ 2 - 2
include/locident.h

@@ -18,7 +18,7 @@
 /* Type definition */
 // this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
 // TODO: why ?
-struct COND_EXPR;
+struct Expr;
 struct AstIdent;
 struct ICODE;
 struct LLInst;
@@ -132,7 +132,7 @@ public:
     size_t csym() const {return id_arr.size();}
     void newRegArg(iICODE picode, iICODE ticode) const;
     void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong) const;
-    void forwardSubs(COND_EXPR *lhs, COND_EXPR *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const;
+    void forwardSubs(Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const;
     AstIdent *createId(const ID *retVal, iICODE ix_);
 };
 

+ 4 - 0
include/state.h

@@ -30,6 +30,10 @@ struct STATE
         memset(r,0,sizeof(int16_t)*INDEX_BX_SI); //TODO: move this to machine_x86
         memset(f,0,sizeof(uint8_t)*INDEX_BX_SI);
     }
+    void setMemoryByte(uint32_t addr,uint8_t val)
+    {
+        //TODO: make this into a full scale value tracking class !
+    };
 };
 
 

+ 2 - 2
include/symtab.h

@@ -7,7 +7,7 @@
 #include <stdint.h>
 #include "Enums.h"
 #include "types.h"
-struct COND_EXPR;
+struct Expr;
 struct AstIdent;
 struct TypeContainer;
 /* * * * * * * * * * * * * * * * * */
@@ -36,7 +36,7 @@ struct SYM : public SymbolCommon
 struct STKSYM : public SymbolCommon
 {
     typedef int16_t tLabel;
-    COND_EXPR	*actual;	/* Expression tree of actual parameter 		*/
+    Expr	*actual;	/* Expression tree of actual parameter 		*/
     AstIdent 	*regs;		/* For register arguments only				*/
     tLabel      label;        /* Immediate off from BP (+:args, -:params) */
     uint8_t     regOff;     /* Offset is a register (e.g. SI, DI)       */

+ 1 - 0
regression_tester.rb

@@ -14,6 +14,7 @@ def perform_test(exepath,filepath,outname,args)
 	filepath=path_local(filepath)
 	joined_args = args.join(' ')
 	printf("calling:" + "#{exepath} -a1 #{joined_args} -o#{output_path}.a1 #{filepath}\n")
+	STDERR << "Errors for : #{filepath}"
 	result = `#{exepath} -a1 -o#{output_path}.a1 #{filepath}`
 	result = `#{exepath} -a2 #{joined_args} -o#{output_path}.a2 #{filepath}`
 	result = `#{exepath} #{joined_args} -o#{output_path} #{filepath}`

+ 0 - 1
src/BasicBlock.cpp

@@ -408,7 +408,6 @@ ICODE &BB::back()
 
 size_t BB::size()
 {
-
     return distance(instructions.begin(),instructions.end());
 }
 

+ 3 - 3
src/Procedure.cpp

@@ -14,7 +14,7 @@ void JumpTable::pruneEntries(uint16_t cs)
     PROG *prg(Project::get()->binary());
     for (uint32_t i = start; i < finish; i += 2)
     {
-        uint32_t target = cs + LH(&prg->Image[i]);
+        uint32_t target = cs + LH(&prg->image()[i]);
         if (target < finish && target >= start)
             finish = target;
         else if (target >= (uint32_t)prg->cbImage)
@@ -23,9 +23,9 @@ void JumpTable::pruneEntries(uint16_t cs)
     ICODE _Icode; // used as scan input
     for (uint32_t i = start; i < finish; i += 2)
     {
-        uint32_t target = cs + LH(&prg->Image[i]);
+        uint32_t target = cs + LH(&prg->image()[i]);
         /* Be wary of 00 00 as code - it's probably data */
-        if (! (prg->Image[target] || prg->Image[target+1]) || scan(target, _Icode))
+        if (! (prg->image()[target] || prg->image()[target+1]) || scan(target, _Icode))
             finish = i;
     }
 

+ 193 - 308
src/ast.cpp

@@ -9,11 +9,24 @@
 #include <sstream>
 #include <iostream>
 #include <cassert>
+#include <boost/range.hpp>
+#include <boost/range/adaptors.hpp>
+#include <boost/range/algorithm.hpp>
+#include <boost/assign.hpp>
+
 #include "types.h"
-#include "dcc.h"
+#include "ast.h"
+#include "bundle.h"
 #include "machine_x86.h"
 #include "project.h"
 using namespace std;
+using namespace boost;
+using namespace boost::adaptors;
+extern int     strSize (const uint8_t *, char);
+extern char   *cChar(uint8_t c);
+
+
+
 // Conditional operator symbols in C.  Index by condOp enumeration type
 static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
                                           " & ", " | ", " ^ ", " ~ ",
@@ -97,61 +110,27 @@ void ICODE::copyDU(const ICODE &duIcode, operDu _du, operDu duDu)
 
 
 /* Returns an identifier conditional expression node of type GLOB_VAR */
-AstIdent *GlobalVariable::Create(int16_t segValue, int16_t off)
+GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
 {
-    AstIdent *newExp;
     uint32_t adr;
-
-    newExp = new AstIdent();
-    newExp->ident.idType = GLOB_VAR;
+    valid = true;
+    ident.idType = GLOB_VAR;
     adr = opAdr(segValue, off);
     auto i=Project::get()->getSymIdxByAdd(adr);
     if ( not Project::get()->validSymIdx(i) )
     {
         printf ("Error, glob var not found in symtab\n");
-        delete newExp;
-        return 0;
+        valid = false;
     }
-    newExp->ident.idNode.globIdx = i;
-    return (newExp);
+    globIdx = i;
 }
 
-
-/* Returns an identifier conditional expression node of type REGISTER */
-AstIdent *AstIdent::Reg(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym)
+string GlobalVariable::walkCondExpr(Function *, int *) const
 {
-    AstIdent *newExp;
-
-    newExp = new AstIdent();
-    newExp->ident.idType = REGISTER;
-    hlType type_sel;
-    regType reg_type;
-    if ((icodeFlg & B) || (icodeFlg & SRC_B))
-    {
-        type_sel = TYPE_BYTE_SIGN;
-        reg_type = BYTE_REG;
-    }
-    else    /* uint16_t */
-    {
-        type_sel = TYPE_WORD_SIGN;
-        reg_type = WORD_REG;
-    }
-    newExp->ident.idNode.regiIdx = locsym->newByteWordReg(type_sel, regi);
-    newExp->ident.regiType = reg_type;
-    return (newExp);
-}
-
-
-/* Returns an identifier conditional expression node of type REGISTER */
-AstIdent *AstIdent::RegIdx(int idx, regType reg_type)
-{
-    AstIdent *newExp;
-
-    newExp = new AstIdent();
-    newExp->ident.idType = REGISTER;
-    newExp->ident.regiType = reg_type;
-    newExp->ident.idNode.regiIdx = idx;
-    return (newExp);
+    if(valid)
+        return Project::get()->symtab[globIdx].name;
+    else
+        return "INVALID GlobalVariable";
 }
 
 /* Returns an identifier conditional expression node of type LOCAL_VAR */
@@ -177,7 +156,7 @@ AstIdent *AstIdent::Loc(int off, LOCAL_ID *localId)
 
 
 /* Returns an identifier conditional expression node of type PARAM */
-AstIdent *AstIdent::idParam(int off, const STKFRAME * argSymtab)
+AstIdent *AstIdent::Param(int off, const STKFRAME * argSymtab)
 {
     AstIdent *newExp;
 
@@ -193,11 +172,10 @@ AstIdent *AstIdent::idParam(int off, const STKFRAME * argSymtab)
 
 /* Returns an identifier conditional expression node of type GLOB_VAR_IDX.
  * This global variable is indexed by regi.     */
-AstIdent *idCondExpIdxGlob (int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym)
+GlobalVariableIdx::GlobalVariableIdx (int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym)
 {
     size_t i;
-    AstIdent *newExp  = new AstIdent();
-    newExp->ident.idType = GLOB_VAR_IDX;
+    ident.type(GLOB_VAR_IDX);
     for (i = 0; i < locSym->csym(); i++)
     {
         const BWGLB_TYPE &lID(locSym->id_arr[i].id.bwGlb);
@@ -206,19 +184,14 @@ AstIdent *idCondExpIdxGlob (int16_t segValue, int16_t off, uint8_t regi, const L
     }
     if (i == locSym->csym())
         printf ("Error, indexed-glob var not found in local id table\n");
-    newExp->ident.idNode.idxGlbIdx = i;
-    return (newExp);
+    idxGlbIdx = i;
 }
-
-
-/* Returns an identifier conditional expression node of type CONSTANT */
-AstIdent *AstIdent::Kte(uint32_t kte, uint8_t size)
+string GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
 {
-    AstIdent *newExp = new AstIdent();
-    newExp->ident.idType = CONSTANT;
-    newExp->ident.idNode.kte.kte = kte;
-    newExp->ident.idNode.kte.size = size;
-    return (newExp);
+    ostringstream o;
+    auto bwGlb = &pProc->localId.id_arr[idxGlbIdx].id.bwGlb;
+    o << (bwGlb->seg << 4) + bwGlb->off <<  "["<<Machine_X86::regName(bwGlb->regi)<<"]";
+    return o.str();
 }
 
 
@@ -232,27 +205,34 @@ AstIdent *AstIdent::LongIdx (int idx)
     return (newExp);
 }
 
+AstIdent *AstIdent::String(uint32_t idx)
+{
+    AstIdent *newExp = new AstIdent;
+    newExp->ident.idNode.strIdx = idx;
+    newExp->ident.type(STRING);
+    return newExp;
+}
+
 
 /* Returns an identifier conditional expression node of type LONG_VAR */
-AstIdent *AstIdent::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset)
+AstIdent *AstIdent::Long(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset)
 {
     int idx;
-    AstIdent *newExp  = new AstIdent();
+    AstIdent *newExp;
     /* Check for long constant and save it as a constant expression */
     if ((sd == SRC) && pIcode->ll()->testFlags(I))  /* constant */
     {
-        newExp->ident.idType = CONSTANT;
         int value;
         if (f == HIGH_FIRST)
             value = (pIcode->ll()->src().getImm2() << 16) + atOffset.src().getImm2();
         else/* LOW_FIRST */
             value = (atOffset.src().getImm2() << 16)+ pIcode->ll()->src().getImm2();
-        newExp->ident.idNode.kte.kte = value;
-        newExp->ident.idNode.kte.size = 4;
+        newExp = new Constant(value,4);
     }
     /* Save it as a long expression (reg, stack or glob) */
     else
     {
+        newExp  = new AstIdent();
         idx = localId->newLong(sd, pIcode, f, ix, du, atOffset);
         newExp->ident.idType = LONG_VAR;
         newExp->ident.idNode.longIdx = idx;
@@ -260,19 +240,6 @@ AstIdent *AstIdent::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f
     return (newExp);
 }
 
-
-/* Returns an identifier conditional expression node of type FUNCTION */
-AstIdent *AstIdent::idFunc(Function * pproc, STKFRAME * args)
-{
-    AstIdent *newExp  = new AstIdent();
-
-    newExp->ident.idType = FUNCTION;
-    newExp->ident.idNode.call.proc = pproc;
-    newExp->ident.idNode.call.args = args;
-    return (newExp);
-}
-
-
 /* Returns an identifier conditional expression node of type OTHER.
  * Temporary solution, should really be encoded as an indexed type (eg.
  * arrays). */
@@ -291,20 +258,26 @@ AstIdent *AstIdent::Other(eReg seg, eReg regi, int16_t off)
  * TYPE_WORD_SIGN	*/
 AstIdent *AstIdent::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
 {
-    AstIdent *newExp  = new AstIdent();
     int idx;
-
-    if (retVal->type == TYPE_LONG_SIGN)
-    {
-        idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->id.longId.h,retVal->id.longId.l, ix_);
-        newExp->ident.idType = LONG_VAR;
-        newExp->ident.idNode.longIdx = idx;
-    }
-    else if (retVal->type == TYPE_WORD_SIGN)
+    AstIdent *newExp=nullptr;
+    switch(retVal->type)
     {
-        newExp->ident.idType = REGISTER;
-        newExp->ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_WORD_SIGN, retVal->id.regi);
-        newExp->ident.regiType = WORD_REG;
+        case TYPE_LONG_SIGN:
+        {
+            newExp  = new AstIdent();
+            idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->id.longId.h,retVal->id.longId.l, ix_);
+            newExp->ident.idType = LONG_VAR;
+            newExp->ident.idNode.longIdx = idx;
+            break;
+        }
+        case TYPE_WORD_SIGN:
+            newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),WORD_REG);
+            break;
+        case TYPE_BYTE_SIGN:
+            newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),BYTE_REG);
+            break;
+        default:
+            fprintf(stderr,"AstIdent::idID unhandled type %d\n",retVal->type);
     }
     return (newExp);
 }
@@ -315,9 +288,9 @@ AstIdent *AstIdent::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
  * Arguments:
  *            duIcode: icode instruction that needs the du set.
  *            du: operand is defined or used in current instruction.    */
-COND_EXPR *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
+Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
 {
-    COND_EXPR *newExp;
+    Expr *newExp;
 
     int idx;          /* idx into pIcode->localId table */
 
@@ -335,35 +308,33 @@ COND_EXPR *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICOD
 
     else if ((sd == DST) && ll_insn.testFlags(IM_TMP_DST))
     {                                                   /* implicit tmp */
-        newExp = AstIdent::Reg (rTMP, 0, &pProc->localId);
+        newExp = new RegisterNode(rTMP, 0, &pProc->localId);
         duIcode.setRegDU(rTMP, (operDu)eUSE);
     }
 
     else if ((sd == SRC) && ll_insn.testFlags(I)) /* constant */
-        newExp = AstIdent::Kte (ll_insn.src().getImm2(), 2);
+        newExp = new Constant(ll_insn.src().getImm2(), 2);
     else if (pm.regi == rUNDEF) /* global variable */
-        newExp = GlobalVariable::Create(pm.segValue, pm.off);
+        newExp = new GlobalVariable(pm.segValue, pm.off);
     else if ( pm.isReg() )      /* register */
     {
-        newExp = AstIdent::Reg (pm.regi, (sd == SRC) ? ll_insn.getFlag() :
-                                                       ll_insn.getFlag() & NO_SRC_B,
-                                &pProc->localId);
+        newExp = new RegisterNode(pm.regi, (sd == SRC) ? ll_insn.getFlag() : ll_insn.getFlag() & NO_SRC_B, &pProc->localId);
         duIcode.setRegDU( pm.regi, du);
     }
 
     else if (pm.off)                                   /* offset */
-    {
+    { // TODO: this is ABI specific, should be actually based on Function calling conv
         if ((pm.seg == rSS) && (pm.regi == INDEX_BP)) /* idx on bp */
         {
             if (pm.off >= 0)                           /* argument */
-                newExp = AstIdent::idParam (pm.off, &pProc->args);
+                newExp = AstIdent::Param (pm.off, &pProc->args);
             else                                        /* local variable */
                 newExp = AstIdent::Loc (pm.off, &pProc->localId);
         }
         else if ((pm.seg == rDS) && (pm.regi == INDEX_BX)) /* bx */
         {
             if (pm.off > 0)        /* global variable */
-                newExp = idCondExpIdxGlob (pm.segValue, pm.off, rBX,&pProc->localId);
+                newExp = new GlobalVariableIdx(pm.segValue, pm.off, rBX,&pProc->localId);
             else
                 newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
             duIcode.setRegDU( rBX, eUSE);
@@ -377,26 +348,18 @@ COND_EXPR *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICOD
     {
         if ((pm.seg == rDS) && (pm.regi > INDEX_BP_DI)) /* dereference */
         {
+            eReg selected;
             switch (pm.regi) {
-                case INDEX_SI:
-                    newExp = AstIdent::Reg(rSI, 0, &pProc->localId);
-                    duIcode.setRegDU( rSI, du);
-                    break;
-                case INDEX_DI:
-                    newExp = AstIdent::Reg(rDI, 0, &pProc->localId);
-                    duIcode.setRegDU( rDI, du);
-                    break;
-                case INDEX_BP:
-                    newExp = AstIdent::Reg(rBP, 0, &pProc->localId);
-                    break;
-                case INDEX_BX:
-                    newExp = AstIdent::Reg(rBX, 0, &pProc->localId);
-                    duIcode.setRegDU( rBX, du);
-                    break;
+                case INDEX_SI: selected = rSI; break;
+                case INDEX_DI: selected = rDI; break;
+                case INDEX_BP: selected = rBP; break;
+                case INDEX_BX: selected = rBX; break;
                 default:
                     newExp = 0;
                     assert(false);
             }
+            newExp = new RegisterNode(selected, 0, &pProc->localId);
+            duIcode.setRegDU( selected, du);
             newExp = UnaryOperator::Create(DEREFERENCE, newExp);
         }
         else
@@ -433,30 +396,11 @@ condId LLInst::idType(opLoc sd) const
 /* Size of hl types */
 int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
 
-int COND_EXPR::hlTypeSize(Function * pproc) const
+int Expr::hlTypeSize(Function * pproc) const
 {
     if (this == NULL)
         return (2);		/* for TYPE_UNKNOWN */
-
-    switch (m_type) {
-        case BOOLEAN_OP:
-            assert(false);
-            return 0;
-            //            return expr->hlTypeSize(pproc);
-        case NEGATION:	case ADDRESSOF:
-        case POST_INC:	case POST_DEC:
-        case PRE_INC:		case PRE_DEC:
-        case DEREFERENCE:
-            assert(false);
-            return 0;
-            //return expr->hlTypeSize(pproc);
-        case IDENTIFIER:
-            assert(false);
-            return 0;
-        default:
-            fprintf(stderr,"hlTypeSize queried for Unkown type %d \n",m_type);
-            break;
-    }
+    fprintf(stderr,"hlTypeSize queried for Unkown type %d \n",m_type);
     return 2;			// CC: is this correct?
 }
 
@@ -469,33 +413,35 @@ int UnaryOperator::hlTypeSize(Function *pproc) const
 {
     return (unaryExp->hlTypeSize (pproc));
 }
+int GlobalVariable::hlTypeSize(Function *pproc) const
+{
+    return (Project::get()->symbolSize(globIdx));
+}
+int GlobalVariableIdx::hlTypeSize(Function *pproc) const
+{
+    return (hlSize[pproc->localId.id_arr[idxGlbIdx].type]);
+}
+
 int AstIdent::hlTypeSize(Function *pproc) const
 {
     switch (ident.idType)
     {
         case GLOB_VAR:
-            return (Project::get()->symbolSize(ident.idNode.globIdx));
-        case REGISTER:
-            if (ident.regiType == BYTE_REG)
-                return (1);
-            else
-                return (2);
+            assert(false);
+            return 1;
         case LOCAL_VAR:
             return (hlSize[pproc->localId.id_arr[ident.idNode.localIdx].type]);
         case PARAM:
             return (hlSize[pproc->args[ident.idNode.paramIdx].type]);
-        case GLOB_VAR_IDX:
-            return (hlSize[pproc->localId.id_arr[ident.idNode.idxGlbIdx].type]);
-        case CONSTANT:
-            return (ident.idNode.kte.size);
         case STRING:
             return (2);
         case LONG_VAR:
             return (4);
-        case FUNCTION:
-            return (hlSize[ident.idNode.call.proc->retVal.type]);
         case OTHER:
             return (2);
+        default:
+            assert(false);
+            return -1;
     } /* eos */
 }
 hlType BinaryOperator::expType(Function *pproc) const
@@ -516,70 +462,47 @@ hlType UnaryOperator::expType(Function *pproc) const
 {
     return unaryExp->expType (pproc);
 }
+hlType GlobalVariable::expType(Function *pproc) const
+{
+    return Project::get()->symbolType(globIdx);
+}
+hlType GlobalVariableIdx::expType(Function *pproc) const
+{
+    return (pproc->localId.id_arr[idxGlbIdx].type);
+}
+
 hlType AstIdent::expType(Function *pproc) const
 {
     switch (ident.idType)
     {
-        case GLOB_VAR:
-            return Project::get()->symbolType(ident.idNode.globIdx);
+        case UNDEF:
+        case CONSTANT:
+        case FUNCTION:
         case REGISTER:
-            if (ident.regiType == BYTE_REG)
-                return (TYPE_BYTE_SIGN);
-            else
-                return (TYPE_WORD_SIGN);
+        case GLOB_VAR:
+        case GLOB_VAR_IDX:
+            assert(false);
+            return TYPE_UNKNOWN;
         case LOCAL_VAR:
             return (pproc->localId.id_arr[ident.idNode.localIdx].type);
         case PARAM:
             return (pproc->args[ident.idNode.paramIdx].type);
-        case GLOB_VAR_IDX:
-            return (pproc->localId.id_arr[ident.idNode.idxGlbIdx].type);
-        case CONSTANT:
-            return (TYPE_CONST);
         case STRING:
             return (TYPE_STR);
         case LONG_VAR:
             return (pproc->localId.id_arr[ident.idNode.longIdx].type);
-        case FUNCTION:
-            return (ident.idNode.call.proc->retVal.type);
         default:
             return (TYPE_UNKNOWN);
     } /* eos */
     return (TYPE_UNKNOWN);
 }
 /* Returns the type of the expression */
-hlType COND_EXPR::expType(Function * pproc) const
-{
-
-    if (this == nullptr)
-        return (TYPE_UNKNOWN);
-
-    switch (m_type)
-    {
-        case BOOLEAN_OP:
-            assert(false);
-            return TYPE_UNKNOWN;
-        case POST_INC: case POST_DEC:
-        case PRE_INC:  case PRE_DEC:
-        case NEGATION:
-            assert(false);
-            return TYPE_UNKNOWN;
-        case ADDRESSOF:	return (TYPE_PTR);		/***????****/
-        case DEREFERENCE:	return (TYPE_PTR);
-        case IDENTIFIER:
-            assert(false);
-            return TYPE_UNKNOWN;
-        case UNKNOWN_OP:
-            assert(false);
-            return (TYPE_UNKNOWN);
-    }
-    return TYPE_UNKNOWN;		// CC: Correct?
-}
 
 
 /* Removes the register from the tree.  If the register was part of a long
  * register (eg. dx:ax), the node gets transformed into an integer register
  * node.        */
-void HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, COND_EXPR *tree)
+Expr * HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree)
 {
     switch (tree->m_type) {
         case BOOLEAN_OP:
@@ -588,12 +511,13 @@ void HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, COND_EXPR *t
         case NEGATION: case ADDRESSOF:
         case DEREFERENCE:
         case IDENTIFIER:
-            tree->performLongRemoval(regi,locId);
+            return tree->performLongRemoval(regi,locId);
             break;
         default:
             fprintf(stderr,"performLongRemoval attemped on %d\n",tree->m_type);
             break;
     }
+    return tree;
 }
 
 /* Returns the string located in image, formatted in C format. */
@@ -603,10 +527,10 @@ static std::string getString (int offset)
     ostringstream o;
     int strLen, i;
 
-    strLen = strSize (&prog.Image[offset], '\0');
+    strLen = strSize (&prog.image()[offset], '\0');
     o << '"';
     for (i = 0; i < strLen; i++)
-        o<<cChar(prog.Image[offset+i]);
+        o<<cChar(prog.image()[offset+i]);
     o << "\"\0";
     return (o.str());
 }
@@ -614,7 +538,11 @@ string BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
 {
     std::ostringstream outStr;
     outStr << "(";
-    outStr << lhs()->walkCondExpr(pProc, numLoc);
+    if(m_op!=NOT)
+    {
+        outStr << lhs()->walkCondExpr(pProc, numLoc);
+    }
+    assert(rhs());
     outStr << condOpSym[m_op];
     outStr << rhs()->walkCondExpr(pProc, numLoc);
     outStr << ")";
@@ -631,23 +559,6 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
     std::ostringstream o;
     switch (ident.idType)
     {
-        case GLOB_VAR:
-            o << Project::get()->symtab[ident.idNode.globIdx].name;
-            break;
-        case REGISTER:
-            id = &pProc->localId.id_arr[ident.idNode.regiIdx];
-            if (id->name[0] == '\0')	/* no name */
-            {
-                id->setLocalName(++(*numLoc));
-                codeOut <<TypeContainer::typeName(id->type)<< " "<<id->name<<"; ";
-                codeOut <<"/* "<<Machine_X86::regName(id->id.regi)<<" */\n";
-            }
-            if (id->hasMacro)
-                o << id->macro << "("<<id->name<<")";
-            else
-                o << id->name;
-            break;
-
         case LOCAL_VAR:
             o << pProc->localId.id_arr[ident.idNode.localIdx].name;
             break;
@@ -659,19 +570,6 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
             else
                 o << psym->name;
             break;
-
-        case GLOB_VAR_IDX:
-            bwGlb = &pProc->localId.id_arr[ident.idNode.idxGlbIdx].id.bwGlb;
-            o << (bwGlb->seg << 4) + bwGlb->off <<  "["<<Machine_X86::regName(bwGlb->regi)<<"]";
-            break;
-
-        case CONSTANT:
-            if (ident.idNode.kte.kte < 1000)
-                o << ident.idNode.kte.kte;
-            else
-                o << "0x"<<std::hex << ident.idNode.kte.kte;
-            break;
-
         case STRING:
             o << getString (ident.idNode.strIdx);
             break;
@@ -697,11 +595,6 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
                     o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
             }
             break;
-
-        case FUNCTION:
-            o << writeCall (ident.idNode.call.proc,*ident.idNode.call.args, pProc, numLoc);
-            break;
-
         case OTHER:
             off = ident.idNode.other.off;
             o << Machine_X86::regName(ident.idNode.other.seg)<< "[";
@@ -711,6 +604,12 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
             else if (off>0)
                 o << "+"<< hexStr (off);
             o << "]";
+            break;
+        default:
+            assert(false);
+            return "";
+
+
     } /* eos */
     outStr << o.str();
     cCode.appendDecl(codeOut.str());
@@ -783,35 +682,6 @@ string UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
 
 
 
-/* Makes a copy of the given expression.  Allocates newExp storage for each
- * node.  Returns the copy. */
-COND_EXPR *COND_EXPR::clone() const
-{
-    COND_EXPR* newExp=nullptr;        /* Expression node copy */
-
-    switch (m_type)
-    {
-        case BOOLEAN_OP:
-            assert(false);
-            break;
-
-        case NEGATION:
-        case ADDRESSOF:
-        case DEREFERENCE:
-        case PRE_DEC: case POST_DEC:
-        case PRE_INC: case POST_INC:
-            assert(false);
-            break;
-
-        case IDENTIFIER:
-            assert(false);
-            break;
-
-        default:
-            fprintf(stderr,"Clone attempt on unhandled type %d\n",m_type);
-    }
-    return (newExp);
-}
 
 
 /* Changes the boolean conditional operator at the root of this expression */
@@ -819,9 +689,9 @@ void BinaryOperator::changeBoolOp (condOp newOp)
 {
     m_op = newOp;
 }
-bool COND_EXPR::insertSubTreeReg (AstIdent *&tree, COND_EXPR *_expr, eReg regi,const LOCAL_ID *locsym)
+bool Expr::insertSubTreeReg (AstIdent *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
 {
-    COND_EXPR *nd = tree;
+    Expr *nd = tree;
     bool res=insertSubTreeReg (nd, _expr, regi,locsym);
     if(res)
     {
@@ -833,12 +703,12 @@ bool COND_EXPR::insertSubTreeReg (AstIdent *&tree, COND_EXPR *_expr, eReg regi,c
 }
 /* Inserts the expression exp into the tree at the location specified by the
  * register regi */
-bool COND_EXPR::insertSubTreeReg (COND_EXPR *&tree, COND_EXPR *_expr, eReg regi,const LOCAL_ID *locsym)
+bool Expr::insertSubTreeReg (Expr *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
 {
 
     if (tree == NULL)
         return false;
-    COND_EXPR *temp=tree->insertSubTreeReg(_expr,regi,locsym);
+    Expr *temp=tree->insertSubTreeReg(_expr,regi,locsym);
     if(nullptr!=temp)
     {
         tree=temp;
@@ -847,11 +717,9 @@ bool COND_EXPR::insertSubTreeReg (COND_EXPR *&tree, COND_EXPR *_expr, eReg regi,
     return false;
 }
 
-COND_EXPR *UnaryOperator::insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym)
+Expr *UnaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
 {
-
-    eReg treeReg;
-    COND_EXPR *temp;
+    Expr *temp;
 
     switch (m_type) {
         case NEGATION:
@@ -869,9 +737,9 @@ COND_EXPR *UnaryOperator::insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LO
     }
     return nullptr;
 }
-COND_EXPR *BinaryOperator::insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym)
+Expr *BinaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
 {
-    COND_EXPR *r;
+    Expr *r;
     if(this->op()!=NOT)
     {
         assert(m_lhs);
@@ -891,30 +759,21 @@ COND_EXPR *BinaryOperator::insertSubTreeReg(COND_EXPR *_expr, eReg regi, const L
     }
     return nullptr;
 }
-COND_EXPR *AstIdent::insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym)
+Expr *AstIdent::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
 {
-    eReg treeReg;
     if (ident.idType == REGISTER)
     {
-        treeReg = locsym->id_arr[ident.idNode.regiIdx].id.regi;
-        if (treeReg == regi)                        /* uint16_t reg */
-        {
-            return _expr;
-        }
-        else if(Machine_X86::isSubRegisterOf(treeReg,regi))    /* uint16_t/uint8_t reg */
-        {
-            return _expr;
-        }
+        assert(false);
     }
     return nullptr;
 }
 /* Inserts the expression exp into the tree at the location specified by the
  * long register index longIdx*/
-bool COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, COND_EXPR *&tree, int longIdx)
+bool Expr::insertSubTreeLongReg(Expr *_expr, Expr *&tree, int longIdx)
 {
     if (tree == NULL)
         return false;
-    COND_EXPR *temp=tree->insertSubTreeLongReg(_expr,longIdx);
+    Expr *temp=tree->insertSubTreeLongReg(_expr,longIdx);
     if(nullptr!=temp)
     {
         tree=temp;
@@ -922,9 +781,9 @@ bool COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, COND_EXPR *&tree, int lon
     }
     return false;
 }
-COND_EXPR *UnaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
+Expr *UnaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
 {
-    COND_EXPR *temp = unaryExp->insertSubTreeLongReg(_expr,longIdx);
+    Expr *temp = unaryExp->insertSubTreeLongReg(_expr,longIdx);
     if (nullptr!=temp)
     {
         unaryExp = temp;
@@ -932,9 +791,9 @@ COND_EXPR *UnaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
     }
     return nullptr;
 }
-COND_EXPR *BinaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
+Expr *BinaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
 {
-    COND_EXPR *r;
+    Expr *r;
     if(m_op!=NOT)
     {
         r=m_lhs->insertSubTreeLongReg(_expr,longIdx);
@@ -952,7 +811,7 @@ COND_EXPR *BinaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
     }
     return nullptr;
 }
-COND_EXPR *AstIdent::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
+Expr *AstIdent::insertSubTreeLongReg(Expr *_expr, int longIdx)
 {
     if (ident.idNode.longIdx == longIdx)
     {
@@ -962,24 +821,12 @@ COND_EXPR *AstIdent::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
 }
 
 /* Recursively deallocates the abstract syntax tree rooted at *exp */
-COND_EXPR::~COND_EXPR()
-{
-    switch (m_type)
-    {
-        case BOOLEAN_OP:
-        case NEGATION:
-        case ADDRESSOF:
-        case DEREFERENCE:
-        case IDENTIFIER:
-            break;
-        default:
-            fprintf(stderr,"release attempt on unhandled type %d\n",m_type);
-    }
-}
+Expr::~Expr(){}
+
 /* Makes a copy of the given expression.  Allocates newExp storage for each
  * node.  Returns the copy. */
 
-COND_EXPR *BinaryOperator::clone() const
+Expr *BinaryOperator::clone() const
 {
     BinaryOperator* newExp=new BinaryOperator(m_op);        /* Expression node copy */
     newExp->m_lhs = m_lhs->clone();
@@ -987,7 +834,7 @@ COND_EXPR *BinaryOperator::clone() const
     return newExp;
 }
 
-COND_EXPR *BinaryOperator::inverse() const
+Expr *BinaryOperator::inverse() const
 {
     static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
                                  LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
@@ -1017,18 +864,17 @@ COND_EXPR *BinaryOperator::inverse() const
     return res;
 
 }
-void AstIdent::performLongRemoval(eReg regi, LOCAL_ID *locId)
+Expr *AstIdent::performLongRemoval(eReg regi, LOCAL_ID *locId)
 {
     eReg otherRegi;         /* high or low part of long register */
 
-    IDENTTYPE* ident_2 = &ident;
-    if (ident_2->idType == LONG_VAR)
+    if (ident.idType == LONG_VAR)
     {
-        otherRegi = otherLongRegi (regi, ident_2->idNode.longIdx, locId);
-        ident_2->idType = REGISTER;
-        ident_2->regiType = WORD_REG;
-        ident_2->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
+        otherRegi = otherLongRegi (regi, ident.idNode.longIdx, locId);
+        delete this;
+        return new RegisterNode(locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi),WORD_REG);
     }
+    return this;