aasmcpu.pas 88 KB

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