aasmcpu.pas 84 KB

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