ncgnstld.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. function keep_param_address_in_nested_struct: boolean; virtual;
  35. public
  36. function pass_typecheck: tnode; override;
  37. function pass_1:tnode;override;
  38. function dogetcopy: tnode; override;
  39. function docompare(p: tnode): boolean; override;
  40. constructor ppuload(t: tnodetype; ppufile: tcompilerppufile); override;
  41. procedure ppuwrite(ppufile: tcompilerppufile); override;
  42. procedure buildderefimpl; override;
  43. procedure derefimpl; override;
  44. end;
  45. implementation
  46. uses
  47. cutils,verbose,globtype,globals,systems,constexp,
  48. symnot,
  49. defutil,defcmp,
  50. htypechk,pass_1,procinfo,paramgr,
  51. cpuinfo,
  52. symconst,symbase,symsym,symdef,symtable,symcreat,
  53. ncon,ninl,ncnv,nmem,ncal,nutils,nbas,
  54. pass_2,cgbase
  55. ;
  56. {*****************************************************************************
  57. TCGNESTLOADNODE
  58. *****************************************************************************}
  59. procedure tcgnestloadnode.generate_nested_access(vs: tsym);
  60. begin
  61. { left has been transformed into a string of accesses that result in
  62. the location of the original variable's copy in the appropriate
  63. parentfpstruct (via tcgnestloadparentfpnode.pass_1). In case it is a
  64. var/out/constref parameter, that "copy" will have been a copy of the
  65. address so the normal handling of such parameters in ncgld is ok) }
  66. secondpass(left);
  67. location:=left.location;
  68. end;
  69. function tcgnestloadnode.keep_param_address_in_nested_struct: boolean;
  70. begin
  71. result:=is_addr_param_load;
  72. end;
  73. function tcgnestloadnode.pass_typecheck: tnode;
  74. var
  75. nestedvars: tsym;
  76. begin
  77. result:=inherited pass_typecheck;
  78. if assigned(result) then
  79. exit;
  80. case symtableentry.typ of
  81. paravarsym,
  82. localvarsym :
  83. begin
  84. { Nested variable? Then we have to move it to a structure that
  85. can be passed by reference to nested routines }
  86. if assigned(current_procinfo) and
  87. (symtable.symtabletype in [localsymtable,parasymtable]) and
  88. ((symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) or
  89. { also redirect loads of locals/paras that have been moved to
  90. the parentfpstruct inside the routine in which they were
  91. originally declared, except in the initialisation code for
  92. the parentfpstruct (nf_internal flag) }
  93. (tabstractnormalvarsym(symtableentry).inparentfpstruct and
  94. not(nf_internal in flags))) then
  95. begin
  96. { get struct holding all locals accessed by nested routines }
  97. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  98. { don't add the parentfpstruct to itself! }
  99. if nestedvars=symtableentry then
  100. exit;
  101. if not assigned(nestedvars) then
  102. begin
  103. { create this struct }
  104. build_parentfpstruct(tprocdef(symtable.defowner));
  105. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  106. end;
  107. { store result for use in pass_1 }
  108. nestsym:=maybe_add_sym_to_parentfpstruct(tprocdef(symtableentry.owner.defowner),symtableentry,resultdef,keep_param_address_in_nested_struct);
  109. { left normally holds the parentfp node. If it's not assigned,
  110. this is an access to a local variable/para from the routine
  111. in which it was actually declared -> redirect to its
  112. equivalent in the parentfp struct }
  113. if not assigned(left) then
  114. begin
  115. left:=caddrnode.create_internal(cloadnode.create(tprocdef(symtableentry.owner.defowner).parentfpstruct,tprocdef(symtableentry.owner.defowner).parentfpstruct.owner));
  116. include(left.flags,nf_typedaddr);
  117. end;
  118. typecheckpass(left);
  119. end;
  120. end;
  121. end;
  122. end;
  123. function tcgnestloadnode.pass_1:tnode;
  124. var
  125. thissym,
  126. nestedvars: tsym;
  127. nestedvarsdef: tdef;
  128. begin
  129. result:=inherited;
  130. if assigned(result) then
  131. exit;
  132. case symtableentry.typ of
  133. paravarsym,
  134. localvarsym :
  135. begin
  136. { Nested variable? Then we have to move it to a structure that
  137. can be passed by reference to nested routines }
  138. if assigned(current_procinfo) and
  139. (symtable.symtabletype in [localsymtable,parasymtable]) and
  140. ((symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) or
  141. (tabstractnormalvarsym(symtableentry).inparentfpstruct and
  142. not(nf_internal in flags))) then
  143. begin
  144. { get struct holding all locals accessed by nested routines }
  145. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  146. if not assigned(nestedvars) then
  147. begin
  148. { create this struct }
  149. build_parentfpstruct(tprocdef(symtable.defowner));
  150. nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
  151. end;
  152. nestedvarsdef:=tlocalvarsym(nestedvars).vardef;
  153. if nestedvars<>symtableentry then
  154. thissym:=nestsym
  155. else
  156. thissym:=find_sym_in_parentfpstruct(tprocdef(symtableentry.owner.defowner),symtableentry);
  157. if not assigned(thissym) then
  158. internalerror(2011060406);
  159. { firstpass the parentfpnode. This will transform it into
  160. a load of the appropriate parentfpstruct }
  161. if not assigned(left) then
  162. internalerror(2011060104);
  163. firstpass(left);
  164. { subscript it to get the variable }
  165. left:=csubscriptnode.create(thissym,cderefnode.create(left));
  166. firstpass(left);
  167. end;
  168. end;
  169. end;
  170. end;
  171. function tcgnestloadnode.dogetcopy: tnode;
  172. begin
  173. result:=inherited dogetcopy;
  174. tcgnestloadnode(result).nestsym:=nestsym;
  175. end;
  176. function tcgnestloadnode.docompare(p: tnode): boolean;
  177. begin
  178. result:=
  179. inherited docompare(p) and
  180. (tcgnestloadnode(p).nestsym=nestsym);
  181. end;
  182. constructor tcgnestloadnode.ppuload(t: tnodetype; ppufile: tcompilerppufile);
  183. begin
  184. inherited ppuload(t, ppufile);
  185. ppufile.getderef(nestsymderef);
  186. end;
  187. procedure tcgnestloadnode.ppuwrite(ppufile: tcompilerppufile);
  188. begin
  189. inherited ppuwrite(ppufile);
  190. ppufile.putderef(nestsymderef);
  191. end;
  192. procedure tcgnestloadnode.buildderefimpl;
  193. begin
  194. inherited buildderefimpl;
  195. nestsymderef.build(nestsym);
  196. end;
  197. procedure tcgnestloadnode.derefimpl;
  198. begin
  199. inherited derefimpl;
  200. nestsym:=tsym(nestsymderef.resolve);
  201. end;
  202. begin
  203. cloadnode:=tcgnestloadnode;
  204. end.