type.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* T Y P E D E F I N I T I O N M E C H A N I S M */
  7. #include "nofloat.h"
  8. #include "nobitfield.h"
  9. #include "botch_free.h"
  10. #include <alloc.h>
  11. #include "Lpars.h"
  12. #include "arith.h"
  13. #include "type.h"
  14. #include "idf.h"
  15. #include "def.h"
  16. #include "sizes.h"
  17. #include "align.h"
  18. struct type *function_of(), *array_of();
  19. #ifndef NOBITFIELD
  20. struct type *field_of();
  21. #endif /* NOBITFIELD */
  22. /* To be created dynamically in main() from defaults or from command
  23. line parameters.
  24. */
  25. struct type
  26. *char_type, *uchar_type,
  27. *short_type, *ushort_type,
  28. *word_type, *uword_type,
  29. *int_type, *uint_type,
  30. *long_type, *ulong_type,
  31. #ifndef NOFLOAT
  32. *float_type, *double_type,
  33. #endif /* NOFLOAT */
  34. *void_type, *label_type,
  35. *string_type, *funint_type, *error_type;
  36. struct type *pa_type; /* Pointer-Arithmetic type */
  37. struct type *
  38. create_type(fund)
  39. int fund;
  40. {
  41. /* A brand new struct type is created, and its tp_fund set
  42. to fund.
  43. */
  44. register struct type *ntp = new_type();
  45. ntp->tp_fund = fund;
  46. ntp->tp_size = (arith)-1;
  47. return ntp;
  48. }
  49. struct type *
  50. construct_type(fund, tp, count)
  51. register struct type *tp;
  52. arith count; /* for fund == ARRAY only */
  53. {
  54. /* fund must be a type constructor: FIELD, FUNCTION, POINTER or
  55. ARRAY. The pointer to the constructed type is returned.
  56. */
  57. register struct type *dtp;
  58. switch (fund) {
  59. #ifndef NOBITFIELD
  60. case FIELD:
  61. dtp = field_of(tp);
  62. break;
  63. #endif /* NOBITFIELD */
  64. case FUNCTION:
  65. if (tp->tp_fund == FUNCTION) {
  66. error("function cannot yield function");
  67. return error_type;
  68. }
  69. if (tp->tp_fund == ARRAY) {
  70. error("function cannot yield array");
  71. return error_type;
  72. }
  73. dtp = function_of(tp);
  74. break;
  75. case POINTER:
  76. dtp = pointer_to(tp);
  77. break;
  78. case ARRAY:
  79. if (count >= 0 && tp->tp_size < 0) {
  80. error("cannot construct array of unknown type");
  81. count = (arith)-1;
  82. }
  83. else if (tp->tp_size == 0) /* CJ */
  84. warning("array elements have size 0");
  85. if (count >= (arith)0)
  86. count *= tp->tp_size;
  87. dtp = array_of(tp, count);
  88. break;
  89. default:
  90. crash("bad constructor in construct_type");
  91. /*NOTREACHED*/
  92. }
  93. return dtp;
  94. }
  95. struct type *
  96. function_of(tp)
  97. register struct type *tp;
  98. {
  99. register struct type *dtp = tp->tp_function;
  100. if (!dtp) {
  101. tp->tp_function = dtp = create_type(FUNCTION);
  102. dtp->tp_up = tp;
  103. dtp->tp_size = pointer_size;
  104. dtp->tp_align = pointer_align;
  105. }
  106. return dtp;
  107. }
  108. struct type *
  109. pointer_to(tp)
  110. register struct type *tp;
  111. {
  112. register struct type *dtp = tp->tp_pointer;
  113. if (!dtp) {
  114. tp->tp_pointer = dtp = create_type(POINTER);
  115. dtp->tp_unsigned = 1;
  116. dtp->tp_up = tp;
  117. dtp->tp_size = pointer_size;
  118. dtp->tp_align = pointer_align;
  119. }
  120. return dtp;
  121. }
  122. struct type *
  123. array_of(tp, count)
  124. register struct type *tp;
  125. arith count;
  126. {
  127. register struct type *dtp = tp->tp_array;
  128. /* look for a type with the right size */
  129. while (dtp && dtp->tp_size != count)
  130. dtp = dtp->next;
  131. if (!dtp) {
  132. dtp = create_type(ARRAY);
  133. dtp->tp_up = tp;
  134. dtp->tp_size = count;
  135. dtp->tp_align = tp->tp_align;
  136. dtp->next = tp->tp_array;
  137. tp->tp_array = dtp;
  138. }
  139. return dtp;
  140. }
  141. #ifndef NOBITFIELD
  142. struct type *
  143. field_of(tp)
  144. register struct type *tp;
  145. {
  146. register struct type *dtp = create_type(FIELD);
  147. dtp->tp_up = tp;
  148. dtp->tp_align = tp->tp_align;
  149. dtp->tp_size = tp->tp_size;
  150. return dtp;
  151. }
  152. #endif /* NOBITFIELD */
  153. arith
  154. size_of_type(tp, nm)
  155. struct type *tp;
  156. char nm[];
  157. {
  158. arith sz = tp->tp_size;
  159. if (sz < 0) {
  160. error("size of %s unknown", nm);
  161. return (arith)1;
  162. }
  163. return sz;
  164. }
  165. idf2type(idf, tpp)
  166. struct idf *idf;
  167. struct type **tpp;
  168. {
  169. /* Decoding a typedef-ed identifier: if the size is yet
  170. unknown we have to make copy of the type descriptor to
  171. prevent garbage at the initialisation of arrays with
  172. unknown size.
  173. */
  174. register struct type *tp = idf->id_def->df_type;
  175. if ( tp->tp_size < (arith)0 && tp->tp_fund == ARRAY) {
  176. *tpp = new_type();
  177. **tpp = *tp;
  178. /* this is really a structure assignment, AAGH!!! */
  179. }
  180. else {
  181. *tpp = tp;
  182. }
  183. }
  184. arith
  185. align(pos, al)
  186. arith pos;
  187. int al;
  188. {
  189. return ((pos + al - 1) / al) * al;
  190. }
  191. struct type *
  192. standard_type(fund, sgn, algn, sz)
  193. int algn; arith sz;
  194. {
  195. register struct type *tp = create_type(fund);
  196. tp->tp_unsigned = sgn;
  197. tp->tp_align = algn;
  198. tp->tp_size = sz;
  199. return tp;
  200. }