nllvmutil.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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,aasmllvmmetadata, 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. class procedure GenerateObjCImageInfo; override;
  33. end;
  34. implementation
  35. uses
  36. verbose,cutils,globals,fmodule,systems,
  37. aasmtai,cpubase,llvmbase,aasmllvm,
  38. aasmcnst,nllvmtcon,
  39. symbase,symtable,defutil,
  40. llvmtype,
  41. objcasm;
  42. class procedure tllvmnodeutils.insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint);
  43. var
  44. asmsym: tasmsymbol;
  45. field1, field2: tsym;
  46. begin
  47. if sym.globalasmsym then
  48. asmsym:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_GLOBAL,AT_DATA,sym.vardef)
  49. else
  50. asmsym:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_LOCAL,AT_DATA,sym.vardef);
  51. if not(vo_is_thread_var in sym.varoptions) then
  52. list.concat(taillvmdecl.createdef(asmsym,sym.vardef,nil,sec_data,varalign))
  53. else if tf_section_threadvars in target_info.flags then
  54. list.concat(taillvmdecl.createtls(asmsym,sym.vardef,varalign))
  55. else
  56. list.concat(taillvmdecl.createdef(asmsym,
  57. get_threadvar_record(sym.vardef,field1,field2),
  58. nil,sec_data,varalign));
  59. end;
  60. type
  61. TTypedAsmSym = class
  62. sym: TAsmSymbol;
  63. def: tdef;
  64. constructor Create(s: TAsmSymbol; d: tdef);
  65. end;
  66. constructor TTypedAsmSym.Create(s: TAsmSymbol; d: tdef);
  67. begin
  68. sym:=s;
  69. def:=d;
  70. end;
  71. function TypedAsmSymComparer(p1, p2: Pointer): Integer;
  72. var
  73. sym1: TTypedAsmSym absolute p1;
  74. sym2: TTypedAsmSym absolute p2;
  75. begin
  76. result:=CompareStr(sym1.sym.Name,sym2.sym.Name);
  77. end;
  78. class procedure tllvmnodeutils.InsertUsedList(var usedsyms: tfpobjectlist; const usedsymsname: TSymstr);
  79. var
  80. useddef: tdef;
  81. tcb: ttai_typedconstbuilder;
  82. prevasmsym: TAsmSymbol;
  83. typedsym: TTypedAsmSym;
  84. uniquesyms, i: longint;
  85. begin
  86. if usedsyms.count<>0 then
  87. begin
  88. { a symbol can appear multiple times -> sort the list so we can filter out doubles }
  89. usedsyms.Sort(@TypedAsmSymComparer);
  90. { count uniques }
  91. prevasmsym:=nil;
  92. uniquesyms:=0;
  93. for i:=0 to usedsyms.count-1 do
  94. begin
  95. typedsym:=TTypedAsmSym(usedsyms[i]);
  96. if (prevasmsym<>typedsym.sym) and
  97. { even though we already filter on pure assembler routines when adding the symbols,
  98. some may slip through because of forward definitions that are not yet resolved }
  99. not((typedsym.def.typ=procdef) and
  100. (po_assembler in tprocdef(typedsym.def).procoptions)) then
  101. inc(uniquesyms);
  102. prevasmsym:=typedsym.sym;
  103. end;
  104. { emit uniques }
  105. prevasmsym:=nil;
  106. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  107. tllvmtai_typedconstbuilder(tcb).appendingdef:=true;
  108. useddef:=carraydef.getreusable(voidpointertype,uniquesyms);
  109. tcb.maybe_begin_aggregate(useddef);
  110. for i:=0 to usedsyms.count-1 do
  111. begin
  112. typedsym:=TTypedAsmSym(usedsyms[i]);
  113. if (prevasmsym<>typedsym.sym) and
  114. not((typedsym.def.typ=procdef) and
  115. (po_assembler in tprocdef(typedsym.def).procoptions)) then
  116. begin
  117. tcb.queue_init(voidpointertype);
  118. tcb.queue_emit_asmsym(typedsym.sym,typedsym.def);
  119. prevasmsym:=typedsym.sym;
  120. end;
  121. end;
  122. tcb.maybe_end_aggregate(useddef);
  123. current_asmdata.AsmLists[al_globals].concatlist(
  124. tcb.get_final_asmlist(
  125. current_asmdata.DefineAsmSymbol(
  126. usedsymsname,AB_GLOBAL,AT_DATA,useddef),useddef,sec_user,
  127. 'llvm.metadata',0
  128. )
  129. );
  130. tcb.free;
  131. end;
  132. usedsyms.free;
  133. usedsyms:=nil;
  134. end;
  135. class procedure tllvmnodeutils.InsertObjectInfo;
  136. begin
  137. inherited;
  138. { add the llvm.compiler.used array }
  139. InsertUsedList(current_module.llvmcompilerusedsyms,'llvm.compiler.used');
  140. { add the llvm.used array }
  141. InsertUsedList(current_module.llvmusedsyms,'llvm.used');
  142. { add "type xx = .." statements for all used recorddefs }
  143. with TLLVMTypeInfo.Create do
  144. begin
  145. inserttypeinfo;
  146. free;
  147. end;
  148. end;
  149. class procedure tllvmnodeutils.RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean);
  150. var
  151. last: TTypedAsmSym;
  152. begin
  153. if compileronly then
  154. begin
  155. { filter multiple adds in succession here already }
  156. last:=TTypedAsmSym(current_module.llvmcompilerusedsyms.Last);
  157. if not assigned(last) or
  158. (last.sym<>sym) then
  159. current_module.llvmcompilerusedsyms.Add(TTypedAsmSym.Create(sym,def))
  160. end
  161. else
  162. begin
  163. last:=TTypedAsmSym(current_module.llvmusedsyms.Last);
  164. if not assigned(last) or
  165. (last.sym<>sym) then
  166. current_module.llvmusedsyms.Add(TTypedAsmSym.Create(sym,def))
  167. end;
  168. end;
  169. class procedure tllvmnodeutils.GenerateObjCImageInfo;
  170. var
  171. llvmmoduleflags,
  172. objcmoduleflag: tai_llvmbasemetadatanode;
  173. objcabiversion: longint;
  174. begin
  175. llvmmoduleflags:=tai_llvmnamedmetadatanode.create('llvm.module.flags');
  176. current_asmdata.AsmLists[al_rotypedconsts].Concat(llvmmoduleflags);
  177. { Objective-C ABI version }
  178. if not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin,system_i386_darwin,system_x86_64_darwin]) or
  179. (CompareVersionStrings(MacOSXVersionMin,'10.5')>=0) then
  180. objcabiversion:=2
  181. else
  182. objcabiversion:=1;
  183. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  184. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  185. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Version')));
  186. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(objcabiversion)));
  187. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  188. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  189. { image info version }
  190. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  191. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  192. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Version')));
  193. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  194. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  195. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  196. { image info section }
  197. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  198. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  199. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Section')));
  200. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(objc_section_name(sec_objc_image_info))));
  201. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  202. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  203. { garbage collection }
  204. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  205. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  206. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Garbage Collection')));
  207. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  208. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  209. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  210. end;
  211. begin
  212. cnodeutils:=tllvmnodeutils;
  213. end.