aasmcpu.pas 133 KB

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