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