llvmpara.pas 8.1 KB

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