type.c 10 KB

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