parabase.pas 12 KB

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