home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / src / i386.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-11  |  10.3 KB  |  281 lines

  1. /* i386.h -- Header file for asm.c */
  2.    
  3. #define MAX_OPERANDS 3        /* max operands per insn */
  4. #define MAX_PREFIXES 4        /* max prefixes per opcode */
  5. #define MAX_IMMEDIATE_OPERANDS 2 /* max immediates per insn */
  6. #define MAX_MEMORY_OPERANDS 2    /* max memory ref per insn
  7.                  * lcall uses 2
  8.                  */
  9. /* we define the syntax here (modulo base,index,scale syntax) */
  10. #define REGISTER_PREFIX '%'
  11. #define IMMEDIATE_PREFIX '$'
  12. #define ABSOLUTE_PREFIX '*'
  13. #define PREFIX_SEPERATOR '/'
  14.  
  15. #define TWO_BYTE_OPCODE_ESCAPE 0x0f
  16.  
  17. /* register numbers */
  18. #define EBP_REG_NUM 5
  19. #define ESP_REG_NUM 4
  20.  
  21. /* modrm_byte.regmem for twobyte escape */
  22. #define ESCAPE_TO_TWO_BYTE_ADDRESSING ESP_REG_NUM
  23. /* index_base_byte.index for no index register addressing */
  24. #define NO_INDEX_REGISTER ESP_REG_NUM
  25. /* index_base_byte.base for no base register addressing */
  26. #define NO_BASE_REGISTER EBP_REG_NUM
  27.  
  28. /* these are the att as opcode suffixes, making movl --> mov, for example */
  29. #define DWORD_OPCODE_SUFFIX 'l'
  30. #define WORD_OPCODE_SUFFIX  'w'
  31. #define BYTE_OPCODE_SUFFIX  'b'
  32.  
  33. /* modrm.mode = REGMEM_FIELD_HAS_REG when a register is in there */
  34. #define REGMEM_FIELD_HAS_REG 0x3                /* always = 0x3 */
  35. #define REGMEM_FIELD_HAS_MEM (~REGMEM_FIELD_HAS_REG)
  36.  
  37. #define END_OF_INSN '\0'
  38.  
  39. /*
  40. When an operand is read in it is classified by its type.  This type includes
  41. all the possible ways an operand can be used.  Thus, '%eax' is both 'register
  42. # 0' and 'The Accumulator'.  In our language this is expressed by OR'ing
  43. 'Reg32' (any 32 bit register) and 'Acc' (the accumulator).
  44. Operands are classified so that we can match given operand types with
  45. the opcode table in i386-opcode.h.
  46.  */
  47. #define Unknown 0x0
  48. /* register */
  49. #define Reg8    0x1        /* 8 bit reg */
  50. #define Reg16   0x2        /* 16 bit reg */
  51. #define Reg32   0x4        /* 32 bit reg */
  52. #define Reg     (Reg8|Reg16|Reg32)    /* gen'l register */
  53. #define WordReg (Reg16|Reg32)    /* for push/pop operands */
  54. /* immediate */
  55. #define Imm8    0x8        /* 8 bit immediate */
  56. #define Imm8S    0x10        /* 8 bit immediate sign extended */
  57. #define Imm16   0x20        /* 16 bit immediate */
  58. #define Imm32   0x40        /* 32 bit immediate */
  59. #define Imm1    0x80        /* 1 bit immediate */
  60. #define ImmUnknown Imm32    /* for unknown expressions */
  61. #define Imm     (Imm8|Imm8S|Imm16|Imm32)    /* gen'l immediate */
  62. /* memory */
  63. #define Disp8   0x200        /* 8 bit displacement (for jumps) */
  64. #define Disp16  0x400        /* 16 bit displacement */
  65. #define Disp32  0x800        /* 32 bit displacement */
  66. #define Disp    (Disp8|Disp16|Disp32) /* General displacement */
  67. #define DispUnknown Disp32    /* for unknown size displacements */
  68. #define Mem8    0x1000
  69. #define Mem16   0x2000
  70. #define Mem32   0x4000
  71. #define BaseIndex 0x8000
  72. #define Mem     (Disp|Mem8|Mem16|Mem32|BaseIndex) /* General memory */
  73. #define WordMem   (Mem16|Mem32|Disp|BaseIndex)
  74. #define ByteMem   (Mem8|Disp|BaseIndex)
  75. /* specials */
  76. #define InOutPortReg 0x10000    /* register to hold in/out port addr = dx */
  77. #define ShiftCount 0x20000    /* register to hold shift cound = cl */
  78. #define Control 0x40000        /* Control register */
  79. #define Debug   0x80000        /* Debug register */
  80. #define Test    0x100000        /* Test register */
  81. #define FloatReg 0x200000    /* Float register */
  82. #define FloatAcc 0x400000    /* Float stack top %st(0) */
  83. #define SReg2   0x800000        /* 2 bit segment register */
  84. #define SReg3   0x1000000        /* 3 bit segment register */
  85. #define Acc     0x2000000        /* Accumulator %al or %ax or %eax */
  86. #define ImplicitRegister (InOutPortReg|ShiftCount|Acc|FloatAcc)
  87. #define JumpAbsolute 0x4000000
  88. #define Abs8  0x08000000
  89. #define Abs16 0x10000000
  90. #define Abs32 0x20000000
  91. #define Abs (Abs8|Abs16|Abs32)
  92. #define MmxReg 0x40000000
  93.  
  94. #define MODE_FROM_DISP_SIZE(t) \
  95.       ((t&(Disp8)) ? 1 : \
  96.        ((t&(Disp32)) ? 2 : 0))
  97.  
  98. #define Byte (Reg8|Imm8|Imm8S)
  99. #define Word (Reg16|Imm16)
  100. #define DWord (Reg32|Imm32)
  101.  
  102. /* convert opcode suffix ('b' 'w' 'l' typically) into type specifyer */
  103. #define OPCODE_SUFFIX_TO_TYPE(s)                 \
  104.   (s == BYTE_OPCODE_SUFFIX ? Byte : \
  105.    (s == WORD_OPCODE_SUFFIX ? Word : DWord))
  106.  
  107. #define FITS_IN_SIGNED_BYTE(num) ((num) >= -128 && (num) <= 127)
  108. #define FITS_IN_UNSIGNED_BYTE(num) ((num) >= 0 && (num) <= 255)
  109. #define FITS_IN_UNSIGNED_WORD(num) ((num) >= 0 && (num) <= 65535)
  110. #define FITS_IN_SIGNED_WORD(num) ((num) >= -32768 && (num) <= 32767)
  111.  
  112. #define SMALLEST_DISP_TYPE(num) \
  113.   FITS_IN_SIGNED_BYTE(num) ? (Disp8|Disp32|Abs8|Abs32) : (Disp32|Abs32)
  114.  
  115. #define SMALLEST_IMM_TYPE(num) \
  116.   (num == 1) ? (Imm1|Imm8|Imm8S|Imm16|Imm32): \
  117.   FITS_IN_SIGNED_BYTE(num) ? (Imm8S|Imm8|Imm16|Imm32) : \
  118.   FITS_IN_UNSIGNED_BYTE(num) ? (Imm8|Imm16|Imm32): \
  119.   (FITS_IN_SIGNED_WORD(num)||FITS_IN_UNSIGNED_WORD(num)) ? (Imm16|Imm32) : \
  120.   (Imm32)
  121.  
  122. typedef unsigned char uchar;
  123.  
  124. typedef struct {
  125.   /* instruction name sans width suffix ("mov" for movl insns) */
  126.   char          *name;
  127.  
  128.   /* how many operands */
  129.   unsigned int    operands;
  130.  
  131.   /* base_opcode is the fundamental opcode byte with a optional prefix(es). */
  132.   unsigned int    base_opcode;
  133.  
  134.   /* extension_opcode is the 3 bit extension for group <n> insns.
  135.      If this template has no extension opcode (the usual case) use None */
  136.   uchar    extension_opcode;
  137. #define None 0xff        /* If no extension_opcode is possible. */
  138.  
  139.   /* the bits in opcode_modifier are used to generate the final opcode from
  140.      the base_opcode.  These bits also are used to detect alternate forms of
  141.      the same instruction */
  142.   unsigned int    opcode_modifier;
  143.  
  144. /* opcode_modifier bits: */
  145. #define W        0x1    /* set if operands are words or dwords */
  146. #define D        0x2    /* D = 0 if Reg --> Regmem; D = 1 if Regmem --> Reg */
  147. /* direction flag for floating insns:  MUST BE 0x400 */
  148. #define FloatD 0x400
  149. /* shorthand */
  150. #define DW (D|W)
  151. #define ShortForm 0x10        /* register is in low 3 bits of opcode */
  152. #define ShortFormW 0x20        /* ShortForm and W bit is 0x8 */
  153. #define Seg2ShortForm 0x40    /* encoding of load segment reg insns */
  154. #define Seg3ShortForm 0x80    /* fs/gs segment register insns. */
  155. #define Jump 0x100        /* special case for jump insns. */
  156. #define JumpInterSegment 0x200    /* special case for intersegment leaps/calls */
  157. /* 0x400 CANNOT BE USED since it's already used by FloatD above */
  158. #define DONT_USE 0x400
  159. #define NoModrm 0x800
  160. #define Modrm 0x1000
  161. #define imulKludge 0x2000
  162. #define JumpByte 0x4000
  163. #define JumpDword 0x8000
  164. #define ReverseRegRegmem 0x10000
  165.  
  166.   /* (opcode_modifier & COMES_IN_ALL_SIZES) is true if the
  167.      instuction comes in byte, word, and dword sizes and is encoded into
  168.      machine code in the canonical way. */
  169. #define COMES_IN_ALL_SIZES (W)
  170.  
  171.   /* (opcode_modifier & COMES_IN_BOTH_DIRECTIONS) indicates that the
  172.      source and destination operands can be reversed by setting either
  173.      the D (for integer insns) or the FloatD (for floating insns) bit
  174.      in base_opcode. */
  175. #define COMES_IN_BOTH_DIRECTIONS (D|FloatD)
  176.  
  177.   /* operand_types[i] describes the type of operand i.  This is made
  178.      by OR'ing together all of the possible type masks.  (e.g.
  179.      'operand_types[i] = Reg|Imm' specifies that operand i can be
  180.      either a register or an immediate operand */
  181.   unsigned int operand_types[3];
  182. } template;
  183.  
  184. /*
  185.   'templates' is for grouping together 'template' structures for opcodes
  186.   of the same name.  This is only used for storing the insns in the grand
  187.   ole hash table of insns.
  188.   The templates themselves start at START and range up to (but not including)
  189.   END.
  190. */
  191. typedef struct {
  192.   template      *start;
  193.   template      *end;
  194. } templates;
  195.  
  196. /* these are for register name --> number & type hash lookup */
  197. typedef struct {
  198.   char * reg_name;
  199.   unsigned int   reg_type;
  200.   unsigned int   reg_num;
  201. } reg_entry;
  202.  
  203. typedef struct {
  204.   char * seg_name;
  205.   unsigned int   seg_prefix;
  206. } seg_entry;
  207.  
  208. /* these are for prefix name --> prefix code hash lookup */
  209. typedef struct {
  210.   char * prefix_name;
  211.   uchar  prefix_code;
  212. } prefix_entry;
  213.  
  214. /* 386 operand encoding bytes:  see 386 book for details of this. */
  215. typedef struct {
  216.   unsigned regmem:3;         /* codes register or memory operand */
  217.   unsigned reg:3;         /* codes register operand (or extended opcode) */
  218.   unsigned mode:2;         /* how to interpret regmem & reg */
  219. } modrm_byte;
  220.  
  221. /* 386 opcode byte to code indirect addressing. */
  222. typedef struct {
  223.   unsigned base:3;
  224.   unsigned index:3;
  225.   unsigned scale:2;
  226. } base_index_byte;
  227.  
  228. /* Asm386Instruction gathers together information and puts it into a
  229.    i386_insn. */
  230.  
  231. typedef struct {
  232.   /* TM holds the template for the insn were currently assembling. */
  233.   template          tm;
  234.   /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
  235.   char              suffix;
  236.   /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
  237.  
  238.   /* OPERANDS gives the number of given operands. */
  239.   unsigned int               operands;
  240.  
  241.   /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number of
  242.      given register, displacement, memory operands and immediate operands. */
  243.   unsigned int               reg_operands, disp_operands, mem_operands, imm_operands;
  244.  
  245.   /* TYPES [i] is the type (see above #defines) which tells us how to
  246.      search through DISPS [i] & IMMS [i] & REGS [i] for the required
  247.      operand. */
  248.   unsigned int               types [MAX_OPERANDS];
  249.  
  250.   /* Displacements (if given) for each operand. */
  251.   expressionS       * disps [MAX_OPERANDS];
  252.  
  253.   /* Immediate operands (if given) for each operand. */
  254.   expressionS       * imms [MAX_OPERANDS];
  255.  
  256.   /* Register operands (if given) for each operand. */
  257.   reg_entry         * regs [MAX_OPERANDS];
  258.  
  259.   /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
  260.      the base index byte below.  */
  261.   reg_entry         * base_reg;
  262.   reg_entry         * index_reg;
  263.   unsigned int                log2_scale_factor;
  264.  
  265.   /* SEG gives the seg_entry of this insn.  It is equal to zero unless
  266.      an explicit segment override is given. */
  267.   seg_entry         * seg;    /* segment for memory operands (if given) */
  268.  
  269.   /* PREFIX holds all the given prefix opcodes (usually null).
  270.      PREFIXES is the size of PREFIX. */
  271.   char              prefix [MAX_PREFIXES];
  272.   unsigned int      prefixes;
  273.  
  274.   /* RM and IB are the modrm byte and the base index byte where the addressing
  275.      modes of this insn are encoded. */
  276.  
  277.   modrm_byte        rm;
  278.   base_index_byte   bi;
  279. } i386_insn;
  280.  
  281.