aasmcpu.pas 69 KB

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