aasmcpu.pas 144 KB

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