aasmcpu.pas 132 KB

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