parabase.pas 12 KB

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