stack.c 6.9 KB

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