aasmcpu.pas 86 KB

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