| 1 | /* $Header: d:/cvsroot/tads/tads3/TCPNINT.H,v 1.3 1999/07/11 00:46:53 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 | tcpnint.h - intermediate derived parse node classes |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 05/12/99 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef TCPNINT_H |
| 21 | #define TCPNINT_H |
| 22 | |
| 23 | |
| 24 | |
| 25 | /* ------------------------------------------------------------------------ */ |
| 26 | /* |
| 27 | * Generic unary parse node. Most unary node types don't differ at the |
| 28 | * parser level, so target node types can be subclassed directly off of |
| 29 | * this generic unary node class. |
| 30 | */ |
| 31 | class CTPNUnaryBase: public CTcPrsNode |
| 32 | { |
| 33 | public: |
| 34 | CTPNUnaryBase(class CTcPrsNode *sub) |
| 35 | { |
| 36 | sub_ = sub; |
| 37 | } |
| 38 | |
| 39 | /* get the subexpression */ |
| 40 | class CTcPrsNode *get_sub_expr() const { return sub_; } |
| 41 | |
| 42 | /* fold constants */ |
| 43 | class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab) |
| 44 | { |
| 45 | class CTcPrsNode *ret; |
| 46 | |
| 47 | /* fold constants on my subexpression */ |
| 48 | sub_ = sub_->fold_constants(symtab); |
| 49 | |
| 50 | /* try folding the unary operator */ |
| 51 | ret = fold_unop(); |
| 52 | |
| 53 | /* |
| 54 | * if we folded the operator, return the result; otherwise, |
| 55 | * return the original operator tree for full code generation |
| 56 | */ |
| 57 | return (ret != 0 ? ret : this); |
| 58 | } |
| 59 | |
| 60 | /* adjust for dynamic (run-time) compilation */ |
| 61 | class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info) |
| 62 | { |
| 63 | /* |
| 64 | * if this expression has side effects, don't allow it to be |
| 65 | * evaluated speculatively |
| 66 | */ |
| 67 | if (info->speculative && has_side_effects()) |
| 68 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 69 | |
| 70 | /* adjust my subexpression */ |
| 71 | sub_ = sub_->adjust_for_dyn(info); |
| 72 | |
| 73 | /* return myself otherwise unchanged */ |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | protected: |
| 78 | /* fold constants for the operator */ |
| 79 | virtual class CTcPrsNode *fold_unop() { return 0; } |
| 80 | |
| 81 | /* determine if I have side effects */ |
| 82 | virtual int has_side_effects() const { return FALSE; } |
| 83 | |
| 84 | /* our subexpression */ |
| 85 | class CTcPrsNode *sub_; |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * Define a constructor for a CTPNUnary target subclass. Target |
| 90 | * subclasses can use this macro to define the standard CTPNUnary |
| 91 | * subclass constructor - this is simply for conciseness in those |
| 92 | * subclasses. |
| 93 | */ |
| 94 | #define CTPNUnary_ctor(scname) \ |
| 95 | scname(class CTcPrsNode *sub) \ |
| 96 | : CTPNUnary(sub) { } |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * define a CTPNUnary target subclass |
| 101 | */ |
| 102 | #define CTPNUnary_def(scname) \ |
| 103 | class scname: public CTPNUnary \ |
| 104 | { \ |
| 105 | public: \ |
| 106 | CTPNUnary_ctor(scname); \ |
| 107 | void gen_code(int discard, int for_condition); \ |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * define a CTPNUnary target subclass for a unary operator with side |
| 112 | * effects |
| 113 | */ |
| 114 | #define CTPNUnary_side_def(scname) \ |
| 115 | class scname: public CTPNUnary \ |
| 116 | { \ |
| 117 | public: \ |
| 118 | CTPNUnary_ctor(scname); \ |
| 119 | void gen_code(int discard, int for_condition); \ |
| 120 | virtual int has_side_effects() const { return TRUE; } \ |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | /* ------------------------------------------------------------------------ */ |
| 126 | /* |
| 127 | * Generic binary parse node. Most binary node types don't differ at |
| 128 | * the parser level, so target node types can be subclassed directly off |
| 129 | * of this generic binary node class. |
| 130 | */ |
| 131 | class CTPNBinBase: public CTcPrsNode |
| 132 | { |
| 133 | public: |
| 134 | CTPNBinBase(class CTcPrsNode *left, class CTcPrsNode *right) |
| 135 | { |
| 136 | left_ = left; |
| 137 | right_ = right; |
| 138 | } |
| 139 | |
| 140 | /* fold constants */ |
| 141 | class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab) |
| 142 | { |
| 143 | CTcPrsNode *ret; |
| 144 | |
| 145 | /* fold constants on my subexpressions */ |
| 146 | left_ = left_->fold_constants(symtab); |
| 147 | right_ = right_->fold_constants(symtab); |
| 148 | |
| 149 | /* try folding this operator, if we can */ |
| 150 | ret = fold_binop(); |
| 151 | |
| 152 | /* |
| 153 | * if we got a folded value, return it; otherwise, return |
| 154 | * myself, unchanged except any subnode folding we did |
| 155 | */ |
| 156 | return (ret != 0 ? ret : this); |
| 157 | } |
| 158 | |
| 159 | /* adjust for dynamic (run-time) compilation */ |
| 160 | class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info) |
| 161 | { |
| 162 | /* if I have side effects, don't allow speculative evaluation */ |
| 163 | if (info->speculative && has_side_effects()) |
| 164 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 165 | |
| 166 | /* adjust the left and right subexpressions */ |
| 167 | left_ = left_->adjust_for_dyn(info); |
| 168 | right_ = right_->adjust_for_dyn(info); |
| 169 | |
| 170 | /* return myself otherwise unchanged */ |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | protected: |
| 175 | /* perform folding on this binary operator, if possible */ |
| 176 | virtual class CTcPrsNode *fold_binop() { return 0; } |
| 177 | |
| 178 | /* determine if I have side effects */ |
| 179 | virtual int has_side_effects() const { return FALSE; } |
| 180 | |
| 181 | /* left and right operands */ |
| 182 | class CTcPrsNode *left_; |
| 183 | class CTcPrsNode *right_; |
| 184 | }; |
| 185 | |
| 186 | /* |
| 187 | * Define a constructor for a CTPNBin target subclass. Target |
| 188 | * subclasses can use this macro to define the standard CTPNBin subclass |
| 189 | * constructor - this is simply for conciseness in those subclasses. |
| 190 | */ |
| 191 | #define CTPNBin_ctor(scname) \ |
| 192 | scname(class CTcPrsNode *left, class CTcPrsNode *right) \ |
| 193 | : CTPNBin(left, right) { } |
| 194 | |
| 195 | /* |
| 196 | * Define a full general class interface for a CTPNBin target subclass. |
| 197 | * Most target subclasses will not need to add anything of their own, so |
| 198 | * they can use this generic class definition macro. |
| 199 | */ |
| 200 | #define CTPNBin_def(scname) \ |
| 201 | class scname: public CTPNBin \ |
| 202 | { \ |
| 203 | public: \ |
| 204 | CTPNBin_ctor(scname); \ |
| 205 | void gen_code(int discard, int for_condition); \ |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * define a CTPNBin target subclass for a binary comparison operator |
| 210 | */ |
| 211 | #define CTPNBin_cmp_def(scname) \ |
| 212 | class scname: public CTPNBin \ |
| 213 | { \ |
| 214 | public: \ |
| 215 | CTPNBin_ctor(scname); \ |
| 216 | void gen_code(int discard, int for_condition); \ |
| 217 | virtual int is_bool() const { return TRUE; } \ |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * define a CTPNBin target subclass for a binary operator with side |
| 222 | * effects |
| 223 | */ |
| 224 | #define CTPNBin_side_def(scname) \ |
| 225 | class scname: public CTPNBin \ |
| 226 | { \ |
| 227 | public: \ |
| 228 | CTPNBin_ctor(scname); \ |
| 229 | void gen_code(int discard, int for_condition); \ |
| 230 | virtual int has_side_effects() const { return TRUE; } \ |
| 231 | } |
| 232 | |
| 233 | /* ------------------------------------------------------------------------ */ |
| 234 | /* |
| 235 | * Generic statement node base. |
| 236 | */ |
| 237 | class CTPNStmBase: public CTcPrsNode |
| 238 | { |
| 239 | public: |
| 240 | /* initialize at the tokenizer's current source file position */ |
| 241 | CTPNStmBase(); |
| 242 | |
| 243 | /* initialize at the given source position */ |
| 244 | CTPNStmBase(class CTcTokFileDesc *file, long linenum) |
| 245 | { |
| 246 | init(file, linenum); |
| 247 | } |
| 248 | |
| 249 | /* set the file and line number */ |
| 250 | void set_source_pos(class CTcTokFileDesc *file, long linenum) |
| 251 | { |
| 252 | /* remember the new line information */ |
| 253 | file_ = file; |
| 254 | linenum_ = linenum; |
| 255 | } |
| 256 | |
| 257 | /* get the next statement after this one */ |
| 258 | class CTPNStm *get_next_stm() const { return next_stm_; } |
| 259 | |
| 260 | /* set the next statement after this one */ |
| 261 | void set_next_stm(class CTPNStm *nxt) { next_stm_ = nxt; } |
| 262 | |
| 263 | /* |
| 264 | * log an error, using the source file location of this statement as |
| 265 | * the location of the error |
| 266 | */ |
| 267 | void log_error(int errnum, ...) const; |
| 268 | |
| 269 | /* |
| 270 | * log a warning, using the source file location of this statement |
| 271 | * as the location of the warning |
| 272 | */ |
| 273 | void log_warning(int errnum, ...) const; |
| 274 | |
| 275 | /* get my source location information */ |
| 276 | class CTcTokFileDesc *get_source_desc() const { return file_; } |
| 277 | long get_source_linenum() const { return linenum_; } |
| 278 | |
| 279 | /* |
| 280 | * Get the ending source location - for most statements, this is |
| 281 | * identical to the normal source location information. For certain |
| 282 | * complex statements, such as compound statements, this will |
| 283 | * provide the source location where the statement's structure ends. |
| 284 | */ |
| 285 | virtual class CTcTokFileDesc *get_end_desc() const { return file_; } |
| 286 | virtual long get_end_linenum() const { return linenum_; } |
| 287 | |
| 288 | /* |
| 289 | * Determine the possible control flow routes through this |
| 290 | * statement. Set or clear each flag as appropriate. |
| 291 | * |
| 292 | *. TCPRS_FLOW_NEXT - flow continues to the next statement |
| 293 | *. TCPRS_FLOW_THROW - statement throws an exception |
| 294 | *. TCPRS_FLOW_RET_VOID - statement returns no value |
| 295 | *. TCPRS_FLOW_RET_VAL - statement returns a value |
| 296 | *. TCPRS_FLOW_GOTO - control transfers to a code label |
| 297 | *. TCPRS_FLOW_BREAK - control breaks out of the current loop/case |
| 298 | *. TCPRS_FLOW_CONT - control goes to the top of the current loop |
| 299 | * |
| 300 | * Most statements simply proceed to the next statement in all |
| 301 | * cases, so the default implementation simply sets the 'cont' flag |
| 302 | * and clears the others. |
| 303 | * |
| 304 | * This routine is not meant to evaluate dependencies on called |
| 305 | * functions or methods. Functions and methods that are called can |
| 306 | * be assumed to return, with or without a value as needed for the |
| 307 | * usage here. The possibility of VM exceptions due to run-time |
| 308 | * error conditions can be ignored; we're only interested in |
| 309 | * statements that explicitly throw exceptions. |
| 310 | */ |
| 311 | |
| 312 | #define TCPRS_FLOW_NEXT 0x00000001 |
| 313 | #define TCPRS_FLOW_THROW 0x00000002 |
| 314 | #define TCPRS_FLOW_RET_VOID 0x00000004 |
| 315 | #define TCPRS_FLOW_RET_VAL 0x00000008 |
| 316 | #define TCPRS_FLOW_GOTO 0x00000010 |
| 317 | #define TCPRS_FLOW_BREAK 0x00000020 |
| 318 | #define TCPRS_FLOW_CONT 0x00000040 |
| 319 | |
| 320 | virtual unsigned long get_control_flow(int /*warn*/) const |
| 321 | { |
| 322 | /* by default, a statement continues to the next statement */ |
| 323 | return TCPRS_FLOW_NEXT; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Determine if this statement has a code label. By default, we |
| 328 | * return false. |
| 329 | */ |
| 330 | virtual int has_code_label() const { return FALSE; } |
| 331 | |
| 332 | /* a statement has no return value */ |
| 333 | virtual int has_return_value() const { return FALSE; } |
| 334 | |
| 335 | /* add a debugging line record for this statement */ |
| 336 | void add_debug_line_rec(); |
| 337 | |
| 338 | protected: |
| 339 | /* |
| 340 | * Generate code for a sub-statement (such as the 'then' part of an |
| 341 | * 'if', or a statement in a compount statement). This sets up the |
| 342 | * error reporting location to refer to the sub-statement, then |
| 343 | * generates code for the sub-statement normally. |
| 344 | */ |
| 345 | void gen_code_substm(class CTPNStm *substm); |
| 346 | |
| 347 | /* add a debugging line record at the given position */ |
| 348 | void add_debug_line_rec(class CTcTokFileDesc *desc, long linenum); |
| 349 | |
| 350 | /* initialize */ |
| 351 | void init(class CTcTokFileDesc *file, long linenum) |
| 352 | { |
| 353 | /* remember the file and line number containing the code */ |
| 354 | file_ = file; |
| 355 | linenum_ = linenum; |
| 356 | |
| 357 | /* there's not another statement after this one yet */ |
| 358 | next_stm_ = 0; |
| 359 | } |
| 360 | |
| 361 | /* next statement in execution order */ |
| 362 | class CTPNStm *next_stm_; |
| 363 | |
| 364 | /* file and line number where this statement's source code appears */ |
| 365 | class CTcTokFileDesc *file_; |
| 366 | long linenum_; |
| 367 | }; |
| 368 | |
| 369 | |
| 370 | /* ------------------------------------------------------------------------ */ |
| 371 | /* |
| 372 | * Enclosing Statement Block object. This is a base class for |
| 373 | * statements such as "try" blocks and code labels for which we must |
| 374 | * keep a nesting list. A statement of this class has special needs for |
| 375 | * exiting, via break, continue, goto, or return, the block of code that |
| 376 | * the statement encloses |
| 377 | * |
| 378 | * NOTE: To avoid having to introduce yet another level of intermediate |
| 379 | * #include file in our target-generic/target-specific layering, we |
| 380 | * require that the target-specific intermediate include file define the |
| 381 | * base class CTPNStmEnclosing for us. It must be defined as below, |
| 382 | * with the addition of the target-specific definitions. Sorry about |
| 383 | * any confusion this causes, but inserting a fourth include layer is |
| 384 | * just going too far. |
| 385 | */ |
| 386 | // class CTPNStmEnclosing: public CTPNStm |
| 387 | // { |
| 388 | // public: |
| 389 | // CTPNStmEnclosing(CTPNStmEnclosing *enclosing) |
| 390 | // { |
| 391 | // /* remember the statement that encloses me */ |
| 392 | // enclosing_ = enclosing; |
| 393 | // } |
| 394 | // |
| 395 | // protected: |
| 396 | // /* the block enclosing this block */ |
| 397 | // CTPNStmEnclosing *enclosing_; |
| 398 | // }; |
| 399 | |
| 400 | |
| 401 | /* ------------------------------------------------------------------------ */ |
| 402 | /* |
| 403 | * Base class for 'for' statement 'var in collection' and 'var in from..to' |
| 404 | * expressions. We keep a separate list of these nodes for a 'for' |
| 405 | * statement, for code generation purposes. These expression nodes reside |
| 406 | * in the initializer clause of the 'for', but they implicitly generate |
| 407 | * code in the condition and reinit phases as well. |
| 408 | */ |
| 409 | class CTPNForInBase: public CTcPrsNode |
| 410 | { |
| 411 | public: |
| 412 | CTPNForInBase() |
| 413 | { |
| 414 | nxt_ = 0; |
| 415 | } |
| 416 | |
| 417 | /* get/set the next list entry */ |
| 418 | class CTPNForIn *getnxt() const { return nxt_; } |
| 419 | void setnxt(class CTPNForIn *nxt) { nxt_ = nxt; } |
| 420 | |
| 421 | protected: |
| 422 | /* next in list of 'in' clauses for this 'for' */ |
| 423 | class CTPNForIn *nxt_; |
| 424 | }; |
| 425 | |
| 426 | #endif /* TCPNINT_H */ |
| 427 |