aasmcpu.pas 79 KB

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