aasmcpu.pas 140 KB

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