aasmcpu.pas 63 KB

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