stack.c 6.9 KB

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