aasmtai.pas 71 KB

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