aasmcpu.pas 139 KB

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