aasmcpu.pas 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473
  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_BITS128 = $10000000; { 16 byte SSE }
  43. OT_BITS256 = $20000000; { 32 byte AVX }
  44. OT_BITS80 = $00000010; { FPU only }
  45. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  46. OT_NEAR = $00000040;
  47. OT_SHORT = $00000080;
  48. { TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
  49. but this requires adjusting the opcode table }
  50. OT_SIZE_MASK = $3000001F; { all the size attributes }
  51. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  52. { Bits 8..11: modifiers }
  53. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  54. OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
  55. OT_COLON = $00000400; { operand is followed by a colon }
  56. OT_MODIFIER_MASK = $00000F00;
  57. { Bits 12..15: type of operand }
  58. OT_REGISTER = $00001000;
  59. OT_IMMEDIATE = $00002000;
  60. OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
  61. OT_REGMEM = $00008000; { for r/m, ie EA, operands }
  62. OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
  63. OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
  64. { Bits 20..22, 24..26: register classes
  65. otf_* consts are not used alone, only to build other constants. }
  66. otf_reg_cdt = $00100000;
  67. otf_reg_gpr = $00200000;
  68. otf_reg_sreg = $00400000;
  69. otf_reg_fpu = $01000000;
  70. otf_reg_mmx = $02000000;
  71. otf_reg_xmm = $04000000;
  72. otf_reg_ymm = $08000000;
  73. { Bits 16..19: subclasses, meaning depends on classes field }
  74. otf_sub0 = $00010000;
  75. otf_sub1 = $00020000;
  76. otf_sub2 = $00040000;
  77. otf_sub3 = $00080000;
  78. OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
  79. OT_REG_TYPMASK = otf_reg_cdt or otf_reg_gpr or otf_reg_sreg or otf_reg_fpu or otf_reg_mmx or otf_reg_xmm or otf_reg_ymm;
  80. { register class 0: CRx, DRx and TRx }
  81. {$ifdef x86_64}
  82. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
  83. {$else x86_64}
  84. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
  85. {$endif x86_64}
  86. OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
  87. OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
  88. OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
  89. OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
  90. { register class 1: general-purpose registers }
  91. OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
  92. OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
  93. OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
  94. OT_REG16 = OT_REG_GPR or OT_BITS16;
  95. OT_REG32 = OT_REG_GPR or OT_BITS32;
  96. OT_REG64 = OT_REG_GPR or OT_BITS64;
  97. { GPR subclass 0: accumulator: AL, AX, EAX or RAX }
  98. OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
  99. OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
  100. OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
  101. OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
  102. {$ifdef x86_64}
  103. OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
  104. {$endif x86_64}
  105. { GPR subclass 1: counter: CL, CX, ECX or RCX }
  106. OT_REG_COUNT = OT_REG_GPR or otf_sub1;
  107. OT_REG_CL = OT_REG_COUNT or OT_BITS8;
  108. OT_REG_CX = OT_REG_COUNT or OT_BITS16;
  109. OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
  110. {$ifdef x86_64}
  111. OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
  112. {$endif x86_64}
  113. { GPR subclass 2: data register: DL, DX, EDX or RDX }
  114. OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
  115. OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
  116. { register class 2: Segment registers }
  117. OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
  118. OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
  119. OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
  120. OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
  121. { register class 3: FPU registers }
  122. OT_FPUREG = OT_REGISTER or otf_reg_fpu;
  123. OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
  124. { register class 4: MMX (both reg and r/m) }
  125. OT_MMXREG = OT_REGNORM or otf_reg_mmx;
  126. OT_MMXRM = OT_REGMEM or otf_reg_mmx;
  127. { register class 5: XMM (both reg and r/m) }
  128. OT_XMMREG = OT_REGNORM or otf_reg_xmm;
  129. OT_XMMRM = OT_REGMEM or otf_reg_xmm;
  130. { register class 5: XMM (both reg and r/m) }
  131. OT_YMMREG = OT_REGNORM or otf_reg_ymm;
  132. OT_YMMRM = OT_REGMEM or otf_reg_ymm;
  133. { Memory operands }
  134. OT_MEM8 = OT_MEMORY or OT_BITS8;
  135. OT_MEM16 = OT_MEMORY or OT_BITS16;
  136. OT_MEM32 = OT_MEMORY or OT_BITS32;
  137. OT_MEM64 = OT_MEMORY or OT_BITS64;
  138. OT_MEM128 = OT_MEMORY or OT_BITS128;
  139. OT_MEM256 = OT_MEMORY or OT_BITS256;
  140. OT_MEM80 = OT_MEMORY or OT_BITS80;
  141. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  142. { simple [address] offset }
  143. { Matches any type of r/m operand }
  144. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM;
  145. { Immediate operands }
  146. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  147. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  148. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  149. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  150. OT_ONENESS = otf_sub0; { special type of immediate operand }
  151. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  152. { Size of the instruction table converted by nasmconv.pas }
  153. {$if defined(x86_64)}
  154. instabentries = {$i x8664nop.inc}
  155. {$elseif defined(i386)}
  156. instabentries = {$i i386nop.inc}
  157. {$elseif defined(i8086)}
  158. instabentries = {$i i8086nop.inc}
  159. {$endif}
  160. maxinfolen = 8;
  161. MaxInsChanges = 3; { Max things a instruction can change }
  162. type
  163. { What an instruction can change. Needed for optimizer and spilling code.
  164. Note: The order of this enumeration is should not be changed! }
  165. TInsChange = (Ch_None,
  166. {Read from a register}
  167. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  168. {write from a register}
  169. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  170. {read and write from/to a register}
  171. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  172. {modify the contents of a register with the purpose of using
  173. this changed content afterwards (add/sub/..., but e.g. not rep
  174. or movsd)}
  175. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  176. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  177. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  178. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  179. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  180. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  181. Ch_WMemEDI,
  182. Ch_All,
  183. { x86_64 registers }
  184. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  185. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  186. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  187. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  188. );
  189. TInsProp = packed record
  190. Ch : Array[1..MaxInsChanges] of TInsChange;
  191. end;
  192. TMemRefSizeInfo = (msiUnkown, msiUnsupported, msiNoSize,
  193. msiMultiple, msiMultiple8, msiMultiple16, msiMultiple32,
  194. msiMultiple64, msiMultiple128, msiMultiple256,
  195. msiMemRegSize, msiMemRegx64y128, msiMemRegx64y256,
  196. msiMem8, msiMem16, msiMem32, msiMem64, msiMem128, msiMem256);
  197. TConstSizeInfo = (csiUnkown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
  198. TInsTabMemRefSizeInfoRec = record
  199. MemRefSize : TMemRefSizeInfo;
  200. ExistsSSEAVX: boolean;
  201. ConstSize : TConstSizeInfo;
  202. end;
  203. const
  204. MemRefMultiples: set of TMemRefSizeInfo = [msiMultiple, msiMultiple8,
  205. msiMultiple16, msiMultiple32,
  206. msiMultiple64, msiMultiple128,
  207. msiMultiple256];
  208. InsProp : array[tasmop] of TInsProp =
  209. {$if defined(x86_64)}
  210. {$i x8664pro.inc}
  211. {$elseif defined(i386)}
  212. {$i i386prop.inc}
  213. {$elseif defined(i8086)}
  214. {$i i8086prop.inc}
  215. {$endif}
  216. type
  217. TOperandOrder = (op_intel,op_att);
  218. tinsentry=packed record
  219. opcode : tasmop;
  220. ops : byte;
  221. optypes : array[0..max_operands-1] of longint;
  222. code : array[0..maxinfolen] of char;
  223. flags : int64;
  224. end;
  225. pinsentry=^tinsentry;
  226. { alignment for operator }
  227. tai_align = class(tai_align_abstract)
  228. reg : tregister;
  229. constructor create(b:byte);override;
  230. constructor create_op(b: byte; _op: byte);override;
  231. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  232. end;
  233. taicpu = class(tai_cpu_abstract_sym)
  234. opsize : topsize;
  235. constructor op_none(op : tasmop);
  236. constructor op_none(op : tasmop;_size : topsize);
  237. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  238. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  239. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  240. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  241. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  242. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  243. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  244. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  245. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  246. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  247. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  248. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  249. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  250. constructor op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  251. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  252. { this is for Jmp instructions }
  253. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  254. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  255. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  256. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  257. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  258. procedure changeopsize(siz:topsize);
  259. function GetString:string;
  260. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  261. Early versions of the UnixWare assembler had a bug where some fpu instructions
  262. were reversed and GAS still keeps this "feature" for compatibility.
  263. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  264. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  265. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  266. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  267. when generating output for other assemblers, the opcodes must be fixed before writing them.
  268. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  269. because in case of smartlinking assembler is generated twice so at the second run wrong
  270. assembler is generated.
  271. }
  272. function FixNonCommutativeOpcodes: tasmop;
  273. private
  274. FOperandOrder : TOperandOrder;
  275. procedure init(_size : topsize); { this need to be called by all constructor }
  276. public
  277. { the next will reset all instructions that can change in pass 2 }
  278. procedure ResetPass1;override;
  279. procedure ResetPass2;override;
  280. function CheckIfValid:boolean;
  281. function Pass1(objdata:TObjData):longint;override;
  282. procedure Pass2(objdata:TObjData);override;
  283. procedure SetOperandOrder(order:TOperandOrder);
  284. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  285. { register spilling code }
  286. function spilling_get_operation_type(opnr: longint): topertype;override;
  287. private
  288. { next fields are filled in pass1, so pass2 is faster }
  289. insentry : PInsEntry;
  290. insoffset : longint;
  291. LastInsOffset : longint; { need to be public to be reset }
  292. inssize : shortint;
  293. {$ifdef x86_64}
  294. rex : byte;
  295. {$endif x86_64}
  296. function InsEnd:longint;
  297. procedure create_ot(objdata:TObjData);
  298. function Matches(p:PInsEntry):boolean;
  299. function calcsize(p:PInsEntry):shortint;
  300. procedure gencode(objdata:TObjData);
  301. function NeedAddrPrefix(opidx:byte):boolean;
  302. procedure Swapoperands;
  303. function FindInsentry(objdata:TObjData):boolean;
  304. end;
  305. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  306. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  307. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  308. procedure InitAsm;
  309. procedure DoneAsm;
  310. implementation
  311. uses
  312. cutils,
  313. globals,
  314. systems,
  315. procinfo,
  316. itcpugas,
  317. symsym,
  318. cpuinfo;
  319. {*****************************************************************************
  320. Instruction table
  321. *****************************************************************************}
  322. const
  323. {Instruction flags }
  324. IF_NONE = $00000000;
  325. IF_SM = $00000001; { size match first two operands }
  326. IF_SM2 = $00000002;
  327. IF_SB = $00000004; { unsized operands can't be non-byte }
  328. IF_SW = $00000008; { unsized operands can't be non-word }
  329. IF_SD = $00000010; { unsized operands can't be nondword }
  330. IF_SMASK = $0000001f;
  331. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  332. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  333. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  334. IF_ARMASK = $00000060; { mask for unsized argument spec }
  335. IF_ARSHIFT = 5; { LSB of IF_ARMASK }
  336. IF_PRIV = $00000100; { it's a privileged instruction }
  337. IF_SMM = $00000200; { it's only valid in SMM }
  338. IF_PROT = $00000400; { it's protected mode only }
  339. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  340. IF_UNDOC = $00001000; { it's an undocumented instruction }
  341. IF_FPU = $00002000; { it's an FPU instruction }
  342. IF_MMX = $00004000; { it's an MMX instruction }
  343. { it's a 3DNow! instruction }
  344. IF_3DNOW = $00008000;
  345. { it's a SSE (KNI, MMX2) instruction }
  346. IF_SSE = $00010000;
  347. { SSE2 instructions }
  348. IF_SSE2 = $00020000;
  349. { SSE3 instructions }
  350. IF_SSE3 = $00040000;
  351. { SSE64 instructions }
  352. IF_SSE64 = $00080000;
  353. { the mask for processor types }
  354. {IF_PMASK = longint($FF000000);}
  355. { the mask for disassembly "prefer" }
  356. {IF_PFMASK = longint($F001FF00);}
  357. { SVM instructions }
  358. IF_SVM = $00100000;
  359. { SSE4 instructions }
  360. IF_SSE4 = $00200000;
  361. { TODO: These flags were added to make x86ins.dat more readable.
  362. Values must be reassigned to make any other use of them. }
  363. IF_SSSE3 = $00200000;
  364. IF_SSE41 = $00200000;
  365. IF_SSE42 = $00200000;
  366. IF_AVX = $00200000;
  367. IF_BMI1 = $00200000;
  368. IF_BMI2 = $00200000;
  369. IF_16BITONLY = $00200000;
  370. IF_PLEVEL = $0F000000; { mask for processor level }
  371. IF_8086 = $00000000; { 8086 instruction }
  372. IF_186 = $01000000; { 186+ instruction }
  373. IF_286 = $02000000; { 286+ instruction }
  374. IF_386 = $03000000; { 386+ instruction }
  375. IF_486 = $04000000; { 486+ instruction }
  376. IF_PENT = $05000000; { Pentium instruction }
  377. IF_P6 = $06000000; { P6 instruction }
  378. IF_KATMAI = $07000000; { Katmai instructions }
  379. IF_WILLAMETTE = $08000000; { Willamette instructions }
  380. IF_PRESCOTT = $09000000; { Prescott instructions }
  381. IF_X86_64 = $0a000000;
  382. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  383. IF_AMD = $0c000000; { AMD-specific instruction }
  384. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  385. IF_SANDYBRIDGE = $0e000000; { Sandybridge-specific instruction }
  386. IF_NEC = $0f000000; { NEC V20/V30 instruction }
  387. { added flags }
  388. IF_PRE = $40000000; { it's a prefix instruction }
  389. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  390. type
  391. TInsTabCache=array[TasmOp] of longint;
  392. PInsTabCache=^TInsTabCache;
  393. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  394. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  395. const
  396. {$if defined(x86_64)}
  397. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  398. {$elseif defined(i386)}
  399. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  400. {$elseif defined(i8086)}
  401. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  402. {$endif}
  403. var
  404. InsTabCache : PInsTabCache;
  405. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  406. const
  407. {$if defined(x86_64)}
  408. { Intel style operands ! }
  409. opsize_2_type:array[0..2,topsize] of longint=(
  410. (OT_NONE,
  411. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  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_BITS128,
  418. OT_BITS256
  419. ),
  420. (OT_NONE,
  421. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  422. OT_BITS16,OT_BITS32,OT_BITS64,
  423. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  424. OT_BITS64,
  425. OT_NEAR,OT_FAR,OT_SHORT,
  426. OT_NONE,
  427. OT_BITS128,
  428. OT_BITS256
  429. ),
  430. (OT_NONE,
  431. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  432. OT_BITS16,OT_BITS32,OT_BITS64,
  433. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  434. OT_BITS64,
  435. OT_NEAR,OT_FAR,OT_SHORT,
  436. OT_NONE,
  437. OT_BITS128,
  438. OT_BITS256
  439. )
  440. );
  441. reg_ot_table : array[tregisterindex] of longint = (
  442. {$i r8664ot.inc}
  443. );
  444. {$elseif defined(i386)}
  445. { Intel style operands ! }
  446. opsize_2_type:array[0..2,topsize] of longint=(
  447. (OT_NONE,
  448. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  449. OT_BITS16,OT_BITS32,OT_BITS64,
  450. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  451. OT_BITS64,
  452. OT_NEAR,OT_FAR,OT_SHORT,
  453. OT_NONE,
  454. OT_BITS128,
  455. OT_BITS256
  456. ),
  457. (OT_NONE,
  458. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  459. OT_BITS16,OT_BITS32,OT_BITS64,
  460. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  461. OT_BITS64,
  462. OT_NEAR,OT_FAR,OT_SHORT,
  463. OT_NONE,
  464. OT_BITS128,
  465. OT_BITS256
  466. ),
  467. (OT_NONE,
  468. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  469. OT_BITS16,OT_BITS32,OT_BITS64,
  470. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  471. OT_BITS64,
  472. OT_NEAR,OT_FAR,OT_SHORT,
  473. OT_NONE,
  474. OT_BITS128,
  475. OT_BITS256
  476. )
  477. );
  478. reg_ot_table : array[tregisterindex] of longint = (
  479. {$i r386ot.inc}
  480. );
  481. {$elseif defined(i8086)}
  482. { Intel style operands ! }
  483. opsize_2_type:array[0..2,topsize] of longint=(
  484. (OT_NONE,
  485. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  486. OT_BITS16,OT_BITS32,OT_BITS64,
  487. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  488. OT_BITS64,
  489. OT_NEAR,OT_FAR,OT_SHORT,
  490. OT_NONE,
  491. OT_BITS128,
  492. OT_BITS256
  493. ),
  494. (OT_NONE,
  495. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  496. OT_BITS16,OT_BITS32,OT_BITS64,
  497. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  498. OT_BITS64,
  499. OT_NEAR,OT_FAR,OT_SHORT,
  500. OT_NONE,
  501. OT_BITS128,
  502. OT_BITS256
  503. ),
  504. (OT_NONE,
  505. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  506. OT_BITS16,OT_BITS32,OT_BITS64,
  507. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  508. OT_BITS64,
  509. OT_NEAR,OT_FAR,OT_SHORT,
  510. OT_NONE,
  511. OT_BITS128,
  512. OT_BITS256
  513. )
  514. );
  515. reg_ot_table : array[tregisterindex] of longint = (
  516. {$i r8086ot.inc}
  517. );
  518. {$endif}
  519. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  520. begin
  521. result := InsTabMemRefSizeInfoCache^[aAsmop];
  522. end;
  523. { Operation type for spilling code }
  524. type
  525. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  526. var
  527. operation_type_table : ^toperation_type_table;
  528. {****************************************************************************
  529. TAI_ALIGN
  530. ****************************************************************************}
  531. constructor tai_align.create(b: byte);
  532. begin
  533. inherited create(b);
  534. reg:=NR_ECX;
  535. end;
  536. constructor tai_align.create_op(b: byte; _op: byte);
  537. begin
  538. inherited create_op(b,_op);
  539. reg:=NR_NO;
  540. end;
  541. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  542. const
  543. {$ifdef x86_64}
  544. alignarray:array[0..3] of string[4]=(
  545. #$66#$66#$66#$90,
  546. #$66#$66#$90,
  547. #$66#$90,
  548. #$90
  549. );
  550. {$else x86_64}
  551. alignarray:array[0..5] of string[8]=(
  552. #$8D#$B4#$26#$00#$00#$00#$00,
  553. #$8D#$B6#$00#$00#$00#$00,
  554. #$8D#$74#$26#$00,
  555. #$8D#$76#$00,
  556. #$89#$F6,
  557. #$90);
  558. {$endif x86_64}
  559. var
  560. bufptr : pchar;
  561. j : longint;
  562. localsize: byte;
  563. begin
  564. inherited calculatefillbuf(buf,executable);
  565. if not(use_op) and executable then
  566. begin
  567. bufptr:=pchar(@buf);
  568. { fillsize may still be used afterwards, so don't modify }
  569. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  570. localsize:=fillsize;
  571. while (localsize>0) do
  572. begin
  573. for j:=low(alignarray) to high(alignarray) do
  574. if (localsize>=length(alignarray[j])) then
  575. break;
  576. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  577. inc(bufptr,length(alignarray[j]));
  578. dec(localsize,length(alignarray[j]));
  579. end;
  580. end;
  581. calculatefillbuf:=pchar(@buf);
  582. end;
  583. {*****************************************************************************
  584. Taicpu Constructors
  585. *****************************************************************************}
  586. procedure taicpu.changeopsize(siz:topsize);
  587. begin
  588. opsize:=siz;
  589. end;
  590. procedure taicpu.init(_size : topsize);
  591. begin
  592. { default order is att }
  593. FOperandOrder:=op_att;
  594. segprefix:=NR_NO;
  595. opsize:=_size;
  596. insentry:=nil;
  597. LastInsOffset:=-1;
  598. InsOffset:=0;
  599. InsSize:=0;
  600. end;
  601. constructor taicpu.op_none(op : tasmop);
  602. begin
  603. inherited create(op);
  604. init(S_NO);
  605. end;
  606. constructor taicpu.op_none(op : tasmop;_size : topsize);
  607. begin
  608. inherited create(op);
  609. init(_size);
  610. end;
  611. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  612. begin
  613. inherited create(op);
  614. init(_size);
  615. ops:=1;
  616. loadreg(0,_op1);
  617. end;
  618. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  619. begin
  620. inherited create(op);
  621. init(_size);
  622. ops:=1;
  623. loadconst(0,_op1);
  624. end;
  625. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  626. begin
  627. inherited create(op);
  628. init(_size);
  629. ops:=1;
  630. loadref(0,_op1);
  631. end;
  632. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  633. begin
  634. inherited create(op);
  635. init(_size);
  636. ops:=2;
  637. loadreg(0,_op1);
  638. loadreg(1,_op2);
  639. end;
  640. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  641. begin
  642. inherited create(op);
  643. init(_size);
  644. ops:=2;
  645. loadreg(0,_op1);
  646. loadconst(1,_op2);
  647. end;
  648. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  649. begin
  650. inherited create(op);
  651. init(_size);
  652. ops:=2;
  653. loadreg(0,_op1);
  654. loadref(1,_op2);
  655. end;
  656. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  657. begin
  658. inherited create(op);
  659. init(_size);
  660. ops:=2;
  661. loadconst(0,_op1);
  662. loadreg(1,_op2);
  663. end;
  664. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  665. begin
  666. inherited create(op);
  667. init(_size);
  668. ops:=2;
  669. loadconst(0,_op1);
  670. loadconst(1,_op2);
  671. end;
  672. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  673. begin
  674. inherited create(op);
  675. init(_size);
  676. ops:=2;
  677. loadconst(0,_op1);
  678. loadref(1,_op2);
  679. end;
  680. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  681. begin
  682. inherited create(op);
  683. init(_size);
  684. ops:=2;
  685. loadref(0,_op1);
  686. loadreg(1,_op2);
  687. end;
  688. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  689. begin
  690. inherited create(op);
  691. init(_size);
  692. ops:=3;
  693. loadreg(0,_op1);
  694. loadreg(1,_op2);
  695. loadreg(2,_op3);
  696. end;
  697. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  698. begin
  699. inherited create(op);
  700. init(_size);
  701. ops:=3;
  702. loadconst(0,_op1);
  703. loadreg(1,_op2);
  704. loadreg(2,_op3);
  705. end;
  706. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  707. begin
  708. inherited create(op);
  709. init(_size);
  710. ops:=3;
  711. loadref(0,_op1);
  712. loadreg(1,_op2);
  713. loadreg(2,_op3);
  714. end;
  715. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  716. begin
  717. inherited create(op);
  718. init(_size);
  719. ops:=3;
  720. loadconst(0,_op1);
  721. loadref(1,_op2);
  722. loadreg(2,_op3);
  723. end;
  724. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  725. begin
  726. inherited create(op);
  727. init(_size);
  728. ops:=3;
  729. loadconst(0,_op1);
  730. loadreg(1,_op2);
  731. loadref(2,_op3);
  732. end;
  733. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  734. begin
  735. inherited create(op);
  736. init(_size);
  737. condition:=cond;
  738. ops:=1;
  739. loadsymbol(0,_op1,0);
  740. end;
  741. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  742. begin
  743. inherited create(op);
  744. init(_size);
  745. ops:=1;
  746. loadsymbol(0,_op1,0);
  747. end;
  748. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  749. begin
  750. inherited create(op);
  751. init(_size);
  752. ops:=1;
  753. loadsymbol(0,_op1,_op1ofs);
  754. end;
  755. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  756. begin
  757. inherited create(op);
  758. init(_size);
  759. ops:=2;
  760. loadsymbol(0,_op1,_op1ofs);
  761. loadreg(1,_op2);
  762. end;
  763. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  764. begin
  765. inherited create(op);
  766. init(_size);
  767. ops:=2;
  768. loadsymbol(0,_op1,_op1ofs);
  769. loadref(1,_op2);
  770. end;
  771. function taicpu.GetString:string;
  772. var
  773. i : longint;
  774. s : string;
  775. addsize : boolean;
  776. begin
  777. s:='['+std_op2str[opcode];
  778. for i:=0 to ops-1 do
  779. begin
  780. with oper[i]^ do
  781. begin
  782. if i=0 then
  783. s:=s+' '
  784. else
  785. s:=s+',';
  786. { type }
  787. addsize:=false;
  788. if (ot and OT_XMMREG)=OT_XMMREG then
  789. s:=s+'xmmreg'
  790. else
  791. if (ot and OT_YMMREG)=OT_YMMREG then
  792. s:=s+'ymmreg'
  793. else
  794. if (ot and OT_MMXREG)=OT_MMXREG then
  795. s:=s+'mmxreg'
  796. else
  797. if (ot and OT_FPUREG)=OT_FPUREG then
  798. s:=s+'fpureg'
  799. else
  800. if (ot and OT_REGISTER)=OT_REGISTER then
  801. begin
  802. s:=s+'reg';
  803. addsize:=true;
  804. end
  805. else
  806. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  807. begin
  808. s:=s+'imm';
  809. addsize:=true;
  810. end
  811. else
  812. if (ot and OT_MEMORY)=OT_MEMORY then
  813. begin
  814. s:=s+'mem';
  815. addsize:=true;
  816. end
  817. else
  818. s:=s+'???';
  819. { size }
  820. if addsize then
  821. begin
  822. if (ot and OT_BITS8)<>0 then
  823. s:=s+'8'
  824. else
  825. if (ot and OT_BITS16)<>0 then
  826. s:=s+'16'
  827. else
  828. if (ot and OT_BITS32)<>0 then
  829. s:=s+'32'
  830. else
  831. if (ot and OT_BITS64)<>0 then
  832. s:=s+'64'
  833. else
  834. if (ot and OT_BITS128)<>0 then
  835. s:=s+'128'
  836. else
  837. if (ot and OT_BITS256)<>0 then
  838. s:=s+'256'
  839. else
  840. s:=s+'??';
  841. { signed }
  842. if (ot and OT_SIGNED)<>0 then
  843. s:=s+'s';
  844. end;
  845. end;
  846. end;
  847. GetString:=s+']';
  848. end;
  849. procedure taicpu.Swapoperands;
  850. var
  851. p : POper;
  852. begin
  853. { Fix the operands which are in AT&T style and we need them in Intel style }
  854. case ops of
  855. 0,1:
  856. ;
  857. 2 : begin
  858. { 0,1 -> 1,0 }
  859. p:=oper[0];
  860. oper[0]:=oper[1];
  861. oper[1]:=p;
  862. end;
  863. 3 : begin
  864. { 0,1,2 -> 2,1,0 }
  865. p:=oper[0];
  866. oper[0]:=oper[2];
  867. oper[2]:=p;
  868. end;
  869. 4 : begin
  870. { 0,1,2,3 -> 3,2,1,0 }
  871. p:=oper[0];
  872. oper[0]:=oper[3];
  873. oper[3]:=p;
  874. p:=oper[1];
  875. oper[1]:=oper[2];
  876. oper[2]:=p;
  877. end;
  878. else
  879. internalerror(201108141);
  880. end;
  881. end;
  882. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  883. begin
  884. if FOperandOrder<>order then
  885. begin
  886. Swapoperands;
  887. FOperandOrder:=order;
  888. end;
  889. end;
  890. function taicpu.FixNonCommutativeOpcodes: tasmop;
  891. begin
  892. result:=opcode;
  893. { we need ATT order }
  894. SetOperandOrder(op_att);
  895. if (
  896. (ops=2) and
  897. (oper[0]^.typ=top_reg) and
  898. (oper[1]^.typ=top_reg) and
  899. { if the first is ST and the second is also a register
  900. it is necessarily ST1 .. ST7 }
  901. ((oper[0]^.reg=NR_ST) or
  902. (oper[0]^.reg=NR_ST0))
  903. ) or
  904. { ((ops=1) and
  905. (oper[0]^.typ=top_reg) and
  906. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  907. (ops=0) then
  908. begin
  909. if opcode=A_FSUBR then
  910. result:=A_FSUB
  911. else if opcode=A_FSUB then
  912. result:=A_FSUBR
  913. else if opcode=A_FDIVR then
  914. result:=A_FDIV
  915. else if opcode=A_FDIV then
  916. result:=A_FDIVR
  917. else if opcode=A_FSUBRP then
  918. result:=A_FSUBP
  919. else if opcode=A_FSUBP then
  920. result:=A_FSUBRP
  921. else if opcode=A_FDIVRP then
  922. result:=A_FDIVP
  923. else if opcode=A_FDIVP then
  924. result:=A_FDIVRP;
  925. end;
  926. if (
  927. (ops=1) and
  928. (oper[0]^.typ=top_reg) and
  929. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  930. (oper[0]^.reg<>NR_ST)
  931. ) then
  932. begin
  933. if opcode=A_FSUBRP then
  934. result:=A_FSUBP
  935. else if opcode=A_FSUBP then
  936. result:=A_FSUBRP
  937. else if opcode=A_FDIVRP then
  938. result:=A_FDIVP
  939. else if opcode=A_FDIVP then
  940. result:=A_FDIVRP;
  941. end;
  942. end;
  943. {*****************************************************************************
  944. Assembler
  945. *****************************************************************************}
  946. type
  947. ea = packed record
  948. sib_present : boolean;
  949. bytes : byte;
  950. size : byte;
  951. modrm : byte;
  952. sib : byte;
  953. {$ifdef x86_64}
  954. rex : byte;
  955. {$endif x86_64}
  956. end;
  957. procedure taicpu.create_ot(objdata:TObjData);
  958. {
  959. this function will also fix some other fields which only needs to be once
  960. }
  961. var
  962. i,l,relsize : longint;
  963. currsym : TObjSymbol;
  964. begin
  965. if ops=0 then
  966. exit;
  967. { update oper[].ot field }
  968. for i:=0 to ops-1 do
  969. with oper[i]^ do
  970. begin
  971. case typ of
  972. top_reg :
  973. begin
  974. ot:=reg_ot_table[findreg_by_number(reg)];
  975. end;
  976. top_ref :
  977. begin
  978. if (ref^.refaddr=addr_no)
  979. {$ifdef i386}
  980. or (
  981. (ref^.refaddr in [addr_pic]) and
  982. { allow any base for assembler blocks }
  983. ((assigned(current_procinfo) and
  984. (pi_has_assembler_block in current_procinfo.flags) and
  985. (ref^.base<>NR_NO)) or (ref^.base=NR_EBX))
  986. )
  987. {$endif i386}
  988. {$ifdef x86_64}
  989. or (
  990. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  991. (ref^.base<>NR_NO)
  992. )
  993. {$endif x86_64}
  994. then
  995. begin
  996. { create ot field }
  997. if (ot and OT_SIZE_MASK)=0 then
  998. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  999. else
  1000. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  1001. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1002. ot:=ot or OT_MEM_OFFS;
  1003. { fix scalefactor }
  1004. if (ref^.index=NR_NO) then
  1005. ref^.scalefactor:=0
  1006. else
  1007. if (ref^.scalefactor=0) then
  1008. ref^.scalefactor:=1;
  1009. end
  1010. else
  1011. begin
  1012. { Jumps use a relative offset which can be 8bit,
  1013. for other opcodes we always need to generate the full
  1014. 32bit address }
  1015. if assigned(objdata) and
  1016. is_jmp then
  1017. begin
  1018. currsym:=objdata.symbolref(ref^.symbol);
  1019. l:=ref^.offset;
  1020. {$push}
  1021. {$r-}
  1022. if assigned(currsym) then
  1023. inc(l,currsym.address);
  1024. {$pop}
  1025. { when it is a forward jump we need to compensate the
  1026. offset of the instruction since the previous time,
  1027. because the symbol address is then still using the
  1028. 'old-style' addressing.
  1029. For backwards jumps this is not required because the
  1030. address of the symbol is already adjusted to the
  1031. new offset }
  1032. if (l>InsOffset) and (LastInsOffset<>-1) then
  1033. inc(l,InsOffset-LastInsOffset);
  1034. { instruction size will then always become 2 (PFV) }
  1035. relsize:=(InsOffset+2)-l;
  1036. if (relsize>=-128) and (relsize<=127) and
  1037. (
  1038. not assigned(currsym) or
  1039. (currsym.objsection=objdata.currobjsec)
  1040. ) then
  1041. ot:=OT_IMM8 or OT_SHORT
  1042. else
  1043. ot:=OT_IMM32 or OT_NEAR;
  1044. end
  1045. else
  1046. ot:=OT_IMM32 or OT_NEAR;
  1047. end;
  1048. end;
  1049. top_local :
  1050. begin
  1051. if (ot and OT_SIZE_MASK)=0 then
  1052. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1053. else
  1054. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1055. end;
  1056. top_const :
  1057. begin
  1058. // if opcode is a SSE or AVX-instruction then we need a
  1059. // special handling (opsize can different from const-size)
  1060. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1061. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1062. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
  1063. begin
  1064. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1065. csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
  1066. csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
  1067. csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
  1068. csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
  1069. csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
  1070. end;
  1071. end
  1072. else
  1073. begin
  1074. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1075. { further, allow AAD and AAM with imm. operand }
  1076. if (opsize=S_NO) and not((i in [1,2,3]) or ((i=0) and (opcode in [A_AAD,A_AAM]))) then
  1077. message(asmr_e_invalid_opcode_and_operand);
  1078. if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
  1079. ot:=OT_IMM8 or OT_SIGNED
  1080. else
  1081. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1082. if (val=1) and (i=1) then
  1083. ot := ot or OT_ONENESS;
  1084. end;
  1085. end;
  1086. top_none :
  1087. begin
  1088. { generated when there was an error in the
  1089. assembler reader. It never happends when generating
  1090. assembler }
  1091. end;
  1092. else
  1093. internalerror(200402261);
  1094. end;
  1095. end;
  1096. end;
  1097. function taicpu.InsEnd:longint;
  1098. begin
  1099. InsEnd:=InsOffset+InsSize;
  1100. end;
  1101. function taicpu.Matches(p:PInsEntry):boolean;
  1102. { * IF_SM stands for Size Match: any operand whose size is not
  1103. * explicitly specified by the template is `really' intended to be
  1104. * the same size as the first size-specified operand.
  1105. * Non-specification is tolerated in the input instruction, but
  1106. * _wrong_ specification is not.
  1107. *
  1108. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1109. * three-operand instructions such as SHLD: it implies that the
  1110. * first two operands must match in size, but that the third is
  1111. * required to be _unspecified_.
  1112. *
  1113. * IF_SB invokes Size Byte: operands with unspecified size in the
  1114. * template are really bytes, and so no non-byte specification in
  1115. * the input instruction will be tolerated. IF_SW similarly invokes
  1116. * Size Word, and IF_SD invokes Size Doubleword.
  1117. *
  1118. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1119. * that any operand with unspecified size in the template is
  1120. * required to have unspecified size in the instruction too...)
  1121. }
  1122. var
  1123. insot,
  1124. currot,
  1125. i,j,asize,oprs : longint;
  1126. insflags:cardinal;
  1127. siz : array[0..max_operands-1] of longint;
  1128. begin
  1129. result:=false;
  1130. { Check the opcode and operands }
  1131. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1132. exit;
  1133. for i:=0 to p^.ops-1 do
  1134. begin
  1135. insot:=p^.optypes[i];
  1136. currot:=oper[i]^.ot;
  1137. { Check the operand flags }
  1138. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1139. exit;
  1140. { Check if the passed operand size matches with one of
  1141. the supported operand sizes }
  1142. if ((insot and OT_SIZE_MASK)<>0) and
  1143. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1144. exit;
  1145. end;
  1146. { Check operand sizes }
  1147. insflags:=p^.flags;
  1148. if insflags and IF_SMASK<>0 then
  1149. begin
  1150. { as default an untyped size can get all the sizes, this is different
  1151. from nasm, but else we need to do a lot checking which opcodes want
  1152. size or not with the automatic size generation }
  1153. asize:=-1;
  1154. if (insflags and IF_SB)<>0 then
  1155. asize:=OT_BITS8
  1156. else if (insflags and IF_SW)<>0 then
  1157. asize:=OT_BITS16
  1158. else if (insflags and IF_SD)<>0 then
  1159. asize:=OT_BITS32;
  1160. if (insflags and IF_ARMASK)<>0 then
  1161. begin
  1162. siz[0]:=-1;
  1163. siz[1]:=-1;
  1164. siz[2]:=-1;
  1165. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1166. end
  1167. else
  1168. begin
  1169. siz[0]:=asize;
  1170. siz[1]:=asize;
  1171. siz[2]:=asize;
  1172. end;
  1173. if (insflags and (IF_SM or IF_SM2))<>0 then
  1174. begin
  1175. if (insflags and IF_SM2)<>0 then
  1176. oprs:=2
  1177. else
  1178. oprs:=p^.ops;
  1179. for i:=0 to oprs-1 do
  1180. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1181. begin
  1182. for j:=0 to oprs-1 do
  1183. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1184. break;
  1185. end;
  1186. end
  1187. else
  1188. oprs:=2;
  1189. { Check operand sizes }
  1190. for i:=0 to p^.ops-1 do
  1191. begin
  1192. insot:=p^.optypes[i];
  1193. currot:=oper[i]^.ot;
  1194. if ((insot and OT_SIZE_MASK)=0) and
  1195. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1196. { Immediates can always include smaller size }
  1197. ((currot and OT_IMMEDIATE)=0) and
  1198. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1199. exit;
  1200. end;
  1201. end;
  1202. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1203. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1204. begin
  1205. for i:=0 to p^.ops-1 do
  1206. begin
  1207. insot:=p^.optypes[i];
  1208. if ((insot and OT_XMMRM) = OT_XMMRM) OR
  1209. ((insot and OT_YMMRM) = OT_YMMRM) then
  1210. begin
  1211. if (insot and OT_SIZE_MASK) = 0 then
  1212. begin
  1213. case insot and (OT_XMMRM or OT_YMMRM) of
  1214. OT_XMMRM: insot := insot or OT_BITS128;
  1215. OT_YMMRM: insot := insot or OT_BITS256;
  1216. end;
  1217. end;
  1218. end;
  1219. currot:=oper[i]^.ot;
  1220. { Check the operand flags }
  1221. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1222. exit;
  1223. { Check if the passed operand size matches with one of
  1224. the supported operand sizes }
  1225. if ((insot and OT_SIZE_MASK)<>0) and
  1226. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1227. exit;
  1228. end;
  1229. end;
  1230. result:=true;
  1231. end;
  1232. procedure taicpu.ResetPass1;
  1233. begin
  1234. { we need to reset everything here, because the choosen insentry
  1235. can be invalid for a new situation where the previously optimized
  1236. insentry is not correct }
  1237. InsEntry:=nil;
  1238. InsSize:=0;
  1239. LastInsOffset:=-1;
  1240. end;
  1241. procedure taicpu.ResetPass2;
  1242. begin
  1243. { we are here in a second pass, check if the instruction can be optimized }
  1244. if assigned(InsEntry) and
  1245. ((InsEntry^.flags and IF_PASS2)<>0) then
  1246. begin
  1247. InsEntry:=nil;
  1248. InsSize:=0;
  1249. end;
  1250. LastInsOffset:=-1;
  1251. end;
  1252. function taicpu.CheckIfValid:boolean;
  1253. begin
  1254. result:=FindInsEntry(nil);
  1255. end;
  1256. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1257. var
  1258. i : longint;
  1259. begin
  1260. result:=false;
  1261. { Things which may only be done once, not when a second pass is done to
  1262. optimize }
  1263. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1264. begin
  1265. current_filepos:=fileinfo;
  1266. { We need intel style operands }
  1267. SetOperandOrder(op_intel);
  1268. { create the .ot fields }
  1269. create_ot(objdata);
  1270. { set the file postion }
  1271. end
  1272. else
  1273. begin
  1274. { we've already an insentry so it's valid }
  1275. result:=true;
  1276. exit;
  1277. end;
  1278. { Lookup opcode in the table }
  1279. InsSize:=-1;
  1280. i:=instabcache^[opcode];
  1281. if i=-1 then
  1282. begin
  1283. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1284. exit;
  1285. end;
  1286. insentry:=@instab[i];
  1287. while (insentry^.opcode=opcode) do
  1288. begin
  1289. if matches(insentry) then
  1290. begin
  1291. result:=true;
  1292. exit;
  1293. end;
  1294. inc(insentry);
  1295. end;
  1296. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1297. { No instruction found, set insentry to nil and inssize to -1 }
  1298. insentry:=nil;
  1299. inssize:=-1;
  1300. end;
  1301. function taicpu.Pass1(objdata:TObjData):longint;
  1302. begin
  1303. Pass1:=0;
  1304. { Save the old offset and set the new offset }
  1305. InsOffset:=ObjData.CurrObjSec.Size;
  1306. { Error? }
  1307. if (Insentry=nil) and (InsSize=-1) then
  1308. exit;
  1309. { set the file postion }
  1310. current_filepos:=fileinfo;
  1311. { Get InsEntry }
  1312. if FindInsEntry(ObjData) then
  1313. begin
  1314. { Calculate instruction size }
  1315. InsSize:=calcsize(insentry);
  1316. if segprefix<>NR_NO then
  1317. inc(InsSize);
  1318. { Fix opsize if size if forced }
  1319. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1320. begin
  1321. if (insentry^.flags and IF_ARMASK)=0 then
  1322. begin
  1323. if (insentry^.flags and IF_SB)<>0 then
  1324. begin
  1325. if opsize=S_NO then
  1326. opsize:=S_B;
  1327. end
  1328. else if (insentry^.flags and IF_SW)<>0 then
  1329. begin
  1330. if opsize=S_NO then
  1331. opsize:=S_W;
  1332. end
  1333. else if (insentry^.flags and IF_SD)<>0 then
  1334. begin
  1335. if opsize=S_NO then
  1336. opsize:=S_L;
  1337. end;
  1338. end;
  1339. end;
  1340. LastInsOffset:=InsOffset;
  1341. Pass1:=InsSize;
  1342. exit;
  1343. end;
  1344. LastInsOffset:=-1;
  1345. end;
  1346. const
  1347. segprefixes: array[NR_ES..NR_GS] of Byte=(
  1348. // es cs ss ds fs gs
  1349. $26, $2E, $36, $3E, $64, $65
  1350. );
  1351. procedure taicpu.Pass2(objdata:TObjData);
  1352. begin
  1353. { error in pass1 ? }
  1354. if insentry=nil then
  1355. exit;
  1356. current_filepos:=fileinfo;
  1357. { Segment override }
  1358. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  1359. begin
  1360. objdata.writebytes(segprefixes[segprefix],1);
  1361. { fix the offset for GenNode }
  1362. inc(InsOffset);
  1363. end
  1364. else if segprefix<>NR_NO then
  1365. InternalError(201001071);
  1366. { Generate the instruction }
  1367. GenCode(objdata);
  1368. end;
  1369. function taicpu.needaddrprefix(opidx:byte):boolean;
  1370. begin
  1371. result:=(oper[opidx]^.typ=top_ref) and
  1372. (oper[opidx]^.ref^.refaddr=addr_no) and
  1373. {$ifdef x86_64}
  1374. (oper[opidx]^.ref^.base<>NR_RIP) and
  1375. {$endif x86_64}
  1376. (
  1377. (
  1378. (oper[opidx]^.ref^.index<>NR_NO) and
  1379. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1380. ) or
  1381. (
  1382. (oper[opidx]^.ref^.base<>NR_NO) and
  1383. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1384. )
  1385. );
  1386. end;
  1387. procedure badreg(r:Tregister);
  1388. begin
  1389. Message1(asmw_e_invalid_register,generic_regname(r));
  1390. end;
  1391. function regval(r:Tregister):byte;
  1392. const
  1393. intsupreg2opcode: array[0..7] of byte=
  1394. // ax cx dx bx si di bp sp -- in x86reg.dat
  1395. // ax cx dx bx sp bp si di -- needed order
  1396. (0, 1, 2, 3, 6, 7, 5, 4);
  1397. maxsupreg: array[tregistertype] of tsuperregister=
  1398. {$ifdef x86_64}
  1399. (0, 16, 9, 8, 16, 32, 0);
  1400. {$else x86_64}
  1401. (0, 8, 9, 8, 8, 32, 0);
  1402. {$endif x86_64}
  1403. var
  1404. rs: tsuperregister;
  1405. rt: tregistertype;
  1406. begin
  1407. rs:=getsupreg(r);
  1408. rt:=getregtype(r);
  1409. if (rs>=maxsupreg[rt]) then
  1410. badreg(r);
  1411. result:=rs and 7;
  1412. if (rt=R_INTREGISTER) then
  1413. begin
  1414. if (rs<8) then
  1415. result:=intsupreg2opcode[rs];
  1416. if getsubreg(r)=R_SUBH then
  1417. inc(result,4);
  1418. end;
  1419. end;
  1420. {$ifdef x86_64}
  1421. function rexbits(r: tregister): byte;
  1422. begin
  1423. result:=0;
  1424. case getregtype(r) of
  1425. R_INTREGISTER:
  1426. if (getsupreg(r)>=RS_R8) then
  1427. { Either B,X or R bits can be set, depending on register role in instruction.
  1428. Set all three bits here, caller will discard unnecessary ones. }
  1429. result:=result or $47
  1430. else if (getsubreg(r)=R_SUBL) and
  1431. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1432. result:=result or $40
  1433. else if (getsubreg(r)=R_SUBH) then
  1434. { Not an actual REX bit, used to detect incompatible usage of
  1435. AH/BH/CH/DH }
  1436. result:=result or $80;
  1437. R_MMREGISTER:
  1438. if getsupreg(r)>=RS_XMM8 then
  1439. result:=result or $47;
  1440. end;
  1441. end;
  1442. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1443. var
  1444. sym : tasmsymbol;
  1445. md,s,rv : byte;
  1446. base,index,scalefactor,
  1447. o : longint;
  1448. ir,br : Tregister;
  1449. isub,bsub : tsubregister;
  1450. begin
  1451. process_ea:=false;
  1452. fillchar(output,sizeof(output),0);
  1453. {Register ?}
  1454. if (input.typ=top_reg) then
  1455. begin
  1456. rv:=regval(input.reg);
  1457. output.modrm:=$c0 or (rfield shl 3) or rv;
  1458. output.size:=1;
  1459. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  1460. process_ea:=true;
  1461. exit;
  1462. end;
  1463. {No register, so memory reference.}
  1464. if input.typ<>top_ref then
  1465. internalerror(200409263);
  1466. ir:=input.ref^.index;
  1467. br:=input.ref^.base;
  1468. isub:=getsubreg(ir);
  1469. bsub:=getsubreg(br);
  1470. s:=input.ref^.scalefactor;
  1471. o:=input.ref^.offset;
  1472. sym:=input.ref^.symbol;
  1473. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1474. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1475. internalerror(200301081);
  1476. { it's direct address }
  1477. if (br=NR_NO) and (ir=NR_NO) then
  1478. begin
  1479. output.sib_present:=true;
  1480. output.bytes:=4;
  1481. output.modrm:=4 or (rfield shl 3);
  1482. output.sib:=$25;
  1483. end
  1484. else if (br=NR_RIP) and (ir=NR_NO) then
  1485. begin
  1486. { rip based }
  1487. output.sib_present:=false;
  1488. output.bytes:=4;
  1489. output.modrm:=5 or (rfield shl 3);
  1490. end
  1491. else
  1492. { it's an indirection }
  1493. begin
  1494. { 16 bit? }
  1495. if ((ir<>NR_NO) and (isub<>R_SUBADDR) and (isub<>R_SUBD)) or
  1496. ((br<>NR_NO) and (bsub<>R_SUBADDR) and (bsub<>R_SUBD)) then
  1497. message(asmw_e_16bit_32bit_not_supported);
  1498. { wrong, for various reasons }
  1499. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1500. exit;
  1501. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1502. process_ea:=true;
  1503. { base }
  1504. case br of
  1505. NR_R8D,
  1506. NR_EAX,
  1507. NR_R8,
  1508. NR_RAX : base:=0;
  1509. NR_R9D,
  1510. NR_ECX,
  1511. NR_R9,
  1512. NR_RCX : base:=1;
  1513. NR_R10D,
  1514. NR_EDX,
  1515. NR_R10,
  1516. NR_RDX : base:=2;
  1517. NR_R11D,
  1518. NR_EBX,
  1519. NR_R11,
  1520. NR_RBX : base:=3;
  1521. NR_R12D,
  1522. NR_ESP,
  1523. NR_R12,
  1524. NR_RSP : base:=4;
  1525. NR_R13D,
  1526. NR_EBP,
  1527. NR_R13,
  1528. NR_NO,
  1529. NR_RBP : base:=5;
  1530. NR_R14D,
  1531. NR_ESI,
  1532. NR_R14,
  1533. NR_RSI : base:=6;
  1534. NR_R15D,
  1535. NR_EDI,
  1536. NR_R15,
  1537. NR_RDI : base:=7;
  1538. else
  1539. exit;
  1540. end;
  1541. { index }
  1542. case ir of
  1543. NR_R8D,
  1544. NR_EAX,
  1545. NR_R8,
  1546. NR_RAX : index:=0;
  1547. NR_R9D,
  1548. NR_ECX,
  1549. NR_R9,
  1550. NR_RCX : index:=1;
  1551. NR_R10D,
  1552. NR_EDX,
  1553. NR_R10,
  1554. NR_RDX : index:=2;
  1555. NR_R11D,
  1556. NR_EBX,
  1557. NR_R11,
  1558. NR_RBX : index:=3;
  1559. NR_R12D,
  1560. NR_ESP,
  1561. NR_R12,
  1562. NR_NO : index:=4;
  1563. NR_R13D,
  1564. NR_EBP,
  1565. NR_R13,
  1566. NR_RBP : index:=5;
  1567. NR_R14D,
  1568. NR_ESI,
  1569. NR_R14,
  1570. NR_RSI : index:=6;
  1571. NR_R15D,
  1572. NR_EDI,
  1573. NR_R15,
  1574. NR_RDI : index:=7;
  1575. else
  1576. exit;
  1577. end;
  1578. case s of
  1579. 0,
  1580. 1 : scalefactor:=0;
  1581. 2 : scalefactor:=1;
  1582. 4 : scalefactor:=2;
  1583. 8 : scalefactor:=3;
  1584. else
  1585. exit;
  1586. end;
  1587. { If rbp or r13 is used we must always include an offset }
  1588. if (br=NR_NO) or
  1589. ((br<>NR_RBP) and (br<>NR_R13) and (br<>NR_EBP) and (br<>NR_R13D) and (o=0) and (sym=nil)) then
  1590. md:=0
  1591. else
  1592. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1593. md:=1
  1594. else
  1595. md:=2;
  1596. if (br=NR_NO) or (md=2) then
  1597. output.bytes:=4
  1598. else
  1599. output.bytes:=md;
  1600. { SIB needed ? }
  1601. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) and (br<>NR_ESP) and (br<>NR_R12D) then
  1602. begin
  1603. output.sib_present:=false;
  1604. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1605. end
  1606. else
  1607. begin
  1608. output.sib_present:=true;
  1609. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1610. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1611. end;
  1612. end;
  1613. output.size:=1+ord(output.sib_present)+output.bytes;
  1614. process_ea:=true;
  1615. end;
  1616. {$else x86_64}
  1617. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1618. var
  1619. sym : tasmsymbol;
  1620. md,s,rv : byte;
  1621. base,index,scalefactor,
  1622. o : longint;
  1623. ir,br : Tregister;
  1624. isub,bsub : tsubregister;
  1625. begin
  1626. process_ea:=false;
  1627. fillchar(output,sizeof(output),0);
  1628. {Register ?}
  1629. if (input.typ=top_reg) then
  1630. begin
  1631. rv:=regval(input.reg);
  1632. output.modrm:=$c0 or (rfield shl 3) or rv;
  1633. output.size:=1;
  1634. process_ea:=true;
  1635. exit;
  1636. end;
  1637. {No register, so memory reference.}
  1638. if (input.typ<>top_ref) then
  1639. internalerror(200409262);
  1640. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1641. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1642. internalerror(200301081);
  1643. ir:=input.ref^.index;
  1644. br:=input.ref^.base;
  1645. isub:=getsubreg(ir);
  1646. bsub:=getsubreg(br);
  1647. s:=input.ref^.scalefactor;
  1648. o:=input.ref^.offset;
  1649. sym:=input.ref^.symbol;
  1650. { it's direct address }
  1651. if (br=NR_NO) and (ir=NR_NO) then
  1652. begin
  1653. { it's a pure offset }
  1654. output.sib_present:=false;
  1655. output.bytes:=4;
  1656. output.modrm:=5 or (rfield shl 3);
  1657. end
  1658. else
  1659. { it's an indirection }
  1660. begin
  1661. { 16 bit address? }
  1662. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1663. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1664. message(asmw_e_16bit_not_supported);
  1665. {$ifdef OPTEA}
  1666. { make single reg base }
  1667. if (br=NR_NO) and (s=1) then
  1668. begin
  1669. br:=ir;
  1670. ir:=NR_NO;
  1671. end;
  1672. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1673. if (br=NR_NO) and
  1674. (((s=2) and (ir<>NR_ESP)) or
  1675. (s=3) or (s=5) or (s=9)) then
  1676. begin
  1677. br:=ir;
  1678. dec(s);
  1679. end;
  1680. { swap ESP into base if scalefactor is 1 }
  1681. if (s=1) and (ir=NR_ESP) then
  1682. begin
  1683. ir:=br;
  1684. br:=NR_ESP;
  1685. end;
  1686. {$endif OPTEA}
  1687. { wrong, for various reasons }
  1688. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1689. exit;
  1690. { base }
  1691. case br of
  1692. NR_EAX : base:=0;
  1693. NR_ECX : base:=1;
  1694. NR_EDX : base:=2;
  1695. NR_EBX : base:=3;
  1696. NR_ESP : base:=4;
  1697. NR_NO,
  1698. NR_EBP : base:=5;
  1699. NR_ESI : base:=6;
  1700. NR_EDI : base:=7;
  1701. else
  1702. exit;
  1703. end;
  1704. { index }
  1705. case ir of
  1706. NR_EAX : index:=0;
  1707. NR_ECX : index:=1;
  1708. NR_EDX : index:=2;
  1709. NR_EBX : index:=3;
  1710. NR_NO : index:=4;
  1711. NR_EBP : index:=5;
  1712. NR_ESI : index:=6;
  1713. NR_EDI : index:=7;
  1714. else
  1715. exit;
  1716. end;
  1717. case s of
  1718. 0,
  1719. 1 : scalefactor:=0;
  1720. 2 : scalefactor:=1;
  1721. 4 : scalefactor:=2;
  1722. 8 : scalefactor:=3;
  1723. else
  1724. exit;
  1725. end;
  1726. if (br=NR_NO) or
  1727. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1728. md:=0
  1729. else
  1730. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1731. md:=1
  1732. else
  1733. md:=2;
  1734. if (br=NR_NO) or (md=2) then
  1735. output.bytes:=4
  1736. else
  1737. output.bytes:=md;
  1738. { SIB needed ? }
  1739. if (ir=NR_NO) and (br<>NR_ESP) then
  1740. begin
  1741. output.sib_present:=false;
  1742. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1743. end
  1744. else
  1745. begin
  1746. output.sib_present:=true;
  1747. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1748. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1749. end;
  1750. end;
  1751. if output.sib_present then
  1752. output.size:=2+output.bytes
  1753. else
  1754. output.size:=1+output.bytes;
  1755. process_ea:=true;
  1756. end;
  1757. {$endif x86_64}
  1758. function taicpu.calcsize(p:PInsEntry):shortint;
  1759. var
  1760. codes : pchar;
  1761. c : byte;
  1762. len : shortint;
  1763. ea_data : ea;
  1764. exists_vex: boolean;
  1765. exists_vex_extention: boolean;
  1766. exists_prefix_66: boolean;
  1767. exists_prefix_F2: boolean;
  1768. exists_prefix_F3: boolean;
  1769. {$ifdef x86_64}
  1770. omit_rexw : boolean;
  1771. {$endif x86_64}
  1772. begin
  1773. len:=0;
  1774. codes:=@p^.code[0];
  1775. exists_vex := false;
  1776. exists_vex_extention := false;
  1777. exists_prefix_66 := false;
  1778. exists_prefix_F2 := false;
  1779. exists_prefix_F3 := false;
  1780. {$ifdef x86_64}
  1781. rex:=0;
  1782. omit_rexw:=false;
  1783. {$endif x86_64}
  1784. repeat
  1785. c:=ord(codes^);
  1786. inc(codes);
  1787. case c of
  1788. 0 :
  1789. break;
  1790. 1,2,3 :
  1791. begin
  1792. inc(codes,c);
  1793. inc(len,c);
  1794. end;
  1795. 8,9,10 :
  1796. begin
  1797. {$ifdef x86_64}
  1798. rex:=rex or (rexbits(oper[c-8]^.reg) and $F1);
  1799. {$endif x86_64}
  1800. inc(codes);
  1801. inc(len);
  1802. end;
  1803. 11 :
  1804. begin
  1805. inc(codes);
  1806. inc(len);
  1807. end;
  1808. 4,5,6,7 :
  1809. begin
  1810. if opsize=S_W then
  1811. inc(len,2)
  1812. else
  1813. inc(len);
  1814. end;
  1815. 12,13,14,
  1816. 16,17,18,
  1817. 20,21,22,23,
  1818. 40,41,42 :
  1819. inc(len);
  1820. 24,25,26,
  1821. 31,
  1822. 48,49,50 :
  1823. inc(len,2);
  1824. 28,29,30:
  1825. begin
  1826. if opsize=S_Q then
  1827. inc(len,8)
  1828. else
  1829. inc(len,4);
  1830. end;
  1831. 36,37,38:
  1832. inc(len,sizeof(pint));
  1833. 44,45,46:
  1834. inc(len,8);
  1835. 32,33,34,
  1836. 52,53,54,
  1837. 56,57,58,
  1838. 172,173,174 :
  1839. inc(len,4);
  1840. 60,61,62,63: ; // ignore vex-coded operand-idx
  1841. 208,209,210 :
  1842. begin
  1843. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1844. OT_BITS16:
  1845. inc(len);
  1846. {$ifdef x86_64}
  1847. OT_BITS64:
  1848. begin
  1849. rex:=rex or $48;
  1850. end;
  1851. {$endif x86_64}
  1852. end;
  1853. end;
  1854. 200 :
  1855. {$ifndef x86_64}
  1856. inc(len);
  1857. {$else x86_64}
  1858. { every insentry with code 0310 must be marked with NOX86_64 }
  1859. InternalError(2011051301);
  1860. {$endif x86_64}
  1861. 201 :
  1862. {$ifdef x86_64}
  1863. inc(len)
  1864. {$endif x86_64}
  1865. ;
  1866. 212 :
  1867. inc(len);
  1868. 214 :
  1869. begin
  1870. {$ifdef x86_64}
  1871. rex:=rex or $48;
  1872. {$endif x86_64}
  1873. end;
  1874. 202,
  1875. 211,
  1876. 213,
  1877. 215,
  1878. 217,218: ;
  1879. 219:
  1880. begin
  1881. inc(len);
  1882. exists_prefix_F2 := true;
  1883. end;
  1884. 220:
  1885. begin
  1886. inc(len);
  1887. exists_prefix_F3 := true;
  1888. end;
  1889. 241:
  1890. begin
  1891. inc(len);
  1892. exists_prefix_66 := true;
  1893. end;
  1894. 221:
  1895. {$ifdef x86_64}
  1896. omit_rexw:=true
  1897. {$endif x86_64}
  1898. ;
  1899. 64..151 :
  1900. begin
  1901. {$ifdef x86_64}
  1902. if (c<127) then
  1903. begin
  1904. if (oper[c and 7]^.typ=top_reg) then
  1905. begin
  1906. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  1907. end;
  1908. end;
  1909. {$endif x86_64}
  1910. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1911. Message(asmw_e_invalid_effective_address)
  1912. else
  1913. inc(len,ea_data.size);
  1914. {$ifdef x86_64}
  1915. rex:=rex or ea_data.rex;
  1916. {$endif x86_64}
  1917. end;
  1918. 242: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  1919. // =>> DEFAULT = 2 Bytes
  1920. begin
  1921. if not(exists_vex) then
  1922. begin
  1923. inc(len, 2);
  1924. exists_vex := true;
  1925. end;
  1926. end;
  1927. 243: // REX.W = 1
  1928. // =>> VEX prefix length = 3
  1929. begin
  1930. if not(exists_vex_extention) then
  1931. begin
  1932. inc(len);
  1933. exists_vex_extention := true;
  1934. end;
  1935. end;
  1936. 244: ; // VEX length bit
  1937. 247: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  1938. 248: // VEX-Extention prefix $0F
  1939. // ignore for calculating length
  1940. ;
  1941. 249, // VEX-Extention prefix $0F38
  1942. 250: // VEX-Extention prefix $0F3A
  1943. begin
  1944. if not(exists_vex_extention) then
  1945. begin
  1946. inc(len);
  1947. exists_vex_extention := true;
  1948. end;
  1949. end;
  1950. 192,193,194:
  1951. begin
  1952. {$ifdef x86_64}
  1953. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  1954. inc(len);
  1955. {$endif x86_64}
  1956. end;
  1957. else
  1958. InternalError(200603141);
  1959. end;
  1960. until false;
  1961. {$ifdef x86_64}
  1962. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  1963. Message(asmw_e_bad_reg_with_rex);
  1964. rex:=rex and $4F; { reset extra bits in upper nibble }
  1965. if omit_rexw then
  1966. begin
  1967. if rex=$48 then { remove rex entirely? }
  1968. rex:=0
  1969. else
  1970. rex:=rex and $F7;
  1971. end;
  1972. if not(exists_vex) then
  1973. begin
  1974. if rex<>0 then
  1975. Inc(len);
  1976. end;
  1977. {$endif}
  1978. if exists_vex then
  1979. begin
  1980. if exists_prefix_66 then dec(len);
  1981. if exists_prefix_F2 then dec(len);
  1982. if exists_prefix_F3 then dec(len);
  1983. {$ifdef x86_64}
  1984. if not(exists_vex_extention) then
  1985. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extention
  1986. {$endif x86_64}
  1987. end;
  1988. calcsize:=len;
  1989. end;
  1990. procedure taicpu.GenCode(objdata:TObjData);
  1991. {
  1992. * the actual codes (C syntax, i.e. octal):
  1993. * \0 - terminates the code. (Unless it's a literal of course.)
  1994. * \1, \2, \3 - that many literal bytes follow in the code stream
  1995. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1996. * (POP is never used for CS) depending on operand 0
  1997. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1998. * on operand 0
  1999. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  2000. * to the register value of operand 0, 1 or 2
  2001. * \13 - a literal byte follows in the code stream, to be added
  2002. * to the condition code value of the instruction.
  2003. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  2004. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  2005. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  2006. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  2007. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  2008. * assembly mode or the address-size override on the operand
  2009. * \37 - a word constant, from the _segment_ part of operand 0
  2010. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  2011. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  2012. on the address size of instruction
  2013. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  2014. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  2015. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  2016. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  2017. * assembly mode or the address-size override on the operand
  2018. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  2019. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  2020. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  2021. * field the register value of operand b.
  2022. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  2023. * field equal to digit b.
  2024. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  2025. * \300,\301,\302 - might be an 0x67, depending on the address size of
  2026. * the memory reference in operand x.
  2027. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  2028. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  2029. * \312 - (disassembler only) invalid with non-default address size.
  2030. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  2031. * size of operand x.
  2032. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  2033. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  2034. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  2035. * \327 - indicates that this instruction is only valid when the
  2036. * operand size is the default (instruction to disassembler,
  2037. * generates no code in the assembler)
  2038. * \331 - instruction not valid with REP prefix. Hint for
  2039. * disassembler only; for SSE instructions.
  2040. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  2041. * \333 - 0xF3 prefix for SSE instructions
  2042. * \334 - 0xF2 prefix for SSE instructions
  2043. * \335 - Indicates 64-bit operand size with REX.W not necessary
  2044. * \361 - 0x66 prefix for SSE instructions
  2045. * \362 - VEX prefix for AVX instructions
  2046. * \363 - VEX W1
  2047. * \364 - VEX Vector length 256
  2048. * \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
  2049. * \370 - VEX 0F-FLAG
  2050. * \371 - VEX 0F38-FLAG
  2051. * \372 - VEX 0F3A-FLAG
  2052. }
  2053. var
  2054. currval : aint;
  2055. currsym : tobjsymbol;
  2056. currrelreloc,
  2057. currabsreloc,
  2058. currabsreloc32 : TObjRelocationType;
  2059. {$ifdef x86_64}
  2060. rexwritten : boolean;
  2061. {$endif x86_64}
  2062. procedure getvalsym(opidx:longint);
  2063. begin
  2064. case oper[opidx]^.typ of
  2065. top_ref :
  2066. begin
  2067. currval:=oper[opidx]^.ref^.offset;
  2068. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  2069. {$ifdef i386}
  2070. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2071. (tf_pic_uses_got in target_info.flags) then
  2072. begin
  2073. currrelreloc:=RELOC_PLT32;
  2074. currabsreloc:=RELOC_GOT32;
  2075. currabsreloc32:=RELOC_GOT32;
  2076. end
  2077. else
  2078. {$endif i386}
  2079. {$ifdef x86_64}
  2080. if oper[opidx]^.ref^.refaddr=addr_pic then
  2081. begin
  2082. currrelreloc:=RELOC_PLT32;
  2083. currabsreloc:=RELOC_GOTPCREL;
  2084. currabsreloc32:=RELOC_GOTPCREL;
  2085. end
  2086. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  2087. begin
  2088. currrelreloc:=RELOC_RELATIVE;
  2089. currabsreloc:=RELOC_RELATIVE;
  2090. currabsreloc32:=RELOC_RELATIVE;
  2091. end
  2092. else
  2093. {$endif x86_64}
  2094. begin
  2095. currrelreloc:=RELOC_RELATIVE;
  2096. currabsreloc:=RELOC_ABSOLUTE;
  2097. currabsreloc32:=RELOC_ABSOLUTE32;
  2098. end;
  2099. end;
  2100. top_const :
  2101. begin
  2102. currval:=aint(oper[opidx]^.val);
  2103. currsym:=nil;
  2104. currabsreloc:=RELOC_ABSOLUTE;
  2105. currabsreloc32:=RELOC_ABSOLUTE32;
  2106. end;
  2107. else
  2108. Message(asmw_e_immediate_or_reference_expected);
  2109. end;
  2110. end;
  2111. {$ifdef x86_64}
  2112. procedure maybewriterex;
  2113. begin
  2114. if (rex<>0) and not(rexwritten) then
  2115. begin
  2116. rexwritten:=true;
  2117. objdata.writebytes(rex,1);
  2118. end;
  2119. end;
  2120. {$endif x86_64}
  2121. procedure objdata_writereloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  2122. begin
  2123. {$ifdef i386}
  2124. { Special case of '_GLOBAL_OFFSET_TABLE_'
  2125. which needs a special relocation type R_386_GOTPC }
  2126. if assigned (p) and
  2127. (p.name='_GLOBAL_OFFSET_TABLE_') and
  2128. (tf_pic_uses_got in target_info.flags) then
  2129. begin
  2130. { nothing else than a 4 byte relocation should occur
  2131. for GOT }
  2132. if len<>4 then
  2133. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2134. Reloctype:=RELOC_GOTPC;
  2135. { We need to add the offset of the relocation
  2136. of _GLOBAL_OFFSET_TABLE symbol within
  2137. the current instruction }
  2138. inc(data,objdata.currobjsec.size-insoffset);
  2139. end;
  2140. {$endif i386}
  2141. objdata.writereloc(data,len,p,Reloctype);
  2142. end;
  2143. const
  2144. CondVal:array[TAsmCond] of byte=($0,
  2145. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  2146. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  2147. $0, $A, $A, $B, $8, $4);
  2148. var
  2149. c : byte;
  2150. pb : pbyte;
  2151. codes : pchar;
  2152. bytes : array[0..3] of byte;
  2153. rfield,
  2154. data,s,opidx : longint;
  2155. ea_data : ea;
  2156. relsym : TObjSymbol;
  2157. needed_VEX_Extention: boolean;
  2158. needed_VEX: boolean;
  2159. opmode: integer;
  2160. VEXvvvv: byte;
  2161. VEXmmmmm: byte;
  2162. begin
  2163. { safety check }
  2164. if objdata.currobjsec.size<>longword(insoffset) then
  2165. internalerror(200130121);
  2166. { those variables are initialized inside local procedures, the dfa cannot handle this yet }
  2167. currsym:=nil;
  2168. currabsreloc:=RELOC_NONE;
  2169. currabsreloc32:=RELOC_NONE;
  2170. currrelreloc:=RELOC_NONE;
  2171. currval:=0;
  2172. { load data to write }
  2173. codes:=insentry^.code;
  2174. {$ifdef x86_64}
  2175. rexwritten:=false;
  2176. {$endif x86_64}
  2177. { Force word push/pop for registers }
  2178. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  2179. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  2180. begin
  2181. bytes[0]:=$66;
  2182. objdata.writebytes(bytes,1);
  2183. end;
  2184. // needed VEX Prefix (for AVX etc.)
  2185. needed_VEX := false;
  2186. needed_VEX_Extention := false;
  2187. opmode := -1;
  2188. VEXvvvv := 0;
  2189. VEXmmmmm := 0;
  2190. repeat
  2191. c:=ord(codes^);
  2192. inc(codes);
  2193. case c of
  2194. 0: break;
  2195. 1,
  2196. 2,
  2197. 3: inc(codes,c);
  2198. 60: opmode := 0;
  2199. 61: opmode := 1;
  2200. 62: opmode := 2;
  2201. 219: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  2202. 220: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  2203. 241: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  2204. 242: needed_VEX := true;
  2205. 243: begin
  2206. needed_VEX_Extention := true;
  2207. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  2208. end;
  2209. 244: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  2210. 248: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  2211. 249: begin
  2212. needed_VEX_Extention := true;
  2213. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  2214. end;
  2215. 250: begin
  2216. needed_VEX_Extention := true;
  2217. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  2218. end;
  2219. end;
  2220. until false;
  2221. if needed_VEX then
  2222. begin
  2223. if (opmode > ops) or
  2224. (opmode < -1) then
  2225. begin
  2226. Internalerror(777100);
  2227. end
  2228. else if opmode = -1 then
  2229. begin
  2230. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  2231. end
  2232. else if oper[opmode]^.typ = top_reg then
  2233. begin
  2234. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  2235. {$ifdef x86_64}
  2236. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  2237. {$else}
  2238. VEXvvvv := VEXvvvv or (1 shl 6);
  2239. {$endif x86_64}
  2240. end
  2241. else Internalerror(777101);
  2242. if not(needed_VEX_Extention) then
  2243. begin
  2244. {$ifdef x86_64}
  2245. if rex and $0B <> 0 then needed_VEX_Extention := true;
  2246. {$endif x86_64}
  2247. end;
  2248. if needed_VEX_Extention then
  2249. begin
  2250. // VEX-Prefix-Length = 3 Bytes
  2251. bytes[0]:=$C4;
  2252. objdata.writebytes(bytes,1);
  2253. {$ifdef x86_64}
  2254. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  2255. {$else}
  2256. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  2257. {$endif x86_64}
  2258. bytes[0] := VEXmmmmm;
  2259. objdata.writebytes(bytes,1);
  2260. {$ifdef x86_64}
  2261. VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
  2262. {$endif x86_64}
  2263. bytes[0] := VEXvvvv;
  2264. objdata.writebytes(bytes,1);
  2265. end
  2266. else
  2267. begin
  2268. // VEX-Prefix-Length = 2 Bytes
  2269. bytes[0]:=$C5;
  2270. objdata.writebytes(bytes,1);
  2271. {$ifdef x86_64}
  2272. if rex and $04 = 0 then
  2273. {$endif x86_64}
  2274. begin
  2275. VEXvvvv := VEXvvvv or (1 shl 7);
  2276. end;
  2277. bytes[0] := VEXvvvv;
  2278. objdata.writebytes(bytes,1);
  2279. end;
  2280. end
  2281. else
  2282. begin
  2283. needed_VEX_Extention := false;
  2284. opmode := -1;
  2285. end;
  2286. { load data to write }
  2287. codes:=insentry^.code;
  2288. repeat
  2289. c:=ord(codes^);
  2290. inc(codes);
  2291. case c of
  2292. 0 :
  2293. break;
  2294. 1,2,3 :
  2295. begin
  2296. {$ifdef x86_64}
  2297. if not(needed_VEX) then // TG
  2298. maybewriterex;
  2299. {$endif x86_64}
  2300. objdata.writebytes(codes^,c);
  2301. inc(codes,c);
  2302. end;
  2303. 4,6 :
  2304. begin
  2305. case oper[0]^.reg of
  2306. NR_CS:
  2307. bytes[0]:=$e;
  2308. NR_NO,
  2309. NR_DS:
  2310. bytes[0]:=$1e;
  2311. NR_ES:
  2312. bytes[0]:=$6;
  2313. NR_SS:
  2314. bytes[0]:=$16;
  2315. else
  2316. internalerror(777004);
  2317. end;
  2318. if c=4 then
  2319. inc(bytes[0]);
  2320. objdata.writebytes(bytes,1);
  2321. end;
  2322. 5,7 :
  2323. begin
  2324. case oper[0]^.reg of
  2325. NR_FS:
  2326. bytes[0]:=$a0;
  2327. NR_GS:
  2328. bytes[0]:=$a8;
  2329. else
  2330. internalerror(777005);
  2331. end;
  2332. if c=5 then
  2333. inc(bytes[0]);
  2334. objdata.writebytes(bytes,1);
  2335. end;
  2336. 8,9,10 :
  2337. begin
  2338. {$ifdef x86_64}
  2339. if not(needed_VEX) then // TG
  2340. maybewriterex;
  2341. {$endif x86_64}
  2342. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  2343. inc(codes);
  2344. objdata.writebytes(bytes,1);
  2345. end;
  2346. 11 :
  2347. begin
  2348. bytes[0]:=ord(codes^)+condval[condition];
  2349. inc(codes);
  2350. objdata.writebytes(bytes,1);
  2351. end;
  2352. 12,13,14 :
  2353. begin
  2354. getvalsym(c-12);
  2355. if (currval<-128) or (currval>127) then
  2356. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  2357. if assigned(currsym) then
  2358. objdata_writereloc(currval,1,currsym,currabsreloc)
  2359. else
  2360. objdata.writebytes(currval,1);
  2361. end;
  2362. 16,17,18 :
  2363. begin
  2364. getvalsym(c-16);
  2365. if (currval<-256) or (currval>255) then
  2366. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  2367. if assigned(currsym) then
  2368. objdata_writereloc(currval,1,currsym,currabsreloc)
  2369. else
  2370. objdata.writebytes(currval,1);
  2371. end;
  2372. 20,21,22,23 :
  2373. begin
  2374. getvalsym(c-20);
  2375. if (currval<0) or (currval>255) then
  2376. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  2377. if assigned(currsym) then
  2378. objdata_writereloc(currval,1,currsym,currabsreloc)
  2379. else
  2380. objdata.writebytes(currval,1);
  2381. end;
  2382. 24,25,26 : // 030..032
  2383. begin
  2384. getvalsym(c-24);
  2385. {$ifndef i8086}
  2386. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  2387. if (currval<-65536) or (currval>65535) then
  2388. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  2389. {$endif i8086}
  2390. if assigned(currsym) then
  2391. objdata_writereloc(currval,2,currsym,currabsreloc)
  2392. else
  2393. objdata.writebytes(currval,2);
  2394. end;
  2395. 28,29,30 : // 034..036
  2396. { !!! These are intended (and used in opcode table) to select depending
  2397. on address size, *not* operand size. Works by coincidence only. }
  2398. begin
  2399. getvalsym(c-28);
  2400. if opsize=S_Q then
  2401. begin
  2402. if assigned(currsym) then
  2403. objdata_writereloc(currval,8,currsym,currabsreloc)
  2404. else
  2405. objdata.writebytes(currval,8);
  2406. end
  2407. else
  2408. begin
  2409. if assigned(currsym) then
  2410. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2411. else
  2412. objdata.writebytes(currval,4);
  2413. end
  2414. end;
  2415. 32,33,34 : // 040..042
  2416. begin
  2417. getvalsym(c-32);
  2418. if assigned(currsym) then
  2419. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2420. else
  2421. objdata.writebytes(currval,4);
  2422. end;
  2423. 36,37,38 : // 044..046 - select between word/dword/qword depending on
  2424. begin // address size (we support only default address sizes).
  2425. getvalsym(c-36);
  2426. {$ifdef x86_64}
  2427. if assigned(currsym) then
  2428. objdata_writereloc(currval,8,currsym,currabsreloc)
  2429. else
  2430. objdata.writebytes(currval,8);
  2431. {$else x86_64}
  2432. if assigned(currsym) then
  2433. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2434. else
  2435. objdata.writebytes(currval,4);
  2436. {$endif x86_64}
  2437. end;
  2438. 40,41,42 : // 050..052 - byte relative operand
  2439. begin
  2440. getvalsym(c-40);
  2441. data:=currval-insend;
  2442. {$push}
  2443. {$r-}
  2444. if assigned(currsym) then
  2445. inc(data,currsym.address);
  2446. {$pop}
  2447. if (data>127) or (data<-128) then
  2448. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2449. objdata.writebytes(data,1);
  2450. end;
  2451. 44,45,46: // 054..056 - qword immediate operand
  2452. begin
  2453. getvalsym(c-44);
  2454. if assigned(currsym) then
  2455. objdata_writereloc(currval,8,currsym,currabsreloc)
  2456. else
  2457. objdata.writebytes(currval,8);
  2458. end;
  2459. 52,53,54 : // 064..066 - select between 16/32 address mode, but we support only 32
  2460. begin
  2461. getvalsym(c-52);
  2462. if assigned(currsym) then
  2463. objdata_writereloc(currval,4,currsym,currrelreloc)
  2464. else
  2465. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2466. end;
  2467. 56,57,58 : // 070..072 - long relative operand
  2468. begin
  2469. getvalsym(c-56);
  2470. if assigned(currsym) then
  2471. objdata_writereloc(currval,4,currsym,currrelreloc)
  2472. else
  2473. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2474. end;
  2475. 60,61,62 : ; // 074..076 - vex-coded vector operand
  2476. // ignore
  2477. 172,173,174 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2478. begin
  2479. getvalsym(c-172);
  2480. {$ifdef x86_64}
  2481. { for i386 as aint type is longint the
  2482. following test is useless }
  2483. if (currval<low(longint)) or (currval>high(longint)) then
  2484. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2485. {$endif x86_64}
  2486. if assigned(currsym) then
  2487. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2488. else
  2489. objdata.writebytes(currval,4);
  2490. end;
  2491. 192,193,194:
  2492. begin
  2493. {$ifdef x86_64}
  2494. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  2495. begin
  2496. bytes[0]:=$67;
  2497. objdata.writebytes(bytes,1);
  2498. end;
  2499. {$endif x86_64}
  2500. end;
  2501. 200 : { fixed 16-bit addr }
  2502. {$ifndef x86_64}
  2503. begin
  2504. bytes[0]:=$67;
  2505. objdata.writebytes(bytes,1);
  2506. end;
  2507. {$else x86_64}
  2508. { every insentry having code 0310 must be marked with NOX86_64 }
  2509. InternalError(2011051302);
  2510. {$endif}
  2511. 201 : { fixed 32-bit addr }
  2512. {$ifdef x86_64}
  2513. begin
  2514. bytes[0]:=$67;
  2515. objdata.writebytes(bytes,1);
  2516. end
  2517. {$endif x86_64}
  2518. ;
  2519. 208,209,210 :
  2520. begin
  2521. case oper[c-208]^.ot and OT_SIZE_MASK of
  2522. OT_BITS16 :
  2523. begin
  2524. bytes[0]:=$66;
  2525. objdata.writebytes(bytes,1);
  2526. end;
  2527. {$ifndef x86_64}
  2528. OT_BITS64 :
  2529. Message(asmw_e_64bit_not_supported);
  2530. {$endif x86_64}
  2531. end;
  2532. end;
  2533. 211,
  2534. 213 : {no action needed};
  2535. 212,
  2536. 241:
  2537. begin
  2538. if not(needed_VEX) then
  2539. begin
  2540. bytes[0]:=$66;
  2541. objdata.writebytes(bytes,1);
  2542. end;
  2543. end;
  2544. 214 :
  2545. begin
  2546. {$ifndef x86_64}
  2547. Message(asmw_e_64bit_not_supported);
  2548. {$endif x86_64}
  2549. end;
  2550. 219 :
  2551. begin
  2552. if not(needed_VEX) then
  2553. begin
  2554. bytes[0]:=$f3;
  2555. objdata.writebytes(bytes,1);
  2556. end;
  2557. end;
  2558. 220 :
  2559. begin
  2560. if not(needed_VEX) then
  2561. begin
  2562. bytes[0]:=$f2;
  2563. objdata.writebytes(bytes,1);
  2564. end;
  2565. end;
  2566. 221:
  2567. ;
  2568. 202,
  2569. 215,
  2570. 217,218 :
  2571. begin
  2572. { these are dissambler hints or 32 bit prefixes which
  2573. are not needed }
  2574. end;
  2575. 242..244: ; // VEX flags =>> nothing todo
  2576. 247: begin
  2577. if needed_VEX then
  2578. begin
  2579. if ops = 4 then
  2580. begin
  2581. if (oper[3]^.typ=top_reg) then
  2582. begin
  2583. if (oper[3]^.ot and otf_reg_xmm <> 0) or
  2584. (oper[3]^.ot and otf_reg_ymm <> 0) then
  2585. begin
  2586. bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
  2587. objdata.writebytes(bytes,1);
  2588. end
  2589. else Internalerror(777102);
  2590. end
  2591. else Internalerror(777103);
  2592. end
  2593. else Internalerror(777104);
  2594. end
  2595. else Internalerror(777105);
  2596. end;
  2597. 248..250: ; // VEX flags =>> nothing todo
  2598. 31,
  2599. 48,49,50 :
  2600. begin
  2601. InternalError(777006);
  2602. end
  2603. else
  2604. begin
  2605. { rex should be written at this point }
  2606. {$ifdef x86_64}
  2607. if not(needed_VEX) then // TG
  2608. if (rex<>0) and not(rexwritten) then
  2609. internalerror(200603191);
  2610. {$endif x86_64}
  2611. if (c>=64) and (c<=151) then // 0100..0227
  2612. begin
  2613. if (c<127) then // 0177
  2614. begin
  2615. if (oper[c and 7]^.typ=top_reg) then
  2616. rfield:=regval(oper[c and 7]^.reg)
  2617. else
  2618. rfield:=regval(oper[c and 7]^.ref^.base);
  2619. end
  2620. else
  2621. rfield:=c and 7;
  2622. opidx:=(c shr 3) and 7;
  2623. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2624. Message(asmw_e_invalid_effective_address);
  2625. pb:=@bytes[0];
  2626. pb^:=ea_data.modrm;
  2627. inc(pb);
  2628. if ea_data.sib_present then
  2629. begin
  2630. pb^:=ea_data.sib;
  2631. inc(pb);
  2632. end;
  2633. s:=pb-@bytes[0];
  2634. objdata.writebytes(bytes,s);
  2635. case ea_data.bytes of
  2636. 0 : ;
  2637. 1 :
  2638. begin
  2639. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2640. begin
  2641. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2642. {$ifdef i386}
  2643. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2644. (tf_pic_uses_got in target_info.flags) then
  2645. currabsreloc:=RELOC_GOT32
  2646. else
  2647. {$endif i386}
  2648. {$ifdef x86_64}
  2649. if oper[opidx]^.ref^.refaddr=addr_pic then
  2650. currabsreloc:=RELOC_GOTPCREL
  2651. else
  2652. {$endif x86_64}
  2653. currabsreloc:=RELOC_ABSOLUTE;
  2654. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2655. end
  2656. else
  2657. begin
  2658. bytes[0]:=oper[opidx]^.ref^.offset;
  2659. objdata.writebytes(bytes,1);
  2660. end;
  2661. inc(s);
  2662. end;
  2663. 2,4 :
  2664. begin
  2665. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2666. currval:=oper[opidx]^.ref^.offset;
  2667. {$ifdef x86_64}
  2668. if oper[opidx]^.ref^.refaddr=addr_pic then
  2669. currabsreloc:=RELOC_GOTPCREL
  2670. else
  2671. if oper[opidx]^.ref^.base=NR_RIP then
  2672. begin
  2673. currabsreloc:=RELOC_RELATIVE;
  2674. { Adjust reloc value by number of bytes following the displacement,
  2675. but not if displacement is specified by literal constant }
  2676. if Assigned(currsym) then
  2677. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  2678. end
  2679. else
  2680. {$endif x86_64}
  2681. {$ifdef i386}
  2682. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2683. (tf_pic_uses_got in target_info.flags) then
  2684. currabsreloc:=RELOC_GOT32
  2685. else
  2686. {$endif i386}
  2687. currabsreloc:=RELOC_ABSOLUTE32;
  2688. if (currabsreloc=RELOC_ABSOLUTE32) and
  2689. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  2690. begin
  2691. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  2692. if relsym.objsection=objdata.CurrObjSec then
  2693. begin
  2694. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  2695. currabsreloc:=RELOC_RELATIVE;
  2696. end
  2697. else
  2698. begin
  2699. currabsreloc:=RELOC_PIC_PAIR;
  2700. currval:=relsym.offset;
  2701. end;
  2702. end;
  2703. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  2704. inc(s,ea_data.bytes);
  2705. end;
  2706. end;
  2707. end
  2708. else
  2709. InternalError(777007);
  2710. end;
  2711. end;
  2712. until false;
  2713. end;
  2714. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2715. begin
  2716. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2717. (regtype = R_INTREGISTER) and
  2718. (ops=2) and
  2719. (oper[0]^.typ=top_reg) and
  2720. (oper[1]^.typ=top_reg) and
  2721. (oper[0]^.reg=oper[1]^.reg)
  2722. ) or
  2723. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2724. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD) or
  2725. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or (opcode=A_VMOVQ) or
  2726. (opcode=A_VMOVAPS) or (OPCODE=A_VMOVAPD)) and
  2727. (regtype = R_MMREGISTER) and
  2728. (ops=2) and
  2729. (oper[0]^.typ=top_reg) and
  2730. (oper[1]^.typ=top_reg) and
  2731. (oper[0]^.reg=oper[1]^.reg)
  2732. );
  2733. end;
  2734. procedure build_spilling_operation_type_table;
  2735. var
  2736. opcode : tasmop;
  2737. i : integer;
  2738. begin
  2739. new(operation_type_table);
  2740. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2741. for opcode:=low(tasmop) to high(tasmop) do
  2742. begin
  2743. for i:=1 to MaxInsChanges do
  2744. begin
  2745. case InsProp[opcode].Ch[i] of
  2746. Ch_Rop1 :
  2747. operation_type_table^[opcode,0]:=operand_read;
  2748. Ch_Wop1 :
  2749. operation_type_table^[opcode,0]:=operand_write;
  2750. Ch_RWop1,
  2751. Ch_Mop1 :
  2752. operation_type_table^[opcode,0]:=operand_readwrite;
  2753. Ch_Rop2 :
  2754. operation_type_table^[opcode,1]:=operand_read;
  2755. Ch_Wop2 :
  2756. operation_type_table^[opcode,1]:=operand_write;
  2757. Ch_RWop2,
  2758. Ch_Mop2 :
  2759. operation_type_table^[opcode,1]:=operand_readwrite;
  2760. Ch_Rop3 :
  2761. operation_type_table^[opcode,2]:=operand_read;
  2762. Ch_Wop3 :
  2763. operation_type_table^[opcode,2]:=operand_write;
  2764. Ch_RWop3,
  2765. Ch_Mop3 :
  2766. operation_type_table^[opcode,2]:=operand_readwrite;
  2767. end;
  2768. end;
  2769. end;
  2770. end;
  2771. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2772. begin
  2773. { the information in the instruction table is made for the string copy
  2774. operation MOVSD so hack here (FK)
  2775. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  2776. so fix it here (FK)
  2777. }
  2778. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  2779. begin
  2780. case opnr of
  2781. 0:
  2782. result:=operand_read;
  2783. 1:
  2784. result:=operand_write;
  2785. else
  2786. internalerror(200506055);
  2787. end
  2788. end
  2789. { IMUL has 1, 2 and 3-operand forms }
  2790. else if opcode=A_IMUL then
  2791. begin
  2792. case ops of
  2793. 1:
  2794. if opnr=0 then
  2795. result:=operand_read
  2796. else
  2797. internalerror(2014011802);
  2798. 2:
  2799. begin
  2800. case opnr of
  2801. 0:
  2802. result:=operand_read;
  2803. 1:
  2804. result:=operand_readwrite;
  2805. else
  2806. internalerror(2014011803);
  2807. end;
  2808. end;
  2809. 3:
  2810. begin
  2811. case opnr of
  2812. 0,1:
  2813. result:=operand_read;
  2814. 2:
  2815. result:=operand_write;
  2816. else
  2817. internalerror(2014011804);
  2818. end;
  2819. end;
  2820. else
  2821. internalerror(2014011805);
  2822. end;
  2823. end
  2824. else
  2825. result:=operation_type_table^[opcode,opnr];
  2826. end;
  2827. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2828. var
  2829. tmpref: treference;
  2830. begin
  2831. case getregtype(r) of
  2832. R_INTREGISTER :
  2833. begin
  2834. tmpref:=ref;
  2835. if getsubreg(r)=R_SUBH then
  2836. inc(tmpref.offset);
  2837. { we don't need special code here for 32 bit loads on x86_64, since
  2838. those will automatically zero-extend the upper 32 bits. }
  2839. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  2840. end;
  2841. R_MMREGISTER :
  2842. if current_settings.fputype in fpu_avx_instructionsets then
  2843. case getsubreg(r) of
  2844. R_SUBMMD:
  2845. result:=taicpu.op_ref_reg(A_VMOVSD,reg2opsize(r),ref,r);
  2846. R_SUBMMS:
  2847. result:=taicpu.op_ref_reg(A_VMOVSS,reg2opsize(r),ref,r);
  2848. R_SUBQ,
  2849. R_SUBMMWHOLE:
  2850. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,ref,r);
  2851. else
  2852. internalerror(200506043);
  2853. end
  2854. else
  2855. case getsubreg(r) of
  2856. R_SUBMMD:
  2857. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2858. R_SUBMMS:
  2859. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2860. R_SUBQ,
  2861. R_SUBMMWHOLE:
  2862. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
  2863. else
  2864. internalerror(200506043);
  2865. end;
  2866. else
  2867. internalerror(200401041);
  2868. end;
  2869. end;
  2870. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2871. var
  2872. size: topsize;
  2873. tmpref: treference;
  2874. begin
  2875. case getregtype(r) of
  2876. R_INTREGISTER :
  2877. begin
  2878. tmpref:=ref;
  2879. if getsubreg(r)=R_SUBH then
  2880. inc(tmpref.offset);
  2881. size:=reg2opsize(r);
  2882. {$ifdef x86_64}
  2883. { even if it's a 32 bit reg, we still have to spill 64 bits
  2884. because we often perform 64 bit operations on them }
  2885. if (size=S_L) then
  2886. begin
  2887. size:=S_Q;
  2888. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  2889. end;
  2890. {$endif x86_64}
  2891. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  2892. end;
  2893. R_MMREGISTER :
  2894. if current_settings.fputype in fpu_avx_instructionsets then
  2895. case getsubreg(r) of
  2896. R_SUBMMD:
  2897. result:=taicpu.op_reg_ref(A_VMOVSD,reg2opsize(r),r,ref);
  2898. R_SUBMMS:
  2899. result:=taicpu.op_reg_ref(A_VMOVSS,reg2opsize(r),r,ref);
  2900. R_SUBQ,
  2901. R_SUBMMWHOLE:
  2902. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,ref);
  2903. else
  2904. internalerror(200506042);
  2905. end
  2906. else
  2907. case getsubreg(r) of
  2908. R_SUBMMD:
  2909. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2910. R_SUBMMS:
  2911. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2912. R_SUBQ,
  2913. R_SUBMMWHOLE:
  2914. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
  2915. else
  2916. internalerror(200506042);
  2917. end;
  2918. else
  2919. internalerror(200401041);
  2920. end;
  2921. end;
  2922. {*****************************************************************************
  2923. Instruction table
  2924. *****************************************************************************}
  2925. procedure BuildInsTabCache;
  2926. var
  2927. i : longint;
  2928. begin
  2929. new(instabcache);
  2930. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2931. i:=0;
  2932. while (i<InsTabEntries) do
  2933. begin
  2934. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2935. InsTabCache^[InsTab[i].OPcode]:=i;
  2936. inc(i);
  2937. end;
  2938. end;
  2939. procedure BuildInsTabMemRefSizeInfoCache;
  2940. var
  2941. AsmOp: TasmOp;
  2942. i,j: longint;
  2943. insentry : PInsEntry;
  2944. MRefInfo: TMemRefSizeInfo;
  2945. SConstInfo: TConstSizeInfo;
  2946. actRegSize: int64;
  2947. actMemSize: int64;
  2948. actConstSize: int64;
  2949. actRegCount: integer;
  2950. actMemCount: integer;
  2951. actConstCount: integer;
  2952. actRegTypes : int64;
  2953. actRegMemTypes: int64;
  2954. NewRegSize: int64;
  2955. RegMMXSizeMask: int64;
  2956. RegXMMSizeMask: int64;
  2957. RegYMMSizeMask: int64;
  2958. bitcount: integer;
  2959. function bitcnt(aValue: int64): integer;
  2960. var
  2961. i: integer;
  2962. begin
  2963. result := 0;
  2964. for i := 0 to 63 do
  2965. begin
  2966. if (aValue mod 2) = 1 then
  2967. begin
  2968. inc(result);
  2969. end;
  2970. aValue := aValue shr 1;
  2971. end;
  2972. end;
  2973. begin
  2974. new(InsTabMemRefSizeInfoCache);
  2975. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  2976. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  2977. begin
  2978. i := InsTabCache^[AsmOp];
  2979. if i >= 0 then
  2980. begin
  2981. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  2982. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  2983. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  2984. insentry:=@instab[i];
  2985. RegMMXSizeMask := 0;
  2986. RegXMMSizeMask := 0;
  2987. RegYMMSizeMask := 0;
  2988. while (insentry^.opcode=AsmOp) do
  2989. begin
  2990. MRefInfo := msiUnkown;
  2991. actRegSize := 0;
  2992. actRegCount := 0;
  2993. actRegTypes := 0;
  2994. NewRegSize := 0;
  2995. actMemSize := 0;
  2996. actMemCount := 0;
  2997. actRegMemTypes := 0;
  2998. actConstSize := 0;
  2999. actConstCount := 0;
  3000. if asmop = a_movups then
  3001. begin
  3002. RegXMMSizeMask := RegXMMSizeMask;
  3003. end;
  3004. for j := 0 to insentry^.ops -1 do
  3005. begin
  3006. if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  3007. begin
  3008. inc(actRegCount);
  3009. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  3010. if NewRegSize = 0 then
  3011. begin
  3012. case insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG) of
  3013. OT_MMXREG: begin
  3014. NewRegSize := OT_BITS64;
  3015. end;
  3016. OT_XMMREG: begin
  3017. NewRegSize := OT_BITS128;
  3018. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3019. end;
  3020. OT_YMMREG: begin
  3021. NewRegSize := OT_BITS256;
  3022. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3023. end;
  3024. else NewRegSize := not(0);
  3025. end;
  3026. end;
  3027. actRegSize := actRegSize or NewRegSize;
  3028. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG));
  3029. end
  3030. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  3031. begin
  3032. inc(actMemCount);
  3033. actMemSize := actMemSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3034. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  3035. begin
  3036. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  3037. end;
  3038. end
  3039. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  3040. begin
  3041. inc(actConstCount);
  3042. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3043. end
  3044. end;
  3045. if actConstCount > 0 then
  3046. begin
  3047. case actConstSize of
  3048. 0: SConstInfo := csiNoSize;
  3049. OT_BITS8: SConstInfo := csiMem8;
  3050. OT_BITS16: SConstInfo := csiMem16;
  3051. OT_BITS32: SConstInfo := csiMem32;
  3052. OT_BITS64: SConstInfo := csiMem64;
  3053. else SConstInfo := csiMultiple;
  3054. end;
  3055. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
  3056. begin
  3057. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  3058. end
  3059. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  3060. begin
  3061. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  3062. end;
  3063. end;
  3064. case actMemCount of
  3065. 0: ; // nothing todo
  3066. 1: begin
  3067. MRefInfo := msiUnkown;
  3068. case actRegMemTypes and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM) of
  3069. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  3070. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  3071. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  3072. end;
  3073. case actMemSize of
  3074. 0: MRefInfo := msiNoSize;
  3075. OT_BITS8: MRefInfo := msiMem8;
  3076. OT_BITS16: MRefInfo := msiMem16;
  3077. OT_BITS32: MRefInfo := msiMem32;
  3078. OT_BITS64: MRefInfo := msiMem64;
  3079. OT_BITS128: MRefInfo := msiMem128;
  3080. OT_BITS256: MRefInfo := msiMem256;
  3081. OT_BITS80,
  3082. OT_FAR,
  3083. OT_NEAR,
  3084. OT_SHORT: ; // ignore
  3085. else begin
  3086. bitcount := bitcnt(actMemSize);
  3087. if bitcount > 1 then MRefInfo := msiMultiple
  3088. else InternalError(777203);
  3089. end;
  3090. end;
  3091. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3092. begin
  3093. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3094. end
  3095. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3096. begin
  3097. with InsTabMemRefSizeInfoCache^[AsmOp] do
  3098. begin
  3099. if ((MemRefSize = msiMem8) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultiple8
  3100. else if ((MemRefSize = msiMem16) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultiple16
  3101. else if ((MemRefSize = msiMem32) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultiple32
  3102. else if ((MemRefSize = msiMem64) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultiple64
  3103. else if ((MemRefSize = msiMem128) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultiple128
  3104. else if ((MemRefSize = msiMem256) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultiple256
  3105. else MemRefSize := msiMultiple;
  3106. end;
  3107. end;
  3108. if actRegCount > 0 then
  3109. begin
  3110. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG) of
  3111. OT_MMXREG: RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  3112. OT_XMMREG: RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  3113. OT_YMMREG: RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  3114. else begin
  3115. RegMMXSizeMask := not(0);
  3116. RegXMMSizeMask := not(0);
  3117. RegYMMSizeMask := not(0);
  3118. end;
  3119. end;
  3120. end;
  3121. end;
  3122. else InternalError(777202);
  3123. end;
  3124. inc(insentry);
  3125. end;
  3126. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  3127. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  3128. begin
  3129. case RegXMMSizeMask of
  3130. OT_BITS64: case RegYMMSizeMask of
  3131. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3132. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  3133. end;
  3134. OT_BITS128: begin
  3135. if RegMMXSizeMask = 0 then
  3136. begin
  3137. case RegYMMSizeMask of
  3138. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3139. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3140. end;
  3141. end
  3142. else if RegYMMSizeMask = 0 then
  3143. begin
  3144. case RegMMXSizeMask of
  3145. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3146. end;
  3147. end
  3148. else InternalError(777205);
  3149. end;
  3150. end;
  3151. end;
  3152. end;
  3153. end;
  3154. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3155. begin
  3156. // only supported intructiones with SSE- or AVX-operands
  3157. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  3158. begin
  3159. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3160. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3161. end;
  3162. end;
  3163. end;
  3164. procedure InitAsm;
  3165. begin
  3166. build_spilling_operation_type_table;
  3167. if not assigned(instabcache) then
  3168. BuildInsTabCache;
  3169. if not assigned(InsTabMemRefSizeInfoCache) then
  3170. BuildInsTabMemRefSizeInfoCache;
  3171. end;
  3172. procedure DoneAsm;
  3173. begin
  3174. if assigned(operation_type_table) then
  3175. begin
  3176. dispose(operation_type_table);
  3177. operation_type_table:=nil;
  3178. end;
  3179. if assigned(instabcache) then
  3180. begin
  3181. dispose(instabcache);
  3182. instabcache:=nil;
  3183. end;
  3184. if assigned(InsTabMemRefSizeInfoCache) then
  3185. begin
  3186. dispose(InsTabMemRefSizeInfoCache);
  3187. InsTabMemRefSizeInfoCache:=nil;
  3188. end;
  3189. end;
  3190. begin
  3191. cai_align:=tai_align;
  3192. cai_cpu:=taicpu;
  3193. end.