tgllvm.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public
  46. alloclist: tasmlist;
  47. constructor create; override;
  48. destructor destroy; override;
  49. procedure setfirsttemp(l: asizeint); override;
  50. function istemp(const ref: treference): boolean; override;
  51. procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference); override;
  52. procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); override;
  53. procedure ungetiftemp(list: TAsmList; const 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,
  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. oldfileinfo: tfileposinfo;
  71. begin
  72. reference_reset_base(ref,cg.gettempregister(list),0,alignment);
  73. new(tl);
  74. tl^.temptype:=temptype;
  75. tl^.def:=def;
  76. tl^.fini:=fini;
  77. tl^.alignment:=alignment;
  78. tl^.pos:=getsupreg(ref.base);
  79. tl^.size:=size;
  80. tl^.next:=templist;
  81. tl^.nextfree:=nil;
  82. templist:=tl;
  83. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  84. { TODO: add llvm.lifetime.start() for this allocation and afterwards
  85. llvm.lifetime.end() for freetemp (if the llvm version supports it) }
  86. inc(lasttemp);
  87. { allocation for the temp -- should have lineinfo of the start of the
  88. routine }
  89. if assigned(current_procinfo) then
  90. begin
  91. oldfileinfo:=current_filepos;
  92. current_filepos:=current_procinfo.procdef.fileinfo;
  93. end
  94. else
  95. { avoid uninitialised warning later }
  96. oldfileinfo.line:=0;
  97. alloclist.concat(taillvm.op_ref_size(la_alloca,ref,def));
  98. if assigned(current_procinfo) then
  99. current_filepos:=oldfileinfo;
  100. end;
  101. function ttgllvm.istemp(const ref: treference): boolean;
  102. begin
  103. result:=getregtype(ref.base)=R_TEMPREGISTER;
  104. end;
  105. constructor ttgllvm.create;
  106. begin
  107. inherited create;
  108. direction:=1;
  109. alloclist:=TAsmList.create;
  110. end;
  111. destructor ttgllvm.destroy;
  112. begin
  113. alloclist.free;
  114. inherited destroy;
  115. end;
  116. procedure ttgllvm.setfirsttemp(l: asizeint);
  117. begin
  118. firsttemp:=l;
  119. lasttemp:=l;
  120. end;
  121. procedure ttgllvm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
  122. begin
  123. gethltemp(list,def,size,tt_persistent,ref);
  124. end;
  125. procedure ttgllvm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  126. begin
  127. alloctemp(list,def.size,def.alignment,temptype,def,false,ref);
  128. end;
  129. procedure ttgllvm.ungetiftemp(list: TAsmList; const ref: treference);
  130. begin
  131. if istemp(ref) then
  132. FreeTemp(list,getsupreg(ref.base),[tt_normal]);
  133. end;
  134. begin
  135. orgtgclass:=tgobjclass;
  136. tgobjclass:=ttgllvm;
  137. end.