aasmcpu.pas 182 KB

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