parabase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. DefDeref : tderef;
  60. {$ifdef powerpc}
  61. composite: boolean; { under the AIX abi, how certain parameters are passed depends on whether they are composite or not }
  62. {$endif powerpc}
  63. constructor init;
  64. destructor done;
  65. procedure reset;
  66. function getcopy:tcgpara;
  67. procedure check_simple_location;
  68. function add_location:pcgparalocation;
  69. procedure get_location(var newloc:tlocation);
  70. procedure buildderef;
  71. procedure deref;
  72. procedure ppuwrite(ppufile:tcompilerppufile);
  73. procedure ppuload(ppufile:tcompilerppufile);
  74. end;
  75. tvarargsinfo = (
  76. va_uses_float_reg
  77. );
  78. tparalist = class(TFPObjectList)
  79. procedure SortParas;
  80. end;
  81. tvarargsparalist = class(tparalist)
  82. varargsinfo : set of tvarargsinfo;
  83. {$ifdef x86_64}
  84. { x86_64 requires %al to contain the no. SSE regs passed }
  85. mmregsused : longint;
  86. {$endif x86_64}
  87. end;
  88. implementation
  89. uses
  90. systems,verbose,
  91. symsym;
  92. {****************************************************************************
  93. TCGPara
  94. ****************************************************************************}
  95. constructor tcgpara.init;
  96. begin
  97. alignment:=0;
  98. size:=OS_NO;
  99. intsize:=0;
  100. location:=nil;
  101. def:=nil;
  102. {$ifdef powerpc}
  103. composite:=false;
  104. {$endif powerpc}
  105. end;
  106. destructor tcgpara.done;
  107. begin
  108. reset;
  109. end;
  110. procedure tcgpara.reset;
  111. var
  112. hlocation : pcgparalocation;
  113. begin
  114. while assigned(location) do
  115. begin
  116. hlocation:=location^.next;
  117. dispose(location);
  118. location:=hlocation;
  119. end;
  120. alignment:=0;
  121. size:=OS_NO;
  122. intsize:=0;
  123. {$ifdef powerpc}
  124. composite:=false;
  125. {$endif powerpc}
  126. end;
  127. function tcgpara.getcopy:tcgpara;
  128. var
  129. hlocation : pcgparalocation;
  130. begin
  131. result.init;
  132. while assigned(location) do
  133. begin
  134. hlocation:=result.add_location;
  135. hlocation^:=location^;
  136. hlocation^.next:=nil;
  137. location:=location^.next;
  138. end;
  139. result.alignment:=alignment;
  140. result.size:=size;
  141. result.intsize:=intsize;
  142. {$ifdef powerpc}
  143. result.composite:=composite;
  144. {$endif powerpc}
  145. end;
  146. function tcgpara.add_location:pcgparalocation;
  147. var
  148. prevlocation,
  149. hlocation : pcgparalocation;
  150. begin
  151. prevlocation:=nil;
  152. hlocation:=location;
  153. while assigned(hlocation) do
  154. begin
  155. prevlocation:=hlocation;
  156. hlocation:=hlocation^.next;
  157. end;
  158. new(hlocation);
  159. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  160. if assigned(prevlocation) then
  161. prevlocation^.next:=hlocation
  162. else
  163. location:=hlocation;
  164. result:=hlocation;
  165. end;
  166. procedure tcgpara.check_simple_location;
  167. begin
  168. if not assigned(location) then
  169. internalerror(200408161);
  170. if assigned(location^.next) then
  171. internalerror(200408162);
  172. end;
  173. procedure tcgpara.get_location(var newloc:tlocation);
  174. begin
  175. if not assigned(location) then
  176. internalerror(200408205);
  177. fillchar(newloc,sizeof(newloc),0);
  178. newloc.loc:=location^.loc;
  179. newloc.size:=size;
  180. case location^.loc of
  181. LOC_REGISTER :
  182. begin
  183. {$ifndef cpu64bitalu}
  184. if size in [OS_64,OS_S64] then
  185. begin
  186. if not assigned(location^.next) then
  187. internalerror(200408206);
  188. if (location^.next^.loc<>LOC_REGISTER) then
  189. internalerror(200408207);
  190. if (target_info.endian = ENDIAN_BIG) then
  191. begin
  192. newloc.register64.reghi:=location^.register;
  193. newloc.register64.reglo:=location^.next^.register;
  194. end
  195. else
  196. begin
  197. newloc.register64.reglo:=location^.register;
  198. newloc.register64.reghi:=location^.next^.register;
  199. end;
  200. end
  201. else
  202. {$endif}
  203. newloc.register:=location^.register;
  204. end;
  205. LOC_FPUREGISTER,
  206. LOC_MMREGISTER :
  207. newloc.register:=location^.register;
  208. LOC_REFERENCE :
  209. begin
  210. newloc.reference.base:=location^.reference.index;
  211. newloc.reference.offset:=location^.reference.offset;
  212. newloc.reference.alignment:=alignment;
  213. end;
  214. end;
  215. end;
  216. procedure TCGPara.buildderef;
  217. begin
  218. defderef.build(def);
  219. end;
  220. procedure TCGPara.deref;
  221. begin
  222. def:=tdef(defderef.resolve);
  223. end;
  224. procedure TCGPara.ppuwrite(ppufile: tcompilerppufile);
  225. var
  226. hparaloc: PCGParaLocation;
  227. nparaloc: byte;
  228. begin
  229. ppufile.putbyte(byte(Alignment));
  230. ppufile.putbyte(ord(Size));
  231. ppufile.putaint(IntSize);
  232. {$ifdef powerpc}
  233. ppufile.putbyte(byte(composite));
  234. {$endif}
  235. ppufile.putderef(defderef);
  236. nparaloc:=0;
  237. hparaloc:=location;
  238. while assigned(hparaloc) do
  239. begin
  240. inc(nparaloc);
  241. hparaloc:=hparaloc^.Next;
  242. end;
  243. ppufile.putbyte(nparaloc);
  244. hparaloc:=location;
  245. while assigned(hparaloc) do
  246. begin
  247. ppufile.putbyte(byte(hparaloc^.Size));
  248. ppufile.putbyte(byte(hparaloc^.loc));
  249. case hparaloc^.loc of
  250. LOC_REFERENCE:
  251. begin
  252. ppufile.putlongint(longint(hparaloc^.reference.index));
  253. ppufile.putaint(hparaloc^.reference.offset);
  254. end;
  255. LOC_FPUREGISTER,
  256. LOC_CFPUREGISTER,
  257. LOC_MMREGISTER,
  258. LOC_CMMREGISTER,
  259. LOC_REGISTER,
  260. LOC_CREGISTER :
  261. begin
  262. {$ifdef powerpc64}
  263. ppufile.putbyte(hparaloc^.shiftval);
  264. {$endif}
  265. ppufile.putlongint(longint(hparaloc^.register));
  266. end;
  267. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  268. or otherwise it hits the internalerror below. I don't know if this is
  269. the proper way to fix this, someone else with clue might want to take a
  270. look. The compiler cycles on the affected systems with this enabled. (KB) }
  271. LOC_VOID:
  272. begin end
  273. else
  274. internalerror(2010053115);
  275. end;
  276. hparaloc:=hparaloc^.next;
  277. end;
  278. end;
  279. procedure TCGPara.ppuload(ppufile: tcompilerppufile);
  280. var
  281. hparaloc: PCGParaLocation;
  282. nparaloc: byte;
  283. begin
  284. reset;
  285. Alignment:=shortint(ppufile.getbyte);
  286. Size:=TCgSize(ppufile.getbyte);
  287. IntSize:=ppufile.getaint;
  288. {$ifdef powerpc}
  289. composite:=boolean(ppufile.getbyte);
  290. {$endif}
  291. ppufile.getderef(defderef);
  292. nparaloc:=ppufile.getbyte;
  293. while nparaloc>0 do
  294. begin
  295. hparaloc:=add_location;
  296. hparaloc^.size:=TCGSize(ppufile.getbyte);
  297. hparaloc^.loc:=TCGLoc(ppufile.getbyte);
  298. case hparaloc^.loc of
  299. LOC_REFERENCE:
  300. begin
  301. hparaloc^.reference.index:=tregister(ppufile.getlongint);
  302. hparaloc^.reference.offset:=ppufile.getaint;
  303. end;
  304. LOC_FPUREGISTER,
  305. LOC_CFPUREGISTER,
  306. LOC_MMREGISTER,
  307. LOC_CMMREGISTER,
  308. LOC_REGISTER,
  309. LOC_CREGISTER :
  310. begin
  311. {$ifdef powerpc64}
  312. hparaloc^.shiftval:=ppufile.getbyte;
  313. {$endif}
  314. hparaloc^.register:=tregister(ppufile.getlongint);
  315. end;
  316. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  317. or otherwise it hits the internalerror below. I don't know if this is
  318. the proper way to fix this, someone else with clue might want to take a
  319. look. The compiler cycles on the affected systems with this enabled. (KB) }
  320. LOC_VOID:
  321. begin end
  322. else
  323. internalerror(2010051301);
  324. end;
  325. dec(nparaloc);
  326. end;
  327. end;
  328. {****************************************************************************
  329. TParaList
  330. ****************************************************************************}
  331. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  332. var
  333. I1 : tparavarsym absolute Item1;
  334. I2 : tparavarsym absolute Item2;
  335. begin
  336. Result:=longint(I1.paranr)-longint(I2.paranr);
  337. end;
  338. procedure TParaList.SortParas;
  339. begin
  340. Sort(@ParaNrCompare);
  341. end;
  342. end.