aasmcpu.pas 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  4. Contains the abstract assembler implementation for the i386
  5. * Portions of this code was inspired by the NASM sources
  6. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. unit aasmcpu;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cclasses,globals,verbose,
  26. cpuinfo,cpubase,
  27. cgbase,
  28. symppu,symtype,symsym,
  29. aasmbase,aasmtai;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  38. OT_BITS16 = $00000002;
  39. OT_BITS32 = $00000004;
  40. OT_BITS64 = $00000008; { FPU only }
  41. OT_BITS80 = $00000010;
  42. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  43. OT_NEAR = $00000040;
  44. OT_SHORT = $00000080;
  45. OT_SIZE_MASK = $000000FF; { all the size attributes }
  46. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  47. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  48. OT_TO = $00000200; { operand is followed by a colon }
  49. { reverse effect in FADD, FSUB &c }
  50. OT_COLON = $00000400;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_IMM8 = $00002001;
  54. OT_IMM16 = $00002002;
  55. OT_IMM32 = $00002004;
  56. OT_IMM64 = $00002008;
  57. OT_IMM80 = $00002010;
  58. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  59. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  60. OT_REG8 = $00201001;
  61. OT_REG16 = $00201002;
  62. OT_REG32 = $00201004;
  63. OT_REG64 = $00201008;
  64. OT_MMXREG = $00201008; { MMX registers }
  65. OT_XMMREG = $00201010; { Katmai registers }
  66. OT_MEMORY = $00204000; { register number in 'basereg' }
  67. OT_MEM8 = $00204001;
  68. OT_MEM16 = $00204002;
  69. OT_MEM32 = $00204004;
  70. OT_MEM64 = $00204008;
  71. OT_MEM80 = $00204010;
  72. OT_FPUREG = $01000000; { floating point stack registers }
  73. OT_FPU0 = $01000800; { FPU stack register zero }
  74. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  75. { a mask for the following }
  76. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  77. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  78. OT_REG_AX = $00211002; { ditto }
  79. OT_REG_EAX = $00211004; { and again }
  80. {$ifdef x86_64}
  81. OT_REG_RAX = $00211008;
  82. {$endif x86_64}
  83. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  84. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  85. OT_REG_CX = $00221002; { ditto }
  86. OT_REG_ECX = $00221004; { another one }
  87. {$ifdef x86_64}
  88. OT_REG_RCX = $00221008;
  89. {$endif x86_64}
  90. OT_REG_DX = $00241002;
  91. OT_REG_EDX = $00241004;
  92. OT_REG_SREG = $00081002; { any segment register }
  93. OT_REG_CS = $01081002; { CS }
  94. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  95. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  96. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  97. OT_REG_CREG = $08101004; { CRn }
  98. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  99. OT_REG_DREG = $10101004; { DRn }
  100. OT_REG_TREG = $20101004; { TRn }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. { Size of the instruction table converted by nasmconv.pas }
  107. {$ifdef x86_64}
  108. instabentries = {$i 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. function spill_registers(list:Taasmoutput;
  172. rgget:Trggetproc;
  173. rgunget:Trgungetproc;
  174. const r:Tsuperregisterset;
  175. var unusedregsint:Tsuperregisterset;
  176. const spilltemplist:Tspill_temp_list):boolean;override;
  177. protected
  178. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  179. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  180. procedure ppubuildderefimploper(var o:toper);override;
  181. procedure ppuderefoper(var o:toper);override;
  182. private
  183. { next fields are filled in pass1, so pass2 is faster }
  184. inssize : shortint;
  185. insoffset,
  186. LastInsOffset : longint; { need to be public to be reset }
  187. insentry : PInsEntry;
  188. function InsEnd:longint;
  189. procedure create_ot;
  190. function Matches(p:PInsEntry):longint;
  191. function calcsize(p:PInsEntry):longint;
  192. procedure gencode(sec:TAsmObjectData);
  193. function NeedAddrPrefix(opidx:byte):boolean;
  194. procedure Swapoperands;
  195. function FindInsentry:boolean;
  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. { SSE3 instructions }
  232. IF_SSE3 = $00040000;
  233. { the mask for processor types }
  234. {IF_PMASK = longint($FF000000);}
  235. { the mask for disassembly "prefer" }
  236. {IF_PFMASK = longint($F001FF00);}
  237. IF_8086 = $00000000; { 8086 instruction }
  238. IF_186 = $01000000; { 186+ instruction }
  239. IF_286 = $02000000; { 286+ instruction }
  240. IF_386 = $03000000; { 386+ instruction }
  241. IF_486 = $04000000; { 486+ instruction }
  242. IF_PENT = $05000000; { Pentium instruction }
  243. IF_P6 = $06000000; { P6 instruction }
  244. IF_KATMAI = $07000000; { Katmai instructions }
  245. { Willamette instructions }
  246. IF_WILLAMETTE = $08000000;
  247. { Prescott instructions }
  248. IF_PRESCOTT = $09000000;
  249. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  250. IF_AMD = $20000000; { AMD-specific instruction }
  251. { added flags }
  252. IF_PRE = $40000000; { it's a prefix instruction }
  253. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  254. type
  255. TInsTabCache=array[TasmOp] of longint;
  256. PInsTabCache=^TInsTabCache;
  257. const
  258. {$ifdef x86_64}
  259. InsTab:array[0..instabentries-1] of TInsEntry={$i x86_64ta.inc}
  260. {$else x86_64}
  261. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  262. {$endif x86_64}
  263. var
  264. InsTabCache : PInsTabCache;
  265. const
  266. {$ifdef x86_64}
  267. { Intel style operands ! }
  268. opsize_2_type:array[0..2,topsize] of longint=(
  269. (OT_NONE,
  270. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  271. OT_BITS16,OT_BITS32,OT_BITS64,
  272. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  273. OT_NEAR,OT_FAR,OT_SHORT
  274. ),
  275. (OT_NONE,
  276. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  277. OT_BITS16,OT_BITS32,OT_BITS64,
  278. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  279. OT_NEAR,OT_FAR,OT_SHORT
  280. ),
  281. (OT_NONE,
  282. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  283. OT_BITS16,OT_BITS32,OT_BITS64,
  284. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  285. OT_NEAR,OT_FAR,OT_SHORT
  286. )
  287. );
  288. reg_ot_table : array[tregisterindex] of longint = (
  289. {$i r8664ot.inc}
  290. );
  291. {$else x86_64}
  292. { Intel style operands ! }
  293. opsize_2_type:array[0..2,topsize] of longint=(
  294. (OT_NONE,
  295. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,
  296. OT_BITS16,OT_BITS32,OT_BITS64,
  297. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  298. OT_NEAR,OT_FAR,OT_SHORT
  299. ),
  300. (OT_NONE,
  301. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,
  302. OT_BITS16,OT_BITS32,OT_BITS64,
  303. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  304. OT_NEAR,OT_FAR,OT_SHORT
  305. ),
  306. (OT_NONE,
  307. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,
  308. OT_BITS16,OT_BITS32,OT_BITS64,
  309. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  310. OT_NEAR,OT_FAR,OT_SHORT
  311. )
  312. );
  313. reg_ot_table : array[tregisterindex] of longint = (
  314. {$i r386ot.inc}
  315. );
  316. {$endif x86_64}
  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:=0 to ops-1 do
  553. begin
  554. with oper[i]^ do
  555. begin
  556. if i=0 then
  557. s:=s+' '
  558. else
  559. s:=s+',';
  560. { type }
  561. addsize:=false;
  562. if (ot and OT_XMMREG)=OT_XMMREG then
  563. s:=s+'xmmreg'
  564. else
  565. if (ot and OT_MMXREG)=OT_MMXREG then
  566. s:=s+'mmxreg'
  567. else
  568. if (ot and OT_FPUREG)=OT_FPUREG then
  569. s:=s+'fpureg'
  570. else
  571. if (ot and OT_REGISTER)=OT_REGISTER then
  572. begin
  573. s:=s+'reg';
  574. addsize:=true;
  575. end
  576. else
  577. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  578. begin
  579. s:=s+'imm';
  580. addsize:=true;
  581. end
  582. else
  583. if (ot and OT_MEMORY)=OT_MEMORY then
  584. begin
  585. s:=s+'mem';
  586. addsize:=true;
  587. end
  588. else
  589. s:=s+'???';
  590. { size }
  591. if addsize then
  592. begin
  593. if (ot and OT_BITS8)<>0 then
  594. s:=s+'8'
  595. else
  596. if (ot and OT_BITS16)<>0 then
  597. s:=s+'16'
  598. else
  599. if (ot and OT_BITS32)<>0 then
  600. s:=s+'32'
  601. else
  602. s:=s+'??';
  603. { signed }
  604. if (ot and OT_SIGNED)<>0 then
  605. s:=s+'s';
  606. end;
  607. end;
  608. end;
  609. GetString:=s+']';
  610. end;
  611. procedure taicpu.Swapoperands;
  612. var
  613. p : POper;
  614. begin
  615. { Fix the operands which are in AT&T style and we need them in Intel style }
  616. case ops of
  617. 2 : begin
  618. { 0,1 -> 1,0 }
  619. p:=oper[0];
  620. oper[0]:=oper[1];
  621. oper[1]:=p;
  622. end;
  623. 3 : begin
  624. { 0,1,2 -> 2,1,0 }
  625. p:=oper[0];
  626. oper[0]:=oper[2];
  627. oper[2]:=p;
  628. end;
  629. end;
  630. end;
  631. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  632. begin
  633. if FOperandOrder<>order then
  634. begin
  635. Swapoperands;
  636. FOperandOrder:=order;
  637. end;
  638. end;
  639. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  640. begin
  641. o.typ:=toptype(ppufile.getbyte);
  642. o.ot:=ppufile.getlongint;
  643. case o.typ of
  644. top_reg :
  645. ppufile.getdata(o.reg,sizeof(Tregister));
  646. top_ref :
  647. begin
  648. new(o.ref);
  649. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  650. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  651. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  652. o.ref^.scalefactor:=ppufile.getbyte;
  653. o.ref^.offset:=ppufile.getlongint;
  654. o.ref^.symbol:=ppufile.getasmsymbol;
  655. end;
  656. top_const :
  657. o.val:=aword(ppufile.getlongint);
  658. top_symbol :
  659. begin
  660. o.sym:=ppufile.getasmsymbol;
  661. o.symofs:=ppufile.getlongint;
  662. end;
  663. top_local :
  664. begin
  665. ppufile.getderef(o.localsymderef);
  666. o.localsymofs:=ppufile.getlongint;
  667. o.localindexreg:=tregister(ppufile.getlongint);
  668. o.localscale:=ppufile.getbyte;
  669. o.localgetoffset:=(ppufile.getbyte<>0);
  670. end;
  671. end;
  672. end;
  673. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  674. begin
  675. ppufile.putbyte(byte(o.typ));
  676. ppufile.putlongint(o.ot);
  677. case o.typ of
  678. top_reg :
  679. ppufile.putdata(o.reg,sizeof(Tregister));
  680. top_ref :
  681. begin
  682. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  683. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  684. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  685. ppufile.putbyte(o.ref^.scalefactor);
  686. ppufile.putlongint(o.ref^.offset);
  687. ppufile.putasmsymbol(o.ref^.symbol);
  688. end;
  689. top_const :
  690. ppufile.putlongint(longint(o.val));
  691. top_symbol :
  692. begin
  693. ppufile.putasmsymbol(o.sym);
  694. ppufile.putlongint(longint(o.symofs));
  695. end;
  696. top_local :
  697. begin
  698. ppufile.putderef(o.localsymderef);
  699. ppufile.putlongint(longint(o.localsymofs));
  700. ppufile.putlongint(longint(o.localindexreg));
  701. ppufile.putbyte(o.localscale);
  702. ppufile.putbyte(byte(o.localgetoffset));
  703. end;
  704. end;
  705. end;
  706. procedure taicpu.ppubuildderefimploper(var o:toper);
  707. begin
  708. case o.typ of
  709. top_local :
  710. o.localsymderef.build(tvarsym(o.localsym));
  711. end;
  712. end;
  713. procedure taicpu.ppuderefoper(var o:toper);
  714. begin
  715. case o.typ of
  716. top_ref :
  717. begin
  718. if assigned(o.ref^.symbol) then
  719. objectlibrary.derefasmsymbol(o.ref^.symbol);
  720. end;
  721. top_symbol :
  722. objectlibrary.derefasmsymbol(o.sym);
  723. top_local :
  724. o.localsym:=tvarsym(o.localsymderef.resolve);
  725. end;
  726. end;
  727. procedure taicpu.CheckNonCommutativeOpcodes;
  728. begin
  729. { we need ATT order }
  730. SetOperandOrder(op_att);
  731. if (
  732. (ops=2) and
  733. (oper[0]^.typ=top_reg) and
  734. (oper[1]^.typ=top_reg) and
  735. { if the first is ST and the second is also a register
  736. it is necessarily ST1 .. ST7 }
  737. ((oper[0]^.reg=NR_ST) or
  738. (oper[0]^.reg=NR_ST0))
  739. ) or
  740. { ((ops=1) and
  741. (oper[0]^.typ=top_reg) and
  742. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  743. (ops=0) then
  744. begin
  745. if opcode=A_FSUBR then
  746. opcode:=A_FSUB
  747. else if opcode=A_FSUB then
  748. opcode:=A_FSUBR
  749. else if opcode=A_FDIVR then
  750. opcode:=A_FDIV
  751. else if opcode=A_FDIV then
  752. opcode:=A_FDIVR
  753. else if opcode=A_FSUBRP then
  754. opcode:=A_FSUBP
  755. else if opcode=A_FSUBP then
  756. opcode:=A_FSUBRP
  757. else if opcode=A_FDIVRP then
  758. opcode:=A_FDIVP
  759. else if opcode=A_FDIVP then
  760. opcode:=A_FDIVRP;
  761. end;
  762. if (
  763. (ops=1) and
  764. (oper[0]^.typ=top_reg) and
  765. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  766. (oper[0]^.reg<>NR_ST)
  767. ) then
  768. begin
  769. if opcode=A_FSUBRP then
  770. opcode:=A_FSUBP
  771. else if opcode=A_FSUBP then
  772. opcode:=A_FSUBRP
  773. else if opcode=A_FDIVRP then
  774. opcode:=A_FDIVP
  775. else if opcode=A_FDIVP then
  776. opcode:=A_FDIVRP;
  777. end;
  778. end;
  779. {*****************************************************************************
  780. Assembler
  781. *****************************************************************************}
  782. {$ifndef NOAG386BIN}
  783. type
  784. ea=packed record
  785. sib_present : boolean;
  786. bytes : byte;
  787. size : byte;
  788. modrm : byte;
  789. sib : byte;
  790. end;
  791. procedure taicpu.create_ot;
  792. {
  793. this function will also fix some other fields which only needs to be once
  794. }
  795. var
  796. i,l,relsize : longint;
  797. begin
  798. if ops=0 then
  799. exit;
  800. { update oper[].ot field }
  801. for i:=0 to ops-1 do
  802. with oper[i]^ do
  803. begin
  804. case typ of
  805. top_reg :
  806. begin
  807. ot:=reg_ot_table[findreg_by_number(reg)];
  808. end;
  809. top_ref :
  810. begin
  811. { create ot field }
  812. if (ot and OT_SIZE_MASK)=0 then
  813. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  814. else
  815. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  816. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  817. ot:=ot or OT_MEM_OFFS;
  818. { fix scalefactor }
  819. if (ref^.index=NR_NO) then
  820. ref^.scalefactor:=0
  821. else
  822. if (ref^.scalefactor=0) then
  823. ref^.scalefactor:=1;
  824. end;
  825. top_local :
  826. begin
  827. if (ot and OT_SIZE_MASK)=0 then
  828. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  829. else
  830. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  831. end;
  832. top_const :
  833. begin
  834. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  835. ot:=OT_IMM8 or OT_SIGNED
  836. else
  837. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  838. end;
  839. top_symbol :
  840. begin
  841. if LastInsOffset=-1 then
  842. l:=0
  843. else
  844. l:=InsOffset-LastInsOffset;
  845. inc(l,symofs);
  846. if assigned(sym) then
  847. inc(l,sym.address);
  848. { instruction size will then always become 2 (PFV) }
  849. relsize:=(InsOffset+2)-l;
  850. if (not assigned(sym) or
  851. ((sym.currbind<>AB_EXTERNAL) and (sym.address<>0))) and
  852. (relsize>=-128) and (relsize<=127) then
  853. ot:=OT_IMM32 or OT_SHORT
  854. else
  855. ot:=OT_IMM32 or OT_NEAR;
  856. end;
  857. end;
  858. end;
  859. end;
  860. function taicpu.InsEnd:longint;
  861. begin
  862. InsEnd:=InsOffset+InsSize;
  863. end;
  864. function taicpu.Matches(p:PInsEntry):longint;
  865. { * IF_SM stands for Size Match: any operand whose size is not
  866. * explicitly specified by the template is `really' intended to be
  867. * the same size as the first size-specified operand.
  868. * Non-specification is tolerated in the input instruction, but
  869. * _wrong_ specification is not.
  870. *
  871. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  872. * three-operand instructions such as SHLD: it implies that the
  873. * first two operands must match in size, but that the third is
  874. * required to be _unspecified_.
  875. *
  876. * IF_SB invokes Size Byte: operands with unspecified size in the
  877. * template are really bytes, and so no non-byte specification in
  878. * the input instruction will be tolerated. IF_SW similarly invokes
  879. * Size Word, and IF_SD invokes Size Doubleword.
  880. *
  881. * (The default state if neither IF_SM nor IF_SM2 is specified is
  882. * that any operand with unspecified size in the template is
  883. * required to have unspecified size in the instruction too...)
  884. }
  885. var
  886. i,j,asize,oprs : longint;
  887. siz : array[0..2] of longint;
  888. begin
  889. Matches:=100;
  890. { Check the opcode and operands }
  891. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  892. begin
  893. Matches:=0;
  894. exit;
  895. end;
  896. { Check that no spurious colons or TOs are present }
  897. for i:=0 to p^.ops-1 do
  898. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  899. begin
  900. Matches:=0;
  901. exit;
  902. end;
  903. { Check that the operand flags all match up }
  904. for i:=0 to p^.ops-1 do
  905. begin
  906. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  907. ((p^.optypes[i] and OT_SIZE_MASK) and
  908. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  909. begin
  910. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  911. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  912. begin
  913. Matches:=0;
  914. exit;
  915. end
  916. else
  917. Matches:=1;
  918. end;
  919. end;
  920. { Check operand sizes }
  921. { as default an untyped size can get all the sizes, this is different
  922. from nasm, but else we need to do a lot checking which opcodes want
  923. size or not with the automatic size generation }
  924. asize:=longint($ffffffff);
  925. if (p^.flags and IF_SB)<>0 then
  926. asize:=OT_BITS8
  927. else if (p^.flags and IF_SW)<>0 then
  928. asize:=OT_BITS16
  929. else if (p^.flags and IF_SD)<>0 then
  930. asize:=OT_BITS32;
  931. if (p^.flags and IF_ARMASK)<>0 then
  932. begin
  933. siz[0]:=0;
  934. siz[1]:=0;
  935. siz[2]:=0;
  936. if (p^.flags and IF_AR0)<>0 then
  937. siz[0]:=asize
  938. else if (p^.flags and IF_AR1)<>0 then
  939. siz[1]:=asize
  940. else if (p^.flags and IF_AR2)<>0 then
  941. siz[2]:=asize;
  942. end
  943. else
  944. begin
  945. { we can leave because the size for all operands is forced to be
  946. the same
  947. but not if IF_SB IF_SW or IF_SD is set PM }
  948. if asize=-1 then
  949. exit;
  950. siz[0]:=asize;
  951. siz[1]:=asize;
  952. siz[2]:=asize;
  953. end;
  954. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  955. begin
  956. if (p^.flags and IF_SM2)<>0 then
  957. oprs:=2
  958. else
  959. oprs:=p^.ops;
  960. for i:=0 to oprs-1 do
  961. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  962. begin
  963. for j:=0 to oprs-1 do
  964. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  965. break;
  966. end;
  967. end
  968. else
  969. oprs:=2;
  970. { Check operand sizes }
  971. for i:=0 to p^.ops-1 do
  972. begin
  973. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  974. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  975. { Immediates can always include smaller size }
  976. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  977. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  978. Matches:=2;
  979. end;
  980. end;
  981. procedure taicpu.ResetPass1;
  982. begin
  983. { we need to reset everything here, because the choosen insentry
  984. can be invalid for a new situation where the previously optimized
  985. insentry is not correct }
  986. InsEntry:=nil;
  987. InsSize:=0;
  988. LastInsOffset:=-1;
  989. end;
  990. procedure taicpu.ResetPass2;
  991. begin
  992. { we are here in a second pass, check if the instruction can be optimized }
  993. if assigned(InsEntry) and
  994. ((InsEntry^.flags and IF_PASS2)<>0) then
  995. begin
  996. InsEntry:=nil;
  997. InsSize:=0;
  998. end;
  999. LastInsOffset:=-1;
  1000. end;
  1001. function taicpu.CheckIfValid:boolean;
  1002. begin
  1003. result:=FindInsEntry;
  1004. end;
  1005. function taicpu.FindInsentry:boolean;
  1006. var
  1007. i : longint;
  1008. begin
  1009. result:=false;
  1010. { Things which may only be done once, not when a second pass is done to
  1011. optimize }
  1012. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1013. begin
  1014. { We need intel style operands }
  1015. SetOperandOrder(op_intel);
  1016. { create the .ot fields }
  1017. create_ot;
  1018. { set the file postion }
  1019. aktfilepos:=fileinfo;
  1020. end
  1021. else
  1022. begin
  1023. { we've already an insentry so it's valid }
  1024. result:=true;
  1025. exit;
  1026. end;
  1027. { Lookup opcode in the table }
  1028. InsSize:=-1;
  1029. i:=instabcache^[opcode];
  1030. if i=-1 then
  1031. begin
  1032. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1033. exit;
  1034. end;
  1035. insentry:=@instab[i];
  1036. while (insentry^.opcode=opcode) do
  1037. begin
  1038. if matches(insentry)=100 then
  1039. begin
  1040. result:=true;
  1041. exit;
  1042. end;
  1043. inc(i);
  1044. insentry:=@instab[i];
  1045. end;
  1046. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1047. { No instruction found, set insentry to nil and inssize to -1 }
  1048. insentry:=nil;
  1049. inssize:=-1;
  1050. end;
  1051. function taicpu.Pass1(offset:longint):longint;
  1052. begin
  1053. Pass1:=0;
  1054. { Save the old offset and set the new offset }
  1055. InsOffset:=Offset;
  1056. { Things which may only be done once, not when a second pass is done to
  1057. optimize }
  1058. if Insentry=nil then
  1059. begin
  1060. { Check if error last time then InsSize=-1 }
  1061. if InsSize=-1 then
  1062. exit;
  1063. { set the file postion }
  1064. aktfilepos:=fileinfo;
  1065. end
  1066. else
  1067. begin
  1068. {$ifdef PASS2FLAG}
  1069. { we are here in a second pass, check if the instruction can be optimized }
  1070. if (InsEntry^.flags and IF_PASS2)=0 then
  1071. begin
  1072. Pass1:=InsSize;
  1073. exit;
  1074. end;
  1075. { update the .ot fields, some top_const can be updated }
  1076. create_ot;
  1077. {$endif PASS2FLAG}
  1078. end;
  1079. { Get InsEntry }
  1080. if FindInsEntry then
  1081. begin
  1082. { Calculate instruction size }
  1083. InsSize:=calcsize(insentry);
  1084. if segprefix<>NR_NO then
  1085. inc(InsSize);
  1086. { Fix opsize if size if forced }
  1087. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1088. begin
  1089. if (insentry^.flags and IF_ARMASK)=0 then
  1090. begin
  1091. if (insentry^.flags and IF_SB)<>0 then
  1092. begin
  1093. if opsize=S_NO then
  1094. opsize:=S_B;
  1095. end
  1096. else if (insentry^.flags and IF_SW)<>0 then
  1097. begin
  1098. if opsize=S_NO then
  1099. opsize:=S_W;
  1100. end
  1101. else if (insentry^.flags and IF_SD)<>0 then
  1102. begin
  1103. if opsize=S_NO then
  1104. opsize:=S_L;
  1105. end;
  1106. end;
  1107. end;
  1108. LastInsOffset:=InsOffset;
  1109. Pass1:=InsSize;
  1110. exit;
  1111. end;
  1112. LastInsOffset:=-1;
  1113. end;
  1114. procedure taicpu.Pass2(sec:TAsmObjectData);
  1115. var
  1116. c : longint;
  1117. begin
  1118. { error in pass1 ? }
  1119. if insentry=nil then
  1120. exit;
  1121. aktfilepos:=fileinfo;
  1122. { Segment override }
  1123. if (segprefix<>NR_NO) then
  1124. begin
  1125. case segprefix of
  1126. NR_CS : c:=$2e;
  1127. NR_DS : c:=$3e;
  1128. NR_ES : c:=$26;
  1129. NR_FS : c:=$64;
  1130. NR_GS : c:=$65;
  1131. NR_SS : c:=$36;
  1132. end;
  1133. sec.writebytes(c,1);
  1134. { fix the offset for GenNode }
  1135. inc(InsOffset);
  1136. end;
  1137. { Generate the instruction }
  1138. GenCode(sec);
  1139. end;
  1140. function taicpu.needaddrprefix(opidx:byte):boolean;
  1141. begin
  1142. needaddrprefix:=false;
  1143. if (OT_MEMORY and (not oper[opidx]^.ot))=0 then
  1144. begin
  1145. if (
  1146. (oper[opidx]^.ref^.index<>NR_NO) and
  1147. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBD)
  1148. ) or
  1149. (
  1150. (oper[opidx]^.ref^.base<>NR_NO) and
  1151. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBD)
  1152. ) then
  1153. needaddrprefix:=true;
  1154. end;
  1155. end;
  1156. function regval(r:Tregister):byte;
  1157. const
  1158. {$ifdef x86_64}
  1159. opcode_table:array[tregisterindex] of tregisterindex = (
  1160. {$i r8664op.inc}
  1161. );
  1162. {$else x86_64}
  1163. opcode_table:array[tregisterindex] of tregisterindex = (
  1164. {$i r386op.inc}
  1165. );
  1166. {$endif x86_64}
  1167. var
  1168. regidx : tregisterindex;
  1169. begin
  1170. regidx:=findreg_by_number(r);
  1171. if regidx<>0 then
  1172. result:=opcode_table[regidx]
  1173. else
  1174. begin
  1175. Message1(asmw_e_invalid_register,generic_regname(r));
  1176. result:=0;
  1177. end;
  1178. end;
  1179. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1180. var
  1181. sym : tasmsymbol;
  1182. md,s,rv : byte;
  1183. base,index,scalefactor,
  1184. o : longint;
  1185. ir,br : Tregister;
  1186. isub,bsub : tsubregister;
  1187. begin
  1188. process_ea:=false;
  1189. {Register ?}
  1190. if (input.typ=top_reg) then
  1191. begin
  1192. rv:=regval(input.reg);
  1193. output.sib_present:=false;
  1194. output.bytes:=0;
  1195. output.modrm:=$c0 or (rfield shl 3) or rv;
  1196. output.size:=1;
  1197. process_ea:=true;
  1198. exit;
  1199. end;
  1200. {No register, so memory reference.}
  1201. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1202. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1203. internalerror(200301081);
  1204. ir:=input.ref^.index;
  1205. br:=input.ref^.base;
  1206. isub:=getsubreg(ir);
  1207. bsub:=getsubreg(br);
  1208. s:=input.ref^.scalefactor;
  1209. o:=input.ref^.offset;
  1210. sym:=input.ref^.symbol;
  1211. { it's direct address }
  1212. if (br=NR_NO) and (ir=NR_NO) then
  1213. begin
  1214. { it's a pure offset }
  1215. output.sib_present:=false;
  1216. output.bytes:=4;
  1217. output.modrm:=5 or (rfield shl 3);
  1218. end
  1219. else
  1220. { it's an indirection }
  1221. begin
  1222. { 16 bit address? }
  1223. if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  1224. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  1225. message(asmw_e_16bit_not_supported);
  1226. {$ifdef OPTEA}
  1227. { make single reg base }
  1228. if (br=NR_NO) and (s=1) then
  1229. begin
  1230. br:=ir;
  1231. ir:=NR_NO;
  1232. end;
  1233. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1234. if (br=NR_NO) and
  1235. (((s=2) and (ir<>NR_ESP)) or
  1236. (s=3) or (s=5) or (s=9)) then
  1237. begin
  1238. br:=ir;
  1239. dec(s);
  1240. end;
  1241. { swap ESP into base if scalefactor is 1 }
  1242. if (s=1) and (ir=NR_ESP) then
  1243. begin
  1244. ir:=br;
  1245. br:=NR_ESP;
  1246. end;
  1247. {$endif OPTEA}
  1248. { wrong, for various reasons }
  1249. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1250. exit;
  1251. { base }
  1252. case br of
  1253. NR_EAX : base:=0;
  1254. NR_ECX : base:=1;
  1255. NR_EDX : base:=2;
  1256. NR_EBX : base:=3;
  1257. NR_ESP : base:=4;
  1258. NR_NO,
  1259. NR_EBP : base:=5;
  1260. NR_ESI : base:=6;
  1261. NR_EDI : base:=7;
  1262. else
  1263. exit;
  1264. end;
  1265. { index }
  1266. case ir of
  1267. NR_EAX : index:=0;
  1268. NR_ECX : index:=1;
  1269. NR_EDX : index:=2;
  1270. NR_EBX : index:=3;
  1271. NR_NO : index:=4;
  1272. NR_EBP : index:=5;
  1273. NR_ESI : index:=6;
  1274. NR_EDI : index:=7;
  1275. else
  1276. exit;
  1277. end;
  1278. case s of
  1279. 0,
  1280. 1 : scalefactor:=0;
  1281. 2 : scalefactor:=1;
  1282. 4 : scalefactor:=2;
  1283. 8 : scalefactor:=3;
  1284. else
  1285. exit;
  1286. end;
  1287. if (br=NR_NO) or
  1288. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1289. md:=0
  1290. else
  1291. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1292. md:=1
  1293. else
  1294. md:=2;
  1295. if (br=NR_NO) or (md=2) then
  1296. output.bytes:=4
  1297. else
  1298. output.bytes:=md;
  1299. { SIB needed ? }
  1300. if (ir=NR_NO) and (br<>NR_ESP) then
  1301. begin
  1302. output.sib_present:=false;
  1303. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1304. end
  1305. else
  1306. begin
  1307. output.sib_present:=true;
  1308. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1309. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1310. end;
  1311. end;
  1312. if output.sib_present then
  1313. output.size:=2+output.bytes
  1314. else
  1315. output.size:=1+output.bytes;
  1316. process_ea:=true;
  1317. end;
  1318. function taicpu.calcsize(p:PInsEntry):longint;
  1319. var
  1320. codes : pchar;
  1321. c : byte;
  1322. len : longint;
  1323. ea_data : ea;
  1324. begin
  1325. len:=0;
  1326. codes:=@p^.code;
  1327. repeat
  1328. c:=ord(codes^);
  1329. inc(codes);
  1330. case c of
  1331. 0 :
  1332. break;
  1333. 1,2,3 :
  1334. begin
  1335. inc(codes,c);
  1336. inc(len,c);
  1337. end;
  1338. 8,9,10 :
  1339. begin
  1340. inc(codes);
  1341. inc(len);
  1342. end;
  1343. 4,5,6,7 :
  1344. begin
  1345. if opsize=S_W then
  1346. inc(len,2)
  1347. else
  1348. inc(len);
  1349. end;
  1350. 15,
  1351. 12,13,14,
  1352. 16,17,18,
  1353. 20,21,22,
  1354. 40,41,42 :
  1355. inc(len);
  1356. 24,25,26,
  1357. 31,
  1358. 48,49,50 :
  1359. inc(len,2);
  1360. 28,29,30, { we don't have 16 bit immediates code }
  1361. 32,33,34,
  1362. 52,53,54,
  1363. 56,57,58 :
  1364. inc(len,4);
  1365. 192,193,194 :
  1366. if NeedAddrPrefix(c-192) then
  1367. inc(len);
  1368. 208 :
  1369. inc(len);
  1370. 200,
  1371. 201,
  1372. 202,
  1373. 209,
  1374. 210,
  1375. 217,218,219 : ;
  1376. 216 :
  1377. begin
  1378. inc(codes);
  1379. inc(len);
  1380. end;
  1381. 224,225,226 :
  1382. begin
  1383. InternalError(777002);
  1384. end;
  1385. else
  1386. begin
  1387. if (c>=64) and (c<=191) then
  1388. begin
  1389. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1390. Message(asmw_e_invalid_effective_address)
  1391. else
  1392. inc(len,ea_data.size);
  1393. end
  1394. else
  1395. InternalError(777003);
  1396. end;
  1397. end;
  1398. until false;
  1399. calcsize:=len;
  1400. end;
  1401. procedure taicpu.GenCode(sec:TAsmObjectData);
  1402. {
  1403. * the actual codes (C syntax, i.e. octal):
  1404. * \0 - terminates the code. (Unless it's a literal of course.)
  1405. * \1, \2, \3 - that many literal bytes follow in the code stream
  1406. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1407. * (POP is never used for CS) depending on operand 0
  1408. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1409. * on operand 0
  1410. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1411. * to the register value of operand 0, 1 or 2
  1412. * \17 - encodes the literal byte 0. (Some compilers don't take
  1413. * kindly to a zero byte in the _middle_ of a compile time
  1414. * string constant, so I had to put this hack in.)
  1415. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1416. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1417. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1418. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1419. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1420. * assembly mode or the address-size override on the operand
  1421. * \37 - a word constant, from the _segment_ part of operand 0
  1422. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1423. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1424. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1425. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1426. * assembly mode or the address-size override on the operand
  1427. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1428. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1429. * field the register value of operand b.
  1430. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1431. * field equal to digit b.
  1432. * \30x - might be an 0x67 byte, depending on the address size of
  1433. * the memory reference in operand x.
  1434. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1435. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1436. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1437. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1438. * \322 - indicates that this instruction is only valid when the
  1439. * operand size is the default (instruction to disassembler,
  1440. * generates no code in the assembler)
  1441. * \330 - a literal byte follows in the code stream, to be added
  1442. * to the condition code value of the instruction.
  1443. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1444. * Operand 0 had better be a segmentless constant.
  1445. }
  1446. var
  1447. currval : longint;
  1448. currsym : tasmsymbol;
  1449. procedure getvalsym(opidx:longint);
  1450. begin
  1451. case oper[opidx]^.typ of
  1452. top_ref :
  1453. begin
  1454. currval:=oper[opidx]^.ref^.offset;
  1455. currsym:=oper[opidx]^.ref^.symbol;
  1456. end;
  1457. top_const :
  1458. begin
  1459. currval:=longint(oper[opidx]^.val);
  1460. currsym:=nil;
  1461. end;
  1462. top_symbol :
  1463. begin
  1464. currval:=oper[opidx]^.symofs;
  1465. currsym:=oper[opidx]^.sym;
  1466. end;
  1467. else
  1468. Message(asmw_e_immediate_or_reference_expected);
  1469. end;
  1470. end;
  1471. const
  1472. CondVal:array[TAsmCond] of byte=($0,
  1473. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1474. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1475. $0, $A, $A, $B, $8, $4);
  1476. var
  1477. c : byte;
  1478. pb,
  1479. codes : pchar;
  1480. bytes : array[0..3] of byte;
  1481. rfield,
  1482. data,s,opidx : longint;
  1483. ea_data : ea;
  1484. begin
  1485. {$ifdef EXTDEBUG}
  1486. { safety check }
  1487. if sec.sects[sec.currsec].datasize<>insoffset then
  1488. internalerror(200130121);
  1489. {$endif EXTDEBUG}
  1490. { load data to write }
  1491. codes:=insentry^.code;
  1492. { Force word push/pop for registers }
  1493. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1494. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1495. begin
  1496. bytes[0]:=$66;
  1497. sec.writebytes(bytes,1);
  1498. end;
  1499. repeat
  1500. c:=ord(codes^);
  1501. inc(codes);
  1502. case c of
  1503. 0 :
  1504. break;
  1505. 1,2,3 :
  1506. begin
  1507. sec.writebytes(codes^,c);
  1508. inc(codes,c);
  1509. end;
  1510. 4,6 :
  1511. begin
  1512. case oper[0]^.reg of
  1513. NR_CS:
  1514. bytes[0]:=$e;
  1515. NR_NO,
  1516. NR_DS:
  1517. bytes[0]:=$1e;
  1518. NR_ES:
  1519. bytes[0]:=$6;
  1520. NR_SS:
  1521. bytes[0]:=$16;
  1522. else
  1523. internalerror(777004);
  1524. end;
  1525. if c=4 then
  1526. inc(bytes[0]);
  1527. sec.writebytes(bytes,1);
  1528. end;
  1529. 5,7 :
  1530. begin
  1531. case oper[0]^.reg of
  1532. NR_FS:
  1533. bytes[0]:=$a0;
  1534. NR_GS:
  1535. bytes[0]:=$a8;
  1536. else
  1537. internalerror(777005);
  1538. end;
  1539. if c=5 then
  1540. inc(bytes[0]);
  1541. sec.writebytes(bytes,1);
  1542. end;
  1543. 8,9,10 :
  1544. begin
  1545. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1546. inc(codes);
  1547. sec.writebytes(bytes,1);
  1548. end;
  1549. 15 :
  1550. begin
  1551. bytes[0]:=0;
  1552. sec.writebytes(bytes,1);
  1553. end;
  1554. 12,13,14 :
  1555. begin
  1556. getvalsym(c-12);
  1557. if (currval<-128) or (currval>127) then
  1558. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1559. if assigned(currsym) then
  1560. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1561. else
  1562. sec.writebytes(currval,1);
  1563. end;
  1564. 16,17,18 :
  1565. begin
  1566. getvalsym(c-16);
  1567. if (currval<-256) or (currval>255) then
  1568. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1569. if assigned(currsym) then
  1570. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1571. else
  1572. sec.writebytes(currval,1);
  1573. end;
  1574. 20,21,22 :
  1575. begin
  1576. getvalsym(c-20);
  1577. if (currval<0) or (currval>255) then
  1578. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1579. if assigned(currsym) then
  1580. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1581. else
  1582. sec.writebytes(currval,1);
  1583. end;
  1584. 24,25,26 :
  1585. begin
  1586. getvalsym(c-24);
  1587. if (currval<-65536) or (currval>65535) then
  1588. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1589. if assigned(currsym) then
  1590. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1591. else
  1592. sec.writebytes(currval,2);
  1593. end;
  1594. 28,29,30 :
  1595. begin
  1596. getvalsym(c-28);
  1597. if assigned(currsym) then
  1598. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1599. else
  1600. sec.writebytes(currval,4);
  1601. end;
  1602. 32,33,34 :
  1603. begin
  1604. getvalsym(c-32);
  1605. if assigned(currsym) then
  1606. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1607. else
  1608. sec.writebytes(currval,4);
  1609. end;
  1610. 40,41,42 :
  1611. begin
  1612. getvalsym(c-40);
  1613. data:=currval-insend;
  1614. if assigned(currsym) then
  1615. inc(data,currsym.address);
  1616. if (data>127) or (data<-128) then
  1617. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1618. sec.writebytes(data,1);
  1619. end;
  1620. 52,53,54 :
  1621. begin
  1622. getvalsym(c-52);
  1623. if assigned(currsym) then
  1624. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1625. else
  1626. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1627. end;
  1628. 56,57,58 :
  1629. begin
  1630. getvalsym(c-56);
  1631. if assigned(currsym) then
  1632. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1633. else
  1634. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1635. end;
  1636. 192,193,194 :
  1637. begin
  1638. if NeedAddrPrefix(c-192) then
  1639. begin
  1640. bytes[0]:=$67;
  1641. sec.writebytes(bytes,1);
  1642. end;
  1643. end;
  1644. 200 :
  1645. begin
  1646. bytes[0]:=$67;
  1647. sec.writebytes(bytes,1);
  1648. end;
  1649. 208 :
  1650. begin
  1651. bytes[0]:=$66;
  1652. sec.writebytes(bytes,1);
  1653. end;
  1654. 216 :
  1655. begin
  1656. bytes[0]:=ord(codes^)+condval[condition];
  1657. inc(codes);
  1658. sec.writebytes(bytes,1);
  1659. end;
  1660. 201,
  1661. 202,
  1662. 209,
  1663. 210,
  1664. 217,218,219 :
  1665. begin
  1666. { these are dissambler hints or 32 bit prefixes which
  1667. are not needed }
  1668. end;
  1669. 31,
  1670. 48,49,50,
  1671. 224,225,226 :
  1672. begin
  1673. InternalError(777006);
  1674. end
  1675. else
  1676. begin
  1677. if (c>=64) and (c<=191) then
  1678. begin
  1679. if (c<127) then
  1680. begin
  1681. if (oper[c and 7]^.typ=top_reg) then
  1682. rfield:=regval(oper[c and 7]^.reg)
  1683. else
  1684. rfield:=regval(oper[c and 7]^.ref^.base);
  1685. end
  1686. else
  1687. rfield:=c and 7;
  1688. opidx:=(c shr 3) and 7;
  1689. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1690. Message(asmw_e_invalid_effective_address);
  1691. pb:=@bytes;
  1692. pb^:=chr(ea_data.modrm);
  1693. inc(pb);
  1694. if ea_data.sib_present then
  1695. begin
  1696. pb^:=chr(ea_data.sib);
  1697. inc(pb);
  1698. end;
  1699. s:=pb-pchar(@bytes);
  1700. sec.writebytes(bytes,s);
  1701. case ea_data.bytes of
  1702. 0 : ;
  1703. 1 :
  1704. begin
  1705. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1706. sec.writereloc(oper[opidx]^.ref^.offset,1,oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE)
  1707. else
  1708. begin
  1709. bytes[0]:=oper[opidx]^.ref^.offset;
  1710. sec.writebytes(bytes,1);
  1711. end;
  1712. inc(s);
  1713. end;
  1714. 2,4 :
  1715. begin
  1716. sec.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1717. oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE);
  1718. inc(s,ea_data.bytes);
  1719. end;
  1720. end;
  1721. end
  1722. else
  1723. InternalError(777007);
  1724. end;
  1725. end;
  1726. until false;
  1727. end;
  1728. {$endif NOAG386BIN}
  1729. function Taicpu.is_nop:boolean;
  1730. begin
  1731. {We do not check the number of operands; we assume that nobody constructs
  1732. a mov or xchg instruction with less than 2 operands. (DM)}
  1733. is_nop:=(opcode=A_NOP) or
  1734. (opcode=A_MOV) and (oper[0]^.typ=top_reg) and (oper[1]^.typ=top_reg) and (oper[0]^.reg=oper[1]^.reg) or
  1735. (opcode=A_XCHG) and (oper[0]^.typ=top_reg) and (oper[1]^.typ=top_reg) and (oper[0]^.reg=oper[1]^.reg);
  1736. end;
  1737. function Taicpu.is_move:boolean;
  1738. begin
  1739. {We do not check the number of operands; we assume that nobody constructs
  1740. a mov, movzx or movsx instruction with less than 2 operands. Note that
  1741. a move between a reference and a register is not a move that is of
  1742. interrest to the register allocation, therefore we only return true
  1743. for a move between two registers. (DM)}
  1744. is_move:=((opcode=A_MOV) or (opcode=A_MOVZX) or (opcode=A_MOVSX)) and
  1745. ((oper[0]^.typ=top_reg) and (oper[1]^.typ=top_reg));
  1746. end;
  1747. function Taicpu.spill_registers(list:Taasmoutput;
  1748. rgget:Trggetproc;
  1749. rgunget:Trgungetproc;
  1750. const r:Tsuperregisterset;
  1751. var unusedregsint:Tsuperregisterset;
  1752. const spilltemplist:Tspill_temp_list):boolean;
  1753. {Spill the registers in r in this instruction. Returns true if any help
  1754. registers are used. This procedure has become one big hack party, because
  1755. of the huge amount of situations you can have. The irregularity of the i386
  1756. instruction set doesn't help either. (DM)}
  1757. var i:byte;
  1758. supreg:Tsuperregister;
  1759. subreg:Tsubregister;
  1760. helpreg:Tregister;
  1761. helpins:Taicpu;
  1762. op:Tasmop;
  1763. hopsize:Topsize;
  1764. pos:Tai;
  1765. begin
  1766. {Situation examples are in intel notation, so operand order:
  1767. mov eax , ebx
  1768. ^^^ ^^^
  1769. oper[1] oper[0]
  1770. (DM)}
  1771. spill_registers:=false;
  1772. case ops of
  1773. 1:
  1774. begin
  1775. if (oper[0]^.typ=top_reg) and
  1776. (getregtype(oper[0]^.reg)=R_INTREGISTER) then
  1777. begin
  1778. supreg:=getsupreg(oper[0]^.reg);
  1779. if supregset_in(r,supreg) then
  1780. begin
  1781. {Situation example:
  1782. push r20d ; r20d must be spilled into [ebp-12]
  1783. Change into:
  1784. push [ebp-12] ; Replace register by reference }
  1785. { hopsize:=reg2opsize(oper[0].reg);}
  1786. oper[0]^.typ:=top_ref;
  1787. new(oper[0]^.ref);
  1788. oper[0]^.ref^:=spilltemplist[supreg];
  1789. { oper[0]^.ref^.size:=hopsize;}
  1790. end;
  1791. end;
  1792. if oper[0]^.typ=top_ref then
  1793. begin
  1794. supreg:=getsupreg(oper[0]^.ref^.base);
  1795. if supregset_in(r,supreg) then
  1796. begin
  1797. {Situation example:
  1798. push [r21d+4*r22d] ; r21d must be spilled into [ebp-12]
  1799. Change into:
  1800. mov r23d,[ebp-12] ; Use a help register
  1801. push [r23d+4*r22d] ; Replace register by helpregister }
  1802. subreg:=getsubreg(oper[0]^.ref^.base);
  1803. if oper[0]^.ref^.index=NR_NO then
  1804. pos:=Tai(previous)
  1805. else
  1806. pos:=get_insert_pos(Tai(previous),getsupreg(oper[0]^.ref^.index),RS_INVALID,RS_INVALID,unusedregsint);
  1807. rgget(list,pos,subreg,helpreg);
  1808. spill_registers:=true;
  1809. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0]^.ref^.base),spilltemplist[supreg],helpreg);
  1810. if pos=nil then
  1811. list.insertafter(helpins,list.first)
  1812. else
  1813. list.insertafter(helpins,pos.next);
  1814. rgunget(list,helpins,helpreg);
  1815. forward_allocation(Tai(helpins.next),unusedregsint);
  1816. oper[0]^.ref^.base:=helpreg;
  1817. end;
  1818. supreg:=getsupreg(oper[0]^.ref^.index);
  1819. if supregset_in(r,supreg) then
  1820. begin
  1821. {Situation example:
  1822. push [r21d+4*r22d] ; r22d must be spilled into [ebp-12]
  1823. Change into:
  1824. mov r23d,[ebp-12] ; Use a help register
  1825. push [r21d+4*r23d] ; Replace register by helpregister }
  1826. subreg:=getsubreg(oper[0]^.ref^.index);
  1827. if oper[0]^.ref^.base=NR_NO then
  1828. pos:=Tai(previous)
  1829. else
  1830. pos:=get_insert_pos(Tai(previous),getsupreg(oper[0]^.ref^.base),RS_INVALID,RS_INVALID,unusedregsint);
  1831. rgget(list,pos,subreg,helpreg);
  1832. spill_registers:=true;
  1833. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0]^.ref^.index),spilltemplist[supreg],helpreg);
  1834. if pos=nil then
  1835. list.insertafter(helpins,list.first)
  1836. else
  1837. list.insertafter(helpins,pos.next);
  1838. rgunget(list,helpins,helpreg);
  1839. forward_allocation(Tai(helpins.next),unusedregsint);
  1840. oper[0]^.ref^.index:=helpreg;
  1841. end;
  1842. end;
  1843. end;
  1844. 2:
  1845. begin
  1846. { First spill the registers from the references. This is
  1847. required because the reference can be moved from this instruction
  1848. to a MOV instruction when spilling of the register operand is done }
  1849. for i:=0 to 1 do
  1850. if oper[i]^.typ=top_ref then
  1851. begin
  1852. supreg:=getsupreg(oper[i]^.ref^.base);
  1853. if supregset_in(r,supreg) then
  1854. begin
  1855. {Situation example:
  1856. add r20d,[r21d+4*r22d] ; r21d must be spilled into [ebp-12]
  1857. Change into:
  1858. mov r23d,[ebp-12] ; Use a help register
  1859. add r20d,[r23d+4*r22d] ; Replace register by helpregister }
  1860. subreg:=getsubreg(oper[i]^.ref^.base);
  1861. if i=1 then
  1862. pos:=get_insert_pos(Tai(previous),getsupreg(oper[i]^.ref^.index),getsupreg(oper[0]^.reg),
  1863. RS_INVALID,unusedregsint)
  1864. else
  1865. pos:=get_insert_pos(Tai(previous),getsupreg(oper[i]^.ref^.index),RS_INVALID,RS_INVALID,unusedregsint);
  1866. rgget(list,pos,subreg,helpreg);
  1867. spill_registers:=true;
  1868. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[i]^.ref^.base),spilltemplist[supreg],helpreg);
  1869. if pos=nil then
  1870. list.insertafter(helpins,list.first)
  1871. else
  1872. list.insertafter(helpins,pos.next);
  1873. oper[i]^.ref^.base:=helpreg;
  1874. rgunget(list,helpins,helpreg);
  1875. forward_allocation(Tai(helpins.next),unusedregsint);
  1876. end;
  1877. supreg:=getsupreg(oper[i]^.ref^.index);
  1878. if supregset_in(r,supreg) then
  1879. begin
  1880. {Situation example:
  1881. add r20d,[r21d+4*r22d] ; r22d must be spilled into [ebp-12]
  1882. Change into:
  1883. mov r23d,[ebp-12] ; Use a help register
  1884. add r20d,[r21d+4*r23d] ; Replace register by helpregister }
  1885. subreg:=getsubreg(oper[i]^.ref^.index);
  1886. if i=1 then
  1887. pos:=get_insert_pos(Tai(previous),getsupreg(oper[i]^.ref^.base),getsupreg(oper[0]^.reg),
  1888. RS_INVALID,unusedregsint)
  1889. else
  1890. pos:=get_insert_pos(Tai(previous),getsupreg(oper[i]^.ref^.base),RS_INVALID,RS_INVALID,unusedregsint);
  1891. rgget(list,pos,subreg,helpreg);
  1892. spill_registers:=true;
  1893. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[i]^.ref^.index),spilltemplist[supreg],helpreg);
  1894. if pos=nil then
  1895. list.insertafter(helpins,list.first)
  1896. else
  1897. list.insertafter(helpins,pos.next);
  1898. oper[i]^.ref^.index:=helpreg;
  1899. rgunget(list,helpins,helpreg);
  1900. forward_allocation(Tai(helpins.next),unusedregsint);
  1901. end;
  1902. end;
  1903. if (oper[0]^.typ=top_reg) and
  1904. (getregtype(oper[0]^.reg)=R_INTREGISTER) then
  1905. begin
  1906. supreg:=getsupreg(oper[0]^.reg);
  1907. subreg:=getsubreg(oper[0]^.reg);
  1908. if supregset_in(r,supreg) then
  1909. if oper[1]^.typ=top_ref then
  1910. begin
  1911. {Situation example:
  1912. add [r20d],r21d ; r21d must be spilled into [ebp-12]
  1913. Change into:
  1914. mov r22d,[ebp-12] ; Use a help register
  1915. add [r20d],r22d ; Replace register by helpregister }
  1916. pos:=get_insert_pos(Tai(previous),getsupreg(oper[0]^.reg),
  1917. getsupreg(oper[1]^.ref^.base),getsupreg(oper[1]^.ref^.index),
  1918. unusedregsint);
  1919. rgget(list,pos,subreg,helpreg);
  1920. spill_registers:=true;
  1921. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0]^.reg),spilltemplist[supreg],helpreg);
  1922. if pos=nil then
  1923. list.insertafter(helpins,list.first)
  1924. else
  1925. list.insertafter(helpins,pos.next);
  1926. oper[0]^.reg:=helpreg;
  1927. rgunget(list,helpins,helpreg);
  1928. forward_allocation(Tai(helpins.next),unusedregsint);
  1929. end
  1930. else
  1931. begin
  1932. {Situation example:
  1933. add r20d,r21d ; r21d must be spilled into [ebp-12]
  1934. Change into:
  1935. add r20d,[ebp-12] ; Replace register by reference }
  1936. oper[0]^.typ:=top_ref;
  1937. new(oper[0]^.ref);
  1938. oper[0]^.ref^:=spilltemplist[supreg];
  1939. end;
  1940. end;
  1941. if (oper[1]^.typ=top_reg) and
  1942. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  1943. begin
  1944. supreg:=getsupreg(oper[1]^.reg);
  1945. subreg:=getsubreg(oper[1]^.reg);
  1946. if supregset_in(r,supreg) then
  1947. begin
  1948. if oper[0]^.typ=top_ref then
  1949. begin
  1950. {Situation example:
  1951. add r20d,[r21d] ; r20d must be spilled into [ebp-12]
  1952. Change into:
  1953. mov r22d,[r21d] ; Use a help register
  1954. add [ebp-12],r22d ; Replace register by helpregister }
  1955. pos:=get_insert_pos(Tai(previous),getsupreg(oper[0]^.ref^.base),
  1956. getsupreg(oper[0]^.ref^.index),RS_INVALID,unusedregsint);
  1957. rgget(list,pos,subreg,helpreg);
  1958. spill_registers:=true;
  1959. op:=A_MOV;
  1960. hopsize:=opsize; {Save old value...}
  1961. if (opcode=A_MOVZX) or (opcode=A_MOVSX) or (opcode=A_LEA) then
  1962. begin
  1963. {Because 'movzx memory,register' does not exist...}
  1964. op:=opcode;
  1965. opcode:=A_MOV;
  1966. opsize:=reg2opsize(oper[1]^.reg);
  1967. end;
  1968. helpins:=Taicpu.op_ref_reg(op,hopsize,oper[0]^.ref^,helpreg);
  1969. if pos=nil then
  1970. list.insertafter(helpins,list.first)
  1971. else
  1972. list.insertafter(helpins,pos.next);
  1973. dispose(oper[0]^.ref);
  1974. oper[0]^.typ:=top_reg;
  1975. oper[0]^.reg:=helpreg;
  1976. oper[1]^.typ:=top_ref;
  1977. new(oper[1]^.ref);
  1978. oper[1]^.ref^:=spilltemplist[supreg];
  1979. rgunget(list,helpins,helpreg);
  1980. forward_allocation(Tai(helpins.next),unusedregsint);
  1981. end
  1982. else
  1983. begin
  1984. {Situation example:
  1985. add r20d,r21d ; r20d must be spilled into [ebp-12]
  1986. Change into:
  1987. add [ebp-12],r21d ; Replace register by reference }
  1988. if (opcode=A_MOVZX) or (opcode=A_MOVSX) then
  1989. begin
  1990. {Because 'movzx memory,register' does not exist...}
  1991. spill_registers:=true;
  1992. op:=opcode;
  1993. hopsize:=opsize;
  1994. opcode:=A_MOV;
  1995. opsize:=reg2opsize(oper[1]^.reg);
  1996. pos:=get_insert_pos(Tai(previous),getsupreg(oper[0]^.reg),RS_INVALID,RS_INVALID,unusedregsint);
  1997. rgget(list,pos,subreg,helpreg);
  1998. helpins:=Taicpu.op_reg_reg(op,hopsize,oper[0]^.reg,helpreg);
  1999. if pos=nil then
  2000. list.insertafter(helpins,list.first)
  2001. else
  2002. list.insertafter(helpins,pos.next);
  2003. oper[0]^.reg:=helpreg;
  2004. rgunget(list,helpins,helpreg);
  2005. forward_allocation(Tai(helpins.next),unusedregsint);
  2006. end;
  2007. oper[1]^.typ:=top_ref;
  2008. new(oper[1]^.ref);
  2009. oper[1]^.ref^:=spilltemplist[supreg];
  2010. end;
  2011. end;
  2012. end;
  2013. { The i386 instruction set never gets boring...
  2014. some opcodes do not support a memory location as destination }
  2015. if (oper[1]^.typ=top_ref) and
  2016. (
  2017. (oper[0]^.typ=top_const) or
  2018. ((oper[0]^.typ=top_reg) and
  2019. (getregtype(oper[0]^.reg)=R_INTREGISTER))
  2020. ) then
  2021. begin
  2022. case opcode of
  2023. A_IMUL :
  2024. begin
  2025. {Yikes! We just changed the destination register into
  2026. a memory location above here.
  2027. Situation examples:
  2028. imul [ebp-12],r21d ; We need a help register
  2029. imul [ebp-12],<const> ; We need a help register
  2030. Change into:
  2031. mov r22d,[ebp-12] ; Use a help instruction (only for IMUL)
  2032. imul r22d,r21d ; Replace reference by helpregister
  2033. mov [ebp-12],r22d ; Use another help instruction}
  2034. rgget(list,Tai(previous),subreg,helpreg);
  2035. spill_registers:=true;
  2036. {First help instruction.}
  2037. helpins:=Taicpu.op_ref_reg(A_MOV,opsize,oper[1]^.ref^,helpreg);
  2038. if previous=nil then
  2039. list.insert(helpins)
  2040. else
  2041. list.insertafter(helpins,previous);
  2042. {Second help instruction.}
  2043. helpins:=Taicpu.op_reg_ref(A_MOV,opsize,helpreg,oper[1]^.ref^);
  2044. dispose(oper[1]^.ref);
  2045. oper[1]^.typ:=top_reg;
  2046. oper[1]^.reg:=helpreg;
  2047. list.insertafter(helpins,self);
  2048. rgunget(list,self,helpreg);
  2049. end;
  2050. end;
  2051. end;
  2052. { The i386 instruction set never gets boring...
  2053. some opcodes do not support a memory location as source }
  2054. if (oper[0]^.typ=top_ref) and
  2055. (oper[1]^.typ=top_reg) and
  2056. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  2057. begin
  2058. case opcode of
  2059. A_BT,A_BTS,
  2060. A_BTC,A_BTR :
  2061. begin
  2062. {Yikes! We just changed the source register into
  2063. a memory location above here.
  2064. Situation example:
  2065. bt r21d,[ebp-12] ; We need a help register
  2066. Change into:
  2067. mov r22d,[ebp-12] ; Use a help instruction (only for IMUL)
  2068. bt r21d,r22d ; Replace reference by helpregister}
  2069. rgget(list,Tai(previous),subreg,helpreg);
  2070. spill_registers:=true;
  2071. {First help instruction.}
  2072. helpins:=Taicpu.op_ref_reg(A_MOV,opsize,oper[0]^.ref^,helpreg);
  2073. if previous=nil then
  2074. list.insert(helpins)
  2075. else
  2076. list.insertafter(helpins,previous);
  2077. dispose(oper[0]^.ref);
  2078. oper[0]^.typ:=top_reg;
  2079. oper[0]^.reg:=helpreg;
  2080. rgunget(list,helpins,helpreg);
  2081. end;
  2082. end;
  2083. end;
  2084. end;
  2085. 3:
  2086. begin
  2087. {$warning todo!!}
  2088. end;
  2089. end;
  2090. end;
  2091. {*****************************************************************************
  2092. Instruction table
  2093. *****************************************************************************}
  2094. procedure BuildInsTabCache;
  2095. {$ifndef NOAG386BIN}
  2096. var
  2097. i : longint;
  2098. {$endif}
  2099. begin
  2100. {$ifndef NOAG386BIN}
  2101. new(instabcache);
  2102. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2103. i:=0;
  2104. while (i<InsTabEntries) do
  2105. begin
  2106. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2107. InsTabCache^[InsTab[i].OPcode]:=i;
  2108. inc(i);
  2109. end;
  2110. {$endif NOAG386BIN}
  2111. end;
  2112. procedure InitAsm;
  2113. begin
  2114. {$ifndef NOAG386BIN}
  2115. if not assigned(instabcache) then
  2116. BuildInsTabCache;
  2117. {$endif NOAG386BIN}
  2118. end;
  2119. procedure DoneAsm;
  2120. begin
  2121. {$ifndef NOAG386BIN}
  2122. if assigned(instabcache) then
  2123. begin
  2124. dispose(instabcache);
  2125. instabcache:=nil;
  2126. end;
  2127. {$endif NOAG386BIN}
  2128. end;
  2129. end.
  2130. {
  2131. $Log$
  2132. Revision 1.37 2003-10-30 19:59:00 peter
  2133. * support scalefactor for opr_local
  2134. * support reference with opr_local set, fixes tw2631
  2135. Revision 1.36 2003/10/29 15:40:20 peter
  2136. * support indexing and offset retrieval for locals
  2137. Revision 1.35 2003/10/23 14:44:07 peter
  2138. * splitted buildderef and buildderefimpl to fix interface crc
  2139. calculation
  2140. Revision 1.34 2003/10/22 20:40:00 peter
  2141. * write derefdata in a separate ppu entry
  2142. Revision 1.33 2003/10/21 15:15:36 peter
  2143. * taicpu_abstract.oper[] changed to pointers
  2144. Revision 1.32 2003/10/17 14:38:32 peter
  2145. * 64k registers supported
  2146. * fixed some memory leaks
  2147. Revision 1.31 2003/10/09 21:31:37 daniel
  2148. * Register allocator splitted, ans abstract now
  2149. Revision 1.30 2003/10/01 20:34:50 peter
  2150. * procinfo unit contains tprocinfo
  2151. * cginfo renamed to cgbase
  2152. * moved cgmessage to verbose
  2153. * fixed ppc and sparc compiles
  2154. Revision 1.29 2003/09/29 20:58:56 peter
  2155. * optimized releasing of registers
  2156. Revision 1.28 2003/09/28 21:49:30 peter
  2157. * fixed invalid opcode handling in spill registers
  2158. Revision 1.27 2003/09/28 13:37:07 peter
  2159. * give error for wrong register number
  2160. Revision 1.26 2003/09/24 21:15:49 florian
  2161. * fixed make cycle
  2162. Revision 1.25 2003/09/24 17:12:36 florian
  2163. * x86-64 adaptions
  2164. Revision 1.24 2003/09/23 17:56:06 peter
  2165. * locals and paras are allocated in the code generation
  2166. * tvarsym.localloc contains the location of para/local when
  2167. generating code for the current procedure
  2168. Revision 1.23 2003/09/14 14:22:51 daniel
  2169. * Fixed incorrect movzx spilling
  2170. Revision 1.22 2003/09/12 20:25:17 daniel
  2171. * Add BTR to destination memory location check in spilling
  2172. Revision 1.21 2003/09/10 19:14:31 daniel
  2173. * Failed attempt to restore broken fastspill functionality
  2174. Revision 1.20 2003/09/10 11:23:09 marco
  2175. * fix from peter for bts reg32,mem32 problem
  2176. Revision 1.19 2003/09/09 12:54:45 florian
  2177. * x86 instruction table updated to nasm 0.98.37:
  2178. - sse3 aka prescott support
  2179. - small fixes
  2180. Revision 1.18 2003/09/07 22:09:35 peter
  2181. * preparations for different default calling conventions
  2182. * various RA fixes
  2183. Revision 1.17 2003/09/03 15:55:02 peter
  2184. * NEWRA branch merged
  2185. Revision 1.16.2.4 2003/08/31 15:46:26 peter
  2186. * more updates for tregister
  2187. Revision 1.16.2.3 2003/08/29 17:29:00 peter
  2188. * next batch of updates
  2189. Revision 1.16.2.2 2003/08/28 18:35:08 peter
  2190. * tregister changed to cardinal
  2191. Revision 1.16.2.1 2003/08/27 19:55:54 peter
  2192. * first tregister patch
  2193. Revision 1.16 2003/08/21 17:20:19 peter
  2194. * first spill the registers of top_ref before spilling top_reg
  2195. Revision 1.15 2003/08/21 14:48:36 peter
  2196. * fix reg-supreg range check error
  2197. Revision 1.14 2003/08/20 16:52:01 daniel
  2198. * Some old register convention code removed
  2199. * A few changes to eliminate a few lines of code
  2200. Revision 1.13 2003/08/20 09:07:00 daniel
  2201. * New register coding now mandatory, some more convert_registers calls
  2202. removed.
  2203. Revision 1.12 2003/08/20 07:48:04 daniel
  2204. * Made internal assembler use new register coding
  2205. Revision 1.11 2003/08/19 13:58:33 daniel
  2206. * Corrected a comment.
  2207. Revision 1.10 2003/08/15 14:44:20 daniel
  2208. * Fixed newra compilation
  2209. Revision 1.9 2003/08/11 21:18:20 peter
  2210. * start of sparc support for newra
  2211. Revision 1.8 2003/08/09 18:56:54 daniel
  2212. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  2213. allocator
  2214. * Some preventive changes to i386 spillinh code
  2215. Revision 1.7 2003/07/06 15:31:21 daniel
  2216. * Fixed register allocator. *Lots* of fixes.
  2217. Revision 1.6 2003/06/14 14:53:50 jonas
  2218. * fixed newra cycle for x86
  2219. * added constants for indicating source and destination operands of the
  2220. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2221. Revision 1.5 2003/06/03 13:01:59 daniel
  2222. * Register allocator finished
  2223. Revision 1.4 2003/05/30 23:57:08 peter
  2224. * more sparc cleanup
  2225. * accumulator removed, splitted in function_return_reg (called) and
  2226. function_result_reg (caller)
  2227. Revision 1.3 2003/05/22 21:33:31 peter
  2228. * removed some unit dependencies
  2229. Revision 1.2 2002/04/25 16:12:09 florian
  2230. * fixed more problems with cpubase and x86-64
  2231. Revision 1.1 2003/04/25 12:43:40 florian
  2232. * merged i386/aasmcpu and x86_64/aasmcpu to x86/aasmcpu
  2233. Revision 1.18 2003/04/25 12:04:31 florian
  2234. * merged agx64att and ag386att to x86/agx86att
  2235. Revision 1.17 2003/04/22 14:33:38 peter
  2236. * removed some notes/hints
  2237. Revision 1.16 2003/04/22 10:09:35 daniel
  2238. + Implemented the actual register allocator
  2239. + Scratch registers unavailable when new register allocator used
  2240. + maybe_save/maybe_restore unavailable when new register allocator used
  2241. Revision 1.15 2003/03/26 12:50:54 armin
  2242. * avoid problems with the ide in init/dome
  2243. Revision 1.14 2003/03/08 08:59:07 daniel
  2244. + $define newra will enable new register allocator
  2245. + getregisterint will return imaginary registers with $newra
  2246. + -sr switch added, will skip register allocation so you can see
  2247. the direct output of the code generator before register allocation
  2248. Revision 1.13 2003/02/25 07:41:54 daniel
  2249. * Properly fixed reversed operands bug
  2250. Revision 1.12 2003/02/19 22:00:15 daniel
  2251. * Code generator converted to new register notation
  2252. - Horribily outdated todo.txt removed
  2253. Revision 1.11 2003/01/09 20:40:59 daniel
  2254. * Converted some code in cgx86.pas to new register numbering
  2255. Revision 1.10 2003/01/08 18:43:57 daniel
  2256. * Tregister changed into a record
  2257. Revision 1.9 2003/01/05 13:36:53 florian
  2258. * x86-64 compiles
  2259. + very basic support for float128 type (x86-64 only)
  2260. Revision 1.8 2002/11/17 16:31:58 carl
  2261. * memory optimization (3-4%) : cleanup of tai fields,
  2262. cleanup of tdef and tsym fields.
  2263. * make it work for m68k
  2264. Revision 1.7 2002/11/15 01:58:54 peter
  2265. * merged changes from 1.0.7 up to 04-11
  2266. - -V option for generating bug report tracing
  2267. - more tracing for option parsing
  2268. - errors for cdecl and high()
  2269. - win32 import stabs
  2270. - win32 records<=8 are returned in eax:edx (turned off by default)
  2271. - heaptrc update
  2272. - more info for temp management in .s file with EXTDEBUG
  2273. Revision 1.6 2002/10/31 13:28:32 pierre
  2274. * correct last wrong fix for tw2158
  2275. Revision 1.5 2002/10/30 17:10:00 pierre
  2276. * merge of fix for tw2158 bug
  2277. Revision 1.4 2002/08/15 19:10:36 peter
  2278. * first things tai,tnode storing in ppu
  2279. Revision 1.3 2002/08/13 18:01:52 carl
  2280. * rename swatoperands to swapoperands
  2281. + m68k first compilable version (still needs a lot of testing):
  2282. assembler generator, system information , inline
  2283. assembler reader.
  2284. Revision 1.2 2002/07/20 11:57:59 florian
  2285. * types.pas renamed to defbase.pas because D6 contains a types
  2286. unit so this would conflicts if D6 programms are compiled
  2287. + Willamette/SSE2 instructions to assembler added
  2288. Revision 1.1 2002/07/01 18:46:29 peter
  2289. * internal linker
  2290. * reorganized aasm layer
  2291. }