2
0

nllvmutil.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. aasmdata,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. public
  30. class procedure InsertObjectInfo; override;
  31. end;
  32. implementation
  33. uses
  34. verbose,cutils,globals,fmodule,systems,
  35. aasmbase,aasmtai,cpubase,llvmbase,aasmllvm,
  36. aasmcnst,nllvmtcon,
  37. symbase,symtable,defutil,
  38. llvmtype;
  39. class procedure tllvmnodeutils.insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint);
  40. var
  41. asmsym: tasmsymbol;
  42. field1, field2: tsym;
  43. tcb: ttai_typedconstbuilder;
  44. begin
  45. if sym.globalasmsym then
  46. asmsym:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_GLOBAL,AT_DATA,sym.vardef)
  47. else
  48. asmsym:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_LOCAL,AT_DATA,sym.vardef);
  49. if not(vo_is_thread_var in sym.varoptions) then
  50. list.concat(taillvmdecl.createdef(asmsym,sym.vardef,nil,sec_data,varalign))
  51. else if tf_section_threadvars in target_info.flags then
  52. list.concat(taillvmdecl.createtls(asmsym,sym.vardef,varalign))
  53. else
  54. list.concat(taillvmdecl.createdef(asmsym,
  55. get_threadvar_record(sym.vardef,field1,field2),
  56. nil,sec_data,varalign));
  57. end;
  58. class procedure tllvmnodeutils.InsertUsedList(var usedsyms: tfpobjectlist; const usedsymsname: TSymstr);
  59. var
  60. useddef: tdef;
  61. tcb: ttai_typedconstbuilder;
  62. decl: taillvmdecl;
  63. i: longint;
  64. begin
  65. if usedsyms.count<>0 then
  66. begin
  67. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  68. tllvmtai_typedconstbuilder(tcb).appendingdef:=true;
  69. useddef:=carraydef.getreusable(voidpointertype,usedsyms.count);
  70. tcb.maybe_begin_aggregate(useddef);
  71. for i:=0 to usedsyms.count-1 do
  72. begin
  73. decl:=taillvmdecl(usedsyms[i]);
  74. tcb.queue_init(voidpointertype);
  75. tcb.queue_emit_asmsym(decl.namesym,decl.def);
  76. end;
  77. tcb.maybe_end_aggregate(useddef);
  78. current_asmdata.AsmLists[al_globals].concatlist(
  79. tcb.get_final_asmlist(
  80. current_asmdata.DefineAsmSymbol(
  81. usedsymsname,AB_GLOBAL,AT_DATA,useddef),useddef,sec_user,
  82. 'llvm.metadata',0
  83. )
  84. );
  85. tcb.free;
  86. end;
  87. usedsyms.free;
  88. usedsyms:=nil;
  89. end;
  90. class procedure tllvmnodeutils.InsertObjectInfo;
  91. begin
  92. inherited;
  93. { add the llvm.compiler.used array }
  94. InsertUsedList(current_module.llvmcompilerusedsyms,'llvm.compiler.used');
  95. { add the llvm.used array }
  96. InsertUsedList(current_module.llvmusedsyms,'llvm.used');
  97. { add "type xx = .." statements for all used recorddefs }
  98. with TLLVMTypeInfo.Create do
  99. begin
  100. inserttypeinfo;
  101. free;
  102. end;
  103. end;
  104. begin
  105. cnodeutils:=tllvmnodeutils;
  106. end.