aasmcpu.pas 57 KB

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