parabase.pas 13 KB

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