parabase.pas 13 KB

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