aasmcpu.pas 137 KB

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