aasmcpu.pas 86 KB

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