| 1 | /* Copyright (c) 2009 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | Name |
| 4 | tcjsbase.h - tads 3 compiler - javascript code generator - parse node |
| 5 | base class definitions |
| 6 | Function |
| 7 | |
| 8 | Notes |
| 9 | |
| 10 | Modified |
| 11 | 02/17/09 MJRoberts - Creation |
| 12 | */ |
| 13 | |
| 14 | #ifndef TCJSBASE_H |
| 15 | #define TCJSBASE_H |
| 16 | |
| 17 | #include "tcprs.h" |
| 18 | #include "tcpnbase.h" |
| 19 | |
| 20 | /* ------------------------------------------------------------------------ */ |
| 21 | /* |
| 22 | * Base parse node class |
| 23 | */ |
| 24 | class CTcPrsNode: public CTcPrsNodeBase |
| 25 | { |
| 26 | public: |
| 27 | /* |
| 28 | * Generate code to assign into the expression as an lvalue. By |
| 29 | * default, we'll throw an error; a default node should never be |
| 30 | * called upon to generate code as an assignment target, because the |
| 31 | * impossibility of this condition should be detected during |
| 32 | * parsing. Each class that overrides check_lvalue() to return true |
| 33 | * must override this to generate code for an assignment. |
| 34 | * |
| 35 | * Returns true if the assignment type was handled, false if not. |
| 36 | * Simple assignment must always be handled, but composite |
| 37 | * assignments (such as "+=" or "*=") can refused. If the |
| 38 | * assignment isn't handled, the caller must perform a generic |
| 39 | * calculation to compute the result of the assignment (for example, |
| 40 | * for "+=", the caller must generate code compute the sum of the |
| 41 | * right-hand side and the original value of the left-hand side), |
| 42 | * then call gen_code_asi() again to generate a simple assignment. |
| 43 | * |
| 44 | * In general, gen_code_asi() must generate code for complex |
| 45 | * assignments itself only when it can take advantage of special |
| 46 | * opcodes that would result in more efficient generated code. When |
| 47 | * no special opcodes are available for the assignment, there's no |
| 48 | * need for special coding here. |
| 49 | * |
| 50 | * If 'ignore_error' is true, this should not generate an error if |
| 51 | * this node is not a suitable lvalue, but should simply return |
| 52 | * false. |
| 53 | */ |
| 54 | virtual int gen_code_asi(int discard, tc_asitype_t typ, |
| 55 | class CTcPrsNode *rhs, |
| 56 | int ignore_error); |
| 57 | |
| 58 | /* |
| 59 | * Generate code to take the address of the expression. By default, |
| 60 | * we'll throw an internal error; a default node should never be |
| 61 | * called upon to generate code to take its address, because we |
| 62 | * shouldn't be able to parse such a thing. Each class that |
| 63 | * overrides has_addr() to return true must override this to |
| 64 | * generate address code. |
| 65 | */ |
| 66 | virtual void gen_code_addr(); |
| 67 | |
| 68 | /* |
| 69 | * Generate code to call the expression as a function or method. |
| 70 | * The caller will already have generated code to push the argument |
| 71 | * list; this routine only needs to generate code to make the call. |
| 72 | * |
| 73 | * By default, we'll assume that the result of evaluating the |
| 74 | * expression is a method or function pointer, so we'll generate a |
| 75 | * call-indirect instruction to call the result of evaluating the |
| 76 | * expression. |
| 77 | */ |
| 78 | virtual void gen_code_call(int discard, int argc, int varargs); |
| 79 | |
| 80 | /* |
| 81 | * Generate code to apply operator 'new' to the expression. By |
| 82 | * default, 'new' cannot be applied to an expression; nodes that |
| 83 | * allow operator 'new' must override this. |
| 84 | */ |
| 85 | virtual void gen_code_new(int discard, int argc, int varargs, |
| 86 | int from_call, int is_transient); |
| 87 | |
| 88 | /* |
| 89 | * Generate code to evaluate a member expression on this object |
| 90 | * expression, calling the property expression given. By default, |
| 91 | * we'll evaluate our own expression to yield the object value, then |
| 92 | * get the property expression (either as a constant or by |
| 93 | * generating code to yield a property pointer), then we'll invoke |
| 94 | * that code. Nodes should override this where more specific |
| 95 | * instructions can be generated. |
| 96 | * |
| 97 | * If 'prop_is_expr' is true, the property expression (prop_expr) is |
| 98 | * a parenthesized expression; otherwise, it's a simple property |
| 99 | * symbol. (We need this information because a symbol enclosed in |
| 100 | * parentheses would be otherwise indistinguishable from a plain |
| 101 | * symbol, but the two syntax cases differ in their behavior: a |
| 102 | * plain symbol is always a property name, whereas a symbol in |
| 103 | * parentheses can be a local variable.) |
| 104 | */ |
| 105 | virtual void gen_code_member(int discard, |
| 106 | class CTcPrsNode *prop_expr, |
| 107 | int prop_is_expr, |
| 108 | int argc, int varargs); |
| 109 | |
| 110 | /* |
| 111 | * Generate code for an object on the left side of a '.' expression. |
| 112 | * If possible, return the constant object rather than generating |
| 113 | * code. If the expression refers to "self", set (*is_self) to true |
| 114 | * and return VM_INVALID_OBJ; otherwise, if the expression refers to |
| 115 | * a constant object reference, set (*is_self) to false and return |
| 116 | * the constant object value; otherwise, generate code for the |
| 117 | * expression, set (*is_self) to false, and return VM_INVALID_OBJ to |
| 118 | * indicate that the value must be obtained from the generated code |
| 119 | * at run-time. |
| 120 | * |
| 121 | * By default, we'll use generated code to get the value. |
| 122 | */ |
| 123 | virtual vm_obj_id_t gen_code_obj_predot(int *is_self) |
| 124 | { |
| 125 | /* generate the expression */ |
| 126 | gen_code(FALSE, FALSE); |
| 127 | |
| 128 | /* tell the caller that the value is not a compile-time constant */ |
| 129 | *is_self = FALSE; |
| 130 | return VM_INVALID_OBJ; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | * generic generation for a member expression after the left side |
| 136 | * has been generated |
| 137 | */ |
| 138 | static void s_gen_member_rhs(int discard, |
| 139 | class CTcPrsNode *prop_expr, |
| 140 | int prop_is_expr, int argc, int varargs); |
| 141 | |
| 142 | /* |
| 143 | * Get the property ID of this expression. If the property ID is |
| 144 | * available as a constant value, this doesn't generate any code and |
| 145 | * simply returns the constant property ID value. If this |
| 146 | * expression requires run-time evaluation, we'll generate code for |
| 147 | * the expression and return VM_INVALID_PROP to indicate that a |
| 148 | * constant property ID is not available. |
| 149 | * |
| 150 | * If 'check_only' is true, this routine should only check to see |
| 151 | * whether a constant property value is available and return the |
| 152 | * appropriate indication, but should not generate any code in any |
| 153 | * case. Errors should also be suppressed in this case, because the |
| 154 | * routine will always be called again to perform the actual |
| 155 | * generation, at which point any errors can be logged. |
| 156 | * |
| 157 | * If 'is_expr' is true, it means that this property was given as an |
| 158 | * expression by being explicitly enclosed in parentheses. If |
| 159 | * 'is_expr' is false, it means that the property was given as a |
| 160 | * simple symbol name. For cases where the property expression is a |
| 161 | * symbol, this distinction is important because we must resolve an |
| 162 | * unparenthesized symbol as a property name, even if it's hidden by |
| 163 | * a local variable. |
| 164 | */ |
| 165 | virtual vm_prop_id_t gen_code_propid(int check_only, int is_expr); |
| 166 | |
| 167 | /* |
| 168 | * generate a jump-ahead instruction, returning a new label which |
| 169 | * serves as the jump destination |
| 170 | */ |
| 171 | static struct CTcCodeLabel *gen_jump_ahead(uchar opc); |
| 172 | |
| 173 | /* define the position of a code label */ |
| 174 | static void def_label_pos(struct CTcCodeLabel *lbl); |
| 175 | |
| 176 | /* allocate a new label at the current write position */ |
| 177 | static struct CTcCodeLabel *new_label_here(); |
| 178 | }; |
| 179 | |
| 180 | /* ------------------------------------------------------------------------ */ |
| 181 | /* |
| 182 | * Basic T3-specific symbol class |
| 183 | */ |
| 184 | class CTcSymbol: public CTcSymbolBase |
| 185 | { |
| 186 | public: |
| 187 | CTcSymbol(const char *str, size_t len, int copy, tc_symtype_t typ) |
| 188 | : CTcSymbolBase(str, len, copy, typ) { } |
| 189 | |
| 190 | /* |
| 191 | * get the object value for the symbol, if the symbol has an object |
| 192 | * value; returns VM_INVALID_OBJ if the symbol is not an object |
| 193 | */ |
| 194 | virtual vm_obj_id_t get_val_obj() const { return VM_INVALID_OBJ; } |
| 195 | |
| 196 | /* generate code to evaluate the symbol */ |
| 197 | virtual void gen_code(int discard) = 0; |
| 198 | |
| 199 | /* |
| 200 | * Generate code to assign to the symbol. By default, we'll |
| 201 | * generate an error indicating that the symbol cannot be assigned |
| 202 | * into. As with CTcPrsNode::gen_code_asi(), this returns true if |
| 203 | * the assignment was generated, false if the caller must generate a |
| 204 | * generic assignment; simple assignment must always return true. |
| 205 | * |
| 206 | * If 'rhs' is null, the caller has already generated the value to |
| 207 | * be assigned, so this code doesn't need to do that. Otherwise, |
| 208 | * this code must generate the rvalue code. The reason that 'rhs' |
| 209 | * is passed down at all is that we can sometimes optimize the type |
| 210 | * of opcode we generate according to the type of value being |
| 211 | * assigned, especially when a constant value is being assigned. |
| 212 | * |
| 213 | * If 'ignore_error' is true, this should not log an error if the |
| 214 | * value cannot be assigned. |
| 215 | */ |
| 216 | virtual int gen_code_asi(int discard, tc_asitype_t typ, |
| 217 | class CTcPrsNode *rhs, int ignore_error); |
| 218 | |
| 219 | /* |
| 220 | * Generate code for taking the address of the symbol. By default, |
| 221 | * we'll generate an error indicating that the symbol's address |
| 222 | * cannot be taken. |
| 223 | */ |
| 224 | virtual void gen_code_addr(); |
| 225 | |
| 226 | /* |
| 227 | * Generate code to call the symbol. By default, we can't call a |
| 228 | * symbol. |
| 229 | */ |
| 230 | virtual void gen_code_call(int discard, int argc, int varargs); |
| 231 | |
| 232 | /* |
| 233 | * generate code for operator 'new' applied to the symbol, with the |
| 234 | * given number of arguments; by default, we can't generate any such |
| 235 | * code |
| 236 | */ |
| 237 | virtual void gen_code_new(int discard, int argc, int varargs, |
| 238 | int is_transient); |
| 239 | |
| 240 | /* evaluate a property ID */ |
| 241 | virtual vm_prop_id_t gen_code_propid(int check_only, int is_expr); |
| 242 | |
| 243 | /* generate code for a member expression */ |
| 244 | virtual void gen_code_member(int discard, |
| 245 | class CTcPrsNode *prop_expr, |
| 246 | int prop_is_expr, |
| 247 | int argc, int varargs); |
| 248 | |
| 249 | /* get the object value for a '.' expression */ |
| 250 | virtual vm_obj_id_t gen_code_obj_predot(int *is_self); |
| 251 | |
| 252 | /* |
| 253 | * Write the symbol to a local frame debugging record in the code |
| 254 | * stream. Returns true if we wrote the symbol, false if not. By |
| 255 | * default, we'll write nothing and return false, since most symbols |
| 256 | * aren't used in local symbol tables. |
| 257 | */ |
| 258 | virtual int write_to_debug_frame() { return FALSE; } |
| 259 | |
| 260 | /* |
| 261 | * Write the symbol to the global symbol table in the debugging |
| 262 | * records in an image file. Returns true if we wrote the symbol, |
| 263 | * false if not. By default, we'll write nothing and return false, |
| 264 | * since by default symbols don't go in the image file's global |
| 265 | * symbol table. |
| 266 | */ |
| 267 | virtual int write_to_image_file_global(class CVmImageWriter *) |
| 268 | { return FALSE; } |
| 269 | }; |
| 270 | |
| 271 | |
| 272 | |
| 273 | #endif /* TCJSBASE_H */ |