stack.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. register struct stack_level *loclev = local_level;
  38. loclev->sl_next = stl;
  39. stl->sl_previous = loclev;
  40. stl->sl_level = ++level;
  41. stl->sl_local_offset =
  42. stl->sl_max_block = loclev->sl_local_offset;
  43. local_level = stl;
  44. }
  45. stack_idf(idf, stl)
  46. struct idf *idf;
  47. register 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. /* 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. def->df_type->tp_size > 0 &&
  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. #ifndef NOFLOAT
  112. case FLOAT:
  113. case DOUBLE:
  114. reg = reg_float;
  115. break;
  116. #endif NOFLOAT
  117. default:
  118. reg = reg_any;
  119. break;
  120. }
  121. C_ms_reg(def->df_address,
  122. def->df_type->tp_size,
  123. reg, def->df_register
  124. );
  125. }
  126. }
  127. idf->id_def = def->next;
  128. free_def(def);
  129. update_ahead(idf);
  130. }
  131. while ( (sdef = idf->id_sdef)
  132. && sdef->sd_level >= level
  133. ) {
  134. /* unlink it from the sdef list under the idf block */
  135. idf->id_sdef = sdef->next;
  136. free_sdef(sdef);
  137. }
  138. while ( (tag = idf->id_struct)
  139. && tag->tg_level >= level
  140. ) {
  141. /* unlink it from the struct list under the idf block */
  142. idf->id_struct = tag->next;
  143. free_tag(tag);
  144. }
  145. while ((tag = idf->id_enum) && tag->tg_level >= level) {
  146. /* unlink it from the enum list under the idf block */
  147. idf->id_enum = tag->next;
  148. free_tag(tag);
  149. }
  150. }
  151. /* Unlink the local stack level from the stack.
  152. */
  153. lastlvl = local_level;
  154. local_level = local_level->sl_previous;
  155. if ( level > L_LOCAL
  156. && lastlvl->sl_max_block < local_level->sl_max_block
  157. ) {
  158. local_level->sl_max_block = lastlvl->sl_max_block;
  159. }
  160. free_stack_level(lastlvl);
  161. local_level->sl_next = (struct stack_level *) 0;
  162. level = local_level->sl_level;
  163. #ifdef DEBUG
  164. if (options['t'])
  165. dumpidftab("after unstackidfs", 0);
  166. #endif DEBUG
  167. }
  168. unstack_world()
  169. {
  170. /* The global level of identifiers is scanned, and final
  171. decisions are taken about such issues as
  172. extern/static/global and un/initialized.
  173. Effects on the code generator: initialised variables
  174. have already been encoded while the uninitialised ones
  175. are not and have to be encoded at this moment.
  176. */
  177. register struct stack_entry *se = local_level->sl_entry;
  178. open_name_list();
  179. while (se) {
  180. register struct idf *idf = se->se_idf;
  181. register struct def *def = idf->id_def;
  182. if (!def) {
  183. /* global selectors, etc. */
  184. se = se->next;
  185. continue;
  186. }
  187. #ifdef DEBUG
  188. if (options['a']) {
  189. char *symbol2str();
  190. print("\"%s\", %s, %s, %s, %s\n",
  191. idf->id_text,
  192. (def->df_alloc == 0) ? "no alloc" :
  193. (def->df_alloc == ALLOC_SEEN) ? "alloc seen" :
  194. (def->df_alloc == ALLOC_DONE) ? "alloc done" :
  195. "illegal alloc info",
  196. symbol2str(def->df_sc),
  197. def->df_initialized ? "init" : "no init",
  198. def->df_used ? "used" : "not used");
  199. }
  200. #endif DEBUG
  201. /*
  202. /_* find final storage class *_/
  203. if (def->df_sc == GLOBAL || def->df_sc == IMPLICIT)
  204. /_* even now we still don't know *_/
  205. def->df_sc = EXTERN;
  206. */
  207. if ( def->df_sc == STATIC
  208. && def->df_type->tp_fund == FUNCTION
  209. && !def->df_initialized
  210. ) {
  211. /* orphaned static function */
  212. if (options['R'])
  213. warning("static function %s never defined, %s",
  214. idf->id_text,
  215. "changed to extern"
  216. );
  217. def->df_sc = EXTERN;
  218. }
  219. if (
  220. def->df_alloc == ALLOC_SEEN &&
  221. !def->df_initialized
  222. ) {
  223. /* space must be allocated */
  224. bss(idf);
  225. if (def->df_sc != STATIC)
  226. namelist(idf->id_text); /* may be common */
  227. def->df_alloc = ALLOC_DONE; /* see Note below */
  228. }
  229. se = se->next;
  230. }
  231. /* Note:
  232. df_alloc must be set to ALLOC_DONE because the idf entry
  233. may occur several times in the list.
  234. The reason for this is that the same name may be used
  235. for different purposes on the same level, e.g.
  236. struct s {int s;} s;
  237. is a legal definition and contains 3 defining occurrences
  238. of s.
  239. Each definition has been entered into the identifier stack.
  240. Although only one of them concerns a variable, we meet the
  241. s 3 times when scanning the identifier stack.
  242. */
  243. }
  244. /* A list of potential common names is kept, to be fed to
  245. an understanding loader. The list is written to a file
  246. the name of which is nmlist. If nmlist == NULL, no name
  247. list is generated.
  248. */
  249. extern char *nmlist; /* BAH! -- main.c */
  250. static File *nfp = 0;
  251. open_name_list()
  252. {
  253. if (nmlist && sys_open(nmlist, OP_WRITE, &nfp) == 0)
  254. fatal("cannot create namelist %s", nmlist);
  255. }
  256. namelist(nm)
  257. char *nm;
  258. {
  259. if (nmlist) {
  260. sys_write(nfp, nm, strlen(nm));
  261. sys_write(nfp, "\n", 1);
  262. }
  263. }