| 1 | /* $Header: d:/cvsroot/tads/tads3/TCT3INT.H,v 1.3 1999/07/11 00:46:57 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | tct3int.h - T3-specific intermediate parse node classes |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 05/12/99 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef TCT3INT_H |
| 21 | #define TCT3INT_H |
| 22 | |
| 23 | /* include our T3-specific base classes */ |
| 24 | #include "tct3base.h" |
| 25 | |
| 26 | /* include the target-independent intermediate classes */ |
| 27 | #include "tcpnint.h" |
| 28 | |
| 29 | /* ------------------------------------------------------------------------ */ |
| 30 | /* |
| 31 | * Unary Operator Expression node |
| 32 | */ |
| 33 | class CTPNUnary: public CTPNUnaryBase |
| 34 | { |
| 35 | public: |
| 36 | CTPNUnary(CTcPrsNode *subexpr) |
| 37 | : CTPNUnaryBase(subexpr) { } |
| 38 | |
| 39 | /* |
| 40 | * Generate code for a unary operator with no side effects. We'll |
| 41 | * generate code for the subexpression, then generate the given |
| 42 | * opcode. |
| 43 | * |
| 44 | * If 'discard' is true, we won't generate the opcode. We know the |
| 45 | * opcode has no side effects, so if the result of the calculation |
| 46 | * isn't going to be used, there's no point in calculating it in the |
| 47 | * first place. |
| 48 | */ |
| 49 | void gen_unary(uchar opc, int discard, int for_condition); |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | /* ------------------------------------------------------------------------ */ |
| 54 | /* |
| 55 | * Binary Operator Expression node |
| 56 | */ |
| 57 | class CTPNBin: public CTPNBinBase |
| 58 | { |
| 59 | public: |
| 60 | CTPNBin(CTcPrsNode *lhs, CTcPrsNode *rhs) |
| 61 | : CTPNBinBase(lhs, rhs) { } |
| 62 | |
| 63 | /* |
| 64 | * Generate code for a binary operator with no side effects. We'll |
| 65 | * generate code for the left operand, then for the right operand, |
| 66 | * then generate the opcode. |
| 67 | * |
| 68 | * If 'discard' is true, we won't generate the opcode. We know that |
| 69 | * the opcode has no side effects, so if the result of the |
| 70 | * calculation isn't needed, we need not apply the opcode; we simply |
| 71 | * want to evaluate the subexpressions for any side effects they |
| 72 | * might have. |
| 73 | */ |
| 74 | void gen_binary(uchar opc, int discard, int for_condition); |
| 75 | |
| 76 | /* |
| 77 | * Generate code for a binary operator as part of a compound |
| 78 | * assignment. For example, this is used to generate the '+' portion |
| 79 | * of '+='. The caller must already have generated code to push the |
| 80 | * left operand; we generate the right operand plus the opcode to apply |
| 81 | * the operator. |
| 82 | */ |
| 83 | void gen_binary_ca(uchar opc); |
| 84 | }; |
| 85 | |
| 86 | /* ------------------------------------------------------------------------ */ |
| 87 | /* |
| 88 | * generic statement |
| 89 | */ |
| 90 | class CTPNStm: public CTPNStmBase |
| 91 | { |
| 92 | public: |
| 93 | /* initialize at the tokenizer's current source file position */ |
| 94 | CTPNStm() : CTPNStmBase() { } |
| 95 | |
| 96 | /* initialize at the given source position */ |
| 97 | CTPNStm(class CTcTokFileDesc *file, long linenum) |
| 98 | : CTPNStmBase(file, linenum) { } |
| 99 | |
| 100 | /* |
| 101 | * Generate code for a labeled 'continue'. These are called when |
| 102 | * this statement is labeled, and the same label is used in a |
| 103 | * 'continue' statement. |
| 104 | * |
| 105 | * Returns true if successful, false if not. This should return |
| 106 | * false if the statement is not suitable for a 'continue'. By |
| 107 | * default, we simply return false, since most statements cannot be |
| 108 | * used as the target of a 'continue'. |
| 109 | */ |
| 110 | virtual int gen_code_labeled_continue() { return FALSE; } |
| 111 | }; |
| 112 | |
| 113 | /* ------------------------------------------------------------------------ */ |
| 114 | /* |
| 115 | * Enclosing Statement class: this is the base class for statements that |
| 116 | * have special needs for exiting due to break, continue, goto, and |
| 117 | * return. Refer to tcpnint.h for details. |
| 118 | */ |
| 119 | class CTPNStmEnclosing: public CTPNStm |
| 120 | { |
| 121 | public: |
| 122 | CTPNStmEnclosing(CTPNStmEnclosing *enclosing) |
| 123 | { |
| 124 | /* remember the statement that encloses me */ |
| 125 | enclosing_ = enclosing; |
| 126 | } |
| 127 | |
| 128 | /* get the enclosing statement */ |
| 129 | CTPNStmEnclosing *get_enclosing() const { return enclosing_; } |
| 130 | |
| 131 | /* |
| 132 | * Generate code for a 'break' to a given label, or an unlabeled |
| 133 | * 'break' if lbl is null. Returns true if the break was fully |
| 134 | * processed, false if not. If any work is necessary to unwind the |
| 135 | * exception stack to break out of this block, that should be done |
| 136 | * here. If this node doesn't actually accomplish the break, it |
| 137 | * should recursively invoke the next enclosing statement's routine |
| 138 | * to do the work. |
| 139 | * |
| 140 | * By default, we'll simply invoke the enclosing handler, if there |
| 141 | * is one, or return failure (i.e., false) if not. |
| 142 | */ |
| 143 | virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len) |
| 144 | { |
| 145 | if (enclosing_) |
| 146 | return enclosing_->gen_code_break(lbl, lbl_len); |
| 147 | else |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Generate code for a 'continue' to a given label, or an unlabeled |
| 153 | * 'continue' if lbl is null. Same rules as gen_code_break(). |
| 154 | * |
| 155 | * By default, we'll simply invoke the enclosing handler, if there |
| 156 | * is one, or return failure (i.e., false) if not. |
| 157 | */ |
| 158 | virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len) |
| 159 | { |
| 160 | if (enclosing_) |
| 161 | return enclosing_->gen_code_continue(lbl, lbl_len); |
| 162 | else |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Generate the code necessary to unwind the stack for returning |
| 168 | * through this enclosing statement. This should generate any code |
| 169 | * necessary (such as a call to a 'finally' block, for a 'try' |
| 170 | * statement), then call the next enclosing statement to do the same |
| 171 | * thing. By default, this does nothing except invoke the enclosing |
| 172 | * statement, since most enclosing statements don't need to generate |
| 173 | * any code to handle a 'return' through them. |
| 174 | */ |
| 175 | virtual void gen_code_unwind_for_return() |
| 176 | { |
| 177 | /* if there's an enclosing statement, ask it to unwind itself */ |
| 178 | if (enclosing_ != 0) |
| 179 | enclosing_->gen_code_unwind_for_return(); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Determine if we'll generate any code to unwind the stack for |
| 184 | * returning through this enclosing statement. This should return |
| 185 | * true if any code will be generated by a call to |
| 186 | * gen_code_unwind_for_return(). By default, we'll return whatever |
| 187 | * our enclosing statement does. |
| 188 | */ |
| 189 | virtual int will_gen_code_unwind_for_return() const |
| 190 | { |
| 191 | /* |
| 192 | * return what the enclosing statement does, or false if we're |
| 193 | * the outermost statement |
| 194 | */ |
| 195 | return (enclosing_ != 0 |
| 196 | ? enclosing_->will_gen_code_unwind_for_return() |
| 197 | : FALSE); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Generate the code necessary to unwind the stack for executing a |
| 202 | * 'goto' to the given label statement. We first determine if the |
| 203 | * target label is contained within this statement; if it is, |
| 204 | * there's nothing we need to do, because we're transferring control |
| 205 | * within the same enclosing statement. |
| 206 | * |
| 207 | * If control is being transferred outside of this statement (i.e., |
| 208 | * the label is not contained within this statement), we must |
| 209 | * generate the necessary code to leave the block; for example, a |
| 210 | * 'try' block must generate a call to its 'finally' block. We must |
| 211 | * then invoke this same function on our enclosing statement to let |
| 212 | * it do the same work. |
| 213 | */ |
| 214 | void gen_code_unwind_for_goto(class CTPNStmGoto *goto_stm, |
| 215 | class CTPNStmLabel *target); |
| 216 | |
| 217 | /* |
| 218 | * Generate code for transferring control out of this statement. We |
| 219 | * invoke this from gen_code_unwind_for_goto() when we determine |
| 220 | * that the 'goto' target is not enclosed within this statement. By |
| 221 | * default we do nothing; statements such as 'try' that must do work |
| 222 | * on transferring control must override this to generate the |
| 223 | * appropriate code. |
| 224 | */ |
| 225 | virtual void gen_code_for_transfer_out() { } |
| 226 | |
| 227 | /* |
| 228 | * Check to see if we are allowed to transfer in to this block via |
| 229 | * 'goto' statements. If so, simply return TRUE. If we don't allow |
| 230 | * this type of transfer, we should log an appropriate error, then |
| 231 | * return FALSE. Note that the function should both log an error |
| 232 | * and return FALSE if the transfer isn't allowed; the return value |
| 233 | * allows the caller to ensure that only one error is displayed for |
| 234 | * this type of problem even if the target label is nested within |
| 235 | * several levels of blocks that don't allow transfers in. |
| 236 | * |
| 237 | * By default, we'll simply return TRUE, since most block types |
| 238 | * allow this type of transfer. |
| 239 | */ |
| 240 | virtual int check_enter_by_goto(class CTPNStmGoto * /*goto_stm*/, |
| 241 | class CTPNStmLabel * /*target*/) |
| 242 | { return TRUE; } |
| 243 | |
| 244 | protected: |
| 245 | /* |
| 246 | * generate code for a break - this can be used as a service routine |
| 247 | * by loop/switch statements |
| 248 | */ |
| 249 | int gen_code_break_loop(CTcCodeLabel *code_label, |
| 250 | const textchar_t *lbl, size_t lbl_len); |
| 251 | |
| 252 | /* |
| 253 | * generate code for a continue - this can be used as a service |
| 254 | * routine by loop/switch statements |
| 255 | */ |
| 256 | int gen_code_continue_loop(CTcCodeLabel *code_label, |
| 257 | const textchar_t *lbl, size_t lbl_len); |
| 258 | |
| 259 | |
| 260 | /* the block enclosing this block */ |
| 261 | CTPNStmEnclosing *enclosing_; |
| 262 | }; |
| 263 | |
| 264 | /* ------------------------------------------------------------------------ */ |
| 265 | /* |
| 266 | * Base class for 'for' statement 'var in collection' and 'var in from..to' |
| 267 | * expressions. We keep a separate list of these nodes for a 'for' |
| 268 | * statement, for code generation purposes. These expression nodes reside |
| 269 | * in the initializer clause of the 'for', but they implicitly generate |
| 270 | * code in the condition and reinit phases as well. |
| 271 | */ |
| 272 | class CTPNForIn: public CTPNForInBase |
| 273 | { |
| 274 | public: |
| 275 | CTPNForIn() { } |
| 276 | |
| 277 | /* generate the condition part of the 'for' */ |
| 278 | virtual void gen_forstm_cond(struct CTcCodeLabel *endlbl) = 0; |
| 279 | |
| 280 | /* generate the reinit part of the 'for' */ |
| 281 | virtual void gen_forstm_reinit() = 0; |
| 282 | |
| 283 | protected: |
| 284 | }; |
| 285 | |
| 286 | |
| 287 | #endif /* TCT3INT_H */ |
| 288 |