parabase.pas 13 KB

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