aasmcpu.pas 126 KB

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