parabase.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 : (register : tregister);
  41. end;
  42. TCGPara = object
  43. Location : PCGParalocation;
  44. Alignment : ShortInt;
  45. Size : TCGSize; { Size of the parameter included in all locations }
  46. IntSize: aint; { size of the total location in bytes }
  47. {$ifdef powerpc}
  48. composite: boolean; { under the AIX abi, how certain parameters are passed depends on whether they are composite or not }
  49. {$endif powerpc}
  50. constructor init;
  51. destructor done;
  52. procedure reset;
  53. function getcopy:tcgpara;
  54. procedure check_simple_location;
  55. function add_location:pcgparalocation;
  56. procedure get_location(var newloc:tlocation);
  57. end;
  58. tvarargsinfo = (
  59. va_uses_float_reg
  60. );
  61. tparalist = class(tlist)
  62. procedure SortParas;
  63. end;
  64. tvarargsparalist = class(tparalist)
  65. varargsinfo : set of tvarargsinfo;
  66. {$ifdef x86_64}
  67. { x86_64 requires %al to contain the no. SSE regs passed }
  68. mmregsused : longint;
  69. {$endif x86_64}
  70. end;
  71. implementation
  72. uses
  73. systems,verbose,
  74. symsym;
  75. {****************************************************************************
  76. TCGPara
  77. ****************************************************************************}
  78. constructor tcgpara.init;
  79. begin
  80. alignment:=0;
  81. size:=OS_NO;
  82. intsize:=0;
  83. location:=nil;
  84. {$ifdef powerpc}
  85. composite:=false;
  86. {$endif powerpc}
  87. end;
  88. destructor tcgpara.done;
  89. begin
  90. reset;
  91. end;
  92. procedure tcgpara.reset;
  93. var
  94. hlocation : pcgparalocation;
  95. begin
  96. while assigned(location) do
  97. begin
  98. hlocation:=location^.next;
  99. dispose(location);
  100. location:=hlocation;
  101. end;
  102. alignment:=0;
  103. size:=OS_NO;
  104. intsize:=0;
  105. {$ifdef powerpc}
  106. composite:=false;
  107. {$endif powerpc}
  108. end;
  109. function tcgpara.getcopy:tcgpara;
  110. var
  111. hlocation : pcgparalocation;
  112. begin
  113. result.init;
  114. while assigned(location) do
  115. begin
  116. hlocation:=result.add_location;
  117. hlocation^:=location^;
  118. hlocation^.next:=nil;
  119. location:=location^.next;
  120. end;
  121. result.alignment:=alignment;
  122. result.size:=size;
  123. result.intsize:=intsize;
  124. {$ifdef powerpc}
  125. result.composite:=composite;
  126. {$endif powerpc}
  127. end;
  128. function tcgpara.add_location:pcgparalocation;
  129. var
  130. prevlocation,
  131. hlocation : pcgparalocation;
  132. begin
  133. prevlocation:=nil;
  134. hlocation:=location;
  135. while assigned(hlocation) do
  136. begin
  137. prevlocation:=hlocation;
  138. hlocation:=hlocation^.next;
  139. end;
  140. new(hlocation);
  141. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  142. if assigned(prevlocation) then
  143. prevlocation^.next:=hlocation
  144. else
  145. location:=hlocation;
  146. result:=hlocation;
  147. end;
  148. procedure tcgpara.check_simple_location;
  149. begin
  150. if not assigned(location) then
  151. internalerror(200408161);
  152. if assigned(location^.next) then
  153. internalerror(200408162);
  154. end;
  155. procedure tcgpara.get_location(var newloc:tlocation);
  156. begin
  157. if not assigned(location) then
  158. internalerror(200408205);
  159. fillchar(newloc,sizeof(newloc),0);
  160. newloc.loc:=location^.loc;
  161. newloc.size:=size;
  162. case location^.loc of
  163. LOC_REGISTER :
  164. begin
  165. {$ifndef cpu64bit}
  166. if size in [OS_64,OS_S64] then
  167. begin
  168. if not assigned(location^.next) then
  169. internalerror(200408206);
  170. if (location^.next^.loc<>LOC_REGISTER) then
  171. internalerror(200408207);
  172. if (target_info.endian = ENDIAN_BIG) then
  173. begin
  174. newloc.register64.reghi:=location^.register;
  175. newloc.register64.reglo:=location^.next^.register;
  176. end
  177. else
  178. begin
  179. newloc.register64.reglo:=location^.register;
  180. newloc.register64.reghi:=location^.next^.register;
  181. end;
  182. end
  183. else
  184. {$endif}
  185. newloc.register:=location^.register;
  186. end;
  187. LOC_FPUREGISTER,
  188. LOC_MMREGISTER :
  189. newloc.register:=location^.register;
  190. LOC_REFERENCE :
  191. begin
  192. newloc.reference.base:=location^.reference.index;
  193. newloc.reference.offset:=location^.reference.offset;
  194. end;
  195. end;
  196. end;
  197. {****************************************************************************
  198. TParaList
  199. ****************************************************************************}
  200. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  201. var
  202. I1 : tparavarsym absolute Item1;
  203. I2 : tparavarsym absolute Item2;
  204. begin
  205. Result:=I1.paranr-I2.paranr;
  206. end;
  207. procedure TParaList.SortParas;
  208. begin
  209. Sort(@ParaNrCompare);
  210. end;
  211. end.