tgllvm.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. {
  2. Copyright (c) 1998-2002,2012 by Florian Klaempfl, Jonas Maebe
  3. This unit implements the LLVM-specific temp. generator
  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 tgllvm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globals,globtype,
  23. symtype,
  24. cpubase,cpuinfo,cgbase,cgutils,
  25. aasmbase,aasmtai,aasmdata,
  26. tgobj;
  27. type
  28. { LLVM temp manager: in LLVM, you allocate every temp separately using
  29. the "alloca" instrinsic. Every such temp is a separate stack slot, but
  30. can be turned into a regvar (or be decomposed) by LLVM. To avoid
  31. problems with turning stack slots into regvars, we don't allocate one
  32. big blob of memory that we manage ourselves using the regular temp
  33. manager. Instead, we just allocate a new "stack pointer register"
  34. (R_TEMPREGISTER) every time we need a new temp. This allows for having
  35. the generic code generator modify the offset without interfering with
  36. our ability to determine which temp the reference points to.
  37. Temps are currently not reused, but that should probably be added in
  38. the future (except if adding liveness information for the temps enables
  39. llvm to do so by itself and we don't run out of temp registers).
  40. }
  41. { ttgllvm }
  42. ttgllvm = class(ttgobj)
  43. protected
  44. procedure alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference); override;
  45. procedure gethltempintern(list: TAsmList; def: tdef; alignment: shortint; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  46. procedure freetemphook(list: TAsmList; temp: ptemprecord); override;
  47. procedure emit_lifetime(list: TAsmList; const procname: string; temp: ptemprecord);
  48. public
  49. alloclist: tasmlist;
  50. constructor create; override;
  51. destructor destroy; override;
  52. procedure setfirsttemp(l: asizeint); override;
  53. procedure temp_to_ref(p: ptemprecord; out ref: treference); override;
  54. procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference); override;
  55. procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); override;
  56. end;
  57. var
  58. orgtgclass: ttgobjclass;
  59. implementation
  60. uses
  61. cutils,
  62. systems,verbose,
  63. procinfo,
  64. llvmbase,aasmllvm,
  65. symconst,symtable,symdef,defutil,
  66. paramgr,parabase,cgobj,hlcgobj
  67. ;
  68. { ttgllvm }
  69. procedure ttgllvm.alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference);
  70. var
  71. tl: ptemprecord;
  72. reg: tregister;
  73. oldfileinfo: tfileposinfo;
  74. begin
  75. reg:=cg.gettempregister(list);
  76. new(tl);
  77. tl^.temptype:=temptype;
  78. tl^.def:=def;
  79. tl^.fini:=fini;
  80. tl^.alignment:=alignment;
  81. tl^.pos:=getsupreg(reg);
  82. tl^.size:=size;
  83. tl^.next:=templist;
  84. tl^.nextfree:=nil;
  85. templist:=tl;
  86. temp_to_ref(tl,ref);
  87. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  88. emit_lifetime(list,'llvm_lifetime_start',tl);
  89. inc(lasttemp);
  90. { allocation for the temp -- should have lineinfo of the start of the
  91. routine }
  92. if assigned(current_procinfo) then
  93. begin
  94. oldfileinfo:=current_filepos;
  95. current_filepos:=current_procinfo.procdef.fileinfo;
  96. end
  97. else
  98. { avoid uninitialised warning later }
  99. oldfileinfo.line:=0;
  100. alloclist.concat(taillvm.op_ref_size(la_alloca,ref,def));
  101. if assigned(current_procinfo) then
  102. current_filepos:=oldfileinfo;
  103. end;
  104. procedure ttgllvm.gethltempintern(list: TAsmList; def: tdef; alignment: shortint; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  105. begin
  106. { empty array (can happen for arrayconstructors) -> don't request the
  107. size, as that will internalerror }
  108. if (def.typ=arraydef) and
  109. (tarraydef(def).highrange<tarraydef(def).lowrange) then
  110. alloctemp(list,0,alignment,temptype,def,false,ref)
  111. else
  112. alloctemp(list,def.size,alignment,temptype,def,false,ref);
  113. end;
  114. procedure ttgllvm.freetemphook(list: TAsmList; temp: ptemprecord);
  115. begin
  116. inherited;
  117. emit_lifetime(list,'llvm_lifetime_end',temp);
  118. end;
  119. procedure ttgllvm.emit_lifetime(list: TAsmList; const procname: string; temp: ptemprecord);
  120. var
  121. sizepara, ptrpara: tcgpara;
  122. pd: tprocdef;
  123. ref: treference;
  124. begin
  125. if (temp^.size<>0) and
  126. not is_managed_type(temp^.def) then
  127. begin
  128. temp_to_ref(temp,ref);
  129. sizepara.init;
  130. ptrpara.init;
  131. pd:=search_system_proc(procname);
  132. paramanager.getcgtempparaloc(list,pd,1,sizepara);
  133. paramanager.getcgtempparaloc(list,pd,2,ptrpara);
  134. hlcg.a_load_const_cgpara(list,sizepara.def,temp^.size,sizepara);
  135. hlcg.a_loadaddr_ref_cgpara(list,temp^.def,ref,ptrpara);
  136. hlcg.g_call_system_proc(list,pd,[@sizepara,@ptrpara],nil).resetiftemp;
  137. sizepara.reset;
  138. ptrpara.reset;
  139. end;
  140. end;
  141. procedure ttgllvm.temp_to_ref(p: ptemprecord; out ref: treference);
  142. var
  143. temppos: treftemppos;
  144. begin
  145. { on the LLVM target, every temp is independent and encoded via a
  146. separate temp register whose superregister number is stored in p^.pos }
  147. temppos.val:=p^.pos;
  148. reference_reset_base(ref,newreg(R_TEMPREGISTER,p^.pos,R_SUBWHOLE),0,temppos,p^.alignment,[]);
  149. end;
  150. constructor ttgllvm.create;
  151. begin
  152. inherited create;
  153. direction:=1;
  154. alloclist:=TAsmList.create;
  155. end;
  156. destructor ttgllvm.destroy;
  157. begin
  158. alloclist.free;
  159. inherited destroy;
  160. end;
  161. procedure ttgllvm.setfirsttemp(l: asizeint);
  162. begin
  163. firsttemp:=l;
  164. lasttemp:=l;
  165. end;
  166. procedure ttgllvm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
  167. begin
  168. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  169. gethltempintern(list,def,alignment,size,tt_persistent,ref);
  170. end;
  171. procedure ttgllvm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  172. begin
  173. gethltempintern(list,def,def.alignment,forcesize,temptype,ref);
  174. end;
  175. begin
  176. if not assigned(tgobjclass) then
  177. begin
  178. writeln('Internalerror 2018052004');
  179. halt(1);
  180. end;
  181. orgtgclass:=tgobjclass;
  182. tgobjclass:=ttgllvm;
  183. end.