tgllvm.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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,alignment : longint; temptype : ttemptype; def:tdef; out ref: treference); override;
  45. public
  46. alloclist: tasmlist;
  47. constructor create; override;
  48. destructor destroy; override;
  49. procedure setfirsttemp(l : longint); override;
  50. function istemp(const ref: treference): boolean; override;
  51. procedure getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference); override;
  52. procedure gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference); override;
  53. procedure gethltemptyped(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference); override;
  54. procedure ungetiftemp(list: TAsmList; const ref: treference); override;
  55. end;
  56. implementation
  57. uses
  58. cutils,
  59. systems,verbose,
  60. procinfo,
  61. llvmbase,aasmllvm,
  62. symconst,
  63. cgobj
  64. ;
  65. { ttgllvm }
  66. procedure ttgllvm.alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef; out ref: treference);
  67. var
  68. tl: ptemprecord;
  69. begin
  70. reference_reset_base(ref,cg.gettempregister(list),0,alignment);
  71. new(tl);
  72. tl^.temptype:=temptype;
  73. tl^.def:=def;
  74. tl^.pos:=getsupreg(ref.base);
  75. tl^.size:=size;
  76. tl^.next:=templist;
  77. tl^.nextfree:=nil;
  78. templist:=tl;
  79. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  80. { TODO: add llvm.lifetime.start() for this allocation and afterwards
  81. llvm.lifetime.end() for freetemp (if the llvm version supports it) }
  82. inc(lasttemp);
  83. { allocation for the temp }
  84. alloclist.concat(taillvm.op_ref_size(la_alloca,ref,def));
  85. end;
  86. function ttgllvm.istemp(const ref: treference): boolean;
  87. begin
  88. result:=getregtype(ref.base)=R_TEMPREGISTER;
  89. end;
  90. constructor ttgllvm.create;
  91. begin
  92. inherited create;
  93. direction:=1;
  94. alloclist:=TAsmList.create;
  95. end;
  96. destructor ttgllvm.destroy;
  97. begin
  98. alloclist.free;
  99. inherited destroy;
  100. end;
  101. procedure ttgllvm.setfirsttemp(l: longint);
  102. begin
  103. firsttemp:=l;
  104. lasttemp:=l;
  105. end;
  106. procedure ttgllvm.getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference);
  107. begin
  108. gethltemp(list,def,size,tt_persistent,ref);
  109. end;
  110. procedure ttgllvm.gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference);
  111. begin
  112. alloctemp(list,def.size,def.alignment,temptype,def,ref);
  113. end;
  114. procedure ttgllvm.gethltemptyped(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  115. begin
  116. inherited gethltemptyped(list, def, temptype, ref);
  117. end;
  118. procedure ttgllvm.ungetiftemp(list: TAsmList; const ref: treference);
  119. begin
  120. if istemp(ref) then
  121. FreeTemp(list,getsupreg(ref.base),[tt_normal]);
  122. end;
  123. begin
  124. tgobjclass:=ttgllvm;
  125. end.