aasmcpu.pas 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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 Swapoperands;
  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. { it's a 3DNow! instruction }
  194. IF_3DNOW = $00008000;
  195. { it's a SSE (KNI, MMX2) instruction }
  196. IF_SSE = $00010000;
  197. { SSE2 instructions }
  198. IF_SSE2 = $00020000;
  199. { the mask for processor types }
  200. IF_PMASK = longint($FF000000);
  201. { the mask for disassembly "prefer" }
  202. IF_PFMASK = longint($F001FF00);
  203. IF_8086 = $00000000; { 8086 instruction }
  204. IF_186 = $01000000; { 186+ instruction }
  205. IF_286 = $02000000; { 286+ instruction }
  206. IF_386 = $03000000; { 386+ instruction }
  207. IF_486 = $04000000; { 486+ instruction }
  208. IF_PENT = $05000000; { Pentium instruction }
  209. IF_P6 = $06000000; { P6 instruction }
  210. IF_KATMAI = $07000000; { Katmai instructions }
  211. { Willamette instructions }
  212. IF_WILLAMETTE = $08000000;
  213. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  214. IF_AMD = $20000000; { AMD-specific instruction }
  215. { added flags }
  216. IF_PRE = $40000000; { it's a prefix instruction }
  217. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  218. type
  219. TInsTabCache=array[TasmOp] of longint;
  220. PInsTabCache=^TInsTabCache;
  221. const
  222. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  223. var
  224. InsTabCache : PInsTabCache;
  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_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  405. begin
  406. inherited create(op);
  407. init(_size);
  408. ops:=3;
  409. loadreg(0,_op1);
  410. loadreg(1,_op2);
  411. loadreg(2,_op3);
  412. end;
  413. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  414. begin
  415. inherited create(op);
  416. init(_size);
  417. ops:=3;
  418. loadconst(0,_op1);
  419. loadreg(1,_op2);
  420. loadreg(2,_op3);
  421. end;
  422. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  423. begin
  424. inherited create(op);
  425. init(_size);
  426. ops:=3;
  427. loadreg(0,_op1);
  428. loadreg(1,_op2);
  429. loadref(2,_op3);
  430. end;
  431. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  432. begin
  433. inherited create(op);
  434. init(_size);
  435. ops:=3;
  436. loadconst(0,_op1);
  437. loadref(1,_op2);
  438. loadreg(2,_op3);
  439. end;
  440. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  441. begin
  442. inherited create(op);
  443. init(_size);
  444. ops:=3;
  445. loadconst(0,_op1);
  446. loadreg(1,_op2);
  447. loadref(2,_op3);
  448. end;
  449. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  450. begin
  451. inherited create(op);
  452. init(_size);
  453. condition:=cond;
  454. ops:=1;
  455. loadsymbol(0,_op1,0);
  456. end;
  457. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  458. begin
  459. inherited create(op);
  460. init(_size);
  461. ops:=1;
  462. loadsymbol(0,_op1,0);
  463. end;
  464. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  465. begin
  466. inherited create(op);
  467. init(_size);
  468. ops:=1;
  469. loadsymbol(0,_op1,_op1ofs);
  470. end;
  471. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  472. begin
  473. inherited create(op);
  474. init(_size);
  475. ops:=2;
  476. loadsymbol(0,_op1,_op1ofs);
  477. loadreg(1,_op2);
  478. end;
  479. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  480. begin
  481. inherited create(op);
  482. init(_size);
  483. ops:=2;
  484. loadsymbol(0,_op1,_op1ofs);
  485. loadref(1,_op2);
  486. end;
  487. function taicpu.GetString:string;
  488. var
  489. i : longint;
  490. s : string;
  491. addsize : boolean;
  492. begin
  493. s:='['+std_op2str[opcode];
  494. for i:=1to ops do
  495. begin
  496. if i=1 then
  497. s:=s+' '
  498. else
  499. s:=s+',';
  500. { type }
  501. addsize:=false;
  502. if (oper[i-1].ot and OT_XMMREG)=OT_XMMREG then
  503. s:=s+'xmmreg'
  504. else
  505. if (oper[i-1].ot and OT_MMXREG)=OT_MMXREG then
  506. s:=s+'mmxreg'
  507. else
  508. if (oper[i-1].ot and OT_FPUREG)=OT_FPUREG then
  509. s:=s+'fpureg'
  510. else
  511. if (oper[i-1].ot and OT_REGISTER)=OT_REGISTER then
  512. begin
  513. s:=s+'reg';
  514. addsize:=true;
  515. end
  516. else
  517. if (oper[i-1].ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  518. begin
  519. s:=s+'imm';
  520. addsize:=true;
  521. end
  522. else
  523. if (oper[i-1].ot and OT_MEMORY)=OT_MEMORY then
  524. begin
  525. s:=s+'mem';
  526. addsize:=true;
  527. end
  528. else
  529. s:=s+'???';
  530. { size }
  531. if addsize then
  532. begin
  533. if (oper[i-1].ot and OT_BITS8)<>0 then
  534. s:=s+'8'
  535. else
  536. if (oper[i-1].ot and OT_BITS16)<>0 then
  537. s:=s+'16'
  538. else
  539. if (oper[i-1].ot and OT_BITS32)<>0 then
  540. s:=s+'32'
  541. else
  542. s:=s+'??';
  543. { signed }
  544. if (oper[i-1].ot and OT_SIGNED)<>0 then
  545. s:=s+'s';
  546. end;
  547. end;
  548. GetString:=s+']';
  549. end;
  550. procedure taicpu.Swapoperands;
  551. var
  552. p : TOper;
  553. begin
  554. { Fix the operands which are in AT&T style and we need them in Intel style }
  555. case ops of
  556. 2 : begin
  557. { 0,1 -> 1,0 }
  558. p:=oper[0];
  559. oper[0]:=oper[1];
  560. oper[1]:=p;
  561. end;
  562. 3 : begin
  563. { 0,1,2 -> 2,1,0 }
  564. p:=oper[0];
  565. oper[0]:=oper[2];
  566. oper[2]:=p;
  567. end;
  568. end;
  569. end;
  570. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  571. begin
  572. if FOperandOrder<>order then
  573. begin
  574. Swapoperands;
  575. FOperandOrder:=order;
  576. end;
  577. end;
  578. { This check must be done with the operand in ATT order
  579. i.e.after swapping in the intel reader
  580. but before swapping in the NASM and TASM writers PM }
  581. procedure taicpu.CheckNonCommutativeOpcodes;
  582. begin
  583. if ((ops=2) and
  584. (oper[0].typ=top_reg) and
  585. (oper[1].typ=top_reg) and
  586. { if the first is ST and the second is also a register
  587. it is necessarily ST1 .. ST7 }
  588. (oper[0].reg=R_ST)) or
  589. { ((ops=1) and
  590. (oper[0].typ=top_reg) and
  591. (oper[0].reg in [R_ST1..R_ST7])) or}
  592. (ops=0) then
  593. if opcode=A_FSUBR then
  594. opcode:=A_FSUB
  595. else if opcode=A_FSUB then
  596. opcode:=A_FSUBR
  597. else if opcode=A_FDIVR then
  598. opcode:=A_FDIV
  599. else if opcode=A_FDIV then
  600. opcode:=A_FDIVR
  601. else if opcode=A_FSUBRP then
  602. opcode:=A_FSUBP
  603. else if opcode=A_FSUBP then
  604. opcode:=A_FSUBRP
  605. else if opcode=A_FDIVRP then
  606. opcode:=A_FDIVP
  607. else if opcode=A_FDIVP then
  608. opcode:=A_FDIVRP;
  609. if ((ops=1) and
  610. (oper[0].typ=top_reg) and
  611. (oper[0].reg in [R_ST1..R_ST7])) then
  612. if opcode=A_FSUBRP then
  613. opcode:=A_FSUBP
  614. else if opcode=A_FSUBP then
  615. opcode:=A_FSUBRP
  616. else if opcode=A_FDIVRP then
  617. opcode:=A_FDIVP
  618. else if opcode=A_FDIVP then
  619. opcode:=A_FDIVRP;
  620. end;
  621. {*****************************************************************************
  622. Assembler
  623. *****************************************************************************}
  624. {$ifndef NOAG386BIN}
  625. type
  626. ea=packed record
  627. sib_present : boolean;
  628. bytes : byte;
  629. size : byte;
  630. modrm : byte;
  631. sib : byte;
  632. end;
  633. procedure taicpu.create_ot;
  634. {
  635. this function will also fix some other fields which only needs to be once
  636. }
  637. var
  638. i,l,relsize : longint;
  639. begin
  640. if ops=0 then
  641. exit;
  642. { update oper[].ot field }
  643. for i:=0 to ops-1 do
  644. with oper[i] do
  645. begin
  646. case typ of
  647. top_reg :
  648. ot:=reg2type[reg];
  649. top_ref :
  650. begin
  651. { create ot field }
  652. if (ot and OT_SIZE_MASK)=0 then
  653. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  654. else
  655. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  656. if (ref^.base=R_NO) and (ref^.index=R_NO) then
  657. ot:=ot or OT_MEM_OFFS;
  658. { fix scalefactor }
  659. if (ref^.index=R_NO) then
  660. ref^.scalefactor:=0
  661. else
  662. if (ref^.scalefactor=0) then
  663. ref^.scalefactor:=1;
  664. end;
  665. top_const :
  666. begin
  667. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  668. ot:=OT_IMM8 or OT_SIGNED
  669. else
  670. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  671. end;
  672. top_symbol :
  673. begin
  674. if LastInsOffset=-1 then
  675. l:=0
  676. else
  677. l:=InsOffset-LastInsOffset;
  678. inc(l,symofs);
  679. if assigned(sym) then
  680. inc(l,sym.address);
  681. { instruction size will then always become 2 (PFV) }
  682. relsize:=(InsOffset+2)-l;
  683. if (not assigned(sym) or
  684. ((sym.currbind<>AB_EXTERNAL) and (sym.address<>0))) and
  685. (relsize>=-128) and (relsize<=127) then
  686. ot:=OT_IMM32 or OT_SHORT
  687. else
  688. ot:=OT_IMM32 or OT_NEAR;
  689. end;
  690. end;
  691. end;
  692. end;
  693. function taicpu.InsEnd:longint;
  694. begin
  695. InsEnd:=InsOffset+InsSize;
  696. end;
  697. function taicpu.Matches(p:PInsEntry):longint;
  698. { * IF_SM stands for Size Match: any operand whose size is not
  699. * explicitly specified by the template is `really' intended to be
  700. * the same size as the first size-specified operand.
  701. * Non-specification is tolerated in the input instruction, but
  702. * _wrong_ specification is not.
  703. *
  704. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  705. * three-operand instructions such as SHLD: it implies that the
  706. * first two operands must match in size, but that the third is
  707. * required to be _unspecified_.
  708. *
  709. * IF_SB invokes Size Byte: operands with unspecified size in the
  710. * template are really bytes, and so no non-byte specification in
  711. * the input instruction will be tolerated. IF_SW similarly invokes
  712. * Size Word, and IF_SD invokes Size Doubleword.
  713. *
  714. * (The default state if neither IF_SM nor IF_SM2 is specified is
  715. * that any operand with unspecified size in the template is
  716. * required to have unspecified size in the instruction too...)
  717. }
  718. var
  719. i,j,asize,oprs : longint;
  720. siz : array[0..2] of longint;
  721. begin
  722. Matches:=100;
  723. { Check the opcode and operands }
  724. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  725. begin
  726. Matches:=0;
  727. exit;
  728. end;
  729. { Check that no spurious colons or TOs are present }
  730. for i:=0 to p^.ops-1 do
  731. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  732. begin
  733. Matches:=0;
  734. exit;
  735. end;
  736. { Check that the operand flags all match up }
  737. for i:=0 to p^.ops-1 do
  738. begin
  739. if ((p^.optypes[i] and (not oper[i].ot)) or
  740. ((p^.optypes[i] and OT_SIZE_MASK) and
  741. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  742. begin
  743. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  744. (oper[i].ot and OT_SIZE_MASK))<>0 then
  745. begin
  746. Matches:=0;
  747. exit;
  748. end
  749. else
  750. Matches:=1;
  751. end;
  752. end;
  753. { Check operand sizes }
  754. { as default an untyped size can get all the sizes, this is different
  755. from nasm, but else we need to do a lot checking which opcodes want
  756. size or not with the automatic size generation }
  757. asize:=longint($ffffffff);
  758. if (p^.flags and IF_SB)<>0 then
  759. asize:=OT_BITS8
  760. else if (p^.flags and IF_SW)<>0 then
  761. asize:=OT_BITS16
  762. else if (p^.flags and IF_SD)<>0 then
  763. asize:=OT_BITS32;
  764. if (p^.flags and IF_ARMASK)<>0 then
  765. begin
  766. siz[0]:=0;
  767. siz[1]:=0;
  768. siz[2]:=0;
  769. if (p^.flags and IF_AR0)<>0 then
  770. siz[0]:=asize
  771. else if (p^.flags and IF_AR1)<>0 then
  772. siz[1]:=asize
  773. else if (p^.flags and IF_AR2)<>0 then
  774. siz[2]:=asize;
  775. end
  776. else
  777. begin
  778. { we can leave because the size for all operands is forced to be
  779. the same
  780. but not if IF_SB IF_SW or IF_SD is set PM }
  781. if asize=-1 then
  782. exit;
  783. siz[0]:=asize;
  784. siz[1]:=asize;
  785. siz[2]:=asize;
  786. end;
  787. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  788. begin
  789. if (p^.flags and IF_SM2)<>0 then
  790. oprs:=2
  791. else
  792. oprs:=p^.ops;
  793. for i:=0 to oprs-1 do
  794. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  795. begin
  796. for j:=0 to oprs-1 do
  797. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  798. break;
  799. end;
  800. end
  801. else
  802. oprs:=2;
  803. { Check operand sizes }
  804. for i:=0 to p^.ops-1 do
  805. begin
  806. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  807. ((oper[i].ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  808. { Immediates can always include smaller size }
  809. ((oper[i].ot and OT_IMMEDIATE)=0) and
  810. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i].ot and OT_SIZE_MASK)) then
  811. Matches:=2;
  812. end;
  813. end;
  814. procedure taicpu.ResetPass1;
  815. begin
  816. { we need to reset everything here, because the choosen insentry
  817. can be invalid for a new situation where the previously optimized
  818. insentry is not correct }
  819. InsEntry:=nil;
  820. InsSize:=0;
  821. LastInsOffset:=-1;
  822. end;
  823. procedure taicpu.ResetPass2;
  824. begin
  825. { we are here in a second pass, check if the instruction can be optimized }
  826. if assigned(InsEntry) and
  827. ((InsEntry^.flags and IF_PASS2)<>0) then
  828. begin
  829. InsEntry:=nil;
  830. InsSize:=0;
  831. end;
  832. LastInsOffset:=-1;
  833. end;
  834. function taicpu.CheckIfValid:boolean;
  835. var
  836. m,i : longint;
  837. begin
  838. CheckIfValid:=false;
  839. { Things which may only be done once, not when a second pass is done to
  840. optimize }
  841. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  842. begin
  843. { We need intel style operands }
  844. SetOperandOrder(op_intel);
  845. { create the .ot fields }
  846. create_ot;
  847. { set the file postion }
  848. aktfilepos:=fileinfo;
  849. end
  850. else
  851. begin
  852. { we've already an insentry so it's valid }
  853. CheckIfValid:=true;
  854. exit;
  855. end;
  856. { Lookup opcode in the table }
  857. InsSize:=-1;
  858. i:=instabcache^[opcode];
  859. if i=-1 then
  860. begin
  861. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  862. exit;
  863. end;
  864. insentry:=@instab[i];
  865. while (insentry^.opcode=opcode) do
  866. begin
  867. m:=matches(insentry);
  868. if m=100 then
  869. begin
  870. InsSize:=calcsize(insentry);
  871. if (segprefix<>R_NO) then
  872. inc(InsSize);
  873. { For opsize if size if forced }
  874. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  875. begin
  876. if (insentry^.flags and IF_ARMASK)=0 then
  877. begin
  878. if (insentry^.flags and IF_SB)<>0 then
  879. begin
  880. if opsize=S_NO then
  881. opsize:=S_B;
  882. end
  883. else if (insentry^.flags and IF_SW)<>0 then
  884. begin
  885. if opsize=S_NO then
  886. opsize:=S_W;
  887. end
  888. else if (insentry^.flags and IF_SD)<>0 then
  889. begin
  890. if opsize=S_NO then
  891. opsize:=S_L;
  892. end;
  893. end;
  894. end;
  895. CheckIfValid:=true;
  896. exit;
  897. end;
  898. inc(i);
  899. insentry:=@instab[i];
  900. end;
  901. if insentry^.opcode<>opcode then
  902. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  903. { No instruction found, set insentry to nil and inssize to -1 }
  904. insentry:=nil;
  905. inssize:=-1;
  906. end;
  907. function taicpu.Pass1(offset:longint):longint;
  908. begin
  909. Pass1:=0;
  910. { Save the old offset and set the new offset }
  911. InsOffset:=Offset;
  912. { Things which may only be done once, not when a second pass is done to
  913. optimize }
  914. if Insentry=nil then
  915. begin
  916. { Check if error last time then InsSize=-1 }
  917. if InsSize=-1 then
  918. exit;
  919. { set the file postion }
  920. aktfilepos:=fileinfo;
  921. end
  922. else
  923. begin
  924. {$ifdef PASS2FLAG}
  925. { we are here in a second pass, check if the instruction can be optimized }
  926. if (InsEntry^.flags and IF_PASS2)=0 then
  927. begin
  928. Pass1:=InsSize;
  929. exit;
  930. end;
  931. { update the .ot fields, some top_const can be updated }
  932. create_ot;
  933. {$endif PASS2FLAG}
  934. end;
  935. { Check if it's a valid instruction }
  936. if CheckIfValid then
  937. begin
  938. LastInsOffset:=InsOffset;
  939. Pass1:=InsSize;
  940. exit;
  941. end;
  942. LastInsOffset:=-1;
  943. end;
  944. procedure taicpu.Pass2(sec:TAsmObjectData);
  945. var
  946. c : longint;
  947. begin
  948. { error in pass1 ? }
  949. if insentry=nil then
  950. exit;
  951. aktfilepos:=fileinfo;
  952. { Segment override }
  953. if (segprefix<>R_NO) then
  954. begin
  955. case segprefix of
  956. R_CS : c:=$2e;
  957. R_DS : c:=$3e;
  958. R_ES : c:=$26;
  959. R_FS : c:=$64;
  960. R_GS : c:=$65;
  961. R_SS : c:=$36;
  962. end;
  963. sec.writebytes(c,1);
  964. { fix the offset for GenNode }
  965. inc(InsOffset);
  966. end;
  967. { Generate the instruction }
  968. GenCode(sec);
  969. end;
  970. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  971. var
  972. i,b : tregister;
  973. begin
  974. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  975. begin
  976. i:=oper[opidx].ref^.index;
  977. b:=oper[opidx].ref^.base;
  978. if not(i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) or
  979. not(b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) then
  980. begin
  981. NeedAddrPrefix:=true;
  982. exit;
  983. end;
  984. end;
  985. NeedAddrPrefix:=false;
  986. end;
  987. function regval(r:tregister):byte;
  988. begin
  989. case r of
  990. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0 :
  991. regval:=0;
  992. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1 :
  993. regval:=1;
  994. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2 :
  995. regval:=2;
  996. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3 :
  997. regval:=3;
  998. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4 :
  999. regval:=4;
  1000. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5 :
  1001. regval:=5;
  1002. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6 :
  1003. regval:=6;
  1004. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7 :
  1005. regval:=7;
  1006. else
  1007. begin
  1008. internalerror(777001);
  1009. regval:=0;
  1010. end;
  1011. end;
  1012. end;
  1013. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1014. const
  1015. regs : array[0..63] of tregister=(
  1016. R_MM0, R_EAX, R_AX, R_AL, R_XMM0, R_NO, R_NO, R_NO,
  1017. R_MM1, R_ECX, R_CX, R_CL, R_XMM1, R_NO, R_NO, R_NO,
  1018. R_MM2, R_EDX, R_DX, R_DL, R_XMM2, R_NO, R_NO, R_NO,
  1019. R_MM3, R_EBX, R_BX, R_BL, R_XMM3, R_NO, R_NO, R_NO,
  1020. R_MM4, R_ESP, R_SP, R_AH, R_XMM4, R_NO, R_NO, R_NO,
  1021. R_MM5, R_EBP, R_BP, R_CH, R_XMM5, R_NO, R_NO, R_NO,
  1022. R_MM6, R_ESI, R_SI, R_DH, R_XMM6, R_NO, R_NO, R_NO,
  1023. R_MM7, R_EDI, R_DI, R_BH, R_XMM7, R_NO, R_NO, R_NO
  1024. );
  1025. var
  1026. j : longint;
  1027. i,b : tregister;
  1028. sym : tasmsymbol;
  1029. md,s : byte;
  1030. base,index,scalefactor,
  1031. o : longint;
  1032. begin
  1033. process_ea:=false;
  1034. { register ? }
  1035. if (input.typ=top_reg) then
  1036. begin
  1037. j:=0;
  1038. while (j<=high(regs)) do
  1039. begin
  1040. if input.reg=regs[j] then
  1041. break;
  1042. inc(j);
  1043. end;
  1044. if j<=high(regs) then
  1045. begin
  1046. output.sib_present:=false;
  1047. output.bytes:=0;
  1048. output.modrm:=$c0 or (rfield shl 3) or (j shr 3);
  1049. output.size:=1;
  1050. process_ea:=true;
  1051. end;
  1052. exit;
  1053. end;
  1054. { memory reference }
  1055. i:=input.ref^.index;
  1056. b:=input.ref^.base;
  1057. s:=input.ref^.scalefactor;
  1058. o:=input.ref^.offset+input.ref^.offsetfixup;
  1059. sym:=input.ref^.symbol;
  1060. { it's direct address }
  1061. if (b=R_NO) and (i=R_NO) then
  1062. begin
  1063. { it's a pure offset }
  1064. output.sib_present:=false;
  1065. output.bytes:=4;
  1066. output.modrm:=5 or (rfield shl 3);
  1067. end
  1068. else
  1069. { it's an indirection }
  1070. begin
  1071. { 16 bit address? }
  1072. if not((i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  1073. (b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  1074. Message(asmw_e_16bit_not_supported);
  1075. {$ifdef OPTEA}
  1076. { make single reg base }
  1077. if (b=R_NO) and (s=1) then
  1078. begin
  1079. b:=i;
  1080. i:=R_NO;
  1081. end;
  1082. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1083. if (b=R_NO) and
  1084. (((s=2) and (i<>R_ESP)) or
  1085. (s=3) or (s=5) or (s=9)) then
  1086. begin
  1087. b:=i;
  1088. dec(s);
  1089. end;
  1090. { swap ESP into base if scalefactor is 1 }
  1091. if (s=1) and (i=R_ESP) then
  1092. begin
  1093. i:=b;
  1094. b:=R_ESP;
  1095. end;
  1096. {$endif OPTEA}
  1097. { wrong, for various reasons }
  1098. if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NO)) then
  1099. exit;
  1100. { base }
  1101. case b of
  1102. R_EAX : base:=0;
  1103. R_ECX : base:=1;
  1104. R_EDX : base:=2;
  1105. R_EBX : base:=3;
  1106. R_ESP : base:=4;
  1107. R_NO,
  1108. R_EBP : base:=5;
  1109. R_ESI : base:=6;
  1110. R_EDI : base:=7;
  1111. else
  1112. exit;
  1113. end;
  1114. { index }
  1115. case i of
  1116. R_EAX : index:=0;
  1117. R_ECX : index:=1;
  1118. R_EDX : index:=2;
  1119. R_EBX : index:=3;
  1120. R_NO : index:=4;
  1121. R_EBP : index:=5;
  1122. R_ESI : index:=6;
  1123. R_EDI : index:=7;
  1124. else
  1125. exit;
  1126. end;
  1127. case s of
  1128. 0,
  1129. 1 : scalefactor:=0;
  1130. 2 : scalefactor:=1;
  1131. 4 : scalefactor:=2;
  1132. 8 : scalefactor:=3;
  1133. else
  1134. exit;
  1135. end;
  1136. if (b=R_NO) or
  1137. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  1138. md:=0
  1139. else
  1140. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1141. md:=1
  1142. else
  1143. md:=2;
  1144. if (b=R_NO) or (md=2) then
  1145. output.bytes:=4
  1146. else
  1147. output.bytes:=md;
  1148. { SIB needed ? }
  1149. if (i=R_NO) and (b<>R_ESP) then
  1150. begin
  1151. output.sib_present:=false;
  1152. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1153. end
  1154. else
  1155. begin
  1156. output.sib_present:=true;
  1157. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1158. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1159. end;
  1160. end;
  1161. if output.sib_present then
  1162. output.size:=2+output.bytes
  1163. else
  1164. output.size:=1+output.bytes;
  1165. process_ea:=true;
  1166. end;
  1167. function taicpu.calcsize(p:PInsEntry):longint;
  1168. var
  1169. codes : pchar;
  1170. c : byte;
  1171. len : longint;
  1172. ea_data : ea;
  1173. begin
  1174. len:=0;
  1175. codes:=@p^.code;
  1176. repeat
  1177. c:=ord(codes^);
  1178. inc(codes);
  1179. case c of
  1180. 0 :
  1181. break;
  1182. 1,2,3 :
  1183. begin
  1184. inc(codes,c);
  1185. inc(len,c);
  1186. end;
  1187. 8,9,10 :
  1188. begin
  1189. inc(codes);
  1190. inc(len);
  1191. end;
  1192. 4,5,6,7 :
  1193. begin
  1194. if opsize=S_W then
  1195. inc(len,2)
  1196. else
  1197. inc(len);
  1198. end;
  1199. 15,
  1200. 12,13,14,
  1201. 16,17,18,
  1202. 20,21,22,
  1203. 40,41,42 :
  1204. inc(len);
  1205. 24,25,26,
  1206. 31,
  1207. 48,49,50 :
  1208. inc(len,2);
  1209. 28,29,30, { we don't have 16 bit immediates code }
  1210. 32,33,34,
  1211. 52,53,54,
  1212. 56,57,58 :
  1213. inc(len,4);
  1214. 192,193,194 :
  1215. if NeedAddrPrefix(c-192) then
  1216. inc(len);
  1217. 208 :
  1218. inc(len);
  1219. 200,
  1220. 201,
  1221. 202,
  1222. 209,
  1223. 210,
  1224. 217,218,219 : ;
  1225. 216 :
  1226. begin
  1227. inc(codes);
  1228. inc(len);
  1229. end;
  1230. 224,225,226 :
  1231. begin
  1232. InternalError(777002);
  1233. end;
  1234. else
  1235. begin
  1236. if (c>=64) and (c<=191) then
  1237. begin
  1238. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1239. Message(asmw_e_invalid_effective_address)
  1240. else
  1241. inc(len,ea_data.size);
  1242. end
  1243. else
  1244. InternalError(777003);
  1245. end;
  1246. end;
  1247. until false;
  1248. calcsize:=len;
  1249. end;
  1250. procedure taicpu.GenCode(sec:TAsmObjectData);
  1251. {
  1252. * the actual codes (C syntax, i.e. octal):
  1253. * \0 - terminates the code. (Unless it's a literal of course.)
  1254. * \1, \2, \3 - that many literal bytes follow in the code stream
  1255. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1256. * (POP is never used for CS) depending on operand 0
  1257. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1258. * on operand 0
  1259. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1260. * to the register value of operand 0, 1 or 2
  1261. * \17 - encodes the literal byte 0. (Some compilers don't take
  1262. * kindly to a zero byte in the _middle_ of a compile time
  1263. * string constant, so I had to put this hack in.)
  1264. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1265. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1266. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1267. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1268. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1269. * assembly mode or the address-size override on the operand
  1270. * \37 - a word constant, from the _segment_ part of operand 0
  1271. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1272. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1273. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1274. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1275. * assembly mode or the address-size override on the operand
  1276. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1277. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1278. * field the register value of operand b.
  1279. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1280. * field equal to digit b.
  1281. * \30x - might be an 0x67 byte, depending on the address size of
  1282. * the memory reference in operand x.
  1283. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1284. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1285. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1286. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1287. * \322 - indicates that this instruction is only valid when the
  1288. * operand size is the default (instruction to disassembler,
  1289. * generates no code in the assembler)
  1290. * \330 - a literal byte follows in the code stream, to be added
  1291. * to the condition code value of the instruction.
  1292. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1293. * Operand 0 had better be a segmentless constant.
  1294. }
  1295. var
  1296. currval : longint;
  1297. currsym : tasmsymbol;
  1298. procedure getvalsym(opidx:longint);
  1299. begin
  1300. case oper[opidx].typ of
  1301. top_ref :
  1302. begin
  1303. currval:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1304. currsym:=oper[opidx].ref^.symbol;
  1305. end;
  1306. top_const :
  1307. begin
  1308. currval:=longint(oper[opidx].val);
  1309. currsym:=nil;
  1310. end;
  1311. top_symbol :
  1312. begin
  1313. currval:=oper[opidx].symofs;
  1314. currsym:=oper[opidx].sym;
  1315. end;
  1316. else
  1317. Message(asmw_e_immediate_or_reference_expected);
  1318. end;
  1319. end;
  1320. const
  1321. CondVal:array[TAsmCond] of byte=($0,
  1322. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1323. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1324. $0, $A, $A, $B, $8, $4);
  1325. var
  1326. c : byte;
  1327. pb,
  1328. codes : pchar;
  1329. bytes : array[0..3] of byte;
  1330. rfield,
  1331. data,s,opidx : longint;
  1332. ea_data : ea;
  1333. begin
  1334. {$ifdef EXTDEBUG}
  1335. { safety check }
  1336. if sec.sects[sec.currsec].datasize<>insoffset then
  1337. internalerror(200130121);
  1338. {$endif EXTDEBUG}
  1339. { load data to write }
  1340. codes:=insentry^.code;
  1341. { Force word push/pop for registers }
  1342. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1343. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1344. begin
  1345. bytes[0]:=$66;
  1346. sec.writebytes(bytes,1);
  1347. end;
  1348. repeat
  1349. c:=ord(codes^);
  1350. inc(codes);
  1351. case c of
  1352. 0 :
  1353. break;
  1354. 1,2,3 :
  1355. begin
  1356. sec.writebytes(codes^,c);
  1357. inc(codes,c);
  1358. end;
  1359. 4,6 :
  1360. begin
  1361. case oper[0].reg of
  1362. R_CS :
  1363. begin
  1364. if c=4 then
  1365. bytes[0]:=$f
  1366. else
  1367. bytes[0]:=$e;
  1368. end;
  1369. R_NO,
  1370. R_DS :
  1371. begin
  1372. if c=4 then
  1373. bytes[0]:=$1f
  1374. else
  1375. bytes[0]:=$1e;
  1376. end;
  1377. R_ES :
  1378. begin
  1379. if c=4 then
  1380. bytes[0]:=$7
  1381. else
  1382. bytes[0]:=$6;
  1383. end;
  1384. R_SS :
  1385. begin
  1386. if c=4 then
  1387. bytes[0]:=$17
  1388. else
  1389. bytes[0]:=$16;
  1390. end;
  1391. else
  1392. InternalError(777004);
  1393. end;
  1394. sec.writebytes(bytes,1);
  1395. end;
  1396. 5,7 :
  1397. begin
  1398. case oper[0].reg of
  1399. R_FS :
  1400. begin
  1401. if c=5 then
  1402. bytes[0]:=$a1
  1403. else
  1404. bytes[0]:=$a0;
  1405. end;
  1406. R_GS :
  1407. begin
  1408. if c=5 then
  1409. bytes[0]:=$a9
  1410. else
  1411. bytes[0]:=$a8;
  1412. end;
  1413. else
  1414. InternalError(777005);
  1415. end;
  1416. sec.writebytes(bytes,1);
  1417. end;
  1418. 8,9,10 :
  1419. begin
  1420. bytes[0]:=ord(codes^)+regval(oper[c-8].reg);
  1421. inc(codes);
  1422. sec.writebytes(bytes,1);
  1423. end;
  1424. 15 :
  1425. begin
  1426. bytes[0]:=0;
  1427. sec.writebytes(bytes,1);
  1428. end;
  1429. 12,13,14 :
  1430. begin
  1431. getvalsym(c-12);
  1432. if (currval<-128) or (currval>127) then
  1433. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1434. if assigned(currsym) then
  1435. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1436. else
  1437. sec.writebytes(currval,1);
  1438. end;
  1439. 16,17,18 :
  1440. begin
  1441. getvalsym(c-16);
  1442. if (currval<-256) or (currval>255) then
  1443. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1444. if assigned(currsym) then
  1445. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1446. else
  1447. sec.writebytes(currval,1);
  1448. end;
  1449. 20,21,22 :
  1450. begin
  1451. getvalsym(c-20);
  1452. if (currval<0) or (currval>255) then
  1453. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1454. if assigned(currsym) then
  1455. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1456. else
  1457. sec.writebytes(currval,1);
  1458. end;
  1459. 24,25,26 :
  1460. begin
  1461. getvalsym(c-24);
  1462. if (currval<-65536) or (currval>65535) then
  1463. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1464. if assigned(currsym) then
  1465. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1466. else
  1467. sec.writebytes(currval,2);
  1468. end;
  1469. 28,29,30 :
  1470. begin
  1471. getvalsym(c-28);
  1472. if assigned(currsym) then
  1473. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1474. else
  1475. sec.writebytes(currval,4);
  1476. end;
  1477. 32,33,34 :
  1478. begin
  1479. getvalsym(c-32);
  1480. if assigned(currsym) then
  1481. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1482. else
  1483. sec.writebytes(currval,4);
  1484. end;
  1485. 40,41,42 :
  1486. begin
  1487. getvalsym(c-40);
  1488. data:=currval-insend;
  1489. if assigned(currsym) then
  1490. inc(data,currsym.address);
  1491. if (data>127) or (data<-128) then
  1492. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1493. sec.writebytes(data,1);
  1494. end;
  1495. 52,53,54 :
  1496. begin
  1497. getvalsym(c-52);
  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. 56,57,58 :
  1504. begin
  1505. getvalsym(c-56);
  1506. if assigned(currsym) then
  1507. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1508. else
  1509. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1510. end;
  1511. 192,193,194 :
  1512. begin
  1513. if NeedAddrPrefix(c-192) then
  1514. begin
  1515. bytes[0]:=$67;
  1516. sec.writebytes(bytes,1);
  1517. end;
  1518. end;
  1519. 200 :
  1520. begin
  1521. bytes[0]:=$67;
  1522. sec.writebytes(bytes,1);
  1523. end;
  1524. 208 :
  1525. begin
  1526. bytes[0]:=$66;
  1527. sec.writebytes(bytes,1);
  1528. end;
  1529. 216 :
  1530. begin
  1531. bytes[0]:=ord(codes^)+condval[condition];
  1532. inc(codes);
  1533. sec.writebytes(bytes,1);
  1534. end;
  1535. 201,
  1536. 202,
  1537. 209,
  1538. 210,
  1539. 217,218,219 :
  1540. begin
  1541. { these are dissambler hints or 32 bit prefixes which
  1542. are not needed }
  1543. end;
  1544. 31,
  1545. 48,49,50,
  1546. 224,225,226 :
  1547. begin
  1548. InternalError(777006);
  1549. end
  1550. else
  1551. begin
  1552. if (c>=64) and (c<=191) then
  1553. begin
  1554. if (c<127) then
  1555. begin
  1556. if (oper[c and 7].typ=top_reg) then
  1557. rfield:=regval(oper[c and 7].reg)
  1558. else
  1559. rfield:=regval(oper[c and 7].ref^.base);
  1560. end
  1561. else
  1562. rfield:=c and 7;
  1563. opidx:=(c shr 3) and 7;
  1564. if not process_ea(oper[opidx], ea_data, rfield) then
  1565. Message(asmw_e_invalid_effective_address);
  1566. pb:=@bytes;
  1567. pb^:=chr(ea_data.modrm);
  1568. inc(pb);
  1569. if ea_data.sib_present then
  1570. begin
  1571. pb^:=chr(ea_data.sib);
  1572. inc(pb);
  1573. end;
  1574. s:=pb-pchar(@bytes);
  1575. sec.writebytes(bytes,s);
  1576. case ea_data.bytes of
  1577. 0 : ;
  1578. 1 :
  1579. begin
  1580. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1581. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,1,oper[opidx].ref^.symbol,RELOC_ABSOLUTE)
  1582. else
  1583. begin
  1584. bytes[0]:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1585. sec.writebytes(bytes,1);
  1586. end;
  1587. inc(s);
  1588. end;
  1589. 2,4 :
  1590. begin
  1591. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,ea_data.bytes,
  1592. oper[opidx].ref^.symbol,RELOC_ABSOLUTE);
  1593. inc(s,ea_data.bytes);
  1594. end;
  1595. end;
  1596. end
  1597. else
  1598. InternalError(777007);
  1599. end;
  1600. end;
  1601. until false;
  1602. end;
  1603. {$endif NOAG386BIN}
  1604. {*****************************************************************************
  1605. Instruction table
  1606. *****************************************************************************}
  1607. procedure BuildInsTabCache;
  1608. {$ifndef NOAG386BIN}
  1609. var
  1610. i : longint;
  1611. {$endif}
  1612. begin
  1613. {$ifndef NOAG386BIN}
  1614. new(instabcache);
  1615. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1616. i:=0;
  1617. while (i<InsTabEntries) do
  1618. begin
  1619. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1620. InsTabCache^[InsTab[i].OPcode]:=i;
  1621. inc(i);
  1622. end;
  1623. {$endif NOAG386BIN}
  1624. end;
  1625. procedure InitAsm;
  1626. begin
  1627. {$ifndef NOAG386BIN}
  1628. if not assigned(instabcache) then
  1629. BuildInsTabCache;
  1630. {$endif NOAG386BIN}
  1631. end;
  1632. procedure DoneAsm;
  1633. begin
  1634. {$ifndef NOAG386BIN}
  1635. if assigned(instabcache) then
  1636. dispose(instabcache);
  1637. {$endif NOAG386BIN}
  1638. end;
  1639. end.
  1640. {
  1641. $Log$
  1642. Revision 1.3 2002-08-13 18:01:52 carl
  1643. * rename swatoperands to swapoperands
  1644. + m68k first compilable version (still needs a lot of testing):
  1645. assembler generator, system information , inline
  1646. assembler reader.
  1647. Revision 1.2 2002/07/20 11:57:59 florian
  1648. * types.pas renamed to defbase.pas because D6 contains a types
  1649. unit so this would conflicts if D6 programms are compiled
  1650. + Willamette/SSE2 instructions to assembler added
  1651. Revision 1.1 2002/07/01 18:46:29 peter
  1652. * internal linker
  1653. * reorganized aasm layer
  1654. }