aasmcpu.pas 196 KB

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