aasmcpu.pas 138 KB

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