aasmcpu.pas 196 KB

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