aasmcpu.pas 191 KB

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