aasmcpu.pas 79 KB

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