ncgnstld.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. Support for load nodes on targets that have to group all local variables
  4. and parameters accessed by nested routines into structs (and then pass the
  5. address of these structs to nested routines rather than the frame pointer,
  6. and access the local variables as fields thereof)
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit ncgnstld;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. node,
  25. symtype,
  26. nld,
  27. ncgld;
  28. type
  29. tcgnestloadnode = class(tcgloadnode)
  30. protected
  31. nestsym: tsym;
  32. nestsymderef: tderef;
  33. procedure generate_nested_access(vs: tsym);override;
  34. public
  35. function pass_typecheck: tnode; override;
  36. function pass_1:tnode;override;
  37. function dogetcopy: tnode; override;
  38. function docompare(p: tnode): boolean; override;
  39. constructor ppuload(t: tnodetype; ppufile: tcompilerppufile); override;
  40. procedure ppuwrite(ppufile: tcompilerppufile); override;
  41. procedure buildderefimpl; override;
  42. procedure derefimpl; override;
  43. end;
  44. implementation
  45. uses
  46. cutils,verbose,globtype,globals,systems,constexp,
  47. symnot,
  48. defutil,defcmp,
  49. htypechk,pass_1,procinfo,paramgr,
  50. cpuinfo,
  51. symconst,symbase,symsym,symdef,symtable,symcreat,
  52. ncon,ninl,ncnv,nmem,ncal,nutils,nbas,
  53. pass_2,cgbase
  54. ;
  55. {*****************************************************************************
  56. TCGNESTLOADNODE
  57. *****************************************************************************}
  58. procedure tcgnestloadnode.generate_nested_access(vs: tsym);
  59. begin
  60. { left has been transformed into a string of accesses that result in
  61. the location of the original variable's copy in the appropriate
  62. parentfpstruct (via tcgnestloadparentfpnode.pass_1). In case it is a
  63. var/out/constref parameter, that "copy" will have been a copy of the
  64. address so the normal handling of such parameters in ncgld is ok) }
  65. secondpass(left);
  66. location:=left.location;
  67. end;
  68. function tcgnestloadnode.pass_typecheck: tnode;
  69. var
  70. nestedvars: tsym;
  71. begin
  72. result:=inherited pass_typecheck;
  73. if assigned(result) then
  74. exit;
  75. case symtableentry.typ of
  76. paravarsym,
  77. localvarsym :
  78. begin
  79. { Nested variable? Then we have to move it to a structure that
  80. can be passed by reference to nested routines }
  81. if assigned(current_procinfo) and
  82. (symtable.symtabletype in [localsymtable,parasymtable]) and
  83. ((symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) or
  84. { also redirect loads of locals/paras that have been moved to
  85. the parentfpstruct inside the routine in which they were
  86. originally declared, except in the initialisation code for
  87. the parentfpstruct (nf_internal flag) }
  88. (tabstractnormalvarsym(symtableentry).inparentfpstruct and
  89. not(nf_internal in flags))) then
  90. begin
  91. { get struct holding all locals accessed by nested routines }
  92. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  93. { don't add the parentfpstruct to itself! }
  94. if nestedvars=symtableentry then
  95. exit;
  96. if not assigned(nestedvars) then
  97. begin
  98. { create this struct }
  99. build_parentfpstruct(tprocdef(symtable.defowner));
  100. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  101. end;
  102. { store result for use in pass_1 }
  103. nestsym:=maybe_add_sym_to_parentfpstruct(tprocdef(symtableentry.owner.defowner),symtableentry,resultdef,is_addr_param_load);
  104. { left normally holds the parentfp node. If it's not assigned,
  105. this is an access to a local variable/para from the routine
  106. in which it was actually declared -> redirect to its
  107. equivalent in the parentfp struct }
  108. if not assigned(left) then
  109. begin
  110. left:=caddrnode.create_internal(cloadnode.create(tprocdef(symtableentry.owner.defowner).parentfpstruct,tprocdef(symtableentry.owner.defowner).parentfpstruct.owner));
  111. include(left.flags,nf_typedaddr);
  112. end;
  113. typecheckpass(left);
  114. end;
  115. end;
  116. end;
  117. end;
  118. function tcgnestloadnode.pass_1:tnode;
  119. var
  120. thissym,
  121. nestedvars: tsym;
  122. nestedvarsdef: tdef;
  123. begin
  124. result:=inherited;
  125. if assigned(result) then
  126. exit;
  127. case symtableentry.typ of
  128. paravarsym,
  129. localvarsym :
  130. begin
  131. { Nested variable? Then we have to move it to a structure that
  132. can be passed by reference to nested routines }
  133. if assigned(current_procinfo) and
  134. (symtable.symtabletype in [localsymtable,parasymtable]) and
  135. ((symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) or
  136. (tabstractnormalvarsym(symtableentry).inparentfpstruct and
  137. not(nf_internal in flags))) then
  138. begin
  139. { get struct holding all locals accessed by nested routines }
  140. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  141. if not assigned(nestedvars) then
  142. begin
  143. { create this struct }
  144. build_parentfpstruct(tprocdef(symtable.defowner));
  145. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  146. end;
  147. nestedvarsdef:=tlocalvarsym(nestedvars).vardef;
  148. if nestedvars<>symtableentry then
  149. thissym:=nestsym
  150. else
  151. thissym:=find_sym_in_parentfpstruct(tprocdef(symtableentry.owner.defowner),symtableentry);
  152. if not assigned(thissym) then
  153. internalerror(2011060406);
  154. { firstpass the parentfpnode. This will transform it into
  155. a load of the appropriate parentfpstruct }
  156. if not assigned(left) then
  157. internalerror(2011060104);
  158. firstpass(left);
  159. { subscript it to get the variable }
  160. left:=csubscriptnode.create(thissym,cderefnode.create(left));
  161. firstpass(left);
  162. end;
  163. end;
  164. end;
  165. end;
  166. function tcgnestloadnode.dogetcopy: tnode;
  167. begin
  168. result:=inherited dogetcopy;
  169. tcgnestloadnode(result).nestsym:=nestsym;
  170. end;
  171. function tcgnestloadnode.docompare(p: tnode): boolean;
  172. begin
  173. result:=
  174. inherited docompare(p) and
  175. (tcgnestloadnode(p).nestsym=nestsym);
  176. end;
  177. constructor tcgnestloadnode.ppuload(t: tnodetype; ppufile: tcompilerppufile);
  178. begin
  179. inherited ppuload(t, ppufile);
  180. ppufile.getderef(nestsymderef);
  181. end;
  182. procedure tcgnestloadnode.ppuwrite(ppufile: tcompilerppufile);
  183. begin
  184. inherited ppuwrite(ppufile);
  185. ppufile.putderef(nestsymderef);
  186. end;
  187. procedure tcgnestloadnode.buildderefimpl;
  188. begin
  189. inherited buildderefimpl;
  190. nestsymderef.build(nestsym);
  191. end;
  192. procedure tcgnestloadnode.derefimpl;
  193. begin
  194. inherited derefimpl;
  195. nestsym:=tsym(nestsymderef.resolve);
  196. end;
  197. begin
  198. cloadnode:=tcgnestloadnode;
  199. end.