aasmcpu.pas 201 KB

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