type.c 9.5 KB

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