cpubase.pas 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. Argument Classification
  403. *****************************************************************************}
  404. type
  405. TArgClass = (
  406. { the following classes should be defined by all processor implemnations }
  407. AC_NOCLASS,
  408. AC_MEMORY,
  409. AC_INTEGER,
  410. AC_FPU,
  411. { the following argument classes are i386 specific }
  412. AC_FPUUP,
  413. AC_SSE,
  414. AC_SSEUP);
  415. {*****************************************************************************
  416. Generic Location
  417. *****************************************************************************}
  418. type
  419. TLoc=(
  420. LOC_INVALID, { added for tracking problems}
  421. LOC_FPU, { FPU stack }
  422. LOC_REGISTER, { in a processor register }
  423. LOC_MEM, { in memory }
  424. LOC_REFERENCE, { like LOC_MEM, but lvalue }
  425. LOC_JUMP, { boolean results only, jump to false or true label }
  426. LOC_FLAGS, { boolean results only, flags are set }
  427. LOC_CREGISTER, { Constant register which shouldn't be modified }
  428. LOC_MMXREGISTER, { MMX register }
  429. LOC_CMMXREGISTER, { MMX register variable }
  430. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  431. LOC_SSEREGISTER,
  432. LOC_CSSEREGISTER
  433. );
  434. plocation = ^tlocation;
  435. tlocation = packed record
  436. case loc : tloc of
  437. LOC_MEM,LOC_REFERENCE : (reference : treference);
  438. LOC_FPU : ();
  439. LOC_JUMP : ();
  440. LOC_FLAGS : (resflags : tresflags);
  441. LOC_INVALID : ();
  442. { it's only for better handling }
  443. LOC_MMXREGISTER : (mmxreg : tregister);
  444. { segment in reference at the same place as in loc_register }
  445. LOC_REGISTER,LOC_CREGISTER : (
  446. case longint of
  447. 1 : (register,segment,registerhigh : tregister);
  448. { overlay a registerlow }
  449. 2 : (registerlow : tregister);
  450. );
  451. end;
  452. {*****************************************************************************
  453. Constants
  454. *****************************************************************************}
  455. const
  456. general_registers = [R_EAX,R_EBX,R_ECX,R_EDX];
  457. intregs = general_registers;
  458. fpuregs = [];
  459. mmregs = [R_MM0..R_MM7];
  460. lvaluelocations = [LOC_REFERENCE,LOC_CFPUREGISTER,
  461. LOC_CREGISTER,LOC_MMXREGISTER,LOC_CMMXREGISTER];
  462. registers_saved_on_cdecl = [R_ESI,R_EDI,R_EBX];
  463. { generic register names }
  464. stack_pointer = R_ESP;
  465. frame_pointer = R_EBP;
  466. self_pointer = R_ESI;
  467. accumulator = R_EAX;
  468. accumulatorhigh = R_EDX;
  469. { the register where the vmt offset is passed to the destructor }
  470. { helper routine }
  471. vmt_offset_reg = R_EDI;
  472. scratch_regs : array[1..1] of tregister = (R_EDI);
  473. { low and high of the available maximum width integer general purpose }
  474. { registers }
  475. LoGPReg = R_EAX;
  476. HiGPReg = R_EDI;
  477. { low and high of every possible width general purpose register (same as }
  478. { above on most architctures apart from the 80x86) }
  479. LoReg = R_EAX;
  480. HiReg = R_BL;
  481. cpuflags = [];
  482. { sizes }
  483. pointersize = 4;
  484. extended_size = 10;
  485. sizepostfix_pointer = S_L;
  486. {*****************************************************************************
  487. Instruction table
  488. *****************************************************************************}
  489. {$ifndef NOAG386BIN}
  490. type
  491. tinsentry=packed record
  492. opcode : tasmop;
  493. ops : byte;
  494. optypes : array[0..2] of longint;
  495. code : array[0..maxinfolen] of char;
  496. flags : longint;
  497. end;
  498. pinsentry=^tinsentry;
  499. TInsTabCache=array[TasmOp] of longint;
  500. PInsTabCache=^TInsTabCache;
  501. const
  502. InsTab:array[0..instabentries-1] of TInsEntry=
  503. {$i i386tab.inc}
  504. var
  505. InsTabCache : PInsTabCache;
  506. {$endif NOAG386BIN}
  507. {*****************************************************************************
  508. Opcode propeties (needed for optimizer)
  509. *****************************************************************************}
  510. {$ifndef NOOPT}
  511. Type
  512. {What an instruction can change}
  513. TInsChange = (Ch_None,
  514. {Read from a register}
  515. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  516. {write from a register}
  517. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  518. {read and write from/to a register}
  519. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  520. {modify the contents of a register with the purpose of using
  521. this changed content afterwards (add/sub/..., but e.g. not rep
  522. or movsd)}
  523. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  524. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  525. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  526. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  527. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  528. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  529. Ch_WMemEDI,
  530. Ch_All
  531. );
  532. const
  533. MaxCh = 3; { Max things a instruction can change }
  534. type
  535. TInsProp = packed record
  536. Ch : Array[1..MaxCh] of TInsChange;
  537. end;
  538. const
  539. InsProp : array[tasmop] of TInsProp =
  540. {$i i386prop.inc}
  541. {$endif NOOPT}
  542. {*****************************************************************************
  543. Init/Done
  544. *****************************************************************************}
  545. procedure InitCpu;
  546. procedure DoneCpu;
  547. {*****************************************************************************
  548. Helpers
  549. *****************************************************************************}
  550. const
  551. maxvarregs = 4;
  552. varregs : array[1..maxvarregs] of tregister =
  553. (R_EBX,R_EDX,R_ECX,R_EAX);
  554. maxfpuvarregs = 8;
  555. max_operands = 3;
  556. maxintregs = maxvarregs;
  557. maxfpuregs = maxfpuvarregs;
  558. function imm_2_type(l:longint):longint;
  559. { the following functions allow to convert registers }
  560. { for example reg8toreg32(R_AL) returns R_EAX }
  561. { for example reg16toreg32(R_AL) gives an undefined }
  562. { result }
  563. { these functions expects that the turn of }
  564. { tregister isn't changed }
  565. function reg8toreg16(reg : tregister) : tregister;
  566. function reg8toreg32(reg : tregister) : tregister;
  567. function reg16toreg8(reg : tregister) : tregister;
  568. function reg32toreg8(reg : tregister) : tregister;
  569. function reg32toreg16(reg : tregister) : tregister;
  570. function reg16toreg32(reg : tregister) : tregister;
  571. { these procedures must be defined by all target cpus }
  572. function regtoreg8(reg : tregister) : tregister;
  573. function regtoreg16(reg : tregister) : tregister;
  574. function regtoreg32(reg : tregister) : tregister;
  575. { can be ignored on 32 bit systems }
  576. function regtoreg64(reg : tregister) : tregister;
  577. { returns the operand prefix for a given register }
  578. function regsize(reg : tregister) : topsize;
  579. { resets all values of ref to defaults }
  580. procedure reset_reference(var ref : treference);
  581. { set mostly used values of a new reference }
  582. function new_reference(base : tregister;offset : longint) : preference;
  583. function newreference(const r : treference) : preference;
  584. procedure disposereference(var r : preference);
  585. function reg2str(r : tregister) : string;
  586. function is_calljmp(o:tasmop):boolean;
  587. procedure clear_location(var loc : tlocation);
  588. procedure set_location(var destloc,sourceloc : tlocation);
  589. procedure swap_location(var destloc,sourceloc : tlocation);
  590. implementation
  591. {$ifdef heaptrc}
  592. uses
  593. ppheap;
  594. {$endif heaptrc}
  595. {*****************************************************************************
  596. Helpers
  597. *****************************************************************************}
  598. function imm_2_type(l:longint):longint;
  599. begin
  600. if (l>=-128) and (l<=127) then
  601. imm_2_type:=OT_IMM8 or OT_SIGNED
  602. else
  603. if (l>=-255) and (l<=255) then
  604. imm_2_type:=OT_IMM8
  605. else
  606. if (l>=-32768) and (l<=32767) then
  607. imm_2_type:=OT_IMM16 or OT_SIGNED
  608. else
  609. if (l>=-65536) and (l<=65535) then
  610. imm_2_type:=OT_IMM16 or OT_SIGNED
  611. else
  612. imm_2_type:=OT_IMM32;
  613. end;
  614. function reg2str(r : tregister) : string;
  615. const
  616. a : array[R_NO..R_BL] of string[3] =
  617. ('','EAX','ECX','EDX','EBX','ESP','EBP','ESI','EDI',
  618. 'AX','CX','DX','BX','SP','BP','SI','DI',
  619. 'AL','CL','DL','BL');
  620. begin
  621. if r in [R_ST0..R_ST7] then
  622. reg2str:='ST('+tostr(longint(r)-longint(R_ST0))+')'
  623. else
  624. reg2str:=a[r];
  625. end;
  626. function is_calljmp(o:tasmop):boolean;
  627. begin
  628. case o of
  629. A_CALL,
  630. A_JCXZ,
  631. A_JECXZ,
  632. A_JMP,
  633. A_LOOP,
  634. A_LOOPE,
  635. A_LOOPNE,
  636. A_LOOPNZ,
  637. A_LOOPZ,
  638. A_Jcc :
  639. is_calljmp:=true;
  640. else
  641. is_calljmp:=false;
  642. end;
  643. end;
  644. procedure disposereference(var r : preference);
  645. begin
  646. dispose(r);
  647. r:=nil;
  648. end;
  649. function newreference(const r : treference) : preference;
  650. var
  651. p : preference;
  652. begin
  653. new(p);
  654. p^:=r;
  655. newreference:=p;
  656. end;
  657. function reg8toreg16(reg : tregister) : tregister;
  658. begin
  659. reg8toreg16:=reg32toreg16(reg8toreg32(reg));
  660. end;
  661. function reg16toreg8(reg : tregister) : tregister;
  662. begin
  663. reg16toreg8:=reg32toreg8(reg16toreg32(reg));
  664. end;
  665. function reg16toreg32(reg : tregister) : tregister;
  666. begin
  667. reg16toreg32:=tregister(byte(reg)-byte(R_EDI));
  668. end;
  669. function reg32toreg16(reg : tregister) : tregister;
  670. begin
  671. reg32toreg16:=tregister(byte(reg)+byte(R_EDI));
  672. end;
  673. function reg32toreg8(reg : tregister) : tregister;
  674. begin
  675. reg32toreg8:=tregister(byte(reg)+byte(R_DI));
  676. end;
  677. function reg8toreg32(reg : tregister) : tregister;
  678. begin
  679. reg8toreg32:=tregister(byte(reg)-byte(R_DI));
  680. end;
  681. function regtoreg8(reg : tregister) : tregister;
  682. begin
  683. regtoreg8:=reg32toreg8(reg);
  684. end;
  685. function regtoreg16(reg : tregister) : tregister;
  686. begin
  687. regtoreg16:=reg32toreg16(reg);
  688. end;
  689. function regtoreg32(reg : tregister) : tregister;
  690. begin
  691. regtoreg32:=reg;
  692. end;
  693. function regtoreg64(reg : tregister) : tregister;
  694. begin
  695. { to avoid warning }
  696. regtoreg64:=R_NO;
  697. end;
  698. function regsize(reg : tregister) : topsize;
  699. begin
  700. if reg in regset8bit then
  701. regsize:=S_B
  702. else if reg in regset16bit then
  703. regsize:=S_W
  704. else if reg in regset32bit then
  705. regsize:=S_L;
  706. end;
  707. procedure reset_reference(var ref : treference);
  708. begin
  709. FillChar(ref,sizeof(treference),0);
  710. end;
  711. function new_reference(base : tregister;offset : longint) : preference;
  712. var
  713. r : preference;
  714. begin
  715. new(r);
  716. FillChar(r^,sizeof(treference),0);
  717. r^.base:=base;
  718. r^.offset:=offset;
  719. new_reference:=r;
  720. end;
  721. procedure clear_location(var loc : tlocation);
  722. begin
  723. loc.loc:=LOC_INVALID;
  724. end;
  725. {This is needed if you want to be able to delete the string with the nodes !!}
  726. procedure set_location(var destloc,sourceloc : tlocation);
  727. begin
  728. destloc:= sourceloc;
  729. end;
  730. procedure swap_location(var destloc,sourceloc : tlocation);
  731. var
  732. swapl : tlocation;
  733. begin
  734. swapl := destloc;
  735. destloc := sourceloc;
  736. sourceloc := swapl;
  737. end;
  738. {*****************************************************************************
  739. Instruction table
  740. *****************************************************************************}
  741. procedure DoneCpu;
  742. begin
  743. {exitproc:=saveexit; }
  744. {$ifndef NOAG386BIN}
  745. if assigned(instabcache) then
  746. dispose(instabcache);
  747. {$endif NOAG386BIN}
  748. end;
  749. procedure BuildInsTabCache;
  750. {$ifndef NOAG386BIN}
  751. var
  752. i : longint;
  753. {$endif}
  754. begin
  755. {$ifndef NOAG386BIN}
  756. new(instabcache);
  757. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  758. i:=0;
  759. while (i<InsTabEntries) do
  760. begin
  761. if InsTabCache^[InsTab[i].OPcode]=-1 then
  762. InsTabCache^[InsTab[i].OPcode]:=i;
  763. inc(i);
  764. end;
  765. {$endif NOAG386BIN}
  766. end;
  767. procedure InitCpu;
  768. begin
  769. {$ifndef NOAG386BIN}
  770. if not assigned(instabcache) then
  771. BuildInsTabCache;
  772. {$endif NOAG386BIN}
  773. end;
  774. end.
  775. {
  776. $Log$
  777. Revision 1.7 2001-12-06 17:57:40 florian
  778. + parasym to tparaitem added
  779. Revision 1.6 2001/09/28 20:39:33 jonas
  780. * changed all flow control structures (except for exception handling
  781. related things) to processor independent code (in new ncgflw unit)
  782. + generic cgobj unit which contains lots of code generator helpers with
  783. global "cg" class instance variable
  784. + cgcpu unit for i386 (implements processor specific routines of the above
  785. unit)
  786. * updated cgbase and cpubase for the new code generator units
  787. * include ncgflw unit in cpunode unit
  788. Revision 1.5 2001/05/18 23:01:13 peter
  789. * portable constants
  790. Revision 1.4 2001/04/13 01:22:18 peter
  791. * symtable change to classes
  792. * range check generation and errors fixed, make cycle DEBUG=1 works
  793. * memory leaks fixed
  794. Revision 1.3 2001/02/20 21:34:04 peter
  795. * iret, lret fixes
  796. Revision 1.2 2000/12/07 17:19:45 jonas
  797. * new constant handling: from now on, hex constants >$7fffffff are
  798. parsed as unsigned constants (otherwise, $80000000 got sign extended
  799. and became $ffffffff80000000), all constants in the longint range
  800. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  801. are cardinals and the rest are int64's.
  802. * added lots of longint typecast to prevent range check errors in the
  803. compiler and rtl
  804. * type casts of symbolic ordinal constants are now preserved
  805. * fixed bug where the original resulttype wasn't restored correctly
  806. after doing a 64bit rangecheck
  807. Revision 1.1 2000/10/15 09:39:37 peter
  808. * moved cpu*.pas to i386/
  809. * renamed n386 to common cpunode
  810. Revision 1.7 2000/09/26 20:06:13 florian
  811. * hmm, still a lot of work to get things compilable
  812. Revision 1.6 2000/09/24 15:06:14 peter
  813. * use defines.inc
  814. Revision 1.5 2000/08/27 16:11:50 peter
  815. * moved some util functions from globals,cobjects to cutils
  816. * splitted files into finput,fmodule
  817. Revision 1.4 2000/08/05 13:25:06 peter
  818. * packenum 1 fixes (merged)
  819. Revision 1.3 2000/07/14 05:11:48 michael
  820. + Patch to 1.1
  821. Revision 1.2 2000/07/13 11:32:39 michael
  822. + removed logs
  823. }