enter.c 5.2 KB

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