parabase.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Generic calling convention handling
  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 parabase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,
  22. cpubase,cgbase,cgutils;
  23. type
  24. TCGParaReference = record
  25. index : tregister;
  26. offset : aint;
  27. end;
  28. PCGParaLocation = ^TCGParaLocation;
  29. TCGParaLocation = record
  30. Next : PCGParaLocation;
  31. Size : TCGSize; { size of this location }
  32. Loc : TCGLoc;
  33. case TCGLoc of
  34. LOC_REFERENCE : (reference : TCGParaReference);
  35. LOC_FPUREGISTER,
  36. LOC_CFPUREGISTER,
  37. LOC_MMREGISTER,
  38. LOC_CMMREGISTER,
  39. LOC_REGISTER,
  40. LOC_CREGISTER : (
  41. { The number of bits the value in the register must be shifted to the left before
  42. it can be stored to memory in the function prolog.
  43. This is used for passing OS_NO memory blocks less than register size and of "odd"
  44. (3, 5, 6, 7) size on big endian machines, so that small memory blocks passed via
  45. registers are properly aligned.
  46. E.g. the value $5544433 is passed in bits 40-63 of the register (others are zero),
  47. but they should actually be stored in the first bits of the stack location reserved
  48. for this value. So they have to be shifted left by this amount of bits before. }
  49. {$IFDEF POWERPC64}shiftval : byte;{$ENDIF POWERPC64}
  50. register : tregister);
  51. end;
  52. TCGPara = object
  53. Location : PCGParalocation;
  54. Alignment : ShortInt;
  55. Size : TCGSize; { Size of the parameter included in all locations }
  56. IntSize: aint; { size of the total location in bytes }
  57. {$ifdef powerpc}
  58. composite: boolean; { under the AIX abi, how certain parameters are passed depends on whether they are composite or not }
  59. {$endif powerpc}
  60. constructor init;
  61. destructor done;
  62. procedure reset;
  63. function getcopy:tcgpara;
  64. procedure check_simple_location;
  65. function add_location:pcgparalocation;
  66. procedure get_location(var newloc:tlocation);
  67. end;
  68. tvarargsinfo = (
  69. va_uses_float_reg
  70. );
  71. tparalist = class(TFPObjectList)
  72. procedure SortParas;
  73. end;
  74. tvarargsparalist = class(tparalist)
  75. varargsinfo : set of tvarargsinfo;
  76. {$ifdef x86_64}
  77. { x86_64 requires %al to contain the no. SSE regs passed }
  78. mmregsused : longint;
  79. {$endif x86_64}
  80. end;
  81. implementation
  82. uses
  83. systems,verbose,
  84. symsym;
  85. {****************************************************************************
  86. TCGPara
  87. ****************************************************************************}
  88. constructor tcgpara.init;
  89. begin
  90. alignment:=0;
  91. size:=OS_NO;
  92. intsize:=0;
  93. location:=nil;
  94. {$ifdef powerpc}
  95. composite:=false;
  96. {$endif powerpc}
  97. end;
  98. destructor tcgpara.done;
  99. begin
  100. reset;
  101. end;
  102. procedure tcgpara.reset;
  103. var
  104. hlocation : pcgparalocation;
  105. begin
  106. while assigned(location) do
  107. begin
  108. hlocation:=location^.next;
  109. dispose(location);
  110. location:=hlocation;
  111. end;
  112. alignment:=0;
  113. size:=OS_NO;
  114. intsize:=0;
  115. {$ifdef powerpc}
  116. composite:=false;
  117. {$endif powerpc}
  118. end;
  119. function tcgpara.getcopy:tcgpara;
  120. var
  121. hlocation : pcgparalocation;
  122. begin
  123. result.init;
  124. while assigned(location) do
  125. begin
  126. hlocation:=result.add_location;
  127. hlocation^:=location^;
  128. hlocation^.next:=nil;
  129. location:=location^.next;
  130. end;
  131. result.alignment:=alignment;
  132. result.size:=size;
  133. result.intsize:=intsize;
  134. {$ifdef powerpc}
  135. result.composite:=composite;
  136. {$endif powerpc}
  137. end;
  138. function tcgpara.add_location:pcgparalocation;
  139. var
  140. prevlocation,
  141. hlocation : pcgparalocation;
  142. begin
  143. prevlocation:=nil;
  144. hlocation:=location;
  145. while assigned(hlocation) do
  146. begin
  147. prevlocation:=hlocation;
  148. hlocation:=hlocation^.next;
  149. end;
  150. new(hlocation);
  151. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  152. if assigned(prevlocation) then
  153. prevlocation^.next:=hlocation
  154. else
  155. location:=hlocation;
  156. result:=hlocation;
  157. end;
  158. procedure tcgpara.check_simple_location;
  159. begin
  160. if not assigned(location) then
  161. internalerror(200408161);
  162. if assigned(location^.next) then
  163. internalerror(200408162);
  164. end;
  165. procedure tcgpara.get_location(var newloc:tlocation);
  166. begin
  167. if not assigned(location) then
  168. internalerror(200408205);
  169. fillchar(newloc,sizeof(newloc),0);
  170. newloc.loc:=location^.loc;
  171. newloc.size:=size;
  172. case location^.loc of
  173. LOC_REGISTER :
  174. begin
  175. {$ifndef cpu64bit}
  176. if size in [OS_64,OS_S64] then
  177. begin
  178. if not assigned(location^.next) then
  179. internalerror(200408206);
  180. if (location^.next^.loc<>LOC_REGISTER) then
  181. internalerror(200408207);
  182. if (target_info.endian = ENDIAN_BIG) then
  183. begin
  184. newloc.register64.reghi:=location^.register;
  185. newloc.register64.reglo:=location^.next^.register;
  186. end
  187. else
  188. begin
  189. newloc.register64.reglo:=location^.register;
  190. newloc.register64.reghi:=location^.next^.register;
  191. end;
  192. end
  193. else
  194. {$endif}
  195. newloc.register:=location^.register;
  196. end;
  197. LOC_FPUREGISTER,
  198. LOC_MMREGISTER :
  199. newloc.register:=location^.register;
  200. LOC_REFERENCE :
  201. begin
  202. newloc.reference.base:=location^.reference.index;
  203. newloc.reference.offset:=location^.reference.offset;
  204. end;
  205. end;
  206. end;
  207. {****************************************************************************
  208. TParaList
  209. ****************************************************************************}
  210. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  211. var
  212. I1 : tparavarsym absolute Item1;
  213. I2 : tparavarsym absolute Item2;
  214. begin
  215. Result:=longint(I1.paranr)-longint(I2.paranr);
  216. end;
  217. procedure TParaList.SortParas;
  218. begin
  219. Sort(@ParaNrCompare);
  220. end;
  221. end.