cpuasm.pas 53 KB

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