cpubase.pas 29 KB

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