parabase.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. Alignment : ShortInt;
  45. Size : TCGSize; { Size of the parameter included in all locations }
  46. Location : PCGParalocation;
  47. constructor init;
  48. destructor done;
  49. procedure reset;
  50. function getcopy:tcgpara;
  51. procedure check_simple_location;
  52. function is_simple_reference:boolean;
  53. function add_location:pcgparalocation;
  54. procedure get_location(var newloc:tlocation);
  55. end;
  56. tvarargsinfo = (
  57. va_uses_float_reg
  58. );
  59. tparalist = class(tlist)
  60. procedure SortParas;
  61. end;
  62. tvarargsparalist = class(tparalist)
  63. varargsinfo : set of tvarargsinfo;
  64. {$ifdef x86_64}
  65. { x86_64 requires %al to contain the no. SSE regs passed }
  66. mmregsused : longint;
  67. {$endif x86_64}
  68. end;
  69. implementation
  70. uses
  71. systems,verbose,
  72. symsym;
  73. {****************************************************************************
  74. TCGPara
  75. ****************************************************************************}
  76. constructor tcgpara.init;
  77. begin
  78. alignment:=0;
  79. size:=OS_NO;
  80. location:=nil;
  81. end;
  82. destructor tcgpara.done;
  83. begin
  84. reset;
  85. end;
  86. procedure tcgpara.reset;
  87. var
  88. hlocation : pcgparalocation;
  89. begin
  90. while assigned(location) do
  91. begin
  92. hlocation:=location^.next;
  93. dispose(location);
  94. location:=hlocation;
  95. end;
  96. alignment:=0;
  97. size:=OS_NO;
  98. end;
  99. function tcgpara.getcopy:tcgpara;
  100. var
  101. hlocation : pcgparalocation;
  102. begin
  103. result.init;
  104. while assigned(location) do
  105. begin
  106. hlocation:=result.add_location;
  107. hlocation^:=location^;
  108. hlocation^.next:=nil;
  109. location:=location^.next;
  110. end;
  111. result.alignment:=alignment;
  112. result.size:=size;
  113. end;
  114. function tcgpara.add_location:pcgparalocation;
  115. var
  116. prevlocation,
  117. hlocation : pcgparalocation;
  118. begin
  119. prevlocation:=nil;
  120. hlocation:=location;
  121. while assigned(hlocation) do
  122. begin
  123. prevlocation:=hlocation;
  124. hlocation:=hlocation^.next;
  125. end;
  126. new(hlocation);
  127. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  128. if assigned(prevlocation) then
  129. prevlocation^.next:=hlocation
  130. else
  131. location:=hlocation;
  132. result:=hlocation;
  133. end;
  134. procedure tcgpara.check_simple_location;
  135. begin
  136. if not assigned(location) then
  137. internalerror(200408161);
  138. if assigned(location^.next) then
  139. internalerror(200408162);
  140. end;
  141. function tcgpara.is_simple_reference:boolean;
  142. begin
  143. if not assigned(location) then
  144. internalerror(200410102);
  145. {$ifdef powerpc}
  146. { Powerpc always needs a copy in a local temp }
  147. result:=false;
  148. {$else}
  149. result:=not assigned(location^.next) and
  150. (location^.loc=LOC_REFERENCE);
  151. {$endif}
  152. end;
  153. procedure tcgpara.get_location(var newloc:tlocation);
  154. begin
  155. if not assigned(location) then
  156. internalerror(200408205);
  157. fillchar(newloc,sizeof(newloc),0);
  158. newloc.loc:=location^.loc;
  159. newloc.size:=size;
  160. case location^.loc of
  161. LOC_REGISTER :
  162. begin
  163. {$ifndef cpu64bit}
  164. if size in [OS_64,OS_S64] then
  165. begin
  166. if not assigned(location^.next) then
  167. internalerror(200408206);
  168. if (location^.next^.loc<>LOC_REGISTER) then
  169. internalerror(200408207);
  170. if (target_info.endian = ENDIAN_BIG) then
  171. begin
  172. newloc.register64.reghi:=location^.register;
  173. newloc.register64.reglo:=location^.next^.register;
  174. end
  175. else
  176. begin
  177. newloc.register64.reglo:=location^.register;
  178. newloc.register64.reghi:=location^.next^.register;
  179. end;
  180. end
  181. else
  182. {$endif}
  183. newloc.register:=location^.register;
  184. end;
  185. LOC_FPUREGISTER,
  186. LOC_MMREGISTER :
  187. newloc.register:=location^.register;
  188. LOC_REFERENCE :
  189. begin
  190. newloc.reference.base:=location^.reference.index;
  191. newloc.reference.offset:=location^.reference.offset;
  192. end;
  193. end;
  194. end;
  195. {****************************************************************************
  196. TParaList
  197. ****************************************************************************}
  198. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  199. var
  200. I1 : tparavarsym absolute Item1;
  201. I2 : tparavarsym absolute Item2;
  202. begin
  203. Result:=I1.paranr-I2.paranr;
  204. end;
  205. procedure TParaList.SortParas;
  206. begin
  207. Sort(@ParaNrCompare);
  208. end;
  209. end.
  210. {
  211. $Log$
  212. Revision 1.6 2004-11-22 22:01:19 peter
  213. * fixed varargs
  214. * replaced dynarray with tlist
  215. Revision 1.5 2004/11/15 23:35:31 peter
  216. * tparaitem removed, use tparavarsym instead
  217. * parameter order is now calculated from paranr value in tparavarsym
  218. Revision 1.4 2004/10/31 21:45:03 peter
  219. * generic tlocation
  220. * move tlocation to cgutils
  221. Revision 1.3 2004/10/10 20:22:53 peter
  222. * symtable allocation rewritten
  223. * loading of parameters to local temps/regs cleanup
  224. * regvar support for parameters
  225. * regvar support for staticsymtable (main body)
  226. Revision 1.2 2004/09/21 17:25:12 peter
  227. * paraloc branch merged
  228. Revision 1.1.2.2 2004/09/14 19:09:37 jonas
  229. * fixed typo in IE check
  230. Revision 1.1.2.1 2004/09/02 15:47:58 peter
  231. * missing file
  232. }