stack.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* S T A C K / U N S T A C K R O U T I N E S */
  7. #include "lint.h"
  8. #include <system.h>
  9. #ifndef LINT
  10. #include <em.h>
  11. #else
  12. #include "l_em.h"
  13. #endif /* LINT */
  14. #include "debug.h"
  15. #include "botch_free.h"
  16. #include <alloc.h>
  17. #include "Lpars.h"
  18. #include "arith.h"
  19. #include "stack.h"
  20. #include "type.h"
  21. #include "idf.h"
  22. #include "def.h"
  23. #include "struct.h"
  24. #include "level.h"
  25. #include "mes.h"
  26. #include "code_c.h"
  27. /* #include <em_reg.h> */
  28. extern char options[];
  29. static struct stack_level UniversalLevel;
  30. struct stack_level *local_level = &UniversalLevel;
  31. /* The main reason for having this secondary stacking
  32. mechanism besides the linked lists pointed to by the idf's
  33. is efficiency.
  34. To remove the idf's of a given level, one could scan the
  35. hash table and chase down the idf chains; with a hash
  36. table size of 100 this is feasible, but with a size of say
  37. 100000 this becomes painful. Therefore all idf's are also
  38. kept in a stack of sets, one set for each level.
  39. */
  40. int level; /* Always equal to local_level->sl_level. */
  41. stack_level() {
  42. /* A new level is added on top of the identifier stack.
  43. */
  44. register struct stack_level *stl = new_stack_level();
  45. register struct stack_level *loclev = local_level;
  46. loclev->sl_next = stl;
  47. stl->sl_previous = loclev;
  48. stl->sl_level = ++level;
  49. stl->sl_max_block = loclev->sl_max_block;
  50. local_level = stl;
  51. #ifdef LINT
  52. lint_start_local();
  53. #endif /* LINT */
  54. }
  55. stack_idf(idf, stl)
  56. struct idf *idf;
  57. register struct stack_level *stl;
  58. {
  59. /* The identifier idf is inserted in the stack on level stl,
  60. but only if it is not already present at this level.
  61. */
  62. register struct stack_entry *se;
  63. se = stl->sl_entry;
  64. while (se) {
  65. if (se->se_idf == idf) return;
  66. se = se->next;
  67. }
  68. se = new_stack_entry();
  69. /* link it into the stack level */
  70. se->next = stl->sl_entry;
  71. se->se_idf = idf;
  72. stl->sl_entry = se;
  73. }
  74. struct stack_level *
  75. stack_level_of(lvl)
  76. {
  77. /* The stack_level corresponding to level lvl is returned.
  78. The stack should probably be an array, to be extended with
  79. realloc where needed.
  80. */
  81. register struct stack_level *stl;
  82. if (lvl == level)
  83. return local_level;
  84. stl = &UniversalLevel;
  85. while (stl->sl_level != lvl) {
  86. stl = stl->sl_next;
  87. }
  88. return stl;
  89. }
  90. unstack_level()
  91. {
  92. /* The top level of the identifier stack is removed.
  93. */
  94. struct stack_level *lastlvl;
  95. #ifdef DEBUG
  96. if (options['t'])
  97. dumpidftab("before unstackidfs", 0);
  98. #endif /* DEBUG */
  99. #ifdef LINT
  100. lint_end_local(local_level);
  101. #endif /* LINT */
  102. /* The implementation below is more careful than strictly
  103. necessary. Optimists may optimize it afterwards.
  104. */
  105. while (local_level->sl_entry) {
  106. register struct stack_entry *se = local_level->sl_entry;
  107. register struct idf *idf = se->se_idf;
  108. register struct def *def;
  109. register struct sdef *sdef;
  110. register struct tag *tag;
  111. /* unlink it from the local stack level */
  112. local_level->sl_entry = se->next;
  113. free_stack_entry(se);
  114. if (level == L_LOCAL && (def = idf->id_label)) {
  115. unstack_label(idf);
  116. free_def(def);
  117. idf->id_label = 0;
  118. }
  119. while ((def = idf->id_def) && def->df_level >= level) {
  120. /* unlink it from the def list under the idf block */
  121. if (def->df_sc == REGISTER || def->df_sc == AUTO)
  122. FreeLocal(def->df_address);
  123. idf->id_def = def->next;
  124. free_def(def);
  125. update_ahead(idf);
  126. }
  127. while ( (sdef = idf->id_sdef)
  128. && sdef->sd_level >= level
  129. ) {
  130. /* unlink it from the sdef list under the idf block */
  131. idf->id_sdef = sdef->next;
  132. free_sdef(sdef);
  133. }
  134. while ( (tag = idf->id_tag)
  135. && tag->tg_level >= level
  136. ) {
  137. /* unlink it from the struct list under the idf block */
  138. idf->id_tag = tag->next;
  139. free_tag(tag);
  140. }
  141. }
  142. /* Unlink the local stack level from the stack.
  143. */
  144. lastlvl = local_level;
  145. local_level = local_level->sl_previous;
  146. if (level >= L_LOCAL) {
  147. local_level->sl_max_block = lastlvl->sl_max_block;
  148. }
  149. free_stack_level(lastlvl);
  150. local_level->sl_next = (struct stack_level *) 0;
  151. level = local_level->sl_level;
  152. #ifdef DEBUG
  153. if (options['t'])
  154. dumpidftab("after unstackidfs", 0);
  155. #endif /* DEBUG */
  156. }
  157. unstack_world()
  158. {
  159. /* The global level of identifiers is scanned, and final
  160. decisions are taken about such issues as
  161. extern/static/global and un/initialized.
  162. Effects on the code generator: initialised variables
  163. have already been encoded while the uninitialised ones
  164. are not and have to be encoded at this moment.
  165. */
  166. register struct stack_entry *se = local_level->sl_entry;
  167. #ifdef LINT
  168. lint_end_global(local_level);
  169. #endif /* LINT */
  170. #ifdef GEN_NM_LIST
  171. open_name_list();
  172. #endif /* GEN_NM_LIST */
  173. while (se) {
  174. register struct idf *idf = se->se_idf;
  175. register struct def *def = idf->id_def;
  176. if (!def) {
  177. /* global selectors, etc. */
  178. se = se->next;
  179. continue;
  180. }
  181. #ifdef DEBUG
  182. if (options['a']) {
  183. char *symbol2str();
  184. print("\"%s\", %s, %s, %s, %s\n",
  185. idf->id_text,
  186. (def->df_alloc == 0) ? "no alloc" :
  187. (def->df_alloc == ALLOC_SEEN) ? "alloc seen" :
  188. (def->df_alloc == ALLOC_DONE) ? "alloc done" :
  189. "illegal alloc info",
  190. symbol2str(def->df_sc),
  191. def->df_initialized ? "init" : "no init",
  192. def->df_used ? "used" : "not used");
  193. }
  194. #endif /* DEBUG */
  195. if (def->df_sc == STATIC
  196. && def->df_type->tp_fund == FUNCTION
  197. && !def->df_initialized) {
  198. /* orphaned static function */
  199. warning("static function %s never defined, %s"
  200. , idf->id_text
  201. , "changed to extern");
  202. def->df_sc = EXTERN;
  203. }
  204. if (def->df_alloc == ALLOC_SEEN
  205. && !def->df_initialized) {
  206. /* space must be allocated */
  207. bss(idf);
  208. #ifdef GEN_NM_LIST
  209. if (def->df_sc != STATIC)
  210. namelist(idf->id_text); /* may be common */
  211. #endif /* GEN_NM_LIST */
  212. def->df_alloc = ALLOC_DONE; /* see Note below */
  213. }
  214. se = se->next;
  215. }
  216. /* Note:
  217. df_alloc must be set to ALLOC_DONE because the idf entry
  218. may occur several times in the list.
  219. The reason for this is that the same name may be used
  220. for different purposes on the same level, e.g.
  221. struct s {int s;} s;
  222. is a legal definition and contains 3 defining occurrences
  223. of s.
  224. Each definition has been entered into the identifier stack.
  225. Although only one of them concerns a variable, we meet the
  226. s 3 times when scanning the identifier stack.
  227. */
  228. }
  229. #ifdef GEN_NM_LIST
  230. /* A list of potential common names is kept, to be fed to
  231. an understanding loader. The list is written to a file
  232. the name of which is nmlist. If nmlist == NULL, no name
  233. list is generated.
  234. */
  235. extern char *nmlist; /* BAH! -- main.c */
  236. static File *nfp = 0;
  237. open_name_list()
  238. {
  239. if (nmlist && sys_open(nmlist, OP_WRITE, &nfp) == 0)
  240. fatal("cannot create namelist %s", nmlist);
  241. }
  242. namelist(nm)
  243. char *nm;
  244. {
  245. if (nmlist) {
  246. sys_write(nfp, nm, strlen(nm));
  247. sys_write(nfp, "\n", 1);
  248. }
  249. }
  250. #endif /* GEN_NM_LIST */