type.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* $Header$ */
  2. /* Routines to create type structures */
  3. #include <alloc.h>
  4. #include <assert.h>
  5. #include "type.h"
  6. #include "sizes.h"
  7. #include "symbol.h"
  8. #include "scope.h"
  9. #include "message.h"
  10. #include "langdep.h"
  11. p_type int_type, char_type, short_type, long_type;
  12. p_type uint_type, uchar_type, ushort_type, ulong_type;
  13. p_type void_type, incomplete_type;
  14. p_type float_type, double_type;
  15. p_type string_type;
  16. long int_size = SZ_INT,
  17. char_size = 1,
  18. short_size = SZ_SHORT,
  19. long_size = SZ_LONG,
  20. pointer_size = SZ_POINTER;
  21. long float_size = SZ_FLOAT,
  22. double_size = SZ_DOUBLE;
  23. struct bounds {
  24. long low, high;
  25. };
  26. static struct bounds ibounds[2] = {
  27. { -128, 127 },
  28. { -32768, 32767 }
  29. };
  30. static struct bounds ubounds[2] = {
  31. { 0, 255 },
  32. { 0, 65535 }
  33. };
  34. static long max_int[8], max_uns[8];
  35. struct integer_types {
  36. long maxval;
  37. p_type type;
  38. };
  39. static struct integer_types i_types[4];
  40. static struct integer_types u_types[5];
  41. #define ufit(n, nb) Xfit(n, nb, ubounds)
  42. #define ifit(n, nb) Xfit(n, nb, ibounds)
  43. #define Xfit(n, nb, b) ((n) >= (b)[(nb)-1].low && (n) <= (b)[(nb)-1].high)
  44. /* Create a subrange type, but is it really a subrange? */
  45. p_type
  46. subrange_type(A, base_index, c1, c2, result_index)
  47. int *base_index, *result_index;
  48. long c1, c2;
  49. {
  50. int itself = 0;
  51. register p_type p;
  52. p_type base_type;
  53. if (!A) {
  54. /* Subrange of itself is a special case ... */
  55. if (result_index &&
  56. result_index[0] == base_index[0] &&
  57. result_index[1] == base_index[1]) {
  58. /* c1 = 0 and c2 = 0 -> void */
  59. if (c1 == 0 && c2 == 0) {
  60. return void_type;
  61. }
  62. /* c1 = 0 and c2 = 127 -> char ??? */
  63. if (c1 == 0 && c2 == 127) {
  64. return char_type;
  65. }
  66. itself = 1;
  67. }
  68. }
  69. if (itself) base_type = int_type; else base_type = *(tp_lookup(base_index));
  70. if (! A) {
  71. /* c2 = 0 and c1 > 0 -> real */
  72. if (c2 == 0 && c1 > 0) {
  73. if (c1 == float_size) return float_type;
  74. return double_type;
  75. }
  76. /* c1 = 0 and base_index indicates int_type or itself -> unsigned,
  77. c1 = -c2 - 1 and base_index indicates int_type or itself -> integer
  78. */
  79. if (itself || base_type == int_type) {
  80. register struct integer_types *ip = 0;
  81. if (c1 == 0) {
  82. ip = &u_types[0];
  83. }
  84. else if (c1 == -c2 - 1) {
  85. ip = &i_types[0];
  86. }
  87. if (ip) {
  88. while (ip->maxval != 0 && ip->maxval != c2) ip++;
  89. if (ip->maxval) return ip->type;
  90. }
  91. }
  92. }
  93. /* if we get here, it actually is a subrange type */
  94. p = new_type();
  95. p->ty_class = T_SUBRANGE;
  96. p->ty_low = c1;
  97. p->ty_up = c2;
  98. p->ty_base = base_type;
  99. p->ty_A = A;
  100. /* determine size of subrange type */
  101. p->ty_size = base_type->ty_size;
  102. if (!A && p->ty_base == uint_type) {
  103. if (ufit(p->ty_up, 1)) {
  104. p->ty_size = 1;
  105. }
  106. else if (ufit(p->ty_up, (int)short_size)) {
  107. p->ty_size = short_size;
  108. }
  109. }
  110. if (!A && p->ty_base == int_type) {
  111. if (ifit(p->ty_up, 1) && ifit(p->ty_low, 1)) {
  112. p->ty_size = 1;
  113. }
  114. else if (ifit(p->ty_up, (int)short_size) &&
  115. ifit(p->ty_low, (int)short_size)) {
  116. p->ty_size = short_size;
  117. }
  118. }
  119. return p;
  120. }
  121. static long
  122. nel(tp)
  123. register p_type tp;
  124. {
  125. switch(tp->ty_class) {
  126. case T_SUBRANGE:
  127. if (tp->ty_A) return 0;
  128. if (tp->ty_low <= tp->ty_up) return tp->ty_up - tp->ty_low + 1;
  129. return tp->ty_low - tp->ty_up + 1;
  130. case T_UNSIGNED:
  131. case T_INTEGER:
  132. if (tp->ty_size == 1) return 256;
  133. if (tp->ty_size == 2) return 65536L;
  134. assert(0);
  135. break;
  136. case T_ENUM:
  137. return tp->ty_nenums;
  138. default:
  139. assert(0);
  140. break;
  141. }
  142. return 0;
  143. }
  144. p_type
  145. array_type(bound_type, el_type)
  146. p_type bound_type, el_type;
  147. {
  148. register p_type tp = new_type();
  149. tp->ty_class = T_ARRAY;
  150. tp->ty_index = bound_type;
  151. tp->ty_elements = el_type;
  152. tp->ty_size = (*currlang->arrayelsize)(el_type->ty_size) * nel(bound_type);
  153. return tp;
  154. }
  155. p_type
  156. basic_type(fund, size)
  157. int fund;
  158. long size;
  159. {
  160. register p_type p = new_type();
  161. p->ty_class = fund;
  162. p->ty_size = size;
  163. return p;
  164. }
  165. set_bounds(tp)
  166. register p_type tp;
  167. {
  168. /* Determine the size and low of a set type */
  169. register p_type base = tp->ty_setbase;
  170. if (base->ty_class == T_SUBRANGE) {
  171. tp->ty_size = (base->ty_up - base->ty_low + 7) >> 3;
  172. tp->ty_setlow = base->ty_low;
  173. }
  174. else if (base->ty_class == T_INTEGER) {
  175. tp->ty_size = (max_int[(int)base->ty_size] + 1) >> 2;
  176. tp->ty_setlow = -max_int[(int)base->ty_size] - 1;
  177. }
  178. else {
  179. assert(base->ty_class == T_UNSIGNED);
  180. tp->ty_size = (max_uns[(int)base->ty_size] + 1) >> 3;
  181. tp->ty_setlow = 0;
  182. }
  183. }
  184. init_types()
  185. {
  186. register int i = 0;
  187. register long x = 0;
  188. while (x >= 0) {
  189. i++;
  190. x = (x << 8) + 0377;
  191. max_uns[i] = x;
  192. max_int[i] = x & ~(1L << (8*i - 1));
  193. }
  194. int_type = basic_type(T_INTEGER, int_size);
  195. long_type = basic_type(T_INTEGER, long_size);
  196. short_type = basic_type(T_INTEGER, short_size);
  197. char_type = basic_type(T_INTEGER, char_size);
  198. uint_type = basic_type(T_UNSIGNED, int_size);
  199. ulong_type = basic_type(T_UNSIGNED, long_size);
  200. ushort_type = basic_type(T_UNSIGNED, short_size);
  201. uchar_type = basic_type(T_UNSIGNED, char_size);
  202. string_type = basic_type(T_STRING, 0L);
  203. void_type = basic_type(T_VOID, 0L);
  204. incomplete_type = basic_type(T_INCOMPLETE, 0L);
  205. float_type = basic_type(T_REAL, float_size);
  206. double_type = basic_type(T_REAL, double_size);
  207. i_types[0].maxval = max_int[(int)int_size]; i_types[0].type = int_type;
  208. i_types[1].maxval = max_int[(int)short_size]; i_types[1].type = short_type;
  209. i_types[2].maxval = max_int[(int)long_size]; i_types[2].type = long_type;
  210. u_types[0].maxval = max_uns[(int)int_size]; u_types[0].type = uint_type;
  211. u_types[1].maxval = max_uns[(int)short_size]; u_types[1].type = ushort_type;
  212. u_types[2].maxval = max_uns[(int)long_size]; u_types[2].type = ulong_type;
  213. u_types[3].maxval = max_uns[1]; u_types[3].type = uchar_type;
  214. }
  215. /*
  216. * Some code to handle type indices, which are pairs of integers.
  217. * What we need is a two-dimensional array, but we don't know how large
  218. * it is going to be, so we use a list of rows instead.
  219. */
  220. static struct tp_index {
  221. unsigned len;
  222. p_type *row;
  223. } *list_row;
  224. static unsigned list_len;
  225. #define NINCR 10
  226. p_type *
  227. tp_lookup(type_index)
  228. int *type_index;
  229. {
  230. register int i;
  231. register struct tp_index *p;
  232. while (type_index[0] >= list_len) {
  233. if (list_len) {
  234. list_row = (struct tp_index *) Realloc((char *) list_row,
  235. (list_len += NINCR) * sizeof(struct tp_index));
  236. }
  237. else list_row = (struct tp_index *)
  238. Malloc((list_len = NINCR) * sizeof(struct tp_index));
  239. for (i = NINCR; i > 0; i--) {
  240. list_row[list_len - i].len = 0;
  241. }
  242. }
  243. p = &list_row[type_index[0]];
  244. while (type_index[1] >= p->len) {
  245. if (p->len) {
  246. p->row = (p_type *) Realloc((char *) p->row,
  247. (p->len += NINCR) * sizeof(p_type));
  248. }
  249. else p->row = (p_type *) Malloc((p->len = NINCR) * sizeof(p_type));
  250. for (i = NINCR; i > 0; i--) {
  251. p->row[p->len - i] = 0;
  252. }
  253. }
  254. return &(p->row[type_index[1]]);
  255. }
  256. clean_tp_tab()
  257. {
  258. if (list_len) {
  259. register int i = list_len;
  260. while (--i >= 0) {
  261. register int j = list_row[i].len;
  262. if (j) {
  263. while (--j > 0) {
  264. p_type p = list_row[i].row[j];
  265. if (p == incomplete_type) {
  266. error("incomplete type (%d,%d) 0x%x", i, j, &list_row[i].row[j]);
  267. }
  268. }
  269. free((char *) list_row[i].row);
  270. }
  271. }
  272. free((char *) list_row);
  273. list_len = 0;
  274. list_row = 0;
  275. }
  276. }
  277. end_literal(tp, maxval)
  278. register p_type tp;
  279. long maxval;
  280. {
  281. tp->ty_literals = (struct literal *)
  282. Realloc((char *) tp->ty_literals,
  283. tp->ty_nenums * sizeof(struct literal));
  284. if (ufit(maxval, 1)) tp->ty_size = 1;
  285. else if (ufit(maxval, (int)short_size)) tp->ty_size = short_size;
  286. else tp->ty_size = int_size;
  287. }
  288. long
  289. param_size(t, v)
  290. int v;
  291. p_type t;
  292. {
  293. if (v == 'i' || v == 'v') {
  294. /* addresss; only exception is a conformant array, which also
  295. takes a descriptor.
  296. */
  297. if (t->ty_class == T_ARRAY &&
  298. t->ty_index->ty_class == T_SUBRANGE &&
  299. t->ty_index->ty_A) {
  300. return pointer_size + 3 * int_size;
  301. }
  302. return pointer_size;
  303. }
  304. return ((t->ty_size + int_size - 1) / int_size) * int_size;
  305. }
  306. add_param_type(v, s)
  307. int v; /* 'v' or 'i' for address, 'p' for value */
  308. p_symbol s; /* parameter itself */
  309. {
  310. register p_scope sc = base_scope(s->sy_scope);
  311. register p_type prc_type;
  312. if (! sc) return;
  313. prc_type = sc->sc_definedby->sy_type;
  314. assert(prc_type->ty_class == T_PROCEDURE);
  315. prc_type->ty_nparams++;
  316. prc_type->ty_params = (struct param *) Realloc((char *) prc_type->ty_params,
  317. (unsigned)prc_type->ty_nparams * sizeof(struct param));
  318. prc_type->ty_params[prc_type->ty_nparams - 1].par_type = s->sy_type;
  319. prc_type->ty_params[prc_type->ty_nparams - 1].par_kind = v;
  320. prc_type->ty_nbparams += param_size(s->sy_type, v);
  321. }
  322. /* Compute the size of a parameter of dynamic size
  323. */
  324. long
  325. compute_size(tp, AB)
  326. p_type tp;
  327. char *AB;
  328. {
  329. long low, high;
  330. assert(tp->ty_class == T_ARRAY);
  331. assert(tp->ty_index->ty_class == T_SUBRANGE);
  332. assert(tp->ty_index->ty_A != 0);
  333. if (tp->ty_index->ty_A & 1) {
  334. low = BUFTOI(AB+tp->ty_index->ty_low);
  335. } else low = tp->ty_index->ty_low;
  336. if (tp->ty_index->ty_A & 2) {
  337. high = BUFTOI(AB+tp->ty_index->ty_up);
  338. } else high = tp->ty_index->ty_up;
  339. return (high - low + 1) * tp->ty_elements->ty_size;
  340. }