aasmcpu.pas 86 KB

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