ncgbas.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Florian Klaempfl
  4. This unit implements some basic nodes
  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. unit ncgbas;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nbas;
  23. type
  24. tcgnothingnode = class(tnothingnode)
  25. procedure pass_2;override;
  26. end;
  27. tcgasmnode = class(tasmnode)
  28. procedure pass_2;override;
  29. end;
  30. tcgstatementnode = class(tstatementnode)
  31. procedure pass_2;override;
  32. end;
  33. tcgblocknode = class(tblocknode)
  34. procedure pass_2;override;
  35. end;
  36. tcgtempcreatenode = class(ttempcreatenode)
  37. procedure pass_2;override;
  38. end;
  39. tcgtemprefnode = class(ttemprefnode)
  40. procedure pass_2;override;
  41. end;
  42. tcgtempdeletenode = class(ttempdeletenode)
  43. procedure pass_2;override;
  44. end;
  45. implementation
  46. uses
  47. globtype,systems,
  48. cutils,verbose,globals,
  49. aasm,symsym,
  50. cpubase,cpuasm,
  51. nflw,pass_2,
  52. cgbase,cgobj,tgobj,rgobj
  53. ;
  54. {*****************************************************************************
  55. TNOTHING
  56. *****************************************************************************}
  57. procedure tcgnothingnode.pass_2;
  58. begin
  59. { avoid an abstract rte }
  60. end;
  61. {*****************************************************************************
  62. TSTATEMENTNODE
  63. *****************************************************************************}
  64. procedure tcgstatementnode.pass_2;
  65. var
  66. hp : tnode;
  67. begin
  68. hp:=self;
  69. while assigned(hp) do
  70. begin
  71. if assigned(tstatementnode(hp).right) then
  72. begin
  73. rg.cleartempgen;
  74. secondpass(tstatementnode(hp).right);
  75. { Compiler inserted blocks can return values }
  76. location_copy(location,tstatementnode(hp).right.location);
  77. end;
  78. hp:=tstatementnode(hp).left;
  79. end;
  80. end;
  81. {*****************************************************************************
  82. TASMNODE
  83. *****************************************************************************}
  84. procedure tcgasmnode.pass_2;
  85. procedure ReLabel(var p:tasmsymbol);
  86. begin
  87. if p.proclocal then
  88. begin
  89. if not assigned(p.altsymbol) then
  90. begin
  91. { generatealtsymbol will also increase the refs }
  92. p.GenerateAltSymbol;
  93. UsedAsmSymbolListInsert(p);
  94. end
  95. else
  96. begin
  97. { increase the refs, they will be decreased when the
  98. asmnode is destroyed }
  99. inc(p.refs);
  100. end;
  101. p:=p.altsymbol;
  102. end;
  103. end;
  104. var
  105. hp,hp2 : tai;
  106. localfixup,parafixup,
  107. i : longint;
  108. skipnode : boolean;
  109. begin
  110. if inlining_procedure then
  111. begin
  112. CreateUsedAsmSymbolList;
  113. localfixup:=aktprocdef.localst.address_fixup;
  114. parafixup:=aktprocdef.parast.address_fixup;
  115. hp:=tai(p_asm.first);
  116. while assigned(hp) do
  117. begin
  118. hp2:=tai(hp.getcopy);
  119. skipnode:=false;
  120. case hp2.typ of
  121. ait_label :
  122. begin
  123. { regenerate the labels by setting altsymbol }
  124. ReLabel(tasmsymbol(tai_label(hp2).l));
  125. end;
  126. ait_const_rva,
  127. ait_const_symbol :
  128. begin
  129. ReLabel(tai_const_symbol(hp2).sym);
  130. end;
  131. ait_instruction :
  132. begin
  133. { remove cached insentry, because the new code can
  134. require an other less optimized instruction }
  135. {$ifdef i386}
  136. {$ifndef NOAG386BIN}
  137. taicpu(hp2).ResetPass1;
  138. {$endif}
  139. {$endif}
  140. { fixup the references }
  141. for i:=1 to taicpu(hp2).ops do
  142. begin
  143. with taicpu(hp2).oper[i-1] do
  144. begin
  145. case typ of
  146. top_ref :
  147. begin
  148. case ref^.options of
  149. ref_parafixup :
  150. ref^.offsetfixup:=parafixup;
  151. ref_localfixup :
  152. ref^.offsetfixup:=localfixup;
  153. end;
  154. if assigned(ref^.symbol) then
  155. ReLabel(ref^.symbol);
  156. end;
  157. top_symbol :
  158. begin
  159. ReLabel(sym);
  160. end;
  161. end;
  162. end;
  163. end;
  164. end;
  165. ait_marker :
  166. begin
  167. { it's not an assembler block anymore }
  168. if (tai_marker(hp2).kind in [AsmBlockStart, AsmBlockEnd]) then
  169. skipnode:=true;
  170. end;
  171. else
  172. end;
  173. if not skipnode then
  174. exprasmList.concat(hp2)
  175. else
  176. hp2.free;
  177. hp:=tai(hp.next);
  178. end;
  179. { restore used symbols }
  180. UsedAsmSymbolListResetAltSym;
  181. DestroyUsedAsmSymbolList;
  182. end
  183. else
  184. begin
  185. { if the routine is an inline routine, then we must hold a copy
  186. because it can be necessary for inlining later }
  187. if (aktprocdef.proccalloption=pocall_inline) then
  188. exprasmList.concatlistcopy(p_asm)
  189. else
  190. exprasmList.concatlist(p_asm);
  191. end;
  192. if not (nf_object_preserved in flags) then
  193. cg.g_maybe_loadself(exprasmlist);
  194. end;
  195. {*****************************************************************************
  196. TBLOCKNODE
  197. *****************************************************************************}
  198. procedure tcgblocknode.pass_2;
  199. begin
  200. { do second pass on left node }
  201. if assigned(left) then
  202. begin
  203. secondpass(left);
  204. { Compiler inserted blocks can return values }
  205. location_copy(location,left.location);
  206. end;
  207. end;
  208. {*****************************************************************************
  209. TTEMPCREATENODE
  210. *****************************************************************************}
  211. procedure tcgtempcreatenode.pass_2;
  212. begin
  213. { if we're secondpassing the same tcgtempcreatenode twice, we have a bug }
  214. if tempinfo^.valid then
  215. internalerror(200108222);
  216. { get a (persistent) temp }
  217. if persistent then
  218. tg.gettempofsizereferencepersistant(exprasmlist,size,tempinfo^.ref)
  219. else
  220. tg.gettempofsizereference(exprasmlist,size,tempinfo^.ref);
  221. tempinfo^.valid := true;
  222. end;
  223. {*****************************************************************************
  224. TTEMPREFNODE
  225. *****************************************************************************}
  226. procedure tcgtemprefnode.pass_2;
  227. begin
  228. { check if the temp is valid }
  229. if not tempinfo^.valid then
  230. internalerror(200108231);
  231. { set the temp's location }
  232. location_reset(location,LOC_REFERENCE,def_cgsize(tempinfo^.restype.def));
  233. location.reference := tempinfo^.ref;
  234. inc(location.reference.offset,offset);
  235. end;
  236. {*****************************************************************************
  237. TTEMPDELETENODE
  238. *****************************************************************************}
  239. procedure tcgtempdeletenode.pass_2;
  240. begin
  241. if release_to_normal then
  242. tg.persistanttemptonormal(tempinfo^.ref.offset)
  243. else
  244. tg.ungetpersistanttempreference(exprasmlist,tempinfo^.ref);
  245. end;
  246. begin
  247. cnothingnode:=tcgnothingnode;
  248. casmnode:=tcgasmnode;
  249. cstatementnode:=tcgstatementnode;
  250. cblocknode:=tcgblocknode;
  251. ctempcreatenode:=tcgtempcreatenode;
  252. ctemprefnode:=tcgtemprefnode;
  253. ctempdeletenode:=tcgtempdeletenode;
  254. end.
  255. {
  256. $Log$
  257. Revision 1.18 2002-05-16 19:46:37 carl
  258. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  259. + try to fix temp allocation (still in ifdef)
  260. + generic constructor calls
  261. + start of tassembler / tmodulebase class cleanup
  262. Revision 1.16 2002/05/13 19:54:37 peter
  263. * removed n386ld and n386util units
  264. * maybe_save/maybe_restore added instead of the old maybe_push
  265. Revision 1.15 2002/05/12 16:53:07 peter
  266. * moved entry and exitcode to ncgutil and cgobj
  267. * foreach gets extra argument for passing local data to the
  268. iterator function
  269. * -CR checks also class typecasts at runtime by changing them
  270. into as
  271. * fixed compiler to cycle with the -CR option
  272. * fixed stabs with elf writer, finally the global variables can
  273. be watched
  274. * removed a lot of routines from cga unit and replaced them by
  275. calls to cgobj
  276. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  277. u32bit then the other is typecasted also to u32bit without giving
  278. a rangecheck warning/error.
  279. * fixed pascal calling method with reversing also the high tree in
  280. the parast, detected by tcalcst3 test
  281. Revision 1.14 2002/04/23 19:16:34 peter
  282. * add pinline unit that inserts compiler supported functions using
  283. one or more statements
  284. * moved finalize and setlength from ninl to pinline
  285. Revision 1.13 2002/04/21 19:02:03 peter
  286. * removed newn and disposen nodes, the code is now directly
  287. inlined from pexpr
  288. * -an option that will write the secondpass nodes to the .s file, this
  289. requires EXTDEBUG define to actually write the info
  290. * fixed various internal errors and crashes due recent code changes
  291. Revision 1.12 2002/04/04 19:05:57 peter
  292. * removed unused units
  293. * use tlocation.size in cg.a_*loc*() routines
  294. Revision 1.11 2002/03/31 20:26:34 jonas
  295. + a_loadfpu_* and a_loadmm_* methods in tcg
  296. * register allocation is now handled by a class and is mostly processor
  297. independent (+rgobj.pas and i386/rgcpu.pas)
  298. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  299. * some small improvements and fixes to the optimizer
  300. * some register allocation fixes
  301. * some fpuvaroffset fixes in the unary minus node
  302. * push/popusedregisters is now called rg.save/restoreusedregisters and
  303. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  304. also better optimizable)
  305. * fixed and optimized register saving/restoring for new/dispose nodes
  306. * LOC_FPU locations now also require their "register" field to be set to
  307. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  308. - list field removed of the tnode class because it's not used currently
  309. and can cause hard-to-find bugs
  310. Revision 1.10 2001/12/31 16:54:14 peter
  311. * fixed inline crash with assembler routines
  312. Revision 1.9 2001/11/02 22:58:01 peter
  313. * procsym definition rewrite
  314. Revision 1.8 2001/10/25 21:22:35 peter
  315. * calling convention rewrite
  316. Revision 1.7 2001/08/26 13:36:39 florian
  317. * some cg reorganisation
  318. * some PPC updates
  319. Revision 1.6 2001/08/24 13:47:27 jonas
  320. * moved "reverseparameters" from ninl.pas to ncal.pas
  321. + support for non-persistent temps in ttempcreatenode.create, for use
  322. with typeconversion nodes
  323. Revision 1.5 2001/08/23 14:28:35 jonas
  324. + tempcreate/ref/delete nodes (allows the use of temps in the
  325. resulttype and first pass)
  326. * made handling of read(ln)/write(ln) processor independent
  327. * moved processor independent handling for str and reset/rewrite-typed
  328. from firstpass to resulttype pass
  329. * changed names of helpers in text.inc to be generic for use as
  330. compilerprocs + added "iocheck" directive for most of them
  331. * reading of ordinals is done by procedures instead of functions
  332. because otherwise FPC_IOCHECK overwrote the result before it could
  333. be stored elsewhere (range checking still works)
  334. * compilerprocs can now be used in the system unit before they are
  335. implemented
  336. * added note to errore.msg that booleans can't be read using read/readln
  337. Revision 1.4 2001/06/02 19:22:15 peter
  338. * refs count for relabeled asmsymbols fixed
  339. Revision 1.3 2001/05/18 22:31:06 peter
  340. * tasmnode.pass_2 is independent of cpu, moved to ncgbas
  341. * include ncgbas for independent nodes
  342. Revision 1.2 2001/04/13 01:22:08 peter
  343. * symtable change to classes
  344. * range check generation and errors fixed, make cycle DEBUG=1 works
  345. * memory leaks fixed
  346. Revision 1.1 2000/10/14 10:14:50 peter
  347. * moehrendorf oct 2000 rewrite
  348. }