aasmcpu.pas 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  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. { allow any base for assembler blocks }
  986. ((assigned(current_procinfo) and
  987. (pi_has_assembler_block in current_procinfo.flags) and
  988. (ref^.base<>NR_NO)) or (ref^.base=NR_EBX))
  989. )
  990. {$endif i386}
  991. {$ifdef x86_64}
  992. or (
  993. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  994. (ref^.base<>NR_NO)
  995. )
  996. {$endif x86_64}
  997. then
  998. begin
  999. { create ot field }
  1000. if (ot and OT_SIZE_MASK)=0 then
  1001. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  1002. else
  1003. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  1004. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1005. ot:=ot or OT_MEM_OFFS;
  1006. { fix scalefactor }
  1007. if (ref^.index=NR_NO) then
  1008. ref^.scalefactor:=0
  1009. else
  1010. if (ref^.scalefactor=0) then
  1011. ref^.scalefactor:=1;
  1012. end
  1013. else
  1014. begin
  1015. { Jumps use a relative offset which can be 8bit,
  1016. for other opcodes we always need to generate the full
  1017. 32bit address }
  1018. if assigned(objdata) and
  1019. is_jmp then
  1020. begin
  1021. currsym:=objdata.symbolref(ref^.symbol);
  1022. l:=ref^.offset;
  1023. {$push}
  1024. {$r-}
  1025. if assigned(currsym) then
  1026. inc(l,currsym.address);
  1027. {$pop}
  1028. { when it is a forward jump we need to compensate the
  1029. offset of the instruction since the previous time,
  1030. because the symbol address is then still using the
  1031. 'old-style' addressing.
  1032. For backwards jumps this is not required because the
  1033. address of the symbol is already adjusted to the
  1034. new offset }
  1035. if (l>InsOffset) and (LastInsOffset<>-1) then
  1036. inc(l,InsOffset-LastInsOffset);
  1037. { instruction size will then always become 2 (PFV) }
  1038. relsize:=(InsOffset+2)-l;
  1039. if (relsize>=-128) and (relsize<=127) and
  1040. (
  1041. not assigned(currsym) or
  1042. (currsym.objsection=objdata.currobjsec)
  1043. ) then
  1044. ot:=OT_IMM8 or OT_SHORT
  1045. else
  1046. ot:=OT_IMM32 or OT_NEAR;
  1047. end
  1048. else
  1049. ot:=OT_IMM32 or OT_NEAR;
  1050. end;
  1051. end;
  1052. top_local :
  1053. begin
  1054. if (ot and OT_SIZE_MASK)=0 then
  1055. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1056. else
  1057. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1058. end;
  1059. top_const :
  1060. begin
  1061. // if opcode is a SSE or AVX-instruction then we need a
  1062. // special handling (opsize can different from const-size)
  1063. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1064. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1065. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
  1066. begin
  1067. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1068. csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
  1069. csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
  1070. csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
  1071. csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
  1072. csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
  1073. end;
  1074. end
  1075. else
  1076. begin
  1077. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1078. { further, allow AAD and AAM with imm. operand }
  1079. if (opsize=S_NO) and not((i in [1,2,3])
  1080. {$ifndef x86_64}
  1081. or ((i=0) and (opcode in [A_AAD,A_AAM]))
  1082. {$endif x86_64}
  1083. ) then
  1084. message(asmr_e_invalid_opcode_and_operand);
  1085. if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
  1086. ot:=OT_IMM8 or OT_SIGNED
  1087. else
  1088. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1089. if (val=1) and (i=1) then
  1090. ot := ot or OT_ONENESS;
  1091. end;
  1092. end;
  1093. top_none :
  1094. begin
  1095. { generated when there was an error in the
  1096. assembler reader. It never happends when generating
  1097. assembler }
  1098. end;
  1099. else
  1100. internalerror(200402261);
  1101. end;
  1102. end;
  1103. end;
  1104. function taicpu.InsEnd:longint;
  1105. begin
  1106. InsEnd:=InsOffset+InsSize;
  1107. end;
  1108. function taicpu.Matches(p:PInsEntry):boolean;
  1109. { * IF_SM stands for Size Match: any operand whose size is not
  1110. * explicitly specified by the template is `really' intended to be
  1111. * the same size as the first size-specified operand.
  1112. * Non-specification is tolerated in the input instruction, but
  1113. * _wrong_ specification is not.
  1114. *
  1115. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1116. * three-operand instructions such as SHLD: it implies that the
  1117. * first two operands must match in size, but that the third is
  1118. * required to be _unspecified_.
  1119. *
  1120. * IF_SB invokes Size Byte: operands with unspecified size in the
  1121. * template are really bytes, and so no non-byte specification in
  1122. * the input instruction will be tolerated. IF_SW similarly invokes
  1123. * Size Word, and IF_SD invokes Size Doubleword.
  1124. *
  1125. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1126. * that any operand with unspecified size in the template is
  1127. * required to have unspecified size in the instruction too...)
  1128. }
  1129. var
  1130. insot,
  1131. currot,
  1132. i,j,asize,oprs : longint;
  1133. insflags:cardinal;
  1134. siz : array[0..max_operands-1] of longint;
  1135. begin
  1136. result:=false;
  1137. { Check the opcode and operands }
  1138. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1139. exit;
  1140. for i:=0 to p^.ops-1 do
  1141. begin
  1142. insot:=p^.optypes[i];
  1143. currot:=oper[i]^.ot;
  1144. { Check the operand flags }
  1145. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1146. exit;
  1147. { Check if the passed operand size matches with one of
  1148. the supported operand sizes }
  1149. if ((insot and OT_SIZE_MASK)<>0) and
  1150. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1151. exit;
  1152. end;
  1153. { Check operand sizes }
  1154. insflags:=p^.flags;
  1155. if insflags and IF_SMASK<>0 then
  1156. begin
  1157. { as default an untyped size can get all the sizes, this is different
  1158. from nasm, but else we need to do a lot checking which opcodes want
  1159. size or not with the automatic size generation }
  1160. asize:=-1;
  1161. if (insflags and IF_SB)<>0 then
  1162. asize:=OT_BITS8
  1163. else if (insflags and IF_SW)<>0 then
  1164. asize:=OT_BITS16
  1165. else if (insflags and IF_SD)<>0 then
  1166. asize:=OT_BITS32;
  1167. if (insflags and IF_ARMASK)<>0 then
  1168. begin
  1169. siz[0]:=-1;
  1170. siz[1]:=-1;
  1171. siz[2]:=-1;
  1172. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1173. end
  1174. else
  1175. begin
  1176. siz[0]:=asize;
  1177. siz[1]:=asize;
  1178. siz[2]:=asize;
  1179. end;
  1180. if (insflags and (IF_SM or IF_SM2))<>0 then
  1181. begin
  1182. if (insflags and IF_SM2)<>0 then
  1183. oprs:=2
  1184. else
  1185. oprs:=p^.ops;
  1186. for i:=0 to oprs-1 do
  1187. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1188. begin
  1189. for j:=0 to oprs-1 do
  1190. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1191. break;
  1192. end;
  1193. end
  1194. else
  1195. oprs:=2;
  1196. { Check operand sizes }
  1197. for i:=0 to p^.ops-1 do
  1198. begin
  1199. insot:=p^.optypes[i];
  1200. currot:=oper[i]^.ot;
  1201. if ((insot and OT_SIZE_MASK)=0) and
  1202. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1203. { Immediates can always include smaller size }
  1204. ((currot and OT_IMMEDIATE)=0) and
  1205. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1206. exit;
  1207. end;
  1208. end;
  1209. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1210. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1211. begin
  1212. for i:=0 to p^.ops-1 do
  1213. begin
  1214. insot:=p^.optypes[i];
  1215. if ((insot and OT_XMMRM) = OT_XMMRM) OR
  1216. ((insot and OT_YMMRM) = OT_YMMRM) then
  1217. begin
  1218. if (insot and OT_SIZE_MASK) = 0 then
  1219. begin
  1220. case insot and (OT_XMMRM or OT_YMMRM) of
  1221. OT_XMMRM: insot := insot or OT_BITS128;
  1222. OT_YMMRM: insot := insot or OT_BITS256;
  1223. end;
  1224. end;
  1225. end;
  1226. currot:=oper[i]^.ot;
  1227. { Check the operand flags }
  1228. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1229. exit;
  1230. { Check if the passed operand size matches with one of
  1231. the supported operand sizes }
  1232. if ((insot and OT_SIZE_MASK)<>0) and
  1233. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1234. exit;
  1235. end;
  1236. end;
  1237. result:=true;
  1238. end;
  1239. procedure taicpu.ResetPass1;
  1240. begin
  1241. { we need to reset everything here, because the choosen insentry
  1242. can be invalid for a new situation where the previously optimized
  1243. insentry is not correct }
  1244. InsEntry:=nil;
  1245. InsSize:=0;
  1246. LastInsOffset:=-1;
  1247. end;
  1248. procedure taicpu.ResetPass2;
  1249. begin
  1250. { we are here in a second pass, check if the instruction can be optimized }
  1251. if assigned(InsEntry) and
  1252. ((InsEntry^.flags and IF_PASS2)<>0) then
  1253. begin
  1254. InsEntry:=nil;
  1255. InsSize:=0;
  1256. end;
  1257. LastInsOffset:=-1;
  1258. end;
  1259. function taicpu.CheckIfValid:boolean;
  1260. begin
  1261. result:=FindInsEntry(nil);
  1262. end;
  1263. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1264. var
  1265. i : longint;
  1266. begin
  1267. result:=false;
  1268. { Things which may only be done once, not when a second pass is done to
  1269. optimize }
  1270. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1271. begin
  1272. current_filepos:=fileinfo;
  1273. { We need intel style operands }
  1274. SetOperandOrder(op_intel);
  1275. { create the .ot fields }
  1276. create_ot(objdata);
  1277. { set the file postion }
  1278. end
  1279. else
  1280. begin
  1281. { we've already an insentry so it's valid }
  1282. result:=true;
  1283. exit;
  1284. end;
  1285. { Lookup opcode in the table }
  1286. InsSize:=-1;
  1287. i:=instabcache^[opcode];
  1288. if i=-1 then
  1289. begin
  1290. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1291. exit;
  1292. end;
  1293. insentry:=@instab[i];
  1294. while (insentry^.opcode=opcode) do
  1295. begin
  1296. if matches(insentry) then
  1297. begin
  1298. result:=true;
  1299. exit;
  1300. end;
  1301. inc(insentry);
  1302. end;
  1303. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1304. { No instruction found, set insentry to nil and inssize to -1 }
  1305. insentry:=nil;
  1306. inssize:=-1;
  1307. end;
  1308. function taicpu.Pass1(objdata:TObjData):longint;
  1309. begin
  1310. Pass1:=0;
  1311. { Save the old offset and set the new offset }
  1312. InsOffset:=ObjData.CurrObjSec.Size;
  1313. { Error? }
  1314. if (Insentry=nil) and (InsSize=-1) then
  1315. exit;
  1316. { set the file postion }
  1317. current_filepos:=fileinfo;
  1318. { Get InsEntry }
  1319. if FindInsEntry(ObjData) then
  1320. begin
  1321. { Calculate instruction size }
  1322. InsSize:=calcsize(insentry);
  1323. if segprefix<>NR_NO then
  1324. inc(InsSize);
  1325. { Fix opsize if size if forced }
  1326. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1327. begin
  1328. if (insentry^.flags and IF_ARMASK)=0 then
  1329. begin
  1330. if (insentry^.flags and IF_SB)<>0 then
  1331. begin
  1332. if opsize=S_NO then
  1333. opsize:=S_B;
  1334. end
  1335. else if (insentry^.flags and IF_SW)<>0 then
  1336. begin
  1337. if opsize=S_NO then
  1338. opsize:=S_W;
  1339. end
  1340. else if (insentry^.flags and IF_SD)<>0 then
  1341. begin
  1342. if opsize=S_NO then
  1343. opsize:=S_L;
  1344. end;
  1345. end;
  1346. end;
  1347. LastInsOffset:=InsOffset;
  1348. Pass1:=InsSize;
  1349. exit;
  1350. end;
  1351. LastInsOffset:=-1;
  1352. end;
  1353. const
  1354. segprefixes: array[NR_ES..NR_GS] of Byte=(
  1355. // es cs ss ds fs gs
  1356. $26, $2E, $36, $3E, $64, $65
  1357. );
  1358. procedure taicpu.Pass2(objdata:TObjData);
  1359. begin
  1360. { error in pass1 ? }
  1361. if insentry=nil then
  1362. exit;
  1363. current_filepos:=fileinfo;
  1364. { Segment override }
  1365. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  1366. begin
  1367. objdata.writebytes(segprefixes[segprefix],1);
  1368. { fix the offset for GenNode }
  1369. inc(InsOffset);
  1370. end
  1371. else if segprefix<>NR_NO then
  1372. InternalError(201001071);
  1373. { Generate the instruction }
  1374. GenCode(objdata);
  1375. end;
  1376. function taicpu.needaddrprefix(opidx:byte):boolean;
  1377. begin
  1378. result:=(oper[opidx]^.typ=top_ref) and
  1379. (oper[opidx]^.ref^.refaddr=addr_no) and
  1380. {$ifdef x86_64}
  1381. (oper[opidx]^.ref^.base<>NR_RIP) and
  1382. {$endif x86_64}
  1383. (
  1384. (
  1385. (oper[opidx]^.ref^.index<>NR_NO) and
  1386. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1387. ) or
  1388. (
  1389. (oper[opidx]^.ref^.base<>NR_NO) and
  1390. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1391. )
  1392. );
  1393. end;
  1394. procedure badreg(r:Tregister);
  1395. begin
  1396. Message1(asmw_e_invalid_register,generic_regname(r));
  1397. end;
  1398. function regval(r:Tregister):byte;
  1399. const
  1400. intsupreg2opcode: array[0..7] of byte=
  1401. // ax cx dx bx si di bp sp -- in x86reg.dat
  1402. // ax cx dx bx sp bp si di -- needed order
  1403. (0, 1, 2, 3, 6, 7, 5, 4);
  1404. maxsupreg: array[tregistertype] of tsuperregister=
  1405. {$ifdef x86_64}
  1406. (0, 16, 9, 8, 16, 32, 0);
  1407. {$else x86_64}
  1408. (0, 8, 9, 8, 8, 32, 0);
  1409. {$endif x86_64}
  1410. var
  1411. rs: tsuperregister;
  1412. rt: tregistertype;
  1413. begin
  1414. rs:=getsupreg(r);
  1415. rt:=getregtype(r);
  1416. if (rs>=maxsupreg[rt]) then
  1417. badreg(r);
  1418. result:=rs and 7;
  1419. if (rt=R_INTREGISTER) then
  1420. begin
  1421. if (rs<8) then
  1422. result:=intsupreg2opcode[rs];
  1423. if getsubreg(r)=R_SUBH then
  1424. inc(result,4);
  1425. end;
  1426. end;
  1427. {$ifdef x86_64}
  1428. function rexbits(r: tregister): byte;
  1429. begin
  1430. result:=0;
  1431. case getregtype(r) of
  1432. R_INTREGISTER:
  1433. if (getsupreg(r)>=RS_R8) then
  1434. { Either B,X or R bits can be set, depending on register role in instruction.
  1435. Set all three bits here, caller will discard unnecessary ones. }
  1436. result:=result or $47
  1437. else if (getsubreg(r)=R_SUBL) and
  1438. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1439. result:=result or $40
  1440. else if (getsubreg(r)=R_SUBH) then
  1441. { Not an actual REX bit, used to detect incompatible usage of
  1442. AH/BH/CH/DH }
  1443. result:=result or $80;
  1444. R_MMREGISTER:
  1445. if getsupreg(r)>=RS_XMM8 then
  1446. result:=result or $47;
  1447. end;
  1448. end;
  1449. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1450. var
  1451. sym : tasmsymbol;
  1452. md,s,rv : byte;
  1453. base,index,scalefactor,
  1454. o : longint;
  1455. ir,br : Tregister;
  1456. isub,bsub : tsubregister;
  1457. begin
  1458. process_ea:=false;
  1459. fillchar(output,sizeof(output),0);
  1460. {Register ?}
  1461. if (input.typ=top_reg) then
  1462. begin
  1463. rv:=regval(input.reg);
  1464. output.modrm:=$c0 or (rfield shl 3) or rv;
  1465. output.size:=1;
  1466. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  1467. process_ea:=true;
  1468. exit;
  1469. end;
  1470. {No register, so memory reference.}
  1471. if input.typ<>top_ref then
  1472. internalerror(200409263);
  1473. ir:=input.ref^.index;
  1474. br:=input.ref^.base;
  1475. isub:=getsubreg(ir);
  1476. bsub:=getsubreg(br);
  1477. s:=input.ref^.scalefactor;
  1478. o:=input.ref^.offset;
  1479. sym:=input.ref^.symbol;
  1480. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1481. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1482. internalerror(200301081);
  1483. { it's direct address }
  1484. if (br=NR_NO) and (ir=NR_NO) then
  1485. begin
  1486. output.sib_present:=true;
  1487. output.bytes:=4;
  1488. output.modrm:=4 or (rfield shl 3);
  1489. output.sib:=$25;
  1490. end
  1491. else if (br=NR_RIP) and (ir=NR_NO) then
  1492. begin
  1493. { rip based }
  1494. output.sib_present:=false;
  1495. output.bytes:=4;
  1496. output.modrm:=5 or (rfield shl 3);
  1497. end
  1498. else
  1499. { it's an indirection }
  1500. begin
  1501. { 16 bit? }
  1502. if ((ir<>NR_NO) and (isub<>R_SUBADDR) and (isub<>R_SUBD)) or
  1503. ((br<>NR_NO) and (bsub<>R_SUBADDR) and (bsub<>R_SUBD)) then
  1504. message(asmw_e_16bit_32bit_not_supported);
  1505. { wrong, for various reasons }
  1506. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1507. exit;
  1508. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1509. process_ea:=true;
  1510. { base }
  1511. case br of
  1512. NR_R8D,
  1513. NR_EAX,
  1514. NR_R8,
  1515. NR_RAX : base:=0;
  1516. NR_R9D,
  1517. NR_ECX,
  1518. NR_R9,
  1519. NR_RCX : base:=1;
  1520. NR_R10D,
  1521. NR_EDX,
  1522. NR_R10,
  1523. NR_RDX : base:=2;
  1524. NR_R11D,
  1525. NR_EBX,
  1526. NR_R11,
  1527. NR_RBX : base:=3;
  1528. NR_R12D,
  1529. NR_ESP,
  1530. NR_R12,
  1531. NR_RSP : base:=4;
  1532. NR_R13D,
  1533. NR_EBP,
  1534. NR_R13,
  1535. NR_NO,
  1536. NR_RBP : base:=5;
  1537. NR_R14D,
  1538. NR_ESI,
  1539. NR_R14,
  1540. NR_RSI : base:=6;
  1541. NR_R15D,
  1542. NR_EDI,
  1543. NR_R15,
  1544. NR_RDI : base:=7;
  1545. else
  1546. exit;
  1547. end;
  1548. { index }
  1549. case ir of
  1550. NR_R8D,
  1551. NR_EAX,
  1552. NR_R8,
  1553. NR_RAX : index:=0;
  1554. NR_R9D,
  1555. NR_ECX,
  1556. NR_R9,
  1557. NR_RCX : index:=1;
  1558. NR_R10D,
  1559. NR_EDX,
  1560. NR_R10,
  1561. NR_RDX : index:=2;
  1562. NR_R11D,
  1563. NR_EBX,
  1564. NR_R11,
  1565. NR_RBX : index:=3;
  1566. NR_R12D,
  1567. NR_ESP,
  1568. NR_R12,
  1569. NR_NO : index:=4;
  1570. NR_R13D,
  1571. NR_EBP,
  1572. NR_R13,
  1573. NR_RBP : index:=5;
  1574. NR_R14D,
  1575. NR_ESI,
  1576. NR_R14,
  1577. NR_RSI : index:=6;
  1578. NR_R15D,
  1579. NR_EDI,
  1580. NR_R15,
  1581. NR_RDI : index:=7;
  1582. else
  1583. exit;
  1584. end;
  1585. case s of
  1586. 0,
  1587. 1 : scalefactor:=0;
  1588. 2 : scalefactor:=1;
  1589. 4 : scalefactor:=2;
  1590. 8 : scalefactor:=3;
  1591. else
  1592. exit;
  1593. end;
  1594. { If rbp or r13 is used we must always include an offset }
  1595. if (br=NR_NO) or
  1596. ((br<>NR_RBP) and (br<>NR_R13) and (br<>NR_EBP) and (br<>NR_R13D) and (o=0) and (sym=nil)) then
  1597. md:=0
  1598. else
  1599. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1600. md:=1
  1601. else
  1602. md:=2;
  1603. if (br=NR_NO) or (md=2) then
  1604. output.bytes:=4
  1605. else
  1606. output.bytes:=md;
  1607. { SIB needed ? }
  1608. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) and (br<>NR_ESP) and (br<>NR_R12D) then
  1609. begin
  1610. output.sib_present:=false;
  1611. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1612. end
  1613. else
  1614. begin
  1615. output.sib_present:=true;
  1616. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1617. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1618. end;
  1619. end;
  1620. output.size:=1+ord(output.sib_present)+output.bytes;
  1621. process_ea:=true;
  1622. end;
  1623. {$else x86_64}
  1624. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1625. var
  1626. sym : tasmsymbol;
  1627. md,s,rv : byte;
  1628. base,index,scalefactor,
  1629. o : longint;
  1630. ir,br : Tregister;
  1631. isub,bsub : tsubregister;
  1632. begin
  1633. process_ea:=false;
  1634. fillchar(output,sizeof(output),0);
  1635. {Register ?}
  1636. if (input.typ=top_reg) then
  1637. begin
  1638. rv:=regval(input.reg);
  1639. output.modrm:=$c0 or (rfield shl 3) or rv;
  1640. output.size:=1;
  1641. process_ea:=true;
  1642. exit;
  1643. end;
  1644. {No register, so memory reference.}
  1645. if (input.typ<>top_ref) then
  1646. internalerror(200409262);
  1647. 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)
  1648. ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER) and (getregtype(input.ref^.index)<>R_MMREGISTER)) or
  1649. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1650. internalerror(200301081);
  1651. ir:=input.ref^.index;
  1652. br:=input.ref^.base;
  1653. isub:=getsubreg(ir);
  1654. bsub:=getsubreg(br);
  1655. s:=input.ref^.scalefactor;
  1656. o:=input.ref^.offset;
  1657. sym:=input.ref^.symbol;
  1658. { it's direct address }
  1659. if (br=NR_NO) and (ir=NR_NO) then
  1660. begin
  1661. { it's a pure offset }
  1662. output.sib_present:=false;
  1663. output.bytes:=4;
  1664. output.modrm:=5 or (rfield shl 3);
  1665. end
  1666. else
  1667. { it's an indirection }
  1668. begin
  1669. { 16 bit address? }
  1670. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY]) and
  1671. (br<>NR_NO) and (bsub=R_SUBADDR)
  1672. ) then
  1673. begin
  1674. // vector memory (AVX2) =>> ignore
  1675. end
  1676. else if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1677. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1678. message(asmw_e_16bit_not_supported);
  1679. {$ifdef OPTEA}
  1680. { make single reg base }
  1681. if (br=NR_NO) and (s=1) then
  1682. begin
  1683. br:=ir;
  1684. ir:=NR_NO;
  1685. end;
  1686. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1687. if (br=NR_NO) and
  1688. (((s=2) and (ir<>NR_ESP)) or
  1689. (s=3) or (s=5) or (s=9)) then
  1690. begin
  1691. br:=ir;
  1692. dec(s);
  1693. end;
  1694. { swap ESP into base if scalefactor is 1 }
  1695. if (s=1) and (ir=NR_ESP) then
  1696. begin
  1697. ir:=br;
  1698. br:=NR_ESP;
  1699. end;
  1700. {$endif OPTEA}
  1701. { wrong, for various reasons }
  1702. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1703. exit;
  1704. { base }
  1705. case br of
  1706. NR_EAX : base:=0;
  1707. NR_ECX : base:=1;
  1708. NR_EDX : base:=2;
  1709. NR_EBX : base:=3;
  1710. NR_ESP : base:=4;
  1711. NR_NO,
  1712. NR_EBP : base:=5;
  1713. NR_ESI : base:=6;
  1714. NR_EDI : base:=7;
  1715. else
  1716. exit;
  1717. end;
  1718. { index }
  1719. case ir of
  1720. NR_EAX,
  1721. NR_XMM0,
  1722. NR_YMM0: index:=0;
  1723. NR_ECX,
  1724. NR_XMM1,
  1725. NR_YMM1: index:=1;
  1726. NR_EDX,
  1727. NR_XMM2,
  1728. NR_YMM2: index:=2;
  1729. NR_EBX,
  1730. NR_XMM3,
  1731. NR_YMM3: index:=3;
  1732. NR_NO,
  1733. NR_XMM4,
  1734. NR_YMM4: index:=4;
  1735. NR_EBP,
  1736. NR_XMM5,
  1737. NR_YMM5: index:=5;
  1738. NR_ESI,
  1739. NR_XMM6,
  1740. NR_YMM6: index:=6;
  1741. NR_EDI,
  1742. NR_XMM7,
  1743. NR_YMM7: index:=7;
  1744. else
  1745. exit;
  1746. end;
  1747. case s of
  1748. 0,
  1749. 1 : scalefactor:=0;
  1750. 2 : scalefactor:=1;
  1751. 4 : scalefactor:=2;
  1752. 8 : scalefactor:=3;
  1753. else
  1754. exit;
  1755. end;
  1756. if (br=NR_NO) or
  1757. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1758. md:=0
  1759. else
  1760. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1761. md:=1
  1762. else
  1763. md:=2;
  1764. if (br=NR_NO) or (md=2) then
  1765. output.bytes:=4
  1766. else
  1767. output.bytes:=md;
  1768. { SIB needed ? }
  1769. if (ir=NR_NO) and (br<>NR_ESP) then
  1770. begin
  1771. output.sib_present:=false;
  1772. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1773. end
  1774. else
  1775. begin
  1776. output.sib_present:=true;
  1777. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1778. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1779. end;
  1780. end;
  1781. if output.sib_present then
  1782. output.size:=2+output.bytes
  1783. else
  1784. output.size:=1+output.bytes;
  1785. process_ea:=true;
  1786. end;
  1787. {$endif x86_64}
  1788. function taicpu.calcsize(p:PInsEntry):shortint;
  1789. var
  1790. codes : pchar;
  1791. c : byte;
  1792. len : shortint;
  1793. ea_data : ea;
  1794. exists_vex: boolean;
  1795. exists_vex_extension: boolean;
  1796. exists_prefix_66: boolean;
  1797. exists_prefix_F2: boolean;
  1798. exists_prefix_F3: boolean;
  1799. {$ifdef x86_64}
  1800. omit_rexw : boolean;
  1801. {$endif x86_64}
  1802. begin
  1803. len:=0;
  1804. codes:=@p^.code[0];
  1805. exists_vex := false;
  1806. exists_vex_extension := false;
  1807. exists_prefix_66 := false;
  1808. exists_prefix_F2 := false;
  1809. exists_prefix_F3 := false;
  1810. {$ifdef x86_64}
  1811. rex:=0;
  1812. omit_rexw:=false;
  1813. {$endif x86_64}
  1814. repeat
  1815. c:=ord(codes^);
  1816. inc(codes);
  1817. case c of
  1818. 0 :
  1819. break;
  1820. 1,2,3 :
  1821. begin
  1822. inc(codes,c);
  1823. inc(len,c);
  1824. end;
  1825. 8,9,10 :
  1826. begin
  1827. {$ifdef x86_64}
  1828. rex:=rex or (rexbits(oper[c-8]^.reg) and $F1);
  1829. {$endif x86_64}
  1830. inc(codes);
  1831. inc(len);
  1832. end;
  1833. 11 :
  1834. begin
  1835. inc(codes);
  1836. inc(len);
  1837. end;
  1838. 4,5,6,7 :
  1839. begin
  1840. if opsize=S_W then
  1841. inc(len,2)
  1842. else
  1843. inc(len);
  1844. end;
  1845. 12,13,14,
  1846. 16,17,18,
  1847. 20,21,22,23,
  1848. 40,41,42 :
  1849. inc(len);
  1850. 24,25,26,
  1851. 31,
  1852. 48,49,50 :
  1853. inc(len,2);
  1854. 28,29,30:
  1855. begin
  1856. if opsize=S_Q then
  1857. inc(len,8)
  1858. else
  1859. inc(len,4);
  1860. end;
  1861. 36,37,38:
  1862. inc(len,sizeof(pint));
  1863. 44,45,46:
  1864. inc(len,8);
  1865. 32,33,34,
  1866. 52,53,54,
  1867. 56,57,58,
  1868. 172,173,174 :
  1869. inc(len,4);
  1870. 60,61,62,63: ; // ignore vex-coded operand-idx
  1871. 208,209,210 :
  1872. begin
  1873. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1874. OT_BITS16:
  1875. inc(len);
  1876. {$ifdef x86_64}
  1877. OT_BITS64:
  1878. begin
  1879. rex:=rex or $48;
  1880. end;
  1881. {$endif x86_64}
  1882. end;
  1883. end;
  1884. 200 :
  1885. {$ifndef x86_64}
  1886. inc(len);
  1887. {$else x86_64}
  1888. { every insentry with code 0310 must be marked with NOX86_64 }
  1889. InternalError(2011051301);
  1890. {$endif x86_64}
  1891. 201 :
  1892. {$ifdef x86_64}
  1893. inc(len)
  1894. {$endif x86_64}
  1895. ;
  1896. 212 :
  1897. inc(len);
  1898. 214 :
  1899. begin
  1900. {$ifdef x86_64}
  1901. rex:=rex or $48;
  1902. {$endif x86_64}
  1903. end;
  1904. 202,
  1905. 211,
  1906. 213,
  1907. 215,
  1908. 217,218: ;
  1909. 219:
  1910. begin
  1911. inc(len);
  1912. exists_prefix_F2 := true;
  1913. end;
  1914. 220:
  1915. begin
  1916. inc(len);
  1917. exists_prefix_F3 := true;
  1918. end;
  1919. 241:
  1920. begin
  1921. inc(len);
  1922. exists_prefix_66 := true;
  1923. end;
  1924. 221:
  1925. {$ifdef x86_64}
  1926. omit_rexw:=true
  1927. {$endif x86_64}
  1928. ;
  1929. 64..151 :
  1930. begin
  1931. {$ifdef x86_64}
  1932. if (c<127) then
  1933. begin
  1934. if (oper[c and 7]^.typ=top_reg) then
  1935. begin
  1936. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  1937. end;
  1938. end;
  1939. {$endif x86_64}
  1940. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1941. Message(asmw_e_invalid_effective_address)
  1942. else
  1943. inc(len,ea_data.size);
  1944. {$ifdef x86_64}
  1945. rex:=rex or ea_data.rex;
  1946. {$endif x86_64}
  1947. end;
  1948. 242: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  1949. // =>> DEFAULT = 2 Bytes
  1950. begin
  1951. if not(exists_vex) then
  1952. begin
  1953. inc(len, 2);
  1954. exists_vex := true;
  1955. end;
  1956. end;
  1957. 243: // REX.W = 1
  1958. // =>> VEX prefix length = 3
  1959. begin
  1960. if not(exists_vex_extension) then
  1961. begin
  1962. inc(len);
  1963. exists_vex_extension := true;
  1964. end;
  1965. end;
  1966. 244: ; // VEX length bit
  1967. 246, // operand 2 (ymmreg) encoded immediate byte (bit 4-7)
  1968. 247: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  1969. 248: // VEX-Extension prefix $0F
  1970. // ignore for calculating length
  1971. ;
  1972. 249, // VEX-Extension prefix $0F38
  1973. 250: // VEX-Extension prefix $0F3A
  1974. begin
  1975. if not(exists_vex_extension) then
  1976. begin
  1977. inc(len);
  1978. exists_vex_extension := true;
  1979. end;
  1980. end;
  1981. 192,193,194:
  1982. begin
  1983. {$ifdef x86_64}
  1984. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  1985. inc(len);
  1986. {$endif x86_64}
  1987. end;
  1988. else
  1989. InternalError(200603141);
  1990. end;
  1991. until false;
  1992. {$ifdef x86_64}
  1993. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  1994. Message(asmw_e_bad_reg_with_rex);
  1995. rex:=rex and $4F; { reset extra bits in upper nibble }
  1996. if omit_rexw then
  1997. begin
  1998. if rex=$48 then { remove rex entirely? }
  1999. rex:=0
  2000. else
  2001. rex:=rex and $F7;
  2002. end;
  2003. if not(exists_vex) then
  2004. begin
  2005. if rex<>0 then
  2006. Inc(len);
  2007. end;
  2008. {$endif}
  2009. if exists_vex then
  2010. begin
  2011. if exists_prefix_66 then dec(len);
  2012. if exists_prefix_F2 then dec(len);
  2013. if exists_prefix_F3 then dec(len);
  2014. {$ifdef x86_64}
  2015. if not(exists_vex_extension) then
  2016. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extension
  2017. {$endif x86_64}
  2018. end;
  2019. calcsize:=len;
  2020. end;
  2021. procedure taicpu.GenCode(objdata:TObjData);
  2022. {
  2023. * the actual codes (C syntax, i.e. octal):
  2024. * \0 - terminates the code. (Unless it's a literal of course.)
  2025. * \1, \2, \3 - that many literal bytes follow in the code stream
  2026. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  2027. * (POP is never used for CS) depending on operand 0
  2028. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  2029. * on operand 0
  2030. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  2031. * to the register value of operand 0, 1 or 2
  2032. * \13 - a literal byte follows in the code stream, to be added
  2033. * to the condition code value of the instruction.
  2034. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  2035. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  2036. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  2037. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  2038. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  2039. * assembly mode or the address-size override on the operand
  2040. * \37 - a word constant, from the _segment_ part of operand 0
  2041. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  2042. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  2043. on the address size of instruction
  2044. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  2045. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  2046. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  2047. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  2048. * assembly mode or the address-size override on the operand
  2049. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  2050. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  2051. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  2052. * field the register value of operand b.
  2053. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  2054. * field equal to digit b.
  2055. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  2056. * \300,\301,\302 - might be an 0x67, depending on the address size of
  2057. * the memory reference in operand x.
  2058. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  2059. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  2060. * \312 - (disassembler only) invalid with non-default address size.
  2061. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  2062. * size of operand x.
  2063. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  2064. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  2065. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  2066. * \327 - indicates that this instruction is only valid when the
  2067. * operand size is the default (instruction to disassembler,
  2068. * generates no code in the assembler)
  2069. * \331 - instruction not valid with REP prefix. Hint for
  2070. * disassembler only; for SSE instructions.
  2071. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  2072. * \333 - 0xF3 prefix for SSE instructions
  2073. * \334 - 0xF2 prefix for SSE instructions
  2074. * \335 - Indicates 64-bit operand size with REX.W not necessary
  2075. * \361 - 0x66 prefix for SSE instructions
  2076. * \362 - VEX prefix for AVX instructions
  2077. * \363 - VEX W1
  2078. * \364 - VEX Vector length 256
  2079. * \366 - operand 2 (ymmreg) encoded in bit 4-7 of the immediate byte
  2080. * \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
  2081. * \370 - VEX 0F-FLAG
  2082. * \371 - VEX 0F38-FLAG
  2083. * \372 - VEX 0F3A-FLAG
  2084. }
  2085. var
  2086. currval : aint;
  2087. currsym : tobjsymbol;
  2088. currrelreloc,
  2089. currabsreloc,
  2090. currabsreloc32 : TObjRelocationType;
  2091. {$ifdef x86_64}
  2092. rexwritten : boolean;
  2093. {$endif x86_64}
  2094. procedure getvalsym(opidx:longint);
  2095. begin
  2096. case oper[opidx]^.typ of
  2097. top_ref :
  2098. begin
  2099. currval:=oper[opidx]^.ref^.offset;
  2100. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  2101. {$ifdef i386}
  2102. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2103. (tf_pic_uses_got in target_info.flags) then
  2104. begin
  2105. currrelreloc:=RELOC_PLT32;
  2106. currabsreloc:=RELOC_GOT32;
  2107. currabsreloc32:=RELOC_GOT32;
  2108. end
  2109. else
  2110. {$endif i386}
  2111. {$ifdef x86_64}
  2112. if oper[opidx]^.ref^.refaddr=addr_pic then
  2113. begin
  2114. currrelreloc:=RELOC_PLT32;
  2115. currabsreloc:=RELOC_GOTPCREL;
  2116. currabsreloc32:=RELOC_GOTPCREL;
  2117. end
  2118. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  2119. begin
  2120. currrelreloc:=RELOC_RELATIVE;
  2121. currabsreloc:=RELOC_RELATIVE;
  2122. currabsreloc32:=RELOC_RELATIVE;
  2123. end
  2124. else
  2125. {$endif x86_64}
  2126. begin
  2127. currrelreloc:=RELOC_RELATIVE;
  2128. currabsreloc:=RELOC_ABSOLUTE;
  2129. currabsreloc32:=RELOC_ABSOLUTE32;
  2130. end;
  2131. end;
  2132. top_const :
  2133. begin
  2134. currval:=aint(oper[opidx]^.val);
  2135. currsym:=nil;
  2136. currabsreloc:=RELOC_ABSOLUTE;
  2137. currabsreloc32:=RELOC_ABSOLUTE32;
  2138. end;
  2139. else
  2140. Message(asmw_e_immediate_or_reference_expected);
  2141. end;
  2142. end;
  2143. {$ifdef x86_64}
  2144. procedure maybewriterex;
  2145. begin
  2146. if (rex<>0) and not(rexwritten) then
  2147. begin
  2148. rexwritten:=true;
  2149. objdata.writebytes(rex,1);
  2150. end;
  2151. end;
  2152. {$endif x86_64}
  2153. procedure objdata_writereloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  2154. begin
  2155. {$ifdef i386}
  2156. { Special case of '_GLOBAL_OFFSET_TABLE_'
  2157. which needs a special relocation type R_386_GOTPC }
  2158. if assigned (p) and
  2159. (p.name='_GLOBAL_OFFSET_TABLE_') and
  2160. (tf_pic_uses_got in target_info.flags) then
  2161. begin
  2162. { nothing else than a 4 byte relocation should occur
  2163. for GOT }
  2164. if len<>4 then
  2165. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2166. Reloctype:=RELOC_GOTPC;
  2167. { We need to add the offset of the relocation
  2168. of _GLOBAL_OFFSET_TABLE symbol within
  2169. the current instruction }
  2170. inc(data,objdata.currobjsec.size-insoffset);
  2171. end;
  2172. {$endif i386}
  2173. objdata.writereloc(data,len,p,Reloctype);
  2174. end;
  2175. const
  2176. CondVal:array[TAsmCond] of byte=($0,
  2177. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  2178. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  2179. $0, $A, $A, $B, $8, $4);
  2180. var
  2181. c : byte;
  2182. pb : pbyte;
  2183. codes : pchar;
  2184. bytes : array[0..3] of byte;
  2185. rfield,
  2186. data,s,opidx : longint;
  2187. ea_data : ea;
  2188. relsym : TObjSymbol;
  2189. needed_VEX_Extension: boolean;
  2190. needed_VEX: boolean;
  2191. opmode: integer;
  2192. VEXvvvv: byte;
  2193. VEXmmmmm: byte;
  2194. begin
  2195. { safety check }
  2196. if objdata.currobjsec.size<>longword(insoffset) then
  2197. internalerror(200130121);
  2198. { those variables are initialized inside local procedures, the dfa cannot handle this yet }
  2199. currsym:=nil;
  2200. currabsreloc:=RELOC_NONE;
  2201. currabsreloc32:=RELOC_NONE;
  2202. currrelreloc:=RELOC_NONE;
  2203. currval:=0;
  2204. { load data to write }
  2205. codes:=insentry^.code;
  2206. {$ifdef x86_64}
  2207. rexwritten:=false;
  2208. {$endif x86_64}
  2209. { Force word push/pop for registers }
  2210. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  2211. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  2212. begin
  2213. bytes[0]:=$66;
  2214. objdata.writebytes(bytes,1);
  2215. end;
  2216. // needed VEX Prefix (for AVX etc.)
  2217. needed_VEX := false;
  2218. needed_VEX_Extension := false;
  2219. opmode := -1;
  2220. VEXvvvv := 0;
  2221. VEXmmmmm := 0;
  2222. repeat
  2223. c:=ord(codes^);
  2224. inc(codes);
  2225. case c of
  2226. 0: break;
  2227. 1,
  2228. 2,
  2229. 3: inc(codes,c);
  2230. 60: opmode := 0;
  2231. 61: opmode := 1;
  2232. 62: opmode := 2;
  2233. 219: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  2234. 220: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  2235. 241: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  2236. 242: needed_VEX := true;
  2237. 243: begin
  2238. needed_VEX_Extension := true;
  2239. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  2240. end;
  2241. 244: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  2242. 248: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  2243. 249: begin
  2244. needed_VEX_Extension := true;
  2245. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  2246. end;
  2247. 250: begin
  2248. needed_VEX_Extension := true;
  2249. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  2250. end;
  2251. end;
  2252. until false;
  2253. if needed_VEX then
  2254. begin
  2255. if (opmode > ops) or
  2256. (opmode < -1) then
  2257. begin
  2258. Internalerror(777100);
  2259. end
  2260. else if opmode = -1 then
  2261. begin
  2262. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  2263. end
  2264. else if oper[opmode]^.typ = top_reg then
  2265. begin
  2266. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  2267. {$ifdef x86_64}
  2268. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  2269. {$else}
  2270. VEXvvvv := VEXvvvv or (1 shl 6);
  2271. {$endif x86_64}
  2272. end
  2273. else Internalerror(777101);
  2274. if not(needed_VEX_Extension) then
  2275. begin
  2276. {$ifdef x86_64}
  2277. if rex and $0B <> 0 then needed_VEX_Extension := true;
  2278. {$endif x86_64}
  2279. end;
  2280. if needed_VEX_Extension then
  2281. begin
  2282. // VEX-Prefix-Length = 3 Bytes
  2283. bytes[0]:=$C4;
  2284. objdata.writebytes(bytes,1);
  2285. {$ifdef x86_64}
  2286. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  2287. {$else}
  2288. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  2289. {$endif x86_64}
  2290. bytes[0] := VEXmmmmm;
  2291. objdata.writebytes(bytes,1);
  2292. {$ifdef x86_64}
  2293. VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
  2294. {$endif x86_64}
  2295. bytes[0] := VEXvvvv;
  2296. objdata.writebytes(bytes,1);
  2297. end
  2298. else
  2299. begin
  2300. // VEX-Prefix-Length = 2 Bytes
  2301. bytes[0]:=$C5;
  2302. objdata.writebytes(bytes,1);
  2303. {$ifdef x86_64}
  2304. if rex and $04 = 0 then
  2305. {$endif x86_64}
  2306. begin
  2307. VEXvvvv := VEXvvvv or (1 shl 7);
  2308. end;
  2309. bytes[0] := VEXvvvv;
  2310. objdata.writebytes(bytes,1);
  2311. end;
  2312. end
  2313. else
  2314. begin
  2315. needed_VEX_Extension := false;
  2316. opmode := -1;
  2317. end;
  2318. { load data to write }
  2319. codes:=insentry^.code;
  2320. repeat
  2321. c:=ord(codes^);
  2322. inc(codes);
  2323. case c of
  2324. 0 :
  2325. break;
  2326. 1,2,3 :
  2327. begin
  2328. {$ifdef x86_64}
  2329. if not(needed_VEX) then // TG
  2330. maybewriterex;
  2331. {$endif x86_64}
  2332. objdata.writebytes(codes^,c);
  2333. inc(codes,c);
  2334. end;
  2335. 4,6 :
  2336. begin
  2337. case oper[0]^.reg of
  2338. NR_CS:
  2339. bytes[0]:=$e;
  2340. NR_NO,
  2341. NR_DS:
  2342. bytes[0]:=$1e;
  2343. NR_ES:
  2344. bytes[0]:=$6;
  2345. NR_SS:
  2346. bytes[0]:=$16;
  2347. else
  2348. internalerror(777004);
  2349. end;
  2350. if c=4 then
  2351. inc(bytes[0]);
  2352. objdata.writebytes(bytes,1);
  2353. end;
  2354. 5,7 :
  2355. begin
  2356. case oper[0]^.reg of
  2357. NR_FS:
  2358. bytes[0]:=$a0;
  2359. NR_GS:
  2360. bytes[0]:=$a8;
  2361. else
  2362. internalerror(777005);
  2363. end;
  2364. if c=5 then
  2365. inc(bytes[0]);
  2366. objdata.writebytes(bytes,1);
  2367. end;
  2368. 8,9,10 :
  2369. begin
  2370. {$ifdef x86_64}
  2371. if not(needed_VEX) then // TG
  2372. maybewriterex;
  2373. {$endif x86_64}
  2374. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  2375. inc(codes);
  2376. objdata.writebytes(bytes,1);
  2377. end;
  2378. 11 :
  2379. begin
  2380. bytes[0]:=ord(codes^)+condval[condition];
  2381. inc(codes);
  2382. objdata.writebytes(bytes,1);
  2383. end;
  2384. 12,13,14 :
  2385. begin
  2386. getvalsym(c-12);
  2387. if (currval<-128) or (currval>127) then
  2388. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  2389. if assigned(currsym) then
  2390. objdata_writereloc(currval,1,currsym,currabsreloc)
  2391. else
  2392. objdata.writebytes(currval,1);
  2393. end;
  2394. 16,17,18 :
  2395. begin
  2396. getvalsym(c-16);
  2397. if (currval<-256) or (currval>255) then
  2398. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  2399. if assigned(currsym) then
  2400. objdata_writereloc(currval,1,currsym,currabsreloc)
  2401. else
  2402. objdata.writebytes(currval,1);
  2403. end;
  2404. 20,21,22,23 :
  2405. begin
  2406. getvalsym(c-20);
  2407. if (currval<0) or (currval>255) then
  2408. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  2409. if assigned(currsym) then
  2410. objdata_writereloc(currval,1,currsym,currabsreloc)
  2411. else
  2412. objdata.writebytes(currval,1);
  2413. end;
  2414. 24,25,26 : // 030..032
  2415. begin
  2416. getvalsym(c-24);
  2417. {$ifndef i8086}
  2418. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  2419. if (currval<-65536) or (currval>65535) then
  2420. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  2421. {$endif i8086}
  2422. if assigned(currsym) then
  2423. objdata_writereloc(currval,2,currsym,currabsreloc)
  2424. else
  2425. objdata.writebytes(currval,2);
  2426. end;
  2427. 28,29,30 : // 034..036
  2428. { !!! These are intended (and used in opcode table) to select depending
  2429. on address size, *not* operand size. Works by coincidence only. }
  2430. begin
  2431. getvalsym(c-28);
  2432. if opsize=S_Q then
  2433. begin
  2434. if assigned(currsym) then
  2435. objdata_writereloc(currval,8,currsym,currabsreloc)
  2436. else
  2437. objdata.writebytes(currval,8);
  2438. end
  2439. else
  2440. begin
  2441. if assigned(currsym) then
  2442. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2443. else
  2444. objdata.writebytes(currval,4);
  2445. end
  2446. end;
  2447. 32,33,34 : // 040..042
  2448. begin
  2449. getvalsym(c-32);
  2450. if assigned(currsym) then
  2451. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2452. else
  2453. objdata.writebytes(currval,4);
  2454. end;
  2455. 36,37,38 : // 044..046 - select between word/dword/qword depending on
  2456. begin // address size (we support only default address sizes).
  2457. getvalsym(c-36);
  2458. {$ifdef x86_64}
  2459. if assigned(currsym) then
  2460. objdata_writereloc(currval,8,currsym,currabsreloc)
  2461. else
  2462. objdata.writebytes(currval,8);
  2463. {$else x86_64}
  2464. if assigned(currsym) then
  2465. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2466. else
  2467. objdata.writebytes(currval,4);
  2468. {$endif x86_64}
  2469. end;
  2470. 40,41,42 : // 050..052 - byte relative operand
  2471. begin
  2472. getvalsym(c-40);
  2473. data:=currval-insend;
  2474. {$push}
  2475. {$r-}
  2476. if assigned(currsym) then
  2477. inc(data,currsym.address);
  2478. {$pop}
  2479. if (data>127) or (data<-128) then
  2480. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2481. objdata.writebytes(data,1);
  2482. end;
  2483. 44,45,46: // 054..056 - qword immediate operand
  2484. begin
  2485. getvalsym(c-44);
  2486. if assigned(currsym) then
  2487. objdata_writereloc(currval,8,currsym,currabsreloc)
  2488. else
  2489. objdata.writebytes(currval,8);
  2490. end;
  2491. 52,53,54 : // 064..066 - select between 16/32 address mode, but we support only 32
  2492. begin
  2493. getvalsym(c-52);
  2494. if assigned(currsym) then
  2495. objdata_writereloc(currval,4,currsym,currrelreloc)
  2496. else
  2497. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2498. end;
  2499. 56,57,58 : // 070..072 - long relative operand
  2500. begin
  2501. getvalsym(c-56);
  2502. if assigned(currsym) then
  2503. objdata_writereloc(currval,4,currsym,currrelreloc)
  2504. else
  2505. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2506. end;
  2507. 60,61,62 : ; // 074..076 - vex-coded vector operand
  2508. // ignore
  2509. 172,173,174 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2510. begin
  2511. getvalsym(c-172);
  2512. {$ifdef x86_64}
  2513. { for i386 as aint type is longint the
  2514. following test is useless }
  2515. if (currval<low(longint)) or (currval>high(longint)) then
  2516. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2517. {$endif x86_64}
  2518. if assigned(currsym) then
  2519. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2520. else
  2521. objdata.writebytes(currval,4);
  2522. end;
  2523. 192,193,194:
  2524. begin
  2525. {$ifdef x86_64}
  2526. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  2527. begin
  2528. bytes[0]:=$67;
  2529. objdata.writebytes(bytes,1);
  2530. end;
  2531. {$endif x86_64}
  2532. end;
  2533. 200 : { fixed 16-bit addr }
  2534. {$ifndef x86_64}
  2535. begin
  2536. bytes[0]:=$67;
  2537. objdata.writebytes(bytes,1);
  2538. end;
  2539. {$else x86_64}
  2540. { every insentry having code 0310 must be marked with NOX86_64 }
  2541. InternalError(2011051302);
  2542. {$endif}
  2543. 201 : { fixed 32-bit addr }
  2544. {$ifdef x86_64}
  2545. begin
  2546. bytes[0]:=$67;
  2547. objdata.writebytes(bytes,1);
  2548. end
  2549. {$endif x86_64}
  2550. ;
  2551. 208,209,210 :
  2552. begin
  2553. case oper[c-208]^.ot and OT_SIZE_MASK of
  2554. OT_BITS16 :
  2555. begin
  2556. bytes[0]:=$66;
  2557. objdata.writebytes(bytes,1);
  2558. end;
  2559. {$ifndef x86_64}
  2560. OT_BITS64 :
  2561. Message(asmw_e_64bit_not_supported);
  2562. {$endif x86_64}
  2563. end;
  2564. end;
  2565. 211,
  2566. 213 : {no action needed};
  2567. 212,
  2568. 241:
  2569. begin
  2570. if not(needed_VEX) then
  2571. begin
  2572. bytes[0]:=$66;
  2573. objdata.writebytes(bytes,1);
  2574. end;
  2575. end;
  2576. 214 :
  2577. begin
  2578. {$ifndef x86_64}
  2579. Message(asmw_e_64bit_not_supported);
  2580. {$endif x86_64}
  2581. end;
  2582. 219 :
  2583. begin
  2584. if not(needed_VEX) then
  2585. begin
  2586. bytes[0]:=$f3;
  2587. objdata.writebytes(bytes,1);
  2588. end;
  2589. end;
  2590. 220 :
  2591. begin
  2592. if not(needed_VEX) then
  2593. begin
  2594. bytes[0]:=$f2;
  2595. objdata.writebytes(bytes,1);
  2596. end;
  2597. end;
  2598. 221:
  2599. ;
  2600. 202,
  2601. 215,
  2602. 217,218 :
  2603. begin
  2604. { these are dissambler hints or 32 bit prefixes which
  2605. are not needed }
  2606. end;
  2607. 242..244: ; // VEX flags =>> nothing todo
  2608. 246: begin
  2609. if needed_VEX then
  2610. begin
  2611. if ops = 4 then
  2612. begin
  2613. if (oper[2]^.typ=top_reg) then
  2614. begin
  2615. if (oper[2]^.ot and otf_reg_xmm <> 0) or
  2616. (oper[2]^.ot and otf_reg_ymm <> 0) then
  2617. begin
  2618. bytes[0] := ((getsupreg(oper[2]^.reg) and 15) shl 4);
  2619. objdata.writebytes(bytes,1);
  2620. end
  2621. else Internalerror(2014032001);
  2622. end
  2623. else Internalerror(2014032002);
  2624. end
  2625. else Internalerror(2014032003);
  2626. end
  2627. else Internalerror(2014032004);
  2628. end;
  2629. 247: begin
  2630. if needed_VEX then
  2631. begin
  2632. if ops = 4 then
  2633. begin
  2634. if (oper[3]^.typ=top_reg) then
  2635. begin
  2636. if (oper[3]^.ot and otf_reg_xmm <> 0) or
  2637. (oper[3]^.ot and otf_reg_ymm <> 0) then
  2638. begin
  2639. bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
  2640. objdata.writebytes(bytes,1);
  2641. end
  2642. else Internalerror(2014032005);
  2643. end
  2644. else Internalerror(2014032006);
  2645. end
  2646. else Internalerror(2014032007);
  2647. end
  2648. else Internalerror(2014032008);
  2649. end;
  2650. 248..250: ; // VEX flags =>> nothing todo
  2651. 31,
  2652. 48,49,50 :
  2653. begin
  2654. InternalError(777006);
  2655. end
  2656. else
  2657. begin
  2658. { rex should be written at this point }
  2659. {$ifdef x86_64}
  2660. if not(needed_VEX) then // TG
  2661. if (rex<>0) and not(rexwritten) then
  2662. internalerror(200603191);
  2663. {$endif x86_64}
  2664. if (c>=64) and (c<=151) then // 0100..0227
  2665. begin
  2666. if (c<127) then // 0177
  2667. begin
  2668. if (oper[c and 7]^.typ=top_reg) then
  2669. rfield:=regval(oper[c and 7]^.reg)
  2670. else
  2671. rfield:=regval(oper[c and 7]^.ref^.base);
  2672. end
  2673. else
  2674. rfield:=c and 7;
  2675. opidx:=(c shr 3) and 7;
  2676. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2677. Message(asmw_e_invalid_effective_address);
  2678. pb:=@bytes[0];
  2679. pb^:=ea_data.modrm;
  2680. inc(pb);
  2681. if ea_data.sib_present then
  2682. begin
  2683. pb^:=ea_data.sib;
  2684. inc(pb);
  2685. end;
  2686. s:=pb-@bytes[0];
  2687. objdata.writebytes(bytes,s);
  2688. case ea_data.bytes of
  2689. 0 : ;
  2690. 1 :
  2691. begin
  2692. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2693. begin
  2694. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2695. {$ifdef i386}
  2696. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2697. (tf_pic_uses_got in target_info.flags) then
  2698. currabsreloc:=RELOC_GOT32
  2699. else
  2700. {$endif i386}
  2701. {$ifdef x86_64}
  2702. if oper[opidx]^.ref^.refaddr=addr_pic then
  2703. currabsreloc:=RELOC_GOTPCREL
  2704. else
  2705. {$endif x86_64}
  2706. currabsreloc:=RELOC_ABSOLUTE;
  2707. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2708. end
  2709. else
  2710. begin
  2711. bytes[0]:=oper[opidx]^.ref^.offset;
  2712. objdata.writebytes(bytes,1);
  2713. end;
  2714. inc(s);
  2715. end;
  2716. 2,4 :
  2717. begin
  2718. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2719. currval:=oper[opidx]^.ref^.offset;
  2720. {$ifdef x86_64}
  2721. if oper[opidx]^.ref^.refaddr=addr_pic then
  2722. currabsreloc:=RELOC_GOTPCREL
  2723. else
  2724. if oper[opidx]^.ref^.base=NR_RIP then
  2725. begin
  2726. currabsreloc:=RELOC_RELATIVE;
  2727. { Adjust reloc value by number of bytes following the displacement,
  2728. but not if displacement is specified by literal constant }
  2729. if Assigned(currsym) then
  2730. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  2731. end
  2732. else
  2733. {$endif x86_64}
  2734. {$ifdef i386}
  2735. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2736. (tf_pic_uses_got in target_info.flags) then
  2737. currabsreloc:=RELOC_GOT32
  2738. else
  2739. {$endif i386}
  2740. currabsreloc:=RELOC_ABSOLUTE32;
  2741. if (currabsreloc=RELOC_ABSOLUTE32) and
  2742. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  2743. begin
  2744. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  2745. if relsym.objsection=objdata.CurrObjSec then
  2746. begin
  2747. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  2748. currabsreloc:=RELOC_RELATIVE;
  2749. end
  2750. else
  2751. begin
  2752. currabsreloc:=RELOC_PIC_PAIR;
  2753. currval:=relsym.offset;
  2754. end;
  2755. end;
  2756. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  2757. inc(s,ea_data.bytes);
  2758. end;
  2759. end;
  2760. end
  2761. else
  2762. InternalError(777007);
  2763. end;
  2764. end;
  2765. until false;
  2766. end;
  2767. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2768. begin
  2769. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2770. (regtype = R_INTREGISTER) and
  2771. (ops=2) and
  2772. (oper[0]^.typ=top_reg) and
  2773. (oper[1]^.typ=top_reg) and
  2774. (oper[0]^.reg=oper[1]^.reg)
  2775. ) or
  2776. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2777. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD) or
  2778. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or (opcode=A_VMOVQ) or
  2779. (opcode=A_VMOVAPS) or (OPCODE=A_VMOVAPD)) and
  2780. (regtype = R_MMREGISTER) and
  2781. (ops=2) and
  2782. (oper[0]^.typ=top_reg) and
  2783. (oper[1]^.typ=top_reg) and
  2784. (oper[0]^.reg=oper[1]^.reg)
  2785. );
  2786. end;
  2787. procedure build_spilling_operation_type_table;
  2788. var
  2789. opcode : tasmop;
  2790. i : integer;
  2791. begin
  2792. new(operation_type_table);
  2793. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2794. for opcode:=low(tasmop) to high(tasmop) do
  2795. begin
  2796. for i:=1 to MaxInsChanges do
  2797. begin
  2798. case InsProp[opcode].Ch[i] of
  2799. Ch_Rop1 :
  2800. operation_type_table^[opcode,0]:=operand_read;
  2801. Ch_Wop1 :
  2802. operation_type_table^[opcode,0]:=operand_write;
  2803. Ch_RWop1,
  2804. Ch_Mop1 :
  2805. operation_type_table^[opcode,0]:=operand_readwrite;
  2806. Ch_Rop2 :
  2807. operation_type_table^[opcode,1]:=operand_read;
  2808. Ch_Wop2 :
  2809. operation_type_table^[opcode,1]:=operand_write;
  2810. Ch_RWop2,
  2811. Ch_Mop2 :
  2812. operation_type_table^[opcode,1]:=operand_readwrite;
  2813. Ch_Rop3 :
  2814. operation_type_table^[opcode,2]:=operand_read;
  2815. Ch_Wop3 :
  2816. operation_type_table^[opcode,2]:=operand_write;
  2817. Ch_RWop3,
  2818. Ch_Mop3 :
  2819. operation_type_table^[opcode,2]:=operand_readwrite;
  2820. end;
  2821. end;
  2822. end;
  2823. end;
  2824. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2825. begin
  2826. { the information in the instruction table is made for the string copy
  2827. operation MOVSD so hack here (FK)
  2828. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  2829. so fix it here (FK)
  2830. }
  2831. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  2832. begin
  2833. case opnr of
  2834. 0:
  2835. result:=operand_read;
  2836. 1:
  2837. result:=operand_write;
  2838. else
  2839. internalerror(200506055);
  2840. end
  2841. end
  2842. { IMUL has 1, 2 and 3-operand forms }
  2843. else if opcode=A_IMUL then
  2844. begin
  2845. case ops of
  2846. 1:
  2847. if opnr=0 then
  2848. result:=operand_read
  2849. else
  2850. internalerror(2014011802);
  2851. 2:
  2852. begin
  2853. case opnr of
  2854. 0:
  2855. result:=operand_read;
  2856. 1:
  2857. result:=operand_readwrite;
  2858. else
  2859. internalerror(2014011803);
  2860. end;
  2861. end;
  2862. 3:
  2863. begin
  2864. case opnr of
  2865. 0,1:
  2866. result:=operand_read;
  2867. 2:
  2868. result:=operand_write;
  2869. else
  2870. internalerror(2014011804);
  2871. end;
  2872. end;
  2873. else
  2874. internalerror(2014011805);
  2875. end;
  2876. end
  2877. else
  2878. result:=operation_type_table^[opcode,opnr];
  2879. end;
  2880. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2881. var
  2882. tmpref: treference;
  2883. begin
  2884. tmpref:=ref;
  2885. {$ifdef i8086}
  2886. if tmpref.segment=NR_SS then
  2887. tmpref.segment:=NR_NO;
  2888. {$endif i8086}
  2889. case getregtype(r) of
  2890. R_INTREGISTER :
  2891. begin
  2892. if getsubreg(r)=R_SUBH then
  2893. inc(tmpref.offset);
  2894. { we don't need special code here for 32 bit loads on x86_64, since
  2895. those will automatically zero-extend the upper 32 bits. }
  2896. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  2897. end;
  2898. R_MMREGISTER :
  2899. if current_settings.fputype in fpu_avx_instructionsets then
  2900. case getsubreg(r) of
  2901. R_SUBMMD:
  2902. result:=taicpu.op_ref_reg(A_VMOVSD,reg2opsize(r),tmpref,r);
  2903. R_SUBMMS:
  2904. result:=taicpu.op_ref_reg(A_VMOVSS,reg2opsize(r),tmpref,r);
  2905. R_SUBQ,
  2906. R_SUBMMWHOLE:
  2907. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,tmpref,r);
  2908. else
  2909. internalerror(200506043);
  2910. end
  2911. else
  2912. case getsubreg(r) of
  2913. R_SUBMMD:
  2914. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),tmpref,r);
  2915. R_SUBMMS:
  2916. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),tmpref,r);
  2917. R_SUBQ,
  2918. R_SUBMMWHOLE:
  2919. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,r);
  2920. else
  2921. internalerror(200506043);
  2922. end;
  2923. else
  2924. internalerror(200401041);
  2925. end;
  2926. end;
  2927. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2928. var
  2929. size: topsize;
  2930. tmpref: treference;
  2931. begin
  2932. tmpref:=ref;
  2933. {$ifdef i8086}
  2934. if tmpref.segment=NR_SS then
  2935. tmpref.segment:=NR_NO;
  2936. {$endif i8086}
  2937. case getregtype(r) of
  2938. R_INTREGISTER :
  2939. begin
  2940. if getsubreg(r)=R_SUBH then
  2941. inc(tmpref.offset);
  2942. size:=reg2opsize(r);
  2943. {$ifdef x86_64}
  2944. { even if it's a 32 bit reg, we still have to spill 64 bits
  2945. because we often perform 64 bit operations on them }
  2946. if (size=S_L) then
  2947. begin
  2948. size:=S_Q;
  2949. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  2950. end;
  2951. {$endif x86_64}
  2952. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  2953. end;
  2954. R_MMREGISTER :
  2955. if current_settings.fputype in fpu_avx_instructionsets then
  2956. case getsubreg(r) of
  2957. R_SUBMMD:
  2958. result:=taicpu.op_reg_ref(A_VMOVSD,reg2opsize(r),r,tmpref);
  2959. R_SUBMMS:
  2960. result:=taicpu.op_reg_ref(A_VMOVSS,reg2opsize(r),r,tmpref);
  2961. R_SUBQ,
  2962. R_SUBMMWHOLE:
  2963. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,tmpref);
  2964. else
  2965. internalerror(200506042);
  2966. end
  2967. else
  2968. case getsubreg(r) of
  2969. R_SUBMMD:
  2970. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,tmpref);
  2971. R_SUBMMS:
  2972. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,tmpref);
  2973. R_SUBQ,
  2974. R_SUBMMWHOLE:
  2975. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,tmpref);
  2976. else
  2977. internalerror(200506042);
  2978. end;
  2979. else
  2980. internalerror(200401041);
  2981. end;
  2982. end;
  2983. {*****************************************************************************
  2984. Instruction table
  2985. *****************************************************************************}
  2986. procedure BuildInsTabCache;
  2987. var
  2988. i : longint;
  2989. begin
  2990. new(instabcache);
  2991. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2992. i:=0;
  2993. while (i<InsTabEntries) do
  2994. begin
  2995. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2996. InsTabCache^[InsTab[i].OPcode]:=i;
  2997. inc(i);
  2998. end;
  2999. end;
  3000. procedure BuildInsTabMemRefSizeInfoCache;
  3001. var
  3002. AsmOp: TasmOp;
  3003. i,j: longint;
  3004. insentry : PInsEntry;
  3005. MRefInfo: TMemRefSizeInfo;
  3006. SConstInfo: TConstSizeInfo;
  3007. actRegSize: int64;
  3008. actMemSize: int64;
  3009. actConstSize: int64;
  3010. actRegCount: integer;
  3011. actMemCount: integer;
  3012. actConstCount: integer;
  3013. actRegTypes : int64;
  3014. actRegMemTypes: int64;
  3015. NewRegSize: int64;
  3016. RegMMXSizeMask: int64;
  3017. RegXMMSizeMask: int64;
  3018. RegYMMSizeMask: int64;
  3019. bitcount: integer;
  3020. function bitcnt(aValue: int64): integer;
  3021. var
  3022. i: integer;
  3023. begin
  3024. result := 0;
  3025. for i := 0 to 63 do
  3026. begin
  3027. if (aValue mod 2) = 1 then
  3028. begin
  3029. inc(result);
  3030. end;
  3031. aValue := aValue shr 1;
  3032. end;
  3033. end;
  3034. begin
  3035. new(InsTabMemRefSizeInfoCache);
  3036. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  3037. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3038. begin
  3039. i := InsTabCache^[AsmOp];
  3040. if i >= 0 then
  3041. begin
  3042. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3043. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3044. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  3045. insentry:=@instab[i];
  3046. RegMMXSizeMask := 0;
  3047. RegXMMSizeMask := 0;
  3048. RegYMMSizeMask := 0;
  3049. while (insentry^.opcode=AsmOp) do
  3050. begin
  3051. MRefInfo := msiUnkown;
  3052. actRegSize := 0;
  3053. actRegCount := 0;
  3054. actRegTypes := 0;
  3055. NewRegSize := 0;
  3056. actMemSize := 0;
  3057. actMemCount := 0;
  3058. actRegMemTypes := 0;
  3059. actConstSize := 0;
  3060. actConstCount := 0;
  3061. if asmop = a_vpmovzxbq then
  3062. begin
  3063. RegXMMSizeMask := RegXMMSizeMask;
  3064. end;
  3065. for j := 0 to insentry^.ops -1 do
  3066. begin
  3067. if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  3068. begin
  3069. inc(actRegCount);
  3070. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  3071. if NewRegSize = 0 then
  3072. begin
  3073. case insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG) of
  3074. OT_MMXREG: begin
  3075. NewRegSize := OT_BITS64;
  3076. end;
  3077. OT_XMMREG: begin
  3078. NewRegSize := OT_BITS128;
  3079. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3080. end;
  3081. OT_YMMREG: begin
  3082. NewRegSize := OT_BITS256;
  3083. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3084. end;
  3085. else NewRegSize := not(0);
  3086. end;
  3087. end;
  3088. actRegSize := actRegSize or NewRegSize;
  3089. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG));
  3090. end
  3091. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  3092. begin
  3093. inc(actMemCount);
  3094. actMemSize:=actMemSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3095. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  3096. begin
  3097. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  3098. end;
  3099. end
  3100. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  3101. begin
  3102. inc(actConstCount);
  3103. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3104. end
  3105. end;
  3106. if actConstCount > 0 then
  3107. begin
  3108. case actConstSize of
  3109. 0: SConstInfo := csiNoSize;
  3110. OT_BITS8: SConstInfo := csiMem8;
  3111. OT_BITS16: SConstInfo := csiMem16;
  3112. OT_BITS32: SConstInfo := csiMem32;
  3113. OT_BITS64: SConstInfo := csiMem64;
  3114. else SConstInfo := csiMultiple;
  3115. end;
  3116. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
  3117. begin
  3118. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  3119. end
  3120. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  3121. begin
  3122. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  3123. end;
  3124. end;
  3125. case actMemCount of
  3126. 0: ; // nothing todo
  3127. 1: begin
  3128. MRefInfo := msiUnkown;
  3129. case actRegMemTypes and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM) of
  3130. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  3131. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  3132. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  3133. end;
  3134. case actMemSize of
  3135. 0: MRefInfo := msiNoSize;
  3136. OT_BITS8: MRefInfo := msiMem8;
  3137. OT_BITS16: MRefInfo := msiMem16;
  3138. OT_BITS32: MRefInfo := msiMem32;
  3139. OT_BITS64: MRefInfo := msiMem64;
  3140. OT_BITS128: MRefInfo := msiMem128;
  3141. OT_BITS256: MRefInfo := msiMem256;
  3142. OT_BITS80,
  3143. OT_FAR,
  3144. OT_NEAR,
  3145. OT_SHORT: ; // ignore
  3146. else
  3147. begin
  3148. bitcount := bitcnt(actMemSize);
  3149. if bitcount > 1 then MRefInfo := msiMultiple
  3150. else InternalError(777203);
  3151. end;
  3152. end;
  3153. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3154. begin
  3155. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3156. end
  3157. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3158. begin
  3159. with InsTabMemRefSizeInfoCache^[AsmOp] do
  3160. begin
  3161. if ((MemRefSize = msiMem8) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultiple8
  3162. else if ((MemRefSize = msiMem16) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultiple16
  3163. else if ((MemRefSize = msiMem32) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultiple32
  3164. else if ((MemRefSize = msiMem64) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultiple64
  3165. else if ((MemRefSize = msiMem128) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultiple128
  3166. else if ((MemRefSize = msiMem256) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultiple256
  3167. else MemRefSize := msiMultiple;
  3168. end;
  3169. end;
  3170. if actRegCount > 0 then
  3171. begin
  3172. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG) of
  3173. OT_MMXREG: RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  3174. OT_XMMREG: RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  3175. OT_YMMREG: RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  3176. else begin
  3177. RegMMXSizeMask := not(0);
  3178. RegXMMSizeMask := not(0);
  3179. RegYMMSizeMask := not(0);
  3180. end;
  3181. end;
  3182. end;
  3183. end;
  3184. else InternalError(777202);
  3185. end;
  3186. inc(insentry);
  3187. end;
  3188. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  3189. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  3190. begin
  3191. case RegXMMSizeMask of
  3192. OT_BITS16: case RegYMMSizeMask of
  3193. OT_BITS32: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx16y32;
  3194. end;
  3195. OT_BITS32: case RegYMMSizeMask of
  3196. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx32y64;
  3197. end;
  3198. OT_BITS64: case RegYMMSizeMask of
  3199. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3200. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  3201. end;
  3202. OT_BITS128: begin
  3203. if RegMMXSizeMask = 0 then
  3204. begin
  3205. case RegYMMSizeMask of
  3206. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3207. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3208. end;
  3209. end
  3210. else if RegYMMSizeMask = 0 then
  3211. begin
  3212. case RegMMXSizeMask of
  3213. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3214. end;
  3215. end
  3216. else InternalError(777205);
  3217. end;
  3218. end;
  3219. end;
  3220. end;
  3221. end;
  3222. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3223. begin
  3224. // only supported intructiones with SSE- or AVX-operands
  3225. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  3226. begin
  3227. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3228. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3229. end;
  3230. end;
  3231. end;
  3232. procedure InitAsm;
  3233. begin
  3234. build_spilling_operation_type_table;
  3235. if not assigned(instabcache) then
  3236. BuildInsTabCache;
  3237. if not assigned(InsTabMemRefSizeInfoCache) then
  3238. BuildInsTabMemRefSizeInfoCache;
  3239. end;
  3240. procedure DoneAsm;
  3241. begin
  3242. if assigned(operation_type_table) then
  3243. begin
  3244. dispose(operation_type_table);
  3245. operation_type_table:=nil;
  3246. end;
  3247. if assigned(instabcache) then
  3248. begin
  3249. dispose(instabcache);
  3250. instabcache:=nil;
  3251. end;
  3252. if assigned(InsTabMemRefSizeInfoCache) then
  3253. begin
  3254. dispose(InsTabMemRefSizeInfoCache);
  3255. InsTabMemRefSizeInfoCache:=nil;
  3256. end;
  3257. end;
  3258. begin
  3259. cai_align:=tai_align;
  3260. cai_cpu:=taicpu;
  3261. end.