aasmcpu.pas 149 KB

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