parabase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. procedure buildderef;
  105. procedure deref;
  106. procedure ppuwrite(ppufile:tcompilerppufile);
  107. procedure ppuload(ppufile:tcompilerppufile);
  108. end;
  109. PCGPara = ^TCGPara;
  110. tvarargsinfo = (
  111. va_uses_float_reg
  112. );
  113. tparalist = class(TFPObjectList)
  114. procedure SortParas;
  115. end;
  116. tvarargsparalist = class(tparalist)
  117. varargsinfo : set of tvarargsinfo;
  118. {$ifdef x86_64}
  119. { x86_64 requires %al to contain the no. SSE regs passed }
  120. mmregsused : longint;
  121. {$endif x86_64}
  122. end;
  123. trttiparaloc = record
  124. { contains the regtype in bits 0-6 and whether it's reference or not
  125. in bit 7 }
  126. loctype : byte;
  127. regsub : byte;
  128. regindex : word;
  129. { either stack offset or shiftval }
  130. offset : aint;
  131. end;
  132. trttiparalocs = array of trttiparaloc;
  133. implementation
  134. uses
  135. systems,verbose,
  136. symsym;
  137. {****************************************************************************
  138. TCGPara
  139. ****************************************************************************}
  140. constructor tcgpara.init;
  141. begin
  142. alignment:=0;
  143. size:=OS_NO;
  144. intsize:=0;
  145. location:=nil;
  146. def:=nil;
  147. temporary:=false;
  148. end;
  149. destructor tcgpara.done;
  150. begin
  151. reset;
  152. end;
  153. procedure tcgpara.reset;
  154. var
  155. hlocation : pcgparalocation;
  156. begin
  157. while assigned(location) do
  158. begin
  159. hlocation:=location^.next;
  160. dispose(location);
  161. location:=hlocation;
  162. end;
  163. alignment:=0;
  164. size:=OS_NO;
  165. intsize:=0;
  166. end;
  167. procedure TCGPara.resetiftemp;
  168. begin
  169. if temporary then
  170. reset;
  171. end;
  172. function tcgpara.getcopy:tcgpara;
  173. var
  174. srcloc,hlocation : pcgparalocation;
  175. begin
  176. result.init;
  177. srcloc:=location;
  178. while assigned(srcloc) do
  179. begin
  180. hlocation:=result.add_location;
  181. hlocation^:=srcloc^;
  182. hlocation^.next:=nil;
  183. srcloc:=srcloc^.next;
  184. end;
  185. result.alignment:=alignment;
  186. result.size:=size;
  187. result.intsize:=intsize;
  188. result.def:=def;
  189. end;
  190. function tcgpara.add_location:pcgparalocation;
  191. var
  192. prevlocation,
  193. hlocation : pcgparalocation;
  194. begin
  195. prevlocation:=nil;
  196. hlocation:=location;
  197. while assigned(hlocation) do
  198. begin
  199. prevlocation:=hlocation;
  200. hlocation:=hlocation^.next;
  201. end;
  202. new(hlocation);
  203. Fillchar(hlocation^,sizeof(tcgparalocation),0);
  204. if assigned(prevlocation) then
  205. prevlocation^.next:=hlocation
  206. else
  207. location:=hlocation;
  208. result:=hlocation;
  209. end;
  210. procedure tcgpara.check_simple_location;
  211. begin
  212. if not assigned(location) then
  213. internalerror(200408161);
  214. if assigned(location^.next) then
  215. internalerror(200408162);
  216. end;
  217. procedure tcgpara.get_location(var newloc:tlocation);
  218. begin
  219. if not assigned(location) then
  220. internalerror(200408205);
  221. fillchar(newloc,sizeof(newloc),0);
  222. newloc.loc:=location^.loc;
  223. newloc.size:=size;
  224. case location^.loc of
  225. LOC_REGISTER :
  226. begin
  227. {$ifndef cpu64bitalu}
  228. if size in [OS_64,OS_S64] then
  229. begin
  230. if not assigned(location^.next) then
  231. internalerror(200408206);
  232. if (location^.next^.loc<>LOC_REGISTER) then
  233. internalerror(200408207);
  234. if (target_info.endian = ENDIAN_BIG) then
  235. begin
  236. newloc.register64.reghi:=location^.register;
  237. newloc.register64.reglo:=location^.next^.register;
  238. end
  239. else
  240. begin
  241. newloc.register64.reglo:=location^.register;
  242. newloc.register64.reghi:=location^.next^.register;
  243. end;
  244. end
  245. else
  246. {$endif}
  247. newloc.register:=location^.register;
  248. end;
  249. LOC_FPUREGISTER,
  250. LOC_MMREGISTER :
  251. newloc.register:=location^.register;
  252. LOC_REFERENCE :
  253. begin
  254. newloc.reference.base:=location^.reference.index;
  255. newloc.reference.offset:=location^.reference.offset;
  256. newloc.reference.alignment:=alignment;
  257. end;
  258. end;
  259. end;
  260. function TCGPara.locations_count: integer;
  261. var
  262. hlocation: pcgparalocation;
  263. begin
  264. result:=0;
  265. hlocation:=location;
  266. while assigned(hlocation) do
  267. begin
  268. inc(result);
  269. hlocation:=hlocation^.next;
  270. end;
  271. end;
  272. procedure TCGPara.buildderef;
  273. begin
  274. defderef.build(def);
  275. end;
  276. procedure TCGPara.deref;
  277. begin
  278. def:=tdef(defderef.resolve);
  279. end;
  280. procedure TCGPara.ppuwrite(ppufile: tcompilerppufile);
  281. var
  282. hparaloc: PCGParaLocation;
  283. nparaloc: byte;
  284. begin
  285. ppufile.putbyte(byte(Alignment));
  286. ppufile.putbyte(ord(Size));
  287. ppufile.putaint(IntSize);
  288. ppufile.putderef(defderef);
  289. nparaloc:=0;
  290. hparaloc:=location;
  291. while assigned(hparaloc) do
  292. begin
  293. inc(nparaloc);
  294. hparaloc:=hparaloc^.Next;
  295. end;
  296. ppufile.putbyte(nparaloc);
  297. hparaloc:=location;
  298. while assigned(hparaloc) do
  299. begin
  300. ppufile.putbyte(byte(hparaloc^.Size));
  301. ppufile.putbyte(byte(hparaloc^.loc));
  302. case hparaloc^.loc of
  303. LOC_REFERENCE:
  304. begin
  305. ppufile.putlongint(longint(hparaloc^.reference.index));
  306. ppufile.putaint(hparaloc^.reference.offset);
  307. end;
  308. LOC_FPUREGISTER,
  309. LOC_CFPUREGISTER,
  310. LOC_MMREGISTER,
  311. LOC_CMMREGISTER,
  312. LOC_REGISTER,
  313. LOC_CREGISTER :
  314. begin
  315. ppufile.putbyte(hparaloc^.shiftval);
  316. ppufile.putlongint(longint(hparaloc^.register));
  317. end;
  318. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  319. or otherwise it hits the internalerror below. I don't know if this is
  320. the proper way to fix this, someone else with clue might want to take a
  321. look. The compiler cycles on the affected systems with this enabled. (KB) }
  322. LOC_VOID:
  323. begin end
  324. else
  325. internalerror(2010053115);
  326. end;
  327. hparaloc:=hparaloc^.next;
  328. end;
  329. end;
  330. procedure TCGPara.ppuload(ppufile: tcompilerppufile);
  331. var
  332. hparaloc: PCGParaLocation;
  333. nparaloc: byte;
  334. begin
  335. reset;
  336. Alignment:=shortint(ppufile.getbyte);
  337. Size:=TCgSize(ppufile.getbyte);
  338. IntSize:=ppufile.getaint;
  339. ppufile.getderef(defderef);
  340. nparaloc:=ppufile.getbyte;
  341. while nparaloc>0 do
  342. begin
  343. hparaloc:=add_location;
  344. hparaloc^.size:=TCGSize(ppufile.getbyte);
  345. hparaloc^.loc:=TCGLoc(ppufile.getbyte);
  346. case hparaloc^.loc of
  347. LOC_REFERENCE:
  348. begin
  349. hparaloc^.reference.index:=tregister(ppufile.getlongint);
  350. hparaloc^.reference.offset:=ppufile.getaint;
  351. end;
  352. LOC_FPUREGISTER,
  353. LOC_CFPUREGISTER,
  354. LOC_MMREGISTER,
  355. LOC_CMMREGISTER,
  356. LOC_REGISTER,
  357. LOC_CREGISTER :
  358. begin
  359. hparaloc^.shiftval:=ppufile.getbyte;
  360. hparaloc^.register:=tregister(ppufile.getlongint);
  361. end;
  362. { This seems to be required for systems using explicitparaloc (eg. MorphOS)
  363. or otherwise it hits the internalerror below. I don't know if this is
  364. the proper way to fix this, someone else with clue might want to take a
  365. look. The compiler cycles on the affected systems with this enabled. (KB) }
  366. LOC_VOID:
  367. begin end
  368. else
  369. internalerror(2010051301);
  370. end;
  371. dec(nparaloc);
  372. end;
  373. end;
  374. {****************************************************************************
  375. TParaList
  376. ****************************************************************************}
  377. function ParaNrCompare(Item1, Item2: Pointer): Integer;
  378. var
  379. I1 : tparavarsym absolute Item1;
  380. I2 : tparavarsym absolute Item2;
  381. begin
  382. Result:=longint(I1.paranr)-longint(I2.paranr);
  383. end;
  384. procedure TParaList.SortParas;
  385. begin
  386. Sort(@ParaNrCompare);
  387. end;
  388. end.