aasmcpu.pas 119 KB

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