i386.h 11 KB

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