nllvmutil.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. class procedure InsertAsanGlobals;
  31. public
  32. class procedure InsertObjectInfo; override;
  33. class procedure RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean); 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,finput,versioncmp,
  40. aasmtai,cpubase,llvmbase,aasmllvm,
  41. aasmcnst,nllvmtcon,
  42. symbase,symtable,defutil,
  43. llvminfo,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,sym.vardef,nil,sec_data,varalign))
  58. else if tf_section_threadvars in target_info.flags then
  59. list.concat(taillvmdecl.createtls(asmsym,sym,sym.vardef,varalign))
  60. else
  61. list.concat(taillvmdecl.createdef(asmsym,sym,
  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;
  84. const usedsymsname: TSymStr);
  85. var
  86. useddef: tdef;
  87. tcb: ttai_typedconstbuilder;
  88. prevasmsym: TAsmSymbol;
  89. typedsym: TTypedAsmSym;
  90. uniquesyms, i: longint;
  91. begin
  92. if usedsyms.count<>0 then
  93. begin
  94. { a symbol can appear multiple times -> sort the list so we can filter out doubles }
  95. usedsyms.Sort(@TypedAsmSymComparer);
  96. { count uniques }
  97. prevasmsym:=nil;
  98. uniquesyms:=0;
  99. for i:=0 to usedsyms.count-1 do
  100. begin
  101. typedsym:=TTypedAsmSym(usedsyms[i]);
  102. if (prevasmsym<>typedsym.sym) and
  103. { even though we already filter on pure assembler routines when adding the symbols,
  104. some may slip through because of forward definitions that are not yet resolved }
  105. not((typedsym.def.typ=procdef) and
  106. (po_assembler in tprocdef(typedsym.def).procoptions)) then
  107. inc(uniquesyms);
  108. prevasmsym:=typedsym.sym;
  109. end;
  110. { emit uniques }
  111. prevasmsym:=nil;
  112. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  113. tllvmtai_typedconstbuilder(tcb).appendingdef:=true;
  114. useddef:=carraydef.getreusable(voidpointertype,uniquesyms);
  115. tcb.maybe_begin_aggregate(useddef);
  116. for i:=0 to usedsyms.count-1 do
  117. begin
  118. typedsym:=TTypedAsmSym(usedsyms[i]);
  119. if (prevasmsym<>typedsym.sym) and
  120. not((typedsym.def.typ=procdef) and
  121. (po_assembler in tprocdef(typedsym.def).procoptions)) then
  122. begin
  123. tcb.queue_init(voidpointertype);
  124. tcb.queue_emit_asmsym(typedsym.sym,typedsym.def);
  125. prevasmsym:=typedsym.sym;
  126. end;
  127. end;
  128. tcb.maybe_end_aggregate(useddef);
  129. current_asmdata.AsmLists[al_globals].concatlist(
  130. tcb.get_final_asmlist(
  131. current_asmdata.DefineAsmSymbol(
  132. usedsymsname,AB_GLOBAL,AT_DATA,useddef),useddef,sec_user,
  133. 'llvm.metadata',0
  134. )
  135. );
  136. tcb.free;
  137. end;
  138. usedsyms.free;
  139. usedsyms:=nil;
  140. end;
  141. class procedure tllvmnodeutils.InsertInitFiniList(var procdefs: tfplist; const initfinisymsname: TSymStr);
  142. var
  143. itemdef: trecorddef;
  144. arraydef: tarraydef;
  145. pd: tprocdef;
  146. fields: array[0..2] of tdef;
  147. tcb: ttai_typedconstbuilder;
  148. i: longint;
  149. begin
  150. if procdefs.count<>0 then
  151. begin
  152. pd:=tprocdef(procdefs[0]);
  153. fields[0]:=s32inttype;
  154. fields[1]:=cprocvardef.getreusableprocaddr(pd,pc_address_only);
  155. fields[2]:=voidpointertype;
  156. itemdef:=llvmgettemprecorddef(fields,C_alignment,
  157. targetinfos[target_info.system]^.alignment.recordalignmin);
  158. include(itemdef.defoptions,df_llvm_no_struct_packing);
  159. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  160. tllvmtai_typedconstbuilder(tcb).appendingdef:=true;
  161. arraydef:=carraydef.getreusable(itemdef,procdefs.Count);
  162. tcb.maybe_begin_aggregate(arraydef);
  163. for i:=0 to procdefs.count-1 do
  164. begin
  165. tcb.maybe_begin_aggregate(itemdef);
  166. tcb.emit_ord_const(65535,s32inttype);
  167. tcb.emit_procdef_const(tprocdef(procdefs[i]));
  168. tcb.emit_tai(Tai_const.Create_sym(nil),voidpointertype);
  169. tcb.maybe_end_aggregate(itemdef);
  170. end;
  171. tcb.maybe_end_aggregate(arraydef);
  172. current_asmdata.AsmLists[al_globals].concatlist(
  173. tcb.get_final_asmlist(
  174. current_asmdata.DefineAsmSymbol(
  175. initfinisymsname,AB_GLOBAL,AT_DATA,arraydef),arraydef,sec_data,
  176. initfinisymsname,voidpointertype.alignment
  177. )
  178. );
  179. tcb.free;
  180. end;
  181. end;
  182. class procedure tllvmnodeutils.InsertAsanGlobals;
  183. var
  184. asanglobal,
  185. asanglobals,
  186. globalfileloc: tai_llvmbasemetadatanode;
  187. hp: tai;
  188. hpdecl: taillvmdecl;
  189. sourcefile: tinputfile;
  190. module: tmodule;
  191. list: TAsmList;
  192. asmlisttype: TAsmListType;
  193. begin
  194. if not(cs_sanitize_address in current_settings.moduleswitches) or
  195. (llvmflag_sanitizer_attributes in llvmversion_properties[current_settings.llvmversion]) then
  196. exit;
  197. asanglobals:=nil;
  198. module:=get_module(current_filepos.moduleindex);
  199. for asmlisttype:=low(asmlisttype) to high(asmlisttype) do
  200. begin
  201. list:=current_asmdata.AsmLists[asmlisttype];
  202. if not assigned(list) then
  203. continue;
  204. hp:=tai(list.first);
  205. while assigned(hp) do
  206. begin
  207. if (hp.typ=ait_llvmdecl) and
  208. (ldf_definition in taillvmdecl(hp).flags) and
  209. (taillvmdecl(hp).def.typ<>procdef) then
  210. begin
  211. if not assigned(asanglobals) then
  212. begin
  213. asanglobals:=tai_llvmnamedmetadatanode.create('llvm.asan.globals');
  214. current_asmdata.AsmLists[al_rotypedconsts].concat(asanglobals);
  215. end;
  216. hpdecl:=taillvmdecl(hp);
  217. globalfileloc:=tai_llvmunnamedmetadatanode.create;
  218. current_asmdata.AsmLists[al_rotypedconsts].concat(globalfileloc);
  219. if assigned(hpdecl.sym) then
  220. begin
  221. sourcefile:=get_source_file(hpdecl.sym.fileinfo.moduleindex,hpdecl.sym.fileinfo.fileindex);
  222. globalfileloc.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(sourcefile.name)));
  223. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(hpdecl.sym.fileinfo.line)));
  224. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(hpdecl.sym.fileinfo.column)));
  225. end
  226. else
  227. begin
  228. sourcefile:=current_module.sourcefiles.get_file(1);
  229. globalfileloc.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(sourcefile.name)));
  230. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  231. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  232. end;
  233. asanglobal:=tai_llvmunnamedmetadatanode.create;
  234. current_asmdata.AsmLists[al_rotypedconsts].concat(asanglobal);
  235. asanglobal.addvalue(tai_simpletypedconst.create(cpointerdef.getreusable(hpdecl.def),tai_const.Create_sym(hpdecl.namesym)));
  236. asanglobal.addvalue(tai_simpletypedconst.create(llvm_metadatatype,llvm_getmetadatareftypedconst(globalfileloc)));
  237. if assigned(hpdecl.sym) then
  238. asanglobal.addvalue(tai_simpletypedconst.create(llvm_metadatatype,tai_string.Create(hpdecl.sym.RealName)))
  239. else
  240. asanglobal.addvalue(tai_simpletypedconst.create(llvm_metadatatype,tai_string.Create(hpdecl.namesym.Name)));
  241. { dynamic init }
  242. asanglobal.addvalue(tai_simpletypedconst.create(llvmbool1type,tai_const.Create_8bit(ord(false))));
  243. { no asan }
  244. asanglobal.addvalue(tai_simpletypedconst.create(llvmbool1type,tai_const.Create_8bit(ord((ldf_vectorized in taillvmdecl(hp).flags)))));
  245. asanglobals.addvalue(tai_simpletypedconst.create(llvm_metadatatype,llvm_getmetadatareftypedconst(asanglobal)));
  246. end;
  247. hp:=tai(hp.next);
  248. end;
  249. end;
  250. end;
  251. class procedure tllvmnodeutils.InsertObjectInfo;
  252. var
  253. llvmmoduleflags,
  254. objcmoduleflag,
  255. dwarfversionflag: tai_llvmbasemetadatanode;
  256. objcabiversion: longint;
  257. begin
  258. InsertAsanGlobals;
  259. llvmmoduleflags:=tai_llvmnamedmetadatanode.create('llvm.module.flags');
  260. current_asmdata.AsmLists[al_rotypedconsts].Concat(llvmmoduleflags);
  261. if (m_objectivec1 in current_settings.modeswitches) then
  262. begin
  263. { Objective-C ABI version }
  264. if not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin,system_i386_darwin,system_x86_64_darwin]) or
  265. (MacOSXVersionMin.relationto(10,5,0)>=0) then
  266. objcabiversion:=2
  267. else
  268. objcabiversion:=1;
  269. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  270. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  271. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Version')));
  272. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(objcabiversion)));
  273. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  274. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  275. { image info version }
  276. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  277. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  278. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Version')));
  279. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  280. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  281. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  282. { image info section }
  283. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  284. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  285. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Section')));
  286. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(objc_section_name(sec_objc_image_info))));
  287. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  288. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  289. { garbage collection }
  290. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  291. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  292. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Garbage Collection')));
  293. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  294. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  295. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  296. { insert newly created defs in the implementation rather than interface symtable
  297. (the interface symtable is sealed at this point) }
  298. end;
  299. { debug information }
  300. if (([cs_debuginfo,cs_lineinfo]*current_settings.moduleswitches)<>[]) and
  301. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) then
  302. begin
  303. { the debug info version is the version of the debug info metadata
  304. format }
  305. dwarfversionflag:=tai_llvmunnamedmetadatanode.create;
  306. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2)));
  307. dwarfversionflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Debug Info Version')));
  308. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(llvm_debuginfo_metadata_format[current_settings.llvmversion])));
  309. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(dwarfversionflag));
  310. current_asmdata.AsmLists[al_rotypedconsts].Concat(dwarfversionflag);
  311. { dwarf version }
  312. dwarfversionflag:=tai_llvmunnamedmetadatanode.create;
  313. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2)));
  314. dwarfversionflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Dwarf Version')));
  315. case target_dbg.id of
  316. dbg_dwarf2:
  317. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2)));
  318. dbg_dwarf3:
  319. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(3)));
  320. dbg_dwarf4:
  321. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(4)));
  322. else
  323. internalerror(2022022012);
  324. end;
  325. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(dwarfversionflag));
  326. current_asmdata.AsmLists[al_rotypedconsts].Concat(dwarfversionflag);
  327. end;
  328. symtablestack.push(current_module.localsymtable);
  329. { add the llvm.compiler.used array }
  330. InsertUsedList(current_module.llvmcompilerusedsyms,'llvm.compiler.used');
  331. { add the llvm.used array }
  332. InsertUsedList(current_module.llvmusedsyms,'llvm.used');
  333. { add the llvm.global_ctors array }
  334. InsertInitFiniList(current_module.llvminitprocs,'llvm.global_ctors');
  335. { add the llvm.global_dtors array }
  336. InsertInitFiniList(current_module.llvmfiniprocs,'llvm.global_dtors');
  337. { add "type xx = .." statements for all used recorddefs }
  338. with TLLVMTypeInfo.Create do
  339. begin
  340. inserttypeinfo;
  341. free;
  342. end;
  343. symtablestack.pop(current_module.localsymtable);
  344. end;
  345. class procedure tllvmnodeutils.RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean);
  346. var
  347. last: TTypedAsmSym;
  348. begin
  349. if compileronly then
  350. begin
  351. { filter multiple adds in succession here already }
  352. last:=TTypedAsmSym(current_module.llvmcompilerusedsyms.Last);
  353. if not assigned(last) or
  354. (last.sym<>sym) then
  355. current_module.llvmcompilerusedsyms.Add(TTypedAsmSym.Create(sym,def))
  356. end
  357. else
  358. begin
  359. last:=TTypedAsmSym(current_module.llvmusedsyms.Last);
  360. if not assigned(last) or
  361. (last.sym<>sym) then
  362. current_module.llvmusedsyms.Add(TTypedAsmSym.Create(sym,def))
  363. end;
  364. end;
  365. class procedure tllvmnodeutils.RegisterModuleInitFunction(pd: tprocdef);
  366. begin
  367. current_module.llvminitprocs.add(pd);
  368. end;
  369. class procedure tllvmnodeutils.RegisterModuleFiniFunction(pd: tprocdef);
  370. begin
  371. current_module.llvmfiniprocs.add(pd);
  372. end;
  373. begin
  374. cnodeutils:=tllvmnodeutils;
  375. end.