nllvmutil.pas 12 KB

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