enter.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* H I G H L E V E L S Y M B O L E N T R Y */
  2. #include <alloc.h>
  3. #include <assert.h>
  4. #include <em_arith.h>
  5. #include <em_label.h>
  6. #include "LLlex.h"
  7. #include "def.h"
  8. #include "idf.h"
  9. #include "main.h"
  10. #include "node.h"
  11. #include "scope.h"
  12. #include "type.h"
  13. #include "dbsymtab.h"
  14. extern int proclevel;
  15. extern int parlevel;
  16. struct def *
  17. Enter(name, kind, type, pnam)
  18. char *name;
  19. register struct type *type;
  20. long kind;
  21. {
  22. /* Enter a definition for "name" with kind "kind" and type
  23. "type" in the Current Scope. If it is a standard name, also
  24. put its number in the definition structure, and mark the
  25. name as set, to inhibit warnings about used before set.
  26. */
  27. register struct def *df;
  28. df = define(str2idf(name, 0), CurrentScope, kind);
  29. df->df_type = type;
  30. if( pnam ) {
  31. df->df_value.df_reqname = pnam;
  32. df->df_flags |= D_SET;
  33. }
  34. #ifdef DBSYMTAB
  35. else if (options['g']) stb_string(df, kind);
  36. #endif /* DBSYMTAB */
  37. return df;
  38. }
  39. EnterProgList(Idlist)
  40. register struct node *Idlist;
  41. {
  42. register struct node *idlist = Idlist;
  43. register struct def *df;
  44. for( ; idlist; idlist = idlist->nd_next )
  45. if ( !strcmp(input, idlist->nd_IDF->id_text)
  46. ||
  47. !strcmp(output, idlist->nd_IDF->id_text)
  48. ) {
  49. /* the occurence of input or output as program-
  50. * parameter is their declaration as a GLOBAL
  51. * variable of type text
  52. */
  53. if( df = define(idlist->nd_IDF, CurrentScope,
  54. D_VARIABLE) ) {
  55. df->df_type = text_type;
  56. df->df_flags |= (D_SET | D_PROGPAR | D_NOREG);
  57. if( !strcmp(input, idlist->nd_IDF->id_text) ) {
  58. df->var_name = input;
  59. set_inp();
  60. }
  61. else {
  62. df->var_name = output;
  63. set_outp();
  64. }
  65. #ifdef DBSYMTAB
  66. if (options['g']) stb_string(df, D_VARIABLE);
  67. #endif /* DBSYMTAB */
  68. }
  69. }
  70. else {
  71. if( df = define(idlist->nd_IDF, CurrentScope,
  72. D_PARAMETER) ) {
  73. df->df_type = error_type;
  74. df->df_flags |= D_PROGPAR;
  75. df->var_name = idlist->nd_IDF->id_text;
  76. }
  77. }
  78. FreeNode(Idlist);
  79. }
  80. EnterEnumList(Idlist, type)
  81. struct node *Idlist;
  82. register struct type *type;
  83. {
  84. /* Put a list of enumeration literals in the symbol table.
  85. They all have type "type". Also assign numbers to them.
  86. */
  87. register struct def *df, *df1 = 0;
  88. register struct node *idlist = Idlist;
  89. type->enm_ncst = 0;
  90. for( ; idlist; idlist = idlist->nd_next )
  91. if( df = define(idlist->nd_IDF, CurrentScope, D_ENUM) ) {
  92. df->df_type = type;
  93. df->enm_val = (type->enm_ncst)++;
  94. df->df_flags |= D_SET;
  95. if (! df1) {
  96. type->enm_enums = df;
  97. }
  98. else df1->enm_next = df;
  99. df1 = df;
  100. }
  101. FreeNode(Idlist);
  102. }
  103. EnterFieldList(Idlist, type, scope, addr, packed)
  104. struct node *Idlist;
  105. register struct type *type;
  106. struct scope *scope;
  107. arith *addr;
  108. unsigned short packed;
  109. {
  110. /* Put a list of fields in the symbol table.
  111. They all have type "type", and are put in scope "scope".
  112. */
  113. register struct def *df;
  114. register struct node *idlist = Idlist;
  115. for( ; idlist; idlist = idlist->nd_next )
  116. if( df = define(idlist->nd_IDF, scope, D_FIELD) ) {
  117. df->df_type = type;
  118. if( packed ) {
  119. df->fld_flags |= F_PACKED;
  120. df->fld_off = align(*addr, type->tp_palign);
  121. *addr = df->fld_off + type->tp_psize;
  122. }
  123. else {
  124. df->fld_off = align(*addr, type->tp_align);
  125. *addr = df->fld_off + type->tp_size;
  126. }
  127. }
  128. FreeNode(Idlist);
  129. }
  130. EnterVarList(Idlist, type, local)
  131. struct node *Idlist;
  132. struct type *type;
  133. {
  134. /* Enter a list of identifiers representing variables into the
  135. name list. "type" represents the type of the variables.
  136. "local" is set if the variables are declared local to a
  137. procedure.
  138. */
  139. register struct def *df;
  140. register struct node *idlist = Idlist;
  141. register struct scopelist *sc = CurrVis;
  142. for( ; idlist; idlist = idlist->nd_next ) {
  143. if( !(df = define(idlist->nd_IDF, CurrentScope, D_VARIABLE)) )
  144. continue; /* skip this identifier */
  145. df->df_type = type;
  146. if( local ) {
  147. /* subtract size, which is already aligned, of
  148. * variable to the offset, as the variable list
  149. * exists only local to a procedure
  150. */
  151. sc->sc_scope->sc_off -= type->tp_size;
  152. df->var_off = sc->sc_scope->sc_off;
  153. }
  154. else { /* Global name */
  155. df->var_name = df->df_idf->id_text;
  156. df->df_flags |= D_NOREG;
  157. }
  158. #ifdef DBSYMTAB
  159. if (options['g']) stb_string(df, D_VARIABLE);
  160. #endif /* DBSYMTAB */
  161. }
  162. FreeNode(Idlist);
  163. }
  164. arith
  165. EnterParamList(fpl, parlist)
  166. register struct node *fpl;
  167. struct paramlist **parlist;
  168. {
  169. register arith nb_pars = (proclevel > 1) ? pointer_size : 0;
  170. register struct node *id;
  171. struct type *tp;
  172. struct def *df;
  173. for( ; fpl; fpl = fpl->nd_right ) {
  174. assert(fpl->nd_class == Link);
  175. tp = fpl->nd_type;
  176. for( id = fpl->nd_left; id; id = id->nd_next )
  177. if( df = define(id->nd_IDF, CurrentScope, D_VARIABLE) ) {
  178. df->var_off = nb_pars;
  179. if( fpl->nd_INT & D_VARPAR || IsConformantArray(tp) )
  180. nb_pars += pointer_size;
  181. else
  182. nb_pars += tp->tp_size;
  183. LinkParam(parlist, df);
  184. df->df_type = tp;
  185. df->df_flags |= fpl->nd_INT;
  186. }
  187. while( IsConformantArray(tp) ) {
  188. /* we need room for the descriptors */
  189. tp->arr_sclevel = CurrentScope->sc_level;
  190. tp->arr_cfdescr = nb_pars;
  191. nb_pars += 3 * word_size;
  192. tp = tp->arr_elem;
  193. }
  194. }
  195. return nb_pars;
  196. }
  197. arith
  198. EnterParTypes(fpl, parlist)
  199. register struct node *fpl;
  200. struct paramlist **parlist;
  201. {
  202. /* Parameters in heading of procedural and functional
  203. parameters (only types are important, not the names).
  204. */
  205. register arith nb_pars = 0;
  206. register struct node *id;
  207. struct type *tp;
  208. struct def *df;
  209. for( ; fpl; fpl = fpl->nd_right ) {
  210. tp = fpl->nd_type;
  211. for( id = fpl->nd_left; id; id = id->nd_next )
  212. if( df = new_def() ) {
  213. if( fpl->nd_INT & D_VARPAR ||
  214. IsConformantArray(tp) )
  215. nb_pars += pointer_size;
  216. else
  217. nb_pars += tp->tp_size;
  218. LinkParam(parlist, df);
  219. df->df_type = tp;
  220. df->df_flags |= fpl->nd_INT;
  221. }
  222. while( IsConformantArray(tp) ) {
  223. nb_pars += 3 * word_size;
  224. tp = tp->arr_elem;
  225. }
  226. }
  227. return nb_pars;
  228. }
  229. LinkParam(parlist, df)
  230. struct paramlist **parlist;
  231. struct def *df;
  232. {
  233. static struct paramlist *pr;
  234. if( !*parlist )
  235. *parlist = pr = new_paramlist();
  236. else {
  237. pr->next = new_paramlist();
  238. pr = pr->next;
  239. }
  240. pr->par_def = df;
  241. }