nllvmutil.pas 12 KB

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