aasmcpu.pas 177 KB

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