llvmpara.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. Includes the llvm parameter manager
  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 llvmpara;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,aasmdata,
  22. symconst,symtype,symdef,symsym,
  23. parabase,
  24. cpupara;
  25. type
  26. { LLVM stands for "low level code generator", and regarding parameter
  27. handling it is indeed very low level. We are responsible for decomposing
  28. aggregate parameters into multiple simple parameters in case they have
  29. to be passed in special registers (such as floating point or SSE), and
  30. also for indicating whether e.g. 8 bit parameters need to be sign or
  31. zero exntended. This corresponds to pretty much what we do when creating
  32. parameter locations, so we reuse the original parameter manager and then
  33. process its output.
  34. The future will tell whether we can do this without
  35. architecture-specific code, or whether we will have to integrate parts
  36. into the various tcpuparamanager classes }
  37. tllvmparamanager = class(tcpuparamanager)
  38. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  39. procedure createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara); override;
  40. function create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint; override;
  41. function get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara; override;
  42. private
  43. procedure set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  44. procedure add_llvm_callee_paraloc_names(p: tabstractprocdef);
  45. end;
  46. implementation
  47. uses
  48. verbose,
  49. aasmbase,
  50. llvmsym,
  51. paramgr,defutil,llvmdef,
  52. cgbase,cgutils,tgobj,hlcgobj;
  53. { tllvmparamanager }
  54. function tllvmparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  55. begin
  56. { we can use the paraloc on the callee side if the SSA property is
  57. guaranteed, i.e., if it is a constant location (and if it's not been
  58. split up into multiple locations for ABI reasons). We can't deduce that
  59. from the paraloc though, we need the parasym for that. Potential
  60. future optimisation, although llvm will probably optimise away the
  61. temps we create anyway }
  62. result:=false;
  63. end;
  64. procedure tllvmparamanager.createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara);
  65. var
  66. paraloc,
  67. nextloc: pcgparalocation;
  68. begin
  69. inherited;
  70. paraloc:=cgpara.location;
  71. { No need to set paraloc^.llvmloc.*, these are not used/needed for temp
  72. paralocs }
  73. while assigned(paraloc) do
  74. begin
  75. if paramanager.push_addr_param(parasym.varspez,parasym.vardef,tabstractprocdef(parasym.owner.defowner).proccalloption) or
  76. not llvmbyvalparaloc(paraloc) then
  77. begin
  78. case paraloc^.loc of
  79. LOC_REFERENCE:
  80. begin
  81. case hlcg.def2regtyp(paraloc^.def) of
  82. R_INTREGISTER,
  83. R_ADDRESSREGISTER:
  84. paraloc^.loc:=LOC_REGISTER;
  85. R_FPUREGISTER:
  86. paraloc^.loc:=LOC_FPUREGISTER;
  87. R_MMREGISTER:
  88. paraloc^.Loc:=LOC_MMREGISTER;
  89. else
  90. internalerror(2013012308);
  91. end;
  92. paraloc^.register:=hlcg.getregisterfordef(list,paraloc^.def);
  93. paraloc^.llvmvalueloc:=true;
  94. end;
  95. LOC_REGISTER,
  96. LOC_FPUREGISTER,
  97. LOC_MMREGISTER:
  98. begin
  99. paraloc^.llvmvalueloc:=true;
  100. end;
  101. LOC_VOID:
  102. ;
  103. else
  104. internalerror(2014012302);
  105. end;
  106. end
  107. else
  108. begin
  109. { turn this paraloc into the "byval" parameter: at the llvm level,
  110. a pointer to the value that it should place on the stack (or
  111. passed in registers, in some cases) }
  112. paraloc^.llvmvalueloc:=false;
  113. paraloc^.def:=getpointerdef(paraloc^.def);
  114. paraloc^.size:=def_cgsize(paraloc^.def);
  115. paraloc^.loc:=LOC_REGISTER;
  116. paraloc^.register:=hlcg.getaddressregister(list,paraloc^.def);
  117. { remove all other paralocs }
  118. nextloc:=paraloc^.next;
  119. while assigned(nextloc) do
  120. begin
  121. dispose(nextloc);
  122. nextloc:=paraloc^.next;
  123. end;
  124. end;
  125. paraloc^.llvmloc.loc:=paraloc^.loc;
  126. paraloc^.llvmloc.reg:=paraloc^.register;
  127. paraloc:=paraloc^.next;
  128. end;
  129. end;
  130. function tllvmparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
  131. begin
  132. result:=inherited create_paraloc_info(p, side);
  133. { on the calleeside, llvm declares the parameters similar to Pascal or C
  134. (a list of parameters and their types), but they correspond more
  135. closely to parameter locations than to parameters -> add names to the
  136. locations }
  137. if side=calleeside then
  138. add_llvm_callee_paraloc_names(p)
  139. end;
  140. function tllvmparamanager.get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  141. var
  142. paraloc: pcgparalocation;
  143. begin
  144. result:=inherited;
  145. paraloc:=result.location;
  146. repeat
  147. paraloc^.llvmvalueloc:=true;
  148. paraloc:=paraloc^.next;
  149. until not assigned(paraloc);
  150. end;
  151. { hp non-nil: parasym to check
  152. hp nil: function result
  153. }
  154. procedure tllvmparamanager.set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  155. var
  156. paraloc: PCGParaLocation;
  157. paralocnr: longint;
  158. begin
  159. paraloc:=hp.paraloc[calleeside].location;
  160. paralocnr:=0;
  161. repeat
  162. paraloc^.llvmloc.loc:=LOC_REFERENCE;
  163. paraloc^.llvmloc.sym:=current_asmdata.DefineAsmSymbol(llvmparaname(hp,paralocnr),AB_LOCAL,AT_DATA);
  164. { byval: a pointer to a type that should actually be passed by
  165. value (e.g. a record that should be passed on the stack) }
  166. paraloc^.llvmvalueloc:=
  167. paramanager.push_addr_param(hp.varspez,hp.vardef,p.proccalloption) or
  168. not llvmbyvalparaloc(paraloc);
  169. paraloc:=paraloc^.next;
  170. inc(paralocnr);
  171. until not assigned(paraloc);
  172. end;
  173. procedure tllvmparamanager.add_llvm_callee_paraloc_names(p: tabstractprocdef);
  174. var
  175. paranr: longint;
  176. hp: tparavarsym;
  177. begin
  178. for paranr:=0 to p.paras.count-1 do
  179. begin
  180. hp:=tparavarsym(p.paras[paranr]);
  181. set_llvm_paraloc_name(p,hp,hp.paraloc[calleeside]);
  182. end;
  183. end;
  184. begin
  185. { replace the native parameter manager. Maybe this has to be moved to a
  186. procedure like the creations of the code generators, but possibly not since
  187. we still call the original paramanager }
  188. paramanager.free;
  189. paramanager:=tllvmparamanager.create;
  190. end.