parabase.pas 14 KB

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