llvmpara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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,cgbase,
  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 getcgtempparaloc(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 create_varargs_paraloc_info(p: tabstractprocdef; side: tcallercallee; varargspara: tvarargsparalist): longint; override;
  43. function get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara; override;
  44. private
  45. procedure create_paraloc_info_internllvm(p: tabstractprocdef; side: tcallercallee);
  46. procedure set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  47. procedure add_llvm_callee_paraloc_names(p: tabstractprocdef);
  48. procedure reducetosingleregparaloc(paraloc: PCGParaLocation; def: tdef; reg: tregister);
  49. procedure reduceparalocs(p: tabstractprocdef; side: tcallercallee; paras: tparalist);
  50. end;
  51. implementation
  52. uses
  53. verbose,
  54. aasmbase,
  55. llvmsym,
  56. paramgr,defutil,llvmdef,
  57. cgutils,tgobj,hlcgobj;
  58. { tllvmparamanager }
  59. procedure tllvmparamanager.getcgtempparaloc(list: TAsmList; pd: tabstractprocdef; nr: longint; var cgpara: tcgpara);
  60. begin
  61. if (nr<1) or (nr>pd.paras.count) then
  62. InternalError(2015040401);
  63. pd.init_paraloc_info(callerside);
  64. createtempparaloc(list,pd.proccalloption,tparavarsym(pd.paras[nr-1]),true,cgpara);
  65. end;
  66. function tllvmparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  67. begin
  68. { we can use the paraloc on the callee side if the SSA property is
  69. guaranteed, i.e., if it is a constant location (and if it's not been
  70. split up into multiple locations for ABI reasons). We can't deduce that
  71. from the paraloc though, we need the parasym for that. Potential
  72. future optimisation, although llvm will probably optimise away the
  73. temps we create anyway }
  74. result:=false;
  75. end;
  76. procedure tllvmparamanager.reducetosingleregparaloc(paraloc: PCGParaLocation; def: tdef; reg: tregister);
  77. var
  78. nextloc: pcgparalocation;
  79. begin
  80. paraloc^.def:=def;
  81. paraloc^.size:=def_cgsize(def);
  82. paraloc^.register:=reg;
  83. paraloc^.shiftval:=0;
  84. { remove all other paralocs }
  85. while assigned(paraloc^.next) do
  86. begin
  87. nextloc:=paraloc^.next;
  88. paraloc^.next:=nextloc^.next;
  89. dispose(nextloc);
  90. end;
  91. end;
  92. procedure tllvmparamanager.reduceparalocs(p: tabstractprocdef; side: tcallercallee; paras: tparalist);
  93. var
  94. paranr: longint;
  95. hp: tparavarsym;
  96. paraloc: PCGParaLocation;
  97. begin
  98. for paranr:=0 to paras.count-1 do
  99. begin
  100. hp:=tparavarsym(paras[paranr]);
  101. paraloc:=hp.paraloc[side].location;
  102. if assigned(paraloc) and
  103. assigned(paraloc^.next) and
  104. (hp.paraloc[side].def.typ in [orddef,enumdef,floatdef]) then
  105. begin
  106. if not(paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER]) then
  107. internalerror(2019011902);
  108. reducetosingleregparaloc(paraloc,hp.paraloc[side].def,paraloc^.register);
  109. end;
  110. end;
  111. end;
  112. procedure tllvmparamanager.createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara);
  113. var
  114. paraloc: pcgparalocation;
  115. paralocdef: tdef;
  116. begin
  117. inherited;
  118. paraloc:=cgpara.location;
  119. { No need to set paraloc^.llvmloc.*, these are not used/needed for temp
  120. paralocs }
  121. while assigned(paraloc) do
  122. begin
  123. if vo_is_funcret in parasym.varoptions then
  124. paraloc^.retvalloc:=true;
  125. { ordinal parameters must be passed as a single paraloc }
  126. if (cgpara.def.typ in [orddef,enumdef,floatdef]) and
  127. assigned(paraloc^.next) then
  128. begin
  129. paraloc^.loc:=LOC_REGISTER;
  130. reducetosingleregparaloc(paraloc,cgpara.def,hlcg.getintregister(list,cgpara.def));
  131. end;
  132. { varargs parameters do not have a parasym.owner, but they're always
  133. by value }
  134. if (assigned(parasym.owner) and
  135. paramanager.push_addr_param(parasym.varspez,parasym.vardef,tabstractprocdef(parasym.owner.defowner).proccalloption)) or
  136. not llvmbyvalparaloc(paraloc) then
  137. begin
  138. case paraloc^.loc of
  139. LOC_REFERENCE:
  140. begin
  141. case hlcg.def2regtyp(paraloc^.def) of
  142. R_INTREGISTER,
  143. R_ADDRESSREGISTER:
  144. paraloc^.loc:=LOC_REGISTER;
  145. R_FPUREGISTER:
  146. paraloc^.loc:=LOC_FPUREGISTER;
  147. R_MMREGISTER:
  148. paraloc^.Loc:=LOC_MMREGISTER;
  149. else
  150. internalerror(2013012308);
  151. end;
  152. paraloc^.register:=hlcg.getregisterfordef(list,paraloc^.def);
  153. paraloc^.llvmvalueloc:=true;
  154. { paraloc^.reference overlaid this field, so zero it now
  155. that we turned it into a register location }
  156. paraloc^.shiftval:=0;
  157. end;
  158. LOC_REGISTER,
  159. LOC_FPUREGISTER,
  160. LOC_MMREGISTER:
  161. begin
  162. paraloc^.llvmvalueloc:=true;
  163. end;
  164. LOC_VOID:
  165. begin
  166. { for empty records, ensure these don't get a byval
  167. attribute }
  168. paraloc^.llvmvalueloc:=true;
  169. end;
  170. else
  171. internalerror(2014012302);
  172. end;
  173. end
  174. else
  175. begin
  176. { turn this paraloc into the "byval" parameter: at the llvm level,
  177. a pointer to the value that it should place on the stack (or
  178. passed in registers, in some cases) }
  179. paraloc^.llvmvalueloc:=false;
  180. paraloc^.loc:=LOC_REGISTER;
  181. paralocdef:=cpointerdef.getreusable_no_free(paraloc^.def);
  182. reducetosingleregparaloc(paraloc,paralocdef,hlcg.getaddressregister(list,paralocdef));
  183. end;
  184. paraloc^.llvmloc.loc:=paraloc^.loc;
  185. paraloc^.llvmloc.reg:=paraloc^.register;
  186. paraloc:=paraloc^.next;
  187. end;
  188. end;
  189. function tllvmparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
  190. begin
  191. result:=inherited;
  192. create_paraloc_info_internllvm(p,side);
  193. end;
  194. function tllvmparamanager.create_varargs_paraloc_info(p: tabstractprocdef; side: tcallercallee; varargspara: tvarargsparalist): longint;
  195. begin
  196. result:=inherited;
  197. create_paraloc_info_internllvm(p,side);
  198. if assigned(varargspara) then
  199. reduceparalocs(p,side,varargspara);
  200. end;
  201. function tllvmparamanager.get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  202. var
  203. paraloc: pcgparalocation;
  204. begin
  205. result:=inherited;
  206. paraloc:=result.location;
  207. repeat
  208. paraloc^.llvmvalueloc:=true;
  209. paraloc:=paraloc^.next;
  210. until not assigned(paraloc);
  211. paraloc:=result.location;
  212. if assigned(paraloc^.next) and
  213. (result.def.typ in [orddef,enumdef,floatdef]) and
  214. ((side=callerside) or
  215. not(po_assembler in p.procoptions)) then
  216. begin
  217. if not(paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER]) then
  218. internalerror(2019011902);
  219. reducetosingleregparaloc(paraloc,result.def,paraloc^.register);
  220. end;
  221. end;
  222. procedure tllvmparamanager.create_paraloc_info_internllvm(p: tabstractprocdef; side: tcallercallee);
  223. begin
  224. { on the calleeside, llvm declares the parameters similar to Pascal or C
  225. (a list of parameters and their types), but they correspond more
  226. closely to parameter locations than to parameters -> add names to the
  227. locations }
  228. if (side=calleeside) and
  229. not(po_assembler in p.procoptions) then
  230. begin
  231. add_llvm_callee_paraloc_names(p);
  232. reduceparalocs(p,side,p.paras);
  233. end
  234. else if side=callerside then
  235. begin
  236. reduceparalocs(p,side,p.paras);
  237. end;
  238. end;
  239. { hp non-nil: parasym to check
  240. hp nil: function result
  241. }
  242. procedure tllvmparamanager.set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  243. var
  244. paraloc: PCGParaLocation;
  245. paralocnr: longint;
  246. begin
  247. paraloc:=hp.paraloc[calleeside].location;
  248. paralocnr:=0;
  249. repeat
  250. paraloc^.llvmloc.loc:=LOC_REFERENCE;
  251. paraloc^.llvmloc.sym:=current_asmdata.DefineAsmSymbol(llvmparaname(hp,paralocnr),AB_TEMP,AT_DATA,paraloc^.def);
  252. { byval: a pointer to a type that should actually be passed by
  253. value (e.g. a record that should be passed on the stack) }
  254. paraloc^.llvmvalueloc:=
  255. paramanager.push_addr_param(hp.varspez,hp.vardef,p.proccalloption) or
  256. not llvmbyvalparaloc(paraloc);
  257. paraloc:=paraloc^.next;
  258. inc(paralocnr);
  259. until not assigned(paraloc);
  260. end;
  261. procedure tllvmparamanager.add_llvm_callee_paraloc_names(p: tabstractprocdef);
  262. var
  263. paranr: longint;
  264. hp: tparavarsym;
  265. begin
  266. for paranr:=0 to p.paras.count-1 do
  267. begin
  268. hp:=tparavarsym(p.paras[paranr]);
  269. set_llvm_paraloc_name(p,hp,hp.paraloc[calleeside]);
  270. end;
  271. end;
  272. begin
  273. if not assigned(paramanager) then
  274. begin
  275. writeln('Internalerror 2018052006');
  276. halt(1);
  277. end;
  278. { replace the native parameter manager. Maybe this has to be moved to a
  279. procedure like the creations of the code generators, but possibly not since
  280. we still call the original paramanager }
  281. paramanager.free;
  282. paramanager:=tllvmparamanager.create;
  283. end.