aasmcpu.pas 184 KB

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