parabase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. symtype, ppu;
  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 : (
  42. { The number of bits the value in the register must be shifted to the left before
  43. it can be stored to memory in the function prolog.
  44. This is used for passing OS_NO memory blocks less than register size and of "odd"
  45. (3, 5, 6, 7) size on big endian machines, so that small memory blocks passed via
  46. registers are properly aligned.
  47. E.g. the value $5544433 is passed in bits 40-63 of the register (others are zero),
  48. but they should actually be stored in the first bits of the stack location reserved
  49. for this value. So they have to be shifted left by this amount of bits before. }
  50. {$IFDEF POWERPC64}shiftval : byte;{$ENDIF POWERPC64}
  51. register : tregister);
  52. end;
  53. TCGPara = object
  54. Location : PCGParalocation;
  55. IntSize : tcgint; { size of the total location in bytes }
  56. Alignment : ShortInt;
  57. Size : TCGSize; { Size of the parameter included in all locations }
  58. Def : tdef; { Type of the parameter }
  59. {$ifdef powerpc}
  60. composite: boolean; { under the AIX abi, how certain parameters are passed depends on whether they are composite or not }
  61. {$endif powerpc}
  62. constructor init;
  63. destructor done;
  64. procedure reset;
  65. function getcopy:tcgpara;
  66. procedure check_simple_location;
  67. function add_location:pcgparalocation;
  68. procedure get_location(var newloc:tlocation);
  69. procedure ppuwrite(ppufile:tcompilerppufile);
  70. procedure ppuload(ppufile:tcompilerppufile);
  71. end;
  72. tvarargsinfo = (
  73. va_uses_float_reg
  74. );
  75. tparalist = class(TFPObjectList)
  76. procedure SortParas;
  77. end;
  78. tvarargsparalist = class(tparalist)
  79. varargsinfo : set of tvarargsinfo;
  80. {$ifdef x86_64}
  81. { x86_64 requires %al to contain the no. SSE regs passed }
  82. mmregsused : longint;
  83. {$endif x86_64}
  84. end;
  85. implementation
  86. uses
  87. systems,verbose,
  88. symsym;
  89. {****************************************************************************
  90. TCGPara
  91. ****************************************************************************}
  92. constructor tcgpara.init;
  93. begin
  94. alignment:=0;
  95. size:=OS_NO;
  96. intsize:=0;
  97. location:=nil;
  98. {$ifdef powerpc}
  99. composite:=false;
  100. {$endif powerpc}
  101. end;
  102. destructor tcgpara.done;
  103. begin
  104. reset;
  105. end;
  106. procedure tcgpara.reset;
  107. var
  108. hlocation : pcgparalocation;
  109. begin
  110. while assigned(location) do
  111. begin
  112. hlocation:=location^.next;
  113. dispose(location);
  114. location:=hlocation;
  115. end;
  116. alignment:=0;
  117. size:=OS_NO;
  118. intsize:=0;
  119. {$ifdef powerpc}
  120. composite:=false;
  121. {$endif powerpc}
  122. end;
  123. function tcgpara.getcopy:tcgpara;
  124. var
  125. hlocation : pcgparalocation;
  126. begin
  127. result.init;
  128. while assigned(location) do
  129. begin
  130. hlocation:=result.add_location;
  131. hlocation^:=location^;
  132. hlocation^.next:=nil;
  133. location:=location^.next;
  134. end;
  135. result.alignment:=alignment;
  136. result.size:=size;
  137. result.intsize:=intsize;
  138. {$ifdef powerpc}
  139. result.composite:=composite;
  140. {$endif powerpc}
  141. end;
  142. function tcgpara.add_location:pcgparalocation;
  143. var
  144. prevlocation,
  145. hlocation : pcgparalocation;
  146. begin
  147. prevlocation:=nil;
  148. hlocation:=location;
  149. while assigned(hlocation) do
  150. begin
  151. prevlocation:=hlocation;
  152. hlocation:=hlocation^.next;
  153. end;
  154. new(hlocation);
  155. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  156. if assigned(prevlocation) then
  157. prevlocation^.next:=hlocation
  158. else
  159. location:=hlocation;
  160. result:=hlocation;
  161. end;
  162. procedure tcgpara.check_simple_location;
  163. begin
  164. if not assigned(location) then
  165. internalerror(200408161);
  166. if assigned(location^.next) then
  167. internalerror(200408162);
  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 cpu64bitalu}
  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. newloc.reference.alignment:=alignment;
  209. end;
  210. end;
  211. end;
  212. procedure TCGPara.ppuwrite(ppufile: tcompilerppufile);
  213. var
  214. hparaloc: PCGParaLocation;
  215. nparaloc: byte;
  216. begin
  217. ppufile.putbyte(byte(Alignment));
  218. ppufile.putbyte(ord(Size));
  219. ppufile.putaint(IntSize);
  220. {$ifdef powerpc}
  221. ppufile.putbyte(byte(composite));
  222. {$endif}
  223. nparaloc:=0;
  224. hparaloc:=location;
  225. while assigned(hparaloc) do
  226. begin
  227. inc(nparaloc);
  228. hparaloc:=hparaloc^.Next;
  229. end;
  230. ppufile.putbyte(nparaloc);
  231. hparaloc:=location;
  232. while assigned(hparaloc) do
  233. begin
  234. ppufile.putbyte(byte(hparaloc^.Size));
  235. ppufile.putbyte(byte(hparaloc^.loc));
  236. case hparaloc^.loc of
  237. LOC_REFERENCE:
  238. begin
  239. ppufile.putlongint(longint(hparaloc^.reference.index));
  240. ppufile.putaint(hparaloc^.reference.offset);
  241. end;
  242. LOC_FPUREGISTER,
  243. LOC_CFPUREGISTER,
  244. LOC_MMREGISTER,
  245. LOC_CMMREGISTER,
  246. LOC_REGISTER,
  247. LOC_CREGISTER :
  248. begin
  249. {$ifdef powerpc64}
  250. ppufile.putbyte(hparaloc^.shiftval);
  251. {$endif}
  252. ppufile.putlongint(longint(hparaloc^.register));
  253. end;
  254. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  255. or otherwise it hits the internalerror below. I don't know if this is
  256. the proper way to fix this, someone else with clue might want to take a
  257. look. The compiler cycles on the affected systems with this enabled. (KB) }
  258. LOC_VOID:
  259. begin end
  260. else
  261. internalerror(2010053115);
  262. end;
  263. hparaloc:=hparaloc^.next;
  264. end;
  265. end;
  266. procedure TCGPara.ppuload(ppufile: tcompilerppufile);
  267. var
  268. hparaloc: PCGParaLocation;
  269. nparaloc: byte;
  270. begin
  271. reset;
  272. Alignment:=shortint(ppufile.getbyte);
  273. Size:=TCgSize(ppufile.getbyte);
  274. IntSize:=ppufile.getaint;
  275. {$ifdef powerpc}
  276. composite:=boolean(ppufile.getbyte);
  277. {$endif}
  278. nparaloc:=ppufile.getbyte;
  279. while nparaloc>0 do
  280. begin
  281. hparaloc:=add_location;
  282. hparaloc^.size:=TCGSize(ppufile.getbyte);
  283. hparaloc^.loc:=TCGLoc(ppufile.getbyte);
  284. case hparaloc^.loc of
  285. LOC_REFERENCE:
  286. begin
  287. hparaloc^.reference.index:=tregister(ppufile.getlongint);
  288. hparaloc^.reference.offset:=ppufile.getaint;
  289. end;
  290. LOC_FPUREGISTER,
  291. LOC_CFPUREGISTER,
  292. LOC_MMREGISTER,
  293. LOC_CMMREGISTER,
  294. LOC_REGISTER,
  295. LOC_CREGISTER :
  296. begin
  297. {$ifdef powerpc64}
  298. hparaloc^.shiftval:=ppufile.getbyte;
  299. {$endif}
  300. hparaloc^.register:=tregister(ppufile.getlongint);
  301. end;
  302. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  303. or otherwise it hits the internalerror below. I don't know if this is
  304. the proper way to fix this, someone else with clue might want to take a
  305. look. The compiler cycles on the affected systems with this enabled. (KB) }
  306. LOC_VOID:
  307. begin end
  308. else
  309. internalerror(2010051301);
  310. end;
  311. dec(nparaloc);
  312. end;
  313. end;
  314. {****************************************************************************
  315. TParaList
  316. ****************************************************************************}
  317. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  318. var
  319. I1 : tparavarsym absolute Item1;
  320. I2 : tparavarsym absolute Item2;
  321. begin
  322. Result:=longint(I1.paranr)-longint(I2.paranr);
  323. end;
  324. procedure TParaList.SortParas;
  325. begin
  326. Sort(@ParaNrCompare);
  327. end;
  328. end.