stack.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 = stl->sl_max_block = local_level->sl_local_offset;
  42. local_level = stl;
  43. }
  44. stack_idf(idf, stl)
  45. struct idf *idf;
  46. struct stack_level *stl;
  47. {
  48. /* The identifier idf is inserted in the stack on level stl.
  49. */
  50. register struct stack_entry *se = new_stack_entry();
  51. clear((char *)se, sizeof(struct stack_entry));
  52. /* link it into the stack level */
  53. se->next = stl->sl_entry;
  54. se->se_idf = idf;
  55. stl->sl_entry = se;
  56. }
  57. struct stack_level *
  58. stack_level_of(lvl)
  59. {
  60. /* The stack_level corresponding to level lvl is returned.
  61. The stack should probably be an array, to be extended with
  62. realloc where needed.
  63. */
  64. if (lvl == level)
  65. return local_level;
  66. else {
  67. register struct stack_level *stl = &UniversalLevel;
  68. while (stl->sl_level != lvl)
  69. stl = stl->sl_next;
  70. return stl;
  71. }
  72. /*NOTREACHED*/
  73. }
  74. unstack_level()
  75. {
  76. /* The top level of the identifier stack is removed.
  77. */
  78. struct stack_level *lastlvl;
  79. #ifdef DEBUG
  80. if (options['t'])
  81. dumpidftab("before unstackidfs", 0);
  82. #endif DEBUG
  83. /* The implementation below is more careful than strictly
  84. necessary. Optimists may optimize it afterwards.
  85. */
  86. while (local_level->sl_entry) {
  87. register struct stack_entry *se = local_level->sl_entry;
  88. register struct idf *idf = se->se_idf;
  89. register struct def *def;
  90. register struct sdef *sdef;
  91. register struct tag *tag;
  92. /* unlink it from the local stack level */
  93. local_level->sl_entry = se->next;
  94. free_stack_entry(se);
  95. while ((def = idf->id_def) && def->df_level >= level) {
  96. /* unlink it from the def list under the idf block */
  97. if (def->df_sc == LABEL)
  98. unstack_label(idf);
  99. else
  100. if (level == L_LOCAL || level == L_FORMAL1) {
  101. if ( def->df_register != REG_NONE &&
  102. def->df_sc != STATIC &&
  103. options['n'] == 0
  104. ) {
  105. int reg;
  106. switch (def->df_type->tp_fund) {
  107. case POINTER:
  108. reg = reg_pointer;
  109. break;
  110. case FLOAT:
  111. case DOUBLE:
  112. reg = reg_float;
  113. break;
  114. default:
  115. reg = reg_any;
  116. break;
  117. }
  118. C_ms_reg(def->df_address,
  119. def->df_type->tp_size,
  120. reg, def->df_register
  121. );
  122. }
  123. }
  124. idf->id_def = def->next;
  125. free_def(def);
  126. update_ahead(idf);
  127. }
  128. while ((sdef = idf->id_sdef) && sdef->sd_level >= level) {
  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_struct) && tag->tg_level >= level) {
  134. /* unlink it from the struct list under the idf block */
  135. idf->id_struct = tag->next;
  136. free_tag(tag);
  137. }
  138. while ((tag = idf->id_enum) && tag->tg_level >= level) {
  139. /* unlink it from the enum list under the idf block */
  140. idf->id_enum = tag->next;
  141. free_tag(tag);
  142. }
  143. }
  144. /* Unlink the local stack level from the stack.
  145. */
  146. lastlvl = local_level;
  147. local_level = local_level->sl_previous;
  148. if (level > L_LOCAL && lastlvl->sl_max_block < local_level->sl_max_block)
  149. local_level->sl_max_block = lastlvl->sl_max_block;
  150. free_stack_level(lastlvl);
  151. local_level->sl_next = (struct stack_level *) 0;
  152. level = local_level->sl_level;
  153. #ifdef DEBUG
  154. if (options['t'])
  155. dumpidftab("after unstackidfs", 0);
  156. #endif DEBUG
  157. }
  158. unstack_world()
  159. {
  160. /* The global level of identifiers is scanned, and final
  161. decisions are taken about such issues as
  162. extern/static/global and un/initialized.
  163. Effects on the code generator: initialised variables
  164. have already been encoded while the uninitialised ones
  165. are not and have to be encoded at this moment.
  166. */
  167. struct stack_entry *se = local_level->sl_entry;
  168. open_name_list();
  169. while (se) {
  170. register struct idf *idf = se->se_idf;
  171. register struct def *def = idf->id_def;
  172. if (!def) {
  173. /* global selectors, etc. */
  174. se = se->next;
  175. continue;
  176. }
  177. #ifdef DEBUG
  178. if (options['a']) {
  179. printf("\"%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. def->df_initialized ? "init" : "no init",
  186. def->df_used ? "used" : "not used");
  187. }
  188. #endif DEBUG
  189. /* find final storage class */
  190. if (def->df_sc == GLOBAL || def->df_sc == IMPLICIT) {
  191. /* even now we still don't know */
  192. def->df_sc = EXTERN;
  193. }
  194. if ( def->df_sc == STATIC
  195. && def->df_type->tp_fund == FUNCTION
  196. && !def->df_initialized
  197. ) {
  198. /* orphaned static function */
  199. if (options['R'])
  200. warning("static function %s never defined, %s",
  201. idf->id_text,
  202. "changed to extern"
  203. );
  204. def->df_sc = EXTERN;
  205. }
  206. if ( def->df_alloc == ALLOC_SEEN &&
  207. !def->df_initialized
  208. ) {
  209. /* space must be allocated */
  210. bss(idf);
  211. namelist(idf->id_text); /* may be common */
  212. def->df_alloc = ALLOC_DONE;
  213. /* df_alloc must be set to ALLOC_DONE because
  214. the idf entry may occur several times in
  215. the list.
  216. The reason is that the same name may be used
  217. for different purposes on the same level, e.g
  218. struct s {int s;} s;
  219. is a legal definition and contains 3 defining
  220. occurrences of s. Each definition has been
  221. entered into the idfstack. Although only
  222. one of them concerns a variable, we meet the
  223. s 3 times when scanning the idfstack.
  224. */
  225. }
  226. se = se->next;
  227. }
  228. }
  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 int nfd;
  236. open_name_list()
  237. {
  238. if (nmlist) {
  239. if ((nfd = sys_creat(nmlist, 0644)) < 0) {
  240. fatal("cannot create namelist %s", nmlist);
  241. }
  242. }
  243. }
  244. namelist(nm)
  245. char *nm;
  246. {
  247. if (nmlist) {
  248. sys_write(nfd, nm, strlen(nm));
  249. sys_write(nfd, "\n", 1);
  250. }
  251. }