stack.c 7.2 KB

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