aasmcpu.pas 82 KB

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