cpubase.pas 29 KB

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