aasmcpu.pas 74 KB

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