Przeglądaj źródła

* reworked external symbol handling on llvm to deal with the fact that
a single external symbol may be used with multiple declarations that
have different types:
o always declare an alias with the Pascal-mangled name that aliases the
external symbol, with the type of that Pascal declaration
o if the external symbol is not actually external but in the same unit,
we will determine the type of that symbol via its declaration and
insert type conversions in the alias declarations in case they use a
different type
o if it is external (so there is no declaration from which we can determine
its real type) and there are nevertheless multiple aliases for it with
different types, we just take the type of the first alias and insert
type conversions for the other aliases. LLVM will take care of the
conflicting types in multiple modules when performing WPO if necessary

git-svn-id: trunk@31054 -

Jonas Maebe 10 lat temu
rodzic
commit
cf2e46c2c1

+ 1 - 0
.gitattributes

@@ -344,6 +344,7 @@ compiler/llvm/nllvmmem.pas svneol=native#text/plain
 compiler/llvm/nllvmtcon.pas svneol=native#text/plain
 compiler/llvm/nllvmutil.pas svneol=native#text/plain
 compiler/llvm/rgllvm.pas svneol=native#text/plain
+compiler/llvm/symllvm.pas svneol=native#text/plain
 compiler/llvm/tgllvm.pas svneol=native#text/plain
 compiler/m68k/aasmcpu.pas svneol=native#text/plain
 compiler/m68k/ag68kgas.pas svneol=native#text/plain

+ 20 - 5
compiler/llvm/hlcgllvm.pas

@@ -85,6 +85,7 @@ uses
 
       procedure gen_proc_symbol(list: TAsmList); override;
       procedure gen_proc_symbol_end(list: TAsmList); override;
+      procedure handle_external_proc(list: TAsmList; pd: tprocdef; const importname: TSymStr); override;
       procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
       procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
      protected
@@ -148,7 +149,7 @@ implementation
     defutil,llvmdef,llvmsym,
     aasmtai,aasmcpu,
     aasmllvm,llvmbase,tgllvm,
-    symtable,
+    symtable,symllvm,
     paramgr,llvmpara,
     procinfo,cpuinfo,cgobj,cgllvm,cghlcpu;
 
@@ -442,12 +443,11 @@ implementation
       calldef: tdef;
       res: tregister;
     begin
-      if not pd.owner.iscurrentunit or
-         (s<>pd.mangledname) or
-         (po_external in pd.procoptions) then
+      if not pd.owner.iscurrentunit then
         begin
           asmsym:=current_asmdata.RefAsmSymbol(tprocdef(pd).mangledname);
-          if not asmsym.declared then
+          if (not asmsym.declared) and
+             (asmsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) then
             current_asmdata.AsmLists[al_imports].Concat(taillvmdecl.create(asmsym,pd,nil,sec_code,pd.alignment));
         end;
       a_call_common(list,pd,paras,forceresdef,res,calldef,hlretdef,llvmretdef,callparas);
@@ -1032,6 +1032,8 @@ implementation
       mangledname: TSymStr;
       asmsym: tasmsymbol;
     begin
+      if po_external in current_procinfo.procdef.procoptions then
+        exit;
       item:=TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
       mangledname:=current_procinfo.procdef.mangledname;
       { predefine the real function name as local/global, so the aliases can
@@ -1058,6 +1060,17 @@ implementation
     end;
 
 
+  procedure thlcgllvm.handle_external_proc(list: TAsmList; pd: tprocdef; const importname: TSymStr);
+    var
+      asmsym: tasmsymbol;
+    begin
+      { Windows-style import names are not yet supported -> ignore
+        importname for now }
+      asmsym:=current_asmdata.RefAsmSymbol(tllvmprocdef(pd).external_mangledname,AT_FUNCTION);
+      list.concat(taillvmalias.create(asmsym,pd.mangledname,pd,llv_default,lll_internal));
+    end;
+
+
   procedure thlcgllvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
     begin
       list.concatlist(ttgllvm(tg).alloclist)
@@ -1661,6 +1674,8 @@ implementation
     var
       asmsym: TAsmSymbol;
     begin
+      if po_external in procdef.procoptions then
+        exit;
       asmsym:=current_asmdata.RefAsmSymbol(externalname,AT_FUNCTION);
       list.concat(taillvmalias.create(asmsym,procdef.mangledname,procdef,llv_default,lll_default));
     end;

+ 2 - 1
compiler/llvm/llvmnode.pas

@@ -39,6 +39,7 @@ implementation
     tgllvm,hlcgllvm,
     nllvmadd,nllvmcal,nllvmcnv,nllvmcon,nllvminl,nllvmld,nllvmmat,nllvmmem,
     nllvmtcon,nllvmutil,
-    llvmpara;
+    llvmpara,
+    symllvm;
 
 end.

+ 50 - 8
compiler/llvm/llvmtype.pas

@@ -54,7 +54,7 @@ interface
           generated, as these alias declarations can appear anywhere }
         asmsymtypes: THashSet;
 
-        procedure record_asmsym_def(sym: TAsmSymbol; def: tdef);
+        procedure record_asmsym_def(sym: TAsmSymbol; def: tdef; redefine: boolean);
         function  get_asmsym_def(sym: TAsmSymbol): tdef;
 
         function record_def(def:tdef): tdef;
@@ -86,6 +86,7 @@ interface
         procedure insert_typedconst_typeconversion(p: tai_abstracttypedconst);
         procedure insert_tai_typeconversions(p: tai);
         procedure insert_asmlist_typeconversions(list: tasmlist);
+        procedure update_asmlist_alias_types(list: tasmlist);
 
       public
         constructor Create;override;
@@ -108,15 +109,17 @@ implementation
                               TDebugInfoDwarf
 ****************************************************************************}
 
-    procedure TLLVMTypeInfo.record_asmsym_def(sym: TAsmSymbol; def: tdef);
+    procedure TLLVMTypeInfo.record_asmsym_def(sym: TAsmSymbol; def: tdef; redefine: boolean);
       var
         res: PHashSetItem;
       begin
         res:=asmsymtypes.FindOrAdd(@sym,sizeof(sym));
-        { if there are multiple definitions of the same symbol, we're in
-          trouble anyway, so don't bother checking whether data is already
-          assigned }
-        res^.Data:=def;
+        { due to internal aliases with different signatures, we may end up with
+          multiple defs for the same symbol -> use the one from the declaration,
+          and insert typecasts as necessary elsewhere }
+        if redefine or
+           not assigned(res^.Data) then
+          res^.Data:=def;
       end;
 
 
@@ -193,12 +196,12 @@ implementation
           ait_llvmalias:
             begin
               appenddef(deftypelist,taillvmalias(p).def);
-              record_asmsym_def(taillvmalias(p).newsym,taillvmalias(p).def);
+              record_asmsym_def(taillvmalias(p).newsym,taillvmalias(p).def,false);
             end;
           ait_llvmdecl:
             begin
               appenddef(deftypelist,taillvmdecl(p).def);
-              record_asmsym_def(taillvmdecl(p).namesym,taillvmdecl(p).def);
+              record_asmsym_def(taillvmdecl(p).namesym,taillvmdecl(p).def,true);
               collect_asmlist_info(deftypelist,taillvmdecl(p).initdata);
             end;
           ait_llvmins:
@@ -365,6 +368,38 @@ implementation
           end;
       end;
 
+    procedure TLLVMTypeInfo.update_asmlist_alias_types(list: tasmlist);
+      var
+        hp: tai;
+        def: tdef;
+      begin
+        if not assigned(list) then
+          exit;
+        hp:=tai(list.first);
+        while assigned(hp) do
+          begin
+            case hp.typ of
+              ait_llvmalias:
+                begin
+                  { replace the def of the alias declaration with the def of
+                    the aliased symbol -> we'll insert the appropriate type
+                    conversions for all uses of this symbol in the code (since
+                    every use also specifies the used type) }
+                  record_asmsym_def(taillvmalias(hp).oldsym,taillvmalias(hp).def,false);
+                  def:=get_asmsym_def(taillvmalias(hp).oldsym);
+                  if taillvmalias(hp).def<>def then
+                    begin
+                      taillvmalias(hp).def:=def;
+                      record_asmsym_def(taillvmalias(hp).newsym,def,true);
+                    end;
+                end;
+              ait_llvmdecl:
+                update_asmlist_alias_types(taillvmdecl(hp).initdata);
+            end;
+            hp:=tai(hp.next);
+          end;
+      end;
+
 
     procedure TLLVMTypeInfo.appenddef_array(list:TAsmList;def:tarraydef);
       begin
@@ -536,6 +571,13 @@ implementation
           if hal<>al_start then
             collect_asmlist_info(current_asmdata.asmlists[al_start],current_asmdata.asmlists[hal]);
 
+        { update the defs of all alias declarations so they match those of the
+          declarations of the symbols they alias }
+        for hal:=low(TasmlistType) to high(TasmlistType) do
+          if hal<>al_start then
+            update_asmlist_alias_types(current_asmdata.asmlists[hal]);
+
+        { and insert the necessary type conversions }
         for hal:=low(TasmlistType) to high(TasmlistType) do
           if hal<>al_start then
             insert_asmlist_typeconversions(current_asmdata.asmlists[hal]);

+ 223 - 0
compiler/llvm/symllvm.pas

@@ -0,0 +1,223 @@
+{
+    Copyright (c) 2014 by Florian Klaempfl
+
+    Symbol table overrides for LLVM
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ ****************************************************************************
+}
+unit symllvm;
+
+{$i fpcdefs.inc}
+
+interface
+
+uses
+  globtype,
+  symcpu;
+
+type
+  { defs }
+  tllvmfiledef = class(tcpufiledef)
+  end;
+
+  tllvmvariantdef = class(tcpuvariantdef)
+  end;
+
+  tllvmformaldef = class(tcpuformaldef)
+  end;
+
+  tllvmforwarddef = class(tcpuforwarddef)
+  end;
+
+  tllvmundefineddef = class(tcpuundefineddef)
+  end;
+
+  tllvmerrordef = class(tcpuerrordef)
+  end;
+
+  tllvmpointerdef = class(tcpupointerdef)
+  end;
+
+  tllvmrecorddef = class(tcpurecorddef)
+  end;
+
+  tllvmimplementedinterface = class(tcpuimplementedinterface)
+  end;
+
+  tllvmobjectdef = class(tcpuobjectdef)
+  end;
+
+  tllvmclassrefdef = class(tcpuclassrefdef)
+  end;
+
+  tllvmarraydef = class(tcpuarraydef)
+  end;
+
+  tllvmorddef = class(tcpuorddef)
+  end;
+
+  tllvmfloatdef = class(tcpufloatdef)
+  end;
+
+  tllvmprocvardef = class(tcpuprocvardef)
+  end;
+
+  { tllvmprocdef }
+
+  tllvmprocdef = class(tcpuprocdef)
+   protected
+    external_decl_mangled_name: TSymStr;
+   public
+    { overried the mangled name of external functions }
+    function mangledname: TSymStr; override;
+    { provide access to the original mangled name of external functions }
+    function external_mangledname: TSymStr;
+  end;
+
+  tllvmstringdef = class(tcpustringdef)
+  end;
+
+  tllvmenumdef = class(tcpuenumdef)
+  end;
+
+  tllvmsetdef = class(tcpusetdef)
+  end;
+
+  { syms }
+  tllvmlabelsym = class(tcpulabelsym)
+  end;
+
+  tllvmunitsym = class(tcpuunitsym)
+  end;
+
+  tllvmprogramparasym = class(tcpuprogramparasym)
+  end;
+
+  tllvmnamespacesym = class(tcpunamespacesym)
+  end;
+
+  tllvmprocsym = class(tcpuprocsym)
+  end;
+
+  tllvmtypesym = class(tcputypesym)
+  end;
+
+  tllvmfieldvarsym = class(tcpufieldvarsym)
+  end;
+
+  tllvmlocalvarsym = class(tcpulocalvarsym)
+  end;
+
+  tllvmparavarsym = class(tcpuparavarsym)
+  end;
+
+  tllvmstaticvarsym = class(tcpustaticvarsym)
+  end;
+
+  tllvmabsolutevarsym = class(tcpuabsolutevarsym)
+  end;
+
+  tllvmpropertysym = class(tcpupropertysym)
+  end;
+
+  tllvmconstsym = class(tcpuconstsym)
+  end;
+
+  tllvmenumsym = class(tcpuenumsym)
+  end;
+
+  tllvmsyssym = class(tcpusyssym)
+  end;
+
+
+implementation
+
+uses
+  symconst,symdef,symsym;
+
+{ tllvmprocdef }
+
+function tllvmprocdef.mangledname: TSymStr;
+  begin
+    { External declarations are handled separately for the LLVM target and need
+      a different mangled name than on other targets, because we have to
+      create a declaration based on the declared name that redirects to the
+      external name in order to deal with potential different signatures between
+      the external declaration the symbol that's referred.
+
+      There's no need to store the external_decl_mangled_name in the ppu file,
+      since it can always be regenerated on the fly. }
+    if not(po_external in procoptions) then
+      result:=inherited
+    else
+      begin
+        if external_decl_mangled_name='' then
+          begin
+            result:=defaultmangledname;
+            external_decl_mangled_name:=result;
+          end
+        else
+          result:=external_decl_mangled_name;
+      end;
+  end;
+
+
+function tllvmprocdef.external_mangledname: TSymStr;
+  begin
+    result:=inherited mangledname;
+  end;
+
+begin
+  { used tdef classes }
+  cfiledef:=tllvmfiledef;
+  cvariantdef:=tllvmvariantdef;
+  cformaldef:=tllvmformaldef;
+  cforwarddef:=tllvmforwarddef;
+  cundefineddef:=tllvmundefineddef;
+  cerrordef:=tllvmerrordef;
+  cpointerdef:=tllvmpointerdef;
+  crecorddef:=tllvmrecorddef;
+  cimplementedinterface:=tllvmimplementedinterface;
+  cobjectdef:=tllvmobjectdef;
+  cclassrefdef:=tllvmclassrefdef;
+  carraydef:=tllvmarraydef;
+  corddef:=tllvmorddef;
+  cfloatdef:=tllvmfloatdef;
+  cprocvardef:=tllvmprocvardef;
+  cprocdef:=tllvmprocdef;
+  cstringdef:=tllvmstringdef;
+  cenumdef:=tllvmenumdef;
+  csetdef:=tllvmsetdef;
+
+  { used tsym classes }
+  clabelsym:=tllvmlabelsym;
+  cunitsym:=tllvmunitsym;
+  cprogramparasym:=tllvmprogramparasym;
+  cnamespacesym:=tllvmnamespacesym;
+  cprocsym:=tllvmprocsym;
+  ctypesym:=tllvmtypesym;
+  cfieldvarsym:=tllvmfieldvarsym;
+  clocalvarsym:=tllvmlocalvarsym;
+  cparavarsym:=tllvmparavarsym;
+  cstaticvarsym:=tllvmstaticvarsym;
+  cabsolutevarsym:=tllvmabsolutevarsym;
+  cpropertysym:=tllvmpropertysym;
+  cconstsym:=tllvmconstsym;
+  cenumsym:=tllvmenumsym;
+  csyssym:=tllvmsyssym;
+end.
+