cpubase.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl and Peter Vreman
  4. Contains the base types for the i386
  5. * This code was inspired by the NASM sources
  6. The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. unit cpubase;
  22. interface
  23. {$ifdef TP}
  24. {$L-,Y-}
  25. {$endif}
  26. uses
  27. globals,strings,cobjects,aasm;
  28. const
  29. { Size of the instruction table converted by nasmconv.pas }
  30. instabentries = {$i i386nop.inc}
  31. maxinfolen = 8;
  32. { By default we want everything }
  33. {$define ATTOP}
  34. {$define ATTREG}
  35. {$define INTELOP}
  36. {$define ITTABLE}
  37. { For TP we can't use asmdebug due the table sizes }
  38. {$ifndef TP}
  39. {$define ASMDEBUG}
  40. {$endif}
  41. { We Don't need the intel style opcodes if we don't have a intel
  42. reader or generator }
  43. {$ifndef ASMDEBUG}
  44. {$ifdef NORA386INT}
  45. {$ifdef NOAG386NSM}
  46. {$ifdef NOAG386INT}
  47. {$undef INTELOP}
  48. {$endif}
  49. {$endif}
  50. {$endif}
  51. {$endif}
  52. { We Don't need the AT&T style opcodes if we don't have a AT&T
  53. reader or generator }
  54. {$ifdef NORA386ATT}
  55. {$ifdef NOAG386ATT}
  56. {$undef ATTOP}
  57. {$ifdef NOAG386DIR}
  58. {$undef ATTREG}
  59. {$endif}
  60. {$endif}
  61. {$endif}
  62. { We need the AT&T suffix table for both asm readers and AT&T writer }
  63. {$define ATTSUF}
  64. {$ifdef NORA386INT}
  65. {$ifdef NORA386ATT}
  66. {$ifdef NOAG386ATT}
  67. {$undef ATTSUF}
  68. {$endif}
  69. {$endif}
  70. {$endif}
  71. const
  72. { Operand types }
  73. OT_NONE = $00000000;
  74. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  75. OT_BITS16 = $00000002;
  76. OT_BITS32 = $00000004;
  77. OT_BITS64 = $00000008; { FPU only }
  78. OT_BITS80 = $00000010;
  79. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  80. OT_NEAR = $00000040;
  81. OT_SHORT = $00000080;
  82. OT_SIZE_MASK = $000000FF; { all the size attributes }
  83. OT_NON_SIZE = not OT_SIZE_MASK;
  84. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  85. OT_TO = $00000200; { operand is followed by a colon }
  86. { reverse effect in FADD, FSUB &c }
  87. OT_COLON = $00000400;
  88. OT_REGISTER = $00001000;
  89. OT_IMMEDIATE = $00002000;
  90. OT_IMM8 = $00002001;
  91. OT_IMM16 = $00002002;
  92. OT_IMM32 = $00002004;
  93. OT_IMM64 = $00002008;
  94. OT_IMM80 = $00002010;
  95. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  96. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  97. OT_REG8 = $00201001;
  98. OT_REG16 = $00201002;
  99. OT_REG32 = $00201004;
  100. OT_MMXREG = $00201008; { MMX registers }
  101. OT_XMMREG = $00201010; { Katmai registers }
  102. OT_MEMORY = $00204000; { register number in 'basereg' }
  103. OT_MEM8 = $00204001;
  104. OT_MEM16 = $00204002;
  105. OT_MEM32 = $00204004;
  106. OT_MEM64 = $00204008;
  107. OT_MEM80 = $00204010;
  108. OT_FPUREG = $01000000; { floating point stack registers }
  109. OT_FPU0 = $01000800; { FPU stack register zero }
  110. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  111. { a mask for the following }
  112. OT_REG_ACCUM = $00211000; { accumulator: AL, AX or EAX }
  113. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  114. OT_REG_AX = $00211002; { ditto }
  115. OT_REG_EAX = $00211004; { and again }
  116. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  117. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  118. OT_REG_CX = $00221002; { ditto }
  119. OT_REG_ECX = $00221004; { another one }
  120. OT_REG_DX = $00241002;
  121. OT_REG_SREG = $00081002; { any segment register }
  122. OT_REG_CS = $01081002; { CS }
  123. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  124. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  125. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  126. OT_REG_CREG = $08101004; { CRn }
  127. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  128. OT_REG_DREG = $10101004; { DRn }
  129. OT_REG_TREG = $20101004; { TRn }
  130. OT_MEM_OFFS = $00604000; { special type of EA }
  131. { simple [address] offset }
  132. OT_ONENESS = $00800000; { special type of immediate operand }
  133. { so UNITY == IMMEDIATE | ONENESS }
  134. OT_UNITY = $00802000; { for shift/rotate instructions }
  135. {Instruction flags }
  136. IF_NONE = $00000000;
  137. IF_SM = $00000001; { size match first two operands }
  138. IF_SM2 = $00000002;
  139. IF_SB = $00000004; { unsized operands can't be non-byte }
  140. IF_SW = $00000008; { unsized operands can't be non-word }
  141. IF_SD = $00000010; { unsized operands can't be nondword }
  142. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  143. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  144. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  145. IF_ARMASK = $00000060; { mask for unsized argument spec }
  146. IF_PRIV = $00000100; { it's a privileged instruction }
  147. IF_SMM = $00000200; { it's only valid in SMM }
  148. IF_PROT = $00000400; { it's protected mode only }
  149. IF_UNDOC = $00001000; { it's an undocumented instruction }
  150. IF_FPU = $00002000; { it's an FPU instruction }
  151. IF_MMX = $00004000; { it's an MMX instruction }
  152. IF_3DNOW = $00008000; { it's a 3DNow! instruction }
  153. IF_SSE = $00010000; { it's a SSE (KNI, MMX2) instruction }
  154. IF_PMASK = $FF000000; { the mask for processor types }
  155. IF_PFMASK = $F001FF00; { the mask for disassembly "prefer" }
  156. IF_8086 = $00000000; { 8086 instruction }
  157. IF_186 = $01000000; { 186+ instruction }
  158. IF_286 = $02000000; { 286+ instruction }
  159. IF_386 = $03000000; { 386+ instruction }
  160. IF_486 = $04000000; { 486+ instruction }
  161. IF_PENT = $05000000; { Pentium instruction }
  162. IF_P6 = $06000000; { P6 instruction }
  163. IF_KATMAI = $07000000; { Katmai instructions }
  164. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  165. IF_AMD = $20000000; { AMD-specific instruction }
  166. { added flags }
  167. IF_PRE = $40000000; { it's a prefix instruction }
  168. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  169. type
  170. TAttSuffix = (AttSufNONE,AttSufINT,AttSufFPU,AttSufFPUint);
  171. TAsmOp=
  172. {$i i386op.inc}
  173. op2strtable=array[tasmop] of string[11];
  174. pstr2opentry = ^tstr2opentry;
  175. tstr2opentry = object(Tnamedindexobject)
  176. op: TAsmOp;
  177. end;
  178. const
  179. firstop = low(tasmop);
  180. lastop = high(tasmop);
  181. AsmPrefixes = 6;
  182. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  183. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ
  184. );
  185. AsmOverrides = 6;
  186. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  187. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  188. );
  189. {$ifdef INTELOP}
  190. int_op2str:op2strtable=
  191. {$i i386int.inc}
  192. {$endif INTELOP}
  193. {$ifdef ATTOP}
  194. att_op2str:op2strtable=
  195. {$i i386att.inc}
  196. {$endif ATTOP}
  197. {$ifdef ATTSUF}
  198. att_needsuffix:array[tasmop] of TAttSuffix=
  199. {$i i386atts.inc}
  200. {$endif ATTSUF}
  201. {*****************************************************************************
  202. Operand Sizes
  203. *****************************************************************************}
  204. type
  205. topsize = (S_NO,
  206. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  207. S_IS,S_IL,S_IQ,
  208. S_FS,S_FL,S_FX,S_D,S_Q,S_FV
  209. );
  210. const
  211. { Intel style operands ! }
  212. opsize_2_type:array[0..2,topsize] of longint=(
  213. (OT_NONE,
  214. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,
  215. OT_BITS16,OT_BITS32,OT_BITS64,
  216. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64
  217. ),
  218. (OT_NONE,
  219. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,
  220. OT_BITS16,OT_BITS32,OT_BITS64,
  221. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64
  222. ),
  223. (OT_NONE,
  224. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,
  225. OT_BITS16,OT_BITS32,OT_BITS64,
  226. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64
  227. )
  228. );
  229. {$ifdef ATTOP}
  230. att_opsize2str : array[topsize] of string[2] = ('',
  231. 'b','w','l','bw','bl','wl',
  232. 's','l','q',
  233. 's','l','t','d','q','v'
  234. );
  235. {$endif}
  236. {*****************************************************************************
  237. Conditions
  238. *****************************************************************************}
  239. type
  240. TAsmCond=(C_None,
  241. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  242. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  243. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z
  244. );
  245. const
  246. cond2str:array[TAsmCond] of string[3]=('',
  247. 'a','ae','b','be','c','e','g','ge','l','le','na','nae',
  248. 'nb','nbe','nc','ne','ng','nge','nl','nle','no','np',
  249. 'ns','nz','o','p','pe','po','s','z'
  250. );
  251. inverse_cond:array[TAsmCond] of TAsmCond=(C_None,
  252. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  253. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  254. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ
  255. );
  256. const
  257. CondAsmOps=3;
  258. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  259. A_CMOVcc, A_Jcc, A_SETcc
  260. );
  261. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  262. 'CMOV','J','SET'
  263. );
  264. {*****************************************************************************
  265. Registers
  266. *****************************************************************************}
  267. type
  268. { enumeration for registers, don't change the order }
  269. { it's used by the register size conversions }
  270. tregister = (R_NO,
  271. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  272. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  273. R_AL,R_CL,R_DL,R_BL,R_AH,R_CH,R_BH,R_DH,
  274. R_CS,R_DS,R_ES,R_SS,R_FS,R_GS,
  275. R_ST,R_ST0,R_ST1,R_ST2,R_ST3,R_ST4,R_ST5,R_ST6,R_ST7,
  276. R_DR0,R_DR1,R_DR2,R_DR3,R_DR6,R_DR7,
  277. R_CR0,R_CR2,R_CR3,R_CR4,
  278. R_TR3,R_TR4,R_TR5,R_TR6,R_TR7,
  279. R_MM0,R_MM1,R_MM2,R_MM3,R_MM4,R_MM5,R_MM6,R_MM7,
  280. R_XMM0,R_XMM1,R_XMM2,R_XMM3,R_XMM4,R_XMM5,R_XMM6,R_XMM7
  281. );
  282. tregisterset = set of tregister;
  283. reg2strtable = array[tregister] of string[6];
  284. const
  285. firstreg = low(tregister);
  286. lastreg = high(tregister);
  287. firstsreg = R_CS;
  288. lastsreg = R_GS;
  289. regset8bit : tregisterset = [R_AL..R_DH];
  290. regset16bit : tregisterset = [R_AX..R_DI,R_CS..R_SS];
  291. regset32bit : tregisterset = [R_EAX..R_EDI];
  292. { Convert reg to opsize }
  293. reg_2_opsize:array[firstreg..lastreg] of topsize = (S_NO,
  294. S_L,S_L,S_L,S_L,S_L,S_L,S_L,S_L,
  295. S_W,S_W,S_W,S_W,S_W,S_W,S_W,S_W,
  296. S_B,S_B,S_B,S_B,S_B,S_B,S_B,S_B,
  297. S_W,S_W,S_W,S_W,S_W,S_W,
  298. S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,
  299. S_L,S_L,S_L,S_L,S_L,S_L,
  300. S_L,S_L,S_L,S_L,
  301. S_L,S_L,S_L,S_L,S_L,
  302. S_D,S_D,S_D,S_D,S_D,S_D,S_D,S_D,
  303. S_D,S_D,S_D,S_D,S_D,S_D,S_D,S_D
  304. );
  305. { Convert reg to operand type }
  306. reg_2_type:array[firstreg..lastreg] of longint = (OT_NONE,
  307. OT_REG_EAX,OT_REG_ECX,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  308. OT_REG_AX,OT_REG_CX,OT_REG_DX,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  309. OT_REG_AL,OT_REG_CL,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  310. OT_REG_CS,OT_REG_DESS,OT_REG_DESS,OT_REG_DESS,OT_REG_FSGS,OT_REG_FSGS,
  311. OT_FPU0,OT_FPU0,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,
  312. OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,
  313. OT_REG_CREG,OT_REG_CREG,OT_REG_CREG,OT_REG_CR4,
  314. OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,
  315. OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,
  316. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG
  317. );
  318. {$ifdef INTELOP}
  319. int_reg2str : reg2strtable = ('',
  320. 'eax','ecx','edx','ebx','esp','ebp','esi','edi',
  321. 'ax','cx','dx','bx','sp','bp','si','di',
  322. 'al','cl','dl','bl','ah','ch','bh','dh',
  323. 'cs','ds','es','ss','fs','gs',
  324. 'st','st(0)','st(1)','st(2)','st(3)','st(4)','st(5)','st(6)','st(7)',
  325. 'dr0','dr1','dr2','dr3','dr6','dr7',
  326. 'cr0','cr2','cr3','cr4',
  327. 'tr3','tr4','tr5','tr6','tr7',
  328. 'mm0','mm1','mm2','mm3','mm4','mm5','mm6','mm7',
  329. 'xmm0','xmm1','xmm2','xmm3','xmm4','xmm5','xmm6','xmm7'
  330. );
  331. int_nasmreg2str : reg2strtable = ('',
  332. 'eax','ecx','edx','ebx','esp','ebp','esi','edi',
  333. 'ax','cx','dx','bx','sp','bp','si','di',
  334. 'al','cl','dl','bl','ah','ch','bh','dh',
  335. 'cs','ds','es','ss','fs','gs',
  336. 'st0','st0','st1','st2','st3','st4','st5','st6','st7',
  337. 'dr0','dr1','dr2','dr3','dr6','dr7',
  338. 'cr0','cr2','cr3','cr4',
  339. 'tr3','tr4','tr5','tr6','tr7',
  340. 'mm0','mm1','mm2','mm3','mm4','mm5','mm6','mm7',
  341. 'xmm0','xmm1','xmm2','xmm3','xmm4','xmm5','xmm6','xmm7'
  342. );
  343. {$endif}
  344. {$ifdef ATTREG}
  345. att_reg2str : reg2strtable = ('',
  346. '%eax','%ecx','%edx','%ebx','%esp','%ebp','%esi','%edi',
  347. '%ax','%cx','%dx','%bx','%sp','%bp','%si','%di',
  348. '%al','%cl','%dl','%bl','%ah','%ch','%bh','%dh',
  349. '%cs','%ds','%es','%ss','%fs','%gs',
  350. '%st','%st(0)','%st(1)','%st(2)','%st(3)','%st(4)','%st(5)','%st(6)','%st(7)',
  351. '%dr0','%dr1','%dr2','%dr3','%dr6','%dr7',
  352. '%cr0','%cr2','%cr3','%cr4',
  353. '%tr3','%tr4','%tr5','%tr6','%tr7',
  354. '%mm0','%mm1','%mm2','%mm3','%mm4','%mm5','%mm6','%mm7',
  355. '%xmm0','%xmm1','%xmm2','%xmm3','%xmm4','%xmm5','%xmm6','%xmm7'
  356. );
  357. {$endif ATTREG}
  358. {*****************************************************************************
  359. Flags
  360. *****************************************************************************}
  361. type
  362. TResFlags = (F_E,F_NE,F_G,F_L,F_GE,F_LE,F_C,F_NC,F_A,F_AE,F_B,F_BE);
  363. const
  364. { arrays for boolean location conversions }
  365. flag_2_cond : array[TResFlags] of TAsmCond =
  366. (C_E,C_NE,C_G,C_L,C_GE,C_LE,C_C,C_NC,C_A,C_AE,C_B,C_BE);
  367. {*****************************************************************************
  368. Reference
  369. *****************************************************************************}
  370. type
  371. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  372. { immediate/reference record }
  373. preference = ^treference;
  374. treference = packed record
  375. is_immediate : boolean; { is this used as reference or immediate }
  376. segment,
  377. base,
  378. index : tregister;
  379. scalefactor : byte;
  380. offset : longint;
  381. symbol : pasmsymbol;
  382. offsetfixup : longint;
  383. options : trefoptions;
  384. {$ifdef newcg}
  385. alignment : byte;
  386. {$endif newcg}
  387. end;
  388. {*****************************************************************************
  389. Operands
  390. *****************************************************************************}
  391. { Types of operand }
  392. toptype=(top_none,top_reg,top_ref,top_const,top_symbol);
  393. toper=record
  394. ot : longint;
  395. case typ : toptype of
  396. top_none : ();
  397. top_reg : (reg:tregister);
  398. top_ref : (ref:preference);
  399. top_const : (val:longint);
  400. top_symbol : (sym:pasmsymbol;symofs:longint);
  401. end;
  402. {*****************************************************************************
  403. Generic Location
  404. *****************************************************************************}
  405. type
  406. TLoc=(
  407. LOC_INVALID, { added for tracking problems}
  408. LOC_FPU, { FPU stack }
  409. LOC_REGISTER, { in a processor register }
  410. LOC_MEM, { in memory }
  411. LOC_REFERENCE, { like LOC_MEM, but lvalue }
  412. LOC_JUMP, { boolean results only, jump to false or true label }
  413. LOC_FLAGS, { boolean results only, flags are set }
  414. LOC_CREGISTER, { Constant register which shouldn't be modified }
  415. LOC_MMXREGISTER, { MMX register }
  416. LOC_CMMXREGISTER,{ Constant MMX register }
  417. LOC_CFPUREGISTER { if it is a FPU register variable on the fpu stack }
  418. );
  419. plocation = ^tlocation;
  420. tlocation = packed record
  421. case loc : tloc of
  422. LOC_MEM,LOC_REFERENCE : (reference : treference);
  423. LOC_FPU : ();
  424. LOC_JUMP : ();
  425. LOC_FLAGS : (resflags : tresflags);
  426. LOC_INVALID : ();
  427. { it's only for better handling }
  428. LOC_MMXREGISTER : (mmxreg : tregister);
  429. { segment in reference at the same place as in loc_register }
  430. LOC_REGISTER,LOC_CREGISTER : (
  431. case longint of
  432. 1 : (register,segment,registerhigh : tregister);
  433. { overlay a registerlow }
  434. 2 : (registerlow : tregister);
  435. );
  436. end;
  437. {*****************************************************************************
  438. Constants
  439. *****************************************************************************}
  440. const
  441. general_registers = [R_EAX,R_EBX,R_ECX,R_EDX];
  442. intregs = general_registers;
  443. fpuregs = [];
  444. mmregs = [R_MM0..R_MM7];
  445. lvaluelocations = [LOC_REFERENCE,LOC_CFPUREGISTER,
  446. LOC_CREGISTER,LOC_MMXREGISTER,LOC_CMMXREGISTER];
  447. registers_saved_on_cdecl = [R_ESI,R_EDI,R_EBX];
  448. { generic register names }
  449. stack_pointer = R_ESP;
  450. frame_pointer = R_EBP;
  451. self_pointer = R_ESI;
  452. accumulator = R_EAX;
  453. { the register where the vmt offset is passed to the destructor }
  454. { helper routine }
  455. vmt_offset_reg = R_EDI;
  456. scratch_regs : array[1..1] of tregister = (R_EDI);
  457. max_scratch_regs = 1;
  458. { low and high of the available maximum width integer general purpose }
  459. { registers }
  460. LoGPReg = R_EAX;
  461. HiGPReg = R_EDI;
  462. { low and high of every possible width general purpose register (same as }
  463. { above on most architctures apart from the 80x86) }
  464. LoReg = R_EAX;
  465. HiReg = R_BL;
  466. cpuflags = [];
  467. { sizes }
  468. pointersize = 4;
  469. extended_size = 10;
  470. sizepostfix_pointer = S_L;
  471. {*****************************************************************************
  472. Instruction table
  473. *****************************************************************************}
  474. {$ifndef NOAG386BIN}
  475. type
  476. tinsentry=packed record
  477. opcode : tasmop;
  478. ops : byte;
  479. optypes : array[0..2] of longint;
  480. code : array[0..maxinfolen] of char;
  481. flags : longint;
  482. end;
  483. pinsentry=^tinsentry;
  484. TInsTabCache=array[TasmOp] of longint;
  485. PInsTabCache=^TInsTabCache;
  486. const
  487. InsTab:array[0..instabentries-1] of TInsEntry=
  488. {$i i386tab.inc}
  489. var
  490. InsTabCache : PInsTabCache;
  491. {$endif NOAG386BIN}
  492. {*****************************************************************************
  493. Opcode propeties (needed for optimizer)
  494. *****************************************************************************}
  495. {$ifndef NOOPT}
  496. Type
  497. {What an instruction can change}
  498. TInsChange = (Ch_None,
  499. {Read from a register}
  500. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  501. {write from a register}
  502. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  503. {read and write from/to a register}
  504. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  505. {modify the contents of a register with the purpose of using
  506. this changed content afterwards (add/sub/..., but e.g. not rep
  507. or movsd)}
  508. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  509. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  510. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  511. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  512. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  513. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  514. Ch_WMemEDI,
  515. Ch_All
  516. );
  517. const
  518. MaxCh = 3; { Max things a instruction can change }
  519. type
  520. TInsProp = packed record
  521. Ch : Array[1..MaxCh] of TInsChange;
  522. end;
  523. const
  524. InsProp : array[tasmop] of TInsProp =
  525. {$i i386prop.inc}
  526. {$endif NOOPT}
  527. {*****************************************************************************
  528. Init/Done
  529. *****************************************************************************}
  530. procedure InitCpu;
  531. procedure DoneCpu;
  532. {*****************************************************************************
  533. Helpers
  534. *****************************************************************************}
  535. const
  536. maxvarregs = 4;
  537. varregs : array[1..maxvarregs] of tregister =
  538. (R_EBX,R_EDX,R_ECX,R_EAX);
  539. maxfpuvarregs = 8;
  540. max_operands = 3;
  541. function imm_2_type(l:longint):longint;
  542. { the following functions allow to convert registers }
  543. { for example reg8toreg32(R_AL) returns R_EAX }
  544. { for example reg16toreg32(R_AL) gives an undefined }
  545. { result }
  546. { these functions expects that the turn of }
  547. { tregister isn't changed }
  548. function reg8toreg16(reg : tregister) : tregister;
  549. function reg8toreg32(reg : tregister) : tregister;
  550. function reg16toreg8(reg : tregister) : tregister;
  551. function reg32toreg8(reg : tregister) : tregister;
  552. function reg32toreg16(reg : tregister) : tregister;
  553. function reg16toreg32(reg : tregister) : tregister;
  554. { these procedures must be defined by all target cpus }
  555. function regtoreg8(reg : tregister) : tregister;
  556. function regtoreg16(reg : tregister) : tregister;
  557. function regtoreg32(reg : tregister) : tregister;
  558. { can be ignored on 32 bit systems }
  559. function regtoreg64(reg : tregister) : tregister;
  560. { returns the operand prefix for a given register }
  561. function regsize(reg : tregister) : topsize;
  562. { resets all values of ref to defaults }
  563. procedure reset_reference(var ref : treference);
  564. { set mostly used values of a new reference }
  565. function new_reference(base : tregister;offset : longint) : preference;
  566. function newreference(const r : treference) : preference;
  567. procedure disposereference(var r : preference);
  568. function reg2str(r : tregister) : string;
  569. function is_calljmp(o:tasmop):boolean;
  570. implementation
  571. {$ifdef heaptrc}
  572. uses
  573. ppheap;
  574. {$endif heaptrc}
  575. {*****************************************************************************
  576. Helpers
  577. *****************************************************************************}
  578. function imm_2_type(l:longint):longint;
  579. begin
  580. if (l>=-128) and (l<=127) then
  581. imm_2_type:=OT_IMM8 or OT_SIGNED
  582. else
  583. if (l>=-255) and (l<=255) then
  584. imm_2_type:=OT_IMM8
  585. else
  586. if (l>=-32768) and (l<=32767) then
  587. imm_2_type:=OT_IMM16 or OT_SIGNED
  588. else
  589. if (l>=-65536) and (l<=65535) then
  590. imm_2_type:=OT_IMM16 or OT_SIGNED
  591. else
  592. imm_2_type:=OT_IMM32;
  593. end;
  594. function reg2str(r : tregister) : string;
  595. const
  596. a : array[R_NO..R_BL] of string[3] =
  597. ('','EAX','ECX','EDX','EBX','ESP','EBP','ESI','EDI',
  598. 'AX','CX','DX','BX','SP','BP','SI','DI',
  599. 'AL','CL','DL','BL');
  600. begin
  601. if r in [R_ST0..R_ST7] then
  602. reg2str:='ST('+tostr(longint(r)-longint(R_ST0))+')'
  603. else
  604. reg2str:=a[r];
  605. end;
  606. function is_calljmp(o:tasmop):boolean;
  607. begin
  608. case o of
  609. A_CALL,
  610. A_JCXZ,
  611. A_JECXZ,
  612. A_JMP,
  613. A_LOOP,
  614. A_LOOPE,
  615. A_LOOPNE,
  616. A_LOOPNZ,
  617. A_LOOPZ,
  618. A_Jcc :
  619. is_calljmp:=true;
  620. else
  621. is_calljmp:=false;
  622. end;
  623. end;
  624. procedure disposereference(var r : preference);
  625. begin
  626. dispose(r);
  627. r:=nil;
  628. end;
  629. function newreference(const r : treference) : preference;
  630. var
  631. p : preference;
  632. begin
  633. new(p);
  634. p^:=r;
  635. newreference:=p;
  636. end;
  637. function reg8toreg16(reg : tregister) : tregister;
  638. begin
  639. reg8toreg16:=reg32toreg16(reg8toreg32(reg));
  640. end;
  641. function reg16toreg8(reg : tregister) : tregister;
  642. begin
  643. reg16toreg8:=reg32toreg8(reg16toreg32(reg));
  644. end;
  645. function reg16toreg32(reg : tregister) : tregister;
  646. begin
  647. reg16toreg32:=tregister(byte(reg)-byte(R_EDI));
  648. end;
  649. function reg32toreg16(reg : tregister) : tregister;
  650. begin
  651. reg32toreg16:=tregister(byte(reg)+byte(R_EDI));
  652. end;
  653. function reg32toreg8(reg : tregister) : tregister;
  654. begin
  655. reg32toreg8:=tregister(byte(reg)+byte(R_DI));
  656. end;
  657. function reg8toreg32(reg : tregister) : tregister;
  658. begin
  659. reg8toreg32:=tregister(byte(reg)-byte(R_DI));
  660. end;
  661. function regtoreg8(reg : tregister) : tregister;
  662. begin
  663. regtoreg8:=reg32toreg8(reg);
  664. end;
  665. function regtoreg16(reg : tregister) : tregister;
  666. begin
  667. regtoreg16:=reg32toreg16(reg);
  668. end;
  669. function regtoreg32(reg : tregister) : tregister;
  670. begin
  671. regtoreg32:=reg;
  672. end;
  673. function regtoreg64(reg : tregister) : tregister;
  674. begin
  675. { to avoid warning }
  676. regtoreg64:=R_NO;
  677. end;
  678. function regsize(reg : tregister) : topsize;
  679. begin
  680. if reg in regset8bit then
  681. regsize:=S_B
  682. else if reg in regset16bit then
  683. regsize:=S_W
  684. else if reg in regset32bit then
  685. regsize:=S_L;
  686. end;
  687. procedure reset_reference(var ref : treference);
  688. begin
  689. FillChar(ref,sizeof(treference),0);
  690. end;
  691. function new_reference(base : tregister;offset : longint) : preference;
  692. var
  693. r : preference;
  694. begin
  695. new(r);
  696. FillChar(r^,sizeof(treference),0);
  697. r^.base:=base;
  698. r^.offset:=offset;
  699. new_reference:=r;
  700. end;
  701. {*****************************************************************************
  702. Instruction table
  703. *****************************************************************************}
  704. procedure DoneCpu;
  705. begin
  706. {exitproc:=saveexit; }
  707. {$ifndef NOAG386BIN}
  708. if assigned(instabcache) then
  709. dispose(instabcache);
  710. {$endif NOAG386BIN}
  711. end;
  712. procedure BuildInsTabCache;
  713. {$ifndef NOAG386BIN}
  714. var
  715. i : longint;
  716. {$endif}
  717. begin
  718. {$ifndef NOAG386BIN}
  719. new(instabcache);
  720. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  721. i:=0;
  722. while (i<InsTabEntries) do
  723. begin
  724. if InsTabCache^[InsTab[i].OPcode]=-1 then
  725. InsTabCache^[InsTab[i].OPcode]:=i;
  726. inc(i);
  727. end;
  728. {$endif NOAG386BIN}
  729. end;
  730. procedure InitCpu;
  731. begin
  732. {$ifndef NOAG386BIN}
  733. if not assigned(instabcache) then
  734. BuildInsTabCache;
  735. {$endif NOAG386BIN}
  736. end;
  737. end.
  738. {
  739. $Log$
  740. Revision 1.3 2000-07-14 05:11:48 michael
  741. + Patch to 1.1
  742. Revision 1.2 2000/07/13 11:32:39 michael
  743. + removed logs
  744. }