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