ncgbas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 defines.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,cclasses,verbose,globals,
  49. aasm,symconst,symsym,symtable,types,
  50. htypechk,
  51. cpubase,cpuasm,
  52. nflw,pass_2,
  53. cgbase,
  54. cga,
  55. tgcpu,temp_gen
  56. ;
  57. {*****************************************************************************
  58. TNOTHING
  59. *****************************************************************************}
  60. procedure tcgnothingnode.pass_2;
  61. begin
  62. { avoid an abstract rte }
  63. end;
  64. {*****************************************************************************
  65. TSTATEMENTNODE
  66. *****************************************************************************}
  67. procedure tcgstatementnode.pass_2;
  68. var
  69. hp : tnode;
  70. begin
  71. hp:=self;
  72. while assigned(hp) do
  73. begin
  74. if assigned(tstatementnode(hp).right) then
  75. begin
  76. cleartempgen;
  77. secondpass(tstatementnode(hp).right);
  78. end;
  79. hp:=tstatementnode(hp).left;
  80. end;
  81. end;
  82. {*****************************************************************************
  83. TASMNODE
  84. *****************************************************************************}
  85. procedure tcgasmnode.pass_2;
  86. procedure ReLabel(var p:tasmsymbol);
  87. begin
  88. if p.proclocal then
  89. begin
  90. if not assigned(p.altsymbol) then
  91. begin
  92. { generatealtsymbol will also increase the refs }
  93. p.GenerateAltSymbol;
  94. UsedAsmSymbolListInsert(p);
  95. end
  96. else
  97. begin
  98. { increase the refs, they will be decreased when the
  99. asmnode is destroyed }
  100. inc(p.refs);
  101. end;
  102. p:=p.altsymbol;
  103. end;
  104. end;
  105. var
  106. hp,hp2 : tai;
  107. localfixup,parafixup,
  108. i : longint;
  109. skipnode : boolean;
  110. begin
  111. if inlining_procedure then
  112. begin
  113. CreateUsedAsmSymbolList;
  114. localfixup:=aktprocdef.localst.address_fixup;
  115. parafixup:=aktprocdef.parast.address_fixup;
  116. hp:=tai(p_asm.first);
  117. while assigned(hp) do
  118. begin
  119. hp2:=tai(hp.getcopy);
  120. skipnode:=false;
  121. case hp2.typ of
  122. ait_label :
  123. begin
  124. { regenerate the labels by setting altsymbol }
  125. ReLabel(tasmsymbol(tai_label(hp2).l));
  126. end;
  127. ait_const_rva,
  128. ait_const_symbol :
  129. begin
  130. ReLabel(tai_const_symbol(hp2).sym);
  131. end;
  132. ait_instruction :
  133. begin
  134. { fixup the references }
  135. for i:=1 to taicpu(hp2).ops do
  136. begin
  137. with taicpu(hp2).oper[i-1] do
  138. begin
  139. case typ of
  140. top_ref :
  141. begin
  142. case ref^.options of
  143. ref_parafixup :
  144. ref^.offsetfixup:=parafixup;
  145. ref_localfixup :
  146. ref^.offsetfixup:=localfixup;
  147. end;
  148. if assigned(ref^.symbol) then
  149. ReLabel(ref^.symbol);
  150. end;
  151. top_symbol :
  152. begin
  153. ReLabel(sym);
  154. end;
  155. end;
  156. end;
  157. end;
  158. end;
  159. ait_marker :
  160. begin
  161. { it's not an assembler block anymore }
  162. if (tai_marker(hp2).kind in [AsmBlockStart, AsmBlockEnd]) then
  163. skipnode:=true;
  164. end;
  165. else
  166. end;
  167. if not skipnode then
  168. exprasmList.concat(hp2)
  169. else
  170. hp2.free;
  171. hp:=tai(hp.next);
  172. end;
  173. { restore used symbols }
  174. UsedAsmSymbolListResetAltSym;
  175. DestroyUsedAsmSymbolList;
  176. end
  177. else
  178. begin
  179. { if the routine is an inline routine, then we must hold a copy
  180. because it can be necessary for inlining later }
  181. if (aktprocdef.proccalloption=pocall_inline) then
  182. exprasmList.concatlistcopy(p_asm)
  183. else
  184. exprasmList.concatlist(p_asm);
  185. end;
  186. if not (nf_object_preserved in flags) then
  187. maybe_loadself;
  188. end;
  189. {*****************************************************************************
  190. TBLOCKNODE
  191. *****************************************************************************}
  192. procedure tcgblocknode.pass_2;
  193. begin
  194. { do second pass on left node }
  195. if assigned(left) then
  196. secondpass(left);
  197. end;
  198. {*****************************************************************************
  199. TTEMPCREATENODE
  200. *****************************************************************************}
  201. procedure tcgtempcreatenode.pass_2;
  202. begin
  203. { if we're secondpassing the same tcgtempcreatenode twice, we have a bug }
  204. if tempinfo^.valid then
  205. internalerror(200108222);
  206. { get a (persistent) temp }
  207. if persistent then
  208. gettempofsizereferencepersistant(size,tempinfo^.ref)
  209. else
  210. gettempofsizereference(size,tempinfo^.ref);
  211. tempinfo^.valid := true;
  212. end;
  213. {*****************************************************************************
  214. TTEMPREFNODE
  215. *****************************************************************************}
  216. procedure tcgtemprefnode.pass_2;
  217. begin
  218. { check if the temp is valid }
  219. if not tempinfo^.valid then
  220. internalerror(200108231);
  221. { set the temp's location }
  222. location.loc := LOC_REFERENCE;
  223. location.reference := tempinfo^.ref;
  224. end;
  225. {*****************************************************************************
  226. TTEMPDELETENODE
  227. *****************************************************************************}
  228. procedure tcgtempdeletenode.pass_2;
  229. begin
  230. ungetpersistanttempreference(tempinfo^.ref);
  231. end;
  232. begin
  233. cnothingnode:=tcgnothingnode;
  234. casmnode:=tcgasmnode;
  235. cstatementnode:=tcgstatementnode;
  236. cblocknode:=tcgblocknode;
  237. ctempcreatenode:=tcgtempcreatenode;
  238. ctemprefnode:=tcgtemprefnode;
  239. ctempdeletenode:=tcgtempdeletenode;
  240. end.
  241. {
  242. $Log$
  243. Revision 1.9 2001-11-02 22:58:01 peter
  244. * procsym definition rewrite
  245. Revision 1.8 2001/10/25 21:22:35 peter
  246. * calling convention rewrite
  247. Revision 1.7 2001/08/26 13:36:39 florian
  248. * some cg reorganisation
  249. * some PPC updates
  250. Revision 1.6 2001/08/24 13:47:27 jonas
  251. * moved "reverseparameters" from ninl.pas to ncal.pas
  252. + support for non-persistent temps in ttempcreatenode.create, for use
  253. with typeconversion nodes
  254. Revision 1.5 2001/08/23 14:28:35 jonas
  255. + tempcreate/ref/delete nodes (allows the use of temps in the
  256. resulttype and first pass)
  257. * made handling of read(ln)/write(ln) processor independent
  258. * moved processor independent handling for str and reset/rewrite-typed
  259. from firstpass to resulttype pass
  260. * changed names of helpers in text.inc to be generic for use as
  261. compilerprocs + added "iocheck" directive for most of them
  262. * reading of ordinals is done by procedures instead of functions
  263. because otherwise FPC_IOCHECK overwrote the result before it could
  264. be stored elsewhere (range checking still works)
  265. * compilerprocs can now be used in the system unit before they are
  266. implemented
  267. * added note to errore.msg that booleans can't be read using read/readln
  268. Revision 1.4 2001/06/02 19:22:15 peter
  269. * refs count for relabeled asmsymbols fixed
  270. Revision 1.3 2001/05/18 22:31:06 peter
  271. * tasmnode.pass_2 is independent of cpu, moved to ncgbas
  272. * include ncgbas for independent nodes
  273. Revision 1.2 2001/04/13 01:22:08 peter
  274. * symtable change to classes
  275. * range check generation and errors fixed, make cycle DEBUG=1 works
  276. * memory leaks fixed
  277. Revision 1.1 2000/10/14 10:14:50 peter
  278. * moehrendorf oct 2000 rewrite
  279. }