parabase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. Def : tdef;
  34. Loc : TCGLoc;
  35. case TCGLoc of
  36. LOC_REFERENCE : (reference : TCGParaReference);
  37. LOC_FPUREGISTER,
  38. LOC_CFPUREGISTER,
  39. LOC_MMREGISTER,
  40. LOC_CMMREGISTER,
  41. LOC_REGISTER,
  42. LOC_CREGISTER : (
  43. {
  44. * If shiftval > 0:
  45. The number of bits the value in the register must be shifted to the left before
  46. it can be stored to memory in the function prolog.
  47. This is used for passing OS_NO memory blocks less than register size and of "odd"
  48. (3, 5, 6, 7) size on big endian machines, so that small memory blocks passed via
  49. registers are properly aligned.
  50. E.g. the value $5544433 is passed in bits 40-63 of the register (others are zero),
  51. but they should actually be stored in the first bits of the stack location reserved
  52. for this value. So they have to be shifted left by this amount of bits before.
  53. * if shiftval < 0:
  54. Similar as above, but the shifting must always be done and
  55. 1) for all parameter sizes < regsize
  56. 2) on the caller side
  57. }
  58. shiftval : shortint;
  59. register : tregister);
  60. end;
  61. { TCGPara }
  62. TCGPara = object
  63. Def : tdef; { Type of the parameter }
  64. Location : PCGParalocation;
  65. IntSize : tcgint; { size of the total location in bytes }
  66. DefDeref : tderef;
  67. Alignment : ShortInt;
  68. Size : TCGSize; { Size of the parameter included in all locations }
  69. Temporary : boolean; { created on the fly, no permanent references exist to this somewhere that will cause it to be disposed }
  70. constructor init;
  71. destructor done;
  72. procedure reset;
  73. procedure resetiftemp; { reset if Temporary }
  74. function getcopy:tcgpara;
  75. procedure check_simple_location;
  76. function add_location:pcgparalocation;
  77. procedure get_location(var newloc:tlocation);
  78. function locations_count:integer;
  79. procedure buildderef;
  80. procedure deref;
  81. procedure ppuwrite(ppufile:tcompilerppufile);
  82. procedure ppuload(ppufile:tcompilerppufile);
  83. end;
  84. tvarargsinfo = (
  85. va_uses_float_reg
  86. );
  87. tparalist = class(TFPObjectList)
  88. procedure SortParas;
  89. end;
  90. tvarargsparalist = class(tparalist)
  91. varargsinfo : set of tvarargsinfo;
  92. {$ifdef x86_64}
  93. { x86_64 requires %al to contain the no. SSE regs passed }
  94. mmregsused : longint;
  95. {$endif x86_64}
  96. end;
  97. implementation
  98. uses
  99. systems,verbose,
  100. symsym;
  101. {****************************************************************************
  102. TCGPara
  103. ****************************************************************************}
  104. constructor tcgpara.init;
  105. begin
  106. alignment:=0;
  107. size:=OS_NO;
  108. intsize:=0;
  109. location:=nil;
  110. def:=nil;
  111. temporary:=false;
  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. end;
  131. procedure TCGPara.resetiftemp;
  132. begin
  133. if temporary then
  134. reset;
  135. end;
  136. function tcgpara.getcopy:tcgpara;
  137. var
  138. srcloc,hlocation : pcgparalocation;
  139. begin
  140. result.init;
  141. srcloc:=location;
  142. while assigned(srcloc) do
  143. begin
  144. hlocation:=result.add_location;
  145. hlocation^:=srcloc^;
  146. hlocation^.next:=nil;
  147. srcloc:=srcloc^.next;
  148. end;
  149. result.alignment:=alignment;
  150. result.size:=size;
  151. result.intsize:=intsize;
  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. function TCGPara.locations_count: integer;
  224. var
  225. hlocation: pcgparalocation;
  226. begin
  227. result:=0;
  228. hlocation:=location;
  229. while assigned(hlocation) do
  230. begin
  231. inc(result);
  232. hlocation:=hlocation^.next;
  233. end;
  234. end;
  235. procedure TCGPara.buildderef;
  236. begin
  237. defderef.build(def);
  238. end;
  239. procedure TCGPara.deref;
  240. begin
  241. def:=tdef(defderef.resolve);
  242. end;
  243. procedure TCGPara.ppuwrite(ppufile: tcompilerppufile);
  244. var
  245. hparaloc: PCGParaLocation;
  246. nparaloc: byte;
  247. begin
  248. ppufile.putbyte(byte(Alignment));
  249. ppufile.putbyte(ord(Size));
  250. ppufile.putaint(IntSize);
  251. ppufile.putderef(defderef);
  252. nparaloc:=0;
  253. hparaloc:=location;
  254. while assigned(hparaloc) do
  255. begin
  256. inc(nparaloc);
  257. hparaloc:=hparaloc^.Next;
  258. end;
  259. ppufile.putbyte(nparaloc);
  260. hparaloc:=location;
  261. while assigned(hparaloc) do
  262. begin
  263. ppufile.putbyte(byte(hparaloc^.Size));
  264. ppufile.putbyte(byte(hparaloc^.loc));
  265. case hparaloc^.loc of
  266. LOC_REFERENCE:
  267. begin
  268. ppufile.putlongint(longint(hparaloc^.reference.index));
  269. ppufile.putaint(hparaloc^.reference.offset);
  270. end;
  271. LOC_FPUREGISTER,
  272. LOC_CFPUREGISTER,
  273. LOC_MMREGISTER,
  274. LOC_CMMREGISTER,
  275. LOC_REGISTER,
  276. LOC_CREGISTER :
  277. begin
  278. ppufile.putbyte(hparaloc^.shiftval);
  279. ppufile.putlongint(longint(hparaloc^.register));
  280. end;
  281. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  282. or otherwise it hits the internalerror below. I don't know if this is
  283. the proper way to fix this, someone else with clue might want to take a
  284. look. The compiler cycles on the affected systems with this enabled. (KB) }
  285. LOC_VOID:
  286. begin end
  287. else
  288. internalerror(2010053115);
  289. end;
  290. hparaloc:=hparaloc^.next;
  291. end;
  292. end;
  293. procedure TCGPara.ppuload(ppufile: tcompilerppufile);
  294. var
  295. hparaloc: PCGParaLocation;
  296. nparaloc: byte;
  297. begin
  298. reset;
  299. Alignment:=shortint(ppufile.getbyte);
  300. Size:=TCgSize(ppufile.getbyte);
  301. IntSize:=ppufile.getaint;
  302. ppufile.getderef(defderef);
  303. nparaloc:=ppufile.getbyte;
  304. while nparaloc>0 do
  305. begin
  306. hparaloc:=add_location;
  307. hparaloc^.size:=TCGSize(ppufile.getbyte);
  308. hparaloc^.loc:=TCGLoc(ppufile.getbyte);
  309. case hparaloc^.loc of
  310. LOC_REFERENCE:
  311. begin
  312. hparaloc^.reference.index:=tregister(ppufile.getlongint);
  313. hparaloc^.reference.offset:=ppufile.getaint;
  314. end;
  315. LOC_FPUREGISTER,
  316. LOC_CFPUREGISTER,
  317. LOC_MMREGISTER,
  318. LOC_CMMREGISTER,
  319. LOC_REGISTER,
  320. LOC_CREGISTER :
  321. begin
  322. hparaloc^.shiftval:=ppufile.getbyte;
  323. hparaloc^.register:=tregister(ppufile.getlongint);
  324. end;
  325. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  326. or otherwise it hits the internalerror below. I don't know if this is
  327. the proper way to fix this, someone else with clue might want to take a
  328. look. The compiler cycles on the affected systems with this enabled. (KB) }
  329. LOC_VOID:
  330. begin end
  331. else
  332. internalerror(2010051301);
  333. end;
  334. dec(nparaloc);
  335. end;
  336. end;
  337. {****************************************************************************
  338. TParaList
  339. ****************************************************************************}
  340. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  341. var
  342. I1 : tparavarsym absolute Item1;
  343. I2 : tparavarsym absolute Item2;
  344. begin
  345. Result:=longint(I1.paranr)-longint(I2.paranr);
  346. end;
  347. procedure TParaList.SortParas;
  348. begin
  349. Sort(@ParaNrCompare);
  350. end;
  351. end.