aasmcpu.pas 196 KB

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