aasmcpu.pas 138 KB

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