aasmcpu.pas 83 KB

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