home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / iconc / ccode.h < prev    next >
C/C++ Source or Header  |  1996-03-22  |  11KB  |  252 lines

  1. /*
  2.  * ccode.h - definitions used in code generation.
  3.  */
  4.  
  5. /*
  6.  * ChkPrefix - allocate a prefix to x if it has not already been done.
  7.  */
  8. #define ChkPrefix(x) if ((x)[0] == '\0') nxt_pre(x, pre, PrfxSz);
  9.  
  10. /*
  11.  * sig_act - list of possible signals returned by a call and the action to be
  12.  *  to be taken when the signal is returned: in effect a switch statement.
  13.  */
  14. struct sig_act {
  15.    struct code *sig;         /* signal */
  16.    struct code *cd;          /* action to be taken: goto, return, break */
  17.    struct sig_act *shar_act; /* signals that share this action */
  18.    struct sig_act *next;
  19.    };
  20.  
  21. /*
  22.  * val_loc - location of a value. Used for intermediate and final results
  23.  *   of expressions.
  24.  */
  25. #define V_NamedVar 1  /* Icon named variable indicated by nvar */
  26. #define V_Temp     2  /* temporary variable indicated by tmp */
  27. #define V_ITemp    3  /* C integer temporary variable indicated by tmp */
  28. #define V_DTemp    4  /* C double temporary variable indicated by tmp */
  29. #define V_PRslt    5  /* procedure result location */
  30. #define V_Const    6  /* integer constant - used for size of varargs */
  31. #define V_CVar     7  /* C named variable */
  32. #define V_Ignore   8  /* "trashcan" - a write-only location */
  33.  
  34. #define M_None     0  /* access simply as descriptor */
  35. #define M_CharPtr  1  /* access v-word as "char *" */
  36. #define M_BlkPtr   2  /* access v-word as block pointer using blk_name */
  37. #define M_CInt     3  /* access v-word as C integer */
  38. #define M_Addr     4  /* address of descriptor for varargs */
  39. struct val_loc {
  40.    int loc_type;      /* manifest constants V_* */
  41.    int mod_access;    /* manifest constants M_* */
  42.    char *blk_name;    /* used with M_BlkPtr */
  43.    union {
  44.      struct lentry *nvar; /* Icon named variable */
  45.      int tmp;             /* index of temporary variable */
  46.      int int_const;       /* integer constant value */
  47.      char *name;          /* C named variable */
  48.      } u;
  49.    };
  50.  
  51. /*
  52.  * "code" contains the information needed to print a piece of C code.
  53.  *  C_... manifest constants are cd_id's. These are followed by
  54.  *  corresponding field access expressions.
  55.  */
  56. #define Rslt        fld[0].vloc    /* place to put result of expression */
  57. #define Cont        fld[1].fnc       /* continuation function or null */
  58.  
  59. #define C_Null      0              /* no code */
  60.  
  61. #define C_NamedVar  1           /* reference to a named variable */
  62. /* uses Rslt */
  63. #define NamedVar    fld[1].nvar
  64.  
  65. #define C_CallSig   2           /* call and handling of returned signal */
  66. #define OperName    fld[0].oper_nm /* run-time routine name or null */
  67. /* uses Cont */
  68. #define Flags       fld[2].n       /* flag: NeedCont, ForeignSig */
  69. #define ArgLst      fld[3].cd       /* argument list */
  70. #define ContFail    fld[4].cd       /* label/signal to goto/return on failure */
  71. #define SigActs     fld[5].sa       /* actions to take for returned signals */
  72. #define NextCall    fld[6].cd       /* for chaining calls within a continuation*/
  73. #define NeedCont    1              /* pass NULL continuation if Cont == NULL */
  74. #define ForeignSig  2              /* may get foreign signal from a suspend */
  75.  
  76. #define C_RetSig    3            /* return signal */
  77. #define SigRef      fld[0].sigref  /* pointer to func's reference to signal */
  78.  
  79. #define C_Goto      4           /* goto label */
  80. #define Lbl         fld[0].cd      /* label */
  81.  
  82. #define C_Label     5           /* statment label "Ln:" and signal "n" */
  83. #define Container   fld[0].fnc       /* continuation containing label */
  84. #define SeqNum      fld[1].n       /* sequence number, n */
  85. #define Desc        fld[2].s       /* description of how label/signal is used */
  86. #define RefCnt      fld[3].n       /* reference count for label */
  87. #define LabFlg      fld[4].n       /* flag: FncPtrd, BndSig */
  88. #define FncPrtd     1           /*   function sig_n has been printed */
  89. #define Bounding    2           /*   this is a bounding label */
  90.  
  91. #define C_Lit      6            /* literal (integer, real, string, cset) */
  92. /* uses Rslt */
  93. #define Literal    fld[1].lit
  94.  
  95. #define C_Resume   7           /* resume signal */
  96. #define C_Continue 8            /* continue signal */
  97. #define C_FallThru 9           /* fall through signal */
  98. #define C_PFail    10           /* procedure failure */
  99. #define C_PRet     11           /* procedure return (result already set) */
  100. #define C_PSusp    12           /* procedure suspend */
  101. #define C_Break    13           /* break out of signal handling switch */
  102. #define C_LBrack   14              /* '{' */
  103. #define C_RBrack   15              /* '}' */
  104.  
  105. #define C_Create   16              /* call of create() for create expression */
  106. /* uses Rslt */
  107. /* uses Cont */
  108. #define NTemps      fld[2].n       /* number of temporary descriptors needed */
  109. #define WrkSize     fld[3].n       /* size of non-descriptor work area */
  110. #define NextCreat   fld[4].cd       /* for chaining creates in a continuation */
  111.  
  112.  
  113. #define C_If       17           /* conditional (goto or return) */
  114. #define Cond       fld[0].cd       /* condition */
  115. #define ThenStmt   fld[1].cd       /* what to do if condition is true */
  116.  
  117. #define C_SrcLoc   18
  118. #define FileName    fld[0].s      /* name of source file */
  119. #define LineNum     fld[1].n      /* line number within source file */
  120.  
  121. #define C_CdAry    19             /* array of code pieces, each with type code*/
  122. #define A_Str      0              /* code represented as a string */
  123. #define A_ValLoc   1              /* value location */
  124. #define A_Intgr    2              /* integer */
  125. #define A_ProcCont 3              /* procedure continuation */
  126. #define A_SBuf     4              /* string buffer (integer index) */
  127. #define A_CBuf     5              /* cset buffer (integer index) */
  128. #define A_Ary      6              /* pointer to subarray of code pieces */
  129. #define A_End      7              /* marker for end of array */
  130. #define ElemTyp(i) fld[2*i].n     /* type of element i (A_* codes) */
  131. #define Str(i)     fld[2*i+1].s   /* string in element i */
  132. #define ValLoc(i)  fld[2*i+1].vloc /* value location in element i */
  133. #define Intgr(i)   fld[2*i+1].n   /* integer in element i */
  134. #define Array(i)   fld[2*i+1].cd  /* pointer to subarray in element i */
  135.  
  136. /*
  137.  * union cd_fld - fields within a code struct.
  138.  */
  139. union cd_fld {
  140.    int n;                  /* various integer values */
  141.    char *s;                /* various string values */
  142.    struct lentry *nvar;    /* symbol table entry for a named variable */
  143.    struct code *cd;        /* various pointers to other pieces of code */
  144.    struct c_fnc *fnc;      /* pointer to function information */
  145.    struct centry *lit;     /* symbol table entry for a literal */
  146.    struct sig_act *sa;     /* actions to take for a returned signal */
  147.    struct sig_lst *sigref; /* pointer to func's reference to signal */
  148.    struct val_loc *vloc;   /* value location */
  149.    char *oper_nm;          /* name of run-time operation or NULL */
  150.    };
  151.  
  152. /*
  153.  * code - struct used to hold the internal representation of generated code.
  154.  */
  155. struct code {
  156.    int cd_id;           /* kind of code: C_* */
  157.    struct code *next;   /* next code fragment in list */
  158.    struct code *prev;   /* previous code fragment in list */
  159.    union cd_fld fld[1]; /* fields of code fragment, actual number varies */
  160.    };
  161.  
  162. /*
  163.  * NewCode - allocate a code structure with "size" fields.
  164.  */
  165. #define NewCode(size) (struct code *)alloc((unsigned int)\
  166.     (sizeof(struct code) + (size-1) * sizeof(union cd_fld)))
  167.  
  168. /*
  169.  * c_fnc contains information about a C function that implements a continuation.
  170.  */
  171. #define CF_SigOnly    1   /* this function only returns a signal */
  172. #define CF_ForeignSig 2   /* may return foreign signal from a suspend */
  173. #define CF_Mark       4   /* this function has been visited by fix_fncs() */
  174. #define CF_Coexpr     8   /* this function implements a co-expression */
  175. struct c_fnc {
  176.    char prefix[PrfxSz+1];   /* function prefix */
  177.    char frm_prfx[PrfxSz+1]; /* procedure frame prefix */
  178.    int flag;                /* CF_* flags */
  179.    struct code cd;          /* start of code sequence */
  180.    struct code *cursor;     /* place to insert more code into sequence */
  181.    struct code *call_lst;   /* functions called by this function */
  182.    struct code *creatlst;   /* list of creates in this function */
  183.    struct sig_lst *sig_lst; /* signals returned by this function */
  184.    int ref_cnt;             /* reference count for this function */
  185.    struct c_fnc *next;
  186.    };
  187.  
  188.  
  189. /*
  190.  * sig_lst - a list of signals returned by a continuation along with a count
  191.  *  of the number of places each signal is returned.
  192.  */
  193. struct sig_lst {
  194.    struct code *sig;     /* signal */
  195.    int ref_cnt;          /* number of places returned */
  196.    struct sig_lst *next;
  197.    };
  198.  
  199. /*
  200.  * op_symentry - entry in symbol table for an operation
  201.  */
  202. #define AdjNone  1   /* no adjustment to this argument */
  203. #define AdjDrf   2   /* deref in place */
  204. #define AdjNDrf  3   /* deref into a new temporary */
  205. #define AdjCpy   4   /* copy into a new temporary */
  206. struct op_symentry {
  207.     int n_refs;          /* number of non-modifying references */
  208.     int n_mods;          /* number of modifying referenced */
  209.     int n_rets;          /* number of times directly returned from operation */
  210.     int var_safe;        /* if arg is named var, it may be used directly */
  211.     int adjust;          /* AdjNone, AdjInplc, or AdjToNew */
  212.     int itmp_indx;       /* index of temporary C integer variable */
  213.     int dtmp_indx;       /* index of temporary C double variable */
  214.     struct val_loc *loc;
  215.     };
  216.  
  217. extern int num_tmp;        /* number of temporary descriptor variables */
  218. extern int num_itmp;        /* number of temporary C integer variables */
  219. extern int num_dtmp;        /* number of temporary C double variables */
  220. extern int num_sbuf;        /* number of string buffers */
  221. extern int num_cbuf;        /* number of cset buffers */
  222.  
  223. extern struct code *bound_sig;  /* bounding signal for current procedure */
  224.  
  225. /*
  226.  * statically declared "signals".
  227.  */
  228. extern struct code resume;
  229. extern struct code contin;
  230. extern struct code fallthru;
  231. extern struct code next_fail;
  232.  
  233. extern struct val_loc ignore;    /* no values, just something to point at */
  234. extern struct c_fnc *cur_fnc;    /* C function currently being built */
  235. extern struct code *on_failure;  /* place to go on failure */
  236.  
  237. extern int lbl_seq_num;        /* next label sequence number */
  238.  
  239. extern char pre[PrfxSz];        /* next unused prefix */
  240.  
  241. extern struct op_symentry *cur_symtab; /* current operation symbol table */
  242.  
  243. #define SepFnc  1   /* success continuation goes in separate function */
  244. #define SContIL 2   /* in line success continuation */
  245. #define EndOper 3   /* success continuation goes at end of operation */
  246.  
  247. #define HasVal 1    /* type contains values */
  248. #define HasLcl 2    /* type contains local variables */
  249. #define HasPrm 4    /* type contains parameters */
  250. #define HasGlb 8    /* type contains globals (including statics and elements) */
  251. #define HasVar(x) ((x) & (HasLcl | HasPrm | HasGlb))
  252.