2
0

llvmpara.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. procedure getintparaloc(list: TAsmList; pd: tabstractprocdef; nr: longint; var cgpara: tcgpara); override;
  39. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  40. procedure createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara); override;
  41. function create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint; override;
  42. function get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara; override;
  43. private
  44. procedure set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  45. procedure add_llvm_callee_paraloc_names(p: tabstractprocdef);
  46. end;
  47. implementation
  48. uses
  49. verbose,
  50. aasmbase,
  51. llvmsym,
  52. paramgr,defutil,llvmdef,
  53. cgbase,cgutils,tgobj,hlcgobj;
  54. { tllvmparamanager }
  55. procedure tllvmparamanager.getintparaloc(list: TAsmList; pd: tabstractprocdef; nr: longint; var cgpara: tcgpara);
  56. begin
  57. if (nr<1) or (nr>pd.paras.count) then
  58. InternalError(2015040401);
  59. pd.init_paraloc_info(callerside);
  60. createtempparaloc(list,pd.proccalloption,tparavarsym(pd.paras[nr-1]),true,cgpara);
  61. end;
  62. function tllvmparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  63. begin
  64. { we can use the paraloc on the callee side if the SSA property is
  65. guaranteed, i.e., if it is a constant location (and if it's not been
  66. split up into multiple locations for ABI reasons). We can't deduce that
  67. from the paraloc though, we need the parasym for that. Potential
  68. future optimisation, although llvm will probably optimise away the
  69. temps we create anyway }
  70. result:=false;
  71. end;
  72. procedure tllvmparamanager.createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara);
  73. var
  74. paraloc,
  75. nextloc: pcgparalocation;
  76. begin
  77. inherited;
  78. paraloc:=cgpara.location;
  79. { No need to set paraloc^.llvmloc.*, these are not used/needed for temp
  80. paralocs }
  81. while assigned(paraloc) do
  82. begin
  83. { varargs parameters do not have a parasym.owner, but they're always
  84. by value }
  85. if (assigned(parasym.owner) and
  86. paramanager.push_addr_param(parasym.varspez,parasym.vardef,tabstractprocdef(parasym.owner.defowner).proccalloption)) or
  87. not llvmbyvalparaloc(paraloc) then
  88. begin
  89. case paraloc^.loc of
  90. LOC_REFERENCE:
  91. begin
  92. case hlcg.def2regtyp(paraloc^.def) of
  93. R_INTREGISTER,
  94. R_ADDRESSREGISTER:
  95. paraloc^.loc:=LOC_REGISTER;
  96. R_FPUREGISTER:
  97. paraloc^.loc:=LOC_FPUREGISTER;
  98. R_MMREGISTER:
  99. paraloc^.Loc:=LOC_MMREGISTER;
  100. else
  101. internalerror(2013012308);
  102. end;
  103. paraloc^.register:=hlcg.getregisterfordef(list,paraloc^.def);
  104. paraloc^.llvmvalueloc:=true;
  105. end;
  106. LOC_REGISTER,
  107. LOC_FPUREGISTER,
  108. LOC_MMREGISTER:
  109. begin
  110. paraloc^.llvmvalueloc:=true;
  111. end;
  112. LOC_VOID:
  113. ;
  114. else
  115. internalerror(2014012302);
  116. end;
  117. end
  118. else
  119. begin
  120. { turn this paraloc into the "byval" parameter: at the llvm level,
  121. a pointer to the value that it should place on the stack (or
  122. passed in registers, in some cases) }
  123. paraloc^.llvmvalueloc:=false;
  124. paraloc^.def:=cpointerdef.getreusable(paraloc^.def);
  125. paraloc^.size:=def_cgsize(paraloc^.def);
  126. paraloc^.loc:=LOC_REGISTER;
  127. paraloc^.register:=hlcg.getaddressregister(list,paraloc^.def);
  128. { remove all other paralocs }
  129. nextloc:=paraloc^.next;
  130. while assigned(nextloc) do
  131. begin
  132. dispose(nextloc);
  133. nextloc:=paraloc^.next;
  134. end;
  135. end;
  136. paraloc^.llvmloc.loc:=paraloc^.loc;
  137. paraloc^.llvmloc.reg:=paraloc^.register;
  138. paraloc:=paraloc^.next;
  139. end;
  140. end;
  141. function tllvmparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
  142. begin
  143. result:=inherited create_paraloc_info(p, side);
  144. { on the calleeside, llvm declares the parameters similar to Pascal or C
  145. (a list of parameters and their types), but they correspond more
  146. closely to parameter locations than to parameters -> add names to the
  147. locations }
  148. if side=calleeside then
  149. add_llvm_callee_paraloc_names(p)
  150. end;
  151. function tllvmparamanager.get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  152. var
  153. paraloc: pcgparalocation;
  154. begin
  155. result:=inherited;
  156. paraloc:=result.location;
  157. repeat
  158. paraloc^.llvmvalueloc:=true;
  159. paraloc:=paraloc^.next;
  160. until not assigned(paraloc);
  161. end;
  162. { hp non-nil: parasym to check
  163. hp nil: function result
  164. }
  165. procedure tllvmparamanager.set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  166. var
  167. paraloc: PCGParaLocation;
  168. paralocnr: longint;
  169. begin
  170. paraloc:=hp.paraloc[calleeside].location;
  171. paralocnr:=0;
  172. repeat
  173. paraloc^.llvmloc.loc:=LOC_REFERENCE;
  174. paraloc^.llvmloc.sym:=current_asmdata.DefineAsmSymbol(llvmparaname(hp,paralocnr),AB_TEMP,AT_DATA);
  175. { byval: a pointer to a type that should actually be passed by
  176. value (e.g. a record that should be passed on the stack) }
  177. paraloc^.llvmvalueloc:=
  178. paramanager.push_addr_param(hp.varspez,hp.vardef,p.proccalloption) or
  179. not llvmbyvalparaloc(paraloc);
  180. paraloc:=paraloc^.next;
  181. inc(paralocnr);
  182. until not assigned(paraloc);
  183. end;
  184. procedure tllvmparamanager.add_llvm_callee_paraloc_names(p: tabstractprocdef);
  185. var
  186. paranr: longint;
  187. hp: tparavarsym;
  188. begin
  189. for paranr:=0 to p.paras.count-1 do
  190. begin
  191. hp:=tparavarsym(p.paras[paranr]);
  192. set_llvm_paraloc_name(p,hp,hp.paraloc[calleeside]);
  193. end;
  194. end;
  195. begin
  196. { replace the native parameter manager. Maybe this has to be moved to a
  197. procedure like the creations of the code generators, but possibly not since
  198. we still call the original paramanager }
  199. paramanager.free;
  200. paramanager:=tllvmparamanager.create;
  201. end.