nllvmutil.pas 14 KB

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