type.c 4.1 KB

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