aasmcpu.pas 87 KB

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