aasmcpu.pas 59 KB

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