aasmtai.pas 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an abstract asmoutput class for all processor types
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { @abstract(This unit implements an abstract asm output class for all processor types)
  19. This unit implements an abstract assembler output class for all processors, these
  20. are then overriden for each assembler writer to actually write the data in these
  21. classes to an assembler file.
  22. }
  23. unit aasmtai;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cutils,cclasses,
  28. globtype,globals,systems,
  29. cpuinfo,cpubase,
  30. cgbase,
  31. symppu,symtype,
  32. aasmbase;
  33. type
  34. taitype = (
  35. ait_none,
  36. ait_align,
  37. ait_section,
  38. ait_comment,
  39. ait_direct,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. ait_symbol_end, { needed to calc the size of a symbol }
  45. ait_label,
  46. ait_const_32bit,
  47. ait_const_16bit,
  48. ait_const_8bit,
  49. ait_const_symbol,
  50. { the following is only used by the win32 version of the compiler }
  51. { and only the GNU AS Win32 is able to write it }
  52. ait_const_rva,
  53. ait_real_32bit,
  54. ait_real_64bit,
  55. ait_real_80bit,
  56. ait_comp_64bit,
  57. ait_real_128bit,
  58. {$ifdef GDB}
  59. ait_stabn,
  60. ait_stabs,
  61. ait_force_line,
  62. ait_stab_function_name,
  63. {$endif GDB}
  64. {$ifdef alpha}
  65. { the follow is for the DEC Alpha }
  66. ait_frame,
  67. ait_ent,
  68. {$endif alpha}
  69. {$ifdef ia64}
  70. ait_bundle,
  71. ait_stop,
  72. {$endif ia64}
  73. {$ifdef m68k}
  74. ait_labeled_instruction,
  75. {$endif m68k}
  76. {$ifdef SPARC}
  77. ait_labeled_instruction,
  78. {$endif SPARC}
  79. ait_cut, { used to split into tiny assembler files }
  80. ait_regalloc,
  81. ait_tempalloc,
  82. ait_marker { used to mark assembler blocks and inlined functions }
  83. );
  84. const
  85. taitypestr : array[taitype] of string[14] = (
  86. '<none>',
  87. 'align',
  88. 'section',
  89. 'comment',
  90. 'direct',
  91. 'string',
  92. 'instruction',
  93. 'datablock',
  94. 'symbol',
  95. 'symbol_end',
  96. 'label',
  97. 'const_32bit',
  98. 'const_16bit',
  99. 'const_8bit',
  100. 'const_symbol',
  101. 'const_rva',
  102. 'real_32bit',
  103. 'real_64bit',
  104. 'real_80bit',
  105. 'comp_64bit',
  106. 'real_128bit',
  107. {$ifdef GDB}
  108. 'stabn',
  109. 'stabs',
  110. 'force_line',
  111. 'stab_funcname',
  112. {$endif GDB}
  113. {$ifdef alpha}
  114. { the follow is for the DEC Alpha }
  115. 'frame',
  116. 'ent',
  117. {$endif alpha}
  118. {$ifdef ia64}
  119. 'bundle',
  120. 'stop',
  121. {$endif ia64}
  122. {$ifdef m68k}
  123. 'labeled_instr',
  124. {$endif m68k}
  125. {$ifdef SPARC}
  126. 'labeled_instr',
  127. {$endif SPARC}
  128. 'cut',
  129. 'regalloc',
  130. 'tempalloc',
  131. 'marker'
  132. );
  133. type
  134. { Types of operand }
  135. toptype=(top_none,top_reg,top_ref,top_const,top_symbol,top_bool,top_local,
  136. { ARM only }
  137. top_regset,
  138. { ARM only }
  139. top_shifterop);
  140. toper=record
  141. ot : longint;
  142. case typ : toptype of
  143. top_none : ();
  144. top_reg : (reg:tregister);
  145. top_ref : (ref:preference);
  146. top_const : (val:aword);
  147. top_symbol : (sym:tasmsymbol;symofs:longint);
  148. top_bool : (b:boolean);
  149. { local varsym that will be inserted in pass_2 }
  150. top_local : (localsym:pointer;localsymderef:tderef;localsymofs:longint;localindexreg:tregister;
  151. localscale:byte;localgetoffset:boolean);
  152. {$ifdef arm}
  153. top_regset : (regset:^tcpuregisterset);
  154. top_shifterop : (shifterop : pshifterop);
  155. {$endif arm}
  156. end;
  157. poper=^toper;
  158. { ait_* types which don't result in executable code or which don't influence }
  159. { the way the program runs/behaves, but which may be encountered by the }
  160. { optimizer (= if it's sometimes added to the exprasm list). Update if you add }
  161. { a new ait type! }
  162. const
  163. SkipInstr = [ait_comment, ait_symbol,ait_section
  164. {$ifdef GDB}
  165. ,ait_stabs, ait_stabn, ait_stab_function_name, ait_force_line
  166. {$endif GDB}
  167. ,ait_regalloc, ait_tempalloc, ait_symbol_end];
  168. { ait_* types which do not have line information (and hence which are of type
  169. tai, otherwise, they are of type tailineinfo }
  170. { ait_* types which do not have line information (and hence which are of type
  171. tai, otherwise, they are of type tailineinfo }
  172. SkipLineInfo =[ait_label,
  173. ait_regalloc,ait_tempalloc,
  174. {$ifdef GDB}
  175. ait_stabn,ait_stabs,ait_stab_function_name,
  176. {$endif GDB}
  177. ait_cut,ait_marker,ait_align,ait_section,ait_comment,
  178. ait_const_8bit,ait_const_16bit,ait_const_32bit,
  179. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit
  180. ];
  181. type
  182. { cut type, required for alphanumeric ordering of the assembler filenames }
  183. TCutPlace=(cut_normal,cut_begin,cut_end);
  184. TMarker = (NoPropInfoStart,NoPropInfoEnd,
  185. AsmBlockStart,AsmBlockEnd,
  186. InlineStart,InlineEnd,marker_blockstart,
  187. marker_position);
  188. { Buffer type used for alignment }
  189. tfillbuffer = array[0..63] of char;
  190. Tspill_temp_list=array[tsuperregister] of Treference;
  191. { abstract assembler item }
  192. tai = class(TLinkedListItem)
  193. {$ifndef NOOPT}
  194. { pointer to record with optimizer info about this tai object }
  195. optinfo : pointer;
  196. {$endif NOOPT}
  197. typ : taitype;
  198. constructor Create;
  199. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  200. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  201. procedure buildderefimpl;virtual;
  202. procedure derefimpl;virtual;
  203. end;
  204. { abstract assembler item with line information }
  205. tailineinfo = class(tai)
  206. fileinfo : tfileposinfo;
  207. constructor Create;
  208. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  209. procedure ppuwrite(ppufile:tcompilerppufile);override;
  210. end;
  211. taiclass = class of tai;
  212. taiclassarray = array[taitype] of taiclass;
  213. { Generates an assembler string }
  214. tai_string = class(tailineinfo)
  215. str : pchar;
  216. { extra len so the string can contain an \0 }
  217. len : longint;
  218. constructor Create(const _str : string);
  219. constructor Create_pchar(_str : pchar);
  220. constructor Create_length_pchar(_str : pchar;length : longint);
  221. destructor Destroy;override;
  222. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  223. procedure ppuwrite(ppufile:tcompilerppufile);override;
  224. function getcopy:tlinkedlistitem;override;
  225. end;
  226. { Generates a common label }
  227. tai_symbol = class(tailineinfo)
  228. is_global : boolean;
  229. sym : tasmsymbol;
  230. size : longint;
  231. constructor Create(_sym:tasmsymbol;siz:longint);
  232. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  233. constructor Createname(const _name : string;siz:longint);
  234. constructor Createname_global(const _name : string;siz:longint);
  235. constructor Createdataname(const _name : string;siz:longint);
  236. constructor Createdataname_global(const _name : string;siz:longint);
  237. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  238. procedure ppuwrite(ppufile:tcompilerppufile);override;
  239. procedure derefimpl;override;
  240. end;
  241. tai_symbol_end = class(tailineinfo)
  242. sym : tasmsymbol;
  243. constructor Create(_sym:tasmsymbol);
  244. constructor Createname(const _name : string);
  245. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  246. procedure ppuwrite(ppufile:tcompilerppufile);override;
  247. procedure derefimpl;override;
  248. end;
  249. { Generates an assembler label }
  250. tai_label = class(tai)
  251. is_global : boolean;
  252. l : tasmlabel;
  253. constructor Create(_l : tasmlabel);
  254. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  255. procedure ppuwrite(ppufile:tcompilerppufile);override;
  256. procedure derefimpl;override;
  257. end;
  258. { Directly output data to final assembler file }
  259. tai_direct = class(tailineinfo)
  260. str : pchar;
  261. constructor Create(_str : pchar);
  262. destructor Destroy; override;
  263. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  264. procedure ppuwrite(ppufile:tcompilerppufile);override;
  265. function getcopy:tlinkedlistitem;override;
  266. end;
  267. { Generates an assembler comment }
  268. tai_comment = class(tai)
  269. str : pchar;
  270. constructor Create(_str : pchar);
  271. destructor Destroy; override;
  272. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  273. procedure ppuwrite(ppufile:tcompilerppufile);override;
  274. function getcopy:tlinkedlistitem;override;
  275. end;
  276. { Generates a section / segment directive }
  277. tai_section = class(tai)
  278. sec : TSection;
  279. constructor Create(s : TSection);
  280. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  281. procedure ppuwrite(ppufile:tcompilerppufile);override;
  282. end;
  283. { Generates an uninitializised data block }
  284. tai_datablock = class(tailineinfo)
  285. is_global : boolean;
  286. sym : tasmsymbol;
  287. size : longint;
  288. constructor Create(const _name : string;_size : longint);
  289. constructor Create_global(const _name : string;_size : longint);
  290. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  291. procedure ppuwrite(ppufile:tcompilerppufile);override;
  292. procedure derefimpl;override;
  293. end;
  294. { Generates a long integer (32 bit) }
  295. tai_const = class(tai)
  296. value : longint;
  297. constructor Create_32bit(_value : longint);
  298. constructor Create_16bit(_value : word);
  299. constructor Create_8bit(_value : byte);
  300. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  301. procedure ppuwrite(ppufile:tcompilerppufile);override;
  302. end;
  303. tai_const_symbol = class(tailineinfo)
  304. sym : tasmsymbol;
  305. offset : longint;
  306. constructor Create(_sym:tasmsymbol);
  307. constructor Create_offset(_sym:tasmsymbol;ofs:longint);
  308. constructor Create_rva(_sym:tasmsymbol);
  309. constructor Createname(const name:string);
  310. constructor Createname_offset(const name:string;ofs:longint);
  311. constructor Createname_rva(const name:string);
  312. constructor Createdataname(const name:string);
  313. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  314. procedure ppuwrite(ppufile:tcompilerppufile);override;
  315. procedure derefimpl;override;
  316. function getcopy:tlinkedlistitem;override;
  317. end;
  318. { Generates a single float (32 bit real) }
  319. tai_real_32bit = class(tai)
  320. value : ts32real;
  321. constructor Create(_value : ts32real);
  322. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. end;
  325. { Generates a double float (64 bit real) }
  326. tai_real_64bit = class(tai)
  327. value : ts64real;
  328. constructor Create(_value : ts64real);
  329. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  330. procedure ppuwrite(ppufile:tcompilerppufile);override;
  331. end;
  332. { Generates an extended float (80 bit real) }
  333. tai_real_80bit = class(tai)
  334. value : ts80real;
  335. constructor Create(_value : ts80real);
  336. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  337. procedure ppuwrite(ppufile:tcompilerppufile);override;
  338. end;
  339. { Generates an extended float (128 bit real) }
  340. tai_real_128bit = class(tai)
  341. value : ts128real;
  342. constructor Create(_value : ts128real);
  343. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  344. procedure ppuwrite(ppufile:tcompilerppufile);override;
  345. end;
  346. { Generates a comp int (integer over 64 bits)
  347. This is Intel 80x86 specific, and is not
  348. really supported on other processors.
  349. }
  350. tai_comp_64bit = class(tai)
  351. value : ts64comp;
  352. constructor Create(_value : ts64comp);
  353. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  354. procedure ppuwrite(ppufile:tcompilerppufile);override;
  355. end;
  356. { Insert a cut to split assembler into several smaller files }
  357. tai_cut = class(tai)
  358. place : tcutplace;
  359. constructor Create;
  360. constructor Create_begin;
  361. constructor Create_end;
  362. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  363. procedure ppuwrite(ppufile:tcompilerppufile);override;
  364. end;
  365. { Insert a marker for assembler and inline blocks }
  366. tai_marker = class(tai)
  367. Kind: TMarker;
  368. Constructor Create(_Kind: TMarker);
  369. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  370. procedure ppuwrite(ppufile:tcompilerppufile);override;
  371. end;
  372. tai_tempalloc = class(tai)
  373. allocation : boolean;
  374. {$ifdef EXTDEBUG}
  375. problem : pstring;
  376. {$endif EXTDEBUG}
  377. temppos,
  378. tempsize : longint;
  379. constructor alloc(pos,size:longint);
  380. constructor dealloc(pos,size:longint);
  381. {$ifdef EXTDEBUG}
  382. constructor allocinfo(pos,size:longint;const st:string);
  383. {$endif EXTDEBUG}
  384. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  385. destructor destroy;override;
  386. procedure ppuwrite(ppufile:tcompilerppufile);override;
  387. end;
  388. tai_regalloc = class(tai)
  389. allocation : boolean;
  390. reg : tregister;
  391. constructor alloc(r : tregister);
  392. constructor dealloc(r : tregister);
  393. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  394. procedure ppuwrite(ppufile:tcompilerppufile);override;
  395. end;
  396. Taasmoutput=class;
  397. Trggetproc=procedure(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister) of object;
  398. Trgungetproc=procedure(list:Taasmoutput;position:Tai;r:Tregister) of object;
  399. { Class template for assembler instructions
  400. }
  401. taicpu_abstract = class(tailineinfo)
  402. protected
  403. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;abstract;
  404. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;abstract;
  405. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  406. procedure ppuderefoper(var o:toper);virtual;abstract;
  407. public
  408. { Condition flags for instruction }
  409. condition : TAsmCond;
  410. { Number of operands to instruction }
  411. ops : byte;
  412. { Number of allocate oper structures }
  413. opercnt : byte;
  414. { Operands of instruction }
  415. oper : array[0..max_operands-1] of poper;
  416. { Actual opcode of instruction }
  417. opcode : tasmop;
  418. {$ifdef x86}
  419. segprefix : tregister;
  420. {$endif x86}
  421. { true if instruction is a jmp }
  422. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  423. Constructor Create(op : tasmop);
  424. Destructor Destroy;override;
  425. function getcopy:TLinkedListItem;override;
  426. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  427. procedure ppuwrite(ppufile:tcompilerppufile);override;
  428. procedure buildderefimpl;override;
  429. procedure derefimpl;override;
  430. procedure SetCondition(const c:TAsmCond);
  431. procedure allocate_oper(opers:longint);
  432. procedure loadconst(opidx:longint;l:aword);
  433. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  434. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset:boolean);
  435. procedure loadref(opidx:longint;const r:treference);
  436. procedure loadreg(opidx:longint;r:tregister);
  437. procedure loadoper(opidx:longint;o:toper);
  438. procedure clearop(opidx:longint);
  439. function is_nop:boolean;virtual;abstract;
  440. function is_move:boolean;virtual;abstract;
  441. { register allocator }
  442. function get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister;var unusedregsint:tsuperregisterset):Tai;
  443. procedure forward_allocation(p:Tai;var unusedregsint:tsuperregisterset);
  444. function spill_registers(list:Taasmoutput;
  445. rgget:Trggetproc;
  446. rgunget:Trgungetproc;
  447. const r:tsuperregisterset;
  448. var unusedregsint:tsuperregisterset;
  449. const spilltemplist:Tspill_temp_list):boolean;virtual;
  450. function spilling_decode_loadstore(op: tasmop; var counterpart: tasmop; var wasload: boolean): boolean;virtual;abstract;
  451. function spilling_create_loadstore(op: tasmop; r:tregister; const ref:treference): tai;virtual;abstract;
  452. function spilling_create_load(const ref:treference;r:tregister): tai;virtual;abstract;
  453. function spilling_create_store(r:tregister; const ref:treference): tai;virtual;abstract;
  454. end;
  455. { alignment for operator }
  456. tai_align_abstract = class(tai)
  457. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  458. fillsize : byte; { real size to fill }
  459. fillop : byte; { value to fill with - optional }
  460. use_op : boolean;
  461. constructor Create(b:byte);
  462. constructor Create_op(b: byte; _op: byte);
  463. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  464. procedure ppuwrite(ppufile:tcompilerppufile);override;
  465. function calculatefillbuf(var buf : tfillbuffer):pchar;virtual;
  466. end;
  467. taasmoutput = class(tlinkedlist)
  468. constructor create;
  469. function getlasttaifilepos : pfileposinfo;
  470. procedure InsertAfter(Item,Loc : TLinkedListItem);
  471. end;
  472. var
  473. { array with all class types for tais }
  474. aiclass : taiclassarray;
  475. { Current expression list }
  476. exprasmlist : taasmoutput;
  477. { labels for BREAK and CONTINUE }
  478. aktbreaklabel,aktcontinuelabel : tasmlabel;
  479. { label when the result is true or false }
  480. truelabel,falselabel : tasmlabel;
  481. { default lists }
  482. datasegment,codesegment,bsssegment,
  483. debuglist,withdebuglist,consts,
  484. importssection,exportssection,
  485. resourcesection,rttilist,
  486. resourcestringlist : taasmoutput;
  487. function ppuloadai(ppufile:tcompilerppufile):tai;
  488. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  489. implementation
  490. uses
  491. {$ifdef delphi}
  492. sysutils,
  493. {$else}
  494. strings,
  495. {$endif}
  496. verbose;
  497. const
  498. pputaimarker = 254;
  499. {****************************************************************************
  500. Helpers
  501. ****************************************************************************}
  502. function ppuloadai(ppufile:tcompilerppufile):tai;
  503. var
  504. b : byte;
  505. t : taitype;
  506. begin
  507. { marker }
  508. b:=ppufile.getbyte;
  509. if b<>pputaimarker then
  510. internalerror(200208181);
  511. { load nodetype }
  512. t:=taitype(ppufile.getbyte);
  513. if t<>ait_none then
  514. begin
  515. if t>high(taitype) then
  516. internalerror(200208182);
  517. if not assigned(aiclass[t]) then
  518. internalerror(200208183);
  519. {writeln('taiload: ',taitypestr[t]);}
  520. { generate tai of the correct class }
  521. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  522. end
  523. else
  524. ppuloadai:=nil;
  525. end;
  526. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  527. begin
  528. { marker, read by ppuloadnode }
  529. ppufile.putbyte(pputaimarker);
  530. if assigned(n) then
  531. begin
  532. { type, read by ppuloadnode }
  533. ppufile.putbyte(byte(n.typ));
  534. {writeln('taiwrite: ',taitypestr[n.typ]);}
  535. n.ppuwrite(ppufile);
  536. end
  537. else
  538. ppufile.putbyte(byte(ait_none));
  539. end;
  540. {****************************************************************************
  541. TAI
  542. ****************************************************************************}
  543. constructor tai.Create;
  544. begin
  545. {$ifndef NOOPT}
  546. optinfo:=nil;
  547. {$endif NOOPT}
  548. end;
  549. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  550. begin
  551. typ:=t;
  552. {$ifndef NOOPT}
  553. optinfo:=nil;
  554. {$endif}
  555. end;
  556. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  557. begin
  558. end;
  559. procedure tai.buildderefimpl;
  560. begin
  561. end;
  562. procedure tai.derefimpl;
  563. begin
  564. end;
  565. {****************************************************************************
  566. TAILINEINFO
  567. ****************************************************************************}
  568. constructor tailineinfo.create;
  569. begin
  570. inherited create;
  571. fileinfo:=aktfilepos;
  572. end;
  573. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  574. begin
  575. inherited ppuload(t,ppufile);
  576. ppufile.getposinfo(fileinfo);
  577. end;
  578. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  579. begin
  580. inherited ppuwrite(ppufile);
  581. ppufile.putposinfo(fileinfo);
  582. end;
  583. {****************************************************************************
  584. TAI_SECTION
  585. ****************************************************************************}
  586. constructor tai_section.Create(s : TSection);
  587. begin
  588. inherited Create;
  589. typ:=ait_section;
  590. sec:=s;
  591. end;
  592. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  593. begin
  594. inherited ppuload(t,ppufile);
  595. sec:=tsection(ppufile.getbyte);
  596. end;
  597. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  598. begin
  599. inherited ppuwrite(ppufile);
  600. ppufile.putbyte(byte(sec));
  601. end;
  602. {****************************************************************************
  603. TAI_DATABLOCK
  604. ****************************************************************************}
  605. constructor tai_datablock.Create(const _name : string;_size : longint);
  606. begin
  607. inherited Create;
  608. typ:=ait_datablock;
  609. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  610. { keep things aligned }
  611. if _size<=0 then
  612. _size:=4;
  613. size:=_size;
  614. is_global:=false;
  615. end;
  616. constructor tai_datablock.Create_global(const _name : string;_size : longint);
  617. begin
  618. inherited Create;
  619. typ:=ait_datablock;
  620. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  621. { keep things aligned }
  622. if _size<=0 then
  623. _size:=4;
  624. size:=_size;
  625. is_global:=true;
  626. end;
  627. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  628. begin
  629. inherited Create;
  630. sym:=ppufile.getasmsymbol;
  631. size:=ppufile.getlongint;
  632. is_global:=boolean(ppufile.getbyte);
  633. end;
  634. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  635. begin
  636. inherited ppuwrite(ppufile);
  637. ppufile.putasmsymbol(sym);
  638. ppufile.putlongint(size);
  639. ppufile.putbyte(byte(is_global));
  640. end;
  641. procedure tai_datablock.derefimpl;
  642. begin
  643. objectlibrary.DerefAsmsymbol(sym);
  644. end;
  645. {****************************************************************************
  646. TAI_SYMBOL
  647. ****************************************************************************}
  648. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  649. begin
  650. inherited Create;
  651. typ:=ait_symbol;
  652. sym:=_sym;
  653. size:=siz;
  654. sym.defbind:=AB_LOCAL;
  655. is_global:=false;
  656. end;
  657. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  658. begin
  659. inherited Create;
  660. typ:=ait_symbol;
  661. sym:=_sym;
  662. size:=siz;
  663. sym.defbind:=AB_GLOBAL;
  664. is_global:=true;
  665. end;
  666. constructor tai_symbol.Createname(const _name : string;siz:longint);
  667. begin
  668. inherited Create;
  669. typ:=ait_symbol;
  670. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_FUNCTION);
  671. size:=siz;
  672. is_global:=false;
  673. end;
  674. constructor tai_symbol.Createname_global(const _name : string;siz:longint);
  675. begin
  676. inherited Create;
  677. typ:=ait_symbol;
  678. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_FUNCTION);
  679. size:=siz;
  680. is_global:=true;
  681. end;
  682. constructor tai_symbol.Createdataname(const _name : string;siz:longint);
  683. begin
  684. inherited Create;
  685. typ:=ait_symbol;
  686. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  687. size:=siz;
  688. is_global:=false;
  689. end;
  690. constructor tai_symbol.Createdataname_global(const _name : string;siz:longint);
  691. begin
  692. inherited Create;
  693. typ:=ait_symbol;
  694. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  695. size:=siz;
  696. is_global:=true;
  697. end;
  698. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  699. begin
  700. inherited ppuload(t,ppufile);
  701. sym:=ppufile.getasmsymbol;
  702. size:=ppufile.getlongint;
  703. is_global:=boolean(ppufile.getbyte);
  704. end;
  705. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  706. begin
  707. inherited ppuwrite(ppufile);
  708. ppufile.putasmsymbol(sym);
  709. ppufile.putlongint(size);
  710. ppufile.putbyte(byte(is_global));
  711. end;
  712. procedure tai_symbol.derefimpl;
  713. begin
  714. objectlibrary.DerefAsmsymbol(sym);
  715. end;
  716. {****************************************************************************
  717. TAI_SYMBOL
  718. ****************************************************************************}
  719. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  720. begin
  721. inherited Create;
  722. typ:=ait_symbol_end;
  723. sym:=_sym;
  724. end;
  725. constructor tai_symbol_end.Createname(const _name : string);
  726. begin
  727. inherited Create;
  728. typ:=ait_symbol_end;
  729. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_NONE);
  730. end;
  731. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  732. begin
  733. inherited ppuload(t,ppufile);
  734. sym:=ppufile.getasmsymbol;
  735. end;
  736. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  737. begin
  738. inherited ppuwrite(ppufile);
  739. ppufile.putasmsymbol(sym);
  740. end;
  741. procedure tai_symbol_end.derefimpl;
  742. begin
  743. objectlibrary.DerefAsmsymbol(sym);
  744. end;
  745. {****************************************************************************
  746. TAI_CONST
  747. ****************************************************************************}
  748. constructor tai_const.Create_32bit(_value : longint);
  749. begin
  750. inherited Create;
  751. typ:=ait_const_32bit;
  752. value:=_value;
  753. end;
  754. constructor tai_const.Create_16bit(_value : word);
  755. begin
  756. inherited Create;
  757. typ:=ait_const_16bit;
  758. value:=_value;
  759. end;
  760. constructor tai_const.Create_8bit(_value : byte);
  761. begin
  762. inherited Create;
  763. typ:=ait_const_8bit;
  764. value:=_value;
  765. end;
  766. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  767. begin
  768. inherited ppuload(t,ppufile);
  769. value:=ppufile.getlongint;
  770. end;
  771. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  772. begin
  773. inherited ppuwrite(ppufile);
  774. ppufile.putlongint(value);
  775. end;
  776. {****************************************************************************
  777. TAI_CONST_SYMBOL
  778. ****************************************************************************}
  779. constructor tai_const_symbol.Create(_sym:tasmsymbol);
  780. begin
  781. inherited Create;
  782. typ:=ait_const_symbol;
  783. sym:=_sym;
  784. offset:=0;
  785. { update sym info }
  786. sym.increfs;
  787. end;
  788. constructor tai_const_symbol.Create_offset(_sym:tasmsymbol;ofs:longint);
  789. begin
  790. inherited Create;
  791. typ:=ait_const_symbol;
  792. sym:=_sym;
  793. offset:=ofs;
  794. { update sym info }
  795. sym.increfs;
  796. end;
  797. constructor tai_const_symbol.Create_rva(_sym:tasmsymbol);
  798. begin
  799. inherited Create;
  800. typ:=ait_const_rva;
  801. sym:=_sym;
  802. offset:=0;
  803. { update sym info }
  804. sym.increfs;
  805. end;
  806. constructor tai_const_symbol.Createname(const name:string);
  807. begin
  808. inherited Create;
  809. typ:=ait_const_symbol;
  810. sym:=objectlibrary.newasmsymbol(name);
  811. offset:=0;
  812. { update sym info }
  813. sym.increfs;
  814. end;
  815. constructor tai_const_symbol.Createname_offset(const name:string;ofs:longint);
  816. begin
  817. inherited Create;
  818. typ:=ait_const_symbol;
  819. sym:=objectlibrary.newasmsymbol(name);
  820. offset:=ofs;
  821. { update sym info }
  822. sym.increfs;
  823. end;
  824. constructor tai_const_symbol.Createname_rva(const name:string);
  825. begin
  826. inherited Create;
  827. typ:=ait_const_rva;
  828. sym:=objectlibrary.newasmsymbol(name);
  829. offset:=0;
  830. { update sym info }
  831. sym.increfs;
  832. end;
  833. constructor tai_const_symbol.Createdataname(const name:string);
  834. begin
  835. inherited Create;
  836. typ:=ait_const_symbol;
  837. sym:=objectlibrary.newasmsymboltype(name,AB_EXTERNAL,AT_DATA);
  838. offset:=0;
  839. { update sym info }
  840. sym.increfs;
  841. end;
  842. constructor tai_const_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  843. begin
  844. inherited ppuload(t,ppufile);
  845. sym:=ppufile.getasmsymbol;
  846. offset:=ppufile.getlongint;
  847. end;
  848. procedure tai_const_symbol.ppuwrite(ppufile:tcompilerppufile);
  849. begin
  850. inherited ppuwrite(ppufile);
  851. ppufile.putasmsymbol(sym);
  852. ppufile.putlongint(offset);
  853. end;
  854. procedure tai_const_symbol.derefimpl;
  855. begin
  856. objectlibrary.DerefAsmsymbol(sym);
  857. end;
  858. function tai_const_symbol.getcopy:tlinkedlistitem;
  859. begin
  860. getcopy:=inherited getcopy;
  861. { we need to increase the reference number }
  862. sym.increfs;
  863. end;
  864. {****************************************************************************
  865. TAI_real_32bit
  866. ****************************************************************************}
  867. constructor tai_real_32bit.Create(_value : ts32real);
  868. begin
  869. inherited Create;
  870. typ:=ait_real_32bit;
  871. value:=_value;
  872. end;
  873. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  874. begin
  875. inherited ppuload(t,ppufile);
  876. value:=ppufile.getreal;
  877. end;
  878. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  879. begin
  880. inherited ppuwrite(ppufile);
  881. ppufile.putreal(value);
  882. end;
  883. {****************************************************************************
  884. TAI_real_64bit
  885. ****************************************************************************}
  886. constructor tai_real_64bit.Create(_value : ts64real);
  887. begin
  888. inherited Create;
  889. typ:=ait_real_64bit;
  890. value:=_value;
  891. end;
  892. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  893. begin
  894. inherited ppuload(t,ppufile);
  895. value:=ppufile.getreal;
  896. end;
  897. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  898. begin
  899. inherited ppuwrite(ppufile);
  900. ppufile.putreal(value);
  901. end;
  902. {****************************************************************************
  903. TAI_real_80bit
  904. ****************************************************************************}
  905. constructor tai_real_80bit.Create(_value : ts80real);
  906. begin
  907. inherited Create;
  908. typ:=ait_real_80bit;
  909. value:=_value;
  910. end;
  911. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  912. begin
  913. inherited ppuload(t,ppufile);
  914. value:=ppufile.getreal;
  915. end;
  916. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  917. begin
  918. inherited ppuwrite(ppufile);
  919. ppufile.putreal(value);
  920. end;
  921. {****************************************************************************
  922. TAI_real_80bit
  923. ****************************************************************************}
  924. constructor tai_real_128bit.Create(_value : ts128real);
  925. begin
  926. inherited Create;
  927. typ:=ait_real_128bit;
  928. value:=_value;
  929. end;
  930. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  931. begin
  932. inherited ppuload(t,ppufile);
  933. value:=ppufile.getreal;
  934. end;
  935. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  936. begin
  937. inherited ppuwrite(ppufile);
  938. ppufile.putreal(value);
  939. end;
  940. {****************************************************************************
  941. Tai_comp_64bit
  942. ****************************************************************************}
  943. constructor tai_comp_64bit.Create(_value : ts64comp);
  944. begin
  945. inherited Create;
  946. typ:=ait_comp_64bit;
  947. value:=_value;
  948. end;
  949. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  950. begin
  951. inherited ppuload(t,ppufile);
  952. ppufile.putdata(value,sizeof(value));
  953. end;
  954. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  955. begin
  956. inherited ppuwrite(ppufile);
  957. ppufile.getdata(value,sizeof(value));
  958. end;
  959. {****************************************************************************
  960. TAI_STRING
  961. ****************************************************************************}
  962. constructor tai_string.Create(const _str : string);
  963. begin
  964. inherited Create;
  965. typ:=ait_string;
  966. len:=length(_str);
  967. getmem(str,len+1);
  968. strpcopy(str,_str);
  969. end;
  970. constructor tai_string.Create_pchar(_str : pchar);
  971. begin
  972. inherited Create;
  973. typ:=ait_string;
  974. str:=_str;
  975. len:=strlen(_str);
  976. end;
  977. constructor tai_string.Create_length_pchar(_str : pchar;length : longint);
  978. begin
  979. inherited Create;
  980. typ:=ait_string;
  981. str:=_str;
  982. len:=length;
  983. end;
  984. destructor tai_string.destroy;
  985. begin
  986. { you can have #0 inside the strings so }
  987. if str<>nil then
  988. freemem(str,len+1);
  989. inherited Destroy;
  990. end;
  991. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  992. begin
  993. inherited ppuload(t,ppufile);
  994. len:=ppufile.getlongint;
  995. getmem(str,len+1);
  996. ppufile.getdata(str^,len);
  997. str[len]:=#0;
  998. end;
  999. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1000. begin
  1001. inherited ppuwrite(ppufile);
  1002. ppufile.putlongint(len);
  1003. ppufile.putdata(str^,len);
  1004. end;
  1005. function tai_string.getcopy : tlinkedlistitem;
  1006. var
  1007. p : tlinkedlistitem;
  1008. begin
  1009. p:=inherited getcopy;
  1010. getmem(tai_string(p).str,len+1);
  1011. move(str^,tai_string(p).str^,len+1);
  1012. getcopy:=p;
  1013. end;
  1014. {****************************************************************************
  1015. TAI_LABEL
  1016. ****************************************************************************}
  1017. constructor tai_label.create(_l : tasmlabel);
  1018. begin
  1019. inherited Create;
  1020. typ:=ait_label;
  1021. l:=_l;
  1022. l.is_set:=true;
  1023. is_global:=(l.defbind=AB_GLOBAL);
  1024. end;
  1025. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1026. begin
  1027. inherited ppuload(t,ppufile);
  1028. l:=tasmlabel(ppufile.getasmsymbol);
  1029. is_global:=boolean(ppufile.getbyte);
  1030. end;
  1031. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1032. begin
  1033. inherited ppuwrite(ppufile);
  1034. ppufile.putasmsymbol(l);
  1035. ppufile.putbyte(byte(is_global));
  1036. end;
  1037. procedure tai_label.derefimpl;
  1038. begin
  1039. objectlibrary.DerefAsmsymbol(tasmsymbol(l));
  1040. l.is_set:=true;
  1041. end;
  1042. {****************************************************************************
  1043. TAI_DIRECT
  1044. ****************************************************************************}
  1045. constructor tai_direct.Create(_str : pchar);
  1046. begin
  1047. inherited Create;
  1048. typ:=ait_direct;
  1049. str:=_str;
  1050. end;
  1051. destructor tai_direct.destroy;
  1052. begin
  1053. strdispose(str);
  1054. inherited Destroy;
  1055. end;
  1056. constructor tai_direct.ppuload(t:taitype;ppufile:tcompilerppufile);
  1057. var
  1058. len : longint;
  1059. begin
  1060. inherited ppuload(t,ppufile);
  1061. len:=ppufile.getlongint;
  1062. getmem(str,len+1);
  1063. ppufile.getdata(str^,len);
  1064. str[len]:=#0;
  1065. end;
  1066. procedure tai_direct.ppuwrite(ppufile:tcompilerppufile);
  1067. var
  1068. len : longint;
  1069. begin
  1070. inherited ppuwrite(ppufile);
  1071. len:=strlen(str);
  1072. ppufile.putlongint(len);
  1073. ppufile.putdata(str^,len);
  1074. end;
  1075. function tai_direct.getcopy : tlinkedlistitem;
  1076. var
  1077. p : tlinkedlistitem;
  1078. begin
  1079. p:=inherited getcopy;
  1080. getmem(tai_direct(p).str,strlen(str)+1);
  1081. move(str^,tai_direct(p).str^,strlen(str)+1);
  1082. getcopy:=p;
  1083. end;
  1084. {****************************************************************************
  1085. tai_comment comment to be inserted in the assembler file
  1086. ****************************************************************************}
  1087. constructor tai_comment.Create(_str : pchar);
  1088. begin
  1089. inherited Create;
  1090. typ:=ait_comment;
  1091. str:=_str;
  1092. end;
  1093. destructor tai_comment.destroy;
  1094. begin
  1095. strdispose(str);
  1096. inherited Destroy;
  1097. end;
  1098. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1099. var
  1100. len : longint;
  1101. begin
  1102. inherited ppuload(t,ppufile);
  1103. len:=ppufile.getlongint;
  1104. getmem(str,len+1);
  1105. ppufile.getdata(str^,len);
  1106. str[len]:=#0;
  1107. end;
  1108. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1109. var
  1110. len : longint;
  1111. begin
  1112. inherited ppuwrite(ppufile);
  1113. len:=strlen(str);
  1114. ppufile.putlongint(len);
  1115. ppufile.putdata(str^,len);
  1116. end;
  1117. function tai_comment.getcopy : tlinkedlistitem;
  1118. var
  1119. p : tlinkedlistitem;
  1120. begin
  1121. p:=inherited getcopy;
  1122. getmem(tai_comment(p).str,strlen(str)+1);
  1123. move(str^,tai_comment(p).str^,strlen(str)+1);
  1124. getcopy:=p;
  1125. end;
  1126. {****************************************************************************
  1127. TAI_CUT
  1128. ****************************************************************************}
  1129. constructor tai_cut.Create;
  1130. begin
  1131. inherited Create;
  1132. typ:=ait_cut;
  1133. place:=cut_normal;
  1134. end;
  1135. constructor tai_cut.Create_begin;
  1136. begin
  1137. inherited Create;
  1138. typ:=ait_cut;
  1139. place:=cut_begin;
  1140. end;
  1141. constructor tai_cut.Create_end;
  1142. begin
  1143. inherited Create;
  1144. typ:=ait_cut;
  1145. place:=cut_end;
  1146. end;
  1147. constructor tai_cut.ppuload(t:taitype;ppufile:tcompilerppufile);
  1148. begin
  1149. inherited ppuload(t,ppufile);
  1150. place:=TCutPlace(ppufile.getbyte);
  1151. end;
  1152. procedure tai_cut.ppuwrite(ppufile:tcompilerppufile);
  1153. begin
  1154. inherited ppuwrite(ppufile);
  1155. ppufile.putbyte(byte(place));
  1156. end;
  1157. {****************************************************************************
  1158. Tai_Marker
  1159. ****************************************************************************}
  1160. constructor Tai_Marker.Create(_Kind: TMarker);
  1161. begin
  1162. Inherited Create;
  1163. typ := ait_marker;
  1164. Kind := _Kind;
  1165. end;
  1166. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1167. begin
  1168. inherited ppuload(t,ppufile);
  1169. kind:=TMarker(ppufile.getbyte);
  1170. end;
  1171. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1172. begin
  1173. inherited ppuwrite(ppufile);
  1174. ppufile.putbyte(byte(kind));
  1175. end;
  1176. {*****************************************************************************
  1177. tai_tempalloc
  1178. *****************************************************************************}
  1179. constructor tai_tempalloc.alloc(pos,size:longint);
  1180. begin
  1181. inherited Create;
  1182. typ:=ait_tempalloc;
  1183. allocation:=true;
  1184. temppos:=pos;
  1185. tempsize:=size;
  1186. {$ifdef EXTDEBUG}
  1187. problem:=nil;
  1188. {$endif EXTDEBUG}
  1189. end;
  1190. destructor tai_tempalloc.destroy;
  1191. begin
  1192. {$ifdef EXTDEBUG}
  1193. stringdispose(problem);
  1194. {$endif EXTDEBUG}
  1195. inherited destroy;
  1196. end;
  1197. constructor tai_tempalloc.dealloc(pos,size:longint);
  1198. begin
  1199. inherited Create;
  1200. typ:=ait_tempalloc;
  1201. allocation:=false;
  1202. temppos:=pos;
  1203. tempsize:=size;
  1204. {$ifdef EXTDEBUG}
  1205. problem:=nil;
  1206. {$endif EXTDEBUG}
  1207. end;
  1208. {$ifdef EXTDEBUG}
  1209. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1210. begin
  1211. inherited Create;
  1212. typ:=ait_tempalloc;
  1213. allocation:=false;
  1214. temppos:=pos;
  1215. tempsize:=size;
  1216. problem:=stringdup(st);
  1217. end;
  1218. {$endif EXTDEBUG}
  1219. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1220. begin
  1221. inherited ppuload(t,ppufile);
  1222. temppos:=ppufile.getlongint;
  1223. tempsize:=ppufile.getlongint;
  1224. allocation:=boolean(ppufile.getbyte);
  1225. {$ifdef EXTDEBUG}
  1226. problem:=nil;
  1227. {$endif EXTDEBUG}
  1228. end;
  1229. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1230. begin
  1231. inherited ppuwrite(ppufile);
  1232. ppufile.putlongint(temppos);
  1233. ppufile.putlongint(tempsize);
  1234. ppufile.putbyte(byte(allocation));
  1235. end;
  1236. {*****************************************************************************
  1237. tai_regalloc
  1238. *****************************************************************************}
  1239. constructor tai_regalloc.alloc(r : tregister);
  1240. begin
  1241. inherited create;
  1242. typ:=ait_regalloc;
  1243. allocation:=true;
  1244. reg:=r;
  1245. end;
  1246. constructor tai_regalloc.dealloc(r : tregister);
  1247. begin
  1248. inherited create;
  1249. typ:=ait_regalloc;
  1250. allocation:=false;
  1251. reg:=r;
  1252. end;
  1253. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1254. begin
  1255. inherited ppuload(t,ppufile);
  1256. ppufile.getdata(reg,sizeof(Tregister));
  1257. allocation:=boolean(ppufile.getbyte);
  1258. end;
  1259. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1260. begin
  1261. inherited ppuwrite(ppufile);
  1262. ppufile.putdata(reg,sizeof(Tregister));
  1263. ppufile.putbyte(byte(allocation));
  1264. end;
  1265. {*****************************************************************************
  1266. TaiInstruction
  1267. *****************************************************************************}
  1268. constructor taicpu_abstract.Create(op : tasmop);
  1269. begin
  1270. inherited create;
  1271. typ:=ait_instruction;
  1272. is_jmp:=false;
  1273. opcode:=op;
  1274. ops:=0;
  1275. fillchar(condition,sizeof(condition),0);
  1276. fillchar(oper,sizeof(oper),0);
  1277. end;
  1278. destructor taicpu_abstract.Destroy;
  1279. var
  1280. i : integer;
  1281. begin
  1282. for i:=0 to opercnt-1 do
  1283. begin
  1284. with oper[i]^ do
  1285. begin
  1286. case typ of
  1287. top_ref:
  1288. dispose(ref);
  1289. {$ifdef ARM}
  1290. top_shifterop:
  1291. dispose(shifterop);
  1292. {$endif ARM}
  1293. end;
  1294. end;
  1295. dispose(oper[i]);
  1296. end;
  1297. inherited destroy;
  1298. end;
  1299. { ---------------------------------------------------------------------
  1300. Loading of operands.
  1301. ---------------------------------------------------------------------}
  1302. procedure taicpu_abstract.allocate_oper(opers:longint);
  1303. begin
  1304. while (opers>opercnt) do
  1305. begin
  1306. new(oper[opercnt]);
  1307. fillchar(oper[opercnt]^,sizeof(toper),0);
  1308. inc(opercnt);
  1309. end;
  1310. end;
  1311. procedure taicpu_abstract.loadconst(opidx:longint;l:aword);
  1312. begin
  1313. allocate_oper(opidx+1);
  1314. with oper[opidx]^ do
  1315. begin
  1316. if typ<>top_const then
  1317. clearop(opidx);
  1318. val:=l;
  1319. typ:=top_const;
  1320. end;
  1321. end;
  1322. procedure taicpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1323. begin
  1324. if not assigned(s) then
  1325. internalerror(200204251);
  1326. allocate_oper(opidx+1);
  1327. with oper[opidx]^ do
  1328. begin
  1329. if typ<>top_symbol then
  1330. clearop(opidx);
  1331. sym:=s;
  1332. symofs:=sofs;
  1333. typ:=top_symbol;
  1334. end;
  1335. s.increfs;
  1336. end;
  1337. procedure taicpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset:boolean);
  1338. begin
  1339. if not assigned(s) then
  1340. internalerror(200204251);
  1341. allocate_oper(opidx+1);
  1342. with oper[opidx]^ do
  1343. begin
  1344. if typ<>top_local then
  1345. clearop(opidx);
  1346. localsym:=s;
  1347. localsymofs:=sofs;
  1348. localindexreg:=indexreg;
  1349. localscale:=scale;
  1350. localgetoffset:=getoffset;
  1351. typ:=top_local;
  1352. end;
  1353. end;
  1354. procedure taicpu_abstract.loadref(opidx:longint;const r:treference);
  1355. begin
  1356. allocate_oper(opidx+1);
  1357. with oper[opidx]^ do
  1358. begin
  1359. if typ<>top_ref then
  1360. begin
  1361. clearop(opidx);
  1362. new(ref);
  1363. end;
  1364. ref^:=r;
  1365. {$ifdef i386}
  1366. { We allow this exception for i386, since overloading this would be
  1367. too much of a a speed penalty}
  1368. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1369. segprefix:=ref^.segment;
  1370. {$endif}
  1371. typ:=top_ref;
  1372. { mark symbol as used }
  1373. if assigned(ref^.symbol) then
  1374. ref^.symbol.increfs;
  1375. end;
  1376. end;
  1377. procedure taicpu_abstract.loadreg(opidx:longint;r:tregister);
  1378. begin
  1379. allocate_oper(opidx+1);
  1380. with oper[opidx]^ do
  1381. begin
  1382. if typ<>top_reg then
  1383. clearop(opidx);
  1384. reg:=r;
  1385. typ:=top_reg;
  1386. end;
  1387. {$ifdef ARM}
  1388. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1389. Due to speed considerations we don't use a virtual overridden method here.
  1390. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1391. problems with iregs getting r15.
  1392. }
  1393. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1394. {$endif ARM}
  1395. end;
  1396. procedure taicpu_abstract.loadoper(opidx:longint;o:toper);
  1397. begin
  1398. allocate_oper(opidx+1);
  1399. clearop(opidx);
  1400. oper[opidx]^:=o;
  1401. { copy also the reference }
  1402. with oper[opidx]^ do
  1403. begin
  1404. case typ of
  1405. top_ref:
  1406. begin
  1407. new(ref);
  1408. ref^:=o.ref^;
  1409. end;
  1410. {$ifdef ARM}
  1411. top_shifterop:
  1412. begin
  1413. new(shifterop);
  1414. shifterop^:=o.shifterop^;
  1415. end;
  1416. {$endif ARM}
  1417. end;
  1418. end;
  1419. end;
  1420. procedure taicpu_abstract.clearop(opidx:longint);
  1421. begin
  1422. with oper[opidx]^ do
  1423. begin
  1424. case typ of
  1425. top_ref:
  1426. dispose(ref);
  1427. {$ifdef ARM}
  1428. top_shifterop:
  1429. dispose(shifterop);
  1430. top_regset:
  1431. dispose(regset);
  1432. {$endif ARM}
  1433. end;
  1434. typ:=top_none;
  1435. end;
  1436. end;
  1437. { ---------------------------------------------------------------------
  1438. Register allocator methods.
  1439. ---------------------------------------------------------------------}
  1440. function taicpu_abstract.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister;var unusedregsint:tsuperregisterset):Tai;
  1441. var
  1442. back : tsuperregisterset;
  1443. supreg : tsuperregister;
  1444. begin
  1445. back:=unusedregsint;
  1446. get_insert_pos:=p;
  1447. while (p<>nil) and (p.typ=ait_regalloc) do
  1448. begin
  1449. supreg:=getsupreg(Tai_regalloc(p).reg);
  1450. {Rewind the register allocation.}
  1451. if Tai_regalloc(p).allocation then
  1452. supregset_include(unusedregsint,supreg)
  1453. else
  1454. begin
  1455. supregset_exclude(unusedregsint,supreg);
  1456. if supreg=huntfor1 then
  1457. begin
  1458. get_insert_pos:=Tai(p.previous);
  1459. back:=unusedregsint;
  1460. end;
  1461. if supreg=huntfor2 then
  1462. begin
  1463. get_insert_pos:=Tai(p.previous);
  1464. back:=unusedregsint;
  1465. end;
  1466. if supreg=huntfor3 then
  1467. begin
  1468. get_insert_pos:=Tai(p.previous);
  1469. back:=unusedregsint;
  1470. end;
  1471. end;
  1472. p:=Tai(p.previous);
  1473. end;
  1474. unusedregsint:=back;
  1475. end;
  1476. procedure taicpu_abstract.forward_allocation(p:Tai;var unusedregsint:Tsuperregisterset);
  1477. begin
  1478. {Forward the register allocation again.}
  1479. while (p<>self) do
  1480. begin
  1481. if p.typ<>ait_regalloc then
  1482. internalerror(200305311);
  1483. if Tai_regalloc(p).allocation then
  1484. supregset_exclude(unusedregsint,getsupreg(Tai_regalloc(p).reg))
  1485. else
  1486. supregset_include(unusedregsint,getsupreg(Tai_regalloc(p).reg));
  1487. p:=Tai(p.next);
  1488. end;
  1489. end;
  1490. function taicpu_abstract.spill_registers(list:Taasmoutput;
  1491. rgget:Trggetproc;
  1492. rgunget:Trgungetproc;
  1493. const r:Tsuperregisterset;
  1494. var unusedregsint:Tsuperregisterset;
  1495. const spilltemplist:Tspill_temp_list): boolean;
  1496. var
  1497. supreg, reg1, reg2, reg3: Tsuperregister;
  1498. helpreg:Tregister;
  1499. helpins:Tai;
  1500. op:Tasmop;
  1501. pos:Tai;
  1502. procedure DoSpill2RegInstructions;
  1503. var
  1504. wasload: boolean;
  1505. begin
  1506. { the register that's being stored/loaded }
  1507. supreg:=getsupreg(oper[0]^.reg);
  1508. if supregset_in(r,supreg) then
  1509. begin
  1510. if oper[1]^.typ=top_const then
  1511. begin
  1512. // Example:
  1513. // mov r20d, 1 ; r20d must be spilled into -60(r1)
  1514. //
  1515. // Change into:
  1516. //
  1517. // mov r21d, 1
  1518. // st? r21d, -60(r1)
  1519. //
  1520. pos := get_insert_pos(tai(previous),supreg,
  1521. RS_INVALID,
  1522. RS_INVALID,
  1523. unusedregsint);
  1524. rgget(list,pos,R_SUBWHOLE,helpreg);
  1525. spill_registers := true;
  1526. helpins:=spilling_create_store(helpreg,spilltemplist[supreg]);
  1527. if pos=nil then
  1528. list.insertafter(helpins,list.first)
  1529. else
  1530. list.insertafter(helpins,pos.next);
  1531. loadreg(0,helpreg);
  1532. rgunget(list,helpins,helpreg);
  1533. forward_allocation(tai(helpins.next),unusedregsint);
  1534. // list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);}
  1535. end
  1536. else if oper[1]^.typ=top_ref then
  1537. begin
  1538. // Example:
  1539. // l?? r20d, 8(r1) ; r20d must be spilled into -60(r1)
  1540. //
  1541. // Change into:
  1542. //
  1543. // l?? r21d, 8(r1)
  1544. // st? r21d, -60(r1)
  1545. //
  1546. // And:
  1547. //
  1548. // st? r20d, 8(r1) ; r20d must be spilled into -60(r1)
  1549. //
  1550. // Change into:
  1551. //
  1552. // l?? r21d, -60(r1)
  1553. // st? r21d, 8(r1)
  1554. pos := get_insert_pos(tai(previous),supreg,
  1555. getsupreg(oper[1]^.ref^.base),
  1556. getsupreg(oper[1]^.ref^.index),
  1557. unusedregsint);
  1558. rgget(list,pos,R_SUBWHOLE,helpreg);
  1559. spill_registers := true;
  1560. if wasload then
  1561. begin
  1562. helpins:=spilling_create_loadstore(opcode,helpreg,oper[1]^.ref^);
  1563. loadref(1,spilltemplist[supreg]);
  1564. opcode := op;
  1565. end
  1566. else
  1567. helpins:=spilling_create_loadstore(op,helpreg,spilltemplist[supreg]);
  1568. if pos=nil then
  1569. list.insertafter(helpins,list.first)
  1570. else
  1571. list.insertafter(helpins,pos.next);
  1572. loadreg(0,helpreg);
  1573. rgunget(list,helpins,helpreg);
  1574. forward_allocation(tai(helpins.next),unusedregsint);
  1575. // list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);}
  1576. end;
  1577. end;
  1578. { oper[1] can also be ref in case of "lis r3,symbol@ha" or so }
  1579. if not((oper[1]^.typ=top_ref) and
  1580. spilling_decode_loadstore(opcode,op,wasload)) then
  1581. Exit;
  1582. { now the registers used in the reference }
  1583. { a) base }
  1584. supreg := getsupreg(oper[1]^.ref^.base);
  1585. if supregset_in(r,supreg) then
  1586. begin
  1587. if wasload then
  1588. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1]^.ref^.index),getsupreg(oper[0]^.reg),0,unusedregsint)
  1589. else
  1590. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1]^.ref^.index),0,0,unusedregsint);
  1591. rgget(list,pos,R_SUBWHOLE,helpreg);
  1592. spill_registers:=true;
  1593. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1594. if pos=nil then
  1595. list.insertafter(helpins,list.first)
  1596. else
  1597. list.insertafter(helpins,pos.next);
  1598. oper[1]^.ref^.base:=helpreg;
  1599. rgunget(list,helpins,helpreg);
  1600. forward_allocation(Tai(helpins.next),unusedregsint);
  1601. { list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);}
  1602. end;
  1603. { b) index }
  1604. supreg := getsupreg(oper[1]^.ref^.index);
  1605. if supregset_in(r,supreg) then
  1606. begin
  1607. if wasload then
  1608. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1]^.ref^.base),getsupreg(oper[0]^.reg),0,unusedregsint)
  1609. else
  1610. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1]^.ref^.base),0,0,unusedregsint);
  1611. rgget(list,pos,R_SUBWHOLE,helpreg);
  1612. spill_registers:=true;
  1613. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1614. if pos=nil then
  1615. list.insertafter(helpins,list.first)
  1616. else
  1617. list.insertafter(helpins,pos.next);
  1618. oper[1]^.ref^.index:=helpreg;
  1619. rgunget(list,helpins,helpreg);
  1620. forward_allocation(Tai(helpins.next),unusedregsint);
  1621. { list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);}
  1622. end;
  1623. end;
  1624. procedure DoSpill3RegInstructions;
  1625. var
  1626. i:byte;
  1627. begin
  1628. if oper[0]^.typ <> top_reg then
  1629. Exit;
  1630. reg1 := getsupreg(oper[0]^.reg);
  1631. if oper[1]^.typ = top_reg then
  1632. reg2 := getsupreg(oper[1]^.reg)
  1633. else
  1634. reg2 := 0;
  1635. if (ops >= 3) and
  1636. (oper[2]^.typ = top_reg) then
  1637. reg3 := getsupreg(oper[2]^.reg)
  1638. else
  1639. reg3 := 0;
  1640. supreg:=reg1;
  1641. if supregset_in(r,supreg) then
  1642. begin
  1643. // Example:
  1644. // add r20d, r21d, r22d ; r20d must be spilled into -60(r1)
  1645. //
  1646. // Change into:
  1647. //
  1648. // lwz r23d, -60(r1)
  1649. // add r23d, r21d, r22d
  1650. // stw r23d, -60(r1)
  1651. pos := get_insert_pos(Tai(previous),reg1,reg2,reg3,unusedregsint);
  1652. rgget(list,pos,R_SUBWHOLE,helpreg);
  1653. spill_registers := true;
  1654. helpins:=spilling_create_store(helpreg,spilltemplist[supreg]);
  1655. list.insertafter(helpins,self);
  1656. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1657. if pos=nil then
  1658. list.insertafter(helpins,list.first)
  1659. else
  1660. list.insertafter(helpins,pos.next);
  1661. loadreg(0,helpreg);
  1662. rgunget(list,helpins,helpreg);
  1663. forward_allocation(tai(helpins.next),unusedregsint);
  1664. { list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);}
  1665. end;
  1666. for i := 1 to 2 do
  1667. if (oper[i]^.typ = top_reg) then
  1668. begin
  1669. supreg:=getsupreg(oper[i]^.reg);
  1670. if supregset_in(r,supreg) then
  1671. begin
  1672. // Example:
  1673. // add r20d, r21d, r22d ; r20d must be spilled into -60(r1)
  1674. //
  1675. // Change into:
  1676. //
  1677. // lwz r23d, -60(r1)
  1678. // add r23d, r21d, r22d
  1679. // stw r23d, -60(r1)
  1680. pos := get_insert_pos(Tai(previous),reg1,reg2,reg3,unusedregsint);
  1681. rgget(list,pos,R_SUBWHOLE,helpreg);
  1682. spill_registers := true;
  1683. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1684. if pos=nil then
  1685. list.insertafter(helpins,list.first)
  1686. else
  1687. list.insertafter(helpins,pos.next);
  1688. loadreg(i,helpreg);
  1689. rgunget(list,helpins,helpreg);
  1690. forward_allocation(tai(helpins.next),unusedregsint);
  1691. { list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);}
  1692. end;
  1693. end;
  1694. end;
  1695. begin
  1696. spill_registers:=false;
  1697. case ops of
  1698. 0,1:
  1699. {Mazen : Do no thing like in a delay slot for sparc : nop;};
  1700. 2:
  1701. DoSpill2RegInstructions;
  1702. else
  1703. {all other instructions the compiler generates are the same (I hope): }
  1704. {operand 0 is a register and is the destination, the others are sources}
  1705. {and can be either registers or constants }
  1706. {exception: branches (is_jmp isn't always set for them) }
  1707. DoSpill3RegInstructions;
  1708. end;
  1709. end;
  1710. { ---------------------------------------------------------------------
  1711. Miscellaneous methods.
  1712. ---------------------------------------------------------------------}
  1713. procedure taicpu_abstract.SetCondition(const c:TAsmCond);
  1714. begin
  1715. condition:=c;
  1716. end;
  1717. Function taicpu_abstract.getcopy:TLinkedListItem;
  1718. var
  1719. i : longint;
  1720. p : taicpu_abstract;
  1721. begin
  1722. p:=taicpu_abstract(inherited getcopy);
  1723. { make a copy of the references }
  1724. p.opercnt:=0;
  1725. p.allocate_oper(ops);
  1726. for i:=0 to ops-1 do
  1727. begin
  1728. p.oper[i]^:=oper[i]^;
  1729. if (oper[i]^.typ=top_ref) then
  1730. begin
  1731. new(p.oper[i]^.ref);
  1732. p.oper[i]^.ref^:=oper[i]^.ref^;
  1733. end;
  1734. end;
  1735. getcopy:=p;
  1736. end;
  1737. constructor taicpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1738. var
  1739. i : integer;
  1740. begin
  1741. inherited ppuload(t,ppufile);
  1742. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1743. ppufile.getdata(condition,sizeof(tasmcond));
  1744. allocate_oper(ppufile.getbyte);
  1745. for i:=0 to ops-1 do
  1746. ppuloadoper(ppufile,oper[i]^);
  1747. opcode:=tasmop(ppufile.getword);
  1748. {$ifdef i386}
  1749. ppufile.getdata(segprefix,sizeof(Tregister));
  1750. {$endif i386}
  1751. is_jmp:=boolean(ppufile.getbyte);
  1752. end;
  1753. procedure taicpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1754. var
  1755. i : integer;
  1756. begin
  1757. inherited ppuwrite(ppufile);
  1758. ppufile.putdata(condition,sizeof(tasmcond));
  1759. ppufile.putbyte(ops);
  1760. for i:=0 to ops-1 do
  1761. ppuwriteoper(ppufile,oper[i]^);
  1762. ppufile.putword(word(opcode));
  1763. {$ifdef i386}
  1764. ppufile.putdata(segprefix,sizeof(Tregister));
  1765. {$endif i386}
  1766. ppufile.putbyte(byte(is_jmp));
  1767. end;
  1768. procedure taicpu_abstract.buildderefimpl;
  1769. var
  1770. i : integer;
  1771. begin
  1772. for i:=0 to ops-1 do
  1773. ppubuildderefimploper(oper[i]^);
  1774. end;
  1775. procedure taicpu_abstract.derefimpl;
  1776. var
  1777. i : integer;
  1778. begin
  1779. for i:=0 to ops-1 do
  1780. ppuderefoper(oper[i]^);
  1781. end;
  1782. {****************************************************************************
  1783. tai_align_abstract
  1784. ****************************************************************************}
  1785. constructor tai_align_abstract.Create(b: byte);
  1786. begin
  1787. inherited Create;
  1788. typ:=ait_align;
  1789. if b in [1,2,4,8,16,32] then
  1790. aligntype := b
  1791. else
  1792. aligntype := 1;
  1793. fillsize:=0;
  1794. fillop:=0;
  1795. use_op:=false;
  1796. end;
  1797. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1798. begin
  1799. inherited Create;
  1800. typ:=ait_align;
  1801. if b in [1,2,4,8,16,32] then
  1802. aligntype := b
  1803. else
  1804. aligntype := 1;
  1805. fillsize:=0;
  1806. fillop:=_op;
  1807. use_op:=true;
  1808. end;
  1809. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  1810. begin
  1811. fillchar(buf,high(buf),fillop);
  1812. calculatefillbuf:=pchar(@buf);
  1813. end;
  1814. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1815. begin
  1816. inherited ppuload(t,ppufile);
  1817. aligntype:=ppufile.getbyte;
  1818. fillsize:=0;
  1819. fillop:=ppufile.getbyte;
  1820. use_op:=boolean(ppufile.getbyte);
  1821. end;
  1822. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  1823. begin
  1824. inherited ppuwrite(ppufile);
  1825. ppufile.putbyte(aligntype);
  1826. ppufile.putbyte(fillop);
  1827. ppufile.putbyte(byte(use_op));
  1828. end;
  1829. {*****************************************************************************
  1830. TAAsmOutput
  1831. *****************************************************************************}
  1832. constructor taasmoutput.create;
  1833. begin
  1834. inherited create;
  1835. { make sure the optimizer won't remove the first tai of this list}
  1836. insert(tai_marker.create(marker_blockstart));
  1837. end;
  1838. function taasmoutput.getlasttaifilepos : pfileposinfo;
  1839. var
  1840. hp : tlinkedlistitem;
  1841. begin
  1842. getlasttaifilepos := nil;
  1843. if assigned(last) then
  1844. begin
  1845. { find the last file information record }
  1846. if not (tai(last).typ in SkipLineInfo) then
  1847. getlasttaifilepos:=@tailineinfo(last).fileinfo
  1848. else
  1849. { go through list backwards to find the first entry
  1850. with line information
  1851. }
  1852. begin
  1853. hp:=tai(last);
  1854. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  1855. hp:=hp.Previous;
  1856. { found entry }
  1857. if assigned(hp) then
  1858. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  1859. end;
  1860. end;
  1861. end;
  1862. procedure Taasmoutput.InsertAfter(Item,Loc : TLinkedListItem);
  1863. begin
  1864. { This is not possible because it is not sure that the
  1865. tai at Loc has taifileinfo as parent }
  1866. {if assigned(Loc) then
  1867. tailineinfo(Item).fileinfo:=tailineinfo(Loc).fileinfo;}
  1868. inherited InsertAfter(Item,Loc);
  1869. end;
  1870. end.
  1871. {
  1872. $Log$
  1873. Revision 1.57 2003-12-03 17:39:04 florian
  1874. * fixed several arm calling conventions issues
  1875. * fixed reference reading in the assembler reader
  1876. * fixed a_loadaddr_ref_reg
  1877. Revision 1.55 2003/11/12 16:05:39 florian
  1878. * assembler readers OOPed
  1879. + typed currency constants
  1880. + typed 128 bit float constants if the CPU supports it
  1881. Revision 1.54 2003/11/07 15:58:32 florian
  1882. * Florian's culmutative nr. 1; contains:
  1883. - invalid calling conventions for a certain cpu are rejected
  1884. - arm softfloat calling conventions
  1885. - -Sp for cpu dependend code generation
  1886. - several arm fixes
  1887. - remaining code for value open array paras on heap
  1888. Revision 1.53 2003/10/30 19:59:00 peter
  1889. * support scalefactor for opr_local
  1890. * support reference with opr_local set, fixes tw2631
  1891. Revision 1.52 2003/10/29 21:06:39 jonas
  1892. * allow more than 3 args in the spilling routine
  1893. Revision 1.51 2003/10/29 15:40:20 peter
  1894. * support indexing and offset retrieval for locals
  1895. Revision 1.50 2003/10/29 14:42:14 mazen
  1896. * code reformatted
  1897. Revision 1.49 2003/10/29 14:05:45 mazen
  1898. * Splling function devided to sub functions to make it easy to understand.
  1899. This commit is just to allow easy diffs to validate the migration (hint use -w)
  1900. Revision 1.48 2003/10/24 17:39:41 peter
  1901. * asmnode.get_position now inserts a marker
  1902. Revision 1.47 2003/10/23 14:44:07 peter
  1903. * splitted buildderef and buildderefimpl to fix interface crc
  1904. calculation
  1905. Revision 1.46 2003/10/22 20:39:59 peter
  1906. * write derefdata in a separate ppu entry
  1907. Revision 1.45 2003/10/21 15:15:35 peter
  1908. * taicpu_abstract.oper[] changed to pointers
  1909. Revision 1.44 2003/10/17 14:38:32 peter
  1910. * 64k registers supported
  1911. * fixed some memory leaks
  1912. Revision 1.43 2003/10/11 16:06:42 florian
  1913. * fixed some MMX<->SSE
  1914. * started to fix ppc, needs an overhaul
  1915. + stabs info improve for spilling, not sure if it works correctly/completly
  1916. - MMX_SUPPORT removed from Makefile.fpc
  1917. Revision 1.42 2003/10/10 17:48:13 peter
  1918. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1919. * tregisteralloctor renamed to trgobj
  1920. * removed rgobj from a lot of units
  1921. * moved location_* and reference_* to cgobj
  1922. * first things for mmx register allocation
  1923. Revision 1.41 2003/10/01 20:34:48 peter
  1924. * procinfo unit contains tprocinfo
  1925. * cginfo renamed to cgbase
  1926. * moved cgmessage to verbose
  1927. * fixed ppc and sparc compiles
  1928. Revision 1.40 2003/09/23 17:56:05 peter
  1929. * locals and paras are allocated in the code generation
  1930. * tvarsym.localloc contains the location of para/local when
  1931. generating code for the current procedure
  1932. Revision 1.39 2003/09/07 22:09:34 peter
  1933. * preparations for different default calling conventions
  1934. * various RA fixes
  1935. Revision 1.38 2003/09/04 00:15:28 florian
  1936. * first bunch of adaptions of arm compiler for new register type
  1937. Revision 1.37 2003/09/03 15:55:00 peter
  1938. * NEWRA branch merged
  1939. Revision 1.36 2003/09/03 11:18:36 florian
  1940. * fixed arm concatcopy
  1941. + arm support in the common compiler sources added
  1942. * moved some generic cg code around
  1943. + tfputype added
  1944. * ...
  1945. Revision 1.35.2.5 2003/08/31 21:08:16 peter
  1946. * first batch of sparc fixes
  1947. Revision 1.35.2.4 2003/08/29 17:28:59 peter
  1948. * next batch of updates
  1949. Revision 1.35.2.3 2003/08/28 18:35:07 peter
  1950. * tregister changed to cardinal
  1951. Revision 1.35.2.2 2003/08/27 20:23:55 peter
  1952. * remove old ra code
  1953. Revision 1.35.2.1 2003/08/27 19:55:54 peter
  1954. * first tregister patch
  1955. Revision 1.35 2003/08/21 14:47:41 peter
  1956. * remove convert_registers
  1957. Revision 1.34 2003/08/20 20:29:06 daniel
  1958. * Some more R_NO changes
  1959. * Preventive code to loadref added
  1960. Revision 1.33 2003/08/17 20:47:47 daniel
  1961. * Notranslation changed into -sr functionality
  1962. Revision 1.32 2003/08/17 16:59:20 jonas
  1963. * fixed regvars so they work with newra (at least for ppc)
  1964. * fixed some volatile register bugs
  1965. + -dnotranslation option for -dnewra, which causes the registers not to
  1966. be translated from virtual to normal registers. Requires support in
  1967. the assembler writer as well, which is only implemented in aggas/
  1968. agppcgas currently
  1969. Revision 1.31 2003/08/11 21:18:20 peter
  1970. * start of sparc support for newra
  1971. Revision 1.30 2003/07/02 16:43:48 jonas
  1972. * always add dummy marker object at the start of an assembler list, so
  1973. the optimizer can't remove the first object
  1974. Revision 1.29 2003/06/03 13:01:59 daniel
  1975. * Register allocator finished
  1976. Revision 1.28 2003/05/12 18:13:57 peter
  1977. * create rtti label using newasmsymboldata and update binding
  1978. only when calling tai_symbol.create
  1979. * tai_symbol.create_global added
  1980. Revision 1.27 2003/04/25 20:59:33 peter
  1981. * removed funcretn,funcretsym, function result is now in varsym
  1982. and aliases for result and function name are added using absolutesym
  1983. * vs_hidden parameter for funcret passed in parameter
  1984. * vs_hidden fixes
  1985. * writenode changed to printnode and released from extdebug
  1986. * -vp option added to generate a tree.log with the nodetree
  1987. * nicer printnode for statements, callnode
  1988. Revision 1.26 2002/04/25 16:12:09 florian
  1989. * fixed more problems with cpubase and x86-64
  1990. Revision 1.25 2003/04/25 08:25:26 daniel
  1991. * Ifdefs around a lot of calls to cleartempgen
  1992. * Fixed registers that are allocated but not freed in several nodes
  1993. * Tweak to register allocator to cause less spills
  1994. * 8-bit registers now interfere with esi,edi and ebp
  1995. Compiler can now compile rtl successfully when using new register
  1996. allocator
  1997. Revision 1.24 2003/04/24 13:03:01 florian
  1998. * comp is now written with its bit pattern to the ppu instead as an extended
  1999. Revision 1.23 2003/04/22 14:33:38 peter
  2000. * removed some notes/hints
  2001. Revision 1.22 2003/04/22 10:09:34 daniel
  2002. + Implemented the actual register allocator
  2003. + Scratch registers unavailable when new register allocator used
  2004. + maybe_save/maybe_restore unavailable when new register allocator used
  2005. Revision 1.21 2003/02/19 22:00:14 daniel
  2006. * Code generator converted to new register notation
  2007. - Horribily outdated todo.txt removed
  2008. Revision 1.20 2003/01/30 21:46:20 peter
  2009. * tai_const_symbol.createdataname added
  2010. Revision 1.19 2003/01/21 08:48:08 daniel
  2011. * Another 200301081 fixed
  2012. Revision 1.18 2003/01/09 20:40:59 daniel
  2013. * Converted some code in cgx86.pas to new register numbering
  2014. Revision 1.17 2003/01/09 15:49:56 daniel
  2015. * Added register conversion
  2016. Revision 1.16 2003/01/08 18:43:56 daniel
  2017. * Tregister changed into a record
  2018. Revision 1.15 2003/01/05 13:36:53 florian
  2019. * x86-64 compiles
  2020. + very basic support for float128 type (x86-64 only)
  2021. Revision 1.14 2002/12/06 17:50:21 peter
  2022. * symbol count fix merged
  2023. Revision 1.13 2002/11/17 16:31:55 carl
  2024. * memory optimization (3-4%) : cleanup of tai fields,
  2025. cleanup of tdef and tsym fields.
  2026. * make it work for m68k
  2027. Revision 1.12 2002/11/15 16:29:30 peter
  2028. * made tasmsymbol.refs private (merged)
  2029. Revision 1.11 2002/11/15 01:58:45 peter
  2030. * merged changes from 1.0.7 up to 04-11
  2031. - -V option for generating bug report tracing
  2032. - more tracing for option parsing
  2033. - errors for cdecl and high()
  2034. - win32 import stabs
  2035. - win32 records<=8 are returned in eax:edx (turned off by default)
  2036. - heaptrc update
  2037. - more info for temp management in .s file with EXTDEBUG
  2038. Revision 1.10 2002/11/09 15:38:03 carl
  2039. + NOOPT removed the optinfo field
  2040. Revision 1.9 2002/10/05 12:43:23 carl
  2041. * fixes for Delphi 6 compilation
  2042. (warning : Some features do not work under Delphi)
  2043. Revision 1.8 2002/08/19 19:36:42 peter
  2044. * More fixes for cross unit inlining, all tnodes are now implemented
  2045. * Moved pocall_internconst to po_internconst because it is not a
  2046. calling type at all and it conflicted when inlining of these small
  2047. functions was requested
  2048. Revision 1.7 2002/08/18 20:06:23 peter
  2049. * inlining is now also allowed in interface
  2050. * renamed write/load to ppuwrite/ppuload
  2051. * tnode storing in ppu
  2052. * nld,ncon,nbas are already updated for storing in ppu
  2053. Revision 1.6 2002/08/16 05:21:09 florian
  2054. * powerpc compilation fix
  2055. Revision 1.5 2002/08/15 19:10:35 peter
  2056. * first things tai,tnode storing in ppu
  2057. Revision 1.4 2002/08/11 14:32:25 peter
  2058. * renamed current_library to objectlibrary
  2059. Revision 1.3 2002/08/11 13:24:10 peter
  2060. * saving of asmsymbols in ppu supported
  2061. * asmsymbollist global is removed and moved into a new class
  2062. tasmlibrarydata that will hold the info of a .a file which
  2063. corresponds with a single module. Added librarydata to tmodule
  2064. to keep the library info stored for the module. In the future the
  2065. objectfiles will also be stored to the tasmlibrarydata class
  2066. * all getlabel/newasmsymbol and friends are moved to the new class
  2067. Revision 1.2 2002/08/05 18:27:48 carl
  2068. + more more more documentation
  2069. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2070. Revision 1.1 2002/07/01 18:46:20 peter
  2071. * internal linker
  2072. * reorganized aasm layer
  2073. Revision 1.27 2002/05/18 13:34:04 peter
  2074. * readded missing revisions
  2075. Revision 1.25 2002/05/14 19:34:38 peter
  2076. * removed old logs and updated copyright year
  2077. Revision 1.24 2002/05/14 17:28:08 peter
  2078. * synchronized cpubase between powerpc and i386
  2079. * moved more tables from cpubase to cpuasm
  2080. * tai_align_abstract moved to tainst, cpuasm must define
  2081. the tai_align class now, which may be empty
  2082. Revision 1.23 2002/04/15 18:54:34 carl
  2083. - removed tcpuflags
  2084. Revision 1.22 2002/04/07 13:18:19 carl
  2085. + more documentation
  2086. Revision 1.21 2002/04/07 10:17:40 carl
  2087. - remove packenumfixed (requires version 1.0.2 or later to compile now!)
  2088. + changing some comments so its commented automatically
  2089. Revision 1.20 2002/03/24 19:04:31 carl
  2090. + patch for SPARC from Mazen NEIFER
  2091. }