aasmcpu.pas 68 KB

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