| 1 | /* $Header: d:/cvsroot/tads/tads3/TCT3BASE.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 | tct3base.h - base parse node classes for T3 code generator |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 05/10/99 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef TCT3BASE_H |
| 21 | #define TCT3BASE_H |
| 22 | |
| 23 | #include "tcprs.h" |
| 24 | |
| 25 | /* include the target-independent base classes */ |
| 26 | #include "tcpnbase.h" |
| 27 | |
| 28 | |
| 29 | /* ------------------------------------------------------------------------ */ |
| 30 | /* |
| 31 | * Base parse node class |
| 32 | */ |
| 33 | class CTcPrsNode: public CTcPrsNodeBase |
| 34 | { |
| 35 | public: |
| 36 | /* |
| 37 | * Generate code to assign into the expression as an lvalue. By |
| 38 | * default, we'll throw an error; a default node should never be called |
| 39 | * upon to generate code as an assignment target, because the |
| 40 | * impossibility of this condition should be detected during parsing. |
| 41 | * Each class that overrides check_lvalue() to return true must |
| 42 | * override this to generate code for an assignment. |
| 43 | * |
| 44 | * 'typ' specifies the type of assignment - this specifies the |
| 45 | * assignment operator being generated. |
| 46 | * |
| 47 | * 'phase' indicates what part of the assignment we're generating. For |
| 48 | * a simple assignment, this routine will be called only once, with |
| 49 | * phase set to 1. For a compound assignment, this routine will be |
| 50 | * called twice. The first call is phase 1. This can do one of two |
| 51 | * things: |
| 52 | * |
| 53 | * - Generate the entire assignment code, and return TRUE to indicate |
| 54 | * that the assignment has been handled. The caller will generate no |
| 55 | * more code. This should be used when the particular combination of |
| 56 | * assignment type and lvalue can be specially optimized in the |
| 57 | * bytecode. |
| 58 | * |
| 59 | * - Generate code to evaluate the lvalue's value, and prepare the |
| 60 | * stack for eventual assignment to the lvalue, and return FALSE. The |
| 61 | * caller in this case will then generate generic code to combine the |
| 62 | * lvalue and rvalue. For example, with operator '+=', the caller will |
| 63 | * generate the ADD instruction to compute 'lvalue + rvalue'. The |
| 64 | * caller will finally call the routine again with phase set to 2. |
| 65 | * |
| 66 | * During phase 2, the routine must generate code to assign the rvalue |
| 67 | * at top of stack to the lvalue. |
| 68 | * |
| 69 | * Returns true if the assignment type was handled, false if not. |
| 70 | * Simple assignment must always be handled, but composite assignments |
| 71 | * (such as "+=" or "*=") can refused. If the assignment isn't |
| 72 | * handled, the caller must perform a generic calculation to compute |
| 73 | * the result of the assignment (for example, for "+=", the caller must |
| 74 | * generate code compute the sum of the right-hand side and the |
| 75 | * original value of the left-hand side), then call gen_code_asi() |
| 76 | * again to generate a simple assignment. |
| 77 | * |
| 78 | * In general, gen_code_asi() must generate code for complex |
| 79 | * assignments itself only when it can take advantage of special |
| 80 | * opcodes that would result in more efficient generated code. When no |
| 81 | * special opcodes are available for the assignment, there's no need |
| 82 | * for special coding here. |
| 83 | * |
| 84 | * If 'ignore_error' is true, this should not generate an error if this |
| 85 | * node is not a suitable lvalue, but should simply return false. |
| 86 | * |
| 87 | * 'explicit' is true if this is an explicit assignment by user code, |
| 88 | * false if it's an assignment generated automatically by the compiler. |
| 89 | * Implicit assignments don't count for the purposes of unused value |
| 90 | * warnings. These warnings are primarily to alert the user that they |
| 91 | * could delete an unnecessary assignment from their code, or |
| 92 | * conversely that they might have forgotten to do something with the |
| 93 | * value that the assignment would suggest they intended to do, but |
| 94 | * with a compiler-generated assignment there's nothing for the user to |
| 95 | * delete to fix the warning. |
| 96 | * |
| 97 | * 'ctx' is the address of a 'void *' variable (a generic pointer). |
| 98 | * This is for the callee's use during a two-phase compound operator |
| 99 | * assignment, for passing itself context information between the |
| 100 | * phases. On phase 1, the callee can allocate a structure and store |
| 101 | * it at *ctx. On phase 2, the callee can then look at this structure |
| 102 | * to retrieve information saved from the first phase. The callee is |
| 103 | * responsible for deleting the structure on the second phase. For |
| 104 | * single-phase TC_ASI_SIMPLE calls, the caller can simply pass a null |
| 105 | * pointer for ctx, so the callee must not attempt to store anything |
| 106 | * here during a simple assignment call. |
| 107 | */ |
| 108 | virtual int gen_code_asi(int discard, int phase, |
| 109 | tc_asitype_t typ, const char *op, |
| 110 | class CTcPrsNode *rhs, |
| 111 | int ignore_error, int xplicit, void **ctx); |
| 112 | |
| 113 | /* |
| 114 | * Generate code to take the address of the expression. By default, |
| 115 | * we'll throw an internal error; a default node should never be |
| 116 | * called upon to generate code to take its address, because we |
| 117 | * shouldn't be able to parse such a thing. Each class that |
| 118 | * overrides has_addr() to return true must override this to |
| 119 | * generate address code. |
| 120 | */ |
| 121 | virtual void gen_code_addr(); |
| 122 | |
| 123 | /* |
| 124 | * Generate an if-then-else condition test. This is called on the |
| 125 | * controlling expression of an if-then-else, and can also be called on |
| 126 | * subexpressions of short-circuit operators (|| and &&). The purpose |
| 127 | * of this type of code generation is to avoid pushing the result of |
| 128 | * the controlling expression onto the stack when this can be avoided, |
| 129 | * and generate a jump to the appropriate if-else branch directly. |
| 130 | * |
| 131 | * Either then_label or else_label will be non-null; the one that's |
| 132 | * null directly follows the condition test, so we are simply to fall |
| 133 | * through to that code rather than jump to it explicitly. |
| 134 | */ |
| 135 | virtual void gen_code_cond(struct CTcCodeLabel *then_label, |
| 136 | struct CTcCodeLabel *else_label); |
| 137 | |
| 138 | /* |
| 139 | * Generate code to call the expression as a function or method. |
| 140 | * The caller will already have generated code to push the argument |
| 141 | * list; this routine only needs to generate code to make the call. |
| 142 | * |
| 143 | * By default, we'll assume that the result of evaluating the |
| 144 | * expression is a method or function pointer, so we'll generate a |
| 145 | * call-indirect instruction to call the result of evaluating the |
| 146 | * expression. |
| 147 | */ |
| 148 | virtual void gen_code_call(int discard, int argc, int varargs, |
| 149 | struct CTcNamedArgs *named_args); |
| 150 | |
| 151 | /* |
| 152 | * Generate code to apply operator 'new' to the expression. By |
| 153 | * default, 'new' cannot be applied to an expression; nodes that |
| 154 | * allow operator 'new' must override this. |
| 155 | */ |
| 156 | virtual void gen_code_new(int discard, int argc, |
| 157 | int varargs, struct CTcNamedArgs *named_args, |
| 158 | int from_call, int is_transient); |
| 159 | |
| 160 | /* |
| 161 | * Generate code to evaluate a member expression on this object |
| 162 | * expression, calling the property expression given. By default, |
| 163 | * we'll evaluate our own expression to yield the object value, then |
| 164 | * get the property expression (either as a constant or by |
| 165 | * generating code to yield a property pointer), then we'll invoke |
| 166 | * that code. Nodes should override this where more specific |
| 167 | * instructions can be generated. |
| 168 | * |
| 169 | * If 'prop_is_expr' is true, the property expression (prop_expr) is |
| 170 | * a parenthesized expression; otherwise, it's a simple property |
| 171 | * symbol. (We need this information because a symbol enclosed in |
| 172 | * parentheses would be otherwise indistinguishable from a plain |
| 173 | * symbol, but the two syntax cases differ in their behavior: a |
| 174 | * plain symbol is always a property name, whereas a symbol in |
| 175 | * parentheses can be a local variable.) |
| 176 | */ |
| 177 | virtual void gen_code_member(int discard, |
| 178 | class CTcPrsNode *prop_expr, |
| 179 | int prop_is_expr, |
| 180 | int argc, int varargs, |
| 181 | struct CTcNamedArgs *named_args); |
| 182 | |
| 183 | /* |
| 184 | * Generate code for an object on the left side of a '.' expression. |
| 185 | * If possible, return the constant object rather than generating |
| 186 | * code. If the expression refers to "self", set (*is_self) to true |
| 187 | * and return VM_INVALID_OBJ; otherwise, if the expression refers to |
| 188 | * a constant object reference, set (*is_self) to false and return |
| 189 | * the constant object value; otherwise, generate code for the |
| 190 | * expression, set (*is_self) to false, and return VM_INVALID_OBJ to |
| 191 | * indicate that the value must be obtained from the generated code |
| 192 | * at run-time. |
| 193 | * |
| 194 | * By default, we'll use generated code to get the value. |
| 195 | */ |
| 196 | virtual vm_obj_id_t gen_code_obj_predot(int *is_self) |
| 197 | { |
| 198 | /* generate the expression */ |
| 199 | gen_code(FALSE, FALSE); |
| 200 | |
| 201 | /* tell the caller that the value is not a compile-time constant */ |
| 202 | *is_self = FALSE; |
| 203 | return VM_INVALID_OBJ; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * generic generation for a member expression after the left side |
| 209 | * has been generated |
| 210 | */ |
| 211 | static void s_gen_member_rhs(int discard, |
| 212 | class CTcPrsNode *prop_expr, |
| 213 | int prop_is_expr, |
| 214 | int argc, int varargs, |
| 215 | struct CTcNamedArgs *named_args); |
| 216 | |
| 217 | /* |
| 218 | * Get the property ID of this expression. If the property ID is |
| 219 | * available as a constant value, this doesn't generate any code and |
| 220 | * simply returns the constant property ID value. If this |
| 221 | * expression requires run-time evaluation, we'll generate code for |
| 222 | * the expression and return VM_INVALID_PROP to indicate that a |
| 223 | * constant property ID is not available. |
| 224 | * |
| 225 | * If 'check_only' is true, this routine should only check to see |
| 226 | * whether a constant property value is available and return the |
| 227 | * appropriate indication, but should not generate any code in any |
| 228 | * case. Errors should also be suppressed in this case, because the |
| 229 | * routine will always be called again to perform the actual |
| 230 | * generation, at which point any errors can be logged. |
| 231 | * |
| 232 | * If 'is_expr' is true, it means that this property was given as an |
| 233 | * expression by being explicitly enclosed in parentheses. If |
| 234 | * 'is_expr' is false, it means that the property was given as a |
| 235 | * simple symbol name. For cases where the property expression is a |
| 236 | * symbol, this distinction is important because we must resolve an |
| 237 | * unparenthesized symbol as a property name, even if it's hidden by |
| 238 | * a local variable. |
| 239 | */ |
| 240 | virtual vm_prop_id_t gen_code_propid(int check_only, int is_expr); |
| 241 | |
| 242 | /* |
| 243 | * generate a jump-ahead instruction, returning a new label which |
| 244 | * serves as the jump destination |
| 245 | */ |
| 246 | static struct CTcCodeLabel *gen_jump_ahead(uchar opc); |
| 247 | |
| 248 | /* define the position of a code label */ |
| 249 | static void def_label_pos(struct CTcCodeLabel *lbl); |
| 250 | |
| 251 | /* allocate a new label at the current write position */ |
| 252 | static struct CTcCodeLabel *new_label_here(); |
| 253 | }; |
| 254 | |
| 255 | /* ------------------------------------------------------------------------ */ |
| 256 | /* |
| 257 | * Basic T3-specific symbol class |
| 258 | */ |
| 259 | class CTcSymbol: public CTcSymbolBase |
| 260 | { |
| 261 | public: |
| 262 | CTcSymbol(const char *str, size_t len, int copy, tc_symtype_t typ) |
| 263 | : CTcSymbolBase(str, len, copy, typ) { } |
| 264 | |
| 265 | /* |
| 266 | * get the object value for the symbol, if the symbol has an object |
| 267 | * value; returns VM_INVALID_OBJ if the symbol is not an object |
| 268 | */ |
| 269 | virtual vm_obj_id_t get_val_obj() const { return VM_INVALID_OBJ; } |
| 270 | |
| 271 | /* generate code to evaluate the symbol */ |
| 272 | virtual void gen_code(int discard) = 0; |
| 273 | |
| 274 | /* |
| 275 | * Generate code to assign to the symbol. By default, we'll generate |
| 276 | * an error indicating that the symbol cannot be assigned into. As |
| 277 | * with CTcPrsNode::gen_code_asi(), this returns true if the assignment |
| 278 | * was generated, false if the caller must generate a generic |
| 279 | * assignment; simple assignment must always return true. |
| 280 | * |
| 281 | * If 'rhs' is null, the caller has already generated the value to be |
| 282 | * assigned, so this code doesn't need to do that. Otherwise, this |
| 283 | * code must generate the rvalue code. The reason that 'rhs' is passed |
| 284 | * down at all is that we can sometimes optimize the type of opcode we |
| 285 | * generate according to the type of value being assigned, especially |
| 286 | * when a constant value is being assigned. |
| 287 | * |
| 288 | * If 'ignore_error' is true, this should not log an error if the value |
| 289 | * cannot be assigned. |
| 290 | * |
| 291 | * 'xplicit' is true if this is an explicit assignment by user code, |
| 292 | * false if it's an assignment generated automatically by the compiler. |
| 293 | * Implicit assignments don't count for the purposes of unused value |
| 294 | * warnings. These warnings are primarily to alert the user that they |
| 295 | * could delete an unnecessary assignment from their code, or |
| 296 | * conversely that they might have forgotten to do something with the |
| 297 | * value that the assignment would suggest they intended to do, but |
| 298 | * with a compiler-generated assignment there's nothing for the user to |
| 299 | * delete to fix the warning. |
| 300 | */ |
| 301 | virtual int gen_code_asi(int discard, int phase, |
| 302 | tc_asitype_t typ, const char *op, |
| 303 | class CTcPrsNode *rhs, |
| 304 | int ignore_error, int xplicit, void **ctx); |
| 305 | |
| 306 | /* |
| 307 | * Generate code for taking the address of the symbol. By default, |
| 308 | * we'll generate an error indicating that the symbol's address |
| 309 | * cannot be taken. |
| 310 | */ |
| 311 | virtual void gen_code_addr(); |
| 312 | |
| 313 | /* |
| 314 | * Generate code to call the symbol. By default, we can't call a |
| 315 | * symbol. |
| 316 | */ |
| 317 | virtual void gen_code_call(int discard, int argc, int varargs, |
| 318 | struct CTcNamedArgs *named_args); |
| 319 | |
| 320 | /* |
| 321 | * generate code for operator 'new' applied to the symbol, with the |
| 322 | * given number of arguments; by default, we can't generate any such |
| 323 | * code |
| 324 | */ |
| 325 | virtual void gen_code_new(int discard, int argc, |
| 326 | int varargs, struct CTcNamedArgs *named_args, |
| 327 | int is_transient); |
| 328 | |
| 329 | /* evaluate a property ID */ |
| 330 | virtual vm_prop_id_t gen_code_propid(int check_only, int is_expr); |
| 331 | |
| 332 | /* generate code for a member expression */ |
| 333 | virtual void gen_code_member(int discard, |
| 334 | class CTcPrsNode *prop_expr, |
| 335 | int prop_is_expr, |
| 336 | int argc, int varargs, |
| 337 | struct CTcNamedArgs *named_args); |
| 338 | |
| 339 | /* get the object value for a '.' expression */ |
| 340 | virtual vm_obj_id_t gen_code_obj_predot(int *is_self); |
| 341 | |
| 342 | /* |
| 343 | * Write the symbol to a local frame debugging record in the code |
| 344 | * stream. Returns true if we wrote the symbol, false if not. By |
| 345 | * default, we'll write nothing and return false, since most symbols |
| 346 | * aren't used in local symbol tables. |
| 347 | */ |
| 348 | virtual int write_to_debug_frame(int test_only) { return FALSE; } |
| 349 | |
| 350 | /* |
| 351 | * Write the symbol to the global symbol table in the debugging |
| 352 | * records in an image file. Returns true if we wrote the symbol, |
| 353 | * false if not. By default, we'll write nothing and return false, |
| 354 | * since by default symbols don't go in the image file's global |
| 355 | * symbol table. |
| 356 | */ |
| 357 | virtual int write_to_image_file_global(class CVmImageWriter *) |
| 358 | { return FALSE; } |
| 359 | }; |
| 360 | |
| 361 | #endif /* TCT3BASE_H */ |
| 362 |