parabase.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_simple_reference: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. procedure tcgpara.check_simple_location;
  151. begin
  152. if not assigned(location) then
  153. internalerror(200408161);
  154. if assigned(location^.next) then
  155. internalerror(200408162);
  156. end;
  157. function tcgpara.is_simple_reference:boolean;
  158. begin
  159. if not assigned(location) then
  160. internalerror(200410102);
  161. {$ifdef powerpc}
  162. { Powerpc always needs a copy in a local temp }
  163. result:=false;
  164. {$else}
  165. result:=not assigned(location^.next) and
  166. (location^.loc=LOC_REFERENCE);
  167. {$endif}
  168. end;
  169. procedure tcgpara.get_location(var newloc:tlocation);
  170. begin
  171. if not assigned(location) then
  172. internalerror(200408205);
  173. fillchar(newloc,sizeof(newloc),0);
  174. newloc.loc:=location^.loc;
  175. newloc.size:=size;
  176. case location^.loc of
  177. LOC_REGISTER :
  178. begin
  179. {$ifndef cpu64bit}
  180. if size in [OS_64,OS_S64] then
  181. begin
  182. if not assigned(location^.next) then
  183. internalerror(200408206);
  184. if (location^.next^.loc<>LOC_REGISTER) then
  185. internalerror(200408207);
  186. if (target_info.endian = ENDIAN_BIG) then
  187. begin
  188. newloc.register64.reghi:=location^.register;
  189. newloc.register64.reglo:=location^.next^.register;
  190. end
  191. else
  192. begin
  193. newloc.register64.reglo:=location^.register;
  194. newloc.register64.reghi:=location^.next^.register;
  195. end;
  196. end
  197. else
  198. {$endif}
  199. newloc.register:=location^.register;
  200. end;
  201. LOC_FPUREGISTER,
  202. LOC_MMREGISTER :
  203. newloc.register:=location^.register;
  204. LOC_REFERENCE :
  205. begin
  206. newloc.reference.base:=location^.reference.index;
  207. newloc.reference.offset:=location^.reference.offset;
  208. end;
  209. end;
  210. end;
  211. {****************************************************************************
  212. TParaList
  213. ****************************************************************************}
  214. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  215. var
  216. I1 : tparavarsym absolute Item1;
  217. I2 : tparavarsym absolute Item2;
  218. begin
  219. Result:=I1.paranr-I2.paranr;
  220. end;
  221. procedure TParaList.SortParas;
  222. begin
  223. Sort(@ParaNrCompare);
  224. end;
  225. end.
  226. {
  227. $Log$
  228. Revision 1.8 2005-01-10 21:50:05 jonas
  229. + support for passing records in registers under darwin
  230. * tcgpara now also has an intsize field, which contains the size in
  231. bytes of the whole parameter
  232. Revision 1.7 2005/01/07 16:22:54 florian
  233. + implemented abi compliant handling of strucutured functions results on sparc platform
  234. Revision 1.6 2004/11/22 22:01:19 peter
  235. * fixed varargs
  236. * replaced dynarray with tlist
  237. Revision 1.5 2004/11/15 23:35:31 peter
  238. * tparaitem removed, use tparavarsym instead
  239. * parameter order is now calculated from paranr value in tparavarsym
  240. Revision 1.4 2004/10/31 21:45:03 peter
  241. * generic tlocation
  242. * move tlocation to cgutils
  243. Revision 1.3 2004/10/10 20:22:53 peter
  244. * symtable allocation rewritten
  245. * loading of parameters to local temps/regs cleanup
  246. * regvar support for parameters
  247. * regvar support for staticsymtable (main body)
  248. Revision 1.2 2004/09/21 17:25:12 peter
  249. * paraloc branch merged
  250. Revision 1.1.2.2 2004/09/14 19:09:37 jonas
  251. * fixed typo in IE check
  252. Revision 1.1.2.1 2004/09/02 15:47:58 peter
  253. * missing file
  254. }