aasmcpu.pas 208 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816
  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_BITS512 = $40000000; { 64 byte AVX512 }
  44. OT_BITS128 = $20000000; { 16 byte SSE }
  45. OT_BITS256 = $40000000; { 32 byte AVX }
  46. OT_BITS512 = $80000000; { 64 byte AVX512 }
  47. OT_VECTORMASK = $1000000000; { OPTIONAL VECTORMASK AVX512}
  48. OT_VECTORZERO = $2000000000; { OPTIONAL ZERO-FLAG AVX512}
  49. OT_VECTORBCST = $4000000000; { BROADCAST-MEM-FLAG AVX512}
  50. OT_VECTORSAE = $8000000000; { OPTIONAL SAE-FLAG AVX512}
  51. OT_VECTORER = $10000000000; { OPTIONAL ER-FLAG-FLAG AVX512}
  52. OT_VECTORSIB = $20000000000; { SIB-MEM-FLAG AMX (in 64 bit mode only)}
  53. OT_VECTOR_EXT = OT_VECTORMASK or OT_VECTORZERO or OT_VECTORBCST or OT_VECTORSAE or OT_VECTORER;
  54. OT_BITSB16 = OT_BITS16 or OT_VECTORBCST;
  55. OT_BITSB32 = OT_BITS32 or OT_VECTORBCST;
  56. OT_BITSB64 = OT_BITS64 or OT_VECTORBCST;
  57. OT_BITS80 = $00000010; { FPU only }
  58. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  59. OT_NEAR = $00000040;
  60. OT_SHORT = $00000080;
  61. { TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
  62. but this requires adjusting the opcode table }
  63. //OT_SIZE_MASK = $3000001F; { all the size attributes }
  64. OT_SIZE_MASK = $E000001F; { all the size attributes }
  65. OT_NON_SIZE = longint(not(longint(OT_SIZE_MASK)));
  66. { Bits 8..10: modifiers }
  67. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  68. OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
  69. OT_COLON = $00000400; { operand is followed by a colon }
  70. OT_MODIFIER_MASK = $00000700;
  71. { Bits 12..15: type of operand }
  72. OT_REGISTER = $00001000;
  73. OT_IMMEDIATE = $00002000;
  74. OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
  75. OT_REGMEM = $00008000; { for r/m, ie EA, operands }
  76. OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
  77. OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
  78. { Bits 11, 20..29: register classes
  79. otf_* consts are not used alone, only to build other constants. }
  80. otf_reg_cdt = $00100000;
  81. otf_reg_gpr = $00200000;
  82. otf_reg_sreg = $00400000;
  83. otf_reg_k = $00800000;
  84. otf_reg_fpu = $01000000;
  85. otf_reg_mmx = $02000000;
  86. otf_reg_xmm = $04000000;
  87. otf_reg_ymm = $08000000;
  88. otf_reg_zmm = $10000000;
  89. otf_reg_tmm = $00000800;
  90. //otf_reg_extra_mask = $0F000000;
  91. otf_reg_extra_mask = $1F000800;
  92. { Bits 16..19: subclasses, meaning depends on classes field }
  93. otf_sub0 = $00010000;
  94. otf_sub1 = $00020000;
  95. otf_sub2 = $00040000;
  96. otf_sub3 = $00080000;
  97. OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
  98. //OT_REG_EXTRA_MASK = $0F000000;
  99. OT_REG_EXTRA_MASK = $1F000800;
  100. OT_REG_TYPMASK = otf_reg_cdt or otf_reg_gpr or otf_reg_sreg or otf_reg_k or otf_reg_extra_mask;
  101. { register class 0: CRx, DRx and TRx }
  102. {$ifdef x86_64}
  103. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
  104. {$else x86_64}
  105. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
  106. {$endif x86_64}
  107. OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
  108. OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
  109. OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
  110. OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
  111. { register class 1: general-purpose registers }
  112. OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
  113. OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
  114. OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
  115. OT_REG16 = OT_REG_GPR or OT_BITS16;
  116. OT_REG32 = OT_REG_GPR or OT_BITS32;
  117. OT_REG64 = OT_REG_GPR or OT_BITS64;
  118. { GPR subclass 0: accumulator: AL, AX, EAX or RAX }
  119. OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
  120. OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
  121. OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
  122. OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
  123. {$ifdef x86_64}
  124. OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
  125. {$endif x86_64}
  126. { GPR subclass 1: counter: CL, CX, ECX or RCX }
  127. OT_REG_COUNT = OT_REG_GPR or otf_sub1;
  128. OT_REG_CL = OT_REG_COUNT or OT_BITS8;
  129. OT_REG_CX = OT_REG_COUNT or OT_BITS16;
  130. OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
  131. {$ifdef x86_64}
  132. OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
  133. {$endif x86_64}
  134. { GPR subclass 2: data register: DL, DX, EDX or RDX }
  135. OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
  136. OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
  137. { register class 2: Segment registers }
  138. OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
  139. OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
  140. OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
  141. OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
  142. { register class 3: FPU registers }
  143. OT_FPUREG = OT_REGISTER or otf_reg_fpu;
  144. OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
  145. { register class 4: MMX (both reg and r/m) }
  146. OT_MMXREG = OT_REGNORM or otf_reg_mmx;
  147. OT_MMXRM = OT_REGMEM or otf_reg_mmx;
  148. { register class 5: XMM (both reg and r/m) }
  149. OT_XMMREG = OT_REGNORM or otf_reg_xmm;
  150. OT_XMMRM = OT_REGMEM or otf_reg_xmm;
  151. OT_XMEM32 = OT_REGNORM or otf_reg_xmm or otf_reg_gpr or OT_BITS32;
  152. OT_XMEM32_M = OT_XMEM32 or OT_VECTORMASK;
  153. OT_XMEM64 = OT_REGNORM or otf_reg_xmm or otf_reg_gpr or OT_BITS64;
  154. OT_XMEM64_M = OT_XMEM64 or OT_VECTORMASK;
  155. OT_XMMREG_M = OT_XMMREG or OT_VECTORMASK;
  156. OT_XMMREG_MZ = OT_XMMREG or OT_VECTORMASK or OT_VECTORZERO;
  157. OT_XMMRM_MZ = OT_XMMRM or OT_VECTORMASK or OT_VECTORZERO;
  158. OT_XMMREG_SAE = OT_XMMREG or OT_VECTORSAE;
  159. OT_XMMRM_SAE = OT_XMMRM or OT_VECTORSAE;
  160. OT_XMMREG_ER = OT_XMMREG or OT_VECTORER;
  161. OT_XMMRM_ER = OT_XMMRM or OT_VECTORER;
  162. { register class 5: YMM (both reg and r/m) }
  163. OT_YMMREG = OT_REGNORM or otf_reg_ymm;
  164. OT_YMMRM = OT_REGMEM or otf_reg_ymm;
  165. OT_YMEM32 = OT_REGNORM or otf_reg_ymm or otf_reg_gpr or OT_BITS32;
  166. OT_YMEM32_M = OT_YMEM32 or OT_VECTORMASK;
  167. OT_YMEM64 = OT_REGNORM or otf_reg_ymm or otf_reg_gpr or OT_BITS64;
  168. OT_YMEM64_M = OT_YMEM64 or OT_VECTORMASK;
  169. OT_YMMREG_M = OT_YMMREG or OT_VECTORMASK;
  170. OT_YMMREG_MZ = OT_YMMREG or OT_VECTORMASK or OT_VECTORZERO;
  171. OT_YMMRM_MZ = OT_YMMRM or OT_VECTORMASK or OT_VECTORZERO;
  172. OT_YMMREG_SAE = OT_YMMREG or OT_VECTORSAE;
  173. OT_YMMRM_SAE = OT_YMMRM or OT_VECTORSAE;
  174. OT_YMMREG_ER = OT_YMMREG or OT_VECTORER;
  175. OT_YMMRM_ER = OT_YMMRM or OT_VECTORER;
  176. { register class 5: ZMM (both reg and r/m) }
  177. OT_ZMMREG = OT_REGNORM or otf_reg_zmm;
  178. OT_ZMMRM = OT_REGMEM or otf_reg_zmm;
  179. OT_ZMEM32 = OT_REGNORM or otf_reg_zmm or otf_reg_gpr or OT_BITS32;
  180. OT_ZMEM32_M = OT_ZMEM32 or OT_VECTORMASK;
  181. OT_ZMEM64 = OT_REGNORM or otf_reg_zmm or otf_reg_gpr or OT_BITS64;
  182. OT_ZMEM64_M = OT_ZMEM64 or OT_VECTORMASK;
  183. OT_ZMMREG_M = OT_ZMMREG or OT_VECTORMASK;
  184. OT_ZMMREG_MZ = OT_ZMMREG or OT_VECTORMASK or OT_VECTORZERO;
  185. OT_ZMMRM_MZ = OT_ZMMRM or OT_VECTORMASK or OT_VECTORZERO;
  186. OT_ZMMREG_SAE = OT_ZMMREG or OT_VECTORSAE;
  187. OT_ZMMRM_SAE = OT_ZMMRM or OT_VECTORSAE;
  188. OT_ZMMREG_ER = OT_ZMMREG or OT_VECTORER;
  189. OT_ZMMRM_ER = OT_ZMMRM or OT_VECTORER;
  190. OT_KREG = OT_REGNORM or otf_reg_k;
  191. OT_KREG_M = OT_KREG or OT_VECTORMASK;
  192. { register class 5: TMM (both reg and r/m) }
  193. OT_TMMREG = OT_REGNORM or otf_reg_tmm;
  194. //OT_TMMRM = OT_REGMEM or otf_reg_tmm;
  195. { Vector-Memory operands }
  196. OT_VMEM_ANY = OT_XMEM32 or OT_XMEM64 or OT_YMEM32 or OT_YMEM64 or OT_ZMEM32 or OT_ZMEM64;
  197. { Memory operands }
  198. OT_MEM8 = OT_MEMORY or OT_BITS8;
  199. OT_MEM16 = OT_MEMORY or OT_BITS16;
  200. OT_MEM16_M = OT_MEM16 or OT_VECTORMASK;
  201. OT_BMEM16 = OT_MEMORY or OT_BITS16 or OT_VECTORBCST;
  202. OT_MEM32 = OT_MEMORY or OT_BITS32;
  203. OT_MEM32_M = OT_MEMORY or OT_BITS32 or OT_VECTORMASK;
  204. OT_BMEM32 = OT_MEMORY or OT_BITS32 or OT_VECTORBCST;
  205. OT_BMEM32_SAE= OT_MEMORY or OT_BITS32 or OT_VECTORBCST or OT_VECTORSAE;
  206. OT_MEM64 = OT_MEMORY or OT_BITS64;
  207. OT_MEM64_M = OT_MEMORY or OT_BITS64 or OT_VECTORMASK;
  208. OT_BMEM64 = OT_MEMORY or OT_BITS64 or OT_VECTORBCST;
  209. OT_BMEM64_SAE= OT_MEMORY or OT_BITS64 or OT_VECTORBCST or OT_VECTORSAE;
  210. OT_MEM128 = OT_MEMORY or OT_BITS128;
  211. OT_MEM128_M = OT_MEMORY or OT_BITS128 or OT_VECTORMASK;
  212. OT_MEM256 = OT_MEMORY or OT_BITS256;
  213. OT_MEM256_M = OT_MEMORY or OT_BITS256 or OT_VECTORMASK;
  214. OT_MEM512 = OT_MEMORY or OT_BITS512;
  215. OT_MEM512_M = OT_MEMORY or OT_BITS512 or OT_VECTORMASK;
  216. OT_MEM80 = OT_MEMORY or OT_BITS80;
  217. OT_SIBMEM = OT_MEMORY or OT_VECTORSIB;
  218. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  219. { simple [address] offset }
  220. { Matches any type of r/m operand }
  221. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM or OT_ZMMRM or OT_REG_EXTRA_MASK;
  222. { Immediate operands }
  223. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  224. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  225. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  226. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  227. OT_ONENESS = otf_sub0; { special type of immediate operand }
  228. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  229. OTVE_VECTOR_SAE = 1 shl 8;
  230. OTVE_VECTOR_ER = 1 shl 9;
  231. OTVE_VECTOR_ZERO = 1 shl 10;
  232. OTVE_VECTOR_WRITEMASK = 1 shl 11;
  233. OTVE_VECTOR_BCST = 1 shl 12;
  234. OTVE_VECTOR_BCST2 = 0;
  235. OTVE_VECTOR_BCST4 = 1 shl 4;
  236. OTVE_VECTOR_BCST8 = 1 shl 5;
  237. OTVE_VECTOR_BCST16 = 3 shl 4;
  238. OTVE_VECTOR_BCST32 = 1 shl 13;
  239. OTVE_VECTOR_RNSAE = OTVE_VECTOR_ER or 0;
  240. OTVE_VECTOR_RDSAE = OTVE_VECTOR_ER or 1 shl 6;
  241. OTVE_VECTOR_RUSAE = OTVE_VECTOR_ER or 1 shl 7;
  242. OTVE_VECTOR_RZSAE = OTVE_VECTOR_ER or 3 shl 6;
  243. OTVE_VECTOR_BCST_MASK = OTVE_VECTOR_BCST2 or OTVE_VECTOR_BCST4 or OTVE_VECTOR_BCST8 or OTVE_VECTOR_BCST16 or OTVE_VECTOR_BCST32;
  244. OTVE_VECTOR_ER_MASK = OTVE_VECTOR_RNSAE or OTVE_VECTOR_RDSAE or OTVE_VECTOR_RUSAE or OTVE_VECTOR_RZSAE;
  245. OTVE_VECTOR_MASK = OTVE_VECTOR_SAE or OTVE_VECTOR_ER or OTVE_VECTOR_ZERO or OTVE_VECTOR_WRITEMASK or OTVE_VECTOR_BCST;
  246. { Size of the instruction table converted by nasmconv.pas }
  247. {$if defined(x86_64)}
  248. instabentries = {$i x8664nop.inc}
  249. {$elseif defined(i386)}
  250. instabentries = {$i i386nop.inc}
  251. {$elseif defined(i8086)}
  252. instabentries = {$i i8086nop.inc}
  253. {$endif}
  254. maxinfolen = 11;
  255. type
  256. { What an instruction can change. Needed for optimizer and spilling code.
  257. Note: The order of this enumeration is should not be changed! }
  258. TInsChange = (Ch_None,
  259. {Read from a register}
  260. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  261. {write from a register}
  262. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  263. {read and write from/to a register}
  264. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  265. {modify the contents of a register with the purpose of using
  266. this changed content afterwards (add/sub/..., but e.g. not rep
  267. or movsd)}
  268. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  269. {read individual flag bits from the flags register}
  270. Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  271. {write individual flag bits to the flags register}
  272. Ch_WCarryFlag,Ch_WParityFlag,Ch_WAuxiliaryFlag,Ch_WZeroFlag,Ch_WSignFlag,Ch_WOverflowFlag,
  273. {set individual flag bits to 0 in the flags register}
  274. Ch_W0CarryFlag,Ch_W0ParityFlag,Ch_W0AuxiliaryFlag,Ch_W0ZeroFlag,Ch_W0SignFlag,Ch_W0OverflowFlag,
  275. {set individual flag bits to 1 in the flags register}
  276. Ch_W1CarryFlag,Ch_W1ParityFlag,Ch_W1AuxiliaryFlag,Ch_W1ZeroFlag,Ch_W1SignFlag,Ch_W1OverflowFlag,
  277. {write an undefined value to individual flag bits in the flags register}
  278. Ch_WUCarryFlag,Ch_WUParityFlag,Ch_WUAuxiliaryFlag,Ch_WUZeroFlag,Ch_WUSignFlag,Ch_WUOverflowFlag,
  279. {read and write flag bits}
  280. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  281. {more specialized flag bits (not considered part of NR_DEFAULTFLAGS by the compiler)}
  282. Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_W0IntFlag,Ch_W1IntFlag,
  283. {instruction reads flag bits, according to its condition (used by Jcc/SETcc/CMOVcc)}
  284. Ch_RFLAGScc,
  285. {read/write/read+write the entire flags/eflags/rflags register}
  286. Ch_RFlags, Ch_WFlags, Ch_RWFlags,
  287. Ch_FPU,
  288. Ch_Rop1, Ch_Wop1, Ch_RWop1, Ch_Mop1,
  289. Ch_Rop2, Ch_Wop2, Ch_RWop2, Ch_Mop2,
  290. Ch_Rop3, Ch_WOp3, Ch_RWOp3, Ch_Mop3,
  291. Ch_Rop4, Ch_WOp4, Ch_RWOp4, Ch_Mop4,
  292. { instruction doesn't read it's input register, in case both parameters
  293. are the same register (e.g. xor eax,eax; sub eax,eax; sbb eax,eax (reads flags only), etc.) }
  294. Ch_NoReadIfEqualRegs,
  295. Ch_RMemEDI,Ch_WMemEDI,
  296. Ch_All,
  297. { x86_64 registers }
  298. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  299. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  300. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  301. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI,
  302. { xmm register }
  303. Ch_RXMM0,
  304. Ch_WXMM0,
  305. Ch_RWXMM0,
  306. Ch_MXMM0
  307. );
  308. TInsProp = packed record
  309. Ch : set of TInsChange;
  310. end;
  311. TMemRefSizeInfo = (msiUnknown, msiUnsupported, msiNoSize, msiNoMemRef,
  312. msiMultiple, msiMultipleMinSize8, msiMultipleMinSize16, msiMultipleMinSize32,
  313. msiMultipleMinSize64, msiMultipleMinSize128, msiMultipleminSize256, msiMultipleMinSize512,
  314. msiMemRegSize, msiMemRegx16y32, msiMemRegx16y32z64, msiMemRegx32y64, msiMemRegx32y64z128, msiMemRegx64y128, msiMemRegx64y128z256,
  315. msiMemRegx64y256, msiMemRegx64y256z512,
  316. msiMem8, msiMem16, msiBMem16, msiMem32, msiBMem32, msiMem64, msiBMem64, msiMem128, msiMem256, msiMem512,
  317. msiXMem32, msiXMem64, msiYMem32, msiYMem64, msiZMem32, msiZMem64,
  318. msiVMemMultiple, msiVMemRegSize,
  319. msiMemRegConst128,msiMemRegConst256,msiMemRegConst512);
  320. TMemRefSizeInfoBCST = (msbUnknown, msbBCST16, msbBCST32, msbBCST64, msbMultiple);
  321. TMemRefSizeInfoBCSTType = (btUnknown, bt1to2, bt1to4, bt1to8, bt1to16, bt1to32);
  322. TEVEXTupleState = (etsUnknown, etsIsTuple, etsNotTuple);
  323. TConstSizeInfo = (csiUnknown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
  324. TInsTabMemRefSizeInfoRec = record
  325. MemRefSize : TMemRefSizeInfo;
  326. MemRefSizeBCST : TMemRefSizeInfoBCST;
  327. BCSTXMMMultiplicator : byte;
  328. ExistsSSEAVX : boolean;
  329. ConstSize : TConstSizeInfo;
  330. BCSTTypes : Set of TMemRefSizeInfoBCSTType;
  331. RegXMMSizeMask : int64;
  332. RegYMMSizeMask : int64;
  333. RegZMMSizeMask : int64;
  334. end;
  335. const
  336. MemRefMultiples: set of TMemRefSizeInfo = [msiMultiple, msiMultipleMinSize8,
  337. msiMultipleMinSize16, msiMultipleMinSize32,
  338. msiMultipleMinSize64, msiMultipleMinSize128,
  339. msiMultipleMinSize256, msiMultipleMinSize512,
  340. msiVMemMultiple];
  341. MemRefSizeInfoVMems: Set of TMemRefSizeInfo = [msiXMem32, msiXMem64, msiYMem32, msiYMem64,
  342. msiZMem32, msiZMem64,
  343. msiVMemMultiple, msiVMemRegSize];
  344. InsProp : array[tasmop] of TInsProp =
  345. {$if defined(x86_64)}
  346. {$i x8664pro.inc}
  347. {$elseif defined(i386)}
  348. {$i i386prop.inc}
  349. {$elseif defined(i8086)}
  350. {$i i8086prop.inc}
  351. {$endif}
  352. type
  353. TOperandOrder = (op_intel,op_att);
  354. {Instruction flags }
  355. tinsflag = (
  356. { please keep these in order and in sync with IF_SMASK }
  357. IF_SM, { size match first two operands }
  358. IF_SM2,
  359. IF_SB, { unsized operands can't be non-byte }
  360. IF_SW, { unsized operands can't be non-word }
  361. IF_SD, { unsized operands can't be nondword }
  362. { unsized argument spec }
  363. { please keep these in order and in sync with IF_ARMASK }
  364. IF_AR0, { SB, SW, SD applies to argument 0 }
  365. IF_AR1, { SB, SW, SD applies to argument 1 }
  366. IF_AR2, { SB, SW, SD applies to argument 2 }
  367. IF_PRIV, { it's a privileged instruction }
  368. IF_SMM, { it's only valid in SMM }
  369. IF_PROT, { it's protected mode only }
  370. IF_NOX86_64, { removed instruction in x86_64 }
  371. IF_UNDOC, { it's an undocumented instruction }
  372. IF_FPU, { it's an FPU instruction }
  373. IF_MMX, { it's an MMX instruction }
  374. { it's a 3DNow! instruction }
  375. IF_3DNOW,
  376. { it's a SSE (KNI, MMX2) instruction }
  377. IF_SSE,
  378. { SSE2 instructions }
  379. IF_SSE2,
  380. { SSE3 instructions }
  381. IF_SSE3,
  382. { SSE64 instructions }
  383. IF_SSE64,
  384. { SVM instructions }
  385. IF_SVM,
  386. { SSE4 instructions }
  387. IF_SSE4,
  388. IF_SSSE3,
  389. IF_SSE41,
  390. IF_SSE42,
  391. IF_MOVBE,
  392. IF_CLMUL,
  393. IF_AVX,
  394. IF_AVX2,
  395. IF_AVX512,
  396. IF_BMI1,
  397. IF_BMI2,
  398. { Intel ADX (Multi-Precision Add-Carry Instruction Extensions) }
  399. IF_ADX,
  400. IF_16BITONLY,
  401. IF_FMA,
  402. IF_FMA4,
  403. IF_TSX,
  404. IF_RAND,
  405. IF_XSAVE,
  406. IF_PREFETCHWT1,
  407. IF_SHA,
  408. IF_SHA512,
  409. IF_SM3NI, { instruction set SM3: ShangMi 3 hash function }
  410. IF_SM4NI, { instruction set SM4 }
  411. IF_GFNI,
  412. { mask for processor level }
  413. { please keep these in order and in sync with IF_PLEVEL }
  414. IF_8086, { 8086 instruction }
  415. IF_186, { 186+ instruction }
  416. IF_286, { 286+ instruction }
  417. IF_386, { 386+ instruction }
  418. IF_486, { 486+ instruction }
  419. IF_PENT, { Pentium instruction }
  420. IF_P6, { P6 instruction }
  421. IF_KATMAI, { Katmai instructions }
  422. IF_WILLAMETTE, { Willamette instructions }
  423. IF_PRESCOTT, { Prescott instructions }
  424. IF_X86_64,
  425. IF_SANDYBRIDGE, { Sandybridge-specific instruction }
  426. IF_NEC, { NEC V20/V30 instruction }
  427. { the following are not strictly part of the processor level, because
  428. they are never used standalone, but always in combination with a
  429. separate processor level flag. Therefore, they use bits outside of
  430. IF_PLEVEL, otherwise they would mess up the processor level they're
  431. used in combination with.
  432. The following combinations are currently used:
  433. [IF_AMD, IF_P6],
  434. [IF_CYRIX, IF_486],
  435. [IF_CYRIX, IF_PENT],
  436. [IF_CYRIX, IF_P6] }
  437. IF_CYRIX, { Cyrix, Centaur or VIA-specific instruction }
  438. IF_AMD, { AMD-specific instruction }
  439. { added flags }
  440. IF_PRE, { it's a prefix instruction }
  441. IF_PASS2, { if the instruction can change in a second pass }
  442. IF_IMM4, { immediate operand is a nibble (must be in range [0..15]) }
  443. IF_IMM3, { immediate operand is a triad (must be in range [0..7]) }
  444. { avx512 flags }
  445. IF_BCST2,
  446. IF_BCST4,
  447. IF_BCST8,
  448. IF_BCST16,
  449. IF_BCST32,
  450. IF_T2, { disp8 - tuple - 2 }
  451. IF_T4, { disp8 - tuple - 4 }
  452. IF_T8, { disp8 - tuple - 8 }
  453. IF_T1S, { disp8 - tuple - 1 scalar }
  454. IF_T1S8, { disp8 - tuple - 1 scalar byte }
  455. IF_T1S16, { disp8 - tuple - 1 scalar word }
  456. IF_T1F32,
  457. IF_T1F64,
  458. IF_TMDDUP,
  459. IF_TFV, { disp8 - tuple - full vector }
  460. IF_TFVM, { disp8 - tuple - full vector memory }
  461. IF_TQVM,
  462. IF_TMEM128,
  463. IF_THV,
  464. IF_THVM,
  465. IF_TOVM,
  466. IF_DISTINCT, { destination and source registers must be distinct }
  467. IF_DALL { destination, index and mask registers should be distinct (use together with IF_DISTINCT) }
  468. );
  469. tinsflags=set of tinsflag;
  470. const
  471. IF_SMASK=[IF_SM,IF_SM2,IF_SB,IF_SW,IF_SD];
  472. IF_ARMASK=[IF_AR0,IF_AR1,IF_AR2]; { mask for unsized argument spec }
  473. IF_PLEVEL=[IF_8086..IF_NEC]; { mask for processor level }
  474. IF_TUPLEMASK=[IF_T2..IF_TOVM]; { mask for AVX512 disp8-tuples }
  475. type
  476. tinsentry=packed record
  477. opcode : tasmop;
  478. ops : byte;
  479. optypes : array[0..max_operands-1] of int64;
  480. code : array[0..maxinfolen] of char;
  481. flags : tinsflags;
  482. end;
  483. pinsentry=^tinsentry;
  484. { alignment for operator }
  485. tai_align = class(tai_align_abstract)
  486. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  487. end;
  488. { taicpu }
  489. taicpu = class(tai_cpu_abstract_sym)
  490. opsize : topsize;
  491. constructor op_none(op : tasmop);
  492. constructor op_none(op : tasmop;_size : topsize);
  493. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  494. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  495. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  496. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  497. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  498. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  499. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  500. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  501. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  502. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  503. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  504. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  505. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  506. constructor op_reg_ref_reg(op : tasmop;_size : topsize;_op1 : tregister; const _op2 : treference;_op3 : tregister);
  507. constructor op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  508. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  509. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  510. constructor op_const_reg_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2, _op3, _op4 : tregister);
  511. { this is for Jmp instructions }
  512. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  513. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  514. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  515. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  516. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  517. procedure changeopsize(siz:topsize); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  518. function GetString:string;
  519. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  520. Early versions of the UnixWare assembler had a bug where some fpu instructions
  521. were reversed and GAS still keeps this "feature" for compatibility.
  522. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  523. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  524. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  525. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  526. when generating output for other assemblers, the opcodes must be fixed before writing them.
  527. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  528. because in case of smartlinking assembler is generated twice so at the second run wrong
  529. assembler is generated.
  530. }
  531. function FixNonCommutativeOpcodes: tasmop;
  532. private
  533. FOperandOrder : TOperandOrder;
  534. procedure init(_size : topsize); { this need to be called by all constructor }
  535. public
  536. { the next will reset all instructions that can change in pass 2 }
  537. procedure ResetPass1;override;
  538. procedure ResetPass2;override;
  539. function CheckIfValid:boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  540. function Pass1(objdata:TObjData):longint;override;
  541. procedure Pass2(objdata:TObjData);override;
  542. procedure SetOperandOrder(order:TOperandOrder);
  543. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  544. { register spilling code }
  545. function spilling_get_operation_type(opnr: longint): topertype;override;
  546. {$ifdef i8086}
  547. procedure loadsegsymbol(opidx:longint;s:tasmsymbol);
  548. {$endif i8086}
  549. property OperandOrder : TOperandOrder read FOperandOrder;
  550. private
  551. { next fields are filled in pass1, so pass2 is faster }
  552. insentry : PInsEntry;
  553. insoffset : longint;
  554. LastInsOffset : longint; { need to be public to be reset }
  555. inssize : shortint;
  556. EVEXTupleState: TEVEXTupleState; { AVX512 disp8*N }
  557. {$ifdef x86_64}
  558. rex : byte;
  559. {$endif x86_64}
  560. function InsEnd:longint;
  561. procedure create_ot(objdata:TObjData);
  562. function Matches(p:PInsEntry):boolean;
  563. function calcsize(p:PInsEntry):shortint;
  564. procedure gencode(objdata:TObjData);
  565. function NeedAddrPrefix(opidx:byte):boolean;
  566. function NeedAddrPrefix:boolean;
  567. procedure write0x66prefix(objdata:TObjData);
  568. procedure write0x67prefix(objdata:TObjData);
  569. procedure Swapoperands;
  570. function DistinctRegisters(aAll:boolean):boolean; { distinct vector registers? }
  571. function FindInsentry(objdata:TObjData):boolean;
  572. function CheckUseEVEX: boolean;
  573. procedure CheckEVEXTuple(const aInput:toper; aInsEntry: pInsentry; aIsVector128, aIsVector256, aIsVector512, aIsEVEXW1: boolean);
  574. end;
  575. function is_64_bit_ref(const ref:treference):boolean;
  576. function is_32_bit_ref(const ref:treference):boolean;
  577. function is_16_bit_ref(const ref:treference):boolean;
  578. function get_ref_address_size(const ref:treference):byte;
  579. function get_default_segment_of_ref(const ref:treference):tregister;
  580. procedure optimize_ref(var ref:treference; inlineasm: boolean);
  581. { returns true if opcode can be used with one memory operand without size }
  582. function NoMemorySizeRequired(opcode : TAsmOp) : Boolean;
  583. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  584. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  585. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  586. function MightHaveExtension(AsmOp : TAsmOp) : Boolean;
  587. procedure InitAsm;
  588. procedure DoneAsm;
  589. {*****************************************************************************
  590. External Symbol Chain
  591. used for agx86nsm and agx86int
  592. *****************************************************************************}
  593. type
  594. PExternChain = ^TExternChain;
  595. TExternChain = Record
  596. psym : pshortstring;
  597. is_defined : boolean;
  598. next : PExternChain;
  599. end;
  600. const
  601. FEC : PExternChain = nil;
  602. procedure AddSymbol(symname : string; defined : boolean);
  603. procedure FreeExternChainList;
  604. implementation
  605. uses
  606. cutils,
  607. globals,
  608. systems,
  609. itcpugas,
  610. cpuinfo;
  611. procedure AddSymbol(symname : string; defined : boolean);
  612. var
  613. EC : PExternChain;
  614. begin
  615. EC:=FEC;
  616. while assigned(EC) do
  617. begin
  618. if EC^.psym^=symname then
  619. begin
  620. if defined then
  621. EC^.is_defined:=true;
  622. exit;
  623. end;
  624. EC:=EC^.next;
  625. end;
  626. New(EC);
  627. EC^.next:=FEC;
  628. FEC:=EC;
  629. FEC^.psym:=stringdup(symname);
  630. FEC^.is_defined := defined;
  631. end;
  632. procedure FreeExternChainList;
  633. var
  634. EC : PExternChain;
  635. begin
  636. EC:=FEC;
  637. while assigned(EC) do
  638. begin
  639. FEC:=EC^.next;
  640. stringdispose(EC^.psym);
  641. Dispose(EC);
  642. EC:=FEC;
  643. end;
  644. end;
  645. {*****************************************************************************
  646. Instruction table
  647. *****************************************************************************}
  648. type
  649. TInsTabCache=array[TasmOp] of longint;
  650. PInsTabCache=^TInsTabCache;
  651. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  652. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  653. const
  654. {$if defined(x86_64)}
  655. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  656. {$elseif defined(i386)}
  657. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  658. {$elseif defined(i8086)}
  659. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  660. {$endif}
  661. var
  662. InsTabCache : PInsTabCache;
  663. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  664. const
  665. {$if defined(x86_64)}
  666. { Intel style operands ! }
  667. opsize_2_type:array[0..2,topsize] of int64=(
  668. (OT_NONE,
  669. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  670. OT_BITS16,OT_BITS32,OT_BITS64,
  671. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  672. OT_BITS64,
  673. OT_NEAR,OT_FAR,OT_SHORT,
  674. OT_NONE,
  675. OT_BITS128,
  676. OT_BITS256,
  677. OT_BITS512
  678. ),
  679. (OT_NONE,
  680. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  681. OT_BITS16,OT_BITS32,OT_BITS64,
  682. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  683. OT_BITS64,
  684. OT_NEAR,OT_FAR,OT_SHORT,
  685. OT_NONE,
  686. OT_BITS128,
  687. OT_BITS256,
  688. OT_BITS512
  689. ),
  690. (OT_NONE,
  691. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  692. OT_BITS16,OT_BITS32,OT_BITS64,
  693. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  694. OT_BITS64,
  695. OT_NEAR,OT_FAR,OT_SHORT,
  696. OT_NONE,
  697. OT_BITS128,
  698. OT_BITS256,
  699. OT_BITS512
  700. )
  701. );
  702. reg_ot_table : array[tregisterindex] of longint = (
  703. {$i r8664ot.inc}
  704. );
  705. {$elseif defined(i386)}
  706. { Intel style operands ! }
  707. opsize_2_type:array[0..2,topsize] of int64=(
  708. (OT_NONE,
  709. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  710. OT_BITS16,OT_BITS32,OT_BITS64,
  711. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  712. OT_BITS64,
  713. OT_NEAR,OT_FAR,OT_SHORT,
  714. OT_NONE,
  715. OT_BITS128,
  716. OT_BITS256,
  717. OT_BITS512
  718. ),
  719. (OT_NONE,
  720. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  721. OT_BITS16,OT_BITS32,OT_BITS64,
  722. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  723. OT_BITS64,
  724. OT_NEAR,OT_FAR,OT_SHORT,
  725. OT_NONE,
  726. OT_BITS128,
  727. OT_BITS256,
  728. OT_BITS512
  729. ),
  730. (OT_NONE,
  731. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  732. OT_BITS16,OT_BITS32,OT_BITS64,
  733. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  734. OT_BITS64,
  735. OT_NEAR,OT_FAR,OT_SHORT,
  736. OT_NONE,
  737. OT_BITS128,
  738. OT_BITS256,
  739. OT_BITS512
  740. )
  741. );
  742. reg_ot_table : array[tregisterindex] of longint = (
  743. {$i r386ot.inc}
  744. );
  745. {$elseif defined(i8086)}
  746. { Intel style operands ! }
  747. opsize_2_type:array[0..2,topsize] of int64=(
  748. (OT_NONE,
  749. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  750. OT_BITS16,OT_BITS32,OT_BITS64,
  751. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  752. OT_BITS64,
  753. OT_NEAR,OT_FAR,OT_SHORT,
  754. OT_NONE,
  755. OT_BITS128,
  756. OT_BITS256,
  757. OT_BITS512
  758. ),
  759. (OT_NONE,
  760. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  761. OT_BITS16,OT_BITS32,OT_BITS64,
  762. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  763. OT_BITS64,
  764. OT_NEAR,OT_FAR,OT_SHORT,
  765. OT_NONE,
  766. OT_BITS128,
  767. OT_BITS256,
  768. OT_BITS512
  769. ),
  770. (OT_NONE,
  771. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  772. OT_BITS16,OT_BITS32,OT_BITS64,
  773. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  774. OT_BITS64,
  775. OT_NEAR,OT_FAR,OT_SHORT,
  776. OT_NONE,
  777. OT_BITS128,
  778. OT_BITS256,
  779. OT_BITS512
  780. )
  781. );
  782. reg_ot_table : array[tregisterindex] of longint = (
  783. {$i r8086ot.inc}
  784. );
  785. {$endif}
  786. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  787. begin
  788. result := InsTabMemRefSizeInfoCache^[aAsmop];
  789. end;
  790. function MightHaveExtension(AsmOp : TAsmOp): Boolean;
  791. var
  792. i,j: LongInt;
  793. insentry: pinsentry;
  794. begin
  795. Result:=true;
  796. i:=InsTabCache^[AsmOp];
  797. if i>=0 then
  798. begin
  799. insentry:=@instab[i];
  800. while insentry^.opcode=AsmOp do
  801. begin
  802. for j:=0 to insentry^.ops-1 do
  803. begin
  804. if (insentry^.optypes[j] and OT_VECTOR_EXT)<>0 then
  805. exit;
  806. end;
  807. inc(i);
  808. if i>high(instab) then
  809. exit;
  810. insentry:=@instab[i];
  811. end;
  812. end;
  813. Result:=false;
  814. end;
  815. { Operation type for spilling code }
  816. type
  817. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  818. var
  819. operation_type_table : ^toperation_type_table;
  820. {****************************************************************************
  821. TAI_ALIGN
  822. ****************************************************************************}
  823. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  824. const
  825. { Updated according to
  826. Software Optimization Guide for AMD Family 15h Processors, Verison 3.08, January 2014
  827. and
  828. Intel 64 and IA-32 Architectures Software Developer’s Manual
  829. Volume 2B: Instruction Set Reference, N-Z, January 2015
  830. }
  831. {$ifndef i8086}
  832. alignarray_cmovcpus:array[0..10] of string[11]=(
  833. #$66#$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  834. #$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  835. #$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  836. #$0F#$1F#$84#$00#$00#$00#$00#$00,
  837. #$0F#$1F#$80#$00#$00#$00#$00,
  838. #$66#$0F#$1F#$44#$00#$00,
  839. #$0F#$1F#$44#$00#$00,
  840. #$0F#$1F#$40#$00,
  841. #$0F#$1F#$00,
  842. #$66#$90,
  843. #$90);
  844. {$endif i8086}
  845. {$ifdef i8086}
  846. alignarray:array[0..5] of string[8]=(
  847. #$90#$90#$90#$90#$90#$90#$90,
  848. #$90#$90#$90#$90#$90#$90,
  849. #$90#$90#$90#$90,
  850. #$90#$90#$90,
  851. #$90#$90,
  852. #$90);
  853. {$else i8086}
  854. alignarray:array[0..5] of string[8]=(
  855. #$8D#$B4#$26#$00#$00#$00#$00,
  856. #$8D#$B6#$00#$00#$00#$00,
  857. #$8D#$74#$26#$00,
  858. #$8D#$76#$00,
  859. #$89#$F6,
  860. #$90);
  861. {$endif i8086}
  862. var
  863. bufptr : pchar;
  864. j : longint;
  865. localsize: byte;
  866. begin
  867. inherited calculatefillbuf(buf,executable);
  868. if not(use_op) and executable then
  869. begin
  870. bufptr:=pchar(@buf);
  871. { fillsize may still be used afterwards, so don't modify }
  872. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  873. localsize:=fillsize;
  874. while (localsize>0) do
  875. begin
  876. {$ifndef i8086}
  877. if (CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) then
  878. begin
  879. for j:=low(alignarray_cmovcpus) to high(alignarray_cmovcpus) do
  880. if (localsize>=length(alignarray_cmovcpus[j])) then
  881. break;
  882. move(alignarray_cmovcpus[j][1],bufptr^,length(alignarray_cmovcpus[j]));
  883. inc(bufptr,length(alignarray_cmovcpus[j]));
  884. dec(localsize,length(alignarray_cmovcpus[j]));
  885. end
  886. else
  887. {$endif not i8086}
  888. begin
  889. for j:=low(alignarray) to high(alignarray) do
  890. if (localsize>=length(alignarray[j])) then
  891. break;
  892. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  893. inc(bufptr,length(alignarray[j]));
  894. dec(localsize,length(alignarray[j]));
  895. end
  896. end;
  897. end;
  898. calculatefillbuf:=pchar(@buf);
  899. end;
  900. {*****************************************************************************
  901. Taicpu Constructors
  902. *****************************************************************************}
  903. procedure taicpu.changeopsize(siz:topsize); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  904. begin
  905. opsize:=siz;
  906. end;
  907. procedure taicpu.init(_size : topsize);
  908. begin
  909. { default order is att }
  910. FOperandOrder:=op_att;
  911. segprefix:=NR_NO;
  912. opsize:=_size;
  913. insentry:=nil;
  914. LastInsOffset:=-1;
  915. InsOffset:=0;
  916. InsSize:=0;
  917. EVEXTupleState := etsUnknown;
  918. end;
  919. constructor taicpu.op_none(op : tasmop);
  920. begin
  921. inherited create(op);
  922. init(S_NO);
  923. end;
  924. constructor taicpu.op_none(op : tasmop;_size : topsize);
  925. begin
  926. inherited create(op);
  927. init(_size);
  928. end;
  929. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  930. begin
  931. inherited create(op);
  932. init(_size);
  933. ops:=1;
  934. loadreg(0,_op1);
  935. end;
  936. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  937. begin
  938. inherited create(op);
  939. init(_size);
  940. ops:=1;
  941. loadconst(0,_op1);
  942. end;
  943. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  944. begin
  945. inherited create(op);
  946. init(_size);
  947. ops:=1;
  948. loadref(0,_op1);
  949. end;
  950. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  951. begin
  952. inherited create(op);
  953. init(_size);
  954. ops:=2;
  955. loadreg(0,_op1);
  956. loadreg(1,_op2);
  957. end;
  958. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  959. begin
  960. inherited create(op);
  961. init(_size);
  962. ops:=2;
  963. loadreg(0,_op1);
  964. loadconst(1,_op2);
  965. end;
  966. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  967. begin
  968. inherited create(op);
  969. init(_size);
  970. ops:=2;
  971. loadreg(0,_op1);
  972. loadref(1,_op2);
  973. end;
  974. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  975. begin
  976. inherited create(op);
  977. init(_size);
  978. ops:=2;
  979. loadconst(0,_op1);
  980. loadreg(1,_op2);
  981. end;
  982. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  983. begin
  984. inherited create(op);
  985. init(_size);
  986. ops:=2;
  987. loadconst(0,_op1);
  988. loadconst(1,_op2);
  989. end;
  990. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  991. begin
  992. inherited create(op);
  993. init(_size);
  994. ops:=2;
  995. loadconst(0,_op1);
  996. loadref(1,_op2);
  997. end;
  998. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  999. begin
  1000. inherited create(op);
  1001. init(_size);
  1002. ops:=2;
  1003. loadref(0,_op1);
  1004. loadreg(1,_op2);
  1005. end;
  1006. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  1007. begin
  1008. inherited create(op);
  1009. init(_size);
  1010. ops:=3;
  1011. loadreg(0,_op1);
  1012. loadreg(1,_op2);
  1013. loadreg(2,_op3);
  1014. end;
  1015. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  1016. begin
  1017. inherited create(op);
  1018. init(_size);
  1019. ops:=3;
  1020. loadconst(0,_op1);
  1021. loadreg(1,_op2);
  1022. loadreg(2,_op3);
  1023. end;
  1024. constructor taicpu.op_reg_ref_reg(op : tasmop;_size : topsize;_op1 : tregister; const _op2 : treference;_op3 : tregister);
  1025. begin
  1026. inherited create(op);
  1027. init(_size);
  1028. ops:=3;
  1029. loadreg(0,_op1);
  1030. loadref(1,_op2);
  1031. loadreg(2,_op3);
  1032. end;
  1033. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  1034. begin
  1035. inherited create(op);
  1036. init(_size);
  1037. ops:=3;
  1038. loadref(0,_op1);
  1039. loadreg(1,_op2);
  1040. loadreg(2,_op3);
  1041. end;
  1042. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  1043. begin
  1044. inherited create(op);
  1045. init(_size);
  1046. ops:=3;
  1047. loadconst(0,_op1);
  1048. loadref(1,_op2);
  1049. loadreg(2,_op3);
  1050. end;
  1051. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  1052. begin
  1053. inherited create(op);
  1054. init(_size);
  1055. ops:=3;
  1056. loadconst(0,_op1);
  1057. loadreg(1,_op2);
  1058. loadref(2,_op3);
  1059. end;
  1060. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  1061. begin
  1062. inherited create(op);
  1063. init(_size);
  1064. ops:=3;
  1065. loadreg(0,_op1);
  1066. loadreg(1,_op2);
  1067. loadref(2,_op3);
  1068. end;
  1069. constructor taicpu.op_const_reg_reg_reg(op : tasmop; _size : topsize; _op1 : aint; _op2, _op3, _op4 : tregister);
  1070. begin
  1071. inherited create(op);
  1072. init(_size);
  1073. ops:=4;
  1074. loadconst(0,_op1);
  1075. loadreg(1,_op2);
  1076. loadreg(2,_op3);
  1077. loadreg(3,_op4);
  1078. end;
  1079. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  1080. begin
  1081. inherited create(op);
  1082. init(_size);
  1083. condition:=cond;
  1084. ops:=1;
  1085. loadsymbol(0,_op1,0);
  1086. end;
  1087. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  1088. begin
  1089. inherited create(op);
  1090. init(_size);
  1091. ops:=1;
  1092. loadsymbol(0,_op1,0);
  1093. end;
  1094. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  1095. begin
  1096. inherited create(op);
  1097. init(_size);
  1098. ops:=1;
  1099. loadsymbol(0,_op1,_op1ofs);
  1100. end;
  1101. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  1102. begin
  1103. inherited create(op);
  1104. init(_size);
  1105. ops:=2;
  1106. loadsymbol(0,_op1,_op1ofs);
  1107. loadreg(1,_op2);
  1108. end;
  1109. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  1110. begin
  1111. inherited create(op);
  1112. init(_size);
  1113. ops:=2;
  1114. loadsymbol(0,_op1,_op1ofs);
  1115. loadref(1,_op2);
  1116. end;
  1117. function taicpu.GetString:string;
  1118. var
  1119. i : longint;
  1120. s : string;
  1121. regnr: string;
  1122. addsize : boolean;
  1123. begin
  1124. s:='['+std_op2str[opcode];
  1125. for i:=0 to ops-1 do
  1126. begin
  1127. with oper[i]^ do
  1128. begin
  1129. if i=0 then
  1130. s:=s+' '
  1131. else
  1132. s:=s+',';
  1133. { type }
  1134. addsize:=false;
  1135. regnr := '';
  1136. if getregtype(reg) = R_MMREGISTER then
  1137. str(getsupreg(reg),regnr);
  1138. if (ot and OT_XMMREG)=OT_XMMREG then
  1139. s:=s+'xmmreg' + regnr
  1140. else
  1141. if (ot and OT_YMMREG)=OT_YMMREG then
  1142. s:=s+'ymmreg' + regnr
  1143. else
  1144. if (ot and OT_ZMMREG)=OT_ZMMREG then
  1145. s:=s+'zmmreg' + regnr
  1146. else
  1147. if (ot and OT_TMMREG)=OT_TMMREG then
  1148. s:=s+'tmmreg' + regnr
  1149. else
  1150. if (ot and OT_REG_EXTRA_MASK)=OT_MMXREG then
  1151. s:=s+'mmxreg'
  1152. else
  1153. if (ot and OT_REG_EXTRA_MASK)=OT_FPUREG then
  1154. s:=s+'fpureg'
  1155. else
  1156. if (ot and OT_KREG)=OT_KREG then
  1157. s:=s+'kreg'+ regnr
  1158. else
  1159. if (ot and OT_REGISTER)=OT_REGISTER then
  1160. begin
  1161. s:=s+'reg';
  1162. addsize:=true;
  1163. end
  1164. else
  1165. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1166. begin
  1167. s:=s+'imm';
  1168. addsize:=true;
  1169. end
  1170. else
  1171. if (ot and OT_MEMORY)=OT_MEMORY then
  1172. begin
  1173. s:=s+'mem';
  1174. addsize:=true;
  1175. end
  1176. else
  1177. s:=s+'???';
  1178. { size }
  1179. if addsize then
  1180. begin
  1181. if (ot and OT_BITS8)<>0 then
  1182. s:=s+'8'
  1183. else
  1184. if (ot and OT_BITS16)<>0 then
  1185. s:=s+'16'
  1186. else
  1187. if (ot and OT_BITS32)<>0 then
  1188. s:=s+'32'
  1189. else
  1190. if (ot and OT_BITS64)<>0 then
  1191. s:=s+'64'
  1192. else
  1193. if (ot and OT_BITS128)<>0 then
  1194. s:=s+'128'
  1195. else
  1196. if (ot and OT_BITS256)<>0 then
  1197. s:=s+'256'
  1198. else
  1199. if (ot and OT_BITS512)<>0 then
  1200. s:=s+'512'
  1201. else
  1202. s:=s+'??';
  1203. { signed }
  1204. if (ot and OT_SIGNED)<>0 then
  1205. s:=s+'s';
  1206. end;
  1207. if vopext <> 0 then
  1208. begin
  1209. str(vopext and $07, regnr);
  1210. if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  1211. s := s + ' {k' + regnr + '}';
  1212. if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  1213. s := s + ' {z}';
  1214. if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  1215. s := s + ' {sae}';
  1216. if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  1217. case vopext and OTVE_VECTOR_BCST_MASK of
  1218. OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  1219. OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  1220. OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  1221. OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  1222. OTVE_VECTOR_BCST32: s := s + ' {1to32}';
  1223. end;
  1224. if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  1225. case vopext and OTVE_VECTOR_ER_MASK of
  1226. OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  1227. OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  1228. OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  1229. OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  1230. end;
  1231. end;
  1232. end;
  1233. end;
  1234. GetString:=s+']';
  1235. end;
  1236. procedure taicpu.Swapoperands;
  1237. var
  1238. p : POper;
  1239. begin
  1240. { Fix the operands which are in AT&T style and we need them in Intel style }
  1241. case ops of
  1242. 0,1:
  1243. ;
  1244. 2 : begin
  1245. { 0,1 -> 1,0 }
  1246. p:=oper[0];
  1247. oper[0]:=oper[1];
  1248. oper[1]:=p;
  1249. end;
  1250. 3 : begin
  1251. { 0,1,2 -> 2,1,0 }
  1252. p:=oper[0];
  1253. oper[0]:=oper[2];
  1254. oper[2]:=p;
  1255. end;
  1256. 4 : begin
  1257. { 0,1,2,3 -> 3,2,1,0 }
  1258. p:=oper[0];
  1259. oper[0]:=oper[3];
  1260. oper[3]:=p;
  1261. p:=oper[1];
  1262. oper[1]:=oper[2];
  1263. oper[2]:=p;
  1264. end;
  1265. else
  1266. internalerror(201108141);
  1267. end;
  1268. end;
  1269. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  1270. begin
  1271. if FOperandOrder<>order then
  1272. begin
  1273. Swapoperands;
  1274. FOperandOrder:=order;
  1275. end;
  1276. end;
  1277. function taicpu.FixNonCommutativeOpcodes: tasmop;
  1278. begin
  1279. result:=opcode;
  1280. { we need ATT order }
  1281. SetOperandOrder(op_att);
  1282. if (
  1283. (ops=2) and
  1284. (oper[0]^.typ=top_reg) and
  1285. (oper[1]^.typ=top_reg) and
  1286. { if the first is ST and the second is also a register
  1287. it is necessarily ST1 .. ST7 }
  1288. ((oper[0]^.reg=NR_ST) or
  1289. (oper[0]^.reg=NR_ST0))
  1290. ) or
  1291. { ((ops=1) and
  1292. (oper[0]^.typ=top_reg) and
  1293. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  1294. (ops=0) then
  1295. begin
  1296. if opcode=A_FSUBR then
  1297. result:=A_FSUB
  1298. else if opcode=A_FSUB then
  1299. result:=A_FSUBR
  1300. else if opcode=A_FDIVR then
  1301. result:=A_FDIV
  1302. else if opcode=A_FDIV then
  1303. result:=A_FDIVR
  1304. else if opcode=A_FSUBRP then
  1305. result:=A_FSUBP
  1306. else if opcode=A_FSUBP then
  1307. result:=A_FSUBRP
  1308. else if opcode=A_FDIVRP then
  1309. result:=A_FDIVP
  1310. else if opcode=A_FDIVP then
  1311. result:=A_FDIVRP;
  1312. end;
  1313. if (
  1314. (ops=1) and
  1315. (oper[0]^.typ=top_reg) and
  1316. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  1317. (oper[0]^.reg<>NR_ST)
  1318. ) then
  1319. begin
  1320. if opcode=A_FSUBRP then
  1321. result:=A_FSUBP
  1322. else if opcode=A_FSUBP then
  1323. result:=A_FSUBRP
  1324. else if opcode=A_FDIVRP then
  1325. result:=A_FDIVP
  1326. else if opcode=A_FDIVP then
  1327. result:=A_FDIVRP;
  1328. end;
  1329. end;
  1330. {*****************************************************************************
  1331. Assembler
  1332. *****************************************************************************}
  1333. type
  1334. ea = packed record
  1335. sib_present : boolean;
  1336. bytes : byte;
  1337. size : byte;
  1338. modrm : byte;
  1339. sib : byte;
  1340. {$ifdef x86_64}
  1341. rex : byte;
  1342. {$endif x86_64}
  1343. end;
  1344. procedure taicpu.create_ot(objdata:TObjData);
  1345. {
  1346. this function will also fix some other fields which only needs to be once
  1347. }
  1348. var
  1349. i,l,relsize : longint;
  1350. currsym : TObjSymbol;
  1351. begin
  1352. if ops=0 then
  1353. exit;
  1354. { update oper[].ot field }
  1355. for i:=0 to ops-1 do
  1356. with oper[i]^ do
  1357. begin
  1358. case typ of
  1359. top_reg :
  1360. begin
  1361. ot:=reg_ot_table[findreg_by_number(reg)];
  1362. end;
  1363. top_ref :
  1364. begin
  1365. if (ref^.refaddr in [addr_no{$ifdef x86_64},addr_tpoff{$endif x86_64}{$ifdef i386},addr_ntpoff{$endif i386}])
  1366. {$ifdef i386}
  1367. or (
  1368. (ref^.refaddr in [addr_pic,addr_tlsgd]) and
  1369. ((ref^.base<>NR_NO) or (ref^.index<>NR_NO))
  1370. )
  1371. {$endif i386}
  1372. {$ifdef x86_64}
  1373. or (
  1374. (ref^.refaddr in [addr_pic,addr_pic_no_got,addr_tlsgd]) and
  1375. (ref^.base<>NR_NO)
  1376. )
  1377. {$endif x86_64}
  1378. then
  1379. begin
  1380. { create ot field }
  1381. if (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR = OT_REG_GPR) and
  1382. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1383. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG) or
  1384. (reg_ot_table[findreg_by_number(ref^.index)] = OT_ZMMREG)
  1385. ) then
  1386. // AVX2 - vector-memory-referenz (e.g. vgatherdpd xmm0, [rax xmm1], xmm2)
  1387. ot := (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR) or
  1388. (reg_ot_table[findreg_by_number(ref^.index)])
  1389. else if (ref^.base = NR_NO) and
  1390. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1391. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG) or
  1392. (reg_ot_table[findreg_by_number(ref^.index)] = OT_ZMMREG)
  1393. ) then
  1394. // AVX2 - vector-memory-referenz without base-register (e.g. vgatherdpd xmm0, [xmm1], xmm2)
  1395. ot := (OT_REG_GPR) or
  1396. (reg_ot_table[findreg_by_number(ref^.index)])
  1397. else if (ot and OT_SIZE_MASK)=0 then
  1398. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  1399. else
  1400. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  1401. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1402. ot:=ot or OT_MEM_OFFS;
  1403. { fix scalefactor }
  1404. if (ref^.index=NR_NO) then
  1405. ref^.scalefactor:=0
  1406. else
  1407. if (ref^.scalefactor=0) then
  1408. ref^.scalefactor:=1;
  1409. end
  1410. else
  1411. begin
  1412. { Jumps use a relative offset which can be 8bit,
  1413. for other opcodes we always need to generate the full
  1414. 32bit address }
  1415. if assigned(objdata) and
  1416. is_jmp then
  1417. begin
  1418. currsym:=objdata.symbolref(ref^.symbol);
  1419. l:=ref^.offset;
  1420. {$push}
  1421. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  1422. if assigned(currsym) then
  1423. inc(l,currsym.address);
  1424. {$pop}
  1425. { when it is a forward jump we need to compensate the
  1426. offset of the instruction since the previous time,
  1427. because the symbol address is then still using the
  1428. 'old-style' addressing.
  1429. For backwards jumps this is not required because the
  1430. address of the symbol is already adjusted to the
  1431. new offset }
  1432. if (l>InsOffset) and (LastInsOffset<>-1) then
  1433. inc(l,InsOffset-LastInsOffset);
  1434. { instruction size will then always become 2 (PFV) }
  1435. relsize:=(InsOffset+2)-l;
  1436. if (relsize>=-128) and (relsize<=127) and
  1437. (
  1438. not assigned(currsym) or
  1439. (currsym.objsection=objdata.currobjsec)
  1440. ) then
  1441. ot:=OT_IMM8 or OT_SHORT
  1442. else
  1443. {$ifdef i8086}
  1444. ot:=OT_IMM16 or OT_NEAR;
  1445. {$else i8086}
  1446. ot:=OT_IMM32 or OT_NEAR;
  1447. {$endif i8086}
  1448. end
  1449. else
  1450. {$ifdef i8086}
  1451. if opsize=S_FAR then
  1452. ot:=OT_IMM16 or OT_FAR
  1453. else
  1454. ot:=OT_IMM16 or OT_NEAR;
  1455. {$else i8086}
  1456. ot:=OT_IMM32 or OT_NEAR;
  1457. {$endif i8086}
  1458. end;
  1459. end;
  1460. top_local :
  1461. begin
  1462. if (ot and OT_SIZE_MASK)=0 then
  1463. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1464. else
  1465. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1466. end;
  1467. top_const :
  1468. begin
  1469. // if opcode is a SSE or AVX-instruction then we need a
  1470. // special handling (opsize can different from const-size)
  1471. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1472. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1473. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnknown])) then
  1474. begin
  1475. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1476. csiNoSize: ot := ot and OT_NON_SIZE or OT_IMMEDIATE;
  1477. csiMem8: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS8;
  1478. csiMem16: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS16;
  1479. csiMem32: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS32;
  1480. csiMem64: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS64;
  1481. else
  1482. ;
  1483. end;
  1484. end
  1485. else
  1486. begin
  1487. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1488. { further, allow ENTER, AAD and AAM with imm. operand }
  1489. if (opsize=S_NO) and not((i in [1,2,3])
  1490. or ((i=0) and (opcode in [A_ENTER]))
  1491. {$ifndef x86_64}
  1492. or ((i=0) and (opcode in [A_AAD,A_AAM]))
  1493. {$endif x86_64}
  1494. ) then
  1495. message(asmr_e_invalid_opcode_and_operand);
  1496. if
  1497. {$ifdef i8086}
  1498. (longint(val)>=-128) and (val<=127) then
  1499. {$else i8086}
  1500. (opsize<>S_W) and
  1501. (aint(val)>=-128) and (val<=127) then
  1502. {$endif not i8086}
  1503. ot:=OT_IMM8 or OT_SIGNED
  1504. else
  1505. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1506. if (val=1) and (i=1) then
  1507. ot := ot or OT_ONENESS;
  1508. end;
  1509. end;
  1510. top_none :
  1511. begin
  1512. { generated when there was an error in the
  1513. assembler reader. It never happends when generating
  1514. assembler }
  1515. end;
  1516. else
  1517. internalerror(200402266);
  1518. end;
  1519. end;
  1520. end;
  1521. function taicpu.InsEnd:longint;
  1522. begin
  1523. InsEnd:=InsOffset+InsSize;
  1524. end;
  1525. function taicpu.Matches(p:PInsEntry):boolean;
  1526. { * IF_SM stands for Size Match: any operand whose size is not
  1527. * explicitly specified by the template is `really' intended to be
  1528. * the same size as the first size-specified operand.
  1529. * Non-specification is tolerated in the input instruction, but
  1530. * _wrong_ specification is not.
  1531. *
  1532. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1533. * three-operand instructions such as SHLD: it implies that the
  1534. * first two operands must match in size, but that the third is
  1535. * required to be _unspecified_.
  1536. *
  1537. * IF_SB invokes Size Byte: operands with unspecified size in the
  1538. * template are really bytes, and so no non-byte specification in
  1539. * the input instruction will be tolerated. IF_SW similarly invokes
  1540. * Size Word, and IF_SD invokes Size Doubleword.
  1541. *
  1542. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1543. * that any operand with unspecified size in the template is
  1544. * required to have unspecified size in the instruction too...)
  1545. }
  1546. var
  1547. insot,
  1548. currot: int64;
  1549. i,j,asize,oprs : longint;
  1550. insflags:tinsflags;
  1551. vopext: int64;
  1552. siz : array[0..max_operands-1] of longint;
  1553. begin
  1554. result:=false;
  1555. { Check the opcode and operands }
  1556. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1557. exit;
  1558. {$ifdef i8086}
  1559. { On i8086, we need to skip the i386+ version of Jcc near, if the target
  1560. cpu is earlier than 386. There's another entry, later in the table for
  1561. i8086, which simulates it with i8086 instructions:
  1562. JNcc short +3
  1563. JMP near target }
  1564. if (p^.opcode=A_Jcc) and (current_settings.cputype<cpu_386) and
  1565. (IF_386 in p^.flags) then
  1566. exit;
  1567. {$endif i8086}
  1568. for i:=0 to p^.ops-1 do
  1569. begin
  1570. insot:=p^.optypes[i];
  1571. currot:=oper[i]^.ot;
  1572. { Check the operand flags }
  1573. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1574. exit;
  1575. // IGNORE VECTOR-MEMORY-SIZE
  1576. if insot and OT_TYPE_MASK = OT_MEMORY then
  1577. insot := insot and not(int64(OT_BITS128 or OT_BITS256 or OT_BITS512));
  1578. { Check if the passed operand size matches with one of
  1579. the supported operand sizes }
  1580. if ((insot and OT_SIZE_MASK)<>0) and
  1581. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1582. exit;
  1583. { "far" matches only with "far" }
  1584. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1585. exit;
  1586. end;
  1587. { Check operand sizes }
  1588. insflags:=p^.flags;
  1589. if (insflags*IF_SMASK)<>[] then
  1590. begin
  1591. { as default an untyped size can get all the sizes, this is different
  1592. from nasm, but else we need to do a lot checking which opcodes want
  1593. size or not with the automatic size generation }
  1594. asize:=-1;
  1595. if IF_SB in insflags then
  1596. asize:=OT_BITS8
  1597. else if IF_SW in insflags then
  1598. asize:=OT_BITS16
  1599. else if IF_SD in insflags then
  1600. asize:=OT_BITS32;
  1601. if insflags*IF_ARMASK<>[] then
  1602. begin
  1603. siz[0]:=-1;
  1604. siz[1]:=-1;
  1605. siz[2]:=-1;
  1606. if IF_AR0 in insflags then
  1607. siz[0]:=asize
  1608. else if IF_AR1 in insflags then
  1609. siz[1]:=asize
  1610. else if IF_AR2 in insflags then
  1611. siz[2]:=asize
  1612. else
  1613. internalerror(2017092101);
  1614. end
  1615. else
  1616. begin
  1617. siz[0]:=asize;
  1618. siz[1]:=asize;
  1619. siz[2]:=asize;
  1620. end;
  1621. if insflags*[IF_SM,IF_SM2]<>[] then
  1622. begin
  1623. if IF_SM2 in insflags then
  1624. oprs:=2
  1625. else
  1626. oprs:=p^.ops;
  1627. for i:=0 to oprs-1 do
  1628. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1629. begin
  1630. for j:=0 to oprs-1 do
  1631. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1632. break;
  1633. end;
  1634. end
  1635. else
  1636. oprs:=2;
  1637. { Check operand sizes }
  1638. for i:=0 to p^.ops-1 do
  1639. begin
  1640. insot:=p^.optypes[i];
  1641. currot:=oper[i]^.ot;
  1642. if ((insot and OT_SIZE_MASK)=0) and
  1643. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1644. { Immediates can always include smaller size }
  1645. ((currot and OT_IMMEDIATE)=0) and
  1646. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1647. exit;
  1648. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1649. exit;
  1650. end;
  1651. end;
  1652. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1653. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1654. begin
  1655. for i:=0 to p^.ops-1 do
  1656. begin
  1657. insot:=p^.optypes[i];
  1658. currot:=oper[i]^.ot;
  1659. { Check the operand flags }
  1660. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1661. exit;
  1662. { Check if the passed operand size matches with one of
  1663. the supported operand sizes }
  1664. if ((insot and OT_SIZE_MASK)<>0) and
  1665. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1666. exit;
  1667. end;
  1668. end;
  1669. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1670. begin
  1671. for i:=0 to p^.ops-1 do
  1672. begin
  1673. // check vectoroperand-extention e.g. {k1} {z}
  1674. vopext := 0;
  1675. if (oper[i]^.vopext and OTVE_VECTOR_WRITEMASK) = OTVE_VECTOR_WRITEMASK then
  1676. begin
  1677. vopext := vopext or OT_VECTORMASK;
  1678. if (oper[i]^.vopext and OTVE_VECTOR_ZERO) = OTVE_VECTOR_ZERO then
  1679. vopext := vopext or OT_VECTORZERO;
  1680. end;
  1681. if (oper[i]^.vopext and OTVE_VECTOR_BCST) = OTVE_VECTOR_BCST then
  1682. begin
  1683. vopext := vopext or OT_VECTORBCST;
  1684. if (InsTabMemRefSizeInfoCache^[opcode].BCSTTypes <> []) then
  1685. begin
  1686. // any opcodes needs a special handling
  1687. // default broadcast calculation is
  1688. // bmem32
  1689. // xmmreg: {1to4}
  1690. // ymmreg: {1to8}
  1691. // zmmreg: {1to16}
  1692. // bmem64
  1693. // xmmreg: {1to2}
  1694. // ymmreg: {1to4}
  1695. // zmmreg: {1to8}
  1696. // in any opcodes not exists a mmregister
  1697. // e.g. vfpclasspd k1, [RAX] {1to8}, 0
  1698. // =>> check flags
  1699. case oper[i]^.vopext and (OTVE_VECTOR_BCST2 or OTVE_VECTOR_BCST4 or OTVE_VECTOR_BCST8 or OTVE_VECTOR_BCST16 or OTVE_VECTOR_BCST32) of
  1700. OTVE_VECTOR_BCST2: if not(IF_BCST2 in p^.flags) then exit;
  1701. OTVE_VECTOR_BCST4: if not(IF_BCST4 in p^.flags) then exit;
  1702. OTVE_VECTOR_BCST8: if not(IF_BCST8 in p^.flags) then exit;
  1703. OTVE_VECTOR_BCST16: if not(IF_BCST16 in p^.flags) then exit;
  1704. OTVE_VECTOR_BCST32: if not(IF_BCST32 in p^.flags) then exit;
  1705. else exit;
  1706. end;
  1707. end;
  1708. end;
  1709. if (oper[i]^.vopext and OTVE_VECTOR_ER) = OTVE_VECTOR_ER then
  1710. vopext := vopext or OT_VECTORER;
  1711. if (oper[i]^.vopext and OTVE_VECTOR_SAE) = OTVE_VECTOR_SAE then
  1712. vopext := vopext or OT_VECTORSAE;
  1713. if p^.optypes[i] and vopext <> vopext then
  1714. exit;
  1715. end;
  1716. end;
  1717. result:=true;
  1718. end;
  1719. procedure taicpu.ResetPass1;
  1720. begin
  1721. { we need to reset everything here, because the choosen insentry
  1722. can be invalid for a new situation where the previously optimized
  1723. insentry is not correct }
  1724. InsEntry:=nil;
  1725. InsSize:=0;
  1726. LastInsOffset:=-1;
  1727. end;
  1728. procedure taicpu.ResetPass2;
  1729. begin
  1730. { we are here in a second pass, check if the instruction can be optimized }
  1731. if assigned(InsEntry) and
  1732. (IF_PASS2 in InsEntry^.flags) then
  1733. begin
  1734. InsEntry:=nil;
  1735. InsSize:=0;
  1736. end;
  1737. LastInsOffset:=-1;
  1738. end;
  1739. function taicpu.CheckIfValid:boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  1740. begin
  1741. result:=FindInsEntry(nil);
  1742. end;
  1743. function taicpu.DistinctRegisters(aAll:boolean):boolean; { distinct vector registers? }
  1744. var i : longint;
  1745. nr : array[0..max_operands-1] of shortint;
  1746. begin
  1747. result:=true;
  1748. if ops>1 then
  1749. begin
  1750. { avoid error about uninitialized variable }
  1751. fillchar(nr,sizeof(nr),0);
  1752. for i:=0 to ops-1 do
  1753. begin
  1754. with oper[i]^ do
  1755. begin
  1756. nr[i]:=-i-1;
  1757. if getregtype(reg) = R_MMREGISTER then
  1758. nr[i]:=getsupreg(reg);
  1759. if aAll and (nr[i]<0) then
  1760. if (ot and (OT_REGNORM or otf_reg_gpr))=(OT_REGNORM or otf_reg_gpr) then
  1761. if (ot and (otf_reg_xmm or otf_reg_ymm or otf_reg_zmm)) > 0 then
  1762. nr[i]:=getsupreg(ref^.index);
  1763. end;
  1764. end;
  1765. if nr[0]=nr[1] then result:=false;
  1766. if ops>2 then
  1767. begin
  1768. if nr[0]=nr[2] then result:=false;
  1769. if aAll then if nr[1]=nr[2] then result:=false;
  1770. end;
  1771. end;
  1772. end;
  1773. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1774. var
  1775. i : longint;
  1776. begin
  1777. result:=false;
  1778. { Things which may only be done once, not when a second pass is done to
  1779. optimize }
  1780. if (Insentry=nil) or (IF_PASS2 in InsEntry^.flags) then
  1781. begin
  1782. current_filepos:=fileinfo;
  1783. { We need intel style operands }
  1784. SetOperandOrder(op_intel);
  1785. { create the .ot fields }
  1786. create_ot(objdata);
  1787. { set the file postion }
  1788. end
  1789. else
  1790. begin
  1791. { we've already an insentry so it's valid }
  1792. result:=true;
  1793. exit;
  1794. end;
  1795. { Lookup opcode in the table }
  1796. InsSize:=-1;
  1797. i:=instabcache^[opcode];
  1798. if i=-1 then
  1799. begin
  1800. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1801. exit;
  1802. end;
  1803. insentry:=@instab[i];
  1804. while (insentry^.opcode=opcode) do
  1805. begin
  1806. if matches(insentry) then
  1807. begin
  1808. if (IF_DISTINCT in insentry^.flags) then
  1809. if not DistinctRegisters(IF_DALL in insentry^.flags) then
  1810. begin
  1811. if IF_DALL in insentry^.flags then
  1812. Message1(asmw_e_destination_index_mask_registers_should_be_distinct,GetString)
  1813. else
  1814. Message1(asmw_e_destination_and_source_registers_must_be_distinct,GetString);
  1815. exit; { unacceptable register combination (shoud be distinct) }
  1816. end;
  1817. result:=true;
  1818. exit;
  1819. end;
  1820. inc(i);
  1821. if i>high(instab) then
  1822. break; { not found and run out of entries to test for, jump into error report }
  1823. insentry:=@instab[i];
  1824. end;
  1825. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1826. { No instruction found, set insentry to nil and inssize to -1 }
  1827. insentry:=nil;
  1828. inssize:=-1;
  1829. end;
  1830. function taicpu.CheckUseEVEX: boolean;
  1831. var
  1832. i: integer;
  1833. begin
  1834. result := false;
  1835. for i := 0 to ops - 1 do
  1836. begin
  1837. if (oper[i]^.typ=top_reg) and
  1838. (getregtype(oper[i]^.reg) = R_MMREGISTER) then
  1839. if getsupreg(oper[i]^.reg)>=16 then
  1840. result := true;
  1841. if (oper[i]^.vopext and OTVE_VECTOR_MASK) <> 0 then
  1842. result := true;
  1843. end;
  1844. end;
  1845. procedure taicpu.CheckEVEXTuple(const aInput:toper; aInsEntry: pInsentry; aIsVector128, aIsVector256, aIsVector512, aIsEVEXW1: boolean);
  1846. var
  1847. i: integer;
  1848. tuplesize: integer;
  1849. memsize: integer;
  1850. begin
  1851. if EVEXTupleState = etsUnknown then
  1852. begin
  1853. EVEXTupleState := etsNotTuple;
  1854. if aInsEntry^.Flags * IF_TUPLEMASK <> [] then
  1855. begin
  1856. tuplesize := 0;
  1857. if IF_TFV in aInsEntry^.Flags then
  1858. begin
  1859. for i := 0 to aInsEntry^.ops - 1 do
  1860. if (aInsEntry^.optypes[i] and OT_BMEM16 = OT_BMEM16) then
  1861. begin
  1862. tuplesize := 2;
  1863. break;
  1864. end
  1865. else if (aInsEntry^.optypes[i] and OT_BMEM32 = OT_BMEM32) then
  1866. begin
  1867. tuplesize := 4;
  1868. break;
  1869. end
  1870. else if (aInsEntry^.optypes[i] and OT_BMEM64 = OT_BMEM64) then
  1871. begin
  1872. tuplesize := 8;
  1873. break;
  1874. end
  1875. else if (aInsEntry^.optypes[i] and OT_MEMORY = OT_MEMORY) then
  1876. begin
  1877. if aIsVector512 then tuplesize := 64
  1878. else if aIsVector256 then tuplesize := 32
  1879. else tuplesize := 16;
  1880. break;
  1881. end
  1882. else if (aInsEntry^.optypes[i] and OT_REGNORM = OT_REGMEM) then
  1883. begin
  1884. if aIsVector512 then tuplesize := 64
  1885. else if aIsVector256 then tuplesize := 32
  1886. else tuplesize := 16;
  1887. break;
  1888. end;
  1889. end
  1890. else if IF_THV in aInsEntry^.Flags then
  1891. begin
  1892. for i := 0 to aInsEntry^.ops - 1 do
  1893. if (aInsEntry^.optypes[i] and OT_BMEM32 = OT_BMEM32) then
  1894. begin
  1895. tuplesize := 4;
  1896. break;
  1897. end
  1898. else if (aInsEntry^.optypes[i] and OT_REGNORM = OT_REGMEM) then
  1899. begin
  1900. if aIsVector512 then tuplesize := 32
  1901. else if aIsVector256 then tuplesize := 16
  1902. else tuplesize := 8;
  1903. break;
  1904. end
  1905. end
  1906. else if IF_TFVM in aInsEntry^.Flags then
  1907. begin
  1908. if aIsVector512 then tuplesize := 64
  1909. else if aIsVector256 then tuplesize := 32
  1910. else tuplesize := 16;
  1911. end
  1912. else
  1913. begin
  1914. memsize := 0;
  1915. for i := 0 to aInsEntry^.ops - 1 do
  1916. begin
  1917. if aInsEntry^.optypes[i] and (OT_REGNORM or OT_MEMORY) = OT_REGMEM then
  1918. begin
  1919. case aInsEntry^.optypes[i] and (OT_BITS16 or OT_BITS32 or OT_BITS64) of
  1920. OT_BITS16: begin
  1921. memsize := 16;
  1922. break;
  1923. end;
  1924. OT_BITS32: begin
  1925. memsize := 32;
  1926. break;
  1927. end;
  1928. OT_BITS64: begin
  1929. memsize := 64;
  1930. break;
  1931. end;
  1932. end;
  1933. end
  1934. else
  1935. case aInsEntry^.optypes[i] and (OT_MEM8 or OT_MEM16 or OT_MEM32 or OT_MEM64) of
  1936. OT_MEM8: begin
  1937. memsize := 8;
  1938. break;
  1939. end;
  1940. OT_MEM16: begin
  1941. memsize := 16;
  1942. break;
  1943. end;
  1944. OT_MEM32: begin
  1945. memsize := 32;
  1946. break;
  1947. end;
  1948. OT_MEM64: //if aIsEVEXW1 then
  1949. begin
  1950. memsize := 64;
  1951. break;
  1952. end;
  1953. end;
  1954. end;
  1955. if IF_T1S in aInsEntry^.Flags then
  1956. begin
  1957. case memsize of
  1958. 8: tuplesize := 1;
  1959. 16: tuplesize := 2;
  1960. else if aIsEVEXW1 then tuplesize := 8
  1961. else tuplesize := 4;
  1962. end;
  1963. end
  1964. else if IF_T1S8 in aInsEntry^.Flags then tuplesize := 1
  1965. else if IF_T1S16 in aInsEntry^.Flags then tuplesize := 2
  1966. else if IF_T1F32 in aInsEntry^.Flags then tuplesize := 4
  1967. else if IF_T1F64 in aInsEntry^.Flags then tuplesize := 8
  1968. else if IF_T2 in aInsEntry^.Flags then
  1969. begin
  1970. case aIsEVEXW1 of
  1971. false: tuplesize := 8;
  1972. else if aIsVector256 or aIsVector512 then tuplesize := 16;
  1973. end;
  1974. end
  1975. else if IF_T4 in aInsEntry^.Flags then
  1976. begin
  1977. case aIsEVEXW1 of
  1978. false: if aIsVector256 or aIsVector512 then tuplesize := 16;
  1979. else if aIsVector512 then tuplesize := 32;
  1980. end;
  1981. end
  1982. else if IF_T8 in aInsEntry^.Flags then
  1983. begin
  1984. case aIsEVEXW1 of
  1985. false: if aIsVector512 then tuplesize := 32;
  1986. else
  1987. Internalerror(2019081013);
  1988. end;
  1989. end
  1990. else if IF_THVM in aInsEntry^.Flags then
  1991. begin
  1992. tuplesize := 8; // default 128bit-vectorlength
  1993. if aIsVector256 then tuplesize := 16
  1994. else if aIsVector512 then tuplesize := 32;
  1995. end
  1996. else if IF_TQVM in aInsEntry^.Flags then
  1997. begin
  1998. tuplesize := 4; // default 128bit-vectorlength
  1999. if aIsVector256 then tuplesize := 8
  2000. else if aIsVector512 then tuplesize := 16;
  2001. end
  2002. else if IF_TOVM in aInsEntry^.Flags then
  2003. begin
  2004. tuplesize := 2; // default 128bit-vectorlength
  2005. if aIsVector256 then tuplesize := 4
  2006. else if aIsVector512 then tuplesize := 8;
  2007. end
  2008. else if IF_TMEM128 in aInsEntry^.Flags then tuplesize := 16
  2009. else if IF_TMDDUP in aInsEntry^.Flags then
  2010. begin
  2011. tuplesize := 8; // default 128bit-vectorlength
  2012. if aIsVector256 then tuplesize := 32
  2013. else if aIsVector512 then tuplesize := 64;
  2014. end;
  2015. end;
  2016. if tuplesize > 0 then
  2017. begin
  2018. if aInput.typ = top_ref then
  2019. begin
  2020. if aInput.ref^.base <> NR_NO then
  2021. begin
  2022. if (aInput.ref^.offset <> 0) and
  2023. ((aInput.ref^.offset mod tuplesize) = 0) and
  2024. (abs(aInput.ref^.offset) div tuplesize <= 127) then
  2025. begin
  2026. aInput.ref^.offset := aInput.ref^.offset div tuplesize;
  2027. EVEXTupleState := etsIsTuple;
  2028. end;
  2029. end;
  2030. end;
  2031. end;
  2032. end;
  2033. end;
  2034. end;
  2035. function taicpu.Pass1(objdata:TObjData):longint;
  2036. begin
  2037. Pass1:=0;
  2038. { Save the old offset and set the new offset }
  2039. InsOffset:=ObjData.CurrObjSec.Size;
  2040. { Error? }
  2041. if (Insentry=nil) and (InsSize=-1) then
  2042. exit;
  2043. { set the file postion }
  2044. current_filepos:=fileinfo;
  2045. { Get InsEntry }
  2046. if FindInsEntry(ObjData) then
  2047. begin
  2048. { Calculate instruction size }
  2049. InsSize:=calcsize(insentry);
  2050. if segprefix<>NR_NO then
  2051. inc(InsSize);
  2052. if NeedAddrPrefix then
  2053. inc(InsSize);
  2054. { Fix opsize if size if forced }
  2055. if insentry^.flags*[IF_SB,IF_SW,IF_SD]<>[] then
  2056. begin
  2057. if insentry^.flags*IF_ARMASK=[] then
  2058. begin
  2059. if IF_SB in insentry^.flags then
  2060. begin
  2061. if opsize=S_NO then
  2062. opsize:=S_B;
  2063. end
  2064. else if IF_SW in insentry^.flags then
  2065. begin
  2066. if opsize=S_NO then
  2067. opsize:=S_W;
  2068. end
  2069. else if IF_SD in insentry^.flags then
  2070. begin
  2071. if opsize=S_NO then
  2072. opsize:=S_L;
  2073. end;
  2074. end;
  2075. end;
  2076. LastInsOffset:=InsOffset;
  2077. Pass1:=InsSize;
  2078. exit;
  2079. end;
  2080. LastInsOffset:=-1;
  2081. end;
  2082. const
  2083. segprefixes: array[NR_ES..NR_GS] of Byte=(
  2084. // es cs ss ds fs gs
  2085. $26, $2E, $36, $3E, $64, $65
  2086. );
  2087. procedure taicpu.Pass2(objdata:TObjData);
  2088. begin
  2089. { error in pass1 ? }
  2090. if insentry=nil then
  2091. exit;
  2092. current_filepos:=fileinfo;
  2093. { Segment override }
  2094. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  2095. begin
  2096. {$ifdef i8086}
  2097. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) and
  2098. ((segprefix=NR_FS) or (segprefix=NR_GS)) then
  2099. Message(asmw_e_instruction_not_supported_by_cpu);
  2100. {$endif i8086}
  2101. objdata.writebytes(segprefixes[segprefix],1);
  2102. { fix the offset for GenNode }
  2103. inc(InsOffset);
  2104. end
  2105. else if segprefix<>NR_NO then
  2106. InternalError(201001071);
  2107. { Address size prefix? }
  2108. if NeedAddrPrefix then
  2109. begin
  2110. write0x67prefix(objdata);
  2111. { fix the offset for GenNode }
  2112. inc(InsOffset);
  2113. end;
  2114. { Generate the instruction }
  2115. GenCode(objdata);
  2116. end;
  2117. function is_64_bit_ref(const ref:treference):boolean;
  2118. begin
  2119. {$if defined(x86_64)}
  2120. result:=not is_32_bit_ref(ref);
  2121. {$elseif defined(i386) or defined(i8086)}
  2122. result:=false;
  2123. {$endif}
  2124. end;
  2125. function is_32_bit_ref(const ref:treference):boolean;
  2126. begin
  2127. {$if defined(x86_64)}
  2128. result:=(ref.refaddr=addr_no) and
  2129. (ref.base<>NR_RIP) and
  2130. (
  2131. ((ref.index<>NR_NO) and (getsubreg(ref.index)=R_SUBD)) or
  2132. ((ref.base<>NR_NO) and (getsubreg(ref.base)=R_SUBD))
  2133. );
  2134. {$elseif defined(i386) or defined(i8086)}
  2135. result:=not is_16_bit_ref(ref);
  2136. {$endif}
  2137. end;
  2138. function is_16_bit_ref(const ref:treference):boolean;
  2139. var
  2140. ir,br : Tregister;
  2141. isub,bsub : tsubregister;
  2142. begin
  2143. if (ref.index<>NR_NO) and (getregtype(ref.index)=R_MMREGISTER) then
  2144. exit(false);
  2145. ir:=ref.index;
  2146. br:=ref.base;
  2147. isub:=getsubreg(ir);
  2148. bsub:=getsubreg(br);
  2149. { it's a direct address }
  2150. if (br=NR_NO) and (ir=NR_NO) then
  2151. begin
  2152. {$ifdef i8086}
  2153. result:=true;
  2154. {$else i8086}
  2155. result:=false;
  2156. {$endif}
  2157. end
  2158. else
  2159. { it's an indirection }
  2160. begin
  2161. result := ((ir<>NR_NO) and (isub=R_SUBW)) or
  2162. ((br<>NR_NO) and (bsub=R_SUBW));
  2163. end;
  2164. end;
  2165. function get_ref_address_size(const ref:treference):byte;
  2166. begin
  2167. if is_64_bit_ref(ref) then
  2168. result:=64
  2169. else if is_32_bit_ref(ref) then
  2170. result:=32
  2171. else if is_16_bit_ref(ref) then
  2172. result:=16
  2173. else
  2174. internalerror(2017101601);
  2175. end;
  2176. function get_default_segment_of_ref(const ref:treference):tregister;
  2177. begin
  2178. { for 16-bit registers, we allow base and index to be swapped, that's
  2179. why we also we check whether ref.index=NR_BP. For 32-bit registers,
  2180. however, index=NR_EBP is encoded differently than base=NR_EBP and has
  2181. a different default segment. }
  2182. if (ref.base=NR_BP) or (ref.index=NR_BP) or
  2183. (ref.base=NR_EBP) or (ref.base=NR_ESP)
  2184. {$ifdef x86_64}
  2185. or (ref.base=NR_RBP) or (ref.base=NR_RSP)
  2186. {$endif x86_64}
  2187. then
  2188. result:=NR_SS
  2189. else
  2190. result:=NR_DS;
  2191. end;
  2192. procedure optimize_ref(var ref:treference; inlineasm: boolean);
  2193. var
  2194. ss_equals_ds: boolean;
  2195. tmpreg: TRegister;
  2196. begin
  2197. {$ifdef x86_64}
  2198. { x86_64 in long mode ignores all segment base, limit and access rights
  2199. checks for the DS, ES and SS registers, so we can set ss_equals_ds to
  2200. true (and thus, perform stronger optimizations on the reference),
  2201. regardless of whether this is inline asm or not (so, even if the user
  2202. is doing tricks by loading different values into DS and SS, it still
  2203. doesn't matter while the processor is in long mode) }
  2204. ss_equals_ds:=True;
  2205. {$else x86_64}
  2206. { for i8086 and i386 inline asm, we assume SS<>DS, even if we're
  2207. compiling for a memory model, where SS=DS, because the user might be
  2208. doing something tricky with the segment registers (and may have
  2209. temporarily set them differently) }
  2210. if inlineasm then
  2211. ss_equals_ds:=False
  2212. else
  2213. ss_equals_ds:=segment_regs_equal(NR_DS,NR_SS);
  2214. {$endif x86_64}
  2215. { remove redundant segment overrides }
  2216. if (ref.segment<>NR_NO) and
  2217. ((inlineasm and (ref.segment=get_default_segment_of_ref(ref))) or
  2218. ((not inlineasm) and (segment_regs_equal(ref.segment,get_default_segment_of_ref(ref))))) then
  2219. ref.segment:=NR_NO;
  2220. if not is_16_bit_ref(ref) then
  2221. begin
  2222. { Switching index to base position gives shorter assembler instructions.
  2223. Converting index*2 to base+index also gives shorter instructions. }
  2224. if (ref.base=NR_NO) and (ref.index<>NR_NO) and (ref.scalefactor<=2) and
  2225. (ss_equals_ds or (ref.segment<>NR_NO) or (ref.index<>NR_EBP))
  2226. { do not mess with tls references, they have the (,reg,1) format on purpose
  2227. else the linker cannot resolve/replace them }
  2228. {$ifdef i386} and (ref.refaddr<>addr_tlsgd) {$endif i386} then
  2229. begin
  2230. ref.base:=ref.index;
  2231. if ref.scalefactor=2 then
  2232. ref.scalefactor:=1
  2233. else
  2234. begin
  2235. ref.index:=NR_NO;
  2236. ref.scalefactor:=0;
  2237. end;
  2238. end;
  2239. { Switching rBP+reg to reg+rBP sometimes gives shorter instructions (if there's no offset)
  2240. On x86_64 this also works for switching r13+reg to reg+r13. }
  2241. if ((ref.base=NR_EBP) {$ifdef x86_64}or (ref.base=NR_RBP) or (ref.base=NR_R13) or (ref.base=NR_R13D){$endif}) and
  2242. (ref.index<>NR_NO) and
  2243. (ref.index<>NR_EBP) and {$ifdef x86_64}(ref.index<>NR_RBP) and (ref.index<>NR_R13) and (ref.index<>NR_R13D) and{$endif}
  2244. (ref.scalefactor<=1) and (ref.offset=0) and (ref.refaddr=addr_no) and
  2245. (ss_equals_ds or (ref.segment<>NR_NO)) then
  2246. begin
  2247. tmpreg:=ref.base;
  2248. ref.base:=ref.index;
  2249. ref.index:=tmpreg;
  2250. end;
  2251. end;
  2252. { remove redundant segment overrides again }
  2253. if (ref.segment<>NR_NO) and
  2254. ((inlineasm and (ref.segment=get_default_segment_of_ref(ref))) or
  2255. ((not inlineasm) and (segment_regs_equal(ref.segment,get_default_segment_of_ref(ref))))) then
  2256. ref.segment:=NR_NO;
  2257. end;
  2258. function taicpu.NeedAddrPrefix(opidx: byte): boolean;
  2259. begin
  2260. {$if defined(x86_64)}
  2261. result:=(oper[opidx]^.typ=top_ref) and is_32_bit_ref(oper[opidx]^.ref^);
  2262. {$elseif defined(i386)}
  2263. result:=(oper[opidx]^.typ=top_ref) and is_16_bit_ref(oper[opidx]^.ref^);
  2264. {$elseif defined(i8086)}
  2265. result:=(oper[opidx]^.typ=top_ref) and is_32_bit_ref(oper[opidx]^.ref^);
  2266. {$endif}
  2267. end;
  2268. function taicpu.NeedAddrPrefix:boolean;
  2269. var
  2270. i: Integer;
  2271. begin
  2272. for i:=0 to ops-1 do
  2273. if needaddrprefix(i) then
  2274. exit(true);
  2275. result:=false;
  2276. end;
  2277. procedure badreg(r:Tregister);
  2278. begin
  2279. Message1(asmw_e_invalid_register,generic_regname(r));
  2280. end;
  2281. function regval(r:Tregister):byte;
  2282. const
  2283. intsupreg2opcode: array[0..7] of byte=
  2284. // ax cx dx bx si di bp sp -- in x86reg.dat
  2285. // ax cx dx bx sp bp si di -- needed order
  2286. (0, 1, 2, 3, 6, 7, 5, 4);
  2287. maxsupreg: array[tregistertype] of tsuperregister=
  2288. {$ifdef x86_64}
  2289. (0, 16, 9, 8, 32, 32, 8, 0, 0, 0, 0, 0);
  2290. {$else x86_64}
  2291. (0, 8, 9, 8, 8, 32, 8, 0, 0, 0, 0, 0);
  2292. {$endif x86_64}
  2293. var
  2294. rs: tsuperregister;
  2295. rt: tregistertype;
  2296. begin
  2297. rs:=getsupreg(r);
  2298. rt:=getregtype(r);
  2299. if (rs>=maxsupreg[rt]) then
  2300. badreg(r);
  2301. result:=rs and 7;
  2302. if (rt=R_INTREGISTER) then
  2303. begin
  2304. if (rs<8) then
  2305. result:=intsupreg2opcode[rs];
  2306. if getsubreg(r)=R_SUBH then
  2307. inc(result,4);
  2308. end;
  2309. end;
  2310. {$if defined(x86_64)}
  2311. function rexbits(r: tregister): byte;
  2312. begin
  2313. result:=0;
  2314. case getregtype(r) of
  2315. R_INTREGISTER:
  2316. if (getsupreg(r)>=RS_R8) then
  2317. { Either B,X or R bits can be set, depending on register role in instruction.
  2318. Set all three bits here, caller will discard unnecessary ones. }
  2319. result:=result or $47
  2320. else if (getsubreg(r)=R_SUBL) and
  2321. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  2322. result:=result or $40
  2323. else if (getsubreg(r)=R_SUBH) then
  2324. { Not an actual REX bit, used to detect incompatible usage of
  2325. AH/BH/CH/DH }
  2326. result:=result or $80;
  2327. R_MMREGISTER:
  2328. //if getsupreg(r)>=RS_XMM8 then
  2329. // AVX512 = 32 register
  2330. // rexbit = 0 => MMRegister 0..7 or 16..23
  2331. // rexbit = 1 => MMRegister 8..15 or 24..31
  2332. if (getsupreg(r) and $08) = $08 then
  2333. result:=result or $47;
  2334. else
  2335. ;
  2336. end;
  2337. end;
  2338. function process_ea_ref_64_32(const input:toper;var output:ea;rfield:longint; uselargeoffset,forceSibByte: boolean):boolean;
  2339. var
  2340. sym : tasmsymbol;
  2341. md,s : byte;
  2342. base,index,scalefactor,
  2343. o : longint;
  2344. ir,br : Tregister;
  2345. isub,bsub : tsubregister;
  2346. begin
  2347. result:=false;
  2348. ir:=input.ref^.index;
  2349. br:=input.ref^.base;
  2350. isub:=getsubreg(ir);
  2351. bsub:=getsubreg(br);
  2352. s:=input.ref^.scalefactor;
  2353. o:=input.ref^.offset;
  2354. sym:=input.ref^.symbol;
  2355. //if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  2356. // ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  2357. if ((ir<>NR_NO) and (getregtype(ir)=R_MMREGISTER) and (br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) or // vector memory (AVX2)
  2358. ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER) and (getregtype(ir)<>R_MMREGISTER)) or
  2359. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  2360. internalerror(200301081);
  2361. { it's direct address }
  2362. if (br=NR_NO) and (ir=NR_NO) then
  2363. begin
  2364. output.sib_present:=true;
  2365. output.bytes:=4;
  2366. output.modrm:=4 or (rfield shl 3);
  2367. output.sib:=$25;
  2368. end
  2369. else if (br=NR_RIP) and (ir=NR_NO) then
  2370. begin
  2371. { rip based }
  2372. output.sib_present:=false;
  2373. output.bytes:=4;
  2374. output.modrm:=5 or (rfield shl 3);
  2375. end
  2376. else
  2377. { it's an indirection }
  2378. begin
  2379. if ((br=NR_RIP) and (ir<>NR_NO)) or
  2380. (ir=NR_RIP) then
  2381. message(asmw_e_illegal_use_of_rip);
  2382. if ir=NR_STACK_POINTER_REG then
  2383. Message(asmw_e_illegal_use_of_sp);
  2384. { 16 bit? }
  2385. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY,R_SUBMMZ]) and
  2386. (br<>NR_NO) and (bsub=R_SUBQ)
  2387. ) then
  2388. begin
  2389. // vector memory (AVX2) =>> ignore
  2390. end
  2391. else if ((ir<>NR_NO) and (isub<>R_SUBQ) and (isub<>R_SUBD)) or
  2392. ((br<>NR_NO) and (bsub<>R_SUBQ) and (bsub<>R_SUBD)) then
  2393. begin
  2394. message(asmw_e_16bit_32bit_not_supported);
  2395. end;
  2396. { wrong, for various reasons }
  2397. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  2398. exit;
  2399. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  2400. result:=true;
  2401. { base }
  2402. case br of
  2403. NR_R8D,
  2404. NR_EAX,
  2405. NR_R8,
  2406. NR_RAX : base:=0;
  2407. NR_R9D,
  2408. NR_ECX,
  2409. NR_R9,
  2410. NR_RCX : base:=1;
  2411. NR_R10D,
  2412. NR_EDX,
  2413. NR_R10,
  2414. NR_RDX : base:=2;
  2415. NR_R11D,
  2416. NR_EBX,
  2417. NR_R11,
  2418. NR_RBX : base:=3;
  2419. NR_R12D,
  2420. NR_ESP,
  2421. NR_R12,
  2422. NR_RSP : base:=4;
  2423. NR_R13D,
  2424. NR_EBP,
  2425. NR_R13,
  2426. NR_NO,
  2427. NR_RBP : base:=5;
  2428. NR_R14D,
  2429. NR_ESI,
  2430. NR_R14,
  2431. NR_RSI : base:=6;
  2432. NR_R15D,
  2433. NR_EDI,
  2434. NR_R15,
  2435. NR_RDI : base:=7;
  2436. else
  2437. exit;
  2438. end;
  2439. { index }
  2440. case ir of
  2441. NR_R8D,
  2442. NR_EAX,
  2443. NR_R8,
  2444. NR_RAX,
  2445. NR_XMM0,
  2446. NR_XMM8,
  2447. NR_XMM16,
  2448. NR_XMM24,
  2449. NR_YMM0,
  2450. NR_YMM8,
  2451. NR_YMM16,
  2452. NR_YMM24,
  2453. NR_ZMM0,
  2454. NR_ZMM8,
  2455. NR_ZMM16,
  2456. NR_ZMM24: index:=0;
  2457. NR_R9D,
  2458. NR_ECX,
  2459. NR_R9,
  2460. NR_RCX,
  2461. NR_XMM1,
  2462. NR_XMM9,
  2463. NR_XMM17,
  2464. NR_XMM25,
  2465. NR_YMM1,
  2466. NR_YMM9,
  2467. NR_YMM17,
  2468. NR_YMM25,
  2469. NR_ZMM1,
  2470. NR_ZMM9,
  2471. NR_ZMM17,
  2472. NR_ZMM25: index:=1;
  2473. NR_R10D,
  2474. NR_EDX,
  2475. NR_R10,
  2476. NR_RDX,
  2477. NR_XMM2,
  2478. NR_XMM10,
  2479. NR_XMM18,
  2480. NR_XMM26,
  2481. NR_YMM2,
  2482. NR_YMM10,
  2483. NR_YMM18,
  2484. NR_YMM26,
  2485. NR_ZMM2,
  2486. NR_ZMM10,
  2487. NR_ZMM18,
  2488. NR_ZMM26: index:=2;
  2489. NR_R11D,
  2490. NR_EBX,
  2491. NR_R11,
  2492. NR_RBX,
  2493. NR_XMM3,
  2494. NR_XMM11,
  2495. NR_XMM19,
  2496. NR_XMM27,
  2497. NR_YMM3,
  2498. NR_YMM11,
  2499. NR_YMM19,
  2500. NR_YMM27,
  2501. NR_ZMM3,
  2502. NR_ZMM11,
  2503. NR_ZMM19,
  2504. NR_ZMM27: index:=3;
  2505. NR_R12D,
  2506. NR_ESP,
  2507. NR_R12,
  2508. NR_NO,
  2509. NR_XMM4,
  2510. NR_XMM12,
  2511. NR_XMM20,
  2512. NR_XMM28,
  2513. NR_YMM4,
  2514. NR_YMM12,
  2515. NR_YMM20,
  2516. NR_YMM28,
  2517. NR_ZMM4,
  2518. NR_ZMM12,
  2519. NR_ZMM20,
  2520. NR_ZMM28: index:=4;
  2521. NR_R13D,
  2522. NR_EBP,
  2523. NR_R13,
  2524. NR_RBP,
  2525. NR_XMM5,
  2526. NR_XMM13,
  2527. NR_XMM21,
  2528. NR_XMM29,
  2529. NR_YMM5,
  2530. NR_YMM13,
  2531. NR_YMM21,
  2532. NR_YMM29,
  2533. NR_ZMM5,
  2534. NR_ZMM13,
  2535. NR_ZMM21,
  2536. NR_ZMM29: index:=5;
  2537. NR_R14D,
  2538. NR_ESI,
  2539. NR_R14,
  2540. NR_RSI,
  2541. NR_XMM6,
  2542. NR_XMM14,
  2543. NR_XMM22,
  2544. NR_XMM30,
  2545. NR_YMM6,
  2546. NR_YMM14,
  2547. NR_YMM22,
  2548. NR_YMM30,
  2549. NR_ZMM6,
  2550. NR_ZMM14,
  2551. NR_ZMM22,
  2552. NR_ZMM30: index:=6;
  2553. NR_R15D,
  2554. NR_EDI,
  2555. NR_R15,
  2556. NR_RDI,
  2557. NR_XMM7,
  2558. NR_XMM15,
  2559. NR_XMM23,
  2560. NR_XMM31,
  2561. NR_YMM7,
  2562. NR_YMM15,
  2563. NR_YMM23,
  2564. NR_YMM31,
  2565. NR_ZMM7,
  2566. NR_ZMM15,
  2567. NR_ZMM23,
  2568. NR_ZMM31: index:=7;
  2569. else
  2570. exit;
  2571. end;
  2572. case s of
  2573. 0,
  2574. 1 : scalefactor:=0;
  2575. 2 : scalefactor:=1;
  2576. 4 : scalefactor:=2;
  2577. 8 : scalefactor:=3;
  2578. else
  2579. exit;
  2580. end;
  2581. { If rbp or r13 is used we must always include an offset }
  2582. if (br=NR_NO) or
  2583. ((br<>NR_RBP) and (br<>NR_R13) and (br<>NR_EBP) and (br<>NR_R13D) and (o=0) and (sym=nil)) then
  2584. md:=0
  2585. else
  2586. if ((o>=-128) and (o<=127) and (sym=nil) and (not(uselargeoffset) or (o = 0))) then
  2587. md:=1
  2588. else
  2589. md:=2;
  2590. if (br=NR_NO) or (md=2) then
  2591. output.bytes:=4
  2592. else
  2593. output.bytes:=md;
  2594. { SIB needed ? }
  2595. if not forceSibByte and (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) and (br<>NR_ESP) and (br<>NR_R12D) then
  2596. begin
  2597. output.sib_present:=false;
  2598. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  2599. end
  2600. else
  2601. begin
  2602. output.sib_present:=true;
  2603. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  2604. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  2605. end;
  2606. end;
  2607. output.size:=1+ord(output.sib_present)+output.bytes;
  2608. result:=true;
  2609. end;
  2610. {$elseif defined(i386) or defined(i8086)}
  2611. function process_ea_ref_32(const input:toper;out output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2612. var
  2613. sym : tasmsymbol;
  2614. md,s : byte;
  2615. base,index,scalefactor,
  2616. o : longint;
  2617. ir,br : Tregister;
  2618. isub,bsub : tsubregister;
  2619. begin
  2620. result:=false;
  2621. 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)
  2622. ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER) and (getregtype(input.ref^.index)<>R_MMREGISTER)) or
  2623. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  2624. internalerror(2003010802);
  2625. ir:=input.ref^.index;
  2626. br:=input.ref^.base;
  2627. isub:=getsubreg(ir);
  2628. bsub:=getsubreg(br);
  2629. s:=input.ref^.scalefactor;
  2630. o:=input.ref^.offset;
  2631. sym:=input.ref^.symbol;
  2632. { it's direct address }
  2633. if (br=NR_NO) and (ir=NR_NO) then
  2634. begin
  2635. { it's a pure offset }
  2636. output.sib_present:=false;
  2637. output.bytes:=4;
  2638. output.modrm:=5 or (rfield shl 3);
  2639. end
  2640. else
  2641. { it's an indirection }
  2642. begin
  2643. { 16 bit address? }
  2644. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY,R_SUBMMZ]) and
  2645. (br<>NR_NO) and (bsub=R_SUBD)
  2646. ) then
  2647. begin
  2648. // vector memory (AVX2) =>> ignore
  2649. end
  2650. else if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  2651. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  2652. message(asmw_e_16bit_not_supported);
  2653. {$ifdef OPTEA}
  2654. { make single reg base }
  2655. if (br=NR_NO) and (s=1) then
  2656. begin
  2657. br:=ir;
  2658. ir:=NR_NO;
  2659. end;
  2660. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  2661. if (br=NR_NO) and
  2662. (((s=2) and (ir<>NR_ESP)) or
  2663. (s=3) or (s=5) or (s=9)) then
  2664. begin
  2665. br:=ir;
  2666. dec(s);
  2667. end;
  2668. { swap ESP into base if scalefactor is 1 }
  2669. if (s=1) and (ir=NR_ESP) then
  2670. begin
  2671. ir:=br;
  2672. br:=NR_ESP;
  2673. end;
  2674. {$endif OPTEA}
  2675. { wrong, for various reasons }
  2676. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  2677. exit;
  2678. { base }
  2679. case br of
  2680. NR_EAX : base:=0;
  2681. NR_ECX : base:=1;
  2682. NR_EDX : base:=2;
  2683. NR_EBX : base:=3;
  2684. NR_ESP : base:=4;
  2685. NR_NO,
  2686. NR_EBP : base:=5;
  2687. NR_ESI : base:=6;
  2688. NR_EDI : base:=7;
  2689. else
  2690. exit;
  2691. end;
  2692. { index }
  2693. case ir of
  2694. NR_EAX,
  2695. NR_XMM0,
  2696. NR_YMM0,
  2697. NR_ZMM0: index:=0;
  2698. NR_ECX,
  2699. NR_XMM1,
  2700. NR_YMM1,
  2701. NR_ZMM1: index:=1;
  2702. NR_EDX,
  2703. NR_XMM2,
  2704. NR_YMM2,
  2705. NR_ZMM2: index:=2;
  2706. NR_EBX,
  2707. NR_XMM3,
  2708. NR_YMM3,
  2709. NR_ZMM3: index:=3;
  2710. NR_NO,
  2711. NR_XMM4,
  2712. NR_YMM4,
  2713. NR_ZMM4: index:=4;
  2714. NR_EBP,
  2715. NR_XMM5,
  2716. NR_YMM5,
  2717. NR_ZMM5: index:=5;
  2718. NR_ESI,
  2719. NR_XMM6,
  2720. NR_YMM6,
  2721. NR_ZMM6: index:=6;
  2722. NR_EDI,
  2723. NR_XMM7,
  2724. NR_YMM7,
  2725. NR_ZMM7: index:=7;
  2726. else
  2727. exit;
  2728. end;
  2729. case s of
  2730. 0,
  2731. 1 : scalefactor:=0;
  2732. 2 : scalefactor:=1;
  2733. 4 : scalefactor:=2;
  2734. 8 : scalefactor:=3;
  2735. else
  2736. exit;
  2737. end;
  2738. if (br=NR_NO) or
  2739. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  2740. md:=0
  2741. else
  2742. if ((o>=-128) and (o<=127) and (sym=nil) and (not(uselargeoffset) or (o = 0))) then
  2743. md:=1
  2744. else
  2745. md:=2;
  2746. if (br=NR_NO) or (md=2) then
  2747. output.bytes:=4
  2748. else
  2749. output.bytes:=md;
  2750. { SIB needed ? }
  2751. if (ir=NR_NO) and (br<>NR_ESP) then
  2752. begin
  2753. output.sib_present:=false;
  2754. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  2755. end
  2756. else
  2757. begin
  2758. output.sib_present:=true;
  2759. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  2760. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  2761. end;
  2762. end;
  2763. if output.sib_present then
  2764. output.size:=2+output.bytes
  2765. else
  2766. output.size:=1+output.bytes;
  2767. result:=true;
  2768. end;
  2769. procedure maybe_swap_index_base(var br,ir:Tregister);
  2770. var
  2771. tmpreg: Tregister;
  2772. begin
  2773. if ((br=NR_NO) or (br=NR_SI) or (br=NR_DI)) and
  2774. ((ir=NR_NO) or (ir=NR_BP) or (ir=NR_BX)) then
  2775. begin
  2776. tmpreg:=br;
  2777. br:=ir;
  2778. ir:=tmpreg;
  2779. end;
  2780. end;
  2781. function process_ea_ref_16(const input:toper;out output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2782. var
  2783. sym : tasmsymbol;
  2784. md,s : byte;
  2785. base,
  2786. o : longint;
  2787. ir,br : Tregister;
  2788. isub,bsub : tsubregister;
  2789. begin
  2790. result:=false;
  2791. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  2792. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  2793. internalerror(2003010803);
  2794. ir:=input.ref^.index;
  2795. br:=input.ref^.base;
  2796. isub:=getsubreg(ir);
  2797. bsub:=getsubreg(br);
  2798. s:=input.ref^.scalefactor;
  2799. o:=input.ref^.offset;
  2800. sym:=input.ref^.symbol;
  2801. { it's a direct address }
  2802. if (br=NR_NO) and (ir=NR_NO) then
  2803. begin
  2804. { it's a pure offset }
  2805. output.bytes:=2;
  2806. output.modrm:=6 or (rfield shl 3);
  2807. end
  2808. else
  2809. { it's an indirection }
  2810. begin
  2811. { 32 bit address? }
  2812. if ((ir<>NR_NO) and (isub<>R_SUBW)) or
  2813. ((br<>NR_NO) and (bsub<>R_SUBW)) then
  2814. message(asmw_e_32bit_not_supported);
  2815. { scalefactor can only be 1 in 16-bit addresses }
  2816. if (s<>1) and (ir<>NR_NO) then
  2817. exit;
  2818. maybe_swap_index_base(br,ir);
  2819. if (br=NR_BX) and (ir=NR_SI) then
  2820. base:=0
  2821. else if (br=NR_BX) and (ir=NR_DI) then
  2822. base:=1
  2823. else if (br=NR_BP) and (ir=NR_SI) then
  2824. base:=2
  2825. else if (br=NR_BP) and (ir=NR_DI) then
  2826. base:=3
  2827. else if (br=NR_NO) and (ir=NR_SI) then
  2828. base:=4
  2829. else if (br=NR_NO) and (ir=NR_DI) then
  2830. base:=5
  2831. else if (br=NR_BP) and (ir=NR_NO) then
  2832. base:=6
  2833. else if (br=NR_BX) and (ir=NR_NO) then
  2834. base:=7
  2835. else
  2836. exit;
  2837. if (base<>6) and (o=0) and (sym=nil) then
  2838. md:=0
  2839. else if ((o>=-128) and (o<=127) and (sym=nil) and (not(uselargeoffset) or (o = 0))) then
  2840. md:=1
  2841. else
  2842. md:=2;
  2843. output.bytes:=md;
  2844. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  2845. end;
  2846. output.size:=1+output.bytes;
  2847. output.sib_present:=false;
  2848. result:=true;
  2849. end;
  2850. {$endif}
  2851. {$ifdef x86_64}
  2852. function process_ea(const input:toper;out output:ea;rfield:longint; uselargeoffset, forceSibByte: boolean):boolean;
  2853. {$else x86_64}
  2854. function process_ea(const input:toper;out output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2855. {$endif x86_64}
  2856. var
  2857. rv : byte;
  2858. begin
  2859. result:=false;
  2860. fillchar(output,sizeof(output),0);
  2861. {Register ?}
  2862. if (input.typ=top_reg) then
  2863. begin
  2864. rv:=regval(input.reg);
  2865. output.modrm:=$c0 or (rfield shl 3) or rv;
  2866. output.size:=1;
  2867. {$ifdef x86_64}
  2868. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  2869. {$endif x86_64}
  2870. result:=true;
  2871. exit;
  2872. end;
  2873. {No register, so memory reference.}
  2874. if input.typ<>top_ref then
  2875. internalerror(200409263);
  2876. {$if defined(x86_64)}
  2877. result:=process_ea_ref_64_32(input,output,rfield, uselargeoffset,forceSibByte);
  2878. {$elseif defined(i386) or defined(i8086)}
  2879. if is_16_bit_ref(input.ref^) then
  2880. result:=process_ea_ref_16(input,output,rfield, uselargeoffset)
  2881. else
  2882. result:=process_ea_ref_32(input,output,rfield, uselargeoffset);
  2883. {$endif}
  2884. end;
  2885. function taicpu.calcsize(p:PInsEntry):shortint;
  2886. var
  2887. codes : pchar;
  2888. c : byte;
  2889. len : shortint;
  2890. ea_data : ea;
  2891. exists_evex: boolean;
  2892. exists_vex: boolean;
  2893. exists_vex_extension: boolean;
  2894. exists_prefix_66: boolean;
  2895. exists_prefix_F2: boolean;
  2896. exists_prefix_F3: boolean;
  2897. exists_l256: boolean;
  2898. exists_l512: boolean;
  2899. exists_EVEXW1: boolean;
  2900. {$ifdef x86_64}
  2901. omit_rexw : boolean;
  2902. {$endif x86_64}
  2903. begin
  2904. len:=0;
  2905. codes:=@p^.code[0];
  2906. exists_vex := false;
  2907. exists_vex_extension := false;
  2908. exists_prefix_66 := false;
  2909. exists_prefix_F2 := false;
  2910. exists_prefix_F3 := false;
  2911. exists_evex := false;
  2912. exists_l256 := false;
  2913. exists_l512 := false;
  2914. exists_EVEXW1 := false;
  2915. {$ifdef x86_64}
  2916. rex:=0;
  2917. omit_rexw:=false;
  2918. {$endif x86_64}
  2919. repeat
  2920. c:=ord(codes^);
  2921. inc(codes);
  2922. case c of
  2923. &0 :
  2924. break;
  2925. &1,&2,&3 :
  2926. begin
  2927. inc(codes,c);
  2928. inc(len,c);
  2929. end;
  2930. &10,&11,&12 :
  2931. begin
  2932. {$ifdef x86_64}
  2933. rex:=rex or (rexbits(oper[c-&10]^.reg) and $F1);
  2934. {$endif x86_64}
  2935. inc(codes);
  2936. inc(len);
  2937. end;
  2938. &13,&23 :
  2939. begin
  2940. inc(codes);
  2941. inc(len);
  2942. end;
  2943. &4,&5,&6,&7 :
  2944. begin
  2945. if opsize={$ifdef i8086}S_L{$else}S_W{$endif} then
  2946. inc(len,2)
  2947. else
  2948. inc(len);
  2949. end;
  2950. &14,&15,&16,
  2951. &20,&21,&22,
  2952. &24,&25,&26,&27,
  2953. &50,&51,&52 :
  2954. inc(len);
  2955. &30,&31,&32,
  2956. &37,
  2957. &60,&61,&62 :
  2958. inc(len,2);
  2959. &34,&35,&36:
  2960. begin
  2961. {$ifdef i8086}
  2962. inc(len,2);
  2963. {$else i8086}
  2964. if opsize=S_Q then
  2965. inc(len,8)
  2966. else
  2967. inc(len,4);
  2968. {$endif i8086}
  2969. end;
  2970. &44,&45,&46:
  2971. inc(len,sizeof(pint));
  2972. &54,&55,&56:
  2973. inc(len,8);
  2974. &40,&41,&42,
  2975. &70,&71,&72,
  2976. &254,&255,&256 :
  2977. inc(len,4);
  2978. &64,&65,&66:
  2979. {$ifdef i8086}
  2980. inc(len,2);
  2981. {$else i8086}
  2982. inc(len,4);
  2983. {$endif i8086}
  2984. &74,&75,&76,&77: ; // ignore vex-coded operand-idx
  2985. &320,&321,&322 :
  2986. begin
  2987. case (oper[c-&320]^.ot and OT_SIZE_MASK) of
  2988. {$if defined(i386) or defined(x86_64)}
  2989. OT_BITS16 :
  2990. {$elseif defined(i8086)}
  2991. OT_BITS32 :
  2992. {$endif}
  2993. inc(len);
  2994. {$ifdef x86_64}
  2995. OT_BITS64:
  2996. begin
  2997. rex:=rex or $48;
  2998. end;
  2999. {$endif x86_64}
  3000. end;
  3001. end;
  3002. &310 :
  3003. {$if defined(x86_64)}
  3004. { every insentry with code 0310 must be marked with NOX86_64 }
  3005. InternalError(2011051301);
  3006. {$elseif defined(i386)}
  3007. inc(len);
  3008. {$elseif defined(i8086)}
  3009. {nothing};
  3010. {$endif}
  3011. &311 :
  3012. {$if defined(x86_64) or defined(i8086)}
  3013. inc(len)
  3014. {$endif x86_64 or i8086}
  3015. ;
  3016. &324 :
  3017. {$ifndef i8086}
  3018. inc(len)
  3019. {$endif not i8086}
  3020. ;
  3021. &326 :
  3022. begin
  3023. {$ifdef x86_64}
  3024. rex:=rex or $48;
  3025. {$endif x86_64}
  3026. end;
  3027. &312,
  3028. &323,
  3029. &327,
  3030. &331,&332: ;
  3031. &325:
  3032. {$ifdef i8086}
  3033. inc(len)
  3034. {$endif i8086}
  3035. ;
  3036. &333:
  3037. begin
  3038. inc(len);
  3039. exists_prefix_F2 := true;
  3040. end;
  3041. &334:
  3042. begin
  3043. inc(len);
  3044. exists_prefix_F3 := true;
  3045. end;
  3046. &361:
  3047. begin
  3048. {$ifndef i8086}
  3049. inc(len);
  3050. exists_prefix_66 := true;
  3051. {$endif not i8086}
  3052. end;
  3053. &335:
  3054. {$ifdef x86_64}
  3055. omit_rexw:=true
  3056. {$endif x86_64}
  3057. ;
  3058. &336,
  3059. &337: {nothing};
  3060. &100..&227 :
  3061. begin
  3062. {$ifdef x86_64}
  3063. if (c<&177) then
  3064. begin
  3065. if (oper[c and 7]^.typ=top_reg) then
  3066. begin
  3067. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  3068. end;
  3069. end;
  3070. {$endif x86_64}
  3071. if (oper[(c shr 3) and 7]^.typ = top_ref) and
  3072. (oper[(c shr 3) and 7]^.ref^.offset <> 0) then
  3073. begin
  3074. if (exists_vex and exists_evex and CheckUseEVEX) or
  3075. (not(exists_vex) and exists_evex) then
  3076. begin
  3077. CheckEVEXTuple(oper[(c shr 3) and 7]^, p, not(exists_l256 or exists_l512), exists_l256, exists_l512, exists_EVEXW1);
  3078. //const aInput:toper; aInsEntry: pInsentry; aIsVector128, aIsVector256, aIsVector512, aIsEVEXW1: boolean);
  3079. end;
  3080. end;
  3081. {$ifdef x86_64}
  3082. if process_ea(oper[(c shr 3) and 7]^, ea_data, 0, EVEXTupleState = etsNotTuple, (p^.optypes[(c shr 3) and 7] and ot_sibmem)=ot_sibmem) then
  3083. {$else x86_64}
  3084. if process_ea(oper[(c shr 3) and 7]^, ea_data, 0, EVEXTupleState = etsNotTuple) then
  3085. {$endif x86_64}
  3086. inc(len,ea_data.size)
  3087. else Message(asmw_e_invalid_effective_address);
  3088. {$ifdef x86_64}
  3089. rex:=rex or ea_data.rex;
  3090. {$endif x86_64}
  3091. end;
  3092. &240..&243:
  3093. begin
  3094. {$ifdef x86_64}
  3095. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  3096. {$endif x86_64}
  3097. inc(len);
  3098. end;
  3099. &350:
  3100. begin
  3101. exists_evex := true;
  3102. end;
  3103. &351: exists_l512 := true; // EVEX length bit 512
  3104. &352: exists_EVEXW1 := true; // EVEX W1
  3105. &362: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  3106. // =>> DEFAULT = 2 Bytes
  3107. begin
  3108. //if not(exists_vex) then
  3109. //begin
  3110. // inc(len, 2);
  3111. //end;
  3112. exists_vex := true;
  3113. end;
  3114. &363: // REX.W = 1
  3115. // =>> VEX prefix length = 3
  3116. begin
  3117. if not(exists_vex_extension) then
  3118. begin
  3119. //inc(len);
  3120. exists_vex_extension := true;
  3121. end;
  3122. end;
  3123. &364: exists_l256 := true; // VEX length bit 256
  3124. &366, // operand 2 (ymmreg) encoded immediate byte (bit 4-7)
  3125. &367: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  3126. &370: // VEX-Extension prefix $0F
  3127. // ignore for calculating length
  3128. ;
  3129. &371, // VEX-Extension prefix $0F38
  3130. &372, // VEX-Extension prefix $0F3A
  3131. &375..&377: // opcode map 5,6,7
  3132. begin
  3133. if not(exists_vex_extension) then
  3134. begin
  3135. //inc(len);
  3136. exists_vex_extension := true;
  3137. end;
  3138. end;
  3139. &300,&301,&302:
  3140. begin
  3141. {$if defined(x86_64) or defined(i8086)}
  3142. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  3143. inc(len);
  3144. {$endif x86_64 or i8086}
  3145. end;
  3146. else
  3147. InternalError(200603141);
  3148. end;
  3149. until false;
  3150. {$ifdef x86_64}
  3151. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  3152. Message(asmw_e_bad_reg_with_rex);
  3153. rex:=rex and $4F; { reset extra bits in upper nibble }
  3154. if omit_rexw then
  3155. begin
  3156. if rex=$48 then { remove rex entirely? }
  3157. rex:=0
  3158. else
  3159. rex:=rex and $F7;
  3160. end;
  3161. if not(exists_vex or exists_evex) then
  3162. begin
  3163. if rex<>0 then
  3164. Inc(len);
  3165. end;
  3166. {$endif}
  3167. if exists_evex and
  3168. exists_vex then
  3169. begin
  3170. if CheckUseEVEX then
  3171. begin
  3172. inc(len, 4);
  3173. end
  3174. else
  3175. begin
  3176. inc(len, 2);
  3177. if exists_vex_extension then inc(len);
  3178. {$ifdef x86_64}
  3179. if not(exists_vex_extension) then
  3180. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extension
  3181. {$endif x86_64}
  3182. end;
  3183. if exists_prefix_66 then dec(len);
  3184. if exists_prefix_F2 then dec(len);
  3185. if exists_prefix_F3 then dec(len);
  3186. end
  3187. else if exists_evex then
  3188. begin
  3189. inc(len, 4);
  3190. if exists_prefix_66 then dec(len);
  3191. if exists_prefix_F2 then dec(len);
  3192. if exists_prefix_F3 then dec(len);
  3193. end
  3194. else
  3195. begin
  3196. if exists_vex then
  3197. begin
  3198. inc(len,2);
  3199. if exists_prefix_66 then dec(len);
  3200. if exists_prefix_F2 then dec(len);
  3201. if exists_prefix_F3 then dec(len);
  3202. if exists_vex_extension then inc(len);
  3203. {$ifdef x86_64}
  3204. if not(exists_vex_extension) then
  3205. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extension
  3206. {$endif x86_64}
  3207. end;
  3208. end;
  3209. calcsize:=len;
  3210. end;
  3211. procedure taicpu.write0x66prefix(objdata:TObjData);
  3212. const
  3213. b66: Byte=$66;
  3214. begin
  3215. {$ifdef i8086}
  3216. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) then
  3217. Message(asmw_e_instruction_not_supported_by_cpu);
  3218. {$endif i8086}
  3219. objdata.writebytes(b66,1);
  3220. end;
  3221. procedure taicpu.write0x67prefix(objdata:TObjData);
  3222. const
  3223. b67: Byte=$67;
  3224. begin
  3225. {$ifdef i8086}
  3226. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) then
  3227. Message(asmw_e_instruction_not_supported_by_cpu);
  3228. {$endif i8086}
  3229. objdata.writebytes(b67,1);
  3230. end;
  3231. procedure taicpu.gencode(objdata: TObjData);
  3232. {
  3233. * the actual codes (C syntax, i.e. octal):
  3234. * \0 - terminates the code. (Unless it's a literal of course.)
  3235. * \1, \2, \3 - that many literal bytes follow in the code stream
  3236. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  3237. * (POP is never used for CS) depending on operand 0
  3238. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  3239. * on operand 0
  3240. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  3241. * to the register value of operand 0, 1 or 2
  3242. * \13 - a literal byte follows in the code stream, to be added
  3243. * to the condition code value of the instruction.
  3244. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  3245. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  3246. * \23 - a literal byte follows in the code stream, to be added
  3247. * to the inverted condition code value of the instruction
  3248. * (inverted version of \13).
  3249. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  3250. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  3251. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  3252. * assembly mode or the address-size override on the operand
  3253. * \37 - a word constant, from the _segment_ part of operand 0
  3254. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  3255. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  3256. on the address size of instruction
  3257. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  3258. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  3259. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  3260. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  3261. * assembly mode or the address-size override on the operand
  3262. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  3263. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  3264. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  3265. * field the register value of operand b.
  3266. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  3267. * field equal to digit b.
  3268. * \24a - operator a in ModRM.reg. ModRM 11:rrr:000
  3269. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  3270. * \300,\301,\302 - might be an 0x67, depending on the address size of
  3271. * the memory reference in operand x.
  3272. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  3273. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  3274. * \312 - (disassembler only) invalid with non-default address size.
  3275. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  3276. * size of operand x.
  3277. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  3278. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  3279. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  3280. * \327 - indicates that this instruction is only valid when the
  3281. * operand size is the default (instruction to disassembler,
  3282. * generates no code in the assembler)
  3283. * \331 - instruction not valid with REP prefix. Hint for
  3284. * disassembler only; for SSE instructions.
  3285. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  3286. * \333 - 0xF3 prefix for SSE instructions
  3287. * \334 - 0xF2 prefix for SSE instructions
  3288. * \335 - Indicates 64-bit operand size with REX.W not necessary / 64-bit scalar vector operand size
  3289. * \336 - Indicates 32-bit scalar vector operand size
  3290. * \337 - Indicates 64-bit scalar vector operand size
  3291. * \350 - EVEX prefix for AVX instructions
  3292. * \351 - EVEX Vector length 512
  3293. * \352 - EVEX W1
  3294. * \361 - 0x66 prefix for SSE instructions
  3295. * \362 - VEX prefix for AVX instructions
  3296. * \363 - VEX W1
  3297. * \364 - VEX Vector length 256
  3298. * \366 - operand 2 (ymmreg,zmmreg) encoded in bit 4-7 of the immediate byte
  3299. * \367 - operand 3 (ymmreg,zmmreg) encoded in bit 4-7 of the immediate byte
  3300. * \370 - VEX 0F-FLAG (map 1)
  3301. * \371 - VEX 0F38-FLAG (map 2)
  3302. * \372 - VEX 0F3A-FLAG (map 3)
  3303. * \375 - EVEX map 5
  3304. * \376 - EVEX map 6
  3305. * \377 - EVEX map 7
  3306. }
  3307. var
  3308. {$ifdef i8086}
  3309. currval : longint;
  3310. {$else i8086}
  3311. currval : aint;
  3312. {$endif i8086}
  3313. currsym : tobjsymbol;
  3314. currrelreloc,
  3315. currabsreloc,
  3316. currabsreloc32 : TObjRelocationType;
  3317. {$ifdef x86_64}
  3318. rexwritten : boolean;
  3319. {$endif x86_64}
  3320. procedure getvalsym(opidx:longint);
  3321. begin
  3322. case oper[opidx]^.typ of
  3323. top_ref :
  3324. begin
  3325. currval:=oper[opidx]^.ref^.offset;
  3326. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  3327. {$ifdef i8086}
  3328. if oper[opidx]^.ref^.refaddr=addr_seg then
  3329. begin
  3330. currrelreloc:=RELOC_SEGREL;
  3331. currabsreloc:=RELOC_SEG;
  3332. currabsreloc32:=RELOC_SEG;
  3333. end
  3334. else if oper[opidx]^.ref^.refaddr=addr_dgroup then
  3335. begin
  3336. currrelreloc:=RELOC_DGROUPREL;
  3337. currabsreloc:=RELOC_DGROUP;
  3338. currabsreloc32:=RELOC_DGROUP;
  3339. end
  3340. else if oper[opidx]^.ref^.refaddr=addr_fardataseg then
  3341. begin
  3342. currrelreloc:=RELOC_FARDATASEGREL;
  3343. currabsreloc:=RELOC_FARDATASEG;
  3344. currabsreloc32:=RELOC_FARDATASEG;
  3345. end
  3346. else
  3347. {$endif i8086}
  3348. {$ifdef i386}
  3349. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  3350. (tf_pic_uses_got in target_info.flags) then
  3351. begin
  3352. currrelreloc:=RELOC_PLT32;
  3353. currabsreloc:=RELOC_GOT32;
  3354. currabsreloc32:=RELOC_GOT32;
  3355. end
  3356. else if oper[opidx]^.ref^.refaddr=addr_ntpoff then
  3357. begin
  3358. currrelreloc:=RELOC_NTPOFF;
  3359. currabsreloc:=RELOC_NTPOFF;
  3360. currabsreloc32:=RELOC_NTPOFF;
  3361. end
  3362. else if oper[opidx]^.ref^.refaddr=addr_tlsgd then
  3363. begin
  3364. currrelreloc:=RELOC_TLSGD;
  3365. currabsreloc:=RELOC_TLSGD;
  3366. currabsreloc32:=RELOC_TLSGD;
  3367. end
  3368. else
  3369. {$endif i386}
  3370. {$ifdef x86_64}
  3371. if oper[opidx]^.ref^.refaddr=addr_pic then
  3372. begin
  3373. currrelreloc:=RELOC_PLT32;
  3374. currabsreloc:=RELOC_GOTPCREL;
  3375. currabsreloc32:=RELOC_GOTPCREL;
  3376. end
  3377. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  3378. begin
  3379. currrelreloc:=RELOC_RELATIVE;
  3380. currabsreloc:=RELOC_RELATIVE;
  3381. currabsreloc32:=RELOC_RELATIVE;
  3382. end
  3383. else if oper[opidx]^.ref^.refaddr=addr_tpoff then
  3384. begin
  3385. currrelreloc:=RELOC_TPOFF;
  3386. currabsreloc:=RELOC_TPOFF;
  3387. currabsreloc32:=RELOC_TPOFF;
  3388. end
  3389. else if oper[opidx]^.ref^.refaddr=addr_tlsgd then
  3390. begin
  3391. currrelreloc:=RELOC_TLSGD;
  3392. currabsreloc:=RELOC_TLSGD;
  3393. currabsreloc32:=RELOC_TLSGD;
  3394. end
  3395. else
  3396. {$endif x86_64}
  3397. begin
  3398. currrelreloc:=RELOC_RELATIVE;
  3399. currabsreloc:=RELOC_ABSOLUTE;
  3400. currabsreloc32:=RELOC_ABSOLUTE32;
  3401. end;
  3402. end;
  3403. top_const :
  3404. begin
  3405. {$ifdef i8086}
  3406. currval:=longint(oper[opidx]^.val);
  3407. {$else i8086}
  3408. currval:=aint(oper[opidx]^.val);
  3409. {$endif i8086}
  3410. currsym:=nil;
  3411. currabsreloc:=RELOC_ABSOLUTE;
  3412. currabsreloc32:=RELOC_ABSOLUTE32;
  3413. end;
  3414. else
  3415. Message(asmw_e_immediate_or_reference_expected);
  3416. end;
  3417. end;
  3418. {$ifdef x86_64}
  3419. procedure maybewriterex;
  3420. begin
  3421. if (rex<>0) and not(rexwritten) then
  3422. begin
  3423. rexwritten:=true;
  3424. objdata.writebytes(rex,1);
  3425. end;
  3426. end;
  3427. {$endif x86_64}
  3428. procedure objdata_writereloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  3429. begin
  3430. {$ifdef i386}
  3431. { Special case of '_GLOBAL_OFFSET_TABLE_'
  3432. which needs a special relocation type R_386_GOTPC }
  3433. if assigned (p) and
  3434. (p.name='_GLOBAL_OFFSET_TABLE_') and
  3435. (tf_pic_uses_got in target_info.flags) then
  3436. begin
  3437. { nothing else than a 4 byte relocation should occur
  3438. for GOT }
  3439. if len<>4 then
  3440. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3441. Reloctype:=RELOC_GOTPC;
  3442. { We need to add the offset of the relocation
  3443. of _GLOBAL_OFFSET_TABLE symbol within
  3444. the current instruction }
  3445. inc(data,objdata.currobjsec.size-insoffset);
  3446. end;
  3447. {$endif i386}
  3448. objdata.writereloc(data,len,p,Reloctype);
  3449. {$ifdef x86_64}
  3450. { Computed offset is not yet correct for GOTPC relocation }
  3451. { RELOC_GOTPCREL, RELOC_REX_GOTPCRELX, RELOC_GOTPCRELX need special handling }
  3452. if assigned(p) and (RelocType in [RELOC_GOTPCREL, RELOC_REX_GOTPCRELX, RELOC_GOTPCRELX]) and
  3453. { These relocations seem to be used only for ELF
  3454. which always has relocs_use_addend set to true
  3455. so that it is the orgsize of the last relocation which needs to be fixed PM }
  3456. (insend<>objdata.CurrObjSec.size) then
  3457. dec(TObjRelocation(objdata.CurrObjSec.ObjRelocations.Last).orgsize,insend-objdata.CurrObjSec.size);
  3458. {$endif}
  3459. end;
  3460. const
  3461. CondVal:array[TAsmCond] of byte=($0,
  3462. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  3463. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  3464. $0, $A, $A, $B, $8, $4);
  3465. var
  3466. i: integer;
  3467. c : byte;
  3468. pb : pbyte;
  3469. codes : pchar;
  3470. bytes : array[0..3] of byte;
  3471. rfield,
  3472. data,s,opidx : longint;
  3473. ea_data : ea;
  3474. relsym : TObjSymbol;
  3475. mod11 : boolean;
  3476. needed_VEX_Extension: boolean;
  3477. needed_VEX: boolean;
  3478. needed_EVEX: boolean;
  3479. {$ifdef x86_64}
  3480. needed_VSIB: boolean;
  3481. {$endif x86_64}
  3482. opmode: integer;
  3483. VEXvvvv: byte;
  3484. VEXmmmmm: byte;
  3485. {
  3486. VEXw : byte;
  3487. VEXpp : byte;
  3488. VEXll : byte;
  3489. }
  3490. EVEXvvvv: byte;
  3491. EVEXpp: byte;
  3492. EVEXr: byte;
  3493. EVEXx: byte;
  3494. EVEXv: byte;
  3495. EVEXll: byte;
  3496. EVEXw1: byte;
  3497. EVEXz : byte;
  3498. EVEXaaa : byte;
  3499. EVEXb : byte;
  3500. EVEXu : byte;
  3501. EVEXmmm : byte;
  3502. begin
  3503. { safety check }
  3504. if objdata.currobjsec.size<>longword(insoffset) then
  3505. internalerror(200130121);
  3506. { those variables are initialized inside local procedures, the dfa cannot handle this yet }
  3507. currsym:=nil;
  3508. currabsreloc:=RELOC_NONE;
  3509. currabsreloc32:=RELOC_NONE;
  3510. currrelreloc:=RELOC_NONE;
  3511. currval:=0;
  3512. { check instruction's processor level }
  3513. { todo: maybe adapt and enable this code for i386 and x86_64 as well }
  3514. {$ifdef i8086}
  3515. if objdata.CPUType<>cpu_none then
  3516. begin
  3517. if IF_8086 in insentry^.flags then
  3518. else if IF_186 in insentry^.flags then
  3519. begin
  3520. if objdata.CPUType<cpu_186 then
  3521. Message(asmw_e_instruction_not_supported_by_cpu);
  3522. end
  3523. else if IF_286 in insentry^.flags then
  3524. begin
  3525. if objdata.CPUType<cpu_286 then
  3526. Message(asmw_e_instruction_not_supported_by_cpu);
  3527. end
  3528. else if IF_386 in insentry^.flags then
  3529. begin
  3530. if objdata.CPUType<cpu_386 then
  3531. Message(asmw_e_instruction_not_supported_by_cpu);
  3532. end
  3533. else if IF_486 in insentry^.flags then
  3534. begin
  3535. if objdata.CPUType<cpu_486 then
  3536. Message(asmw_e_instruction_not_supported_by_cpu);
  3537. end
  3538. else if IF_PENT in insentry^.flags then
  3539. begin
  3540. if objdata.CPUType<cpu_Pentium then
  3541. Message(asmw_e_instruction_not_supported_by_cpu);
  3542. end
  3543. else if IF_P6 in insentry^.flags then
  3544. begin
  3545. if objdata.CPUType<cpu_Pentium2 then
  3546. Message(asmw_e_instruction_not_supported_by_cpu);
  3547. end
  3548. else if IF_KATMAI in insentry^.flags then
  3549. begin
  3550. if objdata.CPUType<cpu_Pentium3 then
  3551. Message(asmw_e_instruction_not_supported_by_cpu);
  3552. end
  3553. else if insentry^.flags*[IF_WILLAMETTE,IF_PRESCOTT]<>[] then
  3554. begin
  3555. if objdata.CPUType<cpu_Pentium4 then
  3556. Message(asmw_e_instruction_not_supported_by_cpu);
  3557. end
  3558. else if IF_NEC in insentry^.flags then
  3559. begin
  3560. { the NEC V20/V30 extensions are incompatible with 386+, due to overlapping opcodes }
  3561. if objdata.CPUType>=cpu_386 then
  3562. Message(asmw_e_instruction_not_supported_by_cpu);
  3563. end
  3564. else if IF_SANDYBRIDGE in insentry^.flags then
  3565. begin
  3566. { todo: handle these properly }
  3567. end;
  3568. end;
  3569. {$endif i8086}
  3570. { load data to write }
  3571. codes:=insentry^.code;
  3572. {$ifdef x86_64}
  3573. rexwritten:=false;
  3574. {$endif x86_64}
  3575. { Force word push/pop for registers }
  3576. if (opsize={$ifdef i8086}S_L{$else}S_W{$endif}) and ((codes[0]=#4) or (codes[0]=#6) or
  3577. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  3578. write0x66prefix(objdata);
  3579. // needed VEX Prefix (for AVX etc.)
  3580. needed_VEX := false;
  3581. needed_EVEX := false;
  3582. needed_VEX_Extension := false;
  3583. {$ifdef x86_64}
  3584. needed_VSIB := false;
  3585. {$endif x86_64}
  3586. opmode := -1;
  3587. VEXvvvv := 0;
  3588. VEXmmmmm := 0;
  3589. {
  3590. VEXll := 0;
  3591. VEXw := 0;
  3592. VEXpp := 0;
  3593. }
  3594. EVEXpp := 0;
  3595. EVEXvvvv := 0;
  3596. EVEXr := 0;
  3597. EVEXx := 0;
  3598. EVEXv := 0;
  3599. EVEXll := 0;
  3600. EVEXw1 := 0;
  3601. EVEXz := 0;
  3602. EVEXaaa := 0;
  3603. EVEXb := 0;
  3604. EVEXu := 1;
  3605. EVEXmmm := 0;
  3606. repeat
  3607. c:=ord(codes^);
  3608. inc(codes);
  3609. case c of
  3610. &0: break;
  3611. &1,
  3612. &2,
  3613. &3: inc(codes,c);
  3614. &10,
  3615. &11,
  3616. &12: inc(codes, 1);
  3617. &74: opmode := 0;
  3618. &75: opmode := 1;
  3619. &76: opmode := 2;
  3620. &100..&227: begin
  3621. // AVX 512 - EVEX
  3622. // check operands
  3623. if (c shr 6) = 1 then
  3624. begin
  3625. opidx := c and 7;
  3626. if ops > opidx then
  3627. begin
  3628. if (oper[opidx]^.typ=top_reg) then
  3629. if getsupreg(oper[opidx]^.reg) and $10 = $0 then EVEXr := 1;
  3630. end
  3631. end
  3632. else EVEXr := 1; // modrm:reg not used =>> 1
  3633. opidx := (c shr 3) and 7;
  3634. if ops > opidx then
  3635. case oper[opidx]^.typ of
  3636. top_reg: if getsupreg(oper[opidx]^.reg) and $10 = $0 then EVEXx := 1;
  3637. top_ref: begin
  3638. if getsupreg(oper[opidx]^.ref^.index) and $08 = $0 then EVEXx := 1;
  3639. if getsubreg(oper[opidx]^.ref^.index) in [R_SUBMMX,R_SUBMMY,R_SUBMMZ] then
  3640. begin
  3641. // VSIB memory addresing
  3642. if getsupreg(oper[opidx]^.ref^.index) and $10 = $0 then EVEXv := 1; // VECTOR-Index
  3643. {$ifdef x86_64}
  3644. needed_VSIB := true;
  3645. {$endif x86_64}
  3646. end;
  3647. end;
  3648. else
  3649. Internalerror(2019081014);
  3650. end;
  3651. end;
  3652. &240..&243:
  3653. begin
  3654. opidx := c and 7;
  3655. if ops > opidx then
  3656. begin
  3657. if (oper[opidx]^.typ=top_reg) then
  3658. if getsupreg(oper[opidx]^.reg) and $10 = $0 then EVEXr := 1;
  3659. end else EVEXr := 1; // modrm:reg not used =>> 1
  3660. EVEXx:=1; //-- modrm.rm not used;
  3661. end;
  3662. &333: begin
  3663. VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  3664. //VEXpp := $02; // set SIMD-prefix $F3
  3665. EVEXpp := $02; // set SIMD-prefix $F3
  3666. end;
  3667. &334: begin
  3668. VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  3669. //VEXpp := $03; // set SIMD-prefix $F2
  3670. EVEXpp := $03; // set SIMD-prefix $F2
  3671. end;
  3672. &350: needed_EVEX := true; // AVX512 instruction or AVX128/256/512-instruction (depended on operands [x,y,z]mm16..)
  3673. &351: EVEXll := $02; // vectorlength = 512 bits AND no scalar
  3674. &352: EVEXw1 := $01;
  3675. &361: begin
  3676. VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  3677. //VEXpp := $01; // set SIMD-prefix $66
  3678. EVEXpp := $01; // set SIMD-prefix $66
  3679. end;
  3680. &362: needed_VEX := true;
  3681. &363: begin
  3682. needed_VEX_Extension := true;
  3683. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  3684. //VEXw := 1;
  3685. end;
  3686. &364: begin
  3687. VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  3688. //VEXll := $01;
  3689. EVEXll := $01;
  3690. end;
  3691. &366,
  3692. &367: begin
  3693. opidx:=c-&364; { 0366->operand 2, 0367->operand 3 }
  3694. if (ops > opidx) and
  3695. (oper[opidx]^.typ=top_reg) and
  3696. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK = otf_reg_xmm) or
  3697. (oper[opidx]^.ot and OT_REG_EXTRA_MASK = otf_reg_ymm) or
  3698. (oper[opidx]^.ot and OT_REG_EXTRA_MASK = otf_reg_zmm)) then
  3699. if (getsupreg(oper[opidx]^.reg) and $10 = $0) then EVEXx := 1;
  3700. end;
  3701. &370: begin
  3702. VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  3703. EVEXmmm := $01;
  3704. end;
  3705. &371: begin
  3706. needed_VEX_Extension := true;
  3707. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  3708. EVEXmmm := $02;
  3709. end;
  3710. &372: begin
  3711. needed_VEX_Extension := true;
  3712. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  3713. EVEXmmm := $03;
  3714. end;
  3715. &375: begin
  3716. needed_VEX_Extension := true;
  3717. VEXmmmmm := VEXmmmmm OR $05;
  3718. EVEXmmm := $05; // set opcode map 5
  3719. end;
  3720. &376: begin
  3721. needed_VEX_Extension := true;
  3722. VEXmmmmm := VEXmmmmm OR $06;
  3723. EVEXmmm := $06; // set opcode map 6
  3724. end;
  3725. &377: begin
  3726. needed_VEX_Extension := true;
  3727. VEXmmmmm := VEXmmmmm OR $07;
  3728. EVEXmmm := $07; // set opcode map 7
  3729. end;
  3730. end;
  3731. until false;
  3732. {$ifndef x86_64}
  3733. EVEXv := 1;
  3734. EVEXx := 1;
  3735. EVEXr := 1;
  3736. {$endif}
  3737. if needed_VEX or needed_EVEX then
  3738. begin
  3739. if (opmode > ops) or
  3740. (opmode < -1) then
  3741. begin
  3742. Internalerror(777100);
  3743. end
  3744. else if opmode = -1 then
  3745. begin
  3746. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  3747. EVEXvvvv := $0F;
  3748. {$ifdef x86_64}
  3749. if not(needed_vsib) then EVEXv := 1;
  3750. {$endif x86_64}
  3751. end
  3752. else if oper[opmode]^.typ = top_reg then
  3753. begin
  3754. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  3755. EVEXvvvv := not(regval(oper[opmode]^.reg)) and $07;
  3756. {$ifdef x86_64}
  3757. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  3758. if rexbits(oper[opmode]^.reg) = 0 then EVEXvvvv := EVEXvvvv or (1 shl 3);
  3759. if getsupreg(oper[opmode]^.reg) and $10 = 0 then EVEXv := 1;
  3760. {$else}
  3761. VEXvvvv := VEXvvvv or (1 shl 6);
  3762. EVEXvvvv := EVEXvvvv or (1 shl 3);
  3763. {$endif x86_64}
  3764. end
  3765. else Internalerror(777101);
  3766. if not(needed_VEX_Extension) then
  3767. begin
  3768. {$ifdef x86_64}
  3769. if rex and $0B <> 0 then needed_VEX_Extension := true;
  3770. {$endif x86_64}
  3771. end;
  3772. //TG
  3773. if needed_EVEX and needed_VEX then
  3774. begin
  3775. needed_EVEX := false;
  3776. if CheckUseEVEX then
  3777. begin
  3778. // EVEX-Flags r,v,x indicate extended-MMregister
  3779. // Flag = 0 =>> [x,y,z]mm16..[x,y,z]mm31
  3780. // Flag = 1 =>> [x,y,z]mm00..[x,y,z]mm15
  3781. needed_EVEX := true;
  3782. needed_VEX := false;
  3783. needed_VEX_Extension := false;
  3784. end;
  3785. end;
  3786. if needed_EVEX then
  3787. begin
  3788. EVEXaaa:= 0;
  3789. EVEXz := 0;
  3790. mod11:=true;
  3791. for opidx := 0 to ops - 1 do
  3792. if oper[opidx]^.typ = top_ref then begin mod11:=false; break end;
  3793. for i := 0 to ops - 1 do
  3794. if (oper[i]^.vopext and OTVE_VECTOR_MASK) <> 0 then
  3795. begin
  3796. if oper[i]^.vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  3797. begin
  3798. EVEXaaa := oper[i]^.vopext and $07;
  3799. if oper[i]^.vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then EVEXz := 1;
  3800. end;
  3801. if oper[i]^.vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  3802. begin
  3803. EVEXb := 1;
  3804. end;
  3805. // flag EVEXb is multiple use (broadcast, sae and er)
  3806. if mod11 and (oper[i]^.vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE) then
  3807. begin
  3808. EVEXb := 1;
  3809. if EVEXll = 1 then EVEXu:=0; { AVX10.2 ymmreg_sae }
  3810. end;
  3811. if mod11 and (oper[i]^.vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER) then
  3812. begin
  3813. EVEXb := 1;
  3814. if EVEXll = 1 then EVEXu:=0; { AVX10.2 ymmreg_er }
  3815. case oper[i]^.vopext and OTVE_VECTOR_ER_MASK of
  3816. OTVE_VECTOR_RNSAE: EVEXll := 0;
  3817. OTVE_VECTOR_RDSAE: EVEXll := 1;
  3818. OTVE_VECTOR_RUSAE: EVEXll := 2;
  3819. OTVE_VECTOR_RZSAE: EVEXll := 3;
  3820. else EVEXll := 0;
  3821. end;
  3822. end;
  3823. end;
  3824. bytes[0] := $62;
  3825. bytes[1] := ((EVEXmmm and $07) shl 0) or
  3826. {$ifdef x86_64}
  3827. ((not(rex) and $05) shl 5) or
  3828. {$else}
  3829. (($05) shl 5) or
  3830. {$endif x86_64}
  3831. ((EVEXr and $01) shl 4) or
  3832. ((EVEXx and $01) shl 6);
  3833. bytes[2] := ((EVEXpp and $03) shl 0) or
  3834. ((EVEXu and $01) shl 2) or { EVEX.u if ModRM.mod=11 or EVEX.x4 if ModRM.mod!=11 }
  3835. ((EVEXvvvv and $0F) shl 3) or
  3836. ((EVEXw1 and $01) shl 7);
  3837. bytes[3] := ((EVEXaaa and $07) shl 0) or
  3838. ((EVEXv and $01) shl 3) or
  3839. ((EVEXb and $01) shl 4) or
  3840. ((EVEXll and $03) shl 5) or
  3841. ((EVEXz and $01) shl 7);
  3842. objdata.writebytes(bytes,4);
  3843. end
  3844. else if needed_VEX_Extension then
  3845. begin
  3846. // VEX-Prefix-Length = 3 Bytes
  3847. {$ifdef x86_64}
  3848. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  3849. VEXvvvv := VEXvvvv or ((rex and $08) shl 7); // set REX.w
  3850. {$else}
  3851. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  3852. {$endif x86_64}
  3853. bytes[0]:=$C4;
  3854. bytes[1]:=VEXmmmmm;
  3855. bytes[2]:=VEXvvvv;
  3856. objdata.writebytes(bytes,3);
  3857. end
  3858. else
  3859. begin
  3860. // VEX-Prefix-Length = 2 Bytes
  3861. {$ifdef x86_64}
  3862. if rex and $04 = 0 then
  3863. {$endif x86_64}
  3864. begin
  3865. VEXvvvv := VEXvvvv or (1 shl 7);
  3866. end;
  3867. bytes[0]:=$C5;
  3868. bytes[1]:=VEXvvvv;
  3869. objdata.writebytes(bytes,2);
  3870. end;
  3871. end
  3872. else
  3873. begin
  3874. needed_VEX_Extension := false;
  3875. opmode := -1;
  3876. end;
  3877. if not(needed_EVEX) then
  3878. begin
  3879. for opidx := 0 to ops - 1 do
  3880. begin
  3881. if ops > opidx then
  3882. if (oper[opidx]^.typ=top_reg) and
  3883. (getregtype(oper[opidx]^.reg) = R_MMREGISTER) then
  3884. if getsupreg(oper[opidx]^.reg) and $10 = $10 then
  3885. begin
  3886. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3887. break;
  3888. end;
  3889. //badreg(oper[opidx]^.reg);
  3890. end;
  3891. end;
  3892. { load data to write }
  3893. codes:=insentry^.code;
  3894. repeat
  3895. c:=ord(codes^);
  3896. inc(codes);
  3897. case c of
  3898. &0 :
  3899. break;
  3900. &1,&2,&3 :
  3901. begin
  3902. {$ifdef x86_64}
  3903. if not(needed_VEX or needed_EVEX) then // TG
  3904. maybewriterex;
  3905. {$endif x86_64}
  3906. objdata.writebytes(codes^,c);
  3907. inc(codes,c);
  3908. end;
  3909. &4,&6 :
  3910. begin
  3911. case oper[0]^.reg of
  3912. NR_CS:
  3913. bytes[0]:=$e;
  3914. NR_NO,
  3915. NR_DS:
  3916. bytes[0]:=$1e;
  3917. NR_ES:
  3918. bytes[0]:=$6;
  3919. NR_SS:
  3920. bytes[0]:=$16;
  3921. else
  3922. internalerror(777004);
  3923. end;
  3924. if c=&4 then
  3925. inc(bytes[0]);
  3926. objdata.writebytes(bytes,1);
  3927. end;
  3928. &5,&7 :
  3929. begin
  3930. case oper[0]^.reg of
  3931. NR_FS:
  3932. bytes[0]:=$a0;
  3933. NR_GS:
  3934. bytes[0]:=$a8;
  3935. else
  3936. internalerror(777005);
  3937. end;
  3938. if c=&5 then
  3939. inc(bytes[0]);
  3940. objdata.writebytes(bytes,1);
  3941. end;
  3942. &10,&11,&12 :
  3943. begin
  3944. {$ifdef x86_64}
  3945. if not(needed_VEX or needed_EVEX) then // TG
  3946. maybewriterex;
  3947. {$endif x86_64}
  3948. bytes[0]:=ord(codes^)+regval(oper[c-&10]^.reg);
  3949. inc(codes);
  3950. objdata.writebytes(bytes,1);
  3951. end;
  3952. &13 :
  3953. begin
  3954. bytes[0]:=ord(codes^)+condval[condition];
  3955. inc(codes);
  3956. objdata.writebytes(bytes,1);
  3957. end;
  3958. &14,&15,&16 :
  3959. begin
  3960. getvalsym(c-&14);
  3961. if (currval<-128) or (currval>127) then
  3962. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  3963. if assigned(currsym) then
  3964. objdata_writereloc(currval,1,currsym,currabsreloc)
  3965. else
  3966. objdata.writeint8(shortint(currval));
  3967. end;
  3968. &20,&21,&22 :
  3969. begin
  3970. getvalsym(c-&20);
  3971. if (currval<-256) or (currval>255) then
  3972. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  3973. if assigned(currsym) then
  3974. objdata_writereloc(currval,1,currsym,currabsreloc)
  3975. else
  3976. objdata.writeuint8(byte(currval));
  3977. end;
  3978. &23 :
  3979. begin
  3980. bytes[0]:=ord(codes^)+condval[inverse_cond(condition)];
  3981. inc(codes);
  3982. objdata.writebytes(bytes,1);
  3983. end;
  3984. &24,&25,&26,&27 :
  3985. begin
  3986. getvalsym(c-&24);
  3987. if IF_IMM3 in insentry^.flags then
  3988. begin
  3989. if (currval<0) or (currval>7) then
  3990. Message2(asmw_e_value_exceeds_bounds,'unsigned triad',tostr(currval));
  3991. end
  3992. else if IF_IMM4 in insentry^.flags then
  3993. begin
  3994. if (currval<0) or (currval>15) then
  3995. Message2(asmw_e_value_exceeds_bounds,'unsigned nibble',tostr(currval));
  3996. end
  3997. else
  3998. if (currval<0) or (currval>255) then
  3999. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  4000. if assigned(currsym) then
  4001. objdata_writereloc(currval,1,currsym,currabsreloc)
  4002. else
  4003. objdata.writeuint8(byte(currval));
  4004. end;
  4005. &30,&31,&32 : // 030..032
  4006. begin
  4007. getvalsym(c-&30);
  4008. {$ifndef i8086}
  4009. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  4010. if (currval<-65536) or (currval>65535) then
  4011. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  4012. {$endif i8086}
  4013. if assigned(currsym)
  4014. {$ifdef i8086}
  4015. or (currabsreloc in [RELOC_DGROUP,RELOC_FARDATASEG])
  4016. {$endif i8086}
  4017. then
  4018. objdata_writereloc(currval,2,currsym,currabsreloc)
  4019. else
  4020. objdata.writeInt16LE(int16(currval));
  4021. end;
  4022. &34,&35,&36 : // 034..036
  4023. { !!! These are intended (and used in opcode table) to select depending
  4024. on address size, *not* operand size. Works by coincidence only. }
  4025. begin
  4026. getvalsym(c-&34);
  4027. {$ifdef i8086}
  4028. if assigned(currsym) then
  4029. objdata_writereloc(currval,2,currsym,currabsreloc)
  4030. else
  4031. objdata.writeInt16LE(int16(currval));
  4032. {$else i8086}
  4033. if opsize=S_Q then
  4034. begin
  4035. if assigned(currsym) then
  4036. objdata_writereloc(currval,8,currsym,currabsreloc)
  4037. else
  4038. objdata.writeInt64LE(int64(currval));
  4039. end
  4040. else
  4041. begin
  4042. if assigned(currsym) then
  4043. objdata_writereloc(currval,4,currsym,currabsreloc32)
  4044. else
  4045. objdata.writeInt32LE(int32(currval));
  4046. end
  4047. {$endif i8086}
  4048. end;
  4049. &40,&41,&42 : // 040..042
  4050. begin
  4051. getvalsym(c-&40);
  4052. if assigned(currsym)
  4053. {$ifdef i8086}
  4054. or (currabsreloc in [RELOC_DGROUP,RELOC_FARDATASEG])
  4055. {$endif i8086}
  4056. then
  4057. objdata_writereloc(currval,4,currsym,currabsreloc32)
  4058. else
  4059. objdata.writeInt32LE(int32(currval));
  4060. end;
  4061. &44,&45,&46 :// 044..046 - select between word/dword/qword depending on
  4062. begin // address size (we support only default address sizes).
  4063. getvalsym(c-&44);
  4064. {$if defined(x86_64)}
  4065. if assigned(currsym) then
  4066. objdata_writereloc(currval,8,currsym,currabsreloc)
  4067. else
  4068. objdata.writeInt64LE(int64(currval));
  4069. {$elseif defined(i386)}
  4070. if assigned(currsym) then
  4071. objdata_writereloc(currval,4,currsym,currabsreloc32)
  4072. else
  4073. objdata.writeInt32LE(int32(currval));
  4074. {$elseif defined(i8086)}
  4075. if assigned(currsym) then
  4076. objdata_writereloc(currval,2,currsym,currabsreloc)
  4077. else
  4078. objdata.writeInt16LE(int16(currval));
  4079. {$endif}
  4080. end;
  4081. &50,&51,&52 : // 050..052 - byte relative operand
  4082. begin
  4083. getvalsym(c-&50);
  4084. data:=currval-insend;
  4085. {$push}
  4086. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  4087. if assigned(currsym) then
  4088. inc(data,currsym.address);
  4089. {$pop}
  4090. if (data>127) or (data<-128) then
  4091. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  4092. objdata.writeint8(shortint(data));
  4093. end;
  4094. &54,&55,&56: // 054..056 - qword immediate operand
  4095. begin
  4096. getvalsym(c-&54);
  4097. if assigned(currsym) then
  4098. objdata_writereloc(currval,8,currsym,currabsreloc)
  4099. else
  4100. objdata.writeInt64LE(int64(currval));
  4101. end;
  4102. &60,&61,&62 :
  4103. begin
  4104. getvalsym(c-&60);
  4105. {$ifdef i8086}
  4106. if assigned(currsym) then
  4107. objdata_writereloc(currval,2,currsym,currrelreloc)
  4108. else
  4109. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  4110. {$else i8086}
  4111. InternalError(2020100821);
  4112. {$endif i8086}
  4113. end;
  4114. &64,&65,&66 : // 064..066 - select between 16/32 address mode, but we support only 32 (only 16 on i8086)
  4115. begin
  4116. getvalsym(c-&64);
  4117. {$ifdef i8086}
  4118. if assigned(currsym) then
  4119. objdata_writereloc(currval,2,currsym,currrelreloc)
  4120. else
  4121. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  4122. {$else i8086}
  4123. if assigned(currsym) then
  4124. objdata_writereloc(currval,4,currsym,currrelreloc)
  4125. else
  4126. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  4127. {$endif i8086}
  4128. end;
  4129. &70,&71,&72 : // 070..072 - long relative operand
  4130. begin
  4131. getvalsym(c-&70);
  4132. if assigned(currsym) then
  4133. objdata_writereloc(currval,4,currsym,currrelreloc)
  4134. else
  4135. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  4136. end;
  4137. &74,&75,&76 : ; // 074..076 - vex-coded vector operand
  4138. // ignore
  4139. &240..&243:
  4140. begin
  4141. bytes[0]:=$C0 or ((byte(oper[c and 7]^.reg) and 7) shl 3); {ModRM 11:rrr:000}
  4142. objdata.writebytes(bytes,1);
  4143. end;
  4144. &254,&255,&256 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  4145. begin
  4146. getvalsym(c-&254);
  4147. {$ifdef x86_64}
  4148. { for i386 as aint type is longint the
  4149. following test is useless }
  4150. if (currval<low(longint)) or (currval>high(longint)) then
  4151. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  4152. {$endif x86_64}
  4153. if assigned(currsym) then
  4154. objdata_writereloc(currval,4,currsym,currabsreloc32)
  4155. else
  4156. objdata.writeInt32LE(int32(currval));
  4157. end;
  4158. &300,&301,&302:
  4159. begin
  4160. {$if defined(x86_64) or defined(i8086)}
  4161. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  4162. write0x67prefix(objdata);
  4163. {$endif x86_64 or i8086}
  4164. end;
  4165. &310 : { fixed 16-bit addr }
  4166. {$if defined(x86_64)}
  4167. { every insentry having code 0310 must be marked with NOX86_64 }
  4168. InternalError(2011051302);
  4169. {$elseif defined(i386)}
  4170. write0x67prefix(objdata);
  4171. {$elseif defined(i8086)}
  4172. {nothing};
  4173. {$endif}
  4174. &311 : { fixed 32-bit addr }
  4175. {$if defined(x86_64) or defined(i8086)}
  4176. write0x67prefix(objdata)
  4177. {$endif x86_64 or i8086}
  4178. ;
  4179. &320,&321,&322 :
  4180. begin
  4181. case oper[c-&320]^.ot and OT_SIZE_MASK of
  4182. {$if defined(i386) or defined(x86_64)}
  4183. OT_BITS16 :
  4184. {$elseif defined(i8086)}
  4185. OT_BITS32 :
  4186. {$endif}
  4187. write0x66prefix(objdata);
  4188. {$ifndef x86_64}
  4189. OT_BITS64 :
  4190. Message(asmw_e_64bit_not_supported);
  4191. {$endif x86_64}
  4192. end;
  4193. end;
  4194. &323 : {no action needed};
  4195. &325:
  4196. {$ifdef i8086}
  4197. write0x66prefix(objdata);
  4198. {$else i8086}
  4199. {no action needed};
  4200. {$endif i8086}
  4201. &324,
  4202. &361:
  4203. begin
  4204. {$ifndef i8086}
  4205. if not(needed_VEX or needed_EVEX) then
  4206. write0x66prefix(objdata);
  4207. {$endif not i8086}
  4208. end;
  4209. &326 :
  4210. begin
  4211. {$ifndef x86_64}
  4212. Message(asmw_e_64bit_not_supported);
  4213. {$endif x86_64}
  4214. end;
  4215. &333 :
  4216. begin
  4217. if not(needed_VEX or needed_EVEX) then
  4218. begin
  4219. bytes[0]:=$f3;
  4220. objdata.writebytes(bytes,1);
  4221. end;
  4222. end;
  4223. &334 :
  4224. begin
  4225. if not(needed_VEX or needed_EVEX) then
  4226. begin
  4227. bytes[0]:=$f2;
  4228. objdata.writebytes(bytes,1);
  4229. end;
  4230. end;
  4231. &335:
  4232. ;
  4233. &336: ; // indicates 32-bit scalar vector operand {no action needed}
  4234. &337: ; // indicates 64-bit scalar vector operand {no action needed}
  4235. &312,
  4236. &327,
  4237. &331,&332 :
  4238. begin
  4239. { these are dissambler hints or 32 bit prefixes which
  4240. are not needed }
  4241. end;
  4242. &362..&364: ; // VEX flags =>> nothing todo
  4243. &366, &367:
  4244. begin
  4245. opidx:=c-&364; { 0366->operand 2, 0367->operand 3 }
  4246. if (needed_VEX or needed_EVEX) and
  4247. (ops=4) and
  4248. (oper[opidx]^.typ=top_reg) and
  4249. (
  4250. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_xmm) or
  4251. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_ymm) or
  4252. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_zmm) or
  4253. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_tmm)
  4254. ) then
  4255. begin
  4256. bytes[0] := ((getsupreg(oper[opidx]^.reg) and 15) shl 4);
  4257. objdata.writebytes(bytes,1);
  4258. end
  4259. else
  4260. Internalerror(2014032001);
  4261. end;
  4262. &350..&352: ; // EVEX flags =>> nothing todo
  4263. &370..&377: ; // VEX and EVEX flags =>> nothing todo
  4264. &37:
  4265. begin
  4266. {$ifdef i8086}
  4267. if assigned(currsym) then
  4268. objdata_writereloc(0,2,currsym,RELOC_SEG)
  4269. else
  4270. InternalError(2015041503);
  4271. {$else i8086}
  4272. InternalError(2020100822);
  4273. {$endif i8086}
  4274. end;
  4275. else
  4276. begin
  4277. { rex should be written at this point }
  4278. {$ifdef x86_64}
  4279. if not(needed_VEX or needed_EVEX) then // TG
  4280. if (rex<>0) and not(rexwritten) then
  4281. internalerror(200603191);
  4282. {$endif x86_64}
  4283. if (c>=&100) and (c<=&227) then // 0100..0227
  4284. begin
  4285. if (c<&177) then // 0177
  4286. begin
  4287. if (oper[c and 7]^.typ=top_reg) then
  4288. rfield:=regval(oper[c and 7]^.reg)
  4289. else
  4290. rfield:=regval(oper[c and 7]^.ref^.base);
  4291. end
  4292. else
  4293. rfield:=c and 7;
  4294. opidx:=(c shr 3) and 7;
  4295. {$ifdef x86_64}
  4296. if not process_ea(oper[opidx]^,ea_data,rfield, EVEXTupleState = etsNotTuple, (insentry^.optypes[(c shr 3) and 7] and ot_sibmem)=ot_sibmem) then
  4297. {$else x86_64}
  4298. if not process_ea(oper[opidx]^,ea_data,rfield, EVEXTupleState = etsNotTuple) then
  4299. {$endif x86_64}
  4300. Message(asmw_e_invalid_effective_address);
  4301. pb:=@bytes[0];
  4302. pb^:=ea_data.modrm;
  4303. inc(pb);
  4304. if ea_data.sib_present then
  4305. begin
  4306. pb^:=ea_data.sib;
  4307. inc(pb);
  4308. end;
  4309. s:=pb-@bytes[0];
  4310. objdata.writebytes(bytes,s);
  4311. case ea_data.bytes of
  4312. 0 : ;
  4313. 1 :
  4314. begin
  4315. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  4316. begin
  4317. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  4318. {$ifdef i386}
  4319. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  4320. (tf_pic_uses_got in target_info.flags) then
  4321. currabsreloc:=RELOC_GOT32
  4322. else
  4323. {$endif i386}
  4324. {$ifdef x86_64}
  4325. if oper[opidx]^.ref^.refaddr=addr_pic then
  4326. currabsreloc:=RELOC_GOTPCREL
  4327. else
  4328. {$endif x86_64}
  4329. currabsreloc:=RELOC_ABSOLUTE;
  4330. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  4331. end
  4332. else
  4333. begin
  4334. bytes[0]:=oper[opidx]^.ref^.offset;
  4335. objdata.writebytes(bytes,1);
  4336. end;
  4337. inc(s);
  4338. end;
  4339. 2,4 :
  4340. begin
  4341. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  4342. currval:=oper[opidx]^.ref^.offset;
  4343. {$ifdef x86_64}
  4344. if oper[opidx]^.ref^.refaddr=addr_pic then
  4345. currabsreloc:=RELOC_GOTPCREL
  4346. else if oper[opidx]^.ref^.refaddr=addr_tlsgd then
  4347. currabsreloc:=RELOC_TLSGD
  4348. else if oper[opidx]^.ref^.refaddr=addr_tpoff then
  4349. currabsreloc:=RELOC_TPOFF
  4350. else
  4351. if oper[opidx]^.ref^.base=NR_RIP then
  4352. begin
  4353. currabsreloc:=RELOC_RELATIVE;
  4354. { Adjust reloc value by number of bytes following the displacement,
  4355. but not if displacement is specified by literal constant }
  4356. if Assigned(currsym) then
  4357. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  4358. end
  4359. else
  4360. {$endif x86_64}
  4361. {$ifdef i386}
  4362. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  4363. (tf_pic_uses_got in target_info.flags) then
  4364. currabsreloc:=RELOC_GOT32
  4365. else if oper[opidx]^.ref^.refaddr=addr_tlsgd then
  4366. currabsreloc:=RELOC_TLSGD
  4367. else if oper[opidx]^.ref^.refaddr=addr_ntpoff then
  4368. currabsreloc:=RELOC_NTPOFF
  4369. else
  4370. {$endif i386}
  4371. {$ifdef i8086}
  4372. if ea_data.bytes=2 then
  4373. currabsreloc:=RELOC_ABSOLUTE
  4374. else
  4375. {$endif i8086}
  4376. currabsreloc:=RELOC_ABSOLUTE32;
  4377. if (currabsreloc in [RELOC_ABSOLUTE32{$ifdef i8086},RELOC_ABSOLUTE{$endif}]) and
  4378. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  4379. begin
  4380. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  4381. if relsym.objsection=objdata.CurrObjSec then
  4382. begin
  4383. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  4384. {$ifdef i8086}
  4385. if ea_data.bytes=4 then
  4386. currabsreloc:=RELOC_RELATIVE32
  4387. else
  4388. {$endif i8086}
  4389. currabsreloc:=RELOC_RELATIVE;
  4390. end
  4391. else
  4392. begin
  4393. currabsreloc:=RELOC_PIC_PAIR;
  4394. currval:=relsym.offset;
  4395. end;
  4396. end;
  4397. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  4398. inc(s,ea_data.bytes);
  4399. end;
  4400. end;
  4401. end
  4402. else
  4403. InternalError(777007);
  4404. end;
  4405. end;
  4406. until false;
  4407. end;
  4408. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  4409. begin
  4410. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  4411. (regtype = R_INTREGISTER) and
  4412. (ops=2) and
  4413. (oper[0]^.typ=top_reg) and
  4414. (oper[1]^.typ=top_reg) and
  4415. (oper[0]^.reg=oper[1]^.reg)
  4416. ) or
  4417. ({ checking the opcodes is a long "or" chain, so check first the registers which is more selective }
  4418. ((regtype = R_MMREGISTER) and
  4419. (ops=2) and
  4420. (oper[0]^.typ=top_reg) and
  4421. (oper[1]^.typ=top_reg) and
  4422. (oper[0]^.reg=oper[1]^.reg)) and
  4423. (
  4424. (opcode=A_MOVSS) or (opcode=A_MOVSD) or
  4425. (opcode=A_MOVQ) or (opcode=A_MOVD) or
  4426. (opcode=A_MOVAPS) or (opcode=A_MOVAPD) or
  4427. (opcode=A_MOVUPS) or (opcode=A_MOVUPD) or
  4428. (opcode=A_MOVDQA) or (opcode=A_MOVDQU) or
  4429. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or
  4430. (opcode=A_VMOVQ) or (opcode=A_VMOVD) or
  4431. (opcode=A_VMOVAPS) or (opcode=A_VMOVAPD) or
  4432. (opcode=A_VMOVUPS) or (opcode=A_VMOVUPD) or
  4433. (opcode=A_VMOVDQA) or (opcode=A_VMOVDQU)
  4434. )
  4435. );
  4436. end;
  4437. procedure build_spilling_operation_type_table;
  4438. var
  4439. opcode : tasmop;
  4440. begin
  4441. new(operation_type_table);
  4442. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  4443. for opcode:=low(tasmop) to high(tasmop) do
  4444. with InsProp[opcode] do
  4445. begin
  4446. if Ch_Rop1 in Ch then
  4447. operation_type_table^[opcode,0]:=operand_read;
  4448. if Ch_Wop1 in Ch then
  4449. operation_type_table^[opcode,0]:=operand_write;
  4450. if [Ch_RWop1,Ch_Mop1]*Ch<>[] then
  4451. operation_type_table^[opcode,0]:=operand_readwrite;
  4452. if Ch_Rop2 in Ch then
  4453. operation_type_table^[opcode,1]:=operand_read;
  4454. if Ch_Wop2 in Ch then
  4455. operation_type_table^[opcode,1]:=operand_write;
  4456. if [Ch_RWop2,Ch_Mop2]*Ch<>[] then
  4457. operation_type_table^[opcode,1]:=operand_readwrite;
  4458. if Ch_Rop3 in Ch then
  4459. operation_type_table^[opcode,2]:=operand_read;
  4460. if Ch_Wop3 in Ch then
  4461. operation_type_table^[opcode,2]:=operand_write;
  4462. if [Ch_RWop3,Ch_Mop3]*Ch<>[] then
  4463. operation_type_table^[opcode,2]:=operand_readwrite;
  4464. if Ch_Rop4 in Ch then
  4465. operation_type_table^[opcode,3]:=operand_read;
  4466. if Ch_Wop4 in Ch then
  4467. operation_type_table^[opcode,3]:=operand_write;
  4468. if [Ch_RWop4,Ch_Mop4]*Ch<>[] then
  4469. operation_type_table^[opcode,3]:=operand_readwrite;
  4470. end;
  4471. end;
  4472. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  4473. begin
  4474. { the information in the instruction table is made for the string copy
  4475. operation MOVSD so hack here (FK)
  4476. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  4477. so fix it here (FK)
  4478. }
  4479. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  4480. begin
  4481. case opnr of
  4482. 0:
  4483. result:=operand_read;
  4484. 1:
  4485. result:=operand_write;
  4486. else
  4487. internalerror(200506055);
  4488. end
  4489. end
  4490. else if (opcode=A_VMOVHPD) or (opcode=A_VMOVHPS) or (opcode=A_VMOVLHPS) or (opcode=A_VMOVLPD) or (opcode=A_VMOVLPS) then
  4491. begin
  4492. if ops=2 then
  4493. case opnr of
  4494. 0:
  4495. result:=operand_read;
  4496. 1:
  4497. result:=operand_readwrite;
  4498. else
  4499. internalerror(2024060101);
  4500. end
  4501. else if ops=3 then
  4502. case opnr of
  4503. 0,1:
  4504. result:=operand_read;
  4505. 2:
  4506. result:=operand_write;
  4507. else
  4508. internalerror(2024060102);
  4509. end
  4510. else
  4511. internalerror(2024060103);
  4512. end
  4513. { IMUL has 1, 2 and 3-operand forms }
  4514. else if opcode=A_IMUL then
  4515. begin
  4516. case ops of
  4517. 1:
  4518. if opnr=0 then
  4519. result:=operand_read
  4520. else
  4521. internalerror(2014011802);
  4522. 2:
  4523. begin
  4524. case opnr of
  4525. 0:
  4526. result:=operand_read;
  4527. 1:
  4528. result:=operand_readwrite;
  4529. else
  4530. internalerror(2014011803);
  4531. end;
  4532. end;
  4533. 3:
  4534. begin
  4535. case opnr of
  4536. 0,1:
  4537. result:=operand_read;
  4538. 2:
  4539. result:=operand_write;
  4540. else
  4541. internalerror(2014011804);
  4542. end;
  4543. end;
  4544. else
  4545. internalerror(2014011805);
  4546. end;
  4547. end
  4548. else
  4549. result:=operation_type_table^[opcode,opnr];
  4550. end;
  4551. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  4552. var
  4553. tmpref: treference;
  4554. begin
  4555. tmpref:=ref;
  4556. {$ifdef i8086}
  4557. if tmpref.segment=NR_SS then
  4558. tmpref.segment:=NR_NO;
  4559. {$endif i8086}
  4560. case getregtype(r) of
  4561. R_INTREGISTER :
  4562. begin
  4563. if getsubreg(r)=R_SUBH then
  4564. inc(tmpref.offset);
  4565. { we don't need special code here for 32 bit loads on x86_64, since
  4566. those will automatically zero-extend the upper 32 bits. }
  4567. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  4568. end;
  4569. R_MMREGISTER :
  4570. if current_settings.fputype in fpu_avx_instructionsets then
  4571. case getsubreg(r) of
  4572. R_SUBMMD:
  4573. result:=taicpu.op_ref_reg(A_VMOVSD,S_NO,tmpref,r);
  4574. R_SUBMMS:
  4575. result:=taicpu.op_ref_reg(A_VMOVSS,S_NO,tmpref,r);
  4576. R_SUBQ,
  4577. R_SUBMMWHOLE:
  4578. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,tmpref,r);
  4579. R_SUBMMY:
  4580. if ref.alignment>=32 then
  4581. result:=taicpu.op_ref_reg(A_VMOVDQA,S_NO,tmpref,r)
  4582. else
  4583. result:=taicpu.op_ref_reg(A_VMOVDQU,S_NO,tmpref,r);
  4584. R_SUBMMZ:
  4585. if ref.alignment>=64 then
  4586. result:=taicpu.op_ref_reg(A_VMOVDQA64,S_NO,tmpref,r)
  4587. else
  4588. result:=taicpu.op_ref_reg(A_VMOVDQU64,S_NO,tmpref,r);
  4589. R_SUBMMX:
  4590. result:=taicpu.op_ref_reg(A_VMOVDQU,S_NO,tmpref,r);
  4591. else
  4592. internalerror(200506043);
  4593. end
  4594. else
  4595. case getsubreg(r) of
  4596. R_SUBMMD:
  4597. result:=taicpu.op_ref_reg(A_MOVSD,S_NO,tmpref,r);
  4598. R_SUBMMS:
  4599. result:=taicpu.op_ref_reg(A_MOVSS,S_NO,tmpref,r);
  4600. R_SUBQ,
  4601. R_SUBMMWHOLE:
  4602. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,r);
  4603. R_SUBMMX:
  4604. result:=taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,r);
  4605. else
  4606. internalerror(2005060405);
  4607. end;
  4608. else
  4609. internalerror(2004010411);
  4610. end;
  4611. end;
  4612. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  4613. var
  4614. size: topsize;
  4615. tmpref: treference;
  4616. begin
  4617. tmpref:=ref;
  4618. {$ifdef i8086}
  4619. if tmpref.segment=NR_SS then
  4620. tmpref.segment:=NR_NO;
  4621. {$endif i8086}
  4622. case getregtype(r) of
  4623. R_INTREGISTER :
  4624. begin
  4625. if getsubreg(r)=R_SUBH then
  4626. inc(tmpref.offset);
  4627. size:=reg2opsize(r);
  4628. {$ifdef x86_64}
  4629. { even if it's a 32 bit reg, we still have to spill 64 bits
  4630. because we often perform 64 bit operations on them }
  4631. if (size=S_L) then
  4632. begin
  4633. size:=S_Q;
  4634. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  4635. end;
  4636. {$endif x86_64}
  4637. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  4638. end;
  4639. R_MMREGISTER :
  4640. if current_settings.fputype in fpu_avx_instructionsets then
  4641. case getsubreg(r) of
  4642. R_SUBMMD:
  4643. result:=taicpu.op_reg_ref(A_VMOVSD,S_NO,r,tmpref);
  4644. R_SUBMMS:
  4645. result:=taicpu.op_reg_ref(A_VMOVSS,S_NO,r,tmpref);
  4646. R_SUBMMY:
  4647. if ref.alignment>=32 then
  4648. result:=taicpu.op_reg_ref(A_VMOVDQA,S_NO,r,tmpref)
  4649. else
  4650. result:=taicpu.op_reg_ref(A_VMOVDQU,S_NO,r,tmpref);
  4651. R_SUBMMZ:
  4652. if ref.alignment>=64 then
  4653. result:=taicpu.op_reg_ref(A_VMOVDQA64,S_NO,r,tmpref)
  4654. else
  4655. result:=taicpu.op_reg_ref(A_VMOVDQU64,S_NO,r,tmpref);
  4656. R_SUBQ,
  4657. R_SUBMMWHOLE:
  4658. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,tmpref);
  4659. else
  4660. internalerror(200506042);
  4661. end
  4662. else
  4663. case getsubreg(r) of
  4664. R_SUBMMD:
  4665. result:=taicpu.op_reg_ref(A_MOVSD,S_NO,r,tmpref);
  4666. R_SUBMMS:
  4667. result:=taicpu.op_reg_ref(A_MOVSS,S_NO,r,tmpref);
  4668. R_SUBQ,
  4669. R_SUBMMWHOLE:
  4670. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,tmpref);
  4671. R_SUBMMX:
  4672. result:=taicpu.op_reg_ref(A_MOVDQA,S_NO,r,tmpref);
  4673. else
  4674. internalerror(2005060404);
  4675. end;
  4676. else
  4677. internalerror(2004010412);
  4678. end;
  4679. end;
  4680. {$ifdef i8086}
  4681. procedure taicpu.loadsegsymbol(opidx:longint;s:tasmsymbol);
  4682. var
  4683. r: treference;
  4684. begin
  4685. reference_reset_symbol(r,s,0,1,[]);
  4686. r.refaddr:=addr_seg;
  4687. loadref(opidx,r);
  4688. end;
  4689. {$endif i8086}
  4690. {*****************************************************************************
  4691. Instruction table
  4692. *****************************************************************************}
  4693. procedure BuildInsTabCache;
  4694. var
  4695. i : longint;
  4696. begin
  4697. new(instabcache);
  4698. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  4699. i:=0;
  4700. while (i<InsTabEntries) do
  4701. begin
  4702. if InsTabCache^[InsTab[i].OPcode]=-1 then
  4703. InsTabCache^[InsTab[i].OPcode]:=i;
  4704. inc(i);
  4705. end;
  4706. end;
  4707. procedure BuildInsTabMemRefSizeInfoCache;
  4708. var
  4709. AsmOp: TasmOp;
  4710. i,j: longint;
  4711. iCntOpcodeValError: longint;
  4712. insentry : PInsEntry;
  4713. MRefInfo: TMemRefSizeInfo;
  4714. SConstInfo: TConstSizeInfo;
  4715. actRegSize: int64;
  4716. actMemSize: int64;
  4717. actConstSize: int64;
  4718. actRegCount: integer;
  4719. actMemCount: integer;
  4720. actConstCount: integer;
  4721. actRegTypes : int64;
  4722. actRegMemTypes: int64;
  4723. NewRegSize: int64;
  4724. actVMemCount : integer;
  4725. actVMemTypes : int64;
  4726. RegMMXSizeMask: int64;
  4727. RegXMMSizeMask: int64;
  4728. RegYMMSizeMask: int64;
  4729. RegZMMSizeMask: int64;
  4730. RegMMXConstSizeMask: int64;
  4731. RegXMMConstSizeMask: int64;
  4732. RegYMMConstSizeMask: int64;
  4733. RegZMMConstSizeMask: int64;
  4734. RegBCSTSizeMask: int64;
  4735. RegBCSTXMMSizeMask: int64;
  4736. RegBCSTYMMSizeMask: int64;
  4737. RegBCSTZMMSizeMask: int64;
  4738. ExistsMemRef : boolean;
  4739. bitcount : integer;
  4740. ExistsCode336 : boolean;
  4741. ExistsCode337 : boolean;
  4742. ExistsSSEAVXReg : boolean;
  4743. hs1,hs2 : String;
  4744. begin
  4745. new(InsTabMemRefSizeInfoCache);
  4746. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  4747. iCntOpcodeValError := 0;
  4748. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  4749. begin
  4750. i := InsTabCache^[AsmOp];
  4751. if i >= 0 then
  4752. begin
  4753. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnknown;
  4754. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSizeBCST := msbUnknown;
  4755. InsTabMemRefSizeInfoCache^[AsmOp].BCSTXMMMultiplicator := 0;
  4756. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnknown;
  4757. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  4758. InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes := [];
  4759. insentry:=@instab[i];
  4760. RegMMXSizeMask := 0;
  4761. RegXMMSizeMask := 0;
  4762. RegYMMSizeMask := 0;
  4763. RegZMMSizeMask := 0;
  4764. RegMMXConstSizeMask := 0;
  4765. RegXMMConstSizeMask := 0;
  4766. RegYMMConstSizeMask := 0;
  4767. RegZMMConstSizeMask := 0;
  4768. RegBCSTSizeMask:= 0;
  4769. RegBCSTXMMSizeMask := 0;
  4770. RegBCSTYMMSizeMask := 0;
  4771. RegBCSTZMMSizeMask := 0;
  4772. ExistsMemRef := false;
  4773. while (insentry<=@instab[high(instab)]) and
  4774. (insentry^.opcode=AsmOp) do
  4775. begin
  4776. MRefInfo := msiUnknown;
  4777. actRegSize := 0;
  4778. actRegCount := 0;
  4779. actRegTypes := 0;
  4780. NewRegSize := 0;
  4781. actMemSize := 0;
  4782. actMemCount := 0;
  4783. actRegMemTypes := 0;
  4784. actVMemCount := 0;
  4785. actVMemTypes := 0;
  4786. actConstSize := 0;
  4787. actConstCount := 0;
  4788. ExistsCode336 := false; // indicate fixed operand size 32 bit
  4789. ExistsCode337 := false; // indicate fixed operand size 64 bit
  4790. ExistsSSEAVXReg := false;
  4791. // parse insentry^.code for &336 and &337
  4792. // &336 (octal) = 222 (decimal) == fixed operand size 32 bit
  4793. // &337 (octal) = 223 (decimal) == fixed operand size 64 bit
  4794. for i := low(insentry^.code) to high(insentry^.code) do
  4795. begin
  4796. case insentry^.code[i] of
  4797. #222: ExistsCode336 := true;
  4798. #223: ExistsCode337 := true;
  4799. #0,#1,#2,#3: break;
  4800. end;
  4801. end;
  4802. for i := 0 to insentry^.ops -1 do
  4803. begin
  4804. if (insentry^.optypes[i] and OT_REGISTER) = OT_REGISTER then
  4805. case insentry^.optypes[i] and (OT_TMMREG or OT_XMMREG or OT_YMMREG or OT_ZMMREG or OT_KREG or OT_REG_EXTRA_MASK) of
  4806. OT_TMMREG,
  4807. OT_XMMREG,
  4808. OT_YMMREG,
  4809. OT_ZMMREG: ExistsSSEAVXReg := true;
  4810. else;
  4811. end;
  4812. end;
  4813. for j := 0 to insentry^.ops -1 do
  4814. begin
  4815. if ((insentry^.optypes[j] and OT_XMEM32) = OT_XMEM32) OR
  4816. ((insentry^.optypes[j] and OT_XMEM64) = OT_XMEM64) OR
  4817. ((insentry^.optypes[j] and OT_YMEM32) = OT_YMEM32) OR
  4818. ((insentry^.optypes[j] and OT_YMEM64) = OT_YMEM64) OR
  4819. ((insentry^.optypes[j] and OT_ZMEM32) = OT_ZMEM32) OR
  4820. ((insentry^.optypes[j] and OT_ZMEM64) = OT_ZMEM64) then
  4821. begin
  4822. inc(actVMemCount);
  4823. case insentry^.optypes[j] and (OT_XMEM32 OR OT_XMEM64 OR OT_YMEM32 OR OT_YMEM64 OR OT_ZMEM32 OR OT_ZMEM64) of
  4824. OT_XMEM32: actVMemTypes := actVMemTypes or OT_XMEM32;
  4825. OT_XMEM64: actVMemTypes := actVMemTypes or OT_XMEM64;
  4826. OT_YMEM32: actVMemTypes := actVMemTypes or OT_YMEM32;
  4827. OT_YMEM64: actVMemTypes := actVMemTypes or OT_YMEM64;
  4828. OT_ZMEM32: actVMemTypes := actVMemTypes or OT_ZMEM32;
  4829. OT_ZMEM64: actVMemTypes := actVMemTypes or OT_ZMEM64;
  4830. else InternalError(777206);
  4831. end;
  4832. end
  4833. else if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  4834. begin
  4835. inc(actRegCount);
  4836. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  4837. if NewRegSize = 0 then
  4838. begin
  4839. case insentry^.optypes[j] and (OT_MMXREG or OT_TMMREG or OT_XMMREG or OT_YMMREG or OT_ZMMREG or OT_KREG or OT_REG_EXTRA_MASK) of
  4840. OT_MMXREG: begin
  4841. NewRegSize := OT_BITS64;
  4842. end;
  4843. OT_XMMREG: begin
  4844. NewRegSize := OT_BITS128;
  4845. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  4846. end;
  4847. OT_YMMREG: begin
  4848. NewRegSize := OT_BITS256;
  4849. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  4850. end;
  4851. OT_ZMMREG: begin
  4852. NewRegSize := OT_BITS512;
  4853. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  4854. end;
  4855. OT_KREG: begin
  4856. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  4857. end;
  4858. OT_TMMREG: begin
  4859. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  4860. end;
  4861. else NewRegSize := not(0);
  4862. end;
  4863. end;
  4864. actRegSize := actRegSize or NewRegSize;
  4865. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG or OT_TMMREG or OT_XMMREG or OT_YMMREG or OT_ZMMREG or OT_KREG or OT_REG_EXTRA_MASK));
  4866. end
  4867. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  4868. begin
  4869. inc(actMemCount);
  4870. if ExistsSSEAVXReg and ExistsCode336 then
  4871. actMemSize := actMemSize or OT_BITS32
  4872. else if ExistsSSEAVXReg and ExistsCode337 then
  4873. actMemSize := actMemSize or OT_BITS64
  4874. else
  4875. actMemSize:=actMemSize or (insentry^.optypes[j] and (OT_SIZE_MASK OR OT_VECTORBCST));
  4876. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  4877. begin
  4878. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  4879. end;
  4880. end
  4881. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  4882. begin
  4883. inc(actConstCount);
  4884. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  4885. end
  4886. end;
  4887. if actConstCount > 0 then
  4888. begin
  4889. case actConstSize of
  4890. 0: SConstInfo := csiNoSize;
  4891. OT_BITS8: SConstInfo := csiMem8;
  4892. OT_BITS16: SConstInfo := csiMem16;
  4893. OT_BITS32: SConstInfo := csiMem32;
  4894. OT_BITS64: SConstInfo := csiMem64;
  4895. else SConstInfo := csiMultiple;
  4896. end;
  4897. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnknown then
  4898. begin
  4899. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  4900. end
  4901. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  4902. begin
  4903. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  4904. end;
  4905. end;
  4906. if actVMemCount > 0 then
  4907. begin
  4908. if actVMemCount = 1 then
  4909. begin
  4910. if actVMemTypes > 0 then
  4911. begin
  4912. case actVMemTypes of
  4913. OT_XMEM32: MRefInfo := msiXMem32;
  4914. OT_XMEM64: MRefInfo := msiXMem64;
  4915. OT_YMEM32: MRefInfo := msiYMem32;
  4916. OT_YMEM64: MRefInfo := msiYMem64;
  4917. OT_ZMEM32: MRefInfo := msiZMem32;
  4918. OT_ZMEM64: MRefInfo := msiZMem64;
  4919. else InternalError(777208);
  4920. end;
  4921. case actRegTypes of
  4922. OT_XMMREG: case MRefInfo of
  4923. msiXMem32,
  4924. msiXMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS128;
  4925. msiYMem32,
  4926. msiYMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS256;
  4927. msiZMem32,
  4928. msiZMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS512;
  4929. else InternalError(777210);
  4930. end;
  4931. OT_YMMREG: case MRefInfo of
  4932. msiXMem32,
  4933. msiXMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS128;
  4934. msiYMem32,
  4935. msiYMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS256;
  4936. msiZMem32,
  4937. msiZMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS512;
  4938. else InternalError(2020100823);
  4939. end;
  4940. OT_ZMMREG: case MRefInfo of
  4941. msiXMem32,
  4942. msiXMem64: RegZMMSizeMask := RegZMMSizeMask or OT_BITS128;
  4943. msiYMem32,
  4944. msiYMem64: RegZMMSizeMask := RegZMMSizeMask or OT_BITS256;
  4945. msiZMem32,
  4946. msiZMem64: RegZMMSizeMask := RegZMMSizeMask or OT_BITS512;
  4947. else InternalError(2020100824);
  4948. end;
  4949. //else InternalError(777209);
  4950. end;
  4951. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnknown then
  4952. begin
  4953. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  4954. end
  4955. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  4956. begin
  4957. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in [msiXMem32, msiXMem64, msiYMem32, msiYMem64, msiZMem32, msiZMem64] then
  4958. begin
  4959. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiVMemMultiple;
  4960. end
  4961. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> msiVMemMultiple then InternalError(777212);
  4962. end;
  4963. end;
  4964. end
  4965. else InternalError(777207);
  4966. end
  4967. else
  4968. begin
  4969. if (actMemCount=2) and ((AsmOp=A_MOVS) or (AsmOp=A_CMPS)) then actMemCount:=1;
  4970. ExistsMemRef := ExistsMemRef or (actMemCount > 0);
  4971. case actMemCount of
  4972. 0: ; // nothing todo
  4973. 1: begin
  4974. MRefInfo := msiUnknown;
  4975. if not(ExistsCode336 or ExistsCode337) then
  4976. case actRegMemTypes and (OT_MMXRM or OT_XMMRM or OT_YMMRM or OT_ZMMRM or OT_REG_EXTRA_MASK) of
  4977. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  4978. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  4979. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  4980. OT_ZMMRM: actMemSize := actMemSize or OT_BITS512;
  4981. end;
  4982. case actMemSize of
  4983. 0: MRefInfo := msiNoSize;
  4984. OT_BITS8: MRefInfo := msiMem8;
  4985. OT_BITS16: MRefInfo := msiMem16;
  4986. OT_BITSB16: MRefInfo := msiBMem16;
  4987. OT_BITS32: MRefInfo := msiMem32;
  4988. OT_BITSB32: MRefInfo := msiBMem32;
  4989. OT_BITS64: MRefInfo := msiMem64;
  4990. OT_BITSB64: MRefInfo := msiBMem64;
  4991. OT_BITS128: MRefInfo := msiMem128;
  4992. OT_BITS256: MRefInfo := msiMem256;
  4993. OT_BITS512: MRefInfo := msiMem512;
  4994. OT_BITS80,
  4995. OT_FAR,
  4996. OT_NEAR,
  4997. OT_SHORT: ; // ignore
  4998. else
  4999. begin
  5000. bitcount := popcnt(qword(actMemSize));
  5001. if bitcount > 1 then MRefInfo := msiMultiple
  5002. else InternalError(777203);
  5003. end;
  5004. end;
  5005. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnknown then
  5006. begin
  5007. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  5008. end
  5009. else
  5010. begin
  5011. // ignore broadcast-memory
  5012. if not(MRefInfo in [msiBMem16, msiBMem32, msiBMem64]) then
  5013. begin
  5014. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  5015. begin
  5016. with InsTabMemRefSizeInfoCache^[AsmOp] do
  5017. begin
  5018. if ((MemRefSize in [msiMem8, msiMULTIPLEMinSize8]) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultipleMinSize8
  5019. else if ((MemRefSize in [ msiMem16, msiMULTIPLEMinSize16]) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultipleMinSize16
  5020. else if ((MemRefSize in [ msiMem32, msiMULTIPLEMinSize32]) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultipleMinSize32
  5021. else if ((MemRefSize in [ msiMem64, msiMULTIPLEMinSize64]) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultipleMinSize64
  5022. else if ((MemRefSize in [msiMem128, msiMULTIPLEMinSize128]) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultipleMinSize128
  5023. else if ((MemRefSize in [msiMem256, msiMULTIPLEMinSize256]) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultipleMinSize256
  5024. else if ((MemRefSize in [msiMem512, msiMULTIPLEMinSize512]) OR (MRefInfo = msiMem512)) then MemRefSize := msiMultipleMinSize512
  5025. else MemRefSize := msiMultiple;
  5026. end;
  5027. end;
  5028. end;
  5029. end;
  5030. //if not(MRefInfo in [msiBMem32, msiBMem64]) and (actRegCount > 0) then
  5031. if actRegCount > 0 then
  5032. begin
  5033. if MRefInfo in [msiBMem16, msiBMem32, msiBMem64] then
  5034. begin
  5035. if IF_BCST2 in insentry^.flags then InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes := InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes + [bt1to2];
  5036. if IF_BCST4 in insentry^.flags then InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes := InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes + [bt1to4];
  5037. if IF_BCST8 in insentry^.flags then InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes := InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes + [bt1to8];
  5038. if IF_BCST16 in insentry^.flags then InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes := InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes + [bt1to16];
  5039. if IF_BCST32 in insentry^.flags then InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes := InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes + [bt1to32];
  5040. //InsTabMemRefSizeInfoCache^[AsmOp].BCSTTypes
  5041. // BROADCAST - OPERAND
  5042. RegBCSTSizeMask := RegBCSTSizeMask or actMemSize;
  5043. case actRegTypes and (OT_XMMREG or OT_YMMREG or OT_ZMMREG or OT_REG_EXTRA_MASK) of
  5044. OT_XMMREG: RegBCSTXMMSizeMask := RegBCSTXMMSizeMask or actMemSize;
  5045. OT_YMMREG: RegBCSTYMMSizeMask := RegBCSTYMMSizeMask or actMemSize;
  5046. OT_ZMMREG: RegBCSTZMMSizeMask := RegBCSTZMMSizeMask or actMemSize;
  5047. else begin
  5048. RegBCSTXMMSizeMask := not(0);
  5049. RegBCSTYMMSizeMask := not(0);
  5050. RegBCSTZMMSizeMask := not(0);
  5051. end;
  5052. end;
  5053. end
  5054. else
  5055. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG or OT_ZMMREG or OT_REG_EXTRA_MASK) of
  5056. OT_MMXREG: if actConstCount > 0 then RegMMXConstSizeMask := RegMMXConstSizeMask or actMemSize
  5057. else RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  5058. OT_XMMREG: if actConstCount > 0 then RegXMMConstSizeMask := RegXMMConstSizeMask or actMemSize
  5059. else RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  5060. OT_YMMREG: if actConstCount > 0 then RegYMMConstSizeMask := RegYMMConstSizeMask or actMemSize
  5061. else RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  5062. OT_ZMMREG: if actConstCount > 0 then RegZMMConstSizeMask := RegZMMConstSizeMask or actMemSize
  5063. else RegZMMSizeMask := RegZMMSizeMask or actMemSize;
  5064. else begin
  5065. RegMMXSizeMask := not(0);
  5066. RegXMMSizeMask := not(0);
  5067. RegYMMSizeMask := not(0);
  5068. RegZMMSizeMask := not(0);
  5069. RegMMXConstSizeMask := not(0);
  5070. RegXMMConstSizeMask := not(0);
  5071. RegYMMConstSizeMask := not(0);
  5072. RegZMMConstSizeMask := not(0);
  5073. end;
  5074. end;
  5075. end
  5076. else
  5077. end
  5078. else InternalError(777202);
  5079. end;
  5080. end;
  5081. inc(insentry);
  5082. end;
  5083. if InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX then
  5084. begin
  5085. case RegBCSTSizeMask of
  5086. 0: ; // ignore;
  5087. OT_BITSB16: begin
  5088. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSizeBCST := msbBCST16;
  5089. InsTabMemRefSizeInfoCache^[AsmOp].BCSTXMMMultiplicator := 8;
  5090. end;
  5091. OT_BITSB32: begin
  5092. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSizeBCST := msbBCST32;
  5093. InsTabMemRefSizeInfoCache^[AsmOp].BCSTXMMMultiplicator := 4;
  5094. end;
  5095. OT_BITSB64: begin
  5096. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSizeBCST := msbBCST64;
  5097. InsTabMemRefSizeInfoCache^[AsmOp].BCSTXMMMultiplicator := 2;
  5098. end;
  5099. else begin
  5100. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSizeBCST := msbMultiple;
  5101. end;
  5102. end;
  5103. end;
  5104. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  5105. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  5106. begin
  5107. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiVMemMultiple then
  5108. begin
  5109. if ((RegXMMSizeMask = OT_BITS128) or (RegXMMSizeMask = 0)) and
  5110. ((RegYMMSizeMask = OT_BITS256) or (RegYMMSizeMask = 0)) and
  5111. ((RegZMMSizeMask = OT_BITS512) or (RegZMMSizeMask = 0)) and
  5112. ((RegXMMSizeMask or RegYMMSizeMask or RegZMMSizeMask) <> 0) then
  5113. begin
  5114. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiVMemRegSize;
  5115. end;
  5116. end
  5117. else if (RegMMXSizeMask or RegMMXConstSizeMask) <> 0 then
  5118. begin
  5119. if ((RegMMXSizeMask or RegMMXConstSizeMask) = OT_BITS64) and
  5120. ((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS128) and
  5121. ((RegYMMSizeMask or RegYMMConstSizeMask) = 0) and
  5122. ((RegZMMSizeMask or RegZMMConstSizeMask) = 0) then
  5123. begin
  5124. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  5125. end;
  5126. end
  5127. else if (((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS128) or ((RegXMMSizeMask or RegXMMConstSizeMask) = 0)) and
  5128. (((RegYMMSizeMask or RegYMMConstSizeMask) = OT_BITS256) or ((RegYMMSizeMask or RegYMMConstSizeMask) = 0)) and
  5129. (((RegZMMSizeMask or RegZMMConstSizeMask) = OT_BITS512) or ((RegZMMSizeMask or RegZMMConstSizeMask) = 0)) and
  5130. (((RegXMMSizeMask or RegXMMConstSizeMask or
  5131. RegYMMSizeMask or RegYMMConstSizeMask or
  5132. RegZMMSizeMask or RegZMMConstSizeMask)) <> 0) then
  5133. begin
  5134. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  5135. end
  5136. else if (RegXMMSizeMask or RegXMMConstSizeMask = OT_BITS16) and
  5137. (RegYMMSizeMask or RegYMMConstSizeMask = OT_BITS32) and
  5138. (RegZMMSizeMask or RegZMMConstSizeMask = 0) then
  5139. begin
  5140. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx16y32;
  5141. end
  5142. else if (RegXMMSizeMask or RegXMMConstSizeMask = OT_BITS16) and
  5143. (RegYMMSizeMask or RegYMMConstSizeMask = OT_BITS32) and
  5144. (RegZMMSizeMask or RegZMMConstSizeMask = OT_BITS64) then
  5145. begin
  5146. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx16y32z64;
  5147. end
  5148. else if ((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS32) and
  5149. ((RegYMMSizeMask or RegYMMConstSizeMask) = OT_BITS64) then
  5150. begin
  5151. if ((RegZMMSizeMask or RegZMMConstSizeMask) = 0) then
  5152. begin
  5153. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx32y64;
  5154. end
  5155. else if ((RegZMMSizeMask or RegZMMConstSizeMask) = OT_BITS128) then
  5156. begin
  5157. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx32y64z128;
  5158. end;
  5159. end
  5160. else if ((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS64) and
  5161. ((RegYMMSizeMask or RegYMMConstSizeMask) = OT_BITS128) and
  5162. ((RegZMMSizeMask or RegZMMConstSizeMask) = 0) then
  5163. begin
  5164. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  5165. end
  5166. else if ((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS64) and
  5167. ((RegYMMSizeMask or RegYMMConstSizeMask) = OT_BITS128) and
  5168. ((RegZMMSizeMask or RegZMMConstSizeMask) = OT_BITS256) then
  5169. begin
  5170. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128z256;
  5171. end
  5172. else if ((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS64) and
  5173. ((RegYMMSizeMask or RegYMMConstSizeMask) = OT_BITS256) and
  5174. ((RegZMMSizeMask or RegZMMConstSizeMask) = 0) then
  5175. begin
  5176. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  5177. end
  5178. else if ((RegXMMSizeMask or RegXMMConstSizeMask) = OT_BITS64) and
  5179. ((RegYMMSizeMask or RegYMMConstSizeMask) = OT_BITS256) and
  5180. ((RegZMMSizeMask or RegZMMConstSizeMask) = OT_BITS512) then
  5181. begin
  5182. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256z512;
  5183. end
  5184. else if ((RegXMMConstSizeMask = 0) or (RegXMMConstSizeMask = OT_BITS128)) and
  5185. ((RegYMMConstSizeMask = 0) or (RegYMMConstSizeMask = OT_BITS256)) and
  5186. ((RegZMMConstSizeMask = 0) or (RegZMMConstSizeMask = OT_BITS512)) and
  5187. ((RegXMMConstSizeMask or RegYMMConstSizeMask or RegZMMConstSizeMask) <> 0) and
  5188. (
  5189. ((RegXMMSizeMask or RegYMMSizeMask or RegZMMSizeMask) = OT_BITS128) or
  5190. ((RegXMMSizeMask or RegYMMSizeMask or RegZMMSizeMask) = OT_BITS256) or
  5191. ((RegXMMSizeMask or RegYMMSizeMask or RegZMMSizeMask) = OT_BITS512)
  5192. ) then
  5193. begin
  5194. case RegXMMSizeMask or RegYMMSizeMask or RegZMMSizeMask of
  5195. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegConst128;
  5196. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegConst256;
  5197. OT_BITS512: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegConst512;
  5198. else InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMultiple;
  5199. end;
  5200. end
  5201. else
  5202. begin
  5203. if not(
  5204. (AsmOp = A_CVTSI2SS) or
  5205. (AsmOp = A_CVTSI2SD) or
  5206. (AsmOp = A_CVTPD2DQ) or
  5207. (AsmOp = A_VCVTPD2DQ) or
  5208. (AsmOp = A_VCVTPD2PS) or
  5209. (AsmOp = A_VCVTSI2SD) or
  5210. (AsmOp = A_VCVTSI2SS) or
  5211. (AsmOp = A_VCVTTPD2DQ) or
  5212. (AsmOp = A_VCVTPD2UDQ) or
  5213. (AsmOp = A_VCVTQQ2PS) or
  5214. (AsmOp = A_VCVTTPD2UDQ) or
  5215. (AsmOp = A_VCVTUQQ2PS) or
  5216. (AsmOp = A_VCVTUSI2SD) or
  5217. (AsmOp = A_VCVTUSI2SS) or
  5218. (AsmOp = A_vcvtdq2ph) or
  5219. (AsmOp = A_vcvtpd2ph) or
  5220. (AsmOp = A_vcvtph2pd) or
  5221. (AsmOp = A_vcvtqq2ph) or
  5222. (AsmOp = A_vcvtsi2sh) or
  5223. (AsmOp = A_vcvttph2qq) or
  5224. (AsmOp = A_vcvttph2uqq) or
  5225. (AsmOp = A_vcvtudq2ph) or
  5226. (AsmOp = A_vcvtuqq2ph) or
  5227. (AsmOp = A_vcvtusi2sh) or
  5228. (AsmOp = A_VCVTNEPS2BF16) or
  5229. (AsmOp = A_vcvtps2phx) or
  5230. // TODO check
  5231. (AsmOp = A_VCMPSS)
  5232. ) then
  5233. InternalError(777205);
  5234. end;
  5235. end
  5236. else if (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) and
  5237. (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnknown) and
  5238. (not(ExistsMemRef)) then
  5239. begin
  5240. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiNoMemRef;
  5241. end;
  5242. InsTabMemRefSizeInfoCache^[AsmOp].RegXMMSizeMask:=RegXMMSizeMask;
  5243. InsTabMemRefSizeInfoCache^[AsmOp].RegYMMSizeMask:=RegYMMSizeMask;
  5244. InsTabMemRefSizeInfoCache^[AsmOp].RegZMMSizeMask:=RegZMMSizeMask;
  5245. if (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) and
  5246. (gas_needsuffix[AsmOp] <> AttSufNONE) and
  5247. (not(InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples)) then
  5248. begin
  5249. // combination (attsuffix <> "AttSufNONE") and (MemRefSize is not in MemRefMultiples) is not supported =>> check opcode-definition in x86ins.dat
  5250. if (AsmOp <> A_CVTSI2SD) and
  5251. (AsmOp <> A_CVTSI2SS) then
  5252. begin
  5253. inc(iCntOpcodeValError);
  5254. Str(gas_needsuffix[AsmOp],hs1);
  5255. Str(InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize,hs2);
  5256. Message3(asmr_e_not_supported_combination_attsuffix_memrefsize_type,
  5257. std_op2str[AsmOp],hs1,hs2);
  5258. end;
  5259. end;
  5260. end;
  5261. end;
  5262. if iCntOpcodeValError > 0 then
  5263. InternalError(2021011201);
  5264. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  5265. begin
  5266. // only supported intructiones with SSE- or AVX-operands
  5267. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  5268. begin
  5269. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnknown;
  5270. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnknown;
  5271. end;
  5272. end;
  5273. end;
  5274. function NoMemorySizeRequired(opcode : TAsmOp) : Boolean;
  5275. var
  5276. i : LongInt;
  5277. insentry : PInsEntry;
  5278. begin
  5279. result:=false;
  5280. i:=instabcache^[opcode];
  5281. if i=-1 then
  5282. begin
  5283. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  5284. exit;
  5285. end;
  5286. insentry:=@instab[i];
  5287. while (insentry^.opcode=opcode) do
  5288. begin
  5289. if (insentry^.ops=1) and (insentry^.optypes[0]=OT_MEMORY) then
  5290. begin
  5291. result:=true;
  5292. exit;
  5293. end;
  5294. inc(insentry);
  5295. end;
  5296. end;
  5297. procedure InitAsm;
  5298. begin
  5299. build_spilling_operation_type_table;
  5300. if not assigned(instabcache) then
  5301. BuildInsTabCache;
  5302. if not assigned(InsTabMemRefSizeInfoCache) then
  5303. BuildInsTabMemRefSizeInfoCache;
  5304. end;
  5305. procedure DoneAsm;
  5306. begin
  5307. if assigned(operation_type_table) then
  5308. begin
  5309. dispose(operation_type_table);
  5310. operation_type_table:=nil;
  5311. end;
  5312. if assigned(instabcache) then
  5313. begin
  5314. dispose(instabcache);
  5315. instabcache:=nil;
  5316. end;
  5317. if assigned(InsTabMemRefSizeInfoCache) then
  5318. begin
  5319. dispose(InsTabMemRefSizeInfoCache);
  5320. InsTabMemRefSizeInfoCache:=nil;
  5321. end;
  5322. end;
  5323. begin
  5324. cai_align:=tai_align;
  5325. cai_cpu:=taicpu;
  5326. end.