2
0

nllvmutil.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. include(itemdef.defoptions,df_llvm_no_typename);
  160. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  161. tllvmtai_typedconstbuilder(tcb).appendingdef:=true;
  162. arraydef:=carraydef.getreusable(itemdef,procdefs.Count);
  163. tcb.maybe_begin_aggregate(arraydef);
  164. for i:=0 to procdefs.count-1 do
  165. begin
  166. tcb.maybe_begin_aggregate(itemdef);
  167. tcb.emit_ord_const(65535,s32inttype);
  168. tcb.emit_procdef_const(tprocdef(procdefs[i]));
  169. tcb.emit_tai(Tai_const.Create_sym(nil),voidpointertype);
  170. tcb.maybe_end_aggregate(itemdef);
  171. end;
  172. tcb.maybe_end_aggregate(arraydef);
  173. current_asmdata.AsmLists[al_globals].concatlist(
  174. tcb.get_final_asmlist(
  175. current_asmdata.DefineAsmSymbol(
  176. initfinisymsname,AB_GLOBAL,AT_DATA,arraydef),arraydef,sec_data,
  177. initfinisymsname,voidpointertype.alignment
  178. )
  179. );
  180. tcb.free;
  181. end;
  182. end;
  183. class procedure tllvmnodeutils.InsertAsanGlobals;
  184. var
  185. asanglobal,
  186. asanglobals,
  187. globalfileloc: tai_llvmbasemetadatanode;
  188. hp: tai;
  189. hpdecl: taillvmdecl;
  190. sourcefile: tinputfile;
  191. module: tmodule;
  192. list: TAsmList;
  193. asmlisttype: TAsmListType;
  194. begin
  195. if not(cs_sanitize_address in current_settings.moduleswitches) or
  196. (llvmflag_sanitizer_attributes in llvmversion_properties[current_settings.llvmversion]) then
  197. exit;
  198. asanglobals:=nil;
  199. module:=get_module(current_filepos.moduleindex);
  200. for asmlisttype:=low(asmlisttype) to high(asmlisttype) do
  201. begin
  202. list:=current_asmdata.AsmLists[asmlisttype];
  203. if not assigned(list) then
  204. continue;
  205. hp:=tai(list.first);
  206. while assigned(hp) do
  207. begin
  208. if (hp.typ=ait_llvmdecl) and
  209. (ldf_definition in taillvmdecl(hp).flags) and
  210. (taillvmdecl(hp).def.typ<>procdef) then
  211. begin
  212. if not assigned(asanglobals) then
  213. begin
  214. asanglobals:=tai_llvmnamedmetadatanode.create('llvm.asan.globals');
  215. current_asmdata.AsmLists[al_rotypedconsts].concat(asanglobals);
  216. end;
  217. hpdecl:=taillvmdecl(hp);
  218. globalfileloc:=tai_llvmunnamedmetadatanode.create;
  219. current_asmdata.AsmLists[al_rotypedconsts].concat(globalfileloc);
  220. if assigned(hpdecl.sym) then
  221. begin
  222. sourcefile:=get_source_file(hpdecl.sym.fileinfo.moduleindex,hpdecl.sym.fileinfo.fileindex);
  223. globalfileloc.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(sourcefile.name)));
  224. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(hpdecl.sym.fileinfo.line)));
  225. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(hpdecl.sym.fileinfo.column)));
  226. end
  227. else
  228. begin
  229. sourcefile:=current_module.sourcefiles.get_file(1);
  230. globalfileloc.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(sourcefile.name)));
  231. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  232. globalfileloc.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  233. end;
  234. asanglobal:=tai_llvmunnamedmetadatanode.create;
  235. current_asmdata.AsmLists[al_rotypedconsts].concat(asanglobal);
  236. asanglobal.addvalue(tai_simpletypedconst.create(cpointerdef.getreusable(hpdecl.def),tai_const.Create_sym(hpdecl.namesym)));
  237. asanglobal.addvalue(tai_simpletypedconst.create(llvm_metadatatype,llvm_getmetadatareftypedconst(globalfileloc)));
  238. if assigned(hpdecl.sym) then
  239. asanglobal.addvalue(tai_simpletypedconst.create(llvm_metadatatype,tai_string.Create(hpdecl.sym.RealName)))
  240. else
  241. asanglobal.addvalue(tai_simpletypedconst.create(llvm_metadatatype,tai_string.Create(hpdecl.namesym.Name)));
  242. { dynamic init }
  243. asanglobal.addvalue(tai_simpletypedconst.create(llvmbool1type,tai_const.Create_8bit(ord(false))));
  244. { no asan }
  245. asanglobal.addvalue(tai_simpletypedconst.create(llvmbool1type,tai_const.Create_8bit(ord((ldf_vectorized in taillvmdecl(hp).flags)))));
  246. asanglobals.addvalue(tai_simpletypedconst.create(llvm_metadatatype,llvm_getmetadatareftypedconst(asanglobal)));
  247. end;
  248. hp:=tai(hp.next);
  249. end;
  250. end;
  251. end;
  252. class procedure tllvmnodeutils.InsertObjectInfo;
  253. var
  254. llvmmoduleflags,
  255. objcmoduleflag,
  256. dwarfversionflag: tai_llvmbasemetadatanode;
  257. objcabiversion: longint;
  258. begin
  259. InsertAsanGlobals;
  260. llvmmoduleflags:=tai_llvmnamedmetadatanode.create('llvm.module.flags');
  261. current_asmdata.AsmLists[al_rotypedconsts].Concat(llvmmoduleflags);
  262. if (m_objectivec1 in current_settings.modeswitches) then
  263. begin
  264. { Objective-C ABI version }
  265. if not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin,system_i386_darwin,system_x86_64_darwin]) or
  266. (MacOSXVersionMin.relationto(10,5,0)>=0) then
  267. objcabiversion:=2
  268. else
  269. objcabiversion:=1;
  270. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  271. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  272. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Version')));
  273. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(objcabiversion)));
  274. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  275. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  276. { image info version }
  277. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  278. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  279. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Version')));
  280. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  281. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  282. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  283. { image info section }
  284. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  285. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  286. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Section')));
  287. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(objc_section_name(sec_objc_image_info))));
  288. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  289. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  290. { garbage collection }
  291. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  292. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  293. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Garbage Collection')));
  294. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  295. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  296. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  297. { insert newly created defs in the implementation rather than interface symtable
  298. (the interface symtable is sealed at this point) }
  299. end;
  300. { debug information }
  301. if (([cs_debuginfo,cs_lineinfo]*current_settings.moduleswitches)<>[]) and
  302. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) then
  303. begin
  304. { the debug info version is the version of the debug info metadata
  305. format }
  306. dwarfversionflag:=tai_llvmunnamedmetadatanode.create;
  307. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2)));
  308. dwarfversionflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Debug Info Version')));
  309. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(llvm_debuginfo_metadata_format[current_settings.llvmversion])));
  310. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(dwarfversionflag));
  311. current_asmdata.AsmLists[al_rotypedconsts].Concat(dwarfversionflag);
  312. { dwarf version }
  313. dwarfversionflag:=tai_llvmunnamedmetadatanode.create;
  314. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2)));
  315. dwarfversionflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Dwarf Version')));
  316. case target_dbg.id of
  317. dbg_dwarf2:
  318. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2)));
  319. dbg_dwarf3:
  320. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(3)));
  321. dbg_dwarf4:
  322. dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(4)));
  323. else
  324. internalerror(2022022012);
  325. end;
  326. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(dwarfversionflag));
  327. current_asmdata.AsmLists[al_rotypedconsts].Concat(dwarfversionflag);
  328. end;
  329. symtablestack.push(current_module.localsymtable);
  330. { add the llvm.compiler.used array }
  331. InsertUsedList(current_module.llvmcompilerusedsyms,'llvm.compiler.used');
  332. { add the llvm.used array }
  333. InsertUsedList(current_module.llvmusedsyms,'llvm.used');
  334. { add the llvm.global_ctors array }
  335. InsertInitFiniList(current_module.llvminitprocs,'llvm.global_ctors');
  336. { add the llvm.global_dtors array }
  337. InsertInitFiniList(current_module.llvmfiniprocs,'llvm.global_dtors');
  338. { add "type xx = .." statements for all used recorddefs }
  339. with TLLVMTypeInfo.Create do
  340. begin
  341. inserttypeinfo;
  342. free;
  343. end;
  344. symtablestack.pop(current_module.localsymtable);
  345. end;
  346. class procedure tllvmnodeutils.RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean);
  347. var
  348. last: TTypedAsmSym;
  349. begin
  350. if compileronly then
  351. begin
  352. { filter multiple adds in succession here already }
  353. last:=TTypedAsmSym(current_module.llvmcompilerusedsyms.Last);
  354. if not assigned(last) or
  355. (last.sym<>sym) then
  356. current_module.llvmcompilerusedsyms.Add(TTypedAsmSym.Create(sym,def))
  357. end
  358. else
  359. begin
  360. last:=TTypedAsmSym(current_module.llvmusedsyms.Last);
  361. if not assigned(last) or
  362. (last.sym<>sym) then
  363. current_module.llvmusedsyms.Add(TTypedAsmSym.Create(sym,def))
  364. end;
  365. end;
  366. class procedure tllvmnodeutils.RegisterModuleInitFunction(pd: tprocdef);
  367. begin
  368. current_module.llvminitprocs.add(pd);
  369. end;
  370. class procedure tllvmnodeutils.RegisterModuleFiniFunction(pd: tprocdef);
  371. begin
  372. current_module.llvmfiniprocs.add(pd);
  373. end;
  374. begin
  375. cnodeutils:=tllvmnodeutils;
  376. end.