tgllvm.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. public
  47. alloclist: tasmlist;
  48. constructor create; override;
  49. destructor destroy; override;
  50. procedure setfirsttemp(l: asizeint); override;
  51. procedure temp_to_ref(p: ptemprecord; out ref: treference); override;
  52. procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference); override;
  53. procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); override;
  54. end;
  55. var
  56. orgtgclass: ttgobjclass;
  57. implementation
  58. uses
  59. cutils,
  60. systems,verbose,
  61. procinfo,
  62. llvmbase,aasmllvm,
  63. symconst,symdef,
  64. cgobj
  65. ;
  66. { ttgllvm }
  67. procedure ttgllvm.alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference);
  68. var
  69. tl: ptemprecord;
  70. reg: tregister;
  71. oldfileinfo: tfileposinfo;
  72. begin
  73. reg:=cg.gettempregister(list);
  74. new(tl);
  75. tl^.temptype:=temptype;
  76. tl^.def:=def;
  77. tl^.fini:=fini;
  78. tl^.alignment:=alignment;
  79. tl^.pos:=getsupreg(reg);
  80. tl^.size:=size;
  81. tl^.next:=templist;
  82. tl^.nextfree:=nil;
  83. templist:=tl;
  84. temp_to_ref(tl,ref);
  85. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  86. { TODO: add llvm.lifetime.start() for this allocation and afterwards
  87. llvm.lifetime.end() for freetemp (if the llvm version supports it) }
  88. inc(lasttemp);
  89. { allocation for the temp -- should have lineinfo of the start of the
  90. routine }
  91. if assigned(current_procinfo) then
  92. begin
  93. oldfileinfo:=current_filepos;
  94. current_filepos:=current_procinfo.procdef.fileinfo;
  95. end
  96. else
  97. { avoid uninitialised warning later }
  98. oldfileinfo.line:=0;
  99. alloclist.concat(taillvm.op_ref_size(la_alloca,ref,def));
  100. if assigned(current_procinfo) then
  101. current_filepos:=oldfileinfo;
  102. end;
  103. procedure ttgllvm.gethltempintern(list: TAsmList; def: tdef; alignment: shortint; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  104. begin
  105. { empty array (can happen for arrayconstructors) -> don't request the
  106. size, as that will internalerror }
  107. if (def.typ=arraydef) and
  108. (tarraydef(def).highrange<tarraydef(def).lowrange) then
  109. alloctemp(list,0,alignment,temptype,def,false,ref)
  110. else
  111. alloctemp(list,def.size,alignment,temptype,def,false,ref);
  112. end;
  113. procedure ttgllvm.temp_to_ref(p: ptemprecord; out ref: treference);
  114. var
  115. temppos: treftemppos;
  116. begin
  117. { on the LLVM target, every temp is independent and encoded via a
  118. separate temp register whose superregister number is stored in p^.pos }
  119. temppos.val:=p^.pos;
  120. reference_reset_base(ref,newreg(R_TEMPREGISTER,p^.pos,R_SUBWHOLE),0,temppos,p^.alignment,[]);
  121. end;
  122. constructor ttgllvm.create;
  123. begin
  124. inherited create;
  125. direction:=1;
  126. alloclist:=TAsmList.create;
  127. end;
  128. destructor ttgllvm.destroy;
  129. begin
  130. alloclist.free;
  131. inherited destroy;
  132. end;
  133. procedure ttgllvm.setfirsttemp(l: asizeint);
  134. begin
  135. firsttemp:=l;
  136. lasttemp:=l;
  137. end;
  138. procedure ttgllvm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
  139. begin
  140. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  141. gethltempintern(list,def,alignment,size,tt_persistent,ref);
  142. end;
  143. procedure ttgllvm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  144. begin
  145. gethltempintern(list,def,def.alignment,forcesize,tt_persistent,ref);
  146. end;
  147. begin
  148. orgtgclass:=tgobjclass;
  149. tgobjclass:=ttgllvm;
  150. end.