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