aasmcpu.pas 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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. symppu,
  28. aasmbase,aasmtai;
  29. const
  30. { Operand types }
  31. OT_NONE = $00000000;
  32. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  33. OT_BITS16 = $00000002;
  34. OT_BITS32 = $00000004;
  35. OT_BITS64 = $00000008; { FPU only }
  36. OT_BITS80 = $00000010;
  37. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  38. OT_NEAR = $00000040;
  39. OT_SHORT = $00000080;
  40. OT_SIZE_MASK = $000000FF; { all the size attributes }
  41. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  42. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  43. OT_TO = $00000200; { operand is followed by a colon }
  44. { reverse effect in FADD, FSUB &c }
  45. OT_COLON = $00000400;
  46. OT_REGISTER = $00001000;
  47. OT_IMMEDIATE = $00002000;
  48. OT_IMM8 = $00002001;
  49. OT_IMM16 = $00002002;
  50. OT_IMM32 = $00002004;
  51. OT_IMM64 = $00002008;
  52. OT_IMM80 = $00002010;
  53. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  54. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  55. OT_REG8 = $00201001;
  56. OT_REG16 = $00201002;
  57. OT_REG32 = $00201004;
  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_COUNT = $00221000; { counter: CL, CX or ECX }
  75. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  76. OT_REG_CX = $00221002; { ditto }
  77. OT_REG_ECX = $00221004; { another one }
  78. OT_REG_DX = $00241002;
  79. OT_REG_SREG = $00081002; { any segment register }
  80. OT_REG_CS = $01081002; { CS }
  81. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  82. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  83. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  84. OT_REG_CREG = $08101004; { CRn }
  85. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  86. OT_REG_DREG = $10101004; { DRn }
  87. OT_REG_TREG = $20101004; { TRn }
  88. OT_MEM_OFFS = $00604000; { special type of EA }
  89. { simple [address] offset }
  90. OT_ONENESS = $00800000; { special type of immediate operand }
  91. { so UNITY == IMMEDIATE | ONENESS }
  92. OT_UNITY = $00802000; { for shift/rotate instructions }
  93. { Size of the instruction table converted by nasmconv.pas }
  94. instabentries = {$i i386nop.inc}
  95. maxinfolen = 8;
  96. type
  97. TOperandOrder = (op_intel,op_att);
  98. tinsentry=packed record
  99. opcode : tasmop;
  100. ops : byte;
  101. optypes : array[0..2] of longint;
  102. code : array[0..maxinfolen] of char;
  103. flags : longint;
  104. end;
  105. pinsentry=^tinsentry;
  106. { alignment for operator }
  107. tai_align = class(tai_align_abstract)
  108. reg : tregister;
  109. constructor create(b:byte);
  110. constructor create_op(b: byte; _op: byte);
  111. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  112. end;
  113. taicpu = class(taicpu_abstract)
  114. opsize : topsize;
  115. constructor op_none(op : tasmop;_size : topsize);
  116. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  117. constructor op_const(op : tasmop;_size : topsize;_op1 : aword);
  118. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  119. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  120. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  121. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  122. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  123. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  124. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  125. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  126. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  127. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  128. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  129. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  130. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  131. { this is for Jmp instructions }
  132. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  133. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  134. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  135. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  136. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  137. procedure changeopsize(siz:topsize);
  138. function GetString:string;
  139. procedure CheckNonCommutativeOpcodes;
  140. private
  141. FOperandOrder : TOperandOrder;
  142. procedure init(_size : topsize); { this need to be called by all constructor }
  143. {$ifndef NOAG386BIN}
  144. public
  145. { the next will reset all instructions that can change in pass 2 }
  146. procedure ResetPass1;
  147. procedure ResetPass2;
  148. function CheckIfValid:boolean;
  149. function Pass1(offset:longint):longint;virtual;
  150. procedure Pass2(sec:TAsmObjectdata);virtual;
  151. procedure SetOperandOrder(order:TOperandOrder);
  152. protected
  153. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  154. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  155. procedure ppuderefoper(var o:toper);override;
  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. ag386att;
  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. { it's a 3DNow! instruction }
  199. IF_3DNOW = $00008000;
  200. { it's a SSE (KNI, MMX2) instruction }
  201. IF_SSE = $00010000;
  202. { SSE2 instructions }
  203. IF_SSE2 = $00020000;
  204. { the mask for processor types }
  205. IF_PMASK = longint($FF000000);
  206. { the mask for disassembly "prefer" }
  207. IF_PFMASK = longint($F001FF00);
  208. IF_8086 = $00000000; { 8086 instruction }
  209. IF_186 = $01000000; { 186+ instruction }
  210. IF_286 = $02000000; { 286+ instruction }
  211. IF_386 = $03000000; { 386+ instruction }
  212. IF_486 = $04000000; { 486+ instruction }
  213. IF_PENT = $05000000; { Pentium instruction }
  214. IF_P6 = $06000000; { P6 instruction }
  215. IF_KATMAI = $07000000; { Katmai instructions }
  216. { Willamette instructions }
  217. IF_WILLAMETTE = $08000000;
  218. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  219. IF_AMD = $20000000; { AMD-specific instruction }
  220. { added flags }
  221. IF_PRE = $40000000; { it's a prefix instruction }
  222. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  223. type
  224. TInsTabCache=array[TasmOp] of longint;
  225. PInsTabCache=^TInsTabCache;
  226. const
  227. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  228. var
  229. InsTabCache : PInsTabCache;
  230. const
  231. { Intel style operands ! }
  232. opsize_2_type:array[0..2,topsize] of longint=(
  233. (OT_NONE,
  234. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,
  235. OT_BITS16,OT_BITS32,OT_BITS64,
  236. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  237. OT_NEAR,OT_FAR,OT_SHORT
  238. ),
  239. (OT_NONE,
  240. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,
  241. OT_BITS16,OT_BITS32,OT_BITS64,
  242. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  243. OT_NEAR,OT_FAR,OT_SHORT
  244. ),
  245. (OT_NONE,
  246. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,
  247. OT_BITS16,OT_BITS32,OT_BITS64,
  248. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  249. OT_NEAR,OT_FAR,OT_SHORT
  250. )
  251. );
  252. subreg2type:array[R_SUBL..R_SUBD] of longint = (
  253. OT_REG8,OT_REG8,OT_REG16,OT_REG32
  254. );
  255. { Convert reg to operand type }
  256. reg2type : array[firstreg..lastreg] of longint = (OT_NONE,
  257. OT_REG_EAX,OT_REG_ECX,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  258. OT_REG_AX,OT_REG_CX,OT_REG_DX,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  259. OT_REG_AL,OT_REG_CL,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  260. OT_REG_CS,OT_REG_DESS,OT_REG_DESS,OT_REG_DESS,OT_REG_FSGS,OT_REG_FSGS,
  261. OT_FPU0,OT_FPU0,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,
  262. OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,
  263. OT_REG_CREG,OT_REG_CREG,OT_REG_CREG,OT_REG_CR4,
  264. OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,
  265. OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,
  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.enum := R_ECX;
  275. end;
  276. constructor tai_align.create_op(b: byte; _op: byte);
  277. begin
  278. inherited create_op(b,_op);
  279. reg.enum := 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.enum:=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. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  588. begin
  589. o.typ:=toptype(ppufile.getbyte);
  590. o.ot:=ppufile.getlongint;
  591. case o.typ of
  592. top_reg :
  593. ppufile.getdata(o.reg,sizeof(Tregister));
  594. top_ref :
  595. begin
  596. new(o.ref);
  597. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  598. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  599. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  600. o.ref^.scalefactor:=ppufile.getbyte;
  601. o.ref^.offset:=ppufile.getlongint;
  602. o.ref^.symbol:=ppufile.getasmsymbol;
  603. o.ref^.offsetfixup:=ppufile.getlongint;
  604. o.ref^.options:=trefoptions(ppufile.getbyte);
  605. end;
  606. top_const :
  607. o.val:=aword(ppufile.getlongint);
  608. top_symbol :
  609. begin
  610. o.sym:=ppufile.getasmsymbol;
  611. o.symofs:=ppufile.getlongint;
  612. end;
  613. end;
  614. end;
  615. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  616. begin
  617. ppufile.putbyte(byte(o.typ));
  618. ppufile.putlongint(o.ot);
  619. case o.typ of
  620. top_reg :
  621. ppufile.putdata(o.reg,sizeof(Tregister));
  622. top_ref :
  623. begin
  624. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  625. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  626. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  627. ppufile.putbyte(o.ref^.scalefactor);
  628. ppufile.putlongint(o.ref^.offset);
  629. ppufile.putasmsymbol(o.ref^.symbol);
  630. ppufile.putlongint(o.ref^.offsetfixup);
  631. ppufile.putbyte(byte(o.ref^.options));
  632. end;
  633. top_const :
  634. ppufile.putlongint(longint(o.val));
  635. top_symbol :
  636. begin
  637. ppufile.putasmsymbol(o.sym);
  638. ppufile.putlongint(longint(o.symofs));
  639. end;
  640. end;
  641. end;
  642. procedure taicpu.ppuderefoper(var o:toper);
  643. begin
  644. case o.typ of
  645. top_ref :
  646. begin
  647. if assigned(o.ref^.symbol) then
  648. objectlibrary.derefasmsymbol(o.ref^.symbol);
  649. end;
  650. top_symbol :
  651. objectlibrary.derefasmsymbol(o.sym);
  652. end;
  653. end;
  654. procedure taicpu.CheckNonCommutativeOpcodes;
  655. begin
  656. { we need ATT order }
  657. SetOperandOrder(op_att);
  658. if ((ops=2) and
  659. (oper[0].typ=top_reg) and
  660. (oper[1].typ=top_reg) and
  661. { if the first is ST and the second is also a register
  662. it is necessarily ST1 .. ST7 }
  663. (oper[0].reg.enum in [R_ST..R_ST0])) or
  664. { ((ops=1) and
  665. (oper[0].typ=top_reg) and
  666. (oper[0].reg in [R_ST1..R_ST7])) or}
  667. (ops=0) then
  668. if opcode=A_FSUBR then
  669. opcode:=A_FSUB
  670. else if opcode=A_FSUB then
  671. opcode:=A_FSUBR
  672. else if opcode=A_FDIVR then
  673. opcode:=A_FDIV
  674. else if opcode=A_FDIV then
  675. opcode:=A_FDIVR
  676. else if opcode=A_FSUBRP then
  677. opcode:=A_FSUBP
  678. else if opcode=A_FSUBP then
  679. opcode:=A_FSUBRP
  680. else if opcode=A_FDIVRP then
  681. opcode:=A_FDIVP
  682. else if opcode=A_FDIVP then
  683. opcode:=A_FDIVRP;
  684. if ((ops=1) and
  685. (oper[0].typ=top_reg) and
  686. (oper[0].reg.enum in [R_ST1..R_ST7])) then
  687. if opcode=A_FSUBRP then
  688. opcode:=A_FSUBP
  689. else if opcode=A_FSUBP then
  690. opcode:=A_FSUBRP
  691. else if opcode=A_FDIVRP then
  692. opcode:=A_FDIVP
  693. else if opcode=A_FDIVP then
  694. opcode:=A_FDIVRP;
  695. end;
  696. {*****************************************************************************
  697. Assembler
  698. *****************************************************************************}
  699. {$ifndef NOAG386BIN}
  700. type
  701. ea=packed record
  702. sib_present : boolean;
  703. bytes : byte;
  704. size : byte;
  705. modrm : byte;
  706. sib : byte;
  707. end;
  708. procedure taicpu.create_ot;
  709. {
  710. this function will also fix some other fields which only needs to be once
  711. }
  712. var
  713. i,l,relsize : longint;
  714. nb,ni:boolean;
  715. begin
  716. if ops=0 then
  717. exit;
  718. { update oper[].ot field }
  719. for i:=0 to ops-1 do
  720. with oper[i] do
  721. begin
  722. case typ of
  723. top_reg :
  724. begin
  725. if reg.enum=R_INTREGISTER then
  726. case reg.number of
  727. NR_AL:
  728. ot:=OT_REG_AL;
  729. NR_AX:
  730. ot:=OT_REG_AX;
  731. NR_EAX:
  732. ot:=OT_REG_EAX;
  733. NR_CL:
  734. ot:=OT_REG_CL;
  735. NR_CX:
  736. ot:=OT_REG_CX;
  737. NR_ECX:
  738. ot:=OT_REG_ECX;
  739. NR_DX:
  740. ot:=OT_REG_DX;
  741. NR_CS:
  742. ot:=OT_REG_CS;
  743. NR_DS,NR_ES,NR_SS:
  744. ot:=OT_REG_DESS;
  745. NR_FS,NR_GS:
  746. ot:=OT_REG_FSGS;
  747. NR_DR0..NR_DR7:
  748. ot:=OT_REG_DREG;
  749. NR_CR0..NR_CR3:
  750. ot:=OT_REG_CREG;
  751. NR_CR4:
  752. ot:=OT_REG_CR4;
  753. NR_TR3..NR_TR7:
  754. ot:=OT_REG_TREG;
  755. else
  756. ot:=subreg2type[reg.number and $ff];
  757. end
  758. else
  759. ot:=reg2type[reg.enum];
  760. end;
  761. top_ref :
  762. begin
  763. nb:=(ref^.base.enum=R_NO) or
  764. ((ref^.base.enum=R_INTREGISTER) and (ref^.base.number=NR_NO));
  765. ni:=(ref^.index.enum=R_NO) or
  766. ((ref^.index.enum=R_INTREGISTER) and (ref^.index.number=NR_NO));
  767. { create ot field }
  768. if (ot and OT_SIZE_MASK)=0 then
  769. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  770. else
  771. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  772. if nb and ni then
  773. ot:=ot or OT_MEM_OFFS;
  774. { fix scalefactor }
  775. if ni then
  776. ref^.scalefactor:=0
  777. else
  778. if (ref^.scalefactor=0) then
  779. ref^.scalefactor:=1;
  780. end;
  781. top_const :
  782. begin
  783. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  784. ot:=OT_IMM8 or OT_SIGNED
  785. else
  786. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  787. end;
  788. top_symbol :
  789. begin
  790. if LastInsOffset=-1 then
  791. l:=0
  792. else
  793. l:=InsOffset-LastInsOffset;
  794. inc(l,symofs);
  795. if assigned(sym) then
  796. inc(l,sym.address);
  797. { instruction size will then always become 2 (PFV) }
  798. relsize:=(InsOffset+2)-l;
  799. if (not assigned(sym) or
  800. ((sym.currbind<>AB_EXTERNAL) and (sym.address<>0))) and
  801. (relsize>=-128) and (relsize<=127) then
  802. ot:=OT_IMM32 or OT_SHORT
  803. else
  804. ot:=OT_IMM32 or OT_NEAR;
  805. end;
  806. end;
  807. end;
  808. end;
  809. function taicpu.InsEnd:longint;
  810. begin
  811. InsEnd:=InsOffset+InsSize;
  812. end;
  813. function taicpu.Matches(p:PInsEntry):longint;
  814. { * IF_SM stands for Size Match: any operand whose size is not
  815. * explicitly specified by the template is `really' intended to be
  816. * the same size as the first size-specified operand.
  817. * Non-specification is tolerated in the input instruction, but
  818. * _wrong_ specification is not.
  819. *
  820. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  821. * three-operand instructions such as SHLD: it implies that the
  822. * first two operands must match in size, but that the third is
  823. * required to be _unspecified_.
  824. *
  825. * IF_SB invokes Size Byte: operands with unspecified size in the
  826. * template are really bytes, and so no non-byte specification in
  827. * the input instruction will be tolerated. IF_SW similarly invokes
  828. * Size Word, and IF_SD invokes Size Doubleword.
  829. *
  830. * (The default state if neither IF_SM nor IF_SM2 is specified is
  831. * that any operand with unspecified size in the template is
  832. * required to have unspecified size in the instruction too...)
  833. }
  834. var
  835. i,j,asize,oprs : longint;
  836. siz : array[0..2] of longint;
  837. begin
  838. Matches:=100;
  839. { Check the opcode and operands }
  840. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  841. begin
  842. Matches:=0;
  843. exit;
  844. end;
  845. { Check that no spurious colons or TOs are present }
  846. for i:=0 to p^.ops-1 do
  847. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  848. begin
  849. Matches:=0;
  850. exit;
  851. end;
  852. { Check that the operand flags all match up }
  853. for i:=0 to p^.ops-1 do
  854. begin
  855. if ((p^.optypes[i] and (not oper[i].ot)) or
  856. ((p^.optypes[i] and OT_SIZE_MASK) and
  857. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  858. begin
  859. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  860. (oper[i].ot and OT_SIZE_MASK))<>0 then
  861. begin
  862. Matches:=0;
  863. exit;
  864. end
  865. else
  866. Matches:=1;
  867. end;
  868. end;
  869. { Check operand sizes }
  870. { as default an untyped size can get all the sizes, this is different
  871. from nasm, but else we need to do a lot checking which opcodes want
  872. size or not with the automatic size generation }
  873. asize:=longint($ffffffff);
  874. if (p^.flags and IF_SB)<>0 then
  875. asize:=OT_BITS8
  876. else if (p^.flags and IF_SW)<>0 then
  877. asize:=OT_BITS16
  878. else if (p^.flags and IF_SD)<>0 then
  879. asize:=OT_BITS32;
  880. if (p^.flags and IF_ARMASK)<>0 then
  881. begin
  882. siz[0]:=0;
  883. siz[1]:=0;
  884. siz[2]:=0;
  885. if (p^.flags and IF_AR0)<>0 then
  886. siz[0]:=asize
  887. else if (p^.flags and IF_AR1)<>0 then
  888. siz[1]:=asize
  889. else if (p^.flags and IF_AR2)<>0 then
  890. siz[2]:=asize;
  891. end
  892. else
  893. begin
  894. { we can leave because the size for all operands is forced to be
  895. the same
  896. but not if IF_SB IF_SW or IF_SD is set PM }
  897. if asize=-1 then
  898. exit;
  899. siz[0]:=asize;
  900. siz[1]:=asize;
  901. siz[2]:=asize;
  902. end;
  903. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  904. begin
  905. if (p^.flags and IF_SM2)<>0 then
  906. oprs:=2
  907. else
  908. oprs:=p^.ops;
  909. for i:=0 to oprs-1 do
  910. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  911. begin
  912. for j:=0 to oprs-1 do
  913. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  914. break;
  915. end;
  916. end
  917. else
  918. oprs:=2;
  919. { Check operand sizes }
  920. for i:=0 to p^.ops-1 do
  921. begin
  922. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  923. ((oper[i].ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  924. { Immediates can always include smaller size }
  925. ((oper[i].ot and OT_IMMEDIATE)=0) and
  926. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i].ot and OT_SIZE_MASK)) then
  927. Matches:=2;
  928. end;
  929. end;
  930. procedure taicpu.ResetPass1;
  931. begin
  932. { we need to reset everything here, because the choosen insentry
  933. can be invalid for a new situation where the previously optimized
  934. insentry is not correct }
  935. InsEntry:=nil;
  936. InsSize:=0;
  937. LastInsOffset:=-1;
  938. end;
  939. procedure taicpu.ResetPass2;
  940. begin
  941. { we are here in a second pass, check if the instruction can be optimized }
  942. if assigned(InsEntry) and
  943. ((InsEntry^.flags and IF_PASS2)<>0) then
  944. begin
  945. InsEntry:=nil;
  946. InsSize:=0;
  947. end;
  948. LastInsOffset:=-1;
  949. end;
  950. function taicpu.CheckIfValid:boolean;
  951. var
  952. m,i : longint;
  953. begin
  954. CheckIfValid:=false;
  955. { Things which may only be done once, not when a second pass is done to
  956. optimize }
  957. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  958. begin
  959. { We need intel style operands }
  960. SetOperandOrder(op_intel);
  961. { create the .ot fields }
  962. create_ot;
  963. { set the file postion }
  964. aktfilepos:=fileinfo;
  965. end
  966. else
  967. begin
  968. { we've already an insentry so it's valid }
  969. CheckIfValid:=true;
  970. exit;
  971. end;
  972. { Lookup opcode in the table }
  973. InsSize:=-1;
  974. i:=instabcache^[opcode];
  975. if i=-1 then
  976. begin
  977. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  978. exit;
  979. end;
  980. insentry:=@instab[i];
  981. while (insentry^.opcode=opcode) do
  982. begin
  983. m:=matches(insentry);
  984. if m=100 then
  985. begin
  986. InsSize:=calcsize(insentry);
  987. if not((segprefix.enum=R_NO) or ((segprefix.enum=R_INTREGISTER) and (segprefix.number=NR_NO))) then
  988. inc(InsSize);
  989. { For opsize if size if forced }
  990. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  991. begin
  992. if (insentry^.flags and IF_ARMASK)=0 then
  993. begin
  994. if (insentry^.flags and IF_SB)<>0 then
  995. begin
  996. if opsize=S_NO then
  997. opsize:=S_B;
  998. end
  999. else if (insentry^.flags and IF_SW)<>0 then
  1000. begin
  1001. if opsize=S_NO then
  1002. opsize:=S_W;
  1003. end
  1004. else if (insentry^.flags and IF_SD)<>0 then
  1005. begin
  1006. if opsize=S_NO then
  1007. opsize:=S_L;
  1008. end;
  1009. end;
  1010. end;
  1011. CheckIfValid:=true;
  1012. exit;
  1013. end;
  1014. inc(i);
  1015. insentry:=@instab[i];
  1016. end;
  1017. if insentry^.opcode<>opcode then
  1018. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1019. { No instruction found, set insentry to nil and inssize to -1 }
  1020. insentry:=nil;
  1021. inssize:=-1;
  1022. end;
  1023. function taicpu.Pass1(offset:longint):longint;
  1024. begin
  1025. Pass1:=0;
  1026. { Save the old offset and set the new offset }
  1027. InsOffset:=Offset;
  1028. { Things which may only be done once, not when a second pass is done to
  1029. optimize }
  1030. if Insentry=nil then
  1031. begin
  1032. { Check if error last time then InsSize=-1 }
  1033. if InsSize=-1 then
  1034. exit;
  1035. { set the file postion }
  1036. aktfilepos:=fileinfo;
  1037. end
  1038. else
  1039. begin
  1040. {$ifdef PASS2FLAG}
  1041. { we are here in a second pass, check if the instruction can be optimized }
  1042. if (InsEntry^.flags and IF_PASS2)=0 then
  1043. begin
  1044. Pass1:=InsSize;
  1045. exit;
  1046. end;
  1047. { update the .ot fields, some top_const can be updated }
  1048. create_ot;
  1049. {$endif PASS2FLAG}
  1050. end;
  1051. { Check if it's a valid instruction }
  1052. if CheckIfValid then
  1053. begin
  1054. LastInsOffset:=InsOffset;
  1055. Pass1:=InsSize;
  1056. exit;
  1057. end;
  1058. LastInsOffset:=-1;
  1059. end;
  1060. procedure taicpu.Pass2(sec:TAsmObjectData);
  1061. var
  1062. c : longint;
  1063. begin
  1064. { error in pass1 ? }
  1065. if insentry=nil then
  1066. exit;
  1067. aktfilepos:=fileinfo;
  1068. { Segment override }
  1069. if segprefix.enum>lastreg then
  1070. internalerror(200201081);
  1071. if (segprefix.enum<>R_NO) then
  1072. begin
  1073. case segprefix.enum of
  1074. R_CS : c:=$2e;
  1075. R_DS : c:=$3e;
  1076. R_ES : c:=$26;
  1077. R_FS : c:=$64;
  1078. R_GS : c:=$65;
  1079. R_SS : c:=$36;
  1080. end;
  1081. sec.writebytes(c,1);
  1082. { fix the offset for GenNode }
  1083. inc(InsOffset);
  1084. end;
  1085. { Generate the instruction }
  1086. GenCode(sec);
  1087. end;
  1088. function taicpu.needaddrprefix(opidx:byte):boolean;
  1089. var i,b:Tnewregister;
  1090. ia,ba:boolean;
  1091. begin
  1092. needaddrprefix:=false;
  1093. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  1094. begin
  1095. if oper[opidx].ref^.index.enum=R_INTREGISTER then
  1096. begin
  1097. i:=oper[opidx].ref^.index.number;
  1098. ia:=(i<>NR_NO) and (i and $ff<>R_SUBD);
  1099. end
  1100. else
  1101. ia:=not(oper[opidx].ref^.index.enum in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]);
  1102. if oper[opidx].ref^.base.enum=R_INTREGISTER then
  1103. begin
  1104. b:=oper[opidx].ref^.base.number;
  1105. ba:=(b<>NR_NO) and (b and $ff<>R_SUBD);
  1106. end
  1107. else
  1108. ba:=not(oper[opidx].ref^.base.enum in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]);
  1109. b:=oper[opidx].ref^.base.number;
  1110. i:=oper[opidx].ref^.index.number;
  1111. if ia or ba then
  1112. needaddrprefix:=true;
  1113. end;
  1114. end;
  1115. function regval(r:tregister):byte;
  1116. begin
  1117. case r.enum of
  1118. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0 :
  1119. regval:=0;
  1120. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1 :
  1121. regval:=1;
  1122. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2 :
  1123. regval:=2;
  1124. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3 :
  1125. regval:=3;
  1126. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4 :
  1127. regval:=4;
  1128. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5 :
  1129. regval:=5;
  1130. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6 :
  1131. regval:=6;
  1132. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7 :
  1133. regval:=7;
  1134. else
  1135. begin
  1136. internalerror(777001);
  1137. regval:=0;
  1138. end;
  1139. end;
  1140. end;
  1141. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1142. const
  1143. regs : array[0..63] of Toldregister=(
  1144. R_MM0, R_EAX, R_AX, R_AL, R_XMM0, R_NO, R_NO, R_NO,
  1145. R_MM1, R_ECX, R_CX, R_CL, R_XMM1, R_NO, R_NO, R_NO,
  1146. R_MM2, R_EDX, R_DX, R_DL, R_XMM2, R_NO, R_NO, R_NO,
  1147. R_MM3, R_EBX, R_BX, R_BL, R_XMM3, R_NO, R_NO, R_NO,
  1148. R_MM4, R_ESP, R_SP, R_AH, R_XMM4, R_NO, R_NO, R_NO,
  1149. R_MM5, R_EBP, R_BP, R_CH, R_XMM5, R_NO, R_NO, R_NO,
  1150. R_MM6, R_ESI, R_SI, R_DH, R_XMM6, R_NO, R_NO, R_NO,
  1151. R_MM7, R_EDI, R_DI, R_BH, R_XMM7, R_NO, R_NO, R_NO
  1152. );
  1153. var
  1154. j : longint;
  1155. i,b : Toldregister;
  1156. sym : tasmsymbol;
  1157. md,s : byte;
  1158. base,index,scalefactor,
  1159. o : longint;
  1160. ireg : Tregister;
  1161. ir,br : Tregister;
  1162. begin
  1163. process_ea:=false;
  1164. { register ? }
  1165. if (input.typ=top_reg) then
  1166. begin
  1167. ireg:=input.reg;
  1168. convert_register_to_enum(ireg);
  1169. j:=0;
  1170. while (j<=high(regs)) do
  1171. begin
  1172. if ireg.enum=regs[j] then
  1173. break;
  1174. inc(j);
  1175. end;
  1176. if j<=high(regs) then
  1177. begin
  1178. output.sib_present:=false;
  1179. output.bytes:=0;
  1180. output.modrm:=$c0 or (rfield shl 3) or (j shr 3);
  1181. output.size:=1;
  1182. process_ea:=true;
  1183. end;
  1184. exit;
  1185. end;
  1186. { memory reference }
  1187. ir:=input.ref^.index;
  1188. br:=input.ref^.base;
  1189. convert_register_to_enum(ir);
  1190. convert_register_to_enum(br);
  1191. i:=ir.enum;
  1192. b:=br.enum;
  1193. if (i>lastreg) or (b>lastreg) then
  1194. internalerror(200301081);
  1195. s:=input.ref^.scalefactor;
  1196. o:=input.ref^.offset+input.ref^.offsetfixup;
  1197. sym:=input.ref^.symbol;
  1198. { it's direct address }
  1199. if (b=R_NO) and (i=R_NO) then
  1200. begin
  1201. { it's a pure offset }
  1202. output.sib_present:=false;
  1203. output.bytes:=4;
  1204. output.modrm:=5 or (rfield shl 3);
  1205. end
  1206. else
  1207. { it's an indirection }
  1208. begin
  1209. { 16 bit address? }
  1210. if not((i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  1211. (b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  1212. Message(asmw_e_16bit_not_supported);
  1213. {$ifdef OPTEA}
  1214. { make single reg base }
  1215. if (b=R_NO) and (s=1) then
  1216. begin
  1217. b:=i;
  1218. i:=R_NO;
  1219. end;
  1220. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1221. if (b=R_NO) and
  1222. (((s=2) and (i<>R_ESP)) or
  1223. (s=3) or (s=5) or (s=9)) then
  1224. begin
  1225. b:=i;
  1226. dec(s);
  1227. end;
  1228. { swap ESP into base if scalefactor is 1 }
  1229. if (s=1) and (i=R_ESP) then
  1230. begin
  1231. i:=b;
  1232. b:=R_ESP;
  1233. end;
  1234. {$endif OPTEA}
  1235. { wrong, for various reasons }
  1236. if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NO)) then
  1237. exit;
  1238. { base }
  1239. case b of
  1240. R_EAX : base:=0;
  1241. R_ECX : base:=1;
  1242. R_EDX : base:=2;
  1243. R_EBX : base:=3;
  1244. R_ESP : base:=4;
  1245. R_NO,
  1246. R_EBP : base:=5;
  1247. R_ESI : base:=6;
  1248. R_EDI : base:=7;
  1249. else
  1250. exit;
  1251. end;
  1252. { index }
  1253. case i of
  1254. R_EAX : index:=0;
  1255. R_ECX : index:=1;
  1256. R_EDX : index:=2;
  1257. R_EBX : index:=3;
  1258. R_NO : index:=4;
  1259. R_EBP : index:=5;
  1260. R_ESI : index:=6;
  1261. R_EDI : index:=7;
  1262. else
  1263. exit;
  1264. end;
  1265. case s of
  1266. 0,
  1267. 1 : scalefactor:=0;
  1268. 2 : scalefactor:=1;
  1269. 4 : scalefactor:=2;
  1270. 8 : scalefactor:=3;
  1271. else
  1272. exit;
  1273. end;
  1274. if (b=R_NO) or
  1275. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  1276. md:=0
  1277. else
  1278. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1279. md:=1
  1280. else
  1281. md:=2;
  1282. if (b=R_NO) or (md=2) then
  1283. output.bytes:=4
  1284. else
  1285. output.bytes:=md;
  1286. { SIB needed ? }
  1287. if (i=R_NO) and (b<>R_ESP) then
  1288. begin
  1289. output.sib_present:=false;
  1290. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1291. end
  1292. else
  1293. begin
  1294. output.sib_present:=true;
  1295. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1296. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1297. end;
  1298. end;
  1299. if output.sib_present then
  1300. output.size:=2+output.bytes
  1301. else
  1302. output.size:=1+output.bytes;
  1303. process_ea:=true;
  1304. end;
  1305. function taicpu.calcsize(p:PInsEntry):longint;
  1306. var
  1307. codes : pchar;
  1308. c : byte;
  1309. len : longint;
  1310. ea_data : ea;
  1311. begin
  1312. len:=0;
  1313. codes:=@p^.code;
  1314. repeat
  1315. c:=ord(codes^);
  1316. inc(codes);
  1317. case c of
  1318. 0 :
  1319. break;
  1320. 1,2,3 :
  1321. begin
  1322. inc(codes,c);
  1323. inc(len,c);
  1324. end;
  1325. 8,9,10 :
  1326. begin
  1327. inc(codes);
  1328. inc(len);
  1329. end;
  1330. 4,5,6,7 :
  1331. begin
  1332. if opsize=S_W then
  1333. inc(len,2)
  1334. else
  1335. inc(len);
  1336. end;
  1337. 15,
  1338. 12,13,14,
  1339. 16,17,18,
  1340. 20,21,22,
  1341. 40,41,42 :
  1342. inc(len);
  1343. 24,25,26,
  1344. 31,
  1345. 48,49,50 :
  1346. inc(len,2);
  1347. 28,29,30, { we don't have 16 bit immediates code }
  1348. 32,33,34,
  1349. 52,53,54,
  1350. 56,57,58 :
  1351. inc(len,4);
  1352. 192,193,194 :
  1353. if NeedAddrPrefix(c-192) then
  1354. inc(len);
  1355. 208 :
  1356. inc(len);
  1357. 200,
  1358. 201,
  1359. 202,
  1360. 209,
  1361. 210,
  1362. 217,218,219 : ;
  1363. 216 :
  1364. begin
  1365. inc(codes);
  1366. inc(len);
  1367. end;
  1368. 224,225,226 :
  1369. begin
  1370. InternalError(777002);
  1371. end;
  1372. else
  1373. begin
  1374. if (c>=64) and (c<=191) then
  1375. begin
  1376. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1377. Message(asmw_e_invalid_effective_address)
  1378. else
  1379. inc(len,ea_data.size);
  1380. end
  1381. else
  1382. InternalError(777003);
  1383. end;
  1384. end;
  1385. until false;
  1386. calcsize:=len;
  1387. end;
  1388. procedure taicpu.GenCode(sec:TAsmObjectData);
  1389. {
  1390. * the actual codes (C syntax, i.e. octal):
  1391. * \0 - terminates the code. (Unless it's a literal of course.)
  1392. * \1, \2, \3 - that many literal bytes follow in the code stream
  1393. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1394. * (POP is never used for CS) depending on operand 0
  1395. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1396. * on operand 0
  1397. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1398. * to the register value of operand 0, 1 or 2
  1399. * \17 - encodes the literal byte 0. (Some compilers don't take
  1400. * kindly to a zero byte in the _middle_ of a compile time
  1401. * string constant, so I had to put this hack in.)
  1402. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1403. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1404. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1405. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1406. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1407. * assembly mode or the address-size override on the operand
  1408. * \37 - a word constant, from the _segment_ part of operand 0
  1409. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1410. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1411. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1412. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1413. * assembly mode or the address-size override on the operand
  1414. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1415. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1416. * field the register value of operand b.
  1417. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1418. * field equal to digit b.
  1419. * \30x - might be an 0x67 byte, depending on the address size of
  1420. * the memory reference in operand x.
  1421. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1422. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1423. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1424. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1425. * \322 - indicates that this instruction is only valid when the
  1426. * operand size is the default (instruction to disassembler,
  1427. * generates no code in the assembler)
  1428. * \330 - a literal byte follows in the code stream, to be added
  1429. * to the condition code value of the instruction.
  1430. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1431. * Operand 0 had better be a segmentless constant.
  1432. }
  1433. var
  1434. currval : longint;
  1435. currsym : tasmsymbol;
  1436. procedure getvalsym(opidx:longint);
  1437. begin
  1438. case oper[opidx].typ of
  1439. top_ref :
  1440. begin
  1441. currval:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1442. currsym:=oper[opidx].ref^.symbol;
  1443. end;
  1444. top_const :
  1445. begin
  1446. currval:=longint(oper[opidx].val);
  1447. currsym:=nil;
  1448. end;
  1449. top_symbol :
  1450. begin
  1451. currval:=oper[opidx].symofs;
  1452. currsym:=oper[opidx].sym;
  1453. end;
  1454. else
  1455. Message(asmw_e_immediate_or_reference_expected);
  1456. end;
  1457. end;
  1458. const
  1459. CondVal:array[TAsmCond] of byte=($0,
  1460. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1461. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1462. $0, $A, $A, $B, $8, $4);
  1463. var
  1464. c : byte;
  1465. pb,
  1466. codes : pchar;
  1467. bytes : array[0..3] of byte;
  1468. rfield,
  1469. data,s,opidx : longint;
  1470. ea_data : ea;
  1471. begin
  1472. {$ifdef EXTDEBUG}
  1473. { safety check }
  1474. if sec.sects[sec.currsec].datasize<>insoffset then
  1475. internalerror(200130121);
  1476. {$endif EXTDEBUG}
  1477. { load data to write }
  1478. codes:=insentry^.code;
  1479. { Force word push/pop for registers }
  1480. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1481. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1482. begin
  1483. bytes[0]:=$66;
  1484. sec.writebytes(bytes,1);
  1485. end;
  1486. repeat
  1487. c:=ord(codes^);
  1488. inc(codes);
  1489. case c of
  1490. 0 :
  1491. break;
  1492. 1,2,3 :
  1493. begin
  1494. sec.writebytes(codes^,c);
  1495. inc(codes,c);
  1496. end;
  1497. 4,6 :
  1498. begin
  1499. case oper[0].reg.enum of
  1500. R_CS :
  1501. begin
  1502. if c=4 then
  1503. bytes[0]:=$f
  1504. else
  1505. bytes[0]:=$e;
  1506. end;
  1507. R_NO,
  1508. R_DS :
  1509. begin
  1510. if c=4 then
  1511. bytes[0]:=$1f
  1512. else
  1513. bytes[0]:=$1e;
  1514. end;
  1515. R_ES :
  1516. begin
  1517. if c=4 then
  1518. bytes[0]:=$7
  1519. else
  1520. bytes[0]:=$6;
  1521. end;
  1522. R_SS :
  1523. begin
  1524. if c=4 then
  1525. bytes[0]:=$17
  1526. else
  1527. bytes[0]:=$16;
  1528. end;
  1529. else
  1530. InternalError(777004);
  1531. end;
  1532. sec.writebytes(bytes,1);
  1533. end;
  1534. 5,7 :
  1535. begin
  1536. case oper[0].reg.enum of
  1537. R_FS :
  1538. begin
  1539. if c=5 then
  1540. bytes[0]:=$a1
  1541. else
  1542. bytes[0]:=$a0;
  1543. end;
  1544. R_GS :
  1545. begin
  1546. if c=5 then
  1547. bytes[0]:=$a9
  1548. else
  1549. bytes[0]:=$a8;
  1550. end;
  1551. else
  1552. InternalError(777005);
  1553. end;
  1554. sec.writebytes(bytes,1);
  1555. end;
  1556. 8,9,10 :
  1557. begin
  1558. bytes[0]:=ord(codes^)+regval(oper[c-8].reg);
  1559. inc(codes);
  1560. sec.writebytes(bytes,1);
  1561. end;
  1562. 15 :
  1563. begin
  1564. bytes[0]:=0;
  1565. sec.writebytes(bytes,1);
  1566. end;
  1567. 12,13,14 :
  1568. begin
  1569. getvalsym(c-12);
  1570. if (currval<-128) or (currval>127) then
  1571. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1572. if assigned(currsym) then
  1573. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1574. else
  1575. sec.writebytes(currval,1);
  1576. end;
  1577. 16,17,18 :
  1578. begin
  1579. getvalsym(c-16);
  1580. if (currval<-256) or (currval>255) then
  1581. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1582. if assigned(currsym) then
  1583. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1584. else
  1585. sec.writebytes(currval,1);
  1586. end;
  1587. 20,21,22 :
  1588. begin
  1589. getvalsym(c-20);
  1590. if (currval<0) or (currval>255) then
  1591. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1592. if assigned(currsym) then
  1593. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1594. else
  1595. sec.writebytes(currval,1);
  1596. end;
  1597. 24,25,26 :
  1598. begin
  1599. getvalsym(c-24);
  1600. if (currval<-65536) or (currval>65535) then
  1601. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1602. if assigned(currsym) then
  1603. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1604. else
  1605. sec.writebytes(currval,2);
  1606. end;
  1607. 28,29,30 :
  1608. begin
  1609. getvalsym(c-28);
  1610. if assigned(currsym) then
  1611. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1612. else
  1613. sec.writebytes(currval,4);
  1614. end;
  1615. 32,33,34 :
  1616. begin
  1617. getvalsym(c-32);
  1618. if assigned(currsym) then
  1619. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1620. else
  1621. sec.writebytes(currval,4);
  1622. end;
  1623. 40,41,42 :
  1624. begin
  1625. getvalsym(c-40);
  1626. data:=currval-insend;
  1627. if assigned(currsym) then
  1628. inc(data,currsym.address);
  1629. if (data>127) or (data<-128) then
  1630. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1631. sec.writebytes(data,1);
  1632. end;
  1633. 52,53,54 :
  1634. begin
  1635. getvalsym(c-52);
  1636. if assigned(currsym) then
  1637. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1638. else
  1639. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1640. end;
  1641. 56,57,58 :
  1642. begin
  1643. getvalsym(c-56);
  1644. if assigned(currsym) then
  1645. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1646. else
  1647. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1648. end;
  1649. 192,193,194 :
  1650. begin
  1651. if NeedAddrPrefix(c-192) then
  1652. begin
  1653. bytes[0]:=$67;
  1654. sec.writebytes(bytes,1);
  1655. end;
  1656. end;
  1657. 200 :
  1658. begin
  1659. bytes[0]:=$67;
  1660. sec.writebytes(bytes,1);
  1661. end;
  1662. 208 :
  1663. begin
  1664. bytes[0]:=$66;
  1665. sec.writebytes(bytes,1);
  1666. end;
  1667. 216 :
  1668. begin
  1669. bytes[0]:=ord(codes^)+condval[condition];
  1670. inc(codes);
  1671. sec.writebytes(bytes,1);
  1672. end;
  1673. 201,
  1674. 202,
  1675. 209,
  1676. 210,
  1677. 217,218,219 :
  1678. begin
  1679. { these are dissambler hints or 32 bit prefixes which
  1680. are not needed }
  1681. end;
  1682. 31,
  1683. 48,49,50,
  1684. 224,225,226 :
  1685. begin
  1686. InternalError(777006);
  1687. end
  1688. else
  1689. begin
  1690. if (c>=64) and (c<=191) then
  1691. begin
  1692. if (c<127) then
  1693. begin
  1694. if (oper[c and 7].typ=top_reg) then
  1695. rfield:=regval(oper[c and 7].reg)
  1696. else
  1697. rfield:=regval(oper[c and 7].ref^.base);
  1698. end
  1699. else
  1700. rfield:=c and 7;
  1701. opidx:=(c shr 3) and 7;
  1702. if not process_ea(oper[opidx], ea_data, rfield) then
  1703. Message(asmw_e_invalid_effective_address);
  1704. pb:=@bytes;
  1705. pb^:=chr(ea_data.modrm);
  1706. inc(pb);
  1707. if ea_data.sib_present then
  1708. begin
  1709. pb^:=chr(ea_data.sib);
  1710. inc(pb);
  1711. end;
  1712. s:=pb-pchar(@bytes);
  1713. sec.writebytes(bytes,s);
  1714. case ea_data.bytes of
  1715. 0 : ;
  1716. 1 :
  1717. begin
  1718. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1719. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,1,oper[opidx].ref^.symbol,RELOC_ABSOLUTE)
  1720. else
  1721. begin
  1722. bytes[0]:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1723. sec.writebytes(bytes,1);
  1724. end;
  1725. inc(s);
  1726. end;
  1727. 2,4 :
  1728. begin
  1729. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,ea_data.bytes,
  1730. oper[opidx].ref^.symbol,RELOC_ABSOLUTE);
  1731. inc(s,ea_data.bytes);
  1732. end;
  1733. end;
  1734. end
  1735. else
  1736. InternalError(777007);
  1737. end;
  1738. end;
  1739. until false;
  1740. end;
  1741. {$endif NOAG386BIN}
  1742. {*****************************************************************************
  1743. Instruction table
  1744. *****************************************************************************}
  1745. procedure BuildInsTabCache;
  1746. {$ifndef NOAG386BIN}
  1747. var
  1748. i : longint;
  1749. {$endif}
  1750. begin
  1751. {$ifndef NOAG386BIN}
  1752. new(instabcache);
  1753. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1754. i:=0;
  1755. while (i<InsTabEntries) do
  1756. begin
  1757. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1758. InsTabCache^[InsTab[i].OPcode]:=i;
  1759. inc(i);
  1760. end;
  1761. {$endif NOAG386BIN}
  1762. end;
  1763. procedure InitAsm;
  1764. begin
  1765. {$ifndef NOAG386BIN}
  1766. if not assigned(instabcache) then
  1767. BuildInsTabCache;
  1768. {$endif NOAG386BIN}
  1769. end;
  1770. procedure DoneAsm;
  1771. begin
  1772. {$ifndef NOAG386BIN}
  1773. if assigned(instabcache) then
  1774. begin
  1775. dispose(instabcache);
  1776. instabcache:=nil;
  1777. end;
  1778. {$endif NOAG386BIN}
  1779. end;
  1780. end.
  1781. {
  1782. $Log$
  1783. Revision 1.15 2003-03-26 12:50:54 armin
  1784. * avoid problems with the ide in init/dome
  1785. Revision 1.14 2003/03/08 08:59:07 daniel
  1786. + $define newra will enable new register allocator
  1787. + getregisterint will return imaginary registers with $newra
  1788. + -sr switch added, will skip register allocation so you can see
  1789. the direct output of the code generator before register allocation
  1790. Revision 1.13 2003/02/25 07:41:54 daniel
  1791. * Properly fixed reversed operands bug
  1792. Revision 1.12 2003/02/19 22:00:15 daniel
  1793. * Code generator converted to new register notation
  1794. - Horribily outdated todo.txt removed
  1795. Revision 1.11 2003/01/09 20:40:59 daniel
  1796. * Converted some code in cgx86.pas to new register numbering
  1797. Revision 1.10 2003/01/08 18:43:57 daniel
  1798. * Tregister changed into a record
  1799. Revision 1.9 2003/01/05 13:36:53 florian
  1800. * x86-64 compiles
  1801. + very basic support for float128 type (x86-64 only)
  1802. Revision 1.8 2002/11/17 16:31:58 carl
  1803. * memory optimization (3-4%) : cleanup of tai fields,
  1804. cleanup of tdef and tsym fields.
  1805. * make it work for m68k
  1806. Revision 1.7 2002/11/15 01:58:54 peter
  1807. * merged changes from 1.0.7 up to 04-11
  1808. - -V option for generating bug report tracing
  1809. - more tracing for option parsing
  1810. - errors for cdecl and high()
  1811. - win32 import stabs
  1812. - win32 records<=8 are returned in eax:edx (turned off by default)
  1813. - heaptrc update
  1814. - more info for temp management in .s file with EXTDEBUG
  1815. Revision 1.6 2002/10/31 13:28:32 pierre
  1816. * correct last wrong fix for tw2158
  1817. Revision 1.5 2002/10/30 17:10:00 pierre
  1818. * merge of fix for tw2158 bug
  1819. Revision 1.4 2002/08/15 19:10:36 peter
  1820. * first things tai,tnode storing in ppu
  1821. Revision 1.3 2002/08/13 18:01:52 carl
  1822. * rename swatoperands to swapoperands
  1823. + m68k first compilable version (still needs a lot of testing):
  1824. assembler generator, system information , inline
  1825. assembler reader.
  1826. Revision 1.2 2002/07/20 11:57:59 florian
  1827. * types.pas renamed to defbase.pas because D6 contains a types
  1828. unit so this would conflicts if D6 programms are compiled
  1829. + Willamette/SSE2 instructions to assembler added
  1830. Revision 1.1 2002/07/01 18:46:29 peter
  1831. * internal linker
  1832. * reorganized aasm layer
  1833. }