aasmcpu.pas 58 KB

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