parabase.pas 12 KB

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