nllvmutil.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. {
  2. Copyright (c) 20011 by Jonas Maebe
  3. LLVM version of some node tree helper routines
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nllvmutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,
  22. aasmbase,aasmdata,ngenutil,
  23. symtype,symconst,symsym,symdef;
  24. type
  25. tllvmnodeutils = class(tnodeutils)
  26. strict protected
  27. class procedure insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint); override;
  28. class procedure InsertUsedList(var usedsyms: tfpobjectlist; const usedsymsname: TSymstr);
  29. public
  30. class procedure InsertObjectInfo; override;
  31. class procedure RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean); override;
  32. end;
  33. implementation
  34. uses
  35. verbose,cutils,globals,fmodule,systems,
  36. aasmtai,cpubase,llvmbase,aasmllvm,
  37. aasmcnst,nllvmtcon,
  38. symbase,symtable,defutil,
  39. llvmtype;
  40. class procedure tllvmnodeutils.insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint);
  41. var
  42. asmsym: tasmsymbol;
  43. field1, field2: tsym;
  44. tcb: ttai_typedconstbuilder;
  45. begin
  46. if sym.globalasmsym then
  47. asmsym:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_GLOBAL,AT_DATA,sym.vardef)
  48. else
  49. asmsym:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_LOCAL,AT_DATA,sym.vardef);
  50. if not(vo_is_thread_var in sym.varoptions) then
  51. list.concat(taillvmdecl.createdef(asmsym,sym.vardef,nil,sec_data,varalign))
  52. else if tf_section_threadvars in target_info.flags then
  53. list.concat(taillvmdecl.createtls(asmsym,sym.vardef,varalign))
  54. else
  55. list.concat(taillvmdecl.createdef(asmsym,
  56. get_threadvar_record(sym.vardef,field1,field2),
  57. nil,sec_data,varalign));
  58. end;
  59. type
  60. TTypedAsmSym = class
  61. sym: TAsmSymbol;
  62. def: tdef;
  63. constructor Create(s: TAsmSymbol; d: tdef);
  64. end;
  65. constructor TTypedAsmSym.Create(s: TAsmSymbol; d: tdef);
  66. begin
  67. sym:=s;
  68. def:=d;
  69. end;
  70. function TypedAsmSymComparer(p1, p2: Pointer): Integer;
  71. var
  72. sym1: TTypedAsmSym absolute p1;
  73. sym2: TTypedAsmSym absolute p2;
  74. begin
  75. result:=CompareStr(sym1.sym.Name,sym2.sym.Name);
  76. end;
  77. class procedure tllvmnodeutils.InsertUsedList(var usedsyms: tfpobjectlist; const usedsymsname: TSymstr);
  78. var
  79. useddef: tdef;
  80. tcb: ttai_typedconstbuilder;
  81. prevasmsym: TAsmSymbol;
  82. typedsym: TTypedAsmSym;
  83. uniquesyms, i: longint;
  84. begin
  85. if usedsyms.count<>0 then
  86. begin
  87. { a symbol can appear multiple times -> sort the list so we can filter out doubles }
  88. usedsyms.Sort(@TypedAsmSymComparer);
  89. { count uniques }
  90. prevasmsym:=nil;
  91. uniquesyms:=0;
  92. for i:=0 to usedsyms.count-1 do
  93. begin
  94. typedsym:=TTypedAsmSym(usedsyms[i]);
  95. if (prevasmsym<>typedsym.sym) and
  96. { even though we already filter on pure assembler routines when adding the symbols,
  97. some may slip through because of forward definitions that are not yet resolved }
  98. not((typedsym.def.typ=procdef) and
  99. (po_assembler in tprocdef(typedsym.def).procoptions)) then
  100. inc(uniquesyms);
  101. prevasmsym:=typedsym.sym;
  102. end;
  103. { emit uniques }
  104. prevasmsym:=nil;
  105. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  106. tllvmtai_typedconstbuilder(tcb).appendingdef:=true;
  107. useddef:=carraydef.getreusable(voidpointertype,uniquesyms);
  108. tcb.maybe_begin_aggregate(useddef);
  109. for i:=0 to usedsyms.count-1 do
  110. begin
  111. typedsym:=TTypedAsmSym(usedsyms[i]);
  112. if (prevasmsym<>typedsym.sym) and
  113. not((typedsym.def.typ=procdef) and
  114. (po_assembler in tprocdef(typedsym.def).procoptions)) then
  115. begin
  116. tcb.queue_init(voidpointertype);
  117. tcb.queue_emit_asmsym(typedsym.sym,typedsym.def);
  118. prevasmsym:=typedsym.sym;
  119. end;
  120. end;
  121. tcb.maybe_end_aggregate(useddef);
  122. current_asmdata.AsmLists[al_globals].concatlist(
  123. tcb.get_final_asmlist(
  124. current_asmdata.DefineAsmSymbol(
  125. usedsymsname,AB_GLOBAL,AT_DATA,useddef),useddef,sec_user,
  126. 'llvm.metadata',0
  127. )
  128. );
  129. tcb.free;
  130. end;
  131. usedsyms.free;
  132. usedsyms:=nil;
  133. end;
  134. class procedure tllvmnodeutils.InsertObjectInfo;
  135. begin
  136. inherited;
  137. { add the llvm.compiler.used array }
  138. InsertUsedList(current_module.llvmcompilerusedsyms,'llvm.compiler.used');
  139. { add the llvm.used array }
  140. InsertUsedList(current_module.llvmusedsyms,'llvm.used');
  141. { add "type xx = .." statements for all used recorddefs }
  142. with TLLVMTypeInfo.Create do
  143. begin
  144. inserttypeinfo;
  145. free;
  146. end;
  147. end;
  148. class procedure tllvmnodeutils.RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean);
  149. var
  150. last: TTypedAsmSym;
  151. begin
  152. if compileronly then
  153. begin
  154. { filter multiple adds in succession here already }
  155. last:=TTypedAsmSym(current_module.llvmcompilerusedsyms.Last);
  156. if not assigned(last) or
  157. (last.sym<>sym) then
  158. current_module.llvmcompilerusedsyms.Add(TTypedAsmSym.Create(sym,def))
  159. end
  160. else
  161. begin
  162. last:=TTypedAsmSym(current_module.llvmusedsyms.Last);
  163. if not assigned(last) or
  164. (last.sym<>sym) then
  165. current_module.llvmusedsyms.Add(TTypedAsmSym.Create(sym,def))
  166. end;
  167. end;
  168. begin
  169. cnodeutils:=tllvmnodeutils;
  170. end.