| 1 | /* Copyright (c) 2009 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | Name |
| 4 | tcjs.cpp - tads 3 compiler - javascript code generator |
| 5 | Function |
| 6 | |
| 7 | Notes |
| 8 | |
| 9 | Modified |
| 10 | 02/17/09 MJRoberts - Creation |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <ctype.h> |
| 15 | #include <stdarg.h> |
| 16 | #include <assert.h> |
| 17 | |
| 18 | #include "t3std.h" |
| 19 | #include "os.h" |
| 20 | #include "tcprs.h" |
| 21 | #include "tcjs.h" |
| 22 | #include "tcgen.h" |
| 23 | #include "tcmain.h" |
| 24 | #include "tcerr.h" |
| 25 | |
| 26 | |
| 27 | /* ------------------------------------------------------------------------ */ |
| 28 | /* |
| 29 | * JS stack element |
| 30 | */ |
| 31 | |
| 32 | js_expr_ele::js_expr_ele(js_expr_buf &expr, js_expr_ele *tos) |
| 33 | { |
| 34 | /* assume ownership of the expression's buffer */ |
| 35 | assume_buffer(expr.yield_buffer(), expr.getlen()); |
| 36 | |
| 37 | /* remember the old top-of-stack as our next element */ |
| 38 | nxt = tos; |
| 39 | } |
| 40 | |
| 41 | /* ------------------------------------------------------------------------ */ |
| 42 | /* |
| 43 | * Javascript code generator |
| 44 | */ |
| 45 | |
| 46 | CTcGenTarg::CTcGenTarg() |
| 47 | { |
| 48 | /* start at the first valid property and object ID */ |
| 49 | next_prop_ = 1; |
| 50 | next_obj_ = 1; |
| 51 | |
| 52 | /* no expression stack yet */ |
| 53 | expr_stack = 0; |
| 54 | expr_stack_depth = 0; |
| 55 | } |
| 56 | |
| 57 | CTcGenTarg::~CTcGenTarg() |
| 58 | { |
| 59 | /* clear the expression stack */ |
| 60 | while (expr_stack != 0) |
| 61 | pop_js_expr(); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * push a computed expression onto the value stack, replacing operands |
| 66 | * taken from the stack |
| 67 | */ |
| 68 | void CTcGenTarg::js_expr(const char *str, ...) |
| 69 | { |
| 70 | char tpl[128]; |
| 71 | js_expr_buf expr; |
| 72 | char *src; |
| 73 | size_t rem; |
| 74 | int stk_cnt = 0; |
| 75 | int n, m; |
| 76 | |
| 77 | /* first, expand the printf-style formatting to form the template */ |
| 78 | va_list args; |
| 79 | va_start(args, str); |
| 80 | vsnprintf(tpl, sizeof(tpl), str, args); |
| 81 | va_end(args); |
| 82 | |
| 83 | /* now replace $n stack elements in the template to make the expression */ |
| 84 | for (src = tpl ; *src != '\0' ; ) |
| 85 | { |
| 86 | /* check what we have */ |
| 87 | switch (*src) |
| 88 | { |
| 89 | case '$': |
| 90 | /* it's a substitution string - check the next character */ |
| 91 | switch (*++src) |
| 92 | { |
| 93 | case '1': |
| 94 | case '2': |
| 95 | case '3': |
| 96 | case '4': |
| 97 | case '5': |
| 98 | case '6': |
| 99 | case '7': |
| 100 | case '8': |
| 101 | case '9': |
| 102 | /* number - read and skip it */ |
| 103 | for (n = atoi(src++) ; isdigit(*src) ; ++src) ; |
| 104 | |
| 105 | /* note the new high-water mark, if applicable */ |
| 106 | if (n > stk_cnt) |
| 107 | stk_cnt = n; |
| 108 | |
| 109 | /* add stack element 'n' */ |
| 110 | copy_js_expr(expr, n); |
| 111 | |
| 112 | /* done */ |
| 113 | break; |
| 114 | |
| 115 | case '{': |
| 116 | /* check what follows */ |
| 117 | ++src; |
| 118 | if ((memcmp(src, "args ", 5) == 0 && isdigit(*(src+5)) |
| 119 | || (memcmp(src, ",args ", 6) == 0 && isdigit(*src+6)) |
| 120 | || (memcmp(src, "args[] ", 7) == 0 && isdigit(*src+6)))) |
| 121 | { |
| 122 | /* note if it's the comma form */ |
| 123 | int comma = (*src == ','); |
| 124 | if (comma) |
| 125 | ++src; |
| 126 | |
| 127 | /* note if it's the array form */ |
| 128 | int array = *src == '[' && *(src+1) == ']'; |
| 129 | if (array) |
| 130 | src += 2; |
| 131 | |
| 132 | /* get and skip the stack index value */ |
| 133 | for (src += 5, n = atoi(src++) ; isdigit(*src) ; ++src) ; |
| 134 | |
| 135 | /* skip the space */ |
| 136 | if (*src++ != ' ') |
| 137 | G_tok->throw_internal_error(TCERR_BAD_JS_TPL); |
| 138 | |
| 139 | /* get and skip the arg count value */ |
| 140 | for (m = atoi(src) ; isdigit(*src) ; ++src) ; |
| 141 | |
| 142 | /* for the array form, get and skip the varargs flag */ |
| 143 | int varargs = FALSE; |
| 144 | if (array) |
| 145 | { |
| 146 | /* skip the space */ |
| 147 | if (*src++ != ' ') |
| 148 | G_tok->throw_internal_error(TCERR_BAD_JS_TPL); |
| 149 | |
| 150 | /* get and skip the varargs flag */ |
| 151 | for (varargs = atoi(src) ; isdigit(*src) ; ++src) ; |
| 152 | } |
| 153 | |
| 154 | /* add a leading comma if desired and applicable */ |
| 155 | if (comma && m != 0) |
| 156 | expr += ','; |
| 157 | |
| 158 | /* start the varargs list if applicable */ |
| 159 | if (array) |
| 160 | { |
| 161 | /* check for varargs */ |
| 162 | if (varargs) |
| 163 | { |
| 164 | /* |
| 165 | * it's varargs, so the argument list was set |
| 166 | * up as an array to start with - all we need |
| 167 | * to do is add the single argument list |
| 168 | * expression from the stack |
| 169 | */ |
| 170 | m = 1; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | /* |
| 175 | * it's not varargs, so the argument list |
| 176 | * consists of the 'm' expression on the stack |
| 177 | * - add an open bracket to make the list we |
| 178 | * generate into an array |
| 179 | */ |
| 180 | expr += '['; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* copy each expression */ |
| 185 | for ( ; m != 0 ; ++n, --m) |
| 186 | { |
| 187 | /* copy this element */ |
| 188 | copy_js_expr(expr, n); |
| 189 | |
| 190 | /* if this isn't the last element, add a separator */ |
| 191 | if (m > 1 && rem != 0) |
| 192 | expr += ','; |
| 193 | } |
| 194 | |
| 195 | /* add the ']' at the end of the array, if applicable */ |
| 196 | if (array && !varargs) |
| 197 | expr += ']'; |
| 198 | |
| 199 | /* mark the last element as used */ |
| 200 | --n; |
| 201 | if (n > stk_cnt) |
| 202 | stk_cnt = n; |
| 203 | } |
| 204 | else if (memcmp(src, "disc ", 5) == 0 && isdigit(*(src+5))) |
| 205 | { |
| 206 | /* get and skip the 'n' value */ |
| 207 | src += 5; |
| 208 | n = (*src++) - '0'; |
| 209 | |
| 210 | /* mark this argument for discarding */ |
| 211 | if (n > stk_cnt) |
| 212 | stk_cnt = n; |
| 213 | } |
| 214 | else if (isdigit(*src)) |
| 215 | { |
| 216 | /* it's a number in braces - read it and skip it */ |
| 217 | for (n = atoi(src) ; isdigit(*src) ; ++src) ; |
| 218 | |
| 219 | /* add stack element 'n' */ |
| 220 | copy_js_expr(expr, n); |
| 221 | |
| 222 | /* note the new high-water mark, if applicable */ |
| 223 | if (n > stk_cnt) |
| 224 | stk_cnt = n; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* looks like a syntax error in the template */ |
| 229 | G_tok->throw_internal_error(TCERR_BAD_JS_TPL); |
| 230 | } |
| 231 | |
| 232 | /* ensure that we have a '}' following */ |
| 233 | if (*src++ != '}') |
| 234 | G_tok->throw_internal_error(TCERR_BAD_JS_TPL); |
| 235 | |
| 236 | /* done with the ${...} expression */ |
| 237 | break; |
| 238 | |
| 239 | case '$': |
| 240 | default: |
| 241 | /* copy '$', or anything not otherwise special, literally */ |
| 242 | expr += *src++; |
| 243 | break; |
| 244 | } |
| 245 | break; |
| 246 | |
| 247 | default: |
| 248 | /* copy anything else literally */ |
| 249 | expr += *src++; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* pop all of the stack elements we used */ |
| 255 | pop_js_exprs(stk_cnt); |
| 256 | |
| 257 | /* push the new expression */ |
| 258 | push_js_expr(expr); |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Push an expression onto the js stack |
| 263 | */ |
| 264 | void CTcGenTarg::push_js_expr(js_expr_buf &buf) |
| 265 | { |
| 266 | /* allocate a new expression element and link it at the top of the stack */ |
| 267 | expr_stack = new js_expr_ele(buf, expr_stack); |
| 268 | ++expr_stack_depth; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Pop a number of expressions from the js stack |
| 273 | */ |
| 274 | void CTcGenTarg::pop_js_exprs(int n) |
| 275 | { |
| 276 | while (n-- != 0 && expr_stack != 0) |
| 277 | { |
| 278 | /* unlink this item from the stack */ |
| 279 | js_expr_ele *ele = expr_stack; |
| 280 | expr_stack = expr_stack->getnxt(); |
| 281 | --expr_stack_depth; |
| 282 | |
| 283 | /* delete this element */ |
| 284 | delete ele; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Copy a js expression into the given buffer |
| 290 | */ |
| 291 | void CTcGenTarg::copy_js_expr(js_expr_buf &dst, int n) |
| 292 | { |
| 293 | js_expr_ele *ele; |
| 294 | |
| 295 | /* find the nth expression stack element (1 = top of stack) */ |
| 296 | for (ele = expr_stack ; ele != 0 && n > 1 ; --n, ele = ele->getnxt()) ; |
| 297 | |
| 298 | /* if we found it, copy it */ |
| 299 | if (n == 1) |
| 300 | dst += ele; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /* ------------------------------------------------------------------------ */ |
| 305 | /* |
| 306 | * Generic parse node |
| 307 | */ |
| 308 | |
| 309 | |
| 310 | /* |
| 311 | * generate code for assignment to this node |
| 312 | */ |
| 313 | int CTcPrsNode::gen_code_asi(int, tc_asitype_t, CTcPrsNode *, |
| 314 | int ignore_error) |
| 315 | { |
| 316 | /* |
| 317 | * if ignoring errors, the caller is trying to assign if possible |
| 318 | * but doesn't require it to be possible; simply return false to |
| 319 | * indicate that nothing happened if this is the case |
| 320 | */ |
| 321 | if (ignore_error) |
| 322 | return FALSE; |
| 323 | |
| 324 | /* we should never get here - throw an internal error */ |
| 325 | G_tok->throw_internal_error(TCERR_GEN_BAD_LVALUE); |
| 326 | AFTER_ERR_THROW(return FALSE;) |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * generate code for taking the address of this node |
| 331 | */ |
| 332 | void CTcPrsNode::gen_code_addr() |
| 333 | { |
| 334 | /* we should never get here - throw an internal error */ |
| 335 | G_tok->throw_internal_error(TCERR_GEN_BAD_ADDR); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Generate code to call the expression as a function or method. |
| 340 | */ |
| 341 | void CTcPrsNode::gen_code_call(int discard, int argc, int varargs) |
| 342 | { |
| 343 | /* function/method calls are never valid in speculative mode */ |
| 344 | if (G_cg->is_speculative()) |
| 345 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 346 | |
| 347 | /* |
| 348 | * For default nodes, assume that the result of evaluating the |
| 349 | * expression contained in the node is a method or function pointer. |
| 350 | * First, generate code to evaluate the expression, which should |
| 351 | * yield an appropriate pointer value. |
| 352 | */ |
| 353 | gen_code(FALSE, FALSE); |
| 354 | |
| 355 | /* if we have a varargs list, we have to use special syntax */ |
| 356 | if (varargs) |
| 357 | { |
| 358 | /* varargs - use the 'apply' syntax to pass an argument array */ |
| 359 | G_cg->js_expr("(($1).apply(null, $2))"); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | /* fixed arguments - generate a regular call */ |
| 364 | G_cg->js_expr("(($1).call(null ${,args 2 %d})))", argc); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Generate code for operator 'new' applied to this node |
| 370 | */ |
| 371 | void CTcPrsNode::gen_code_new(int, int, int, int, int) |
| 372 | { |
| 373 | /* operator 'new' cannot be applied to a default node */ |
| 374 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Generate code for a member evaluation |
| 379 | */ |
| 380 | void CTcPrsNode::gen_code_member(int discard, |
| 381 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 382 | int argc, int varargs) |
| 383 | { |
| 384 | /* evaluate my own expression to yield the object value */ |
| 385 | gen_code(FALSE, FALSE); |
| 386 | |
| 387 | /* use the generic code to generate the rest */ |
| 388 | s_gen_member_rhs(discard, prop_expr, prop_is_expr, argc, varargs); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Generic code to generate the rest of a member expression after the |
| 393 | * left side of the '.' has been generated. This can be used for cases |
| 394 | * where the left of the '.' is an arbitrary expression, and hence must |
| 395 | * be evaluated at run-time. |
| 396 | */ |
| 397 | void CTcPrsNode::s_gen_member_rhs(int discard, |
| 398 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 399 | int argc, int varargs) |
| 400 | { |
| 401 | /* we can't call methods with arguments in speculative mode */ |
| 402 | if (argc != 0 && G_cg->is_speculative()) |
| 403 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 404 | |
| 405 | /* get or generate the property ID value */ |
| 406 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 407 | |
| 408 | /* generate the getprop */ |
| 409 | G_cg->js_expr("T3_getprop($1, $2, ${args[] 3 %d %d})", argc, varargs); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Generate code to get the property ID of the expression. |
| 414 | */ |
| 415 | vm_prop_id_t CTcPrsNode::gen_code_propid(int check_only, int is_expr) |
| 416 | { |
| 417 | /* |
| 418 | * simply evaluate the expression normally, anticipating that this |
| 419 | * will yield a property ID value at run-time |
| 420 | */ |
| 421 | if (!check_only) |
| 422 | gen_code(FALSE, FALSE); |
| 423 | |
| 424 | /* tell the caller that there's no constant ID available */ |
| 425 | return VM_INVALID_PROP; |
| 426 | } |
| 427 | |
| 428 | /* ------------------------------------------------------------------------ */ |
| 429 | /* |
| 430 | * "self" |
| 431 | */ |
| 432 | |
| 433 | /* |
| 434 | * generate code |
| 435 | */ |
| 436 | void CTPNSelf::gen_code(int discard, int) |
| 437 | { |
| 438 | /* it's an error if we're not in a method context */ |
| 439 | if (!G_cs->is_self_available()) |
| 440 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 441 | |
| 442 | /* "self" is the js "this" */ |
| 443 | if (!discard) |
| 444 | G_cg->js_expr("this"); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * evaluate a property |
| 449 | */ |
| 450 | void CTPNSelf::gen_code_member(int discard, |
| 451 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 452 | int argc, int varargs) |
| 453 | { |
| 454 | /* make sure "self" is available */ |
| 455 | if (!G_cs->is_self_available()) |
| 456 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 457 | |
| 458 | /* don't allow arguments in speculative eval mode */ |
| 459 | if (argc != 0 && G_cg->is_speculative()) |
| 460 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 461 | |
| 462 | /* we currently don't implement speculative evaluation at all */ |
| 463 | if (G_cg->is_speculative()) |
| 464 | assert(FALSE); |
| 465 | |
| 466 | /* generate the property value */ |
| 467 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 468 | |
| 469 | /* generate the getprop */ |
| 470 | G_cg->js_expr("T3_getprop(this, $1, ${args[] 2 %d %d})", argc, varargs); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* |
| 475 | * generate code for an object before a '.' |
| 476 | */ |
| 477 | vm_obj_id_t CTPNSelf::gen_code_obj_predot(int *is_self) |
| 478 | { |
| 479 | /* make sure "self" is available */ |
| 480 | if (!G_cs->is_self_available()) |
| 481 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 482 | |
| 483 | /* tell the caller that this is "self" */ |
| 484 | *is_self = TRUE; |
| 485 | return VM_INVALID_OBJ; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | /* ------------------------------------------------------------------------ */ |
| 490 | /* |
| 491 | * "replaced" |
| 492 | */ |
| 493 | |
| 494 | /* |
| 495 | * evaluate 'replaced' on its own - this simply yields a function pointer |
| 496 | * to the modified base code |
| 497 | */ |
| 498 | void CTPNReplaced::gen_code(int discard, int for_condition) |
| 499 | { |
| 500 | /* get the modified base function symbol */ |
| 501 | CTcSymFunc *mod_base = G_cs->get_code_body()->get_replaced_func(); |
| 502 | |
| 503 | /* make sure we're in a 'modify func()' context */ |
| 504 | if (mod_base == 0) |
| 505 | G_tok->log_error(TCERR_REPLACED_NOT_AVAIL); |
| 506 | |
| 507 | /* this expression yields a pointer to the modified base function */ |
| 508 | G_cg->js_expr("T3F_%.*s", mod_base->getlen(), mod_base->getstr()); |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * 'replaced()' call - this invokes the modified base code |
| 513 | */ |
| 514 | void CTPNReplaced::gen_code_call(int discard, int argc, int varargs) |
| 515 | { |
| 516 | /* get the modified base function symbol */ |
| 517 | CTcSymFunc *mod_base = G_cs->get_code_body()->get_replaced_func(); |
| 518 | |
| 519 | /* make sure we're in a 'modify func()' context */ |
| 520 | if (mod_base == 0) |
| 521 | G_tok->log_error(TCERR_REPLACED_NOT_AVAIL); |
| 522 | |
| 523 | /* call it */ |
| 524 | if (varargs) |
| 525 | G_cg->js_expr("T3F_%.*s.apply(null, $1)"); |
| 526 | else |
| 527 | G_cg->js_expr("T3F_%.*s(${args 1 %d})", argc); |
| 528 | |
| 529 | /* make sure the argument count is correct */ |
| 530 | if (mod_base != 0 |
| 531 | && (mod_base->is_varargs() ? argc < mod_base->get_argc() |
| 532 | : argc != mod_base->get_argc())) |
| 533 | G_tok->log_error(TCERR_WRONG_ARGC_FOR_FUNC, |
| 534 | (int)mod_base->get_sym_len(), mod_base->get_sym(), |
| 535 | mod_base->get_argc(), argc); |
| 536 | } |
| 537 | |
| 538 | /* ------------------------------------------------------------------------ */ |
| 539 | /* |
| 540 | * "targetprop" |
| 541 | */ |
| 542 | |
| 543 | /* |
| 544 | * generate code |
| 545 | */ |
| 546 | void CTPNTargetprop::gen_code(int discard, int) |
| 547 | { |
| 548 | /* it's an error if we're not in a method context */ |
| 549 | if (!G_cs->is_self_available()) |
| 550 | G_tok->log_error(TCERR_TARGETPROP_NOT_AVAIL); |
| 551 | |
| 552 | /* if we're not discarding the result, push the target property ID */ |
| 553 | if (!discard) |
| 554 | G_cg->js_expr("T3V_targetprop"); |
| 555 | } |
| 556 | |
| 557 | /* ------------------------------------------------------------------------ */ |
| 558 | /* |
| 559 | * "targetobj" |
| 560 | */ |
| 561 | |
| 562 | /* |
| 563 | * generate code |
| 564 | */ |
| 565 | void CTPNTargetobj::gen_code(int discard, int) |
| 566 | { |
| 567 | /* it's an error if we're not in a method context */ |
| 568 | if (!G_cs->is_self_available()) |
| 569 | G_tok->log_error(TCERR_TARGETOBJ_NOT_AVAIL); |
| 570 | |
| 571 | /* if we're not discarding the result, push the target object ID */ |
| 572 | if (!discard) |
| 573 | G_cg->js_expr("T3V_targetobj"); |
| 574 | } |
| 575 | |
| 576 | /* ------------------------------------------------------------------------ */ |
| 577 | /* |
| 578 | * "definingobj" |
| 579 | */ |
| 580 | |
| 581 | /* |
| 582 | * generate code |
| 583 | */ |
| 584 | void CTPNDefiningobj::gen_code(int discard, int) |
| 585 | { |
| 586 | /* it's an error if we're not in a method context */ |
| 587 | if (!G_cs->is_self_available()) |
| 588 | G_tok->log_error(TCERR_DEFININGOBJ_NOT_AVAIL); |
| 589 | |
| 590 | /* if we're not discarding the result, push the defining object ID */ |
| 591 | if (!discard) |
| 592 | G_cg->js_expr("T3V_defobj"); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /* ------------------------------------------------------------------------ */ |
| 597 | /* |
| 598 | * "inherited" |
| 599 | */ |
| 600 | void CTPNInh::gen_code(int, int) |
| 601 | { |
| 602 | /* |
| 603 | * we should never be asked to generate an "inherited" node |
| 604 | * directly; these nodes should always be generated as part of |
| 605 | * member evaluation |
| 606 | */ |
| 607 | G_tok->throw_internal_error(TCERR_GEN_CODE_INH); |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * evaluate a property |
| 612 | */ |
| 613 | void CTPNInh::gen_code_member(int discard, |
| 614 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 615 | int argc, int varargs) |
| 616 | { |
| 617 | /* |
| 618 | * make sure "self" is available - we obviously can't inherit |
| 619 | * anything if we're not in an object's method |
| 620 | */ |
| 621 | if (!G_cs->is_self_available()) |
| 622 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 623 | |
| 624 | /* don't allow 'inherited' in speculative evaluation mode */ |
| 625 | if (G_cg->is_speculative()) |
| 626 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 627 | |
| 628 | /* generate the property value */ |
| 629 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 630 | |
| 631 | /* generate the inhprop call */ |
| 632 | G_cg->js_expr("T3ctx.T3_inhprop($1, ${args[] 2 %d %d})", |
| 633 | argc, varargs); |
| 634 | } |
| 635 | |
| 636 | /* ------------------------------------------------------------------------ */ |
| 637 | /* |
| 638 | * "inherited class" |
| 639 | */ |
| 640 | void CTPNInhClass::gen_code(int discard, int for_condition) |
| 641 | { |
| 642 | /* |
| 643 | * we should never be asked to generate an "inherited" node |
| 644 | * directly; these nodes should always be generated as part of |
| 645 | * member evaluation |
| 646 | */ |
| 647 | G_tok->throw_internal_error(TCERR_GEN_CODE_INH); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * evaluate a property |
| 652 | */ |
| 653 | void CTPNInhClass::gen_code_member(int discard, |
| 654 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 655 | int argc, int varargs) |
| 656 | { |
| 657 | CTcSymbol *objsym; |
| 658 | vm_obj_id_t obj; |
| 659 | |
| 660 | /* |
| 661 | * make sure "self" is available - we obviously can't inherit |
| 662 | * anything if we're not in an object's method |
| 663 | */ |
| 664 | if (!G_cs->is_self_available()) |
| 665 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 666 | |
| 667 | /* don't allow 'inherited' in speculative evaluation mode */ |
| 668 | if (G_cg->is_speculative()) |
| 669 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 670 | |
| 671 | /* get the superclass name symbol */ |
| 672 | objsym = G_cs->get_symtab()->find_or_def_undef(sym_, len_, FALSE); |
| 673 | |
| 674 | /* if it's not an object, we can't inherit from it */ |
| 675 | obj = objsym->get_val_obj(); |
| 676 | if (obj == VM_INVALID_OBJ) |
| 677 | { |
| 678 | G_tok->log_error(TCERR_INH_NOT_OBJ, (int)len_, sym_); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | /* generate the property value */ |
| 683 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 684 | |
| 685 | /* generate the inhprop */ |
| 686 | G_cg->js_expr("T3ctx.T3_inhprop_from(T3O_%.*s, $1, ${args[] 2 %d %d})", |
| 687 | objsym->getlen(), objsym->getstr(), |
| 688 | argc, varargs); |
| 689 | } |
| 690 | |
| 691 | /* ------------------------------------------------------------------------ */ |
| 692 | /* |
| 693 | * "delegated" |
| 694 | */ |
| 695 | void CTPNDelegated::gen_code(int discard, int for_condition) |
| 696 | { |
| 697 | /* |
| 698 | * we should never be asked to generate a "delegated" node directly; |
| 699 | * these nodes should always be generated as part of member evaluation |
| 700 | */ |
| 701 | G_tok->throw_internal_error(TCERR_GEN_CODE_DELEGATED); |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * evaluate a property |
| 706 | */ |
| 707 | void CTPNDelegated::gen_code_member(int discard, |
| 708 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 709 | int argc, int varargs) |
| 710 | { |
| 711 | /* |
| 712 | * make sure "self" is available - we obviously can't delegate |
| 713 | * anything if we're not in an object's method |
| 714 | */ |
| 715 | if (!G_cs->is_self_available()) |
| 716 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 717 | |
| 718 | /* don't allow 'delegated' in speculative evaluation mode */ |
| 719 | if (G_cg->is_speculative()) |
| 720 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 721 | |
| 722 | /* generate the property value */ |
| 723 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 724 | |
| 725 | /* generate the delegatee expression */ |
| 726 | delegatee_->gen_code(FALSE, FALSE); |
| 727 | |
| 728 | /* generate the delegate call */ |
| 729 | G_cg->js_expr("T3_delegate($1, $2, ${args[] 3 %d %d})", argc, varargs); |
| 730 | } |
| 731 | |
| 732 | |
| 733 | /* ------------------------------------------------------------------------ */ |
| 734 | /* |
| 735 | * "argcount" |
| 736 | */ |
| 737 | void CTPNArgc::gen_code(int discard, int) |
| 738 | { |
| 739 | /* generate the argument count, if we're not discarding */ |
| 740 | if (!discard) |
| 741 | G_cg->js_expr("arguments.length"); |
| 742 | } |
| 743 | |
| 744 | |
| 745 | /* ------------------------------------------------------------------------ */ |
| 746 | /* |
| 747 | * constant |
| 748 | */ |
| 749 | void CTPNConst::gen_code(int discard, int) |
| 750 | { |
| 751 | /* if we're discarding the value, do nothing */ |
| 752 | if (discard) |
| 753 | return; |
| 754 | |
| 755 | /* generate the value into an expression buffer */ |
| 756 | js_expr_buf expr; |
| 757 | gen_code(expr); |
| 758 | |
| 759 | /* push the expression onto the value stack */ |
| 760 | G_cg->push_js_expr(expr); |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * append a constant value to an expression buffer |
| 765 | */ |
| 766 | void CTPNConst::gen_code(js_expr_buf &expr) |
| 767 | { |
| 768 | char buf[64]; |
| 769 | |
| 770 | /* generate the appropriate type of push for the value */ |
| 771 | switch(val_.get_type()) |
| 772 | { |
| 773 | case TC_CVT_NIL: |
| 774 | expr += "null"; |
| 775 | break; |
| 776 | |
| 777 | case TC_CVT_TRUE: |
| 778 | expr += "true"; |
| 779 | break; |
| 780 | |
| 781 | case TC_CVT_INT: |
| 782 | /* write the push-integer instruction */ |
| 783 | s_gen_code_int(expr, val_.get_val_int()); |
| 784 | break; |
| 785 | |
| 786 | case TC_CVT_FLOAT: |
| 787 | /* represent it as a regular js floating point value */ |
| 788 | expr.append(val_.get_val_float(), val_.get_val_float_len()); |
| 789 | break; |
| 790 | |
| 791 | case TC_CVT_SSTR: |
| 792 | { |
| 793 | /* start with an open quote */ |
| 794 | expr += '"'; |
| 795 | |
| 796 | /* add characters from the string, quoting specials */ |
| 797 | const char *start, *str = val_.get_val_str(); |
| 798 | size_t len = val_.get_val_str_len(); |
| 799 | for (start = str ; ; ++str, --len) |
| 800 | { |
| 801 | /* we need to escape double quotes and control characters */ |
| 802 | int esc = (*str == '"' || *str < 32); |
| 803 | |
| 804 | /* |
| 805 | * if we're escaping this character, or we've reached the |
| 806 | * end of the string, copy out the chunk up to this point |
| 807 | */ |
| 808 | if ((len == 0 || esc) && str != start) |
| 809 | expr.append(start, str - start); |
| 810 | |
| 811 | /* if this is the end of the string, we're done */ |
| 812 | if (len == 0) |
| 813 | break; |
| 814 | |
| 815 | /* escape the current character if necessary */ |
| 816 | if (esc) |
| 817 | { |
| 818 | /* escape it */ |
| 819 | if (*str == '"') |
| 820 | { |
| 821 | /* double quote - generate the \" sequence */ |
| 822 | expr += "\\\""; |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | /* control character - generate an octal \nnn sequence */ |
| 827 | char escbuf[30]; |
| 828 | sprintf(escbuf, "\\%03o", *str); |
| 829 | expr += escbuf; |
| 830 | } |
| 831 | |
| 832 | /* the next segment starts at the next character */ |
| 833 | start = str + 1; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* add the close quote */ |
| 838 | expr += '"'; |
| 839 | } |
| 840 | break; |
| 841 | |
| 842 | case TC_CVT_LIST: |
| 843 | { |
| 844 | /* open the array expression */ |
| 845 | expr += '['; |
| 846 | |
| 847 | /* add the list elements */ |
| 848 | int i; |
| 849 | CTPNListEle *cur; |
| 850 | for (cur = val_.get_val_list()->get_head(), i = 0 ; cur != 0 ; |
| 851 | cur = cur->get_next(), ++i) |
| 852 | { |
| 853 | /* add a comma before each element after the first */ |
| 854 | if (i != 0) |
| 855 | expr += ','; |
| 856 | |
| 857 | /* generate this expression */ |
| 858 | cur->get_expr()->gen_code(FALSE, FALSE); |
| 859 | |
| 860 | /* pop it into the expression buffer */ |
| 861 | G_cg->pop_js_expr(expr); |
| 862 | } |
| 863 | |
| 864 | /* close the array expression */ |
| 865 | expr += ']'; |
| 866 | } |
| 867 | break; |
| 868 | |
| 869 | case TC_CVT_OBJ: |
| 870 | /* generate the object ID */ |
| 871 | // $$$ need the symbolic name instead |
| 872 | sprintf(buf, "T3O_%d", val_.get_val_obj()); |
| 873 | expr += buf; |
| 874 | break; |
| 875 | |
| 876 | case TC_CVT_PROP: |
| 877 | /* generate the property ID */ |
| 878 | // $$$ need the symbolic name instead |
| 879 | sprintf(buf, "T3P_%d", val_.get_val_prop()); |
| 880 | expr += buf; |
| 881 | break; |
| 882 | |
| 883 | case TC_CVT_ENUM: |
| 884 | /* generate the enum value */ |
| 885 | // $$$ need the symbolic name instead |
| 886 | sprintf(buf, "T3E_%d", val_.get_val_enum()); |
| 887 | expr += buf; |
| 888 | break; |
| 889 | |
| 890 | case TC_CVT_FUNCPTR: |
| 891 | { |
| 892 | /* generate the function pointer */ |
| 893 | CTcSymFunc *func = val_.get_val_funcptr_sym(); |
| 894 | expr.append(func->getstr(), func->getlen()); |
| 895 | } |
| 896 | break; |
| 897 | |
| 898 | case TC_CVT_ANONFUNCPTR: |
| 899 | /* generate a js anonymous function from the code body */ |
| 900 | val_.get_val_anon_func_ptr()->js_anon_func(); |
| 901 | |
| 902 | /* that's our value - pop it into the expression buffer */ |
| 903 | G_cg->pop_js_expr(expr); |
| 904 | break; |
| 905 | |
| 906 | default: |
| 907 | /* anything else is an internal error */ |
| 908 | G_tok->throw_internal_error(TCERR_GEN_UNK_CONST_TYPE); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * generate code to push an integer value |
| 914 | */ |
| 915 | void CTPNConst::s_gen_code_int(js_expr_buf &expr, long intval) |
| 916 | { |
| 917 | char buf[32]; |
| 918 | |
| 919 | sprintf(buf, "%d", intval); |
| 920 | expr += buf; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Generate code to apply operator 'new' to the constant. We can apply |
| 925 | * 'new' only to constant object values. |
| 926 | */ |
| 927 | void CTPNConst::gen_code_new(int discard, int argc, int varargs, |
| 928 | int /*from_call*/, int is_transient) |
| 929 | { |
| 930 | /* check the type */ |
| 931 | switch(val_.get_type()) |
| 932 | { |
| 933 | case TC_CVT_OBJ: |
| 934 | /* |
| 935 | * Treat this the same as any other 'new' call. An object symbol |
| 936 | * folded into a constant is guaranteed to be of metaclass |
| 937 | * TadsObject - that's the only kind of symbol we'll ever fold this |
| 938 | * way. |
| 939 | */ |
| 940 | CTcSymObj::s_gen_code_new(discard, |
| 941 | val_.get_val_obj(), val_.get_val_obj_meta(), |
| 942 | argc, varargs, is_transient); |
| 943 | break; |
| 944 | |
| 945 | default: |
| 946 | /* can't apply 'new' to other constant values */ |
| 947 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * Generate code to make a function call to this expression. If we're |
| 954 | * calling a function, we can generate this directly. |
| 955 | */ |
| 956 | void CTPNConst::gen_code_call(int discard, int argc, int varargs) |
| 957 | { |
| 958 | /* check our type */ |
| 959 | switch(val_.get_type()) |
| 960 | { |
| 961 | case TC_CVT_FUNCPTR: |
| 962 | /* generate a call to our function symbol */ |
| 963 | val_.get_val_funcptr_sym()->gen_code_call(discard, argc, varargs); |
| 964 | break; |
| 965 | |
| 966 | default: |
| 967 | /* other types cannot be called */ |
| 968 | G_tok->log_error(TCERR_CANNOT_CALL_CONST); |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * generate a property ID expression |
| 975 | */ |
| 976 | vm_prop_id_t CTPNConst::gen_code_propid(int check_only, int is_expr) |
| 977 | { |
| 978 | /* check the type */ |
| 979 | switch(val_.get_type()) |
| 980 | { |
| 981 | case TC_CVT_PROP: |
| 982 | /* generate the code */ |
| 983 | // $$$ make it symbolic? |
| 984 | G_cg->js_expr("T3P_%d", val_.get_val_prop()); |
| 985 | |
| 986 | /* return the constant property ID */ |
| 987 | return (vm_prop_id_t)val_.get_val_prop(); |
| 988 | |
| 989 | default: |
| 990 | /* other values cannot be used as properties */ |
| 991 | if (!check_only) |
| 992 | G_tok->log_error(TCERR_INVAL_PROP_EXPR); |
| 993 | |
| 994 | /* indicate it's not a property value */ |
| 995 | return VM_INVALID_PROP; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | /* |
| 1001 | * Generate code for a member evaluation |
| 1002 | */ |
| 1003 | void CTPNConst::gen_code_member(int discard, |
| 1004 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 1005 | int argc, int varargs) |
| 1006 | { |
| 1007 | /* check our constant type */ |
| 1008 | switch(val_.get_type()) |
| 1009 | { |
| 1010 | case TC_CVT_OBJ: |
| 1011 | /* call the object symbol code to do the work */ |
| 1012 | CTcSymObj::s_gen_code_member(discard, prop_expr, prop_is_expr, |
| 1013 | argc, val_.get_val_obj(), varargs); |
| 1014 | break; |
| 1015 | |
| 1016 | case TC_CVT_LIST: |
| 1017 | case TC_CVT_SSTR: |
| 1018 | case TC_CVT_FLOAT: |
| 1019 | /* |
| 1020 | * list/string/BigNumber constant - generate our value as |
| 1021 | * normal, then use the standard member generation |
| 1022 | */ |
| 1023 | gen_code(FALSE, FALSE); |
| 1024 | |
| 1025 | /* use standard member generation */ |
| 1026 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 1027 | argc, varargs); |
| 1028 | break; |
| 1029 | |
| 1030 | default: |
| 1031 | G_tok->log_error(TCERR_INVAL_OBJ_EXPR); |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | /* |
| 1038 | * generate code for an object before a '.' |
| 1039 | */ |
| 1040 | vm_obj_id_t CTPNConst::gen_code_obj_predot(int *is_self) |
| 1041 | { |
| 1042 | /* we're certainly not "self" */ |
| 1043 | *is_self = FALSE; |
| 1044 | |
| 1045 | /* if I don't have an object value, this is illegal */ |
| 1046 | if (val_.get_type() != TC_CVT_OBJ) |
| 1047 | { |
| 1048 | G_tok->log_error(TCERR_INVAL_OBJ_EXPR); |
| 1049 | return VM_INVALID_OBJ; |
| 1050 | } |
| 1051 | |
| 1052 | /* report our constant object value */ |
| 1053 | return val_.get_val_obj(); |
| 1054 | } |
| 1055 | |
| 1056 | /* ------------------------------------------------------------------------ */ |
| 1057 | /* |
| 1058 | * debugger constant |
| 1059 | */ |
| 1060 | void CTPNDebugConst::gen_code(int discard, int for_condition) |
| 1061 | { |
| 1062 | /* not currently implemented */ |
| 1063 | assert(FALSE); |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | /* ------------------------------------------------------------------------ */ |
| 1068 | /* |
| 1069 | * Generic Unary Operator |
| 1070 | */ |
| 1071 | |
| 1072 | /* |
| 1073 | * Generate a unary-operator opcode. We assume that the opcode has no |
| 1074 | * side effects other than to compute the result, so we do not generate |
| 1075 | * the opcode at all if 'discard' is true; we do, however, always |
| 1076 | * generate code for the subexpression to ensure that its side effects |
| 1077 | * are performed. |
| 1078 | * |
| 1079 | * In most cases, the caller simply should pass through its 'discard' |
| 1080 | * status, since the result of the subexpression is generally needed |
| 1081 | * only when the result of the enclosing expression is needed. |
| 1082 | * |
| 1083 | * In most cases, the caller should pass FALSE for 'for_condition', |
| 1084 | * because applying an operator to the result generally requires that |
| 1085 | * the result be properly converted for use as a temporary value. |
| 1086 | * However, when the caller knows that its own opcode will perform the |
| 1087 | * same conversions that a conditional opcode would, 'for_condition' |
| 1088 | * should be TRUE. In most cases, the caller's own 'for_condition' |
| 1089 | * status is not relevant and should thus not be passed through. |
| 1090 | */ |
| 1091 | void CTPNUnary::gen_unary(const char *op, int discard, int for_condition) |
| 1092 | { |
| 1093 | /* |
| 1094 | * Generate the operand. Pass through the 'discard' status to the |
| 1095 | * operand - if the result of the parent operator is being |
| 1096 | * discarded, then so is the result of this subexpression. In |
| 1097 | * addition, pass through the caller's 'for_condition' disposition. |
| 1098 | */ |
| 1099 | sub_->gen_code(discard, for_condition); |
| 1100 | |
| 1101 | /* apply the operator if we're not discarding the result */ |
| 1102 | if (!discard) |
| 1103 | G_cg->js_expr("%s($1)", op); |
| 1104 | } |
| 1105 | |
| 1106 | /* ------------------------------------------------------------------------ */ |
| 1107 | /* |
| 1108 | * Generic Binary Operator |
| 1109 | */ |
| 1110 | |
| 1111 | /* |
| 1112 | * Generate a binary-operator opcode. |
| 1113 | * |
| 1114 | * In most cases, the caller's 'discard' status should be passed |
| 1115 | * through, since the results of the operands are usually needed if and |
| 1116 | * only if the results of the enclosing expression are needed. |
| 1117 | * |
| 1118 | * In most cases, the caller should pass FALSE for 'for_condition'. |
| 1119 | * Only when the caller knows that the opcode will perform the same |
| 1120 | * conversions as a BOOLIZE instruction should it pass TRUE for |
| 1121 | * 'for_condition'. |
| 1122 | */ |
| 1123 | void CTPNBin::gen_binary(const char *op, int discard, int for_condition) |
| 1124 | { |
| 1125 | /* |
| 1126 | * generate the operands, passing through the discard and |
| 1127 | * conditional status |
| 1128 | */ |
| 1129 | left_->gen_code(discard, for_condition); |
| 1130 | right_->gen_code(discard, for_condition); |
| 1131 | |
| 1132 | /* generate our operand if we're not discarding the result */ |
| 1133 | if (!discard) |
| 1134 | G_cg->js_expr("($1)%s($2)", op); |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | /* ------------------------------------------------------------------------ */ |
| 1140 | /* |
| 1141 | * logical NOT |
| 1142 | */ |
| 1143 | void CTPNNot::gen_code(int discard, int) |
| 1144 | { |
| 1145 | /* |
| 1146 | * Generate the subexpression and apply the NOT opcode. Note that we |
| 1147 | * have to generate the subexpression as a regular value, not a |
| 1148 | * conditional, since javascript treats empty strings as false values |
| 1149 | * with this operator. |
| 1150 | */ |
| 1151 | gen_unary("!", discard, FALSE); |
| 1152 | } |
| 1153 | |
| 1154 | /* ------------------------------------------------------------------------ */ |
| 1155 | /* |
| 1156 | * Boolean-ize operator |
| 1157 | */ |
| 1158 | void CTPNBoolize::gen_code(int discard, int for_condition) |
| 1159 | { |
| 1160 | /* |
| 1161 | * If the result will be used for a conditional, there's no need to |
| 1162 | * generate an instruction to convert the value to boolean. The opcode |
| 1163 | * that will be used for the condition will perform exactly the same |
| 1164 | * conversions that this opcode would apply; avoid the redundant work |
| 1165 | * in this case, and simply generate the underlying expression |
| 1166 | * directly. |
| 1167 | */ |
| 1168 | if (for_condition) |
| 1169 | { |
| 1170 | /* generate the underlying expression without modification */ |
| 1171 | sub_->gen_code(discard, for_condition); |
| 1172 | |
| 1173 | /* done */ |
| 1174 | return; |
| 1175 | } |
| 1176 | |
| 1177 | /* turn it into a boolean value */ |
| 1178 | gen_unary("T3_boolize", discard, for_condition); |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | /* ------------------------------------------------------------------------ */ |
| 1183 | /* |
| 1184 | * bitwise NOT |
| 1185 | */ |
| 1186 | void CTPNBNot::gen_code(int discard, int) |
| 1187 | { |
| 1188 | gen_unary("~", discard, FALSE); |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | /* ------------------------------------------------------------------------ */ |
| 1193 | /* |
| 1194 | * arithmetic positive |
| 1195 | */ |
| 1196 | void CTPNPos::gen_code(int discard, int) |
| 1197 | { |
| 1198 | /* |
| 1199 | * simply generate our operand, since the operator itself has no |
| 1200 | * effect |
| 1201 | */ |
| 1202 | sub_->gen_code(discard, FALSE); |
| 1203 | } |
| 1204 | |
| 1205 | /* ------------------------------------------------------------------------ */ |
| 1206 | /* |
| 1207 | * unary arithmetic negative |
| 1208 | */ |
| 1209 | void CTPNNeg::gen_code(int discard, int) |
| 1210 | { |
| 1211 | gen_unary("-", discard, FALSE); |
| 1212 | } |
| 1213 | |
| 1214 | /* ------------------------------------------------------------------------ */ |
| 1215 | /* |
| 1216 | * pre-increment |
| 1217 | */ |
| 1218 | void CTPNPreInc::gen_code(int discard, int) |
| 1219 | { |
| 1220 | /* ask the subnode to generate it */ |
| 1221 | if (!sub_->gen_code_asi(discard, TC_ASI_PREINC, 0, FALSE)) |
| 1222 | { |
| 1223 | /* |
| 1224 | * the subnode didn't handle it - generate code to evaluate the |
| 1225 | * subnode, increment that value, then assign the result back to |
| 1226 | * the subnode with a simple assignment |
| 1227 | */ |
| 1228 | sub_->gen_code(FALSE, FALSE); |
| 1229 | |
| 1230 | /* increment the value at top of stack */ |
| 1231 | G_cg->js_expr("++($1)"); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* ------------------------------------------------------------------------ */ |
| 1236 | /* |
| 1237 | * pre-decrement |
| 1238 | */ |
| 1239 | void CTPNPreDec::gen_code(int discard, int) |
| 1240 | { |
| 1241 | /* ask the subnode to generate it */ |
| 1242 | if (!sub_->gen_code_asi(discard, TC_ASI_PREDEC, 0, FALSE)) |
| 1243 | { |
| 1244 | /* |
| 1245 | * the subnode didn't handle it - generate code to evaluate the |
| 1246 | * subnode, decrement that value, then assign the result back to |
| 1247 | * the subnode with a simple assignment |
| 1248 | */ |
| 1249 | sub_->gen_code(FALSE, FALSE); |
| 1250 | |
| 1251 | /* decrement the value at top of stack */ |
| 1252 | G_cg->js_expr("--($1)"); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* ------------------------------------------------------------------------ */ |
| 1257 | /* |
| 1258 | * post-increment |
| 1259 | */ |
| 1260 | void CTPNPostInc::gen_code(int discard, int) |
| 1261 | { |
| 1262 | /* ask the subnode to generate it */ |
| 1263 | if (!sub_->gen_code_asi(discard, TC_ASI_POSTINC, 0, FALSE)) |
| 1264 | { |
| 1265 | /* |
| 1266 | * the subnode didn't handle it - generate code to evaluate the |
| 1267 | * subnode, increment that value, then assign the result back to |
| 1268 | * the subnode with a simple assignment |
| 1269 | */ |
| 1270 | sub_->gen_code(FALSE, FALSE); |
| 1271 | |
| 1272 | /* increment the value at top of stack */ |
| 1273 | G_cg->js_expr("($1)++"); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /* ------------------------------------------------------------------------ */ |
| 1278 | /* |
| 1279 | * post-decrement |
| 1280 | */ |
| 1281 | void CTPNPostDec::gen_code(int discard, int) |
| 1282 | { |
| 1283 | /* ask the subnode to generate it */ |
| 1284 | if (!sub_->gen_code_asi(discard, TC_ASI_POSTDEC, 0, FALSE)) |
| 1285 | { |
| 1286 | /* |
| 1287 | * the subnode didn't handle it - generate code to evaluate the |
| 1288 | * subnode, decrement that value, then assign the result back to |
| 1289 | * the subnode with a simple assignment |
| 1290 | */ |
| 1291 | sub_->gen_code(FALSE, FALSE); |
| 1292 | |
| 1293 | /* decrement the value at top of stack */ |
| 1294 | G_cg->js_expr("($1)--"); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | /* ------------------------------------------------------------------------ */ |
| 1299 | /* |
| 1300 | * operator 'new' |
| 1301 | */ |
| 1302 | void CTPNNew::gen_code(int discard, int /*for condition*/) |
| 1303 | { |
| 1304 | /* |
| 1305 | * ask my subexpression to generate the code - at this point we |
| 1306 | * don't know the number of arguments, so pass in zero for now |
| 1307 | */ |
| 1308 | sub_->gen_code_new(discard, 0, FALSE, FALSE, transient_); |
| 1309 | } |
| 1310 | |
| 1311 | /* ------------------------------------------------------------------------ */ |
| 1312 | /* |
| 1313 | * operator 'delete' |
| 1314 | */ |
| 1315 | void CTPNDelete::gen_code(int, int) |
| 1316 | { |
| 1317 | /* 'delete' generates no code for T3 VM */ |
| 1318 | } |
| 1319 | |
| 1320 | /* ------------------------------------------------------------------------ */ |
| 1321 | /* |
| 1322 | * comma operator |
| 1323 | */ |
| 1324 | void CTPNComma::gen_code(int discard, int for_condition) |
| 1325 | { |
| 1326 | /* |
| 1327 | * Generate each side's code. Note that the left side is *always* |
| 1328 | * discarded, regardless of whether the result of the comma operator |
| 1329 | * will be discarded. After we generate our subexpressions, there's |
| 1330 | * nothing left to do, since the comma operator itself doesn't |
| 1331 | * change anything - we simply use the right operand result as our |
| 1332 | * result. |
| 1333 | * |
| 1334 | * Pass through the 'for_condition' status to the right operand, |
| 1335 | * since we pass through its result to the caller. For the left |
| 1336 | * operand, treat it as a condition - we don't care about the result |
| 1337 | * value, so don't bother performing any extra conversions on it. |
| 1338 | */ |
| 1339 | left_->gen_code(TRUE, TRUE); |
| 1340 | right_->gen_code(discard, for_condition); |
| 1341 | |
| 1342 | /* write the js comma expression */ |
| 1343 | G_cg->js_expr("($1),($2)"); |
| 1344 | } |
| 1345 | |
| 1346 | /* ------------------------------------------------------------------------ */ |
| 1347 | /* |
| 1348 | * logical OR (short-circuit logic) |
| 1349 | */ |
| 1350 | void CTPNOr::gen_code(int discard, int for_condition) |
| 1351 | { |
| 1352 | /* generate the left and right subexpressions */ |
| 1353 | left_->gen_code(FALSE, TRUE); |
| 1354 | right_->gen_code(discard, TRUE); |
| 1355 | |
| 1356 | /* generate the OR */ |
| 1357 | G_cg->js_expr("($1)||($2)"); |
| 1358 | |
| 1359 | /* |
| 1360 | * if the result is not going to be used directly for a condition, |
| 1361 | * we must boolean-ize the value |
| 1362 | */ |
| 1363 | if (!for_condition) |
| 1364 | G_cg->js_expr("T3_boolize($1)"); |
| 1365 | } |
| 1366 | |
| 1367 | /* ------------------------------------------------------------------------ */ |
| 1368 | /* |
| 1369 | * logical AND (short-circuit logic) |
| 1370 | */ |
| 1371 | void CTPNAnd::gen_code(int discard, int for_condition) |
| 1372 | { |
| 1373 | /* generate the left and right subexpressions */ |
| 1374 | left_->gen_code(FALSE, TRUE); |
| 1375 | right_->gen_code(discard, TRUE); |
| 1376 | |
| 1377 | /* generate the AND */ |
| 1378 | G_cg->js_expr("($1)&&($2)"); |
| 1379 | |
| 1380 | /* |
| 1381 | * if the result is not going to be used directly for a condition, |
| 1382 | * we must boolean-ize the value |
| 1383 | */ |
| 1384 | if (!for_condition) |
| 1385 | G_cg->js_expr("T3_boolize($1)"); |
| 1386 | } |
| 1387 | |
| 1388 | /* ------------------------------------------------------------------------ */ |
| 1389 | /* |
| 1390 | * bitwise OR |
| 1391 | */ |
| 1392 | void CTPNBOr::gen_code(int discard, int) |
| 1393 | { |
| 1394 | gen_binary(OPC_BOR, discard, FALSE); |
| 1395 | } |
| 1396 | |
| 1397 | /* ------------------------------------------------------------------------ */ |
| 1398 | /* |
| 1399 | * bitwise AND |
| 1400 | */ |
| 1401 | void CTPNBAnd::gen_code(int discard, int) |
| 1402 | { |
| 1403 | gen_binary(OPC_BAND, discard, FALSE); |
| 1404 | } |
| 1405 | |
| 1406 | /* ------------------------------------------------------------------------ */ |
| 1407 | /* |
| 1408 | * bitwise XOR |
| 1409 | */ |
| 1410 | void CTPNBXor::gen_code(int discard, int) |
| 1411 | { |
| 1412 | gen_binary(OPC_XOR, discard, FALSE); |
| 1413 | } |
| 1414 | |
| 1415 | /* ------------------------------------------------------------------------ */ |
| 1416 | /* |
| 1417 | * greater-than |
| 1418 | */ |
| 1419 | void CTPNGt::gen_code(int discard, int) |
| 1420 | { |
| 1421 | gen_binary(OPC_GT, discard, FALSE); |
| 1422 | } |
| 1423 | |
| 1424 | /* ------------------------------------------------------------------------ */ |
| 1425 | /* |
| 1426 | * greater-or-equal |
| 1427 | */ |
| 1428 | void CTPNGe::gen_code(int discard, int) |
| 1429 | { |
| 1430 | gen_binary(OPC_GE, discard, FALSE); |
| 1431 | } |
| 1432 | |
| 1433 | /* ------------------------------------------------------------------------ */ |
| 1434 | /* |
| 1435 | * less-than |
| 1436 | */ |
| 1437 | void CTPNLt::gen_code(int discard, int) |
| 1438 | { |
| 1439 | gen_binary(OPC_LT, discard, FALSE); |
| 1440 | } |
| 1441 | |
| 1442 | /* ------------------------------------------------------------------------ */ |
| 1443 | /* |
| 1444 | * less-or-equal |
| 1445 | */ |
| 1446 | void CTPNLe::gen_code(int discard, int) |
| 1447 | { |
| 1448 | gen_binary(OPC_LE, discard, FALSE); |
| 1449 | } |
| 1450 | |
| 1451 | /* ------------------------------------------------------------------------ */ |
| 1452 | /* |
| 1453 | * compare for equality |
| 1454 | */ |
| 1455 | void CTPNEq::gen_code(int discard, int) |
| 1456 | { |
| 1457 | gen_binary(OPC_EQ, discard, FALSE); |
| 1458 | } |
| 1459 | |
| 1460 | /* ------------------------------------------------------------------------ */ |
| 1461 | /* |
| 1462 | * compare for inequality |
| 1463 | */ |
| 1464 | void CTPNNe::gen_code(int discard, int) |
| 1465 | { |
| 1466 | gen_binary(OPC_NE, discard, FALSE); |
| 1467 | } |
| 1468 | |
| 1469 | /* ------------------------------------------------------------------------ */ |
| 1470 | /* |
| 1471 | * 'is in' |
| 1472 | */ |
| 1473 | void CTPNIsIn::gen_code(int discard, int) |
| 1474 | { |
| 1475 | CTPNArglist *lst; |
| 1476 | CTPNArg *arg; |
| 1477 | CTcCodeLabel *lbl_found; |
| 1478 | CTcCodeLabel *lbl_done; |
| 1479 | |
| 1480 | /* allocate our 'found' label */ |
| 1481 | lbl_found = G_cs->new_label_fwd(); |
| 1482 | |
| 1483 | /* |
| 1484 | * allocate our 'done' label - we only need to do this if we don't |
| 1485 | * have a constant true value and we're not discarding the result |
| 1486 | */ |
| 1487 | if (!const_true_ && !discard) |
| 1488 | lbl_done = G_cs->new_label_fwd(); |
| 1489 | |
| 1490 | /* generate my left-side expression */ |
| 1491 | left_->gen_code(FALSE, FALSE); |
| 1492 | |
| 1493 | /* the right side is always an argument list */ |
| 1494 | lst = (CTPNArglist *)right_; |
| 1495 | |
| 1496 | /* compare to each element in the list on the right */ |
| 1497 | for (arg = lst->get_arg_list_head() ; arg != 0 ; |
| 1498 | arg = arg->get_next_arg()) |
| 1499 | { |
| 1500 | /* |
| 1501 | * duplicate the left-side value, so we don't have to generate |
| 1502 | * it again for this comparison |
| 1503 | */ |
| 1504 | G_cg->write_op(OPC_DUP); |
| 1505 | |
| 1506 | /* generate this list element */ |
| 1507 | arg->gen_code(FALSE, FALSE); |
| 1508 | |
| 1509 | /* if they're equal, jump to the 'found' label */ |
| 1510 | G_cg->write_op(OPC_JE); |
| 1511 | G_cs->write_ofs2(lbl_found, 0); |
| 1512 | |
| 1513 | /* we pushed one more (DUP) and popped two (JE) */ |
| 1514 | G_cg->note_push(1); |
| 1515 | G_cg->note_pop(2); |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * Generate the code that comes at the end of all of tests when we |
| 1520 | * fail to find any matches - we simply discard the left-side value |
| 1521 | * from the stack, push our 'nil' value, and jump to the end label. |
| 1522 | * |
| 1523 | * If we have a constant 'true' value, there's no need to do any of |
| 1524 | * this, because we know that, even after testing all of our |
| 1525 | * non-constant values, there's a constant value that makes the |
| 1526 | * entire expression true, and we can thus just fall through to the |
| 1527 | * 'found' code. |
| 1528 | * |
| 1529 | * If we're discarding the result, there's no need to push a |
| 1530 | * separate value for the result, so we can just fall through to the |
| 1531 | * common ending code in this case. |
| 1532 | */ |
| 1533 | if (!const_true_ && !discard) |
| 1534 | { |
| 1535 | G_cg->write_op(OPC_DISC); |
| 1536 | G_cg->write_op(OPC_PUSHNIL); |
| 1537 | G_cg->write_op(OPC_JMP); |
| 1538 | G_cs->write_ofs2(lbl_done, 0); |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * Generate the 'found' code - this discards the left-side value and |
| 1543 | * pushes our 'true' result. Note that there's no reason to push |
| 1544 | * our result if we're discarding it. |
| 1545 | */ |
| 1546 | def_label_pos(lbl_found); |
| 1547 | G_cg->write_op(OPC_DISC); |
| 1548 | |
| 1549 | /* |
| 1550 | * if we're discarding the result, just note the pop of the left |
| 1551 | * value; otherwise, push our result |
| 1552 | */ |
| 1553 | if (discard) |
| 1554 | G_cg->note_pop(); |
| 1555 | else |
| 1556 | G_cg->write_op(OPC_PUSHTRUE); |
| 1557 | |
| 1558 | /* our 'done' label is here, if we needed one */ |
| 1559 | if (!const_true_ && !discard) |
| 1560 | def_label_pos(lbl_done); |
| 1561 | } |
| 1562 | |
| 1563 | /* ------------------------------------------------------------------------ */ |
| 1564 | /* |
| 1565 | * 'not in' |
| 1566 | */ |
| 1567 | void CTPNNotIn::gen_code(int discard, int) |
| 1568 | { |
| 1569 | CTPNArglist *lst; |
| 1570 | CTPNArg *arg; |
| 1571 | CTcCodeLabel *lbl_found; |
| 1572 | CTcCodeLabel *lbl_done; |
| 1573 | |
| 1574 | /* allocate our 'found' label */ |
| 1575 | lbl_found = G_cs->new_label_fwd(); |
| 1576 | |
| 1577 | /* |
| 1578 | * allocate our 'done' label - we only need to do this if we don't |
| 1579 | * have a constant false value |
| 1580 | */ |
| 1581 | if (!const_false_ && !discard) |
| 1582 | lbl_done = G_cs->new_label_fwd(); |
| 1583 | |
| 1584 | /* generate my left-side expression */ |
| 1585 | left_->gen_code(FALSE, FALSE); |
| 1586 | |
| 1587 | /* the right side is always an argument list */ |
| 1588 | lst = (CTPNArglist *)right_; |
| 1589 | |
| 1590 | /* compare to each element in the list on the right */ |
| 1591 | for (arg = lst->get_arg_list_head() ; arg != 0 ; |
| 1592 | arg = arg->get_next_arg()) |
| 1593 | { |
| 1594 | /* |
| 1595 | * duplicate the left-side value, so we don't have to generate |
| 1596 | * it again for this comparison |
| 1597 | */ |
| 1598 | G_cg->write_op(OPC_DUP); |
| 1599 | |
| 1600 | /* generate this list element */ |
| 1601 | arg->gen_code(FALSE, FALSE); |
| 1602 | |
| 1603 | /* if they're equal, jump to the 'found' label */ |
| 1604 | G_cg->write_op(OPC_JE); |
| 1605 | G_cs->write_ofs2(lbl_found, 0); |
| 1606 | |
| 1607 | /* we pushed one more (DUP) and popped two (JE) */ |
| 1608 | G_cg->note_push(1); |
| 1609 | G_cg->note_pop(2); |
| 1610 | } |
| 1611 | |
| 1612 | /* |
| 1613 | * Generate the code that comes at the end of all of tests when we |
| 1614 | * fail to find any matches - we simply discard the left-side value |
| 1615 | * from the stack, push our 'true' value, and jump to the end label. |
| 1616 | * |
| 1617 | * If we have a constant 'nil' value, however, there's no need to do |
| 1618 | * any of this, because we know that, even after testing all of our |
| 1619 | * non-constant values, there's a matching constant value that makes |
| 1620 | * the entire expression false (because 'not in' is false if we find |
| 1621 | * a match), and we can thus just fall through to the 'found' code. |
| 1622 | */ |
| 1623 | if (!const_false_ && !discard) |
| 1624 | { |
| 1625 | G_cg->write_op(OPC_DISC); |
| 1626 | G_cg->write_op(OPC_PUSHTRUE); |
| 1627 | G_cg->write_op(OPC_JMP); |
| 1628 | G_cs->write_ofs2(lbl_done, 0); |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * generate the 'found' code - this discards the left-side value and |
| 1633 | * pushes our 'nil' result (because the result of 'not in' is false |
| 1634 | * if we found the value) |
| 1635 | */ |
| 1636 | def_label_pos(lbl_found); |
| 1637 | G_cg->write_op(OPC_DISC); |
| 1638 | |
| 1639 | /* push the result, or note the pop if we're just discarding it */ |
| 1640 | if (discard) |
| 1641 | G_cg->note_pop(); |
| 1642 | else |
| 1643 | G_cg->write_op(OPC_PUSHNIL); |
| 1644 | |
| 1645 | /* our 'done' label is here, if we needed one */ |
| 1646 | if (!const_false_ && !discard) |
| 1647 | def_label_pos(lbl_done); |
| 1648 | } |
| 1649 | |
| 1650 | /* ------------------------------------------------------------------------ */ |
| 1651 | /* |
| 1652 | * bit-shift left |
| 1653 | */ |
| 1654 | void CTPNShl::gen_code(int discard, int) |
| 1655 | { |
| 1656 | gen_binary(OPC_SHL, discard, FALSE); |
| 1657 | } |
| 1658 | |
| 1659 | /* ------------------------------------------------------------------------ */ |
| 1660 | /* |
| 1661 | * bit-shift right |
| 1662 | */ |
| 1663 | void CTPNShr::gen_code(int discard, int) |
| 1664 | { |
| 1665 | gen_binary(OPC_SHR, discard, FALSE); |
| 1666 | } |
| 1667 | |
| 1668 | /* ------------------------------------------------------------------------ */ |
| 1669 | /* |
| 1670 | * multiply |
| 1671 | */ |
| 1672 | void CTPNMul::gen_code(int discard, int) |
| 1673 | { |
| 1674 | /* if either side is zero or one, we can apply special handling */ |
| 1675 | if (left_->is_const_int(0)) |
| 1676 | { |
| 1677 | /* evaluate the right for side effects and discard the result */ |
| 1678 | right_->gen_code(TRUE, TRUE); |
| 1679 | |
| 1680 | /* the result is zero */ |
| 1681 | G_cg->write_op(OPC_PUSH_0); |
| 1682 | G_cg->note_push(); |
| 1683 | |
| 1684 | /* done */ |
| 1685 | return; |
| 1686 | } |
| 1687 | else if (right_->is_const_int(0)) |
| 1688 | { |
| 1689 | /* evaluate the left for side effects and discard the result */ |
| 1690 | left_->gen_code(TRUE, TRUE); |
| 1691 | |
| 1692 | /* the result is zero */ |
| 1693 | G_cg->write_op(OPC_PUSH_0); |
| 1694 | G_cg->note_push(); |
| 1695 | |
| 1696 | /* done */ |
| 1697 | return; |
| 1698 | } |
| 1699 | else if (left_->is_const_int(1)) |
| 1700 | { |
| 1701 | /* |
| 1702 | * evaluate the right side - it's the result; note that, because |
| 1703 | * of the explicit multiplication, we must compute logical |
| 1704 | * results using assignment (not 'for condition') rules |
| 1705 | */ |
| 1706 | right_->gen_code(discard, FALSE); |
| 1707 | |
| 1708 | /* done */ |
| 1709 | return; |
| 1710 | } |
| 1711 | else if (right_->is_const_int(1)) |
| 1712 | { |
| 1713 | /* evaluate the right side - it's the result */ |
| 1714 | left_->gen_code(discard, FALSE); |
| 1715 | |
| 1716 | /* done */ |
| 1717 | return; |
| 1718 | } |
| 1719 | |
| 1720 | /* apply generic handling */ |
| 1721 | gen_binary(OPC_MUL, discard, FALSE); |
| 1722 | } |
| 1723 | |
| 1724 | /* ------------------------------------------------------------------------ */ |
| 1725 | /* |
| 1726 | * divide |
| 1727 | */ |
| 1728 | void CTPNDiv::gen_code(int discard, int for_cond) |
| 1729 | { |
| 1730 | /* if dividing by 1, we can skip the whole thing (except side effects) */ |
| 1731 | if (right_->is_const_int(1)) |
| 1732 | { |
| 1733 | /* |
| 1734 | * simply generate the left side for side effects; actually |
| 1735 | * doing the arithmetic has no effect |
| 1736 | */ |
| 1737 | left_->gen_code(discard, for_cond); |
| 1738 | return; |
| 1739 | } |
| 1740 | |
| 1741 | /* if the left side is zero, the result is always zero */ |
| 1742 | if (left_->is_const_int(0)) |
| 1743 | { |
| 1744 | /* evaluate the right for side effects, but discard the result */ |
| 1745 | right_->gen_code(TRUE, TRUE); |
| 1746 | |
| 1747 | /* the result is zero */ |
| 1748 | G_cg->write_op(OPC_PUSH_0); |
| 1749 | G_cg->note_push(); |
| 1750 | return; |
| 1751 | } |
| 1752 | |
| 1753 | /* use generic code generation */ |
| 1754 | gen_binary(OPC_DIV, discard, FALSE); |
| 1755 | } |
| 1756 | |
| 1757 | /* ------------------------------------------------------------------------ */ |
| 1758 | /* |
| 1759 | * modulo |
| 1760 | */ |
| 1761 | void CTPNMod::gen_code(int discard, int for_condition) |
| 1762 | { |
| 1763 | /* if dividing by 1, we can skip the whole thing (except side effects) */ |
| 1764 | if (right_->is_const_int(1)) |
| 1765 | { |
| 1766 | /* |
| 1767 | * simply generate the left side for side effects; actually |
| 1768 | * doing the arithmetic has no effect |
| 1769 | */ |
| 1770 | left_->gen_code(discard, for_condition); |
| 1771 | |
| 1772 | /* the result is zero */ |
| 1773 | G_cg->write_op(OPC_PUSH_0); |
| 1774 | G_cg->note_push(); |
| 1775 | return; |
| 1776 | } |
| 1777 | |
| 1778 | /* if the left side is zero, the result is always zero */ |
| 1779 | if (left_->is_const_int(0)) |
| 1780 | { |
| 1781 | /* evaluate the right for side effects, but discard the result */ |
| 1782 | right_->gen_code(TRUE, TRUE); |
| 1783 | |
| 1784 | /* the result is zero */ |
| 1785 | G_cg->write_op(OPC_PUSH_0); |
| 1786 | G_cg->note_push(); |
| 1787 | return; |
| 1788 | } |
| 1789 | |
| 1790 | /* use generic processing */ |
| 1791 | gen_binary(OPC_MOD, discard, FALSE); |
| 1792 | } |
| 1793 | |
| 1794 | /* ------------------------------------------------------------------------ */ |
| 1795 | /* |
| 1796 | * subtract |
| 1797 | */ |
| 1798 | void CTPNSub::gen_code(int discard, int for_cond) |
| 1799 | { |
| 1800 | /* check for subtracting 1, which we can accomplish more efficiently */ |
| 1801 | if (right_->is_const_int(1)) |
| 1802 | { |
| 1803 | /* |
| 1804 | * We're subtracting one - use decrement. The decrement |
| 1805 | * operator itself has no side effects, so we can pass through |
| 1806 | * the 'discard' status to the subnode. |
| 1807 | */ |
| 1808 | left_->gen_code(discard, FALSE); |
| 1809 | |
| 1810 | /* apply decrement if we're not discarding the result */ |
| 1811 | if (!discard) |
| 1812 | G_cg->write_op(OPC_DEC); |
| 1813 | } |
| 1814 | else |
| 1815 | { |
| 1816 | /* we can't do anything special - use the general-purpose code */ |
| 1817 | gen_binary(OPC_SUB, discard, FALSE); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | /* ------------------------------------------------------------------------ */ |
| 1822 | /* |
| 1823 | * add |
| 1824 | */ |
| 1825 | void CTPNAdd::gen_code(int discard, int) |
| 1826 | { |
| 1827 | /* check for adding 1, which we can accomplish more efficiently */ |
| 1828 | if (right_->is_const_int(1)) |
| 1829 | { |
| 1830 | /* |
| 1831 | * We're adding one - use increment. The increment operator |
| 1832 | * itself has no side effects, so we can pass through the |
| 1833 | * 'discard' status to the subnode. |
| 1834 | */ |
| 1835 | left_->gen_code(discard, FALSE); |
| 1836 | |
| 1837 | /* apply increment if we're not discarding the result */ |
| 1838 | if (!discard) |
| 1839 | G_cg->write_op(OPC_INC); |
| 1840 | } |
| 1841 | else |
| 1842 | { |
| 1843 | /* we can't do anything special - use the general-purpose code */ |
| 1844 | gen_binary(OPC_ADD, discard, FALSE); |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | /* ------------------------------------------------------------------------ */ |
| 1849 | /* |
| 1850 | * simple assignment |
| 1851 | */ |
| 1852 | void CTPNAsi::gen_code(int discard, int) |
| 1853 | { |
| 1854 | /* |
| 1855 | * Ask the left subnode to generate a simple assignment to the value |
| 1856 | * on the right. Simple assignments cannot be refused, so we don't |
| 1857 | * need to try to do any assignment work ourselves. |
| 1858 | */ |
| 1859 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, right_, FALSE); |
| 1860 | } |
| 1861 | |
| 1862 | /* ------------------------------------------------------------------------ */ |
| 1863 | /* |
| 1864 | * add and assign |
| 1865 | */ |
| 1866 | void CTPNAddAsi::gen_code(int discard, int) |
| 1867 | { |
| 1868 | /* |
| 1869 | * ask the left subnode to generate an add-and-assign; if it can't, |
| 1870 | * handle it generically |
| 1871 | */ |
| 1872 | if (!left_->gen_code_asi(discard, TC_ASI_ADD, right_, FALSE)) |
| 1873 | { |
| 1874 | /* |
| 1875 | * there's no special coding for this assignment type -- compute |
| 1876 | * the result generically, then assign the result as a simple |
| 1877 | * assignment, which cannot be refused |
| 1878 | */ |
| 1879 | gen_binary(OPC_ADD, FALSE, FALSE); |
| 1880 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | /* ------------------------------------------------------------------------ */ |
| 1885 | /* |
| 1886 | * subtract and assign |
| 1887 | */ |
| 1888 | void CTPNSubAsi::gen_code(int discard, int) |
| 1889 | { |
| 1890 | /* |
| 1891 | * ask the left subnode to generate a subtract-and-assign; if it |
| 1892 | * can't, handle it generically |
| 1893 | */ |
| 1894 | if (!left_->gen_code_asi(discard, TC_ASI_SUB, right_, FALSE)) |
| 1895 | { |
| 1896 | /* |
| 1897 | * there's no special coding for this assignment type -- compute |
| 1898 | * the result generically, then assign the result as a simple |
| 1899 | * assignment, which cannot be refused |
| 1900 | */ |
| 1901 | gen_binary(OPC_SUB, FALSE, FALSE); |
| 1902 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | /* ------------------------------------------------------------------------ */ |
| 1907 | /* |
| 1908 | * multiply and assign |
| 1909 | */ |
| 1910 | void CTPNMulAsi::gen_code(int discard, int) |
| 1911 | { |
| 1912 | /* |
| 1913 | * ask the left subnode to generate a multiply-and-assign; if it |
| 1914 | * can't, handle it generically |
| 1915 | */ |
| 1916 | if (!left_->gen_code_asi(discard, TC_ASI_MUL, right_, FALSE)) |
| 1917 | { |
| 1918 | /* |
| 1919 | * there's no special coding for this assignment type -- compute |
| 1920 | * the result generically, then assign the result as a simple |
| 1921 | * assignment, which cannot be refused |
| 1922 | */ |
| 1923 | gen_binary(OPC_MUL, FALSE, FALSE); |
| 1924 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | /* ------------------------------------------------------------------------ */ |
| 1929 | /* |
| 1930 | * divide and assign |
| 1931 | */ |
| 1932 | void CTPNDivAsi::gen_code(int discard, int) |
| 1933 | { |
| 1934 | /* |
| 1935 | * ask the left subnode to generate a divide-and-assign; if it |
| 1936 | * can't, handle it generically |
| 1937 | */ |
| 1938 | if (!left_->gen_code_asi(discard, TC_ASI_DIV, right_, FALSE)) |
| 1939 | { |
| 1940 | /* |
| 1941 | * there's no special coding for this assignment type -- compute |
| 1942 | * the result generically, then assign the result as a simple |
| 1943 | * assignment, which cannot be refused |
| 1944 | */ |
| 1945 | gen_binary(OPC_DIV, FALSE, FALSE); |
| 1946 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | /* ------------------------------------------------------------------------ */ |
| 1951 | /* |
| 1952 | * modulo and assign |
| 1953 | */ |
| 1954 | void CTPNModAsi::gen_code(int discard, int) |
| 1955 | { |
| 1956 | /* |
| 1957 | * ask the left subnode to generate a mod-and-assign; if it can't, |
| 1958 | * handle it generically |
| 1959 | */ |
| 1960 | if (!left_->gen_code_asi(discard, TC_ASI_MOD, right_, FALSE)) |
| 1961 | { |
| 1962 | /* |
| 1963 | * there's no special coding for this assignment type -- compute |
| 1964 | * the result generically, then assign the result as a simple |
| 1965 | * assignment, which cannot be refused |
| 1966 | */ |
| 1967 | gen_binary(OPC_MOD, FALSE, FALSE); |
| 1968 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | /* ------------------------------------------------------------------------ */ |
| 1973 | /* |
| 1974 | * bitwise-AND and assign |
| 1975 | */ |
| 1976 | void CTPNBAndAsi::gen_code(int discard, int) |
| 1977 | { |
| 1978 | /* |
| 1979 | * ask the left subnode to generate an AND-and-assign; if it can't, |
| 1980 | * handle it generically |
| 1981 | */ |
| 1982 | if (!left_->gen_code_asi(discard, TC_ASI_BAND, right_, FALSE)) |
| 1983 | { |
| 1984 | /* |
| 1985 | * there's no special coding for this assignment type -- compute |
| 1986 | * the result generically, then assign the result as a simple |
| 1987 | * assignment, which cannot be refused |
| 1988 | */ |
| 1989 | gen_binary(OPC_BAND, FALSE, FALSE); |
| 1990 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | /* ------------------------------------------------------------------------ */ |
| 1995 | /* |
| 1996 | * bitwise-OR and assign |
| 1997 | */ |
| 1998 | void CTPNBOrAsi::gen_code(int discard, int) |
| 1999 | { |
| 2000 | /* |
| 2001 | * ask the left subnode to generate an OR-and-assign; if it can't, |
| 2002 | * handle it generically |
| 2003 | */ |
| 2004 | if (!left_->gen_code_asi(discard, TC_ASI_BOR, right_, FALSE)) |
| 2005 | { |
| 2006 | /* |
| 2007 | * there's no special coding for this assignment type -- compute |
| 2008 | * the result generically, then assign the result as a simple |
| 2009 | * assignment, which cannot be refused |
| 2010 | */ |
| 2011 | gen_binary(OPC_BOR, FALSE, FALSE); |
| 2012 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | /* ------------------------------------------------------------------------ */ |
| 2017 | /* |
| 2018 | * bitwise-XOR and assign |
| 2019 | */ |
| 2020 | void CTPNBXorAsi::gen_code(int discard, int) |
| 2021 | { |
| 2022 | /* |
| 2023 | * ask the left subnode to generate an XOR-and-assign; if it can't, |
| 2024 | * handle it generically |
| 2025 | */ |
| 2026 | if (!left_->gen_code_asi(discard, TC_ASI_BXOR, right_, FALSE)) |
| 2027 | { |
| 2028 | /* |
| 2029 | * there's no special coding for this assignment type -- compute |
| 2030 | * the result generically, then assign the result as a simple |
| 2031 | * assignment, which cannot be refused |
| 2032 | */ |
| 2033 | gen_binary(OPC_XOR, FALSE, FALSE); |
| 2034 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | /* ------------------------------------------------------------------------ */ |
| 2039 | /* |
| 2040 | * bit-shift left and assign |
| 2041 | */ |
| 2042 | void CTPNShlAsi::gen_code(int discard, int) |
| 2043 | { |
| 2044 | /* |
| 2045 | * ask the left subnode to generate an shift-left-and-assign; if it |
| 2046 | * can't, handle it generically |
| 2047 | */ |
| 2048 | if (!left_->gen_code_asi(discard, TC_ASI_SHL, right_, FALSE)) |
| 2049 | { |
| 2050 | /* |
| 2051 | * there's no special coding for this assignment type -- compute |
| 2052 | * the result generically, then assign the result as a simple |
| 2053 | * assignment, which cannot be refused |
| 2054 | */ |
| 2055 | gen_binary(OPC_SHL, FALSE, FALSE); |
| 2056 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | /* ------------------------------------------------------------------------ */ |
| 2061 | /* |
| 2062 | * bit-shift right and assign |
| 2063 | */ |
| 2064 | void CTPNShrAsi::gen_code(int discard, int) |
| 2065 | { |
| 2066 | /* |
| 2067 | * ask the left subnode to generate a shift-right-and-assign; if it |
| 2068 | * can't, handle it generically |
| 2069 | */ |
| 2070 | if (!left_->gen_code_asi(discard, TC_ASI_SHR, right_, FALSE)) |
| 2071 | { |
| 2072 | /* |
| 2073 | * there's no special coding for this assignment type -- compute |
| 2074 | * the result generically, then assign the result as a simple |
| 2075 | * assignment, which cannot be refused |
| 2076 | */ |
| 2077 | gen_binary(OPC_SHR, FALSE, FALSE); |
| 2078 | left_->gen_code_asi(discard, TC_ASI_SIMPLE, 0, FALSE); |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | /* ------------------------------------------------------------------------ */ |
| 2083 | /* |
| 2084 | * subscript a list/array value |
| 2085 | */ |
| 2086 | void CTPNSubscript::gen_code(int discard, int) |
| 2087 | { |
| 2088 | gen_binary(OPC_INDEX, discard, FALSE); |
| 2089 | } |
| 2090 | |
| 2091 | /* |
| 2092 | * assign to a subscripted value |
| 2093 | */ |
| 2094 | int CTPNSubscript::gen_code_asi(int discard, tc_asitype_t typ, |
| 2095 | CTcPrsNode *rhs, int) |
| 2096 | { |
| 2097 | /* |
| 2098 | * If this isn't a simple assignment, tell the caller to emit the |
| 2099 | * generic code to compute the composite result, then call us again |
| 2100 | * for a simple assignment. We can't add any value with specialized |
| 2101 | * instructions for composite assignments, so there's no point in |
| 2102 | * dealing with those here. |
| 2103 | */ |
| 2104 | if (typ != TC_ASI_SIMPLE) |
| 2105 | return FALSE; |
| 2106 | |
| 2107 | /* |
| 2108 | * Generate the value to assign to the element - that's the right |
| 2109 | * side of the assignment operator. If rhs is null, it means the |
| 2110 | * caller has already done this. |
| 2111 | */ |
| 2112 | if (rhs != 0) |
| 2113 | rhs->gen_code(FALSE, FALSE); |
| 2114 | |
| 2115 | /* |
| 2116 | * if we're not discarding the result, duplicate the value to be |
| 2117 | * assigned, so that it's left on the stack after we're finished |
| 2118 | * (this is necessary because we'll consume one copy with the SETIND |
| 2119 | * instruction) |
| 2120 | */ |
| 2121 | if (!discard) |
| 2122 | { |
| 2123 | G_cg->write_op(OPC_DUP); |
| 2124 | G_cg->note_push(); |
| 2125 | } |
| 2126 | |
| 2127 | /* generate the value to be subscripted - that's my left-hand side */ |
| 2128 | left_->gen_code(FALSE, FALSE); |
| 2129 | |
| 2130 | /* generate the index value - that's my right-hand side */ |
| 2131 | right_->gen_code(FALSE, FALSE); |
| 2132 | |
| 2133 | /* generate the assign-to-indexed-value opcode */ |
| 2134 | G_cg->write_op(OPC_SETIND); |
| 2135 | |
| 2136 | /* setind pops three and pushes one - net of pop 2 */ |
| 2137 | G_cg->note_pop(2); |
| 2138 | |
| 2139 | /* |
| 2140 | * The top value now on the stack is the new container value. The new |
| 2141 | * container will be different from the old container in some cases |
| 2142 | * (with lists, for example, because we must create a new list object |
| 2143 | * to contain the modified list value). Therefore, if my left-hand |
| 2144 | * side is an lvalue, we must assign the new container to the left-hand |
| 2145 | * side - this makes something like "x[1] = 5" actually change the |
| 2146 | * value in "x" if "x" is a local variable. If my left-hand side isn't |
| 2147 | * an lvalue, don't bother with this step, and simply discard the new |
| 2148 | * container value. |
| 2149 | * |
| 2150 | * Regardless of whether we're keeping the result of the overall |
| 2151 | * expression, we're definitely not keeping the result of assigning the |
| 2152 | * new container - the result of the assignment is the value assigned, |
| 2153 | * not the container. Thus, discard = true in this call. |
| 2154 | * |
| 2155 | * There's a special case that's handled through the peep-hole |
| 2156 | * optimizer: if we are assigning to a local variable and indexing with |
| 2157 | * a constant integer value, we will have converted the whole operation |
| 2158 | * to a SETINDLCL1I8. That instruction takes care of assigning the |
| 2159 | * value back to the rvalue, so we don't need to generate a separate |
| 2160 | * rvalue assignment. |
| 2161 | */ |
| 2162 | if (G_cg->get_last_op() == OPC_SETINDLCL1I8) |
| 2163 | { |
| 2164 | /* |
| 2165 | * no assignment is necessary - we just need to account for the |
| 2166 | * difference in the stack arrangement with this form of the |
| 2167 | * assignment, which is that we don't leave the value on the stack |
| 2168 | */ |
| 2169 | G_cg->note_pop(); |
| 2170 | } |
| 2171 | else if (!left_->gen_code_asi(TRUE, TC_ASI_SIMPLE, 0, TRUE)) |
| 2172 | { |
| 2173 | /* no assignment is possible; discard the new container value */ |
| 2174 | G_cg->write_op(OPC_DISC); |
| 2175 | G_cg->note_pop(); |
| 2176 | } |
| 2177 | |
| 2178 | /* handled */ |
| 2179 | return TRUE; |
| 2180 | } |
| 2181 | |
| 2182 | /* ------------------------------------------------------------------------ */ |
| 2183 | /* |
| 2184 | * conditional operator |
| 2185 | */ |
| 2186 | void CTPNIf::gen_code(int discard, int for_condition) |
| 2187 | { |
| 2188 | CTcCodeLabel *lbl_else; |
| 2189 | CTcCodeLabel *lbl_end; |
| 2190 | |
| 2191 | /* |
| 2192 | * Generate the condition value - we need the value regardless of |
| 2193 | * whether the overall result is going to be used, because we need |
| 2194 | * it to determine which branch to take. Generate the subexpression |
| 2195 | * for a condition, so that we don't perform any extra unnecessary |
| 2196 | * conversions on it. |
| 2197 | */ |
| 2198 | first_->gen_code(FALSE, TRUE); |
| 2199 | |
| 2200 | /* if the condition is false, jump to the 'else' expression part */ |
| 2201 | lbl_else = gen_jump_ahead(OPC_JF); |
| 2202 | |
| 2203 | /* JF pops a value */ |
| 2204 | G_cg->note_pop(); |
| 2205 | |
| 2206 | /* |
| 2207 | * Generate the 'then' expression part. Only request a return value if |
| 2208 | * it has one AND we're not discarding it. If it doesn't return a |
| 2209 | * value, and we actually need one, we'll supply a default 'nil' value |
| 2210 | * next. This value will be our yielded value (in this branch, |
| 2211 | * anyway), so pass through the for-condition flag. |
| 2212 | */ |
| 2213 | second_->gen_code(discard || !second_->has_return_value(), for_condition); |
| 2214 | |
| 2215 | /* |
| 2216 | * If this expression has no return value, and we need the return |
| 2217 | * value, supply nil as the result. |
| 2218 | */ |
| 2219 | if (!discard && !second_->has_return_value()) |
| 2220 | { |
| 2221 | G_cg->write_op(OPC_PUSHNIL); |
| 2222 | G_cg->note_push(); |
| 2223 | } |
| 2224 | |
| 2225 | /* unconditionally jump over the 'else' part */ |
| 2226 | lbl_end = gen_jump_ahead(OPC_JMP); |
| 2227 | |
| 2228 | /* set the label for the 'else' part */ |
| 2229 | def_label_pos(lbl_else); |
| 2230 | |
| 2231 | /* |
| 2232 | * Generate the 'else' part. Only request a return value if it has one |
| 2233 | * AND we're not discarding it. Pass through 'discard' and |
| 2234 | * 'for_condition', since this result is our result. |
| 2235 | */ |
| 2236 | third_->gen_code(discard || !third_->has_return_value(), for_condition); |
| 2237 | |
| 2238 | /* |
| 2239 | * If this expression has no return value, and we need the return |
| 2240 | * value, supply nil as the result. |
| 2241 | */ |
| 2242 | if (!discard && !third_->has_return_value()) |
| 2243 | { |
| 2244 | G_cg->write_op(OPC_PUSHNIL); |
| 2245 | G_cg->note_push(); |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * Because of the jump, we only evaluate one of the two expressions |
| 2250 | * we generated, so note an extra pop for the branch we didn't take. |
| 2251 | * Note that if either one pushes a value, both will, since we'll |
| 2252 | * explicitly have pushed nil for the one that doesn't generate a |
| 2253 | * value to keep the stack balanced on both branches. |
| 2254 | * |
| 2255 | * If neither of our expressions yields a value, don't pop anything |
| 2256 | * extra, since we won't think we've pushed two values in the course |
| 2257 | * of generating the two expressions. |
| 2258 | */ |
| 2259 | if (second_->has_return_value() || third_->has_return_value()) |
| 2260 | G_cg->note_pop(); |
| 2261 | |
| 2262 | /* set the label for the end of the expression */ |
| 2263 | def_label_pos(lbl_end); |
| 2264 | } |
| 2265 | |
| 2266 | /* ------------------------------------------------------------------------ */ |
| 2267 | /* |
| 2268 | * symbol |
| 2269 | */ |
| 2270 | void CTPNSym::gen_code(int discard, int) |
| 2271 | { |
| 2272 | /* |
| 2273 | * Look up the symbol; if it's undefined, add a default property |
| 2274 | * symbol entry if possible. Then ask the symbol to generate the |
| 2275 | * code. |
| 2276 | */ |
| 2277 | G_cs->get_symtab() |
| 2278 | ->find_or_def_prop_implied(get_sym_text(), get_sym_text_len(), |
| 2279 | FALSE, G_cs->is_self_available()) |
| 2280 | ->gen_code(discard); |
| 2281 | } |
| 2282 | |
| 2283 | /* |
| 2284 | * assign to a symbol |
| 2285 | */ |
| 2286 | int CTPNSym::gen_code_asi(int discard, tc_asitype_t typ, CTcPrsNode *rhs, |
| 2287 | int ignore_errors) |
| 2288 | { |
| 2289 | /* |
| 2290 | * Look up the symbol; if it's undefined and there's a "self" object |
| 2291 | * available, define it as a property by default, since a property |
| 2292 | * is the only kind of symbol that we could possibly assign to |
| 2293 | * without having defined anywhere in the program. Once we have the |
| 2294 | * symbol, tell it to generate the code for assigning to it. |
| 2295 | */ |
| 2296 | return G_cs->get_symtab() |
| 2297 | ->find_or_def_prop_implied(get_sym_text(), get_sym_text_len(), |
| 2298 | FALSE, G_cs->is_self_available()) |
| 2299 | ->gen_code_asi(discard, typ, rhs, ignore_errors); |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * take the address of the symbol |
| 2304 | */ |
| 2305 | void CTPNSym::gen_code_addr() |
| 2306 | { |
| 2307 | /* |
| 2308 | * Look up our symbol in the symbol table, then ask the resulting |
| 2309 | * symbol to generate the appropriate code. If the symbol isn't |
| 2310 | * defined, and we have a "self" object available (i.e., we're in |
| 2311 | * method code), define the symbol by default as a property. |
| 2312 | * |
| 2313 | * Note that we look only in the global symbol table, because local |
| 2314 | * symbols have no address value. So, even if the symbol is defined |
| 2315 | * in the local table, ignore the local definition and look at the |
| 2316 | * global definition. |
| 2317 | */ |
| 2318 | G_prs->get_global_symtab() |
| 2319 | ->find_or_def_prop_explicit(get_sym_text(), get_sym_text_len(), |
| 2320 | FALSE) |
| 2321 | ->gen_code_addr(); |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | * call the symbol |
| 2326 | */ |
| 2327 | void CTPNSym::gen_code_call(int discard, int argc, int varargs) |
| 2328 | { |
| 2329 | /* |
| 2330 | * Look up our symbol in the symbol table, then ask the resulting |
| 2331 | * symbol to generate the appropriate call. The symbol is |
| 2332 | * implicitly a property (if in a method context), since that's the |
| 2333 | * only kind of undefined symbol that we could be calling. |
| 2334 | */ |
| 2335 | G_cs->get_symtab() |
| 2336 | ->find_or_def_prop_implied(get_sym_text(), get_sym_text_len(), |
| 2337 | FALSE, G_cs->is_self_available()) |
| 2338 | ->gen_code_call(discard, argc, varargs); |
| 2339 | } |
| 2340 | |
| 2341 | /* |
| 2342 | * generate code for 'new' |
| 2343 | */ |
| 2344 | void CTPNSym::gen_code_new(int discard, int argc, int varargs, |
| 2345 | int /*from_call*/, int is_transient) |
| 2346 | { |
| 2347 | /* |
| 2348 | * Look up our symbol, then ask the resulting symbol to generate the |
| 2349 | * 'new' code. If the symbol is undefined, add an 'undefined' entry |
| 2350 | * to the table; we can't implicitly create an object symbol. |
| 2351 | */ |
| 2352 | G_cs->get_symtab() |
| 2353 | ->find_or_def_undef(get_sym_text(), get_sym_text_len(), FALSE) |
| 2354 | ->gen_code_new(discard, argc, varargs, is_transient); |
| 2355 | } |
| 2356 | |
| 2357 | /* |
| 2358 | * generate a property ID expression |
| 2359 | */ |
| 2360 | vm_prop_id_t CTPNSym::gen_code_propid(int check_only, int is_expr) |
| 2361 | { |
| 2362 | CTcSymbol *sym; |
| 2363 | CTcPrsSymtab *symtab; |
| 2364 | |
| 2365 | /* |
| 2366 | * Figure out where to look for the symbol. If the symbol was given |
| 2367 | * as an expression (in other words, it was explicitly enclosed in |
| 2368 | * parentheses), look it up in the local symbol table, since it |
| 2369 | * could refer to a local. Otherwise, it must refer to a property, |
| 2370 | * so look only in the global table. |
| 2371 | * |
| 2372 | * If the symbol isn't defined already, define it as a property now. |
| 2373 | * Because the symbol is explicitly on the right side of a member |
| 2374 | * evaluation, we can define it as a property whether or not there's |
| 2375 | * a valid "self" in this context. |
| 2376 | */ |
| 2377 | if (is_expr) |
| 2378 | { |
| 2379 | /* it's an expression - look it up in the local symbol table */ |
| 2380 | symtab = G_cs->get_symtab(); |
| 2381 | } |
| 2382 | else |
| 2383 | { |
| 2384 | /* it's a simple symbol - look only in the global symbol table */ |
| 2385 | symtab = G_prs->get_global_symtab(); |
| 2386 | } |
| 2387 | |
| 2388 | /* |
| 2389 | * look it up (note that this will always return a valid symbol, |
| 2390 | * since it will create one if we can't find an existing entry) |
| 2391 | */ |
| 2392 | sym = symtab->find_or_def_prop(get_sym_text(), get_sym_text_len(), FALSE); |
| 2393 | |
| 2394 | /* ask the symbol to generate the property reference */ |
| 2395 | return sym->gen_code_propid(check_only, is_expr); |
| 2396 | } |
| 2397 | |
| 2398 | /* |
| 2399 | * generate code for a member expression |
| 2400 | */ |
| 2401 | void CTPNSym::gen_code_member(int discard, CTcPrsNode *prop_expr, |
| 2402 | int prop_is_expr, int argc, int varargs) |
| 2403 | { |
| 2404 | /* |
| 2405 | * Look up the symbol, and let it do the work. There's no |
| 2406 | * appropriate default for the symbol, so leave it undefined if we |
| 2407 | * can't find it. |
| 2408 | */ |
| 2409 | G_cs->get_symtab() |
| 2410 | ->find_or_def_undef(get_sym_text(), get_sym_text_len(), FALSE) |
| 2411 | ->gen_code_member(discard, prop_expr, prop_is_expr, argc, varargs); |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | * generate code for an object before a '.' |
| 2416 | */ |
| 2417 | vm_obj_id_t CTPNSym::gen_code_obj_predot(int *is_self) |
| 2418 | { |
| 2419 | /* |
| 2420 | * Look up the symbol, and let it do the work. There's no default |
| 2421 | * type for the symbol, so leave it undefined if we don't find it. |
| 2422 | */ |
| 2423 | return G_cs->get_symtab() |
| 2424 | ->find_or_def_undef(get_sym_text(), get_sym_text_len(), FALSE) |
| 2425 | ->gen_code_obj_predot(is_self); |
| 2426 | } |
| 2427 | |
| 2428 | /* ------------------------------------------------------------------------ */ |
| 2429 | /* |
| 2430 | * resolved symbol |
| 2431 | */ |
| 2432 | void CTPNSymResolved::gen_code(int discard, int) |
| 2433 | { |
| 2434 | /* let the symbol handle it */ |
| 2435 | sym_->gen_code(discard); |
| 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * assign to a symbol |
| 2440 | */ |
| 2441 | int CTPNSymResolved::gen_code_asi(int discard, tc_asitype_t typ, |
| 2442 | CTcPrsNode *rhs, |
| 2443 | int ignore_errors) |
| 2444 | { |
| 2445 | /* let the symbol handle it */ |
| 2446 | return sym_->gen_code_asi(discard, typ, rhs, ignore_errors); |
| 2447 | } |
| 2448 | |
| 2449 | /* |
| 2450 | * take the address of the symbol |
| 2451 | */ |
| 2452 | void CTPNSymResolved::gen_code_addr() |
| 2453 | { |
| 2454 | /* let the symbol handle it */ |
| 2455 | sym_->gen_code_addr(); |
| 2456 | } |
| 2457 | |
| 2458 | /* |
| 2459 | * call the symbol |
| 2460 | */ |
| 2461 | void CTPNSymResolved::gen_code_call(int discard, int argc, int varargs) |
| 2462 | { |
| 2463 | /* let the symbol handle it */ |
| 2464 | sym_->gen_code_call(discard, argc, varargs); |
| 2465 | } |
| 2466 | |
| 2467 | /* |
| 2468 | * generate code for 'new' |
| 2469 | */ |
| 2470 | void CTPNSymResolved::gen_code_new(int discard, int argc, int varargs, |
| 2471 | int /*from_call*/, int is_transient) |
| 2472 | { |
| 2473 | /* let the symbol handle it */ |
| 2474 | sym_->gen_code_new(discard, argc, varargs, is_transient); |
| 2475 | } |
| 2476 | |
| 2477 | /* |
| 2478 | * generate a property ID expression |
| 2479 | */ |
| 2480 | vm_prop_id_t CTPNSymResolved::gen_code_propid(int check_only, int is_expr) |
| 2481 | { |
| 2482 | /* let the symbol handle it */ |
| 2483 | return sym_->gen_code_propid(check_only, is_expr); |
| 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | * generate code for a member expression |
| 2488 | */ |
| 2489 | void CTPNSymResolved::gen_code_member(int discard, |
| 2490 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 2491 | int argc, int varargs) |
| 2492 | { |
| 2493 | /* let the symbol handle it */ |
| 2494 | sym_->gen_code_member(discard, prop_expr, prop_is_expr, argc, varargs); |
| 2495 | } |
| 2496 | |
| 2497 | /* |
| 2498 | * generate code for an object before a '.' |
| 2499 | */ |
| 2500 | vm_obj_id_t CTPNSymResolved::gen_code_obj_predot(int *is_self) |
| 2501 | { |
| 2502 | /* let the symbol handle it */ |
| 2503 | return sym_->gen_code_obj_predot(is_self); |
| 2504 | } |
| 2505 | |
| 2506 | /* ------------------------------------------------------------------------ */ |
| 2507 | /* |
| 2508 | * Debugger local variable symbol |
| 2509 | */ |
| 2510 | |
| 2511 | /* |
| 2512 | * generate code to evaluate the variable |
| 2513 | */ |
| 2514 | void CTPNSymDebugLocal::gen_code(int discard, int for_condition) |
| 2515 | { |
| 2516 | /* if we're not discarding the value, push the local */ |
| 2517 | if (!discard) |
| 2518 | { |
| 2519 | /* generate the debugger local/parameter variable instruction */ |
| 2520 | G_cg->write_op(is_param_ ? OPC_GETDBARG : OPC_GETDBLCL); |
| 2521 | G_cs->write2(var_id_); |
| 2522 | G_cs->write2(frame_idx_); |
| 2523 | |
| 2524 | /* note that we pushed the value */ |
| 2525 | G_cg->note_push(); |
| 2526 | |
| 2527 | /* if it's a context local, get the value from the context array */ |
| 2528 | if (ctx_arr_idx_ != 0) |
| 2529 | { |
| 2530 | CTPNConst::s_gen_code_int(ctx_arr_idx_); |
| 2531 | G_cg->write_op(OPC_INDEX); |
| 2532 | |
| 2533 | /* |
| 2534 | * the 'index' operation pops two values and pushes one, for a |
| 2535 | * net of one pop |
| 2536 | */ |
| 2537 | G_cg->note_pop(); |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | /* |
| 2543 | * generate code for assigning to this variable |
| 2544 | */ |
| 2545 | int CTPNSymDebugLocal::gen_code_asi(int discard, tc_asitype_t typ, |
| 2546 | CTcPrsNode *rhs, int ignore_error) |
| 2547 | { |
| 2548 | /* |
| 2549 | * if this isn't a simple assignment, use the generic combination |
| 2550 | * assignment computation |
| 2551 | */ |
| 2552 | if (typ != TC_ASI_SIMPLE) |
| 2553 | return FALSE; |
| 2554 | |
| 2555 | /* generate the value to be assigned */ |
| 2556 | if (rhs != 0) |
| 2557 | rhs->gen_code(FALSE, FALSE); |
| 2558 | |
| 2559 | /* |
| 2560 | * if we're not discarding the result, duplicate the value so we'll |
| 2561 | * have a copy after the assignment |
| 2562 | */ |
| 2563 | if (!discard) |
| 2564 | { |
| 2565 | G_cg->write_op(OPC_DUP); |
| 2566 | G_cg->note_push(); |
| 2567 | } |
| 2568 | |
| 2569 | /* check for a context property */ |
| 2570 | if (ctx_arr_idx_ == 0) |
| 2571 | { |
| 2572 | /* |
| 2573 | * generate the debug-local-set instruction - the operands are |
| 2574 | * the variable number and the stack frame index |
| 2575 | */ |
| 2576 | G_cg->write_op(is_param_ ? OPC_SETDBARG : OPC_SETDBLCL); |
| 2577 | G_cs->write2(var_id_); |
| 2578 | G_cs->write2(frame_idx_); |
| 2579 | } |
| 2580 | else |
| 2581 | { |
| 2582 | /* get the local containing our context object */ |
| 2583 | G_cg->write_op(OPC_GETDBLCL); |
| 2584 | G_cs->write2(var_id_); |
| 2585 | G_cs->write2(frame_idx_); |
| 2586 | |
| 2587 | /* set the actual variable value in the context object */ |
| 2588 | CTPNConst::s_gen_code_int(ctx_arr_idx_); |
| 2589 | G_cg->write_op(OPC_SETIND); |
| 2590 | G_cg->write_op(OPC_DISC); |
| 2591 | |
| 2592 | /* |
| 2593 | * we did three pops (SETIND), then a push (SETIND), then a pop |
| 2594 | * (DISC) - this is a net of three extra pops |
| 2595 | */ |
| 2596 | G_cg->note_pop(3); |
| 2597 | } |
| 2598 | |
| 2599 | /* the debug-local-set removes the rvalue from the stack */ |
| 2600 | G_cg->note_pop(); |
| 2601 | |
| 2602 | /* handled */ |
| 2603 | return TRUE; |
| 2604 | } |
| 2605 | |
| 2606 | /* ------------------------------------------------------------------------ */ |
| 2607 | /* |
| 2608 | * Double-quoted string. The 'discard' status is irrelevant, because we |
| 2609 | * evaluate double-quoted strings for their side effects. |
| 2610 | */ |
| 2611 | void CTPNDstr::gen_code(int discard, int) |
| 2612 | { |
| 2613 | /* if we're not discarding the value, it's an error */ |
| 2614 | if (!discard) |
| 2615 | G_tok->log_error(TCERR_DQUOTE_IN_EXPR, (int)len_, str_); |
| 2616 | |
| 2617 | /* generate the instruction to display it */ |
| 2618 | G_cg->write_op(OPC_SAY); |
| 2619 | |
| 2620 | /* add the string to the constant pool, creating a fixup here */ |
| 2621 | G_cg->add_const_str(str_, len_, G_cs, G_cs->get_ofs()); |
| 2622 | |
| 2623 | /* write a placeholder value, which will be corrected by the fixup */ |
| 2624 | G_cs->write4(0); |
| 2625 | } |
| 2626 | |
| 2627 | /* ------------------------------------------------------------------------ */ |
| 2628 | /* |
| 2629 | * Double-quoted debug string |
| 2630 | */ |
| 2631 | void CTPNDebugDstr::gen_code(int, int) |
| 2632 | { |
| 2633 | /* generate code to push the in-line string */ |
| 2634 | G_cg->write_op(OPC_PUSHSTRI); |
| 2635 | G_cs->write2(len_); |
| 2636 | G_cs->write(str_, len_); |
| 2637 | |
| 2638 | /* write code to display the value */ |
| 2639 | G_cg->write_op(OPC_SAYVAL); |
| 2640 | |
| 2641 | /* note that we pushed the string and then popped it */ |
| 2642 | G_cg->note_push(); |
| 2643 | G_cg->note_pop(); |
| 2644 | } |
| 2645 | |
| 2646 | /* ------------------------------------------------------------------------ */ |
| 2647 | /* |
| 2648 | * Double-quoted string embedding |
| 2649 | */ |
| 2650 | |
| 2651 | /* |
| 2652 | * create an embedding |
| 2653 | */ |
| 2654 | CTPNDstrEmbed::CTPNDstrEmbed(CTcPrsNode *sub) |
| 2655 | : CTPNDstrEmbedBase(sub) |
| 2656 | { |
| 2657 | } |
| 2658 | |
| 2659 | /* |
| 2660 | * Generate code for a double-quoted string embedding |
| 2661 | */ |
| 2662 | void CTPNDstrEmbed::gen_code(int, int) |
| 2663 | { |
| 2664 | int orig_depth; |
| 2665 | |
| 2666 | /* note the stack depth before generating the expression */ |
| 2667 | orig_depth = G_cg->get_sp_depth(); |
| 2668 | |
| 2669 | /* |
| 2670 | * Generate code for the embedded expression. If the expression has a |
| 2671 | * return value, generate the value so that it can be displayed in the |
| 2672 | * string; but don't request a value if it doesn't have one, as a |
| 2673 | * return value is optional in this context. This is a normal value |
| 2674 | * invocation, not a conditional, so we need any applicable normal |
| 2675 | * value conversions. |
| 2676 | */ |
| 2677 | sub_->gen_code(!sub_->has_return_value(), FALSE); |
| 2678 | |
| 2679 | /* |
| 2680 | * If the code generation left anything on the stack, generate code |
| 2681 | * to display the value via the default display function. |
| 2682 | */ |
| 2683 | if (G_cg->get_sp_depth() > orig_depth) |
| 2684 | { |
| 2685 | /* add a SAYVAL instruction */ |
| 2686 | G_cg->write_op(OPC_SAYVAL); |
| 2687 | |
| 2688 | /* SAYVAL pops the argument value */ |
| 2689 | G_cg->note_pop(); |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | /* ------------------------------------------------------------------------ */ |
| 2695 | /* |
| 2696 | * Argument list |
| 2697 | * |
| 2698 | * For fixed arguments, generate as 'argc' stack elements. |
| 2699 | * |
| 2700 | * For varargs, generate as a single stack entry containing an array with |
| 2701 | * the arguments. |
| 2702 | */ |
| 2703 | void CTPNArglist::gen_code_arglist(int *varargs) |
| 2704 | { |
| 2705 | CTPNArg *arg; |
| 2706 | int i; |
| 2707 | int *va = new int[argc_ + 1]; |
| 2708 | |
| 2709 | /* |
| 2710 | * scan the argument list for varargs - if we have any, we must |
| 2711 | * treat all of them as varargs |
| 2712 | */ |
| 2713 | for (i = argc_, *varargs = FALSE, arg = get_arg_list_head() ; |
| 2714 | arg != 0 ; arg = arg->get_next_arg(), --i) |
| 2715 | { |
| 2716 | /* note the individual varargs value */ |
| 2717 | va[i] = arg->is_varargs(); |
| 2718 | |
| 2719 | /* if this is a varargs argument, it's a varargs list overall */ |
| 2720 | *varargs |= va[i]; |
| 2721 | } |
| 2722 | |
| 2723 | /* |
| 2724 | * Push each argument in the list - start with the last element and |
| 2725 | * work backwards through the list to the first element. The parser |
| 2726 | * builds the list in reverse order, so we must merely follow the |
| 2727 | * list from head to tail. |
| 2728 | * |
| 2729 | * We need each argument value to be pushed (hence discard = false), |
| 2730 | * and we need the assignable value of each argument expression |
| 2731 | * (hence for_condition = false). |
| 2732 | */ |
| 2733 | for (i = argc_, arg = get_arg_list_head() ; arg != 0 ; |
| 2734 | arg = arg->get_next_arg(), --i) |
| 2735 | { |
| 2736 | int depth; |
| 2737 | |
| 2738 | /* note the stack depth before generating the value */ |
| 2739 | depth = G_cg->get_sp_depth(); |
| 2740 | |
| 2741 | /* generate the argument's code */ |
| 2742 | arg->gen_code(FALSE, FALSE); |
| 2743 | |
| 2744 | /* ensure that it generated something */ |
| 2745 | if (G_cg->get_sp_depth() <= depth) |
| 2746 | G_tok->log_error(TCERR_ARG_EXPR_HAS_NO_VAL, i); |
| 2747 | } |
| 2748 | |
| 2749 | /* |
| 2750 | * If this is a varargs list, build an array containing the expanded |
| 2751 | * argument values. |
| 2752 | */ |
| 2753 | if (*varargs) |
| 2754 | { |
| 2755 | /* set up an expression buffer for the result */ |
| 2756 | js_expr_buf expr; |
| 2757 | |
| 2758 | /* start with an empty array */ |
| 2759 | expr += "[]"; |
| 2760 | |
| 2761 | /* append each element in turn */ |
| 2762 | for (i = 1 ; i <= argc_ ; --i) |
| 2763 | { |
| 2764 | char buf[30]; |
| 2765 | |
| 2766 | /* |
| 2767 | * if it's a varargs element, it's an array, so use concat() to |
| 2768 | * append its members to the array under construction; |
| 2769 | * otherwise use push() to add this element individually |
| 2770 | */ |
| 2771 | sprintf(buf, va[i] ? ".concat($%d)" : ".push($%d)", i); |
| 2772 | |
| 2773 | /* add it to the expression under construction */ |
| 2774 | expr += buf; |
| 2775 | } |
| 2776 | |
| 2777 | /* replace the argument list with the array expression */ |
| 2778 | G_cg->js_expr(buf.getbuf()); |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | /* ------------------------------------------------------------------------ */ |
| 2783 | /* |
| 2784 | * argument list entry |
| 2785 | */ |
| 2786 | void CTPNArg::gen_code(int, int) |
| 2787 | { |
| 2788 | /* |
| 2789 | * Generate the argument expression. We need the value (hence |
| 2790 | * discard = false), and we need the assignable value (hence |
| 2791 | * for_condition = false). |
| 2792 | */ |
| 2793 | get_arg_expr()->gen_code(FALSE, FALSE); |
| 2794 | } |
| 2795 | |
| 2796 | /* ------------------------------------------------------------------------ */ |
| 2797 | /* |
| 2798 | * function/method call |
| 2799 | */ |
| 2800 | |
| 2801 | /* |
| 2802 | * create |
| 2803 | */ |
| 2804 | CTPNCall::CTPNCall(CTcPrsNode *func, class CTPNArglist *arglist) |
| 2805 | : CTPNCallBase(func, arglist) |
| 2806 | { |
| 2807 | } |
| 2808 | |
| 2809 | |
| 2810 | /* |
| 2811 | * generate code |
| 2812 | */ |
| 2813 | void CTPNCall::gen_code(int discard, int) |
| 2814 | { |
| 2815 | int varargs; |
| 2816 | |
| 2817 | /* push the argument list */ |
| 2818 | get_arg_list()->gen_code_arglist(&varargs); |
| 2819 | |
| 2820 | /* generate an appropriate call instruction */ |
| 2821 | get_func()->gen_code_call(discard, get_arg_list()->get_argc(), |
| 2822 | varargs); |
| 2823 | } |
| 2824 | |
| 2825 | /* |
| 2826 | * Generate code for operator 'new'. A 'new' with an argument list |
| 2827 | * looks like a function call: NEW(CALL(object-contents, ARGLIST(...))). |
| 2828 | */ |
| 2829 | void CTPNCall::gen_code_new(int discard, int argc, int varargs, |
| 2830 | int from_call, int is_transient) |
| 2831 | { |
| 2832 | /* |
| 2833 | * if this is a recursive call from another 'call' node, it's not |
| 2834 | * allowed - we'd be trying to use the result of a call as the base |
| 2835 | * class of the 'new', which is illegal |
| 2836 | */ |
| 2837 | if (from_call) |
| 2838 | { |
| 2839 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 2840 | return; |
| 2841 | } |
| 2842 | |
| 2843 | /* generate the argument list */ |
| 2844 | get_arg_list()->gen_code_arglist(&varargs); |
| 2845 | |
| 2846 | /* generate the code for the 'new' call */ |
| 2847 | get_func()->gen_code_new(discard, get_arg_list()->get_argc(), varargs, |
| 2848 | TRUE, is_transient); |
| 2849 | } |
| 2850 | |
| 2851 | |
| 2852 | /* ------------------------------------------------------------------------ */ |
| 2853 | /* |
| 2854 | * member property evaluation |
| 2855 | */ |
| 2856 | void CTPNMember::gen_code(int discard, int) |
| 2857 | { |
| 2858 | /* ask the object expression to generate the code */ |
| 2859 | get_obj_expr()->gen_code_member(discard, get_prop_expr(), prop_is_expr_, |
| 2860 | 0, FALSE); |
| 2861 | } |
| 2862 | |
| 2863 | /* |
| 2864 | * assign to member expression |
| 2865 | */ |
| 2866 | int CTPNMember::gen_code_asi(int discard, tc_asitype_t typ, CTcPrsNode *rhs, |
| 2867 | int ignore_errors) |
| 2868 | { |
| 2869 | int is_self; |
| 2870 | vm_obj_id_t obj; |
| 2871 | vm_prop_id_t prop; |
| 2872 | |
| 2873 | /* |
| 2874 | * if it's not a simple assignment, tell the caller to generate the |
| 2875 | * generic code to compute the composite value, and then call us |
| 2876 | * again for a simple assignment |
| 2877 | */ |
| 2878 | if (typ != TC_ASI_SIMPLE) |
| 2879 | return FALSE; |
| 2880 | |
| 2881 | /* generate the right-hand side, unless the caller has already done so */ |
| 2882 | if (rhs != 0) |
| 2883 | rhs->gen_code(FALSE, FALSE); |
| 2884 | |
| 2885 | /* |
| 2886 | * if the caller wants to use the assigned value, push a copy -- |
| 2887 | * we'll consume one copy in the SETPROP or related instruction, so |
| 2888 | * we'll need another copy for the caller |
| 2889 | */ |
| 2890 | if (!discard) |
| 2891 | { |
| 2892 | G_cg->write_op(OPC_DUP); |
| 2893 | G_cg->note_push(); |
| 2894 | } |
| 2895 | |
| 2896 | /* |
| 2897 | * Determine what we have on the left: we could have self, a |
| 2898 | * constant object value, or any other expression. |
| 2899 | */ |
| 2900 | obj = get_obj_expr()->gen_code_obj_predot(&is_self); |
| 2901 | |
| 2902 | /* |
| 2903 | * determine what kind of property expression we have - don't |
| 2904 | * generate any code for now, since we may need to generate some |
| 2905 | * more code ahead of the property generation |
| 2906 | */ |
| 2907 | prop = get_prop_expr()->gen_code_propid(TRUE, prop_is_expr_); |
| 2908 | |
| 2909 | /* determine what we need to do based on the operands */ |
| 2910 | if (prop == VM_INVALID_PROP) |
| 2911 | { |
| 2912 | /* |
| 2913 | * We're assigning through a property pointer -- we must |
| 2914 | * generate a PTRSETPROP instruction. |
| 2915 | * |
| 2916 | * Before we generate the property expression, we must generate |
| 2917 | * the object expression. If we got a constant object, we must |
| 2918 | * generate code to push that object value; otherwise, the code |
| 2919 | * to generate the object value is already generated. |
| 2920 | */ |
| 2921 | if (is_self) |
| 2922 | { |
| 2923 | /* self - generate code to push the "self" value */ |
| 2924 | G_cg->write_op(OPC_PUSHSELF); |
| 2925 | G_cg->note_push(); |
| 2926 | } |
| 2927 | else if (obj != VM_INVALID_OBJ) |
| 2928 | { |
| 2929 | /* constant object - generate code to push the value */ |
| 2930 | G_cg->write_op(OPC_PUSHOBJ); |
| 2931 | G_cs->write_obj_id(obj); |
| 2932 | G_cg->note_push(); |
| 2933 | } |
| 2934 | |
| 2935 | /* generate the property value expression */ |
| 2936 | get_prop_expr()->gen_code_propid(FALSE, prop_is_expr_); |
| 2937 | |
| 2938 | /* generate the PTRSETPROP instruction */ |
| 2939 | G_cg->write_op(OPC_PTRSETPROP); |
| 2940 | |
| 2941 | /* ptrsetprop removes three elements */ |
| 2942 | G_cg->note_pop(3); |
| 2943 | } |
| 2944 | else |
| 2945 | { |
| 2946 | /* |
| 2947 | * We have a constant property value, so we have several |
| 2948 | * instructions to choose from. If we're assigning to a |
| 2949 | * property of "self", use SETPROPSELF. If we're assigning to a |
| 2950 | * constant object, use OBJSETPROP. Otherwise, use the plain |
| 2951 | * SETPROP. |
| 2952 | */ |
| 2953 | if (is_self) |
| 2954 | { |
| 2955 | /* write the SETPROPSELF */ |
| 2956 | G_cg->write_op(OPC_SETPROPSELF); |
| 2957 | G_cs->write_prop_id(prop); |
| 2958 | |
| 2959 | /* setpropself removes the value */ |
| 2960 | G_cg->note_pop(); |
| 2961 | } |
| 2962 | else if (obj != VM_INVALID_OBJ) |
| 2963 | { |
| 2964 | /* write the OBJSETPROP */ |
| 2965 | G_cg->write_op(OPC_OBJSETPROP); |
| 2966 | G_cs->write_obj_id(obj); |
| 2967 | G_cs->write_prop_id(prop); |
| 2968 | |
| 2969 | /* objsetprop removes the value */ |
| 2970 | G_cg->note_pop(); |
| 2971 | } |
| 2972 | else |
| 2973 | { |
| 2974 | /* |
| 2975 | * write the normal SETPROP; we already generated the code |
| 2976 | * to push the object value, so it's where it should be |
| 2977 | */ |
| 2978 | G_cg->write_op(OPC_SETPROP); |
| 2979 | G_cs->write_prop_id(prop); |
| 2980 | |
| 2981 | /* setprop removes the value and the object */ |
| 2982 | G_cg->note_pop(2); |
| 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | /* handled */ |
| 2987 | return TRUE; |
| 2988 | } |
| 2989 | |
| 2990 | /* ------------------------------------------------------------------------ */ |
| 2991 | /* |
| 2992 | * member with argument list |
| 2993 | */ |
| 2994 | void CTPNMemArg::gen_code(int discard, int) |
| 2995 | { |
| 2996 | int varargs; |
| 2997 | |
| 2998 | /* push the argument list */ |
| 2999 | get_arg_list()->gen_code_arglist(&varargs); |
| 3000 | |
| 3001 | /* ask the object expression to generate the code */ |
| 3002 | get_obj_expr()->gen_code_member(discard, get_prop_expr(), prop_is_expr_, |
| 3003 | get_arg_list()->get_argc(), |
| 3004 | varargs); |
| 3005 | } |
| 3006 | |
| 3007 | /* ------------------------------------------------------------------------ */ |
| 3008 | /* |
| 3009 | * construct a list |
| 3010 | */ |
| 3011 | void CTPNList::gen_code(int discard, int for_condition) |
| 3012 | { |
| 3013 | CTPNListEle *ele; |
| 3014 | |
| 3015 | /* |
| 3016 | * Before we construct the list dynamically, check to see if the |
| 3017 | * list is constant. If it is, we need only built the list in the |
| 3018 | * constant pool, and push its offset. |
| 3019 | */ |
| 3020 | if (is_const()) |
| 3021 | { |
| 3022 | /* push the value only if we're not discarding it */ |
| 3023 | if (!discard) |
| 3024 | { |
| 3025 | /* write the instruction */ |
| 3026 | G_cg->write_op(OPC_PUSHLST); |
| 3027 | |
| 3028 | /* add the list to the constant pool */ |
| 3029 | G_cg->add_const_list(this, G_cs, G_cs->get_ofs()); |
| 3030 | |
| 3031 | /* |
| 3032 | * write a placeholder address, which will be corrected by |
| 3033 | * the fixup that add_const_list() created |
| 3034 | */ |
| 3035 | G_cs->write4(0); |
| 3036 | |
| 3037 | /* note the push */ |
| 3038 | G_cg->note_push(); |
| 3039 | } |
| 3040 | |
| 3041 | /* done */ |
| 3042 | return; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * It's not a constant list, so we must generate code to construct a |
| 3047 | * list dynamically. Push each element of the list. We need each |
| 3048 | * value (hence discard = false), and we require the assignable |
| 3049 | * value of each expression (hence for_condition = false). Push the |
| 3050 | * argument list in reverse order, since the run-time metaclass |
| 3051 | * requires this ordering. |
| 3052 | */ |
| 3053 | for (ele = get_tail() ; ele != 0 ; ele = ele->get_prev()) |
| 3054 | ele->gen_code(FALSE, FALSE); |
| 3055 | |
| 3056 | /* generate a NEW instruction for an object of metaclass LIST */ |
| 3057 | if (get_count() <= 255) |
| 3058 | { |
| 3059 | /* the count will fit in one byte - use the short form */ |
| 3060 | G_cg->write_op(OPC_NEW1); |
| 3061 | G_cs->write((char)get_count()); |
| 3062 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_LIST)); |
| 3063 | } |
| 3064 | else |
| 3065 | { |
| 3066 | /* count doesn't fit in one byte - use the long form */ |
| 3067 | G_cg->write_op(OPC_NEW2); |
| 3068 | G_cs->write2(get_count()); |
| 3069 | G_cs->write2(G_cg->get_predef_meta_idx(TCT3_METAID_LIST)); |
| 3070 | } |
| 3071 | |
| 3072 | /* new1/new2 remove arguments */ |
| 3073 | G_cg->note_pop(get_count()); |
| 3074 | |
| 3075 | /* if we're not discarding the value, push it */ |
| 3076 | if (!discard) |
| 3077 | { |
| 3078 | G_cg->write_op(OPC_GETR0); |
| 3079 | G_cg->note_push(); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | /* ------------------------------------------------------------------------ */ |
| 3084 | /* |
| 3085 | * list element |
| 3086 | */ |
| 3087 | void CTPNListEle::gen_code(int discard, int for_condition) |
| 3088 | { |
| 3089 | /* generate the subexpression */ |
| 3090 | expr_->gen_code(discard, for_condition); |
| 3091 | } |
| 3092 | |
| 3093 | /* ------------------------------------------------------------------------ */ |
| 3094 | /* |
| 3095 | * Basic T3-specific symbol class |
| 3096 | */ |
| 3097 | |
| 3098 | /* |
| 3099 | * generate code to take the address of a symbol - in general, we cannot |
| 3100 | * take the address of a symbol, so we'll just log an error |
| 3101 | */ |
| 3102 | void CTcSymbol::gen_code_addr() |
| 3103 | { |
| 3104 | G_tok->log_error(TCERR_NO_ADDR_SYM, (int)get_sym_len(), get_sym()); |
| 3105 | } |
| 3106 | |
| 3107 | /* |
| 3108 | * generate code to assign to the symbol - in general, we cannot assign |
| 3109 | * to a symbol, so we'll just log an error |
| 3110 | */ |
| 3111 | int CTcSymbol::gen_code_asi(int, tc_asitype_t, class CTcPrsNode *, |
| 3112 | int ignore_error) |
| 3113 | { |
| 3114 | /* |
| 3115 | * if we're ignoring errors, simply return false to indicate that |
| 3116 | * nothing happened |
| 3117 | */ |
| 3118 | if (ignore_error) |
| 3119 | return FALSE; |
| 3120 | |
| 3121 | /* log the error */ |
| 3122 | G_tok->log_error(TCERR_CANNOT_ASSIGN_SYM, (int)get_sym_len(), get_sym()); |
| 3123 | |
| 3124 | /* |
| 3125 | * even though we didn't generate anything, this has been fully |
| 3126 | * handled - the caller shouldn't attempt to generate any additional |
| 3127 | * code for this |
| 3128 | */ |
| 3129 | return TRUE; |
| 3130 | } |
| 3131 | |
| 3132 | /* |
| 3133 | * Generate code for calling the symbol. By default, we can't call a |
| 3134 | * symbol. |
| 3135 | */ |
| 3136 | void CTcSymbol::gen_code_call(int, int, int) |
| 3137 | { |
| 3138 | /* log an error */ |
| 3139 | G_tok->log_error(TCERR_CANNOT_CALL_SYM, (int)get_sym_len(), get_sym()); |
| 3140 | } |
| 3141 | |
| 3142 | /* |
| 3143 | * Generate code for operator 'new' |
| 3144 | */ |
| 3145 | void CTcSymbol::gen_code_new(int, int, int, int) |
| 3146 | { |
| 3147 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 3148 | } |
| 3149 | |
| 3150 | /* |
| 3151 | * evaluate a property ID |
| 3152 | */ |
| 3153 | vm_prop_id_t CTcSymbol::gen_code_propid(int check_only, int is_expr) |
| 3154 | { |
| 3155 | /* by default, a symbol cannot be used as a property ID */ |
| 3156 | if (!check_only) |
| 3157 | G_tok->log_error(TCERR_SYM_NOT_PROP, (int)get_sym_len(), get_sym()); |
| 3158 | |
| 3159 | /* we can't return a valid property ID */ |
| 3160 | return VM_INVALID_PROP; |
| 3161 | } |
| 3162 | |
| 3163 | /* |
| 3164 | * evaluate a member expression |
| 3165 | */ |
| 3166 | void CTcSymbol::gen_code_member(int discard, |
| 3167 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3168 | int argc, int varargs) |
| 3169 | { |
| 3170 | /* by default, a symbol cannot be used as an object expression */ |
| 3171 | G_tok->log_error(TCERR_SYM_NOT_OBJ, (int)get_sym_len(), get_sym()); |
| 3172 | } |
| 3173 | |
| 3174 | /* |
| 3175 | * generate code for an object expression before a '.' |
| 3176 | */ |
| 3177 | vm_obj_id_t CTcSymbol::gen_code_obj_predot(int *is_self) |
| 3178 | { |
| 3179 | /* by default, a symbol cannot be used as an object expression */ |
| 3180 | G_tok->log_error(TCERR_SYM_NOT_OBJ, (int)get_sym_len(), get_sym()); |
| 3181 | |
| 3182 | /* indicate that we don't have a constant object */ |
| 3183 | *is_self = FALSE; |
| 3184 | return VM_INVALID_OBJ; |
| 3185 | } |
| 3186 | |
| 3187 | |
| 3188 | |
| 3189 | /* ------------------------------------------------------------------------ */ |
| 3190 | /* |
| 3191 | * T3-specific function symbol class |
| 3192 | */ |
| 3193 | |
| 3194 | /* |
| 3195 | * evaluate the symbol |
| 3196 | */ |
| 3197 | void CTcSymFunc::gen_code(int discard) |
| 3198 | { |
| 3199 | /* |
| 3200 | * function address are always unknown during code generation; |
| 3201 | * generate a placeholder instruction and add a fixup record for it |
| 3202 | */ |
| 3203 | G_cg->write_op(OPC_PUSHFNPTR); |
| 3204 | |
| 3205 | /* add a fixup for the current code location */ |
| 3206 | add_abs_fixup(G_cs); |
| 3207 | |
| 3208 | /* write a placeholder offset - arbitrarily use zero */ |
| 3209 | G_cs->write4(0); |
| 3210 | |
| 3211 | /* note the push */ |
| 3212 | G_cg->note_push(); |
| 3213 | } |
| 3214 | |
| 3215 | /* |
| 3216 | * take the address of the function |
| 3217 | */ |
| 3218 | void CTcSymFunc::gen_code_addr() |
| 3219 | { |
| 3220 | /* |
| 3221 | * the address of a function cannot be taken - using the name alone |
| 3222 | * yields the address |
| 3223 | */ |
| 3224 | G_tok->log_error(TCERR_INVAL_FUNC_ADDR, (int)get_sym_len(), get_sym()); |
| 3225 | } |
| 3226 | |
| 3227 | |
| 3228 | /* |
| 3229 | * call the symbol |
| 3230 | */ |
| 3231 | void CTcSymFunc::gen_code_call(int discard, int argc, int varargs) |
| 3232 | { |
| 3233 | /* |
| 3234 | * If this is a multi-method, a call to the function is actually a call |
| 3235 | * to _multiMethodCall('name', args). |
| 3236 | */ |
| 3237 | if (is_multimethod_) |
| 3238 | { |
| 3239 | /* make a list out of the arguments */ |
| 3240 | if (varargs) |
| 3241 | { |
| 3242 | G_cg->write_op(OPC_VARARGC); |
| 3243 | G_cs->write((char)argc); |
| 3244 | } |
| 3245 | else if (argc <= 255) |
| 3246 | { |
| 3247 | G_cg->write_op(OPC_NEW1); |
| 3248 | G_cs->write((char)argc); |
| 3249 | } |
| 3250 | else |
| 3251 | { |
| 3252 | G_cg->write_op(OPC_NEW2); |
| 3253 | G_cs->write2(argc); |
| 3254 | } |
| 3255 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_LIST)); |
| 3256 | G_cg->note_pop(argc); |
| 3257 | |
| 3258 | G_cg->write_op(OPC_GETR0); |
| 3259 | G_cg->note_push(); |
| 3260 | |
| 3261 | /* add the base function pointer argument */ |
| 3262 | CTcConstVal funcval; |
| 3263 | funcval.set_funcptr(this); |
| 3264 | CTPNConst func(&funcval); |
| 3265 | func.gen_code(FALSE, FALSE); |
| 3266 | |
| 3267 | /* look up _multiMethodCall */ |
| 3268 | static const char mmc_name[] = "_multiMethodCall"; |
| 3269 | static const size_t mmc_len = sizeof(mmc_name) - 1; |
| 3270 | CTcSymFunc *mmc = (CTcSymFunc *)G_prs->get_global_symtab()->find( |
| 3271 | mmc_name, mmc_len); |
| 3272 | if (mmc == 0) |
| 3273 | { |
| 3274 | /* it's not defined - add an implied declaration for it */ |
| 3275 | mmc = new CTcSymFunc(mmc_name, mmc_len, FALSE, 1, TRUE, TRUE, |
| 3276 | FALSE, FALSE, TRUE); |
| 3277 | G_prs->get_global_symtab()->add_entry(mmc); |
| 3278 | } |
| 3279 | else if(mmc->get_type() != TC_SYM_FUNC) |
| 3280 | { |
| 3281 | /* it's defined, but not as a function - this is an error */ |
| 3282 | G_tok->log_error(TCERR_REDEF_AS_FUNC, (int)mmc_len, mmc_name); |
| 3283 | return; |
| 3284 | } |
| 3285 | |
| 3286 | /* |
| 3287 | * Generate the call. Note that there are always two arguments at |
| 3288 | * this point: the base function pointer, and the argument list. |
| 3289 | * The argument list is just one argument because we've already |
| 3290 | * constructed a list out of it. |
| 3291 | */ |
| 3292 | mmc->gen_code_call(discard, 2, FALSE); |
| 3293 | } |
| 3294 | else |
| 3295 | { |
| 3296 | /* write the varargs modifier if appropriate */ |
| 3297 | if (varargs) |
| 3298 | G_cg->write_op(OPC_VARARGC); |
| 3299 | |
| 3300 | /* generate the call instruction and argument count */ |
| 3301 | G_cg->write_op(OPC_CALL); |
| 3302 | G_cs->write((char)argc); |
| 3303 | |
| 3304 | /* check the mode */ |
| 3305 | if (G_cg->is_eval_for_debug()) |
| 3306 | { |
| 3307 | /* |
| 3308 | * debugger expression compilation - we know the absolute |
| 3309 | * address already, since all symbols are pre-resolved in the |
| 3310 | * debugger |
| 3311 | */ |
| 3312 | G_cs->write4(get_code_pool_addr()); |
| 3313 | } |
| 3314 | else |
| 3315 | { |
| 3316 | /* |
| 3317 | * Normal compilation - we won't know the function's address |
| 3318 | * until after generation is completed, so add a fixup for the |
| 3319 | * current location, then write a placeholder for the offset |
| 3320 | * field. |
| 3321 | */ |
| 3322 | add_abs_fixup(G_cs); |
| 3323 | G_cs->write4(0); |
| 3324 | } |
| 3325 | |
| 3326 | /* call removes arguments */ |
| 3327 | G_cg->note_pop(argc); |
| 3328 | |
| 3329 | /* make sure the argument count is correct */ |
| 3330 | if (varargs_ ? argc < argc_ : argc != argc_) |
| 3331 | G_tok->log_error(TCERR_WRONG_ARGC_FOR_FUNC, |
| 3332 | (int)get_sym_len(), get_sym(), argc_, argc); |
| 3333 | |
| 3334 | /* if we're not discarding, push the return value from R0 */ |
| 3335 | if (!discard) |
| 3336 | { |
| 3337 | G_cg->write_op(OPC_GETR0); |
| 3338 | G_cg->note_push(); |
| 3339 | } |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | /* |
| 3344 | * Get my code pool address. Valid only after linking. |
| 3345 | */ |
| 3346 | ulong CTcSymFunc::get_code_pool_addr() const |
| 3347 | { |
| 3348 | /* check for an absolute address */ |
| 3349 | if (abs_addr_valid_) |
| 3350 | { |
| 3351 | /* |
| 3352 | * we have an absolute address - this means the symbol was |
| 3353 | * loaded from a fully-linked image file (specifically, from the |
| 3354 | * debug records) |
| 3355 | */ |
| 3356 | return abs_addr_; |
| 3357 | } |
| 3358 | else |
| 3359 | { |
| 3360 | /* |
| 3361 | * we don't have an absolute address, so our address must have |
| 3362 | * been determined through a linking step - get the final |
| 3363 | * address from the anchor |
| 3364 | */ |
| 3365 | return anchor_->get_addr(); |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | /* |
| 3370 | * add a runtime symbol table entry |
| 3371 | */ |
| 3372 | void CTcSymFunc::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 3373 | { |
| 3374 | vm_val_t val; |
| 3375 | |
| 3376 | /* add an entry for our absolute address */ |
| 3377 | val.set_fnptr(get_code_pool_addr()); |
| 3378 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 3379 | } |
| 3380 | |
| 3381 | |
| 3382 | /* ------------------------------------------------------------------------ */ |
| 3383 | /* |
| 3384 | * T3-specific object symbol class |
| 3385 | */ |
| 3386 | |
| 3387 | /* |
| 3388 | * evaluate the symbol |
| 3389 | */ |
| 3390 | void CTcSymObj::gen_code(int discard) |
| 3391 | { |
| 3392 | /* write code to push the object ID */ |
| 3393 | if (!discard) |
| 3394 | { |
| 3395 | /* push the object */ |
| 3396 | G_cg->write_op(OPC_PUSHOBJ); |
| 3397 | G_cs->write_obj_id(obj_id_); |
| 3398 | |
| 3399 | /* note the push */ |
| 3400 | G_cg->note_push(); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | /* |
| 3405 | * take the address of the object |
| 3406 | */ |
| 3407 | void CTcSymObj::gen_code_addr() |
| 3408 | { |
| 3409 | /* act as though we were pushing the object ID directly */ |
| 3410 | gen_code(FALSE); |
| 3411 | } |
| 3412 | |
| 3413 | /* |
| 3414 | * Generate a 'new' expression |
| 3415 | */ |
| 3416 | void CTcSymObj::gen_code_new(int discard, int argc, int varargs, |
| 3417 | int is_transient) |
| 3418 | { |
| 3419 | /* use our static generator */ |
| 3420 | s_gen_code_new(discard, obj_id_, metaclass_, argc, varargs, is_transient); |
| 3421 | } |
| 3422 | |
| 3423 | /* |
| 3424 | * Generate a 'new' expression. (This is a static method so that this |
| 3425 | * code can be used by all of the possible expression types to which |
| 3426 | * 'new' can be applied.) |
| 3427 | * |
| 3428 | * This type of generation applies only to objects of metaclass TADS |
| 3429 | * Object. |
| 3430 | */ |
| 3431 | void CTcSymObj::s_gen_code_new(int discard, vm_obj_id_t obj_id, |
| 3432 | tc_metaclass_t meta, |
| 3433 | int argc, int varargs, int is_transient) |
| 3434 | { |
| 3435 | /* |
| 3436 | * push the base class object - this is always the first argument |
| 3437 | * (hence last pushed) to the metaclass constructor |
| 3438 | */ |
| 3439 | G_cg->write_op(OPC_PUSHOBJ); |
| 3440 | G_cs->write_obj_id(obj_id); |
| 3441 | |
| 3442 | /* note the push */ |
| 3443 | G_cg->note_push(); |
| 3444 | |
| 3445 | /* |
| 3446 | * note that we can only allow 126 arguments to a constructor, |
| 3447 | * because we must add the implicit superclass argument |
| 3448 | */ |
| 3449 | if (argc > 126) |
| 3450 | G_tok->log_error(TCERR_TOO_MANY_CTOR_ARGS); |
| 3451 | |
| 3452 | /* |
| 3453 | * if we have varargs, swap the top stack elements to get the |
| 3454 | * argument count back on top, and then generate the varargs |
| 3455 | * modifier opcode |
| 3456 | */ |
| 3457 | if (varargs) |
| 3458 | { |
| 3459 | /* swap the top stack elements to get argc back to the top */ |
| 3460 | G_cg->write_op(OPC_SWAP); |
| 3461 | |
| 3462 | /* |
| 3463 | * increment the argument count to account for the superclass |
| 3464 | * object argument |
| 3465 | */ |
| 3466 | G_cg->write_op(OPC_INC); |
| 3467 | |
| 3468 | /* write the varargs modifier opcode */ |
| 3469 | G_cg->write_op(OPC_VARARGC); |
| 3470 | } |
| 3471 | |
| 3472 | /* figure the metaclass index - the compiler can only generate known |
| 3473 | |
| 3474 | /* |
| 3475 | * write the NEW instruction - since we always add TADS Object to |
| 3476 | * our metaclass table before we start compiling any code, we know |
| 3477 | * it always has a small metaclass number and will always fit in the |
| 3478 | * short form of the instruction |
| 3479 | * |
| 3480 | * Note that the actual argument count we generate is one higher |
| 3481 | * than the source code argument list, because we add the implicit |
| 3482 | * first argument to the metaclass constructor |
| 3483 | */ |
| 3484 | G_cg->write_op(is_transient ? OPC_TRNEW1 : OPC_NEW1); |
| 3485 | G_cs->write((char)(argc + 1)); |
| 3486 | |
| 3487 | /* write out the dependency table index for the metaclass */ |
| 3488 | switch (meta) |
| 3489 | { |
| 3490 | case TC_META_TADSOBJ: |
| 3491 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_TADSOBJ)); |
| 3492 | break; |
| 3493 | |
| 3494 | default: |
| 3495 | /* we can't use 'new' on symbols of other metaclasses */ |
| 3496 | G_tok->log_error(TCERR_BAD_META_FOR_NEW); |
| 3497 | G_cs->write(0); |
| 3498 | break; |
| 3499 | } |
| 3500 | |
| 3501 | /* new1 removes the arguments */ |
| 3502 | G_cg->note_pop(argc + 1); |
| 3503 | |
| 3504 | /* |
| 3505 | * if they're not discarding the value, push the new object |
| 3506 | * reference, which will be in R0 when the constructor returns |
| 3507 | */ |
| 3508 | if (!discard) |
| 3509 | { |
| 3510 | G_cg->write_op(OPC_GETR0); |
| 3511 | G_cg->note_push(); |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | /* |
| 3516 | * Generate code for a member expression |
| 3517 | */ |
| 3518 | void CTcSymObj::gen_code_member(int discard, |
| 3519 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3520 | int argc, int varargs) |
| 3521 | { |
| 3522 | s_gen_code_member(discard, prop_expr, prop_is_expr, |
| 3523 | argc, obj_id_, varargs); |
| 3524 | } |
| 3525 | |
| 3526 | /* |
| 3527 | * Static method to generate code for a member expression. This is |
| 3528 | * static so that constant object nodes can share it. |
| 3529 | */ |
| 3530 | void CTcSymObj::s_gen_code_member(int discard, |
| 3531 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3532 | int argc, vm_obj_id_t obj_id, int varargs) |
| 3533 | { |
| 3534 | vm_prop_id_t prop; |
| 3535 | |
| 3536 | /* |
| 3537 | * generate the property expression - don't generate the code right |
| 3538 | * now even if code generation is necessary, because this isn't the |
| 3539 | * right place for it; for now, simply check to determine if we're |
| 3540 | * going to need to generate any code for the property expression |
| 3541 | */ |
| 3542 | prop = prop_expr->gen_code_propid(TRUE, prop_is_expr); |
| 3543 | |
| 3544 | /* don't allow method calls with arguments in speculative mode */ |
| 3545 | if (argc != 0 && G_cg->is_speculative()) |
| 3546 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3547 | |
| 3548 | /* check for a constant property value */ |
| 3549 | if (prop != VM_INVALID_PROP) |
| 3550 | { |
| 3551 | /* generate an OBJGETPROP or OBJCALLPROP as appropriate */ |
| 3552 | if (G_cg->is_speculative()) |
| 3553 | { |
| 3554 | /* speculative evaluation - use GETPROPDATA */ |
| 3555 | G_cg->write_op(OPC_PUSHOBJ); |
| 3556 | G_cs->write_obj_id(obj_id); |
| 3557 | G_cg->write_op(OPC_GETPROPDATA); |
| 3558 | G_cs->write_prop_id(prop); |
| 3559 | |
| 3560 | /* we pushed the object, then popped it */ |
| 3561 | G_cg->note_push(); |
| 3562 | G_cg->note_pop(); |
| 3563 | } |
| 3564 | else if (argc == 0) |
| 3565 | { |
| 3566 | /* no arguments - use OBJGETPROP */ |
| 3567 | G_cg->write_op(OPC_OBJGETPROP); |
| 3568 | G_cs->write_obj_id(obj_id); |
| 3569 | G_cs->write_prop_id(prop); |
| 3570 | } |
| 3571 | else |
| 3572 | { |
| 3573 | /* generate a varargs modifier if needed */ |
| 3574 | if (varargs) |
| 3575 | G_cg->write_op(OPC_VARARGC); |
| 3576 | |
| 3577 | /* arguments - use OBJCALLPROP */ |
| 3578 | G_cg->write_op(OPC_OBJCALLPROP); |
| 3579 | G_cs->write((char)argc); |
| 3580 | G_cs->write_obj_id(obj_id); |
| 3581 | G_cs->write_prop_id(prop); |
| 3582 | |
| 3583 | /* objcallprop removes arguments */ |
| 3584 | G_cg->note_pop(argc); |
| 3585 | } |
| 3586 | } |
| 3587 | else |
| 3588 | { |
| 3589 | /* |
| 3590 | * non-constant property value - we must first push the object |
| 3591 | * value, then push the property value, then write a PTRCALLPROP |
| 3592 | * instruction |
| 3593 | */ |
| 3594 | |
| 3595 | /* generate the object push */ |
| 3596 | G_cg->write_op(OPC_PUSHOBJ); |
| 3597 | G_cs->write_obj_id(obj_id); |
| 3598 | |
| 3599 | /* note the pushes */ |
| 3600 | G_cg->note_push(); |
| 3601 | |
| 3602 | /* keep the argument counter on top if necessary */ |
| 3603 | if (varargs) |
| 3604 | G_cg->write_op(OPC_SWAP); |
| 3605 | |
| 3606 | /* generate the property push */ |
| 3607 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 3608 | |
| 3609 | /* generate the PTRCALLPROP or PTRGETPROPDATA */ |
| 3610 | if (G_cg->is_speculative()) |
| 3611 | { |
| 3612 | /* speculative - use the data-only property evaluation */ |
| 3613 | G_cg->write_op(OPC_PTRGETPROPDATA); |
| 3614 | } |
| 3615 | else |
| 3616 | { |
| 3617 | /* |
| 3618 | * if we have a varargs list, modify the call instruction |
| 3619 | * that follows to make it a varargs call |
| 3620 | */ |
| 3621 | if (varargs) |
| 3622 | { |
| 3623 | /* swap to get the arg counter back on top */ |
| 3624 | G_cg->write_op(OPC_SWAP); |
| 3625 | |
| 3626 | /* write the varargs modifier */ |
| 3627 | G_cg->write_op(OPC_VARARGC); |
| 3628 | } |
| 3629 | |
| 3630 | /* normal - call the property */ |
| 3631 | G_cg->write_op(OPC_PTRCALLPROP); |
| 3632 | G_cs->write((int)argc); |
| 3633 | } |
| 3634 | |
| 3635 | /* ptrcallprop removes the arguments, the object, and the property */ |
| 3636 | G_cg->note_pop(argc + 2); |
| 3637 | } |
| 3638 | |
| 3639 | /* if they want the result, push it onto the stack */ |
| 3640 | if (!discard) |
| 3641 | { |
| 3642 | G_cg->write_op(OPC_GETR0); |
| 3643 | G_cg->note_push(); |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | /* |
| 3648 | * generate code for an object before a '.' |
| 3649 | */ |
| 3650 | vm_obj_id_t CTcSymObj::gen_code_obj_predot(int *is_self) |
| 3651 | { |
| 3652 | /* return our constant object reference */ |
| 3653 | *is_self = FALSE; |
| 3654 | return obj_id_; |
| 3655 | } |
| 3656 | |
| 3657 | /* |
| 3658 | * add a runtime symbol table entry |
| 3659 | */ |
| 3660 | void CTcSymObj::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 3661 | { |
| 3662 | vm_val_t val; |
| 3663 | |
| 3664 | /* add our entry */ |
| 3665 | val.set_obj(obj_id_); |
| 3666 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 3667 | } |
| 3668 | |
| 3669 | /* ------------------------------------------------------------------------ */ |
| 3670 | /* |
| 3671 | * T3-specific property symbol class |
| 3672 | */ |
| 3673 | |
| 3674 | /* |
| 3675 | * evaluate the symbol |
| 3676 | */ |
| 3677 | void CTcSymProp::gen_code(int discard) |
| 3678 | { |
| 3679 | /* |
| 3680 | * Evaluating a property is equivalent to calling the property on |
| 3681 | * the "self" object with no arguments. If there's no "self" |
| 3682 | * object, an unqualified property evaluation is not possible, so |
| 3683 | * log an error if this is the case. |
| 3684 | */ |
| 3685 | if (!G_cs->is_self_available()) |
| 3686 | { |
| 3687 | G_tok->log_error(TCERR_PROP_NEEDS_OBJ, (int)get_sym_len(), get_sym()); |
| 3688 | return; |
| 3689 | } |
| 3690 | |
| 3691 | if (G_cg->is_speculative()) |
| 3692 | { |
| 3693 | /* push 'self', then evaluate the property in data-only mode */ |
| 3694 | G_cg->write_op(OPC_PUSHSELF); |
| 3695 | G_cg->write_op(OPC_GETPROPDATA); |
| 3696 | G_cs->write_prop_id(prop_); |
| 3697 | |
| 3698 | /* we pushed the 'self' value then popped it again */ |
| 3699 | G_cg->note_push(); |
| 3700 | G_cg->note_pop(); |
| 3701 | } |
| 3702 | else |
| 3703 | { |
| 3704 | /* generate the call to 'self' */ |
| 3705 | G_cg->write_op(OPC_GETPROPSELF); |
| 3706 | G_cs->write_prop_id(prop_); |
| 3707 | } |
| 3708 | |
| 3709 | /* if they're not discarding the value, push the result */ |
| 3710 | if (!discard) |
| 3711 | { |
| 3712 | G_cg->write_op(OPC_GETR0); |
| 3713 | G_cg->note_push(); |
| 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | /* |
| 3718 | * evaluate a member expression |
| 3719 | */ |
| 3720 | void CTcSymProp::gen_code_member(int discard, |
| 3721 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3722 | int argc, int varargs) |
| 3723 | { |
| 3724 | /* generate code to evaluate the property */ |
| 3725 | gen_code(FALSE); |
| 3726 | |
| 3727 | /* if we have an argument counter, put it back on top */ |
| 3728 | if (varargs) |
| 3729 | G_cg->write_op(OPC_SWAP); |
| 3730 | |
| 3731 | /* use the standard member generation */ |
| 3732 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 3733 | argc, varargs); |
| 3734 | } |
| 3735 | |
| 3736 | /* |
| 3737 | * take the address of the property |
| 3738 | */ |
| 3739 | void CTcSymProp::gen_code_addr() |
| 3740 | { |
| 3741 | /* write code to push the property ID */ |
| 3742 | G_cg->write_op(OPC_PUSHPROPID); |
| 3743 | G_cs->write_prop_id(prop_); |
| 3744 | |
| 3745 | /* note the push */ |
| 3746 | G_cg->note_push(); |
| 3747 | } |
| 3748 | |
| 3749 | /* |
| 3750 | * assign to a property, implicitly of the "self" object |
| 3751 | */ |
| 3752 | int CTcSymProp::gen_code_asi(int discard, tc_asitype_t typ, |
| 3753 | class CTcPrsNode *rhs, int /*ignore_errors*/) |
| 3754 | { |
| 3755 | /* if there's no "self" object, we can't make this assignment */ |
| 3756 | if (!G_cs->is_self_available()) |
| 3757 | { |
| 3758 | /* log an error */ |
| 3759 | G_tok->log_error(TCERR_SETPROP_NEEDS_OBJ, |
| 3760 | (int)get_sym_len(), get_sym()); |
| 3761 | |
| 3762 | /* |
| 3763 | * indicate that we're finished, since there's nothing more we |
| 3764 | * can do here |
| 3765 | */ |
| 3766 | return TRUE; |
| 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | * if it's not a simple assignment, tell the caller to do the |
| 3771 | * composite work and get back to us with the value to store |
| 3772 | */ |
| 3773 | if (typ != TC_ASI_SIMPLE) |
| 3774 | return FALSE; |
| 3775 | |
| 3776 | /* |
| 3777 | * generate the right-hand side's expression for assignment, unless |
| 3778 | * the caller has already done so |
| 3779 | */ |
| 3780 | if (rhs != 0) |
| 3781 | rhs->gen_code(FALSE, FALSE); |
| 3782 | |
| 3783 | /* |
| 3784 | * if we're not discarding the value, make a copy - we'll consume a |
| 3785 | * copy in the SETPROP instruction, so we need one more copy to |
| 3786 | * return to the enclosing expression |
| 3787 | */ |
| 3788 | if (!discard) |
| 3789 | { |
| 3790 | G_cg->write_op(OPC_DUP); |
| 3791 | G_cg->note_push(); |
| 3792 | } |
| 3793 | |
| 3794 | /* |
| 3795 | * write the SETPROP instruction - use the special form to assign to |
| 3796 | * "self" |
| 3797 | */ |
| 3798 | G_cg->write_op(OPC_SETPROPSELF); |
| 3799 | G_cs->write_prop_id(prop_); |
| 3800 | |
| 3801 | /* setpropself removes the value */ |
| 3802 | G_cg->note_pop(); |
| 3803 | |
| 3804 | /* handled */ |
| 3805 | return TRUE; |
| 3806 | } |
| 3807 | |
| 3808 | /* |
| 3809 | * call the symbol |
| 3810 | */ |
| 3811 | void CTcSymProp::gen_code_call(int discard, int argc, int varargs) |
| 3812 | { |
| 3813 | /* |
| 3814 | * if there's no "self", we can't invoke a property without an |
| 3815 | * explicit object reference |
| 3816 | */ |
| 3817 | if (!G_cs->is_self_available()) |
| 3818 | { |
| 3819 | G_tok->log_error(TCERR_PROP_NEEDS_OBJ, (int)get_sym_len(), get_sym()); |
| 3820 | return; |
| 3821 | } |
| 3822 | |
| 3823 | /* don't allow calling with arguments in speculative mode */ |
| 3824 | if (argc != 0 && G_cg->is_speculative()) |
| 3825 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3826 | |
| 3827 | /* generate code to invoke the property of "self" */ |
| 3828 | if (G_cg->is_speculative()) |
| 3829 | { |
| 3830 | /* push 'self', then get the property in data-only mode */ |
| 3831 | G_cg->write_op(OPC_PUSHSELF); |
| 3832 | G_cg->write_op(OPC_GETPROPDATA); |
| 3833 | G_cs->write_prop_id(get_prop()); |
| 3834 | |
| 3835 | /* we pushed 'self' then popped it again */ |
| 3836 | G_cg->note_push(); |
| 3837 | G_cg->note_pop(); |
| 3838 | } |
| 3839 | else if (argc == 0) |
| 3840 | { |
| 3841 | /* use the instruction with no arguments */ |
| 3842 | G_cg->write_op(OPC_GETPROPSELF); |
| 3843 | G_cs->write_prop_id(get_prop()); |
| 3844 | } |
| 3845 | else |
| 3846 | { |
| 3847 | /* write the varargs modifier if appropriate */ |
| 3848 | if (varargs) |
| 3849 | G_cg->write_op(OPC_VARARGC); |
| 3850 | |
| 3851 | /* use the instruction with arguments */ |
| 3852 | G_cg->write_op(OPC_CALLPROPSELF); |
| 3853 | G_cs->write((char)argc); |
| 3854 | G_cs->write_prop_id(get_prop()); |
| 3855 | |
| 3856 | /* callpropself removes arguments */ |
| 3857 | G_cg->note_pop(argc); |
| 3858 | } |
| 3859 | |
| 3860 | /* if we're not discarding, push the return value from R0 */ |
| 3861 | if (!discard) |
| 3862 | { |
| 3863 | G_cg->write_op(OPC_GETR0); |
| 3864 | G_cg->note_push(); |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | /* |
| 3869 | * generate a property ID expression |
| 3870 | */ |
| 3871 | vm_prop_id_t CTcSymProp::gen_code_propid(int check_only, int is_expr) |
| 3872 | { |
| 3873 | /* |
| 3874 | * If I'm to be treated as an expression (which indicates that the |
| 3875 | * property symbol is explicitly enclosed in parentheses in the |
| 3876 | * original source code expression), then I must evaluate this |
| 3877 | * property of self. Otherwise, I yield literally the property ID. |
| 3878 | */ |
| 3879 | if (is_expr) |
| 3880 | { |
| 3881 | /* generate code unless we're only checking */ |
| 3882 | if (!check_only) |
| 3883 | { |
| 3884 | /* evaluate this property of self */ |
| 3885 | G_cg->write_op(OPC_GETPROPSELF); |
| 3886 | G_cs->write_prop_id(get_prop()); |
| 3887 | |
| 3888 | /* leave the result on the stack */ |
| 3889 | G_cg->write_op(OPC_GETR0); |
| 3890 | G_cg->note_push(); |
| 3891 | } |
| 3892 | |
| 3893 | /* tell the caller to use the stack value */ |
| 3894 | return VM_INVALID_PROP; |
| 3895 | } |
| 3896 | else |
| 3897 | { |
| 3898 | /* simple '.prop' - return my property ID as a constant value */ |
| 3899 | return get_prop(); |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | /* |
| 3904 | * add a runtime symbol table entry |
| 3905 | */ |
| 3906 | void CTcSymProp::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 3907 | { |
| 3908 | vm_val_t val; |
| 3909 | |
| 3910 | /* add our entry */ |
| 3911 | val.set_propid(get_prop()); |
| 3912 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 3913 | } |
| 3914 | |
| 3915 | /* ------------------------------------------------------------------------ */ |
| 3916 | /* |
| 3917 | * Enumerator symbol |
| 3918 | */ |
| 3919 | |
| 3920 | /* |
| 3921 | * evaluate the symbol |
| 3922 | */ |
| 3923 | void CTcSymEnum::gen_code(int discard) |
| 3924 | { |
| 3925 | if (!discard) |
| 3926 | { |
| 3927 | /* generate code to push the enum value */ |
| 3928 | G_cg->write_op(OPC_PUSHENUM); |
| 3929 | G_cs->write_enum_id(get_enum_id()); |
| 3930 | |
| 3931 | /* note the push */ |
| 3932 | G_cg->note_push(); |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | /* |
| 3937 | * add a runtime symbol table entry |
| 3938 | */ |
| 3939 | void CTcSymEnum::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 3940 | { |
| 3941 | vm_val_t val; |
| 3942 | |
| 3943 | /* add our entry */ |
| 3944 | val.set_enum(get_enum_id()); |
| 3945 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 3946 | } |
| 3947 | |
| 3948 | |
| 3949 | /* ------------------------------------------------------------------------ */ |
| 3950 | /* |
| 3951 | * T3-specific local variable/parameter symbol class |
| 3952 | */ |
| 3953 | |
| 3954 | /* |
| 3955 | * generate code to evaluate the symbol |
| 3956 | */ |
| 3957 | void CTcSymLocal::gen_code(int discard) |
| 3958 | { |
| 3959 | /* generate code to push the local, if we're not discarding it */ |
| 3960 | if (!discard) |
| 3961 | { |
| 3962 | /* |
| 3963 | * generate as a context local if required, otherwise as an |
| 3964 | * ordinary local variable |
| 3965 | */ |
| 3966 | if (is_ctx_local_) |
| 3967 | { |
| 3968 | /* generate the context array lookup */ |
| 3969 | if (ctx_var_num_ <= 255 && get_ctx_arr_idx() <= 255) |
| 3970 | { |
| 3971 | /* we can do this whole operation with one instruction */ |
| 3972 | G_cg->write_op(OPC_IDXLCL1INT8); |
| 3973 | G_cs->write((uchar)ctx_var_num_); |
| 3974 | G_cs->write((uchar)get_ctx_arr_idx()); |
| 3975 | |
| 3976 | /* this pushes one value */ |
| 3977 | G_cg->note_push(); |
| 3978 | } |
| 3979 | else |
| 3980 | { |
| 3981 | /* get our context array */ |
| 3982 | s_gen_code_getlcl(ctx_var_num_, FALSE); |
| 3983 | |
| 3984 | /* get our value from the context array */ |
| 3985 | CTPNConst::s_gen_code_int(get_ctx_arr_idx()); |
| 3986 | G_cg->write_op(OPC_INDEX); |
| 3987 | |
| 3988 | /* the INDEX operation removes two values and pushes one */ |
| 3989 | G_cg->note_pop(); |
| 3990 | } |
| 3991 | } |
| 3992 | else |
| 3993 | { |
| 3994 | /* generate as an ordinary local */ |
| 3995 | s_gen_code_getlcl(get_var_num(), is_param()); |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * Mark the value as referenced, whether or not we're generating the |
| 4001 | * code - the value has been logically referenced in the program |
| 4002 | * even if the result of evaluating it isn't needed. |
| 4003 | */ |
| 4004 | set_val_used(TRUE); |
| 4005 | } |
| 4006 | |
| 4007 | /* |
| 4008 | * generate code to push a local onto the stack |
| 4009 | */ |
| 4010 | void CTcSymLocal::s_gen_code_getlcl(int var_num, int is_param) |
| 4011 | { |
| 4012 | /* use the shortest form of the instruction that we can */ |
| 4013 | if (var_num <= 255) |
| 4014 | { |
| 4015 | /* 8-bit local number - use the one-byte form */ |
| 4016 | G_cg->write_op(is_param ? OPC_GETARG1 : OPC_GETLCL1); |
| 4017 | G_cs->write((char)var_num); |
| 4018 | } |
| 4019 | else |
| 4020 | { |
| 4021 | /* local number won't fit in 8 bits - use the two-byte form */ |
| 4022 | G_cg->write_op(is_param ? OPC_GETARG2 : OPC_GETLCL2); |
| 4023 | G_cs->write2(var_num); |
| 4024 | } |
| 4025 | |
| 4026 | /* note the push */ |
| 4027 | G_cg->note_push(); |
| 4028 | } |
| 4029 | |
| 4030 | /* |
| 4031 | * assign a value |
| 4032 | */ |
| 4033 | int CTcSymLocal::gen_code_asi(int discard, tc_asitype_t typ, |
| 4034 | class CTcPrsNode *rhs, int ignore_errors) |
| 4035 | { |
| 4036 | int adding; |
| 4037 | |
| 4038 | /* mark the variable as having had a value assigned to it */ |
| 4039 | set_val_assigned(TRUE); |
| 4040 | |
| 4041 | /* |
| 4042 | * if the assignment is anything but simple, this references the |
| 4043 | * value as well |
| 4044 | */ |
| 4045 | if (typ != TC_ASI_SIMPLE) |
| 4046 | set_val_used(TRUE); |
| 4047 | |
| 4048 | /* |
| 4049 | * If this is a context variable, use standard assignment (i.e., |
| 4050 | * generate the result first, then generate a simple assignment to the |
| 4051 | * variable). Otherwise, we might be able to generate a fancy |
| 4052 | * combined calculate-and-assign sequence, depending on the type of |
| 4053 | * assignment calculation we're performing. |
| 4054 | */ |
| 4055 | if (is_ctx_local_ && typ != TC_ASI_SIMPLE) |
| 4056 | { |
| 4057 | /* |
| 4058 | * it's a context local and it's not a simple assignment, so we |
| 4059 | * can't perform any special calculate-and-assign sequence - tell |
| 4060 | * the caller to calculate the full result first and then try |
| 4061 | * again using simple assignment |
| 4062 | */ |
| 4063 | return FALSE; |
| 4064 | } |
| 4065 | |
| 4066 | /* |
| 4067 | * check the type of assignment - we can optimize the code |
| 4068 | * generation to use more compact instruction sequences for certain |
| 4069 | * types of assignments |
| 4070 | */ |
| 4071 | switch(typ) |
| 4072 | { |
| 4073 | case TC_ASI_SIMPLE: |
| 4074 | /* |
| 4075 | * Simple assignment to local/parameter. Check for some special |
| 4076 | * cases: when assigning a constant value of 0, 1, or nil to a |
| 4077 | * local, we can generate a short instruction |
| 4078 | */ |
| 4079 | if (!is_param() && !is_ctx_local_ && rhs != 0 && rhs->is_const()) |
| 4080 | { |
| 4081 | CTcConstVal *cval; |
| 4082 | |
| 4083 | /* get the constant value */ |
| 4084 | cval = rhs->get_const_val(); |
| 4085 | |
| 4086 | /* check for nil and 0 or 1 values */ |
| 4087 | if (cval->get_type() == TC_CVT_NIL) |
| 4088 | { |
| 4089 | /* it's nil - generate NILLCL1 or NILLCL2 */ |
| 4090 | if (get_var_num() <= 255) |
| 4091 | { |
| 4092 | G_cg->write_op(OPC_NILLCL1); |
| 4093 | G_cs->write((char)get_var_num()); |
| 4094 | } |
| 4095 | else |
| 4096 | { |
| 4097 | G_cg->write_op(OPC_NILLCL2); |
| 4098 | G_cs->write2(get_var_num()); |
| 4099 | } |
| 4100 | |
| 4101 | /* if not discarding, leave nil on the stack */ |
| 4102 | if (!discard) |
| 4103 | { |
| 4104 | G_cg->write_op(OPC_PUSHNIL); |
| 4105 | G_cg->note_push(); |
| 4106 | } |
| 4107 | |
| 4108 | /* handled */ |
| 4109 | return TRUE; |
| 4110 | } |
| 4111 | else if (cval->get_type() == TC_CVT_INT |
| 4112 | && (cval->get_val_int() == 0 |
| 4113 | || cval->get_val_int() == 1)) |
| 4114 | { |
| 4115 | int ival; |
| 4116 | |
| 4117 | /* get the integer value */ |
| 4118 | ival = cval->get_val_int(); |
| 4119 | |
| 4120 | /* 0 or 1 - generate ZEROLCLn or ONELCLn */ |
| 4121 | if (get_var_num() <= 255) |
| 4122 | { |
| 4123 | G_cg->write_op(ival == 0 ? OPC_ZEROLCL1 : OPC_ONELCL1); |
| 4124 | G_cs->write((char)get_var_num()); |
| 4125 | } |
| 4126 | else |
| 4127 | { |
| 4128 | G_cg->write_op(ival == 0 ? OPC_ZEROLCL2 : OPC_ONELCL2); |
| 4129 | G_cs->write2(get_var_num()); |
| 4130 | } |
| 4131 | |
| 4132 | /* if not discarding, leave the value on the stack */ |
| 4133 | if (!discard) |
| 4134 | { |
| 4135 | G_cg->write_op(ival == 0 ? OPC_PUSH_0 : OPC_PUSH_1); |
| 4136 | G_cg->note_push(); |
| 4137 | } |
| 4138 | |
| 4139 | /* handled */ |
| 4140 | return TRUE; |
| 4141 | } |
| 4142 | } |
| 4143 | |
| 4144 | /* |
| 4145 | * If we got here, we can't generate a specialized constant |
| 4146 | * assignment - so, first, generate the right-hand side's value |
| 4147 | * normally. (If no 'rhs' is specified, the value is already on |
| 4148 | * the stack.) |
| 4149 | */ |
| 4150 | if (rhs != 0) |
| 4151 | rhs->gen_code(FALSE, FALSE); |
| 4152 | |
| 4153 | /* leave an extra copy of the value on the stack if not discarding */ |
| 4154 | if (!discard) |
| 4155 | { |
| 4156 | G_cg->write_op(OPC_DUP); |
| 4157 | G_cg->note_push(); |
| 4158 | } |
| 4159 | |
| 4160 | /* now assign the value at top of stack to the variable */ |
| 4161 | gen_code_setlcl(); |
| 4162 | |
| 4163 | /* handled */ |
| 4164 | return TRUE; |
| 4165 | |
| 4166 | case TC_ASI_ADD: |
| 4167 | adding = TRUE; |
| 4168 | goto add_or_sub; |
| 4169 | |
| 4170 | case TC_ASI_SUB: |
| 4171 | adding = FALSE; |
| 4172 | |
| 4173 | add_or_sub: |
| 4174 | /* if this is a parameter, there's nothing special we can do */ |
| 4175 | if (is_param()) |
| 4176 | return FALSE; |
| 4177 | |
| 4178 | /* |
| 4179 | * Add/subtract to a local/parameter. If the right-hand side is a |
| 4180 | * constant integer value, we might be able to generate a special |
| 4181 | * instruction to add/subtract it. |
| 4182 | */ |
| 4183 | if (rhs != 0 |
| 4184 | && adding |
| 4185 | && rhs->is_const() |
| 4186 | && rhs->get_const_val()->get_type() == TC_CVT_INT) |
| 4187 | { |
| 4188 | long ival; |
| 4189 | |
| 4190 | /* get the integer value to assign */ |
| 4191 | ival = rhs->get_const_val()->get_val_int(); |
| 4192 | |
| 4193 | /* |
| 4194 | * if the right-hand side's integer value fits in one byte, |
| 4195 | * generate the short (8-bit) instruction; otherwise, |
| 4196 | * generate the long (32-bit) format |
| 4197 | */ |
| 4198 | if (ival == 1) |
| 4199 | { |
| 4200 | /* adding one - increment the local */ |
| 4201 | G_cg->write_op(OPC_INCLCL); |
| 4202 | G_cs->write2(get_var_num()); |
| 4203 | } |
| 4204 | else if (ival == -1) |
| 4205 | { |
| 4206 | /* subtracting one - decrement the local */ |
| 4207 | G_cg->write_op(OPC_DECLCL); |
| 4208 | G_cs->write2(get_var_num()); |
| 4209 | } |
| 4210 | else if (ival <= 127 && ival >= -128 |
| 4211 | && get_var_num() <= 255) |
| 4212 | { |
| 4213 | /* fits in 8 bits - use the 8-bit format */ |
| 4214 | G_cg->write_op(OPC_ADDILCL1); |
| 4215 | G_cs->write((char)get_var_num()); |
| 4216 | G_cs->write((char)ival); |
| 4217 | } |
| 4218 | else |
| 4219 | { |
| 4220 | /* |
| 4221 | * either the value or the variable number doesn't fit |
| 4222 | * in 8 bits - use the 32-bit format |
| 4223 | */ |
| 4224 | G_cg->write_op(OPC_ADDILCL4); |
| 4225 | G_cs->write2(get_var_num()); |
| 4226 | G_cs->write4(ival); |
| 4227 | } |
| 4228 | } |
| 4229 | else |
| 4230 | { |
| 4231 | /* |
| 4232 | * We don't have a special instruction for the right side, |
| 4233 | * so generate it normally and add/subtract the value. (If |
| 4234 | * there's no 'rhs' value specified, it means that the value |
| 4235 | * is already on the stack, so there's nothing extra for us |
| 4236 | * to generate.) |
| 4237 | */ |
| 4238 | if (rhs != 0) |
| 4239 | rhs->gen_code(FALSE, FALSE); |
| 4240 | |
| 4241 | /* write the ADDTOLCL instruction */ |
| 4242 | G_cg->write_op(adding ? OPC_ADDTOLCL : OPC_SUBFROMLCL); |
| 4243 | G_cs->write2(get_var_num()); |
| 4244 | |
| 4245 | /* addtolcl/subfromlcl remove the rvalue */ |
| 4246 | G_cg->note_pop(); |
| 4247 | } |
| 4248 | |
| 4249 | /* |
| 4250 | * if not discarding, push the result onto the stack; do this by |
| 4251 | * simply evaluating the local, which is the simplest and most |
| 4252 | * efficient way to obtain the result of the computation |
| 4253 | */ |
| 4254 | if (!discard) |
| 4255 | gen_code(FALSE); |
| 4256 | |
| 4257 | /* handled */ |
| 4258 | return TRUE; |
| 4259 | |
| 4260 | case TC_ASI_PREINC: |
| 4261 | /* if this is a parameter, there's nothing special we can do */ |
| 4262 | if (is_param()) |
| 4263 | return FALSE; |
| 4264 | |
| 4265 | /* generate code to increment the local */ |
| 4266 | G_cg->write_op(OPC_INCLCL); |
| 4267 | G_cs->write2(get_var_num()); |
| 4268 | |
| 4269 | /* if we're not discarding, push the local's new value */ |
| 4270 | if (!discard) |
| 4271 | gen_code(FALSE); |
| 4272 | |
| 4273 | /* handled */ |
| 4274 | return TRUE; |
| 4275 | |
| 4276 | case TC_ASI_POSTINC: |
| 4277 | /* if this is a parameter, there's nothing special we can do */ |
| 4278 | if (is_param()) |
| 4279 | return FALSE; |
| 4280 | |
| 4281 | /* |
| 4282 | * if we're not discarding, push the local's value prior to |
| 4283 | * incrementing it - this will be the result we'll leave on the |
| 4284 | * stack |
| 4285 | */ |
| 4286 | if (!discard) |
| 4287 | gen_code(FALSE); |
| 4288 | |
| 4289 | /* generate code to increment the local */ |
| 4290 | G_cg->write_op(OPC_INCLCL); |
| 4291 | G_cs->write2(get_var_num()); |
| 4292 | |
| 4293 | /* handled */ |
| 4294 | return TRUE; |
| 4295 | |
| 4296 | case TC_ASI_PREDEC: |
| 4297 | /* if this is a parameter, there's nothing special we can do */ |
| 4298 | if (is_param()) |
| 4299 | return FALSE; |
| 4300 | |
| 4301 | /* generate code to decrement the local */ |
| 4302 | G_cg->write_op(OPC_DECLCL); |
| 4303 | G_cs->write2(get_var_num()); |
| 4304 | |
| 4305 | /* if we're not discarding, push the local's new value */ |
| 4306 | if (!discard) |
| 4307 | gen_code(FALSE); |
| 4308 | |
| 4309 | /* handled */ |
| 4310 | return TRUE; |
| 4311 | |
| 4312 | case TC_ASI_POSTDEC: |
| 4313 | /* if this is a parameter, there's nothing special we can do */ |
| 4314 | if (is_param()) |
| 4315 | return FALSE; |
| 4316 | |
| 4317 | /* |
| 4318 | * if we're not discarding, push the local's value prior to |
| 4319 | * decrementing it - this will be the result we'll leave on the |
| 4320 | * stack |
| 4321 | */ |
| 4322 | if (!discard) |
| 4323 | gen_code(FALSE); |
| 4324 | |
| 4325 | /* generate code to decrement the local */ |
| 4326 | G_cg->write_op(OPC_DECLCL); |
| 4327 | G_cs->write2(get_var_num()); |
| 4328 | |
| 4329 | /* handled */ |
| 4330 | return TRUE; |
| 4331 | |
| 4332 | default: |
| 4333 | /* we can't do anything special with other assignment types */ |
| 4334 | return FALSE; |
| 4335 | } |
| 4336 | } |
| 4337 | |
| 4338 | /* |
| 4339 | * generate code to assigin the value at top of stack to the local |
| 4340 | * variable |
| 4341 | */ |
| 4342 | void CTcSymLocal::gen_code_setlcl() |
| 4343 | { |
| 4344 | /* check to see if we're a context local (as opposed to a stack local) */ |
| 4345 | if (is_ctx_local_) |
| 4346 | { |
| 4347 | /* generate the assignment using the appropriate sequence */ |
| 4348 | if (ctx_var_num_ <= 255 && get_ctx_arr_idx() <= 255) |
| 4349 | { |
| 4350 | /* we can fit this in a single instruction */ |
| 4351 | G_cg->write_op(OPC_SETINDLCL1I8); |
| 4352 | G_cs->write((uchar)ctx_var_num_); |
| 4353 | G_cs->write((uchar)get_ctx_arr_idx()); |
| 4354 | |
| 4355 | /* this pops the value being assigned */ |
| 4356 | G_cg->note_pop(); |
| 4357 | } |
| 4358 | else |
| 4359 | { |
| 4360 | /* get our context array */ |
| 4361 | s_gen_code_getlcl(ctx_var_num_, FALSE); |
| 4362 | |
| 4363 | /* set our value in the context array */ |
| 4364 | CTPNConst::s_gen_code_int(get_ctx_arr_idx()); |
| 4365 | G_cg->write_op(OPC_SETIND); |
| 4366 | G_cg->write_op(OPC_DISC); |
| 4367 | |
| 4368 | /* |
| 4369 | * the SETIND pops three values and pushes one (for a net two |
| 4370 | * pops), and the DISC pops one more value, so our total is |
| 4371 | * three pops |
| 4372 | */ |
| 4373 | G_cg->note_pop(3); |
| 4374 | } |
| 4375 | } |
| 4376 | else |
| 4377 | { |
| 4378 | /* we're just a plain stack variable */ |
| 4379 | gen_code_setlcl_stk(); |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | /* |
| 4384 | * Generate code to store the value at the top of the stack into the given |
| 4385 | * local stack slot. Note that this routine will not work with a context |
| 4386 | * local - it only works if the variable is known to be a stack variable. |
| 4387 | */ |
| 4388 | void CTcSymLocal::s_gen_code_setlcl_stk(int var_num, int is_param) |
| 4389 | { |
| 4390 | /* use the shortest form that will fit our variable index */ |
| 4391 | if (var_num <= 255) |
| 4392 | { |
| 4393 | /* use the one-byte instruction */ |
| 4394 | G_cg->write_op(is_param ? OPC_SETARG1 : OPC_SETLCL1); |
| 4395 | G_cs->write((char)var_num); |
| 4396 | } |
| 4397 | else |
| 4398 | { |
| 4399 | /* big number - use the two-byte instruction */ |
| 4400 | G_cg->write_op(is_param ? OPC_SETARG2 : OPC_SETLCL2); |
| 4401 | G_cs->write2(var_num); |
| 4402 | } |
| 4403 | |
| 4404 | /* the setarg/setlcl ops remove the rvalue */ |
| 4405 | G_cg->note_pop(); |
| 4406 | } |
| 4407 | |
| 4408 | /* |
| 4409 | * call the symbol |
| 4410 | */ |
| 4411 | void CTcSymLocal::gen_code_call(int discard, int argc, int varargs) |
| 4412 | { |
| 4413 | /* |
| 4414 | * to call a local, we'll simply evaluate the local normally, then |
| 4415 | * call through the resulting (presumed) property or function |
| 4416 | * pointer value |
| 4417 | */ |
| 4418 | gen_code(FALSE); |
| 4419 | |
| 4420 | /* |
| 4421 | * if we have a varargs list, modify the call instruction that |
| 4422 | * follows to make it a varargs call |
| 4423 | */ |
| 4424 | if (varargs) |
| 4425 | { |
| 4426 | /* swap the top of the stack to get the arg counter back on top */ |
| 4427 | G_cg->write_op(OPC_SWAP); |
| 4428 | |
| 4429 | /* write the varargs modifier */ |
| 4430 | G_cg->write_op(OPC_VARARGC); |
| 4431 | } |
| 4432 | |
| 4433 | /* don't allow this at all in speculative mode */ |
| 4434 | if (G_cg->is_speculative()) |
| 4435 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 4436 | |
| 4437 | /* call the result as a function or method pointer */ |
| 4438 | G_cg->write_op(OPC_PTRCALL); |
| 4439 | G_cs->write((char)argc); |
| 4440 | |
| 4441 | /* ptrcall removes the arguments and the function pointer */ |
| 4442 | G_cg->note_pop(argc + 1); |
| 4443 | |
| 4444 | /* if we're not discarding the value, push the result */ |
| 4445 | if (!discard) |
| 4446 | { |
| 4447 | G_cg->write_op(OPC_GETR0); |
| 4448 | G_cg->note_push(); |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | /* |
| 4453 | * generate a property ID expression |
| 4454 | */ |
| 4455 | vm_prop_id_t CTcSymLocal::gen_code_propid(int check_only, int /*is_expr*/) |
| 4456 | { |
| 4457 | /* |
| 4458 | * treat the local as a property-valued expression; generate the |
| 4459 | * code for the local, then tell the caller that no constant value |
| 4460 | * is available, since the local's property ID value should be on |
| 4461 | * the stack |
| 4462 | */ |
| 4463 | if (!check_only) |
| 4464 | gen_code(FALSE); |
| 4465 | |
| 4466 | /* tell the caller to use the stack value */ |
| 4467 | return VM_INVALID_PROP; |
| 4468 | } |
| 4469 | |
| 4470 | /* |
| 4471 | * evaluate a member expression |
| 4472 | */ |
| 4473 | void CTcSymLocal::gen_code_member(int discard, |
| 4474 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 4475 | int argc, int varargs) |
| 4476 | { |
| 4477 | /* generate code to evaluate the local */ |
| 4478 | gen_code(FALSE); |
| 4479 | |
| 4480 | /* if we have an argument counter, put it back on top */ |
| 4481 | if (varargs) |
| 4482 | G_cg->write_op(OPC_SWAP); |
| 4483 | |
| 4484 | /* use the standard member generation */ |
| 4485 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 4486 | argc, varargs); |
| 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * write to a debug record |
| 4491 | */ |
| 4492 | int CTcSymLocal::write_to_debug_frame() |
| 4493 | { |
| 4494 | int flags; |
| 4495 | |
| 4496 | /* |
| 4497 | * write my ID - if we're a context variable, we want to write the |
| 4498 | * context variable ID; otherwise write our stack location as normal |
| 4499 | */ |
| 4500 | if (is_ctx_local_) |
| 4501 | G_cs->write2(ctx_var_num_); |
| 4502 | else |
| 4503 | G_cs->write2(var_num_); |
| 4504 | |
| 4505 | /* compute my flags */ |
| 4506 | flags = 0; |
| 4507 | if (is_param_) |
| 4508 | flags |= 1; |
| 4509 | if (is_ctx_local_) |
| 4510 | flags |= 2; |
| 4511 | |
| 4512 | /* write my flags */ |
| 4513 | G_cs->write2(flags); |
| 4514 | |
| 4515 | /* write my local context array index */ |
| 4516 | G_cs->write2(get_ctx_arr_idx()); |
| 4517 | |
| 4518 | /* write the length of my symbol name */ |
| 4519 | G_cs->write2(len_); |
| 4520 | G_cs->write(str_, len_); |
| 4521 | |
| 4522 | /* we did write this symbol */ |
| 4523 | return TRUE; |
| 4524 | } |
| 4525 | |
| 4526 | /* ------------------------------------------------------------------------ */ |
| 4527 | /* |
| 4528 | * Built-in function symbol |
| 4529 | */ |
| 4530 | |
| 4531 | /* |
| 4532 | * Evaluate the symbol. Invoking a built-in function without an |
| 4533 | * argument list is simply a call to the built-in function with no |
| 4534 | * arguments. |
| 4535 | */ |
| 4536 | void CTcSymBif::gen_code(int discard) |
| 4537 | { |
| 4538 | /* generate a call */ |
| 4539 | gen_code_call(discard, 0, FALSE); |
| 4540 | } |
| 4541 | |
| 4542 | /* |
| 4543 | * Generate code to call the built-in function |
| 4544 | */ |
| 4545 | void CTcSymBif::gen_code_call(int discard, int argc, int varargs) |
| 4546 | { |
| 4547 | /* don't allow calling built-in functions in speculative mode */ |
| 4548 | if (G_cg->is_speculative()) |
| 4549 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 4550 | |
| 4551 | /* check for minimum and maximum arguments */ |
| 4552 | if (argc < min_argc_) |
| 4553 | { |
| 4554 | G_tok->log_error(TCERR_TOO_FEW_FUNC_ARGS, |
| 4555 | (int)get_sym_len(), get_sym()); |
| 4556 | } |
| 4557 | else if (!varargs_ && argc > max_argc_) |
| 4558 | { |
| 4559 | G_tok->log_error(TCERR_TOO_MANY_FUNC_ARGS, |
| 4560 | (int)get_sym_len(), get_sym()); |
| 4561 | } |
| 4562 | |
| 4563 | /* write the varargs modifier if appropriate */ |
| 4564 | if (varargs) |
| 4565 | G_cg->write_op(OPC_VARARGC); |
| 4566 | |
| 4567 | /* generate the call */ |
| 4568 | if (get_func_set_id() < 4 && get_func_idx() < 256) |
| 4569 | { |
| 4570 | uchar short_ops[] = |
| 4571 | { OPC_BUILTIN_A, OPC_BUILTIN_B, OPC_BUILTIN_C, OPC_BUILTIN_D }; |
| 4572 | |
| 4573 | /* |
| 4574 | * it's one of the first 256 functions in one of the first four |
| 4575 | * function sets - we can generate a short instruction |
| 4576 | */ |
| 4577 | G_cg->write_op(short_ops[get_func_set_id()]); |
| 4578 | G_cs->write((char)argc); |
| 4579 | G_cs->write((char)get_func_idx()); |
| 4580 | } |
| 4581 | else |
| 4582 | { |
| 4583 | /* it's not in the default set - use the longer instruction */ |
| 4584 | if (get_func_idx() < 256) |
| 4585 | { |
| 4586 | /* low function index - write the short form */ |
| 4587 | G_cg->write_op(OPC_BUILTIN1); |
| 4588 | G_cs->write((char)argc); |
| 4589 | G_cs->write((char)get_func_idx()); |
| 4590 | } |
| 4591 | else |
| 4592 | { |
| 4593 | /* big function index - write the long form */ |
| 4594 | G_cg->write_op(OPC_BUILTIN2); |
| 4595 | G_cs->write((char)argc); |
| 4596 | G_cs->write2(get_func_idx()); |
| 4597 | } |
| 4598 | |
| 4599 | /* write the function set ID */ |
| 4600 | G_cs->write((char)get_func_set_id()); |
| 4601 | } |
| 4602 | |
| 4603 | /* the built-in functions always remove arguments */ |
| 4604 | G_cg->note_pop(argc); |
| 4605 | |
| 4606 | /* |
| 4607 | * if they're not discarding the value, push it - the value is |
| 4608 | * sitting in R0 after the call returns |
| 4609 | */ |
| 4610 | if (!discard) |
| 4611 | { |
| 4612 | G_cg->write_op(OPC_GETR0); |
| 4613 | G_cg->note_push(); |
| 4614 | } |
| 4615 | } |
| 4616 | |
| 4617 | |
| 4618 | /* ------------------------------------------------------------------------ */ |
| 4619 | /* |
| 4620 | * External function symbol |
| 4621 | */ |
| 4622 | |
| 4623 | /* |
| 4624 | * evaluate the symbol |
| 4625 | */ |
| 4626 | void CTcSymExtfn::gen_code(int /*discard*/) |
| 4627 | { |
| 4628 | //$$$ to be implemented |
| 4629 | assert(FALSE); |
| 4630 | } |
| 4631 | |
| 4632 | /* |
| 4633 | * generate a call to the symbol |
| 4634 | */ |
| 4635 | void CTcSymExtfn::gen_code_call(int /*discard*/, int /*argc*/, int /*varargs*/) |
| 4636 | { |
| 4637 | //$$$ to be implemented |
| 4638 | assert(FALSE); |
| 4639 | } |
| 4640 | |
| 4641 | |
| 4642 | /* ------------------------------------------------------------------------ */ |
| 4643 | /* |
| 4644 | * Code Label symbol |
| 4645 | */ |
| 4646 | |
| 4647 | /* |
| 4648 | * evaluate the symbol |
| 4649 | */ |
| 4650 | void CTcSymLabel::gen_code(int discard) |
| 4651 | { |
| 4652 | /* it's not legal to evaluate a code label; log an error */ |
| 4653 | G_tok->log_error(TCERR_CANNOT_EVAL_LABEL, |
| 4654 | (int)get_sym_len(), get_sym()); |
| 4655 | } |
| 4656 | |
| 4657 | /* ------------------------------------------------------------------------ */ |
| 4658 | /* |
| 4659 | * Metaclass symbol |
| 4660 | */ |
| 4661 | |
| 4662 | /* |
| 4663 | * generate code for evaluating the symbol |
| 4664 | */ |
| 4665 | void CTcSymMetaclass::gen_code(int discard) |
| 4666 | { |
| 4667 | /* |
| 4668 | * the metaclass name refers to the IntrinsicClass instance |
| 4669 | * associated with the metaclass |
| 4670 | */ |
| 4671 | G_cg->write_op(OPC_PUSHOBJ); |
| 4672 | G_cs->write_obj_id(class_obj_); |
| 4673 | |
| 4674 | /* note the push */ |
| 4675 | G_cg->note_push(); |
| 4676 | } |
| 4677 | |
| 4678 | /* |
| 4679 | * generate code for operator 'new' applied to the metaclass |
| 4680 | */ |
| 4681 | void CTcSymMetaclass::gen_code_new(int discard, int argc, int varargs, |
| 4682 | int is_transient) |
| 4683 | { |
| 4684 | /* if we have varargs, write the modifier */ |
| 4685 | if (varargs) |
| 4686 | G_cg->write_op(OPC_VARARGC); |
| 4687 | |
| 4688 | if (meta_idx_ <= 255 && argc <= 255) |
| 4689 | { |
| 4690 | G_cg->write_op(is_transient ? OPC_TRNEW1 : OPC_NEW1); |
| 4691 | G_cs->write((char)argc); |
| 4692 | G_cs->write((char)meta_idx_); |
| 4693 | } |
| 4694 | else |
| 4695 | { |
| 4696 | G_cg->write_op(is_transient ? OPC_TRNEW1 : OPC_NEW2); |
| 4697 | G_cs->write2(argc); |
| 4698 | G_cs->write2(meta_idx_); |
| 4699 | } |
| 4700 | |
| 4701 | /* new1/new2 remove arguments */ |
| 4702 | G_cg->note_pop(argc); |
| 4703 | |
| 4704 | /* if we're not discarding the value, push it */ |
| 4705 | if (!discard) |
| 4706 | { |
| 4707 | G_cg->write_op(OPC_GETR0); |
| 4708 | G_cg->note_push(); |
| 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | /* |
| 4713 | * generate a member expression |
| 4714 | */ |
| 4715 | void CTcSymMetaclass::gen_code_member(int discard, CTcPrsNode *prop_expr, |
| 4716 | int prop_is_expr, |
| 4717 | int argc, int varargs) |
| 4718 | { |
| 4719 | /* generate code to push our class object onto the stack */ |
| 4720 | gen_code(FALSE); |
| 4721 | |
| 4722 | /* if we have an argument counter, put it back on top */ |
| 4723 | if (varargs) |
| 4724 | G_cg->write_op(OPC_SWAP); |
| 4725 | |
| 4726 | /* use the standard member generation */ |
| 4727 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 4728 | argc, varargs); |
| 4729 | } |
| 4730 | |
| 4731 | /* |
| 4732 | * add a runtime symbol table entry |
| 4733 | */ |
| 4734 | void CTcSymMetaclass::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 4735 | { |
| 4736 | vm_val_t val; |
| 4737 | |
| 4738 | /* add our entry */ |
| 4739 | val.set_obj(get_class_obj()); |
| 4740 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 4741 | } |
| 4742 | |
| 4743 | |
| 4744 | /* ------------------------------------------------------------------------ */ |
| 4745 | /* |
| 4746 | * Exception Table |
| 4747 | */ |
| 4748 | |
| 4749 | /* |
| 4750 | * create |
| 4751 | */ |
| 4752 | CTcT3ExcTable::CTcT3ExcTable() |
| 4753 | { |
| 4754 | /* allocate an initial table */ |
| 4755 | exc_alloced_ = 1024; |
| 4756 | table_ = (CTcT3ExcEntry *)t3malloc(exc_alloced_ * sizeof(table_[0])); |
| 4757 | |
| 4758 | /* no entries are in use yet */ |
| 4759 | exc_used_ = 0; |
| 4760 | |
| 4761 | /* method offset is not yet known */ |
| 4762 | method_ofs_ = 0; |
| 4763 | } |
| 4764 | |
| 4765 | |
| 4766 | /* |
| 4767 | * add an entry to our table |
| 4768 | */ |
| 4769 | void CTcT3ExcTable::add_catch(ulong protected_start_ofs, |
| 4770 | ulong protected_end_ofs, |
| 4771 | ulong exc_obj_id, ulong catch_block_ofs) |
| 4772 | { |
| 4773 | CTcT3ExcEntry *entry; |
| 4774 | |
| 4775 | /* if necessary, expand our table */ |
| 4776 | if (exc_used_ == exc_alloced_) |
| 4777 | { |
| 4778 | /* expand the table a bit */ |
| 4779 | exc_alloced_ += 1024; |
| 4780 | |
| 4781 | /* reallocate the table at the larger size */ |
| 4782 | table_ = (CTcT3ExcEntry *) |
| 4783 | t3realloc(table_, exc_alloced_ * sizeof(table_[0])); |
| 4784 | } |
| 4785 | |
| 4786 | /* |
| 4787 | * set up the new entry - store the offsets relative to the method |
| 4788 | * header start address |
| 4789 | */ |
| 4790 | entry = table_ + exc_used_; |
| 4791 | entry->start_ofs = protected_start_ofs - method_ofs_; |
| 4792 | entry->end_ofs = protected_end_ofs - method_ofs_; |
| 4793 | entry->exc_obj_id = exc_obj_id; |
| 4794 | entry->catch_ofs = catch_block_ofs - method_ofs_; |
| 4795 | |
| 4796 | /* consume the new entry */ |
| 4797 | ++exc_used_; |
| 4798 | } |
| 4799 | |
| 4800 | /* |
| 4801 | * write our exception table to the code stream |
| 4802 | */ |
| 4803 | void CTcT3ExcTable::write_to_code_stream() |
| 4804 | { |
| 4805 | CTcT3ExcEntry *entry; |
| 4806 | size_t i; |
| 4807 | |
| 4808 | /* write the number of entries as a UINT2 */ |
| 4809 | G_cs->write2(exc_used_); |
| 4810 | |
| 4811 | /* write the entries */ |
| 4812 | for (i = 0, entry = table_ ; i < exc_used_ ; ++i, ++entry) |
| 4813 | { |
| 4814 | /* write this entry */ |
| 4815 | G_cs->write2(entry->start_ofs); |
| 4816 | G_cs->write2(entry->end_ofs); |
| 4817 | G_cs->write_obj_id(entry->exc_obj_id); |
| 4818 | G_cs->write2(entry->catch_ofs); |
| 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | |
| 4823 | /* ------------------------------------------------------------------------ */ |
| 4824 | /* |
| 4825 | * Code body |
| 4826 | */ |
| 4827 | |
| 4828 | /* |
| 4829 | * generate code |
| 4830 | */ |
| 4831 | void CTPNCodeBody::gen_code(int, int) |
| 4832 | { |
| 4833 | CTcCodeBodyCtx *cur_ctx; |
| 4834 | int ctx_idx; |
| 4835 | tct3_method_gen_ctx gen_ctx; |
| 4836 | |
| 4837 | /* if I've been replaced, don't bother generating any code */ |
| 4838 | if (replaced_) |
| 4839 | return; |
| 4840 | |
| 4841 | /* |
| 4842 | * Open the method header. |
| 4843 | * |
| 4844 | * Generate to the static stream if this is a static initializer |
| 4845 | * method, otherwise to the main stream. |
| 4846 | * |
| 4847 | * Anchor the fixups in the associated symbol table entry, if any. We |
| 4848 | * maintain our own fixup list if we don't have a symbol, otherwise we |
| 4849 | * use the one from our symbol table entry - in either case, we have to |
| 4850 | * keep track of it ourselves, because a code body might be reachable |
| 4851 | * through multiple references (a function, for example, has a global |
| 4852 | * symbol table entry - fixups referencing us might already have been |
| 4853 | * created by the time we generate our code). |
| 4854 | */ |
| 4855 | G_cg->open_method(is_static_ ? G_cs_static : G_cs_main, |
| 4856 | fixup_owner_sym_, fixup_list_anchor_, |
| 4857 | this, gototab_, |
| 4858 | argc_, varargs_, is_constructor_, self_valid_, |
| 4859 | &gen_ctx); |
| 4860 | |
| 4861 | /* |
| 4862 | * Add each local symbol table enclosing the code body's primary |
| 4863 | * local symbol table to the frame list. The outermost code body |
| 4864 | * table can be outside the primary code body table for situations |
| 4865 | * such as anonymous functions. Since these tables are outside of |
| 4866 | * any statements, we must explicitly add them to ensure that they |
| 4867 | * are assigned debugging frame ID's and are written to the debug |
| 4868 | * data. |
| 4869 | */ |
| 4870 | if (lcltab_ != 0) |
| 4871 | { |
| 4872 | CTcPrsSymtab *tab; |
| 4873 | |
| 4874 | /* add each frame outside the primary frame to the code gen list */ |
| 4875 | for (tab = lcltab_->get_parent() ; tab != 0 ; tab = tab->get_parent()) |
| 4876 | G_cs->set_local_frame(tab); |
| 4877 | } |
| 4878 | |
| 4879 | /* the method's local symbol table is now the active symbol table */ |
| 4880 | G_cs->set_local_frame(lcltab_); |
| 4881 | |
| 4882 | /* if we have a local context, initialize it */ |
| 4883 | if (has_local_ctx_) |
| 4884 | { |
| 4885 | /* write code to create the new Vector to store the context locals */ |
| 4886 | CTPNConst::s_gen_code_int(local_ctx_arr_size_); |
| 4887 | G_cg->write_op(OPC_DUP); |
| 4888 | G_cg->write_op(OPC_NEW1); |
| 4889 | G_cs->write(2); |
| 4890 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_VECTOR)); |
| 4891 | |
| 4892 | /* retrieve the object value */ |
| 4893 | G_cg->write_op(OPC_GETR0); |
| 4894 | |
| 4895 | /* |
| 4896 | * we duplicated the vector size argument, then we popped it and |
| 4897 | * pushed the object; so we have a maximum of one extra push and a |
| 4898 | * net of zero |
| 4899 | */ |
| 4900 | G_cg->note_push(); |
| 4901 | G_cg->note_pop(); |
| 4902 | |
| 4903 | /* store the new object in the context local variable */ |
| 4904 | CTcSymLocal::s_gen_code_setlcl_stk(local_ctx_var_, FALSE); |
| 4905 | |
| 4906 | /* |
| 4907 | * go through our symbol table, and copy each parameter that's |
| 4908 | * also a context local into its context local slot |
| 4909 | */ |
| 4910 | if (lcltab_ != 0) |
| 4911 | lcltab_->enum_entries(&enum_for_param_ctx, this); |
| 4912 | } |
| 4913 | |
| 4914 | /* |
| 4915 | * If we have a varargs-list parameter, generate the code to set up |
| 4916 | * the list value from the actual parameters. Note that we must do |
| 4917 | * this after we set up the local context, in case the varargs list |
| 4918 | * parameter variable is a context local, in which case it will need |
| 4919 | * to be stored in the context, in which case we need the context to |
| 4920 | * be initialized first. |
| 4921 | */ |
| 4922 | if (varargs_list_) |
| 4923 | { |
| 4924 | /* generate the PUSHPARLST instruction to create the list */ |
| 4925 | G_cg->write_op(OPC_PUSHPARLST); |
| 4926 | G_cs->write((uchar)argc_); |
| 4927 | |
| 4928 | /* |
| 4929 | * we pushed at least one value (the list); we don't know how many |
| 4930 | * others we might have pushed, but it doesn't matter because the |
| 4931 | * interpreter is responsible for checking for stack space |
| 4932 | */ |
| 4933 | G_cg->note_push(); |
| 4934 | |
| 4935 | /* store the list in our varargs parameter list local */ |
| 4936 | varargs_list_local_->gen_code_setlcl(); |
| 4937 | } |
| 4938 | |
| 4939 | /* |
| 4940 | * Generate code to initialize each enclosing-context-pointer local - |
| 4941 | * these variables allow us to find the context objects while we're |
| 4942 | * running inside this function. |
| 4943 | * |
| 4944 | * We *have to* generate context level 1 last. Context level 1 does a |
| 4945 | * set-self to re-establish the method context (if there is one), and |
| 4946 | * once we've changed to the method context 'self', we can no longer |
| 4947 | * access the anonymous function pointer context, which is in 'self' |
| 4948 | * until we change it. So, we have to wait and do level 1 last, so |
| 4949 | * that we're completely done with the anonymous function context |
| 4950 | * before we lose it. |
| 4951 | * |
| 4952 | * To ensure we generate level 1 last, make two passes: in the first |
| 4953 | * pass, generate everything except level 1; on the second pass, |
| 4954 | * generate only level 1. This two-pass approach guarantees that level |
| 4955 | * 1 will be the last one generated, regardless of where it appears in |
| 4956 | * the list. (We can't just rearrange the list - not easily, at least |
| 4957 | * - because the list is in order of function object ('self') context |
| 4958 | * slot index.) |
| 4959 | */ |
| 4960 | for (int ctx_pass = 1 ; ctx_pass <= 2 ; ++ctx_pass) |
| 4961 | { |
| 4962 | /* loop over each context entry */ |
| 4963 | for (ctx_idx = 0, cur_ctx = ctx_head_ ; cur_ctx != 0 ; |
| 4964 | cur_ctx = cur_ctx->nxt_, ++ctx_idx) |
| 4965 | { |
| 4966 | /* |
| 4967 | * Context level 1 *must* be generated last. If we're on pass |
| 4968 | * 1 and this is level 1, skip it for now; if we're on pass 2, |
| 4969 | * skip everything *except* level 1. |
| 4970 | */ |
| 4971 | if ((ctx_pass == 1 && cur_ctx->level_ == 1) |
| 4972 | || (ctx_pass == 2 && cur_ctx->level_ != 1)) |
| 4973 | continue; |
| 4974 | |
| 4975 | /* |
| 4976 | * Get this context value, stored in the function object |
| 4977 | * ('self') at index value 2+n (n=0,1,...). Note that the |
| 4978 | * context object indices start at 2 because the code pointer |
| 4979 | * for the function is at index 1. |
| 4980 | */ |
| 4981 | G_cg->write_op(OPC_PUSHSELF); |
| 4982 | CTPNConst::s_gen_code_int(ctx_idx + 2); |
| 4983 | G_cg->write_op(OPC_INDEX); |
| 4984 | |
| 4985 | /* |
| 4986 | * we pushed the object, then popped the object and index and |
| 4987 | * pushed the indexed value - this is a net of no change with |
| 4988 | * one maximum push |
| 4989 | */ |
| 4990 | G_cg->note_push(); |
| 4991 | G_cg->note_pop(); |
| 4992 | |
| 4993 | /* |
| 4994 | * If this is context level 1, and this context has a 'self', |
| 4995 | * and we need either 'self' or the full method context from |
| 4996 | * the lexically enclosing scope, generate code to load the |
| 4997 | * self or the full method context (as appropriate) from our |
| 4998 | * local context. |
| 4999 | * |
| 5000 | * The enclosing method context is always stored in the context |
| 5001 | * at level 1, because this is inherently shared context for |
| 5002 | * all enclosed lexical scopes. We thus only have to worry |
| 5003 | * about this for context level 1. |
| 5004 | */ |
| 5005 | if (cur_ctx->level_ == 1 |
| 5006 | && self_valid_ |
| 5007 | && (self_referenced_ || full_method_ctx_referenced_)) |
| 5008 | { |
| 5009 | CTPNCodeBody *outer; |
| 5010 | |
| 5011 | /* |
| 5012 | * we just put our context object on the stack in |
| 5013 | * preparation for storing it - make a duplicate copy of it |
| 5014 | * for our own purposes |
| 5015 | */ |
| 5016 | G_cg->write_op(OPC_DUP); |
| 5017 | G_cg->note_push(); |
| 5018 | |
| 5019 | /* get the saved method context from the context object */ |
| 5020 | CTPNConst::s_gen_code_int(TCPRS_LOCAL_CTX_METHODCTX); |
| 5021 | G_cg->write_op(OPC_INDEX); |
| 5022 | |
| 5023 | /* |
| 5024 | * Load the context. We must check the outermost context |
| 5025 | * to determine what it stored, because we must load |
| 5026 | * whatever it stored. |
| 5027 | */ |
| 5028 | if ((outer = get_outermost_enclosing()) != 0 |
| 5029 | && outer->local_ctx_needs_full_method_ctx()) |
| 5030 | { |
| 5031 | /* load the full method context */ |
| 5032 | G_cg->write_op(OPC_LOADCTX); |
| 5033 | } |
| 5034 | else |
| 5035 | { |
| 5036 | /* load the 'self' object */ |
| 5037 | G_cg->write_op(OPC_SETSELF); |
| 5038 | } |
| 5039 | |
| 5040 | /* |
| 5041 | * we popped two values and pushed one in the INDEX, then |
| 5042 | * popped a value in the LOADCTX or SETSELF: the net is |
| 5043 | * removal of two elements and no additional maximum depth |
| 5044 | */ |
| 5045 | G_cg->note_pop(2); |
| 5046 | } |
| 5047 | |
| 5048 | /* store the context value in the appropriate local variable */ |
| 5049 | CTcSymLocal::s_gen_code_setlcl_stk(cur_ctx->var_num_, FALSE); |
| 5050 | |
| 5051 | /* |
| 5052 | * if we just did context level 1, and this is pass 2, we're |
| 5053 | * done - pass 2's only function is to do level 1, so once we |
| 5054 | * reach it, there's nothing left to do |
| 5055 | */ |
| 5056 | if (ctx_pass == 2 && cur_ctx->level_ == 1) |
| 5057 | break; |
| 5058 | } |
| 5059 | } |
| 5060 | |
| 5061 | /* |
| 5062 | * if we created our own local context, and we have a 'self' object, |
| 5063 | * and we need access to the 'self' object or the full method context |
| 5064 | * from anonymous functions that refer to the local context, generate |
| 5065 | * code to store the appropriate data in the local context |
| 5066 | */ |
| 5067 | if (has_local_ctx_ && self_valid_ |
| 5068 | && (local_ctx_needs_self_ || local_ctx_needs_full_method_ctx_)) |
| 5069 | { |
| 5070 | /* check to see what we need */ |
| 5071 | if (local_ctx_needs_full_method_ctx_) |
| 5072 | { |
| 5073 | /* |
| 5074 | * we need the full method context - generate code to store it |
| 5075 | * and push a reference to it onto the stack |
| 5076 | */ |
| 5077 | G_cg->write_op(OPC_STORECTX); |
| 5078 | } |
| 5079 | else |
| 5080 | { |
| 5081 | /* we only need 'self' - push it */ |
| 5082 | G_cg->write_op(OPC_PUSHSELF); |
| 5083 | } |
| 5084 | |
| 5085 | /* we just pushed one value */ |
| 5086 | G_cg->note_push(); |
| 5087 | |
| 5088 | /* assign the value to the context variable */ |
| 5089 | if (local_ctx_var_ <= 255 && TCPRS_LOCAL_CTX_METHODCTX <= 255) |
| 5090 | { |
| 5091 | /* we can make the assignment with a single instruction */ |
| 5092 | G_cg->write_op(OPC_SETINDLCL1I8); |
| 5093 | G_cs->write((uchar)local_ctx_var_); |
| 5094 | G_cs->write(TCPRS_LOCAL_CTX_METHODCTX); |
| 5095 | |
| 5096 | /* that pops one value */ |
| 5097 | G_cg->note_pop(); |
| 5098 | } |
| 5099 | else |
| 5100 | { |
| 5101 | /* get the context object */ |
| 5102 | CTcSymLocal::s_gen_code_getlcl(local_ctx_var_, FALSE); |
| 5103 | |
| 5104 | /* store the data in the local context object */ |
| 5105 | CTPNConst::s_gen_code_int(TCPRS_LOCAL_CTX_METHODCTX); |
| 5106 | G_cg->write_op(OPC_SETIND); |
| 5107 | |
| 5108 | /* discard the indexed result */ |
| 5109 | G_cg->write_op(OPC_DISC); |
| 5110 | |
| 5111 | /* |
| 5112 | * the SETIND pops three values and pushes one, then we pop one |
| 5113 | * more with the DISC - this is a net three pops with no extra |
| 5114 | * maximum depth |
| 5115 | */ |
| 5116 | G_cg->note_pop(3); |
| 5117 | } |
| 5118 | } |
| 5119 | |
| 5120 | /* generate the compound statement, if we have one */ |
| 5121 | if (stm_ != 0) |
| 5122 | stm_->gen_code(TRUE, TRUE); |
| 5123 | |
| 5124 | #ifdef T3_DEBUG |
| 5125 | if (G_cg->get_sp_depth() != 0) |
| 5126 | { |
| 5127 | printf("---> stack depth is %d after block codegen!\n", |
| 5128 | G_cg->get_sp_depth()); |
| 5129 | if (fixup_owner_sym_ != 0) |
| 5130 | printf("---> code block for %.*s\n", |
| 5131 | (int)fixup_owner_sym_->get_sym_len(), |
| 5132 | fixup_owner_sym_->get_sym()); |
| 5133 | } |
| 5134 | #endif |
| 5135 | |
| 5136 | /* close the method */ |
| 5137 | G_cg->close_method(local_cnt_, end_desc_, end_linenum_, &gen_ctx); |
| 5138 | |
| 5139 | /* remember the head of the nested symbol table list */ |
| 5140 | first_nested_symtab_ = G_cs->get_first_frame(); |
| 5141 | |
| 5142 | /* generate debug records if appropriate */ |
| 5143 | if (G_debug) |
| 5144 | build_debug_table(gen_ctx.method_ofs); |
| 5145 | |
| 5146 | /* check for unreferenced labels and issue warnings */ |
| 5147 | check_unreferenced_labels(); |
| 5148 | |
| 5149 | /* clean up globals for the end of the method */ |
| 5150 | G_cg->close_method_cleanup(&gen_ctx); |
| 5151 | } |
| 5152 | |
| 5153 | /* |
| 5154 | * Check for unreferenced local variables |
| 5155 | */ |
| 5156 | void CTPNCodeBody::check_locals() |
| 5157 | { |
| 5158 | CTcPrsSymtab *tab; |
| 5159 | |
| 5160 | /* check for unreferenced locals in each nested scope */ |
| 5161 | for (tab = first_nested_symtab_ ; tab != 0 ; tab = tab->get_list_next()) |
| 5162 | { |
| 5163 | /* check this table */ |
| 5164 | tab->check_unreferenced_locals(); |
| 5165 | } |
| 5166 | } |
| 5167 | |
| 5168 | /* |
| 5169 | * local symbol table enumerator for checking for parameter symbols that |
| 5170 | * belong in the local context |
| 5171 | */ |
| 5172 | void CTPNCodeBody::enum_for_param_ctx(void *, class CTcSymbol *sym) |
| 5173 | { |
| 5174 | /* if this is a local, check it further */ |
| 5175 | if (sym->get_type() == TC_SYM_LOCAL || sym->get_type() == TC_SYM_PARAM) |
| 5176 | { |
| 5177 | CTcSymLocal *lcl = (CTcSymLocal *)sym; |
| 5178 | |
| 5179 | /* |
| 5180 | * if it's a parameter, and it's also a context variable, its |
| 5181 | * value needs to be moved into the context |
| 5182 | */ |
| 5183 | if (lcl->is_param() && lcl->is_ctx_local()) |
| 5184 | { |
| 5185 | /* get the actual parameter value from the stack */ |
| 5186 | CTcSymLocal::s_gen_code_getlcl(lcl->get_var_num(), TRUE); |
| 5187 | |
| 5188 | /* store the value in the context variable */ |
| 5189 | lcl->gen_code_asi(TRUE, TC_ASI_SIMPLE, 0, TRUE); |
| 5190 | } |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | /* |
| 5195 | * generate as an anonymous function |
| 5196 | */ |
| 5197 | void CTPNCodeBody::js_anon_func() |
| 5198 | { |
| 5199 | /* generate the body onto the js expression stack */ |
| 5200 | gen_code(FALSE, FALSE); |
| 5201 | |
| 5202 | /* start with the 'function' keyword and open the arg list */ |
| 5203 | js_expr_buf expr; |
| 5204 | expr += "function("; |
| 5205 | |
| 5206 | /* add the argument names */ |
| 5207 | for (int i = 0 ; i < argc_ ; ++i) |
| 5208 | { |
| 5209 | char buf[32]; |
| 5210 | sprintf(buf, "%sa%d", i == 0 ? "" : ",", i); |
| 5211 | expr += buf; |
| 5212 | } |
| 5213 | |
| 5214 | /* close the arg list and add the placeholder for the body */ |
| 5215 | expr += ")$1"; |
| 5216 | |
| 5217 | /* push this as our new expression */ |
| 5218 | G_cg->js_expr((const char *)expr); |
| 5219 | } |
| 5220 | |
| 5221 | |
| 5222 | /* ------------------------------------------------------------------------ */ |
| 5223 | /* |
| 5224 | * 'return' statement |
| 5225 | */ |
| 5226 | |
| 5227 | /* |
| 5228 | * generate code |
| 5229 | */ |
| 5230 | void CTPNStmReturn::gen_code(int, int) |
| 5231 | { |
| 5232 | int val_on_stack; |
| 5233 | int need_gen; |
| 5234 | |
| 5235 | /* add a line record */ |
| 5236 | add_debug_line_rec(); |
| 5237 | |
| 5238 | /* presume we'll generate a value */ |
| 5239 | need_gen = TRUE; |
| 5240 | val_on_stack = FALSE; |
| 5241 | |
| 5242 | /* generate the return value expression, if appropriate */ |
| 5243 | if (expr_ != 0) |
| 5244 | { |
| 5245 | /* |
| 5246 | * it's an error if we're in a constructor, because a |
| 5247 | * constructor implicitly always returns 'self' |
| 5248 | */ |
| 5249 | if (G_cg->is_in_constructor()) |
| 5250 | log_error(TCERR_CONSTRUCT_CANNOT_RET_VAL); |
| 5251 | |
| 5252 | /* check for a constant expression */ |
| 5253 | if (expr_->is_const()) |
| 5254 | { |
| 5255 | switch(expr_->get_const_val()->get_type()) |
| 5256 | { |
| 5257 | case TC_CVT_NIL: |
| 5258 | case TC_CVT_TRUE: |
| 5259 | /* |
| 5260 | * we can use special constant return instructions for |
| 5261 | * these, so there's no need to generate the value |
| 5262 | */ |
| 5263 | need_gen = FALSE; |
| 5264 | break; |
| 5265 | |
| 5266 | default: |
| 5267 | /* |
| 5268 | * other types don't have constant-return opcodes, so we |
| 5269 | * must generate the expression code |
| 5270 | */ |
| 5271 | need_gen = TRUE; |
| 5272 | break; |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | /* if necessary, generate the value */ |
| 5277 | if (need_gen) |
| 5278 | { |
| 5279 | int depth; |
| 5280 | |
| 5281 | /* note the initial stack depth */ |
| 5282 | depth = G_cg->get_sp_depth(); |
| 5283 | |
| 5284 | /* |
| 5285 | * Generate the value. We are obviously not discarding the |
| 5286 | * value, and since returning a value is equivalent to |
| 5287 | * assigning the value, we must use the stricter assignment |
| 5288 | * (not 'for condition') rules for logical expressions |
| 5289 | */ |
| 5290 | expr_->gen_code(FALSE, FALSE); |
| 5291 | |
| 5292 | /* note whether we actually left a value on the stack */ |
| 5293 | val_on_stack = (G_cg->get_sp_depth() > depth); |
| 5294 | } |
| 5295 | else |
| 5296 | { |
| 5297 | /* |
| 5298 | * we obviously aren't leaving a value on the stack if we |
| 5299 | * don't generate anything |
| 5300 | */ |
| 5301 | val_on_stack = FALSE; |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | /* |
| 5306 | * Before we return, let any enclosing statements generate any code |
| 5307 | * necessary to leave their scope (in particular, we must invoke |
| 5308 | * 'finally' handlers in any enclosing 'try' blocks). |
| 5309 | * |
| 5310 | * Note that we generated the expression BEFORE we call any |
| 5311 | * 'finally' handlers. This is necessary because something we call |
| 5312 | * in the course of evaluating the return value could have thrown an |
| 5313 | * exception; if we were to call the 'finally' clauses before |
| 5314 | * generating the return value, we could invoke the 'finally' clause |
| 5315 | * twice (once explicitly, once in the handling of the thrown |
| 5316 | * exception), which would be incorrect. By generating the |
| 5317 | * 'finally' calls after the return expression, we're sure that the |
| 5318 | * 'finally' blocks are invoked only once - either through the |
| 5319 | * throw, or else now, after there's no more possibility of a |
| 5320 | * 'throw' before the return. |
| 5321 | */ |
| 5322 | if (G_cs->get_enclosing() != 0) |
| 5323 | { |
| 5324 | int did_save_retval; |
| 5325 | uint fin_ret_lcl; |
| 5326 | |
| 5327 | /* |
| 5328 | * if we're going to generate any subroutine calls, and we have |
| 5329 | * a return value on the stack, we need to save the return value |
| 5330 | * in a local to make sure the calculated value isn't affected |
| 5331 | * by the subroutine call |
| 5332 | */ |
| 5333 | if (val_on_stack |
| 5334 | && G_cs->get_enclosing()->will_gen_code_unwind_for_return() |
| 5335 | && G_cs->get_code_body() != 0) |
| 5336 | { |
| 5337 | /* allocate a local variable to save the return value */ |
| 5338 | fin_ret_lcl = G_cs->get_code_body()->alloc_fin_ret_lcl(); |
| 5339 | |
| 5340 | /* save the return value in a stack temporary for a moment */ |
| 5341 | CTcSymLocal::s_gen_code_setlcl_stk(fin_ret_lcl, FALSE); |
| 5342 | |
| 5343 | /* |
| 5344 | * note that we saved the return value, so we can retrieve |
| 5345 | * it later |
| 5346 | */ |
| 5347 | did_save_retval = TRUE; |
| 5348 | } |
| 5349 | else |
| 5350 | { |
| 5351 | /* note that we didn't save the return value */ |
| 5352 | did_save_retval = FALSE; |
| 5353 | } |
| 5354 | |
| 5355 | /* generate the unwind */ |
| 5356 | G_cs->get_enclosing()->gen_code_unwind_for_return(); |
| 5357 | |
| 5358 | /* if we saved the return value, retrieve it */ |
| 5359 | if (did_save_retval) |
| 5360 | CTcSymLocal::s_gen_code_getlcl(fin_ret_lcl, FALSE); |
| 5361 | } |
| 5362 | |
| 5363 | /* check for an expression to return */ |
| 5364 | if (G_cg->is_in_constructor()) |
| 5365 | { |
| 5366 | /* we're in a constructor - return 'self' */ |
| 5367 | G_cg->write_op(OPC_PUSHSELF); |
| 5368 | G_cg->write_op(OPC_RETVAL); |
| 5369 | } |
| 5370 | else if (expr_ == 0) |
| 5371 | { |
| 5372 | /* |
| 5373 | * there's no expression - generate a simple void return (but |
| 5374 | * explicitly return nil, so we don't return something left in |
| 5375 | * R0 from a previous function call we made) |
| 5376 | */ |
| 5377 | G_cg->write_op(OPC_RETNIL); |
| 5378 | } |
| 5379 | else |
| 5380 | { |
| 5381 | /* check for a constant expression */ |
| 5382 | if (expr_->is_const()) |
| 5383 | { |
| 5384 | switch(expr_->get_const_val()->get_type()) |
| 5385 | { |
| 5386 | case TC_CVT_NIL: |
| 5387 | /* generate a RETNIL instruction */ |
| 5388 | G_cg->write_op(OPC_RETNIL); |
| 5389 | break; |
| 5390 | |
| 5391 | case TC_CVT_TRUE: |
| 5392 | /* generate a RETTRUE instruction */ |
| 5393 | G_cg->write_op(OPC_RETTRUE); |
| 5394 | break; |
| 5395 | |
| 5396 | default: |
| 5397 | break; |
| 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | /* |
| 5402 | * if we needed code generation to evaluate the return value, we |
| 5403 | * now need to return the value |
| 5404 | */ |
| 5405 | if (need_gen) |
| 5406 | { |
| 5407 | /* |
| 5408 | * Other types don't have constant-return opcodes. We |
| 5409 | * already generated the expression value (before invoking |
| 5410 | * the enclosing 'finally' handlers, if any), so the value |
| 5411 | * is on the stack, and all we need to do is return it. |
| 5412 | * |
| 5413 | * If we didn't actually leave a value on the stack, we'll |
| 5414 | * just return nil. |
| 5415 | */ |
| 5416 | if (val_on_stack) |
| 5417 | { |
| 5418 | /* generate the return-value opcode */ |
| 5419 | G_cg->write_op(OPC_RETVAL); |
| 5420 | |
| 5421 | /* RETVAL removes an element from the stack */ |
| 5422 | G_cg->note_pop(); |
| 5423 | } |
| 5424 | else |
| 5425 | { |
| 5426 | /* |
| 5427 | * The depth didn't change - they must have evaluated an |
| 5428 | * expression involving a dstring or void function. |
| 5429 | * Return nil instead of the non-existent value. |
| 5430 | */ |
| 5431 | G_cg->write_op(OPC_RETNIL); |
| 5432 | } |
| 5433 | } |
| 5434 | } |
| 5435 | } |
| 5436 | |
| 5437 | /* ------------------------------------------------------------------------ */ |
| 5438 | /* |
| 5439 | * Static property initializer statement |
| 5440 | */ |
| 5441 | void CTPNStmStaticPropInit::gen_code(int, int) |
| 5442 | { |
| 5443 | int depth; |
| 5444 | |
| 5445 | /* add a line record */ |
| 5446 | add_debug_line_rec(); |
| 5447 | |
| 5448 | /* note the initial stack depth */ |
| 5449 | depth = G_cg->get_sp_depth(); |
| 5450 | |
| 5451 | /* generate the expression, keeping the generated value */ |
| 5452 | expr_->gen_code(FALSE, FALSE); |
| 5453 | |
| 5454 | /* ensure that we generated a value; if we didn't, push nil by default */ |
| 5455 | if (G_cg->get_sp_depth() <= depth) |
| 5456 | { |
| 5457 | /* push a default nil value */ |
| 5458 | G_cg->write_op(OPC_PUSHNIL); |
| 5459 | G_cg->note_push(); |
| 5460 | } |
| 5461 | |
| 5462 | /* |
| 5463 | * duplicate the value on the stack, so we can assign it to |
| 5464 | * initialize the property and also return it |
| 5465 | */ |
| 5466 | G_cg->write_op(OPC_DUP); |
| 5467 | G_cg->note_push(); |
| 5468 | |
| 5469 | /* write the SETPROPSELF to initialize the property */ |
| 5470 | G_cg->write_op(OPC_SETPROPSELF); |
| 5471 | G_cs->write_prop_id(prop_); |
| 5472 | |
| 5473 | /* SETPROPSELF removes the value */ |
| 5474 | G_cg->note_pop(); |
| 5475 | |
| 5476 | /* return the value (which we duplicated on the stack) */ |
| 5477 | G_cg->write_op(OPC_RETVAL); |
| 5478 | |
| 5479 | /* RETVAL removes the value */ |
| 5480 | G_cg->note_pop(); |
| 5481 | } |
| 5482 | |
| 5483 | |
| 5484 | /* ------------------------------------------------------------------------ */ |
| 5485 | /* |
| 5486 | * Object Definition Statement |
| 5487 | */ |
| 5488 | |
| 5489 | /* |
| 5490 | * generate code |
| 5491 | */ |
| 5492 | void CTPNStmObject::gen_code(int, int) |
| 5493 | { |
| 5494 | CTPNSuperclass *sc; |
| 5495 | CTPNObjProp *prop; |
| 5496 | int sc_cnt; |
| 5497 | ulong start_ofs; |
| 5498 | uint internal_flags; |
| 5499 | uint obj_flags; |
| 5500 | CTcDataStream *str; |
| 5501 | int bad_sc; |
| 5502 | |
| 5503 | /* if this object has been replaced, don't generate any code for it */ |
| 5504 | if (replaced_) |
| 5505 | return; |
| 5506 | |
| 5507 | /* add an implicit constructor if necessary */ |
| 5508 | add_implicit_constructor(); |
| 5509 | |
| 5510 | /* get the appropriate stream for generating the data */ |
| 5511 | str = obj_sym_->get_stream(); |
| 5512 | |
| 5513 | /* clear the internal flags */ |
| 5514 | internal_flags = 0; |
| 5515 | |
| 5516 | /* |
| 5517 | * if we're a modified object, set the 'modified' flag in the object |
| 5518 | * header |
| 5519 | */ |
| 5520 | if (modified_) |
| 5521 | internal_flags |= TCT3_OBJ_MODIFIED; |
| 5522 | |
| 5523 | /* set the 'transient' flag if appropriate */ |
| 5524 | if (transient_) |
| 5525 | internal_flags |= TCT3_OBJ_TRANSIENT; |
| 5526 | |
| 5527 | /* clear the object flags */ |
| 5528 | obj_flags = 0; |
| 5529 | |
| 5530 | /* |
| 5531 | * If we're specifically marked as a 'class' object, or we're a |
| 5532 | * modified object, set the 'class' flag in the object flags. |
| 5533 | */ |
| 5534 | if (is_class_ || modified_) |
| 5535 | obj_flags |= TCT3_OBJFLG_CLASS; |
| 5536 | |
| 5537 | /* remember our starting offset in the object stream */ |
| 5538 | start_ofs = str->get_ofs(); |
| 5539 | |
| 5540 | /* |
| 5541 | * store our stream offset in our defining symbol, for storage in |
| 5542 | * the object file |
| 5543 | */ |
| 5544 | obj_sym_->set_stream_ofs(start_ofs); |
| 5545 | |
| 5546 | /* write our internal flags */ |
| 5547 | str->write2(internal_flags); |
| 5548 | |
| 5549 | /* |
| 5550 | * First, write the per-object image file "OBJS" header - each |
| 5551 | * object starts with its object ID and the number of bytes in the |
| 5552 | * object's metaclass-specific data. For now, write zero as a |
| 5553 | * placeholder for our data size. Note that this is a |
| 5554 | * self-reference: it must be modified if the object is renumbered. |
| 5555 | */ |
| 5556 | str->write_obj_id_selfref(obj_sym_); |
| 5557 | str->write2(0); |
| 5558 | |
| 5559 | /* write a placeholder for the superclass count */ |
| 5560 | str->write2(0); |
| 5561 | |
| 5562 | /* write the fixed property count */ |
| 5563 | str->write2(prop_cnt_); |
| 5564 | |
| 5565 | /* write the object flags */ |
| 5566 | str->write2(obj_flags); |
| 5567 | |
| 5568 | /* |
| 5569 | * First, go through the superclass list and verify that each |
| 5570 | * superclass is actually an object. |
| 5571 | */ |
| 5572 | for (bad_sc = FALSE, sc_cnt = 0, sc = first_sc_ ; sc != 0 ; sc = sc->nxt_) |
| 5573 | { |
| 5574 | CTcSymObj *sc_sym; |
| 5575 | |
| 5576 | /* look up the superclass in the global symbol table */ |
| 5577 | sc_sym = (CTcSymObj *)sc->get_sym(); |
| 5578 | |
| 5579 | /* make sure it's defined, and that it's really an object */ |
| 5580 | if (sc_sym == 0) |
| 5581 | { |
| 5582 | /* not defined */ |
| 5583 | log_error(TCERR_UNDEF_SYM_SC, |
| 5584 | (int)sc->get_sym_len(), sc->get_sym_txt(), |
| 5585 | (int)obj_sym_->get_sym_len(), obj_sym_->get_sym()); |
| 5586 | |
| 5587 | /* note that we have an invalid superclass */ |
| 5588 | bad_sc = TRUE; |
| 5589 | } |
| 5590 | else if (sc_sym->get_type() != TC_SYM_OBJ) |
| 5591 | { |
| 5592 | /* log an error */ |
| 5593 | log_error(TCERR_SC_NOT_OBJECT, |
| 5594 | (int)sc_sym->get_sym_len(), sc_sym->get_sym()); |
| 5595 | |
| 5596 | /* note that we have an invalid superclass */ |
| 5597 | bad_sc = TRUE; |
| 5598 | } |
| 5599 | else |
| 5600 | { |
| 5601 | /* count the superclass */ |
| 5602 | ++sc_cnt; |
| 5603 | |
| 5604 | /* write the superclass to the object header */ |
| 5605 | str->write_obj_id(sc_sym->get_obj_id()); |
| 5606 | } |
| 5607 | } |
| 5608 | |
| 5609 | /* |
| 5610 | * If we detected a 'bad template' error when we were parsing the |
| 5611 | * object definition, and all of our superclasses are valid, report the |
| 5612 | * template error. |
| 5613 | * |
| 5614 | * Do not report this error if we have any undefined or invalid |
| 5615 | * superclasses, because (1) we've already reported one error for this |
| 5616 | * object definition (the bad superclass error), and (2) the missing |
| 5617 | * template is likely just a consequence of the bad superclass, since |
| 5618 | * we can't have scanned the proper superclass's list of templates if |
| 5619 | * they didn't tell us the correct superclass to start with. When they |
| 5620 | * fix the superclass list and re-compile the code, it's likely that |
| 5621 | * this will fix the template problem as well, since we'll probably be |
| 5622 | * able to find the template give the corrected superclass list. |
| 5623 | * |
| 5624 | * If we found an undescribed class anywhere in our hierarchy, a |
| 5625 | * template simply cannot be used with this object; otherwise, the |
| 5626 | * error is that we failed to find a suitable template |
| 5627 | */ |
| 5628 | if (has_bad_template() && !bad_sc) |
| 5629 | log_error(has_undesc_sc() |
| 5630 | ? TCERR_OBJ_DEF_CANNOT_USE_TEMPLATE |
| 5631 | : TCERR_OBJ_DEF_NO_TEMPLATE); |
| 5632 | |
| 5633 | /* go back and write the superclass count to the header */ |
| 5634 | str->write2_at(start_ofs + TCT3_TADSOBJ_HEADER_OFS, sc_cnt); |
| 5635 | |
| 5636 | /* |
| 5637 | * Write the properties. We're required to write the properties in |
| 5638 | * sorted order of property ID, but we can't do that yet, because |
| 5639 | * the property ID's aren't finalized until after linking. For now, |
| 5640 | * just write them out in the order in which they were defined. |
| 5641 | */ |
| 5642 | for (prop = first_prop_ ; prop != 0 ; prop = prop->nxt_) |
| 5643 | { |
| 5644 | /* make sure we have a valid property symbol */ |
| 5645 | if (prop->get_prop_sym() != 0) |
| 5646 | { |
| 5647 | /* write the property ID */ |
| 5648 | str->write_prop_id(prop->get_prop_sym()->get_prop()); |
| 5649 | |
| 5650 | /* generate code for the property */ |
| 5651 | prop->gen_code(FALSE, FALSE); |
| 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | /* |
| 5656 | * go back and write the size of our metaclass-specific data - this |
| 5657 | * goes at offset 4 in the T3 generic metaclass header |
| 5658 | */ |
| 5659 | str->write2_at(start_ofs + TCT3_META_HEADER_OFS + 4, |
| 5660 | str->get_ofs() - (start_ofs + TCT3_META_HEADER_OFS + 6)); |
| 5661 | } |
| 5662 | |
| 5663 | /* |
| 5664 | * Check for unreferenced local variables |
| 5665 | */ |
| 5666 | void CTPNStmObject::check_locals() |
| 5667 | { |
| 5668 | CTPNObjProp *prop; |
| 5669 | |
| 5670 | /* check for unreferenced locals for each property */ |
| 5671 | for (prop = first_prop_ ; prop != 0 ; prop = prop->nxt_) |
| 5672 | prop->check_locals(); |
| 5673 | } |