aasmcpu.pas 137 KB

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