aasmcpu.pas 141 KB

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