nllvmutil.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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]:=cprocvardef.getreusableprocaddr(pd,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. { insert newly created defs in the implementation rather than interface symtable
  185. (the interface symtable is sealed at this point) }
  186. symtablestack.push(current_module.localsymtable);
  187. { add the llvm.compiler.used array }
  188. InsertUsedList(current_module.llvmcompilerusedsyms,'llvm.compiler.used');
  189. { add the llvm.used array }
  190. InsertUsedList(current_module.llvmusedsyms,'llvm.used');
  191. { add the llvm.global_ctors array }
  192. InsertInitFiniList(current_module.llvminitprocs,'llvm.global_ctors');
  193. { add the llvm.global_dtors array }
  194. InsertInitFiniList(current_module.llvmfiniprocs,'llvm.global_dtors');
  195. { add "type xx = .." statements for all used recorddefs }
  196. with TLLVMTypeInfo.Create do
  197. begin
  198. inserttypeinfo;
  199. free;
  200. end;
  201. symtablestack.pop(current_module.localsymtable);
  202. end;
  203. class procedure tllvmnodeutils.RegisterUsedAsmSym(sym: TAsmSymbol; def: tdef; compileronly: boolean);
  204. var
  205. last: TTypedAsmSym;
  206. begin
  207. if compileronly then
  208. begin
  209. { filter multiple adds in succession here already }
  210. last:=TTypedAsmSym(current_module.llvmcompilerusedsyms.Last);
  211. if not assigned(last) or
  212. (last.sym<>sym) then
  213. current_module.llvmcompilerusedsyms.Add(TTypedAsmSym.Create(sym,def))
  214. end
  215. else
  216. begin
  217. last:=TTypedAsmSym(current_module.llvmusedsyms.Last);
  218. if not assigned(last) or
  219. (last.sym<>sym) then
  220. current_module.llvmusedsyms.Add(TTypedAsmSym.Create(sym,def))
  221. end;
  222. end;
  223. class procedure tllvmnodeutils.GenerateObjCImageInfo;
  224. var
  225. llvmmoduleflags,
  226. objcmoduleflag: tai_llvmbasemetadatanode;
  227. objcabiversion: longint;
  228. begin
  229. llvmmoduleflags:=tai_llvmnamedmetadatanode.create('llvm.module.flags');
  230. current_asmdata.AsmLists[al_rotypedconsts].Concat(llvmmoduleflags);
  231. { Objective-C ABI version }
  232. if not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin,system_i386_darwin,system_x86_64_darwin]) or
  233. (CompareVersionStrings(MacOSXVersionMin,'10.5')>=0) then
  234. objcabiversion:=2
  235. else
  236. objcabiversion:=1;
  237. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  238. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  239. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Version')));
  240. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(objcabiversion)));
  241. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  242. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  243. { image info version }
  244. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  245. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  246. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Version')));
  247. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  248. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  249. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  250. { image info section }
  251. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  252. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  253. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Image Info Section')));
  254. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create(objc_section_name(sec_objc_image_info))));
  255. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  256. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  257. { garbage collection }
  258. objcmoduleflag:=tai_llvmunnamedmetadatanode.create;
  259. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(1)));
  260. objcmoduleflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Objective-C Garbage Collection')));
  261. objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0)));
  262. llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag));
  263. current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag);
  264. end;
  265. class procedure tllvmnodeutils.RegisterModuleInitFunction(pd: tprocdef);
  266. begin
  267. current_module.llvminitprocs.add(pd);
  268. end;
  269. class procedure tllvmnodeutils.RegisterModuleFiniFunction(pd: tprocdef);
  270. begin
  271. current_module.llvmfiniprocs.add(pd);
  272. end;
  273. begin
  274. cnodeutils:=tllvmnodeutils;
  275. end.