stor-layout.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /* C-compiler utilities for types and variables storage layout
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include "tree.h"
  20. #include "rtl.h" /* For GET_MODE_SIZE */
  21. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  22. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  23. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  24. /* Data type for the expressions representing sizes of data types.
  25. It is the first integer type laid out.
  26. In C, this is int. */
  27. tree sizetype;
  28. #define GET_MODE_ALIGNMENT(MODE) \
  29. MIN (BIGGEST_ALIGNMENT, \
  30. MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  31. /* Chain of all permanent types we have allocated since last
  32. call to get_permanent_types. */
  33. tree permanent_type_chain;
  34. /* Chain of all temporary types we have allocated in this function. */
  35. tree temporary_type_chain;
  36. /* When the chains is not null, these point at the last
  37. types on the two chains. */
  38. tree permanent_type_end;
  39. tree temporary_type_end;
  40. /* Put the newly-made type T
  41. on either permanent_type_chain or temporary_type_chain.
  42. Types that are const or volatile variants of other types
  43. are not put on any chain, since in the gdb symbol segment
  44. we do not make those distinctions.
  45. If T is already on the chain, we do nothing. */
  46. void
  47. chain_type (t)
  48. tree t;
  49. {
  50. if (TYPE_MAIN_VARIANT (t) != t)
  51. return;
  52. if (TREE_CHAIN (t) != 0)
  53. return;
  54. if (TREE_PERMANENT (t))
  55. {
  56. if (t == permanent_type_end)
  57. return;
  58. if (permanent_type_chain == 0)
  59. permanent_type_end = t;
  60. TREE_CHAIN (t) = permanent_type_chain;
  61. permanent_type_chain = t;
  62. }
  63. else
  64. {
  65. if (t == temporary_type_end)
  66. return;
  67. if (temporary_type_chain == 0)
  68. temporary_type_end = t;
  69. TREE_CHAIN (t) = temporary_type_chain;
  70. temporary_type_chain = t;
  71. }
  72. }
  73. /* Get a chain of all permanent types made since this function
  74. was last called. */
  75. tree
  76. get_permanent_types ()
  77. {
  78. register tree tem = permanent_type_chain;
  79. permanent_type_chain = 0;
  80. permanent_type_end = 0;
  81. return tem;
  82. }
  83. /* Get a chain of all temporary types made since this function
  84. was last called. */
  85. tree
  86. get_temporary_types ()
  87. {
  88. register tree tem = temporary_type_chain;
  89. temporary_type_chain = 0;
  90. temporary_type_end = 0;
  91. return tem;
  92. }
  93. /* Return the greatest common divisor of M and N. */
  94. static
  95. unsigned
  96. gcd (m, n)
  97. register unsigned int m, n;
  98. {
  99. register unsigned int r;
  100. while (1)
  101. {
  102. r = m % n;
  103. if (0 == r) break;
  104. m = n;
  105. n = r;
  106. }
  107. return n;
  108. }
  109. /* Return the machine mode to use for an aggregate of SIZE bits.
  110. Note!!! We only use a non-BLKmode mode if the size matches exactly.
  111. There used to be the idea of using DImode for anything whose
  112. size was less than DImode but more than SImode. This does not work
  113. because DImode moves cannot be used to store such objects in memory. */
  114. static
  115. enum machine_mode
  116. agg_mode (size)
  117. unsigned int size;
  118. {
  119. register int units = size / BITS_PER_UNIT;
  120. register enum machine_mode t;
  121. if (size % BITS_PER_UNIT != 0)
  122. return BLKmode;
  123. for (t = QImode; (int) t <= (int) DImode;
  124. t = (enum machine_mode) ((int) t + 1))
  125. if (GET_MODE_SIZE (t) == units)
  126. return t;
  127. return BLKmode;
  128. }
  129. /* Return an INTEGER_CST with value V and type from `sizetype'. */
  130. static tree
  131. build_int (v)
  132. int v;
  133. {
  134. register tree t;
  135. t = build_int_2 (v, 0);
  136. TREE_TYPE (t) = sizetype;
  137. return t;
  138. }
  139. /* Combine operands OP1 and OP2 with arithmetic operation OPC.
  140. OPC is a tree code. Data type is taken from `sizetype',
  141. If the operands are constant, so is the result. */
  142. static tree
  143. genop (opc, op1, op2)
  144. enum tree_code opc;
  145. tree op1, op2;
  146. {
  147. register tree t;
  148. if (TREE_LITERAL (op1) && TREE_LITERAL (op2))
  149. return combine (opc, op1, op2);
  150. t = build2 (opc, op1, op2);
  151. TREE_TYPE (t) = sizetype;
  152. t = fold (t);
  153. return t;
  154. }
  155. /* Convert a size which is SIZE when expressed in unit INUNITS
  156. into the units OUTUNITS. Rounds up if conversion is not exact.
  157. If SIZE is constant, so is the result. */
  158. tree
  159. convert_units (size, inunits, outunits)
  160. tree size;
  161. register int inunits, outunits;
  162. {
  163. register tree t;
  164. if (inunits == outunits)
  165. return size;
  166. /* Check for inunits divisible by outunits.
  167. In that case, just multiply by their ratio. */
  168. if (0 == (inunits % outunits))
  169. return genop (MULT_EXPR, size, build_int (inunits / outunits));
  170. /* The inverse case. */
  171. if (0 == (outunits % inunits))
  172. return genop (CEIL_DIV_EXPR, size, build_int (outunits / inunits));
  173. /* The general case. */
  174. t = genop (MULT_EXPR, size,
  175. build_int (inunits)); /* convert to bits */
  176. return genop (CEIL_DIV_EXPR, t,
  177. build_int (outunits)); /* then to outunits */
  178. }
  179. /* Form a tree giving the value of
  180. CEIL ((CONST + VAR * VAR_UNIT) / BITS_PER_UNIT)
  181. where VAR is any expression and CONST and VAR_UNIT are integers.
  182. VAR may be null; that counts as zero. */
  183. static
  184. tree
  185. add_vc_sizes (constant, var, coeff)
  186. int constant;
  187. tree var;
  188. int coeff;
  189. {
  190. register tree tmp1, tmp2;
  191. if (var == 0)
  192. return build_int (CEIL (constant, BITS_PER_UNIT));
  193. if (constant == 0)
  194. return convert_units (var, coeff, BITS_PER_UNIT);
  195. tmp1 = genop (PLUS_EXPR, genop (MULT_EXPR, var, integer_one_node),
  196. build_int (constant)); /* add */
  197. return genop (CEIL_DIV_EXPR, tmp1, build_int (BITS_PER_UNIT));
  198. }
  199. /* Set the size, mode and alignment of a ..._DECL node.
  200. Note that LABEL_DECL, TYPE_DECL and CONST_DECL nodes do not need this,
  201. and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  202. Don't call layout_decl for them.
  203. KNOWN_ALIGN is the amount of alignment we can assume this
  204. decl has with no special effort. It is relevant only for FIELD_DECLs
  205. and depends on the previous fields.
  206. All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  207. If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  208. the record will be aligned to suit. */
  209. void
  210. layout_decl (decl, known_align)
  211. tree decl;
  212. unsigned known_align;
  213. {
  214. register tree type = TREE_TYPE (decl);
  215. register enum tree_code code = TREE_CODE (decl);
  216. int spec_size = DECL_SIZE_UNIT (decl);
  217. if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  218. && code != FIELD_DECL)
  219. abort ();
  220. if (type == error_mark_node)
  221. type = void_type_node;
  222. if (TYPE_SIZE_UNIT (type) == 0)
  223. abort ();
  224. /* Usually the size and mode come from the data type without change. */
  225. DECL_MODE (decl) = TYPE_MODE (type);
  226. DECL_SIZE (decl) = TYPE_SIZE (type);
  227. DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
  228. /* Force alignment required for the data type.
  229. But if the decl itself wants greater alignment, don't override that. */
  230. if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
  231. DECL_ALIGN (decl) = TYPE_ALIGN (type);
  232. if (code == FIELD_DECL && spec_size != 0)
  233. {
  234. /* This is a bit-field. We don't know how to handle
  235. them except for integers and enums, and front end should
  236. never generate them otherwise. */
  237. if (! (TREE_CODE (type) == INTEGER_TYPE
  238. || TREE_CODE (type) == ENUMERAL_TYPE))
  239. abort ();
  240. /* Mode is "integer bit field". */
  241. DECL_MODE (decl) = BImode;
  242. /* Size is specified number of bits. */
  243. DECL_SIZE (decl) = integer_one_node;
  244. DECL_SIZE_UNIT (decl) = spec_size;
  245. /* No alignment requirement. */
  246. DECL_ALIGN (decl) = 1;
  247. TREE_UNSIGNED (decl) = type_unsigned_p (type);
  248. /* Promote the field's type to int or unsigned int
  249. if it is narrower than that. */
  250. if (TREE_CODE (type) == INTEGER_TYPE
  251. && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
  252. TREE_TYPE (decl) = type
  253. = (TREE_UNSIGNED (decl) ? unsigned_type_node : integer_type_node);
  254. }
  255. /* See if we can use a scalar mode such as QImode or SImode
  256. in place of BLKmode or a packed byte mode. */
  257. /* Conditions are: a fixed size that is correct for another mode
  258. and occupying a complete byte or bytes on proper boundary. */
  259. if ((DECL_MODE (decl) == BLKmode
  260. || DECL_MODE (decl) == BImode)
  261. && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  262. {
  263. register int packed_size
  264. = TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl);
  265. register enum machine_mode xmode = agg_mode (packed_size);
  266. if (xmode != BLKmode
  267. && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  268. {
  269. DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  270. DECL_ALIGN (decl));
  271. DECL_MODE (decl) = xmode;
  272. DECL_SIZE (decl) = build_int (GET_MODE_SIZE (xmode));
  273. DECL_SIZE_UNIT (decl) = BITS_PER_UNIT;
  274. }
  275. }
  276. }
  277. /* Lay out a RECORD_TYPE type (a C struct).
  278. This means laying out the fields, determining their offsets,
  279. and computing the overall size and required alignment of the record. */
  280. static tree
  281. layout_record (rec)
  282. tree rec;
  283. {
  284. register tree field;
  285. int record_align = BITS_PER_UNIT;
  286. /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
  287. where CONST_SIZE is an integer
  288. and VAR_SIZE is a tree expression.
  289. If VAR_SIZE is null, the size is just CONST_SIZE.
  290. Naturally we try to avoid using VAR_SIZE. */
  291. register int const_size = 0;
  292. register tree var_size = 0;
  293. register int size_unit = 8;
  294. for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  295. {
  296. register int desired_align;
  297. /* Lay out the field so we know what alignment it needs.
  298. For KNOWN_ALIGN, pass the number of bits from start of record
  299. or some divisor of it. */
  300. layout_decl (field, var_size ? size_unit : const_size);
  301. desired_align = DECL_ALIGN (field);
  302. /* Record must have at least as much alignment as any field.
  303. Otherwise, the alignment of the field within the record
  304. is meaningless. */
  305. record_align = MAX (record_align, desired_align);
  306. /* Does this field automatically have alignment it needs
  307. by virtue of the fields that precede it and the record's
  308. own alignment? */
  309. if (const_size % desired_align != 0
  310. || (size_unit % desired_align != 0
  311. && var_size))
  312. {
  313. /* No, we need to skip space before this field.
  314. Bump the cumulative size to multiple of field alignment. */
  315. if (var_size == 0
  316. || size_unit % desired_align == 0)
  317. const_size
  318. = CEIL (const_size, desired_align) * desired_align;
  319. else
  320. {
  321. var_size
  322. = genop (PLUS_EXPR, var_size,
  323. build_int (CEIL (const_size, size_unit)));
  324. const_size = 0;
  325. var_size = convert_units (var_size, size_unit, desired_align);
  326. size_unit = desired_align;
  327. }
  328. }
  329. /* Size so far becomes the offset of this field. */
  330. DECL_OFFSET (field) = const_size;
  331. DECL_VOFFSET (field) = var_size;
  332. DECL_VOFFSET_UNIT (field) = size_unit;
  333. /* Now add size of this field to the size of the record. */
  334. {
  335. register tree dsize = DECL_SIZE (field);
  336. if (TREE_LITERAL (dsize))
  337. const_size += TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  338. else if (var_size == 0)
  339. {
  340. var_size = dsize;
  341. size_unit = DECL_SIZE_UNIT (field);
  342. }
  343. else
  344. {
  345. register int tunits = MIN (size_unit, DECL_SIZE_UNIT (field));
  346. var_size
  347. = genop (PLUS_EXPR,
  348. convert_units (var_size, size_unit, tunits),
  349. convert_units (dsize, DECL_SIZE_UNIT (field), tunits));
  350. }
  351. }
  352. }
  353. /* Work out the total size and alignment of the record
  354. as one expression and store in the record type.
  355. Round it up to a multiple of the record's alignment. */
  356. if (var_size == 0)
  357. TYPE_SIZE (rec)
  358. = build_int (CEIL (CEIL (const_size, record_align) * record_align,
  359. size_unit));
  360. else
  361. {
  362. if (const_size)
  363. var_size
  364. = genop (PLUS_EXPR, var_size,
  365. build_int (CEIL (const_size, size_unit)));
  366. TYPE_SIZE (rec)
  367. = convert_units (var_size,
  368. size_unit,
  369. record_align);
  370. size_unit = record_align;
  371. }
  372. TYPE_SIZE (rec) = convert_units (TYPE_SIZE (rec), size_unit,
  373. BITS_PER_UNIT);
  374. TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
  375. TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, record_align);
  376. }
  377. /* Lay out a UNION_TYPE type.
  378. Lay out all the fields, set their offsets to zero,
  379. and compute the size and alignment of the union (maximum of any field). */
  380. static tree
  381. layout_union (rec)
  382. tree rec;
  383. {
  384. register tree field;
  385. int union_align = BITS_PER_UNIT;
  386. /* The size of the union, based on the fields scanned so far,
  387. is max (CONST_SIZE, VAR_SIZE).
  388. VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
  389. register int const_size = 0;
  390. register tree var_size = 0;
  391. for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  392. {
  393. layout_decl (field, 0);
  394. DECL_OFFSET (field) = 0;
  395. DECL_VOFFSET (field) = 0;
  396. DECL_VOFFSET_UNIT (field) = BITS_PER_UNIT;
  397. /* Union must be at least as aligned as any field requires. */
  398. union_align = MAX (union_align, DECL_ALIGN (field));
  399. /* Set union_size to max (decl_size, union_size).
  400. There are more and less general ways to do this.
  401. Use only CONST_SIZE unless forced to use VAR_SIZE. */
  402. if (TREE_LITERAL (DECL_SIZE (field)))
  403. const_size = MAX (const_size,
  404. TREE_INT_CST_LOW (DECL_SIZE (field))
  405. * DECL_SIZE_UNIT (field));
  406. else if (var_size == 0)
  407. var_size = convert_units (DECL_SIZE (field),
  408. DECL_SIZE_UNIT (field),
  409. BITS_PER_UNIT);
  410. else
  411. var_size = genop (MAX_EXPR,
  412. convert_units (DECL_SIZE (field),
  413. DECL_SIZE_UNIT (field),
  414. BITS_PER_UNIT),
  415. var_size);
  416. }
  417. /* Determine the ultimate size of the union. */
  418. if (NULL == var_size)
  419. TYPE_SIZE (rec) = build_int (CEIL (const_size, BITS_PER_UNIT));
  420. else if (const_size == 0)
  421. TYPE_SIZE (rec) = var_size;
  422. else
  423. TYPE_SIZE (rec) = genop (MAX_EXPR, var_size,
  424. build_int (CEIL (const_size, BITS_PER_UNIT)));
  425. TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
  426. TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, union_align);
  427. }
  428. /* Calculate the mode, size, and alignment for TYPE.
  429. For an array type, calculate the element separation as well.
  430. Record TYPE on the chain of permanent or temporary types
  431. so that dbxout will find out about it. */
  432. void
  433. layout_type (type)
  434. tree type;
  435. {
  436. if (type == 0)
  437. abort ();
  438. /* Do nothing if type has been laid out before. */
  439. if (TYPE_SIZE (type))
  440. return;
  441. chain_type (type);
  442. switch (TREE_CODE (type))
  443. {
  444. case VOID_TYPE:
  445. TYPE_SIZE (type) = integer_zero_node;
  446. TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  447. TYPE_ALIGN (type) = 1;
  448. TYPE_MODE (type) = VOIDmode;
  449. break;
  450. case INTEGER_TYPE:
  451. case ENUMERAL_TYPE:
  452. TYPE_MODE (type) = agg_mode (TYPE_PRECISION (type));
  453. TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  454. TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  455. TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  456. if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
  457. TREE_UNSIGNED (type) = 1;
  458. break;
  459. case REAL_TYPE:
  460. {
  461. register int prec = TYPE_PRECISION (type);
  462. if (prec <= GET_MODE_BITSIZE (SFmode))
  463. TYPE_MODE (type) = SFmode;
  464. else if (prec <= GET_MODE_BITSIZE (DFmode))
  465. TYPE_MODE (type) = DFmode;
  466. else
  467. abort ();
  468. }
  469. TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  470. TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  471. TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  472. break;
  473. case POINTER_TYPE:
  474. TYPE_MODE (type) = Pmode;
  475. TYPE_SIZE (type) = build_int (POINTER_SIZE / BITS_PER_UNIT);
  476. TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  477. TYPE_ALIGN (type) = POINTER_BOUNDARY;
  478. break;
  479. case ARRAY_TYPE:
  480. {
  481. register tree index = TYPE_DOMAIN (type);
  482. register tree length;
  483. register tree element = TREE_TYPE (type);
  484. if (index == 0)
  485. length = integer_zero_node;
  486. else
  487. length = genop (PLUS_EXPR, integer_one_node,
  488. genop (MINUS_EXPR, TYPE_MAX_VALUE (index),
  489. TYPE_MIN_VALUE (index)));
  490. if (TREE_PACKED (type))
  491. abort (); /* ??? Not written yet since not needed for C. */
  492. TYPE_SIZE_UNIT (type) = TYPE_SIZE_UNIT (element);
  493. TYPE_SIZE (type) = genop (MULT_EXPR, TYPE_SIZE (element), length);
  494. TYPE_SEP (type) = TYPE_SIZE (element);
  495. TYPE_SEP_UNIT (type) = TYPE_SIZE_UNIT (element);
  496. TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
  497. TYPE_MODE (type) = BLKmode;
  498. if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  499. {
  500. TYPE_MODE (type)
  501. = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  502. * TYPE_SIZE_UNIT (type));
  503. }
  504. break;
  505. }
  506. case RECORD_TYPE:
  507. layout_record (type);
  508. TYPE_MODE (type) = BLKmode;
  509. if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  510. {
  511. TYPE_MODE (type)
  512. = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  513. * TYPE_SIZE_UNIT (type));
  514. }
  515. break;
  516. case UNION_TYPE:
  517. layout_union (type);
  518. TYPE_MODE (type) = BLKmode;
  519. if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  520. {
  521. TYPE_MODE (type)
  522. = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  523. * TYPE_SIZE_UNIT (type));
  524. }
  525. break;
  526. case FUNCTION_TYPE:
  527. TYPE_MODE (type) = EPmode;
  528. TYPE_SIZE (type) = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
  529. TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  530. TYPE_ALIGN (type) = POINTER_BOUNDARY;
  531. break;
  532. default:
  533. abort ();
  534. } /* end switch */
  535. }
  536. /* Set the offsets of the parameters of a FUNCTION_DECL node.
  537. Assumes layout_decl has been done already on the PARM_DECLs themselves. */
  538. void
  539. layout_parms (fndecl)
  540. tree fndecl;
  541. {
  542. register tree parm;
  543. /* Constant term in the offset so far, measured in bits. */
  544. register int const_offset = FIRST_PARM_OFFSET * BITS_PER_UNIT;
  545. /* Variable term in the offset so far; null if offset so far is constant. */
  546. register tree var_offset = 0;
  547. /* Unit of measurement for VAR_OFFSET. */
  548. register int offset_unit = 8;
  549. for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
  550. {
  551. /* The offset of the next parm is, in general,
  552. CONST_OFFSET + VAR_OFFSET * OFFSET_UNITS.
  553. If VAR_OFFSET is null, then CONST_OFFSET alone says everything. */
  554. register int desired_align
  555. = MAX (PARM_BOUNDARY, TYPE_ALIGN (DECL_ARG_TYPE (parm)));
  556. if (const_offset % desired_align != 0
  557. || (offset_unit % desired_align != 0
  558. && var_offset))
  559. {
  560. /* We need some padding to align this parm.
  561. Round up the variable and constant parts of the offset
  562. separately or together, whichever is best, to a multiple
  563. of DESIRED_ALIGN. */
  564. if (var_offset == 0
  565. || offset_unit % desired_align == 0)
  566. const_offset
  567. = CEIL (const_offset, desired_align) * desired_align;
  568. else
  569. {
  570. var_offset
  571. = genop (PLUS_EXPR, var_offset,
  572. build_int (CEIL (const_offset, offset_unit)));
  573. const_offset = 0;
  574. var_offset = convert_units (var_offset, offset_unit,
  575. desired_align);
  576. offset_unit = desired_align;
  577. }
  578. }
  579. /* Set the parm's offset, constant and variable parts,
  580. according to where we have reached. */
  581. DECL_OFFSET (parm) = const_offset;
  582. DECL_VOFFSET (parm) = var_offset;
  583. DECL_VOFFSET_UNIT (parm) = offset_unit;
  584. /* Now add size of this parm to the offset-so-far. */
  585. {
  586. register tree dsize = TYPE_SIZE (DECL_ARG_TYPE (parm));
  587. register int sizeunit = TYPE_SIZE_UNIT (DECL_ARG_TYPE (parm));
  588. /* Parm size is constant => add to constant part of offset. */
  589. if (TREE_LITERAL (dsize))
  590. const_offset += TREE_INT_CST_LOW (dsize) * sizeunit;
  591. else if (var_offset == 0)
  592. {
  593. /* Offset-so-far is constant but parm size is variable
  594. => parm size becomes variable part of offset-so-far. */
  595. var_offset = dsize;
  596. offset_unit = sizeunit;
  597. }
  598. else
  599. {
  600. /* Add parm size into offset so far
  601. after converting them to common units. */
  602. register int tunits = MIN (offset_unit, sizeunit);
  603. var_offset
  604. = genop (PLUS_EXPR,
  605. convert_units (var_offset, offset_unit, tunits),
  606. convert_units (dsize, sizeunit, tunits));
  607. }
  608. }
  609. }
  610. }
  611. /* Create and return a type for signed integers of PRECISION bits. */
  612. tree
  613. make_signed_type (precision)
  614. int precision;
  615. {
  616. register tree type = make_node (INTEGER_TYPE);
  617. register tree low, high;
  618. TYPE_PRECISION (type) = precision;
  619. /* Create the extreme values based on the number of bits. */
  620. TYPE_MIN_VALUE (type)
  621. = build_int_2 ((precision-BITS_PER_WORD > 0 ? 0 : (-1)<<(precision-1)),
  622. (-1)<<(precision-BITS_PER_WORD-1 > 0
  623. ? precision-BITS_PER_WORD-1
  624. : 0));
  625. TYPE_MAX_VALUE (type)
  626. = build_int_2 ((precision-BITS_PER_WORD > 0 ? -1 : (1<<(precision-1))-1),
  627. (precision-BITS_PER_WORD-1 > 0
  628. ? (1<<(precision-BITS_PER_WORD-1))-1
  629. : 0));
  630. /* Give this type's extreme values this type as their type. */
  631. TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  632. TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  633. /* The first type made with this or `make_unsigned_type'
  634. is the type for size values. */
  635. if (sizetype == 0)
  636. sizetype = type;
  637. /* Lay out the type: set its alignment, size, etc. */
  638. layout_type (type);
  639. return type;
  640. }
  641. /* Create and return a type for unsigned integers of PRECISION bits. */
  642. tree
  643. make_unsigned_type (precision)
  644. int precision;
  645. {
  646. register tree type = make_node (INTEGER_TYPE);
  647. register tree low, high;
  648. TYPE_PRECISION (type) = precision;
  649. /* The first type made with this or `make_unsigned_type'
  650. is the type for size values. */
  651. if (sizetype == 0)
  652. sizetype = type;
  653. fixup_unsigned_type (type);
  654. return type;
  655. }
  656. /* Set the extreme values of TYPE based on its precision in bits,
  657. the lay it out. This is used both in `make_unsigned_type'
  658. and for enumeral types. */
  659. void
  660. fixup_unsigned_type (type)
  661. tree type;
  662. {
  663. register int precision = TYPE_PRECISION (type);
  664. TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
  665. TYPE_MAX_VALUE (type)
  666. = build_int_2 (precision-BITS_PER_WORD >= 0 ? -1 : (1<<precision)-1,
  667. precision-BITS_PER_WORD > 0
  668. ? (1<<(precision-BITS_PER_WORD))-1 : 0);
  669. TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  670. TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  671. /* Lay out the type: set its alignment, size, etc. */
  672. layout_type (type);
  673. }