aasmcpu.pas 69 KB

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