llvmpara.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. begin
  66. inherited;
  67. end;
  68. function tllvmparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
  69. begin
  70. result:=inherited create_paraloc_info(p, side);
  71. { on the calleeside, llvm declares the parameters similar to Pascal or C
  72. (a list of parameters and their types), but they correspond more
  73. closely to parameter locations than to parameters -> add names to the
  74. locations }
  75. if side=calleeside then
  76. add_llvm_callee_paraloc_names(p)
  77. end;
  78. function tllvmparamanager.get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  79. var
  80. paraloc: pcgparalocation;
  81. begin
  82. result:=inherited;
  83. paraloc:=result.location;
  84. repeat
  85. paraloc^.llvmvalueloc:=true;
  86. paraloc:=paraloc^.next;
  87. until not assigned(paraloc);
  88. end;
  89. { hp non-nil: parasym to check
  90. hp nil: function result
  91. }
  92. procedure tllvmparamanager.set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
  93. var
  94. paraloc: PCGParaLocation;
  95. paralocnr: longint;
  96. begin
  97. paraloc:=hp.paraloc[calleeside].location;
  98. paralocnr:=0;
  99. repeat
  100. paraloc^.llvmloc.loc:=LOC_REFERENCE;
  101. paraloc^.llvmloc.sym:=current_asmdata.DefineAsmSymbol(llvmparaname(hp,paralocnr),AB_LOCAL,AT_DATA);
  102. { byval: a pointer to a type that should actually be passed by
  103. value (e.g. a record that should be passed on the stack) }
  104. paraloc^.llvmvalueloc:=
  105. paramanager.push_addr_param(hp.varspez,hp.vardef,p.proccalloption) or
  106. not llvmbyvalparaloc(paraloc);
  107. paraloc:=paraloc^.next;
  108. inc(paralocnr);
  109. until not assigned(paraloc);
  110. end;
  111. procedure tllvmparamanager.add_llvm_callee_paraloc_names(p: tabstractprocdef);
  112. var
  113. paranr: longint;
  114. hp: tparavarsym;
  115. begin
  116. for paranr:=0 to p.paras.count-1 do
  117. begin
  118. hp:=tparavarsym(p.paras[paranr]);
  119. set_llvm_paraloc_name(p,hp,hp.paraloc[calleeside]);
  120. end;
  121. end;
  122. begin
  123. { replace the native parameter manager. Maybe this has to be moved to a
  124. procedure like the creations of the code generators, but possibly not since
  125. we still call the original paramanager }
  126. paramanager.free;
  127. paramanager:=tllvmparamanager.create;
  128. end.