aasmcpu.pas 83 KB

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