aasmcpu.pas 71 KB

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