parabase.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. Generic calling convention handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit parabase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globtype,
  23. cpubase,cgbase,cgutils;
  24. type
  25. TCGParaReference = record
  26. index : tregister;
  27. offset : aint;
  28. end;
  29. PCGParaLocation = ^TCGParaLocation;
  30. TCGParaLocation = record
  31. Next : PCGParaLocation;
  32. Size : TCGSize; { size of this location }
  33. Loc : TCGLoc;
  34. case TCGLoc of
  35. LOC_REFERENCE : (reference : TCGParaReference);
  36. LOC_FPUREGISTER,
  37. LOC_CFPUREGISTER,
  38. LOC_MMREGISTER,
  39. LOC_CMMREGISTER,
  40. LOC_REGISTER,
  41. LOC_CREGISTER : (register : tregister);
  42. end;
  43. TCGPara = object
  44. Location : PCGParalocation;
  45. Alignment : ShortInt;
  46. Size : TCGSize; { Size of the parameter included in all locations }
  47. IntSize: aint; { size of the total location in bytes }
  48. {$ifdef powerpc}
  49. composite: boolean; { under the AIX abi, how certain parameters are passed depends on whether they are composite or not }
  50. {$endif powerpc}
  51. constructor init;
  52. destructor done;
  53. procedure reset;
  54. function getcopy:tcgpara;
  55. procedure check_simple_location;
  56. function is_single_reference(l: pcgparalocation): boolean;
  57. function add_location:pcgparalocation;
  58. procedure get_location(var newloc:tlocation);
  59. end;
  60. tvarargsinfo = (
  61. va_uses_float_reg
  62. );
  63. tparalist = class(tlist)
  64. procedure SortParas;
  65. end;
  66. tvarargsparalist = class(tparalist)
  67. varargsinfo : set of tvarargsinfo;
  68. {$ifdef x86_64}
  69. { x86_64 requires %al to contain the no. SSE regs passed }
  70. mmregsused : longint;
  71. {$endif x86_64}
  72. end;
  73. implementation
  74. uses
  75. systems,verbose,
  76. symsym;
  77. {****************************************************************************
  78. TCGPara
  79. ****************************************************************************}
  80. constructor tcgpara.init;
  81. begin
  82. alignment:=0;
  83. size:=OS_NO;
  84. intsize:=0;
  85. location:=nil;
  86. {$ifdef powerpc}
  87. composite:=false;
  88. {$endif powerpc}
  89. end;
  90. destructor tcgpara.done;
  91. begin
  92. reset;
  93. end;
  94. procedure tcgpara.reset;
  95. var
  96. hlocation : pcgparalocation;
  97. begin
  98. while assigned(location) do
  99. begin
  100. hlocation:=location^.next;
  101. dispose(location);
  102. location:=hlocation;
  103. end;
  104. alignment:=0;
  105. size:=OS_NO;
  106. intsize:=0;
  107. {$ifdef powerpc}
  108. composite:=false;
  109. {$endif powerpc}
  110. end;
  111. function tcgpara.getcopy:tcgpara;
  112. var
  113. hlocation : pcgparalocation;
  114. begin
  115. result.init;
  116. while assigned(location) do
  117. begin
  118. hlocation:=result.add_location;
  119. hlocation^:=location^;
  120. hlocation^.next:=nil;
  121. location:=location^.next;
  122. end;
  123. result.alignment:=alignment;
  124. result.size:=size;
  125. result.intsize:=intsize;
  126. {$ifdef powerpc}
  127. result.composite:=composite;
  128. {$endif powerpc}
  129. end;
  130. function tcgpara.add_location:pcgparalocation;
  131. var
  132. prevlocation,
  133. hlocation : pcgparalocation;
  134. begin
  135. prevlocation:=nil;
  136. hlocation:=location;
  137. while assigned(hlocation) do
  138. begin
  139. prevlocation:=hlocation;
  140. hlocation:=hlocation^.next;
  141. end;
  142. new(hlocation);
  143. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  144. if assigned(prevlocation) then
  145. prevlocation^.next:=hlocation
  146. else
  147. location:=hlocation;
  148. result:=hlocation;
  149. end;
  150. function tcgpara.is_single_reference(l: pcgparalocation): boolean;
  151. begin
  152. result :=
  153. (l^.loc = LOC_REFERENCE) and
  154. not assigned(l^.next);
  155. end;
  156. procedure tcgpara.check_simple_location;
  157. begin
  158. if not assigned(location) then
  159. internalerror(200408161);
  160. if assigned(location^.next) then
  161. internalerror(200408162);
  162. end;
  163. procedure tcgpara.get_location(var newloc:tlocation);
  164. begin
  165. if not assigned(location) then
  166. internalerror(200408205);
  167. fillchar(newloc,sizeof(newloc),0);
  168. newloc.loc:=location^.loc;
  169. newloc.size:=size;
  170. case location^.loc of
  171. LOC_REGISTER :
  172. begin
  173. {$ifndef cpu64bit}
  174. if size in [OS_64,OS_S64] then
  175. begin
  176. if not assigned(location^.next) then
  177. internalerror(200408206);
  178. if (location^.next^.loc<>LOC_REGISTER) then
  179. internalerror(200408207);
  180. if (target_info.endian = ENDIAN_BIG) then
  181. begin
  182. newloc.register64.reghi:=location^.register;
  183. newloc.register64.reglo:=location^.next^.register;
  184. end
  185. else
  186. begin
  187. newloc.register64.reglo:=location^.register;
  188. newloc.register64.reghi:=location^.next^.register;
  189. end;
  190. end
  191. else
  192. {$endif}
  193. newloc.register:=location^.register;
  194. end;
  195. LOC_FPUREGISTER,
  196. LOC_MMREGISTER :
  197. newloc.register:=location^.register;
  198. LOC_REFERENCE :
  199. begin
  200. newloc.reference.base:=location^.reference.index;
  201. newloc.reference.offset:=location^.reference.offset;
  202. end;
  203. end;
  204. end;
  205. {****************************************************************************
  206. TParaList
  207. ****************************************************************************}
  208. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  209. var
  210. I1 : tparavarsym absolute Item1;
  211. I2 : tparavarsym absolute Item2;
  212. begin
  213. Result:=I1.paranr-I2.paranr;
  214. end;
  215. procedure TParaList.SortParas;
  216. begin
  217. Sort(@ParaNrCompare);
  218. end;
  219. end.
  220. {
  221. $Log$
  222. Revision 1.10 2005-01-30 21:51:57 jonas
  223. * fixed darwin cycle
  224. Revision 1.9 2005/01/18 22:19:20 peter
  225. * multiple location support for i386 a_param_ref
  226. * remove a_param_copy_ref for i386
  227. Revision 1.8 2005/01/10 21:50:05 jonas
  228. + support for passing records in registers under darwin
  229. * tcgpara now also has an intsize field, which contains the size in
  230. bytes of the whole parameter
  231. Revision 1.7 2005/01/07 16:22:54 florian
  232. + implemented abi compliant handling of strucutured functions results on sparc platform
  233. Revision 1.6 2004/11/22 22:01:19 peter
  234. * fixed varargs
  235. * replaced dynarray with tlist
  236. Revision 1.5 2004/11/15 23:35:31 peter
  237. * tparaitem removed, use tparavarsym instead
  238. * parameter order is now calculated from paranr value in tparavarsym
  239. Revision 1.4 2004/10/31 21:45:03 peter
  240. * generic tlocation
  241. * move tlocation to cgutils
  242. Revision 1.3 2004/10/10 20:22:53 peter
  243. * symtable allocation rewritten
  244. * loading of parameters to local temps/regs cleanup
  245. * regvar support for parameters
  246. * regvar support for staticsymtable (main body)
  247. Revision 1.2 2004/09/21 17:25:12 peter
  248. * paraloc branch merged
  249. Revision 1.1.2.2 2004/09/14 19:09:37 jonas
  250. * fixed typo in IE check
  251. Revision 1.1.2.1 2004/09/02 15:47:58 peter
  252. * missing file
  253. }