aasmcpu.pas 138 KB

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