2
0

parabase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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: tcgint);
  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;
  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. end;
  191. function tcgpara.add_location:pcgparalocation;
  192. var
  193. prevlocation,
  194. hlocation : pcgparalocation;
  195. begin
  196. prevlocation:=nil;
  197. hlocation:=location;
  198. while assigned(hlocation) do
  199. begin
  200. prevlocation:=hlocation;
  201. hlocation:=hlocation^.next;
  202. end;
  203. new(hlocation);
  204. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  205. if assigned(prevlocation) then
  206. prevlocation^.next:=hlocation
  207. else
  208. location:=hlocation;
  209. result:=hlocation;
  210. end;
  211. procedure tcgpara.check_simple_location;
  212. begin
  213. if not assigned(location) then
  214. internalerror(200408161);
  215. if assigned(location^.next) then
  216. internalerror(200408162);
  217. end;
  218. procedure tcgpara.get_location(var newloc:tlocation);
  219. begin
  220. if not assigned(location) then
  221. internalerror(200408205);
  222. fillchar(newloc,sizeof(newloc),0);
  223. newloc.loc:=location^.loc;
  224. newloc.size:=size;
  225. case location^.loc of
  226. LOC_REGISTER :
  227. begin
  228. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  229. if size in [OS_64,OS_S64] then
  230. begin
  231. if not assigned(location^.next) then
  232. internalerror(200408206);
  233. if (location^.next^.loc<>LOC_REGISTER) then
  234. internalerror(200408207);
  235. if (target_info.endian = ENDIAN_BIG) then
  236. begin
  237. newloc.register64.reghi:=location^.register;
  238. newloc.register64.reglo:=location^.next^.register;
  239. end
  240. else
  241. begin
  242. newloc.register64.reglo:=location^.register;
  243. newloc.register64.reghi:=location^.next^.register;
  244. end;
  245. end
  246. else
  247. {$endif}
  248. newloc.register:=location^.register;
  249. end;
  250. LOC_FPUREGISTER,
  251. LOC_MMREGISTER :
  252. newloc.register:=location^.register;
  253. LOC_REFERENCE :
  254. begin
  255. newloc.reference.base:=location^.reference.index;
  256. newloc.reference.offset:=location^.reference.offset;
  257. newloc.reference.alignment:=alignment;
  258. end;
  259. end;
  260. end;
  261. function TCGPara.locations_count: integer;
  262. var
  263. hlocation: pcgparalocation;
  264. begin
  265. result:=0;
  266. hlocation:=location;
  267. while assigned(hlocation) do
  268. begin
  269. inc(result);
  270. hlocation:=hlocation^.next;
  271. end;
  272. end;
  273. function TCGPara.isempty: boolean;
  274. var
  275. hlocation: pcgparalocation;
  276. begin
  277. { can happen if e.g. [] is passed to a cdecl varargs para }
  278. if not assigned(def) then
  279. exit(true);
  280. if is_array_of_const(def) then
  281. exit(false);
  282. hlocation:=location;
  283. while assigned(hlocation) do
  284. begin
  285. if hlocation^.Loc<>LOC_VOID then
  286. exit(false);
  287. hlocation:=hlocation^.next;
  288. end;
  289. result:=true;
  290. end;
  291. procedure TCGPara.buildderef;
  292. begin
  293. defderef.build(def);
  294. end;
  295. procedure TCGPara.deref;
  296. begin
  297. def:=tdef(defderef.resolve);
  298. end;
  299. procedure TCGPara.ppuwrite(ppufile: tcompilerppufile);
  300. var
  301. hparaloc: PCGParaLocation;
  302. nparaloc: byte;
  303. begin
  304. ppufile.putbyte(byte(Alignment));
  305. ppufile.putbyte(ord(Size));
  306. ppufile.putaint(IntSize);
  307. ppufile.putderef(defderef);
  308. nparaloc:=0;
  309. hparaloc:=location;
  310. while assigned(hparaloc) do
  311. begin
  312. inc(nparaloc);
  313. hparaloc:=hparaloc^.Next;
  314. end;
  315. ppufile.putbyte(nparaloc);
  316. hparaloc:=location;
  317. while assigned(hparaloc) do
  318. begin
  319. ppufile.putbyte(byte(hparaloc^.Size));
  320. ppufile.putbyte(byte(hparaloc^.loc));
  321. case hparaloc^.loc of
  322. LOC_REFERENCE:
  323. begin
  324. ppufile.putlongint(longint(hparaloc^.reference.index));
  325. ppufile.putaint(hparaloc^.reference.offset);
  326. end;
  327. LOC_FPUREGISTER,
  328. LOC_CFPUREGISTER,
  329. LOC_MMREGISTER,
  330. LOC_CMMREGISTER,
  331. LOC_REGISTER,
  332. LOC_CREGISTER :
  333. begin
  334. ppufile.putbyte(hparaloc^.shiftval);
  335. ppufile.putlongint(longint(hparaloc^.register));
  336. end;
  337. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  338. or otherwise it hits the internalerror below. I don't know if this is
  339. the proper way to fix this, someone else with clue might want to take a
  340. look. The compiler cycles on the affected systems with this enabled. (KB) }
  341. LOC_VOID:
  342. begin end
  343. else
  344. internalerror(2010053115);
  345. end;
  346. hparaloc:=hparaloc^.next;
  347. end;
  348. end;
  349. procedure TCGPara.ppuload(ppufile: tcompilerppufile);
  350. var
  351. hparaloc: PCGParaLocation;
  352. nparaloc: byte;
  353. begin
  354. reset;
  355. Alignment:=shortint(ppufile.getbyte);
  356. Size:=TCgSize(ppufile.getbyte);
  357. IntSize:=ppufile.getaint;
  358. ppufile.getderef(defderef);
  359. nparaloc:=ppufile.getbyte;
  360. while nparaloc>0 do
  361. begin
  362. hparaloc:=add_location;
  363. hparaloc^.size:=TCGSize(ppufile.getbyte);
  364. hparaloc^.loc:=TCGLoc(ppufile.getbyte);
  365. case hparaloc^.loc of
  366. LOC_REFERENCE:
  367. begin
  368. hparaloc^.reference.index:=tregister(ppufile.getlongint);
  369. hparaloc^.reference.offset:=ppufile.getaint;
  370. end;
  371. LOC_FPUREGISTER,
  372. LOC_CFPUREGISTER,
  373. LOC_MMREGISTER,
  374. LOC_CMMREGISTER,
  375. LOC_REGISTER,
  376. LOC_CREGISTER :
  377. begin
  378. hparaloc^.shiftval:=ppufile.getbyte;
  379. hparaloc^.register:=tregister(ppufile.getlongint);
  380. end;
  381. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  382. or otherwise it hits the internalerror below. I don't know if this is
  383. the proper way to fix this, someone else with clue might want to take a
  384. look. The compiler cycles on the affected systems with this enabled. (KB) }
  385. LOC_VOID:
  386. begin end
  387. else
  388. internalerror(2010051301);
  389. end;
  390. dec(nparaloc);
  391. end;
  392. end;
  393. {****************************************************************************
  394. TParaList
  395. ****************************************************************************}
  396. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  397. var
  398. I1 : tparavarsym absolute Item1;
  399. I2 : tparavarsym absolute Item2;
  400. begin
  401. Result:=longint(I1.paranr)-longint(I2.paranr);
  402. end;
  403. procedure TParaList.SortParas;
  404. begin
  405. Sort(@ParaNrCompare);
  406. end;
  407. end.