parabase.pas 13 KB

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