Browse Source

Also add the initialization/finalization sections and the class constructors/destructors to the imported symbols of the current program/library.

ngenutil.pas, tnodeutils.InsertInitFinalTable:
  + new function add_initfinal_import to find the procsym for the initialization/finalization code and to add it to the imported unit symbols
  * write_struct_inits: import the found class constructor/destructor symbols if they are not in the program/library unit
  * use add_initfinal_import for the initialization and finalization sections of all used units beside the program/library unit

git-svn-id: branches/svenbarth/packages@28856 -
svenbarth 10 years ago
parent
commit
565afda13b
1 changed files with 51 additions and 5 deletions
  1. 51 5
      compiler/ngenutil.pas

+ 51 - 5
compiler/ngenutil.pas

@@ -109,7 +109,7 @@ implementation
       scanner,systems,procinfo,fmodule,
       scanner,systems,procinfo,fmodule,
       aasmbase,aasmdata,aasmtai,
       aasmbase,aasmdata,aasmtai,
       symbase,symtable,defutil,
       symbase,symtable,defutil,
-      nadd,ncal,ncnv,ncon,nflw,ninl,nld,nmem,nobj,nutils,
+      nadd,ncal,ncnv,ncon,nflw,ninl,nld,nmem,nobj,nutils,ncgutil,
       ppu,
       ppu,
       pass_1;
       pass_1;
 
 
@@ -622,6 +622,7 @@ implementation
       hp : tused_unit;
       hp : tused_unit;
       unitinits : TAsmList;
       unitinits : TAsmList;
       count : longint;
       count : longint;
+      name : tsymstr;
 
 
       procedure write_struct_inits(u: tmodule);
       procedure write_struct_inits(u: tmodule);
         var
         var
@@ -638,12 +639,20 @@ implementation
           begin
           begin
             pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_constructor);
             pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_constructor);
             if assigned(pd) then
             if assigned(pd) then
-              unitinits.concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0))
+              begin
+                unitinits.concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0));
+                if u<>current_module then
+                  u.unitimportsyms.add(pd.procsym);
+              end
             else
             else
               unitinits.concat(Tai_const.Create_nil_codeptr);
               unitinits.concat(Tai_const.Create_nil_codeptr);
             pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_destructor);
             pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_destructor);
             if assigned(pd) then
             if assigned(pd) then
-              unitinits.concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0))
+              begin
+                unitinits.concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0));
+                if u<>current_module then
+                  u.unitimportsyms.add(pd.procsym);
+              end
             else
             else
               unitinits.concat(Tai_const.Create_nil_codeptr);
               unitinits.concat(Tai_const.Create_nil_codeptr);
             inc(count);
             inc(count);
@@ -651,6 +660,35 @@ implementation
           structlist.free;
           structlist.free;
         end;
         end;
 
 
+      procedure add_initfinal_import(name:tsymstr;symtable:tsymtable);
+        var
+          found : boolean;
+          i,j : longint;
+          sym : tsymentry;
+          pd : tprocdef;
+        begin
+          found:=false;
+          for i:=0 to symtable.SymList.count-1 do
+            begin
+              sym:=tsymentry(symtable.symlist[i]);
+              if sym.typ<>procsym then
+                continue;
+              for j:=0 to tprocsym(sym).procdeflist.count-1 do
+                begin
+                  pd:=tprocdef(tprocsym(sym).procdeflist[j]);
+                  if has_alias_name(pd,name) then
+                    begin
+                      current_module.unitimportsyms.add(sym);
+                      found:=true;
+                      break;
+                    end;
+                end;
+            end;
+          if not found then
+            internalerror(2014101003);
+          current_module.unitimportsyms.add(sym);
+        end;
+
     begin
     begin
       unitinits:=TAsmList.Create;
       unitinits:=TAsmList.Create;
       count:=0;
       count:=0;
@@ -664,11 +702,19 @@ implementation
          if (hp.u.flags and (uf_init or uf_finalize))<>0 then
          if (hp.u.flags and (uf_init or uf_finalize))<>0 then
            begin
            begin
              if (hp.u.flags and uf_init)<>0 then
              if (hp.u.flags and uf_init)<>0 then
-               unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
+               begin
+                 name:=make_mangledname('INIT$',hp.u.globalsymtable,'');
+                 unitinits.concat(Tai_const.Createname(name,AT_FUNCTION,0));
+                 add_initfinal_import(name,hp.u.localsymtable);
+               end
              else
              else
                unitinits.concat(Tai_const.Create_nil_codeptr);
                unitinits.concat(Tai_const.Create_nil_codeptr);
              if (hp.u.flags and uf_finalize)<>0 then
              if (hp.u.flags and uf_finalize)<>0 then
-               unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
+               begin
+                 name:=make_mangledname('FINALIZE$',hp.u.globalsymtable,'');
+                 unitinits.concat(Tai_const.Createname(name,AT_FUNCTION,0));
+                 add_initfinal_import(name,hp.u.localsymtable);
+               end
              else
              else
                unitinits.concat(Tai_const.Create_nil_codeptr);
                unitinits.concat(Tai_const.Create_nil_codeptr);
              inc(count);
              inc(count);