aasmcpu.pas 148 KB

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