parabase.pas 14 KB

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