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. /* $Header$ */
  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 "nofloat.h"
  9. #include <system.h>
  10. #ifndef LINT
  11. #include <em.h>
  12. #else
  13. #include "em_lint.h"
  14. #endif LINT
  15. #include "debug.h"
  16. #include "botch_free.h"
  17. #include <alloc.h>
  18. #include "Lpars.h"
  19. #include "arith.h"
  20. #include "stack.h"
  21. #include "type.h"
  22. #include "idf.h"
  23. #include "def.h"
  24. #include "struct.h"
  25. #include "level.h"
  26. #include "mes.h"
  27. #include "noRoption.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. */
  61. register struct stack_entry *se = new_stack_entry();
  62. /* link it into the stack level */
  63. se->next = stl->sl_entry;
  64. se->se_idf = idf;
  65. stl->sl_entry = se;
  66. }
  67. struct stack_level *
  68. stack_level_of(lvl)
  69. {
  70. /* The stack_level corresponding to level lvl is returned.
  71. The stack should probably be an array, to be extended with
  72. realloc where needed.
  73. */
  74. register struct stack_level *stl;
  75. if (lvl == level)
  76. return local_level;
  77. stl = &UniversalLevel;
  78. while (stl->sl_level != lvl)
  79. stl = stl->sl_next;
  80. return stl;
  81. }
  82. unstack_level()
  83. {
  84. /* The top level of the identifier stack is removed.
  85. */
  86. struct stack_level *lastlvl;
  87. #ifdef DEBUG
  88. if (options['t'])
  89. dumpidftab("before unstackidfs", 0);
  90. #endif DEBUG
  91. #ifdef LINT
  92. lint_end_local(local_level);
  93. #endif LINT
  94. /* The implementation below is more careful than strictly
  95. necessary. Optimists may optimize it afterwards.
  96. */
  97. while (local_level->sl_entry) {
  98. register struct stack_entry *se = local_level->sl_entry;
  99. register struct idf *idf = se->se_idf;
  100. register struct def *def;
  101. register struct sdef *sdef;
  102. register struct tag *tag;
  103. /* unlink it from the local stack level */
  104. local_level->sl_entry = se->next;
  105. free_stack_entry(se);
  106. while ((def = idf->id_def) && def->df_level >= level) {
  107. /* unlink it from the def list under the idf block */
  108. if (def->df_sc == LABEL)
  109. unstack_label(idf);
  110. else if (def->df_sc == REGISTER || def->df_sc == AUTO)
  111. FreeLocal(def->df_address);
  112. idf->id_def = def->next;
  113. free_def(def);
  114. update_ahead(idf);
  115. }
  116. while ( (sdef = idf->id_sdef)
  117. && sdef->sd_level >= level
  118. ) {
  119. /* unlink it from the sdef list under the idf block */
  120. idf->id_sdef = sdef->next;
  121. free_sdef(sdef);
  122. }
  123. while ( (tag = idf->id_struct)
  124. && tag->tg_level >= level
  125. ) {
  126. /* unlink it from the struct list under the idf block */
  127. idf->id_struct = tag->next;
  128. free_tag(tag);
  129. }
  130. while ((tag = idf->id_enum) && tag->tg_level >= level) {
  131. /* unlink it from the enum list under the idf block */
  132. idf->id_enum = tag->next;
  133. free_tag(tag);
  134. }
  135. }
  136. /* Unlink the local stack level from the stack.
  137. */
  138. lastlvl = local_level;
  139. local_level = local_level->sl_previous;
  140. if (level >= L_LOCAL) {
  141. local_level->sl_max_block = lastlvl->sl_max_block;
  142. }
  143. free_stack_level(lastlvl);
  144. local_level->sl_next = (struct stack_level *) 0;
  145. level = local_level->sl_level;
  146. #ifdef DEBUG
  147. if (options['t'])
  148. dumpidftab("after unstackidfs", 0);
  149. #endif DEBUG
  150. }
  151. unstack_world()
  152. {
  153. /* The global level of identifiers is scanned, and final
  154. decisions are taken about such issues as
  155. extern/static/global and un/initialized.
  156. Effects on the code generator: initialised variables
  157. have already been encoded while the uninitialised ones
  158. are not and have to be encoded at this moment.
  159. */
  160. register struct stack_entry *se = local_level->sl_entry;
  161. #ifdef LINT
  162. lint_end_global(local_level);
  163. #endif LINT
  164. open_name_list();
  165. while (se) {
  166. register struct idf *idf = se->se_idf;
  167. register struct def *def = idf->id_def;
  168. if (!def) {
  169. /* global selectors, etc. */
  170. se = se->next;
  171. continue;
  172. }
  173. #ifdef DEBUG
  174. if (options['a']) {
  175. char *symbol2str();
  176. print("\"%s\", %s, %s, %s, %s\n",
  177. idf->id_text,
  178. (def->df_alloc == 0) ? "no alloc" :
  179. (def->df_alloc == ALLOC_SEEN) ? "alloc seen" :
  180. (def->df_alloc == ALLOC_DONE) ? "alloc done" :
  181. "illegal alloc info",
  182. symbol2str(def->df_sc),
  183. def->df_initialized ? "init" : "no init",
  184. def->df_used ? "used" : "not used");
  185. }
  186. #endif DEBUG
  187. /*
  188. /_* find final storage class *_/
  189. if (def->df_sc == GLOBAL || def->df_sc == IMPLICIT)
  190. /_* even now we still don't know *_/
  191. def->df_sc = EXTERN;
  192. */
  193. if ( def->df_sc == STATIC
  194. && def->df_type->tp_fund == FUNCTION
  195. && !def->df_initialized
  196. ) {
  197. /* orphaned static function */
  198. #ifndef NOROPTION
  199. if (options['R'])
  200. warning("static function %s never defined, %s",
  201. idf->id_text,
  202. "changed to extern"
  203. );
  204. #endif
  205. def->df_sc = EXTERN;
  206. }
  207. if (
  208. def->df_alloc == ALLOC_SEEN &&
  209. !def->df_initialized
  210. ) {
  211. /* space must be allocated */
  212. bss(idf);
  213. if (def->df_sc != STATIC)
  214. namelist(idf->id_text); /* may be common */
  215. def->df_alloc = ALLOC_DONE; /* see Note below */
  216. }
  217. se = se->next;
  218. }
  219. /* Note:
  220. df_alloc must be set to ALLOC_DONE because the idf entry
  221. may occur several times in the list.
  222. The reason for this is that the same name may be used
  223. for different purposes on the same level, e.g.
  224. struct s {int s;} s;
  225. is a legal definition and contains 3 defining occurrences
  226. of s.
  227. Each definition has been entered into the identifier stack.
  228. Although only one of them concerns a variable, we meet the
  229. s 3 times when scanning the identifier stack.
  230. */
  231. }
  232. /* A list of potential common names is kept, to be fed to
  233. an understanding loader. The list is written to a file
  234. the name of which is nmlist. If nmlist == NULL, no name
  235. list is generated.
  236. */
  237. extern char *nmlist; /* BAH! -- main.c */
  238. static File *nfp = 0;
  239. open_name_list()
  240. {
  241. if (nmlist && sys_open(nmlist, OP_WRITE, &nfp) == 0)
  242. fatal("cannot create namelist %s", nmlist);
  243. }
  244. namelist(nm)
  245. char *nm;
  246. {
  247. if (nmlist) {
  248. sys_write(nfp, nm, strlen(nm));
  249. sys_write(nfp, "\n", 1);
  250. }
  251. }