stack.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* $Header$ */
  2. /* S T A C K / U N S T A C K R O U T I N E S */
  3. #include "nofloat.h"
  4. #include <system.h>
  5. #include <em.h>
  6. #include <em_reg.h>
  7. #include "debug.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 "level.h"
  18. #include "mes.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. register struct stack_level *stl = new_stack_level();
  37. local_level->sl_next = stl;
  38. stl->sl_previous = local_level;
  39. stl->sl_level = ++level;
  40. stl->sl_local_offset =
  41. stl->sl_max_block = local_level->sl_local_offset;
  42. local_level = stl;
  43. }
  44. stack_idf(idf, stl)
  45. struct idf *idf;
  46. register 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. /* link it into the stack level */
  52. se->next = stl->sl_entry;
  53. se->se_idf = idf;
  54. stl->sl_entry = se;
  55. }
  56. struct stack_level *
  57. stack_level_of(lvl)
  58. {
  59. /* The stack_level corresponding to level lvl is returned.
  60. The stack should probably be an array, to be extended with
  61. realloc where needed.
  62. */
  63. if (lvl == level)
  64. return local_level;
  65. else {
  66. register struct stack_level *stl = &UniversalLevel;
  67. while (stl->sl_level != lvl)
  68. stl = stl->sl_next;
  69. return stl;
  70. }
  71. /*NOTREACHED*/
  72. }
  73. unstack_level()
  74. {
  75. /* The top level of the identifier stack is removed.
  76. */
  77. struct stack_level *lastlvl;
  78. #ifdef DEBUG
  79. if (options['t'])
  80. dumpidftab("before unstackidfs", 0);
  81. #endif DEBUG
  82. /* The implementation below is more careful than strictly
  83. necessary. Optimists may optimize it afterwards.
  84. */
  85. while (local_level->sl_entry) {
  86. register struct stack_entry *se = local_level->sl_entry;
  87. register struct idf *idf = se->se_idf;
  88. register struct def *def;
  89. register struct sdef *sdef;
  90. register struct tag *tag;
  91. /* unlink it from the local stack level */
  92. local_level->sl_entry = se->next;
  93. free_stack_entry(se);
  94. while ((def = idf->id_def) && def->df_level >= level) {
  95. /* unlink it from the def list under the idf block */
  96. if (def->df_sc == LABEL)
  97. unstack_label(idf);
  98. else
  99. if (level == L_LOCAL || level == L_FORMAL1) {
  100. if ( def->df_register != REG_NONE &&
  101. def->df_sc != STATIC &&
  102. def->df_type->tp_size > 0 &&
  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. #ifndef NOFLOAT
  111. case FLOAT:
  112. case DOUBLE:
  113. reg = reg_float;
  114. break;
  115. #endif NOFLOAT
  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. register 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. char *symbol2str();
  189. print("\"%s\", %s, %s, %s, %s\n",
  190. idf->id_text,
  191. (def->df_alloc == 0) ? "no alloc" :
  192. (def->df_alloc == ALLOC_SEEN) ? "alloc seen" :
  193. (def->df_alloc == ALLOC_DONE) ? "alloc done" :
  194. "illegal alloc info",
  195. symbol2str(def->df_sc),
  196. def->df_initialized ? "init" : "no init",
  197. def->df_used ? "used" : "not used");
  198. }
  199. #endif DEBUG
  200. /*
  201. /_* find final storage class *_/
  202. if (def->df_sc == GLOBAL || def->df_sc == IMPLICIT)
  203. /_* even now we still don't know *_/
  204. def->df_sc = EXTERN;
  205. */
  206. if ( def->df_sc == STATIC
  207. && def->df_type->tp_fund == FUNCTION
  208. && !def->df_initialized
  209. ) {
  210. /* orphaned static function */
  211. if (options['R'])
  212. warning("static function %s never defined, %s",
  213. idf->id_text,
  214. "changed to extern"
  215. );
  216. def->df_sc = EXTERN;
  217. }
  218. if (
  219. def->df_alloc == ALLOC_SEEN &&
  220. !def->df_initialized
  221. ) {
  222. /* space must be allocated */
  223. bss(idf);
  224. if (def->df_sc != STATIC)
  225. namelist(idf->id_text); /* may be common */
  226. def->df_alloc = ALLOC_DONE; /* see Note below */
  227. }
  228. se = se->next;
  229. }
  230. /* Note:
  231. df_alloc must be set to ALLOC_DONE because the idf entry
  232. may occur several times in the list.
  233. The reason for this is that the same name may be used
  234. for different purposes on the same level, e.g.
  235. struct s {int s;} s;
  236. is a legal definition and contains 3 defining occurrences
  237. of s.
  238. Each definition has been entered into the identifier stack.
  239. Although only one of them concerns a variable, we meet the
  240. s 3 times when scanning the identifier stack.
  241. */
  242. }
  243. /* A list of potential common names is kept, to be fed to
  244. an understanding loader. The list is written to a file
  245. the name of which is nmlist. If nmlist == NULL, no name
  246. list is generated.
  247. */
  248. extern char *nmlist; /* BAH! -- main.c */
  249. static File *nfp = 0;
  250. open_name_list()
  251. {
  252. if (nmlist && sys_open(nmlist, OP_WRITE, &nfp) == 0)
  253. fatal("cannot create namelist %s", nmlist);
  254. }
  255. namelist(nm)
  256. char *nm;
  257. {
  258. if (nmlist) {
  259. sys_write(nfp, nm, strlen(nm));
  260. sys_write(nfp, "\n", 1);
  261. }
  262. }