aasmcpu.pas 82 KB

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