aasmcpu.pas 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the abstract assembler implementation for the i386
  4. * Portions of this code was inspired by the NASM sources
  5. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit aasmcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,aasmsym,
  29. ogbase;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  38. OT_BITS16 = $00000002;
  39. OT_BITS32 = $00000004;
  40. OT_BITS64 = $00000008; { FPU only }
  41. OT_BITS80 = $00000010;
  42. OT_SIZE_MASK = $0000001F; { all the size attributes }
  43. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  44. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  45. OT_NEAR = $00000040;
  46. OT_SHORT = $00000080;
  47. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  48. OT_TO = $00000200; { operand is followed by a colon }
  49. { reverse effect in FADD, FSUB &c }
  50. OT_COLON = $00000400;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_IMM8 = $00002001;
  54. OT_IMM16 = $00002002;
  55. OT_IMM32 = $00002004;
  56. OT_IMM64 = $00002008;
  57. OT_IMM80 = $00002010;
  58. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  59. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  60. OT_REG8 = $00201001;
  61. OT_REG16 = $00201002;
  62. OT_REG32 = $00201004;
  63. OT_REG64 = $00201008;
  64. OT_XMMREG = $00201010; { Katmai registers }
  65. OT_MMXREG = $00201020; { MMX registers }
  66. OT_MEMORY = $00204000; { register number in 'basereg' }
  67. OT_MEM8 = $00204001;
  68. OT_MEM16 = $00204002;
  69. OT_MEM32 = $00204004;
  70. OT_MEM64 = $00204008;
  71. OT_MEM80 = $00204010;
  72. OT_FPUREG = $01000000; { floating point stack registers }
  73. OT_FPU0 = $01000800; { FPU stack register zero }
  74. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  75. { a mask for the following }
  76. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  77. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  78. OT_REG_AX = $00211002; { ditto }
  79. OT_REG_EAX = $00211004; { and again }
  80. {$ifdef x86_64}
  81. OT_REG_RAX = $00211008;
  82. {$endif x86_64}
  83. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  84. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  85. OT_REG_CX = $00221002; { ditto }
  86. OT_REG_ECX = $00221004; { another one }
  87. {$ifdef x86_64}
  88. OT_REG_RCX = $00221008;
  89. {$endif x86_64}
  90. OT_REG_DX = $00241002;
  91. OT_REG_EDX = $00241004;
  92. OT_REG_SREG = $00081002; { any segment register }
  93. OT_REG_CS = $01081002; { CS }
  94. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  95. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  96. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  97. OT_REG_CREG = $08101004; { CRn }
  98. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  99. OT_REG_DREG = $10101004; { DRn }
  100. OT_REG_TREG = $20101004; { TRn }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. { Size of the instruction table converted by nasmconv.pas }
  107. {$ifdef x86_64}
  108. instabentries = {$i x8664nop.inc}
  109. {$else x86_64}
  110. instabentries = {$i i386nop.inc}
  111. {$endif x86_64}
  112. maxinfolen = 11;
  113. MaxInsChanges = 3; { Max things a instruction can change }
  114. type
  115. { What an instruction can change. Needed for optimizer and spilling code.
  116. Note: The order of this enumeration is should not be changed! }
  117. TInsChange = (Ch_None,
  118. {Read from a register}
  119. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  120. {write from a register}
  121. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  122. {read and write from/to a register}
  123. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  124. {modify the contents of a register with the purpose of using
  125. this changed content afterwards (add/sub/..., but e.g. not rep
  126. or movsd)}
  127. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  128. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  129. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  130. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  131. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  132. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  133. Ch_WMemEDI,
  134. Ch_All,
  135. { x86_64 registers }
  136. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  137. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  138. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  139. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  140. );
  141. TInsProp = packed record
  142. Ch : Array[1..MaxInsChanges] of TInsChange;
  143. end;
  144. const
  145. InsProp : array[tasmop] of TInsProp =
  146. {$ifdef x86_64}
  147. {$i x8664pro.inc}
  148. {$else x86_64}
  149. {$i i386prop.inc}
  150. {$endif x86_64}
  151. type
  152. TOperandOrder = (op_intel,op_att);
  153. tinsentry=packed record
  154. opcode : tasmop;
  155. ops : byte;
  156. optypes : array[0..2] of longint;
  157. code : array[0..maxinfolen] of char;
  158. flags : cardinal;
  159. end;
  160. pinsentry=^tinsentry;
  161. { alignment for operator }
  162. tai_align = class(tai_align_abstract)
  163. reg : tregister;
  164. constructor create(b:byte);override;
  165. constructor create_op(b: byte; _op: byte);override;
  166. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  167. end;
  168. taicpu = class(tai_cpu_abstract_sym)
  169. opsize : topsize;
  170. constructor op_none(op : tasmop);
  171. constructor op_none(op : tasmop;_size : topsize);
  172. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  173. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  174. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  175. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  176. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  177. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  178. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  179. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  180. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  181. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  182. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  183. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  184. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  185. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  186. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  187. { this is for Jmp instructions }
  188. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  189. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  190. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  191. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  192. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  193. procedure changeopsize(siz:topsize);
  194. function GetString:string;
  195. procedure CheckNonCommutativeOpcodes;
  196. private
  197. FOperandOrder : TOperandOrder;
  198. procedure init(_size : topsize); { this need to be called by all constructor }
  199. public
  200. { the next will reset all instructions that can change in pass 2 }
  201. procedure ResetPass1;override;
  202. procedure ResetPass2;override;
  203. function CheckIfValid:boolean;
  204. function Pass1(objdata:TObjData):longint;override;
  205. procedure Pass2(objdata:TObjData);override;
  206. procedure SetOperandOrder(order:TOperandOrder);
  207. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  208. { register spilling code }
  209. function spilling_get_operation_type(opnr: longint): topertype;override;
  210. private
  211. { next fields are filled in pass1, so pass2 is faster }
  212. insentry : PInsEntry;
  213. insoffset : longint;
  214. LastInsOffset : longint; { need to be public to be reset }
  215. inssize : shortint;
  216. {$ifdef x86_64}
  217. rex : byte;
  218. {$endif x86_64}
  219. function InsEnd:longint;
  220. procedure create_ot(objdata:TObjData);
  221. function Matches(p:PInsEntry):boolean;
  222. function calcsize(p:PInsEntry):shortint;
  223. procedure gencode(objdata:TObjData);
  224. function NeedAddrPrefix(opidx:byte):boolean;
  225. procedure Swapoperands;
  226. function FindInsentry(objdata:TObjData):boolean;
  227. end;
  228. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  229. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  230. procedure InitAsm;
  231. procedure DoneAsm;
  232. implementation
  233. uses
  234. cutils,
  235. globals,
  236. itcpugas,
  237. symsym;
  238. {*****************************************************************************
  239. Instruction table
  240. *****************************************************************************}
  241. const
  242. {Instruction flags }
  243. IF_NONE = $00000000;
  244. IF_SM = $00000001; { size match first two operands }
  245. IF_SM2 = $00000002;
  246. IF_SB = $00000004; { unsized operands can't be non-byte }
  247. IF_SW = $00000008; { unsized operands can't be non-word }
  248. IF_SD = $00000010; { unsized operands can't be nondword }
  249. IF_SMASK = $0000001f;
  250. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  251. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  252. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  253. IF_ARMASK = $00000060; { mask for unsized argument spec }
  254. IF_PRIV = $00000100; { it's a privileged instruction }
  255. IF_SMM = $00000200; { it's only valid in SMM }
  256. IF_PROT = $00000400; { it's protected mode only }
  257. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  258. IF_UNDOC = $00001000; { it's an undocumented instruction }
  259. IF_FPU = $00002000; { it's an FPU instruction }
  260. IF_MMX = $00004000; { it's an MMX instruction }
  261. { it's a 3DNow! instruction }
  262. IF_3DNOW = $00008000;
  263. { it's a SSE (KNI, MMX2) instruction }
  264. IF_SSE = $00010000;
  265. { SSE2 instructions }
  266. IF_SSE2 = $00020000;
  267. { SSE3 instructions }
  268. IF_SSE3 = $00040000;
  269. { SSE64 instructions }
  270. IF_SSE64 = $00080000;
  271. { the mask for processor types }
  272. {IF_PMASK = longint($FF000000);}
  273. { the mask for disassembly "prefer" }
  274. {IF_PFMASK = longint($F001FF00);}
  275. { SVM instructions }
  276. IF_SVM = $00100000;
  277. { SSE4 instructions }
  278. IF_SSE4 = $00200000;
  279. IF_8086 = $00000000; { 8086 instruction }
  280. IF_186 = $01000000; { 186+ instruction }
  281. IF_286 = $02000000; { 286+ instruction }
  282. IF_386 = $03000000; { 386+ instruction }
  283. IF_486 = $04000000; { 486+ instruction }
  284. IF_PENT = $05000000; { Pentium instruction }
  285. IF_P6 = $06000000; { P6 instruction }
  286. IF_KATMAI = $07000000; { Katmai instructions }
  287. { Willamette instructions }
  288. IF_WILLAMETTE = $08000000;
  289. { Prescott instructions }
  290. IF_PRESCOTT = $09000000;
  291. IF_X86_64 = $0a000000;
  292. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  293. IF_AMD = $0c000000; { AMD-specific instruction }
  294. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  295. { added flags }
  296. IF_PRE = $40000000; { it's a prefix instruction }
  297. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  298. type
  299. TInsTabCache=array[TasmOp] of longint;
  300. PInsTabCache=^TInsTabCache;
  301. const
  302. {$ifdef x86_64}
  303. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  304. {$else x86_64}
  305. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  306. {$endif x86_64}
  307. var
  308. InsTabCache : PInsTabCache;
  309. const
  310. {$ifdef x86_64}
  311. { Intel style operands ! }
  312. opsize_2_type:array[0..2,topsize] of longint=(
  313. (OT_NONE,
  314. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  315. OT_BITS16,OT_BITS32,OT_BITS64,
  316. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  317. OT_BITS64,
  318. OT_NEAR,OT_FAR,OT_SHORT,
  319. OT_NONE,
  320. OT_NONE
  321. ),
  322. (OT_NONE,
  323. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  324. OT_BITS16,OT_BITS32,OT_BITS64,
  325. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  326. OT_BITS64,
  327. OT_NEAR,OT_FAR,OT_SHORT,
  328. OT_NONE,
  329. OT_NONE
  330. ),
  331. (OT_NONE,
  332. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  333. OT_BITS16,OT_BITS32,OT_BITS64,
  334. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  335. OT_BITS64,
  336. OT_NEAR,OT_FAR,OT_SHORT,
  337. OT_NONE,
  338. OT_NONE
  339. )
  340. );
  341. reg_ot_table : array[tregisterindex] of longint = (
  342. {$i r8664ot.inc}
  343. );
  344. {$else x86_64}
  345. { Intel style operands ! }
  346. opsize_2_type:array[0..2,topsize] of longint=(
  347. (OT_NONE,
  348. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  349. OT_BITS16,OT_BITS32,OT_BITS64,
  350. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  351. OT_BITS64,
  352. OT_NEAR,OT_FAR,OT_SHORT,
  353. OT_NONE,
  354. OT_NONE
  355. ),
  356. (OT_NONE,
  357. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  358. OT_BITS16,OT_BITS32,OT_BITS64,
  359. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  360. OT_BITS64,
  361. OT_NEAR,OT_FAR,OT_SHORT,
  362. OT_NONE,
  363. OT_NONE
  364. ),
  365. (OT_NONE,
  366. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  367. OT_BITS16,OT_BITS32,OT_BITS64,
  368. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  369. OT_BITS64,
  370. OT_NEAR,OT_FAR,OT_SHORT,
  371. OT_NONE,
  372. OT_NONE
  373. )
  374. );
  375. reg_ot_table : array[tregisterindex] of longint = (
  376. {$i r386ot.inc}
  377. );
  378. {$endif x86_64}
  379. { Operation type for spilling code }
  380. type
  381. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  382. var
  383. operation_type_table : ^toperation_type_table;
  384. {****************************************************************************
  385. TAI_ALIGN
  386. ****************************************************************************}
  387. constructor tai_align.create(b: byte);
  388. begin
  389. inherited create(b);
  390. reg:=NR_ECX;
  391. end;
  392. constructor tai_align.create_op(b: byte; _op: byte);
  393. begin
  394. inherited create_op(b,_op);
  395. reg:=NR_NO;
  396. end;
  397. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  398. const
  399. {$ifdef x86_64}
  400. alignarray:array[0..3] of string[4]=(
  401. #$66#$66#$66#$90,
  402. #$66#$66#$90,
  403. #$66#$90,
  404. #$90
  405. );
  406. {$else x86_64}
  407. alignarray:array[0..5] of string[8]=(
  408. #$8D#$B4#$26#$00#$00#$00#$00,
  409. #$8D#$B6#$00#$00#$00#$00,
  410. #$8D#$74#$26#$00,
  411. #$8D#$76#$00,
  412. #$89#$F6,
  413. #$90);
  414. {$endif x86_64}
  415. var
  416. bufptr : pchar;
  417. j : longint;
  418. localsize: byte;
  419. begin
  420. inherited calculatefillbuf(buf);
  421. if not use_op then
  422. begin
  423. bufptr:=pchar(@buf);
  424. { fillsize may still be used afterwards, so don't modify }
  425. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  426. localsize:=fillsize;
  427. while (localsize>0) do
  428. begin
  429. for j:=low(alignarray) to high(alignarray) do
  430. if (localsize>=length(alignarray[j])) then
  431. break;
  432. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  433. inc(bufptr,length(alignarray[j]));
  434. dec(localsize,length(alignarray[j]));
  435. end;
  436. end;
  437. calculatefillbuf:=pchar(@buf);
  438. end;
  439. {*****************************************************************************
  440. Taicpu Constructors
  441. *****************************************************************************}
  442. procedure taicpu.changeopsize(siz:topsize);
  443. begin
  444. opsize:=siz;
  445. end;
  446. procedure taicpu.init(_size : topsize);
  447. begin
  448. { default order is att }
  449. FOperandOrder:=op_att;
  450. segprefix:=NR_NO;
  451. opsize:=_size;
  452. insentry:=nil;
  453. LastInsOffset:=-1;
  454. InsOffset:=0;
  455. InsSize:=0;
  456. end;
  457. constructor taicpu.op_none(op : tasmop);
  458. begin
  459. inherited create(op);
  460. init(S_NO);
  461. end;
  462. constructor taicpu.op_none(op : tasmop;_size : topsize);
  463. begin
  464. inherited create(op);
  465. init(_size);
  466. end;
  467. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  468. begin
  469. inherited create(op);
  470. init(_size);
  471. ops:=1;
  472. loadreg(0,_op1);
  473. end;
  474. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  475. begin
  476. inherited create(op);
  477. init(_size);
  478. ops:=1;
  479. loadconst(0,_op1);
  480. end;
  481. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  482. begin
  483. inherited create(op);
  484. init(_size);
  485. ops:=1;
  486. loadref(0,_op1);
  487. end;
  488. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  489. begin
  490. inherited create(op);
  491. init(_size);
  492. ops:=2;
  493. loadreg(0,_op1);
  494. loadreg(1,_op2);
  495. end;
  496. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  497. begin
  498. inherited create(op);
  499. init(_size);
  500. ops:=2;
  501. loadreg(0,_op1);
  502. loadconst(1,_op2);
  503. end;
  504. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  505. begin
  506. inherited create(op);
  507. init(_size);
  508. ops:=2;
  509. loadreg(0,_op1);
  510. loadref(1,_op2);
  511. end;
  512. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  513. begin
  514. inherited create(op);
  515. init(_size);
  516. ops:=2;
  517. loadconst(0,_op1);
  518. loadreg(1,_op2);
  519. end;
  520. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  521. begin
  522. inherited create(op);
  523. init(_size);
  524. ops:=2;
  525. loadconst(0,_op1);
  526. loadconst(1,_op2);
  527. end;
  528. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  529. begin
  530. inherited create(op);
  531. init(_size);
  532. ops:=2;
  533. loadconst(0,_op1);
  534. loadref(1,_op2);
  535. end;
  536. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  537. begin
  538. inherited create(op);
  539. init(_size);
  540. ops:=2;
  541. loadref(0,_op1);
  542. loadreg(1,_op2);
  543. end;
  544. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  545. begin
  546. inherited create(op);
  547. init(_size);
  548. ops:=3;
  549. loadreg(0,_op1);
  550. loadreg(1,_op2);
  551. loadreg(2,_op3);
  552. end;
  553. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  554. begin
  555. inherited create(op);
  556. init(_size);
  557. ops:=3;
  558. loadconst(0,_op1);
  559. loadreg(1,_op2);
  560. loadreg(2,_op3);
  561. end;
  562. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  563. begin
  564. inherited create(op);
  565. init(_size);
  566. ops:=3;
  567. loadreg(0,_op1);
  568. loadreg(1,_op2);
  569. loadref(2,_op3);
  570. end;
  571. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  572. begin
  573. inherited create(op);
  574. init(_size);
  575. ops:=3;
  576. loadconst(0,_op1);
  577. loadref(1,_op2);
  578. loadreg(2,_op3);
  579. end;
  580. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  581. begin
  582. inherited create(op);
  583. init(_size);
  584. ops:=3;
  585. loadconst(0,_op1);
  586. loadreg(1,_op2);
  587. loadref(2,_op3);
  588. end;
  589. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  590. begin
  591. inherited create(op);
  592. init(_size);
  593. condition:=cond;
  594. ops:=1;
  595. loadsymbol(0,_op1,0);
  596. end;
  597. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  598. begin
  599. inherited create(op);
  600. init(_size);
  601. ops:=1;
  602. loadsymbol(0,_op1,0);
  603. end;
  604. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  605. begin
  606. inherited create(op);
  607. init(_size);
  608. ops:=1;
  609. loadsymbol(0,_op1,_op1ofs);
  610. end;
  611. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  612. begin
  613. inherited create(op);
  614. init(_size);
  615. ops:=2;
  616. loadsymbol(0,_op1,_op1ofs);
  617. loadreg(1,_op2);
  618. end;
  619. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  620. begin
  621. inherited create(op);
  622. init(_size);
  623. ops:=2;
  624. loadsymbol(0,_op1,_op1ofs);
  625. loadref(1,_op2);
  626. end;
  627. function taicpu.GetString:string;
  628. var
  629. i : longint;
  630. s : string;
  631. addsize : boolean;
  632. begin
  633. s:='['+std_op2str[opcode];
  634. for i:=0 to ops-1 do
  635. begin
  636. with oper[i]^ do
  637. begin
  638. if i=0 then
  639. s:=s+' '
  640. else
  641. s:=s+',';
  642. { type }
  643. addsize:=false;
  644. if (ot and OT_XMMREG)=OT_XMMREG then
  645. s:=s+'xmmreg'
  646. else
  647. if (ot and OT_MMXREG)=OT_MMXREG then
  648. s:=s+'mmxreg'
  649. else
  650. if (ot and OT_FPUREG)=OT_FPUREG then
  651. s:=s+'fpureg'
  652. else
  653. if (ot and OT_REGISTER)=OT_REGISTER then
  654. begin
  655. s:=s+'reg';
  656. addsize:=true;
  657. end
  658. else
  659. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  660. begin
  661. s:=s+'imm';
  662. addsize:=true;
  663. end
  664. else
  665. if (ot and OT_MEMORY)=OT_MEMORY then
  666. begin
  667. s:=s+'mem';
  668. addsize:=true;
  669. end
  670. else
  671. s:=s+'???';
  672. { size }
  673. if addsize then
  674. begin
  675. if (ot and OT_BITS8)<>0 then
  676. s:=s+'8'
  677. else
  678. if (ot and OT_BITS16)<>0 then
  679. s:=s+'16'
  680. else
  681. if (ot and OT_BITS32)<>0 then
  682. s:=s+'32'
  683. else
  684. if (ot and OT_BITS64)<>0 then
  685. s:=s+'64'
  686. else
  687. s:=s+'??';
  688. { signed }
  689. if (ot and OT_SIGNED)<>0 then
  690. s:=s+'s';
  691. end;
  692. end;
  693. end;
  694. GetString:=s+']';
  695. end;
  696. procedure taicpu.Swapoperands;
  697. var
  698. p : POper;
  699. begin
  700. { Fix the operands which are in AT&T style and we need them in Intel style }
  701. case ops of
  702. 2 : begin
  703. { 0,1 -> 1,0 }
  704. p:=oper[0];
  705. oper[0]:=oper[1];
  706. oper[1]:=p;
  707. end;
  708. 3 : begin
  709. { 0,1,2 -> 2,1,0 }
  710. p:=oper[0];
  711. oper[0]:=oper[2];
  712. oper[2]:=p;
  713. end;
  714. end;
  715. end;
  716. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  717. begin
  718. if FOperandOrder<>order then
  719. begin
  720. Swapoperands;
  721. FOperandOrder:=order;
  722. end;
  723. end;
  724. procedure taicpu.CheckNonCommutativeOpcodes;
  725. begin
  726. { we need ATT order }
  727. SetOperandOrder(op_att);
  728. if (
  729. (ops=2) and
  730. (oper[0]^.typ=top_reg) and
  731. (oper[1]^.typ=top_reg) and
  732. { if the first is ST and the second is also a register
  733. it is necessarily ST1 .. ST7 }
  734. ((oper[0]^.reg=NR_ST) or
  735. (oper[0]^.reg=NR_ST0))
  736. ) or
  737. { ((ops=1) and
  738. (oper[0]^.typ=top_reg) and
  739. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  740. (ops=0) then
  741. begin
  742. if opcode=A_FSUBR then
  743. opcode:=A_FSUB
  744. else if opcode=A_FSUB then
  745. opcode:=A_FSUBR
  746. else if opcode=A_FDIVR then
  747. opcode:=A_FDIV
  748. else if opcode=A_FDIV then
  749. opcode:=A_FDIVR
  750. else if opcode=A_FSUBRP then
  751. opcode:=A_FSUBP
  752. else if opcode=A_FSUBP then
  753. opcode:=A_FSUBRP
  754. else if opcode=A_FDIVRP then
  755. opcode:=A_FDIVP
  756. else if opcode=A_FDIVP then
  757. opcode:=A_FDIVRP;
  758. end;
  759. if (
  760. (ops=1) and
  761. (oper[0]^.typ=top_reg) and
  762. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  763. (oper[0]^.reg<>NR_ST)
  764. ) then
  765. begin
  766. if opcode=A_FSUBRP then
  767. opcode:=A_FSUBP
  768. else if opcode=A_FSUBP then
  769. opcode:=A_FSUBRP
  770. else if opcode=A_FDIVRP then
  771. opcode:=A_FDIVP
  772. else if opcode=A_FDIVP then
  773. opcode:=A_FDIVRP;
  774. end;
  775. end;
  776. {*****************************************************************************
  777. Assembler
  778. *****************************************************************************}
  779. type
  780. ea = packed record
  781. sib_present : boolean;
  782. bytes : byte;
  783. size : byte;
  784. modrm : byte;
  785. sib : byte;
  786. {$ifdef x86_64}
  787. rex_present : boolean;
  788. rex : byte;
  789. {$endif x86_64}
  790. end;
  791. procedure taicpu.create_ot(objdata:TObjData);
  792. {
  793. this function will also fix some other fields which only needs to be once
  794. }
  795. var
  796. i,l,relsize : longint;
  797. currsym : TObjSymbol;
  798. begin
  799. if ops=0 then
  800. exit;
  801. { update oper[].ot field }
  802. for i:=0 to ops-1 do
  803. with oper[i]^ do
  804. begin
  805. case typ of
  806. top_reg :
  807. begin
  808. ot:=reg_ot_table[findreg_by_number(reg)];
  809. end;
  810. top_ref :
  811. begin
  812. if (ref^.refaddr=addr_no)
  813. {$ifdef x86_64}
  814. or (
  815. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  816. (ref^.base<>NR_NO)
  817. )
  818. {$endif x86_64}
  819. then
  820. begin
  821. { create ot field }
  822. if (ot and OT_SIZE_MASK)=0 then
  823. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  824. else
  825. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  826. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  827. ot:=ot or OT_MEM_OFFS;
  828. { fix scalefactor }
  829. if (ref^.index=NR_NO) then
  830. ref^.scalefactor:=0
  831. else
  832. if (ref^.scalefactor=0) then
  833. ref^.scalefactor:=1;
  834. end
  835. else
  836. begin
  837. { Jumps use a relative offset which can be 8bit,
  838. for other opcodes we always need to generate the full
  839. 32bit address }
  840. if assigned(objdata) and
  841. is_jmp then
  842. begin
  843. currsym:=objdata.symbolref(ref^.symbol);
  844. l:=ref^.offset;
  845. if assigned(currsym) then
  846. inc(l,currsym.address);
  847. { when it is a forward jump we need to compensate the
  848. offset of the instruction since the previous time,
  849. because the symbol address is then still using the
  850. 'old-style' addressing.
  851. For backwards jumps this is not required because the
  852. address of the symbol is already adjusted to the
  853. new offset }
  854. if (l>InsOffset) and (LastInsOffset<>-1) then
  855. inc(l,InsOffset-LastInsOffset);
  856. { instruction size will then always become 2 (PFV) }
  857. relsize:=(InsOffset+2)-l;
  858. if (relsize>=-128) and (relsize<=127) and
  859. (
  860. not assigned(currsym) or
  861. (currsym.objsection=objdata.currobjsec)
  862. ) then
  863. ot:=OT_IMM8 or OT_SHORT
  864. else
  865. ot:=OT_IMM32 or OT_NEAR;
  866. end
  867. else
  868. ot:=OT_IMM32 or OT_NEAR;
  869. end;
  870. end;
  871. top_local :
  872. begin
  873. if (ot and OT_SIZE_MASK)=0 then
  874. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  875. else
  876. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  877. end;
  878. top_const :
  879. begin
  880. { allow 2nd or 3rd operand being a constant and expect no size for shuf* etc. }
  881. { further, allow AAD and AAM with imm. operand }
  882. if (opsize=S_NO) and not((i in [1,2]) or ((i=0) and (opcode in [A_AAD,A_AAM]))) then
  883. message(asmr_e_invalid_opcode_and_operand);
  884. if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
  885. ot:=OT_IMM8 or OT_SIGNED
  886. else
  887. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  888. if (val=1) and (i=1) then
  889. ot := ot or OT_ONENESS;
  890. end;
  891. top_none :
  892. begin
  893. { generated when there was an error in the
  894. assembler reader. It never happends when generating
  895. assembler }
  896. end;
  897. else
  898. internalerror(200402261);
  899. end;
  900. end;
  901. end;
  902. function taicpu.InsEnd:longint;
  903. begin
  904. InsEnd:=InsOffset+InsSize;
  905. end;
  906. function taicpu.Matches(p:PInsEntry):boolean;
  907. { * IF_SM stands for Size Match: any operand whose size is not
  908. * explicitly specified by the template is `really' intended to be
  909. * the same size as the first size-specified operand.
  910. * Non-specification is tolerated in the input instruction, but
  911. * _wrong_ specification is not.
  912. *
  913. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  914. * three-operand instructions such as SHLD: it implies that the
  915. * first two operands must match in size, but that the third is
  916. * required to be _unspecified_.
  917. *
  918. * IF_SB invokes Size Byte: operands with unspecified size in the
  919. * template are really bytes, and so no non-byte specification in
  920. * the input instruction will be tolerated. IF_SW similarly invokes
  921. * Size Word, and IF_SD invokes Size Doubleword.
  922. *
  923. * (The default state if neither IF_SM nor IF_SM2 is specified is
  924. * that any operand with unspecified size in the template is
  925. * required to have unspecified size in the instruction too...)
  926. }
  927. var
  928. insot,
  929. currot,
  930. i,j,asize,oprs : longint;
  931. insflags:cardinal;
  932. siz : array[0..2] of longint;
  933. begin
  934. result:=false;
  935. { Check the opcode and operands }
  936. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  937. exit;
  938. for i:=0 to p^.ops-1 do
  939. begin
  940. insot:=p^.optypes[i];
  941. currot:=oper[i]^.ot;
  942. { Check the operand flags }
  943. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  944. exit;
  945. { Check if the passed operand size matches with one of
  946. the supported operand sizes }
  947. if ((insot and OT_SIZE_MASK)<>0) and
  948. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  949. exit;
  950. end;
  951. { Check operand sizes }
  952. insflags:=p^.flags;
  953. if insflags and IF_SMASK<>0 then
  954. begin
  955. { as default an untyped size can get all the sizes, this is different
  956. from nasm, but else we need to do a lot checking which opcodes want
  957. size or not with the automatic size generation }
  958. asize:=-1;
  959. if (insflags and IF_SB)<>0 then
  960. asize:=OT_BITS8
  961. else if (insflags and IF_SW)<>0 then
  962. asize:=OT_BITS16
  963. else if (insflags and IF_SD)<>0 then
  964. asize:=OT_BITS32;
  965. if (insflags and IF_ARMASK)<>0 then
  966. begin
  967. siz[0]:=0;
  968. siz[1]:=0;
  969. siz[2]:=0;
  970. if (insflags and IF_AR0)<>0 then
  971. siz[0]:=asize
  972. else if (insflags and IF_AR1)<>0 then
  973. siz[1]:=asize
  974. else if (insflags and IF_AR2)<>0 then
  975. siz[2]:=asize;
  976. end
  977. else
  978. begin
  979. siz[0]:=asize;
  980. siz[1]:=asize;
  981. siz[2]:=asize;
  982. end;
  983. if (insflags and (IF_SM or IF_SM2))<>0 then
  984. begin
  985. if (insflags and IF_SM2)<>0 then
  986. oprs:=2
  987. else
  988. oprs:=p^.ops;
  989. for i:=0 to oprs-1 do
  990. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  991. begin
  992. for j:=0 to oprs-1 do
  993. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  994. break;
  995. end;
  996. end
  997. else
  998. oprs:=2;
  999. { Check operand sizes }
  1000. for i:=0 to p^.ops-1 do
  1001. begin
  1002. insot:=p^.optypes[i];
  1003. currot:=oper[i]^.ot;
  1004. if ((insot and OT_SIZE_MASK)=0) and
  1005. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1006. { Immediates can always include smaller size }
  1007. ((currot and OT_IMMEDIATE)=0) and
  1008. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1009. exit;
  1010. end;
  1011. end;
  1012. result:=true;
  1013. end;
  1014. procedure taicpu.ResetPass1;
  1015. begin
  1016. { we need to reset everything here, because the choosen insentry
  1017. can be invalid for a new situation where the previously optimized
  1018. insentry is not correct }
  1019. InsEntry:=nil;
  1020. InsSize:=0;
  1021. LastInsOffset:=-1;
  1022. end;
  1023. procedure taicpu.ResetPass2;
  1024. begin
  1025. { we are here in a second pass, check if the instruction can be optimized }
  1026. if assigned(InsEntry) and
  1027. ((InsEntry^.flags and IF_PASS2)<>0) then
  1028. begin
  1029. InsEntry:=nil;
  1030. InsSize:=0;
  1031. end;
  1032. LastInsOffset:=-1;
  1033. end;
  1034. function taicpu.CheckIfValid:boolean;
  1035. begin
  1036. result:=FindInsEntry(nil);
  1037. end;
  1038. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1039. var
  1040. i : longint;
  1041. begin
  1042. result:=false;
  1043. { Things which may only be done once, not when a second pass is done to
  1044. optimize }
  1045. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1046. begin
  1047. current_filepos:=fileinfo;
  1048. { We need intel style operands }
  1049. SetOperandOrder(op_intel);
  1050. { create the .ot fields }
  1051. create_ot(objdata);
  1052. { set the file postion }
  1053. end
  1054. else
  1055. begin
  1056. { we've already an insentry so it's valid }
  1057. result:=true;
  1058. exit;
  1059. end;
  1060. { Lookup opcode in the table }
  1061. InsSize:=-1;
  1062. i:=instabcache^[opcode];
  1063. if i=-1 then
  1064. begin
  1065. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1066. exit;
  1067. end;
  1068. insentry:=@instab[i];
  1069. while (insentry^.opcode=opcode) do
  1070. begin
  1071. if matches(insentry) then
  1072. begin
  1073. result:=true;
  1074. exit;
  1075. end;
  1076. inc(insentry);
  1077. end;
  1078. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1079. { No instruction found, set insentry to nil and inssize to -1 }
  1080. insentry:=nil;
  1081. inssize:=-1;
  1082. end;
  1083. function taicpu.Pass1(objdata:TObjData):longint;
  1084. begin
  1085. Pass1:=0;
  1086. { Save the old offset and set the new offset }
  1087. InsOffset:=ObjData.CurrObjSec.Size;
  1088. { Error? }
  1089. if (Insentry=nil) and (InsSize=-1) then
  1090. exit;
  1091. { set the file postion }
  1092. current_filepos:=fileinfo;
  1093. { Get InsEntry }
  1094. if FindInsEntry(ObjData) then
  1095. begin
  1096. { Calculate instruction size }
  1097. InsSize:=calcsize(insentry);
  1098. if segprefix<>NR_NO then
  1099. inc(InsSize);
  1100. { Fix opsize if size if forced }
  1101. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1102. begin
  1103. if (insentry^.flags and IF_ARMASK)=0 then
  1104. begin
  1105. if (insentry^.flags and IF_SB)<>0 then
  1106. begin
  1107. if opsize=S_NO then
  1108. opsize:=S_B;
  1109. end
  1110. else if (insentry^.flags and IF_SW)<>0 then
  1111. begin
  1112. if opsize=S_NO then
  1113. opsize:=S_W;
  1114. end
  1115. else if (insentry^.flags and IF_SD)<>0 then
  1116. begin
  1117. if opsize=S_NO then
  1118. opsize:=S_L;
  1119. end;
  1120. end;
  1121. end;
  1122. LastInsOffset:=InsOffset;
  1123. Pass1:=InsSize;
  1124. exit;
  1125. end;
  1126. LastInsOffset:=-1;
  1127. end;
  1128. procedure taicpu.Pass2(objdata:TObjData);
  1129. var
  1130. c : longint;
  1131. begin
  1132. { error in pass1 ? }
  1133. if insentry=nil then
  1134. exit;
  1135. current_filepos:=fileinfo;
  1136. { Segment override }
  1137. if (segprefix<>NR_NO) then
  1138. begin
  1139. case segprefix of
  1140. NR_CS : c:=$2e;
  1141. NR_DS : c:=$3e;
  1142. NR_ES : c:=$26;
  1143. NR_FS : c:=$64;
  1144. NR_GS : c:=$65;
  1145. NR_SS : c:=$36;
  1146. end;
  1147. objdata.writebytes(c,1);
  1148. { fix the offset for GenNode }
  1149. inc(InsOffset);
  1150. end;
  1151. { Generate the instruction }
  1152. GenCode(objdata);
  1153. end;
  1154. function taicpu.needaddrprefix(opidx:byte):boolean;
  1155. begin
  1156. result:=(oper[opidx]^.typ=top_ref) and
  1157. (oper[opidx]^.ref^.refaddr=addr_no) and
  1158. {$ifdef x86_64}
  1159. (oper[opidx]^.ref^.base<>NR_RIP) and
  1160. {$endif x86_64}
  1161. (
  1162. (
  1163. (oper[opidx]^.ref^.index<>NR_NO) and
  1164. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1165. ) or
  1166. (
  1167. (oper[opidx]^.ref^.base<>NR_NO) and
  1168. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1169. )
  1170. );
  1171. end;
  1172. function regval(r:Tregister):byte;
  1173. const
  1174. {$ifdef x86_64}
  1175. opcode_table:array[tregisterindex] of tregisterindex = (
  1176. {$i r8664op.inc}
  1177. );
  1178. {$else x86_64}
  1179. opcode_table:array[tregisterindex] of tregisterindex = (
  1180. {$i r386op.inc}
  1181. );
  1182. {$endif x86_64}
  1183. var
  1184. regidx : tregisterindex;
  1185. begin
  1186. regidx:=findreg_by_number(r);
  1187. if regidx<>0 then
  1188. result:=opcode_table[regidx]
  1189. else
  1190. begin
  1191. Message1(asmw_e_invalid_register,generic_regname(r));
  1192. result:=0;
  1193. end;
  1194. end;
  1195. {$ifdef x86_64}
  1196. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1197. var
  1198. sym : tasmsymbol;
  1199. md,s,rv : byte;
  1200. base,index,scalefactor,
  1201. o : longint;
  1202. ir,br : Tregister;
  1203. isub,bsub : tsubregister;
  1204. begin
  1205. process_ea:=false;
  1206. fillchar(output,sizeof(output),0);
  1207. {Register ?}
  1208. if (input.typ=top_reg) then
  1209. begin
  1210. rv:=regval(input.reg);
  1211. output.modrm:=$c0 or (rfield shl 3) or rv;
  1212. output.size:=1;
  1213. if ((getregtype(input.reg)=R_INTREGISTER) and
  1214. (getsupreg(input.reg)>=RS_R8)) or
  1215. ((getregtype(input.reg)=R_MMREGISTER) and
  1216. (getsupreg(input.reg)>=RS_XMM8)) then
  1217. begin
  1218. output.rex_present:=true;
  1219. output.rex:=output.rex or $41;
  1220. inc(output.size,1);
  1221. end
  1222. else if (getregtype(input.reg)=R_INTREGISTER) and
  1223. (getsubreg(input.reg)=R_SUBL) and
  1224. (getsupreg(input.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1225. begin
  1226. output.rex_present:=true;
  1227. output.rex:=output.rex or $40;
  1228. inc(output.size,1);
  1229. end;
  1230. process_ea:=true;
  1231. exit;
  1232. end;
  1233. {No register, so memory reference.}
  1234. if input.typ<>top_ref then
  1235. internalerror(200409263);
  1236. ir:=input.ref^.index;
  1237. br:=input.ref^.base;
  1238. isub:=getsubreg(ir);
  1239. bsub:=getsubreg(br);
  1240. s:=input.ref^.scalefactor;
  1241. o:=input.ref^.offset;
  1242. sym:=input.ref^.symbol;
  1243. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1244. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1245. internalerror(200301081);
  1246. { it's direct address }
  1247. if (br=NR_NO) and (ir=NR_NO) then
  1248. begin
  1249. output.sib_present:=true;
  1250. output.bytes:=4;
  1251. output.modrm:=4 or (rfield shl 3);
  1252. output.sib:=$25;
  1253. end
  1254. else if (br=NR_RIP) and (ir=NR_NO) then
  1255. begin
  1256. { rip based }
  1257. output.sib_present:=false;
  1258. output.bytes:=4;
  1259. output.modrm:=5 or (rfield shl 3);
  1260. end
  1261. else
  1262. { it's an indirection }
  1263. begin
  1264. { 16 bit or 32 bit address? }
  1265. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1266. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1267. message(asmw_e_16bit_32bit_not_supported);
  1268. { wrong, for various reasons }
  1269. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1270. exit;
  1271. if ((getregtype(br)=R_INTREGISTER) and
  1272. (getsupreg(br)>=RS_R8)) or
  1273. ((getregtype(br)=R_MMREGISTER) and
  1274. (getsupreg(br)>=RS_XMM8)) then
  1275. begin
  1276. output.rex_present:=true;
  1277. output.rex:=output.rex or $41;
  1278. end;
  1279. if ((getregtype(ir)=R_INTREGISTER) and
  1280. (getsupreg(ir)>=RS_R8)) or
  1281. ((getregtype(ir)=R_MMREGISTER) and
  1282. (getsupreg(ir)>=RS_XMM8)) then
  1283. begin
  1284. output.rex_present:=true;
  1285. output.rex:=output.rex or $42;
  1286. end;
  1287. process_ea:=true;
  1288. { base }
  1289. case br of
  1290. NR_R8,
  1291. NR_RAX : base:=0;
  1292. NR_R9,
  1293. NR_RCX : base:=1;
  1294. NR_R10,
  1295. NR_RDX : base:=2;
  1296. NR_R11,
  1297. NR_RBX : base:=3;
  1298. NR_R12,
  1299. NR_RSP : base:=4;
  1300. NR_R13,
  1301. NR_NO,
  1302. NR_RBP : base:=5;
  1303. NR_R14,
  1304. NR_RSI : base:=6;
  1305. NR_R15,
  1306. NR_RDI : base:=7;
  1307. else
  1308. exit;
  1309. end;
  1310. { index }
  1311. case ir of
  1312. NR_R8,
  1313. NR_RAX : index:=0;
  1314. NR_R9,
  1315. NR_RCX : index:=1;
  1316. NR_R10,
  1317. NR_RDX : index:=2;
  1318. NR_R11,
  1319. NR_RBX : index:=3;
  1320. NR_R12,
  1321. NR_NO : index:=4;
  1322. NR_R13,
  1323. NR_RBP : index:=5;
  1324. NR_R14,
  1325. NR_RSI : index:=6;
  1326. NR_R15,
  1327. NR_RDI : index:=7;
  1328. else
  1329. exit;
  1330. end;
  1331. case s of
  1332. 0,
  1333. 1 : scalefactor:=0;
  1334. 2 : scalefactor:=1;
  1335. 4 : scalefactor:=2;
  1336. 8 : scalefactor:=3;
  1337. else
  1338. exit;
  1339. end;
  1340. { If rbp or r13 is used we must always include an offset }
  1341. if (br=NR_NO) or
  1342. ((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
  1343. md:=0
  1344. else
  1345. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1346. md:=1
  1347. else
  1348. md:=2;
  1349. if (br=NR_NO) or (md=2) then
  1350. output.bytes:=4
  1351. else
  1352. output.bytes:=md;
  1353. { SIB needed ? }
  1354. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1355. begin
  1356. output.sib_present:=false;
  1357. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1358. end
  1359. else
  1360. begin
  1361. output.sib_present:=true;
  1362. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1363. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1364. end;
  1365. end;
  1366. output.size:=1+ord(output.sib_present)+ord(output.rex_present)+output.bytes;
  1367. process_ea:=true;
  1368. end;
  1369. {$else x86_64}
  1370. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1371. var
  1372. sym : tasmsymbol;
  1373. md,s,rv : byte;
  1374. base,index,scalefactor,
  1375. o : longint;
  1376. ir,br : Tregister;
  1377. isub,bsub : tsubregister;
  1378. begin
  1379. process_ea:=false;
  1380. fillchar(output,sizeof(output),0);
  1381. {Register ?}
  1382. if (input.typ=top_reg) then
  1383. begin
  1384. rv:=regval(input.reg);
  1385. output.modrm:=$c0 or (rfield shl 3) or rv;
  1386. output.size:=1;
  1387. process_ea:=true;
  1388. exit;
  1389. end;
  1390. {No register, so memory reference.}
  1391. if (input.typ<>top_ref) then
  1392. internalerror(200409262);
  1393. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1394. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1395. internalerror(200301081);
  1396. ir:=input.ref^.index;
  1397. br:=input.ref^.base;
  1398. isub:=getsubreg(ir);
  1399. bsub:=getsubreg(br);
  1400. s:=input.ref^.scalefactor;
  1401. o:=input.ref^.offset;
  1402. sym:=input.ref^.symbol;
  1403. { it's direct address }
  1404. if (br=NR_NO) and (ir=NR_NO) then
  1405. begin
  1406. { it's a pure offset }
  1407. output.sib_present:=false;
  1408. output.bytes:=4;
  1409. output.modrm:=5 or (rfield shl 3);
  1410. end
  1411. else
  1412. { it's an indirection }
  1413. begin
  1414. { 16 bit address? }
  1415. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1416. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1417. message(asmw_e_16bit_not_supported);
  1418. {$ifdef OPTEA}
  1419. { make single reg base }
  1420. if (br=NR_NO) and (s=1) then
  1421. begin
  1422. br:=ir;
  1423. ir:=NR_NO;
  1424. end;
  1425. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1426. if (br=NR_NO) and
  1427. (((s=2) and (ir<>NR_ESP)) or
  1428. (s=3) or (s=5) or (s=9)) then
  1429. begin
  1430. br:=ir;
  1431. dec(s);
  1432. end;
  1433. { swap ESP into base if scalefactor is 1 }
  1434. if (s=1) and (ir=NR_ESP) then
  1435. begin
  1436. ir:=br;
  1437. br:=NR_ESP;
  1438. end;
  1439. {$endif OPTEA}
  1440. { wrong, for various reasons }
  1441. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1442. exit;
  1443. { base }
  1444. case br of
  1445. NR_EAX : base:=0;
  1446. NR_ECX : base:=1;
  1447. NR_EDX : base:=2;
  1448. NR_EBX : base:=3;
  1449. NR_ESP : base:=4;
  1450. NR_NO,
  1451. NR_EBP : base:=5;
  1452. NR_ESI : base:=6;
  1453. NR_EDI : base:=7;
  1454. else
  1455. exit;
  1456. end;
  1457. { index }
  1458. case ir of
  1459. NR_EAX : index:=0;
  1460. NR_ECX : index:=1;
  1461. NR_EDX : index:=2;
  1462. NR_EBX : index:=3;
  1463. NR_NO : index:=4;
  1464. NR_EBP : index:=5;
  1465. NR_ESI : index:=6;
  1466. NR_EDI : index:=7;
  1467. else
  1468. exit;
  1469. end;
  1470. case s of
  1471. 0,
  1472. 1 : scalefactor:=0;
  1473. 2 : scalefactor:=1;
  1474. 4 : scalefactor:=2;
  1475. 8 : scalefactor:=3;
  1476. else
  1477. exit;
  1478. end;
  1479. if (br=NR_NO) or
  1480. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1481. md:=0
  1482. else
  1483. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1484. md:=1
  1485. else
  1486. md:=2;
  1487. if (br=NR_NO) or (md=2) then
  1488. output.bytes:=4
  1489. else
  1490. output.bytes:=md;
  1491. { SIB needed ? }
  1492. if (ir=NR_NO) and (br<>NR_ESP) then
  1493. begin
  1494. output.sib_present:=false;
  1495. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1496. end
  1497. else
  1498. begin
  1499. output.sib_present:=true;
  1500. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1501. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1502. end;
  1503. end;
  1504. if output.sib_present then
  1505. output.size:=2+output.bytes
  1506. else
  1507. output.size:=1+output.bytes;
  1508. process_ea:=true;
  1509. end;
  1510. {$endif x86_64}
  1511. function taicpu.calcsize(p:PInsEntry):shortint;
  1512. var
  1513. codes : pchar;
  1514. c : byte;
  1515. len : shortint;
  1516. ea_data : ea;
  1517. begin
  1518. len:=0;
  1519. codes:=@p^.code[0];
  1520. {$ifdef x86_64}
  1521. rex:=0;
  1522. {$endif x86_64}
  1523. repeat
  1524. c:=ord(codes^);
  1525. inc(codes);
  1526. case c of
  1527. 0 :
  1528. break;
  1529. 1,2,3 :
  1530. begin
  1531. inc(codes,c);
  1532. inc(len,c);
  1533. end;
  1534. 8,9,10 :
  1535. begin
  1536. {$ifdef x86_64}
  1537. if ((getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1538. (getsupreg(oper[c-8]^.reg)>=RS_R8)) or
  1539. ((getregtype(oper[c-8]^.reg)=R_MMREGISTER) and
  1540. (getsupreg(oper[c-8]^.reg)>=RS_XMM8)) then
  1541. begin
  1542. if rex=0 then
  1543. inc(len);
  1544. rex:=rex or $41;
  1545. end
  1546. else if (getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1547. (getsubreg(oper[c-8]^.reg)=R_SUBL) and
  1548. (getsupreg(oper[c-8]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1549. begin
  1550. if rex=0 then
  1551. inc(len);
  1552. rex:=rex or $40;
  1553. end;
  1554. {$endif x86_64}
  1555. inc(codes);
  1556. inc(len);
  1557. end;
  1558. 11 :
  1559. begin
  1560. inc(codes);
  1561. inc(len);
  1562. end;
  1563. 4,5,6,7 :
  1564. begin
  1565. if opsize=S_W then
  1566. inc(len,2)
  1567. else
  1568. inc(len);
  1569. end;
  1570. 15,
  1571. 12,13,14,
  1572. 16,17,18,
  1573. 20,21,22,
  1574. 40,41,42 :
  1575. inc(len);
  1576. 24,25,26,
  1577. 31,
  1578. 48,49,50 :
  1579. inc(len,2);
  1580. 28,29,30:
  1581. begin
  1582. if opsize=S_Q then
  1583. inc(len,8)
  1584. else
  1585. inc(len,4);
  1586. end;
  1587. 32,33,34,
  1588. 52,53,54,
  1589. 56,57,58 :
  1590. inc(len,4);
  1591. 192,193,194 :
  1592. if NeedAddrPrefix(c-192) then
  1593. inc(len);
  1594. 208,209,210 :
  1595. begin
  1596. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1597. OT_BITS16:
  1598. inc(len);
  1599. {$ifdef x86_64}
  1600. OT_BITS64:
  1601. begin
  1602. if rex=0 then
  1603. inc(len);
  1604. rex:=rex or $48;
  1605. end;
  1606. {$endif x86_64}
  1607. end;
  1608. end;
  1609. 200,
  1610. 212 :
  1611. inc(len);
  1612. 214 :
  1613. begin
  1614. {$ifdef x86_64}
  1615. if rex=0 then
  1616. inc(len);
  1617. rex:=rex or $48;
  1618. {$endif x86_64}
  1619. end;
  1620. 201,
  1621. 202,
  1622. 211,
  1623. 213,
  1624. 215,
  1625. 217,218: ;
  1626. 219,220 :
  1627. inc(len);
  1628. 221:
  1629. {$ifdef x86_64}
  1630. { remove rex competely? }
  1631. if rex=$48 then
  1632. begin
  1633. rex:=0;
  1634. dec(len);
  1635. end
  1636. else
  1637. rex:=rex and $f7
  1638. {$endif x86_64}
  1639. ;
  1640. 64..191 :
  1641. begin
  1642. {$ifdef x86_64}
  1643. if (c<127) then
  1644. begin
  1645. if (oper[c and 7]^.typ=top_reg) then
  1646. begin
  1647. if ((getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1648. (getsupreg(oper[c and 7]^.reg)>=RS_R8)) or
  1649. ((getregtype(oper[c and 7]^.reg)=R_MMREGISTER) and
  1650. (getsupreg(oper[c and 7]^.reg)>=RS_XMM8)) then
  1651. begin
  1652. if rex=0 then
  1653. inc(len);
  1654. rex:=rex or $44;
  1655. end
  1656. else if (getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1657. (getsubreg(oper[c and 7]^.reg)=R_SUBL) and
  1658. (getsupreg(oper[c and 7]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1659. begin
  1660. if rex=0 then
  1661. inc(len);
  1662. rex:=rex or $40;
  1663. end;
  1664. end;
  1665. end;
  1666. {$endif x86_64}
  1667. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1668. Message(asmw_e_invalid_effective_address)
  1669. else
  1670. inc(len,ea_data.size);
  1671. {$ifdef x86_64}
  1672. { did we already create include a rex into the length calculation? }
  1673. if (rex<>0) and (ea_data.rex<>0) then
  1674. dec(len);
  1675. rex:=rex or ea_data.rex;
  1676. {$endif x86_64}
  1677. end;
  1678. else
  1679. InternalError(200603141);
  1680. end;
  1681. until false;
  1682. calcsize:=len;
  1683. end;
  1684. procedure taicpu.GenCode(objdata:TObjData);
  1685. {
  1686. * the actual codes (C syntax, i.e. octal):
  1687. * \0 - terminates the code. (Unless it's a literal of course.)
  1688. * \1, \2, \3 - that many literal bytes follow in the code stream
  1689. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1690. * (POP is never used for CS) depending on operand 0
  1691. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1692. * on operand 0
  1693. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1694. * to the register value of operand 0, 1 or 2
  1695. * \13 - a literal byte follows in the code stream, to be added
  1696. * to the condition code value of the instruction.
  1697. * \17 - encodes the literal byte 0. (Some compilers don't take
  1698. * kindly to a zero byte in the _middle_ of a compile time
  1699. * string constant, so I had to put this hack in.)
  1700. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1701. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1702. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1703. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1704. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1705. * assembly mode or the address-size override on the operand
  1706. * \37 - a word constant, from the _segment_ part of operand 0
  1707. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1708. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1709. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1710. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1711. * assembly mode or the address-size override on the operand
  1712. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1713. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1714. * field the register value of operand b.
  1715. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1716. * field equal to digit b.
  1717. * \300,\301,\302 - might be an 0x67, depending on the address size of
  1718. * the memory reference in operand x.
  1719. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1720. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1721. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1722. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1723. * size of operand x.
  1724. * \323 - insert x86_64 REX at this position.
  1725. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1726. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1727. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1728. * \327 - indicates that this instruction is only valid when the
  1729. * operand size is the default (instruction to disassembler,
  1730. * generates no code in the assembler)
  1731. * \331 - instruction not valid with REP prefix. Hint for
  1732. * disassembler only; for SSE instructions.
  1733. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  1734. * \333 - REP prefix (0xF3 byte); for SSE instructions. Not encoded
  1735. * \335 - removes rex size prefix, i.e. rex.w must be the last opcode
  1736. }
  1737. var
  1738. currval : aint;
  1739. currsym : tobjsymbol;
  1740. currrelreloc,
  1741. currabsreloc,
  1742. currabsreloc32 : TObjRelocationType;
  1743. {$ifdef x86_64}
  1744. rexwritten : boolean;
  1745. {$endif x86_64}
  1746. procedure getvalsym(opidx:longint);
  1747. begin
  1748. case oper[opidx]^.typ of
  1749. top_ref :
  1750. begin
  1751. currval:=oper[opidx]^.ref^.offset;
  1752. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  1753. {$ifdef x86_64}
  1754. if oper[opidx]^.ref^.refaddr=addr_pic then
  1755. begin
  1756. currrelreloc:=RELOC_PLT32;
  1757. currabsreloc:=RELOC_GOTPCREL;
  1758. currabsreloc32:=RELOC_GOTPCREL;
  1759. end
  1760. else
  1761. {$endif x86_64}
  1762. begin
  1763. currrelreloc:=RELOC_RELATIVE;
  1764. currabsreloc:=RELOC_ABSOLUTE;
  1765. currabsreloc32:=RELOC_ABSOLUTE32;
  1766. end;
  1767. end;
  1768. top_const :
  1769. begin
  1770. currval:=aint(oper[opidx]^.val);
  1771. currsym:=nil;
  1772. currabsreloc:=RELOC_ABSOLUTE;
  1773. currabsreloc32:=RELOC_ABSOLUTE32;
  1774. end;
  1775. else
  1776. Message(asmw_e_immediate_or_reference_expected);
  1777. end;
  1778. end;
  1779. {$ifdef x86_64}
  1780. procedure maybewriterex;
  1781. begin
  1782. if (rex<>0) and not(rexwritten) then
  1783. begin
  1784. rexwritten:=true;
  1785. objdata.writebytes(rex,1);
  1786. end;
  1787. end;
  1788. {$endif x86_64}
  1789. const
  1790. CondVal:array[TAsmCond] of byte=($0,
  1791. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1792. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1793. $0, $A, $A, $B, $8, $4);
  1794. var
  1795. c : byte;
  1796. pb : pbyte;
  1797. codes : pchar;
  1798. bytes : array[0..3] of byte;
  1799. rfield,
  1800. data,s,opidx : longint;
  1801. ea_data : ea;
  1802. begin
  1803. { safety check }
  1804. if objdata.currobjsec.size<>longword(insoffset) then
  1805. internalerror(200130121);
  1806. { load data to write }
  1807. codes:=insentry^.code;
  1808. {$ifdef x86_64}
  1809. rexwritten:=false;
  1810. {$endif x86_64}
  1811. { Force word push/pop for registers }
  1812. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1813. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1814. begin
  1815. bytes[0]:=$66;
  1816. objdata.writebytes(bytes,1);
  1817. end;
  1818. repeat
  1819. c:=ord(codes^);
  1820. inc(codes);
  1821. case c of
  1822. 0 :
  1823. break;
  1824. 1,2,3 :
  1825. begin
  1826. objdata.writebytes(codes^,c);
  1827. inc(codes,c);
  1828. end;
  1829. 4,6 :
  1830. begin
  1831. case oper[0]^.reg of
  1832. NR_CS:
  1833. bytes[0]:=$e;
  1834. NR_NO,
  1835. NR_DS:
  1836. bytes[0]:=$1e;
  1837. NR_ES:
  1838. bytes[0]:=$6;
  1839. NR_SS:
  1840. bytes[0]:=$16;
  1841. else
  1842. internalerror(777004);
  1843. end;
  1844. if c=4 then
  1845. inc(bytes[0]);
  1846. objdata.writebytes(bytes,1);
  1847. end;
  1848. 5,7 :
  1849. begin
  1850. case oper[0]^.reg of
  1851. NR_FS:
  1852. bytes[0]:=$a0;
  1853. NR_GS:
  1854. bytes[0]:=$a8;
  1855. else
  1856. internalerror(777005);
  1857. end;
  1858. if c=5 then
  1859. inc(bytes[0]);
  1860. objdata.writebytes(bytes,1);
  1861. end;
  1862. 8,9,10 :
  1863. begin
  1864. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1865. inc(codes);
  1866. objdata.writebytes(bytes,1);
  1867. end;
  1868. 11 :
  1869. begin
  1870. bytes[0]:=ord(codes^)+condval[condition];
  1871. inc(codes);
  1872. objdata.writebytes(bytes,1);
  1873. end;
  1874. 15 :
  1875. begin
  1876. bytes[0]:=0;
  1877. objdata.writebytes(bytes,1);
  1878. end;
  1879. 12,13,14 :
  1880. begin
  1881. getvalsym(c-12);
  1882. if (currval<-128) or (currval>127) then
  1883. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1884. if assigned(currsym) then
  1885. objdata.writereloc(currval,1,currsym,currabsreloc)
  1886. else
  1887. objdata.writebytes(currval,1);
  1888. end;
  1889. 16,17,18 :
  1890. begin
  1891. getvalsym(c-16);
  1892. if (currval<-256) or (currval>255) then
  1893. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1894. if assigned(currsym) then
  1895. objdata.writereloc(currval,1,currsym,currabsreloc)
  1896. else
  1897. objdata.writebytes(currval,1);
  1898. end;
  1899. 20,21,22 :
  1900. begin
  1901. getvalsym(c-20);
  1902. if (currval<0) or (currval>255) then
  1903. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1904. if assigned(currsym) then
  1905. objdata.writereloc(currval,1,currsym,currabsreloc)
  1906. else
  1907. objdata.writebytes(currval,1);
  1908. end;
  1909. 24,25,26 :
  1910. begin
  1911. getvalsym(c-24);
  1912. if (currval<-65536) or (currval>65535) then
  1913. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1914. if assigned(currsym) then
  1915. objdata.writereloc(currval,2,currsym,currabsreloc)
  1916. else
  1917. objdata.writebytes(currval,2);
  1918. end;
  1919. 28,29,30 :
  1920. begin
  1921. getvalsym(c-28);
  1922. if opsize=S_Q then
  1923. begin
  1924. if assigned(currsym) then
  1925. objdata.writereloc(currval,8,currsym,currabsreloc)
  1926. else
  1927. objdata.writebytes(currval,8);
  1928. end
  1929. else
  1930. begin
  1931. if assigned(currsym) then
  1932. objdata.writereloc(currval,4,currsym,currabsreloc32)
  1933. else
  1934. objdata.writebytes(currval,4);
  1935. end
  1936. end;
  1937. 32,33,34 :
  1938. begin
  1939. getvalsym(c-32);
  1940. if assigned(currsym) then
  1941. objdata.writereloc(currval,4,currsym,currabsreloc32)
  1942. else
  1943. objdata.writebytes(currval,4);
  1944. end;
  1945. 40,41,42 :
  1946. begin
  1947. getvalsym(c-40);
  1948. data:=currval-insend;
  1949. if assigned(currsym) then
  1950. inc(data,currsym.address);
  1951. if (data>127) or (data<-128) then
  1952. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1953. objdata.writebytes(data,1);
  1954. end;
  1955. 52,53,54 :
  1956. begin
  1957. getvalsym(c-52);
  1958. if assigned(currsym) then
  1959. objdata.writereloc(currval,4,currsym,currrelreloc)
  1960. else
  1961. objdata.writereloc(currval-insend,4,nil,currabsreloc32)
  1962. end;
  1963. 56,57,58 :
  1964. begin
  1965. getvalsym(c-56);
  1966. if assigned(currsym) then
  1967. objdata.writereloc(currval,4,currsym,currrelreloc)
  1968. else
  1969. objdata.writereloc(currval-insend,4,nil,currabsreloc32)
  1970. end;
  1971. 192,193,194 :
  1972. begin
  1973. if NeedAddrPrefix(c-192) then
  1974. begin
  1975. bytes[0]:=$67;
  1976. objdata.writebytes(bytes,1);
  1977. end;
  1978. end;
  1979. 200 :
  1980. begin
  1981. bytes[0]:=$67;
  1982. objdata.writebytes(bytes,1);
  1983. end;
  1984. 208,209,210 :
  1985. begin
  1986. case oper[c-208]^.ot and OT_SIZE_MASK of
  1987. OT_BITS16 :
  1988. begin
  1989. bytes[0]:=$66;
  1990. objdata.writebytes(bytes,1);
  1991. end;
  1992. {$ifndef x86_64}
  1993. OT_BITS64 :
  1994. Message(asmw_e_64bit_not_supported);
  1995. {$endif x86_64}
  1996. end;
  1997. {$ifdef x86_64}
  1998. maybewriterex;
  1999. {$endif x86_64}
  2000. end;
  2001. 211,
  2002. 213 :
  2003. begin
  2004. {$ifdef x86_64}
  2005. maybewriterex;
  2006. {$endif x86_64}
  2007. end;
  2008. 212 :
  2009. begin
  2010. bytes[0]:=$66;
  2011. objdata.writebytes(bytes,1);
  2012. {$ifdef x86_64}
  2013. maybewriterex;
  2014. {$endif x86_64}
  2015. end;
  2016. 214 :
  2017. begin
  2018. {$ifdef x86_64}
  2019. maybewriterex;
  2020. {$else x86_64}
  2021. Message(asmw_e_64bit_not_supported);
  2022. {$endif x86_64}
  2023. end;
  2024. 219 :
  2025. begin
  2026. bytes[0]:=$f3;
  2027. objdata.writebytes(bytes,1);
  2028. {$ifdef x86_64}
  2029. maybewriterex;
  2030. {$endif x86_64}
  2031. end;
  2032. 220 :
  2033. begin
  2034. bytes[0]:=$f2;
  2035. objdata.writebytes(bytes,1);
  2036. end;
  2037. 221:
  2038. ;
  2039. 201,
  2040. 202,
  2041. 215,
  2042. 217,218 :
  2043. begin
  2044. { these are dissambler hints or 32 bit prefixes which
  2045. are not needed
  2046. It's usefull to write rex :) (FK) }
  2047. {$ifdef x86_64}
  2048. maybewriterex;
  2049. {$endif x86_64}
  2050. end;
  2051. 31,
  2052. 48,49,50 :
  2053. begin
  2054. InternalError(777006);
  2055. end
  2056. else
  2057. begin
  2058. { rex should be written at this point }
  2059. {$ifdef x86_64}
  2060. if (rex<>0) and not(rexwritten) then
  2061. internalerror(200603191);
  2062. {$endif x86_64}
  2063. if (c>=64) and (c<=191) then
  2064. begin
  2065. if (c<127) then
  2066. begin
  2067. if (oper[c and 7]^.typ=top_reg) then
  2068. rfield:=regval(oper[c and 7]^.reg)
  2069. else
  2070. rfield:=regval(oper[c and 7]^.ref^.base);
  2071. end
  2072. else
  2073. rfield:=c and 7;
  2074. opidx:=(c shr 3) and 7;
  2075. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2076. Message(asmw_e_invalid_effective_address);
  2077. pb:=@bytes[0];
  2078. pb^:=ea_data.modrm;
  2079. inc(pb);
  2080. if ea_data.sib_present then
  2081. begin
  2082. pb^:=ea_data.sib;
  2083. inc(pb);
  2084. end;
  2085. s:=pb-@bytes[0];
  2086. objdata.writebytes(bytes,s);
  2087. case ea_data.bytes of
  2088. 0 : ;
  2089. 1 :
  2090. begin
  2091. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2092. begin
  2093. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2094. {$ifdef x86_64}
  2095. if oper[opidx]^.ref^.refaddr=addr_pic then
  2096. currabsreloc:=RELOC_GOTPCREL
  2097. else
  2098. {$endif x86_64}
  2099. currabsreloc:=RELOC_ABSOLUTE;
  2100. objdata.writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2101. end
  2102. else
  2103. begin
  2104. bytes[0]:=oper[opidx]^.ref^.offset;
  2105. objdata.writebytes(bytes,1);
  2106. end;
  2107. inc(s);
  2108. end;
  2109. 2,4 :
  2110. begin
  2111. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2112. currval:=oper[opidx]^.ref^.offset;
  2113. {$ifdef x86_64}
  2114. if oper[opidx]^.ref^.refaddr=addr_pic then
  2115. currabsreloc:=RELOC_GOTPCREL
  2116. else
  2117. if oper[opidx]^.ref^.base=NR_RIP then
  2118. begin
  2119. currabsreloc:=RELOC_RELATIVE;
  2120. { Adjust reloc value depending of immediate operand size }
  2121. case Ord(codes^) of
  2122. 12,13,14,16,17,18,20,21,22:
  2123. Dec(currval, 1);
  2124. 24,25,26:
  2125. Dec(currval, 2);
  2126. 32,33,34:
  2127. Dec(currval, 4);
  2128. end;
  2129. end
  2130. else
  2131. {$endif x86_64}
  2132. currabsreloc:=RELOC_ABSOLUTE32;
  2133. objdata.writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  2134. inc(s,ea_data.bytes);
  2135. end;
  2136. end;
  2137. end
  2138. else
  2139. InternalError(777007);
  2140. end;
  2141. end;
  2142. until false;
  2143. end;
  2144. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2145. begin
  2146. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2147. (regtype = R_INTREGISTER) and
  2148. (ops=2) and
  2149. (oper[0]^.typ=top_reg) and
  2150. (oper[1]^.typ=top_reg) and
  2151. (oper[0]^.reg=oper[1]^.reg)
  2152. ) or
  2153. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2154. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD)) and
  2155. (regtype = R_MMREGISTER) and
  2156. (ops=2) and
  2157. (oper[0]^.typ=top_reg) and
  2158. (oper[1]^.typ=top_reg) and
  2159. (oper[0]^.reg=oper[1]^.reg)
  2160. );
  2161. end;
  2162. procedure build_spilling_operation_type_table;
  2163. var
  2164. opcode : tasmop;
  2165. i : integer;
  2166. begin
  2167. new(operation_type_table);
  2168. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2169. for opcode:=low(tasmop) to high(tasmop) do
  2170. begin
  2171. for i:=1 to MaxInsChanges do
  2172. begin
  2173. case InsProp[opcode].Ch[i] of
  2174. Ch_Rop1 :
  2175. operation_type_table^[opcode,0]:=operand_read;
  2176. Ch_Wop1 :
  2177. operation_type_table^[opcode,0]:=operand_write;
  2178. Ch_RWop1,
  2179. Ch_Mop1 :
  2180. operation_type_table^[opcode,0]:=operand_readwrite;
  2181. Ch_Rop2 :
  2182. operation_type_table^[opcode,1]:=operand_read;
  2183. Ch_Wop2 :
  2184. operation_type_table^[opcode,1]:=operand_write;
  2185. Ch_RWop2,
  2186. Ch_Mop2 :
  2187. operation_type_table^[opcode,1]:=operand_readwrite;
  2188. Ch_Rop3 :
  2189. operation_type_table^[opcode,2]:=operand_read;
  2190. Ch_Wop3 :
  2191. operation_type_table^[opcode,2]:=operand_write;
  2192. Ch_RWop3,
  2193. Ch_Mop3 :
  2194. operation_type_table^[opcode,2]:=operand_readwrite;
  2195. end;
  2196. end;
  2197. end;
  2198. { Special cases that can't be decoded from the InsChanges flags }
  2199. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2200. end;
  2201. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2202. begin
  2203. { the information in the instruction table is made for the string copy
  2204. operation MOVSD so hack here (FK)
  2205. }
  2206. if (opcode=A_MOVSD) and (ops=2) then
  2207. begin
  2208. case opnr of
  2209. 0:
  2210. result:=operand_read;
  2211. 1:
  2212. result:=operand_write;
  2213. else
  2214. internalerror(200506055);
  2215. end
  2216. end
  2217. else
  2218. result:=operation_type_table^[opcode,opnr];
  2219. end;
  2220. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2221. begin
  2222. case getregtype(r) of
  2223. R_INTREGISTER :
  2224. { we don't need special code here for 32 bit loads on x86_64, since
  2225. those will automatically zero-extend the upper 32 bits. }
  2226. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  2227. R_MMREGISTER :
  2228. case getsubreg(r) of
  2229. R_SUBMMD:
  2230. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2231. R_SUBMMS:
  2232. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2233. R_SUBMMWHOLE:
  2234. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
  2235. else
  2236. internalerror(200506043);
  2237. end;
  2238. else
  2239. internalerror(200401041);
  2240. end;
  2241. end;
  2242. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2243. var
  2244. size: topsize;
  2245. begin
  2246. case getregtype(r) of
  2247. R_INTREGISTER :
  2248. begin
  2249. size:=reg2opsize(r);
  2250. {$ifdef x86_64}
  2251. { even if it's a 32 bit reg, we still have to spill 64 bits
  2252. because we often perform 64 bit operations on them }
  2253. if (size=S_L) then
  2254. begin
  2255. size:=S_Q;
  2256. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  2257. end;
  2258. {$endif x86_64}
  2259. result:=taicpu.op_reg_ref(A_MOV,size,r,ref);
  2260. end;
  2261. R_MMREGISTER :
  2262. case getsubreg(r) of
  2263. R_SUBMMD:
  2264. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2265. R_SUBMMS:
  2266. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2267. R_SUBMMWHOLE:
  2268. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
  2269. else
  2270. internalerror(200506042);
  2271. end;
  2272. else
  2273. internalerror(200401041);
  2274. end;
  2275. end;
  2276. {*****************************************************************************
  2277. Instruction table
  2278. *****************************************************************************}
  2279. procedure BuildInsTabCache;
  2280. var
  2281. i : longint;
  2282. begin
  2283. new(instabcache);
  2284. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2285. i:=0;
  2286. while (i<InsTabEntries) do
  2287. begin
  2288. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2289. InsTabCache^[InsTab[i].OPcode]:=i;
  2290. inc(i);
  2291. end;
  2292. end;
  2293. procedure InitAsm;
  2294. begin
  2295. build_spilling_operation_type_table;
  2296. if not assigned(instabcache) then
  2297. BuildInsTabCache;
  2298. end;
  2299. procedure DoneAsm;
  2300. begin
  2301. if assigned(operation_type_table) then
  2302. begin
  2303. dispose(operation_type_table);
  2304. operation_type_table:=nil;
  2305. end;
  2306. if assigned(instabcache) then
  2307. begin
  2308. dispose(instabcache);
  2309. instabcache:=nil;
  2310. end;
  2311. end;
  2312. begin
  2313. cai_align:=tai_align;
  2314. cai_cpu:=taicpu;
  2315. end.