type.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* $Id$ */
  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. #include "misc.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;
  15. p_type float_type, double_type;
  16. p_type string_type, address_type;
  17. long int_size = sizeof(int),
  18. char_size = 1,
  19. short_size = sizeof(short),
  20. long_size = sizeof(long),
  21. pointer_size = sizeof(char *);
  22. long float_size = sizeof(float),
  23. double_size = sizeof(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. address_type = basic_type(T_POINTER, pointer_size);
  226. void_type = basic_type(T_VOID, 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. int indx = p->len/NINCR;
  267. if (p->len) {
  268. p->row = (p_type **) Realloc((char *) p->row,
  269. (unsigned) (indx + 1) * sizeof(p_type *));
  270. }
  271. else p->row = (p_type **) Malloc(sizeof(p_type *));
  272. p->len += NINCR;
  273. p->row[indx] = (p_type *) Malloc(NINCR * sizeof(p_type));
  274. for (i = NINCR-1; i >= 0; i--) {
  275. p->row[indx][i] = 0;
  276. }
  277. }
  278. return &(p->row[type_index[1]/NINCR][type_index[1]%NINCR]);
  279. }
  280. clean_tp_tab()
  281. {
  282. if (list_len) {
  283. register int i = list_len;
  284. while (--i >= 0) {
  285. register int j = list_row[i].len;
  286. if (j) {
  287. while (--j > 0) {
  288. p_type p = list_row[i].row[j/NINCR][j%NINCR];
  289. if (p && p->ty_class == 0) {
  290. error("%s: incomplete type (%d,%d)",
  291. FileScope->sc_definedby->sy_idf->id_text,
  292. i,
  293. j);
  294. }
  295. }
  296. j = list_row[i].len;
  297. while (j > 0) {
  298. free((char *) list_row[i].row[j/NINCR-1]);
  299. j -= NINCR;
  300. }
  301. free((char *) list_row[i].row);
  302. }
  303. }
  304. free((char *) list_row);
  305. list_len = 0;
  306. list_row = 0;
  307. }
  308. }
  309. end_literal(tp, maxval)
  310. register p_type tp;
  311. long maxval;
  312. {
  313. tp->ty_literals = (struct literal *)
  314. Realloc((char *) tp->ty_literals,
  315. tp->ty_nenums * sizeof(struct literal));
  316. if (ufit(maxval, 1)) tp->ty_size = 1;
  317. else if (ufit(maxval, (int)short_size)) tp->ty_size = short_size;
  318. else tp->ty_size = int_size;
  319. if (! bool_type) bool_type = tp;
  320. }
  321. long
  322. param_size(t, v)
  323. int v;
  324. p_type t;
  325. {
  326. if (v == 'i' || v == 'v') {
  327. /* addresss; only exception is a conformant array, which also
  328. takes a descriptor.
  329. */
  330. if (currlang == m2_dep &&
  331. t->ty_class == T_ARRAY &&
  332. t->ty_index->ty_class == T_SUBRANGE &&
  333. t->ty_index->ty_A) {
  334. return pointer_size + 3 * int_size;
  335. }
  336. return pointer_size;
  337. }
  338. return ((t->ty_size + int_size - 1) / int_size) * int_size;
  339. }
  340. add_param_type(v, s)
  341. int v; /* 'v' or 'i' for address, 'p' for value */
  342. p_symbol s; /* parameter itself */
  343. {
  344. register p_scope sc = base_scope(s->sy_scope);
  345. register p_type prc_type;
  346. if (! sc) return;
  347. prc_type = sc->sc_definedby->sy_type;
  348. assert(prc_type->ty_class == T_PROCEDURE);
  349. if (v == 'Z') {
  350. prc_type->ty_nbparams += 3 * int_size;
  351. return;
  352. }
  353. prc_type->ty_nparams++;
  354. prc_type->ty_params = (struct param *) Realloc((char *) prc_type->ty_params,
  355. (unsigned)prc_type->ty_nparams * sizeof(struct param));
  356. prc_type->ty_params[prc_type->ty_nparams - 1].par_type = s->sy_type;
  357. prc_type->ty_params[prc_type->ty_nparams - 1].par_kind = v;
  358. prc_type->ty_params[prc_type->ty_nparams - 1].par_off = s->sy_name.nm_value;
  359. prc_type->ty_nbparams += param_size(s->sy_type, v);
  360. }
  361. /* Compute the size of a parameter of dynamic size
  362. */
  363. long
  364. compute_size(tp, AB)
  365. p_type tp;
  366. char *AB;
  367. {
  368. long low, high;
  369. assert(tp->ty_class == T_ARRAY);
  370. assert(tp->ty_index->ty_class == T_SUBRANGE);
  371. assert(tp->ty_index->ty_A != 0);
  372. if (tp->ty_index->ty_A & 1) {
  373. low = get_int(AB+tp->ty_index->ty_low, int_size, T_INTEGER);
  374. } else low = tp->ty_index->ty_low;
  375. tp->ty_lb = low;
  376. if (tp->ty_index->ty_A & 2) {
  377. high = get_int(AB+tp->ty_index->ty_up, int_size, T_INTEGER);
  378. } else if (tp->ty_index->ty_A & 0200) {
  379. high = get_int(AB+tp->ty_index->ty_up, int_size, T_INTEGER);
  380. high += get_int(AB+tp->ty_index->ty_up+int_size, int_size, T_INTEGER);
  381. } else high = tp->ty_index->ty_up;
  382. tp->ty_hb = high;
  383. return (high - low + 1) * tp->ty_elements->ty_size;
  384. }