stack.c 7.1 KB

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