aasmcpu.pas 201 KB

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