aasmcpu.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  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,globals,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,
  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. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  38. OT_BITS16 = $00000002;
  39. OT_BITS32 = $00000004;
  40. OT_BITS64 = $00000008; { FPU only }
  41. OT_BITS80 = $00000010;
  42. OT_SIZE_MASK = $0000001F; { all the size attributes }
  43. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  44. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  45. OT_NEAR = $00000040;
  46. OT_SHORT = $00000080;
  47. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  48. OT_TO = $00000200; { operand is followed by a colon }
  49. { reverse effect in FADD, FSUB &c }
  50. OT_COLON = $00000400;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_IMM8 = $00002001;
  54. OT_IMM16 = $00002002;
  55. OT_IMM32 = $00002004;
  56. OT_IMM64 = $00002008;
  57. OT_IMM80 = $00002010;
  58. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  59. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  60. OT_REG8 = $00201001;
  61. OT_REG16 = $00201002;
  62. OT_REG32 = $00201004;
  63. OT_REG64 = $00201008;
  64. OT_XMMREG = $00201010; { Katmai registers }
  65. OT_MMXREG = $00201020; { MMX registers }
  66. OT_MEMORY = $00204000; { register number in 'basereg' }
  67. OT_MEM8 = $00204001;
  68. OT_MEM16 = $00204002;
  69. OT_MEM32 = $00204004;
  70. OT_MEM64 = $00204008;
  71. OT_MEM80 = $00204010;
  72. OT_FPUREG = $01000000; { floating point stack registers }
  73. OT_FPU0 = $01000800; { FPU stack register zero }
  74. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  75. { a mask for the following }
  76. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  77. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  78. OT_REG_AX = $00211002; { ditto }
  79. OT_REG_EAX = $00211004; { and again }
  80. {$ifdef x86_64}
  81. OT_REG_RAX = $00211008;
  82. {$endif x86_64}
  83. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  84. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  85. OT_REG_CX = $00221002; { ditto }
  86. OT_REG_ECX = $00221004; { another one }
  87. {$ifdef x86_64}
  88. OT_REG_RCX = $00221008;
  89. {$endif x86_64}
  90. OT_REG_DX = $00241002;
  91. OT_REG_EDX = $00241004;
  92. OT_REG_SREG = $00081002; { any segment register }
  93. OT_REG_CS = $01081002; { CS }
  94. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  95. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  96. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  97. OT_REG_CREG = $08101004; { CRn }
  98. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  99. OT_REG_DREG = $10101004; { DRn }
  100. OT_REG_TREG = $20101004; { TRn }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. { Size of the instruction table converted by nasmconv.pas }
  107. {$ifdef x86_64}
  108. instabentries = {$i x8664nop.inc}
  109. {$else x86_64}
  110. instabentries = {$i i386nop.inc}
  111. {$endif x86_64}
  112. maxinfolen = 8;
  113. MaxInsChanges = 3; { Max things a instruction can change }
  114. type
  115. { What an instruction can change. Needed for optimizer and spilling code.
  116. Note: The order of this enumeration is should not be changed! }
  117. TInsChange = (Ch_None,
  118. {Read from a register}
  119. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  120. {write from a register}
  121. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  122. {read and write from/to a register}
  123. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  124. {modify the contents of a register with the purpose of using
  125. this changed content afterwards (add/sub/..., but e.g. not rep
  126. or movsd)}
  127. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  128. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  129. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  130. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  131. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  132. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  133. Ch_WMemEDI,
  134. Ch_All,
  135. { x86_64 registers }
  136. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  137. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  138. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  139. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  140. );
  141. TInsProp = packed record
  142. Ch : Array[1..MaxInsChanges] of TInsChange;
  143. end;
  144. const
  145. InsProp : array[tasmop] of TInsProp =
  146. {$ifdef x86_64}
  147. {$i x8664pro.inc}
  148. {$else x86_64}
  149. {$i i386prop.inc}
  150. {$endif x86_64}
  151. type
  152. TOperandOrder = (op_intel,op_att);
  153. tinsentry=packed record
  154. opcode : tasmop;
  155. ops : byte;
  156. optypes : array[0..2] of longint;
  157. code : array[0..maxinfolen] of char;
  158. flags : longint;
  159. end;
  160. pinsentry=^tinsentry;
  161. { alignment for operator }
  162. tai_align = class(tai_align_abstract)
  163. reg : tregister;
  164. constructor create(b:byte);override;
  165. constructor create_op(b: byte; _op: byte);override;
  166. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  167. end;
  168. taicpu = class(tai_cpu_abstract)
  169. opsize : topsize;
  170. constructor op_none(op : tasmop);
  171. constructor op_none(op : tasmop;_size : topsize);
  172. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  173. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  174. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  175. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  176. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  177. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  178. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  179. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  180. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  181. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  182. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  183. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  184. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  185. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  186. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  187. { this is for Jmp instructions }
  188. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  189. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  190. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  191. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  192. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  193. procedure changeopsize(siz:topsize);
  194. function GetString:string;
  195. procedure CheckNonCommutativeOpcodes;
  196. private
  197. FOperandOrder : TOperandOrder;
  198. procedure init(_size : topsize); { this need to be called by all constructor }
  199. public
  200. { the next will reset all instructions that can change in pass 2 }
  201. procedure ResetPass1;override;
  202. procedure ResetPass2;override;
  203. function CheckIfValid:boolean;
  204. function Pass1(objdata:TObjData):longint;override;
  205. procedure Pass2(objdata:TObjData);override;
  206. procedure SetOperandOrder(order:TOperandOrder);
  207. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  208. { register spilling code }
  209. function spilling_get_operation_type(opnr: longint): topertype;override;
  210. protected
  211. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  212. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  213. procedure ppubuildderefimploper(var o:toper);override;
  214. procedure ppuderefoper(var o:toper);override;
  215. private
  216. { next fields are filled in pass1, so pass2 is faster }
  217. insentry : PInsEntry;
  218. insoffset : longint;
  219. LastInsOffset : longint; { need to be public to be reset }
  220. inssize : shortint;
  221. {$ifdef x86_64}
  222. rex : byte;
  223. {$endif x86_64}
  224. function InsEnd:longint;
  225. procedure create_ot(objdata:TObjData);
  226. function Matches(p:PInsEntry):boolean;
  227. function calcsize(p:PInsEntry):shortint;
  228. procedure gencode(objdata:TObjData);
  229. function NeedAddrPrefix(opidx:byte):boolean;
  230. procedure Swapoperands;
  231. function FindInsentry(objdata:TObjData):boolean;
  232. end;
  233. function spilling_create_load(const ref:treference;r:tregister): tai;
  234. function spilling_create_store(r:tregister; const ref:treference): tai;
  235. procedure InitAsm;
  236. procedure DoneAsm;
  237. implementation
  238. uses
  239. cutils,
  240. itcpugas,
  241. symsym;
  242. {*****************************************************************************
  243. Instruction table
  244. *****************************************************************************}
  245. const
  246. {Instruction flags }
  247. IF_NONE = $00000000;
  248. IF_SM = $00000001; { size match first two operands }
  249. IF_SM2 = $00000002;
  250. IF_SB = $00000004; { unsized operands can't be non-byte }
  251. IF_SW = $00000008; { unsized operands can't be non-word }
  252. IF_SD = $00000010; { unsized operands can't be nondword }
  253. IF_SMASK = $0000001f;
  254. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  255. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  256. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  257. IF_ARMASK = $00000060; { mask for unsized argument spec }
  258. IF_PRIV = $00000100; { it's a privileged instruction }
  259. IF_SMM = $00000200; { it's only valid in SMM }
  260. IF_PROT = $00000400; { it's protected mode only }
  261. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  262. IF_UNDOC = $00001000; { it's an undocumented instruction }
  263. IF_FPU = $00002000; { it's an FPU instruction }
  264. IF_MMX = $00004000; { it's an MMX instruction }
  265. { it's a 3DNow! instruction }
  266. IF_3DNOW = $00008000;
  267. { it's a SSE (KNI, MMX2) instruction }
  268. IF_SSE = $00010000;
  269. { SSE2 instructions }
  270. IF_SSE2 = $00020000;
  271. { SSE3 instructions }
  272. IF_SSE3 = $00040000;
  273. { SSE64 instructions }
  274. IF_SSE64 = $00080000;
  275. { the mask for processor types }
  276. {IF_PMASK = longint($FF000000);}
  277. { the mask for disassembly "prefer" }
  278. {IF_PFMASK = longint($F001FF00);}
  279. { SVM instructions }
  280. IF_SVM = $00100000;
  281. IF_8086 = $00000000; { 8086 instruction }
  282. IF_186 = $01000000; { 186+ instruction }
  283. IF_286 = $02000000; { 286+ instruction }
  284. IF_386 = $03000000; { 386+ instruction }
  285. IF_486 = $04000000; { 486+ instruction }
  286. IF_PENT = $05000000; { Pentium instruction }
  287. IF_P6 = $06000000; { P6 instruction }
  288. IF_KATMAI = $07000000; { Katmai instructions }
  289. { Willamette instructions }
  290. IF_WILLAMETTE = $08000000;
  291. { Prescott instructions }
  292. IF_PRESCOTT = $09000000;
  293. IF_X86_64 = $0a000000;
  294. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  295. IF_AMD = $0c000000; { AMD-specific instruction }
  296. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  297. { added flags }
  298. IF_PRE = $40000000; { it's a prefix instruction }
  299. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  300. type
  301. TInsTabCache=array[TasmOp] of longint;
  302. PInsTabCache=^TInsTabCache;
  303. const
  304. {$ifdef x86_64}
  305. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  306. {$else x86_64}
  307. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  308. {$endif x86_64}
  309. var
  310. InsTabCache : PInsTabCache;
  311. const
  312. {$ifdef x86_64}
  313. { Intel style operands ! }
  314. opsize_2_type:array[0..2,topsize] of longint=(
  315. (OT_NONE,
  316. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  317. OT_BITS16,OT_BITS32,OT_BITS64,
  318. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  319. OT_BITS64,
  320. OT_NEAR,OT_FAR,OT_SHORT,
  321. OT_NONE,
  322. OT_NONE
  323. ),
  324. (OT_NONE,
  325. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  326. OT_BITS16,OT_BITS32,OT_BITS64,
  327. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  328. OT_BITS64,
  329. OT_NEAR,OT_FAR,OT_SHORT,
  330. OT_NONE,
  331. OT_NONE
  332. ),
  333. (OT_NONE,
  334. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  335. OT_BITS16,OT_BITS32,OT_BITS64,
  336. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  337. OT_BITS64,
  338. OT_NEAR,OT_FAR,OT_SHORT,
  339. OT_NONE,
  340. OT_NONE
  341. )
  342. );
  343. reg_ot_table : array[tregisterindex] of longint = (
  344. {$i r8664ot.inc}
  345. );
  346. {$else x86_64}
  347. { Intel style operands ! }
  348. opsize_2_type:array[0..2,topsize] of longint=(
  349. (OT_NONE,
  350. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  351. OT_BITS16,OT_BITS32,OT_BITS64,
  352. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  353. OT_BITS64,
  354. OT_NEAR,OT_FAR,OT_SHORT,
  355. OT_NONE,
  356. OT_NONE
  357. ),
  358. (OT_NONE,
  359. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  360. OT_BITS16,OT_BITS32,OT_BITS64,
  361. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  362. OT_BITS64,
  363. OT_NEAR,OT_FAR,OT_SHORT,
  364. OT_NONE,
  365. OT_NONE
  366. ),
  367. (OT_NONE,
  368. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  369. OT_BITS16,OT_BITS32,OT_BITS64,
  370. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  371. OT_BITS64,
  372. OT_NEAR,OT_FAR,OT_SHORT,
  373. OT_NONE,
  374. OT_NONE
  375. )
  376. );
  377. reg_ot_table : array[tregisterindex] of longint = (
  378. {$i r386ot.inc}
  379. );
  380. {$endif x86_64}
  381. { Operation type for spilling code }
  382. type
  383. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  384. var
  385. operation_type_table : ^toperation_type_table;
  386. {****************************************************************************
  387. TAI_ALIGN
  388. ****************************************************************************}
  389. constructor tai_align.create(b: byte);
  390. begin
  391. inherited create(b);
  392. reg:=NR_ECX;
  393. end;
  394. constructor tai_align.create_op(b: byte; _op: byte);
  395. begin
  396. inherited create_op(b,_op);
  397. reg:=NR_NO;
  398. end;
  399. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  400. const
  401. alignarray:array[0..5] of string[8]=(
  402. #$8D#$B4#$26#$00#$00#$00#$00,
  403. #$8D#$B6#$00#$00#$00#$00,
  404. #$8D#$74#$26#$00,
  405. #$8D#$76#$00,
  406. #$89#$F6,
  407. #$90
  408. );
  409. var
  410. bufptr : pchar;
  411. j : longint;
  412. begin
  413. inherited calculatefillbuf(buf);
  414. if not use_op then
  415. begin
  416. bufptr:=pchar(@buf);
  417. while (fillsize>0) do
  418. begin
  419. for j:=0 to 5 do
  420. if (fillsize>=length(alignarray[j])) then
  421. break;
  422. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  423. inc(bufptr,length(alignarray[j]));
  424. dec(fillsize,length(alignarray[j]));
  425. end;
  426. end;
  427. calculatefillbuf:=pchar(@buf);
  428. end;
  429. {*****************************************************************************
  430. Taicpu Constructors
  431. *****************************************************************************}
  432. procedure taicpu.changeopsize(siz:topsize);
  433. begin
  434. opsize:=siz;
  435. end;
  436. procedure taicpu.init(_size : topsize);
  437. begin
  438. { default order is att }
  439. FOperandOrder:=op_att;
  440. segprefix:=NR_NO;
  441. opsize:=_size;
  442. insentry:=nil;
  443. LastInsOffset:=-1;
  444. InsOffset:=0;
  445. InsSize:=0;
  446. end;
  447. constructor taicpu.op_none(op : tasmop);
  448. begin
  449. inherited create(op);
  450. init(S_NO);
  451. end;
  452. constructor taicpu.op_none(op : tasmop;_size : topsize);
  453. begin
  454. inherited create(op);
  455. init(_size);
  456. end;
  457. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  458. begin
  459. inherited create(op);
  460. init(_size);
  461. ops:=1;
  462. loadreg(0,_op1);
  463. end;
  464. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  465. begin
  466. inherited create(op);
  467. init(_size);
  468. ops:=1;
  469. loadconst(0,_op1);
  470. end;
  471. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  472. begin
  473. inherited create(op);
  474. init(_size);
  475. ops:=1;
  476. loadref(0,_op1);
  477. end;
  478. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  479. begin
  480. inherited create(op);
  481. init(_size);
  482. ops:=2;
  483. loadreg(0,_op1);
  484. loadreg(1,_op2);
  485. end;
  486. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  487. begin
  488. inherited create(op);
  489. init(_size);
  490. ops:=2;
  491. loadreg(0,_op1);
  492. loadconst(1,_op2);
  493. end;
  494. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  495. begin
  496. inherited create(op);
  497. init(_size);
  498. ops:=2;
  499. loadreg(0,_op1);
  500. loadref(1,_op2);
  501. end;
  502. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  503. begin
  504. inherited create(op);
  505. init(_size);
  506. ops:=2;
  507. loadconst(0,_op1);
  508. loadreg(1,_op2);
  509. end;
  510. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  511. begin
  512. inherited create(op);
  513. init(_size);
  514. ops:=2;
  515. loadconst(0,_op1);
  516. loadconst(1,_op2);
  517. end;
  518. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  519. begin
  520. inherited create(op);
  521. init(_size);
  522. ops:=2;
  523. loadconst(0,_op1);
  524. loadref(1,_op2);
  525. end;
  526. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  527. begin
  528. inherited create(op);
  529. init(_size);
  530. ops:=2;
  531. loadref(0,_op1);
  532. loadreg(1,_op2);
  533. end;
  534. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  535. begin
  536. inherited create(op);
  537. init(_size);
  538. ops:=3;
  539. loadreg(0,_op1);
  540. loadreg(1,_op2);
  541. loadreg(2,_op3);
  542. end;
  543. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  544. begin
  545. inherited create(op);
  546. init(_size);
  547. ops:=3;
  548. loadconst(0,_op1);
  549. loadreg(1,_op2);
  550. loadreg(2,_op3);
  551. end;
  552. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  553. begin
  554. inherited create(op);
  555. init(_size);
  556. ops:=3;
  557. loadreg(0,_op1);
  558. loadreg(1,_op2);
  559. loadref(2,_op3);
  560. end;
  561. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  562. begin
  563. inherited create(op);
  564. init(_size);
  565. ops:=3;
  566. loadconst(0,_op1);
  567. loadref(1,_op2);
  568. loadreg(2,_op3);
  569. end;
  570. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  571. begin
  572. inherited create(op);
  573. init(_size);
  574. ops:=3;
  575. loadconst(0,_op1);
  576. loadreg(1,_op2);
  577. loadref(2,_op3);
  578. end;
  579. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  580. begin
  581. inherited create(op);
  582. init(_size);
  583. condition:=cond;
  584. ops:=1;
  585. loadsymbol(0,_op1,0);
  586. end;
  587. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  588. begin
  589. inherited create(op);
  590. init(_size);
  591. ops:=1;
  592. loadsymbol(0,_op1,0);
  593. end;
  594. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  595. begin
  596. inherited create(op);
  597. init(_size);
  598. ops:=1;
  599. loadsymbol(0,_op1,_op1ofs);
  600. end;
  601. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  602. begin
  603. inherited create(op);
  604. init(_size);
  605. ops:=2;
  606. loadsymbol(0,_op1,_op1ofs);
  607. loadreg(1,_op2);
  608. end;
  609. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  610. begin
  611. inherited create(op);
  612. init(_size);
  613. ops:=2;
  614. loadsymbol(0,_op1,_op1ofs);
  615. loadref(1,_op2);
  616. end;
  617. function taicpu.GetString:string;
  618. var
  619. i : longint;
  620. s : string;
  621. addsize : boolean;
  622. begin
  623. s:='['+std_op2str[opcode];
  624. for i:=0 to ops-1 do
  625. begin
  626. with oper[i]^ do
  627. begin
  628. if i=0 then
  629. s:=s+' '
  630. else
  631. s:=s+',';
  632. { type }
  633. addsize:=false;
  634. if (ot and OT_XMMREG)=OT_XMMREG then
  635. s:=s+'xmmreg'
  636. else
  637. if (ot and OT_MMXREG)=OT_MMXREG then
  638. s:=s+'mmxreg'
  639. else
  640. if (ot and OT_FPUREG)=OT_FPUREG then
  641. s:=s+'fpureg'
  642. else
  643. if (ot and OT_REGISTER)=OT_REGISTER then
  644. begin
  645. s:=s+'reg';
  646. addsize:=true;
  647. end
  648. else
  649. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  650. begin
  651. s:=s+'imm';
  652. addsize:=true;
  653. end
  654. else
  655. if (ot and OT_MEMORY)=OT_MEMORY then
  656. begin
  657. s:=s+'mem';
  658. addsize:=true;
  659. end
  660. else
  661. s:=s+'???';
  662. { size }
  663. if addsize then
  664. begin
  665. if (ot and OT_BITS8)<>0 then
  666. s:=s+'8'
  667. else
  668. if (ot and OT_BITS16)<>0 then
  669. s:=s+'16'
  670. else
  671. if (ot and OT_BITS32)<>0 then
  672. s:=s+'32'
  673. else
  674. if (ot and OT_BITS64)<>0 then
  675. s:=s+'64'
  676. else
  677. s:=s+'??';
  678. { signed }
  679. if (ot and OT_SIGNED)<>0 then
  680. s:=s+'s';
  681. end;
  682. end;
  683. end;
  684. GetString:=s+']';
  685. end;
  686. procedure taicpu.Swapoperands;
  687. var
  688. p : POper;
  689. begin
  690. { Fix the operands which are in AT&T style and we need them in Intel style }
  691. case ops of
  692. 2 : begin
  693. { 0,1 -> 1,0 }
  694. p:=oper[0];
  695. oper[0]:=oper[1];
  696. oper[1]:=p;
  697. end;
  698. 3 : begin
  699. { 0,1,2 -> 2,1,0 }
  700. p:=oper[0];
  701. oper[0]:=oper[2];
  702. oper[2]:=p;
  703. end;
  704. end;
  705. end;
  706. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  707. begin
  708. if FOperandOrder<>order then
  709. begin
  710. Swapoperands;
  711. FOperandOrder:=order;
  712. end;
  713. end;
  714. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  715. begin
  716. o.typ:=toptype(ppufile.getbyte);
  717. o.ot:=ppufile.getlongint;
  718. case o.typ of
  719. top_reg :
  720. ppufile.getdata(o.reg,sizeof(Tregister));
  721. top_ref :
  722. begin
  723. new(o.ref);
  724. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  725. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  726. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  727. o.ref^.scalefactor:=ppufile.getbyte;
  728. o.ref^.offset:=ppufile.getaint;
  729. o.ref^.symbol:=ppufile.getasmsymbol;
  730. o.ref^.relsymbol:=ppufile.getasmsymbol;
  731. end;
  732. top_const :
  733. o.val:=ppufile.getaint;
  734. top_local :
  735. begin
  736. new(o.localoper);
  737. with o.localoper^ do
  738. begin
  739. ppufile.getderef(localsymderef);
  740. localsymofs:=ppufile.getaint;
  741. localindexreg:=tregister(ppufile.getlongint);
  742. localscale:=ppufile.getbyte;
  743. localgetoffset:=(ppufile.getbyte<>0);
  744. end;
  745. end;
  746. end;
  747. end;
  748. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  749. begin
  750. ppufile.putbyte(byte(o.typ));
  751. ppufile.putlongint(o.ot);
  752. case o.typ of
  753. top_reg :
  754. ppufile.putdata(o.reg,sizeof(Tregister));
  755. top_ref :
  756. begin
  757. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  758. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  759. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  760. ppufile.putbyte(o.ref^.scalefactor);
  761. ppufile.putaint(o.ref^.offset);
  762. ppufile.putasmsymbol(o.ref^.symbol);
  763. ppufile.putasmsymbol(o.ref^.relsymbol);
  764. end;
  765. top_const :
  766. ppufile.putaint(o.val);
  767. top_local :
  768. begin
  769. with o.localoper^ do
  770. begin
  771. ppufile.putderef(localsymderef);
  772. ppufile.putaint(localsymofs);
  773. ppufile.putlongint(longint(localindexreg));
  774. ppufile.putbyte(localscale);
  775. ppufile.putbyte(byte(localgetoffset));
  776. end;
  777. end;
  778. end;
  779. end;
  780. procedure taicpu.ppubuildderefimploper(var o:toper);
  781. begin
  782. case o.typ of
  783. top_local :
  784. o.localoper^.localsymderef.build(tlocalvarsym(o.localoper^.localsym));
  785. end;
  786. end;
  787. procedure taicpu.ppuderefoper(var o:toper);
  788. begin
  789. case o.typ of
  790. top_ref :
  791. begin
  792. end;
  793. top_local :
  794. o.localoper^.localsym:=tlocalvarsym(o.localoper^.localsymderef.resolve);
  795. end;
  796. end;
  797. procedure taicpu.CheckNonCommutativeOpcodes;
  798. begin
  799. { we need ATT order }
  800. SetOperandOrder(op_att);
  801. if (
  802. (ops=2) and
  803. (oper[0]^.typ=top_reg) and
  804. (oper[1]^.typ=top_reg) and
  805. { if the first is ST and the second is also a register
  806. it is necessarily ST1 .. ST7 }
  807. ((oper[0]^.reg=NR_ST) or
  808. (oper[0]^.reg=NR_ST0))
  809. ) or
  810. { ((ops=1) and
  811. (oper[0]^.typ=top_reg) and
  812. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  813. (ops=0) then
  814. begin
  815. if opcode=A_FSUBR then
  816. opcode:=A_FSUB
  817. else if opcode=A_FSUB then
  818. opcode:=A_FSUBR
  819. else if opcode=A_FDIVR then
  820. opcode:=A_FDIV
  821. else if opcode=A_FDIV then
  822. opcode:=A_FDIVR
  823. else if opcode=A_FSUBRP then
  824. opcode:=A_FSUBP
  825. else if opcode=A_FSUBP then
  826. opcode:=A_FSUBRP
  827. else if opcode=A_FDIVRP then
  828. opcode:=A_FDIVP
  829. else if opcode=A_FDIVP then
  830. opcode:=A_FDIVRP;
  831. end;
  832. if (
  833. (ops=1) and
  834. (oper[0]^.typ=top_reg) and
  835. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  836. (oper[0]^.reg<>NR_ST)
  837. ) then
  838. begin
  839. if opcode=A_FSUBRP then
  840. opcode:=A_FSUBP
  841. else if opcode=A_FSUBP then
  842. opcode:=A_FSUBRP
  843. else if opcode=A_FDIVRP then
  844. opcode:=A_FDIVP
  845. else if opcode=A_FDIVP then
  846. opcode:=A_FDIVRP;
  847. end;
  848. end;
  849. {*****************************************************************************
  850. Assembler
  851. *****************************************************************************}
  852. type
  853. ea = packed record
  854. sib_present : boolean;
  855. bytes : byte;
  856. size : byte;
  857. modrm : byte;
  858. sib : byte;
  859. {$ifdef x86_64}
  860. rex_present : boolean;
  861. rex : byte;
  862. {$endif x86_64}
  863. end;
  864. procedure taicpu.create_ot(objdata:TObjData);
  865. {
  866. this function will also fix some other fields which only needs to be once
  867. }
  868. var
  869. i,l,relsize : longint;
  870. currsym : TObjSymbol;
  871. begin
  872. if ops=0 then
  873. exit;
  874. { update oper[].ot field }
  875. for i:=0 to ops-1 do
  876. with oper[i]^ do
  877. begin
  878. case typ of
  879. top_reg :
  880. begin
  881. ot:=reg_ot_table[findreg_by_number(reg)];
  882. end;
  883. top_ref :
  884. begin
  885. if ref^.refaddr=addr_no then
  886. begin
  887. { create ot field }
  888. if (ot and OT_SIZE_MASK)=0 then
  889. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  890. else
  891. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  892. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  893. ot:=ot or OT_MEM_OFFS;
  894. { fix scalefactor }
  895. if (ref^.index=NR_NO) then
  896. ref^.scalefactor:=0
  897. else
  898. if (ref^.scalefactor=0) then
  899. ref^.scalefactor:=1;
  900. end
  901. else
  902. begin
  903. if assigned(objdata) then
  904. begin
  905. currsym:=objdata.symbolref(ref^.symbol);
  906. l:=ref^.offset;
  907. if assigned(currsym) then
  908. inc(l,currsym.address);
  909. { when it is a forward jump we need to compensate the
  910. offset of the instruction since the previous time,
  911. because the symbol address is then still using the
  912. 'old-style' addressing.
  913. For backwards jumps this is not required because the
  914. address of the symbol is already adjusted to the
  915. new offset }
  916. if (l>InsOffset) and (LastInsOffset<>-1) then
  917. inc(l,InsOffset-LastInsOffset);
  918. { instruction size will then always become 2 (PFV) }
  919. relsize:=(InsOffset+2)-l;
  920. if (relsize>=-128) and (relsize<=127) and
  921. (
  922. not assigned(currsym) or
  923. (currsym.objsection=objdata.currobjsec)
  924. ) then
  925. ot:=OT_IMM8 or OT_SHORT
  926. else
  927. ot:=OT_IMM32 or OT_NEAR;
  928. end
  929. else
  930. ot:=OT_IMM32 or OT_NEAR;
  931. end;
  932. end;
  933. top_local :
  934. begin
  935. if (ot and OT_SIZE_MASK)=0 then
  936. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  937. else
  938. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  939. end;
  940. top_const :
  941. begin
  942. if opsize=S_NO then
  943. message(asmr_e_invalid_opcode_and_operand);
  944. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  945. ot:=OT_IMM8 or OT_SIGNED
  946. else
  947. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  948. end;
  949. top_none :
  950. begin
  951. { generated when there was an error in the
  952. assembler reader. It never happends when generating
  953. assembler }
  954. end;
  955. else
  956. internalerror(200402261);
  957. end;
  958. end;
  959. end;
  960. function taicpu.InsEnd:longint;
  961. begin
  962. InsEnd:=InsOffset+InsSize;
  963. end;
  964. function taicpu.Matches(p:PInsEntry):boolean;
  965. { * IF_SM stands for Size Match: any operand whose size is not
  966. * explicitly specified by the template is `really' intended to be
  967. * the same size as the first size-specified operand.
  968. * Non-specification is tolerated in the input instruction, but
  969. * _wrong_ specification is not.
  970. *
  971. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  972. * three-operand instructions such as SHLD: it implies that the
  973. * first two operands must match in size, but that the third is
  974. * required to be _unspecified_.
  975. *
  976. * IF_SB invokes Size Byte: operands with unspecified size in the
  977. * template are really bytes, and so no non-byte specification in
  978. * the input instruction will be tolerated. IF_SW similarly invokes
  979. * Size Word, and IF_SD invokes Size Doubleword.
  980. *
  981. * (The default state if neither IF_SM nor IF_SM2 is specified is
  982. * that any operand with unspecified size in the template is
  983. * required to have unspecified size in the instruction too...)
  984. }
  985. var
  986. insot,
  987. insflags,
  988. currot,
  989. i,j,asize,oprs : longint;
  990. siz : array[0..2] of longint;
  991. begin
  992. result:=false;
  993. { Check the opcode and operands }
  994. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  995. exit;
  996. for i:=0 to p^.ops-1 do
  997. begin
  998. insot:=p^.optypes[i];
  999. currot:=oper[i]^.ot;
  1000. { Check the operand flags }
  1001. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1002. exit;
  1003. { Check if the passed operand size matches with one of
  1004. the supported operand sizes }
  1005. if ((insot and OT_SIZE_MASK)<>0) and
  1006. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1007. exit;
  1008. end;
  1009. { Check operand sizes }
  1010. insflags:=p^.flags;
  1011. if insflags and IF_SMASK<>0 then
  1012. begin
  1013. { as default an untyped size can get all the sizes, this is different
  1014. from nasm, but else we need to do a lot checking which opcodes want
  1015. size or not with the automatic size generation }
  1016. asize:=-1;
  1017. if (insflags and IF_SB)<>0 then
  1018. asize:=OT_BITS8
  1019. else if (insflags and IF_SW)<>0 then
  1020. asize:=OT_BITS16
  1021. else if (insflags and IF_SD)<>0 then
  1022. asize:=OT_BITS32;
  1023. if (insflags and IF_ARMASK)<>0 then
  1024. begin
  1025. siz[0]:=0;
  1026. siz[1]:=0;
  1027. siz[2]:=0;
  1028. if (insflags and IF_AR0)<>0 then
  1029. siz[0]:=asize
  1030. else if (insflags and IF_AR1)<>0 then
  1031. siz[1]:=asize
  1032. else if (insflags and IF_AR2)<>0 then
  1033. siz[2]:=asize;
  1034. end
  1035. else
  1036. begin
  1037. siz[0]:=asize;
  1038. siz[1]:=asize;
  1039. siz[2]:=asize;
  1040. end;
  1041. if (insflags and (IF_SM or IF_SM2))<>0 then
  1042. begin
  1043. if (insflags and IF_SM2)<>0 then
  1044. oprs:=2
  1045. else
  1046. oprs:=p^.ops;
  1047. for i:=0 to oprs-1 do
  1048. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1049. begin
  1050. for j:=0 to oprs-1 do
  1051. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1052. break;
  1053. end;
  1054. end
  1055. else
  1056. oprs:=2;
  1057. { Check operand sizes }
  1058. for i:=0 to p^.ops-1 do
  1059. begin
  1060. insot:=p^.optypes[i];
  1061. currot:=oper[i]^.ot;
  1062. if ((insot and OT_SIZE_MASK)=0) and
  1063. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1064. { Immediates can always include smaller size }
  1065. ((currot and OT_IMMEDIATE)=0) and
  1066. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1067. exit;
  1068. end;
  1069. end;
  1070. result:=true;
  1071. end;
  1072. procedure taicpu.ResetPass1;
  1073. begin
  1074. { we need to reset everything here, because the choosen insentry
  1075. can be invalid for a new situation where the previously optimized
  1076. insentry is not correct }
  1077. InsEntry:=nil;
  1078. InsSize:=0;
  1079. LastInsOffset:=-1;
  1080. end;
  1081. procedure taicpu.ResetPass2;
  1082. begin
  1083. { we are here in a second pass, check if the instruction can be optimized }
  1084. if assigned(InsEntry) and
  1085. ((InsEntry^.flags and IF_PASS2)<>0) then
  1086. begin
  1087. InsEntry:=nil;
  1088. InsSize:=0;
  1089. end;
  1090. LastInsOffset:=-1;
  1091. end;
  1092. function taicpu.CheckIfValid:boolean;
  1093. begin
  1094. result:=FindInsEntry(nil);
  1095. end;
  1096. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1097. var
  1098. i : longint;
  1099. begin
  1100. result:=false;
  1101. { Things which may only be done once, not when a second pass is done to
  1102. optimize }
  1103. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1104. begin
  1105. { We need intel style operands }
  1106. SetOperandOrder(op_intel);
  1107. { create the .ot fields }
  1108. create_ot(objdata);
  1109. { set the file postion }
  1110. aktfilepos:=fileinfo;
  1111. end
  1112. else
  1113. begin
  1114. { we've already an insentry so it's valid }
  1115. result:=true;
  1116. exit;
  1117. end;
  1118. { Lookup opcode in the table }
  1119. InsSize:=-1;
  1120. i:=instabcache^[opcode];
  1121. if i=-1 then
  1122. begin
  1123. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1124. exit;
  1125. end;
  1126. insentry:=@instab[i];
  1127. while (insentry^.opcode=opcode) do
  1128. begin
  1129. if matches(insentry) then
  1130. begin
  1131. result:=true;
  1132. exit;
  1133. end;
  1134. inc(insentry);
  1135. end;
  1136. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1137. { No instruction found, set insentry to nil and inssize to -1 }
  1138. insentry:=nil;
  1139. inssize:=-1;
  1140. end;
  1141. function taicpu.Pass1(objdata:TObjData):longint;
  1142. begin
  1143. Pass1:=0;
  1144. { Save the old offset and set the new offset }
  1145. InsOffset:=ObjData.CurrObjSec.Size;
  1146. { Error? }
  1147. if (Insentry=nil) and (InsSize=-1) then
  1148. exit;
  1149. { set the file postion }
  1150. aktfilepos:=fileinfo;
  1151. { Get InsEntry }
  1152. if FindInsEntry(ObjData) then
  1153. begin
  1154. { Calculate instruction size }
  1155. InsSize:=calcsize(insentry);
  1156. if segprefix<>NR_NO then
  1157. inc(InsSize);
  1158. { Fix opsize if size if forced }
  1159. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1160. begin
  1161. if (insentry^.flags and IF_ARMASK)=0 then
  1162. begin
  1163. if (insentry^.flags and IF_SB)<>0 then
  1164. begin
  1165. if opsize=S_NO then
  1166. opsize:=S_B;
  1167. end
  1168. else if (insentry^.flags and IF_SW)<>0 then
  1169. begin
  1170. if opsize=S_NO then
  1171. opsize:=S_W;
  1172. end
  1173. else if (insentry^.flags and IF_SD)<>0 then
  1174. begin
  1175. if opsize=S_NO then
  1176. opsize:=S_L;
  1177. end;
  1178. end;
  1179. end;
  1180. LastInsOffset:=InsOffset;
  1181. Pass1:=InsSize;
  1182. exit;
  1183. end;
  1184. LastInsOffset:=-1;
  1185. end;
  1186. procedure taicpu.Pass2(objdata:TObjData);
  1187. var
  1188. c : longint;
  1189. begin
  1190. { error in pass1 ? }
  1191. if insentry=nil then
  1192. exit;
  1193. aktfilepos:=fileinfo;
  1194. { Segment override }
  1195. if (segprefix<>NR_NO) then
  1196. begin
  1197. case segprefix of
  1198. NR_CS : c:=$2e;
  1199. NR_DS : c:=$3e;
  1200. NR_ES : c:=$26;
  1201. NR_FS : c:=$64;
  1202. NR_GS : c:=$65;
  1203. NR_SS : c:=$36;
  1204. end;
  1205. objdata.writebytes(c,1);
  1206. { fix the offset for GenNode }
  1207. inc(InsOffset);
  1208. end;
  1209. { Generate the instruction }
  1210. GenCode(objdata);
  1211. end;
  1212. function taicpu.needaddrprefix(opidx:byte):boolean;
  1213. begin
  1214. result:=(oper[opidx]^.typ=top_ref) and
  1215. (oper[opidx]^.ref^.refaddr=addr_no) and
  1216. (
  1217. (
  1218. (oper[opidx]^.ref^.index<>NR_NO) and
  1219. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1220. ) or
  1221. (
  1222. (oper[opidx]^.ref^.base<>NR_NO) and
  1223. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1224. )
  1225. );
  1226. end;
  1227. function regval(r:Tregister):byte;
  1228. const
  1229. {$ifdef x86_64}
  1230. opcode_table:array[tregisterindex] of tregisterindex = (
  1231. {$i r8664op.inc}
  1232. );
  1233. {$else x86_64}
  1234. opcode_table:array[tregisterindex] of tregisterindex = (
  1235. {$i r386op.inc}
  1236. );
  1237. {$endif x86_64}
  1238. var
  1239. regidx : tregisterindex;
  1240. begin
  1241. regidx:=findreg_by_number(r);
  1242. if regidx<>0 then
  1243. result:=opcode_table[regidx]
  1244. else
  1245. begin
  1246. Message1(asmw_e_invalid_register,generic_regname(r));
  1247. result:=0;
  1248. end;
  1249. end;
  1250. {$ifdef x86_64}
  1251. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1252. var
  1253. sym : tasmsymbol;
  1254. md,s,rv : byte;
  1255. base,index,scalefactor,
  1256. o : longint;
  1257. ir,br : Tregister;
  1258. isub,bsub : tsubregister;
  1259. begin
  1260. process_ea:=false;
  1261. fillchar(output,sizeof(output),0);
  1262. {Register ?}
  1263. if (input.typ=top_reg) then
  1264. begin
  1265. rv:=regval(input.reg);
  1266. output.modrm:=$c0 or (rfield shl 3) or rv;
  1267. output.size:=1;
  1268. if ((getregtype(input.reg)=R_INTREGISTER) and
  1269. (getsupreg(input.reg)>=RS_R8)) or
  1270. ((getregtype(input.reg)=R_MMREGISTER) and
  1271. (getsupreg(input.reg)>=RS_XMM8)) then
  1272. begin
  1273. output.rex_present:=true;
  1274. output.rex:=output.rex or $41;
  1275. inc(output.size,1);
  1276. end
  1277. else if (getregtype(input.reg)=R_INTREGISTER) and
  1278. (getsubreg(input.reg)=R_SUBL) and
  1279. (getsupreg(input.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1280. begin
  1281. output.rex_present:=true;
  1282. output.rex:=output.rex or $40;
  1283. inc(output.size,1);
  1284. end;
  1285. process_ea:=true;
  1286. exit;
  1287. end;
  1288. {No register, so memory reference.}
  1289. if (input.typ<>top_ref) then
  1290. internalerror(200409262);
  1291. ir:=input.ref^.index;
  1292. br:=input.ref^.base;
  1293. isub:=getsubreg(ir);
  1294. bsub:=getsubreg(br);
  1295. s:=input.ref^.scalefactor;
  1296. o:=input.ref^.offset;
  1297. sym:=input.ref^.symbol;
  1298. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1299. ((br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) then
  1300. internalerror(200301081);
  1301. { it's direct address }
  1302. if (br=NR_NO) and (ir=NR_NO) then
  1303. begin
  1304. output.sib_present:=true;
  1305. output.bytes:=4;
  1306. output.modrm:=4 or (rfield shl 3);
  1307. output.sib:=$25;
  1308. end
  1309. else if (br=NR_RIP) and (ir=NR_NO) then
  1310. begin
  1311. { rip based }
  1312. output.sib_present:=false;
  1313. output.bytes:=4;
  1314. output.modrm:=5 or (rfield shl 3);
  1315. end
  1316. else
  1317. { it's an indirection }
  1318. begin
  1319. { 16 bit or 32 bit address? }
  1320. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1321. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1322. message(asmw_e_16bit_32bit_not_supported);
  1323. { wrong, for various reasons }
  1324. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1325. exit;
  1326. if ((getregtype(br)=R_INTREGISTER) and
  1327. (getsupreg(br)>=RS_R8)) or
  1328. ((getregtype(br)=R_MMREGISTER) and
  1329. (getsupreg(br)>=RS_XMM8)) then
  1330. begin
  1331. output.rex_present:=true;
  1332. output.rex:=output.rex or $41;
  1333. end;
  1334. if ((getregtype(ir)=R_INTREGISTER) and
  1335. (getsupreg(ir)>=RS_R8)) or
  1336. ((getregtype(ir)=R_MMREGISTER) and
  1337. (getsupreg(ir)>=RS_XMM8)) then
  1338. begin
  1339. output.rex_present:=true;
  1340. output.rex:=output.rex or $42;
  1341. end;
  1342. process_ea:=true;
  1343. { base }
  1344. case br of
  1345. NR_R8,
  1346. NR_RAX : base:=0;
  1347. NR_R9,
  1348. NR_RCX : base:=1;
  1349. NR_R10,
  1350. NR_RDX : base:=2;
  1351. NR_R11,
  1352. NR_RBX : base:=3;
  1353. NR_R12,
  1354. NR_RSP : base:=4;
  1355. NR_R13,
  1356. NR_NO,
  1357. NR_RBP : base:=5;
  1358. NR_R14,
  1359. NR_RSI : base:=6;
  1360. NR_R15,
  1361. NR_RDI : base:=7;
  1362. else
  1363. exit;
  1364. end;
  1365. { index }
  1366. case ir of
  1367. NR_R8,
  1368. NR_RAX : index:=0;
  1369. NR_R9,
  1370. NR_RCX : index:=1;
  1371. NR_R10,
  1372. NR_RDX : index:=2;
  1373. NR_R11,
  1374. NR_RBX : index:=3;
  1375. NR_R12,
  1376. NR_NO : index:=4;
  1377. NR_R13,
  1378. NR_RBP : index:=5;
  1379. NR_R14,
  1380. NR_RSI : index:=6;
  1381. NR_R15,
  1382. NR_RDI : index:=7;
  1383. else
  1384. exit;
  1385. end;
  1386. case s of
  1387. 0,
  1388. 1 : scalefactor:=0;
  1389. 2 : scalefactor:=1;
  1390. 4 : scalefactor:=2;
  1391. 8 : scalefactor:=3;
  1392. else
  1393. exit;
  1394. end;
  1395. if (br=NR_NO) or
  1396. ((br<>NR_RBP) and (o=0) and (sym=nil)) then
  1397. md:=0
  1398. else
  1399. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1400. md:=1
  1401. else
  1402. md:=2;
  1403. if (br=NR_NO) or (md=2) then
  1404. output.bytes:=4
  1405. else
  1406. output.bytes:=md;
  1407. { SIB needed ? }
  1408. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1409. begin
  1410. output.sib_present:=false;
  1411. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1412. end
  1413. else
  1414. begin
  1415. output.sib_present:=true;
  1416. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1417. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1418. end;
  1419. end;
  1420. output.size:=1+ord(output.sib_present)+ord(output.rex_present)+output.bytes;
  1421. process_ea:=true;
  1422. end;
  1423. {$else x86_64}
  1424. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1425. var
  1426. sym : tasmsymbol;
  1427. md,s,rv : byte;
  1428. base,index,scalefactor,
  1429. o : longint;
  1430. ir,br : Tregister;
  1431. isub,bsub : tsubregister;
  1432. begin
  1433. process_ea:=false;
  1434. fillchar(output,sizeof(output),0);
  1435. {Register ?}
  1436. if (input.typ=top_reg) then
  1437. begin
  1438. rv:=regval(input.reg);
  1439. output.modrm:=$c0 or (rfield shl 3) or rv;
  1440. output.size:=1;
  1441. process_ea:=true;
  1442. exit;
  1443. end;
  1444. {No register, so memory reference.}
  1445. if (input.typ<>top_ref) then
  1446. internalerror(200409262);
  1447. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1448. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1449. internalerror(200301081);
  1450. ir:=input.ref^.index;
  1451. br:=input.ref^.base;
  1452. isub:=getsubreg(ir);
  1453. bsub:=getsubreg(br);
  1454. s:=input.ref^.scalefactor;
  1455. o:=input.ref^.offset;
  1456. sym:=input.ref^.symbol;
  1457. { it's direct address }
  1458. if (br=NR_NO) and (ir=NR_NO) then
  1459. begin
  1460. { it's a pure offset }
  1461. output.sib_present:=false;
  1462. output.bytes:=4;
  1463. output.modrm:=5 or (rfield shl 3);
  1464. end
  1465. else
  1466. { it's an indirection }
  1467. begin
  1468. { 16 bit address? }
  1469. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1470. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1471. message(asmw_e_16bit_not_supported);
  1472. {$ifdef OPTEA}
  1473. { make single reg base }
  1474. if (br=NR_NO) and (s=1) then
  1475. begin
  1476. br:=ir;
  1477. ir:=NR_NO;
  1478. end;
  1479. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1480. if (br=NR_NO) and
  1481. (((s=2) and (ir<>NR_ESP)) or
  1482. (s=3) or (s=5) or (s=9)) then
  1483. begin
  1484. br:=ir;
  1485. dec(s);
  1486. end;
  1487. { swap ESP into base if scalefactor is 1 }
  1488. if (s=1) and (ir=NR_ESP) then
  1489. begin
  1490. ir:=br;
  1491. br:=NR_ESP;
  1492. end;
  1493. {$endif OPTEA}
  1494. { wrong, for various reasons }
  1495. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1496. exit;
  1497. { base }
  1498. case br of
  1499. NR_EAX : base:=0;
  1500. NR_ECX : base:=1;
  1501. NR_EDX : base:=2;
  1502. NR_EBX : base:=3;
  1503. NR_ESP : base:=4;
  1504. NR_NO,
  1505. NR_EBP : base:=5;
  1506. NR_ESI : base:=6;
  1507. NR_EDI : base:=7;
  1508. else
  1509. exit;
  1510. end;
  1511. { index }
  1512. case ir of
  1513. NR_EAX : index:=0;
  1514. NR_ECX : index:=1;
  1515. NR_EDX : index:=2;
  1516. NR_EBX : index:=3;
  1517. NR_NO : index:=4;
  1518. NR_EBP : index:=5;
  1519. NR_ESI : index:=6;
  1520. NR_EDI : index:=7;
  1521. else
  1522. exit;
  1523. end;
  1524. case s of
  1525. 0,
  1526. 1 : scalefactor:=0;
  1527. 2 : scalefactor:=1;
  1528. 4 : scalefactor:=2;
  1529. 8 : scalefactor:=3;
  1530. else
  1531. exit;
  1532. end;
  1533. if (br=NR_NO) or
  1534. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1535. md:=0
  1536. else
  1537. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1538. md:=1
  1539. else
  1540. md:=2;
  1541. if (br=NR_NO) or (md=2) then
  1542. output.bytes:=4
  1543. else
  1544. output.bytes:=md;
  1545. { SIB needed ? }
  1546. if (ir=NR_NO) and (br<>NR_ESP) then
  1547. begin
  1548. output.sib_present:=false;
  1549. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1550. end
  1551. else
  1552. begin
  1553. output.sib_present:=true;
  1554. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1555. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1556. end;
  1557. end;
  1558. if output.sib_present then
  1559. output.size:=2+output.bytes
  1560. else
  1561. output.size:=1+output.bytes;
  1562. process_ea:=true;
  1563. end;
  1564. {$endif x86_64}
  1565. function taicpu.calcsize(p:PInsEntry):shortint;
  1566. var
  1567. codes : pchar;
  1568. c : byte;
  1569. len : shortint;
  1570. ea_data : ea;
  1571. begin
  1572. len:=0;
  1573. codes:=@p^.code;
  1574. {$ifdef x86_64}
  1575. rex:=0;
  1576. {$endif x86_64}
  1577. repeat
  1578. c:=ord(codes^);
  1579. inc(codes);
  1580. case c of
  1581. 0 :
  1582. break;
  1583. 1,2,3 :
  1584. begin
  1585. inc(codes,c);
  1586. inc(len,c);
  1587. end;
  1588. 8,9,10 :
  1589. begin
  1590. {$ifdef x86_64}
  1591. if ((getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1592. (getsupreg(oper[c-8]^.reg)>=RS_R8)) or
  1593. ((getregtype(oper[c-8]^.reg)=R_MMREGISTER) and
  1594. (getsupreg(oper[c-8]^.reg)>=RS_XMM8)) then
  1595. begin
  1596. if rex=0 then
  1597. inc(len);
  1598. rex:=rex or $41;
  1599. end
  1600. else if (getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1601. (getsubreg(oper[c-8]^.reg)=R_SUBL) and
  1602. (getsupreg(oper[c-8]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1603. begin
  1604. if rex=0 then
  1605. inc(len);
  1606. rex:=rex or $40;
  1607. end;
  1608. {$endif x86_64}
  1609. inc(codes);
  1610. inc(len);
  1611. end;
  1612. 11 :
  1613. begin
  1614. inc(codes);
  1615. inc(len);
  1616. end;
  1617. 4,5,6,7 :
  1618. begin
  1619. if opsize=S_W then
  1620. inc(len,2)
  1621. else
  1622. inc(len);
  1623. end;
  1624. 15,
  1625. 12,13,14,
  1626. 16,17,18,
  1627. 20,21,22,
  1628. 40,41,42 :
  1629. inc(len);
  1630. 24,25,26,
  1631. 31,
  1632. 48,49,50 :
  1633. inc(len,2);
  1634. 28,29,30:
  1635. begin
  1636. if opsize=S_Q then
  1637. inc(len,8)
  1638. else
  1639. inc(len,4);
  1640. end;
  1641. 32,33,34,
  1642. 52,53,54,
  1643. 56,57,58 :
  1644. inc(len,4);
  1645. 192,193,194 :
  1646. if NeedAddrPrefix(c-192) then
  1647. inc(len);
  1648. 208,209,210 :
  1649. begin
  1650. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1651. OT_BITS16:
  1652. inc(len);
  1653. {$ifdef x86_64}
  1654. OT_BITS64:
  1655. begin
  1656. if rex=0 then
  1657. inc(len);
  1658. rex:=rex or $48;
  1659. end;
  1660. {$endif x86_64}
  1661. end;
  1662. end;
  1663. 212 :
  1664. inc(len);
  1665. 214 :
  1666. begin
  1667. {$ifdef x86_64}
  1668. if rex=0 then
  1669. inc(len);
  1670. rex:=rex or $48;
  1671. {$endif x86_64}
  1672. end;
  1673. 200,
  1674. 201,
  1675. 202,
  1676. 213,
  1677. 215,
  1678. 217,218: ;
  1679. 219,220 :
  1680. inc(len);
  1681. 221:
  1682. {$ifdef x86_64}
  1683. { remove rex competely? }
  1684. if rex=$48 then
  1685. begin
  1686. rex:=0;
  1687. dec(len);
  1688. end
  1689. else
  1690. rex:=rex and $f7
  1691. {$endif x86_64}
  1692. ;
  1693. 64..191 :
  1694. begin
  1695. {$ifdef x86_64}
  1696. if (c<127) then
  1697. begin
  1698. if (oper[c and 7]^.typ=top_reg) then
  1699. begin
  1700. if ((getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1701. (getsupreg(oper[c and 7]^.reg)>=RS_R8)) or
  1702. ((getregtype(oper[c and 7]^.reg)=R_MMREGISTER) and
  1703. (getsupreg(oper[c and 7]^.reg)>=RS_XMM8)) then
  1704. begin
  1705. if rex=0 then
  1706. inc(len);
  1707. rex:=rex or $44;
  1708. end
  1709. else if (getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1710. (getsubreg(oper[c and 7]^.reg)=R_SUBL) and
  1711. (getsupreg(oper[c and 7]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1712. begin
  1713. if rex=0 then
  1714. inc(len);
  1715. rex:=rex or $40;
  1716. end;
  1717. end;
  1718. end;
  1719. {$endif x86_64}
  1720. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1721. Message(asmw_e_invalid_effective_address)
  1722. else
  1723. inc(len,ea_data.size);
  1724. {$ifdef x86_64}
  1725. { did we already create include a rex into the length calculation? }
  1726. if (rex<>0) and (ea_data.rex<>0) then
  1727. dec(len);
  1728. rex:=rex or ea_data.rex;
  1729. {$endif x86_64}
  1730. end;
  1731. else
  1732. InternalError(200603141);
  1733. end;
  1734. until false;
  1735. calcsize:=len;
  1736. end;
  1737. procedure taicpu.GenCode(objdata:TObjData);
  1738. {
  1739. * the actual codes (C syntax, i.e. octal):
  1740. * \0 - terminates the code. (Unless it's a literal of course.)
  1741. * \1, \2, \3 - that many literal bytes follow in the code stream
  1742. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1743. * (POP is never used for CS) depending on operand 0
  1744. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1745. * on operand 0
  1746. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1747. * to the register value of operand 0, 1 or 2
  1748. * \13 - a literal byte follows in the code stream, to be added
  1749. * to the condition code value of the instruction.
  1750. * \17 - encodes the literal byte 0. (Some compilers don't take
  1751. * kindly to a zero byte in the _middle_ of a compile time
  1752. * string constant, so I had to put this hack in.)
  1753. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1754. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1755. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1756. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1757. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1758. * assembly mode or the address-size override on the operand
  1759. * \37 - a word constant, from the _segment_ part of operand 0
  1760. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1761. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1762. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1763. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1764. * assembly mode or the address-size override on the operand
  1765. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1766. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1767. * field the register value of operand b.
  1768. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1769. * field equal to digit b.
  1770. * \300,\301,\302 - might be an 0x67 or 0x48 byte, depending on the address size of
  1771. * the memory reference in operand x.
  1772. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1773. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1774. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1775. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1776. * size of operand x.
  1777. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1778. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1779. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1780. * \327 - indicates that this instruction is only valid when the
  1781. * operand size is the default (instruction to disassembler,
  1782. * generates no code in the assembler)
  1783. * \335 - removes rex size prefix, i.e. rex.w must be the last opcode
  1784. }
  1785. var
  1786. currval : aint;
  1787. currsym : tobjsymbol;
  1788. procedure getvalsym(opidx:longint);
  1789. begin
  1790. case oper[opidx]^.typ of
  1791. top_ref :
  1792. begin
  1793. currval:=oper[opidx]^.ref^.offset;
  1794. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  1795. end;
  1796. top_const :
  1797. begin
  1798. currval:=aint(oper[opidx]^.val);
  1799. currsym:=nil;
  1800. end;
  1801. else
  1802. Message(asmw_e_immediate_or_reference_expected);
  1803. end;
  1804. end;
  1805. const
  1806. CondVal:array[TAsmCond] of byte=($0,
  1807. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1808. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1809. $0, $A, $A, $B, $8, $4);
  1810. var
  1811. c : byte;
  1812. pb,
  1813. codes : pchar;
  1814. bytes : array[0..3] of byte;
  1815. rfield,
  1816. data,s,opidx : longint;
  1817. ea_data : ea;
  1818. {$ifdef extdebug}
  1819. rexwritten : boolean;
  1820. {$endif extdebug}
  1821. begin
  1822. {$ifdef extdebug}
  1823. rexwritten:=false;
  1824. {$endif extdebug}
  1825. { safety check }
  1826. if objdata.currobjsec.size<>insoffset then
  1827. internalerror(200130121);
  1828. { load data to write }
  1829. codes:=insentry^.code;
  1830. { Force word push/pop for registers }
  1831. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1832. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1833. begin
  1834. bytes[0]:=$66;
  1835. objdata.writebytes(bytes,1);
  1836. end;
  1837. repeat
  1838. c:=ord(codes^);
  1839. inc(codes);
  1840. case c of
  1841. 0 :
  1842. break;
  1843. 1,2,3 :
  1844. begin
  1845. {$ifdef x86_64}
  1846. if rex<>0 then
  1847. begin
  1848. bytes[0]:=rex;
  1849. {$ifdef extdebug}
  1850. rexwritten:=true;
  1851. {$endif extdebug}
  1852. objdata.writebytes(bytes,1);
  1853. end;
  1854. {$endif x86_64}
  1855. objdata.writebytes(codes^,c);
  1856. inc(codes,c);
  1857. end;
  1858. 4,6 :
  1859. begin
  1860. case oper[0]^.reg of
  1861. NR_CS:
  1862. bytes[0]:=$e;
  1863. NR_NO,
  1864. NR_DS:
  1865. bytes[0]:=$1e;
  1866. NR_ES:
  1867. bytes[0]:=$6;
  1868. NR_SS:
  1869. bytes[0]:=$16;
  1870. else
  1871. internalerror(777004);
  1872. end;
  1873. if c=4 then
  1874. inc(bytes[0]);
  1875. objdata.writebytes(bytes,1);
  1876. end;
  1877. 5,7 :
  1878. begin
  1879. case oper[0]^.reg of
  1880. NR_FS:
  1881. bytes[0]:=$a0;
  1882. NR_GS:
  1883. bytes[0]:=$a8;
  1884. else
  1885. internalerror(777005);
  1886. end;
  1887. if c=5 then
  1888. inc(bytes[0]);
  1889. objdata.writebytes(bytes,1);
  1890. end;
  1891. 8,9,10 :
  1892. begin
  1893. { rex should be written at this point }
  1894. (*
  1895. {$ifdef x86_64}
  1896. {$ifdef extdebug}
  1897. if (rex<>0) and not(rexwritten) then
  1898. internalerror(200603192);
  1899. {$endif extdebug}
  1900. {$endif x86_64}
  1901. *)
  1902. {$ifdef x86_64}
  1903. if rex<>0 then
  1904. begin
  1905. bytes[0]:=rex;
  1906. {$ifdef extdebug}
  1907. rexwritten:=true;
  1908. {$endif extdebug}
  1909. objdata.writebytes(bytes,1);
  1910. end;
  1911. {$endif x86_64}
  1912. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1913. inc(codes);
  1914. objdata.writebytes(bytes,1);
  1915. end;
  1916. 11 :
  1917. begin
  1918. bytes[0]:=ord(codes^)+condval[condition];
  1919. inc(codes);
  1920. objdata.writebytes(bytes,1);
  1921. end;
  1922. 15 :
  1923. begin
  1924. bytes[0]:=0;
  1925. objdata.writebytes(bytes,1);
  1926. end;
  1927. 12,13,14 :
  1928. begin
  1929. getvalsym(c-12);
  1930. if (currval<-128) or (currval>127) then
  1931. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1932. if assigned(currsym) then
  1933. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1934. else
  1935. objdata.writebytes(currval,1);
  1936. end;
  1937. 16,17,18 :
  1938. begin
  1939. getvalsym(c-16);
  1940. if (currval<-256) or (currval>255) then
  1941. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1942. if assigned(currsym) then
  1943. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1944. else
  1945. objdata.writebytes(currval,1);
  1946. end;
  1947. 20,21,22 :
  1948. begin
  1949. getvalsym(c-20);
  1950. if (currval<0) or (currval>255) then
  1951. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1952. if assigned(currsym) then
  1953. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1954. else
  1955. objdata.writebytes(currval,1);
  1956. end;
  1957. 24,25,26 :
  1958. begin
  1959. getvalsym(c-24);
  1960. if (currval<-65536) or (currval>65535) then
  1961. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1962. if assigned(currsym) then
  1963. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1964. else
  1965. objdata.writebytes(currval,2);
  1966. end;
  1967. 28,29,30 :
  1968. begin
  1969. getvalsym(c-28);
  1970. if opsize=S_Q then
  1971. begin
  1972. if assigned(currsym) then
  1973. objdata.writereloc(currval,8,currsym,RELOC_ABSOLUTE)
  1974. else
  1975. objdata.writebytes(currval,8);
  1976. end
  1977. else
  1978. begin
  1979. if assigned(currsym) then
  1980. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE32)
  1981. else
  1982. objdata.writebytes(currval,4);
  1983. end
  1984. end;
  1985. 32,33,34 :
  1986. begin
  1987. getvalsym(c-32);
  1988. if assigned(currsym) then
  1989. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE32)
  1990. else
  1991. objdata.writebytes(currval,4);
  1992. end;
  1993. 40,41,42 :
  1994. begin
  1995. getvalsym(c-40);
  1996. data:=currval-insend;
  1997. if assigned(currsym) then
  1998. inc(data,currsym.address);
  1999. if (data>127) or (data<-128) then
  2000. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2001. objdata.writebytes(data,1);
  2002. end;
  2003. 52,53,54 :
  2004. begin
  2005. getvalsym(c-52);
  2006. if assigned(currsym) then
  2007. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  2008. else
  2009. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE32)
  2010. end;
  2011. 56,57,58 :
  2012. begin
  2013. getvalsym(c-56);
  2014. if assigned(currsym) then
  2015. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  2016. else
  2017. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE32)
  2018. end;
  2019. 192,193,194 :
  2020. begin
  2021. if NeedAddrPrefix(c-192) then
  2022. begin
  2023. bytes[0]:=$67;
  2024. objdata.writebytes(bytes,1);
  2025. end;
  2026. end;
  2027. 200 :
  2028. begin
  2029. bytes[0]:=$67;
  2030. objdata.writebytes(bytes,1);
  2031. end;
  2032. 208,209,210 :
  2033. begin
  2034. case oper[c-208]^.ot and OT_SIZE_MASK of
  2035. OT_BITS16 :
  2036. begin
  2037. bytes[0]:=$66;
  2038. objdata.writebytes(bytes,1);
  2039. end;
  2040. {$ifndef x86_64}
  2041. OT_BITS64 :
  2042. Message(asmw_e_64bit_not_supported);
  2043. {$endif x86_64}
  2044. end;
  2045. (*
  2046. {$ifdef x86_64}
  2047. if rex<>0 then
  2048. begin
  2049. bytes[0]:=rex;
  2050. {$ifdef extdebug}
  2051. rexwritten:=true;
  2052. {$endif extdebug}
  2053. objdata.writebytes(bytes,1);
  2054. end;
  2055. {$endif x86_64}
  2056. *)
  2057. end;
  2058. 212 :
  2059. begin
  2060. bytes[0]:=$66;
  2061. objdata.writebytes(bytes,1);
  2062. end;
  2063. 214 :
  2064. begin
  2065. {$ifndef x86_64}
  2066. Message(asmw_e_64bit_not_supported);
  2067. {$endif x86_64}
  2068. end;
  2069. 219 :
  2070. begin
  2071. bytes[0]:=$f3;
  2072. objdata.writebytes(bytes,1);
  2073. end;
  2074. 220 :
  2075. begin
  2076. bytes[0]:=$f2;
  2077. objdata.writebytes(bytes,1);
  2078. end;
  2079. 221:
  2080. ;
  2081. 201,
  2082. 202,
  2083. 213,
  2084. 215,
  2085. 217,218 :
  2086. begin
  2087. { these are dissambler hints or 32 bit prefixes which
  2088. are not needed }
  2089. end;
  2090. 31,
  2091. 48,49,50 :
  2092. begin
  2093. InternalError(777006);
  2094. end
  2095. else
  2096. begin
  2097. { rex should be written at this point }
  2098. {$ifdef x86_64}
  2099. {$ifdef extdebug}
  2100. if (rex<>0) and not(rexwritten) then
  2101. internalerror(200603191);
  2102. {$endif extdebug}
  2103. {$endif x86_64}
  2104. if (c>=64) and (c<=191) then
  2105. begin
  2106. if (c<127) then
  2107. begin
  2108. if (oper[c and 7]^.typ=top_reg) then
  2109. rfield:=regval(oper[c and 7]^.reg)
  2110. else
  2111. rfield:=regval(oper[c and 7]^.ref^.base);
  2112. end
  2113. else
  2114. rfield:=c and 7;
  2115. opidx:=(c shr 3) and 7;
  2116. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2117. Message(asmw_e_invalid_effective_address);
  2118. pb:=@bytes;
  2119. pb^:=chr(ea_data.modrm);
  2120. inc(pb);
  2121. if ea_data.sib_present then
  2122. begin
  2123. pb^:=chr(ea_data.sib);
  2124. inc(pb);
  2125. end;
  2126. s:=pb-pchar(@bytes);
  2127. objdata.writebytes(bytes,s);
  2128. case ea_data.bytes of
  2129. 0 : ;
  2130. 1 :
  2131. begin
  2132. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2133. begin
  2134. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2135. objdata.writereloc(oper[opidx]^.ref^.offset,1,currsym,RELOC_ABSOLUTE);
  2136. end
  2137. else
  2138. begin
  2139. bytes[0]:=oper[opidx]^.ref^.offset;
  2140. objdata.writebytes(bytes,1);
  2141. end;
  2142. inc(s);
  2143. end;
  2144. 2,4 :
  2145. begin
  2146. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  2147. objdata.symbolref(oper[opidx]^.ref^.symbol),RELOC_ABSOLUTE32);
  2148. inc(s,ea_data.bytes);
  2149. end;
  2150. end;
  2151. end
  2152. else
  2153. InternalError(777007);
  2154. end;
  2155. end;
  2156. until false;
  2157. end;
  2158. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2159. begin
  2160. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2161. (regtype = R_INTREGISTER) and
  2162. (ops=2) and
  2163. (oper[0]^.typ=top_reg) and
  2164. (oper[1]^.typ=top_reg) and
  2165. (oper[0]^.reg=oper[1]^.reg)
  2166. ) or
  2167. (((opcode=A_MOVSS) or (opcode=A_MOVSD)) and
  2168. (regtype = R_MMREGISTER) and
  2169. (ops=2) and
  2170. (oper[0]^.typ=top_reg) and
  2171. (oper[1]^.typ=top_reg) and
  2172. (oper[0]^.reg=oper[1]^.reg)
  2173. );
  2174. end;
  2175. procedure build_spilling_operation_type_table;
  2176. var
  2177. opcode : tasmop;
  2178. i : integer;
  2179. begin
  2180. new(operation_type_table);
  2181. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2182. for opcode:=low(tasmop) to high(tasmop) do
  2183. begin
  2184. for i:=1 to MaxInsChanges do
  2185. begin
  2186. case InsProp[opcode].Ch[i] of
  2187. Ch_Rop1 :
  2188. operation_type_table^[opcode,0]:=operand_read;
  2189. Ch_Wop1 :
  2190. operation_type_table^[opcode,0]:=operand_write;
  2191. Ch_RWop1,
  2192. Ch_Mop1 :
  2193. operation_type_table^[opcode,0]:=operand_readwrite;
  2194. Ch_Rop2 :
  2195. operation_type_table^[opcode,1]:=operand_read;
  2196. Ch_Wop2 :
  2197. operation_type_table^[opcode,1]:=operand_write;
  2198. Ch_RWop2,
  2199. Ch_Mop2 :
  2200. operation_type_table^[opcode,1]:=operand_readwrite;
  2201. Ch_Rop3 :
  2202. operation_type_table^[opcode,2]:=operand_read;
  2203. Ch_Wop3 :
  2204. operation_type_table^[opcode,2]:=operand_write;
  2205. Ch_RWop3,
  2206. Ch_Mop3 :
  2207. operation_type_table^[opcode,2]:=operand_readwrite;
  2208. end;
  2209. end;
  2210. end;
  2211. { Special cases that can't be decoded from the InsChanges flags }
  2212. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2213. end;
  2214. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2215. begin
  2216. { the information in the instruction table is made for the string copy
  2217. operation MOVSD so hack here (FK)
  2218. }
  2219. if (opcode=A_MOVSD) and (ops=2) then
  2220. begin
  2221. case opnr of
  2222. 0:
  2223. result:=operand_read;
  2224. 1:
  2225. result:=operand_write;
  2226. else
  2227. internalerror(200506055);
  2228. end
  2229. end
  2230. else
  2231. result:=operation_type_table^[opcode,opnr];
  2232. end;
  2233. function spilling_create_load(const ref:treference;r:tregister): tai;
  2234. begin
  2235. case getregtype(r) of
  2236. R_INTREGISTER :
  2237. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  2238. R_MMREGISTER :
  2239. case getsubreg(r) of
  2240. R_SUBMMD:
  2241. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2242. R_SUBMMS:
  2243. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2244. else
  2245. internalerror(200506043);
  2246. end;
  2247. else
  2248. internalerror(200401041);
  2249. end;
  2250. end;
  2251. function spilling_create_store(r:tregister; const ref:treference): tai;
  2252. begin
  2253. case getregtype(r) of
  2254. R_INTREGISTER :
  2255. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  2256. R_MMREGISTER :
  2257. case getsubreg(r) of
  2258. R_SUBMMD:
  2259. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2260. R_SUBMMS:
  2261. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2262. else
  2263. internalerror(200506042);
  2264. end;
  2265. else
  2266. internalerror(200401041);
  2267. end;
  2268. end;
  2269. {*****************************************************************************
  2270. Instruction table
  2271. *****************************************************************************}
  2272. procedure BuildInsTabCache;
  2273. var
  2274. i : longint;
  2275. begin
  2276. new(instabcache);
  2277. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2278. i:=0;
  2279. while (i<InsTabEntries) do
  2280. begin
  2281. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2282. InsTabCache^[InsTab[i].OPcode]:=i;
  2283. inc(i);
  2284. end;
  2285. end;
  2286. procedure InitAsm;
  2287. begin
  2288. build_spilling_operation_type_table;
  2289. if not assigned(instabcache) then
  2290. BuildInsTabCache;
  2291. end;
  2292. procedure DoneAsm;
  2293. begin
  2294. if assigned(operation_type_table) then
  2295. begin
  2296. dispose(operation_type_table);
  2297. operation_type_table:=nil;
  2298. end;
  2299. if assigned(instabcache) then
  2300. begin
  2301. dispose(instabcache);
  2302. instabcache:=nil;
  2303. end;
  2304. end;
  2305. begin
  2306. cai_align:=tai_align;
  2307. cai_cpu:=taicpu;
  2308. end.