aasmcpu.pas 79 KB

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