stack.c 6.9 KB

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