2
0

llvmpara.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if vo_is_funcret in parasym.varoptions then
  84. paraloc^.retvalloc:=true;
  85. { varargs parameters do not have a parasym.owner, but they're always
  86. by value }
  87. if (assigned(parasym.owner) and
  88. paramanager.push_addr_param(parasym.varspez,parasym.vardef,tabstractprocdef(parasym.owner.defowner).proccalloption)) or
  89. not llvmbyvalparaloc(paraloc) then
  90. begin
  91. case paraloc^.loc of
  92. LOC_REFERENCE:
  93. begin
  94. case hlcg.def2regtyp(paraloc^.def) of
  95. R_INTREGISTER,
  96. R_ADDRESSREGISTER:
  97. paraloc^.loc:=LOC_REGISTER;
  98. R_FPUREGISTER:
  99. paraloc^.loc:=LOC_FPUREGISTER;
  100. R_MMREGISTER:
  101. paraloc^.Loc:=LOC_MMREGISTER;
  102. else
  103. internalerror(2013012308);
  104. end;
  105. paraloc^.register:=hlcg.getregisterfordef(list,paraloc^.def);
  106. paraloc^.llvmvalueloc:=true;
  107. { paraloc^.reference overlaid this field, so zero it now
  108. that we turned it into a register location }
  109. paraloc^.shiftval:=0;
  110. end;
  111. LOC_REGISTER,
  112. LOC_FPUREGISTER,
  113. LOC_MMREGISTER:
  114. begin
  115. paraloc^.llvmvalueloc:=true;
  116. end;
  117. LOC_VOID:
  118. begin
  119. { for empty records, ensure these don't get a byval
  120. attribute }
  121. paraloc^.llvmvalueloc:=true;
  122. end;
  123. else
  124. internalerror(2014012302);
  125. end;
  126. end
  127. else
  128. begin
  129. { turn this paraloc into the "byval" parameter: at the llvm level,
  130. a pointer to the value that it should place on the stack (or
  131. passed in registers, in some cases) }
  132. paraloc^.llvmvalueloc:=false;
  133. paraloc^.def:=cpointerdef.getreusable_no_free(paraloc^.def);
  134. paraloc^.size:=def_cgsize(paraloc^.def);
  135. paraloc^.loc:=LOC_REGISTER;
  136. paraloc^.register:=hlcg.getaddressregister(list,paraloc^.def);
  137. paraloc^.shiftval:=0;
  138. { remove all other paralocs }
  139. while assigned(paraloc^.next) do
  140. begin
  141. nextloc:=paraloc^.next;
  142. paraloc^.next:=nextloc^.next;
  143. dispose(nextloc);
  144. end;
  145. end;
  146. paraloc^.llvmloc.loc:=paraloc^.loc;
  147. paraloc^.llvmloc.reg:=paraloc^.register;
  148. paraloc:=paraloc^.next;
  149. end;
  150. end;
  151. function tllvmparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
  152. begin
  153. result:=inherited create_paraloc_info(p, side);
  154. { on the calleeside, llvm declares the parameters similar to Pascal or C
  155. (a list of parameters and their types), but they correspond more
  156. closely to parameter locations than to parameters -> add names to the
  157. locations }
  158. if side=calleeside then
  159. add_llvm_callee_paraloc_names(p)
  160. end;
  161. function tllvmparamanager.get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  162. var
  163. paraloc: pcgparalocation;
  164. begin
  165. result:=inherited;
  166. paraloc:=result.location;
  167. repeat
  168. paraloc^.llvmvalueloc:=true;
  169. paraloc:=paraloc^.next;
  170. until not assigned(paraloc);
  171. end;
  172. { hp non-nil: parasym to check
  173. hp nil: function result
  174. }
  175. procedure tllvmparamanager.set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  176. var
  177. paraloc: PCGParaLocation;
  178. paralocnr: longint;
  179. begin
  180. paraloc:=hp.paraloc[calleeside].location;
  181. paralocnr:=0;
  182. repeat
  183. paraloc^.llvmloc.loc:=LOC_REFERENCE;
  184. paraloc^.llvmloc.sym:=current_asmdata.DefineAsmSymbol(llvmparaname(hp,paralocnr),AB_TEMP,AT_DATA,paraloc^.def);
  185. { byval: a pointer to a type that should actually be passed by
  186. value (e.g. a record that should be passed on the stack) }
  187. paraloc^.llvmvalueloc:=
  188. paramanager.push_addr_param(hp.varspez,hp.vardef,p.proccalloption) or
  189. not llvmbyvalparaloc(paraloc);
  190. paraloc:=paraloc^.next;
  191. inc(paralocnr);
  192. until not assigned(paraloc);
  193. end;
  194. procedure tllvmparamanager.add_llvm_callee_paraloc_names(p: tabstractprocdef);
  195. var
  196. paranr: longint;
  197. hp: tparavarsym;
  198. begin
  199. for paranr:=0 to p.paras.count-1 do
  200. begin
  201. hp:=tparavarsym(p.paras[paranr]);
  202. set_llvm_paraloc_name(p,hp,hp.paraloc[calleeside]);
  203. end;
  204. end;
  205. begin
  206. { replace the native parameter manager. Maybe this has to be moved to a
  207. procedure like the creations of the code generators, but possibly not since
  208. we still call the original paramanager }
  209. paramanager.free;
  210. paramanager:=tllvmparamanager.create;
  211. end.