aasmcpu.pas 149 KB

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