parabase.pas 14 KB

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