aasmcpu.pas 69 KB

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