aasmcpu.pas 199 KB

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