aasmcpu.pas 68 KB

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