aasmcpu.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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. cgbase,
  28. symtype,symsym,
  29. aasmbase,aasmtai;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  38. OT_BITS16 = $00000002;
  39. OT_BITS32 = $00000004;
  40. OT_BITS64 = $00000008; { FPU only }
  41. OT_BITS80 = $00000010;
  42. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  43. OT_NEAR = $00000040;
  44. OT_SHORT = $00000080;
  45. OT_SIZE_MASK = $000000FF; { all the size attributes }
  46. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  47. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  48. OT_TO = $00000200; { operand is followed by a colon }
  49. { reverse effect in FADD, FSUB &c }
  50. OT_COLON = $00000400;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_IMM8 = $00002001;
  54. OT_IMM16 = $00002002;
  55. OT_IMM32 = $00002004;
  56. OT_IMM64 = $00002008;
  57. OT_IMM80 = $00002010;
  58. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  59. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  60. OT_REG8 = $00201001;
  61. OT_REG16 = $00201002;
  62. OT_REG32 = $00201004;
  63. OT_REG64 = $00201008;
  64. OT_MMXREG = $00201008; { MMX registers }
  65. OT_XMMREG = $00201010; { Katmai registers }
  66. OT_MEMORY = $00204000; { register number in 'basereg' }
  67. OT_MEM8 = $00204001;
  68. OT_MEM16 = $00204002;
  69. OT_MEM32 = $00204004;
  70. OT_MEM64 = $00204008;
  71. OT_MEM80 = $00204010;
  72. OT_FPUREG = $01000000; { floating point stack registers }
  73. OT_FPU0 = $01000800; { FPU stack register zero }
  74. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  75. { a mask for the following }
  76. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  77. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  78. OT_REG_AX = $00211002; { ditto }
  79. OT_REG_EAX = $00211004; { and again }
  80. {$ifdef x86_64}
  81. OT_REG_RAX = $00211008;
  82. {$endif x86_64}
  83. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  84. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  85. OT_REG_CX = $00221002; { ditto }
  86. OT_REG_ECX = $00221004; { another one }
  87. {$ifdef x86_64}
  88. OT_REG_RCX = $00221008;
  89. {$endif x86_64}
  90. OT_REG_DX = $00241002;
  91. OT_REG_EDX = $00241004;
  92. OT_REG_SREG = $00081002; { any segment register }
  93. OT_REG_CS = $01081002; { CS }
  94. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  95. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  96. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  97. OT_REG_CREG = $08101004; { CRn }
  98. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  99. OT_REG_DREG = $10101004; { DRn }
  100. OT_REG_TREG = $20101004; { TRn }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. { Size of the instruction table converted by nasmconv.pas }
  107. {$ifdef x86_64}
  108. instabentries = {$i x8664nop.inc}
  109. {$else x86_64}
  110. instabentries = {$i i386nop.inc}
  111. {$endif x86_64}
  112. maxinfolen = 8;
  113. type
  114. TOperandOrder = (op_intel,op_att);
  115. tinsentry=packed record
  116. opcode : tasmop;
  117. ops : byte;
  118. optypes : array[0..2] of longint;
  119. code : array[0..maxinfolen] of char;
  120. flags : longint;
  121. end;
  122. pinsentry=^tinsentry;
  123. { alignment for operator }
  124. tai_align = class(tai_align_abstract)
  125. reg : tregister;
  126. constructor create(b:byte);
  127. constructor create_op(b: byte; _op: byte);
  128. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  129. end;
  130. taicpu = class(taicpu_abstract)
  131. opsize : topsize;
  132. constructor op_none(op : tasmop);
  133. constructor op_none(op : tasmop;_size : topsize);
  134. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  135. constructor op_const(op : tasmop;_size : topsize;_op1 : aword);
  136. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  137. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  138. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  139. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  140. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  141. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  142. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  143. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  144. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  145. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  146. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  147. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  148. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  149. { this is for Jmp instructions }
  150. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  151. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  152. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  153. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  154. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  155. procedure changeopsize(siz:topsize);
  156. function GetString:string;
  157. procedure CheckNonCommutativeOpcodes;
  158. private
  159. FOperandOrder : TOperandOrder;
  160. procedure init(_size : topsize); { this need to be called by all constructor }
  161. {$ifndef NOAG386BIN}
  162. public
  163. { the next will reset all instructions that can change in pass 2 }
  164. procedure ResetPass1;
  165. procedure ResetPass2;
  166. function CheckIfValid:boolean;
  167. function Pass1(offset:longint):longint;virtual;
  168. procedure Pass2(sec:TAsmObjectdata);virtual;
  169. procedure SetOperandOrder(order:TOperandOrder);
  170. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  171. protected
  172. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  173. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  174. procedure ppubuildderefimploper(var o:toper);override;
  175. procedure ppuderefoper(var o:toper);override;
  176. private
  177. { next fields are filled in pass1, so pass2 is faster }
  178. inssize : shortint;
  179. insoffset : longint;
  180. LastInsOffset : longint; { need to be public to be reset }
  181. insentry : PInsEntry;
  182. function InsEnd:longint;
  183. procedure create_ot;
  184. function Matches(p:PInsEntry):longint;
  185. function calcsize(p:PInsEntry):longint;
  186. procedure gencode(sec:TAsmObjectData);
  187. function NeedAddrPrefix(opidx:byte):boolean;
  188. procedure Swapoperands;
  189. function FindInsentry:boolean;
  190. {$endif NOAG386BIN}
  191. end;
  192. procedure InitAsm;
  193. procedure DoneAsm;
  194. implementation
  195. uses
  196. cutils,
  197. itcpugas;
  198. {*****************************************************************************
  199. Instruction table
  200. *****************************************************************************}
  201. const
  202. {Instruction flags }
  203. IF_NONE = $00000000;
  204. IF_SM = $00000001; { size match first two operands }
  205. IF_SM2 = $00000002;
  206. IF_SB = $00000004; { unsized operands can't be non-byte }
  207. IF_SW = $00000008; { unsized operands can't be non-word }
  208. IF_SD = $00000010; { unsized operands can't be nondword }
  209. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  210. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  211. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  212. IF_ARMASK = $00000060; { mask for unsized argument spec }
  213. IF_PRIV = $00000100; { it's a privileged instruction }
  214. IF_SMM = $00000200; { it's only valid in SMM }
  215. IF_PROT = $00000400; { it's protected mode only }
  216. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  217. IF_UNDOC = $00001000; { it's an undocumented instruction }
  218. IF_FPU = $00002000; { it's an FPU instruction }
  219. IF_MMX = $00004000; { it's an MMX instruction }
  220. { it's a 3DNow! instruction }
  221. IF_3DNOW = $00008000;
  222. { it's a SSE (KNI, MMX2) instruction }
  223. IF_SSE = $00010000;
  224. { SSE2 instructions }
  225. IF_SSE2 = $00020000;
  226. { SSE3 instructions }
  227. IF_SSE3 = $00040000;
  228. { SSE64 instructions }
  229. IF_SSE64 = $00080000;
  230. { the mask for processor types }
  231. {IF_PMASK = longint($FF000000);}
  232. { the mask for disassembly "prefer" }
  233. {IF_PFMASK = longint($F001FF00);}
  234. IF_8086 = $00000000; { 8086 instruction }
  235. IF_186 = $01000000; { 186+ instruction }
  236. IF_286 = $02000000; { 286+ instruction }
  237. IF_386 = $03000000; { 386+ instruction }
  238. IF_486 = $04000000; { 486+ instruction }
  239. IF_PENT = $05000000; { Pentium instruction }
  240. IF_P6 = $06000000; { P6 instruction }
  241. IF_KATMAI = $07000000; { Katmai instructions }
  242. { Willamette instructions }
  243. IF_WILLAMETTE = $08000000;
  244. { Prescott instructions }
  245. IF_PRESCOTT = $09000000;
  246. IF_X86_64 = $0a000000;
  247. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  248. IF_AMD = $20000000; { AMD-specific instruction }
  249. { added flags }
  250. IF_PRE = $40000000; { it's a prefix instruction }
  251. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  252. type
  253. TInsTabCache=array[TasmOp] of longint;
  254. PInsTabCache=^TInsTabCache;
  255. const
  256. {$ifdef x86_64}
  257. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  258. {$else x86_64}
  259. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  260. {$endif x86_64}
  261. var
  262. InsTabCache : PInsTabCache;
  263. const
  264. {$ifdef x86_64}
  265. { Intel style operands ! }
  266. opsize_2_type:array[0..2,topsize] of longint=(
  267. (OT_NONE,
  268. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  269. OT_BITS16,OT_BITS32,OT_BITS64,
  270. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  271. OT_BITS64,
  272. OT_NEAR,OT_FAR,OT_SHORT
  273. ),
  274. (OT_NONE,
  275. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  276. OT_BITS16,OT_BITS32,OT_BITS64,
  277. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  278. OT_BITS64,
  279. OT_NEAR,OT_FAR,OT_SHORT
  280. ),
  281. (OT_NONE,
  282. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  283. OT_BITS16,OT_BITS32,OT_BITS64,
  284. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  285. OT_BITS64,
  286. OT_NEAR,OT_FAR,OT_SHORT
  287. )
  288. );
  289. reg_ot_table : array[tregisterindex] of longint = (
  290. {$i r8664ot.inc}
  291. );
  292. {$else x86_64}
  293. { Intel style operands ! }
  294. opsize_2_type:array[0..2,topsize] of longint=(
  295. (OT_NONE,
  296. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  297. OT_BITS16,OT_BITS32,OT_BITS64,
  298. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  299. OT_BITS64,
  300. OT_NEAR,OT_FAR,OT_SHORT
  301. ),
  302. (OT_NONE,
  303. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  304. OT_BITS16,OT_BITS32,OT_BITS64,
  305. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  306. OT_BITS64,
  307. OT_NEAR,OT_FAR,OT_SHORT
  308. ),
  309. (OT_NONE,
  310. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  311. OT_BITS16,OT_BITS32,OT_BITS64,
  312. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  313. OT_BITS64,
  314. OT_NEAR,OT_FAR,OT_SHORT
  315. )
  316. );
  317. reg_ot_table : array[tregisterindex] of longint = (
  318. {$i r386ot.inc}
  319. );
  320. {$endif x86_64}
  321. {****************************************************************************
  322. TAI_ALIGN
  323. ****************************************************************************}
  324. constructor tai_align.create(b: byte);
  325. begin
  326. inherited create(b);
  327. reg:=NR_ECX;
  328. end;
  329. constructor tai_align.create_op(b: byte; _op: byte);
  330. begin
  331. inherited create_op(b,_op);
  332. reg:=NR_NO;
  333. end;
  334. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  335. const
  336. alignarray:array[0..5] of string[8]=(
  337. #$8D#$B4#$26#$00#$00#$00#$00,
  338. #$8D#$B6#$00#$00#$00#$00,
  339. #$8D#$74#$26#$00,
  340. #$8D#$76#$00,
  341. #$89#$F6,
  342. #$90
  343. );
  344. var
  345. bufptr : pchar;
  346. j : longint;
  347. begin
  348. inherited calculatefillbuf(buf);
  349. if not use_op then
  350. begin
  351. bufptr:=pchar(@buf);
  352. while (fillsize>0) do
  353. begin
  354. for j:=0 to 5 do
  355. if (fillsize>=length(alignarray[j])) then
  356. break;
  357. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  358. inc(bufptr,length(alignarray[j]));
  359. dec(fillsize,length(alignarray[j]));
  360. end;
  361. end;
  362. calculatefillbuf:=pchar(@buf);
  363. end;
  364. {*****************************************************************************
  365. Taicpu Constructors
  366. *****************************************************************************}
  367. procedure taicpu.changeopsize(siz:topsize);
  368. begin
  369. opsize:=siz;
  370. end;
  371. procedure taicpu.init(_size : topsize);
  372. begin
  373. { default order is att }
  374. FOperandOrder:=op_att;
  375. segprefix:=NR_NO;
  376. opsize:=_size;
  377. {$ifndef NOAG386BIN}
  378. insentry:=nil;
  379. LastInsOffset:=-1;
  380. InsOffset:=0;
  381. InsSize:=0;
  382. {$endif}
  383. end;
  384. constructor taicpu.op_none(op : tasmop);
  385. begin
  386. inherited create(op);
  387. init(S_NO);
  388. end;
  389. constructor taicpu.op_none(op : tasmop;_size : topsize);
  390. begin
  391. inherited create(op);
  392. init(_size);
  393. end;
  394. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  395. begin
  396. inherited create(op);
  397. init(_size);
  398. ops:=1;
  399. loadreg(0,_op1);
  400. end;
  401. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aword);
  402. begin
  403. inherited create(op);
  404. init(_size);
  405. ops:=1;
  406. loadconst(0,_op1);
  407. end;
  408. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  409. begin
  410. inherited create(op);
  411. init(_size);
  412. ops:=1;
  413. loadref(0,_op1);
  414. end;
  415. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  416. begin
  417. inherited create(op);
  418. init(_size);
  419. ops:=2;
  420. loadreg(0,_op1);
  421. loadreg(1,_op2);
  422. end;
  423. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  424. begin
  425. inherited create(op);
  426. init(_size);
  427. ops:=2;
  428. loadreg(0,_op1);
  429. loadconst(1,_op2);
  430. end;
  431. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  432. begin
  433. inherited create(op);
  434. init(_size);
  435. ops:=2;
  436. loadreg(0,_op1);
  437. loadref(1,_op2);
  438. end;
  439. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  440. begin
  441. inherited create(op);
  442. init(_size);
  443. ops:=2;
  444. loadconst(0,_op1);
  445. loadreg(1,_op2);
  446. end;
  447. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  448. begin
  449. inherited create(op);
  450. init(_size);
  451. ops:=2;
  452. loadconst(0,_op1);
  453. loadconst(1,_op2);
  454. end;
  455. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  456. begin
  457. inherited create(op);
  458. init(_size);
  459. ops:=2;
  460. loadconst(0,_op1);
  461. loadref(1,_op2);
  462. end;
  463. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  464. begin
  465. inherited create(op);
  466. init(_size);
  467. ops:=2;
  468. loadref(0,_op1);
  469. loadreg(1,_op2);
  470. end;
  471. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  472. begin
  473. inherited create(op);
  474. init(_size);
  475. ops:=3;
  476. loadreg(0,_op1);
  477. loadreg(1,_op2);
  478. loadreg(2,_op3);
  479. end;
  480. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  481. begin
  482. inherited create(op);
  483. init(_size);
  484. ops:=3;
  485. loadconst(0,_op1);
  486. loadreg(1,_op2);
  487. loadreg(2,_op3);
  488. end;
  489. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  490. begin
  491. inherited create(op);
  492. init(_size);
  493. ops:=3;
  494. loadreg(0,_op1);
  495. loadreg(1,_op2);
  496. loadref(2,_op3);
  497. end;
  498. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  499. begin
  500. inherited create(op);
  501. init(_size);
  502. ops:=3;
  503. loadconst(0,_op1);
  504. loadref(1,_op2);
  505. loadreg(2,_op3);
  506. end;
  507. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  508. begin
  509. inherited create(op);
  510. init(_size);
  511. ops:=3;
  512. loadconst(0,_op1);
  513. loadreg(1,_op2);
  514. loadref(2,_op3);
  515. end;
  516. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  517. begin
  518. inherited create(op);
  519. init(_size);
  520. condition:=cond;
  521. ops:=1;
  522. loadsymbol(0,_op1,0);
  523. end;
  524. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  525. begin
  526. inherited create(op);
  527. init(_size);
  528. ops:=1;
  529. loadsymbol(0,_op1,0);
  530. end;
  531. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  532. begin
  533. inherited create(op);
  534. init(_size);
  535. ops:=1;
  536. loadsymbol(0,_op1,_op1ofs);
  537. end;
  538. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  539. begin
  540. inherited create(op);
  541. init(_size);
  542. ops:=2;
  543. loadsymbol(0,_op1,_op1ofs);
  544. loadreg(1,_op2);
  545. end;
  546. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  547. begin
  548. inherited create(op);
  549. init(_size);
  550. ops:=2;
  551. loadsymbol(0,_op1,_op1ofs);
  552. loadref(1,_op2);
  553. end;
  554. function taicpu.GetString:string;
  555. var
  556. i : longint;
  557. s : string;
  558. addsize : boolean;
  559. begin
  560. s:='['+std_op2str[opcode];
  561. for i:=0 to ops-1 do
  562. begin
  563. with oper[i]^ do
  564. begin
  565. if i=0 then
  566. s:=s+' '
  567. else
  568. s:=s+',';
  569. { type }
  570. addsize:=false;
  571. if (ot and OT_XMMREG)=OT_XMMREG then
  572. s:=s+'xmmreg'
  573. else
  574. if (ot and OT_MMXREG)=OT_MMXREG then
  575. s:=s+'mmxreg'
  576. else
  577. if (ot and OT_FPUREG)=OT_FPUREG then
  578. s:=s+'fpureg'
  579. else
  580. if (ot and OT_REGISTER)=OT_REGISTER then
  581. begin
  582. s:=s+'reg';
  583. addsize:=true;
  584. end
  585. else
  586. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  587. begin
  588. s:=s+'imm';
  589. addsize:=true;
  590. end
  591. else
  592. if (ot and OT_MEMORY)=OT_MEMORY then
  593. begin
  594. s:=s+'mem';
  595. addsize:=true;
  596. end
  597. else
  598. s:=s+'???';
  599. { size }
  600. if addsize then
  601. begin
  602. if (ot and OT_BITS8)<>0 then
  603. s:=s+'8'
  604. else
  605. if (ot and OT_BITS16)<>0 then
  606. s:=s+'16'
  607. else
  608. if (ot and OT_BITS32)<>0 then
  609. s:=s+'32'
  610. else
  611. s:=s+'??';
  612. { signed }
  613. if (ot and OT_SIGNED)<>0 then
  614. s:=s+'s';
  615. end;
  616. end;
  617. end;
  618. GetString:=s+']';
  619. end;
  620. procedure taicpu.Swapoperands;
  621. var
  622. p : POper;
  623. begin
  624. { Fix the operands which are in AT&T style and we need them in Intel style }
  625. case ops of
  626. 2 : begin
  627. { 0,1 -> 1,0 }
  628. p:=oper[0];
  629. oper[0]:=oper[1];
  630. oper[1]:=p;
  631. end;
  632. 3 : begin
  633. { 0,1,2 -> 2,1,0 }
  634. p:=oper[0];
  635. oper[0]:=oper[2];
  636. oper[2]:=p;
  637. end;
  638. end;
  639. end;
  640. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  641. begin
  642. if FOperandOrder<>order then
  643. begin
  644. Swapoperands;
  645. FOperandOrder:=order;
  646. end;
  647. end;
  648. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  649. begin
  650. o.typ:=toptype(ppufile.getbyte);
  651. o.ot:=ppufile.getlongint;
  652. case o.typ of
  653. top_reg :
  654. ppufile.getdata(o.reg,sizeof(Tregister));
  655. top_ref :
  656. begin
  657. new(o.ref);
  658. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  659. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  660. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  661. o.ref^.scalefactor:=ppufile.getbyte;
  662. o.ref^.offset:=ppufile.getaint;
  663. o.ref^.symbol:=ppufile.getasmsymbol;
  664. o.ref^.relsymbol:=ppufile.getasmsymbol;
  665. end;
  666. top_const :
  667. o.val:=aword(ppufile.getaint);
  668. top_local :
  669. begin
  670. new(o.localoper);
  671. with o.localoper^ do
  672. begin
  673. ppufile.getderef(localsymderef);
  674. localsymofs:=ppufile.getaint;
  675. localindexreg:=tregister(ppufile.getlongint);
  676. localscale:=ppufile.getbyte;
  677. localgetoffset:=(ppufile.getbyte<>0);
  678. end;
  679. end;
  680. end;
  681. end;
  682. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  683. begin
  684. ppufile.putbyte(byte(o.typ));
  685. ppufile.putlongint(o.ot);
  686. case o.typ of
  687. top_reg :
  688. ppufile.putdata(o.reg,sizeof(Tregister));
  689. top_ref :
  690. begin
  691. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  692. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  693. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  694. ppufile.putbyte(o.ref^.scalefactor);
  695. ppufile.putaint(o.ref^.offset);
  696. ppufile.putasmsymbol(o.ref^.symbol);
  697. ppufile.putasmsymbol(o.ref^.relsymbol);
  698. end;
  699. top_const :
  700. ppufile.putaint(aint(o.val));
  701. top_local :
  702. begin
  703. with o.localoper^ do
  704. begin
  705. ppufile.putderef(localsymderef);
  706. ppufile.putaint(aint(localsymofs));
  707. ppufile.putlongint(longint(localindexreg));
  708. ppufile.putbyte(localscale);
  709. ppufile.putbyte(byte(localgetoffset));
  710. end;
  711. end;
  712. end;
  713. end;
  714. procedure taicpu.ppubuildderefimploper(var o:toper);
  715. begin
  716. case o.typ of
  717. top_local :
  718. o.localoper^.localsymderef.build(tvarsym(o.localoper^.localsym));
  719. end;
  720. end;
  721. procedure taicpu.ppuderefoper(var o:toper);
  722. begin
  723. case o.typ of
  724. top_ref :
  725. begin
  726. if assigned(o.ref^.symbol) then
  727. objectlibrary.derefasmsymbol(o.ref^.symbol);
  728. if assigned(o.ref^.relsymbol) then
  729. objectlibrary.derefasmsymbol(o.ref^.relsymbol);
  730. end;
  731. top_local :
  732. o.localoper^.localsym:=tvarsym(o.localoper^.localsymderef.resolve);
  733. end;
  734. end;
  735. procedure taicpu.CheckNonCommutativeOpcodes;
  736. begin
  737. { we need ATT order }
  738. SetOperandOrder(op_att);
  739. if (
  740. (ops=2) and
  741. (oper[0]^.typ=top_reg) and
  742. (oper[1]^.typ=top_reg) and
  743. { if the first is ST and the second is also a register
  744. it is necessarily ST1 .. ST7 }
  745. ((oper[0]^.reg=NR_ST) or
  746. (oper[0]^.reg=NR_ST0))
  747. ) or
  748. { ((ops=1) and
  749. (oper[0]^.typ=top_reg) and
  750. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  751. (ops=0) then
  752. begin
  753. if opcode=A_FSUBR then
  754. opcode:=A_FSUB
  755. else if opcode=A_FSUB then
  756. opcode:=A_FSUBR
  757. else if opcode=A_FDIVR then
  758. opcode:=A_FDIV
  759. else if opcode=A_FDIV then
  760. opcode:=A_FDIVR
  761. else if opcode=A_FSUBRP then
  762. opcode:=A_FSUBP
  763. else if opcode=A_FSUBP then
  764. opcode:=A_FSUBRP
  765. else if opcode=A_FDIVRP then
  766. opcode:=A_FDIVP
  767. else if opcode=A_FDIVP then
  768. opcode:=A_FDIVRP;
  769. end;
  770. if (
  771. (ops=1) and
  772. (oper[0]^.typ=top_reg) and
  773. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  774. (oper[0]^.reg<>NR_ST)
  775. ) then
  776. begin
  777. if opcode=A_FSUBRP then
  778. opcode:=A_FSUBP
  779. else if opcode=A_FSUBP then
  780. opcode:=A_FSUBRP
  781. else if opcode=A_FDIVRP then
  782. opcode:=A_FDIVP
  783. else if opcode=A_FDIVP then
  784. opcode:=A_FDIVRP;
  785. end;
  786. end;
  787. {*****************************************************************************
  788. Assembler
  789. *****************************************************************************}
  790. {$ifndef NOAG386BIN}
  791. type
  792. ea=packed record
  793. sib_present : boolean;
  794. bytes : byte;
  795. size : byte;
  796. modrm : byte;
  797. sib : byte;
  798. end;
  799. procedure taicpu.create_ot;
  800. {
  801. this function will also fix some other fields which only needs to be once
  802. }
  803. var
  804. i,l,relsize : longint;
  805. begin
  806. if ops=0 then
  807. exit;
  808. { update oper[].ot field }
  809. for i:=0 to ops-1 do
  810. with oper[i]^ do
  811. begin
  812. case typ of
  813. top_reg :
  814. begin
  815. ot:=reg_ot_table[findreg_by_number(reg)];
  816. end;
  817. top_ref :
  818. begin
  819. if ref^.refaddr=addr_no then
  820. begin
  821. { create ot field }
  822. if (ot and OT_SIZE_MASK)=0 then
  823. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  824. else
  825. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  826. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  827. ot:=ot or OT_MEM_OFFS;
  828. { fix scalefactor }
  829. if (ref^.index=NR_NO) then
  830. ref^.scalefactor:=0
  831. else
  832. if (ref^.scalefactor=0) then
  833. ref^.scalefactor:=1;
  834. end
  835. else
  836. begin
  837. l:=ref^.offset;
  838. if assigned(ref^.symbol) then
  839. inc(l,ref^.symbol.address);
  840. { when it is a forward jump we need to compensate the
  841. offset of the instruction since the previous time,
  842. because the symbol address is then still using the
  843. 'old-style' addressing.
  844. For backwards jumps this is not required because the
  845. address of the symbol is already adjusted to the
  846. new offset }
  847. if (l>InsOffset) and (LastInsOffset<>-1) then
  848. inc(l,InsOffset-LastInsOffset);
  849. { instruction size will then always become 2 (PFV) }
  850. relsize:=(InsOffset+2)-l;
  851. if (not assigned(ref^.symbol) or
  852. ((ref^.symbol.currbind<>AB_EXTERNAL) and (ref^.symbol.address<>0))) and
  853. (relsize>=-128) and (relsize<=127) then
  854. ot:=OT_IMM32 or OT_SHORT
  855. else
  856. ot:=OT_IMM32 or OT_NEAR;
  857. end;
  858. end;
  859. top_local :
  860. begin
  861. if (ot and OT_SIZE_MASK)=0 then
  862. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  863. else
  864. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  865. end;
  866. top_const :
  867. begin
  868. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  869. ot:=OT_IMM8 or OT_SIGNED
  870. else
  871. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  872. end;
  873. top_none :
  874. begin
  875. { generated when there was an error in the
  876. assembler reader. It never happends when generating
  877. assembler }
  878. end;
  879. else
  880. internalerror(200402261);
  881. end;
  882. end;
  883. end;
  884. function taicpu.InsEnd:longint;
  885. begin
  886. InsEnd:=InsOffset+InsSize;
  887. end;
  888. function taicpu.Matches(p:PInsEntry):longint;
  889. { * IF_SM stands for Size Match: any operand whose size is not
  890. * explicitly specified by the template is `really' intended to be
  891. * the same size as the first size-specified operand.
  892. * Non-specification is tolerated in the input instruction, but
  893. * _wrong_ specification is not.
  894. *
  895. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  896. * three-operand instructions such as SHLD: it implies that the
  897. * first two operands must match in size, but that the third is
  898. * required to be _unspecified_.
  899. *
  900. * IF_SB invokes Size Byte: operands with unspecified size in the
  901. * template are really bytes, and so no non-byte specification in
  902. * the input instruction will be tolerated. IF_SW similarly invokes
  903. * Size Word, and IF_SD invokes Size Doubleword.
  904. *
  905. * (The default state if neither IF_SM nor IF_SM2 is specified is
  906. * that any operand with unspecified size in the template is
  907. * required to have unspecified size in the instruction too...)
  908. }
  909. var
  910. i,j,asize,oprs : longint;
  911. siz : array[0..2] of longint;
  912. begin
  913. Matches:=100;
  914. { Check the opcode and operands }
  915. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  916. begin
  917. Matches:=0;
  918. exit;
  919. end;
  920. { Check that no spurious colons or TOs are present }
  921. for i:=0 to p^.ops-1 do
  922. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  923. begin
  924. Matches:=0;
  925. exit;
  926. end;
  927. { Check that the operand flags all match up }
  928. for i:=0 to p^.ops-1 do
  929. begin
  930. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  931. ((p^.optypes[i] and OT_SIZE_MASK) and
  932. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  933. begin
  934. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  935. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  936. begin
  937. Matches:=0;
  938. exit;
  939. end
  940. else
  941. Matches:=1;
  942. end;
  943. end;
  944. { Check operand sizes }
  945. { as default an untyped size can get all the sizes, this is different
  946. from nasm, but else we need to do a lot checking which opcodes want
  947. size or not with the automatic size generation }
  948. asize:=longint($ffffffff);
  949. if (p^.flags and IF_SB)<>0 then
  950. asize:=OT_BITS8
  951. else if (p^.flags and IF_SW)<>0 then
  952. asize:=OT_BITS16
  953. else if (p^.flags and IF_SD)<>0 then
  954. asize:=OT_BITS32;
  955. if (p^.flags and IF_ARMASK)<>0 then
  956. begin
  957. siz[0]:=0;
  958. siz[1]:=0;
  959. siz[2]:=0;
  960. if (p^.flags and IF_AR0)<>0 then
  961. siz[0]:=asize
  962. else if (p^.flags and IF_AR1)<>0 then
  963. siz[1]:=asize
  964. else if (p^.flags and IF_AR2)<>0 then
  965. siz[2]:=asize;
  966. end
  967. else
  968. begin
  969. { we can leave because the size for all operands is forced to be
  970. the same
  971. but not if IF_SB IF_SW or IF_SD is set PM }
  972. if asize=-1 then
  973. exit;
  974. siz[0]:=asize;
  975. siz[1]:=asize;
  976. siz[2]:=asize;
  977. end;
  978. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  979. begin
  980. if (p^.flags and IF_SM2)<>0 then
  981. oprs:=2
  982. else
  983. oprs:=p^.ops;
  984. for i:=0 to oprs-1 do
  985. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  986. begin
  987. for j:=0 to oprs-1 do
  988. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  989. break;
  990. end;
  991. end
  992. else
  993. oprs:=2;
  994. { Check operand sizes }
  995. for i:=0 to p^.ops-1 do
  996. begin
  997. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  998. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  999. { Immediates can always include smaller size }
  1000. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1001. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1002. Matches:=2;
  1003. end;
  1004. end;
  1005. procedure taicpu.ResetPass1;
  1006. begin
  1007. { we need to reset everything here, because the choosen insentry
  1008. can be invalid for a new situation where the previously optimized
  1009. insentry is not correct }
  1010. InsEntry:=nil;
  1011. InsSize:=0;
  1012. LastInsOffset:=-1;
  1013. end;
  1014. procedure taicpu.ResetPass2;
  1015. begin
  1016. { we are here in a second pass, check if the instruction can be optimized }
  1017. if assigned(InsEntry) and
  1018. ((InsEntry^.flags and IF_PASS2)<>0) then
  1019. begin
  1020. InsEntry:=nil;
  1021. InsSize:=0;
  1022. end;
  1023. LastInsOffset:=-1;
  1024. end;
  1025. function taicpu.CheckIfValid:boolean;
  1026. begin
  1027. result:=FindInsEntry;
  1028. end;
  1029. function taicpu.FindInsentry:boolean;
  1030. var
  1031. i : longint;
  1032. begin
  1033. result:=false;
  1034. { Things which may only be done once, not when a second pass is done to
  1035. optimize }
  1036. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1037. begin
  1038. { We need intel style operands }
  1039. SetOperandOrder(op_intel);
  1040. { create the .ot fields }
  1041. create_ot;
  1042. { set the file postion }
  1043. aktfilepos:=fileinfo;
  1044. end
  1045. else
  1046. begin
  1047. { we've already an insentry so it's valid }
  1048. result:=true;
  1049. exit;
  1050. end;
  1051. { Lookup opcode in the table }
  1052. InsSize:=-1;
  1053. i:=instabcache^[opcode];
  1054. if i=-1 then
  1055. begin
  1056. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1057. exit;
  1058. end;
  1059. insentry:=@instab[i];
  1060. while (insentry^.opcode=opcode) do
  1061. begin
  1062. if matches(insentry)=100 then
  1063. begin
  1064. result:=true;
  1065. exit;
  1066. end;
  1067. inc(i);
  1068. insentry:=@instab[i];
  1069. end;
  1070. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1071. { No instruction found, set insentry to nil and inssize to -1 }
  1072. insentry:=nil;
  1073. inssize:=-1;
  1074. end;
  1075. function taicpu.Pass1(offset:longint):longint;
  1076. begin
  1077. Pass1:=0;
  1078. { Save the old offset and set the new offset }
  1079. InsOffset:=Offset;
  1080. { Error? }
  1081. if (Insentry=nil) and (InsSize=-1) then
  1082. exit;
  1083. { set the file postion }
  1084. aktfilepos:=fileinfo;
  1085. { Get InsEntry }
  1086. if FindInsEntry then
  1087. begin
  1088. { Calculate instruction size }
  1089. InsSize:=calcsize(insentry);
  1090. if segprefix<>NR_NO then
  1091. inc(InsSize);
  1092. { Fix opsize if size if forced }
  1093. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1094. begin
  1095. if (insentry^.flags and IF_ARMASK)=0 then
  1096. begin
  1097. if (insentry^.flags and IF_SB)<>0 then
  1098. begin
  1099. if opsize=S_NO then
  1100. opsize:=S_B;
  1101. end
  1102. else if (insentry^.flags and IF_SW)<>0 then
  1103. begin
  1104. if opsize=S_NO then
  1105. opsize:=S_W;
  1106. end
  1107. else if (insentry^.flags and IF_SD)<>0 then
  1108. begin
  1109. if opsize=S_NO then
  1110. opsize:=S_L;
  1111. end;
  1112. end;
  1113. end;
  1114. LastInsOffset:=InsOffset;
  1115. Pass1:=InsSize;
  1116. exit;
  1117. end;
  1118. LastInsOffset:=-1;
  1119. end;
  1120. procedure taicpu.Pass2(sec:TAsmObjectData);
  1121. var
  1122. c : longint;
  1123. begin
  1124. { error in pass1 ? }
  1125. if insentry=nil then
  1126. exit;
  1127. aktfilepos:=fileinfo;
  1128. { Segment override }
  1129. if (segprefix<>NR_NO) then
  1130. begin
  1131. case segprefix of
  1132. NR_CS : c:=$2e;
  1133. NR_DS : c:=$3e;
  1134. NR_ES : c:=$26;
  1135. NR_FS : c:=$64;
  1136. NR_GS : c:=$65;
  1137. NR_SS : c:=$36;
  1138. end;
  1139. sec.writebytes(c,1);
  1140. { fix the offset for GenNode }
  1141. inc(InsOffset);
  1142. end;
  1143. { Generate the instruction }
  1144. GenCode(sec);
  1145. end;
  1146. function taicpu.needaddrprefix(opidx:byte):boolean;
  1147. begin
  1148. needaddrprefix:=false;
  1149. if (OT_MEMORY and (not oper[opidx]^.ot))=0 then
  1150. begin
  1151. if (
  1152. (oper[opidx]^.ref^.index<>NR_NO) and
  1153. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBD)
  1154. ) or
  1155. (
  1156. (oper[opidx]^.ref^.base<>NR_NO) and
  1157. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBD)
  1158. ) then
  1159. needaddrprefix:=true;
  1160. end;
  1161. end;
  1162. function regval(r:Tregister):byte;
  1163. const
  1164. {$ifdef x86_64}
  1165. opcode_table:array[tregisterindex] of tregisterindex = (
  1166. {$i r8664op.inc}
  1167. );
  1168. {$else x86_64}
  1169. opcode_table:array[tregisterindex] of tregisterindex = (
  1170. {$i r386op.inc}
  1171. );
  1172. {$endif x86_64}
  1173. var
  1174. regidx : tregisterindex;
  1175. begin
  1176. regidx:=findreg_by_number(r);
  1177. if regidx<>0 then
  1178. result:=opcode_table[regidx]
  1179. else
  1180. begin
  1181. Message1(asmw_e_invalid_register,generic_regname(r));
  1182. result:=0;
  1183. end;
  1184. end;
  1185. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1186. var
  1187. sym : tasmsymbol;
  1188. md,s,rv : byte;
  1189. base,index,scalefactor,
  1190. o : longint;
  1191. ir,br : Tregister;
  1192. isub,bsub : tsubregister;
  1193. begin
  1194. process_ea:=false;
  1195. {Register ?}
  1196. if (input.typ=top_reg) then
  1197. begin
  1198. rv:=regval(input.reg);
  1199. output.sib_present:=false;
  1200. output.bytes:=0;
  1201. output.modrm:=$c0 or (rfield shl 3) or rv;
  1202. output.size:=1;
  1203. process_ea:=true;
  1204. exit;
  1205. end;
  1206. {No register, so memory reference.}
  1207. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1208. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1209. internalerror(200301081);
  1210. ir:=input.ref^.index;
  1211. br:=input.ref^.base;
  1212. isub:=getsubreg(ir);
  1213. bsub:=getsubreg(br);
  1214. s:=input.ref^.scalefactor;
  1215. o:=input.ref^.offset;
  1216. sym:=input.ref^.symbol;
  1217. { it's direct address }
  1218. if (br=NR_NO) and (ir=NR_NO) then
  1219. begin
  1220. { it's a pure offset }
  1221. output.sib_present:=false;
  1222. output.bytes:=4;
  1223. output.modrm:=5 or (rfield shl 3);
  1224. end
  1225. else
  1226. { it's an indirection }
  1227. begin
  1228. { 16 bit address? }
  1229. if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  1230. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  1231. message(asmw_e_16bit_not_supported);
  1232. {$ifdef OPTEA}
  1233. { make single reg base }
  1234. if (br=NR_NO) and (s=1) then
  1235. begin
  1236. br:=ir;
  1237. ir:=NR_NO;
  1238. end;
  1239. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1240. if (br=NR_NO) and
  1241. (((s=2) and (ir<>NR_ESP)) or
  1242. (s=3) or (s=5) or (s=9)) then
  1243. begin
  1244. br:=ir;
  1245. dec(s);
  1246. end;
  1247. { swap ESP into base if scalefactor is 1 }
  1248. if (s=1) and (ir=NR_ESP) then
  1249. begin
  1250. ir:=br;
  1251. br:=NR_ESP;
  1252. end;
  1253. {$endif OPTEA}
  1254. { wrong, for various reasons }
  1255. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1256. exit;
  1257. { base }
  1258. case br of
  1259. NR_EAX : base:=0;
  1260. NR_ECX : base:=1;
  1261. NR_EDX : base:=2;
  1262. NR_EBX : base:=3;
  1263. NR_ESP : base:=4;
  1264. NR_NO,
  1265. NR_EBP : base:=5;
  1266. NR_ESI : base:=6;
  1267. NR_EDI : base:=7;
  1268. else
  1269. exit;
  1270. end;
  1271. { index }
  1272. case ir of
  1273. NR_EAX : index:=0;
  1274. NR_ECX : index:=1;
  1275. NR_EDX : index:=2;
  1276. NR_EBX : index:=3;
  1277. NR_NO : index:=4;
  1278. NR_EBP : index:=5;
  1279. NR_ESI : index:=6;
  1280. NR_EDI : index:=7;
  1281. else
  1282. exit;
  1283. end;
  1284. case s of
  1285. 0,
  1286. 1 : scalefactor:=0;
  1287. 2 : scalefactor:=1;
  1288. 4 : scalefactor:=2;
  1289. 8 : scalefactor:=3;
  1290. else
  1291. exit;
  1292. end;
  1293. if (br=NR_NO) or
  1294. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1295. md:=0
  1296. else
  1297. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1298. md:=1
  1299. else
  1300. md:=2;
  1301. if (br=NR_NO) or (md=2) then
  1302. output.bytes:=4
  1303. else
  1304. output.bytes:=md;
  1305. { SIB needed ? }
  1306. if (ir=NR_NO) and (br<>NR_ESP) then
  1307. begin
  1308. output.sib_present:=false;
  1309. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1310. end
  1311. else
  1312. begin
  1313. output.sib_present:=true;
  1314. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1315. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1316. end;
  1317. end;
  1318. if output.sib_present then
  1319. output.size:=2+output.bytes
  1320. else
  1321. output.size:=1+output.bytes;
  1322. process_ea:=true;
  1323. end;
  1324. function taicpu.calcsize(p:PInsEntry):longint;
  1325. var
  1326. codes : pchar;
  1327. c : byte;
  1328. len : longint;
  1329. ea_data : ea;
  1330. begin
  1331. len:=0;
  1332. codes:=@p^.code;
  1333. repeat
  1334. c:=ord(codes^);
  1335. inc(codes);
  1336. case c of
  1337. 0 :
  1338. break;
  1339. 1,2,3 :
  1340. begin
  1341. inc(codes,c);
  1342. inc(len,c);
  1343. end;
  1344. 8,9,10 :
  1345. begin
  1346. inc(codes);
  1347. inc(len);
  1348. end;
  1349. 4,5,6,7 :
  1350. begin
  1351. if opsize=S_W then
  1352. inc(len,2)
  1353. else
  1354. inc(len);
  1355. end;
  1356. 15,
  1357. 12,13,14,
  1358. 16,17,18,
  1359. 20,21,22,
  1360. 40,41,42 :
  1361. inc(len);
  1362. 24,25,26,
  1363. 31,
  1364. 48,49,50 :
  1365. inc(len,2);
  1366. 28,29,30, { we don't have 16 bit immediates code }
  1367. 32,33,34,
  1368. 52,53,54,
  1369. 56,57,58 :
  1370. inc(len,4);
  1371. 192,193,194 :
  1372. if NeedAddrPrefix(c-192) then
  1373. inc(len);
  1374. 208,
  1375. 210 :
  1376. inc(len);
  1377. 200,
  1378. 201,
  1379. 202,
  1380. 209,
  1381. 211,
  1382. 217,218: ;
  1383. 219,220 :
  1384. inc(len);
  1385. 216 :
  1386. begin
  1387. inc(codes);
  1388. inc(len);
  1389. end;
  1390. 224,225,226 :
  1391. begin
  1392. InternalError(777002);
  1393. end;
  1394. else
  1395. begin
  1396. if (c>=64) and (c<=191) then
  1397. begin
  1398. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1399. Message(asmw_e_invalid_effective_address)
  1400. else
  1401. inc(len,ea_data.size);
  1402. end
  1403. else
  1404. InternalError(777003);
  1405. end;
  1406. end;
  1407. until false;
  1408. calcsize:=len;
  1409. end;
  1410. procedure taicpu.GenCode(sec:TAsmObjectData);
  1411. {
  1412. * the actual codes (C syntax, i.e. octal):
  1413. * \0 - terminates the code. (Unless it's a literal of course.)
  1414. * \1, \2, \3 - that many literal bytes follow in the code stream
  1415. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1416. * (POP is never used for CS) depending on operand 0
  1417. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1418. * on operand 0
  1419. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1420. * to the register value of operand 0, 1 or 2
  1421. * \17 - encodes the literal byte 0. (Some compilers don't take
  1422. * kindly to a zero byte in the _middle_ of a compile time
  1423. * string constant, so I had to put this hack in.)
  1424. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1425. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1426. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1427. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1428. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1429. * assembly mode or the address-size override on the operand
  1430. * \37 - a word constant, from the _segment_ part of operand 0
  1431. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1432. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1433. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1434. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1435. * assembly mode or the address-size override on the operand
  1436. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1437. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1438. * field the register value of operand b.
  1439. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1440. * field equal to digit b.
  1441. * \30x - might be an 0x67 byte, depending on the address size of
  1442. * the memory reference in operand x.
  1443. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1444. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1445. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1446. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1447. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1448. * \322 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1449. * \323 - indicates that this instruction is only valid when the
  1450. * operand size is the default (instruction to disassembler,
  1451. * generates no code in the assembler)
  1452. * \330 - a literal byte follows in the code stream, to be added
  1453. * to the condition code value of the instruction.
  1454. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1455. * Operand 0 had better be a segmentless constant.
  1456. }
  1457. var
  1458. currval : longint;
  1459. currsym : tasmsymbol;
  1460. procedure getvalsym(opidx:longint);
  1461. begin
  1462. case oper[opidx]^.typ of
  1463. top_ref :
  1464. begin
  1465. currval:=oper[opidx]^.ref^.offset;
  1466. currsym:=oper[opidx]^.ref^.symbol;
  1467. end;
  1468. top_const :
  1469. begin
  1470. currval:=longint(oper[opidx]^.val);
  1471. currsym:=nil;
  1472. end;
  1473. else
  1474. Message(asmw_e_immediate_or_reference_expected);
  1475. end;
  1476. end;
  1477. const
  1478. CondVal:array[TAsmCond] of byte=($0,
  1479. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1480. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1481. $0, $A, $A, $B, $8, $4);
  1482. var
  1483. c : byte;
  1484. pb,
  1485. codes : pchar;
  1486. bytes : array[0..3] of byte;
  1487. rfield,
  1488. data,s,opidx : longint;
  1489. ea_data : ea;
  1490. begin
  1491. {$ifdef EXTDEBUG}
  1492. { safety check }
  1493. if sec.sects[sec.currsec].datasize<>insoffset then
  1494. internalerror(200130121);
  1495. {$endif EXTDEBUG}
  1496. { load data to write }
  1497. codes:=insentry^.code;
  1498. { Force word push/pop for registers }
  1499. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1500. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1501. begin
  1502. bytes[0]:=$66;
  1503. sec.writebytes(bytes,1);
  1504. end;
  1505. repeat
  1506. c:=ord(codes^);
  1507. inc(codes);
  1508. case c of
  1509. 0 :
  1510. break;
  1511. 1,2,3 :
  1512. begin
  1513. sec.writebytes(codes^,c);
  1514. inc(codes,c);
  1515. end;
  1516. 4,6 :
  1517. begin
  1518. case oper[0]^.reg of
  1519. NR_CS:
  1520. bytes[0]:=$e;
  1521. NR_NO,
  1522. NR_DS:
  1523. bytes[0]:=$1e;
  1524. NR_ES:
  1525. bytes[0]:=$6;
  1526. NR_SS:
  1527. bytes[0]:=$16;
  1528. else
  1529. internalerror(777004);
  1530. end;
  1531. if c=4 then
  1532. inc(bytes[0]);
  1533. sec.writebytes(bytes,1);
  1534. end;
  1535. 5,7 :
  1536. begin
  1537. case oper[0]^.reg of
  1538. NR_FS:
  1539. bytes[0]:=$a0;
  1540. NR_GS:
  1541. bytes[0]:=$a8;
  1542. else
  1543. internalerror(777005);
  1544. end;
  1545. if c=5 then
  1546. inc(bytes[0]);
  1547. sec.writebytes(bytes,1);
  1548. end;
  1549. 8,9,10 :
  1550. begin
  1551. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1552. inc(codes);
  1553. sec.writebytes(bytes,1);
  1554. end;
  1555. 15 :
  1556. begin
  1557. bytes[0]:=0;
  1558. sec.writebytes(bytes,1);
  1559. end;
  1560. 12,13,14 :
  1561. begin
  1562. getvalsym(c-12);
  1563. if (currval<-128) or (currval>127) then
  1564. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1565. if assigned(currsym) then
  1566. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1567. else
  1568. sec.writebytes(currval,1);
  1569. end;
  1570. 16,17,18 :
  1571. begin
  1572. getvalsym(c-16);
  1573. if (currval<-256) or (currval>255) then
  1574. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1575. if assigned(currsym) then
  1576. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1577. else
  1578. sec.writebytes(currval,1);
  1579. end;
  1580. 20,21,22 :
  1581. begin
  1582. getvalsym(c-20);
  1583. if (currval<0) or (currval>255) then
  1584. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1585. if assigned(currsym) then
  1586. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1587. else
  1588. sec.writebytes(currval,1);
  1589. end;
  1590. 24,25,26 :
  1591. begin
  1592. getvalsym(c-24);
  1593. if (currval<-65536) or (currval>65535) then
  1594. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1595. if assigned(currsym) then
  1596. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1597. else
  1598. sec.writebytes(currval,2);
  1599. end;
  1600. 28,29,30 :
  1601. begin
  1602. getvalsym(c-28);
  1603. if assigned(currsym) then
  1604. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1605. else
  1606. sec.writebytes(currval,4);
  1607. end;
  1608. 32,33,34 :
  1609. begin
  1610. getvalsym(c-32);
  1611. if assigned(currsym) then
  1612. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1613. else
  1614. sec.writebytes(currval,4);
  1615. end;
  1616. 40,41,42 :
  1617. begin
  1618. getvalsym(c-40);
  1619. data:=currval-insend;
  1620. if assigned(currsym) then
  1621. inc(data,currsym.address);
  1622. if (data>127) or (data<-128) then
  1623. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1624. sec.writebytes(data,1);
  1625. end;
  1626. 52,53,54 :
  1627. begin
  1628. getvalsym(c-52);
  1629. if assigned(currsym) then
  1630. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1631. else
  1632. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1633. end;
  1634. 56,57,58 :
  1635. begin
  1636. getvalsym(c-56);
  1637. if assigned(currsym) then
  1638. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1639. else
  1640. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1641. end;
  1642. 192,193,194 :
  1643. begin
  1644. if NeedAddrPrefix(c-192) then
  1645. begin
  1646. bytes[0]:=$67;
  1647. sec.writebytes(bytes,1);
  1648. end;
  1649. end;
  1650. 200 :
  1651. begin
  1652. bytes[0]:=$67;
  1653. sec.writebytes(bytes,1);
  1654. end;
  1655. 208 :
  1656. begin
  1657. bytes[0]:=$66;
  1658. sec.writebytes(bytes,1);
  1659. end;
  1660. 210 :
  1661. begin
  1662. bytes[0]:=$48;
  1663. sec.writebytes(bytes,1);
  1664. end;
  1665. 216 :
  1666. begin
  1667. bytes[0]:=ord(codes^)+condval[condition];
  1668. inc(codes);
  1669. sec.writebytes(bytes,1);
  1670. end;
  1671. 201,
  1672. 202,
  1673. 209,
  1674. 211,
  1675. 217,218 :
  1676. begin
  1677. { these are dissambler hints or 32 bit prefixes which
  1678. are not needed }
  1679. end;
  1680. 219 :
  1681. begin
  1682. bytes[0]:=$f3;
  1683. sec.writebytes(bytes,1);
  1684. end;
  1685. 220 :
  1686. begin
  1687. bytes[0]:=$f2;
  1688. sec.writebytes(bytes,1);
  1689. end;
  1690. 31,
  1691. 48,49,50,
  1692. 224,225,226 :
  1693. begin
  1694. InternalError(777006);
  1695. end
  1696. else
  1697. begin
  1698. if (c>=64) and (c<=191) then
  1699. begin
  1700. if (c<127) then
  1701. begin
  1702. if (oper[c and 7]^.typ=top_reg) then
  1703. rfield:=regval(oper[c and 7]^.reg)
  1704. else
  1705. rfield:=regval(oper[c and 7]^.ref^.base);
  1706. end
  1707. else
  1708. rfield:=c and 7;
  1709. opidx:=(c shr 3) and 7;
  1710. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1711. Message(asmw_e_invalid_effective_address);
  1712. pb:=@bytes;
  1713. pb^:=chr(ea_data.modrm);
  1714. inc(pb);
  1715. if ea_data.sib_present then
  1716. begin
  1717. pb^:=chr(ea_data.sib);
  1718. inc(pb);
  1719. end;
  1720. s:=pb-pchar(@bytes);
  1721. sec.writebytes(bytes,s);
  1722. case ea_data.bytes of
  1723. 0 : ;
  1724. 1 :
  1725. begin
  1726. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1727. sec.writereloc(oper[opidx]^.ref^.offset,1,oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE)
  1728. else
  1729. begin
  1730. bytes[0]:=oper[opidx]^.ref^.offset;
  1731. sec.writebytes(bytes,1);
  1732. end;
  1733. inc(s);
  1734. end;
  1735. 2,4 :
  1736. begin
  1737. sec.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1738. oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE);
  1739. inc(s,ea_data.bytes);
  1740. end;
  1741. end;
  1742. end
  1743. else
  1744. InternalError(777007);
  1745. end;
  1746. end;
  1747. until false;
  1748. end;
  1749. {$endif NOAG386BIN}
  1750. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1751. begin
  1752. result:=(regtype = R_INTREGISTER) and
  1753. (ops=2) and
  1754. (oper[0]^.typ=top_reg) and
  1755. (oper[1]^.typ=top_reg) and
  1756. (oper[0]^.reg=oper[1]^.reg) and
  1757. ((opcode=A_MOV) or (opcode=A_XCHG));
  1758. end;
  1759. {*****************************************************************************
  1760. Instruction table
  1761. *****************************************************************************}
  1762. procedure BuildInsTabCache;
  1763. {$ifndef NOAG386BIN}
  1764. var
  1765. i : longint;
  1766. {$endif}
  1767. begin
  1768. {$ifndef NOAG386BIN}
  1769. new(instabcache);
  1770. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1771. i:=0;
  1772. while (i<InsTabEntries) do
  1773. begin
  1774. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1775. InsTabCache^[InsTab[i].OPcode]:=i;
  1776. inc(i);
  1777. end;
  1778. {$endif NOAG386BIN}
  1779. end;
  1780. procedure InitAsm;
  1781. begin
  1782. {$ifndef NOAG386BIN}
  1783. if not assigned(instabcache) then
  1784. BuildInsTabCache;
  1785. {$endif NOAG386BIN}
  1786. end;
  1787. procedure DoneAsm;
  1788. begin
  1789. {$ifndef NOAG386BIN}
  1790. if assigned(instabcache) then
  1791. begin
  1792. dispose(instabcache);
  1793. instabcache:=nil;
  1794. end;
  1795. {$endif NOAG386BIN}
  1796. end;
  1797. end.
  1798. {
  1799. $Log$
  1800. Revision 1.54 2004-03-15 08:44:51 michael
  1801. + Fix from peter: fixes crash when inlining assembler code referencing local vars
  1802. Revision 1.53 2004/03/04 17:25:38 peter
  1803. * top_none in create_ot, it is used in error situations
  1804. Revision 1.52 2004/02/27 10:21:05 florian
  1805. * top_symbol killed
  1806. + refaddr to treference added
  1807. + refsymbol to treference added
  1808. * top_local stuff moved to an extra record to save memory
  1809. + aint introduced
  1810. * tppufile.get/putint64/aint implemented
  1811. Revision 1.51 2004/02/09 22:14:17 peter
  1812. * more x86_64 parameter fixes
  1813. * tparalocation.lochigh is now used to indicate if registerhigh
  1814. is used and what the type is
  1815. Revision 1.50 2004/02/08 23:10:21 jonas
  1816. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  1817. removes moves of that particular register type. This is necessary so
  1818. we don't remove the live_start instruction of a register before it
  1819. has been processed
  1820. Revision 1.49 2004/02/08 20:15:43 jonas
  1821. - removed taicpu.is_reg_move because it's not used anymore
  1822. + support tracking fpu register moves by rgobj for the ppc
  1823. Revision 1.48 2004/02/05 18:28:37 peter
  1824. * x86_64 fixes for opsize
  1825. Revision 1.47 2004/02/03 21:21:23 peter
  1826. * real fix for the short jmp out of range problem. Only forward jumps
  1827. needs an offset correction. For backward jumps both the address of
  1828. the symbol and the instruction are already updated so no correction
  1829. is required.
  1830. Revision 1.46 2004/01/26 16:12:28 daniel
  1831. * reginfo now also only allocated during register allocation
  1832. * third round of gdb cleanups: kick out most of concatstabto
  1833. Revision 1.45 2004/01/15 14:01:32 florian
  1834. + x86 instruction tables for x86-64 extended
  1835. Revision 1.44 2004/01/12 16:37:59 peter
  1836. * moved spilling code from taicpu to rg
  1837. Revision 1.43 2003/12/26 14:02:30 peter
  1838. * sparc updates
  1839. * use registertype in spill_register
  1840. Revision 1.42 2003/12/25 12:01:35 florian
  1841. + possible sse2 unit usage for double calculations
  1842. * some sse2 assembler issues fixed
  1843. Revision 1.41 2003/12/25 01:07:09 florian
  1844. + $fputype directive support
  1845. + single data type operations with sse unit
  1846. * fixed more x86-64 stuff
  1847. Revision 1.40 2003/12/15 21:25:49 peter
  1848. * reg allocations for imaginary register are now inserted just
  1849. before reg allocation
  1850. * tregister changed to enum to allow compile time check
  1851. * fixed several tregister-tsuperregister errors
  1852. Revision 1.39 2003/12/14 20:24:28 daniel
  1853. * Register allocator speed optimizations
  1854. - Worklist no longer a ringbuffer
  1855. - No find operations are left
  1856. - Simplify now done in constant time
  1857. - unusedregs is now a Tsuperregisterworklist
  1858. - Microoptimizations
  1859. Revision 1.38 2003/11/12 16:05:40 florian
  1860. * assembler readers OOPed
  1861. + typed currency constants
  1862. + typed 128 bit float constants if the CPU supports it
  1863. Revision 1.37 2003/10/30 19:59:00 peter
  1864. * support scalefactor for opr_local
  1865. * support reference with opr_local set, fixes tw2631
  1866. Revision 1.36 2003/10/29 15:40:20 peter
  1867. * support indexing and offset retrieval for locals
  1868. Revision 1.35 2003/10/23 14:44:07 peter
  1869. * splitted buildderef and buildderefimpl to fix interface crc
  1870. calculation
  1871. Revision 1.34 2003/10/22 20:40:00 peter
  1872. * write derefdata in a separate ppu entry
  1873. Revision 1.33 2003/10/21 15:15:36 peter
  1874. * taicpu_abstract.oper[] changed to pointers
  1875. Revision 1.32 2003/10/17 14:38:32 peter
  1876. * 64k registers supported
  1877. * fixed some memory leaks
  1878. Revision 1.31 2003/10/09 21:31:37 daniel
  1879. * Register allocator splitted, ans abstract now
  1880. Revision 1.30 2003/10/01 20:34:50 peter
  1881. * procinfo unit contains tprocinfo
  1882. * cginfo renamed to cgbase
  1883. * moved cgmessage to verbose
  1884. * fixed ppc and sparc compiles
  1885. Revision 1.29 2003/09/29 20:58:56 peter
  1886. * optimized releasing of registers
  1887. Revision 1.28 2003/09/28 21:49:30 peter
  1888. * fixed invalid opcode handling in spill registers
  1889. Revision 1.27 2003/09/28 13:37:07 peter
  1890. * give error for wrong register number
  1891. Revision 1.26 2003/09/24 21:15:49 florian
  1892. * fixed make cycle
  1893. Revision 1.25 2003/09/24 17:12:36 florian
  1894. * x86-64 adaptions
  1895. Revision 1.24 2003/09/23 17:56:06 peter
  1896. * locals and paras are allocated in the code generation
  1897. * tvarsym.localloc contains the location of para/local when
  1898. generating code for the current procedure
  1899. Revision 1.23 2003/09/14 14:22:51 daniel
  1900. * Fixed incorrect movzx spilling
  1901. Revision 1.22 2003/09/12 20:25:17 daniel
  1902. * Add BTR to destination memory location check in spilling
  1903. Revision 1.21 2003/09/10 19:14:31 daniel
  1904. * Failed attempt to restore broken fastspill functionality
  1905. Revision 1.20 2003/09/10 11:23:09 marco
  1906. * fix from peter for bts reg32,mem32 problem
  1907. Revision 1.19 2003/09/09 12:54:45 florian
  1908. * x86 instruction table updated to nasm 0.98.37:
  1909. - sse3 aka prescott support
  1910. - small fixes
  1911. Revision 1.18 2003/09/07 22:09:35 peter
  1912. * preparations for different default calling conventions
  1913. * various RA fixes
  1914. Revision 1.17 2003/09/03 15:55:02 peter
  1915. * NEWRA branch merged
  1916. Revision 1.16.2.4 2003/08/31 15:46:26 peter
  1917. * more updates for tregister
  1918. Revision 1.16.2.3 2003/08/29 17:29:00 peter
  1919. * next batch of updates
  1920. Revision 1.16.2.2 2003/08/28 18:35:08 peter
  1921. * tregister changed to cardinal
  1922. Revision 1.16.2.1 2003/08/27 19:55:54 peter
  1923. * first tregister patch
  1924. Revision 1.16 2003/08/21 17:20:19 peter
  1925. * first spill the registers of top_ref before spilling top_reg
  1926. Revision 1.15 2003/08/21 14:48:36 peter
  1927. * fix reg-supreg range check error
  1928. Revision 1.14 2003/08/20 16:52:01 daniel
  1929. * Some old register convention code removed
  1930. * A few changes to eliminate a few lines of code
  1931. Revision 1.13 2003/08/20 09:07:00 daniel
  1932. * New register coding now mandatory, some more convert_registers calls
  1933. removed.
  1934. Revision 1.12 2003/08/20 07:48:04 daniel
  1935. * Made internal assembler use new register coding
  1936. Revision 1.11 2003/08/19 13:58:33 daniel
  1937. * Corrected a comment.
  1938. Revision 1.10 2003/08/15 14:44:20 daniel
  1939. * Fixed newra compilation
  1940. Revision 1.9 2003/08/11 21:18:20 peter
  1941. * start of sparc support for newra
  1942. Revision 1.8 2003/08/09 18:56:54 daniel
  1943. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1944. allocator
  1945. * Some preventive changes to i386 spillinh code
  1946. Revision 1.7 2003/07/06 15:31:21 daniel
  1947. * Fixed register allocator. *Lots* of fixes.
  1948. Revision 1.6 2003/06/14 14:53:50 jonas
  1949. * fixed newra cycle for x86
  1950. * added constants for indicating source and destination operands of the
  1951. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1952. Revision 1.5 2003/06/03 13:01:59 daniel
  1953. * Register allocator finished
  1954. Revision 1.4 2003/05/30 23:57:08 peter
  1955. * more sparc cleanup
  1956. * accumulator removed, splitted in function_return_reg (called) and
  1957. function_result_reg (caller)
  1958. Revision 1.3 2003/05/22 21:33:31 peter
  1959. * removed some unit dependencies
  1960. Revision 1.2 2002/04/25 16:12:09 florian
  1961. * fixed more problems with cpubase and x86-64
  1962. Revision 1.1 2003/04/25 12:43:40 florian
  1963. * merged i386/aasmcpu and x86_64/aasmcpu to x86/aasmcpu
  1964. Revision 1.18 2003/04/25 12:04:31 florian
  1965. * merged agx64att and ag386att to x86/agx86att
  1966. Revision 1.17 2003/04/22 14:33:38 peter
  1967. * removed some notes/hints
  1968. Revision 1.16 2003/04/22 10:09:35 daniel
  1969. + Implemented the actual register allocator
  1970. + Scratch registers unavailable when new register allocator used
  1971. + maybe_save/maybe_restore unavailable when new register allocator used
  1972. Revision 1.15 2003/03/26 12:50:54 armin
  1973. * avoid problems with the ide in init/dome
  1974. Revision 1.14 2003/03/08 08:59:07 daniel
  1975. + $define newra will enable new register allocator
  1976. + getregisterint will return imaginary registers with $newra
  1977. + -sr switch added, will skip register allocation so you can see
  1978. the direct output of the code generator before register allocation
  1979. Revision 1.13 2003/02/25 07:41:54 daniel
  1980. * Properly fixed reversed operands bug
  1981. Revision 1.12 2003/02/19 22:00:15 daniel
  1982. * Code generator converted to new register notation
  1983. - Horribily outdated todo.txt removed
  1984. Revision 1.11 2003/01/09 20:40:59 daniel
  1985. * Converted some code in cgx86.pas to new register numbering
  1986. Revision 1.10 2003/01/08 18:43:57 daniel
  1987. * Tregister changed into a record
  1988. Revision 1.9 2003/01/05 13:36:53 florian
  1989. * x86-64 compiles
  1990. + very basic support for float128 type (x86-64 only)
  1991. Revision 1.8 2002/11/17 16:31:58 carl
  1992. * memory optimization (3-4%) : cleanup of tai fields,
  1993. cleanup of tdef and tsym fields.
  1994. * make it work for m68k
  1995. Revision 1.7 2002/11/15 01:58:54 peter
  1996. * merged changes from 1.0.7 up to 04-11
  1997. - -V option for generating bug report tracing
  1998. - more tracing for option parsing
  1999. - errors for cdecl and high()
  2000. - win32 import stabs
  2001. - win32 records<=8 are returned in eax:edx (turned off by default)
  2002. - heaptrc update
  2003. - more info for temp management in .s file with EXTDEBUG
  2004. Revision 1.6 2002/10/31 13:28:32 pierre
  2005. * correct last wrong fix for tw2158
  2006. Revision 1.5 2002/10/30 17:10:00 pierre
  2007. * merge of fix for tw2158 bug
  2008. Revision 1.4 2002/08/15 19:10:36 peter
  2009. * first things tai,tnode storing in ppu
  2010. Revision 1.3 2002/08/13 18:01:52 carl
  2011. * rename swatoperands to swapoperands
  2012. + m68k first compilable version (still needs a lot of testing):
  2013. assembler generator, system information , inline
  2014. assembler reader.
  2015. Revision 1.2 2002/07/20 11:57:59 florian
  2016. * types.pas renamed to defbase.pas because D6 contains a types
  2017. unit so this would conflicts if D6 programms are compiled
  2018. + Willamette/SSE2 instructions to assembler added
  2019. Revision 1.1 2002/07/01 18:46:29 peter
  2020. * internal linker
  2021. * reorganized aasm layer
  2022. }