| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/tct3.cpp,v 1.5 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | tct3.cpp - TADS 3 Compiler - T3 VM Code Generator |
| 15 | Function |
| 16 | Generate code for the T3 VM |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/08/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "os.h" |
| 28 | #include "tcprs.h" |
| 29 | #include "tct3.h" |
| 30 | #include "tcgen.h" |
| 31 | #include "vmtype.h" |
| 32 | #include "vmwrtimg.h" |
| 33 | #include "vmfile.h" |
| 34 | #include "tcmain.h" |
| 35 | #include "tcerr.h" |
| 36 | #include "vmbignum.h" |
| 37 | #include "vmrunsym.h" |
| 38 | #include "vmhash.h" |
| 39 | #include "vmmeta.h" |
| 40 | #include "tct3unas.h" |
| 41 | #include "vmop.h" |
| 42 | |
| 43 | |
| 44 | /* ------------------------------------------------------------------------ */ |
| 45 | /* |
| 46 | * Named argument call table. The code body maintains a list of tables, |
| 47 | * one per call with named arguments. At the end of the code body, we |
| 48 | * generate the tables as part of the instruction stream. |
| 49 | */ |
| 50 | struct CTcNamedArgTab |
| 51 | { |
| 52 | CTcNamedArgTab(const CTcNamedArgs *args) |
| 53 | { |
| 54 | /* remember the argument list */ |
| 55 | arglist = args->args; |
| 56 | cnt = args->cnt; |
| 57 | |
| 58 | /* create a code label for the table's opcode stream location */ |
| 59 | lbl = G_cs->new_label_fwd(); |
| 60 | |
| 61 | /* not in a list yet */ |
| 62 | nxt = 0; |
| 63 | } |
| 64 | |
| 65 | /* generate this table */ |
| 66 | void generate() |
| 67 | { |
| 68 | /* define the label location and start the table */ |
| 69 | CTcPrsNode::def_label_pos(lbl); |
| 70 | G_cg->write_op(OPC_NAMEDARGTAB); |
| 71 | |
| 72 | /* write a placeholder for the table size */ |
| 73 | ulong start = G_cs->get_ofs(); |
| 74 | G_cs->write2(0); |
| 75 | |
| 76 | /* write the number of arguments in the list */ |
| 77 | G_cs->write2(cnt); |
| 78 | |
| 79 | /* write placeholders for the index entries */ |
| 80 | ulong idx = G_cs->get_ofs(), idx0 = idx; |
| 81 | for (CTPNArg *arg = arglist->get_arg_list_head() ; arg != 0 ; |
| 82 | arg = arg->get_next_arg()) |
| 83 | { |
| 84 | /* if it's a named argument, write an index placeholder */ |
| 85 | if (arg->is_named_param()) |
| 86 | G_cs->write2(0); |
| 87 | } |
| 88 | |
| 89 | /* write a final index marker, for computing the last item's length */ |
| 90 | G_cs->write2(0); |
| 91 | |
| 92 | /* write the strings */ |
| 93 | for (CTPNArg *arg = arglist->get_arg_list_head() ; arg != 0 ; |
| 94 | arg = arg->get_next_arg()) |
| 95 | { |
| 96 | /* if it's a named argument, write it out */ |
| 97 | if (arg->is_named_param()) |
| 98 | { |
| 99 | /* write its index pointer */ |
| 100 | G_cs->write2_at(idx, G_cs->get_ofs() - idx0); |
| 101 | |
| 102 | /* write the string */ |
| 103 | G_cs->write(arg->get_name(), arg->get_name_len()); |
| 104 | |
| 105 | /* advance the index pointer */ |
| 106 | idx += 2; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /* write the final index marker */ |
| 111 | G_cs->write2_at(idx, G_cs->get_ofs() - idx0); |
| 112 | |
| 113 | /* go back and fill in the table length prefix */ |
| 114 | G_cs->write2_at(start, G_cs->get_ofs() - start - 2); |
| 115 | } |
| 116 | |
| 117 | /* argument list */ |
| 118 | const CTPNArglist *arglist; |
| 119 | |
| 120 | /* number of named arguments */ |
| 121 | int cnt; |
| 122 | |
| 123 | /* code label for the table's location in the opcode stream */ |
| 124 | CTcCodeLabel *lbl; |
| 125 | |
| 126 | /* next in list */ |
| 127 | CTcNamedArgTab *nxt; |
| 128 | }; |
| 129 | |
| 130 | |
| 131 | /* ------------------------------------------------------------------------ */ |
| 132 | /* |
| 133 | * T3 Target Code Generator class |
| 134 | */ |
| 135 | |
| 136 | /* |
| 137 | * initialize the code generator |
| 138 | */ |
| 139 | CTcGenTarg::CTcGenTarg() |
| 140 | { |
| 141 | int i; |
| 142 | |
| 143 | /* |
| 144 | * we haven't written any instructions yet - fill the pipe with |
| 145 | * no-op instructions so that we don't think we can combine the |
| 146 | * first instruction with anything previous |
| 147 | */ |
| 148 | last_op_ = OPC_NOP; |
| 149 | second_last_op_ = OPC_NOP; |
| 150 | |
| 151 | /* |
| 152 | * we haven't seen any strings or lists yet - set the initial |
| 153 | * maximum lengths to zero |
| 154 | */ |
| 155 | max_str_len_ = 0; |
| 156 | max_list_cnt_ = 0; |
| 157 | |
| 158 | /* we haven't generated any code yet */ |
| 159 | max_bytecode_len_ = 0; |
| 160 | |
| 161 | /* there are no metaclasses defined yet */ |
| 162 | meta_head_ = meta_tail_ = 0; |
| 163 | meta_cnt_ = 0; |
| 164 | |
| 165 | /* no function sets defined yet */ |
| 166 | fnset_head_ = fnset_tail_ = 0; |
| 167 | fnset_cnt_ = 0; |
| 168 | |
| 169 | /* |
| 170 | * Add the built-in metaclass entries. The order of these entries |
| 171 | * is fixed to match the TCT3_METAID_xxx constants - if this order |
| 172 | * changes, those constants must change to match. |
| 173 | */ |
| 174 | add_meta("tads-object"); |
| 175 | add_meta("list"); |
| 176 | add_meta("dictionary2/030000"); |
| 177 | add_meta("grammar-production/030000"); |
| 178 | add_meta("vector"); |
| 179 | add_meta("anon-func-ptr"); |
| 180 | add_meta("int-class-mod/030000"); |
| 181 | add_meta("lookuptable/030003"); |
| 182 | |
| 183 | /* |
| 184 | * Set up the initial translation table for translating from our |
| 185 | * internal metaclass index values - TCT3_METAID_xxx - to the *actual* |
| 186 | * image file dependency index. When we're compiling a program, these |
| 187 | * are simply in our own internal order - index N in our scheme is |
| 188 | * simply index N in the actual image file - because we get to lay out |
| 189 | * exactly what the dependency table looks like. However, we need the |
| 190 | * translation table for when we're being used as part of the debugger |
| 191 | * - in those cases, we don't get to dictate the dependency table |
| 192 | * layout, because the image file (and thus the dependency table) might |
| 193 | * have been generated by a different version of the compiler. In |
| 194 | * those cases, the image file loader will have to set up the |
| 195 | * translation information for us. |
| 196 | */ |
| 197 | for (i = 0 ; i <= TCT3_METAID_LAST ; ++i) |
| 198 | predef_meta_idx_[i] = i; |
| 199 | |
| 200 | /* start at the first valid property ID */ |
| 201 | next_prop_ = 1; |
| 202 | |
| 203 | /* start at the first valid object ID */ |
| 204 | next_obj_ = 1; |
| 205 | |
| 206 | /* allocate an initial sort buffer */ |
| 207 | sort_buf_size_ = 4096; |
| 208 | sort_buf_ = (char *)t3malloc(sort_buf_size_); |
| 209 | |
| 210 | /* not in a constructor */ |
| 211 | in_constructor_ = FALSE; |
| 212 | |
| 213 | /* not in an operator overload */ |
| 214 | in_op_overload_ = FALSE; |
| 215 | |
| 216 | /* no debug line record pointers yet */ |
| 217 | debug_line_cnt_ = 0; |
| 218 | debug_line_head_ = debug_line_tail_ = 0; |
| 219 | |
| 220 | /* normal (static compiler, non-debug) evaluation mode */ |
| 221 | eval_for_dyn_ = FALSE; |
| 222 | eval_for_debug_ = FALSE; |
| 223 | speculative_ = FALSE; |
| 224 | debug_stack_level_ = 0; |
| 225 | |
| 226 | /* no multi-method initializer object yet */ |
| 227 | mminit_obj_ = VM_INVALID_OBJ; |
| 228 | |
| 229 | /* create the string interning hash table */ |
| 230 | strtab_ = new CVmHashTable(1024, new CVmHashFuncCS(), TRUE); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * delete the code generator |
| 235 | */ |
| 236 | CTcGenTarg::~CTcGenTarg() |
| 237 | { |
| 238 | /* delete all of the metaclass list entries */ |
| 239 | while (meta_head_ != 0) |
| 240 | { |
| 241 | tc_meta_entry *nxt; |
| 242 | |
| 243 | /* remember the next item */ |
| 244 | nxt = meta_head_->nxt; |
| 245 | |
| 246 | /* delete this item */ |
| 247 | t3free(meta_head_); |
| 248 | |
| 249 | /* move on */ |
| 250 | meta_head_ = nxt; |
| 251 | } |
| 252 | |
| 253 | /* delete all of the function set list entries */ |
| 254 | while (fnset_head_ != 0) |
| 255 | { |
| 256 | tc_fnset_entry *nxt; |
| 257 | |
| 258 | /* remember the next item */ |
| 259 | nxt = fnset_head_->nxt; |
| 260 | |
| 261 | /* delete this item */ |
| 262 | t3free(fnset_head_); |
| 263 | |
| 264 | /* move on */ |
| 265 | fnset_head_ = nxt; |
| 266 | } |
| 267 | |
| 268 | /* delete our sort buffer */ |
| 269 | t3free(sort_buf_); |
| 270 | |
| 271 | /* delete the debug line record pointers */ |
| 272 | while (debug_line_head_ != 0) |
| 273 | { |
| 274 | tct3_debug_line_page *nxt; |
| 275 | |
| 276 | /* remember the next one */ |
| 277 | nxt = debug_line_head_->nxt; |
| 278 | |
| 279 | /* delete this one */ |
| 280 | t3free(debug_line_head_); |
| 281 | |
| 282 | /* move on */ |
| 283 | debug_line_head_ = nxt; |
| 284 | } |
| 285 | |
| 286 | /* delete the string table */ |
| 287 | delete strtab_; |
| 288 | } |
| 289 | |
| 290 | /* start loading the image file metaclass dependency table */ |
| 291 | void CTcGenTarg::start_image_file_meta_table() |
| 292 | { |
| 293 | int i; |
| 294 | |
| 295 | /* |
| 296 | * clear out all of the entries - set them to -1 to indicate that |
| 297 | * they're invalid |
| 298 | */ |
| 299 | for (i = 0 ; i <= TCT3_METAID_LAST ; ++i) |
| 300 | predef_meta_idx_[i] = -1; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Load an image file metaclass dependency table. When we're being used in |
| 305 | * a debugger, the image loader must call this with the run-time dependency |
| 306 | * table to establish the translation from our internal metaclass |
| 307 | * identifiers (TCT3_METAID_xxx) to the actual run-time index values. |
| 308 | */ |
| 309 | void CTcGenTarg::load_image_file_meta_table( |
| 310 | const char *nm, size_t len, int idx) |
| 311 | { |
| 312 | /* |
| 313 | * Check the name against our known names. If it matches one of the |
| 314 | * known types, store the actual index in our translation table under |
| 315 | * our internal index entry. If it's not a known name, ignore it - we |
| 316 | * only care about the names we actually pre-define, because those are |
| 317 | * the only ones we need to generate our own code for. |
| 318 | */ |
| 319 | if (len == 11 && memcmp(nm, "tads-object", 11) == 0) |
| 320 | predef_meta_idx_[TCT3_METAID_TADSOBJ] = idx; |
| 321 | else if (len == 4 && memcmp(nm, "list", 4) == 0) |
| 322 | predef_meta_idx_[TCT3_METAID_LIST] = idx; |
| 323 | else if (len == 11 && memcmp(nm, "dictionary2", 11) == 0) |
| 324 | predef_meta_idx_[TCT3_METAID_DICT] = idx; |
| 325 | else if (len == 18 && memcmp(nm, "grammar-production", 18) == 0) |
| 326 | predef_meta_idx_[TCT3_METAID_GRAMPROD] = idx; |
| 327 | else if (len == 6 && memcmp(nm, "vector", 6) == 0) |
| 328 | predef_meta_idx_[TCT3_METAID_VECTOR] = idx; |
| 329 | else if (len == 13 && memcmp(nm, "anon-func-ptr", 13) == 0) |
| 330 | predef_meta_idx_[TCT3_METAID_ANONFN] = idx; |
| 331 | else if (len == 13 && memcmp(nm, "int-class-mod", 13) == 0) |
| 332 | predef_meta_idx_[TCT3_METAID_ICMOD] = idx; |
| 333 | else if (len == 11 && memcmp(nm, "lookuptable", 11) == 0) |
| 334 | predef_meta_idx_[TCT3_METAID_LOOKUP_TABLE] = idx; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * End the image file metaclass dependency table. |
| 339 | */ |
| 340 | void CTcGenTarg::end_image_file_meta_table() |
| 341 | { |
| 342 | int i; |
| 343 | |
| 344 | /* |
| 345 | * Scan the metaclass translation table and make sure they've all been |
| 346 | * set. If any are unset, it means that these metaclasses aren't |
| 347 | * available; since we depend upon these metaclasses, this means that |
| 348 | * we can't generate code interactively, so we can't act as a debugger. |
| 349 | */ |
| 350 | for (i = 0 ; i <= TCT3_METAID_LAST ; ++i) |
| 351 | { |
| 352 | /* if this entry hasn't been set properly, abort */ |
| 353 | if (predef_meta_idx_[i] == -1) |
| 354 | err_throw(VMERR_IMAGE_INCOMPAT_VSN_DBG); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Add an entry to the metaclass dependency table |
| 360 | */ |
| 361 | int CTcGenTarg::add_meta(const char *nm, size_t len, |
| 362 | CTcSymMetaclass *sym) |
| 363 | { |
| 364 | tc_meta_entry *ent; |
| 365 | size_t extra_len; |
| 366 | const char *extra_ptr; |
| 367 | const char *p; |
| 368 | size_t rem; |
| 369 | |
| 370 | /* |
| 371 | * if the name string doesn't contain a slash, allocate enough space |
| 372 | * to add an implied version suffix of "/000000" |
| 373 | */ |
| 374 | for (p = nm, rem = len ; rem != 0 && *p != '/' ; ++p, --rem) ; |
| 375 | if (rem == 0) |
| 376 | { |
| 377 | /* we didn't find a version suffix - add space for one */ |
| 378 | extra_len = 7; |
| 379 | extra_ptr = "/000000"; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | /* |
| 384 | * there's already a version suffix - but make sure we have |
| 385 | * space for a six-character string |
| 386 | */ |
| 387 | if (rem < 7) |
| 388 | { |
| 389 | /* add zeros to pad out to a six-place version string */ |
| 390 | extra_len = 7 - rem; |
| 391 | extra_ptr = "000000"; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | /* we need nothing extra */ |
| 396 | extra_len = 0; |
| 397 | extra_ptr = 0; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* allocate a new entry for the item */ |
| 402 | ent = (tc_meta_entry *)t3malloc(sizeof(tc_meta_entry) + len + extra_len); |
| 403 | if (ent == 0) |
| 404 | err_throw(TCERR_CODEGEN_NO_MEM); |
| 405 | |
| 406 | /* copy the name into the entry */ |
| 407 | memcpy(ent->nm, nm, len); |
| 408 | |
| 409 | /* add any extra version suffix information */ |
| 410 | if (extra_len != 0) |
| 411 | memcpy(ent->nm + len, extra_ptr, extra_len); |
| 412 | |
| 413 | /* null-terminate the name string in the entry */ |
| 414 | ent->nm[len + extra_len] = '\0'; |
| 415 | |
| 416 | /* remember the symbol */ |
| 417 | ent->sym = sym; |
| 418 | |
| 419 | /* link the entry in at the end of the list */ |
| 420 | ent->nxt = 0; |
| 421 | if (meta_tail_ != 0) |
| 422 | meta_tail_->nxt = ent; |
| 423 | else |
| 424 | meta_head_ = ent; |
| 425 | meta_tail_ = ent; |
| 426 | |
| 427 | /* count the entry, returning the index of the entry in the list */ |
| 428 | return meta_cnt_++; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Find a metaclass index given the global identifier. |
| 433 | */ |
| 434 | tc_meta_entry *CTcGenTarg::find_meta_entry(const char *nm, size_t len, |
| 435 | int update_vsn, int *entry_idx) |
| 436 | { |
| 437 | tc_meta_entry *ent; |
| 438 | int idx; |
| 439 | size_t name_len; |
| 440 | const char *p; |
| 441 | const char *vsn; |
| 442 | size_t vsn_len; |
| 443 | size_t rem; |
| 444 | |
| 445 | /* find the version suffix, if any */ |
| 446 | for (rem = len, p = nm ; rem != 0 && *p != '/' ; --rem, ++p) ; |
| 447 | |
| 448 | /* note the length of the name portion (up to the '/') */ |
| 449 | name_len = len - rem; |
| 450 | |
| 451 | /* note the version string, if there is one */ |
| 452 | if (rem != 0) |
| 453 | { |
| 454 | vsn = p + 1; |
| 455 | vsn_len = rem - 1; |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | vsn = 0; |
| 460 | vsn_len = 0; |
| 461 | } |
| 462 | |
| 463 | /* search the existing entries */ |
| 464 | for (idx = 0, ent = meta_head_ ; ent != 0 ; ent = ent->nxt, ++idx) |
| 465 | { |
| 466 | size_t ent_name_len; |
| 467 | char *ent_vsn; |
| 468 | |
| 469 | /* find the version suffix in the entry */ |
| 470 | for (ent_vsn = ent->nm ; *ent_vsn != '\0' && *ent_vsn != '/' ; |
| 471 | ++ent_vsn) ; |
| 472 | |
| 473 | /* the name is the part up to the '/' */ |
| 474 | ent_name_len = ent_vsn - ent->nm; |
| 475 | |
| 476 | /* note the length of the name and the version suffix */ |
| 477 | if (*ent_vsn == '/') |
| 478 | { |
| 479 | /* the version is what follows the '/' */ |
| 480 | ++ent_vsn; |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | /* there is no version suffix */ |
| 485 | ent_vsn = 0; |
| 486 | } |
| 487 | |
| 488 | /* if this is the one, return it */ |
| 489 | if (ent_name_len == name_len && memcmp(ent->nm, nm, name_len) == 0) |
| 490 | { |
| 491 | /* |
| 492 | * if this version number is higher than the version number |
| 493 | * we previously recorded, remember the new, higher version |
| 494 | * number |
| 495 | */ |
| 496 | if (update_vsn && ent_vsn != 0 && strlen(ent_vsn) == vsn_len |
| 497 | && memcmp(vsn, ent_vsn, vsn_len) > 0) |
| 498 | { |
| 499 | /* store the new version string */ |
| 500 | memcpy(ent_vsn, vsn, vsn_len); |
| 501 | } |
| 502 | |
| 503 | /* tell the caller the index, and return the entry */ |
| 504 | *entry_idx = idx; |
| 505 | return ent; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* we didn't find it */ |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Get the property ID for the given method table index in the given |
| 515 | * metaclass. The metaclass ID is given as the internal metaclass ID |
| 516 | * (without the "/version" suffix), not as the external class name. |
| 517 | * |
| 518 | * The method table index is 0-based. |
| 519 | */ |
| 520 | CTcPrsNode *CTcGenTarg::get_metaclass_prop(const char *name, ushort idx) const |
| 521 | { |
| 522 | vm_prop_id_t propid; |
| 523 | |
| 524 | /* |
| 525 | * If we have a loaded metaclass table from the running program, use |
| 526 | * it. This will be the case if we're doing a dynamic or debugging |
| 527 | * compilation. If we do, use it, since it has the direct information |
| 528 | * on the loaded classes. |
| 529 | * |
| 530 | * If there's no metaclass table, this is an ordinary static |
| 531 | * compilation, so get the metaclass information from the symbol table. |
| 532 | */ |
| 533 | if (G_metaclass_tab != 0) |
| 534 | { |
| 535 | /* look up the metaclass in the table */ |
| 536 | vm_meta_entry_t *entry = G_metaclass_tab->get_entry_by_id(name); |
| 537 | |
| 538 | /* if we didn't find the entry, return failure */ |
| 539 | if (entry == 0) |
| 540 | return 0; |
| 541 | |
| 542 | /* |
| 543 | * Get the property from the table. Note that xlat_func() uses a |
| 544 | * 1-based index. |
| 545 | */ |
| 546 | if ((propid = entry->xlat_func(idx + 1)) == VM_INVALID_PROP) |
| 547 | return 0; |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | /* static compilation - look up the metaclass in the symbol table */ |
| 552 | CTcSymMetaclass *sym = G_cg->find_meta_sym(name, strlen(name)); |
| 553 | if (sym == 0) |
| 554 | return 0; |
| 555 | |
| 556 | /* get the metaclass method table entry at the given index */ |
| 557 | CTcSymMetaProp *mprop = sym->get_nth_prop(idx); |
| 558 | if (mprop == 0 || mprop->prop_ == 0) |
| 559 | return 0; |
| 560 | |
| 561 | /* get the property ID from the method table entry */ |
| 562 | propid = mprop->prop_->get_prop(); |
| 563 | } |
| 564 | |
| 565 | /* set up a constant value for the property ID */ |
| 566 | CTcConstVal pv; |
| 567 | pv.set_prop(propid); |
| 568 | |
| 569 | /* return a constant parse node for the property ID */ |
| 570 | return new CTPNConst(&pv); |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Find a metaclass symbol given the global identifier |
| 575 | */ |
| 576 | class CTcSymMetaclass *CTcGenTarg::find_meta_sym(const char *nm, size_t len) |
| 577 | { |
| 578 | tc_meta_entry *ent; |
| 579 | int idx; |
| 580 | |
| 581 | /* find the entry */ |
| 582 | ent = find_meta_entry(nm, len, TRUE, &idx); |
| 583 | |
| 584 | /* |
| 585 | * if we found it, return the associated metaclass symbol; if |
| 586 | * there's no entry, there's no symbol |
| 587 | */ |
| 588 | if (ent != 0) |
| 589 | return ent->sym; |
| 590 | else |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /* |
| 596 | * Find or add a metaclass entry |
| 597 | */ |
| 598 | int CTcGenTarg::find_or_add_meta(const char *nm, size_t len, |
| 599 | CTcSymMetaclass *sym) |
| 600 | { |
| 601 | tc_meta_entry *ent; |
| 602 | int idx; |
| 603 | |
| 604 | /* find the entry */ |
| 605 | ent = find_meta_entry(nm, len, TRUE, &idx); |
| 606 | |
| 607 | /* if we found it, return the index */ |
| 608 | if (ent != 0) |
| 609 | { |
| 610 | /* |
| 611 | * found it - if it didn't already have a symbol mapping, use |
| 612 | * the new symbol; if there is a symbol in the table entry |
| 613 | * already, however, do not change it |
| 614 | */ |
| 615 | if (ent->sym == 0) |
| 616 | ent->sym = sym; |
| 617 | |
| 618 | /* return the index */ |
| 619 | return idx; |
| 620 | } |
| 621 | |
| 622 | /* we didn't find an existing entry - add a new one */ |
| 623 | return add_meta(nm, len, sym); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Get the symbol for a given metaclass dependency table entry |
| 628 | */ |
| 629 | CTcSymMetaclass *CTcGenTarg::get_meta_sym(int meta_idx) |
| 630 | { |
| 631 | tc_meta_entry *ent; |
| 632 | |
| 633 | /* find the list entry at the given index */ |
| 634 | for (ent = meta_head_ ; ent != 0 && meta_idx != 0 ; |
| 635 | ent = ent->nxt, --meta_idx) ; |
| 636 | |
| 637 | /* if we didn't find the entry, do nothing */ |
| 638 | if (ent == 0 || meta_idx != 0) |
| 639 | return 0; |
| 640 | |
| 641 | /* return this entry's symbol */ |
| 642 | return ent->sym; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Get the external name for the given metaclass index |
| 647 | */ |
| 648 | const char *CTcGenTarg::get_meta_name(int meta_idx) const |
| 649 | { |
| 650 | tc_meta_entry *ent; |
| 651 | |
| 652 | /* find the list entry at the given index */ |
| 653 | for (ent = meta_head_ ; ent != 0 && meta_idx != 0 ; |
| 654 | ent = ent->nxt, --meta_idx) ; |
| 655 | |
| 656 | /* if we didn't find the entry, do nothing */ |
| 657 | if (ent == 0 || meta_idx != 0) |
| 658 | return 0; |
| 659 | |
| 660 | /* return this entry's external name */ |
| 661 | return ent->nm; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Set the symbol for a given metaclass dependency table entry |
| 666 | */ |
| 667 | void CTcGenTarg::set_meta_sym(int meta_idx, class CTcSymMetaclass *sym) |
| 668 | { |
| 669 | tc_meta_entry *ent; |
| 670 | |
| 671 | /* find the list entry at the given index */ |
| 672 | for (ent = meta_head_ ; ent != 0 && meta_idx != 0 ; |
| 673 | ent = ent->nxt, --meta_idx) ; |
| 674 | |
| 675 | /* if we didn't find the entry, do nothing */ |
| 676 | if (ent == 0 || meta_idx != 0) |
| 677 | return; |
| 678 | |
| 679 | /* set this entry's symbol */ |
| 680 | ent->sym = sym; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Add an entry to the function set dependency table |
| 685 | */ |
| 686 | ushort CTcGenTarg::add_fnset(const char *nm, size_t len) |
| 687 | { |
| 688 | tc_fnset_entry *ent; |
| 689 | const char *sl; |
| 690 | size_t sl_len; |
| 691 | ushort idx; |
| 692 | |
| 693 | /* find the version part of the new name, if present */ |
| 694 | for (sl = nm, sl_len = len ; sl_len != 0 && *sl != '/' ; ++sl, --sl_len) ; |
| 695 | |
| 696 | /* look for an existing entry with the same name prefix */ |
| 697 | for (idx = 0, ent = fnset_head_ ; ent != 0 ; ent = ent->nxt, ++idx) |
| 698 | { |
| 699 | char *ent_sl; |
| 700 | |
| 701 | /* find the version part of this entry's name, if present */ |
| 702 | for (ent_sl = ent->nm ; *ent_sl != '\0' && *ent_sl != '/' ; |
| 703 | ++ent_sl) ; |
| 704 | |
| 705 | /* check to see if the prefixes match */ |
| 706 | if (ent_sl - ent->nm == sl - nm |
| 707 | && memcmp(ent->nm, nm, sl - nm) == 0) |
| 708 | { |
| 709 | /* |
| 710 | * This one matches. Keep the one with the higher version |
| 711 | * number. If one has a version number and the other doesn't, |
| 712 | * keep the one with the version number. |
| 713 | */ |
| 714 | if (*ent_sl == '/' && sl_len != 0) |
| 715 | { |
| 716 | /* |
| 717 | * Both have version numbers - keep the higher version. |
| 718 | * Limit the version length to 6 characters plus the |
| 719 | * slash. |
| 720 | */ |
| 721 | if (sl_len > 7) |
| 722 | sl_len = 7; |
| 723 | |
| 724 | /* check if the new version number is higher */ |
| 725 | if (memcmp(sl, ent_sl, sl_len) > 0) |
| 726 | { |
| 727 | /* the new one is higher - copy it over the old one */ |
| 728 | memcpy(ent_sl, sl, sl_len); |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * in any case, we're going to keep the existing entry, so |
| 733 | * we're done - just return the existing entry's index |
| 734 | */ |
| 735 | return idx; |
| 736 | } |
| 737 | else if (*ent_sl == '/') |
| 738 | { |
| 739 | /* |
| 740 | * only the old entry has a version number, so keep it and |
| 741 | * ignore the new definition - this means we're done, so |
| 742 | * just return the existing item's index |
| 743 | */ |
| 744 | return idx; |
| 745 | } |
| 746 | else |
| 747 | { |
| 748 | /* |
| 749 | * Only the new entry has a version number, so store the |
| 750 | * new version number. To do this, simply copy the new |
| 751 | * entry over the old entry, but limit the version number |
| 752 | * field to 7 characters including the slash. |
| 753 | */ |
| 754 | if (sl_len > 7) |
| 755 | len -= (sl_len - 7); |
| 756 | |
| 757 | /* copy the new value */ |
| 758 | memcpy(ent->nm, nm, len); |
| 759 | |
| 760 | /* done - return the existing item's index */ |
| 761 | return idx; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Allocate a new entry for the item. Always allocate space for a |
| 768 | * version number, even if the entry doesn't have a version number - |
| 769 | * if the part from the slash on is 7 characters or more, add nothing, |
| 770 | * else add enough to pad it out to seven characters. |
| 771 | */ |
| 772 | ent = (tc_fnset_entry *)t3malloc(sizeof(tc_fnset_entry) + len |
| 773 | + (sl_len < 7 ? 7 - sl_len : 0)); |
| 774 | if (ent == 0) |
| 775 | err_throw(TCERR_CODEGEN_NO_MEM); |
| 776 | |
| 777 | /* copy the name into the entry */ |
| 778 | memcpy(ent->nm, nm, len); |
| 779 | ent->nm[len] = '\0'; |
| 780 | |
| 781 | /* link the entry in at the end of the list */ |
| 782 | ent->nxt = 0; |
| 783 | if (fnset_tail_ != 0) |
| 784 | fnset_tail_->nxt = ent; |
| 785 | else |
| 786 | fnset_head_ = ent; |
| 787 | fnset_tail_ = ent; |
| 788 | |
| 789 | /* count the entry, returning the index of the entry in the list */ |
| 790 | return fnset_cnt_++; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * get a function set's name given its index |
| 795 | */ |
| 796 | const char *CTcGenTarg::get_fnset_name(int idx) const |
| 797 | { |
| 798 | tc_fnset_entry *ent; |
| 799 | |
| 800 | /* scan the linked list to find the given index */ |
| 801 | for (ent = fnset_head_ ; idx != 0 && ent != 0 ; ent = ent->nxt, --idx) ; |
| 802 | |
| 803 | /* return the one we found */ |
| 804 | return ent->nm; |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Determine if we can skip an opcode because it is unreachable from the |
| 809 | * previous instruction. |
| 810 | */ |
| 811 | int CTcGenTarg::can_skip_op() |
| 812 | { |
| 813 | /* |
| 814 | * if the previous instruction was a return or throw of some kind, |
| 815 | * we can skip any subsequent opcodes until a label is defined |
| 816 | */ |
| 817 | switch(last_op_) |
| 818 | { |
| 819 | case OPC_RET: |
| 820 | case OPC_RETVAL: |
| 821 | case OPC_RETTRUE: |
| 822 | case OPC_RETNIL: |
| 823 | case OPC_THROW: |
| 824 | case OPC_JMP: |
| 825 | case OPC_LRET: |
| 826 | /* it's a return, throw, or jump - this new op is unreachable */ |
| 827 | return TRUE; |
| 828 | |
| 829 | default: |
| 830 | /* this new op is reachable */ |
| 831 | return FALSE; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Remove the last JMP instruction |
| 837 | */ |
| 838 | void CTcGenTarg::remove_last_jmp() |
| 839 | { |
| 840 | /* a JMP instruction is three bytes long, so back up three bytes */ |
| 841 | G_cs->dec_ofs(3); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Add a line record |
| 846 | */ |
| 847 | void CTcGenTarg::add_line_rec(CTcTokFileDesc *file, long linenum) |
| 848 | { |
| 849 | /* include line records only in debug mode */ |
| 850 | if (G_debug) |
| 851 | { |
| 852 | /* |
| 853 | * clear the peephole, to ensure that the line boundary isn't |
| 854 | * blurred by code optimization |
| 855 | */ |
| 856 | clear_peephole(); |
| 857 | |
| 858 | /* add the record to the code stream */ |
| 859 | G_cs->add_line_rec(file, linenum); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Write an opcode to the output stream. We'll watch for certain |
| 865 | * combinations of opcodes being generated, and apply peephole |
| 866 | * optimization when we see sequences that can be collapsed to more |
| 867 | * efficient single instructions. |
| 868 | */ |
| 869 | void CTcGenTarg::write_op(uchar opc) |
| 870 | { |
| 871 | int prv_len; |
| 872 | int op_len; |
| 873 | |
| 874 | /* write the new opcode byte to the output stream */ |
| 875 | G_cs->write((char)opc); |
| 876 | |
| 877 | /* we've only written one byte so far for the current instruction */ |
| 878 | op_len = 1; |
| 879 | |
| 880 | /* presume the previous instruction length is just one byte */ |
| 881 | prv_len = 1; |
| 882 | |
| 883 | /* |
| 884 | * check for pairs of instructions that we can reduce to more |
| 885 | * efficient single instructions |
| 886 | */ |
| 887 | try_combine: |
| 888 | switch(opc) |
| 889 | { |
| 890 | case OPC_DUP: |
| 891 | /* combine GETR0 + DUP -> DUPR0 */ |
| 892 | if (last_op_ == OPC_GETR0) |
| 893 | { |
| 894 | opc = OPC_DUPR0; |
| 895 | goto combine; |
| 896 | } |
| 897 | break; |
| 898 | |
| 899 | case OPC_DISC: |
| 900 | /* combine DISC+DISC -> DISC1<2> */ |
| 901 | if (last_op_ == OPC_DISC) |
| 902 | { |
| 903 | /* |
| 904 | * Switch the old DISC to DISC1<2>. We're deleting a one-byte |
| 905 | * op and replacing a one-byte op with a two-byte op, so we're |
| 906 | * going from two bytes to two bytes total - no net deletion |
| 907 | * (op_len = 0). The new DISC1 is a two-byte instruction |
| 908 | * (prv_len). |
| 909 | */ |
| 910 | op_len = 0; |
| 911 | prv_len = 2; |
| 912 | opc = OPC_DISC1; |
| 913 | |
| 914 | /* write the <2> operand, overwriting the second DISC */ |
| 915 | G_cs->write_at(G_cs->get_ofs() - 1, 2); |
| 916 | |
| 917 | /* go replace the op */ |
| 918 | goto combine; |
| 919 | } |
| 920 | break; |
| 921 | |
| 922 | case OPC_DISC1: |
| 923 | /* combine DISC1<n> + DISC -> DISC1<n+1> */ |
| 924 | if (last_op_ == OPC_DISC |
| 925 | && (uchar)G_cs->get_byte_at(G_cs->get_ofs() - 2) != 0xff) |
| 926 | { |
| 927 | /* up the DISC1 operand */ |
| 928 | G_cs->write_at(G_cs->get_ofs() - 2, |
| 929 | G_cs->get_byte_at(G_cs->get_ofs() - 2) + 1); |
| 930 | |
| 931 | /* keep the DISC1 */ |
| 932 | opc = OPC_DISC1; |
| 933 | prv_len = 2; |
| 934 | |
| 935 | /* apply the combination */ |
| 936 | goto combine; |
| 937 | } |
| 938 | break; |
| 939 | |
| 940 | case OPC_JF: |
| 941 | /* |
| 942 | * if the last instruction was a comparison, we can use the |
| 943 | * opposite compare-and-jump instruction |
| 944 | */ |
| 945 | switch(last_op_) |
| 946 | { |
| 947 | case OPC_NOT: |
| 948 | /* invert the sense of the test */ |
| 949 | opc = OPC_JT; |
| 950 | goto combine; |
| 951 | |
| 952 | combine: |
| 953 | /* |
| 954 | * delete the new opcode we wrote, since we're going to combine |
| 955 | * it with the preceding opcode |
| 956 | */ |
| 957 | G_cs->dec_ofs(op_len); |
| 958 | |
| 959 | /* overwrite the preceding opcode with the new combined opcode */ |
| 960 | G_cs->write_at(G_cs->get_ofs() - prv_len, opc); |
| 961 | |
| 962 | /* roll back our internal peephole */ |
| 963 | last_op_ = second_last_op_; |
| 964 | second_last_op_ = OPC_NOP; |
| 965 | |
| 966 | /* |
| 967 | * we've deleted our own opcode, so the current (most recent) |
| 968 | * instruction in the output stream has the length of the |
| 969 | * current opcode |
| 970 | */ |
| 971 | op_len = prv_len; |
| 972 | |
| 973 | /* presume the previous opcode is one byte again */ |
| 974 | prv_len = 1; |
| 975 | |
| 976 | /* |
| 977 | * go back for another try, since we may be able to do a |
| 978 | * three-way combination (for example, GT/NOT/JT would |
| 979 | * change to GT/JF, which would in turn change to JLE) |
| 980 | */ |
| 981 | goto try_combine; |
| 982 | |
| 983 | case OPC_EQ: |
| 984 | opc = OPC_JNE; |
| 985 | goto combine; |
| 986 | |
| 987 | case OPC_NE: |
| 988 | opc = OPC_JE; |
| 989 | goto combine; |
| 990 | |
| 991 | case OPC_LT: |
| 992 | opc = OPC_JGE; |
| 993 | goto combine; |
| 994 | |
| 995 | case OPC_LE: |
| 996 | opc = OPC_JGT; |
| 997 | goto combine; |
| 998 | |
| 999 | case OPC_GT: |
| 1000 | opc = OPC_JLE; |
| 1001 | goto combine; |
| 1002 | |
| 1003 | case OPC_GE: |
| 1004 | opc = OPC_JLT; |
| 1005 | goto combine; |
| 1006 | |
| 1007 | case OPC_GETR0: |
| 1008 | opc = OPC_JR0F; |
| 1009 | goto combine; |
| 1010 | } |
| 1011 | break; |
| 1012 | |
| 1013 | case OPC_JE: |
| 1014 | /* |
| 1015 | * if we just pushed nil, convert the PUSHNIL + JE to JNIL, since |
| 1016 | * we simply want to jump if a value is nil |
| 1017 | */ |
| 1018 | if (last_op_ == OPC_PUSHNIL) |
| 1019 | { |
| 1020 | /* convert it to a jump-if-nil */ |
| 1021 | opc = OPC_JNIL; |
| 1022 | goto combine; |
| 1023 | } |
| 1024 | break; |
| 1025 | |
| 1026 | case OPC_JNE: |
| 1027 | /* if we just pushed nil, convert to JNOTNIL */ |
| 1028 | if (last_op_ == OPC_PUSHNIL) |
| 1029 | { |
| 1030 | /* convert to jump-if-not-nil */ |
| 1031 | opc = OPC_JNOTNIL; |
| 1032 | goto combine; |
| 1033 | } |
| 1034 | break; |
| 1035 | |
| 1036 | case OPC_JT: |
| 1037 | /* |
| 1038 | * if the last instruction was a comparison, we can use a |
| 1039 | * compare-and-jump instruction |
| 1040 | */ |
| 1041 | switch(last_op_) |
| 1042 | { |
| 1043 | case OPC_NOT: |
| 1044 | /* invert the sense of the test */ |
| 1045 | opc = OPC_JF; |
| 1046 | goto combine; |
| 1047 | |
| 1048 | case OPC_EQ: |
| 1049 | opc = OPC_JE; |
| 1050 | goto combine; |
| 1051 | |
| 1052 | case OPC_NE: |
| 1053 | opc = OPC_JNE; |
| 1054 | goto combine; |
| 1055 | |
| 1056 | case OPC_LT: |
| 1057 | opc = OPC_JLT; |
| 1058 | goto combine; |
| 1059 | |
| 1060 | case OPC_LE: |
| 1061 | opc = OPC_JLE; |
| 1062 | goto combine; |
| 1063 | |
| 1064 | case OPC_GT: |
| 1065 | opc = OPC_JGT; |
| 1066 | goto combine; |
| 1067 | |
| 1068 | case OPC_GE: |
| 1069 | opc = OPC_JGE; |
| 1070 | goto combine; |
| 1071 | |
| 1072 | case OPC_GETR0: |
| 1073 | opc = OPC_JR0T; |
| 1074 | goto combine; |
| 1075 | } |
| 1076 | break; |
| 1077 | |
| 1078 | case OPC_NOT: |
| 1079 | /* |
| 1080 | * If the previous instruction was a comparison test of some |
| 1081 | * kind, we can invert the sense of the test. If the previous |
| 1082 | * instruction was a BOOLIZE op, we can eliminate it entirely, |
| 1083 | * because the NOT will perform the same conversion before |
| 1084 | * negating the value. If the previous was a NOT, we're |
| 1085 | * inverting an inversion; we can simply perform a single |
| 1086 | * BOOLIZE to get the same effect. |
| 1087 | */ |
| 1088 | switch(last_op_) |
| 1089 | { |
| 1090 | case OPC_EQ: |
| 1091 | opc = OPC_NE; |
| 1092 | goto combine; |
| 1093 | |
| 1094 | case OPC_NE: |
| 1095 | opc = OPC_EQ; |
| 1096 | goto combine; |
| 1097 | |
| 1098 | case OPC_GT: |
| 1099 | opc = OPC_LE; |
| 1100 | goto combine; |
| 1101 | |
| 1102 | case OPC_GE: |
| 1103 | opc = OPC_LT; |
| 1104 | goto combine; |
| 1105 | |
| 1106 | case OPC_LT: |
| 1107 | opc = OPC_GE; |
| 1108 | goto combine; |
| 1109 | |
| 1110 | case OPC_LE: |
| 1111 | opc = OPC_GT; |
| 1112 | goto combine; |
| 1113 | |
| 1114 | case OPC_BOOLIZE: |
| 1115 | opc = OPC_NOT; |
| 1116 | goto combine; |
| 1117 | |
| 1118 | case OPC_NOT: |
| 1119 | opc = OPC_BOOLIZE; |
| 1120 | goto combine; |
| 1121 | } |
| 1122 | break; |
| 1123 | |
| 1124 | case OPC_RET: |
| 1125 | /* |
| 1126 | * If we're writing a return instruction immediately after |
| 1127 | * another return instruction, we can skip the additional |
| 1128 | * instruction, since it will never be reached. This case |
| 1129 | * typically arises only when we generate the catch-all RET |
| 1130 | * instruction at the end of a function. |
| 1131 | */ |
| 1132 | switch(last_op_) |
| 1133 | { |
| 1134 | case OPC_RET: |
| 1135 | case OPC_RETVAL: |
| 1136 | case OPC_RETNIL: |
| 1137 | case OPC_RETTRUE: |
| 1138 | /* simply suppress this additional RET instruction */ |
| 1139 | return; |
| 1140 | } |
| 1141 | break; |
| 1142 | |
| 1143 | case OPC_RETNIL: |
| 1144 | /* we don't need to write two RETNIL's in a row */ |
| 1145 | if (last_op_ == OPC_RETNIL) |
| 1146 | return; |
| 1147 | break; |
| 1148 | |
| 1149 | case OPC_RETTRUE: |
| 1150 | /* we don't need to write two RETTRUE's in a row */ |
| 1151 | if (last_op_ == OPC_RETTRUE) |
| 1152 | return; |
| 1153 | break; |
| 1154 | |
| 1155 | case OPC_RETVAL: |
| 1156 | /* check the last opcode */ |
| 1157 | switch(last_op_) |
| 1158 | { |
| 1159 | case OPC_GETR0: |
| 1160 | /* |
| 1161 | * if we just pushed R0 onto the stack, we can compress the |
| 1162 | * GETR0 + RETVAL sequence into a simple RET, since RET leaves |
| 1163 | * the R0 value unchanged |
| 1164 | */ |
| 1165 | opc = OPC_RET; |
| 1166 | goto combine; |
| 1167 | |
| 1168 | case OPC_PUSHNIL: |
| 1169 | /* PUSHNIL + RET can be converted to RETNIL */ |
| 1170 | opc = OPC_RETNIL; |
| 1171 | goto combine; |
| 1172 | |
| 1173 | case OPC_PUSHTRUE: |
| 1174 | /* PUSHTRUE + RET can be converted to RETTRUE */ |
| 1175 | opc = OPC_RETTRUE; |
| 1176 | goto combine; |
| 1177 | } |
| 1178 | break; |
| 1179 | |
| 1180 | case OPC_SETLCL1: |
| 1181 | /* combine GETR0 + SETLCL1 -> SETLCL1R0 */ |
| 1182 | if (last_op_ == OPC_GETR0) |
| 1183 | { |
| 1184 | /* generate a combined SETLCL1R0 */ |
| 1185 | opc = OPC_SETLCL1R0; |
| 1186 | goto combine; |
| 1187 | } |
| 1188 | |
| 1189 | /* combine DUP + SETLCL1 -> GETSETLCL1 */ |
| 1190 | if (last_op_ == OPC_DUP) |
| 1191 | { |
| 1192 | opc = OPC_GETSETLCL1; |
| 1193 | goto combine; |
| 1194 | } |
| 1195 | |
| 1196 | /* combine DUPR0 + SETLCL1 -> GETSETLCL1R0 */ |
| 1197 | if (last_op_ == OPC_DUPR0) |
| 1198 | { |
| 1199 | opc = OPC_GETSETLCL1R0; |
| 1200 | goto combine; |
| 1201 | } |
| 1202 | break; |
| 1203 | |
| 1204 | break; |
| 1205 | |
| 1206 | case OPC_GETPROP: |
| 1207 | /* check the previous instruction for combination possibilities */ |
| 1208 | switch(last_op_) |
| 1209 | { |
| 1210 | case OPC_GETLCL1: |
| 1211 | /* get property of one-byte-addressable local */ |
| 1212 | opc = OPC_GETPROPLCL1; |
| 1213 | |
| 1214 | /* overwrite the preceding two-byte instruction */ |
| 1215 | prv_len = 2; |
| 1216 | goto combine; |
| 1217 | |
| 1218 | case OPC_GETLCLN0: |
| 1219 | case OPC_GETLCLN1: |
| 1220 | case OPC_GETLCLN2: |
| 1221 | case OPC_GETLCLN3: |
| 1222 | case OPC_GETLCLN4: |
| 1223 | case OPC_GETLCLN5: |
| 1224 | /* add a byte to pad things to the same length as a GETLCL1 */ |
| 1225 | G_cs->write(0); |
| 1226 | |
| 1227 | /* write the local number operand of the GETPROPLCL1 */ |
| 1228 | G_cs->write_at(G_cs->get_ofs() - 2, last_op_ - OPC_GETLCLN0); |
| 1229 | |
| 1230 | /* get property of one-byte-addressable local */ |
| 1231 | opc = OPC_GETPROPLCL1; |
| 1232 | |
| 1233 | /* overwrite the preceding now-two-byte instruction */ |
| 1234 | prv_len = 2; |
| 1235 | goto combine; |
| 1236 | |
| 1237 | case OPC_GETR0: |
| 1238 | /* get property of R0 */ |
| 1239 | opc = OPC_GETPROPR0; |
| 1240 | goto combine; |
| 1241 | } |
| 1242 | break; |
| 1243 | |
| 1244 | case OPC_CALLPROP: |
| 1245 | /* check the previous instruction */ |
| 1246 | switch(last_op_) |
| 1247 | { |
| 1248 | case OPC_GETR0: |
| 1249 | /* call property of R0 */ |
| 1250 | opc = OPC_CALLPROPR0; |
| 1251 | goto combine; |
| 1252 | } |
| 1253 | break; |
| 1254 | |
| 1255 | case OPC_INDEX: |
| 1256 | /* we can combine small integer constants with INDEX */ |
| 1257 | switch(last_op_) |
| 1258 | { |
| 1259 | case OPC_PUSH_0: |
| 1260 | case OPC_PUSH_1: |
| 1261 | /* |
| 1262 | * We can combine these into IDXINT8, but we must write an |
| 1263 | * extra byte for the index value. Go back and plug in the |
| 1264 | * extra index value byte, and add another byte at the end of |
| 1265 | * the stream to compensate for the insertion. (We're just |
| 1266 | * going to remove and overwrite everything after the inserted |
| 1267 | * byte, so don't bother actually fixing up that part with real |
| 1268 | * data; we merely need to make sure we have the right number |
| 1269 | * of bytes in the stream.) |
| 1270 | */ |
| 1271 | G_cs->write_at(G_cs->get_ofs() - 1, |
| 1272 | last_op_ == OPC_PUSH_0 ? 0 : 1); |
| 1273 | G_cs->write(0); |
| 1274 | |
| 1275 | /* combine the instructions */ |
| 1276 | opc = OPC_IDXINT8; |
| 1277 | prv_len = 2; |
| 1278 | goto combine; |
| 1279 | |
| 1280 | case OPC_PUSHINT8: |
| 1281 | /* combine the PUSHINT8 + INDEX into IDXINT8 */ |
| 1282 | opc = OPC_IDXINT8; |
| 1283 | prv_len = 2; |
| 1284 | goto combine; |
| 1285 | } |
| 1286 | break; |
| 1287 | |
| 1288 | case OPC_IDXINT8: |
| 1289 | /* we can replace GETLCL1 + IDXINT8 with IDXLCL1INT8 */ |
| 1290 | if (last_op_ == OPC_GETLCL1) |
| 1291 | { |
| 1292 | /* add the index operand at the second byte after the GETLCL1 */ |
| 1293 | uchar idx = G_cs->get_byte_at(G_cs->get_ofs() - op_len + 1); |
| 1294 | G_cs->write_at(G_cs->get_ofs() - op_len, idx); |
| 1295 | |
| 1296 | /* |
| 1297 | * we're going from GETLCL1 <l> IDXINT8 <i> (4 bytes) to |
| 1298 | * IDXLCL1INT8 <l> <i> (3 bytes), for a net deletion of one |
| 1299 | * byte - but that's only if we've already written the <i> |
| 1300 | * operand, so the net deletion is actually op_len-1 |
| 1301 | */ |
| 1302 | op_len -= 1; |
| 1303 | |
| 1304 | /* go back and combine into what's now a three-byte opcode */ |
| 1305 | opc = OPC_IDXLCL1INT8; |
| 1306 | prv_len = 3; |
| 1307 | goto combine; |
| 1308 | } |
| 1309 | if (last_op_ >= OPC_GETLCLN0 && last_op_ <= OPC_GETLCLN5) |
| 1310 | { |
| 1311 | /* write the local# at the byte after the GETLCLNx */ |
| 1312 | G_cs->write_at(G_cs->get_ofs() - op_len, last_op_ - OPC_GETLCLN0); |
| 1313 | |
| 1314 | /* |
| 1315 | * if we haven't already written the IDXINT8 operand, write a |
| 1316 | * placeholder for it, since 'combine:' can't add bytes |
| 1317 | */ |
| 1318 | if (op_len == 1) |
| 1319 | G_cs->write(0); |
| 1320 | |
| 1321 | /* |
| 1322 | * We're going from GETLCLNx IDXINT8 <i> (3 bytes) to |
| 1323 | * IDXLCL1INT8 <l> <i> (3 bytes), so there's no net deletion. |
| 1324 | */ |
| 1325 | op_len = 0; |
| 1326 | |
| 1327 | /* go back and combine into the three-byte index-by-local op */ |
| 1328 | opc = OPC_IDXLCL1INT8; |
| 1329 | prv_len = 3; |
| 1330 | goto combine; |
| 1331 | } |
| 1332 | break; |
| 1333 | |
| 1334 | case OPC_SETIND: |
| 1335 | /* we can replace GETLCL1 + <small int> + SETIND with SETINDLCL1I8 */ |
| 1336 | if (second_last_op_ == OPC_GETLCL1 |
| 1337 | || (second_last_op_ >= OPC_GETLCLN0 |
| 1338 | && second_last_op_ <= OPC_GETLCLN5)) |
| 1339 | { |
| 1340 | int idx, lcl; |
| 1341 | int getlcl_len, push_len; |
| 1342 | |
| 1343 | /* get the local number and the GETLCL instruction length */ |
| 1344 | if (second_last_op_ == OPC_GETLCL1) |
| 1345 | { |
| 1346 | /* it's GETLCL1 <n> - two bytes */ |
| 1347 | getlcl_len = 2; |
| 1348 | } |
| 1349 | else |
| 1350 | { |
| 1351 | /* no operands - the local is encoded in the opcode itself */ |
| 1352 | lcl = second_last_op_ - OPC_GETLCLN0; |
| 1353 | getlcl_len = 1; |
| 1354 | } |
| 1355 | |
| 1356 | /* check the middle opcode for an int push */ |
| 1357 | switch(last_op_) |
| 1358 | { |
| 1359 | case OPC_PUSHINT8: |
| 1360 | /* get the index from the PUSHINT8 */ |
| 1361 | idx = (int)(uchar)G_cs->get_byte_at(G_cs->get_ofs() - 2); |
| 1362 | push_len = 2; |
| 1363 | |
| 1364 | /* get the local, if it's a GETLCL1 */ |
| 1365 | if (second_last_op_ == OPC_GETLCL1) |
| 1366 | lcl = G_cs->get_byte_at(G_cs->get_ofs() - push_len - 2); |
| 1367 | |
| 1368 | /* |
| 1369 | * PUSHINT8 pushes a signed value, but SETINDLCL1I8 uses an |
| 1370 | * unsigned value, so make sure the PUSHINT8 value is in |
| 1371 | * range |
| 1372 | */ |
| 1373 | if (idx < 127) |
| 1374 | goto combine_setind; |
| 1375 | |
| 1376 | /* not combinable */ |
| 1377 | break; |
| 1378 | |
| 1379 | case OPC_PUSHINT: |
| 1380 | /* get the int value */ |
| 1381 | idx = G_cs->read4_at(G_cs->get_ofs() - 5); |
| 1382 | push_len = 5; |
| 1383 | |
| 1384 | /* get the local, if it's a GETLCL1 */ |
| 1385 | if (second_last_op_ == OPC_GETLCL1) |
| 1386 | lcl = G_cs->get_byte_at(G_cs->get_ofs() - push_len - 2); |
| 1387 | |
| 1388 | /* if the index is 0-255, we can combine the instructions */ |
| 1389 | if (idx >= 0 && idx <= 255) |
| 1390 | goto combine_setind; |
| 1391 | |
| 1392 | /* not combinable */ |
| 1393 | break; |
| 1394 | |
| 1395 | case OPC_PUSH_0: |
| 1396 | case OPC_PUSH_1: |
| 1397 | /* it's a one-byte PUSH */ |
| 1398 | idx = last_op_ - OPC_PUSH_0; |
| 1399 | push_len = 1; |
| 1400 | |
| 1401 | /* get the local, if it's a GETLCL1 */ |
| 1402 | if (second_last_op_ == OPC_GETLCL1) |
| 1403 | lcl = G_cs->get_byte_at(G_cs->get_ofs() - push_len - 2); |
| 1404 | |
| 1405 | combine_setind: |
| 1406 | /* write the operands of the SETINDLCLI8 */ |
| 1407 | G_cs->write_at( |
| 1408 | G_cs->get_ofs() - getlcl_len - push_len, (uchar)lcl); |
| 1409 | G_cs->write_at( |
| 1410 | G_cs->get_ofs() - getlcl_len - push_len + 1, (uchar)idx); |
| 1411 | |
| 1412 | /* |
| 1413 | * Go back and do the combine. Remove GETLCL<var>, |
| 1414 | * PUSH<idx>, and SETIND (getlcl_len + push_len + 1 bytes), |
| 1415 | * and replace them with SETINDLCL1I8<var><idx> (3 bytes). |
| 1416 | * The net deletion goes in op_len, and the new length goes |
| 1417 | * in prv_len. Also, since we're combining three |
| 1418 | * instructions into one, we're losing our second-to-last |
| 1419 | * opcode. |
| 1420 | */ |
| 1421 | opc = OPC_SETINDLCL1I8; |
| 1422 | second_last_op_ = OPC_NOP; |
| 1423 | op_len = getlcl_len + push_len - 2; |
| 1424 | prv_len = 3; |
| 1425 | goto combine; |
| 1426 | } |
| 1427 | } |
| 1428 | break; |
| 1429 | |
| 1430 | default: |
| 1431 | /* write this instruction as-is */ |
| 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | /* remember the last opcode we wrote */ |
| 1436 | second_last_op_ = last_op_; |
| 1437 | last_op_ = opc; |
| 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * Write a CALLPROP instruction, combining with preceding opcodes if |
| 1442 | * possible. |
| 1443 | */ |
| 1444 | void CTcGenTarg::write_callprop(int argc, int varargs, vm_prop_id_t prop) |
| 1445 | { |
| 1446 | /* |
| 1447 | * if the previous instruction was GETLCL1, combine it with the |
| 1448 | * CALLPROP to form a single CALLPROPLCL1 instruction |
| 1449 | */ |
| 1450 | if (last_op_ == OPC_GETLCL1 |
| 1451 | || (last_op_ >= OPC_GETLCLN0 && last_op_ <= OPC_GETLCLN5)) |
| 1452 | { |
| 1453 | uchar lcl; |
| 1454 | |
| 1455 | /* if it was a GETLCLNx, expand it out to a GETLCL1 */ |
| 1456 | if (last_op_ == OPC_GETLCL1) |
| 1457 | { |
| 1458 | /* it's already a GETLCL1 - just get the index */ |
| 1459 | lcl = G_cs->get_byte_at(G_cs->get_ofs() - 1); |
| 1460 | |
| 1461 | /* back up and delete the GETLCL1 instruction */ |
| 1462 | G_cs->dec_ofs(2); |
| 1463 | } |
| 1464 | else |
| 1465 | { |
| 1466 | /* it's a GETLCLNx - infer the local number from the opcode */ |
| 1467 | lcl = last_op_ - OPC_GETLCLN0; |
| 1468 | |
| 1469 | /* delete the instruction */ |
| 1470 | G_cs->dec_ofs(1); |
| 1471 | } |
| 1472 | |
| 1473 | /* roll back the peephole for the instruction deletion */ |
| 1474 | last_op_ = second_last_op_; |
| 1475 | second_last_op_ = OPC_NOP; |
| 1476 | |
| 1477 | /* write the varargs modifier if appropriate */ |
| 1478 | if (varargs) |
| 1479 | write_op(OPC_VARARGC); |
| 1480 | |
| 1481 | /* write the CALLPROPLCL1 */ |
| 1482 | write_op(OPC_CALLPROPLCL1); |
| 1483 | G_cs->write((char)argc); |
| 1484 | G_cs->write(lcl); |
| 1485 | G_cs->write_prop_id(prop); |
| 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | /* generate the varargs modifier if appropriate */ |
| 1490 | if (varargs) |
| 1491 | write_op(OPC_VARARGC); |
| 1492 | |
| 1493 | /* we have arguments - generate a CALLPROP */ |
| 1494 | write_op(OPC_CALLPROP); |
| 1495 | G_cs->write((char)argc); |
| 1496 | G_cs->write_prop_id(prop); |
| 1497 | } |
| 1498 | |
| 1499 | /* callprop removes arguments and the object */ |
| 1500 | note_pop(argc + 1); |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * Note a string's length |
| 1505 | */ |
| 1506 | void CTcGenTarg::note_str(size_t len) |
| 1507 | { |
| 1508 | /* if it's the longest so far, remember it */ |
| 1509 | if (len > max_str_len_) |
| 1510 | { |
| 1511 | /* |
| 1512 | * flag an warning the length plus overhead would exceed 32k |
| 1513 | * (only do this the first time we cross this limit) |
| 1514 | */ |
| 1515 | if (len > (32*1024 - VMB_LEN) |
| 1516 | && max_str_len_ <= (32*1024 - VMB_LEN)) |
| 1517 | G_tok->log_warning(TCERR_CONST_POOL_OVER_32K); |
| 1518 | |
| 1519 | /* remember the length */ |
| 1520 | max_str_len_ = len; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * note a list's length |
| 1526 | */ |
| 1527 | void CTcGenTarg::note_list(size_t element_count) |
| 1528 | { |
| 1529 | /* if it's the longest list so far, remember it */ |
| 1530 | if (element_count > max_list_cnt_) |
| 1531 | { |
| 1532 | /* flag a warning if the stored length would be over 32k */ |
| 1533 | if (element_count > ((32*1024 - VMB_LEN) / VMB_DATAHOLDER) |
| 1534 | && max_list_cnt_ <= ((32*1024 - VMB_LEN) / VMB_DATAHOLDER)) |
| 1535 | G_tok->log_warning(TCERR_CONST_POOL_OVER_32K); |
| 1536 | |
| 1537 | /* remember the length */ |
| 1538 | max_list_cnt_ = element_count; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Note a bytecode block length |
| 1544 | */ |
| 1545 | void CTcGenTarg::note_bytecode(ulong len) |
| 1546 | { |
| 1547 | /* if it's the longest bytecode block yet, remember it */ |
| 1548 | if (len > max_bytecode_len_) |
| 1549 | { |
| 1550 | /* flag a warning the first time we go over 32k */ |
| 1551 | if (len >= 32*1024 && max_bytecode_len_ < 32*1024) |
| 1552 | G_tok->log_warning(TCERR_CODE_POOL_OVER_32K); |
| 1553 | |
| 1554 | /* remember the new length */ |
| 1555 | max_bytecode_len_ = len; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Hash table entry for an interned string. This keeps track of a string |
| 1561 | * that we've previously generated to the data segment, for reuse if we |
| 1562 | * need to generate another copy of the same string literal. |
| 1563 | */ |
| 1564 | class CInternEntry: public CVmHashEntryCS |
| 1565 | { |
| 1566 | public: |
| 1567 | CInternEntry(const char *str, size_t len, CTcStreamAnchor *anchor) |
| 1568 | : CVmHashEntryCS(str, len, FALSE) |
| 1569 | { |
| 1570 | anchor_ = anchor; |
| 1571 | } |
| 1572 | |
| 1573 | /* the string's fixup list anchor */ |
| 1574 | CTcStreamAnchor *anchor_; |
| 1575 | }; |
| 1576 | |
| 1577 | /* |
| 1578 | * Add a string to the constant pool |
| 1579 | */ |
| 1580 | void CTcGenTarg::add_const_str(const char *str, size_t len, |
| 1581 | CTcDataStream *ds, ulong ofs) |
| 1582 | { |
| 1583 | /* |
| 1584 | * If it's a short string, look to see if we've already written it. If |
| 1585 | * so, we can reuse the previous copy. The odds of a string being |
| 1586 | * duplicated generally decrease rapidly with the length of the string, |
| 1587 | * and the cost of checking for an old copy increases, so don't bother |
| 1588 | * if the string is over a threshold length. |
| 1589 | */ |
| 1590 | const size_t intern_threshold = 40; |
| 1591 | if (len < intern_threshold) |
| 1592 | { |
| 1593 | /* look for an old copy */ |
| 1594 | CInternEntry *e = (CInternEntry *)strtab_->find(str, len); |
| 1595 | |
| 1596 | /* |
| 1597 | * If we found it, simply add a fixup for the new use to the |
| 1598 | * existing fixup list. This will cause the caller to point the |
| 1599 | * new literal to the existing copy, without generating a duplicate |
| 1600 | * copy in the data segment. |
| 1601 | */ |
| 1602 | if (e != 0) |
| 1603 | { |
| 1604 | /* |
| 1605 | * add a fixup to the existing string's fixup list for the |
| 1606 | * caller's new reference |
| 1607 | */ |
| 1608 | CTcAbsFixup::add_abs_fixup(e->anchor_->fixup_list_head_, ds, ofs); |
| 1609 | |
| 1610 | /* we're done */ |
| 1611 | return; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Add an anchor for the item, and add a fixup for the reference |
| 1617 | * from ds@ofs to the item. |
| 1618 | */ |
| 1619 | CTcStreamAnchor *anchor = G_ds->add_anchor(0, 0); |
| 1620 | CTcAbsFixup::add_abs_fixup(anchor->fixup_list_head_, ds, ofs); |
| 1621 | |
| 1622 | /* |
| 1623 | * If this is a short string, add it to our list of interned strings. |
| 1624 | * This will let us reuse this copy of the string in the data segment |
| 1625 | * if we need to generate another literal later with identical |
| 1626 | * contents. |
| 1627 | */ |
| 1628 | if (len < intern_threshold) |
| 1629 | strtab_->add(new CInternEntry(str, len, anchor)); |
| 1630 | |
| 1631 | /* write the length prefix */ |
| 1632 | G_ds->write2(len); |
| 1633 | |
| 1634 | /* write the string bytes */ |
| 1635 | G_ds->write(str, len); |
| 1636 | |
| 1637 | /* note the length of the string stored */ |
| 1638 | note_str(len); |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * Add a list to the constant pool |
| 1643 | */ |
| 1644 | void CTcGenTarg::add_const_list(CTPNList *lst, |
| 1645 | CTcDataStream *ds, ulong ofs) |
| 1646 | { |
| 1647 | int i; |
| 1648 | CTPNListEle *cur; |
| 1649 | ulong dst; |
| 1650 | CTcStreamAnchor *anchor; |
| 1651 | |
| 1652 | /* |
| 1653 | * Add an anchor for the item, and add a fixup for the reference |
| 1654 | * from ds@ofs to the item. |
| 1655 | */ |
| 1656 | anchor = G_ds->add_anchor(0, 0); |
| 1657 | CTcAbsFixup::add_abs_fixup(anchor->fixup_list_head_, ds, ofs); |
| 1658 | |
| 1659 | /* |
| 1660 | * Reserve space for the list. We need to do this first, because |
| 1661 | * the list might contain elements which themselves must be written |
| 1662 | * to the data stream; we must therefore reserve space for the |
| 1663 | * entire list before we start writing its elements. |
| 1664 | */ |
| 1665 | dst = G_ds->reserve(2 + lst->get_count()*VMB_DATAHOLDER); |
| 1666 | |
| 1667 | /* set the length prefix */ |
| 1668 | G_ds->write2_at(dst, lst->get_count()); |
| 1669 | dst += 2; |
| 1670 | |
| 1671 | /* store the elements */ |
| 1672 | for (i = 0, cur = lst->get_head() ; cur != 0 ; |
| 1673 | ++i, cur = cur->get_next(), dst += VMB_DATAHOLDER) |
| 1674 | { |
| 1675 | CTcConstVal *ele; |
| 1676 | |
| 1677 | /* get this element */ |
| 1678 | ele = cur->get_expr()->get_const_val(); |
| 1679 | |
| 1680 | /* write it to the element buffer */ |
| 1681 | write_const_as_dh(G_ds, dst, ele); |
| 1682 | } |
| 1683 | |
| 1684 | /* make sure the list wasn't corrupted */ |
| 1685 | if (i != lst->get_count()) |
| 1686 | G_tok->throw_internal_error(TCERR_CORRUPT_LIST); |
| 1687 | |
| 1688 | /* note the number of elements in the list */ |
| 1689 | note_list(lst->get_count()); |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Generate a BigNumber object |
| 1694 | */ |
| 1695 | vm_obj_id_t CTcGenTarg::gen_bignum_obj( |
| 1696 | const char *txt, size_t len, int promoted) |
| 1697 | { |
| 1698 | /* if the value was promoted from integer due to overflow, warn */ |
| 1699 | if (promoted) |
| 1700 | G_tok->log_warning(TCERR_INT_CONST_OV); |
| 1701 | |
| 1702 | /* write to BigNumber object stream */ |
| 1703 | CTcDataStream *str = G_bignum_stream; |
| 1704 | |
| 1705 | /* generate a new object ID for the BigNumber */ |
| 1706 | vm_obj_id_t id = new_obj_id(); |
| 1707 | |
| 1708 | /* |
| 1709 | * add the object ID to the non-symbol object list - this is |
| 1710 | * necessary to ensure that the object ID is fixed up during linking |
| 1711 | */ |
| 1712 | G_prs->add_nonsym_obj(id); |
| 1713 | |
| 1714 | /* |
| 1715 | * Get the BigNumber representation, inferring the radix. The |
| 1716 | * tokenizer can pass us integer strings with '0x' (hex) or '0' (octal) |
| 1717 | * prefixes when the numbers they contain are too big for the 32-bit |
| 1718 | * signed integer type. Floating-point values never have a radix |
| 1719 | * prefix. |
| 1720 | */ |
| 1721 | size_t bnlen; |
| 1722 | char *bn = CVmObjBigNum::parse_str_alo(bnlen, txt, len, 0); |
| 1723 | |
| 1724 | /* write the OBJS header - object ID plus byte count for metaclass data */ |
| 1725 | str->write_obj_id(id); |
| 1726 | str->write2(bnlen); |
| 1727 | |
| 1728 | /* write the BigNumber contents */ |
| 1729 | str->write(bn, bnlen); |
| 1730 | |
| 1731 | /* free the BigNumber value */ |
| 1732 | delete [] bn; |
| 1733 | |
| 1734 | /* return the new object ID */ |
| 1735 | return id; |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * Generate a RexPattern object |
| 1740 | */ |
| 1741 | vm_obj_id_t CTcGenTarg::gen_rexpat_obj(const char *txt, size_t len) |
| 1742 | { |
| 1743 | /* write to RexPattern object stream */ |
| 1744 | CTcDataStream *rs = G_rexpat_stream; |
| 1745 | |
| 1746 | /* generate a new object ID for the RexPattern object */ |
| 1747 | vm_obj_id_t id = new_obj_id(); |
| 1748 | |
| 1749 | /* add the object ID to the non-symbol object list */ |
| 1750 | G_prs->add_nonsym_obj(id); |
| 1751 | |
| 1752 | /* |
| 1753 | * write the OBJS header - object ID plus byte count for metaclass data |
| 1754 | * (a DATAHOLDER pointing to the constant string) |
| 1755 | */ |
| 1756 | rs->write_obj_id(id); |
| 1757 | rs->write2(VMB_DATAHOLDER); |
| 1758 | |
| 1759 | /* |
| 1760 | * Add the source text as an ordinary constant string. Generate its |
| 1761 | * fixup in the RexPattern stream, in the DATAHOLDER we're about to |
| 1762 | * write. |
| 1763 | */ |
| 1764 | add_const_str(txt, len, rs, rs->get_ofs() + 1); |
| 1765 | |
| 1766 | /* |
| 1767 | * Set up the constant string value. Use zero as the pool offset, |
| 1768 | * since the fixup for the constant string will fill this in when the |
| 1769 | * pool layout is finalized. |
| 1770 | */ |
| 1771 | vm_val_t val; |
| 1772 | val.set_sstring(0); |
| 1773 | |
| 1774 | /* write the RexPattern data (a DATAHOLDER for the const string) */ |
| 1775 | char buf[VMB_DATAHOLDER]; |
| 1776 | vmb_put_dh(buf, &val); |
| 1777 | rs->write(buf, VMB_DATAHOLDER); |
| 1778 | |
| 1779 | /* return the new object ID */ |
| 1780 | return id; |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * Convert a constant value from a CTcConstVal (compiler internal |
| 1785 | * representation) to a vm_val_t (interpreter representation). |
| 1786 | */ |
| 1787 | void CTcGenTarg::write_const_as_dh(CTcDataStream *ds, ulong ofs, |
| 1788 | const CTcConstVal *src) |
| 1789 | { |
| 1790 | vm_val_t val; |
| 1791 | char buf[VMB_DATAHOLDER]; |
| 1792 | |
| 1793 | /* convert according to the value's type */ |
| 1794 | switch(src->get_type()) |
| 1795 | { |
| 1796 | case TC_CVT_NIL: |
| 1797 | val.set_nil(); |
| 1798 | break; |
| 1799 | |
| 1800 | case TC_CVT_TRUE: |
| 1801 | val.set_true(); |
| 1802 | break; |
| 1803 | |
| 1804 | case TC_CVT_INT: |
| 1805 | val.set_int(src->get_val_int()); |
| 1806 | break; |
| 1807 | |
| 1808 | case TC_CVT_FLOAT: |
| 1809 | /* generate the BigNumber object */ |
| 1810 | val.set_obj(gen_bignum_obj(src->get_val_float(), |
| 1811 | src->get_val_float_len(), |
| 1812 | src->is_promoted())); |
| 1813 | |
| 1814 | /* add a fixup for the object ID */ |
| 1815 | if (G_keep_objfixups) |
| 1816 | CTcIdFixup::add_fixup(&G_objfixup, ds, ofs + 1, val.val.obj); |
| 1817 | break; |
| 1818 | |
| 1819 | case TC_CVT_SSTR: |
| 1820 | /* |
| 1821 | * Store the string in the constant pool. Note that our fixup |
| 1822 | * is at the destination stream offset plus one, since the |
| 1823 | * DATAHOLDER has the type byte followed by the offset value. |
| 1824 | */ |
| 1825 | add_const_str(src->get_val_str(), src->get_val_str_len(), |
| 1826 | ds, ofs + 1); |
| 1827 | |
| 1828 | /* |
| 1829 | * set the offset to zero for now - the fixup that |
| 1830 | * add_const_str() generates will take care of supplying the |
| 1831 | * real value |
| 1832 | */ |
| 1833 | val.set_sstring(0); |
| 1834 | break; |
| 1835 | |
| 1836 | |
| 1837 | case TC_CVT_RESTR: |
| 1838 | /* generate the RexPattern object */ |
| 1839 | val.set_obj(gen_rexpat_obj(src->get_val_str(), |
| 1840 | src->get_val_str_len())); |
| 1841 | |
| 1842 | /* add a fixup for the object ID */ |
| 1843 | if (G_keep_objfixups) |
| 1844 | CTcIdFixup::add_fixup(&G_objfixup, ds, ofs + 1, val.val.obj); |
| 1845 | break; |
| 1846 | |
| 1847 | case TC_CVT_LIST: |
| 1848 | /* |
| 1849 | * Store the sublist in the constant pool. Our fixup is at the |
| 1850 | * destination stream offset plus one, since the DATAHOLDER has |
| 1851 | * the type byte followed by the offset value. |
| 1852 | */ |
| 1853 | add_const_list(src->get_val_list(), ds, ofs + 1); |
| 1854 | |
| 1855 | /* |
| 1856 | * set the offset to zero for now - the fixup that |
| 1857 | * add_const_list() generates will take care of supplying the |
| 1858 | * real value |
| 1859 | */ |
| 1860 | val.set_list(0); |
| 1861 | break; |
| 1862 | |
| 1863 | case TC_CVT_OBJ: |
| 1864 | /* set the object ID value */ |
| 1865 | val.set_obj((vm_obj_id_t)src->get_val_obj()); |
| 1866 | |
| 1867 | /* |
| 1868 | * add a fixup (at the current offset plus one, for the type |
| 1869 | * byte) if we're keeping object ID fixups |
| 1870 | */ |
| 1871 | if (G_keep_objfixups) |
| 1872 | CTcIdFixup::add_fixup(&G_objfixup, ds, ofs + 1, |
| 1873 | src->get_val_obj()); |
| 1874 | break; |
| 1875 | |
| 1876 | case TC_CVT_ENUM: |
| 1877 | /* set the enum value */ |
| 1878 | val.set_enum(src->get_val_enum()); |
| 1879 | |
| 1880 | /* add a fixup */ |
| 1881 | if (G_keep_enumfixups) |
| 1882 | CTcIdFixup::add_fixup(&G_enumfixup, ds, ofs + 1, |
| 1883 | src->get_val_enum()); |
| 1884 | break; |
| 1885 | |
| 1886 | case TC_CVT_PROP: |
| 1887 | /* set the property ID value */ |
| 1888 | val.set_propid((vm_prop_id_t)src->get_val_prop()); |
| 1889 | |
| 1890 | /* |
| 1891 | * add a fixup (at the current offset plus one, for the type |
| 1892 | * byte) if we're keeping property ID fixups |
| 1893 | */ |
| 1894 | if (G_keep_propfixups) |
| 1895 | CTcIdFixup::add_fixup(&G_propfixup, ds, ofs + 1, |
| 1896 | src->get_val_prop()); |
| 1897 | break; |
| 1898 | |
| 1899 | case TC_CVT_FUNCPTR: |
| 1900 | /* |
| 1901 | * use a placeholder value of zero for now - a function's final |
| 1902 | * address is never known until after all code generation has |
| 1903 | * been completed (the fixup will take care of supplying the |
| 1904 | * correct value when the time comes) |
| 1905 | */ |
| 1906 | val.set_fnptr(0); |
| 1907 | |
| 1908 | /* |
| 1909 | * Add a fixup. The fixup is at the destination stream offset |
| 1910 | * plus one, because the DATAHOLDER has a type byte followed by |
| 1911 | * the function pointer value. |
| 1912 | */ |
| 1913 | src->get_val_funcptr_sym()->add_abs_fixup(ds, ofs + 1); |
| 1914 | break; |
| 1915 | |
| 1916 | case TC_CVT_ANONFUNCPTR: |
| 1917 | /* use a placeholder of zero for now, until we fix up the pointer */ |
| 1918 | val.set_fnptr(0); |
| 1919 | |
| 1920 | /* add a fixup for the code body */ |
| 1921 | src->get_val_anon_func_ptr()->add_abs_fixup(ds, ofs + 1); |
| 1922 | break; |
| 1923 | |
| 1924 | case TC_CVT_VOCAB_LIST: |
| 1925 | /* |
| 1926 | * it's an internal vocabulary list type - this is used as a |
| 1927 | * placeholder only, and will be replaced during linking with an |
| 1928 | * actual vocabulary string list |
| 1929 | */ |
| 1930 | val.typ = VM_VOCAB_LIST; |
| 1931 | break; |
| 1932 | |
| 1933 | case TC_CVT_UNK: |
| 1934 | case TC_CVT_BIFPTR: |
| 1935 | /* unknown - ignore it */ |
| 1936 | break; |
| 1937 | } |
| 1938 | |
| 1939 | /* write the vm_val_t in DATA_HOLDER format into the stream */ |
| 1940 | vmb_put_dh(buf, &val); |
| 1941 | ds->write_at(ofs, buf, VMB_DATAHOLDER); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * Write a DATAHOLDER at the current offset in a stream |
| 1946 | */ |
| 1947 | void CTcGenTarg::write_const_as_dh(CTcDataStream *ds, |
| 1948 | const CTcConstVal *src) |
| 1949 | { |
| 1950 | /* write to the current stream offset */ |
| 1951 | write_const_as_dh(ds, ds->get_ofs(), src); |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * Notify that parsing is finished |
| 1956 | */ |
| 1957 | void CTcGenTarg::parsing_done() |
| 1958 | { |
| 1959 | /* nothing special to do */ |
| 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | /* |
| 1964 | * notify the code generator that we're replacing an object |
| 1965 | */ |
| 1966 | void CTcGenTarg::notify_replace_object(ulong stream_ofs) |
| 1967 | { |
| 1968 | uint flags; |
| 1969 | |
| 1970 | /* set the 'replaced' flag in the flags prefix */ |
| 1971 | flags = G_os->read2_at(stream_ofs); |
| 1972 | flags |= TCT3_OBJ_REPLACED; |
| 1973 | G_os->write2_at(stream_ofs, flags); |
| 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | /* |
| 1978 | * Set the starting offset of the current method |
| 1979 | */ |
| 1980 | void CTcGenTarg::set_method_ofs(ulong ofs) |
| 1981 | { |
| 1982 | /* tell the exception table object about it */ |
| 1983 | get_exc_table()->set_method_ofs(ofs); |
| 1984 | |
| 1985 | /* remember it in the code stream */ |
| 1986 | G_cs->set_method_ofs(ofs); |
| 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | |
| 1991 | /* ------------------------------------------------------------------------ */ |
| 1992 | /* |
| 1993 | * Method header generator |
| 1994 | */ |
| 1995 | |
| 1996 | /* |
| 1997 | * Open a method |
| 1998 | */ |
| 1999 | void CTcGenTarg::open_method( |
| 2000 | CTcCodeStream *stream, |
| 2001 | CTcSymbol *fixup_owner_sym, CTcAbsFixup **fixup_list_head, |
| 2002 | CTPNCodeBody *code_body, CTcPrsSymtab *goto_tab, |
| 2003 | int argc, int opt_argc, int varargs, |
| 2004 | int is_constructor, int is_op_overload, int is_self_available, |
| 2005 | tct3_method_gen_ctx *ctx) |
| 2006 | { |
| 2007 | /* set the code stream as the current code generator output stream */ |
| 2008 | G_cs = ctx->stream = stream; |
| 2009 | |
| 2010 | /* set the method properties in the code generator */ |
| 2011 | set_in_constructor(is_constructor); |
| 2012 | set_in_op_overload(is_op_overload); |
| 2013 | stream->set_self_available(is_self_available); |
| 2014 | |
| 2015 | /* we obviously can't combine any past instructions */ |
| 2016 | clear_peephole(); |
| 2017 | |
| 2018 | /* clear the old line records */ |
| 2019 | stream->clear_line_recs(); |
| 2020 | |
| 2021 | /* clear the old frame list */ |
| 2022 | stream->clear_local_frames(); |
| 2023 | |
| 2024 | /* clear the old exception table */ |
| 2025 | get_exc_table()->clear_table(); |
| 2026 | |
| 2027 | /* reset the stack depth counters */ |
| 2028 | reset_sp_depth(); |
| 2029 | |
| 2030 | /* there are no enclosing 'switch' or block statements yet */ |
| 2031 | stream->set_switch(0); |
| 2032 | stream->set_enclosing(0); |
| 2033 | |
| 2034 | /* |
| 2035 | * remember where the method header starts - we'll need to come back |
| 2036 | * and fix up some placeholder entries in "Close" |
| 2037 | */ |
| 2038 | ctx->method_ofs = stream->get_ofs(); |
| 2039 | |
| 2040 | /* set the method start offset in the code generator */ |
| 2041 | set_method_ofs(ctx->method_ofs); |
| 2042 | |
| 2043 | /* set up a fixup anchor for the new method */ |
| 2044 | ctx->anchor = stream->add_anchor(fixup_owner_sym, fixup_list_head); |
| 2045 | |
| 2046 | /* set the anchor in the associated symbol, if applicable */ |
| 2047 | if (fixup_owner_sym != 0) |
| 2048 | fixup_owner_sym->set_anchor(ctx->anchor); |
| 2049 | |
| 2050 | /* |
| 2051 | * Generate the function header. At the moment, we don't know the |
| 2052 | * stack usage, exception table offset, or debug record offset, since |
| 2053 | * these all come after the byte code; we won't know how big the byte |
| 2054 | * code is until after we generate it. For now, write zero bytes as |
| 2055 | * placeholders for these slots; we'll come back and fix them up to |
| 2056 | * their real values after we've generated the byte code. |
| 2057 | */ |
| 2058 | stream->write(argc | (varargs ? 0x80 : 0)); /* argument count */ |
| 2059 | stream->write((char)opt_argc); /* additional optional argument count */ |
| 2060 | stream->write2(0); /* number of locals - won't know until after codegen */ |
| 2061 | stream->write2(0); /* total stack - won't know until after codegen */ |
| 2062 | stream->write2(0); /* exception table offset - presume none */ |
| 2063 | stream->write2(0); /* debug record offset - presume no debug records */ |
| 2064 | |
| 2065 | /* |
| 2066 | * If the header size in the globals is greater than the fixed size we |
| 2067 | * write, pad out the remainder with zero bytes. |
| 2068 | */ |
| 2069 | for (int i = G_sizes.mhdr - 10 ; i > 0 ; --i) |
| 2070 | stream->write(0); |
| 2071 | |
| 2072 | /* remember the starting offset of the code */ |
| 2073 | ctx->code_start_ofs = stream->get_ofs(); |
| 2074 | |
| 2075 | /* set the current code body being generated */ |
| 2076 | ctx->old_code_body = stream->set_code_body(code_body); |
| 2077 | |
| 2078 | /* get the 'goto' symbol table for this function */ |
| 2079 | stream->set_goto_symtab(goto_tab); |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * Close a method |
| 2084 | */ |
| 2085 | void CTcGenTarg::close_method(int local_cnt, |
| 2086 | CTcPrsSymtab *local_symtab, |
| 2087 | CTcTokFileDesc *end_desc, long end_linenum, |
| 2088 | tct3_method_gen_ctx *ctx, |
| 2089 | CTcNamedArgTab *named_arg_tab_head) |
| 2090 | { |
| 2091 | /* get the output code stream from the context */ |
| 2092 | CTcCodeStream *stream = ctx->stream; |
| 2093 | |
| 2094 | /* |
| 2095 | * Generate a 'return' opcode with a default 'nil' return value - this |
| 2096 | * will ensure that code that reaches the end of the procedure returns |
| 2097 | * normally. If this is a constructor, return the 'self' object rather |
| 2098 | * than nil. |
| 2099 | * |
| 2100 | * We only need to generate this epilogue if the next instruction would |
| 2101 | * be reachable. If it's not reachable, then the code explicitly took |
| 2102 | * care of all types of exits. |
| 2103 | */ |
| 2104 | if (!can_skip_op()) |
| 2105 | { |
| 2106 | /* |
| 2107 | * add a line record for the implied return at the last source line |
| 2108 | * of the code body |
| 2109 | */ |
| 2110 | add_line_rec(end_desc, end_linenum); |
| 2111 | |
| 2112 | /* write the appropriate return */ |
| 2113 | if (is_in_constructor()) |
| 2114 | { |
| 2115 | /* we're in a constructor - return 'self' */ |
| 2116 | write_op(OPC_PUSHSELF); |
| 2117 | write_op(OPC_RETVAL); |
| 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | /* |
| 2122 | * Normal method/function - return without a value (explicitly |
| 2123 | * set R0 to nil, though, so we don't return something returned |
| 2124 | * from a called function). |
| 2125 | */ |
| 2126 | write_op(OPC_RETNIL); |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | /* generate any named argument tables */ |
| 2131 | for (CTcNamedArgTab *nt = named_arg_tab_head ; nt != 0 ; nt = nt->nxt) |
| 2132 | nt->generate(); |
| 2133 | |
| 2134 | /* |
| 2135 | * release labels allocated for the code block; this will log an error |
| 2136 | * if any labels are undefined |
| 2137 | */ |
| 2138 | stream->release_labels(); |
| 2139 | |
| 2140 | /* |
| 2141 | * Eliminate jump-to-jump sequences in the generated code. Don't |
| 2142 | * bother if we've found any errors, as the generated code will not |
| 2143 | * necessarily be valid if this is the case. |
| 2144 | */ |
| 2145 | if (G_tcmain->get_error_count() == 0) |
| 2146 | remove_jumps_to_jumps(stream, ctx->code_start_ofs); |
| 2147 | |
| 2148 | /* note the code block's end point */ |
| 2149 | ctx->code_end_ofs = stream->get_ofs(); |
| 2150 | |
| 2151 | /* |
| 2152 | * Fix up the local variable count in the function header. We might |
| 2153 | * allocate extra locals for internal use while generating code, so we |
| 2154 | * must wait until after generating our code before we know the final |
| 2155 | * local count. |
| 2156 | */ |
| 2157 | stream->write2_at(ctx->method_ofs + 2, local_cnt); |
| 2158 | |
| 2159 | /* |
| 2160 | * Fix up the total stack space indicator in the function header. The |
| 2161 | * total stack size must include the locals, as well as stack space |
| 2162 | * needed for intermediate computations. |
| 2163 | */ |
| 2164 | stream->write2_at(ctx->method_ofs + 4, get_max_sp_depth() + local_cnt); |
| 2165 | |
| 2166 | /* |
| 2167 | * Before writing the exception table, mark the endpoint of the range |
| 2168 | * of the main local scope and any enclosing scopes. |
| 2169 | */ |
| 2170 | for (CTcPrsSymtab *t = local_symtab ; t != 0 ; t = t->get_parent()) |
| 2171 | t->add_to_range(G_cs->get_ofs() - ctx->method_ofs); |
| 2172 | |
| 2173 | /* |
| 2174 | * Generate the exception table, if we have one. If we have no |
| 2175 | * exception records, leave the exception table offset set to zero to |
| 2176 | * indicate that there is no exception table for the method. |
| 2177 | */ |
| 2178 | if (get_exc_table()->get_entry_count() != 0) |
| 2179 | { |
| 2180 | /* |
| 2181 | * write the exception table offset - it's at the current offset in |
| 2182 | * the code |
| 2183 | */ |
| 2184 | stream->write2_at(ctx->method_ofs + 6, |
| 2185 | stream->get_ofs() - ctx->method_ofs); |
| 2186 | |
| 2187 | /* write the table */ |
| 2188 | get_exc_table()->write_to_code_stream(); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * Clean up after generating a method |
| 2194 | */ |
| 2195 | void CTcGenTarg::close_method_cleanup(tct3_method_gen_ctx *ctx) |
| 2196 | { |
| 2197 | /* |
| 2198 | * Tell the code generator our code block byte length so that it can |
| 2199 | * keep track of the longest single byte-code block; it will use this |
| 2200 | * to choose the code pool page size when generating the image file. |
| 2201 | */ |
| 2202 | note_bytecode(ctx->anchor->get_len(ctx->stream)); |
| 2203 | |
| 2204 | /* we're no longer in a constructor, if we ever were */ |
| 2205 | set_in_constructor(FALSE); |
| 2206 | |
| 2207 | /* likewise an operator overload method */ |
| 2208 | set_in_op_overload(FALSE); |
| 2209 | |
| 2210 | /* clear the current code body */ |
| 2211 | ctx->stream->set_code_body(ctx->old_code_body); |
| 2212 | |
| 2213 | /* always leave the main code stream active by default */ |
| 2214 | G_cs = G_cs_main; |
| 2215 | } |
| 2216 | |
| 2217 | /* ------------------------------------------------------------------------ */ |
| 2218 | /* |
| 2219 | * Do post-CALL cleanup |
| 2220 | */ |
| 2221 | void CTcGenTarg::post_call_cleanup(const CTcNamedArgs *named_args) |
| 2222 | { |
| 2223 | /* write the named argument table pointer, if necessary */ |
| 2224 | if (named_args != 0 && named_args->cnt != 0) |
| 2225 | { |
| 2226 | /* |
| 2227 | * We need a named argument table. Add the table to the curren |
| 2228 | * code body, which will generate a label for us to use to |
| 2229 | * reference the table. |
| 2230 | */ |
| 2231 | CTcCodeLabel *tab_lbl = G_cs->get_code_body() |
| 2232 | ->add_named_arg_tab(named_args); |
| 2233 | |
| 2234 | /* write the op: NamedArgPtr <named arg cnt>, <table offset> */ |
| 2235 | G_cg->write_op(OPC_NAMEDARGPTR); |
| 2236 | G_cs->write((char)named_args->cnt); |
| 2237 | G_cs->write_ofs2(tab_lbl, 0); |
| 2238 | |
| 2239 | /* NamedArgPtr pops the arguments when executed */ |
| 2240 | G_cg->note_pop(named_args->cnt); |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | /* ------------------------------------------------------------------------ */ |
| 2245 | /* |
| 2246 | * Run through the generated code stream starting at the given offset (and |
| 2247 | * running up to the current offset), and eliminate jump-to-jump sequences: |
| 2248 | * |
| 2249 | * - whenever we find any jump instruction that points directly to an |
| 2250 | * unconditional jump, we'll change the first jump so that it points to |
| 2251 | * the target of the second jump, saving the unnecessary stop at the |
| 2252 | * intermediate jump |
| 2253 | * |
| 2254 | * - whenever we find an unconditional jump to any return or throw |
| 2255 | * instruction, we'll replace the jump with a copy of the target |
| 2256 | * return/throw instruction. |
| 2257 | */ |
| 2258 | void CTcGenTarg::remove_jumps_to_jumps(CTcCodeStream *str, ulong start_ofs) |
| 2259 | { |
| 2260 | ulong ofs; |
| 2261 | ulong end_ofs; |
| 2262 | uchar prv_op; |
| 2263 | |
| 2264 | /* |
| 2265 | * scan the code stream starting at the given offset, continuing |
| 2266 | * through the current offset |
| 2267 | */ |
| 2268 | prv_op = OPC_NOP; |
| 2269 | for (ofs = start_ofs, end_ofs = str->get_ofs() ; ofs < end_ofs ; ) |
| 2270 | { |
| 2271 | uchar op; |
| 2272 | ulong target_ofs; |
| 2273 | ulong orig_target_ofs; |
| 2274 | uchar target_op; |
| 2275 | int done; |
| 2276 | int chain_len; |
| 2277 | |
| 2278 | /* check the byte code instruction at the current location */ |
| 2279 | switch(op = str->get_byte_at(ofs)) |
| 2280 | { |
| 2281 | case OPC_RETVAL: |
| 2282 | /* |
| 2283 | * If our previous opcode was PUSHTRUE or PUSHNIL, we can |
| 2284 | * replace the previous opcode with RETTRUE or RETNIL. This |
| 2285 | * sequence can occur when we generate conditional code that |
| 2286 | * returns a value; in such cases, we sometimes can't elide |
| 2287 | * the PUSHx/RETVAL sequence during the original code |
| 2288 | * generation because the RETVAL itself is the target of a |
| 2289 | * label and thus must be retained as a separate instruction. |
| 2290 | * Converting the PUSHTRUE or PUSHNIL here won't do any harm, |
| 2291 | * as we'll still leave the RETVAL as a separate instruction. |
| 2292 | * Likewise, if the previous instruction was GET_R0, we can |
| 2293 | * change it to a simple RET. |
| 2294 | */ |
| 2295 | switch(prv_op) |
| 2296 | { |
| 2297 | case OPC_PUSHTRUE: |
| 2298 | /* convert the PUSHTRUE to a RETTRUE */ |
| 2299 | str->write_at(ofs - 1, OPC_RETTRUE); |
| 2300 | break; |
| 2301 | |
| 2302 | case OPC_PUSHNIL: |
| 2303 | /* convert the PUSHNIL to a RETNIL */ |
| 2304 | str->write_at(ofs - 1, OPC_RETNIL); |
| 2305 | break; |
| 2306 | |
| 2307 | case OPC_GETR0: |
| 2308 | /* convert the GETR0 to a RET */ |
| 2309 | str->write_at(ofs - 1, OPC_RET); |
| 2310 | break; |
| 2311 | } |
| 2312 | |
| 2313 | /* skip the RETVAL */ |
| 2314 | ofs += 1; |
| 2315 | break; |
| 2316 | |
| 2317 | case OPC_PUSHSTRI: |
| 2318 | /* |
| 2319 | * push in-line string: we have a UINT2 operand giving the |
| 2320 | * length in bytes of the string, followed by the bytes of the |
| 2321 | * string, so read the uint2 and then skip that amount plus |
| 2322 | * three additional bytes (one for the opcode, two for the |
| 2323 | * uint2 itself) |
| 2324 | */ |
| 2325 | ofs += 3 + str->readu2_at(ofs+1); |
| 2326 | break; |
| 2327 | |
| 2328 | case OPC_SWITCH: |
| 2329 | /* |
| 2330 | * Switch: we have a UINT2 giving the number of cases, |
| 2331 | * followed by the cases, followed by an INT2; each case |
| 2332 | * consists of a DATAHOLDER plus a UINT2, for a total of 7 |
| 2333 | * bytes. The total is thus 5 bytes (the opcode, the case |
| 2334 | * count UINT2, the final INT2) plus 7 bytes times the number |
| 2335 | * of cases. |
| 2336 | */ |
| 2337 | ofs += 1 + 2 + (VMB_DATAHOLDER + 2)*str->readu2_at(ofs+1) + 2; |
| 2338 | break; |
| 2339 | |
| 2340 | case OPC_NAMEDARGTAB: |
| 2341 | /* |
| 2342 | * NamedArgTab - the first operand is a UINT2 giving the length |
| 2343 | * of the table |
| 2344 | */ |
| 2345 | ofs += 1 + 2 + str->readu2_at(ofs+1); |
| 2346 | break; |
| 2347 | |
| 2348 | case OPC_JMP: |
| 2349 | /* |
| 2350 | * Unconditional jump: check for a jump to a RETURN of any |
| 2351 | * kind or a THROW. If the destination consists of either of |
| 2352 | * those, replace the JMP with the target instruction. If the |
| 2353 | * destination is an unconditional JMP, iteratively check its |
| 2354 | * destination. |
| 2355 | */ |
| 2356 | orig_target_ofs = target_ofs = ofs + 1 + str->read2_at(ofs + 1); |
| 2357 | |
| 2358 | /* |
| 2359 | * Iterate through any chain of JMP's we find. Abort if we |
| 2360 | * try following a chain longer than 20 jumps, in case we |
| 2361 | * should encounter any circular chains. |
| 2362 | */ |
| 2363 | for (done = FALSE, chain_len = 0 ; !done && chain_len < 20 ; |
| 2364 | ++chain_len) |
| 2365 | { |
| 2366 | switch(target_op = str->get_byte_at(target_ofs)) |
| 2367 | { |
| 2368 | case OPC_RETVAL: |
| 2369 | /* |
| 2370 | * Check for a special sequence that we can optimize |
| 2371 | * even better than the usual. If we have a GETR0 |
| 2372 | * followed by a JMP to a RETVAL, then we can |
| 2373 | * eliminate the JMP *and* the GETR0, and just convert |
| 2374 | * the GETR0 to a RET. |
| 2375 | */ |
| 2376 | if (prv_op == OPC_GETR0) |
| 2377 | { |
| 2378 | /* |
| 2379 | * The GETR0 is the byte before the original JMP: |
| 2380 | * simply replace it with a RET. Note that we can |
| 2381 | * leave the original jump intact, in case anyone |
| 2382 | * else is pointing to it. |
| 2383 | */ |
| 2384 | str->write_at(ofs - 1, OPC_RET); |
| 2385 | |
| 2386 | /* we're done iterating the chain of jumps-to-jumps */ |
| 2387 | done = TRUE; |
| 2388 | } |
| 2389 | else |
| 2390 | { |
| 2391 | /* handle the same as any return instruction */ |
| 2392 | goto any_RET; |
| 2393 | } |
| 2394 | break; |
| 2395 | |
| 2396 | case OPC_RETNIL: |
| 2397 | case OPC_RETTRUE: |
| 2398 | case OPC_RET: |
| 2399 | case OPC_THROW: |
| 2400 | any_RET: |
| 2401 | /* |
| 2402 | * it's a THROW or RETURN of some kind - simply copy |
| 2403 | * it to the current slot; write NOP's over our jump |
| 2404 | * offset operand, to make sure the code continues to |
| 2405 | * be deterministically readable |
| 2406 | */ |
| 2407 | str->write_at(ofs, target_op); |
| 2408 | str->write_at(ofs + 1, (uchar)OPC_NOP); |
| 2409 | str->write_at(ofs + 2, (uchar)OPC_NOP); |
| 2410 | |
| 2411 | /* we're done iterating the chain of jumps-to-jumps */ |
| 2412 | done = TRUE; |
| 2413 | break; |
| 2414 | |
| 2415 | case OPC_JMP: |
| 2416 | /* |
| 2417 | * We're jumping to another jump - there's no reason |
| 2418 | * to stop at the intermediate jump instruction, since |
| 2419 | * we can simply jump directly to the destination |
| 2420 | * address. Calculate the new target address, and |
| 2421 | * continue iterating, in case this jumps to something |
| 2422 | * we can further optimize away. |
| 2423 | */ |
| 2424 | target_ofs = target_ofs + 1 |
| 2425 | + str->read2_at(target_ofs + 1); |
| 2426 | |
| 2427 | /* |
| 2428 | * if it's a jump to the original location, it must be |
| 2429 | * some unreachable code that generated a circular |
| 2430 | * jump; ignore it in this case |
| 2431 | */ |
| 2432 | if (target_ofs == ofs) |
| 2433 | done = TRUE; |
| 2434 | |
| 2435 | /* proceed */ |
| 2436 | break; |
| 2437 | |
| 2438 | default: |
| 2439 | /* |
| 2440 | * For anything else, we're done with any chain of |
| 2441 | * jumps to jumps. If we indeed found a new target |
| 2442 | * address, rewrite the original JMP instruction so |
| 2443 | * that it jumps directly to the end of the chain |
| 2444 | * rather than going through the intermediate jumps. |
| 2445 | */ |
| 2446 | if (target_ofs != orig_target_ofs) |
| 2447 | str->write2_at(ofs + 1, target_ofs - (ofs + 1)); |
| 2448 | |
| 2449 | /* we're done iterating */ |
| 2450 | done = TRUE; |
| 2451 | break; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | /* whatever happened, skip past the jump */ |
| 2456 | ofs += 3; |
| 2457 | |
| 2458 | /* done */ |
| 2459 | break; |
| 2460 | |
| 2461 | case OPC_JT: |
| 2462 | case OPC_JF: |
| 2463 | case OPC_JE: |
| 2464 | case OPC_JNE: |
| 2465 | case OPC_JGT: |
| 2466 | case OPC_JGE: |
| 2467 | case OPC_JLT: |
| 2468 | case OPC_JLE: |
| 2469 | case OPC_JST: |
| 2470 | case OPC_JSF: |
| 2471 | case OPC_JNIL: |
| 2472 | case OPC_JNOTNIL: |
| 2473 | case OPC_JR0T: |
| 2474 | case OPC_JR0F: |
| 2475 | /* |
| 2476 | * We have a jump (conditional or otherwise). Check the |
| 2477 | * target instruction to see if it's an unconditional jump; if |
| 2478 | * so, then we can jump straight to the target of the second |
| 2479 | * jump, since there's no reason to stop at the intermediate |
| 2480 | * jump instruction on our way to the final destination. Make |
| 2481 | * this check iteratively, so that we eliminate any chain of |
| 2482 | * jumps to jumps and land at our final non-jump instruction |
| 2483 | * in one go. |
| 2484 | */ |
| 2485 | orig_target_ofs = target_ofs = ofs + 1 + str->read2_at(ofs + 1); |
| 2486 | for (done = FALSE, chain_len = 0 ; !done && chain_len < 20 ; |
| 2487 | ++chain_len) |
| 2488 | { |
| 2489 | uchar target_op; |
| 2490 | |
| 2491 | /* get the target opcode */ |
| 2492 | target_op = str->get_byte_at(target_ofs); |
| 2493 | |
| 2494 | /* |
| 2495 | * if the target is an unconditional JMP, we can retarget |
| 2496 | * the original instruction to jump directly to the target |
| 2497 | * of the target JMP, bypassing the target JMP entirely and |
| 2498 | * thus avoiding some unnecessary work at run-time |
| 2499 | */ |
| 2500 | if (target_op == OPC_JMP) |
| 2501 | { |
| 2502 | /* |
| 2503 | * retarget the original jump to go directly to the |
| 2504 | * target of the target JMP |
| 2505 | */ |
| 2506 | target_ofs = target_ofs + 1 |
| 2507 | + str->read2_at(target_ofs + 1); |
| 2508 | |
| 2509 | /* |
| 2510 | * continue scanning for more opportunities, as the new |
| 2511 | * target could also point to something we can bypass |
| 2512 | */ |
| 2513 | continue; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * Certain combinations are special. If the original |
| 2518 | * opcode was a JST or JSF, and the target is a JT or JF, |
| 2519 | * we can recode the sequence so that the original opcode |
| 2520 | * turns into a more efficient JT or JF and jumps directly |
| 2521 | * past the JT or JF. If we have a JST or JSF jumping to a |
| 2522 | * JST or JSF, we can also recode that sequence to bypass |
| 2523 | * the second jump. In both cases, we can recode the |
| 2524 | * sequence because the original jump will unequivocally |
| 2525 | * determine the behavior at the target jump in such a way |
| 2526 | * that we can compact the sequence into a single jump. |
| 2527 | */ |
| 2528 | switch(op) |
| 2529 | { |
| 2530 | case OPC_JSF: |
| 2531 | /* |
| 2532 | * the original is a JSF: we can recode a jump to a |
| 2533 | * JSF, JST, JF, or JT |
| 2534 | */ |
| 2535 | switch(target_op) |
| 2536 | { |
| 2537 | case OPC_JSF: |
| 2538 | /* |
| 2539 | * We're jumping to another JSF. Since the |
| 2540 | * original jump will only reach the target jump if |
| 2541 | * the value on the top of the stack is false, and |
| 2542 | * will then leave this same value on the stack to |
| 2543 | * be tested again with the target JSF, we know the |
| 2544 | * target JSF will perform its jump and leave the |
| 2545 | * stack unchanged again. So, we can simply |
| 2546 | * retarget the original jump to the target of the |
| 2547 | * target JSF. |
| 2548 | */ |
| 2549 | target_ofs = target_ofs + 1 |
| 2550 | + str->read2_at(target_ofs + 1); |
| 2551 | |
| 2552 | /* keep scanning for additional opportunities */ |
| 2553 | break; |
| 2554 | |
| 2555 | case OPC_JST: |
| 2556 | case OPC_JT: |
| 2557 | /* |
| 2558 | * We're jumping to a JST or a JT. Since the JSF |
| 2559 | * will only reach the JST/JT on a false value, we |
| 2560 | * know the JST/JT will NOT jump - we know for a |
| 2561 | * fact it will pop the non-true stack element and |
| 2562 | * proceed without jumping. Therefore, we can |
| 2563 | * avoid saving the value from the original JSF, |
| 2564 | * which means we can recode the original as the |
| 2565 | * simpler JF (which doesn't bother saving the |
| 2566 | * false value), and jump on false directly to the |
| 2567 | * instruction after the target JST/JT. |
| 2568 | */ |
| 2569 | str->write_at(ofs, (uchar)OPC_JF); |
| 2570 | op = OPC_JF; |
| 2571 | |
| 2572 | /* jump to the instruction after the target JST/JT */ |
| 2573 | target_ofs += 3; |
| 2574 | |
| 2575 | /* keep looking for more jumps */ |
| 2576 | break; |
| 2577 | |
| 2578 | case OPC_JF: |
| 2579 | /* |
| 2580 | * We're jumping to a JF: we know the JF will jump, |
| 2581 | * because we had to have - and then save - a false |
| 2582 | * value for the JSF to reach the JF in the first |
| 2583 | * place. Since we know for a fact the target JF |
| 2584 | * will remove the false value and jump to its |
| 2585 | * target, we can bypass the target JF by recoding |
| 2586 | * the original instruction as a simpler JF and |
| 2587 | * jumping directly to the target of the target JF. |
| 2588 | */ |
| 2589 | str->write_at(ofs, (uchar)OPC_JF); |
| 2590 | op = OPC_JF; |
| 2591 | |
| 2592 | /* jump to the tartet of the target JF */ |
| 2593 | target_ofs = target_ofs + 1 |
| 2594 | + str->read2_at(target_ofs + 1); |
| 2595 | |
| 2596 | /* keep scanning for more jumps */ |
| 2597 | break; |
| 2598 | |
| 2599 | default: |
| 2600 | /* can't make any assumptions about other targets */ |
| 2601 | done = TRUE; |
| 2602 | break; |
| 2603 | } |
| 2604 | break; |
| 2605 | |
| 2606 | case OPC_JST: |
| 2607 | /* |
| 2608 | * the original is a JST: recode it if the target is a |
| 2609 | * JSF, JST, JF, or JT |
| 2610 | */ |
| 2611 | switch(target_op) |
| 2612 | { |
| 2613 | case OPC_JST: |
| 2614 | /* JST jumping to JST: jump to the target's target */ |
| 2615 | target_ofs = target_ofs + 1 |
| 2616 | + str->read2_at(target_ofs + 1); |
| 2617 | |
| 2618 | /* keep looking */ |
| 2619 | break; |
| 2620 | |
| 2621 | case OPC_JSF: |
| 2622 | case OPC_JF: |
| 2623 | /* |
| 2624 | * JST jumping to JSF/JF: the JSF/JF will |
| 2625 | * definitely pop the stack and not jump (since the |
| 2626 | * original JST will have left a true value on the |
| 2627 | * stack), so we can recode the JST as a more |
| 2628 | * efficient JT and jump to the instruction after |
| 2629 | * the JSF/JF target |
| 2630 | */ |
| 2631 | str->write_at(ofs, (uchar)OPC_JT); |
| 2632 | op = OPC_JT; |
| 2633 | |
| 2634 | /* jump to the instruction after the target */ |
| 2635 | target_ofs += 3; |
| 2636 | |
| 2637 | /* keep looking */ |
| 2638 | break; |
| 2639 | |
| 2640 | case OPC_JT: |
| 2641 | /* |
| 2642 | * JST jumping to JT: the JT will definitely pop |
| 2643 | * and jump, so we can recode the original as a |
| 2644 | * simpler JT and jump to the target's target |
| 2645 | */ |
| 2646 | str->write_at(ofs, (uchar)OPC_JT); |
| 2647 | op = OPC_JT; |
| 2648 | |
| 2649 | /* jump to the target of the target */ |
| 2650 | target_ofs = target_ofs + 1 |
| 2651 | + str->read2_at(target_ofs + 1); |
| 2652 | |
| 2653 | /* keep scanning */ |
| 2654 | break; |
| 2655 | |
| 2656 | default: |
| 2657 | /* can't make any assumptions about other targets */ |
| 2658 | done = TRUE; |
| 2659 | break; |
| 2660 | } |
| 2661 | break; |
| 2662 | |
| 2663 | default: |
| 2664 | /* |
| 2665 | * we can't make assumptions about anything else, so |
| 2666 | * we've come to the end of the road - stop scanning |
| 2667 | */ |
| 2668 | done = TRUE; |
| 2669 | break; |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | * if we found a chain of jumps, replace our original jump |
| 2675 | * target with the final jump target, bypassing the |
| 2676 | * intermediate jumps |
| 2677 | */ |
| 2678 | if (target_ofs != orig_target_ofs) |
| 2679 | str->write2_at(ofs + 1, target_ofs - (ofs + 1)); |
| 2680 | |
| 2681 | /* skip past the jump */ |
| 2682 | ofs += 3; |
| 2683 | |
| 2684 | /* done */ |
| 2685 | break; |
| 2686 | |
| 2687 | default: |
| 2688 | /* |
| 2689 | * everything else is a fixed-size instruction, so simply |
| 2690 | * consult our table of instruction lengths to determine the |
| 2691 | * offset of the next instruction |
| 2692 | */ |
| 2693 | ofs += CVmOpcodes::op_siz[op]; |
| 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | /* remember the preceding opcode */ |
| 2698 | prv_op = op; |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | /* ------------------------------------------------------------------------ */ |
| 2703 | /* |
| 2704 | * Generic T3 node |
| 2705 | */ |
| 2706 | |
| 2707 | /* |
| 2708 | * generate a jump-ahead instruction, returning a new label which serves |
| 2709 | * as the jump destination |
| 2710 | */ |
| 2711 | CTcCodeLabel *CTcPrsNode::gen_jump_ahead(uchar opc) |
| 2712 | { |
| 2713 | /* |
| 2714 | * check to see if we should suppress the jump for peephole |
| 2715 | * optimization |
| 2716 | */ |
| 2717 | if (G_cg->can_skip_op()) |
| 2718 | return 0; |
| 2719 | |
| 2720 | /* emit the opcode */ |
| 2721 | G_cg->write_op(opc); |
| 2722 | |
| 2723 | /* allocate a new label */ |
| 2724 | CTcCodeLabel *lbl = G_cs->new_label_fwd(); |
| 2725 | |
| 2726 | /* |
| 2727 | * write the forward offset to the label (this will generate a fixup |
| 2728 | * record attached to the label, so that we'll come back and fix it |
| 2729 | * up when the real offset is known) |
| 2730 | */ |
| 2731 | G_cs->write_ofs2(lbl, 0); |
| 2732 | |
| 2733 | /* return the forward label */ |
| 2734 | return lbl; |
| 2735 | } |
| 2736 | |
| 2737 | /* |
| 2738 | * Allocate a new label at the current write position |
| 2739 | */ |
| 2740 | CTcCodeLabel *CTcPrsNode::new_label_here() |
| 2741 | { |
| 2742 | /* |
| 2743 | * suppress any peephole optimizations at this point -- someone |
| 2744 | * could jump directly to this instruction, so we can't combine an |
| 2745 | * instruction destined for this point with anything previous |
| 2746 | */ |
| 2747 | G_cg->clear_peephole(); |
| 2748 | |
| 2749 | /* create and return a label at the current position */ |
| 2750 | return G_cs->new_label_here(); |
| 2751 | } |
| 2752 | |
| 2753 | /* |
| 2754 | * define the position of a code label |
| 2755 | */ |
| 2756 | void CTcPrsNode::def_label_pos(CTcCodeLabel *lbl) |
| 2757 | { |
| 2758 | /* if the label is null, ignore it */ |
| 2759 | if (lbl == 0) |
| 2760 | return; |
| 2761 | |
| 2762 | /* |
| 2763 | * try eliminating a jump-to-next-instruction sequence: if the last |
| 2764 | * opcode was a JMP to this label, remove the last instruction |
| 2765 | * entirely |
| 2766 | */ |
| 2767 | if (G_cg->get_last_op() == OPC_JMP |
| 2768 | && G_cs->has_fixup_at_ofs(lbl, G_cs->get_ofs() - 2)) |
| 2769 | { |
| 2770 | /* remove the fixup pointing to the preceding JMP */ |
| 2771 | G_cs->remove_fixup_at_ofs(lbl, G_cs->get_ofs() - 2); |
| 2772 | |
| 2773 | /* the JMP is unnecessary - remove it */ |
| 2774 | G_cg->remove_last_jmp(); |
| 2775 | } |
| 2776 | |
| 2777 | /* define the label position and apply the fixup */ |
| 2778 | G_cs->def_label_pos(lbl); |
| 2779 | |
| 2780 | /* |
| 2781 | * whenever we define a label, we must suppress any peephole |
| 2782 | * optimizations at this point - someone could jump directly to this |
| 2783 | * instruction, so we can't combine an instruction destined for this |
| 2784 | * point with anything previous |
| 2785 | */ |
| 2786 | G_cg->clear_peephole(); |
| 2787 | } |
| 2788 | |
| 2789 | /* |
| 2790 | * Generate code for an if-else conditional test. The default |
| 2791 | * implementation is to evaluate the expression, and jump to the false |
| 2792 | * branch if the expression is false (or jump to the true part if the |
| 2793 | * expression is true and there's no false part). |
| 2794 | */ |
| 2795 | void CTcPrsNode::gen_code_cond(CTcCodeLabel *then_label, |
| 2796 | CTcCodeLabel *else_label) |
| 2797 | { |
| 2798 | /* generate our expression code */ |
| 2799 | gen_code(FALSE, TRUE); |
| 2800 | |
| 2801 | /* |
| 2802 | * if we have a 'then' part, jump to the 'then' part if the condition |
| 2803 | * is true; otherwise, jump to the 'else' part if the condition is |
| 2804 | * false |
| 2805 | */ |
| 2806 | if (then_label != 0) |
| 2807 | { |
| 2808 | /* we have a 'then' part, so jump if true to the 'then' part */ |
| 2809 | G_cg->write_op(OPC_JT); |
| 2810 | G_cs->write_ofs2(then_label, 0); |
| 2811 | } |
| 2812 | else |
| 2813 | { |
| 2814 | /* we have an 'else' part, so jump if false to the 'else' */ |
| 2815 | G_cg->write_op(OPC_JF); |
| 2816 | G_cs->write_ofs2(else_label, 0); |
| 2817 | } |
| 2818 | |
| 2819 | /* the JF or JT pops an element off the stack */ |
| 2820 | G_cg->note_pop(); |
| 2821 | } |
| 2822 | |
| 2823 | /* |
| 2824 | * generate code for assignment to this node |
| 2825 | */ |
| 2826 | int CTcPrsNode::gen_code_asi(int, int phase, tc_asitype_t, const char *, |
| 2827 | CTcPrsNode *, |
| 2828 | int ignore_error, int, void **) |
| 2829 | { |
| 2830 | /* |
| 2831 | * If ignoring errors, the caller is trying to assign if possible but |
| 2832 | * doesn't require it to be possible; simply return false to indicate |
| 2833 | * that nothing happened if this is the case. Also ignore errors on |
| 2834 | * phase 2, since we'll already have reported an error on phase 1. |
| 2835 | */ |
| 2836 | if (ignore_error || phase > 1) |
| 2837 | return FALSE; |
| 2838 | |
| 2839 | /* we should never get here - throw an internal error */ |
| 2840 | G_tok->throw_internal_error(TCERR_GEN_BAD_LVALUE); |
| 2841 | AFTER_ERR_THROW(return FALSE;) |
| 2842 | } |
| 2843 | |
| 2844 | /* |
| 2845 | * generate code for taking the address of this node |
| 2846 | */ |
| 2847 | void CTcPrsNode::gen_code_addr() |
| 2848 | { |
| 2849 | /* we should never get here - throw an internal error */ |
| 2850 | G_tok->throw_internal_error(TCERR_GEN_BAD_ADDR); |
| 2851 | } |
| 2852 | |
| 2853 | /* |
| 2854 | * Generate code to call the expression as a function or method. |
| 2855 | */ |
| 2856 | void CTcPrsNode::gen_code_call(int discard, int argc, int varargs, |
| 2857 | CTcNamedArgs *named_args) |
| 2858 | { |
| 2859 | /* function/method calls are never valid in speculative mode */ |
| 2860 | if (G_cg->is_speculative()) |
| 2861 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 2862 | |
| 2863 | /* |
| 2864 | * For default nodes, assume that the result of evaluating the |
| 2865 | * expression contained in the node is a method or function pointer. |
| 2866 | * First, generate code to evaluate the expression, which should |
| 2867 | * yield an appropriate pointer value. |
| 2868 | */ |
| 2869 | gen_code(FALSE, FALSE); |
| 2870 | |
| 2871 | /* |
| 2872 | * if we have a varargs list, modify the call instruction that |
| 2873 | * follows to make it a varargs call |
| 2874 | */ |
| 2875 | if (varargs) |
| 2876 | { |
| 2877 | /* swap the top of the stack to get the arg counter back on top */ |
| 2878 | G_cg->write_op(OPC_SWAP); |
| 2879 | |
| 2880 | /* write the varargs modifier */ |
| 2881 | G_cg->write_op(OPC_VARARGC); |
| 2882 | } |
| 2883 | |
| 2884 | /* generate an indirect function call through the pointer */ |
| 2885 | G_cg->write_op(OPC_PTRCALL); |
| 2886 | G_cs->write((char)argc); |
| 2887 | |
| 2888 | /* PTRCALL pops the arguments plus the function pointer */ |
| 2889 | G_cg->note_pop(argc + 1); |
| 2890 | |
| 2891 | /* do post-call cleanup: named arg removal, etc */ |
| 2892 | G_cg->post_call_cleanup(named_args); |
| 2893 | |
| 2894 | /* |
| 2895 | * if the caller isn't going to discard the return value, push the |
| 2896 | * result, which is sitting in R0 |
| 2897 | */ |
| 2898 | if (!discard) |
| 2899 | { |
| 2900 | G_cg->write_op(OPC_GETR0); |
| 2901 | G_cg->note_push(); |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | /* |
| 2906 | * Generate code for operator 'new' applied to this node |
| 2907 | */ |
| 2908 | void CTcPrsNode::gen_code_new(int, int, int, CTcNamedArgs *, int, int) |
| 2909 | { |
| 2910 | /* operator 'new' cannot be applied to a default node */ |
| 2911 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 2912 | } |
| 2913 | |
| 2914 | /* |
| 2915 | * Generate code for a member evaluation |
| 2916 | */ |
| 2917 | void CTcPrsNode::gen_code_member(int discard, |
| 2918 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 2919 | int argc, int varargs, |
| 2920 | CTcNamedArgs *named_args) |
| 2921 | { |
| 2922 | /* evaluate my own expression to yield the object value */ |
| 2923 | gen_code(FALSE, FALSE); |
| 2924 | |
| 2925 | /* if we have an argument counter, put it back on top */ |
| 2926 | if (varargs) |
| 2927 | G_cg->write_op(OPC_SWAP); |
| 2928 | |
| 2929 | /* use the generic code to generate the rest */ |
| 2930 | s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 2931 | argc, varargs, named_args); |
| 2932 | } |
| 2933 | |
| 2934 | /* |
| 2935 | * Generic code to generate the rest of a member expression after the |
| 2936 | * left side of the '.' has been generated. This can be used for cases |
| 2937 | * where the left of the '.' is an arbitrary expression, and hence must |
| 2938 | * be evaluated at run-time. |
| 2939 | */ |
| 2940 | void CTcPrsNode::s_gen_member_rhs(int discard, |
| 2941 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 2942 | int argc, int varargs, |
| 2943 | CTcNamedArgs *named_args) |
| 2944 | { |
| 2945 | vm_prop_id_t prop; |
| 2946 | |
| 2947 | /* we can't call methods with arguments in speculative mode */ |
| 2948 | if (G_cg->is_speculative() |
| 2949 | && (argc != 0 || (named_args != 0 && named_args->cnt != 0))) |
| 2950 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 2951 | |
| 2952 | /* get or generate the property ID value */ |
| 2953 | prop = prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 2954 | |
| 2955 | /* |
| 2956 | * if we got a property ID, generate a simple GETPROP or CALLPROP |
| 2957 | * instruction; otherwise, generate a PTRCALLPROP instruction |
| 2958 | */ |
| 2959 | if (prop != VM_INVALID_PROP) |
| 2960 | { |
| 2961 | /* |
| 2962 | * we have a constant property ID - generate a GETPROP or |
| 2963 | * CALLPROP with the property ID as constant data |
| 2964 | */ |
| 2965 | if (argc == 0) |
| 2966 | { |
| 2967 | /* no arguments - generate a GETPROP */ |
| 2968 | G_cg->write_op(G_cg->is_speculative() |
| 2969 | ? OPC_GETPROPDATA : OPC_GETPROP); |
| 2970 | G_cs->write_prop_id(prop); |
| 2971 | |
| 2972 | /* this pops an object element */ |
| 2973 | G_cg->note_pop(); |
| 2974 | } |
| 2975 | else |
| 2976 | { |
| 2977 | /* write the CALLPROP instruction */ |
| 2978 | G_cg->write_callprop(argc, varargs, prop); |
| 2979 | } |
| 2980 | } |
| 2981 | else |
| 2982 | { |
| 2983 | if (G_cg->is_speculative()) |
| 2984 | { |
| 2985 | /* |
| 2986 | * speculative - use PTRGETPROPDATA to ensure we don't cause |
| 2987 | * any side effects |
| 2988 | */ |
| 2989 | G_cg->write_op(OPC_PTRGETPROPDATA); |
| 2990 | } |
| 2991 | else |
| 2992 | { |
| 2993 | /* |
| 2994 | * if we have a varargs list, modify the call instruction |
| 2995 | * that follows to make it a varargs call |
| 2996 | */ |
| 2997 | if (varargs) |
| 2998 | { |
| 2999 | /* swap to get the arg counter back on top */ |
| 3000 | G_cg->write_op(OPC_SWAP); |
| 3001 | |
| 3002 | /* write the varargs modifier */ |
| 3003 | G_cg->write_op(OPC_VARARGC); |
| 3004 | } |
| 3005 | |
| 3006 | /* a property pointer is on the stack - write a PTRCALLPROP */ |
| 3007 | G_cg->write_op(OPC_PTRCALLPROP); |
| 3008 | G_cs->write((int)argc); |
| 3009 | } |
| 3010 | |
| 3011 | /* |
| 3012 | * ptrcallprop/ptrgetpropdata removes arguments, the object, and |
| 3013 | * the property |
| 3014 | */ |
| 3015 | G_cg->note_pop(argc + 2); |
| 3016 | } |
| 3017 | |
| 3018 | /* do post-call cleanup: named arg removal, etc */ |
| 3019 | G_cg->post_call_cleanup(named_args); |
| 3020 | |
| 3021 | /* if we're not discarding the result, push it from R0 */ |
| 3022 | if (!discard) |
| 3023 | { |
| 3024 | G_cg->write_op(OPC_GETR0); |
| 3025 | G_cg->note_push(); |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | /* |
| 3030 | * Generate code to get the property ID of the expression. |
| 3031 | */ |
| 3032 | vm_prop_id_t CTcPrsNode::gen_code_propid(int check_only, int is_expr) |
| 3033 | { |
| 3034 | /* |
| 3035 | * simply evaluate the expression normally, anticipating that this |
| 3036 | * will yield a property ID value at run-time |
| 3037 | */ |
| 3038 | if (!check_only) |
| 3039 | gen_code(FALSE, FALSE); |
| 3040 | |
| 3041 | /* tell the caller that there's no constant ID available */ |
| 3042 | return VM_INVALID_PROP; |
| 3043 | } |
| 3044 | |
| 3045 | /* ------------------------------------------------------------------------ */ |
| 3046 | /* |
| 3047 | * "self" |
| 3048 | */ |
| 3049 | |
| 3050 | /* |
| 3051 | * generate code |
| 3052 | */ |
| 3053 | void CTPNSelf::gen_code(int discard, int) |
| 3054 | { |
| 3055 | /* it's an error if we're not in a method context */ |
| 3056 | if (!G_cs->is_self_available()) |
| 3057 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 3058 | |
| 3059 | /* if we're not discarding the result, push the "self" object */ |
| 3060 | if (!discard) |
| 3061 | { |
| 3062 | G_cg->write_op(OPC_PUSHSELF); |
| 3063 | G_cg->note_push(); |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | /* |
| 3068 | * evaluate a property |
| 3069 | */ |
| 3070 | void CTPNSelf::gen_code_member(int discard, |
| 3071 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3072 | int argc, int varargs, |
| 3073 | CTcNamedArgs *named_args) |
| 3074 | { |
| 3075 | vm_prop_id_t prop; |
| 3076 | |
| 3077 | /* make sure "self" is available */ |
| 3078 | if (!G_cs->is_self_available()) |
| 3079 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 3080 | |
| 3081 | /* don't allow arguments in speculative eval mode */ |
| 3082 | if (G_cg->is_speculative() |
| 3083 | && (argc != 0 || (named_args != 0 && named_args->cnt != 0))) |
| 3084 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3085 | |
| 3086 | /* generate the property value */ |
| 3087 | prop = prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 3088 | |
| 3089 | /* |
| 3090 | * if we got a property ID, generate a simple GETPROPSELF or |
| 3091 | * CALLPROPSELF; otherwise, generate a PTRCALLPROPSELF |
| 3092 | */ |
| 3093 | if (prop != VM_INVALID_PROP) |
| 3094 | { |
| 3095 | /* |
| 3096 | * we have a constant property ID - generate a GETPROPDATA, |
| 3097 | * GETPROPSELF, or CALLPROPSELF with the property ID as |
| 3098 | * immediate data |
| 3099 | */ |
| 3100 | if (G_cg->is_speculative()) |
| 3101 | { |
| 3102 | /* speculative - use GETPROPDATA */ |
| 3103 | G_cg->write_op(OPC_PUSHSELF); |
| 3104 | G_cg->write_op(OPC_GETPROPDATA); |
| 3105 | G_cs->write_prop_id(prop); |
| 3106 | |
| 3107 | /* we pushed one element (self) and popped it back off */ |
| 3108 | G_cg->note_push(); |
| 3109 | G_cg->note_pop(); |
| 3110 | } |
| 3111 | else if (argc == 0) |
| 3112 | { |
| 3113 | /* no arguments - generate a GETPROPSELF */ |
| 3114 | G_cg->write_op(OPC_GETPROPSELF); |
| 3115 | G_cs->write_prop_id(prop); |
| 3116 | } |
| 3117 | else |
| 3118 | { |
| 3119 | /* add a varargs modifier if appropriate */ |
| 3120 | if (varargs) |
| 3121 | G_cg->write_op(OPC_VARARGC); |
| 3122 | |
| 3123 | /* we have arguments - generate a CALLPROPSELF */ |
| 3124 | G_cg->write_op(OPC_CALLPROPSELF); |
| 3125 | G_cs->write((char)argc); |
| 3126 | G_cs->write_prop_id(prop); |
| 3127 | |
| 3128 | /* this removes arguments */ |
| 3129 | G_cg->note_pop(argc); |
| 3130 | } |
| 3131 | } |
| 3132 | else |
| 3133 | { |
| 3134 | /* |
| 3135 | * a property pointer is on the stack - use PTRGETPROPDATA or |
| 3136 | * PTRCALLPROPSELF, depending on the speculative mode |
| 3137 | */ |
| 3138 | if (G_cg->is_speculative()) |
| 3139 | { |
| 3140 | /* speculative - use PTRGETPROPDATA after pushing self */ |
| 3141 | G_cg->write_op(OPC_PUSHSELF); |
| 3142 | G_cg->write_op(OPC_PTRGETPROPDATA); |
| 3143 | |
| 3144 | /* we pushed self then removed self and the property ID */ |
| 3145 | G_cg->note_push(); |
| 3146 | G_cg->note_pop(2); |
| 3147 | } |
| 3148 | else |
| 3149 | { |
| 3150 | /* |
| 3151 | * if we have a varargs list, modify the call instruction |
| 3152 | * that follows to make it a varargs call |
| 3153 | */ |
| 3154 | if (varargs) |
| 3155 | { |
| 3156 | /* swap to get the arg counter back on top */ |
| 3157 | G_cg->write_op(OPC_SWAP); |
| 3158 | |
| 3159 | /* write the varargs modifier */ |
| 3160 | G_cg->write_op(OPC_VARARGC); |
| 3161 | } |
| 3162 | |
| 3163 | /* a prop pointer is on the stack - write a PTRCALLPROPSELF */ |
| 3164 | G_cg->write_op(OPC_PTRCALLPROPSELF); |
| 3165 | G_cs->write((int)argc); |
| 3166 | |
| 3167 | /* this removes arguments and the property pointer */ |
| 3168 | G_cg->note_pop(argc + 1); |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* do post-call cleanup: named arg removal, etc */ |
| 3173 | G_cg->post_call_cleanup(named_args); |
| 3174 | |
| 3175 | /* if the result is needed, push it */ |
| 3176 | if (!discard) |
| 3177 | { |
| 3178 | G_cg->write_op(OPC_GETR0); |
| 3179 | G_cg->note_push(); |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | |
| 3184 | /* |
| 3185 | * generate code for an object before a '.' |
| 3186 | */ |
| 3187 | vm_obj_id_t CTPNSelf::gen_code_obj_predot(int *is_self) |
| 3188 | { |
| 3189 | /* make sure "self" is available */ |
| 3190 | if (!G_cs->is_self_available()) |
| 3191 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 3192 | |
| 3193 | /* tell the caller that this is "self" */ |
| 3194 | *is_self = TRUE; |
| 3195 | return VM_INVALID_OBJ; |
| 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | /* ------------------------------------------------------------------------ */ |
| 3200 | /* |
| 3201 | * "replaced" |
| 3202 | */ |
| 3203 | |
| 3204 | /* |
| 3205 | * evaluate 'replaced' on its own - this simply yields a function pointer |
| 3206 | * to the modified base code |
| 3207 | */ |
| 3208 | void CTPNReplaced::gen_code(int discard, int for_condition) |
| 3209 | { |
| 3210 | /* get the modified base function symbol */ |
| 3211 | CTcSymFunc *mod_base = G_cs->get_code_body()->get_replaced_func(); |
| 3212 | |
| 3213 | /* make sure we're in a 'modify func()' context */ |
| 3214 | if (mod_base == 0) |
| 3215 | G_tok->log_error(TCERR_REPLACED_NOT_AVAIL); |
| 3216 | |
| 3217 | /* this expression yields a pointer to the modified base function */ |
| 3218 | G_cg->write_op(OPC_PUSHFNPTR); |
| 3219 | |
| 3220 | /* add a fixup for the current code location */ |
| 3221 | if (mod_base != 0) |
| 3222 | mod_base->add_abs_fixup(G_cs); |
| 3223 | |
| 3224 | /* write a placeholder offset - arbitrarily use zero */ |
| 3225 | G_cs->write4(0); |
| 3226 | |
| 3227 | /* note the push */ |
| 3228 | G_cg->note_push(); |
| 3229 | } |
| 3230 | |
| 3231 | /* |
| 3232 | * 'replaced()' call - this invokes the modified base code |
| 3233 | */ |
| 3234 | void CTPNReplaced::gen_code_call(int discard, int argc, int varargs, |
| 3235 | CTcNamedArgs *named_args) |
| 3236 | { |
| 3237 | /* get the modified base function symbol */ |
| 3238 | CTcSymFunc *mod_base = G_cs->get_code_body()->get_replaced_func(); |
| 3239 | |
| 3240 | /* make sure we're in a 'modify func()' context */ |
| 3241 | if (mod_base == 0) |
| 3242 | G_tok->log_error(TCERR_REPLACED_NOT_AVAIL); |
| 3243 | |
| 3244 | /* write the varargs modifier if appropriate */ |
| 3245 | if (varargs) |
| 3246 | G_cg->write_op(OPC_VARARGC); |
| 3247 | |
| 3248 | /* generate the call instruction and argument count */ |
| 3249 | G_cg->write_op(OPC_CALL); |
| 3250 | G_cs->write((char)argc); |
| 3251 | |
| 3252 | /* generate a fixup for the call to the modified base code */ |
| 3253 | if (mod_base != 0) |
| 3254 | mod_base->add_abs_fixup(G_cs); |
| 3255 | |
| 3256 | /* add a placeholder for the function address */ |
| 3257 | G_cs->write4(0); |
| 3258 | |
| 3259 | /* call removes arguments */ |
| 3260 | G_cg->note_pop(argc); |
| 3261 | |
| 3262 | /* do post-call cleanup: named arg removal, etc */ |
| 3263 | G_cg->post_call_cleanup(named_args); |
| 3264 | |
| 3265 | /* make sure the argument count is correct */ |
| 3266 | if (mod_base != 0 && !mod_base->argc_ok(argc)) |
| 3267 | { |
| 3268 | char buf[128]; |
| 3269 | G_tok->log_error(TCERR_WRONG_ARGC_FOR_FUNC, |
| 3270 | (int)mod_base->get_sym_len(), mod_base->get_sym(), |
| 3271 | mod_base->get_argc_desc(buf), argc); |
| 3272 | } |
| 3273 | |
| 3274 | /* if we're not discarding, push the return value from R0 */ |
| 3275 | if (!discard) |
| 3276 | { |
| 3277 | G_cg->write_op(OPC_GETR0); |
| 3278 | G_cg->note_push(); |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | /* ------------------------------------------------------------------------ */ |
| 3283 | /* |
| 3284 | * "targetprop" |
| 3285 | */ |
| 3286 | |
| 3287 | /* |
| 3288 | * generate code |
| 3289 | */ |
| 3290 | void CTPNTargetprop::gen_code(int discard, int) |
| 3291 | { |
| 3292 | /* it's an error if we're not in a method context */ |
| 3293 | if (!G_cs->is_self_available()) |
| 3294 | G_tok->log_error(TCERR_TARGETPROP_NOT_AVAIL); |
| 3295 | |
| 3296 | /* if we're not discarding the result, push the target property ID */ |
| 3297 | if (!discard) |
| 3298 | { |
| 3299 | G_cg->write_op(OPC_PUSHCTXELE); |
| 3300 | G_cs->write(PUSHCTXELE_TARGPROP); |
| 3301 | G_cg->note_push(); |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | /* ------------------------------------------------------------------------ */ |
| 3306 | /* |
| 3307 | * "targetobj" |
| 3308 | */ |
| 3309 | |
| 3310 | /* |
| 3311 | * generate code |
| 3312 | */ |
| 3313 | void CTPNTargetobj::gen_code(int discard, int) |
| 3314 | { |
| 3315 | /* it's an error if we're not in a method context */ |
| 3316 | if (!G_cs->is_self_available()) |
| 3317 | G_tok->log_error(TCERR_TARGETOBJ_NOT_AVAIL); |
| 3318 | |
| 3319 | /* if we're not discarding the result, push the target object ID */ |
| 3320 | if (!discard) |
| 3321 | { |
| 3322 | G_cg->write_op(OPC_PUSHCTXELE); |
| 3323 | G_cs->write(PUSHCTXELE_TARGOBJ); |
| 3324 | G_cg->note_push(); |
| 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | /* ------------------------------------------------------------------------ */ |
| 3329 | /* |
| 3330 | * "definingobj" |
| 3331 | */ |
| 3332 | |
| 3333 | /* |
| 3334 | * generate code |
| 3335 | */ |
| 3336 | void CTPNDefiningobj::gen_code(int discard, int) |
| 3337 | { |
| 3338 | /* it's an error if we're not in a method context */ |
| 3339 | if (!G_cs->is_self_available()) |
| 3340 | G_tok->log_error(TCERR_DEFININGOBJ_NOT_AVAIL); |
| 3341 | |
| 3342 | /* if we're not discarding the result, push the defining object ID */ |
| 3343 | if (!discard) |
| 3344 | { |
| 3345 | G_cg->write_op(OPC_PUSHCTXELE); |
| 3346 | G_cs->write(PUSHCTXELE_DEFOBJ); |
| 3347 | G_cg->note_push(); |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | |
| 3352 | /* ------------------------------------------------------------------------ */ |
| 3353 | /* |
| 3354 | * "invokee" |
| 3355 | */ |
| 3356 | |
| 3357 | /* |
| 3358 | * generate code |
| 3359 | */ |
| 3360 | void CTPNInvokee::gen_code(int discard, int) |
| 3361 | { |
| 3362 | /* if we're not discarding the result, push the invokee value */ |
| 3363 | if (!discard) |
| 3364 | { |
| 3365 | G_cg->write_op(OPC_PUSHCTXELE); |
| 3366 | G_cs->write(PUSHCTXELE_INVOKEE); |
| 3367 | G_cg->note_push(); |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | |
| 3372 | /* ------------------------------------------------------------------------ */ |
| 3373 | /* |
| 3374 | * "inherited" |
| 3375 | */ |
| 3376 | void CTPNInh::gen_code(int, int) |
| 3377 | { |
| 3378 | /* |
| 3379 | * we should never be asked to generate an "inherited" node |
| 3380 | * directly; these nodes should always be generated as part of |
| 3381 | * member evaluation |
| 3382 | */ |
| 3383 | G_tok->throw_internal_error(TCERR_GEN_CODE_INH); |
| 3384 | } |
| 3385 | |
| 3386 | /* |
| 3387 | * evaluate a property |
| 3388 | */ |
| 3389 | void CTPNInh::gen_code_member(int discard, |
| 3390 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3391 | int argc, int varargs, CTcNamedArgs *named_args) |
| 3392 | { |
| 3393 | vm_prop_id_t prop; |
| 3394 | |
| 3395 | /* don't allow 'inherited' in speculative evaluation mode */ |
| 3396 | if (G_cg->is_speculative()) |
| 3397 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3398 | |
| 3399 | /* |
| 3400 | * If we're in a multi-method context, inherited() has a different |
| 3401 | * meaning from the ordinary method context meaning. |
| 3402 | */ |
| 3403 | for (CTPNCodeBody *cb = G_cs->get_code_body() ; cb != 0 ; |
| 3404 | cb = cb->get_enclosing()) |
| 3405 | { |
| 3406 | /* check for a multi-method context */ |
| 3407 | CTcSymFunc *f = cb->get_func_sym(); |
| 3408 | if (f != 0 && f->is_multimethod()) |
| 3409 | { |
| 3410 | /* generate the multi-method inherited() code */ |
| 3411 | gen_code_mminh(f, discard, prop_expr, prop_is_expr, |
| 3412 | argc, varargs, named_args); |
| 3413 | |
| 3414 | /* we're done */ |
| 3415 | return; |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | /* |
| 3420 | * make sure "self" is available - we obviously can't inherit |
| 3421 | * anything if we're not in an object's method |
| 3422 | */ |
| 3423 | if (!G_cs->is_self_available()) |
| 3424 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 3425 | |
| 3426 | /* a type list ('inherited<>') is invalid for regular method inheritance */ |
| 3427 | if (typelist_ != 0) |
| 3428 | G_tok->log_error(TCERR_MMINH_BAD_CONTEXT); |
| 3429 | |
| 3430 | /* generate the property value */ |
| 3431 | prop = prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 3432 | |
| 3433 | /* |
| 3434 | * if we got a property ID, generate a simple INHERIT; |
| 3435 | * otherwise, generate a PTRINHERIT |
| 3436 | */ |
| 3437 | if (prop != VM_INVALID_PROP) |
| 3438 | { |
| 3439 | /* generate a varargs modifier if necessary */ |
| 3440 | if (varargs) |
| 3441 | G_cg->write_op(OPC_VARARGC); |
| 3442 | |
| 3443 | /* we have a constant property ID - generate a regular INHERIT */ |
| 3444 | G_cg->write_op(OPC_INHERIT); |
| 3445 | G_cs->write((char)argc); |
| 3446 | G_cs->write_prop_id(prop); |
| 3447 | |
| 3448 | /* this removes arguments */ |
| 3449 | G_cg->note_pop(argc); |
| 3450 | } |
| 3451 | else |
| 3452 | { |
| 3453 | /* |
| 3454 | * if we have a varargs list, modify the call instruction that |
| 3455 | * follows to make it a varargs call |
| 3456 | */ |
| 3457 | if (varargs) |
| 3458 | { |
| 3459 | /* swap the top of the stack to get the arg counter back on top */ |
| 3460 | G_cg->write_op(OPC_SWAP); |
| 3461 | |
| 3462 | /* write the varargs modifier */ |
| 3463 | G_cg->write_op(OPC_VARARGC); |
| 3464 | } |
| 3465 | |
| 3466 | /* a property pointer is on the stack - write a PTRINHERIT */ |
| 3467 | G_cg->write_op(OPC_PTRINHERIT); |
| 3468 | G_cs->write((int)argc); |
| 3469 | |
| 3470 | /* this removes arguments and the property pointer */ |
| 3471 | G_cg->note_pop(argc + 1); |
| 3472 | } |
| 3473 | |
| 3474 | /* do post-call cleanup: named arg removal, etc */ |
| 3475 | G_cg->post_call_cleanup(named_args); |
| 3476 | |
| 3477 | /* if the result is needed, push it */ |
| 3478 | if (!discard) |
| 3479 | { |
| 3480 | G_cg->write_op(OPC_GETR0); |
| 3481 | G_cg->note_push(); |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | void CTPNInh::gen_code_mminh(CTcSymFunc *func, int discard, |
| 3486 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3487 | int argc, int varargs, |
| 3488 | CTcNamedArgs *named_args) |
| 3489 | { |
| 3490 | /* |
| 3491 | * If the function has an associated global symbol from a modification, |
| 3492 | * use the global symbol instead. The modified function doesn't have a |
| 3493 | * real symbol, so we can't use it; but fortunately, the global symbol |
| 3494 | * applies to all modified versions as well, since they all inherit |
| 3495 | * from the same base function. |
| 3496 | */ |
| 3497 | while (func->get_mod_global() != 0) |
| 3498 | func = func->get_mod_global(); |
| 3499 | |
| 3500 | /* |
| 3501 | * There are two forms of inherited() for multi-methods. |
| 3502 | * |
| 3503 | * inherited<types>(args) - this form takes an explicit type list, to |
| 3504 | * invoke a specific overridden version. |
| 3505 | * |
| 3506 | * inherited(args) - this form invokes the next inherited version, |
| 3507 | * determined dynamically at run-time. |
| 3508 | */ |
| 3509 | if (typelist_ == 0) |
| 3510 | { |
| 3511 | /* |
| 3512 | * It's the inherited(args) format. |
| 3513 | * |
| 3514 | * Call _multiMethodCallInherited(fromFunc, args). We've already |
| 3515 | * pushed the args list, so just add the source function argument |
| 3516 | * (which is simply our defining function). |
| 3517 | */ |
| 3518 | func->gen_code(FALSE); |
| 3519 | |
| 3520 | /* look up _multiMethodCallInherited */ |
| 3521 | CTcSymFunc *mmci = (CTcSymFunc *)G_prs->get_global_symtab()->find( |
| 3522 | "_multiMethodCallInherited", 25); |
| 3523 | |
| 3524 | if (mmci == 0 || mmci->get_type() != TC_SYM_FUNC) |
| 3525 | { |
| 3526 | /* undefined or incorrectly defined - log an error */ |
| 3527 | G_tok->log_error(TCERR_MMINH_MISSING_SUPPORT_FUNC, |
| 3528 | 25, "_multiMethodCallInherited"); |
| 3529 | } |
| 3530 | else |
| 3531 | { |
| 3532 | /* generate the call */ |
| 3533 | mmci->gen_code_call(discard, argc + 1, varargs, named_args); |
| 3534 | } |
| 3535 | } |
| 3536 | else |
| 3537 | { |
| 3538 | /* |
| 3539 | * It's the inherited<types>(args) format. |
| 3540 | */ |
| 3541 | |
| 3542 | /* |
| 3543 | * Get the base name for the function. 'func' is the decorated |
| 3544 | * name for the containing function, which is of the form |
| 3545 | * 'Base*type1;type2...'. The base name is the part up to the |
| 3546 | * asterisk. |
| 3547 | */ |
| 3548 | const char *nm = func->getstr(), *p; |
| 3549 | size_t rem = func->getlen(); |
| 3550 | for (p = nm ; rem != 0 && *p != '*' ; ++p, --rem) ; |
| 3551 | |
| 3552 | /* make a token for the base name */ |
| 3553 | CTcToken btok; |
| 3554 | btok.set_text(nm, p - nm); |
| 3555 | |
| 3556 | /* build the decorated name for the target function */ |
| 3557 | CTcToken dtok; |
| 3558 | typelist_->decorate_name(&dtok, &btok); |
| 3559 | |
| 3560 | /* look up the decorated name */ |
| 3561 | CTcSymFunc *ifunc = (CTcSymFunc *)G_prs->get_global_symtab()->find( |
| 3562 | dtok.get_text(), dtok.get_text_len()); |
| 3563 | |
| 3564 | /* if we found it, call it */ |
| 3565 | if (ifunc != 0 && ifunc->get_type() == TC_SYM_FUNC) |
| 3566 | { |
| 3567 | /* generate the call */ |
| 3568 | ifunc->gen_code_call(discard, argc, varargs, named_args); |
| 3569 | } |
| 3570 | else |
| 3571 | { |
| 3572 | /* function not found */ |
| 3573 | G_tok->log_error(TCERR_MMINH_UNDEF_FUNC, (int)(p - nm), nm); |
| 3574 | } |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | /* ------------------------------------------------------------------------ */ |
| 3579 | /* |
| 3580 | * "inherited class" |
| 3581 | */ |
| 3582 | void CTPNInhClass::gen_code(int discard, int for_condition) |
| 3583 | { |
| 3584 | /* |
| 3585 | * we should never be asked to generate an "inherited" node |
| 3586 | * directly; these nodes should always be generated as part of |
| 3587 | * member evaluation |
| 3588 | */ |
| 3589 | G_tok->throw_internal_error(TCERR_GEN_CODE_INH); |
| 3590 | } |
| 3591 | |
| 3592 | /* |
| 3593 | * evaluate a property |
| 3594 | */ |
| 3595 | void CTPNInhClass::gen_code_member(int discard, |
| 3596 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3597 | int argc, int varargs, |
| 3598 | CTcNamedArgs *named_args) |
| 3599 | { |
| 3600 | vm_prop_id_t prop; |
| 3601 | CTcSymbol *objsym; |
| 3602 | vm_obj_id_t obj; |
| 3603 | |
| 3604 | /* |
| 3605 | * make sure "self" is available - we obviously can't inherit |
| 3606 | * anything if we're not in an object's method |
| 3607 | */ |
| 3608 | if (!G_cs->is_self_available()) |
| 3609 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 3610 | |
| 3611 | /* don't allow 'inherited' in speculative evaluation mode */ |
| 3612 | if (G_cg->is_speculative()) |
| 3613 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3614 | |
| 3615 | /* get the superclass name symbol */ |
| 3616 | objsym = G_cs->get_symtab()->find_or_def_undef(sym_, len_, FALSE); |
| 3617 | |
| 3618 | /* if it's not an object, we can't inherit from it */ |
| 3619 | obj = objsym->get_val_obj(); |
| 3620 | if (obj == VM_INVALID_OBJ) |
| 3621 | { |
| 3622 | G_tok->log_error(TCERR_INH_NOT_OBJ, (int)len_, sym_); |
| 3623 | return; |
| 3624 | } |
| 3625 | |
| 3626 | /* generate the property value */ |
| 3627 | prop = prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 3628 | |
| 3629 | /* |
| 3630 | * if we got a property ID, generate a simple EXPINHERIT; otherwise, |
| 3631 | * generate a PTREXPINHERIT |
| 3632 | */ |
| 3633 | if (prop != VM_INVALID_PROP) |
| 3634 | { |
| 3635 | /* add a varargs modifier if needed */ |
| 3636 | if (varargs) |
| 3637 | G_cg->write_op(OPC_VARARGC); |
| 3638 | |
| 3639 | /* we have a constant property ID - generate a regular EXPINHERIT */ |
| 3640 | G_cg->write_op(OPC_EXPINHERIT); |
| 3641 | G_cs->write((char)argc); |
| 3642 | G_cs->write_prop_id(prop); |
| 3643 | G_cs->write_obj_id(obj); |
| 3644 | |
| 3645 | /* this removes argumnts */ |
| 3646 | G_cg->note_pop(argc); |
| 3647 | } |
| 3648 | else |
| 3649 | { |
| 3650 | /* |
| 3651 | * if we have a varargs list, modify the call instruction that |
| 3652 | * follows to make it a varargs call |
| 3653 | */ |
| 3654 | if (varargs) |
| 3655 | { |
| 3656 | /* swap the top of the stack to get the arg counter back on top */ |
| 3657 | G_cg->write_op(OPC_SWAP); |
| 3658 | |
| 3659 | /* write the varargs modifier */ |
| 3660 | G_cg->write_op(OPC_VARARGC); |
| 3661 | } |
| 3662 | |
| 3663 | /* a property pointer is on the stack - write a PTREXPINHERIT */ |
| 3664 | G_cg->write_op(OPC_PTREXPINHERIT); |
| 3665 | G_cs->write((int)argc); |
| 3666 | G_cs->write_obj_id(obj); |
| 3667 | |
| 3668 | /* this removes arguments and the property pointer */ |
| 3669 | G_cg->note_pop(argc + 1); |
| 3670 | } |
| 3671 | |
| 3672 | /* do post-call cleanup: named arg removal, etc */ |
| 3673 | G_cg->post_call_cleanup(named_args); |
| 3674 | |
| 3675 | /* if the result is needed, push it */ |
| 3676 | if (!discard) |
| 3677 | { |
| 3678 | G_cg->write_op(OPC_GETR0); |
| 3679 | G_cg->note_push(); |
| 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | /* ------------------------------------------------------------------------ */ |
| 3684 | /* |
| 3685 | * "delegated" |
| 3686 | */ |
| 3687 | void CTPNDelegated::gen_code(int discard, int for_condition) |
| 3688 | { |
| 3689 | /* |
| 3690 | * we should never be asked to generate a "delegated" node directly; |
| 3691 | * these nodes should always be generated as part of member evaluation |
| 3692 | */ |
| 3693 | G_tok->throw_internal_error(TCERR_GEN_CODE_DELEGATED); |
| 3694 | } |
| 3695 | |
| 3696 | /* |
| 3697 | * evaluate a property |
| 3698 | */ |
| 3699 | void CTPNDelegated::gen_code_member(int discard, |
| 3700 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 3701 | int argc, int varargs, |
| 3702 | CTcNamedArgs *named_args) |
| 3703 | { |
| 3704 | vm_prop_id_t prop; |
| 3705 | |
| 3706 | /* |
| 3707 | * make sure "self" is available - we obviously can't delegate |
| 3708 | * anything if we're not in an object's method |
| 3709 | */ |
| 3710 | if (!G_cs->is_self_available()) |
| 3711 | G_tok->log_error(TCERR_SELF_NOT_AVAIL); |
| 3712 | |
| 3713 | /* don't allow 'delegated' in speculative evaluation mode */ |
| 3714 | if (G_cg->is_speculative()) |
| 3715 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 3716 | |
| 3717 | /* generate the delegatee expression */ |
| 3718 | delegatee_->gen_code(FALSE, FALSE); |
| 3719 | |
| 3720 | /* if we have an argument counter, put it back on top */ |
| 3721 | if (varargs) |
| 3722 | G_cg->write_op(OPC_SWAP); |
| 3723 | |
| 3724 | /* generate the property value */ |
| 3725 | prop = prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 3726 | |
| 3727 | /* |
| 3728 | * if we got a property ID, generate a simple DELEGATE; otherwise, |
| 3729 | * generate a PTRDELEGATE |
| 3730 | */ |
| 3731 | if (prop != VM_INVALID_PROP) |
| 3732 | { |
| 3733 | /* add a varargs modifier if needed */ |
| 3734 | if (varargs) |
| 3735 | G_cg->write_op(OPC_VARARGC); |
| 3736 | |
| 3737 | /* we have a constant property ID - generate a regular DELEGATE */ |
| 3738 | G_cg->write_op(OPC_DELEGATE); |
| 3739 | G_cs->write((char)argc); |
| 3740 | G_cs->write_prop_id(prop); |
| 3741 | |
| 3742 | /* this removes arguments and the object value */ |
| 3743 | G_cg->note_pop(argc + 1); |
| 3744 | } |
| 3745 | else |
| 3746 | { |
| 3747 | /* |
| 3748 | * if we have a varargs list, modify the call instruction that |
| 3749 | * follows to make it a varargs call |
| 3750 | */ |
| 3751 | if (varargs) |
| 3752 | { |
| 3753 | /* swap the top of the stack to get the arg counter back on top */ |
| 3754 | G_cg->write_op(OPC_SWAP); |
| 3755 | |
| 3756 | /* write the varargs modifier */ |
| 3757 | G_cg->write_op(OPC_VARARGC); |
| 3758 | } |
| 3759 | |
| 3760 | /* a property pointer is on the stack - write a PTRDELEGATE */ |
| 3761 | G_cg->write_op(OPC_PTRDELEGATE); |
| 3762 | G_cs->write((int)argc); |
| 3763 | |
| 3764 | /* this removes arguments, the object, and the property pointer */ |
| 3765 | G_cg->note_pop(argc + 2); |
| 3766 | } |
| 3767 | |
| 3768 | /* do post-call cleanup: named arg removal, etc */ |
| 3769 | G_cg->post_call_cleanup(named_args); |
| 3770 | |
| 3771 | /* if the result is needed, push it */ |
| 3772 | if (!discard) |
| 3773 | { |
| 3774 | G_cg->write_op(OPC_GETR0); |
| 3775 | G_cg->note_push(); |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | |
| 3780 | |
| 3781 | /* ------------------------------------------------------------------------ */ |
| 3782 | /* |
| 3783 | * "argcount" |
| 3784 | */ |
| 3785 | void CTPNArgc::gen_code(int discard, int) |
| 3786 | { |
| 3787 | /* generate the argument count, if we're not discarding */ |
| 3788 | if (!discard) |
| 3789 | { |
| 3790 | if (G_cg->is_eval_for_debug()) |
| 3791 | { |
| 3792 | /* generate a debug argument count evaluation */ |
| 3793 | G_cg->write_op(OPC_GETDBARGC); |
| 3794 | G_cs->write2(G_cg->get_debug_stack_level()); |
| 3795 | } |
| 3796 | else |
| 3797 | { |
| 3798 | /* generate the normal argument count evaluation */ |
| 3799 | G_cg->write_op(OPC_GETARGC); |
| 3800 | } |
| 3801 | |
| 3802 | /* we push one element */ |
| 3803 | G_cg->note_push(); |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | |
| 3808 | /* ------------------------------------------------------------------------ */ |
| 3809 | /* |
| 3810 | * constant |
| 3811 | */ |
| 3812 | void CTPNConst::gen_code(int discard, int) |
| 3813 | { |
| 3814 | /* if we're discarding the value, do nothing */ |
| 3815 | if (discard) |
| 3816 | return; |
| 3817 | |
| 3818 | /* generate the appropriate type of push for the value */ |
| 3819 | switch(val_.get_type()) |
| 3820 | { |
| 3821 | case TC_CVT_NIL: |
| 3822 | G_cg->write_op(OPC_PUSHNIL); |
| 3823 | break; |
| 3824 | |
| 3825 | case TC_CVT_TRUE: |
| 3826 | G_cg->write_op(OPC_PUSHTRUE); |
| 3827 | break; |
| 3828 | |
| 3829 | case TC_CVT_INT: |
| 3830 | /* write the push-integer instruction */ |
| 3831 | s_gen_code_int(val_.get_val_int()); |
| 3832 | |
| 3833 | /* s_gen_code_int notes a push, which we'll do also, so cancel it */ |
| 3834 | G_cg->note_pop(); |
| 3835 | break; |
| 3836 | |
| 3837 | case TC_CVT_FLOAT: |
| 3838 | /* write the push-object instruction for the BigNumber object */ |
| 3839 | G_cg->write_op(OPC_PUSHOBJ); |
| 3840 | |
| 3841 | /* generate the BigNumber object and write its ID */ |
| 3842 | G_cs->write_obj_id(G_cg->gen_bignum_obj( |
| 3843 | val_.get_val_float(), val_.get_val_float_len(), |
| 3844 | val_.is_promoted())); |
| 3845 | break; |
| 3846 | |
| 3847 | case TC_CVT_RESTR: |
| 3848 | /* write the push-object instruction for the RexPattern object */ |
| 3849 | G_cg->write_op(OPC_PUSHOBJ); |
| 3850 | |
| 3851 | /* generate the RexPattern object and write its ID */ |
| 3852 | G_cs->write_obj_id(G_cg->gen_rexpat_obj( |
| 3853 | val_.get_val_str(), val_.get_val_str_len())); |
| 3854 | break; |
| 3855 | |
| 3856 | case TC_CVT_SSTR: |
| 3857 | /* generate the string */ |
| 3858 | s_gen_code_str(&val_); |
| 3859 | |
| 3860 | /* s_gen_code_int notes a push, which we'll do also, so cancel it */ |
| 3861 | G_cg->note_pop(); |
| 3862 | break; |
| 3863 | |
| 3864 | case TC_CVT_LIST: |
| 3865 | /* write the instruction */ |
| 3866 | G_cg->write_op(OPC_PUSHLST); |
| 3867 | |
| 3868 | /* |
| 3869 | * add the list to the constant pool, creating a fixup at the |
| 3870 | * current code stream location |
| 3871 | */ |
| 3872 | G_cg->add_const_list(val_.get_val_list(), G_cs, G_cs->get_ofs()); |
| 3873 | |
| 3874 | /* |
| 3875 | * write a placeholder address - this will be corrected by the |
| 3876 | * fixup that add_const_list() created for us |
| 3877 | */ |
| 3878 | G_cs->write4(0); |
| 3879 | break; |
| 3880 | |
| 3881 | case TC_CVT_OBJ: |
| 3882 | /* generate the object ID */ |
| 3883 | G_cg->write_op(OPC_PUSHOBJ); |
| 3884 | G_cs->write_obj_id(val_.get_val_obj()); |
| 3885 | break; |
| 3886 | |
| 3887 | case TC_CVT_PROP: |
| 3888 | /* generate the property address */ |
| 3889 | G_cg->write_op(OPC_PUSHPROPID); |
| 3890 | G_cs->write_prop_id(val_.get_val_prop()); |
| 3891 | break; |
| 3892 | |
| 3893 | case TC_CVT_ENUM: |
| 3894 | /* generate the enum value */ |
| 3895 | G_cg->write_op(OPC_PUSHENUM); |
| 3896 | G_cs->write_enum_id(val_.get_val_enum()); |
| 3897 | break; |
| 3898 | |
| 3899 | case TC_CVT_FUNCPTR: |
| 3900 | /* generate the function pointer instruction */ |
| 3901 | G_cg->write_op(OPC_PUSHFNPTR); |
| 3902 | |
| 3903 | /* add a fixup for the function address */ |
| 3904 | val_.get_val_funcptr_sym()->add_abs_fixup(G_cs); |
| 3905 | |
| 3906 | /* write out a placeholder - arbitrarily use zero */ |
| 3907 | G_cs->write4(0); |
| 3908 | break; |
| 3909 | |
| 3910 | case TC_CVT_ANONFUNCPTR: |
| 3911 | /* generate the function pointer instruction */ |
| 3912 | G_cg->write_op(OPC_PUSHFNPTR); |
| 3913 | |
| 3914 | /* add a fixup for the code body address */ |
| 3915 | val_.get_val_anon_func_ptr()->add_abs_fixup(G_cs); |
| 3916 | |
| 3917 | /* write our a placeholder */ |
| 3918 | G_cs->write4(0); |
| 3919 | break; |
| 3920 | |
| 3921 | default: |
| 3922 | /* anything else is an internal error */ |
| 3923 | G_tok->throw_internal_error(TCERR_GEN_UNK_CONST_TYPE); |
| 3924 | } |
| 3925 | |
| 3926 | /* all of these push a value */ |
| 3927 | G_cg->note_push(); |
| 3928 | } |
| 3929 | |
| 3930 | int CTPNConst::gen_code_asi(int discard, int phase, |
| 3931 | tc_asitype_t typ, const char *op, |
| 3932 | CTcPrsNode *, |
| 3933 | int ignore_errors, int, void **) |
| 3934 | { |
| 3935 | /* |
| 3936 | * If ignoring errors, just indicate that we can't do the assignment. |
| 3937 | * Also ignore errors on phase 2, since we'll already have generated an |
| 3938 | * error during phase 1. |
| 3939 | */ |
| 3940 | if (ignore_errors || phase > 1) |
| 3941 | return FALSE; |
| 3942 | |
| 3943 | /* |
| 3944 | * We can't assign to a constant. The parser will catch most errors of |
| 3945 | * this type before we ever get here, but for expressions that resolve |
| 3946 | * to global symbols (such as objects or functions), it can't know |
| 3947 | * until code generation time that the expression won't be assignable. |
| 3948 | */ |
| 3949 | G_tok->log_error(typ == TC_ASI_PREINC || typ == TC_ASI_PREDEC |
| 3950 | || typ == TC_ASI_POSTINC || typ == TC_ASI_POSTDEC |
| 3951 | ? TCERR_INVALID_UNARY_LVALUE : TCERR_INVALID_LVALUE, |
| 3952 | op); |
| 3953 | return FALSE; |
| 3954 | } |
| 3955 | |
| 3956 | |
| 3957 | /* |
| 3958 | * generate code to push an integer value |
| 3959 | */ |
| 3960 | void CTPNConst::s_gen_code_int(long intval) |
| 3961 | { |
| 3962 | /* push the smallest format that will fit the value */ |
| 3963 | if (intval == 0) |
| 3964 | { |
| 3965 | /* write the special PUSH_0 instruction */ |
| 3966 | G_cg->write_op(OPC_PUSH_0); |
| 3967 | } |
| 3968 | else if (intval == 1) |
| 3969 | { |
| 3970 | /* write the special PUSH_1 instruction */ |
| 3971 | G_cg->write_op(OPC_PUSH_1); |
| 3972 | } |
| 3973 | else if (intval < 127 && intval >= -128) |
| 3974 | { |
| 3975 | /* it fits in eight bits */ |
| 3976 | G_cg->write_op(OPC_PUSHINT8); |
| 3977 | G_cs->write((char)intval); |
| 3978 | } |
| 3979 | else |
| 3980 | { |
| 3981 | /* it doesn't fit in 8 bits - use a full 32 bits */ |
| 3982 | G_cg->write_op(OPC_PUSHINT); |
| 3983 | G_cs->write4(intval); |
| 3984 | } |
| 3985 | |
| 3986 | /* however we did it, we left one value on the stack */ |
| 3987 | G_cg->note_push(); |
| 3988 | } |
| 3989 | |
| 3990 | /* |
| 3991 | * Generate code to push a symbol's name as a string constant value |
| 3992 | */ |
| 3993 | void CTPNConst::s_gen_code_str(const CTcSymbol *sym) |
| 3994 | { |
| 3995 | /* generate the string for the symbol name */ |
| 3996 | s_gen_code_str(sym->get_sym(), sym->get_sym_len()); |
| 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * Generate code to push a symbol's name as a string constant value, |
| 4001 | * according to the code generation mode. |
| 4002 | */ |
| 4003 | void CTPNConst::s_gen_code_str_by_mode(const CTcSymbol *sym) |
| 4004 | { |
| 4005 | /* check the mode */ |
| 4006 | if (G_cg->is_eval_for_dyn()) |
| 4007 | { |
| 4008 | /* dynamic evaluation - use in-line strings */ |
| 4009 | CTPNDebugConst::s_gen_code_str(sym->get_sym(), sym->get_sym_len()); |
| 4010 | } |
| 4011 | else |
| 4012 | { |
| 4013 | /* static compilation - use the constant pool */ |
| 4014 | s_gen_code_str(sym->get_sym(), sym->get_sym_len()); |
| 4015 | } |
| 4016 | } |
| 4017 | |
| 4018 | /* |
| 4019 | * Generate code to push a string constant value |
| 4020 | */ |
| 4021 | void CTPNConst::s_gen_code_str(const CTcConstVal *val) |
| 4022 | { |
| 4023 | s_gen_code_str(val->get_val_str(), val->get_val_str_len()); |
| 4024 | } |
| 4025 | |
| 4026 | /* |
| 4027 | * Generate code to push a string constant value |
| 4028 | */ |
| 4029 | void CTPNConst::s_gen_code_str(const char *str, size_t len) |
| 4030 | { |
| 4031 | /* write the instruction to push a constant pool string */ |
| 4032 | G_cg->write_op(OPC_PUSHSTR); |
| 4033 | |
| 4034 | /* note the push */ |
| 4035 | G_cg->note_push(); |
| 4036 | |
| 4037 | /* |
| 4038 | * add the string to the constant pool, creating a fixup at the current |
| 4039 | * code stream location |
| 4040 | */ |
| 4041 | G_cg->add_const_str(str, len, G_cs, G_cs->get_ofs()); |
| 4042 | |
| 4043 | /* |
| 4044 | * write a placeholder address - this will be corrected by the fixup |
| 4045 | * that add_const_str() created for us |
| 4046 | */ |
| 4047 | G_cs->write4(0); |
| 4048 | } |
| 4049 | |
| 4050 | /* |
| 4051 | * Generate code to apply operator 'new' to the constant. We can apply |
| 4052 | * 'new' only to constant object values. |
| 4053 | */ |
| 4054 | void CTPNConst::gen_code_new(int discard, |
| 4055 | int argc, int varargs, CTcNamedArgs *named_args, |
| 4056 | int /*from_call*/, int is_transient) |
| 4057 | { |
| 4058 | /* check the type */ |
| 4059 | switch(val_.get_type()) |
| 4060 | { |
| 4061 | case TC_CVT_OBJ: |
| 4062 | /* |
| 4063 | * Treat this the same as any other 'new' call. An object symbol |
| 4064 | * folded into a constant is guaranteed to be of metaclass |
| 4065 | * TadsObject - that's the only kind of symbol we'll ever fold this |
| 4066 | * way. |
| 4067 | */ |
| 4068 | CTcSymObj::s_gen_code_new(discard, |
| 4069 | val_.get_val_obj(), val_.get_val_obj_meta(), |
| 4070 | argc, varargs, named_args, is_transient); |
| 4071 | break; |
| 4072 | |
| 4073 | default: |
| 4074 | /* can't apply 'new' to other constant values */ |
| 4075 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 4076 | break; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | /* |
| 4081 | * Generate code to make a function call to this expression. If we're |
| 4082 | * calling a function, we can generate this directly. |
| 4083 | */ |
| 4084 | void CTPNConst::gen_code_call(int discard, int argc, |
| 4085 | int varargs, CTcNamedArgs *named_args) |
| 4086 | { |
| 4087 | /* check our type */ |
| 4088 | switch(val_.get_type()) |
| 4089 | { |
| 4090 | case TC_CVT_FUNCPTR: |
| 4091 | /* generate a call to our function symbol */ |
| 4092 | val_.get_val_funcptr_sym()->gen_code_call( |
| 4093 | discard, argc, varargs, named_args); |
| 4094 | break; |
| 4095 | |
| 4096 | default: |
| 4097 | /* other types cannot be called */ |
| 4098 | G_tok->log_error(TCERR_CANNOT_CALL_CONST); |
| 4099 | break; |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | /* |
| 4104 | * generate a property ID expression |
| 4105 | */ |
| 4106 | vm_prop_id_t CTPNConst::gen_code_propid(int check_only, int is_expr) |
| 4107 | { |
| 4108 | /* check the type */ |
| 4109 | switch(val_.get_type()) |
| 4110 | { |
| 4111 | case TC_CVT_PROP: |
| 4112 | /* return the constant property ID */ |
| 4113 | return (vm_prop_id_t)val_.get_val_prop(); |
| 4114 | |
| 4115 | default: |
| 4116 | /* other values cannot be used as properties */ |
| 4117 | if (!check_only) |
| 4118 | G_tok->log_error(TCERR_INVAL_PROP_EXPR); |
| 4119 | return VM_INVALID_PROP; |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | |
| 4124 | /* |
| 4125 | * Generate code for a member evaluation |
| 4126 | */ |
| 4127 | void CTPNConst::gen_code_member(int discard, |
| 4128 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 4129 | int argc, int varargs, CTcNamedArgs *named_args) |
| 4130 | { |
| 4131 | /* check our constant type */ |
| 4132 | switch(val_.get_type()) |
| 4133 | { |
| 4134 | case TC_CVT_OBJ: |
| 4135 | /* call the object symbol code to do the work */ |
| 4136 | CTcSymObj::s_gen_code_member(discard, prop_expr, prop_is_expr, |
| 4137 | argc, val_.get_val_obj(), |
| 4138 | varargs, named_args); |
| 4139 | break; |
| 4140 | |
| 4141 | case TC_CVT_LIST: |
| 4142 | case TC_CVT_SSTR: |
| 4143 | case TC_CVT_RESTR: |
| 4144 | case TC_CVT_FLOAT: |
| 4145 | /* |
| 4146 | * list/string/BigNumber constant - generate our value as |
| 4147 | * normal, then use the standard member generation |
| 4148 | */ |
| 4149 | gen_code(FALSE, FALSE); |
| 4150 | |
| 4151 | /* if we have an argument counter, put it back on top */ |
| 4152 | if (varargs) |
| 4153 | G_cg->write_op(OPC_SWAP); |
| 4154 | |
| 4155 | /* use standard member generation */ |
| 4156 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 4157 | argc, varargs, named_args); |
| 4158 | break; |
| 4159 | |
| 4160 | default: |
| 4161 | G_tok->log_error(TCERR_INVAL_OBJ_EXPR); |
| 4162 | break; |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | |
| 4167 | /* |
| 4168 | * generate code for an object before a '.' |
| 4169 | */ |
| 4170 | vm_obj_id_t CTPNConst::gen_code_obj_predot(int *is_self) |
| 4171 | { |
| 4172 | /* we're certainly not "self" */ |
| 4173 | *is_self = FALSE; |
| 4174 | |
| 4175 | /* if I don't have an object value, this is illegal */ |
| 4176 | if (val_.get_type() != TC_CVT_OBJ) |
| 4177 | { |
| 4178 | G_tok->log_error(TCERR_INVAL_OBJ_EXPR); |
| 4179 | return VM_INVALID_OBJ; |
| 4180 | } |
| 4181 | |
| 4182 | /* report our constant object value */ |
| 4183 | return val_.get_val_obj(); |
| 4184 | } |
| 4185 | |
| 4186 | /* ------------------------------------------------------------------------ */ |
| 4187 | /* |
| 4188 | * debugger constant |
| 4189 | */ |
| 4190 | void CTPNDebugConst::gen_code(int discard, int for_condition) |
| 4191 | { |
| 4192 | /* if we're discarding the value, do nothing */ |
| 4193 | if (discard) |
| 4194 | return; |
| 4195 | |
| 4196 | /* generate the appropriate type of push for the value */ |
| 4197 | switch(val_.get_type()) |
| 4198 | { |
| 4199 | case TC_CVT_SSTR: |
| 4200 | if (val_.get_val_str() == 0) |
| 4201 | { |
| 4202 | /* validate the string address */ |
| 4203 | if (G_vmifc |
| 4204 | && !G_vmifc->validate_pool_str(val_.get_val_str_ofs())) |
| 4205 | { |
| 4206 | /* treat it as nil */ |
| 4207 | G_cg->write_op(OPC_PUSHNIL); |
| 4208 | G_cg->note_push(); |
| 4209 | break; |
| 4210 | } |
| 4211 | |
| 4212 | /* we have a pre-resolved pool offset */ |
| 4213 | G_cg->write_op(OPC_PUSHSTR); |
| 4214 | G_cs->write4(val_.get_val_str_ofs()); |
| 4215 | G_cg->note_push(); |
| 4216 | } |
| 4217 | else |
| 4218 | { |
| 4219 | /* write the in-line string instruction */ |
| 4220 | s_gen_code_str(val_.get_val_str(), val_.get_val_str_len()); |
| 4221 | } |
| 4222 | break; |
| 4223 | |
| 4224 | case TC_CVT_LIST: |
| 4225 | if (val_.get_val_list() == 0) |
| 4226 | { |
| 4227 | /* validate the list address */ |
| 4228 | if (G_vmifc |
| 4229 | && !G_vmifc->validate_pool_list(val_.get_val_list_ofs())) |
| 4230 | { |
| 4231 | /* treat it as nil */ |
| 4232 | G_cg->write_op(OPC_PUSHNIL); |
| 4233 | G_cg->note_push(); |
| 4234 | break; |
| 4235 | } |
| 4236 | |
| 4237 | /* we have a pre-resolved pool offset */ |
| 4238 | G_cg->write_op(OPC_PUSHLST); |
| 4239 | G_cs->write4(val_.get_val_list_ofs()); |
| 4240 | G_cg->note_push(); |
| 4241 | } |
| 4242 | else |
| 4243 | { |
| 4244 | /* we should never have a regular constant list when debugging */ |
| 4245 | assert(FALSE); |
| 4246 | } |
| 4247 | break; |
| 4248 | |
| 4249 | case TC_CVT_FUNCPTR: |
| 4250 | /* generate the function pointer instruction */ |
| 4251 | G_cg->write_op(OPC_PUSHFNPTR); |
| 4252 | |
| 4253 | /* |
| 4254 | * write the actual function address - no need for fixups in the |
| 4255 | * debugger, since everything's fully resolved |
| 4256 | */ |
| 4257 | G_cs->write4(val_.get_val_funcptr_sym()->get_code_pool_addr()); |
| 4258 | |
| 4259 | /* note the value push */ |
| 4260 | G_cg->note_push(); |
| 4261 | break; |
| 4262 | |
| 4263 | case TC_CVT_BIFPTR: |
| 4264 | /* validate the property ID if possible */ |
| 4265 | if (G_vmifc && |
| 4266 | !G_vmifc->validate_bif( |
| 4267 | val_.get_val_bifptr_sym()->get_func_set_id(), |
| 4268 | val_.get_val_bifptr_sym()->get_func_idx())) |
| 4269 | { |
| 4270 | /* treat it as nil */ |
| 4271 | G_cg->write_op(OPC_PUSHNIL); |
| 4272 | G_cg->note_push(); |
| 4273 | break; |
| 4274 | } |
| 4275 | |
| 4276 | /* generate the built-in function pointer instruction */ |
| 4277 | G_cg->write_op(OPC_PUSHBIFPTR); |
| 4278 | G_cs->write2(val_.get_val_bifptr_sym()->get_func_idx()); |
| 4279 | G_cs->write2(val_.get_val_bifptr_sym()->get_func_set_id()); |
| 4280 | |
| 4281 | /* note the value push */ |
| 4282 | G_cg->note_push(); |
| 4283 | break; |
| 4284 | |
| 4285 | case TC_CVT_ANONFUNCPTR: |
| 4286 | /* |
| 4287 | * we should never see an anonymous function pointer constant in |
| 4288 | * the debugger - these will always be represented as objects |
| 4289 | * intead |
| 4290 | */ |
| 4291 | assert(FALSE); |
| 4292 | break; |
| 4293 | |
| 4294 | case TC_CVT_FLOAT: |
| 4295 | { |
| 4296 | /* |
| 4297 | * find the 'BigNumber' metaclass - if it's not defined, we |
| 4298 | * can't create BigNumber values |
| 4299 | */ |
| 4300 | CTcSymMetaclass *sym = |
| 4301 | (CTcSymMetaclass *)G_prs->get_global_symtab()->find( |
| 4302 | "BigNumber", 9); |
| 4303 | if (sym == 0 || sym->get_type() != TC_SYM_METACLASS) |
| 4304 | err_throw(VMERR_INVAL_DBG_EXPR); |
| 4305 | |
| 4306 | /* push the floating value as an immediate string */ |
| 4307 | G_cg->write_op(OPC_PUSHSTRI); |
| 4308 | G_cs->write2(val_.get_val_str_len()); |
| 4309 | G_cs->write(val_.get_val_str(), val_.get_val_str_len()); |
| 4310 | |
| 4311 | /* create the new BigNumber object from the string */ |
| 4312 | G_cg->write_op(OPC_NEW2); |
| 4313 | G_cs->write2(1); |
| 4314 | G_cs->write2(sym->get_meta_idx()); |
| 4315 | |
| 4316 | /* retrieve the value */ |
| 4317 | G_cg->write_op(OPC_GETR0); |
| 4318 | |
| 4319 | /* |
| 4320 | * note the net push of one value (we pushed the argument, |
| 4321 | * popped the argument, and pushed the new object) |
| 4322 | */ |
| 4323 | G_cg->note_push(); |
| 4324 | } |
| 4325 | break; |
| 4326 | |
| 4327 | case TC_CVT_RESTR: |
| 4328 | { |
| 4329 | /* find the RexPattern metaclass */ |
| 4330 | CTcSymMetaclass *sym = |
| 4331 | (CTcSymMetaclass *)G_prs->get_global_symtab()->find( |
| 4332 | "RexPattern", 10); |
| 4333 | if (sym == 0 || sym->get_type() != TC_SYM_METACLASS) |
| 4334 | err_throw(VMERR_INVAL_DBG_EXPR); |
| 4335 | |
| 4336 | /* push the pattern source string value as an immediate string */ |
| 4337 | G_cg->write_op(OPC_PUSHSTRI); |
| 4338 | G_cs->write2(val_.get_val_str_len()); |
| 4339 | G_cs->write(val_.get_val_str(), val_.get_val_str_len()); |
| 4340 | |
| 4341 | /* create the new RexPattern object from the string */ |
| 4342 | G_cg->write_op(OPC_NEW2); |
| 4343 | G_cs->write2(1); |
| 4344 | G_cs->write2(sym->get_meta_idx()); |
| 4345 | |
| 4346 | /* retrieve the value */ |
| 4347 | G_cg->write_op(OPC_GETR0); |
| 4348 | |
| 4349 | /* |
| 4350 | * note the net push of one value (we pushed the argument, |
| 4351 | * popped the argument, and pushed the new object) |
| 4352 | */ |
| 4353 | G_cg->note_push(); |
| 4354 | } |
| 4355 | break; |
| 4356 | |
| 4357 | case TC_CVT_OBJ: |
| 4358 | /* validate the object ID if possible */ |
| 4359 | if (G_vmifc && !G_vmifc->validate_obj(val_.get_val_obj())) |
| 4360 | { |
| 4361 | /* treat it as nil */ |
| 4362 | G_cg->write_op(OPC_PUSHNIL); |
| 4363 | G_cg->note_push(); |
| 4364 | break; |
| 4365 | } |
| 4366 | |
| 4367 | /* use the default handling */ |
| 4368 | goto use_default; |
| 4369 | |
| 4370 | case TC_CVT_PROP: |
| 4371 | /* validate the property ID if possible */ |
| 4372 | if (G_vmifc && |
| 4373 | !G_vmifc->validate_prop((tctarg_prop_id_t)val_.get_val_prop())) |
| 4374 | { |
| 4375 | /* treat it as nil */ |
| 4376 | G_cg->write_op(OPC_PUSHNIL); |
| 4377 | G_cg->note_push(); |
| 4378 | break; |
| 4379 | } |
| 4380 | |
| 4381 | /* use the default handling */ |
| 4382 | goto use_default; |
| 4383 | |
| 4384 | default: |
| 4385 | use_default: |
| 4386 | /* handle normally for anything else */ |
| 4387 | CTPNConst::gen_code(discard, for_condition); |
| 4388 | break; |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | /* |
| 4393 | * Generate a string value in a debug/dynamic code context. This generates |
| 4394 | * an in-line string rather than a constant pool reference. |
| 4395 | */ |
| 4396 | void CTPNDebugConst::s_gen_code_str(const char *str, size_t len) |
| 4397 | { |
| 4398 | /* write the PUSHSTRI <len> <string bytes> */ |
| 4399 | G_cg->write_op(OPC_PUSHSTRI); |
| 4400 | G_cs->write2(len); |
| 4401 | G_cs->write(str, len); |
| 4402 | |
| 4403 | /* note the value push */ |
| 4404 | G_cg->note_push(); |
| 4405 | } |
| 4406 | |
| 4407 | |
| 4408 | /* ------------------------------------------------------------------------ */ |
| 4409 | /* |
| 4410 | * Generic Unary Operator |
| 4411 | */ |
| 4412 | |
| 4413 | /* |
| 4414 | * Generate a unary-operator opcode. We assume that the opcode has no |
| 4415 | * side effects other than to compute the result, so we do not generate |
| 4416 | * the opcode at all if 'discard' is true; we do, however, always |
| 4417 | * generate code for the subexpression to ensure that its side effects |
| 4418 | * are performed. |
| 4419 | * |
| 4420 | * In most cases, the caller simply should pass through its 'discard' |
| 4421 | * status, since the result of the subexpression is generally needed |
| 4422 | * only when the result of the enclosing expression is needed. |
| 4423 | * |
| 4424 | * In most cases, the caller should pass FALSE for 'for_condition', |
| 4425 | * because applying an operator to the result generally requires that |
| 4426 | * the result be properly converted for use as a temporary value. |
| 4427 | * However, when the caller knows that its own opcode will perform the |
| 4428 | * same conversions that a conditional opcode would, 'for_condition' |
| 4429 | * should be TRUE. In most cases, the caller's own 'for_condition' |
| 4430 | * status is not relevant and should thus not be passed through. |
| 4431 | */ |
| 4432 | void CTPNUnary::gen_unary(uchar opc, int discard, int for_condition) |
| 4433 | { |
| 4434 | /* |
| 4435 | * Generate the operand. Pass through the 'discard' status to the |
| 4436 | * operand - if the result of the parent operator is being |
| 4437 | * discarded, then so is the result of this subexpression. In |
| 4438 | * addition, pass through the caller's 'for_condition' disposition. |
| 4439 | */ |
| 4440 | sub_->gen_code(discard, for_condition); |
| 4441 | |
| 4442 | /* apply the operator if we're not discarding the result */ |
| 4443 | if (!discard) |
| 4444 | G_cg->write_op(opc); |
| 4445 | } |
| 4446 | |
| 4447 | /* ------------------------------------------------------------------------ */ |
| 4448 | /* |
| 4449 | * Generic Binary Operator |
| 4450 | */ |
| 4451 | |
| 4452 | /* |
| 4453 | * Generate a binary-operator opcode. |
| 4454 | * |
| 4455 | * In most cases, the caller's 'discard' status should be passed |
| 4456 | * through, since the results of the operands are usually needed if and |
| 4457 | * only if the results of the enclosing expression are needed. |
| 4458 | * |
| 4459 | * In most cases, the caller should pass FALSE for 'for_condition'. |
| 4460 | * Only when the caller knows that the opcode will perform the same |
| 4461 | * conversions as a BOOLIZE instruction should it pass TRUE for |
| 4462 | * 'for_condition'. |
| 4463 | */ |
| 4464 | void CTPNBin::gen_binary(uchar opc, int discard, int for_condition) |
| 4465 | { |
| 4466 | /* |
| 4467 | * generate the operands, passing through the discard and |
| 4468 | * conditional status |
| 4469 | */ |
| 4470 | left_->gen_code(discard, for_condition); |
| 4471 | right_->gen_code(discard, for_condition); |
| 4472 | |
| 4473 | /* generate our operand if we're not discarding the result */ |
| 4474 | if (!discard) |
| 4475 | { |
| 4476 | /* apply the operator */ |
| 4477 | G_cg->write_op(opc); |
| 4478 | |
| 4479 | /* |
| 4480 | * binary operators all remove two values and push one, so there's |
| 4481 | * a net pop |
| 4482 | */ |
| 4483 | G_cg->note_pop(); |
| 4484 | } |
| 4485 | } |
| 4486 | |
| 4487 | /* |
| 4488 | * Generate a binary-operator opcode for a compound assignment. This omits |
| 4489 | * generating the left operand, because the compound assignment operator |
| 4490 | * will already have done that. |
| 4491 | */ |
| 4492 | void CTPNBin::gen_binary_ca(uchar opc) |
| 4493 | { |
| 4494 | /* generate the right operand (the left is already on the stack) */ |
| 4495 | right_->gen_code(FALSE, FALSE); |
| 4496 | |
| 4497 | /* generate the operator */ |
| 4498 | G_cg->write_op(opc); |
| 4499 | |
| 4500 | /* a binary op removes two values and pushes one */ |
| 4501 | G_cg->note_pop(); |
| 4502 | } |
| 4503 | |
| 4504 | |
| 4505 | /* ------------------------------------------------------------------------ */ |
| 4506 | /* |
| 4507 | * logical NOT |
| 4508 | */ |
| 4509 | void CTPNNot::gen_code(int discard, int) |
| 4510 | { |
| 4511 | /* |
| 4512 | * Generate the subexpression and apply the NOT opcode. Note that |
| 4513 | * we can compute the subexpression as though we were applying a |
| 4514 | * condition, because the NOT opcode takes exactly the same kind of |
| 4515 | * input as any condition opcode; we can thus avoid an extra |
| 4516 | * conversion in some cases. |
| 4517 | */ |
| 4518 | gen_unary(OPC_NOT, discard, TRUE); |
| 4519 | } |
| 4520 | |
| 4521 | /* ------------------------------------------------------------------------ */ |
| 4522 | /* |
| 4523 | * Boolean-ize operator |
| 4524 | */ |
| 4525 | void CTPNBoolize::gen_code(int discard, int for_condition) |
| 4526 | { |
| 4527 | /* |
| 4528 | * If the result will be used for a conditional, there's no need to |
| 4529 | * generate an instruction to convert the value to boolean. The opcode |
| 4530 | * that will be used for the condition will perform exactly the same |
| 4531 | * conversions that this opcode would apply; avoid the redundant work |
| 4532 | * in this case, and simply generate the underlying expression |
| 4533 | * directly. |
| 4534 | */ |
| 4535 | if (for_condition) |
| 4536 | { |
| 4537 | /* generate the underlying expression without modification */ |
| 4538 | sub_->gen_code(discard, for_condition); |
| 4539 | |
| 4540 | /* done */ |
| 4541 | return; |
| 4542 | } |
| 4543 | |
| 4544 | /* |
| 4545 | * if the sub-expression is already guaranteed to be a boolean value, |
| 4546 | * there's nothing we need to do |
| 4547 | */ |
| 4548 | if (sub_->is_bool()) |
| 4549 | return; |
| 4550 | |
| 4551 | /* |
| 4552 | * Generate the subexpression and apply the BOOLIZE operator. Since |
| 4553 | * we're explicitly boolean-izing the value, there's no need for the |
| 4554 | * subexpression to do the same thing, so the subexpression can |
| 4555 | * pretend it's generating for a conditional. |
| 4556 | */ |
| 4557 | gen_unary(OPC_BOOLIZE, discard, TRUE); |
| 4558 | } |
| 4559 | |
| 4560 | |
| 4561 | /* ------------------------------------------------------------------------ */ |
| 4562 | /* |
| 4563 | * bitwise NOT |
| 4564 | */ |
| 4565 | void CTPNBNot::gen_code(int discard, int) |
| 4566 | { |
| 4567 | gen_unary(OPC_BNOT, discard, FALSE); |
| 4568 | } |
| 4569 | |
| 4570 | |
| 4571 | /* ------------------------------------------------------------------------ */ |
| 4572 | /* |
| 4573 | * arithmetic positive |
| 4574 | */ |
| 4575 | void CTPNPos::gen_code(int discard, int) |
| 4576 | { |
| 4577 | /* |
| 4578 | * simply generate our operand, since the operator itself has no |
| 4579 | * effect |
| 4580 | */ |
| 4581 | sub_->gen_code(discard, FALSE); |
| 4582 | } |
| 4583 | |
| 4584 | /* ------------------------------------------------------------------------ */ |
| 4585 | /* |
| 4586 | * unary arithmetic negative |
| 4587 | */ |
| 4588 | void CTPNNeg::gen_code(int discard, int) |
| 4589 | { |
| 4590 | gen_unary(OPC_NEG, discard, FALSE); |
| 4591 | } |
| 4592 | |
| 4593 | /* ------------------------------------------------------------------------ */ |
| 4594 | /* |
| 4595 | * pre-increment |
| 4596 | */ |
| 4597 | void CTPNPreInc::gen_code(int discard, int) |
| 4598 | { |
| 4599 | /* use the static handler */ |
| 4600 | CTPNPreInc_gen_code(sub_, discard); |
| 4601 | } |
| 4602 | |
| 4603 | /* static pre-inc code generator, for sharing with other nodes */ |
| 4604 | void CTPNPreInc_gen_code(CTcPrsNode *sub, int discard) |
| 4605 | { |
| 4606 | /* ask the subnode to generate it */ |
| 4607 | void *ctx; |
| 4608 | if (!sub->gen_code_asi(discard, 1, TC_ASI_PREINC, "++", |
| 4609 | 0, FALSE, TRUE, &ctx)) |
| 4610 | { |
| 4611 | /* increment the value at top of stack */ |
| 4612 | G_cg->write_op(OPC_INC); |
| 4613 | |
| 4614 | /* |
| 4615 | * generate a simple assignment back to the subexpression; if |
| 4616 | * we're using the value, let the simple assignment leave its |
| 4617 | * value on the stack, since the result is the value *after* the |
| 4618 | * increment |
| 4619 | */ |
| 4620 | sub->gen_code_asi(discard, 2, TC_ASI_PREINC, "++", |
| 4621 | 0, FALSE, TRUE, &ctx); |
| 4622 | } |
| 4623 | } |
| 4624 | |
| 4625 | /* ------------------------------------------------------------------------ */ |
| 4626 | /* |
| 4627 | * pre-decrement |
| 4628 | */ |
| 4629 | void CTPNPreDec::gen_code(int discard, int) |
| 4630 | { |
| 4631 | /* ask the subnode to generate it */ |
| 4632 | void *ctx; |
| 4633 | if (!sub_->gen_code_asi(discard, 1, TC_ASI_PREDEC, "--", |
| 4634 | 0, FALSE, TRUE, &ctx)) |
| 4635 | { |
| 4636 | /* decrement the value at top of stack */ |
| 4637 | G_cg->write_op(OPC_DEC); |
| 4638 | |
| 4639 | /* |
| 4640 | * generate a simple assignment back to the subexpression; if |
| 4641 | * we're using the value, let the simple assignment leave its |
| 4642 | * value on the stack, since the result is the value *after* the |
| 4643 | * decrement |
| 4644 | */ |
| 4645 | sub_->gen_code_asi(discard, 2, TC_ASI_PREDEC, "--", |
| 4646 | 0, FALSE, TRUE, &ctx); |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | /* ------------------------------------------------------------------------ */ |
| 4651 | /* |
| 4652 | * post-increment |
| 4653 | */ |
| 4654 | void CTPNPostInc::gen_code(int discard, int) |
| 4655 | { |
| 4656 | /* ask the subnode to generate it */ |
| 4657 | void *ctx; |
| 4658 | if (!sub_->gen_code_asi(discard, 1, TC_ASI_POSTINC, "++", |
| 4659 | 0, FALSE, TRUE, &ctx)) |
| 4660 | { |
| 4661 | /* |
| 4662 | * if we're keeping the result, duplicate the value at top of |
| 4663 | * stack prior to the increment - since this is a |
| 4664 | * post-increment, the result is the value *before* the |
| 4665 | * increment |
| 4666 | */ |
| 4667 | if (!discard) |
| 4668 | { |
| 4669 | G_cg->write_op(OPC_DUP); |
| 4670 | G_cg->note_push(); |
| 4671 | } |
| 4672 | |
| 4673 | /* increment the value at top of stack */ |
| 4674 | G_cg->write_op(OPC_INC); |
| 4675 | |
| 4676 | /* |
| 4677 | * Generate a simple assignment back to the subexpression. |
| 4678 | * Discard the result of this assignment, regardless of whether |
| 4679 | * the caller wants the result of the overall expression, |
| 4680 | * because we've already pushed the actual result, which is the |
| 4681 | * original value before the increment operation. |
| 4682 | */ |
| 4683 | sub_->gen_code_asi(TRUE, 2, TC_ASI_POSTINC, "++", |
| 4684 | 0, FALSE, TRUE, &ctx); |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | /* ------------------------------------------------------------------------ */ |
| 4689 | /* |
| 4690 | * post-decrement |
| 4691 | */ |
| 4692 | void CTPNPostDec::gen_code(int discard, int) |
| 4693 | { |
| 4694 | /* ask the subnode to generate it */ |
| 4695 | void *ctx; |
| 4696 | if (!sub_->gen_code_asi(discard, 1, TC_ASI_POSTDEC, "--", |
| 4697 | 0, FALSE, TRUE, &ctx)) |
| 4698 | { |
| 4699 | /* |
| 4700 | * if we're keeping the result, duplicate the value at top of |
| 4701 | * stack prior to the decrement - since this is a |
| 4702 | * post-decrement, the result is the value *before* the |
| 4703 | * decrement |
| 4704 | */ |
| 4705 | if (!discard) |
| 4706 | { |
| 4707 | G_cg->write_op(OPC_DUP); |
| 4708 | G_cg->note_push(); |
| 4709 | } |
| 4710 | |
| 4711 | /* decrement the value at top of stack */ |
| 4712 | G_cg->write_op(OPC_DEC); |
| 4713 | |
| 4714 | /* |
| 4715 | * Generate a simple assignment back to the subexpression. |
| 4716 | * Discard the result of this assignment, regardless of whether |
| 4717 | * the caller wants the result of the overall expression, |
| 4718 | * because we've already pushed the actual result, which is the |
| 4719 | * original value before the decrement operation. |
| 4720 | */ |
| 4721 | sub_->gen_code_asi(TRUE, 2, TC_ASI_POSTDEC, "--", |
| 4722 | 0, FALSE, TRUE, &ctx); |
| 4723 | } |
| 4724 | } |
| 4725 | |
| 4726 | /* ------------------------------------------------------------------------ */ |
| 4727 | /* |
| 4728 | * operator 'new' |
| 4729 | */ |
| 4730 | void CTPNNew::gen_code(int discard, int /*for condition*/) |
| 4731 | { |
| 4732 | /* |
| 4733 | * ask my subexpression to generate the code - at this point we |
| 4734 | * don't know the number of arguments, so pass in zero for now |
| 4735 | */ |
| 4736 | sub_->gen_code_new(discard, 0, FALSE, 0, FALSE, transient_); |
| 4737 | } |
| 4738 | |
| 4739 | /* ------------------------------------------------------------------------ */ |
| 4740 | /* |
| 4741 | * operator 'delete' |
| 4742 | */ |
| 4743 | void CTPNDelete::gen_code(int, int) |
| 4744 | { |
| 4745 | /* 'delete' generates no code for T3 VM */ |
| 4746 | } |
| 4747 | |
| 4748 | /* ------------------------------------------------------------------------ */ |
| 4749 | /* |
| 4750 | * comma operator |
| 4751 | */ |
| 4752 | void CTPNComma::gen_code(int discard, int for_condition) |
| 4753 | { |
| 4754 | /* |
| 4755 | * Generate each side's code. Note that the left side is *always* |
| 4756 | * discarded, regardless of whether the result of the comma operator |
| 4757 | * will be discarded. After we generate our subexpressions, there's |
| 4758 | * nothing left to do, since the comma operator itself doesn't |
| 4759 | * change anything - we simply use the right operand result as our |
| 4760 | * result. |
| 4761 | * |
| 4762 | * Pass through the 'for_condition' status to the right operand, |
| 4763 | * since we pass through its result to the caller. For the left |
| 4764 | * operand, treat it as a condition - we don't care about the result |
| 4765 | * value, so don't bother performing any extra conversions on it. |
| 4766 | */ |
| 4767 | left_->gen_code(TRUE, TRUE); |
| 4768 | right_->gen_code(discard, for_condition); |
| 4769 | } |
| 4770 | |
| 4771 | /* ------------------------------------------------------------------------ */ |
| 4772 | /* |
| 4773 | * logical OR (short-circuit logic) |
| 4774 | */ |
| 4775 | void CTPNOr::gen_code(int discard, int for_condition) |
| 4776 | { |
| 4777 | CTcCodeLabel *lbl; |
| 4778 | |
| 4779 | /* |
| 4780 | * First, evaluate the left-hand side; we need the result even if |
| 4781 | * we're discarding the overall expression, since we will check the |
| 4782 | * result to see if we should even evaluate the right-hand side. |
| 4783 | * We're using the value for a condition, so don't bother |
| 4784 | * boolean-izing it. |
| 4785 | */ |
| 4786 | left_->gen_code(FALSE, TRUE); |
| 4787 | |
| 4788 | /* |
| 4789 | * If the left-hand side is true, there's no need to evaluate the |
| 4790 | * right-hand side (and, in fact, we're not even allowed to evaluate |
| 4791 | * the right-hand side because of the short-circuit logic rule). |
| 4792 | * So, if the lhs is true, we want to jump around the code to |
| 4793 | * evaluate the rhs, saving the 'true' result if we're not |
| 4794 | * discarding the overall result. |
| 4795 | */ |
| 4796 | lbl = gen_jump_ahead(discard ? OPC_JT : OPC_JST); |
| 4797 | |
| 4798 | /* |
| 4799 | * Evaluate the right-hand side. We don't need to save the result |
| 4800 | * unless we need the result of the overall expression. Generate |
| 4801 | * the value as though we were going to booleanize it ourselves, |
| 4802 | * since we'll do just that (hence pass for_condition = TRUE). |
| 4803 | */ |
| 4804 | right_->gen_code(discard, TRUE); |
| 4805 | |
| 4806 | /* |
| 4807 | * If we discarded the result, we generated a JT which explicitly |
| 4808 | * popped a value. If we didn't discard the result, we generated a |
| 4809 | * JST; this may or may not pop the value. However, if it doesn't |
| 4810 | * pop the value (save on true), it will bypass the right side |
| 4811 | * evaluation, and will thus "pop" that value in the sense that it |
| 4812 | * will never be pushed. So, note a pop either way. |
| 4813 | */ |
| 4814 | G_cg->note_pop(); |
| 4815 | |
| 4816 | /* define the label for the jump over the rhs */ |
| 4817 | def_label_pos(lbl); |
| 4818 | |
| 4819 | /* |
| 4820 | * If the result is not going to be used directly for a condition, we |
| 4821 | * must boolean-ize the value. This isn't necessary if both of the |
| 4822 | * operands are already guaranteed to be boolean values. |
| 4823 | */ |
| 4824 | if (!for_condition && !(left_->is_bool() && right_->is_bool())) |
| 4825 | G_cg->write_op(OPC_BOOLIZE); |
| 4826 | } |
| 4827 | |
| 4828 | /* |
| 4829 | * Generate code for the short-circuit OR when used in a condition. We can |
| 4830 | * use the fact that we're being used conditionally to avoid actually |
| 4831 | * pushing the result value onto the stack, instead simply branching to the |
| 4832 | * appropriate point in the enclosing control structure instead. |
| 4833 | */ |
| 4834 | void CTPNOr::gen_code_cond(CTcCodeLabel *then_label, |
| 4835 | CTcCodeLabel *else_label) |
| 4836 | { |
| 4837 | CTcCodeLabel *internal_then; |
| 4838 | |
| 4839 | /* |
| 4840 | * First, generate the conditional code for our left operand. If the |
| 4841 | * condition is true, we can short-circuit the rest of the expression |
| 4842 | * by jumping directly to the 'then' label. If the caller provided a |
| 4843 | * 'then' label, we can jump directly to the caller's 'then' label; |
| 4844 | * otherwise, we must synthesize our own internal label, which we'll |
| 4845 | * define at the end of our generated code so that we'll fall through |
| 4846 | * on true to the enclosing code. In any case, we want to fall through |
| 4847 | * if the condition is false, so that control will flow to the code for |
| 4848 | * our right operand if the left operand is false. |
| 4849 | */ |
| 4850 | internal_then = (then_label == 0 ? G_cs->new_label_fwd() : then_label); |
| 4851 | left_->gen_code_cond(internal_then, 0); |
| 4852 | |
| 4853 | /* |
| 4854 | * Now, generate code for our right operand. We can generate this code |
| 4855 | * using the caller's destination labels directly: if we reach this |
| 4856 | * code at all, it's because the left operand was false, in which case |
| 4857 | * the result is simply the value of the right operand. |
| 4858 | */ |
| 4859 | right_->gen_code_cond(then_label, else_label); |
| 4860 | |
| 4861 | /* |
| 4862 | * If we created an internal 'then' label, it goes at the end of our |
| 4863 | * generated code: this ensures that we fall off the end of our code |
| 4864 | * if the left subexpression is true, which is what the caller told us |
| 4865 | * they wanted when they gave us a null 'then' label. If the caller |
| 4866 | * gave us an explicit 'then' label, we'll have jumped there directly |
| 4867 | * if the first subexpression was true. |
| 4868 | */ |
| 4869 | if (then_label == 0) |
| 4870 | def_label_pos(internal_then); |
| 4871 | } |
| 4872 | |
| 4873 | /* ------------------------------------------------------------------------ */ |
| 4874 | /* |
| 4875 | * logical AND (short-circuit logic) |
| 4876 | */ |
| 4877 | void CTPNAnd::gen_code(int discard, int for_condition) |
| 4878 | { |
| 4879 | /* |
| 4880 | * first, evaluate the left-hand side; we need the result even if |
| 4881 | * we're discarding the overall expression, since we will check the |
| 4882 | * result to see if we should even evaluate the right-hand side |
| 4883 | */ |
| 4884 | left_->gen_code(FALSE, TRUE); |
| 4885 | |
| 4886 | /* |
| 4887 | * If the left-hand side is false, there's no need to evaluate the |
| 4888 | * right-hand side (and, in fact, we're not even allowed to evaluate |
| 4889 | * the right-hand side because of the short-circuit logic rule). |
| 4890 | * So, if the lhs is false, we want to jump around the code to |
| 4891 | * evaluate the rhs, saving the false result if we're not discarding |
| 4892 | * the overall result. |
| 4893 | */ |
| 4894 | CTcCodeLabel *lbl = gen_jump_ahead(discard ? OPC_JF : OPC_JSF); |
| 4895 | |
| 4896 | /* |
| 4897 | * Evaluate the right-hand side. We don't need to save the result |
| 4898 | * unless we need the result of the overall expression. |
| 4899 | */ |
| 4900 | right_->gen_code(discard, TRUE); |
| 4901 | |
| 4902 | /* define the label for the jump over the rhs */ |
| 4903 | def_label_pos(lbl); |
| 4904 | |
| 4905 | /* |
| 4906 | * If we discarded the result, we generated a JF which explicitly |
| 4907 | * popped a value. If we didn't discard the result, we generated a |
| 4908 | * JSF; this may or may not pop the value. However, if it doesn't |
| 4909 | * pop the value (save on false), it will bypass the right side |
| 4910 | * evaluation, and will thus "pop" that value in the sense that it |
| 4911 | * will never be pushed. So, note a pop either way. |
| 4912 | */ |
| 4913 | G_cg->note_pop(); |
| 4914 | |
| 4915 | /* |
| 4916 | * If the result is not going to be used directly for a condition, we |
| 4917 | * must boolean-ize the value. This isn't necessary if both of the |
| 4918 | * sub-expressions are already guaranteed to be boolean values. |
| 4919 | */ |
| 4920 | if (!for_condition && !(left_->is_bool() && right_->is_bool())) |
| 4921 | G_cg->write_op(OPC_BOOLIZE); |
| 4922 | } |
| 4923 | |
| 4924 | /* |
| 4925 | * Generate code for the short-circuit AND when used in a condition. We |
| 4926 | * can use the fact that we're being used conditionally to avoid actually |
| 4927 | * pushing the result value onto the stack, instead simply branching to the |
| 4928 | * appropriate point in the enclosing control structure instead. |
| 4929 | */ |
| 4930 | void CTPNAnd::gen_code_cond(CTcCodeLabel *then_label, |
| 4931 | CTcCodeLabel *else_label) |
| 4932 | { |
| 4933 | CTcCodeLabel *internal_else; |
| 4934 | |
| 4935 | /* |
| 4936 | * First, generate the conditional code for our left operand. If the |
| 4937 | * condition is false, we can short-circuit the rest of the expression |
| 4938 | * by jumping directly to the 'else' label. If the caller provided an |
| 4939 | * 'else' label, we can jump directly to the caller's 'else' label; |
| 4940 | * otherwise, we must synthesize our own internal label, which we'll |
| 4941 | * define at the end of our generated code so that we'll fall through |
| 4942 | * on false to the enclosing code. In any case, we want to fall |
| 4943 | * through if the condition is true, so that control will flow to the |
| 4944 | * code for our right operand if the left operand is true. |
| 4945 | */ |
| 4946 | internal_else = (else_label == 0 ? G_cs->new_label_fwd() : else_label); |
| 4947 | left_->gen_code_cond(0, internal_else); |
| 4948 | |
| 4949 | /* |
| 4950 | * Now, generate code for our right operand. We can generate this code |
| 4951 | * using the caller's destination labels directly: if we reach this |
| 4952 | * code at all, it's because the left operand was true, in which case |
| 4953 | * the result is simply the value of the right operand. |
| 4954 | */ |
| 4955 | right_->gen_code_cond(then_label, else_label); |
| 4956 | |
| 4957 | /* |
| 4958 | * If we created an internal 'else' label, it goes at the end of our |
| 4959 | * generated code: this ensures that we fall off the end of our code |
| 4960 | * if the left subexpression is false, which is what the caller told |
| 4961 | * us they wanted when they gave us a null 'else' label. If the |
| 4962 | * caller gave us an explicit 'else' label, we'll have jumped there |
| 4963 | * directly if the first subexpression was false. |
| 4964 | */ |
| 4965 | if (else_label == 0) |
| 4966 | def_label_pos(internal_else); |
| 4967 | } |
| 4968 | |
| 4969 | |
| 4970 | /* ------------------------------------------------------------------------ */ |
| 4971 | /* |
| 4972 | * bitwise OR |
| 4973 | */ |
| 4974 | void CTPNBOr::gen_code(int discard, int) |
| 4975 | { |
| 4976 | gen_binary(OPC_BOR, discard, FALSE); |
| 4977 | } |
| 4978 | |
| 4979 | /* ------------------------------------------------------------------------ */ |
| 4980 | /* |
| 4981 | * bitwise AND |
| 4982 | */ |
| 4983 | void CTPNBAnd::gen_code(int discard, int) |
| 4984 | { |
| 4985 | gen_binary(OPC_BAND, discard, FALSE); |
| 4986 | } |
| 4987 | |
| 4988 | /* ------------------------------------------------------------------------ */ |
| 4989 | /* |
| 4990 | * bitwise XOR |
| 4991 | */ |
| 4992 | void CTPNBXor::gen_code(int discard, int) |
| 4993 | { |
| 4994 | gen_binary(OPC_XOR, discard, FALSE); |
| 4995 | } |
| 4996 | |
| 4997 | /* ------------------------------------------------------------------------ */ |
| 4998 | /* |
| 4999 | * greater-than |
| 5000 | */ |
| 5001 | void CTPNGt::gen_code(int discard, int) |
| 5002 | { |
| 5003 | gen_binary(OPC_GT, discard, FALSE); |
| 5004 | } |
| 5005 | |
| 5006 | /* ------------------------------------------------------------------------ */ |
| 5007 | /* |
| 5008 | * greater-or-equal |
| 5009 | */ |
| 5010 | void CTPNGe::gen_code(int discard, int) |
| 5011 | { |
| 5012 | gen_binary(OPC_GE, discard, FALSE); |
| 5013 | } |
| 5014 | |
| 5015 | /* ------------------------------------------------------------------------ */ |
| 5016 | /* |
| 5017 | * less-than |
| 5018 | */ |
| 5019 | void CTPNLt::gen_code(int discard, int) |
| 5020 | { |
| 5021 | gen_binary(OPC_LT, discard, FALSE); |
| 5022 | } |
| 5023 | |
| 5024 | /* ------------------------------------------------------------------------ */ |
| 5025 | /* |
| 5026 | * less-or-equal |
| 5027 | */ |
| 5028 | void CTPNLe::gen_code(int discard, int) |
| 5029 | { |
| 5030 | gen_binary(OPC_LE, discard, FALSE); |
| 5031 | } |
| 5032 | |
| 5033 | /* ------------------------------------------------------------------------ */ |
| 5034 | /* |
| 5035 | * compare for equality |
| 5036 | */ |
| 5037 | void CTPNEq::gen_code(int discard, int) |
| 5038 | { |
| 5039 | gen_binary(OPC_EQ, discard, FALSE); |
| 5040 | } |
| 5041 | |
| 5042 | /* ------------------------------------------------------------------------ */ |
| 5043 | /* |
| 5044 | * compare for inequality |
| 5045 | */ |
| 5046 | void CTPNNe::gen_code(int discard, int) |
| 5047 | { |
| 5048 | gen_binary(OPC_NE, discard, FALSE); |
| 5049 | } |
| 5050 | |
| 5051 | /* ------------------------------------------------------------------------ */ |
| 5052 | /* |
| 5053 | * 'is in' |
| 5054 | */ |
| 5055 | void CTPNIsIn::gen_code(int discard, int) |
| 5056 | { |
| 5057 | CTPNArglist *lst; |
| 5058 | CTPNArg *arg; |
| 5059 | CTcCodeLabel *lbl_found; |
| 5060 | CTcCodeLabel *lbl_done; |
| 5061 | |
| 5062 | /* allocate our 'found' label */ |
| 5063 | lbl_found = G_cs->new_label_fwd(); |
| 5064 | |
| 5065 | /* |
| 5066 | * allocate our 'done' label - we only need to do this if we don't |
| 5067 | * have a constant true value and we're not discarding the result |
| 5068 | */ |
| 5069 | if (!const_true_ && !discard) |
| 5070 | lbl_done = G_cs->new_label_fwd(); |
| 5071 | |
| 5072 | /* generate my left-side expression */ |
| 5073 | left_->gen_code(FALSE, FALSE); |
| 5074 | |
| 5075 | /* the right side is always an argument list */ |
| 5076 | lst = (CTPNArglist *)right_; |
| 5077 | |
| 5078 | /* compare to each element in the list on the right */ |
| 5079 | for (arg = lst->get_arg_list_head() ; arg != 0 ; |
| 5080 | arg = arg->get_next_arg()) |
| 5081 | { |
| 5082 | /* |
| 5083 | * duplicate the left-side value, so we don't have to generate |
| 5084 | * it again for this comparison |
| 5085 | */ |
| 5086 | G_cg->write_op(OPC_DUP); |
| 5087 | |
| 5088 | /* generate this list element */ |
| 5089 | arg->gen_code(FALSE, FALSE); |
| 5090 | |
| 5091 | /* if they're equal, jump to the 'found' label */ |
| 5092 | G_cg->write_op(OPC_JE); |
| 5093 | G_cs->write_ofs2(lbl_found, 0); |
| 5094 | |
| 5095 | /* we pushed one more (DUP) and popped two (JE) */ |
| 5096 | G_cg->note_push(1); |
| 5097 | G_cg->note_pop(2); |
| 5098 | } |
| 5099 | |
| 5100 | /* |
| 5101 | * Generate the code that comes at the end of all of tests when we |
| 5102 | * fail to find any matches - we simply discard the left-side value |
| 5103 | * from the stack, push our 'nil' value, and jump to the end label. |
| 5104 | * |
| 5105 | * If we have a constant 'true' value, there's no need to do any of |
| 5106 | * this, because we know that, even after testing all of our |
| 5107 | * non-constant values, there's a constant value that makes the |
| 5108 | * entire expression true, and we can thus just fall through to the |
| 5109 | * 'found' code. |
| 5110 | * |
| 5111 | * If we're discarding the result, there's no need to push a |
| 5112 | * separate value for the result, so we can just fall through to the |
| 5113 | * common ending code in this case. |
| 5114 | */ |
| 5115 | if (!const_true_ && !discard) |
| 5116 | { |
| 5117 | G_cg->write_op(OPC_DISC); |
| 5118 | G_cg->write_op(OPC_PUSHNIL); |
| 5119 | G_cg->write_op(OPC_JMP); |
| 5120 | G_cs->write_ofs2(lbl_done, 0); |
| 5121 | } |
| 5122 | |
| 5123 | /* |
| 5124 | * Generate the 'found' code - this discards the left-side value and |
| 5125 | * pushes our 'true' result. Note that there's no reason to push |
| 5126 | * our result if we're discarding it. |
| 5127 | */ |
| 5128 | def_label_pos(lbl_found); |
| 5129 | G_cg->write_op(OPC_DISC); |
| 5130 | |
| 5131 | /* |
| 5132 | * if we're discarding the result, just note the pop of the left |
| 5133 | * value; otherwise, push our result |
| 5134 | */ |
| 5135 | if (discard) |
| 5136 | G_cg->note_pop(); |
| 5137 | else |
| 5138 | G_cg->write_op(OPC_PUSHTRUE); |
| 5139 | |
| 5140 | /* our 'done' label is here, if we needed one */ |
| 5141 | if (!const_true_ && !discard) |
| 5142 | def_label_pos(lbl_done); |
| 5143 | } |
| 5144 | |
| 5145 | /* ------------------------------------------------------------------------ */ |
| 5146 | /* |
| 5147 | * 'not in' |
| 5148 | */ |
| 5149 | void CTPNNotIn::gen_code(int discard, int) |
| 5150 | { |
| 5151 | CTPNArglist *lst; |
| 5152 | CTPNArg *arg; |
| 5153 | CTcCodeLabel *lbl_found; |
| 5154 | CTcCodeLabel *lbl_done; |
| 5155 | |
| 5156 | /* allocate our 'found' label */ |
| 5157 | lbl_found = G_cs->new_label_fwd(); |
| 5158 | |
| 5159 | /* |
| 5160 | * allocate our 'done' label - we only need to do this if we don't |
| 5161 | * have a constant false value |
| 5162 | */ |
| 5163 | if (!const_false_ && !discard) |
| 5164 | lbl_done = G_cs->new_label_fwd(); |
| 5165 | |
| 5166 | /* generate my left-side expression */ |
| 5167 | left_->gen_code(FALSE, FALSE); |
| 5168 | |
| 5169 | /* the right side is always an argument list */ |
| 5170 | lst = (CTPNArglist *)right_; |
| 5171 | |
| 5172 | /* compare to each element in the list on the right */ |
| 5173 | for (arg = lst->get_arg_list_head() ; arg != 0 ; |
| 5174 | arg = arg->get_next_arg()) |
| 5175 | { |
| 5176 | /* |
| 5177 | * duplicate the left-side value, so we don't have to generate |
| 5178 | * it again for this comparison |
| 5179 | */ |
| 5180 | G_cg->write_op(OPC_DUP); |
| 5181 | |
| 5182 | /* generate this list element */ |
| 5183 | arg->gen_code(FALSE, FALSE); |
| 5184 | |
| 5185 | /* if they're equal, jump to the 'found' label */ |
| 5186 | G_cg->write_op(OPC_JE); |
| 5187 | G_cs->write_ofs2(lbl_found, 0); |
| 5188 | |
| 5189 | /* we pushed one more (DUP) and popped two (JE) */ |
| 5190 | G_cg->note_push(1); |
| 5191 | G_cg->note_pop(2); |
| 5192 | } |
| 5193 | |
| 5194 | /* |
| 5195 | * Generate the code that comes at the end of all of tests when we |
| 5196 | * fail to find any matches - we simply discard the left-side value |
| 5197 | * from the stack, push our 'true' value, and jump to the end label. |
| 5198 | * |
| 5199 | * If we have a constant 'nil' value, however, there's no need to do |
| 5200 | * any of this, because we know that, even after testing all of our |
| 5201 | * non-constant values, there's a matching constant value that makes |
| 5202 | * the entire expression false (because 'not in' is false if we find |
| 5203 | * a match), and we can thus just fall through to the 'found' code. |
| 5204 | */ |
| 5205 | if (!const_false_ && !discard) |
| 5206 | { |
| 5207 | G_cg->write_op(OPC_DISC); |
| 5208 | G_cg->write_op(OPC_PUSHTRUE); |
| 5209 | G_cg->write_op(OPC_JMP); |
| 5210 | G_cs->write_ofs2(lbl_done, 0); |
| 5211 | } |
| 5212 | |
| 5213 | /* |
| 5214 | * generate the 'found' code - this discards the left-side value and |
| 5215 | * pushes our 'nil' result (because the result of 'not in' is false |
| 5216 | * if we found the value) |
| 5217 | */ |
| 5218 | def_label_pos(lbl_found); |
| 5219 | G_cg->write_op(OPC_DISC); |
| 5220 | |
| 5221 | /* push the result, or note the pop if we're just discarding it */ |
| 5222 | if (discard) |
| 5223 | G_cg->note_pop(); |
| 5224 | else |
| 5225 | G_cg->write_op(OPC_PUSHNIL); |
| 5226 | |
| 5227 | /* our 'done' label is here, if we needed one */ |
| 5228 | if (!const_false_ && !discard) |
| 5229 | def_label_pos(lbl_done); |
| 5230 | } |
| 5231 | |
| 5232 | /* ------------------------------------------------------------------------ */ |
| 5233 | /* |
| 5234 | * bit-shift left |
| 5235 | */ |
| 5236 | void CTPNShl::gen_code(int discard, int) |
| 5237 | { |
| 5238 | gen_binary(OPC_SHL, discard, FALSE); |
| 5239 | } |
| 5240 | |
| 5241 | /* ------------------------------------------------------------------------ */ |
| 5242 | /* |
| 5243 | * arithmetic shift right '>>' |
| 5244 | */ |
| 5245 | void CTPNAShr::gen_code(int discard, int) |
| 5246 | { |
| 5247 | gen_binary(OPC_ASHR, discard, FALSE); |
| 5248 | } |
| 5249 | |
| 5250 | /* ------------------------------------------------------------------------ */ |
| 5251 | /* |
| 5252 | * logical shift right '>>>' |
| 5253 | */ |
| 5254 | void CTPNLShr::gen_code(int discard, int) |
| 5255 | { |
| 5256 | gen_binary(OPC_LSHR, discard, FALSE); |
| 5257 | } |
| 5258 | |
| 5259 | /* ------------------------------------------------------------------------ */ |
| 5260 | /* |
| 5261 | * multiply |
| 5262 | */ |
| 5263 | void CTPNMul::gen_code(int discard, int) |
| 5264 | { |
| 5265 | /* if either side is zero or one, we can apply special handling */ |
| 5266 | if (left_->is_const_int(0)) |
| 5267 | { |
| 5268 | /* evaluate the right for side effects and discard the result */ |
| 5269 | right_->gen_code(TRUE, TRUE); |
| 5270 | |
| 5271 | /* the result is zero */ |
| 5272 | G_cg->write_op(OPC_PUSH_0); |
| 5273 | G_cg->note_push(); |
| 5274 | |
| 5275 | /* done */ |
| 5276 | return; |
| 5277 | } |
| 5278 | else if (right_->is_const_int(0)) |
| 5279 | { |
| 5280 | /* evaluate the left for side effects and discard the result */ |
| 5281 | left_->gen_code(TRUE, TRUE); |
| 5282 | |
| 5283 | /* the result is zero */ |
| 5284 | G_cg->write_op(OPC_PUSH_0); |
| 5285 | G_cg->note_push(); |
| 5286 | |
| 5287 | /* done */ |
| 5288 | return; |
| 5289 | } |
| 5290 | else if (left_->is_const_int(1)) |
| 5291 | { |
| 5292 | /* |
| 5293 | * evaluate the right side - it's the result; note that, because |
| 5294 | * of the explicit multiplication, we must compute logical |
| 5295 | * results using assignment (not 'for condition') rules |
| 5296 | */ |
| 5297 | right_->gen_code(discard, FALSE); |
| 5298 | |
| 5299 | /* done */ |
| 5300 | return; |
| 5301 | } |
| 5302 | else if (right_->is_const_int(1)) |
| 5303 | { |
| 5304 | /* evaluate the right side - it's the result */ |
| 5305 | left_->gen_code(discard, FALSE); |
| 5306 | |
| 5307 | /* done */ |
| 5308 | return; |
| 5309 | } |
| 5310 | |
| 5311 | /* apply generic handling */ |
| 5312 | gen_binary(OPC_MUL, discard, FALSE); |
| 5313 | } |
| 5314 | |
| 5315 | /* ------------------------------------------------------------------------ */ |
| 5316 | /* |
| 5317 | * divide |
| 5318 | */ |
| 5319 | void CTPNDiv::gen_code(int discard, int for_cond) |
| 5320 | { |
| 5321 | /* if dividing by 1, we can skip the whole thing (except side effects) */ |
| 5322 | if (right_->is_const_int(1)) |
| 5323 | { |
| 5324 | /* |
| 5325 | * simply generate the left side for side effects; actually |
| 5326 | * doing the arithmetic has no effect |
| 5327 | */ |
| 5328 | left_->gen_code(discard, for_cond); |
| 5329 | return; |
| 5330 | } |
| 5331 | |
| 5332 | /* if the left side is zero, the result is always zero */ |
| 5333 | if (left_->is_const_int(0)) |
| 5334 | { |
| 5335 | /* evaluate the right for side effects, but discard the result */ |
| 5336 | right_->gen_code(TRUE, TRUE); |
| 5337 | |
| 5338 | /* the result is zero */ |
| 5339 | G_cg->write_op(OPC_PUSH_0); |
| 5340 | G_cg->note_push(); |
| 5341 | return; |
| 5342 | } |
| 5343 | |
| 5344 | /* use generic code generation */ |
| 5345 | gen_binary(OPC_DIV, discard, FALSE); |
| 5346 | } |
| 5347 | |
| 5348 | /* ------------------------------------------------------------------------ */ |
| 5349 | /* |
| 5350 | * modulo |
| 5351 | */ |
| 5352 | void CTPNMod::gen_code(int discard, int for_condition) |
| 5353 | { |
| 5354 | /* if dividing by 1, we can skip the whole thing (except side effects) */ |
| 5355 | if (right_->is_const_int(1)) |
| 5356 | { |
| 5357 | /* |
| 5358 | * simply generate the left side for side effects; actually |
| 5359 | * doing the arithmetic has no effect |
| 5360 | */ |
| 5361 | left_->gen_code(discard, for_condition); |
| 5362 | |
| 5363 | /* the result is zero */ |
| 5364 | G_cg->write_op(OPC_PUSH_0); |
| 5365 | G_cg->note_push(); |
| 5366 | return; |
| 5367 | } |
| 5368 | |
| 5369 | /* if the left side is zero, the result is always zero */ |
| 5370 | if (left_->is_const_int(0)) |
| 5371 | { |
| 5372 | /* evaluate the right for side effects, but discard the result */ |
| 5373 | right_->gen_code(TRUE, TRUE); |
| 5374 | |
| 5375 | /* the result is zero */ |
| 5376 | G_cg->write_op(OPC_PUSH_0); |
| 5377 | G_cg->note_push(); |
| 5378 | return; |
| 5379 | } |
| 5380 | |
| 5381 | /* use generic processing */ |
| 5382 | gen_binary(OPC_MOD, discard, FALSE); |
| 5383 | } |
| 5384 | |
| 5385 | /* ------------------------------------------------------------------------ */ |
| 5386 | /* |
| 5387 | * subtract |
| 5388 | */ |
| 5389 | void CTPNSub::gen_code(int discard, int for_cond) |
| 5390 | { |
| 5391 | /* check for subtracting 1, which we can accomplish more efficiently */ |
| 5392 | if (right_->is_const_int(1)) |
| 5393 | { |
| 5394 | /* |
| 5395 | * We're subtracting one - use decrement. The decrement |
| 5396 | * operator itself has no side effects, so we can pass through |
| 5397 | * the 'discard' status to the subnode. |
| 5398 | */ |
| 5399 | left_->gen_code(discard, FALSE); |
| 5400 | |
| 5401 | /* apply decrement if we're not discarding the result */ |
| 5402 | if (!discard) |
| 5403 | G_cg->write_op(OPC_DEC); |
| 5404 | } |
| 5405 | else |
| 5406 | { |
| 5407 | /* we can't do anything special - use the general-purpose code */ |
| 5408 | gen_binary(OPC_SUB, discard, FALSE); |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | /* ------------------------------------------------------------------------ */ |
| 5413 | /* |
| 5414 | * add |
| 5415 | */ |
| 5416 | void CTPNAdd::gen_code(int discard, int) |
| 5417 | { |
| 5418 | /* check for adding 1, which we can accomplish more efficiently */ |
| 5419 | if (right_->is_const_int(1)) |
| 5420 | { |
| 5421 | /* |
| 5422 | * We're adding one - use increment. The increment operator |
| 5423 | * itself has no side effects, so we can pass through the |
| 5424 | * 'discard' status to the subnode. |
| 5425 | */ |
| 5426 | left_->gen_code(discard, FALSE); |
| 5427 | |
| 5428 | /* apply increment if we're not discarding the result */ |
| 5429 | if (!discard) |
| 5430 | G_cg->write_op(OPC_INC); |
| 5431 | } |
| 5432 | else |
| 5433 | { |
| 5434 | /* we can't do anything special - use the general-purpose code */ |
| 5435 | gen_binary(OPC_ADD, discard, FALSE); |
| 5436 | } |
| 5437 | } |
| 5438 | |
| 5439 | /* ------------------------------------------------------------------------ */ |
| 5440 | /* |
| 5441 | * simple assignment |
| 5442 | */ |
| 5443 | void CTPNAsi::gen_code(int discard, int) |
| 5444 | { |
| 5445 | /* |
| 5446 | * Ask the left subnode to generate a simple assignment to the value |
| 5447 | * on the right. Simple assignments cannot be refused, so we don't |
| 5448 | * need to try to do any assignment work ourselves. |
| 5449 | */ |
| 5450 | left_->gen_code_asi(discard, 1, TC_ASI_SIMPLE, "=", |
| 5451 | right_, FALSE, TRUE, 0); |
| 5452 | } |
| 5453 | |
| 5454 | /* ------------------------------------------------------------------------ */ |
| 5455 | /* |
| 5456 | * add and assign |
| 5457 | */ |
| 5458 | void CTPNAddAsi::gen_code(int discard, int) |
| 5459 | { |
| 5460 | /* use the static handler */ |
| 5461 | CTPNAddAsi_gen_code(left_, right_, discard); |
| 5462 | } |
| 5463 | |
| 5464 | /* static handler, for sharing with other node types */ |
| 5465 | void CTPNAddAsi_gen_code(CTcPrsNode *left, CTcPrsNode *right, int discard) |
| 5466 | { |
| 5467 | /* |
| 5468 | * ask the left subnode to generate an add-and-assign; if it can't, |
| 5469 | * handle it generically |
| 5470 | */ |
| 5471 | void *ctx; |
| 5472 | if (!left->gen_code_asi(discard, 1, TC_ASI_ADD, "+=", |
| 5473 | right, FALSE, TRUE, &ctx)) |
| 5474 | { |
| 5475 | /* |
| 5476 | * There's no special coding for this assignment type -- compute |
| 5477 | * the result generically, then assign the result as a simple |
| 5478 | * assignment, which cannot be refused. We've already generated |
| 5479 | * the left side above, so generate the right side. |
| 5480 | */ |
| 5481 | right->gen_code(FALSE, FALSE); |
| 5482 | |
| 5483 | /* generate the add (which pops two and adds one -> net one pop) */ |
| 5484 | G_cg->write_op(OPC_ADD); |
| 5485 | G_cg->note_pop(); |
| 5486 | |
| 5487 | /* generate the simple assignment to the lhs */ |
| 5488 | left->gen_code_asi(discard, 2, TC_ASI_ADD, "+=", |
| 5489 | 0, FALSE, TRUE, &ctx); |
| 5490 | } |
| 5491 | } |
| 5492 | |
| 5493 | /* ------------------------------------------------------------------------ */ |
| 5494 | /* |
| 5495 | * subtract and assign |
| 5496 | */ |
| 5497 | void CTPNSubAsi::gen_code(int discard, int) |
| 5498 | { |
| 5499 | /* |
| 5500 | * ask the left subnode to generate a subtract-and-assign; if it |
| 5501 | * can't, handle it generically |
| 5502 | */ |
| 5503 | void *ctx; |
| 5504 | if (!left_->gen_code_asi(discard, 1, TC_ASI_SUB, "-=", |
| 5505 | right_, FALSE, TRUE, &ctx)) |
| 5506 | { |
| 5507 | /* |
| 5508 | * there's no special coding for this assignment type -- compute |
| 5509 | * the result generically, then assign the result as a simple |
| 5510 | * assignment, which cannot be refused |
| 5511 | */ |
| 5512 | gen_binary_ca(OPC_SUB); |
| 5513 | left_->gen_code_asi(discard, 2, TC_ASI_SUB, "-=", |
| 5514 | 0, FALSE, TRUE, &ctx); |
| 5515 | } |
| 5516 | } |
| 5517 | |
| 5518 | /* ------------------------------------------------------------------------ */ |
| 5519 | /* |
| 5520 | * multiply and assign |
| 5521 | */ |
| 5522 | void CTPNMulAsi::gen_code(int discard, int) |
| 5523 | { |
| 5524 | /* |
| 5525 | * ask the left subnode to generate a multiply-and-assign; if it |
| 5526 | * can't, handle it generically |
| 5527 | */ |
| 5528 | void *ctx; |
| 5529 | if (!left_->gen_code_asi(discard, 1, TC_ASI_MUL, "*=", |
| 5530 | right_, FALSE, TRUE, &ctx)) |
| 5531 | { |
| 5532 | /* |
| 5533 | * there's no special coding for this assignment type -- compute |
| 5534 | * the result generically, then assign the result as a simple |
| 5535 | * assignment, which cannot be refused |
| 5536 | */ |
| 5537 | gen_binary_ca(OPC_MUL); |
| 5538 | left_->gen_code_asi(discard, 2, TC_ASI_MUL, "*=", |
| 5539 | 0, FALSE, TRUE, &ctx); |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | /* ------------------------------------------------------------------------ */ |
| 5544 | /* |
| 5545 | * divide and assign |
| 5546 | */ |
| 5547 | void CTPNDivAsi::gen_code(int discard, int) |
| 5548 | { |
| 5549 | /* |
| 5550 | * ask the left subnode to generate a divide-and-assign; if it |
| 5551 | * can't, handle it generically |
| 5552 | */ |
| 5553 | void *ctx; |
| 5554 | if (!left_->gen_code_asi(discard, 1, TC_ASI_DIV, "/=", |
| 5555 | right_, FALSE, TRUE, &ctx)) |
| 5556 | { |
| 5557 | /* |
| 5558 | * there's no special coding for this assignment type -- compute |
| 5559 | * the result generically, then assign the result as a simple |
| 5560 | * assignment, which cannot be refused |
| 5561 | */ |
| 5562 | gen_binary_ca(OPC_DIV); |
| 5563 | left_->gen_code_asi(discard, 2, TC_ASI_DIV, "/=", |
| 5564 | 0, FALSE, TRUE, &ctx); |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | /* ------------------------------------------------------------------------ */ |
| 5569 | /* |
| 5570 | * modulo and assign |
| 5571 | */ |
| 5572 | void CTPNModAsi::gen_code(int discard, int) |
| 5573 | { |
| 5574 | /* |
| 5575 | * ask the left subnode to generate a mod-and-assign; if it can't, |
| 5576 | * handle it generically |
| 5577 | */ |
| 5578 | void *ctx; |
| 5579 | if (!left_->gen_code_asi(discard, 1, TC_ASI_MOD, "%=", |
| 5580 | right_, FALSE, TRUE, &ctx)) |
| 5581 | { |
| 5582 | /* |
| 5583 | * there's no special coding for this assignment type -- compute |
| 5584 | * the result generically, then assign the result as a simple |
| 5585 | * assignment, which cannot be refused |
| 5586 | */ |
| 5587 | gen_binary_ca(OPC_MOD); |
| 5588 | left_->gen_code_asi(discard, 2, TC_ASI_MOD, "%=", |
| 5589 | 0, FALSE, TRUE, &ctx); |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | /* ------------------------------------------------------------------------ */ |
| 5594 | /* |
| 5595 | * bitwise-AND and assign |
| 5596 | */ |
| 5597 | void CTPNBAndAsi::gen_code(int discard, int) |
| 5598 | { |
| 5599 | /* |
| 5600 | * ask the left subnode to generate an AND-and-assign; if it can't, |
| 5601 | * handle it generically |
| 5602 | */ |
| 5603 | void *ctx; |
| 5604 | if (!left_->gen_code_asi(discard, 1, TC_ASI_BAND, "&=", |
| 5605 | right_, FALSE, TRUE, &ctx)) |
| 5606 | { |
| 5607 | /* |
| 5608 | * there's no special coding for this assignment type -- compute |
| 5609 | * the result generically, then assign the result as a simple |
| 5610 | * assignment, which cannot be refused |
| 5611 | */ |
| 5612 | gen_binary_ca(OPC_BAND); |
| 5613 | left_->gen_code_asi(discard, 2, TC_ASI_BAND, "&=", |
| 5614 | 0, FALSE, TRUE, &ctx); |
| 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | /* ------------------------------------------------------------------------ */ |
| 5619 | /* |
| 5620 | * bitwise-OR and assign |
| 5621 | */ |
| 5622 | void CTPNBOrAsi::gen_code(int discard, int) |
| 5623 | { |
| 5624 | /* |
| 5625 | * ask the left subnode to generate an OR-and-assign; if it can't, |
| 5626 | * handle it generically |
| 5627 | */ |
| 5628 | void *ctx; |
| 5629 | if (!left_->gen_code_asi(discard, 1, TC_ASI_BOR, "|=", |
| 5630 | right_, FALSE, TRUE, &ctx)) |
| 5631 | { |
| 5632 | /* |
| 5633 | * there's no special coding for this assignment type -- compute |
| 5634 | * the result generically, then assign the result as a simple |
| 5635 | * assignment, which cannot be refused |
| 5636 | */ |
| 5637 | gen_binary_ca(OPC_BOR); |
| 5638 | left_->gen_code_asi(discard, 2, TC_ASI_BOR, "|=", |
| 5639 | 0, FALSE, TRUE, &ctx); |
| 5640 | } |
| 5641 | } |
| 5642 | |
| 5643 | /* ------------------------------------------------------------------------ */ |
| 5644 | /* |
| 5645 | * bitwise-XOR and assign |
| 5646 | */ |
| 5647 | void CTPNBXorAsi::gen_code(int discard, int) |
| 5648 | { |
| 5649 | /* |
| 5650 | * ask the left subnode to generate an XOR-and-assign; if it can't, |
| 5651 | * handle it generically |
| 5652 | */ |
| 5653 | void *ctx; |
| 5654 | if (!left_->gen_code_asi(discard, 1, TC_ASI_BXOR, "^=", |
| 5655 | right_, FALSE, TRUE, &ctx)) |
| 5656 | { |
| 5657 | /* |
| 5658 | * there's no special coding for this assignment type -- compute |
| 5659 | * the result generically, then assign the result as a simple |
| 5660 | * assignment, which cannot be refused |
| 5661 | */ |
| 5662 | gen_binary_ca(OPC_XOR); |
| 5663 | left_->gen_code_asi(discard, 2, TC_ASI_BXOR, "^=", |
| 5664 | 0, FALSE, TRUE, &ctx); |
| 5665 | } |
| 5666 | } |
| 5667 | |
| 5668 | /* ------------------------------------------------------------------------ */ |
| 5669 | /* |
| 5670 | * bit-shift left and assign |
| 5671 | */ |
| 5672 | void CTPNShlAsi::gen_code(int discard, int) |
| 5673 | { |
| 5674 | /* |
| 5675 | * ask the left subnode to generate an shift-left-and-assign; if it |
| 5676 | * can't, handle it generically |
| 5677 | */ |
| 5678 | void *ctx; |
| 5679 | if (!left_->gen_code_asi(discard, 1, TC_ASI_SHL, "<<=", |
| 5680 | right_, FALSE, TRUE, &ctx)) |
| 5681 | { |
| 5682 | /* |
| 5683 | * there's no special coding for this assignment type -- compute |
| 5684 | * the result generically, then assign the result as a simple |
| 5685 | * assignment, which cannot be refused |
| 5686 | */ |
| 5687 | gen_binary_ca(OPC_SHL); |
| 5688 | left_->gen_code_asi(discard, 2, TC_ASI_SHL, "<<=", |
| 5689 | 0, FALSE, TRUE, &ctx); |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | /* ------------------------------------------------------------------------ */ |
| 5694 | /* |
| 5695 | * arithmetic shift right and assign '>>=' |
| 5696 | */ |
| 5697 | void CTPNAShrAsi::gen_code(int discard, int) |
| 5698 | { |
| 5699 | /* |
| 5700 | * ask the left subnode to generate a shift-right-and-assign; if it |
| 5701 | * can't, handle it generically |
| 5702 | */ |
| 5703 | void *ctx; |
| 5704 | if (!left_->gen_code_asi(discard, 1, TC_ASI_ASHR, ">>=", |
| 5705 | right_, FALSE, TRUE, &ctx)) |
| 5706 | { |
| 5707 | /* |
| 5708 | * there's no special coding for this assignment type -- compute |
| 5709 | * the result generically, then assign the result as a simple |
| 5710 | * assignment, which cannot be refused |
| 5711 | */ |
| 5712 | gen_binary_ca(OPC_ASHR); |
| 5713 | left_->gen_code_asi(discard, 2, TC_ASI_ASHR, ">>=", |
| 5714 | 0, FALSE, TRUE, &ctx); |
| 5715 | } |
| 5716 | } |
| 5717 | |
| 5718 | /* ------------------------------------------------------------------------ */ |
| 5719 | /* |
| 5720 | * logical shift right and assign '>>>=' |
| 5721 | */ |
| 5722 | void CTPNLShrAsi::gen_code(int discard, int) |
| 5723 | { |
| 5724 | /* |
| 5725 | * ask the left subnode to generate a shift-right-and-assign; if it |
| 5726 | * can't, handle it generically |
| 5727 | */ |
| 5728 | void *ctx; |
| 5729 | if (!left_->gen_code_asi(discard, 1, TC_ASI_LSHR, ">>>=", |
| 5730 | right_, FALSE, TRUE, &ctx)) |
| 5731 | { |
| 5732 | /* |
| 5733 | * there's no special coding for this assignment type -- compute |
| 5734 | * the result generically, then assign the result as a simple |
| 5735 | * assignment, which cannot be refused |
| 5736 | */ |
| 5737 | gen_binary_ca(OPC_LSHR); |
| 5738 | left_->gen_code_asi(discard, 2, TC_ASI_LSHR, ">>>=", |
| 5739 | 0, FALSE, TRUE, &ctx); |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | /* ------------------------------------------------------------------------ */ |
| 5744 | /* |
| 5745 | * subscript a list/array value |
| 5746 | */ |
| 5747 | void CTPNSubscript::gen_code(int discard, int) |
| 5748 | { |
| 5749 | gen_binary(OPC_INDEX, discard, FALSE); |
| 5750 | } |
| 5751 | |
| 5752 | /* |
| 5753 | * assign to a subscripted value |
| 5754 | */ |
| 5755 | int CTPNSubscript::gen_code_asi(int discard, int phase, |
| 5756 | tc_asitype_t typ, const char *op, |
| 5757 | CTcPrsNode *rhs, |
| 5758 | int, int xplicit, void **ctx) |
| 5759 | { |
| 5760 | void *sctx; |
| 5761 | |
| 5762 | /* |
| 5763 | * An assignment to a subscript expression is not what it appears. |
| 5764 | * It's actually a compound assignment that evaluates the subscript |
| 5765 | * expression, assigns the RHS, and then assigns the *result* of that |
| 5766 | * first assignment to the left operand of the index operator. In |
| 5767 | * other words, a[b]=c is effectively rewritten as this: |
| 5768 | * |
| 5769 | *. a = a.operator[]=(b, c) |
| 5770 | * |
| 5771 | * which, by analogy with the other compound assignment operators, we |
| 5772 | * can rewrite as |
| 5773 | * |
| 5774 | *. a []== (b, c) |
| 5775 | */ |
| 5776 | if (typ == TC_ASI_SIMPLE) |
| 5777 | { |
| 5778 | int rhs_depth; |
| 5779 | int extra_disc = 0; |
| 5780 | |
| 5781 | /* |
| 5782 | * Simple assignment. Generate the value to assign to the element |
| 5783 | * - that's the right side of the assignment operator. If rhs is |
| 5784 | * null, it means the caller has already done this. |
| 5785 | */ |
| 5786 | if (rhs != 0) |
| 5787 | { |
| 5788 | /* generate code for the RHS */ |
| 5789 | rhs->gen_code(FALSE, FALSE); |
| 5790 | } |
| 5791 | |
| 5792 | /* |
| 5793 | * if we're not discarding the result, duplicate the value to be |
| 5794 | * assigned, so that we leave a copy on the stack after we're |
| 5795 | * finished. This is necessary because the SETIND instruction will |
| 5796 | * consume one copy of the value. |
| 5797 | */ |
| 5798 | if (!discard) |
| 5799 | { |
| 5800 | G_cg->write_op(OPC_DUP); |
| 5801 | G_cg->note_push(); |
| 5802 | } |
| 5803 | |
| 5804 | /* note the stack depth where the RHS value resides */ |
| 5805 | rhs_depth = G_cg->get_sp_depth(); |
| 5806 | |
| 5807 | /* |
| 5808 | * Generate the value to be subscripted - that's my left-hand side. |
| 5809 | * Generate this as phase 1 of a two-phase assignment, with the |
| 5810 | * special pseudo-operator '[]='. Ignore the return value; no one |
| 5811 | * can fully handle this type of assignment in phase 1. Also, note |
| 5812 | * that we don't yet have the right-hand value to assign - it's the |
| 5813 | * result of the SETIND yet to come. |
| 5814 | */ |
| 5815 | left_->gen_code_asi(FALSE, 1, TC_ASI_IDX, op, |
| 5816 | 0, TRUE, xplicit, &sctx); |
| 5817 | |
| 5818 | /* |
| 5819 | * The phase 1 []= code could have left context information on the |
| 5820 | * stack that it will need in phase 2, in addition to the LHS value |
| 5821 | * that we need for the SETIND we're about to generate. For the |
| 5822 | * SETIND, we need the RHS value at index 1, just above the LHS |
| 5823 | * value. So if the new depth is more than 1 greater than the RHS |
| 5824 | * depth, we need to do some stack rearrangement. |
| 5825 | * |
| 5826 | * If phase 1 left *nothing* on the stack, it means that the value |
| 5827 | * being indexed can't handle assignments at all, as in |
| 5828 | * self[i]=val. This is acceptable: we can still perform the []= |
| 5829 | * portion to assign to the indexed element, but we won't be able |
| 5830 | * to do the second part, which is assigning the result of the []= |
| 5831 | * operator to the lhs of the [] operator. We'll simply ignore |
| 5832 | * that second part in this case. |
| 5833 | */ |
| 5834 | if (G_cg->get_sp_depth() == rhs_depth) |
| 5835 | { |
| 5836 | /* |
| 5837 | * the lhs of [] is not assignable; simply generate the lhs so |
| 5838 | * that we can index it |
| 5839 | */ |
| 5840 | left_->gen_code(FALSE, FALSE); |
| 5841 | } |
| 5842 | else if (G_cg->get_sp_depth() > rhs_depth + 1) |
| 5843 | { |
| 5844 | /* figure the distance to the RHS */ |
| 5845 | int dist = G_cg->get_sp_depth() - rhs_depth; |
| 5846 | |
| 5847 | /* the instruction can only accommodate a distance up to 255 */ |
| 5848 | if (dist > 255) |
| 5849 | G_tok->log_error(TCERR_EXPR_TOO_COMPLEX); |
| 5850 | |
| 5851 | /* re-push the RHS */ |
| 5852 | G_cg->write_op(OPC_GETSPN); |
| 5853 | G_cs->write((uchar)dist); |
| 5854 | G_cg->note_push(); |
| 5855 | |
| 5856 | /* |
| 5857 | * swap it with the left operand of the [] to get everything in |
| 5858 | * the proper order |
| 5859 | */ |
| 5860 | G_cg->write_op(OPC_SWAP); |
| 5861 | |
| 5862 | /* |
| 5863 | * the original RHS we pushed is now superfluous, so we'll need |
| 5864 | * to explicitly pop it when we're done |
| 5865 | */ |
| 5866 | extra_disc += 1; |
| 5867 | } |
| 5868 | |
| 5869 | /* generate the index value - that's my right-hand side */ |
| 5870 | right_->gen_code(FALSE, FALSE); |
| 5871 | |
| 5872 | /* generate the assign-to-indexed-value opcode */ |
| 5873 | G_cg->write_op(OPC_SETIND); |
| 5874 | |
| 5875 | /* setind pops three and pushes one - net of pop 2 */ |
| 5876 | G_cg->note_pop(2); |
| 5877 | |
| 5878 | /* |
| 5879 | * The top value now on the stack is the new container value. The |
| 5880 | * new container will be different from the old container in some |
| 5881 | * cases (with lists, for example, because we must create a new |
| 5882 | * list object to contain the modified list value). Therefore, if |
| 5883 | * my left-hand side is an lvalue, we must assign the new container |
| 5884 | * to the left-hand side - this makes something like "x[1] = 5" |
| 5885 | * actually change the value in "x" if "x" is a local variable. If |
| 5886 | * my left-hand side isn't an lvalue, don't bother with this step, |
| 5887 | * and simply discard the new container value. |
| 5888 | * |
| 5889 | * Handle the assignment to the new container as phase 2 of the |
| 5890 | * '[]=' pseudo-operator that we set up above. This ensures that |
| 5891 | * the left operand had a chance to save any sub-expression values, |
| 5892 | * which it can now reuse. |
| 5893 | * |
| 5894 | * Regardless of whether we're keeping the result of the overall |
| 5895 | * expression, we're definitely not keeping the result of assigning |
| 5896 | * the new container - the result of the assignment is the value |
| 5897 | * assigned, not the container. Thus, discard = true in this call. |
| 5898 | * |
| 5899 | * |
| 5900 | * There's a special case that's handled through the peep-hole |
| 5901 | * optimizer: if we are assigning to a local variable and indexing |
| 5902 | * with a constant integer value, we will have converted the whole |
| 5903 | * operation to a SETINDLCL1I8. That instruction takes care of |
| 5904 | * assigning the value back to the rvalue, so we don't need to |
| 5905 | * generate a separate rvalue assignment. |
| 5906 | */ |
| 5907 | if (G_cg->get_last_op() == OPC_SETINDLCL1I8) |
| 5908 | { |
| 5909 | /* |
| 5910 | * no assignment is necessary - we just need to account for the |
| 5911 | * difference in the stack arrangement with this form of the |
| 5912 | * assignment, which is that we don't leave the container value |
| 5913 | * on the stack |
| 5914 | */ |
| 5915 | G_cg->note_pop(); |
| 5916 | } |
| 5917 | else if (!left_->gen_code_asi(TRUE, 2, TC_ASI_IDX, op, |
| 5918 | 0, TRUE, xplicit, &sctx)) |
| 5919 | { |
| 5920 | /* no assignment is possible; discard the new container value */ |
| 5921 | G_cg->write_op(OPC_DISC); |
| 5922 | G_cg->note_pop(); |
| 5923 | } |
| 5924 | |
| 5925 | /* discard any extra stack elements */ |
| 5926 | while (extra_disc-- != 0) |
| 5927 | { |
| 5928 | G_cg->write_op(OPC_DISC); |
| 5929 | G_cg->note_pop(); |
| 5930 | } |
| 5931 | |
| 5932 | /* handled */ |
| 5933 | return TRUE; |
| 5934 | } |
| 5935 | else if (phase == 1) |
| 5936 | { |
| 5937 | /* |
| 5938 | * Compound assignment, phase 1. Generate the lvalue's value, and |
| 5939 | * also leave the indexee and index values on the stack separately |
| 5940 | * for use later in doing the assignment to indexee[index]. |
| 5941 | */ |
| 5942 | |
| 5943 | /* |
| 5944 | * Generate the value to be subscripted - that's my left-hand side. |
| 5945 | * Generate it as phase 1 of assignment through my pseudo-operator |
| 5946 | * []=. |
| 5947 | */ |
| 5948 | left_->gen_code_asi(FALSE, 1, TC_ASI_IDX, op, |
| 5949 | 0, TRUE, xplicit, &sctx); |
| 5950 | |
| 5951 | /* |
| 5952 | * save the context in the caller's context holder, so that it'll |
| 5953 | * get passed back to us on the second pass |
| 5954 | */ |
| 5955 | *ctx = sctx; |
| 5956 | |
| 5957 | /* generate the index value - that's my right-hand side */ |
| 5958 | right_->gen_code(FALSE, FALSE); |
| 5959 | |
| 5960 | /* duplicate them both so we have them later, for the assignment */ |
| 5961 | G_cg->write_op(OPC_DUP2); |
| 5962 | G_cg->note_push(2); |
| 5963 | |
| 5964 | /* generate the INDEX operator to compute indexee[index] */ |
| 5965 | G_cg->write_op(OPC_INDEX); |
| 5966 | G_cg->note_pop(); |
| 5967 | |
| 5968 | /* |
| 5969 | * If the caller pushed a RHS value for its own use in assigning, |
| 5970 | */ |
| 5971 | |
| 5972 | /* tell the caller to proceed (i.e., we didn't finish the job) */ |
| 5973 | return FALSE; |
| 5974 | } |
| 5975 | else if (phase == 2) |
| 5976 | { |
| 5977 | /* |
| 5978 | * Compound assignment, phase 2. We now have the combined value to |
| 5979 | * be assigned on the stack, followed by the indexee and index |
| 5980 | * values on the stack. |
| 5981 | */ |
| 5982 | |
| 5983 | /* restore the left-side phase 1 context from the caller */ |
| 5984 | sctx = *ctx; |
| 5985 | |
| 5986 | /* if the assigned value will be further assigned, save a copy */ |
| 5987 | if (!discard) |
| 5988 | { |
| 5989 | G_cg->write_op(OPC_DUP); |
| 5990 | G_cg->note_push(); |
| 5991 | } |
| 5992 | |
| 5993 | /* |
| 5994 | * The stack is now (bottom to top) INDEXEE INDEX RHS RHS (if |
| 5995 | * !discard) or INDEXEE INDEX RHS (if discard). SETIND needs it to |
| 5996 | * be (RHS) RHS INDEXEE INDEX. Swap the top one or two elements |
| 5997 | * with the next two to rearrange into SETIDX order. |
| 5998 | */ |
| 5999 | if (discard) |
| 6000 | { |
| 6001 | /* INDEXEE INDEX RHS -> RHS INDEX INDEXEE */ |
| 6002 | G_cg->write_op(OPC_SWAPN); |
| 6003 | G_cs->write(0); |
| 6004 | G_cs->write(2); |
| 6005 | |
| 6006 | /* -> RHS INDEXEE INDEX */ |
| 6007 | G_cg->write_op(OPC_SWAP); |
| 6008 | } |
| 6009 | else |
| 6010 | { |
| 6011 | /* swap INDEXEE INDEX with RHS RHS */ |
| 6012 | G_cg->write_op(OPC_SWAP2); |
| 6013 | } |
| 6014 | |
| 6015 | /* assign the rhs to the indexed value */ |
| 6016 | G_cg->write_op(OPC_SETIND); |
| 6017 | G_cg->note_pop(2); |
| 6018 | |
| 6019 | /* |
| 6020 | * The new container value is now on the stack; assign it to our |
| 6021 | * left-hand side. Note that the SETINDLCL1I8 optimization (see |
| 6022 | * the simple assignment case above) is impossible here, because we |
| 6023 | * had to generate the code to carry out the combination operator |
| 6024 | * (the '+' in '+=', for example) before the SETIND. There's thus |
| 6025 | * nothing for the SETIND to combine with. |
| 6026 | * |
| 6027 | * Generate as phase 2 of the []= assignment we started in phase 1. |
| 6028 | */ |
| 6029 | if (!left_->gen_code_asi(TRUE, 2, TC_ASI_IDX, op, |
| 6030 | 0, TRUE, xplicit, &sctx)) |
| 6031 | { |
| 6032 | /* no assignment was necessary; discard the container value */ |
| 6033 | G_cg->write_op(OPC_DISC); |
| 6034 | G_cg->note_pop(); |
| 6035 | } |
| 6036 | |
| 6037 | /* handled */ |
| 6038 | return TRUE; |
| 6039 | } |
| 6040 | else |
| 6041 | { |
| 6042 | /* ignore any other phases */ |
| 6043 | return FALSE; |
| 6044 | } |
| 6045 | } |
| 6046 | |
| 6047 | /* ------------------------------------------------------------------------ */ |
| 6048 | /* |
| 6049 | * If-nil operator |
| 6050 | */ |
| 6051 | void CTPNIfnil::gen_code(int discard, int for_condition) |
| 6052 | { |
| 6053 | /* generate the first operand */ |
| 6054 | first_->gen_code(FALSE, FALSE); |
| 6055 | |
| 6056 | /* check to see if we're keeping the result or merely testing it */ |
| 6057 | if (!discard) |
| 6058 | { |
| 6059 | /* we're keep the result - make a copy for the JNOTNIL */ |
| 6060 | G_cg->write_op(OPC_DUP); |
| 6061 | G_cg->note_push(); |
| 6062 | } |
| 6063 | |
| 6064 | /* if it's not nil, we're done - jump to the end of the operator */ |
| 6065 | CTcCodeLabel *lbl_end = gen_jump_ahead(OPC_JNOTNIL); |
| 6066 | G_cg->note_pop(); |
| 6067 | |
| 6068 | /* |
| 6069 | * We're on the 'nil' branch now, meaning we're not going to yield the |
| 6070 | * first value after all. If we're yielding anything, discard the |
| 6071 | * extra yielding copy we made of the first value. |
| 6072 | */ |
| 6073 | if (!discard) |
| 6074 | { |
| 6075 | G_cg->write_op(OPC_DISC); |
| 6076 | G_cg->note_pop(); |
| 6077 | } |
| 6078 | |
| 6079 | /* generate the 'nil' branch, yielding the second operand */ |
| 6080 | second_->gen_code(discard, for_condition); |
| 6081 | |
| 6082 | /* this is the end of the test - generate the end label */ |
| 6083 | def_label_pos(lbl_end); |
| 6084 | } |
| 6085 | |
| 6086 | /* ------------------------------------------------------------------------ */ |
| 6087 | /* |
| 6088 | * conditional operator |
| 6089 | */ |
| 6090 | void CTPNIf::gen_code(int discard, int for_condition) |
| 6091 | { |
| 6092 | /* |
| 6093 | * Generate the condition value - we need the value regardless of |
| 6094 | * whether the overall result is going to be used, because we need |
| 6095 | * it to determine which branch to take. Generate the subexpression |
| 6096 | * for a condition, so that we don't perform any extra unnecessary |
| 6097 | * conversions on it. |
| 6098 | */ |
| 6099 | first_->gen_code(FALSE, TRUE); |
| 6100 | |
| 6101 | /* if the condition is false, jump to the 'else' expression part */ |
| 6102 | CTcCodeLabel *lbl_else = gen_jump_ahead(OPC_JF); |
| 6103 | |
| 6104 | /* JF pops a value */ |
| 6105 | G_cg->note_pop(); |
| 6106 | |
| 6107 | /* |
| 6108 | * Generate the 'then' expression part. Only request a return value if |
| 6109 | * it has one AND we're not discarding it. If it doesn't return a |
| 6110 | * value, and we actually need one, we'll supply a default 'nil' value |
| 6111 | * next. This value will be our yielded value (in this branch, |
| 6112 | * anyway), so pass through the for-condition flag. |
| 6113 | */ |
| 6114 | second_->gen_code(discard || !second_->has_return_value(), for_condition); |
| 6115 | |
| 6116 | /* |
| 6117 | * If this expression has no return value, and we need the return |
| 6118 | * value, supply nil as the result. |
| 6119 | */ |
| 6120 | if (!discard && !second_->has_return_value()) |
| 6121 | { |
| 6122 | G_cg->write_op(OPC_PUSHNIL); |
| 6123 | G_cg->note_push(); |
| 6124 | } |
| 6125 | |
| 6126 | /* unconditionally jump over the 'else' part */ |
| 6127 | CTcCodeLabel *lbl_end = gen_jump_ahead(OPC_JMP); |
| 6128 | |
| 6129 | /* set the label for the 'else' part */ |
| 6130 | def_label_pos(lbl_else); |
| 6131 | |
| 6132 | /* |
| 6133 | * Generate the 'else' part. Only request a return value if it has one |
| 6134 | * AND we're not discarding it. Pass through 'discard' and |
| 6135 | * 'for_condition', since this result is our result. |
| 6136 | */ |
| 6137 | third_->gen_code(discard || !third_->has_return_value(), for_condition); |
| 6138 | |
| 6139 | /* |
| 6140 | * If this expression has no return value, and we need the return |
| 6141 | * value, supply nil as the result. |
| 6142 | */ |
| 6143 | if (!discard && !third_->has_return_value()) |
| 6144 | { |
| 6145 | G_cg->write_op(OPC_PUSHNIL); |
| 6146 | G_cg->note_push(); |
| 6147 | } |
| 6148 | |
| 6149 | /* |
| 6150 | * Because of the jump, we only evaluate one of the two expressions |
| 6151 | * we generated, so note an extra pop for the branch we didn't take. |
| 6152 | * Note that if either one pushes a value, both will, since we'll |
| 6153 | * explicitly have pushed nil for the one that doesn't generate a |
| 6154 | * value to keep the stack balanced on both branches. |
| 6155 | * |
| 6156 | * If neither of our expressions yields a value, don't pop anything |
| 6157 | * extra, since we won't think we've pushed two values in the course |
| 6158 | * of generating the two expressions. |
| 6159 | */ |
| 6160 | if (second_->has_return_value() || third_->has_return_value()) |
| 6161 | G_cg->note_pop(); |
| 6162 | |
| 6163 | /* set the label for the end of the expression */ |
| 6164 | def_label_pos(lbl_end); |
| 6165 | } |
| 6166 | |
| 6167 | /* ------------------------------------------------------------------------ */ |
| 6168 | /* |
| 6169 | * symbol |
| 6170 | */ |
| 6171 | void CTPNSym::gen_code(int discard, int) |
| 6172 | { |
| 6173 | /* |
| 6174 | * Look up the symbol; if it's undefined, add a default property |
| 6175 | * symbol entry if possible. Then ask the symbol to generate the |
| 6176 | * code. |
| 6177 | */ |
| 6178 | G_cs->get_symtab() |
| 6179 | ->find_or_def_prop_implied(get_sym_text(), get_sym_text_len(), |
| 6180 | FALSE, G_cs->is_self_available()) |
| 6181 | ->gen_code(discard); |
| 6182 | } |
| 6183 | |
| 6184 | /* |
| 6185 | * assign to a symbol |
| 6186 | */ |
| 6187 | int CTPNSym::gen_code_asi(int discard, int phase, |
| 6188 | tc_asitype_t typ, const char *op, |
| 6189 | CTcPrsNode *rhs, |
| 6190 | int ignore_errors, int xplicit, void **ctx) |
| 6191 | { |
| 6192 | /* |
| 6193 | * Look up the symbol; if it's undefined and there's a "self" object |
| 6194 | * available, define it as a property by default, since a property |
| 6195 | * is the only kind of symbol that we could possibly assign to |
| 6196 | * without having defined anywhere in the program. Once we have the |
| 6197 | * symbol, tell it to generate the code for assigning to it. |
| 6198 | */ |
| 6199 | return G_cs->get_symtab() |
| 6200 | ->find_or_def_prop_implied(get_sym_text(), get_sym_text_len(), |
| 6201 | FALSE, G_cs->is_self_available()) |
| 6202 | ->gen_code_asi(discard, phase, typ, op, rhs, |
| 6203 | ignore_errors, xplicit, ctx); |
| 6204 | } |
| 6205 | |
| 6206 | /* |
| 6207 | * take the address of the symbol |
| 6208 | */ |
| 6209 | void CTPNSym::gen_code_addr() |
| 6210 | { |
| 6211 | /* |
| 6212 | * Look up our symbol in the global symbol table, then ask the |
| 6213 | * resulting symbol to generate the appropriate code. If the symbol |
| 6214 | * isn't defined, assume it's a property. |
| 6215 | * |
| 6216 | * Note that we look only in the global symbol table, because local |
| 6217 | * symbols have no address value. So, even if the symbol is defined in |
| 6218 | * the local table, ignore the local definition and look at the global |
| 6219 | * definition. |
| 6220 | */ |
| 6221 | G_prs->get_global_symtab() |
| 6222 | ->find_or_def_prop_explicit(get_sym_text(), get_sym_text_len(), |
| 6223 | FALSE) |
| 6224 | ->gen_code_addr(); |
| 6225 | } |
| 6226 | |
| 6227 | /* |
| 6228 | * call the symbol |
| 6229 | */ |
| 6230 | void CTPNSym::gen_code_call(int discard, int argc, int varargs, |
| 6231 | CTcNamedArgs *named_args) |
| 6232 | { |
| 6233 | /* |
| 6234 | * Look up our symbol in the symbol table, then ask the resulting |
| 6235 | * symbol to generate the appropriate call. The symbol is |
| 6236 | * implicitly a property (if in a method context), since that's the |
| 6237 | * only kind of undefined symbol that we could be calling. |
| 6238 | */ |
| 6239 | G_cs->get_symtab() |
| 6240 | ->find_or_def_prop_implied(get_sym_text(), get_sym_text_len(), |
| 6241 | FALSE, G_cs->is_self_available()) |
| 6242 | ->gen_code_call(discard, argc, varargs, named_args); |
| 6243 | } |
| 6244 | |
| 6245 | /* |
| 6246 | * generate code for 'new' |
| 6247 | */ |
| 6248 | void CTPNSym::gen_code_new(int discard, int argc, int varargs, |
| 6249 | CTcNamedArgs *named_args, |
| 6250 | int /*from_call*/, int is_transient) |
| 6251 | { |
| 6252 | /* |
| 6253 | * Look up our symbol, then ask the resulting symbol to generate the |
| 6254 | * 'new' code. If the symbol is undefined, add an 'undefined' entry |
| 6255 | * to the table; we can't implicitly create an object symbol. |
| 6256 | */ |
| 6257 | G_cs->get_symtab() |
| 6258 | ->find_or_def_undef(get_sym_text(), get_sym_text_len(), FALSE) |
| 6259 | ->gen_code_new(discard, argc, varargs, named_args, is_transient); |
| 6260 | } |
| 6261 | |
| 6262 | /* |
| 6263 | * generate a property ID expression |
| 6264 | */ |
| 6265 | vm_prop_id_t CTPNSym::gen_code_propid(int check_only, int is_expr) |
| 6266 | { |
| 6267 | CTcSymbol *sym; |
| 6268 | CTcPrsSymtab *symtab; |
| 6269 | |
| 6270 | /* |
| 6271 | * Figure out where to look for the symbol. If the symbol was given |
| 6272 | * as an expression (in other words, it was explicitly enclosed in |
| 6273 | * parentheses), look it up in the local symbol table, since it |
| 6274 | * could refer to a local. Otherwise, it must refer to a property, |
| 6275 | * so look only in the global table. |
| 6276 | * |
| 6277 | * If the symbol isn't defined already, define it as a property now. |
| 6278 | * Because the symbol is explicitly on the right side of a member |
| 6279 | * evaluation, we can define it as a property whether or not there's |
| 6280 | * a valid "self" in this context. |
| 6281 | */ |
| 6282 | if (is_expr) |
| 6283 | { |
| 6284 | /* it's an expression - look it up in the local symbol table */ |
| 6285 | symtab = G_cs->get_symtab(); |
| 6286 | } |
| 6287 | else |
| 6288 | { |
| 6289 | /* it's a simple symbol - look only in the global symbol table */ |
| 6290 | symtab = G_prs->get_global_symtab(); |
| 6291 | } |
| 6292 | |
| 6293 | /* |
| 6294 | * look it up (note that this will always return a valid symbol, |
| 6295 | * since it will create one if we can't find an existing entry) |
| 6296 | */ |
| 6297 | sym = symtab->find_or_def_prop(get_sym_text(), get_sym_text_len(), FALSE); |
| 6298 | |
| 6299 | /* ask the symbol to generate the property reference */ |
| 6300 | return sym->gen_code_propid(check_only, is_expr); |
| 6301 | } |
| 6302 | |
| 6303 | /* |
| 6304 | * generate code for a member expression |
| 6305 | */ |
| 6306 | void CTPNSym::gen_code_member(int discard, CTcPrsNode *prop_expr, |
| 6307 | int prop_is_expr, |
| 6308 | int argc, int varargs, CTcNamedArgs *named_args) |
| 6309 | { |
| 6310 | /* |
| 6311 | * Look up the symbol, and let it do the work. There's no |
| 6312 | * appropriate default for the symbol, so leave it undefined if we |
| 6313 | * can't find it. |
| 6314 | */ |
| 6315 | G_cs->get_symtab() |
| 6316 | ->find_or_def_undef(get_sym_text(), get_sym_text_len(), FALSE) |
| 6317 | ->gen_code_member(discard, prop_expr, prop_is_expr, |
| 6318 | argc, varargs, named_args); |
| 6319 | } |
| 6320 | |
| 6321 | /* |
| 6322 | * generate code for an object before a '.' |
| 6323 | */ |
| 6324 | vm_obj_id_t CTPNSym::gen_code_obj_predot(int *is_self) |
| 6325 | { |
| 6326 | /* |
| 6327 | * Look up the symbol, and let it do the work. There's no default |
| 6328 | * type for the symbol, so leave it undefined if we don't find it. |
| 6329 | */ |
| 6330 | return G_cs->get_symtab() |
| 6331 | ->find_or_def_undef(get_sym_text(), get_sym_text_len(), FALSE) |
| 6332 | ->gen_code_obj_predot(is_self); |
| 6333 | } |
| 6334 | |
| 6335 | /* ------------------------------------------------------------------------ */ |
| 6336 | /* |
| 6337 | * resolved symbol |
| 6338 | */ |
| 6339 | void CTPNSymResolved::gen_code(int discard, int) |
| 6340 | { |
| 6341 | /* let the symbol handle it */ |
| 6342 | sym_->gen_code(discard); |
| 6343 | } |
| 6344 | |
| 6345 | /* |
| 6346 | * assign to a symbol |
| 6347 | */ |
| 6348 | int CTPNSymResolved::gen_code_asi(int discard, int phase, |
| 6349 | tc_asitype_t typ, const char *op, |
| 6350 | CTcPrsNode *rhs, |
| 6351 | int ignore_errors, int xplicit, void **ctx) |
| 6352 | { |
| 6353 | /* let the symbol handle it */ |
| 6354 | return sym_->gen_code_asi(discard, phase, typ, op, rhs, |
| 6355 | ignore_errors, xplicit, ctx); |
| 6356 | } |
| 6357 | |
| 6358 | /* |
| 6359 | * take the address of the symbol |
| 6360 | */ |
| 6361 | void CTPNSymResolved::gen_code_addr() |
| 6362 | { |
| 6363 | /* let the symbol handle it */ |
| 6364 | sym_->gen_code_addr(); |
| 6365 | } |
| 6366 | |
| 6367 | /* |
| 6368 | * call the symbol |
| 6369 | */ |
| 6370 | void CTPNSymResolved::gen_code_call(int discard, int argc, int varargs, |
| 6371 | CTcNamedArgs *named_args) |
| 6372 | { |
| 6373 | /* let the symbol handle it */ |
| 6374 | sym_->gen_code_call(discard, argc, varargs, named_args); |
| 6375 | } |
| 6376 | |
| 6377 | /* |
| 6378 | * generate code for 'new' |
| 6379 | */ |
| 6380 | void CTPNSymResolved::gen_code_new(int discard, int argc, |
| 6381 | int varargs, CTcNamedArgs *named_args, |
| 6382 | int /*from_call*/, int is_transient) |
| 6383 | { |
| 6384 | /* let the symbol handle it */ |
| 6385 | sym_->gen_code_new(discard, argc, varargs, named_args, is_transient); |
| 6386 | } |
| 6387 | |
| 6388 | /* |
| 6389 | * generate a property ID expression |
| 6390 | */ |
| 6391 | vm_prop_id_t CTPNSymResolved::gen_code_propid(int check_only, int is_expr) |
| 6392 | { |
| 6393 | /* let the symbol handle it */ |
| 6394 | return sym_->gen_code_propid(check_only, is_expr); |
| 6395 | } |
| 6396 | |
| 6397 | /* |
| 6398 | * generate code for a member expression |
| 6399 | */ |
| 6400 | void CTPNSymResolved::gen_code_member(int discard, |
| 6401 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 6402 | int argc, int varargs, |
| 6403 | CTcNamedArgs *named_args) |
| 6404 | { |
| 6405 | /* let the symbol handle it */ |
| 6406 | sym_->gen_code_member(discard, prop_expr, prop_is_expr, |
| 6407 | argc, varargs, named_args); |
| 6408 | } |
| 6409 | |
| 6410 | /* |
| 6411 | * generate code for an object before a '.' |
| 6412 | */ |
| 6413 | vm_obj_id_t CTPNSymResolved::gen_code_obj_predot(int *is_self) |
| 6414 | { |
| 6415 | /* let the symbol handle it */ |
| 6416 | return sym_->gen_code_obj_predot(is_self); |
| 6417 | } |
| 6418 | |
| 6419 | /* ------------------------------------------------------------------------ */ |
| 6420 | /* |
| 6421 | * Debugger local variable symbol |
| 6422 | */ |
| 6423 | |
| 6424 | /* |
| 6425 | * generate code to evaluate the variable |
| 6426 | */ |
| 6427 | void CTPNSymDebugLocal::gen_code(int discard, int for_condition) |
| 6428 | { |
| 6429 | /* if we're not discarding the value, push the local */ |
| 6430 | if (!discard) |
| 6431 | { |
| 6432 | /* generate the debugger local/parameter variable instruction */ |
| 6433 | G_cg->write_op(is_param_ ? OPC_GETDBARG : OPC_GETDBLCL); |
| 6434 | G_cs->write2(var_id_); |
| 6435 | G_cs->write2(frame_idx_); |
| 6436 | |
| 6437 | /* note that we pushed the value */ |
| 6438 | G_cg->note_push(); |
| 6439 | |
| 6440 | /* if it's a context local, get the value from the context array */ |
| 6441 | if (ctx_arr_idx_ != 0) |
| 6442 | { |
| 6443 | CTPNConst::s_gen_code_int(ctx_arr_idx_); |
| 6444 | G_cg->write_op(OPC_INDEX); |
| 6445 | |
| 6446 | /* |
| 6447 | * the 'index' operation pops two values and pushes one, for a |
| 6448 | * net of one pop |
| 6449 | */ |
| 6450 | G_cg->note_pop(); |
| 6451 | } |
| 6452 | } |
| 6453 | } |
| 6454 | |
| 6455 | /* |
| 6456 | * generate code for assigning to this variable |
| 6457 | */ |
| 6458 | int CTPNSymDebugLocal::gen_code_asi(int discard, int phase, |
| 6459 | tc_asitype_t typ, const char *, |
| 6460 | CTcPrsNode *rhs, |
| 6461 | int, int, void **) |
| 6462 | { |
| 6463 | /* check what we're doing */ |
| 6464 | if (typ == TC_ASI_SIMPLE || phase == 2) |
| 6465 | { |
| 6466 | /* |
| 6467 | * Simple assignment, or phase 2 of a compound assignment. In |
| 6468 | * either case, just assign the rvalue to the variable. |
| 6469 | */ |
| 6470 | if (rhs != 0) |
| 6471 | rhs->gen_code(FALSE, FALSE); |
| 6472 | |
| 6473 | /* |
| 6474 | * if we're not discarding the result, duplicate the value so we'll |
| 6475 | * have a copy after the assignment |
| 6476 | */ |
| 6477 | if (!discard) |
| 6478 | { |
| 6479 | G_cg->write_op(OPC_DUP); |
| 6480 | G_cg->note_push(); |
| 6481 | } |
| 6482 | |
| 6483 | /* check for a context property */ |
| 6484 | if (ctx_arr_idx_ == 0) |
| 6485 | { |
| 6486 | /* |
| 6487 | * generate the debug-local-set instruction - the operands are |
| 6488 | * the variable number and the stack frame index |
| 6489 | */ |
| 6490 | G_cg->write_op(is_param_ ? OPC_SETDBARG : OPC_SETDBLCL); |
| 6491 | G_cs->write2(var_id_); |
| 6492 | G_cs->write2(frame_idx_); |
| 6493 | } |
| 6494 | else |
| 6495 | { |
| 6496 | /* get the local containing our context object */ |
| 6497 | G_cg->write_op(OPC_GETDBLCL); |
| 6498 | G_cs->write2(var_id_); |
| 6499 | G_cs->write2(frame_idx_); |
| 6500 | |
| 6501 | /* set the actual variable value in the context object */ |
| 6502 | CTPNConst::s_gen_code_int(ctx_arr_idx_); |
| 6503 | G_cg->write_op(OPC_SETIND); |
| 6504 | G_cg->write_op(OPC_DISC); |
| 6505 | |
| 6506 | /* |
| 6507 | * we did three pops (SETIND), then a push (SETIND), then a pop |
| 6508 | * (DISC) - this is a net of three extra pops |
| 6509 | */ |
| 6510 | G_cg->note_pop(3); |
| 6511 | } |
| 6512 | |
| 6513 | /* the debug-local-set removes the rvalue from the stack */ |
| 6514 | G_cg->note_pop(); |
| 6515 | |
| 6516 | /* handled */ |
| 6517 | return TRUE; |
| 6518 | } |
| 6519 | else if (phase == 1) |
| 6520 | { |
| 6521 | /* |
| 6522 | * Compound assignment, phase 1. Simply generate our value and let |
| 6523 | * the caller proceed with the generic combination operator. |
| 6524 | */ |
| 6525 | gen_code(FALSE, FALSE); |
| 6526 | return FALSE; |
| 6527 | } |
| 6528 | else |
| 6529 | { |
| 6530 | /* ignore other phases */ |
| 6531 | return FALSE; |
| 6532 | } |
| 6533 | } |
| 6534 | |
| 6535 | /* ------------------------------------------------------------------------ */ |
| 6536 | /* |
| 6537 | * Double-quoted string. The 'discard' status is irrelevant, because we |
| 6538 | * evaluate double-quoted strings for their side effects. |
| 6539 | */ |
| 6540 | void CTPNDstr::gen_code(int discard, int) |
| 6541 | { |
| 6542 | /* if we're not discarding the value, it's an error */ |
| 6543 | if (!discard) |
| 6544 | G_tok->log_error(TCERR_DQUOTE_IN_EXPR, (int)len_, str_); |
| 6545 | |
| 6546 | /* generate the instruction to display it */ |
| 6547 | G_cg->write_op(OPC_SAY); |
| 6548 | |
| 6549 | /* add the string to the constant pool, creating a fixup here */ |
| 6550 | G_cg->add_const_str(str_, len_, G_cs, G_cs->get_ofs()); |
| 6551 | |
| 6552 | /* write a placeholder value, which will be corrected by the fixup */ |
| 6553 | G_cs->write4(0); |
| 6554 | } |
| 6555 | |
| 6556 | /* ------------------------------------------------------------------------ */ |
| 6557 | /* |
| 6558 | * Double-quoted debug string |
| 6559 | */ |
| 6560 | void CTPNDebugDstr::gen_code(int, int) |
| 6561 | { |
| 6562 | /* generate code to push the in-line string */ |
| 6563 | G_cg->write_op(OPC_PUSHSTRI); |
| 6564 | G_cs->write2(len_); |
| 6565 | G_cs->write(str_, len_); |
| 6566 | |
| 6567 | /* write code to display the value */ |
| 6568 | G_cg->write_op(OPC_SAYVAL); |
| 6569 | |
| 6570 | /* note that we pushed the string and then popped it */ |
| 6571 | G_cg->note_push(); |
| 6572 | G_cg->note_pop(); |
| 6573 | } |
| 6574 | |
| 6575 | /* ------------------------------------------------------------------------ */ |
| 6576 | /* |
| 6577 | * Double-quoted string embedding |
| 6578 | */ |
| 6579 | |
| 6580 | /* |
| 6581 | * create an embedding |
| 6582 | */ |
| 6583 | CTPNDstrEmbed::CTPNDstrEmbed(CTcPrsNode *sub) |
| 6584 | : CTPNDstrEmbedBase(sub) |
| 6585 | { |
| 6586 | } |
| 6587 | |
| 6588 | /* |
| 6589 | * Generate code for a double-quoted string embedding |
| 6590 | */ |
| 6591 | void CTPNDstrEmbed::gen_code(int, int) |
| 6592 | { |
| 6593 | /* note the stack depth before generating the expression */ |
| 6594 | int orig_depth = G_cg->get_sp_depth(); |
| 6595 | |
| 6596 | /* |
| 6597 | * Generate code for the embedded expression. If the expression has a |
| 6598 | * return value, generate the value so that it can be displayed in the |
| 6599 | * string; but don't request a value if it doesn't have one, as a |
| 6600 | * return value is optional in this context. This is a normal value |
| 6601 | * invocation, not a conditional, so we need any applicable normal |
| 6602 | * value conversions. |
| 6603 | */ |
| 6604 | sub_->gen_code(!sub_->has_return_value(), FALSE); |
| 6605 | |
| 6606 | /* |
| 6607 | * If the code generation left anything on the stack, generate code |
| 6608 | * to display the value via the default display function. |
| 6609 | */ |
| 6610 | if (G_cg->get_sp_depth() > orig_depth) |
| 6611 | { |
| 6612 | /* add a SAYVAL instruction */ |
| 6613 | G_cg->write_op(OPC_SAYVAL); |
| 6614 | |
| 6615 | /* SAYVAL pops the argument value */ |
| 6616 | G_cg->note_pop(); |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | /* ------------------------------------------------------------------------ */ |
| 6621 | /* |
| 6622 | * embedded <<one of>> list |
| 6623 | */ |
| 6624 | void CTPNStrOneOf::gen_code(int discard, int for_condition) |
| 6625 | { |
| 6626 | /* |
| 6627 | * There are two components to the <<one of>> list. First, there's the |
| 6628 | * list of strings. Second, there's an anonymous OneOfIndexGen object |
| 6629 | * that we create; this object holds our state and generates our index |
| 6630 | * values. |
| 6631 | * |
| 6632 | * In the simple case, the list is just a constant list of constant |
| 6633 | * strings. However, if the enclosing string is a dstring, each of |
| 6634 | * these will be a dstring; and even if the outer string is an sstring, |
| 6635 | * these could contain further embedded expressions, in which case they |
| 6636 | * won't be constant. |
| 6637 | * |
| 6638 | * If the list is constant, we generate list[generator.getNextIndex()]. |
| 6639 | * This selects a string from the list and leaves it on the stack, |
| 6640 | * which is just what our parent node wants. |
| 6641 | * |
| 6642 | * If the list isn't constant, we generate a switch on |
| 6643 | * generator.getNextIndex(), and then fill in the case table from the |
| 6644 | * list: each case label is the index, and each case's body is the code |
| 6645 | * generated for the list element, followed by a 'break' to the end of |
| 6646 | * the switch. The effect once again is to pick an item from the list |
| 6647 | * and push its value onto the stack. |
| 6648 | */ |
| 6649 | |
| 6650 | /* look up the getNextIndex property */ |
| 6651 | CTcSymProp *propsym = (CTcSymProp *)G_prs->get_global_symtab()-> |
| 6652 | find_or_def_prop_explicit("getNextIndex", 12, FALSE); |
| 6653 | if (propsym->get_type() != TC_SYM_PROP) |
| 6654 | { |
| 6655 | G_tok->log_error(TCERR_ONEOF_REQ_GETNXT); |
| 6656 | return; |
| 6657 | } |
| 6658 | |
| 6659 | /* synthesize a constant value for the property */ |
| 6660 | CTcConstVal propc; |
| 6661 | propc.set_prop(propsym->get_prop()); |
| 6662 | CTPNConst *getNextIndex = new CTPNConst(&propc); |
| 6663 | |
| 6664 | /* generate the list */ |
| 6665 | CTcConstVal *c = lst_->get_const_val(); |
| 6666 | if (c != 0) |
| 6667 | { |
| 6668 | /* the list is a constant - push the list value */ |
| 6669 | lst_->gen_code(FALSE, FALSE); |
| 6670 | |
| 6671 | /* call generator.getNextIndex() to get the next index */ |
| 6672 | state_obj_->gen_code_member(FALSE, getNextIndex, FALSE, 0, FALSE, 0); |
| 6673 | |
| 6674 | /* index the list (this pops two values and pushes one) */ |
| 6675 | G_cg->write_op(OPC_INDEX); |
| 6676 | G_cg->note_pop(); |
| 6677 | } |
| 6678 | else |
| 6679 | { |
| 6680 | /* the list is non-constant - start by generating the index */ |
| 6681 | state_obj_->gen_code_member(FALSE, getNextIndex, FALSE, 0, FALSE, 0); |
| 6682 | |
| 6683 | /* note the number of list elements */ |
| 6684 | int i, cnt = lst_->get_count(); |
| 6685 | |
| 6686 | /* generate the SWITCH <case_cnt> */ |
| 6687 | G_cg->write_op(OPC_SWITCH); |
| 6688 | G_cs->write2(cnt); |
| 6689 | G_cg->note_pop(); |
| 6690 | |
| 6691 | /* |
| 6692 | * Build the case table. Each label is an index value; leave the |
| 6693 | * jump offset zero for now, since we'll have to fill it in as we |
| 6694 | * generate the code. |
| 6695 | */ |
| 6696 | ulong caseofs = G_cs->get_ofs(); |
| 6697 | for (i = 1 ; i <= cnt ; ++i) |
| 6698 | { |
| 6699 | /* write the case label as the 1-based index value */ |
| 6700 | char buf[VMB_DATAHOLDER]; |
| 6701 | vm_val_t val; |
| 6702 | val.set_int(i); |
| 6703 | vmb_put_dh(buf, &val); |
| 6704 | G_cs->write(buf, VMB_DATAHOLDER); |
| 6705 | |
| 6706 | /* write a placeholder for the jump offset */ |
| 6707 | G_cs->write2(0); |
| 6708 | } |
| 6709 | |
| 6710 | /* write a placeholder for the default case */ |
| 6711 | G_cs->write2(0); |
| 6712 | |
| 6713 | /* create a forward label for the 'break' to the end of the switch */ |
| 6714 | CTcCodeLabel *brklbl = G_cs->new_label_fwd(); |
| 6715 | |
| 6716 | /* |
| 6717 | * Now run through the table and generate the code for each list |
| 6718 | * element. |
| 6719 | */ |
| 6720 | CTPNListEle *ele; |
| 6721 | for (i = 1, ele = lst_->get_head() ; ele != 0 && i <= cnt ; |
| 6722 | ++i, caseofs += VMB_DATAHOLDER + 2, ele = ele->get_next()) |
| 6723 | { |
| 6724 | /* set the jump to this case in the case table */ |
| 6725 | G_cs->write2_at(caseofs + VMB_DATAHOLDER, |
| 6726 | G_cs->get_ofs() - (caseofs + VMB_DATAHOLDER)); |
| 6727 | |
| 6728 | /* generate the code for this list element */ |
| 6729 | ele->get_expr()->gen_code(discard, for_condition);; |
| 6730 | |
| 6731 | /* generate a jump to the end of the table */ |
| 6732 | G_cg->write_op(OPC_JMP); |
| 6733 | G_cs->write_ofs2(brklbl, 0); |
| 6734 | |
| 6735 | /* |
| 6736 | * since all of these branches proceed in parallel, only one |
| 6737 | * push counts, so don't count every iteration - pretend that |
| 6738 | * we've already popped the value that this case generated |
| 6739 | */ |
| 6740 | if (!discard) |
| 6741 | G_cg->note_pop(); |
| 6742 | } |
| 6743 | |
| 6744 | /* write the default case - just push nil */ |
| 6745 | G_cs->write2_at(caseofs, G_cs->get_ofs() - caseofs); |
| 6746 | if (!discard) |
| 6747 | { |
| 6748 | G_cg->write_op(OPC_PUSHNIL); |
| 6749 | G_cg->note_push(); |
| 6750 | } |
| 6751 | |
| 6752 | /* this is the end of the switch - set the 'break' label here */ |
| 6753 | def_label_pos(brklbl); |
| 6754 | } |
| 6755 | } |
| 6756 | |
| 6757 | |
| 6758 | /* ------------------------------------------------------------------------ */ |
| 6759 | /* |
| 6760 | * Create or define an internal compiler-defined function in the program. |
| 6761 | */ |
| 6762 | static CTcSymFunc *get_internal_func(const char *nm, |
| 6763 | int argc, int opt_argc, int varargs, |
| 6764 | int has_retval) |
| 6765 | { |
| 6766 | /* get the symbol length */ |
| 6767 | size_t len = strlen(nm); |
| 6768 | |
| 6769 | /* look up the function symbol */ |
| 6770 | CTcSymFunc *fn = |
| 6771 | (CTcSymFunc *)G_prs->get_global_symtab()->find_delete_weak(nm, len); |
| 6772 | if (fn == 0) |
| 6773 | { |
| 6774 | /* it's not defined - create an implied declaration for it */ |
| 6775 | fn = new CTcSymFunc(nm, len, FALSE, argc, opt_argc, varargs, |
| 6776 | has_retval, FALSE, FALSE, TRUE, TRUE); |
| 6777 | |
| 6778 | /* add it to the global symbol table */ |
| 6779 | G_prs->get_global_symtab()->add_entry(fn); |
| 6780 | } |
| 6781 | else if (fn->get_type() != TC_SYM_FUNC) |
| 6782 | { |
| 6783 | /* it's defined, but not as a function - this is an error */ |
| 6784 | G_tok->log_error(TCERR_REDEF_AS_FUNC, (int)len, nm); |
| 6785 | fn = 0; |
| 6786 | } |
| 6787 | |
| 6788 | /* return the function */ |
| 6789 | return fn; |
| 6790 | } |
| 6791 | |
| 6792 | /* |
| 6793 | * Get an internal built-in function by name |
| 6794 | */ |
| 6795 | static CTcSymBif *get_internal_bif(const char *nm, int err) |
| 6796 | { |
| 6797 | /* look up the symbol */ |
| 6798 | size_t len = strlen(nm); |
| 6799 | CTcSymBif *fn = (CTcSymBif *)G_prs->get_global_symtab()->find(nm, len); |
| 6800 | |
| 6801 | /* if it wasn't found, flag an error */ |
| 6802 | if (fn == 0) |
| 6803 | G_tok->log_error(err, (int)len, nm); |
| 6804 | |
| 6805 | /* return the function */ |
| 6806 | return fn; |
| 6807 | } |
| 6808 | |
| 6809 | /* |
| 6810 | * Call an internal built-in function by name. This is for simple calls |
| 6811 | * with fixed arguments and no named parameters, keeping the return value. |
| 6812 | */ |
| 6813 | static void call_internal_bif(const char *nm, int err, int argc) |
| 6814 | { |
| 6815 | /* look up the function */ |
| 6816 | CTcSymBif *fn = get_internal_bif(nm, err); |
| 6817 | |
| 6818 | /* if we got it, call it */ |
| 6819 | if (fn != 0) |
| 6820 | fn->gen_code_call(FALSE, argc, FALSE, 0); |
| 6821 | } |
| 6822 | |
| 6823 | |
| 6824 | /* ------------------------------------------------------------------------ */ |
| 6825 | /* |
| 6826 | * Argument list |
| 6827 | */ |
| 6828 | void CTPNArglist::gen_code_arglist(int *varargs, CTcNamedArgs &named_args) |
| 6829 | { |
| 6830 | CTPNArg *arg; |
| 6831 | int i; |
| 6832 | int fixed_cnt; |
| 6833 | int pushed_varargs_counter; |
| 6834 | |
| 6835 | /* |
| 6836 | * scan the argument list for varargs - if we have any, we must |
| 6837 | * treat all of them as varargs |
| 6838 | */ |
| 6839 | for (*varargs = FALSE, fixed_cnt = named_args.cnt = 0, |
| 6840 | named_args.args = this, arg = get_arg_list_head() ; |
| 6841 | arg != 0 ; |
| 6842 | arg = arg->get_next_arg()) |
| 6843 | { |
| 6844 | /* if this is a varargs argument, we have varargs */ |
| 6845 | if (arg->is_varargs()) |
| 6846 | { |
| 6847 | /* note it */ |
| 6848 | *varargs = TRUE; |
| 6849 | } |
| 6850 | else if (arg->is_named_param()) |
| 6851 | { |
| 6852 | /* count the named argument */ |
| 6853 | named_args.cnt += 1; |
| 6854 | } |
| 6855 | else |
| 6856 | { |
| 6857 | /* count another fixed positional argument */ |
| 6858 | ++fixed_cnt; |
| 6859 | } |
| 6860 | } |
| 6861 | |
| 6862 | /* |
| 6863 | * If we have named arguments, push them ahead of the position |
| 6864 | * arguments. |
| 6865 | */ |
| 6866 | if (named_args.cnt != 0) |
| 6867 | { |
| 6868 | /* |
| 6869 | * Run through the argument list again, evaluating only named |
| 6870 | * arguments on this pass. |
| 6871 | */ |
| 6872 | for (i = argc_, arg = get_arg_list_head() ; arg != 0 ; |
| 6873 | arg = arg->get_next_arg(), --i) |
| 6874 | { |
| 6875 | /* if it's a named argument, push it */ |
| 6876 | if (arg->is_named_param()) |
| 6877 | arg->gen_code(FALSE, FALSE); |
| 6878 | } |
| 6879 | } |
| 6880 | |
| 6881 | /* |
| 6882 | * Push each positional argument in the list. The arguments on the |
| 6883 | * stack go in right-to-left order. The parser builds the internal |
| 6884 | * list of argument expressions in reverse order, so we must merely |
| 6885 | * follow the list from head to tail. |
| 6886 | * |
| 6887 | * We need each argument value to be pushed (hence discard = false), |
| 6888 | * and we need the assignable value of each argument expression (hence |
| 6889 | * for_condition = false). |
| 6890 | */ |
| 6891 | for (pushed_varargs_counter = FALSE, i = argc_, |
| 6892 | arg = get_arg_list_head() ; arg != 0 ; |
| 6893 | arg = arg->get_next_arg(), --i) |
| 6894 | { |
| 6895 | int depth; |
| 6896 | |
| 6897 | /* skip named arguments on this pass */ |
| 6898 | if (arg->is_named_param()) |
| 6899 | continue; |
| 6900 | |
| 6901 | /* note the stack depth before generating the value */ |
| 6902 | depth = G_cg->get_sp_depth(); |
| 6903 | |
| 6904 | /* |
| 6905 | * check for varargs - if this is first varargs argument, push |
| 6906 | * the counter placeholder |
| 6907 | */ |
| 6908 | if (arg->is_varargs() && !pushed_varargs_counter) |
| 6909 | { |
| 6910 | /* |
| 6911 | * write code to push the fixed argument count - we can use |
| 6912 | * this as a starting point, since we always know we have |
| 6913 | * this many argument to start with; we'll dynamically add |
| 6914 | * in the variable count at run-time |
| 6915 | */ |
| 6916 | CTPNConst::s_gen_code_int(fixed_cnt); |
| 6917 | |
| 6918 | /* note that we've pushed the counter */ |
| 6919 | pushed_varargs_counter = TRUE; |
| 6920 | |
| 6921 | /* |
| 6922 | * we will take the extra value off when we evaluate the |
| 6923 | * varargs counter, so simply count it as removed now |
| 6924 | */ |
| 6925 | G_cg->note_pop(); |
| 6926 | } |
| 6927 | |
| 6928 | /* generate the argument's code */ |
| 6929 | arg->gen_code(FALSE, FALSE); |
| 6930 | |
| 6931 | /* |
| 6932 | * if we've pushed the variable argument counter value onto the |
| 6933 | * stack, and this a fixed argument, swap the top two stack |
| 6934 | * elements to get the argument counter back to the top of the |
| 6935 | * stack; if this is a varargs argument there's no need, since |
| 6936 | * it will have taken care of this |
| 6937 | */ |
| 6938 | if (pushed_varargs_counter && !arg->is_varargs()) |
| 6939 | G_cg->write_op(OPC_SWAP); |
| 6940 | |
| 6941 | /* ensure that it generated something */ |
| 6942 | if (G_cg->get_sp_depth() <= depth) |
| 6943 | G_tok->log_error(TCERR_ARG_EXPR_HAS_NO_VAL, i); |
| 6944 | } |
| 6945 | } |
| 6946 | |
| 6947 | /* |
| 6948 | * ------------------------------------------------------------------------ |
| 6949 | */ |
| 6950 | /* |
| 6951 | * argument list entry |
| 6952 | */ |
| 6953 | void CTPNArg::gen_code(int, int) |
| 6954 | { |
| 6955 | /* |
| 6956 | * Generate the argument expression. We need the value (hence |
| 6957 | * discard = false), and we need the assignable value (hence |
| 6958 | * for_condition = false). |
| 6959 | */ |
| 6960 | get_arg_expr()->gen_code(FALSE, FALSE); |
| 6961 | |
| 6962 | /* |
| 6963 | * if this is a list-to-varargs conversion, generate the conversion |
| 6964 | * instruction |
| 6965 | */ |
| 6966 | if (is_varargs_) |
| 6967 | { |
| 6968 | /* write the opcode */ |
| 6969 | G_cg->write_op(OPC_MAKELSTPAR); |
| 6970 | |
| 6971 | /* note the extra push and pop for the argument count */ |
| 6972 | G_cg->note_push(); |
| 6973 | G_cg->note_pop(); |
| 6974 | } |
| 6975 | } |
| 6976 | |
| 6977 | /* ------------------------------------------------------------------------ */ |
| 6978 | /* |
| 6979 | * function/method call |
| 6980 | */ |
| 6981 | |
| 6982 | /* |
| 6983 | * create |
| 6984 | */ |
| 6985 | CTPNCall::CTPNCall(CTcPrsNode *func, class CTPNArglist *arglist) |
| 6986 | : CTPNCallBase(func, arglist) |
| 6987 | { |
| 6988 | /* the T3 instruction set limits calls to 127 arguments */ |
| 6989 | if (arglist->get_argc() > 127) |
| 6990 | G_tok->log_error(TCERR_TOO_MANY_CALL_ARGS); |
| 6991 | } |
| 6992 | |
| 6993 | |
| 6994 | /* |
| 6995 | * generate code |
| 6996 | */ |
| 6997 | void CTPNCall::gen_code(int discard, int for_condition) |
| 6998 | { |
| 6999 | /* check for special functions */ |
| 7000 | if (get_func()->sym_text_matches("rand", 4) |
| 7001 | && gen_code_rand(discard, for_condition)) |
| 7002 | return; |
| 7003 | |
| 7004 | /* push the argument list */ |
| 7005 | int varargs; |
| 7006 | CTcNamedArgs named_args; |
| 7007 | get_arg_list()->gen_code_arglist(&varargs, named_args); |
| 7008 | |
| 7009 | /* generate an appropriate call instruction */ |
| 7010 | get_func()->gen_code_call(discard, get_arg_list()->get_argc(), |
| 7011 | varargs, &named_args); |
| 7012 | } |
| 7013 | |
| 7014 | /* |
| 7015 | * Generate code for a call to 'rand'. If we have multiple arguments, |
| 7016 | * rand() is special because it doesn't evaluate all of its arguments. |
| 7017 | * Instead, it picks an argument at random, and evaluates only that single |
| 7018 | * selected argument, yielding the result. |
| 7019 | */ |
| 7020 | int CTPNCall::gen_code_rand(int discard, int for_condition) |
| 7021 | { |
| 7022 | /* get our argument list, and the number of arguments */ |
| 7023 | CTPNArglist *args = get_arg_list(); |
| 7024 | int argc = args->get_argc(); |
| 7025 | |
| 7026 | /* if we have zero or one argument, use the normal call */ |
| 7027 | if (argc < 2) |
| 7028 | return FALSE; |
| 7029 | |
| 7030 | /* if we have variable arguments, use the normal call */ |
| 7031 | CTPNArg *arg; |
| 7032 | for (arg = args->get_arg_list_head() ; arg != 0 ; |
| 7033 | arg = arg->get_next_arg()) |
| 7034 | { |
| 7035 | if (arg->is_varargs()) |
| 7036 | return FALSE; |
| 7037 | } |
| 7038 | |
| 7039 | /* |
| 7040 | * We have a fixed number of arguments greater than one, so this is the |
| 7041 | * special form of the rand() call. First, generate an ordinary call |
| 7042 | * to rand() to select a number from 0 to argc-1. |
| 7043 | */ |
| 7044 | CTcNamedArgs named_args; |
| 7045 | named_args.cnt = 0; |
| 7046 | CTPNConst::s_gen_code_int(argc); |
| 7047 | get_func()->gen_code_call(FALSE, 1, FALSE, &named_args); |
| 7048 | |
| 7049 | /* generate the SWITCH <case_cnt> */ |
| 7050 | G_cg->write_op(OPC_SWITCH); |
| 7051 | G_cs->write2(argc); |
| 7052 | G_cg->note_pop(); |
| 7053 | |
| 7054 | /* |
| 7055 | * Build the case table. The rand() call gave us an integer from 0 to |
| 7056 | * argc-1 inclusive, so our case labels are simply the argument index |
| 7057 | * values, starting at zero. Note that the argument list is stored |
| 7058 | * right to left, so the index values of the list elements run from |
| 7059 | * argc-1 down to 0. Since we're making a random selection anyway the |
| 7060 | * order shouldn't really matter; however, for the sake of regression |
| 7061 | * tests for pre-3.1 games, we want to use the same index ordering that |
| 7062 | * rand() did in the old version, where rand() itself chose one of its |
| 7063 | * pre-evaluated arguments. When running in regression mode with a |
| 7064 | * fixed random number stream, using the same index ordering will |
| 7065 | * ensure that we get the same results when running existing tests. |
| 7066 | */ |
| 7067 | ulong caseofs = G_cs->get_ofs(); |
| 7068 | int i; |
| 7069 | for (i = argc - 1 ; i >= 0 ; --i) |
| 7070 | { |
| 7071 | /* write the case label as the 0-based argument index */ |
| 7072 | char buf[VMB_DATAHOLDER]; |
| 7073 | vm_val_t val; |
| 7074 | val.set_int(i); |
| 7075 | vmb_put_dh(buf, &val); |
| 7076 | G_cs->write(buf, VMB_DATAHOLDER); |
| 7077 | |
| 7078 | /* write a placeholder for the jump offset */ |
| 7079 | G_cs->write2(0); |
| 7080 | } |
| 7081 | |
| 7082 | /* write a placeholder for the default case */ |
| 7083 | G_cs->write2(0); |
| 7084 | |
| 7085 | /* create a forward label for the 'break' to the end of the switch */ |
| 7086 | CTcCodeLabel *brklbl = G_cs->new_label_fwd(); |
| 7087 | |
| 7088 | /* generate the argument expressions */ |
| 7089 | for (arg = args->get_arg_list_head() ; arg != 0 ; |
| 7090 | arg = arg->get_next_arg(), caseofs += VMB_DATAHOLDER + 2) |
| 7091 | { |
| 7092 | /* set the jump to this case in the case table */ |
| 7093 | G_cs->write2_at(caseofs + VMB_DATAHOLDER, |
| 7094 | G_cs->get_ofs() - (caseofs + VMB_DATAHOLDER)); |
| 7095 | |
| 7096 | /* generate the argument expression */ |
| 7097 | arg->get_arg_expr()->gen_code(discard, for_condition); |
| 7098 | |
| 7099 | /* generate a jump to the end of the table */ |
| 7100 | G_cg->write_op(OPC_JMP); |
| 7101 | G_cs->write_ofs2(brklbl, 0); |
| 7102 | |
| 7103 | /* we generate the branches in parallel, so only one push counts */ |
| 7104 | if (!discard) |
| 7105 | G_cg->note_pop(); |
| 7106 | } |
| 7107 | |
| 7108 | /* write the default case - just push nil if we need a value */ |
| 7109 | G_cs->write2_at(caseofs, G_cs->get_ofs() - caseofs); |
| 7110 | if (!discard) |
| 7111 | { |
| 7112 | G_cg->write_op(OPC_PUSHNIL); |
| 7113 | G_cg->note_push(); |
| 7114 | } |
| 7115 | |
| 7116 | /* this is the end of the switch - set the 'break' label here */ |
| 7117 | def_label_pos(brklbl); |
| 7118 | |
| 7119 | /* tell the caller that we handled the code generation */ |
| 7120 | return TRUE; |
| 7121 | } |
| 7122 | |
| 7123 | /* |
| 7124 | * Generate code for operator 'new'. A 'new' with an argument list |
| 7125 | * looks like a function call: NEW(CALL(object-contents, ARGLIST(...))). |
| 7126 | */ |
| 7127 | void CTPNCall::gen_code_new(int discard, int argc, int varargs, |
| 7128 | CTcNamedArgs *named_args, |
| 7129 | int from_call, int is_transient) |
| 7130 | { |
| 7131 | /* |
| 7132 | * if this is a recursive call from another 'call' node, it's not |
| 7133 | * allowed - we'd be trying to use the result of a call as the base |
| 7134 | * class of the 'new', which is illegal |
| 7135 | */ |
| 7136 | if (from_call) |
| 7137 | { |
| 7138 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 7139 | return; |
| 7140 | } |
| 7141 | |
| 7142 | /* use our own named_args structure if the caller didn't provide one */ |
| 7143 | CTcNamedArgs my_named_args; |
| 7144 | if (named_args == 0) |
| 7145 | named_args = &my_named_args; |
| 7146 | |
| 7147 | /* generate the argument list */ |
| 7148 | get_arg_list()->gen_code_arglist(&varargs, *named_args); |
| 7149 | |
| 7150 | /* generate the code for the 'new' call */ |
| 7151 | get_func()->gen_code_new(discard, get_arg_list()->get_argc(), |
| 7152 | varargs, named_args, TRUE, is_transient); |
| 7153 | } |
| 7154 | |
| 7155 | |
| 7156 | /* ------------------------------------------------------------------------ */ |
| 7157 | /* |
| 7158 | * member property evaluation |
| 7159 | */ |
| 7160 | void CTPNMember::gen_code(int discard, int) |
| 7161 | { |
| 7162 | /* ask the object expression to generate the code */ |
| 7163 | get_obj_expr()->gen_code_member(discard, get_prop_expr(), prop_is_expr_, |
| 7164 | 0, FALSE, 0); |
| 7165 | } |
| 7166 | |
| 7167 | /* context for two-phase compound assignment to a member expression */ |
| 7168 | struct member_ca_ctx |
| 7169 | { |
| 7170 | member_ca_ctx(int is_self, vm_obj_id_t obj, vm_prop_id_t prop) |
| 7171 | { |
| 7172 | this->is_self = is_self; |
| 7173 | this->obj = obj; |
| 7174 | this->prop = prop; |
| 7175 | } |
| 7176 | |
| 7177 | int is_self; |
| 7178 | vm_obj_id_t obj; |
| 7179 | vm_prop_id_t prop; |
| 7180 | }; |
| 7181 | |
| 7182 | /* |
| 7183 | * assign to member expression |
| 7184 | */ |
| 7185 | int CTPNMember::gen_code_asi(int discard, int phase, |
| 7186 | tc_asitype_t typ, const char *op, |
| 7187 | CTcPrsNode *rhs, |
| 7188 | int ignore_errors, int, void **ctx) |
| 7189 | { |
| 7190 | int is_self; |
| 7191 | vm_obj_id_t obj; |
| 7192 | vm_prop_id_t prop; |
| 7193 | |
| 7194 | /* if this is phase 2 of a compound assignment, retrieve the context */ |
| 7195 | if (phase == 2) |
| 7196 | { |
| 7197 | /* cast the generic context pointer to our structure */ |
| 7198 | member_ca_ctx *cctx = (member_ca_ctx *)*ctx; |
| 7199 | |
| 7200 | /* retrieve the context information */ |
| 7201 | is_self = cctx->is_self; |
| 7202 | obj = cctx->obj; |
| 7203 | prop = cctx->prop; |
| 7204 | |
| 7205 | /* we're done with the context object - delete it */ |
| 7206 | delete cctx; |
| 7207 | } |
| 7208 | |
| 7209 | /* |
| 7210 | * if this is a simple assignment, start by generating the right-hand |
| 7211 | * side, unless the caller has already done so |
| 7212 | */ |
| 7213 | if (typ == TC_ASI_SIMPLE && rhs != 0) |
| 7214 | rhs->gen_code(FALSE, FALSE); |
| 7215 | |
| 7216 | /* |
| 7217 | * If this is a simple assignment, or it's phase 2 of a two-phase |
| 7218 | * assignment, we now have the RHS at top of stack, and we're about to |
| 7219 | * perform the actual assignment. If the caller wants to further use |
| 7220 | * the assigned value, push a copy, since we'll consume the RHS value |
| 7221 | * currently on the stack with our SETPROP or related instruction. |
| 7222 | */ |
| 7223 | if (!discard && (typ == TC_ASI_SIMPLE || phase == 2)) |
| 7224 | { |
| 7225 | G_cg->write_op(OPC_DUP); |
| 7226 | G_cg->note_push(); |
| 7227 | } |
| 7228 | |
| 7229 | /* |
| 7230 | * If this is a simple assignment, or phase 1 of a two-phase compound |
| 7231 | * assignment, generate the object and property expressions. |
| 7232 | */ |
| 7233 | if (typ == TC_ASI_SIMPLE || phase == 1) |
| 7234 | { |
| 7235 | /* |
| 7236 | * Determine what we have on the left: we could have self, a |
| 7237 | * constant object value, or any other expression. |
| 7238 | */ |
| 7239 | obj = get_obj_expr()->gen_code_obj_predot(&is_self); |
| 7240 | |
| 7241 | /* |
| 7242 | * determine what kind of property expression we have - don't |
| 7243 | * generate any code for now, since we may need to generate some |
| 7244 | * more code ahead of the property generation |
| 7245 | */ |
| 7246 | prop = get_prop_expr()->gen_code_propid(TRUE, prop_is_expr_); |
| 7247 | |
| 7248 | /* determine what we need to do based on the operands */ |
| 7249 | if (prop == VM_INVALID_PROP) |
| 7250 | { |
| 7251 | /* |
| 7252 | * We're assigning through a property pointer -- we must |
| 7253 | * generate a PTRSETPROP instruction. |
| 7254 | * |
| 7255 | * Before we generate the property expression, we must generate |
| 7256 | * the object expression. If we got a constant object, we must |
| 7257 | * generate code to push that object value; otherwise, the code |
| 7258 | * to generate the object value is already generated. |
| 7259 | */ |
| 7260 | if (is_self) |
| 7261 | { |
| 7262 | /* self - generate code to push the "self" value */ |
| 7263 | G_cg->write_op(OPC_PUSHSELF); |
| 7264 | G_cg->note_push(); |
| 7265 | } |
| 7266 | else if (obj != VM_INVALID_OBJ) |
| 7267 | { |
| 7268 | /* constant object - generate code to push the value */ |
| 7269 | G_cg->write_op(OPC_PUSHOBJ); |
| 7270 | G_cs->write_obj_id(obj); |
| 7271 | G_cg->note_push(); |
| 7272 | } |
| 7273 | |
| 7274 | /* generate the property value expression */ |
| 7275 | get_prop_expr()->gen_code_propid(FALSE, prop_is_expr_); |
| 7276 | |
| 7277 | /* |
| 7278 | * If this is phase 1 of a two-phase compound assignment, we'll |
| 7279 | * need the object and property values that we've just stacked |
| 7280 | * again in phase 2. Save separate copies via DUP2. |
| 7281 | */ |
| 7282 | if (typ != TC_ASI_SIMPLE) |
| 7283 | { |
| 7284 | G_cg->write_op(OPC_DUP2); |
| 7285 | G_cg->note_push(2); |
| 7286 | } |
| 7287 | } |
| 7288 | else |
| 7289 | { |
| 7290 | /* |
| 7291 | * We're assigning to a fixed property ID. If the object |
| 7292 | * expression was not 'self' or a fixed object ID, we have an |
| 7293 | * object value on the stack; otherwise we have nothing on the |
| 7294 | * stack. |
| 7295 | * |
| 7296 | * For a two-phase assignment, we'll need the object value |
| 7297 | * again during the second phase, so if we did indeed stack a |
| 7298 | * value, make a copy of it for our use in phase 2. |
| 7299 | */ |
| 7300 | if (typ != TC_ASI_SIMPLE && !is_self && obj == VM_INVALID_OBJ) |
| 7301 | { |
| 7302 | G_cg->write_op(OPC_DUP); |
| 7303 | G_cg->note_push(); |
| 7304 | } |
| 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | /* |
| 7309 | * If this is phase 1 of a two-phase assignment, stop here: we've |
| 7310 | * stacked the necessary obj and prop information for the actual |
| 7311 | * assignment codegen in phase 2, and we've stacked the obj.prop value |
| 7312 | * for the combination operator to access. Save context information |
| 7313 | * and return, telling the caller to proceed with the unrolled |
| 7314 | * operation. |
| 7315 | */ |
| 7316 | if (typ != TC_ASI_SIMPLE && phase == 1) |
| 7317 | { |
| 7318 | /* save the object and property information in the phase context */ |
| 7319 | *ctx = new member_ca_ctx(is_self, obj, prop); |
| 7320 | |
| 7321 | /* evaluate the property */ |
| 7322 | if (prop == VM_INVALID_PROP) |
| 7323 | { |
| 7324 | /* property pointer - call with zero arguments */ |
| 7325 | G_cg->write_op(OPC_PTRCALLPROP); |
| 7326 | G_cs->write(0); |
| 7327 | |
| 7328 | /* this pops the object and property */ |
| 7329 | G_cg->note_pop(2); |
| 7330 | } |
| 7331 | else |
| 7332 | { |
| 7333 | /* constant property */ |
| 7334 | if (is_self) |
| 7335 | { |
| 7336 | /* get property of self */ |
| 7337 | G_cg->write_op(OPC_GETPROPSELF); |
| 7338 | G_cs->write_prop_id(prop); |
| 7339 | } |
| 7340 | else if (obj != VM_INVALID_OBJ) |
| 7341 | { |
| 7342 | /* constant object */ |
| 7343 | G_cg->write_op(OPC_OBJGETPROP); |
| 7344 | G_cs->write_obj_id(obj); |
| 7345 | G_cs->write_prop_id(prop); |
| 7346 | } |
| 7347 | else |
| 7348 | { |
| 7349 | /* it's an object expression */ |
| 7350 | G_cg->write_op(OPC_GETPROP); |
| 7351 | G_cs->write_prop_id(prop); |
| 7352 | |
| 7353 | /* this pops the object value */ |
| 7354 | G_cg->note_pop(); |
| 7355 | } |
| 7356 | } |
| 7357 | |
| 7358 | /* all of these leave the value in R0 - retrieve it */ |
| 7359 | G_cg->write_op(OPC_GETR0); |
| 7360 | G_cg->note_push(); |
| 7361 | |
| 7362 | /* tell the caller to proceed with the unrolled assignment */ |
| 7363 | return FALSE; |
| 7364 | } |
| 7365 | |
| 7366 | /* |
| 7367 | * If we're doing a simple assignment, or this is phase 2 of a compound |
| 7368 | * assignment, it's time to do the actual assignment. |
| 7369 | */ |
| 7370 | if (typ == TC_ASI_SIMPLE || phase == 2) |
| 7371 | { |
| 7372 | /* determine how to make the assignment based on the operands */ |
| 7373 | if (prop == VM_INVALID_PROP) |
| 7374 | { |
| 7375 | /* |
| 7376 | * If this is phase 2 of a compound assignment, get the stack |
| 7377 | * back in order. We currently have (from bottom to top) obj |
| 7378 | * prop RHS RHS (if !discard), or obj prop RHS (if discard). |
| 7379 | * Swap the top one or two elements for the next two. |
| 7380 | */ |
| 7381 | if (typ != TC_ASI_SIMPLE && phase == 2) |
| 7382 | { |
| 7383 | if (discard) |
| 7384 | { |
| 7385 | /* obj prop RHS -> RHS prop obj */ |
| 7386 | G_cg->write_op(OPC_SWAPN); |
| 7387 | G_cs->write(0); |
| 7388 | G_cs->write(2); |
| 7389 | |
| 7390 | /* RHS prop obj -> RHS obj prop */ |
| 7391 | G_cg->write_op(OPC_SWAP); |
| 7392 | } |
| 7393 | else |
| 7394 | { |
| 7395 | /* obj prop RHS RHS -> RHS RHS obj prop */ |
| 7396 | G_cg->write_op(OPC_SWAP2); |
| 7397 | } |
| 7398 | } |
| 7399 | |
| 7400 | /* generate the PTRSETPROP instruction */ |
| 7401 | G_cg->write_op(OPC_PTRSETPROP); |
| 7402 | |
| 7403 | /* ptrsetprop removes three elements */ |
| 7404 | G_cg->note_pop(3); |
| 7405 | } |
| 7406 | else |
| 7407 | { |
| 7408 | /* constant property value */ |
| 7409 | |
| 7410 | /* |
| 7411 | * If this is phase 2, and we have an object on the stack, we |
| 7412 | * need to rearrange the stack into the proper order: |
| 7413 | * |
| 7414 | * !discard: stack = (obj RHS RHS), and we need (RHS RHS obj). |
| 7415 | * |
| 7416 | * discard: stack = (obj RHS), and we need (obj RHS). Just do |
| 7417 | * a SWAP. |
| 7418 | * |
| 7419 | * If there's no object on the stack, there's no need to do any |
| 7420 | * rearrangement. |
| 7421 | */ |
| 7422 | if (phase == 2 && !is_self && obj == VM_INVALID_OBJ) |
| 7423 | { |
| 7424 | if (discard) |
| 7425 | { |
| 7426 | /* obj RHS -> RHS obj */ |
| 7427 | G_cg->write_op(OPC_SWAP); |
| 7428 | } |
| 7429 | else |
| 7430 | { |
| 7431 | /* obj RHS RHS -> RHS RHS obj */ |
| 7432 | G_cg->write_op(OPC_SWAPN); |
| 7433 | G_cs->write(0); |
| 7434 | G_cs->write(2); |
| 7435 | } |
| 7436 | } |
| 7437 | |
| 7438 | /* |
| 7439 | * We have several instructions to choose from. If we're |
| 7440 | * assigning to a property of "self", use SETPROPSELF. If |
| 7441 | * we're assigning to a constant object, use OBJSETPROP. |
| 7442 | * Otherwise, use the plain SETPROP. |
| 7443 | */ |
| 7444 | if (is_self) |
| 7445 | { |
| 7446 | /* write the SETPROPSELF */ |
| 7447 | G_cg->write_op(OPC_SETPROPSELF); |
| 7448 | G_cs->write_prop_id(prop); |
| 7449 | |
| 7450 | /* setpropself removes the RHS value */ |
| 7451 | G_cg->note_pop(); |
| 7452 | } |
| 7453 | else if (obj != VM_INVALID_OBJ) |
| 7454 | { |
| 7455 | /* write the OBJSETPROP */ |
| 7456 | G_cg->write_op(OPC_OBJSETPROP); |
| 7457 | G_cs->write_obj_id(obj); |
| 7458 | G_cs->write_prop_id(prop); |
| 7459 | |
| 7460 | /* objsetprop removes the RHS value */ |
| 7461 | G_cg->note_pop(); |
| 7462 | } |
| 7463 | else |
| 7464 | { |
| 7465 | /* |
| 7466 | * write the normal SETPROP; we already generated the code |
| 7467 | * to push the object value, so it's where it should be |
| 7468 | */ |
| 7469 | G_cg->write_op(OPC_SETPROP); |
| 7470 | G_cs->write_prop_id(prop); |
| 7471 | |
| 7472 | /* setprop removes the value and the object */ |
| 7473 | G_cg->note_pop(2); |
| 7474 | } |
| 7475 | } |
| 7476 | } |
| 7477 | |
| 7478 | /* we're done with the operation */ |
| 7479 | return TRUE; |
| 7480 | } |
| 7481 | |
| 7482 | /* ------------------------------------------------------------------------ */ |
| 7483 | /* |
| 7484 | * member with argument list |
| 7485 | */ |
| 7486 | void CTPNMemArg::gen_code(int discard, int) |
| 7487 | { |
| 7488 | /* push the argument list */ |
| 7489 | int varargs; |
| 7490 | CTcNamedArgs named_args; |
| 7491 | get_arg_list()->gen_code_arglist(&varargs, named_args); |
| 7492 | |
| 7493 | /* ask the object expression to generate the code */ |
| 7494 | get_obj_expr()->gen_code_member(discard, get_prop_expr(), prop_is_expr_, |
| 7495 | get_arg_list()->get_argc(), |
| 7496 | varargs, &named_args); |
| 7497 | } |
| 7498 | |
| 7499 | /* ------------------------------------------------------------------------ */ |
| 7500 | /* |
| 7501 | * construct a list |
| 7502 | */ |
| 7503 | void CTPNList::gen_code(int discard, int for_condition) |
| 7504 | { |
| 7505 | /* |
| 7506 | * Before we construct the list dynamically, check to see if the |
| 7507 | * list is constant. If it is, we need only built the list in the |
| 7508 | * constant pool, and push its offset. |
| 7509 | */ |
| 7510 | if (is_const()) |
| 7511 | { |
| 7512 | /* push the value only if we're not discarding it */ |
| 7513 | if (!discard) |
| 7514 | { |
| 7515 | /* write the instruction */ |
| 7516 | G_cg->write_op(OPC_PUSHLST); |
| 7517 | |
| 7518 | /* add the list to the constant pool */ |
| 7519 | G_cg->add_const_list(this, G_cs, G_cs->get_ofs()); |
| 7520 | |
| 7521 | /* |
| 7522 | * write a placeholder address, which will be corrected by |
| 7523 | * the fixup that add_const_list() created |
| 7524 | */ |
| 7525 | G_cs->write4(0); |
| 7526 | |
| 7527 | /* note the push */ |
| 7528 | G_cg->note_push(); |
| 7529 | } |
| 7530 | } |
| 7531 | else |
| 7532 | { |
| 7533 | /* |
| 7534 | * It's not a constant list, so we must generate code to construct |
| 7535 | * a list dynamically. Push each element of the list. We need |
| 7536 | * each value (hence discard = false), and we require the |
| 7537 | * assignable value of each expression (hence for_condition = |
| 7538 | * false). Push the argument list in reverse order, since the |
| 7539 | * run-time metaclass requires this ordering. |
| 7540 | */ |
| 7541 | for (CTPNListEle *ele = get_tail() ; ele != 0 ; ele = ele->get_prev()) |
| 7542 | ele->gen_code(FALSE, FALSE); |
| 7543 | |
| 7544 | /* generate a NEW instruction for an object of metaclass LIST */ |
| 7545 | if (get_count() <= 255) |
| 7546 | { |
| 7547 | /* the count will fit in one byte - use the short form */ |
| 7548 | G_cg->write_op(OPC_NEW1); |
| 7549 | G_cs->write((char)get_count()); |
| 7550 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_LIST)); |
| 7551 | } |
| 7552 | else |
| 7553 | { |
| 7554 | /* count doesn't fit in one byte - use the long form */ |
| 7555 | G_cg->write_op(OPC_NEW2); |
| 7556 | G_cs->write2(get_count()); |
| 7557 | G_cs->write2(G_cg->get_predef_meta_idx(TCT3_METAID_LIST)); |
| 7558 | } |
| 7559 | |
| 7560 | /* new1/new2 remove arguments */ |
| 7561 | G_cg->note_pop(get_count()); |
| 7562 | |
| 7563 | /* if we're not discarding the value, push it */ |
| 7564 | if (!discard) |
| 7565 | { |
| 7566 | G_cg->write_op(OPC_GETR0); |
| 7567 | G_cg->note_push(); |
| 7568 | } |
| 7569 | } |
| 7570 | |
| 7571 | /* |
| 7572 | * If this is a lookup table list, and we're not discarding the value, |
| 7573 | * create the actual LookupTable object from the list source. If we're |
| 7574 | * discarding the value there's no need to create the object, since |
| 7575 | * doing so will have no meaningful side effects. |
| 7576 | */ |
| 7577 | if (!discard && is_lookup_table_) |
| 7578 | { |
| 7579 | /* |
| 7580 | * To construct the lookup table, we simply pass the list of [key, |
| 7581 | * value, key, value...] elements to the LookupTable constructor. |
| 7582 | * The list is already on the stack, so simply call the constructor |
| 7583 | * with one argument. |
| 7584 | */ |
| 7585 | G_cg->write_op(OPC_NEW1); |
| 7586 | G_cs->write(1); |
| 7587 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_LOOKUP_TABLE)); |
| 7588 | |
| 7589 | /* NEW1 removes arguments */ |
| 7590 | G_cg->note_pop(1); |
| 7591 | |
| 7592 | /* push the result */ |
| 7593 | G_cg->write_op(OPC_GETR0); |
| 7594 | G_cg->note_push(); |
| 7595 | } |
| 7596 | } |
| 7597 | |
| 7598 | /* ------------------------------------------------------------------------ */ |
| 7599 | /* |
| 7600 | * list element |
| 7601 | */ |
| 7602 | void CTPNListEle::gen_code(int discard, int for_condition) |
| 7603 | { |
| 7604 | /* generate the subexpression */ |
| 7605 | expr_->gen_code(discard, for_condition); |
| 7606 | } |
| 7607 | |
| 7608 | /* ------------------------------------------------------------------------ */ |
| 7609 | /* |
| 7610 | * Basic T3-specific symbol class |
| 7611 | */ |
| 7612 | |
| 7613 | /* |
| 7614 | * generate code to take the address of a symbol - in general, we cannot |
| 7615 | * take the address of a symbol, so we'll just log an error |
| 7616 | */ |
| 7617 | void CTcSymbol::gen_code_addr() |
| 7618 | { |
| 7619 | G_tok->log_error(TCERR_NO_ADDR_SYM, (int)get_sym_len(), get_sym()); |
| 7620 | } |
| 7621 | |
| 7622 | /* |
| 7623 | * generate code to assign to the symbol - in general, we cannot assign |
| 7624 | * to a symbol, so we'll just log an error |
| 7625 | */ |
| 7626 | int CTcSymbol::gen_code_asi(int, int phase, tc_asitype_t, const char *, |
| 7627 | class CTcPrsNode *, |
| 7628 | int ignore_error, int, void **) |
| 7629 | { |
| 7630 | /* |
| 7631 | * If we're ignoring errors, simply return false to indicate that |
| 7632 | * nothing happened. Ignore errors on phase 2, since we'll already |
| 7633 | * have generated an error on phase 1. |
| 7634 | */ |
| 7635 | if (ignore_error || phase > 1) |
| 7636 | return FALSE; |
| 7637 | |
| 7638 | /* log the error */ |
| 7639 | G_tok->log_error(TCERR_CANNOT_ASSIGN_SYM, (int)get_sym_len(), get_sym()); |
| 7640 | |
| 7641 | /* |
| 7642 | * even though we didn't generate anything, this has been fully |
| 7643 | * handled - the caller shouldn't attempt to generate any additional |
| 7644 | * code for this |
| 7645 | */ |
| 7646 | return TRUE; |
| 7647 | } |
| 7648 | |
| 7649 | /* |
| 7650 | * Generate code for calling the symbol. By default, we can't call a |
| 7651 | * symbol. |
| 7652 | */ |
| 7653 | void CTcSymbol::gen_code_call(int, int, int, CTcNamedArgs *) |
| 7654 | { |
| 7655 | /* log an error */ |
| 7656 | G_tok->log_error(TCERR_CANNOT_CALL_SYM, (int)get_sym_len(), get_sym()); |
| 7657 | } |
| 7658 | |
| 7659 | /* |
| 7660 | * Generate code for operator 'new' |
| 7661 | */ |
| 7662 | void CTcSymbol::gen_code_new(int, int, int, CTcNamedArgs *, int) |
| 7663 | { |
| 7664 | G_tok->log_error(TCERR_INVAL_NEW_EXPR); |
| 7665 | } |
| 7666 | |
| 7667 | /* |
| 7668 | * evaluate a property ID |
| 7669 | */ |
| 7670 | vm_prop_id_t CTcSymbol::gen_code_propid(int check_only, int is_expr) |
| 7671 | { |
| 7672 | /* by default, a symbol cannot be used as a property ID */ |
| 7673 | if (!check_only) |
| 7674 | G_tok->log_error(TCERR_SYM_NOT_PROP, (int)get_sym_len(), get_sym()); |
| 7675 | |
| 7676 | /* we can't return a valid property ID */ |
| 7677 | return VM_INVALID_PROP; |
| 7678 | } |
| 7679 | |
| 7680 | /* |
| 7681 | * evaluate a member expression |
| 7682 | */ |
| 7683 | void CTcSymbol::gen_code_member(int discard, |
| 7684 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 7685 | int argc, int varargs, |
| 7686 | CTcNamedArgs *named_args) |
| 7687 | { |
| 7688 | /* by default, a symbol cannot be used as an object expression */ |
| 7689 | G_tok->log_error(TCERR_SYM_NOT_OBJ, (int)get_sym_len(), get_sym()); |
| 7690 | } |
| 7691 | |
| 7692 | /* |
| 7693 | * generate code for an object expression before a '.' |
| 7694 | */ |
| 7695 | vm_obj_id_t CTcSymbol::gen_code_obj_predot(int *is_self) |
| 7696 | { |
| 7697 | /* by default, a symbol cannot be used as an object expression */ |
| 7698 | G_tok->log_error(TCERR_SYM_NOT_OBJ, (int)get_sym_len(), get_sym()); |
| 7699 | |
| 7700 | /* indicate that we don't have a constant object */ |
| 7701 | *is_self = FALSE; |
| 7702 | return VM_INVALID_OBJ; |
| 7703 | } |
| 7704 | |
| 7705 | |
| 7706 | |
| 7707 | /* ------------------------------------------------------------------------ */ |
| 7708 | /* |
| 7709 | * T3-specific function symbol class |
| 7710 | */ |
| 7711 | |
| 7712 | /* |
| 7713 | * evaluate the symbol |
| 7714 | */ |
| 7715 | void CTcSymFunc::gen_code(int discard) |
| 7716 | { |
| 7717 | /* |
| 7718 | * function address are always unknown during code generation; |
| 7719 | * generate a placeholder instruction and add a fixup record for it |
| 7720 | */ |
| 7721 | G_cg->write_op(OPC_PUSHFNPTR); |
| 7722 | |
| 7723 | /* add a fixup for the current code location */ |
| 7724 | add_abs_fixup(G_cs); |
| 7725 | |
| 7726 | /* write a placeholder offset - arbitrarily use zero */ |
| 7727 | G_cs->write4(0); |
| 7728 | |
| 7729 | /* note the push */ |
| 7730 | G_cg->note_push(); |
| 7731 | } |
| 7732 | |
| 7733 | /* |
| 7734 | * take the address of the function |
| 7735 | */ |
| 7736 | void CTcSymFunc::gen_code_addr() |
| 7737 | { |
| 7738 | /* '&function' is the same as 'function' with no arguments */ |
| 7739 | gen_code(FALSE); |
| 7740 | } |
| 7741 | |
| 7742 | |
| 7743 | /* |
| 7744 | * call the symbol |
| 7745 | */ |
| 7746 | void CTcSymFunc::gen_code_call(int discard, int argc, int varargs, |
| 7747 | CTcNamedArgs *named_args) |
| 7748 | { |
| 7749 | /* we can't call a function that doesn't have a prototype */ |
| 7750 | if (!has_proto_) |
| 7751 | { |
| 7752 | G_tok->log_error(TCERR_FUNC_CALL_NO_PROTO, |
| 7753 | (int)get_sym_len(), get_sym()); |
| 7754 | return; |
| 7755 | } |
| 7756 | |
| 7757 | /* |
| 7758 | * If this is a multi-method base function, a call to the function is |
| 7759 | * actually a call to _multiMethodCall('name', args). |
| 7760 | */ |
| 7761 | if (is_multimethod_base_) |
| 7762 | { |
| 7763 | /* make a list out of the arguments */ |
| 7764 | if (varargs) |
| 7765 | { |
| 7766 | G_cg->write_op(OPC_VARARGC); |
| 7767 | G_cs->write((char)argc); |
| 7768 | } |
| 7769 | else if (argc <= 255) |
| 7770 | { |
| 7771 | G_cg->write_op(OPC_NEW1); |
| 7772 | G_cs->write((char)argc); |
| 7773 | } |
| 7774 | else |
| 7775 | { |
| 7776 | G_cg->write_op(OPC_NEW2); |
| 7777 | G_cs->write2(argc); |
| 7778 | } |
| 7779 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_LIST)); |
| 7780 | G_cg->note_pop(argc); |
| 7781 | |
| 7782 | G_cg->write_op(OPC_GETR0); |
| 7783 | G_cg->note_push(); |
| 7784 | |
| 7785 | /* add the base function pointer argument */ |
| 7786 | CTcConstVal funcval; |
| 7787 | funcval.set_funcptr(this); |
| 7788 | CTPNConst func(&funcval); |
| 7789 | func.gen_code(FALSE, FALSE); |
| 7790 | |
| 7791 | /* look up _multiMethodCall */ |
| 7792 | CTcSymFunc *mmc = get_internal_func( |
| 7793 | "_multiMethodCall", 1, 0, TRUE, TRUE); |
| 7794 | |
| 7795 | /* |
| 7796 | * Generate the call. Note that there are always two arguments at |
| 7797 | * this point: the base function pointer, and the argument list. |
| 7798 | * The argument list is just one argument because we've already |
| 7799 | * constructed a list out of it. |
| 7800 | */ |
| 7801 | if (mmc != 0) |
| 7802 | mmc->gen_code_call(discard, 2, FALSE, named_args); |
| 7803 | } |
| 7804 | else |
| 7805 | { |
| 7806 | /* write the varargs modifier if appropriate */ |
| 7807 | if (varargs) |
| 7808 | G_cg->write_op(OPC_VARARGC); |
| 7809 | |
| 7810 | /* generate the call instruction and argument count */ |
| 7811 | G_cg->write_op(OPC_CALL); |
| 7812 | G_cs->write((char)argc); |
| 7813 | |
| 7814 | /* check the mode */ |
| 7815 | if (G_cg->is_eval_for_dyn()) |
| 7816 | { |
| 7817 | /* |
| 7818 | * dynamic (run-time) compilation - we know the absolute |
| 7819 | * address already, since all symbols are pre-resolved in the |
| 7820 | * debugger |
| 7821 | */ |
| 7822 | G_cs->write4(get_code_pool_addr()); |
| 7823 | } |
| 7824 | else |
| 7825 | { |
| 7826 | /* |
| 7827 | * Normal compilation - we won't know the function's address |
| 7828 | * until after generation is completed, so add a fixup for the |
| 7829 | * current location, then write a placeholder for the offset |
| 7830 | * field. |
| 7831 | */ |
| 7832 | add_abs_fixup(G_cs); |
| 7833 | G_cs->write4(0); |
| 7834 | } |
| 7835 | |
| 7836 | /* call removes arguments */ |
| 7837 | G_cg->note_pop(argc); |
| 7838 | |
| 7839 | /* do post-call cleanup: named arg removal, etc */ |
| 7840 | G_cg->post_call_cleanup(named_args); |
| 7841 | |
| 7842 | /* make sure the argument count is correct */ |
| 7843 | if (!argc_ok(argc)) |
| 7844 | { |
| 7845 | char buf[128]; |
| 7846 | G_tok->log_error(TCERR_WRONG_ARGC_FOR_FUNC, |
| 7847 | (int)get_sym_len(), get_sym(), |
| 7848 | get_argc_desc(buf), argc); |
| 7849 | } |
| 7850 | |
| 7851 | /* if we're not discarding, push the return value from R0 */ |
| 7852 | if (!discard) |
| 7853 | { |
| 7854 | G_cg->write_op(OPC_GETR0); |
| 7855 | G_cg->note_push(); |
| 7856 | } |
| 7857 | } |
| 7858 | } |
| 7859 | |
| 7860 | /* |
| 7861 | * Get my code pool address. Valid only after linking. |
| 7862 | */ |
| 7863 | ulong CTcSymFunc::get_code_pool_addr() const |
| 7864 | { |
| 7865 | /* check for an absolute address */ |
| 7866 | if (abs_addr_valid_) |
| 7867 | { |
| 7868 | /* |
| 7869 | * we have an absolute address - this means the symbol was |
| 7870 | * loaded from a fully-linked image file (specifically, from the |
| 7871 | * debug records) |
| 7872 | */ |
| 7873 | return abs_addr_; |
| 7874 | } |
| 7875 | else |
| 7876 | { |
| 7877 | /* |
| 7878 | * we don't have an absolute address, so our address must have |
| 7879 | * been determined through a linking step - get the final |
| 7880 | * address from the anchor |
| 7881 | */ |
| 7882 | return anchor_->get_addr(); |
| 7883 | } |
| 7884 | } |
| 7885 | |
| 7886 | /* |
| 7887 | * add a runtime symbol table entry |
| 7888 | */ |
| 7889 | void CTcSymFunc::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 7890 | { |
| 7891 | vm_val_t val; |
| 7892 | |
| 7893 | /* add an entry for our absolute address */ |
| 7894 | val.set_fnptr(get_code_pool_addr()); |
| 7895 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 7896 | } |
| 7897 | |
| 7898 | |
| 7899 | /* ------------------------------------------------------------------------ */ |
| 7900 | /* |
| 7901 | * T3-specific object symbol class |
| 7902 | */ |
| 7903 | |
| 7904 | /* |
| 7905 | * evaluate the symbol |
| 7906 | */ |
| 7907 | void CTcSymObj::gen_code(int discard) |
| 7908 | { |
| 7909 | /* write code to push the object ID */ |
| 7910 | if (!discard) |
| 7911 | { |
| 7912 | /* push the object */ |
| 7913 | G_cg->write_op(OPC_PUSHOBJ); |
| 7914 | G_cs->write_obj_id(obj_id_); |
| 7915 | |
| 7916 | /* note the push */ |
| 7917 | G_cg->note_push(); |
| 7918 | } |
| 7919 | } |
| 7920 | |
| 7921 | /* |
| 7922 | * take the address of the object |
| 7923 | */ |
| 7924 | void CTcSymObj::gen_code_addr() |
| 7925 | { |
| 7926 | /* act as though we were pushing the object ID directly */ |
| 7927 | gen_code(FALSE); |
| 7928 | } |
| 7929 | |
| 7930 | /* |
| 7931 | * Generate a 'new' expression |
| 7932 | */ |
| 7933 | void CTcSymObj::gen_code_new(int discard, int argc, |
| 7934 | int varargs, CTcNamedArgs *named_args, |
| 7935 | int is_transient) |
| 7936 | { |
| 7937 | /* use our static generator */ |
| 7938 | s_gen_code_new(discard, obj_id_, metaclass_, |
| 7939 | argc, varargs, named_args, is_transient); |
| 7940 | } |
| 7941 | |
| 7942 | /* |
| 7943 | * Generate a 'new' expression. (This is a static method so that this |
| 7944 | * code can be used by all of the possible expression types to which |
| 7945 | * 'new' can be applied.) |
| 7946 | * |
| 7947 | * This type of generation applies only to objects of metaclass TADS |
| 7948 | * Object. |
| 7949 | */ |
| 7950 | void CTcSymObj::s_gen_code_new(int discard, vm_obj_id_t obj_id, |
| 7951 | tc_metaclass_t meta, |
| 7952 | int argc, int varargs, |
| 7953 | CTcNamedArgs *named_args, |
| 7954 | int is_transient) |
| 7955 | { |
| 7956 | /* |
| 7957 | * push the base class object - this is always the first argument |
| 7958 | * (hence last pushed) to the metaclass constructor |
| 7959 | */ |
| 7960 | G_cg->write_op(OPC_PUSHOBJ); |
| 7961 | G_cs->write_obj_id(obj_id); |
| 7962 | |
| 7963 | /* note the push */ |
| 7964 | G_cg->note_push(); |
| 7965 | |
| 7966 | /* |
| 7967 | * note that we can only allow 126 arguments to a constructor, |
| 7968 | * because we must add the implicit superclass argument |
| 7969 | */ |
| 7970 | if (argc > 126) |
| 7971 | G_tok->log_error(TCERR_TOO_MANY_CTOR_ARGS); |
| 7972 | |
| 7973 | /* |
| 7974 | * if we have varargs, swap the top stack elements to get the |
| 7975 | * argument count back on top, and then generate the varargs |
| 7976 | * modifier opcode |
| 7977 | */ |
| 7978 | if (varargs) |
| 7979 | { |
| 7980 | /* swap the top stack elements to get argc back to the top */ |
| 7981 | G_cg->write_op(OPC_SWAP); |
| 7982 | |
| 7983 | /* |
| 7984 | * increment the argument count to account for the superclass |
| 7985 | * object argument |
| 7986 | */ |
| 7987 | G_cg->write_op(OPC_INC); |
| 7988 | |
| 7989 | /* write the varargs modifier opcode */ |
| 7990 | G_cg->write_op(OPC_VARARGC); |
| 7991 | } |
| 7992 | |
| 7993 | /* |
| 7994 | * write the NEW instruction - since we always add TADS Object to |
| 7995 | * our metaclass table before we start compiling any code, we know |
| 7996 | * it always has a small metaclass number and will always fit in the |
| 7997 | * short form of the instruction |
| 7998 | * |
| 7999 | * Note that the actual argument count we generate is one higher |
| 8000 | * than the source code argument list, because we add the implicit |
| 8001 | * first argument to the metaclass constructor |
| 8002 | */ |
| 8003 | G_cg->write_op(is_transient ? OPC_TRNEW1 : OPC_NEW1); |
| 8004 | G_cs->write((char)(argc + 1)); |
| 8005 | |
| 8006 | /* write out the dependency table index for the metaclass */ |
| 8007 | switch (meta) |
| 8008 | { |
| 8009 | case TC_META_TADSOBJ: |
| 8010 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_TADSOBJ)); |
| 8011 | break; |
| 8012 | |
| 8013 | default: |
| 8014 | /* we can't use 'new' on symbols of other metaclasses */ |
| 8015 | G_tok->log_error(TCERR_BAD_META_FOR_NEW); |
| 8016 | G_cs->write(0); |
| 8017 | break; |
| 8018 | } |
| 8019 | |
| 8020 | /* new1 removes the arguments */ |
| 8021 | G_cg->note_pop(argc + 1); |
| 8022 | |
| 8023 | /* do post-call cleanup: named arg removal, etc */ |
| 8024 | G_cg->post_call_cleanup(named_args); |
| 8025 | |
| 8026 | /* |
| 8027 | * if they're not discarding the value, push the new object |
| 8028 | * reference, which will be in R0 when the constructor returns |
| 8029 | */ |
| 8030 | if (!discard) |
| 8031 | { |
| 8032 | G_cg->write_op(OPC_GETR0); |
| 8033 | G_cg->note_push(); |
| 8034 | } |
| 8035 | } |
| 8036 | |
| 8037 | /* |
| 8038 | * Generate code for a member expression |
| 8039 | */ |
| 8040 | void CTcSymObj::gen_code_member(int discard, |
| 8041 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 8042 | int argc, int varargs, |
| 8043 | CTcNamedArgs *named_args) |
| 8044 | { |
| 8045 | s_gen_code_member(discard, prop_expr, prop_is_expr, |
| 8046 | argc, obj_id_, varargs, named_args); |
| 8047 | } |
| 8048 | |
| 8049 | /* |
| 8050 | * Static method to generate code for a member expression. This is |
| 8051 | * static so that constant object nodes can share it. |
| 8052 | */ |
| 8053 | void CTcSymObj::s_gen_code_member(int discard, |
| 8054 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 8055 | int argc, vm_obj_id_t obj_id, |
| 8056 | int varargs, CTcNamedArgs *named_args) |
| 8057 | { |
| 8058 | vm_prop_id_t prop; |
| 8059 | |
| 8060 | /* |
| 8061 | * generate the property expression - don't generate the code right |
| 8062 | * now even if code generation is necessary, because this isn't the |
| 8063 | * right place for it; for now, simply check to determine if we're |
| 8064 | * going to need to generate any code for the property expression |
| 8065 | */ |
| 8066 | prop = prop_expr->gen_code_propid(TRUE, prop_is_expr); |
| 8067 | |
| 8068 | /* don't allow method calls with arguments in speculative mode */ |
| 8069 | if (argc != 0 && G_cg->is_speculative()) |
| 8070 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 8071 | |
| 8072 | /* check for a constant property value */ |
| 8073 | if (prop != VM_INVALID_PROP) |
| 8074 | { |
| 8075 | /* generate an OBJGETPROP or OBJCALLPROP as appropriate */ |
| 8076 | if (G_cg->is_speculative()) |
| 8077 | { |
| 8078 | /* speculative evaluation - use GETPROPDATA */ |
| 8079 | G_cg->write_op(OPC_PUSHOBJ); |
| 8080 | G_cs->write_obj_id(obj_id); |
| 8081 | G_cg->write_op(OPC_GETPROPDATA); |
| 8082 | G_cs->write_prop_id(prop); |
| 8083 | |
| 8084 | /* we pushed the object, then popped it */ |
| 8085 | G_cg->note_push(); |
| 8086 | G_cg->note_pop(); |
| 8087 | } |
| 8088 | else if (argc == 0) |
| 8089 | { |
| 8090 | /* no arguments - use OBJGETPROP */ |
| 8091 | G_cg->write_op(OPC_OBJGETPROP); |
| 8092 | G_cs->write_obj_id(obj_id); |
| 8093 | G_cs->write_prop_id(prop); |
| 8094 | } |
| 8095 | else |
| 8096 | { |
| 8097 | /* generate a varargs modifier if needed */ |
| 8098 | if (varargs) |
| 8099 | G_cg->write_op(OPC_VARARGC); |
| 8100 | |
| 8101 | /* arguments - use OBJCALLPROP */ |
| 8102 | G_cg->write_op(OPC_OBJCALLPROP); |
| 8103 | G_cs->write((char)argc); |
| 8104 | G_cs->write_obj_id(obj_id); |
| 8105 | G_cs->write_prop_id(prop); |
| 8106 | |
| 8107 | /* objcallprop removes arguments */ |
| 8108 | G_cg->note_pop(argc); |
| 8109 | } |
| 8110 | } |
| 8111 | else |
| 8112 | { |
| 8113 | /* |
| 8114 | * non-constant property value - we must first push the object |
| 8115 | * value, then push the property value, then write a PTRCALLPROP |
| 8116 | * instruction |
| 8117 | */ |
| 8118 | |
| 8119 | /* generate the object push */ |
| 8120 | G_cg->write_op(OPC_PUSHOBJ); |
| 8121 | G_cs->write_obj_id(obj_id); |
| 8122 | |
| 8123 | /* note the pushes */ |
| 8124 | G_cg->note_push(); |
| 8125 | |
| 8126 | /* keep the argument counter on top if necessary */ |
| 8127 | if (varargs) |
| 8128 | G_cg->write_op(OPC_SWAP); |
| 8129 | |
| 8130 | /* generate the property push */ |
| 8131 | prop_expr->gen_code_propid(FALSE, prop_is_expr); |
| 8132 | |
| 8133 | /* generate the PTRCALLPROP or PTRGETPROPDATA */ |
| 8134 | if (G_cg->is_speculative()) |
| 8135 | { |
| 8136 | /* speculative - use the data-only property evaluation */ |
| 8137 | G_cg->write_op(OPC_PTRGETPROPDATA); |
| 8138 | } |
| 8139 | else |
| 8140 | { |
| 8141 | /* |
| 8142 | * if we have a varargs list, modify the call instruction |
| 8143 | * that follows to make it a varargs call |
| 8144 | */ |
| 8145 | if (varargs) |
| 8146 | { |
| 8147 | /* swap to get the arg counter back on top */ |
| 8148 | G_cg->write_op(OPC_SWAP); |
| 8149 | |
| 8150 | /* write the varargs modifier */ |
| 8151 | G_cg->write_op(OPC_VARARGC); |
| 8152 | } |
| 8153 | |
| 8154 | /* normal - call the property */ |
| 8155 | G_cg->write_op(OPC_PTRCALLPROP); |
| 8156 | G_cs->write((int)argc); |
| 8157 | } |
| 8158 | |
| 8159 | /* ptrcallprop removes the arguments, the object, and the property */ |
| 8160 | G_cg->note_pop(argc + 2); |
| 8161 | } |
| 8162 | |
| 8163 | /* do post-call cleanup: named arg removal, etc */ |
| 8164 | G_cg->post_call_cleanup(named_args); |
| 8165 | |
| 8166 | /* if they want the result, push it onto the stack */ |
| 8167 | if (!discard) |
| 8168 | { |
| 8169 | G_cg->write_op(OPC_GETR0); |
| 8170 | G_cg->note_push(); |
| 8171 | } |
| 8172 | } |
| 8173 | |
| 8174 | /* |
| 8175 | * generate code for an object before a '.' |
| 8176 | */ |
| 8177 | vm_obj_id_t CTcSymObj::gen_code_obj_predot(int *is_self) |
| 8178 | { |
| 8179 | /* return our constant object reference */ |
| 8180 | *is_self = FALSE; |
| 8181 | return obj_id_; |
| 8182 | } |
| 8183 | |
| 8184 | /* |
| 8185 | * add a runtime symbol table entry |
| 8186 | */ |
| 8187 | void CTcSymObj::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 8188 | { |
| 8189 | vm_val_t val; |
| 8190 | |
| 8191 | /* add our entry */ |
| 8192 | val.set_obj(obj_id_); |
| 8193 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 8194 | } |
| 8195 | |
| 8196 | /* ------------------------------------------------------------------------ */ |
| 8197 | /* |
| 8198 | * Invokable object |
| 8199 | */ |
| 8200 | void CTcSymFuncObj::gen_code_call(int discard, int argc, int varargs, |
| 8201 | CTcNamedArgs *named_args) |
| 8202 | { |
| 8203 | /* write the varargs modifier if appropriate */ |
| 8204 | if (varargs) |
| 8205 | G_cg->write_op(OPC_VARARGC); |
| 8206 | |
| 8207 | /* push my value as the function pointer */ |
| 8208 | gen_code(discard); |
| 8209 | |
| 8210 | /* generate the pointer-call instruction and argument count */ |
| 8211 | G_cg->write_op(OPC_PTRCALL); |
| 8212 | G_cs->write((char)argc); |
| 8213 | |
| 8214 | /* ptrcall removes arguments and the function pointer */ |
| 8215 | G_cg->note_pop(argc + 1); |
| 8216 | |
| 8217 | /* do post-call cleanup: named arg removal, etc */ |
| 8218 | G_cg->post_call_cleanup(named_args); |
| 8219 | |
| 8220 | /* if we're not discarding, push the return value from R0 */ |
| 8221 | if (!discard) |
| 8222 | { |
| 8223 | G_cg->write_op(OPC_GETR0); |
| 8224 | G_cg->note_push(); |
| 8225 | } |
| 8226 | } |
| 8227 | |
| 8228 | /* ------------------------------------------------------------------------ */ |
| 8229 | /* |
| 8230 | * T3-specific property symbol class |
| 8231 | */ |
| 8232 | |
| 8233 | /* |
| 8234 | * evaluate the symbol |
| 8235 | */ |
| 8236 | void CTcSymProp::gen_code(int discard) |
| 8237 | { |
| 8238 | /* |
| 8239 | * Evaluating a property is equivalent to calling the property on |
| 8240 | * the "self" object with no arguments. If there's no "self" |
| 8241 | * object, an unqualified property evaluation is not possible, so |
| 8242 | * log an error if this is the case. |
| 8243 | */ |
| 8244 | if (!G_cs->is_self_available()) |
| 8245 | { |
| 8246 | G_tok->log_error(TCERR_PROP_NEEDS_OBJ, (int)get_sym_len(), get_sym()); |
| 8247 | return; |
| 8248 | } |
| 8249 | |
| 8250 | if (G_cg->is_speculative()) |
| 8251 | { |
| 8252 | /* push 'self', then evaluate the property in data-only mode */ |
| 8253 | G_cg->write_op(OPC_PUSHSELF); |
| 8254 | G_cg->write_op(OPC_GETPROPDATA); |
| 8255 | G_cs->write_prop_id(prop_); |
| 8256 | |
| 8257 | /* we pushed the 'self' value then popped it again */ |
| 8258 | G_cg->note_push(); |
| 8259 | G_cg->note_pop(); |
| 8260 | } |
| 8261 | else |
| 8262 | { |
| 8263 | /* generate the call to 'self' */ |
| 8264 | G_cg->write_op(OPC_GETPROPSELF); |
| 8265 | G_cs->write_prop_id(prop_); |
| 8266 | } |
| 8267 | |
| 8268 | /* if they're not discarding the value, push the result */ |
| 8269 | if (!discard) |
| 8270 | { |
| 8271 | G_cg->write_op(OPC_GETR0); |
| 8272 | G_cg->note_push(); |
| 8273 | } |
| 8274 | } |
| 8275 | |
| 8276 | /* |
| 8277 | * evaluate a member expression |
| 8278 | */ |
| 8279 | void CTcSymProp::gen_code_member(int discard, |
| 8280 | CTcPrsNode *prop_expr, int prop_is_expr, |
| 8281 | int argc, int varargs, |
| 8282 | CTcNamedArgs *named_args) |
| 8283 | { |
| 8284 | /* generate code to evaluate the property */ |
| 8285 | gen_code(FALSE); |
| 8286 | |
| 8287 | /* if we have an argument counter, put it back on top */ |
| 8288 | if (varargs) |
| 8289 | G_cg->write_op(OPC_SWAP); |
| 8290 | |
| 8291 | /* use the standard member generation */ |
| 8292 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 8293 | argc, varargs, named_args); |
| 8294 | } |
| 8295 | |
| 8296 | /* |
| 8297 | * take the address of the property |
| 8298 | */ |
| 8299 | void CTcSymProp::gen_code_addr() |
| 8300 | { |
| 8301 | /* write code to push the property ID */ |
| 8302 | G_cg->write_op(OPC_PUSHPROPID); |
| 8303 | G_cs->write_prop_id(prop_); |
| 8304 | |
| 8305 | /* note the push */ |
| 8306 | G_cg->note_push(); |
| 8307 | } |
| 8308 | |
| 8309 | /* |
| 8310 | * assign to a property, implicitly of the "self" object |
| 8311 | */ |
| 8312 | int CTcSymProp::gen_code_asi(int discard, int phase, |
| 8313 | tc_asitype_t typ, const char *, |
| 8314 | class CTcPrsNode *rhs, |
| 8315 | int /*ignore_errors*/, int /*explicit*/, |
| 8316 | void ** /*ctx*/) |
| 8317 | { |
| 8318 | /* if there's no "self" object, we can't make this assignment */ |
| 8319 | if (!G_cs->is_self_available()) |
| 8320 | { |
| 8321 | /* log an error if it's phase 1 */ |
| 8322 | if (phase == 1) |
| 8323 | G_tok->log_error(TCERR_SETPROP_NEEDS_OBJ, |
| 8324 | (int)get_sym_len(), get_sym()); |
| 8325 | |
| 8326 | /* |
| 8327 | * indicate that we're finished, since there's nothing more we |
| 8328 | * can do here |
| 8329 | */ |
| 8330 | return TRUE; |
| 8331 | } |
| 8332 | |
| 8333 | /* check what we're doing */ |
| 8334 | if (typ == TC_ASI_SIMPLE) |
| 8335 | { |
| 8336 | /* |
| 8337 | * Simple assignment. Generate the right-hand side's expression |
| 8338 | * for assignment, unless the caller has already done so. |
| 8339 | */ |
| 8340 | if (rhs != 0) |
| 8341 | rhs->gen_code(FALSE, FALSE); |
| 8342 | |
| 8343 | /* |
| 8344 | * if we're not discarding the value, make a copy - we'll consume a |
| 8345 | * copy in the SETPROP instruction, so we need one more copy to |
| 8346 | * return to the enclosing expression |
| 8347 | */ |
| 8348 | if (!discard) |
| 8349 | { |
| 8350 | G_cg->write_op(OPC_DUP); |
| 8351 | G_cg->note_push(); |
| 8352 | } |
| 8353 | |
| 8354 | /* |
| 8355 | * write the SETPROP instruction - use the special form to assign |
| 8356 | * to "self" |
| 8357 | */ |
| 8358 | G_cg->write_op(OPC_SETPROPSELF); |
| 8359 | G_cs->write_prop_id(prop_); |
| 8360 | |
| 8361 | /* setpropself removes the value */ |
| 8362 | G_cg->note_pop(); |
| 8363 | |
| 8364 | /* handled */ |
| 8365 | return TRUE; |
| 8366 | } |
| 8367 | else if (phase == 1) |
| 8368 | { |
| 8369 | /* |
| 8370 | * Phase 1 of a compound assignment. Generate the property value |
| 8371 | * so that the compound operator can combine it with the RHS. |
| 8372 | */ |
| 8373 | gen_code(FALSE); |
| 8374 | |
| 8375 | /* proceed with the unrolled assignment */ |
| 8376 | return FALSE; |
| 8377 | } |
| 8378 | else if (phase == 2) |
| 8379 | { |
| 8380 | /* |
| 8381 | * Phase 2 of a compound assignment. The combined value of |
| 8382 | * (self.prop <op> RHS) is on the stack, so complete the |
| 8383 | * assignment. |
| 8384 | */ |
| 8385 | |
| 8386 | /* if the caller will want to use the RHS value, make a copy */ |
| 8387 | if (!discard) |
| 8388 | { |
| 8389 | G_cg->write_op(OPC_DUP); |
| 8390 | G_cg->note_push(); |
| 8391 | } |
| 8392 | |
| 8393 | /* write the SETPROP */ |
| 8394 | G_cg->write_op(OPC_SETPROPSELF); |
| 8395 | G_cs->write_prop_id(prop_); |
| 8396 | |
| 8397 | /* setpropself removes the value */ |
| 8398 | G_cg->note_pop(); |
| 8399 | |
| 8400 | /* done */ |
| 8401 | return TRUE; |
| 8402 | } |
| 8403 | else |
| 8404 | { |
| 8405 | /* other phases aren't handled */ |
| 8406 | return FALSE; |
| 8407 | } |
| 8408 | } |
| 8409 | |
| 8410 | /* |
| 8411 | * call the symbol |
| 8412 | */ |
| 8413 | void CTcSymProp::gen_code_call(int discard, int argc, int varargs, |
| 8414 | CTcNamedArgs *named_args) |
| 8415 | { |
| 8416 | /* |
| 8417 | * if there's no "self", we can't invoke a property without an |
| 8418 | * explicit object reference |
| 8419 | */ |
| 8420 | if (!G_cs->is_self_available()) |
| 8421 | { |
| 8422 | G_tok->log_error(TCERR_PROP_NEEDS_OBJ, (int)get_sym_len(), get_sym()); |
| 8423 | return; |
| 8424 | } |
| 8425 | |
| 8426 | /* don't allow calling with arguments in speculative mode */ |
| 8427 | if (G_cg->is_speculative() |
| 8428 | && (argc != 0 || (named_args != 0 && named_args->cnt != 0))) |
| 8429 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 8430 | |
| 8431 | /* generate code to invoke the property of "self" */ |
| 8432 | if (G_cg->is_speculative()) |
| 8433 | { |
| 8434 | /* push 'self', then get the property in data-only mode */ |
| 8435 | G_cg->write_op(OPC_PUSHSELF); |
| 8436 | G_cg->write_op(OPC_GETPROPDATA); |
| 8437 | G_cs->write_prop_id(get_prop()); |
| 8438 | |
| 8439 | /* we pushed 'self' then popped it again */ |
| 8440 | G_cg->note_push(); |
| 8441 | G_cg->note_pop(); |
| 8442 | } |
| 8443 | else if (argc == 0) |
| 8444 | { |
| 8445 | /* use the instruction with no arguments */ |
| 8446 | G_cg->write_op(OPC_GETPROPSELF); |
| 8447 | G_cs->write_prop_id(get_prop()); |
| 8448 | } |
| 8449 | else |
| 8450 | { |
| 8451 | /* write the varargs modifier if appropriate */ |
| 8452 | if (varargs) |
| 8453 | G_cg->write_op(OPC_VARARGC); |
| 8454 | |
| 8455 | /* use the instruction with arguments */ |
| 8456 | G_cg->write_op(OPC_CALLPROPSELF); |
| 8457 | G_cs->write((char)argc); |
| 8458 | G_cs->write_prop_id(get_prop()); |
| 8459 | |
| 8460 | /* callpropself removes arguments */ |
| 8461 | G_cg->note_pop(argc); |
| 8462 | } |
| 8463 | |
| 8464 | /* do post-call cleanup: named arg removal, etc */ |
| 8465 | G_cg->post_call_cleanup(named_args); |
| 8466 | |
| 8467 | /* if we're not discarding, push the return value from R0 */ |
| 8468 | if (!discard) |
| 8469 | { |
| 8470 | G_cg->write_op(OPC_GETR0); |
| 8471 | G_cg->note_push(); |
| 8472 | } |
| 8473 | } |
| 8474 | |
| 8475 | /* |
| 8476 | * generate a property ID expression |
| 8477 | */ |
| 8478 | vm_prop_id_t CTcSymProp::gen_code_propid(int check_only, int is_expr) |
| 8479 | { |
| 8480 | /* |
| 8481 | * If I'm to be treated as an expression (which indicates that the |
| 8482 | * property symbol is explicitly enclosed in parentheses in the |
| 8483 | * original source code expression), then I must evaluate this |
| 8484 | * property of self. Otherwise, I yield literally the property ID. |
| 8485 | */ |
| 8486 | if (is_expr) |
| 8487 | { |
| 8488 | /* generate code unless we're only checking */ |
| 8489 | if (!check_only) |
| 8490 | { |
| 8491 | /* evaluate this property of self */ |
| 8492 | G_cg->write_op(OPC_GETPROPSELF); |
| 8493 | G_cs->write_prop_id(get_prop()); |
| 8494 | |
| 8495 | /* leave the result on the stack */ |
| 8496 | G_cg->write_op(OPC_GETR0); |
| 8497 | G_cg->note_push(); |
| 8498 | } |
| 8499 | |
| 8500 | /* tell the caller to use the stack value */ |
| 8501 | return VM_INVALID_PROP; |
| 8502 | } |
| 8503 | else |
| 8504 | { |
| 8505 | /* simple '.prop' - return my property ID as a constant value */ |
| 8506 | return get_prop(); |
| 8507 | } |
| 8508 | } |
| 8509 | |
| 8510 | /* |
| 8511 | * add a runtime symbol table entry |
| 8512 | */ |
| 8513 | void CTcSymProp::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 8514 | { |
| 8515 | vm_val_t val; |
| 8516 | |
| 8517 | /* add our entry */ |
| 8518 | val.set_propid(get_prop()); |
| 8519 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 8520 | } |
| 8521 | |
| 8522 | /* ------------------------------------------------------------------------ */ |
| 8523 | /* |
| 8524 | * Enumerator symbol |
| 8525 | */ |
| 8526 | |
| 8527 | /* |
| 8528 | * evaluate the symbol |
| 8529 | */ |
| 8530 | void CTcSymEnum::gen_code(int discard) |
| 8531 | { |
| 8532 | if (!discard) |
| 8533 | { |
| 8534 | /* generate code to push the enum value */ |
| 8535 | G_cg->write_op(OPC_PUSHENUM); |
| 8536 | G_cs->write_enum_id(get_enum_id()); |
| 8537 | |
| 8538 | /* note the push */ |
| 8539 | G_cg->note_push(); |
| 8540 | } |
| 8541 | } |
| 8542 | |
| 8543 | /* |
| 8544 | * add a runtime symbol table entry |
| 8545 | */ |
| 8546 | void CTcSymEnum::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 8547 | { |
| 8548 | vm_val_t val; |
| 8549 | |
| 8550 | /* add our entry */ |
| 8551 | val.set_enum(get_enum_id()); |
| 8552 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 8553 | } |
| 8554 | |
| 8555 | |
| 8556 | /* ------------------------------------------------------------------------ */ |
| 8557 | /* |
| 8558 | * T3-specific local variable/parameter symbol class |
| 8559 | */ |
| 8560 | |
| 8561 | /* |
| 8562 | * generate code to evaluate the symbol |
| 8563 | */ |
| 8564 | void CTcSymLocal::gen_code(int discard) |
| 8565 | { |
| 8566 | /* generate code to push the local, if we're not discarding it */ |
| 8567 | if (!discard) |
| 8568 | { |
| 8569 | /* |
| 8570 | * generate as a context local if required, otherwise as an |
| 8571 | * ordinary local variable |
| 8572 | */ |
| 8573 | if (is_ctx_local_) |
| 8574 | { |
| 8575 | /* generate the context array lookup */ |
| 8576 | if (ctx_var_num_ <= 255 && get_ctx_arr_idx() <= 255) |
| 8577 | { |
| 8578 | /* we can do this whole operation with one instruction */ |
| 8579 | G_cg->write_op(OPC_IDXLCL1INT8); |
| 8580 | G_cs->write((uchar)ctx_var_num_); |
| 8581 | G_cs->write((uchar)get_ctx_arr_idx()); |
| 8582 | |
| 8583 | /* this pushes one value */ |
| 8584 | G_cg->note_push(); |
| 8585 | } |
| 8586 | else |
| 8587 | { |
| 8588 | /* get our context array */ |
| 8589 | s_gen_code_getlcl(ctx_var_num_, FALSE); |
| 8590 | |
| 8591 | /* get our value from the context array */ |
| 8592 | CTPNConst::s_gen_code_int(get_ctx_arr_idx()); |
| 8593 | G_cg->write_op(OPC_INDEX); |
| 8594 | |
| 8595 | /* the INDEX operation removes two values and pushes one */ |
| 8596 | G_cg->note_pop(); |
| 8597 | } |
| 8598 | } |
| 8599 | else |
| 8600 | { |
| 8601 | /* generate as an ordinary local */ |
| 8602 | s_gen_code_getlcl(get_var_num(), is_param()); |
| 8603 | } |
| 8604 | } |
| 8605 | |
| 8606 | /* |
| 8607 | * Mark the value as referenced, whether or not we're generating the |
| 8608 | * code - the value has been logically referenced in the program |
| 8609 | * even if the result of evaluating it isn't needed. |
| 8610 | */ |
| 8611 | set_val_used(TRUE); |
| 8612 | } |
| 8613 | |
| 8614 | /* |
| 8615 | * generate code to push a local onto the stack |
| 8616 | */ |
| 8617 | void CTcSymLocal::s_gen_code_getlcl(int var_num, int is_param) |
| 8618 | { |
| 8619 | /* use the shortest form of the instruction that we can */ |
| 8620 | if (is_param && var_num < 4) |
| 8621 | { |
| 8622 | /* arguments 0-3 have dedicated opcodes */ |
| 8623 | G_cg->write_op(OPC_GETARGN0 + var_num); |
| 8624 | } |
| 8625 | else if (!is_param && var_num < 6) |
| 8626 | { |
| 8627 | /* locals 0-5 have dedicated opcodes */ |
| 8628 | G_cg->write_op(OPC_GETLCLN0 + var_num); |
| 8629 | } |
| 8630 | else if (var_num <= 255) |
| 8631 | { |
| 8632 | /* 8-bit local number - use the one-byte form */ |
| 8633 | G_cg->write_op(is_param ? OPC_GETARG1 : OPC_GETLCL1); |
| 8634 | G_cs->write((char)var_num); |
| 8635 | } |
| 8636 | else |
| 8637 | { |
| 8638 | /* local number won't fit in 8 bits - use the two-byte form */ |
| 8639 | G_cg->write_op(is_param ? OPC_GETARG2 : OPC_GETLCL2); |
| 8640 | G_cs->write2(var_num); |
| 8641 | } |
| 8642 | |
| 8643 | /* note the push */ |
| 8644 | G_cg->note_push(); |
| 8645 | } |
| 8646 | |
| 8647 | /* |
| 8648 | * assign a value |
| 8649 | */ |
| 8650 | int CTcSymLocal::gen_code_asi( |
| 8651 | int discard, int phase, tc_asitype_t typ, const char *, |
| 8652 | class CTcPrsNode *rhs, int ignore_errors, int xplicit, void **) |
| 8653 | { |
| 8654 | /* |
| 8655 | * if this is an explicit assignment, mark the variable as having had a |
| 8656 | * value assigned to it |
| 8657 | */ |
| 8658 | if (xplicit) |
| 8659 | set_val_assigned(TRUE); |
| 8660 | |
| 8661 | /* |
| 8662 | * if the assignment is anything but simple, this references the |
| 8663 | * value as well |
| 8664 | */ |
| 8665 | if (typ != TC_ASI_SIMPLE) |
| 8666 | set_val_used(TRUE); |
| 8667 | |
| 8668 | /* |
| 8669 | * If this is a context variable, use standard assignment (i.e., |
| 8670 | * generate the result first, then generate a simple assignment to the |
| 8671 | * variable). Otherwise, we might be able to generate a fancy |
| 8672 | * combined calculate-and-assign sequence, depending on the type of |
| 8673 | * assignment calculation we're performing. |
| 8674 | */ |
| 8675 | if (is_ctx_local_ && typ != TC_ASI_SIMPLE && phase == 1) |
| 8676 | { |
| 8677 | /* |
| 8678 | * It's a context local, and it's not a simple assignment, so we |
| 8679 | * can't perform any special calculate-and-assign sequence. For |
| 8680 | * phase 1, generate the value and tell the caller to calculate the |
| 8681 | * full result first and then try again using simple assignment. |
| 8682 | */ |
| 8683 | gen_code(FALSE); |
| 8684 | return FALSE; |
| 8685 | } |
| 8686 | |
| 8687 | /* |
| 8688 | * If this is phase 2 of a compound assignment, treat it the same as a |
| 8689 | * simple assignment. The RHS is on the stack, and we simply need to |
| 8690 | * complete the assignment to the local. |
| 8691 | */ |
| 8692 | if (phase == 2) |
| 8693 | typ = TC_ASI_SIMPLE; |
| 8694 | |
| 8695 | /* |
| 8696 | * check the type of assignment - we can optimize the code |
| 8697 | * generation to use more compact instruction sequences for certain |
| 8698 | * types of assignments |
| 8699 | */ |
| 8700 | int adding; |
| 8701 | switch(typ) |
| 8702 | { |
| 8703 | case TC_ASI_SIMPLE: |
| 8704 | /* |
| 8705 | * Simple assignment to local/parameter. Check for some special |
| 8706 | * cases: when assigning a constant value of 0, 1, or nil to a |
| 8707 | * local, we can generate a short instruction |
| 8708 | */ |
| 8709 | if (!is_param() && !is_ctx_local_ && rhs != 0 && rhs->is_const()) |
| 8710 | { |
| 8711 | CTcConstVal *cval; |
| 8712 | |
| 8713 | /* get the constant value */ |
| 8714 | cval = rhs->get_const_val(); |
| 8715 | |
| 8716 | /* check for nil and 0 or 1 values */ |
| 8717 | if (cval->get_type() == TC_CVT_NIL) |
| 8718 | { |
| 8719 | /* it's nil - generate NILLCL1 or NILLCL2 */ |
| 8720 | if (get_var_num() <= 255) |
| 8721 | { |
| 8722 | G_cg->write_op(OPC_NILLCL1); |
| 8723 | G_cs->write((char)get_var_num()); |
| 8724 | } |
| 8725 | else |
| 8726 | { |
| 8727 | G_cg->write_op(OPC_NILLCL2); |
| 8728 | G_cs->write2(get_var_num()); |
| 8729 | } |
| 8730 | |
| 8731 | /* if not discarding, leave nil on the stack */ |
| 8732 | if (!discard) |
| 8733 | { |
| 8734 | G_cg->write_op(OPC_PUSHNIL); |
| 8735 | G_cg->note_push(); |
| 8736 | } |
| 8737 | |
| 8738 | /* handled */ |
| 8739 | return TRUE; |
| 8740 | } |
| 8741 | else if (cval->get_type() == TC_CVT_INT |
| 8742 | && (cval->get_val_int() == 0 |
| 8743 | || cval->get_val_int() == 1)) |
| 8744 | { |
| 8745 | int ival; |
| 8746 | |
| 8747 | /* get the integer value */ |
| 8748 | ival = cval->get_val_int(); |
| 8749 | |
| 8750 | /* 0 or 1 - generate ZEROLCLn or ONELCLn */ |
| 8751 | if (get_var_num() <= 255) |
| 8752 | { |
| 8753 | G_cg->write_op(ival == 0 ? OPC_ZEROLCL1 : OPC_ONELCL1); |
| 8754 | G_cs->write((char)get_var_num()); |
| 8755 | } |
| 8756 | else |
| 8757 | { |
| 8758 | G_cg->write_op(ival == 0 ? OPC_ZEROLCL2 : OPC_ONELCL2); |
| 8759 | G_cs->write2(get_var_num()); |
| 8760 | } |
| 8761 | |
| 8762 | /* if not discarding, leave the value on the stack */ |
| 8763 | if (!discard) |
| 8764 | { |
| 8765 | G_cg->write_op(ival == 0 ? OPC_PUSH_0 : OPC_PUSH_1); |
| 8766 | G_cg->note_push(); |
| 8767 | } |
| 8768 | |
| 8769 | /* handled */ |
| 8770 | return TRUE; |
| 8771 | } |
| 8772 | } |
| 8773 | |
| 8774 | /* |
| 8775 | * If we got here, we can't generate a specialized constant |
| 8776 | * assignment - so, first, generate the right-hand side's value |
| 8777 | * normally. (If no 'rhs' is specified, the value is already on |
| 8778 | * the stack.) |
| 8779 | */ |
| 8780 | if (rhs != 0) |
| 8781 | rhs->gen_code(FALSE, FALSE); |
| 8782 | |
| 8783 | /* leave an extra copy of the value on the stack if not discarding */ |
| 8784 | if (!discard) |
| 8785 | { |
| 8786 | G_cg->write_op(OPC_DUP); |
| 8787 | G_cg->note_push(); |
| 8788 | } |
| 8789 | |
| 8790 | /* now assign the value at top of stack to the variable */ |
| 8791 | gen_code_setlcl(); |
| 8792 | |
| 8793 | /* handled */ |
| 8794 | return TRUE; |
| 8795 | |
| 8796 | case TC_ASI_ADD: |
| 8797 | adding = TRUE; |
| 8798 | goto add_or_sub; |
| 8799 | |
| 8800 | case TC_ASI_SUB: |
| 8801 | adding = FALSE; |
| 8802 | |
| 8803 | add_or_sub: |
| 8804 | /* if this is a parameter, there's nothing special we can do */ |
| 8805 | if (is_param()) |
| 8806 | { |
| 8807 | if (phase == 1) |
| 8808 | gen_code(FALSE); |
| 8809 | return FALSE; |
| 8810 | } |
| 8811 | |
| 8812 | /* |
| 8813 | * Add/subtract to a local/parameter. If the right-hand side is a |
| 8814 | * constant integer value, we might be able to generate a special |
| 8815 | * instruction to add/subtract it. |
| 8816 | */ |
| 8817 | if (rhs != 0 |
| 8818 | && adding |
| 8819 | && rhs->is_const() |
| 8820 | && rhs->get_const_val()->get_type() == TC_CVT_INT) |
| 8821 | { |
| 8822 | long ival; |
| 8823 | |
| 8824 | /* get the integer value to assign */ |
| 8825 | ival = rhs->get_const_val()->get_val_int(); |
| 8826 | |
| 8827 | /* |
| 8828 | * if the right-hand side's integer value fits in one byte, |
| 8829 | * generate the short (8-bit) instruction; otherwise, |
| 8830 | * generate the long (32-bit) format |
| 8831 | */ |
| 8832 | if (ival == 1) |
| 8833 | { |
| 8834 | /* adding one - increment the local */ |
| 8835 | G_cg->write_op(OPC_INCLCL); |
| 8836 | G_cs->write2(get_var_num()); |
| 8837 | } |
| 8838 | else if (ival == -1) |
| 8839 | { |
| 8840 | /* subtracting one - decrement the local */ |
| 8841 | G_cg->write_op(OPC_DECLCL); |
| 8842 | G_cs->write2(get_var_num()); |
| 8843 | } |
| 8844 | else if (ival <= 127 && ival >= -128 |
| 8845 | && get_var_num() <= 255) |
| 8846 | { |
| 8847 | /* fits in 8 bits - use the 8-bit format */ |
| 8848 | G_cg->write_op(OPC_ADDILCL1); |
| 8849 | G_cs->write((char)get_var_num()); |
| 8850 | G_cs->write((char)ival); |
| 8851 | } |
| 8852 | else |
| 8853 | { |
| 8854 | /* |
| 8855 | * either the value or the variable number doesn't fit |
| 8856 | * in 8 bits - use the 32-bit format |
| 8857 | */ |
| 8858 | G_cg->write_op(OPC_ADDILCL4); |
| 8859 | G_cs->write2(get_var_num()); |
| 8860 | G_cs->write4(ival); |
| 8861 | } |
| 8862 | } |
| 8863 | else |
| 8864 | { |
| 8865 | /* |
| 8866 | * We don't have a special instruction for the right side, |
| 8867 | * so generate it normally and add/subtract the value. (If |
| 8868 | * there's no 'rhs' value specified, it means that the value |
| 8869 | * is already on the stack, so there's nothing extra for us |
| 8870 | * to generate.) |
| 8871 | */ |
| 8872 | if (rhs != 0) |
| 8873 | rhs->gen_code(FALSE, FALSE); |
| 8874 | |
| 8875 | /* write the ADDTOLCL instruction */ |
| 8876 | G_cg->write_op(adding ? OPC_ADDTOLCL : OPC_SUBFROMLCL); |
| 8877 | G_cs->write2(get_var_num()); |
| 8878 | |
| 8879 | /* addtolcl/subfromlcl remove the rvalue */ |
| 8880 | G_cg->note_pop(); |
| 8881 | } |
| 8882 | |
| 8883 | /* |
| 8884 | * if not discarding, push the result onto the stack; do this by |
| 8885 | * simply evaluating the local, which is the simplest and most |
| 8886 | * efficient way to obtain the result of the computation |
| 8887 | */ |
| 8888 | if (!discard) |
| 8889 | gen_code(FALSE); |
| 8890 | |
| 8891 | /* handled */ |
| 8892 | return TRUE; |
| 8893 | |
| 8894 | case TC_ASI_PREINC: |
| 8895 | /* if this is a parameter, there's nothing special we can do */ |
| 8896 | if (is_param()) |
| 8897 | { |
| 8898 | if (phase == 1) |
| 8899 | gen_code(FALSE); |
| 8900 | return FALSE; |
| 8901 | } |
| 8902 | |
| 8903 | /* generate code to increment the local */ |
| 8904 | G_cg->write_op(OPC_INCLCL); |
| 8905 | G_cs->write2(get_var_num()); |
| 8906 | |
| 8907 | /* if we're not discarding, push the local's new value */ |
| 8908 | if (!discard) |
| 8909 | gen_code(FALSE); |
| 8910 | |
| 8911 | /* handled */ |
| 8912 | return TRUE; |
| 8913 | |
| 8914 | case TC_ASI_POSTINC: |
| 8915 | /* if this is a parameter, there's nothing special we can do */ |
| 8916 | if (is_param()) |
| 8917 | { |
| 8918 | if (phase == 1) |
| 8919 | gen_code(FALSE); |
| 8920 | return FALSE; |
| 8921 | } |
| 8922 | |
| 8923 | /* |
| 8924 | * if we're not discarding, push the local's value prior to |
| 8925 | * incrementing it - this will be the result we'll leave on the |
| 8926 | * stack |
| 8927 | */ |
| 8928 | if (!discard) |
| 8929 | gen_code(FALSE); |
| 8930 | |
| 8931 | /* generate code to increment the local */ |
| 8932 | G_cg->write_op(OPC_INCLCL); |
| 8933 | G_cs->write2(get_var_num()); |
| 8934 | |
| 8935 | /* handled */ |
| 8936 | return TRUE; |
| 8937 | |
| 8938 | case TC_ASI_PREDEC: |
| 8939 | /* if this is a parameter, there's nothing special we can do */ |
| 8940 | if (is_param()) |
| 8941 | { |
| 8942 | if (phase == 1) |
| 8943 | gen_code(FALSE); |
| 8944 | return FALSE; |
| 8945 | } |
| 8946 | |
| 8947 | /* generate code to decrement the local */ |
| 8948 | G_cg->write_op(OPC_DECLCL); |
| 8949 | G_cs->write2(get_var_num()); |
| 8950 | |
| 8951 | /* if we're not discarding, push the local's new value */ |
| 8952 | if (!discard) |
| 8953 | gen_code(FALSE); |
| 8954 | |
| 8955 | /* handled */ |
| 8956 | return TRUE; |
| 8957 | |
| 8958 | case TC_ASI_POSTDEC: |
| 8959 | /* if this is a parameter, there's nothing special we can do */ |
| 8960 | if (is_param()) |
| 8961 | { |
| 8962 | if (phase == 1) |
| 8963 | gen_code(FALSE); |
| 8964 | return FALSE; |
| 8965 | } |
| 8966 | |
| 8967 | /* |
| 8968 | * if we're not discarding, push the local's value prior to |
| 8969 | * decrementing it - this will be the result we'll leave on the |
| 8970 | * stack |
| 8971 | */ |
| 8972 | if (!discard) |
| 8973 | gen_code(FALSE); |
| 8974 | |
| 8975 | /* generate code to decrement the local */ |
| 8976 | G_cg->write_op(OPC_DECLCL); |
| 8977 | G_cs->write2(get_var_num()); |
| 8978 | |
| 8979 | /* handled */ |
| 8980 | return TRUE; |
| 8981 | |
| 8982 | default: |
| 8983 | /* |
| 8984 | * For other special assignment types, simply generate the value of |
| 8985 | * the local, and tell the caller to proceed with the unrolled |
| 8986 | * assignment. |
| 8987 | */ |
| 8988 | gen_code(FALSE); |
| 8989 | return FALSE; |
| 8990 | } |
| 8991 | } |
| 8992 | |
| 8993 | /* |
| 8994 | * generate code to assigin the value at top of stack to the local |
| 8995 | * variable |
| 8996 | */ |
| 8997 | void CTcSymLocal::gen_code_setlcl() |
| 8998 | { |
| 8999 | /* check to see if we're a context local (as opposed to a stack local) */ |
| 9000 | if (is_ctx_local_) |
| 9001 | { |
| 9002 | /* generate the assignment using the appropriate sequence */ |
| 9003 | if (ctx_var_num_ <= 255 && get_ctx_arr_idx() <= 255) |
| 9004 | { |
| 9005 | /* we can fit this in a single instruction */ |
| 9006 | G_cg->write_op(OPC_SETINDLCL1I8); |
| 9007 | G_cs->write((uchar)ctx_var_num_); |
| 9008 | G_cs->write((uchar)get_ctx_arr_idx()); |
| 9009 | |
| 9010 | /* this pops the value being assigned */ |
| 9011 | G_cg->note_pop(); |
| 9012 | } |
| 9013 | else |
| 9014 | { |
| 9015 | /* get our context array */ |
| 9016 | s_gen_code_getlcl(ctx_var_num_, FALSE); |
| 9017 | |
| 9018 | /* set our value in the context array */ |
| 9019 | CTPNConst::s_gen_code_int(get_ctx_arr_idx()); |
| 9020 | G_cg->write_op(OPC_SETIND); |
| 9021 | G_cg->write_op(OPC_DISC); |
| 9022 | |
| 9023 | /* |
| 9024 | * the SETIND pops three values and pushes one (for a net two |
| 9025 | * pops), and the DISC pops one more value, so our total is |
| 9026 | * three pops |
| 9027 | */ |
| 9028 | G_cg->note_pop(3); |
| 9029 | } |
| 9030 | } |
| 9031 | else |
| 9032 | { |
| 9033 | /* we're just a plain stack variable */ |
| 9034 | gen_code_setlcl_stk(); |
| 9035 | } |
| 9036 | } |
| 9037 | |
| 9038 | /* |
| 9039 | * Generate code to store the value at the top of the stack into the given |
| 9040 | * local stack slot. Note that this routine will not work with a context |
| 9041 | * local - it only works if the variable is known to be a stack variable. |
| 9042 | */ |
| 9043 | void CTcSymLocal::s_gen_code_setlcl_stk(int var_num, int is_param) |
| 9044 | { |
| 9045 | /* use the shortest form that will fit our variable index */ |
| 9046 | if (var_num <= 255) |
| 9047 | { |
| 9048 | /* use the one-byte instruction */ |
| 9049 | G_cg->write_op(is_param ? OPC_SETARG1 : OPC_SETLCL1); |
| 9050 | G_cs->write((char)var_num); |
| 9051 | } |
| 9052 | else |
| 9053 | { |
| 9054 | /* big number - use the two-byte instruction */ |
| 9055 | G_cg->write_op(is_param ? OPC_SETARG2 : OPC_SETLCL2); |
| 9056 | G_cs->write2(var_num); |
| 9057 | } |
| 9058 | |
| 9059 | /* the setarg/setlcl ops remove the rvalue */ |
| 9060 | G_cg->note_pop(); |
| 9061 | } |
| 9062 | |
| 9063 | /* |
| 9064 | * call the symbol |
| 9065 | */ |
| 9066 | void CTcSymLocal::gen_code_call( |
| 9067 | int discard, int argc, int varargs, CTcNamedArgs *named_args) |
| 9068 | { |
| 9069 | /* |
| 9070 | * to call a local, we'll simply evaluate the local normally, then |
| 9071 | * call through the resulting (presumed) property or function |
| 9072 | * pointer value |
| 9073 | */ |
| 9074 | gen_code(FALSE); |
| 9075 | |
| 9076 | /* |
| 9077 | * if we have a varargs list, modify the call instruction that |
| 9078 | * follows to make it a varargs call |
| 9079 | */ |
| 9080 | if (varargs) |
| 9081 | { |
| 9082 | /* swap the top of the stack to get the arg counter back on top */ |
| 9083 | G_cg->write_op(OPC_SWAP); |
| 9084 | |
| 9085 | /* write the varargs modifier */ |
| 9086 | G_cg->write_op(OPC_VARARGC); |
| 9087 | } |
| 9088 | |
| 9089 | /* don't allow this at all in speculative mode */ |
| 9090 | if (G_cg->is_speculative()) |
| 9091 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 9092 | |
| 9093 | /* call the result as a function or method pointer */ |
| 9094 | G_cg->write_op(OPC_PTRCALL); |
| 9095 | G_cs->write((char)argc); |
| 9096 | |
| 9097 | /* ptrcall removes the arguments and the function pointer */ |
| 9098 | G_cg->note_pop(argc + 1); |
| 9099 | |
| 9100 | /* do post-call cleanup: named arg removal, etc */ |
| 9101 | G_cg->post_call_cleanup(named_args); |
| 9102 | |
| 9103 | /* if we're not discarding the value, push the result */ |
| 9104 | if (!discard) |
| 9105 | { |
| 9106 | G_cg->write_op(OPC_GETR0); |
| 9107 | G_cg->note_push(); |
| 9108 | } |
| 9109 | } |
| 9110 | |
| 9111 | /* |
| 9112 | * generate a property ID expression |
| 9113 | */ |
| 9114 | vm_prop_id_t CTcSymLocal::gen_code_propid(int check_only, int /*is_expr*/) |
| 9115 | { |
| 9116 | /* |
| 9117 | * treat the local as a property-valued expression; generate the |
| 9118 | * code for the local, then tell the caller that no constant value |
| 9119 | * is available, since the local's property ID value should be on |
| 9120 | * the stack |
| 9121 | */ |
| 9122 | if (!check_only) |
| 9123 | gen_code(FALSE); |
| 9124 | |
| 9125 | /* tell the caller to use the stack value */ |
| 9126 | return VM_INVALID_PROP; |
| 9127 | } |
| 9128 | |
| 9129 | /* |
| 9130 | * evaluate a member expression |
| 9131 | */ |
| 9132 | void CTcSymLocal::gen_code_member( |
| 9133 | int discard, CTcPrsNode *prop_expr, int prop_is_expr, |
| 9134 | int argc, int varargs, CTcNamedArgs *named_args) |
| 9135 | { |
| 9136 | /* generate code to evaluate the local */ |
| 9137 | gen_code(FALSE); |
| 9138 | |
| 9139 | /* if we have an argument counter, put it back on top */ |
| 9140 | if (varargs) |
| 9141 | G_cg->write_op(OPC_SWAP); |
| 9142 | |
| 9143 | /* use the standard member generation */ |
| 9144 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 9145 | argc, varargs, named_args); |
| 9146 | } |
| 9147 | |
| 9148 | /* |
| 9149 | * write to a debug record |
| 9150 | */ |
| 9151 | int CTcSymLocal::write_to_debug_frame(int test_only) |
| 9152 | { |
| 9153 | /* if this is test-only mode, just note that we will write it */ |
| 9154 | if (test_only) |
| 9155 | return TRUE; |
| 9156 | |
| 9157 | /* |
| 9158 | * For version 2+ static compilation, write the symbol name |
| 9159 | * out-of-line, to the separate stream for local variable names. This |
| 9160 | * allows us to consolidate the names so that each name is stored only |
| 9161 | * once in the file. This saves a lot of space since there are a few |
| 9162 | * common local names that appear over and over. Version 1 always |
| 9163 | * wrote names in-line, and we also have to write the names in-line |
| 9164 | * when generating dynamic code, since we can't add to the constant |
| 9165 | * pool in this case. |
| 9166 | * |
| 9167 | * Also, store all 1- and 2-character names in-line. 1-character names |
| 9168 | * are smaller if stored in-line since they'll take only three bytes |
| 9169 | * (two bytes for the length, one byte for the string) vs four bytes |
| 9170 | * for a shared pool pointer. 2-char names are a wash on the frame |
| 9171 | * storage, but it's still more efficient to store them in-line because |
| 9172 | * we avoid also creating the constant pool entry for them. |
| 9173 | */ |
| 9174 | int inl = (len_ <= 2 |
| 9175 | || G_sizes.dbg_fmt_vsn < 2 |
| 9176 | || G_cg->is_eval_for_dyn()); |
| 9177 | |
| 9178 | /* |
| 9179 | * write my ID - if we're a context variable, we want to write the |
| 9180 | * context variable ID; otherwise write our stack location as normal |
| 9181 | */ |
| 9182 | if (is_ctx_local_) |
| 9183 | G_cs->write2(ctx_var_num_); |
| 9184 | else |
| 9185 | G_cs->write2(var_num_); |
| 9186 | |
| 9187 | /* compute my flags */ |
| 9188 | int flags = 0; |
| 9189 | if (is_param_) |
| 9190 | flags |= 0x0001; |
| 9191 | if (is_ctx_local_) |
| 9192 | flags |= 0x0002; |
| 9193 | if (!inl) |
| 9194 | flags |= 0x0004; |
| 9195 | |
| 9196 | /* write my flags */ |
| 9197 | G_cs->write2(flags); |
| 9198 | |
| 9199 | /* write my local context array index */ |
| 9200 | G_cs->write2(get_ctx_arr_idx()); |
| 9201 | |
| 9202 | /* add zeros to pad any future version information for the target VM */ |
| 9203 | for (int i = G_sizes.lcl_hdr - 6 ; i > 0 ; --i) |
| 9204 | G_cs->write(0); |
| 9205 | |
| 9206 | /* |
| 9207 | * write the name - either to the separate stream for local names, or |
| 9208 | * inline if necessary |
| 9209 | */ |
| 9210 | if (inl) |
| 9211 | { |
| 9212 | /* write the symbol name to the table in-line */ |
| 9213 | G_cs->write2(len_); |
| 9214 | G_cs->write(str_, len_); |
| 9215 | } |
| 9216 | else |
| 9217 | { |
| 9218 | /* add a fixup from the table to the local stream */ |
| 9219 | CTcStreamAnchor *anchor = G_lcl_stream->add_anchor(0, 0); |
| 9220 | CTcAbsFixup::add_abs_fixup(anchor->fixup_list_head_, |
| 9221 | G_cs, G_cs->get_ofs()); |
| 9222 | |
| 9223 | /* add the placeholder to the table (this is what we fix up) */ |
| 9224 | G_cs->write4(0); |
| 9225 | |
| 9226 | /* write the name to the local stream */ |
| 9227 | G_lcl_stream->write2(len_); |
| 9228 | G_lcl_stream->write(str_, len_); |
| 9229 | } |
| 9230 | |
| 9231 | /* we did write this symbol */ |
| 9232 | return TRUE; |
| 9233 | } |
| 9234 | |
| 9235 | /* ------------------------------------------------------------------------ */ |
| 9236 | /* |
| 9237 | * Dynamic code local. This is a symbol for a local variable in an active |
| 9238 | * stack frame during dynamic compilation. |
| 9239 | */ |
| 9240 | |
| 9241 | /* |
| 9242 | * generate code to evaluate the variable's value |
| 9243 | */ |
| 9244 | void CTcSymDynLocal::gen_code(int discard) |
| 9245 | { |
| 9246 | /* there are no side effects, so do nothing if discarding the value */ |
| 9247 | if (discard) |
| 9248 | return; |
| 9249 | |
| 9250 | /* |
| 9251 | * to retrieve a dynamic frame local, we simply need to evaluate |
| 9252 | * fref[index], where 'fref' is the StackFrameRef object and 'index' is |
| 9253 | * an integer giving the frame index of the variable |
| 9254 | */ |
| 9255 | |
| 9256 | /* push the frame object */ |
| 9257 | G_cg->write_op(OPC_PUSHOBJ); |
| 9258 | G_cs->write_obj_id(fref_); |
| 9259 | G_cg->note_push(); |
| 9260 | |
| 9261 | /* push the variable's frame index */ |
| 9262 | CTPNConst::s_gen_code_int(varnum_); |
| 9263 | |
| 9264 | /* index the frame object */ |
| 9265 | G_cg->write_op(OPC_INDEX); |
| 9266 | |
| 9267 | /* INDEX pops two items and pushes one for a net of one pop */ |
| 9268 | G_cg->note_pop(1); |
| 9269 | |
| 9270 | /* |
| 9271 | * if this is a context local, we now have the context object, so we |
| 9272 | * need to further index it by the variable's context index |
| 9273 | */ |
| 9274 | if (ctxidx_ != 0) |
| 9275 | { |
| 9276 | /* index the context object by the context index */ |
| 9277 | CTPNConst::s_gen_code_int(ctxidx_); |
| 9278 | G_cg->write_op(OPC_INDEX); |
| 9279 | |
| 9280 | /* INDEX pops two items and pushes one */ |
| 9281 | G_cg->note_pop(1); |
| 9282 | } |
| 9283 | } |
| 9284 | |
| 9285 | /* |
| 9286 | * generate code to assign to the variable |
| 9287 | */ |
| 9288 | int CTcSymDynLocal::gen_code_asi(int discard, int phase, |
| 9289 | tc_asitype_t typ, const char *, |
| 9290 | class CTcPrsNode *rhs, |
| 9291 | int, int, void **) |
| 9292 | { |
| 9293 | /* if this is a complex assignment, use the default two-phase process */ |
| 9294 | if (typ != TC_ASI_SIMPLE && phase == 1) |
| 9295 | { |
| 9296 | /* |
| 9297 | * generate our value, and tell the caller to calculate the |
| 9298 | * compound expression result and assign the value with a separate |
| 9299 | * call |
| 9300 | */ |
| 9301 | gen_code(FALSE); |
| 9302 | return FALSE; |
| 9303 | } |
| 9304 | |
| 9305 | /* if the rhs isn't already on the stack, generate it */ |
| 9306 | if (rhs != 0) |
| 9307 | rhs->gen_code(FALSE, FALSE); |
| 9308 | |
| 9309 | /* |
| 9310 | * if we're not disarding the rhs value, make an extra copy, as we'll |
| 9311 | * consume our copy in the performing the assignment |
| 9312 | */ |
| 9313 | if (!discard) |
| 9314 | { |
| 9315 | G_cg->write_op(OPC_DUP); |
| 9316 | G_cg->note_push(); |
| 9317 | } |
| 9318 | |
| 9319 | /* |
| 9320 | * to assign to a local variable, perform a set-index operation through |
| 9321 | * the frame object - that is, evaluate "fref[index] = value" |
| 9322 | */ |
| 9323 | |
| 9324 | /* push the frame object */ |
| 9325 | G_cg->write_op(OPC_PUSHOBJ); |
| 9326 | G_cs->write_obj_id(fref_); |
| 9327 | G_cg->note_push(); |
| 9328 | |
| 9329 | /* push the variable's frame index */ |
| 9330 | CTPNConst::s_gen_code_int(varnum_); |
| 9331 | |
| 9332 | /* |
| 9333 | * if this is a context local, frame[index] yields the context object, |
| 9334 | * so we need to retrieve that before proceeding |
| 9335 | */ |
| 9336 | if (ctxidx_ != 0) |
| 9337 | { |
| 9338 | /* get frame[varnum] - that gives us the context object */ |
| 9339 | G_cg->write_op(OPC_INDEX); |
| 9340 | |
| 9341 | /* INDEX pops two items and pushes one for a net 1 pop */ |
| 9342 | G_cg->note_pop(); |
| 9343 | |
| 9344 | /* |
| 9345 | * now push the context index - the actual assignment will be |
| 9346 | * ctxobj[ctxidx] = rhs |
| 9347 | */ |
| 9348 | CTPNConst::s_gen_code_int(ctxidx_); |
| 9349 | } |
| 9350 | |
| 9351 | /* perform the assignment */ |
| 9352 | G_cg->write_op(OPC_SETIND); |
| 9353 | |
| 9354 | /* |
| 9355 | * Discard the new container value. This is the result of doing an |
| 9356 | * index-assign through the frame object, which is simply the original |
| 9357 | * frame object; even if it were changing, it's not something we'd need |
| 9358 | * to store anywhere since this is an internal indexing operation. |
| 9359 | */ |
| 9360 | G_cg->write_op(OPC_DISC); |
| 9361 | |
| 9362 | /* |
| 9363 | * the SETIND pops the rhs, index, and indexee, and pushes the |
| 9364 | * container, which we then discard, for a net pop of 3 |
| 9365 | */ |
| 9366 | G_cg->note_pop(3); |
| 9367 | |
| 9368 | /* handled */ |
| 9369 | return TRUE; |
| 9370 | } |
| 9371 | |
| 9372 | |
| 9373 | /* ------------------------------------------------------------------------ */ |
| 9374 | /* |
| 9375 | * Built-in function symbol |
| 9376 | */ |
| 9377 | |
| 9378 | /* |
| 9379 | * Evaluate the symbol. Invoking a built-in function without an |
| 9380 | * argument list is simply a call to the built-in function with no |
| 9381 | * arguments. |
| 9382 | */ |
| 9383 | void CTcSymBif::gen_code(int discard) |
| 9384 | { |
| 9385 | /* generate a call */ |
| 9386 | gen_code_call(discard, 0, FALSE, 0); |
| 9387 | } |
| 9388 | |
| 9389 | /* |
| 9390 | * Generate code to call the built-in function |
| 9391 | */ |
| 9392 | void CTcSymBif::gen_code_call(int discard, int argc, int varargs, |
| 9393 | CTcNamedArgs *named_args) |
| 9394 | { |
| 9395 | /* don't allow calling built-in functions in speculative mode */ |
| 9396 | if (G_cg->is_speculative()) |
| 9397 | err_throw(VMERR_BAD_SPEC_EVAL); |
| 9398 | |
| 9399 | /* check for minimum and maximum arguments */ |
| 9400 | if (argc < min_argc_) |
| 9401 | { |
| 9402 | G_tok->log_error(TCERR_TOO_FEW_FUNC_ARGS, |
| 9403 | (int)get_sym_len(), get_sym()); |
| 9404 | } |
| 9405 | else if (!varargs_ && argc > max_argc_) |
| 9406 | { |
| 9407 | G_tok->log_error(TCERR_TOO_MANY_FUNC_ARGS, |
| 9408 | (int)get_sym_len(), get_sym()); |
| 9409 | } |
| 9410 | |
| 9411 | /* write the varargs modifier if appropriate */ |
| 9412 | if (varargs) |
| 9413 | G_cg->write_op(OPC_VARARGC); |
| 9414 | |
| 9415 | /* generate the call */ |
| 9416 | if (get_func_set_id() < 4 && get_func_idx() < 256) |
| 9417 | { |
| 9418 | uchar short_ops[] = |
| 9419 | { OPC_BUILTIN_A, OPC_BUILTIN_B, OPC_BUILTIN_C, OPC_BUILTIN_D }; |
| 9420 | |
| 9421 | /* |
| 9422 | * it's one of the first 256 functions in one of the first four |
| 9423 | * function sets - we can generate a short instruction |
| 9424 | */ |
| 9425 | G_cg->write_op(short_ops[get_func_set_id()]); |
| 9426 | G_cs->write((char)argc); |
| 9427 | G_cs->write((char)get_func_idx()); |
| 9428 | } |
| 9429 | else |
| 9430 | { |
| 9431 | /* it's not in the default set - use the longer instruction */ |
| 9432 | if (get_func_idx() < 256) |
| 9433 | { |
| 9434 | /* low function index - write the short form */ |
| 9435 | G_cg->write_op(OPC_BUILTIN1); |
| 9436 | G_cs->write((char)argc); |
| 9437 | G_cs->write((char)get_func_idx()); |
| 9438 | } |
| 9439 | else |
| 9440 | { |
| 9441 | /* big function index - write the long form */ |
| 9442 | G_cg->write_op(OPC_BUILTIN2); |
| 9443 | G_cs->write((char)argc); |
| 9444 | G_cs->write2(get_func_idx()); |
| 9445 | } |
| 9446 | |
| 9447 | /* write the function set ID */ |
| 9448 | G_cs->write((char)get_func_set_id()); |
| 9449 | } |
| 9450 | |
| 9451 | /* the built-in functions always remove arguments */ |
| 9452 | G_cg->note_pop(argc); |
| 9453 | |
| 9454 | /* do post-call cleanup: named arg removal, etc */ |
| 9455 | G_cg->post_call_cleanup(named_args); |
| 9456 | |
| 9457 | /* |
| 9458 | * if they're not discarding the value, push it - the value is |
| 9459 | * sitting in R0 after the call returns |
| 9460 | */ |
| 9461 | if (!discard) |
| 9462 | { |
| 9463 | G_cg->write_op(OPC_GETR0); |
| 9464 | G_cg->note_push(); |
| 9465 | } |
| 9466 | } |
| 9467 | |
| 9468 | /* |
| 9469 | * take the address of the built-in function |
| 9470 | */ |
| 9471 | void CTcSymBif::gen_code_addr() |
| 9472 | { |
| 9473 | /* generate PUSHBIFPTR <function set index> <function index> */ |
| 9474 | G_cg->write_op(OPC_PUSHBIFPTR); |
| 9475 | G_cs->write2(func_idx_); |
| 9476 | G_cs->write2(func_set_id_); |
| 9477 | |
| 9478 | /* this pushes one element */ |
| 9479 | G_cg->note_push(); |
| 9480 | } |
| 9481 | |
| 9482 | /* |
| 9483 | * add a runtime symbol table entry |
| 9484 | */ |
| 9485 | void CTcSymBif::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 9486 | { |
| 9487 | vm_val_t val; |
| 9488 | |
| 9489 | /* add an entry for our absolute address */ |
| 9490 | val.set_bifptr(func_set_id_, func_idx_); |
| 9491 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 9492 | } |
| 9493 | |
| 9494 | /* ------------------------------------------------------------------------ */ |
| 9495 | /* |
| 9496 | * External function symbol |
| 9497 | */ |
| 9498 | |
| 9499 | /* |
| 9500 | * evaluate the symbol |
| 9501 | */ |
| 9502 | void CTcSymExtfn::gen_code(int /*discard*/) |
| 9503 | { |
| 9504 | //$$$ to be implemented |
| 9505 | assert(FALSE); |
| 9506 | } |
| 9507 | |
| 9508 | /* |
| 9509 | * generate a call to the symbol |
| 9510 | */ |
| 9511 | void CTcSymExtfn::gen_code_call(int /*discard*/, int /*argc*/, int /*varargs*/, |
| 9512 | CTcNamedArgs * /*named_args*/) |
| 9513 | { |
| 9514 | //$$$ to be implemented |
| 9515 | assert(FALSE); |
| 9516 | } |
| 9517 | |
| 9518 | |
| 9519 | /* ------------------------------------------------------------------------ */ |
| 9520 | /* |
| 9521 | * Code Label symbol |
| 9522 | */ |
| 9523 | |
| 9524 | /* |
| 9525 | * evaluate the symbol |
| 9526 | */ |
| 9527 | void CTcSymLabel::gen_code(int discard) |
| 9528 | { |
| 9529 | /* it's not legal to evaluate a code label; log an error */ |
| 9530 | G_tok->log_error(TCERR_CANNOT_EVAL_LABEL, (int)get_sym_len(), get_sym()); |
| 9531 | } |
| 9532 | |
| 9533 | /* ------------------------------------------------------------------------ */ |
| 9534 | /* |
| 9535 | * Metaclass symbol |
| 9536 | */ |
| 9537 | |
| 9538 | /* |
| 9539 | * generate code for evaluating the symbol |
| 9540 | */ |
| 9541 | void CTcSymMetaclass::gen_code(int discard) |
| 9542 | { |
| 9543 | /* |
| 9544 | * mark it as referenced - if the metaclass wasn't defined in this file |
| 9545 | * but was merely imported from a symbol file, we now need to write it |
| 9546 | * to the object file anyway, since its class object ID has been |
| 9547 | * referenced |
| 9548 | */ |
| 9549 | ref_ = TRUE; |
| 9550 | |
| 9551 | /* |
| 9552 | * the metaclass name refers to the IntrinsicClass instance |
| 9553 | * associated with the metaclass |
| 9554 | */ |
| 9555 | G_cg->write_op(OPC_PUSHOBJ); |
| 9556 | G_cs->write_obj_id(class_obj_); |
| 9557 | |
| 9558 | /* note the push */ |
| 9559 | G_cg->note_push(); |
| 9560 | } |
| 9561 | |
| 9562 | /* |
| 9563 | * generate code for operator 'new' applied to the metaclass |
| 9564 | */ |
| 9565 | void CTcSymMetaclass::gen_code_new(int discard, int argc, |
| 9566 | int varargs, CTcNamedArgs *named_args, |
| 9567 | int is_transient) |
| 9568 | { |
| 9569 | /* if this is an external metaclass, we can't generate code */ |
| 9570 | if (ext_) |
| 9571 | { |
| 9572 | G_tok->log_error(TCERR_EXT_METACLASS, (int)get_sym_len(), get_sym()); |
| 9573 | return; |
| 9574 | } |
| 9575 | |
| 9576 | /* if we have varargs, write the modifier */ |
| 9577 | if (varargs) |
| 9578 | G_cg->write_op(OPC_VARARGC); |
| 9579 | |
| 9580 | if (meta_idx_ <= 255 && argc <= 255) |
| 9581 | { |
| 9582 | G_cg->write_op(is_transient ? OPC_TRNEW1 : OPC_NEW1); |
| 9583 | G_cs->write((char)argc); |
| 9584 | G_cs->write((char)meta_idx_); |
| 9585 | } |
| 9586 | else |
| 9587 | { |
| 9588 | G_cg->write_op(is_transient ? OPC_TRNEW1 : OPC_NEW2); |
| 9589 | G_cs->write2(argc); |
| 9590 | G_cs->write2(meta_idx_); |
| 9591 | } |
| 9592 | |
| 9593 | /* new1/new2 remove arguments */ |
| 9594 | G_cg->note_pop(argc); |
| 9595 | |
| 9596 | /* do post-call cleanup: named arg removal, etc */ |
| 9597 | G_cg->post_call_cleanup(named_args); |
| 9598 | |
| 9599 | /* if we're not discarding the value, push it */ |
| 9600 | if (!discard) |
| 9601 | { |
| 9602 | G_cg->write_op(OPC_GETR0); |
| 9603 | G_cg->note_push(); |
| 9604 | } |
| 9605 | } |
| 9606 | |
| 9607 | /* |
| 9608 | * generate a member expression |
| 9609 | */ |
| 9610 | void CTcSymMetaclass::gen_code_member(int discard, CTcPrsNode *prop_expr, |
| 9611 | int prop_is_expr, |
| 9612 | int argc, int varargs, |
| 9613 | CTcNamedArgs *named_args) |
| 9614 | { |
| 9615 | /* if this is an external metaclass, we can't generate code */ |
| 9616 | if (ext_) |
| 9617 | { |
| 9618 | G_tok->log_error(TCERR_EXT_METACLASS, (int)get_sym_len(), get_sym()); |
| 9619 | return; |
| 9620 | } |
| 9621 | |
| 9622 | /* generate code to push our class object onto the stack */ |
| 9623 | gen_code(FALSE); |
| 9624 | |
| 9625 | /* if we have an argument counter, put it back on top */ |
| 9626 | if (varargs) |
| 9627 | G_cg->write_op(OPC_SWAP); |
| 9628 | |
| 9629 | /* use the standard member generation */ |
| 9630 | CTcPrsNode::s_gen_member_rhs(discard, prop_expr, prop_is_expr, |
| 9631 | argc, varargs, named_args); |
| 9632 | } |
| 9633 | |
| 9634 | /* |
| 9635 | * add a runtime symbol table entry |
| 9636 | */ |
| 9637 | void CTcSymMetaclass::add_runtime_symbol(CVmRuntimeSymbols *symtab) |
| 9638 | { |
| 9639 | /* don't do this for external metaclasses */ |
| 9640 | if (ext_) |
| 9641 | return; |
| 9642 | |
| 9643 | /* add our entry */ |
| 9644 | vm_val_t val; |
| 9645 | val.set_obj(get_class_obj()); |
| 9646 | symtab->add_sym(get_sym(), get_sym_len(), &val); |
| 9647 | } |
| 9648 | |
| 9649 | |
| 9650 | /* ------------------------------------------------------------------------ */ |
| 9651 | /* |
| 9652 | * Exception Table |
| 9653 | */ |
| 9654 | |
| 9655 | /* |
| 9656 | * create |
| 9657 | */ |
| 9658 | CTcT3ExcTable::CTcT3ExcTable() |
| 9659 | { |
| 9660 | /* allocate an initial table */ |
| 9661 | exc_alloced_ = 1024; |
| 9662 | table_ = (CTcT3ExcEntry *)t3malloc(exc_alloced_ * sizeof(table_[0])); |
| 9663 | |
| 9664 | /* no entries are in use yet */ |
| 9665 | exc_used_ = 0; |
| 9666 | |
| 9667 | /* method offset is not yet known */ |
| 9668 | method_ofs_ = 0; |
| 9669 | } |
| 9670 | |
| 9671 | |
| 9672 | /* |
| 9673 | * add an entry to our table |
| 9674 | */ |
| 9675 | void CTcT3ExcTable::add_catch(ulong protected_start_ofs, |
| 9676 | ulong protected_end_ofs, |
| 9677 | ulong exc_obj_id, ulong catch_block_ofs) |
| 9678 | { |
| 9679 | CTcT3ExcEntry *entry; |
| 9680 | |
| 9681 | /* if necessary, expand our table */ |
| 9682 | if (exc_used_ == exc_alloced_) |
| 9683 | { |
| 9684 | /* expand the table a bit */ |
| 9685 | exc_alloced_ += 1024; |
| 9686 | |
| 9687 | /* reallocate the table at the larger size */ |
| 9688 | table_ = (CTcT3ExcEntry *) |
| 9689 | t3realloc(table_, exc_alloced_ * sizeof(table_[0])); |
| 9690 | } |
| 9691 | |
| 9692 | /* |
| 9693 | * set up the new entry - store the offsets relative to the method |
| 9694 | * header start address |
| 9695 | */ |
| 9696 | entry = table_ + exc_used_; |
| 9697 | entry->start_ofs = protected_start_ofs - method_ofs_; |
| 9698 | entry->end_ofs = protected_end_ofs - method_ofs_; |
| 9699 | entry->exc_obj_id = exc_obj_id; |
| 9700 | entry->catch_ofs = catch_block_ofs - method_ofs_; |
| 9701 | |
| 9702 | /* consume the new entry */ |
| 9703 | ++exc_used_; |
| 9704 | } |
| 9705 | |
| 9706 | /* |
| 9707 | * write our exception table to the code stream |
| 9708 | */ |
| 9709 | void CTcT3ExcTable::write_to_code_stream() |
| 9710 | { |
| 9711 | CTcT3ExcEntry *entry; |
| 9712 | size_t i; |
| 9713 | |
| 9714 | /* write the number of entries as a UINT2 */ |
| 9715 | G_cs->write2(exc_used_); |
| 9716 | |
| 9717 | /* write the entries */ |
| 9718 | for (i = 0, entry = table_ ; i < exc_used_ ; ++i, ++entry) |
| 9719 | { |
| 9720 | /* write this entry */ |
| 9721 | G_cs->write2(entry->start_ofs); |
| 9722 | G_cs->write2(entry->end_ofs); |
| 9723 | G_cs->write_obj_id(entry->exc_obj_id); |
| 9724 | G_cs->write2(entry->catch_ofs); |
| 9725 | |
| 9726 | /* pad any excess beyond the v1 size with zero bytes */ |
| 9727 | for (int j = G_sizes.exc_entry - 10 ; j > 0 ; --j) |
| 9728 | G_cs->write(0); |
| 9729 | } |
| 9730 | } |
| 9731 | |
| 9732 | |
| 9733 | /* ------------------------------------------------------------------------ */ |
| 9734 | /* |
| 9735 | * Parameter default value enumeration context. |
| 9736 | */ |
| 9737 | struct defval_ctx |
| 9738 | { |
| 9739 | defval_ctx() |
| 9740 | { |
| 9741 | nv = 0; |
| 9742 | nvalo = 128; |
| 9743 | v = new CTcSymLocal *[nvalo]; |
| 9744 | } |
| 9745 | |
| 9746 | ~defval_ctx() |
| 9747 | { |
| 9748 | delete [] v; |
| 9749 | } |
| 9750 | |
| 9751 | void add(CTcSymLocal *l) |
| 9752 | { |
| 9753 | /* if there's not room, expand the array */ |
| 9754 | if (nv == nvalo) |
| 9755 | { |
| 9756 | CTcSymLocal **vnew = new CTcSymLocal*[nvalo + 128]; |
| 9757 | memcpy(vnew, v, nvalo * sizeof(*vnew)); |
| 9758 | delete [] v; |
| 9759 | v = vnew; |
| 9760 | nvalo += 128; |
| 9761 | } |
| 9762 | |
| 9763 | /* add it */ |
| 9764 | v[nv++] = l; |
| 9765 | } |
| 9766 | |
| 9767 | /* sort the list in ascending order of sequence number */ |
| 9768 | void sort() { qsort(v, nv, sizeof(*v), &compare); } |
| 9769 | static int compare(const void *a0, const void *b0) |
| 9770 | { |
| 9771 | const CTcSymLocal *a = *(const CTcSymLocal **)a0; |
| 9772 | const CTcSymLocal *b = *(const CTcSymLocal **)b0; |
| 9773 | |
| 9774 | return a->get_defval_seqno() - b->get_defval_seqno(); |
| 9775 | } |
| 9776 | |
| 9777 | /* generate code to initialize an optional positional parameter */ |
| 9778 | static void gen_opt_positional(CTcSymLocal *lcl) |
| 9779 | { |
| 9780 | /* |
| 9781 | * The code we want to generate is this, in pseudo code (where 'n' |
| 9782 | * is our position among the arguments): |
| 9783 | * |
| 9784 | *. if (n <= argcount) |
| 9785 | *. lcl = getArg(n) |
| 9786 | *. else |
| 9787 | *. lcl = default_expression; |
| 9788 | * |
| 9789 | * The 'else' applies only if there's a default expression with the |
| 9790 | * local; otherwise we simply leave it with the nil initial value. |
| 9791 | */ |
| 9792 | |
| 9793 | /* if n > argcount... */ |
| 9794 | CTPNConst::s_gen_code_int(lcl->get_param_index() + 1); |
| 9795 | G_cg->write_op(OPC_GETARGC); |
| 9796 | G_cg->note_push(); |
| 9797 | |
| 9798 | /* ...jump to the 'not found' branch... */ |
| 9799 | CTcCodeLabel *nf_lbl = G_cs->new_label_fwd(); |
| 9800 | G_cg->write_op(OPC_JGT); |
| 9801 | G_cs->write_ofs2(nf_lbl, 0); |
| 9802 | G_cg->note_pop(2); |
| 9803 | |
| 9804 | /* getArg(n) */ |
| 9805 | CTPNConst::s_gen_code_int(lcl->get_param_index() + 1); |
| 9806 | call_internal_bif("getArg", TCERR_OPT_PARAM_MISSING_FUNC, 1); |
| 9807 | |
| 9808 | /* if there's a default value, generate it */ |
| 9809 | if (lcl->get_defval_expr() != 0) |
| 9810 | { |
| 9811 | /* we're done with the 'then' branch - jump to the assignment */ |
| 9812 | CTcCodeLabel *asi_lbl = G_cs->new_label_fwd(); |
| 9813 | G_cg->write_op(OPC_JMP); |
| 9814 | G_cs->write_ofs2(asi_lbl, 0); |
| 9815 | |
| 9816 | /* define the 'not found' label - come here to set the default */ |
| 9817 | CTcPrsNode::def_label_pos(nf_lbl); |
| 9818 | |
| 9819 | /* generate the default expression */ |
| 9820 | lcl->get_defval_expr()->gen_code(FALSE, FALSE); |
| 9821 | |
| 9822 | /* we're ready to do the assignment */ |
| 9823 | CTcPrsNode::def_label_pos(asi_lbl); |
| 9824 | |
| 9825 | /* |
| 9826 | * since we're sharing the assignment (coming up next) with the |
| 9827 | * main branch, we need to account for the extra push on this |
| 9828 | * branch |
| 9829 | */ |
| 9830 | G_cg->note_pop(); |
| 9831 | } |
| 9832 | |
| 9833 | /* |
| 9834 | * Assign the value we decided upon to the local. Don't count this |
| 9835 | * as an explicit assignment of a value for warning purposes; this |
| 9836 | * is effectively the same kind of assignment as the kind that |
| 9837 | * binds an actual argument to this formal parameter, so it |
| 9838 | * shouldn't count as any more explicit an assignment than that |
| 9839 | * does. |
| 9840 | */ |
| 9841 | lcl->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", |
| 9842 | 0, FALSE, FALSE, 0); |
| 9843 | |
| 9844 | /* |
| 9845 | * if there was no default value, the 'not found' branch jumps |
| 9846 | * here, past the assignment, since we have no value to assign |
| 9847 | */ |
| 9848 | if (lcl->get_defval_expr() == 0) |
| 9849 | CTcPrsNode::def_label_pos(nf_lbl); |
| 9850 | } |
| 9851 | |
| 9852 | /* array of local variables with default values */ |
| 9853 | CTcSymLocal **v; |
| 9854 | |
| 9855 | /* allocated size of 'v' array */ |
| 9856 | int nvalo; |
| 9857 | |
| 9858 | /* number of entries in 'v' array */ |
| 9859 | int nv; |
| 9860 | }; |
| 9861 | |
| 9862 | /* ------------------------------------------------------------------------ */ |
| 9863 | /* |
| 9864 | * Code body |
| 9865 | */ |
| 9866 | |
| 9867 | /* |
| 9868 | * generate code |
| 9869 | */ |
| 9870 | void CTPNCodeBody::gen_code(int, int) |
| 9871 | { |
| 9872 | /* if I've been replaced, don't bother generating any code */ |
| 9873 | if (replaced_) |
| 9874 | return; |
| 9875 | |
| 9876 | /* |
| 9877 | * Open the method header. |
| 9878 | * |
| 9879 | * Generate to the static stream if this is a static initializer |
| 9880 | * method, otherwise to the main stream. |
| 9881 | * |
| 9882 | * Anchor the fixups in the associated symbol table entry, if any. We |
| 9883 | * maintain our own fixup list if we don't have a symbol, otherwise we |
| 9884 | * use the one from our symbol table entry - in either case, we have to |
| 9885 | * keep track of it ourselves, because a code body might be reachable |
| 9886 | * through multiple references (a function, for example, has a global |
| 9887 | * symbol table entry - fixups referencing us might already have been |
| 9888 | * created by the time we generate our code). |
| 9889 | */ |
| 9890 | tct3_method_gen_ctx gen_ctx; |
| 9891 | G_cg->open_method( |
| 9892 | is_static_ ? G_cs_static : G_cs_main, |
| 9893 | fixup_owner_sym_, fixup_list_anchor_, |
| 9894 | this, gototab_, argc_, opt_argc_, varargs_, |
| 9895 | is_constructor_, op_overload_, self_valid_, &gen_ctx); |
| 9896 | |
| 9897 | /* |
| 9898 | * Add a line record at the start of the method for all of the |
| 9899 | * generated method prolog code. Some prolog code can throw errors, so |
| 9900 | * we want a line record at the start of the code body to indicate the |
| 9901 | * location of any such errors. |
| 9902 | */ |
| 9903 | if (start_desc_ != 0) |
| 9904 | G_cs->add_line_rec(start_desc_, start_linenum_); |
| 9905 | |
| 9906 | /* |
| 9907 | * Add each local symbol table enclosing the code body's primary |
| 9908 | * local symbol table to the frame list. The outermost code body |
| 9909 | * table can be outside the primary code body table for situations |
| 9910 | * such as anonymous functions. Since these tables are outside of |
| 9911 | * any statements, we must explicitly add them to ensure that they |
| 9912 | * are assigned debugging frame ID's and are written to the debug |
| 9913 | * data. |
| 9914 | */ |
| 9915 | if (lcltab_ != 0) |
| 9916 | { |
| 9917 | /* add each frame outside the primary frame to the code gen list */ |
| 9918 | for (CTcPrsSymtab *tab = lcltab_->get_parent() ; tab != 0 ; |
| 9919 | tab = tab->get_parent()) |
| 9920 | G_cs->set_local_frame(tab); |
| 9921 | } |
| 9922 | |
| 9923 | /* the method's local symbol table is now the active symbol table */ |
| 9924 | G_cs->set_local_frame(lcltab_); |
| 9925 | |
| 9926 | /* if we have a local context, initialize it */ |
| 9927 | if (has_local_ctx_) |
| 9928 | { |
| 9929 | /* write code to create the new Vector to store the context locals */ |
| 9930 | CTPNConst::s_gen_code_int(local_ctx_arr_size_); |
| 9931 | G_cg->write_op(OPC_DUP); |
| 9932 | G_cg->write_op(OPC_NEW1); |
| 9933 | G_cs->write(2); |
| 9934 | G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_VECTOR)); |
| 9935 | |
| 9936 | /* retrieve the object value */ |
| 9937 | G_cg->write_op(OPC_GETR0); |
| 9938 | |
| 9939 | /* |
| 9940 | * we duplicated the vector size argument, then we popped it and |
| 9941 | * pushed the object; so we have a maximum of one extra push and a |
| 9942 | * net of zero |
| 9943 | */ |
| 9944 | G_cg->note_push(); |
| 9945 | G_cg->note_pop(); |
| 9946 | |
| 9947 | /* store the new object in the context local variable */ |
| 9948 | CTcSymLocal::s_gen_code_setlcl_stk(local_ctx_var_, FALSE); |
| 9949 | |
| 9950 | /* |
| 9951 | * go through our symbol table, and copy each parameter that's |
| 9952 | * also a context local into its context local slot |
| 9953 | */ |
| 9954 | if (lcltab_ != 0) |
| 9955 | lcltab_->enum_entries(&enum_for_param_ctx, this); |
| 9956 | } |
| 9957 | |
| 9958 | /* |
| 9959 | * If we have a varargs-list parameter, generate the code to set up |
| 9960 | * the list value from the actual parameters. Note that we must do |
| 9961 | * this after we set up the local context, in case the varargs list |
| 9962 | * parameter variable is a context local, in which case it will need |
| 9963 | * to be stored in the context, in which case we need the context to |
| 9964 | * be initialized first. |
| 9965 | */ |
| 9966 | if (varargs_list_) |
| 9967 | { |
| 9968 | /* generate the PUSHPARLST instruction to create the list */ |
| 9969 | G_cg->write_op(OPC_PUSHPARLST); |
| 9970 | G_cs->write((uchar)argc_); |
| 9971 | |
| 9972 | /* |
| 9973 | * we pushed at least one value (the list); we don't know how many |
| 9974 | * others we might have pushed, but it doesn't matter because the |
| 9975 | * interpreter is responsible for checking for stack space |
| 9976 | */ |
| 9977 | G_cg->note_push(); |
| 9978 | |
| 9979 | /* store the list in our varargs parameter list local */ |
| 9980 | varargs_list_local_->gen_code_setlcl(); |
| 9981 | } |
| 9982 | |
| 9983 | /* |
| 9984 | * generate other special parameter setup code: named arguments and |
| 9985 | * default values |
| 9986 | */ |
| 9987 | if (lcltab_ != 0) |
| 9988 | { |
| 9989 | /* |
| 9990 | * generate bindings for named parameters and optional positional |
| 9991 | * parameters that don't have default expressions, and build the |
| 9992 | * array of optional parameters with default expressions |
| 9993 | */ |
| 9994 | defval_ctx dctx; |
| 9995 | lcltab_->enum_entries(&enum_for_named_params, &dctx); |
| 9996 | |
| 9997 | /* sort the default value list in left-to-right parameter order */ |
| 9998 | dctx.sort(); |
| 9999 | |
| 10000 | /* generate the default value assignments */ |
| 10001 | for (int i = 0 ; i < dctx.nv ; ++i) |
| 10002 | { |
| 10003 | /* get this local */ |
| 10004 | CTcSymLocal *lcl = dctx.v[i]; |
| 10005 | |
| 10006 | /* generate the proper code depending on the parameter type */ |
| 10007 | if (lcl->is_named_param()) |
| 10008 | { |
| 10009 | /* |
| 10010 | * Named parameter with a default. |
| 10011 | * |
| 10012 | * If the default is a constant, call t3GetNamedArg(name, |
| 10013 | * defVal). This returns the argument value, or the |
| 10014 | * default value if the argument isn't defined. |
| 10015 | * |
| 10016 | * If the default value expression isn't a constant, we |
| 10017 | * can't evaluate it until we know whether or not the |
| 10018 | * argument is defined, since the expression could have |
| 10019 | * side effects that we don't want to trigger unless we |
| 10020 | * really need to evaluate the expression. So in this |
| 10021 | * case, we have to look up the argument first, and set the |
| 10022 | * default only the argument doesn't exist. |
| 10023 | * |
| 10024 | * Note that we could handle both cases with the |
| 10025 | * non-constant handling, since it would make no difference |
| 10026 | * semantically to defer evaluating a constant until after |
| 10027 | * checking to see if the argument exists. However, the |
| 10028 | * separate version for constants yields faster and smaller |
| 10029 | * byte code, so we use it as an optimization for a common |
| 10030 | * case. |
| 10031 | */ |
| 10032 | if (lcl->get_defval_expr()->is_const()) |
| 10033 | { |
| 10034 | /* constant default - use t3GetNamedArg(name, defval) */ |
| 10035 | lcl->get_defval_expr()->gen_code(FALSE, FALSE); |
| 10036 | CTPNConst::s_gen_code_str_by_mode(lcl); |
| 10037 | call_internal_bif("t3GetNamedArg", |
| 10038 | TCERR_NAMED_PARAM_MISSING_FUNC, 2); |
| 10039 | } |
| 10040 | else |
| 10041 | { |
| 10042 | /* |
| 10043 | * Non-constant default value. We don't want to |
| 10044 | * evaluate the argument unless necessary in this case, |
| 10045 | * so start by calling t3GetNamedArg(name). This will |
| 10046 | * throw an error if the argument doesn't exist, so do |
| 10047 | * this in an implied try/catch block. If we return |
| 10048 | * without an error, simply proceed with the value |
| 10049 | * returned. If an error is thrown, catch the error |
| 10050 | * and *then* evaluate the default value. |
| 10051 | */ |
| 10052 | ulong start_ofs = G_cs->get_ofs(); |
| 10053 | CTPNConst::s_gen_code_str_by_mode(lcl); |
| 10054 | call_internal_bif("t3GetNamedArg", |
| 10055 | TCERR_NAMED_PARAM_MISSING_FUNC, 1); |
| 10056 | |
| 10057 | /* that's the end of the implied 'try' section */ |
| 10058 | ulong end_ofs = G_cs->get_ofs() - 1; |
| 10059 | |
| 10060 | /* |
| 10061 | * On a successful return, the result value will be at |
| 10062 | * top of stack, so just jump past the evaluation of |
| 10063 | * the default value. Since we're done with this |
| 10064 | * branch, clear what it left on the stack, since the |
| 10065 | * 'catch' branch will leave the same thing on the |
| 10066 | * stack in parallel. |
| 10067 | */ |
| 10068 | CTcCodeLabel *asi_lbl = gen_jump_ahead(OPC_JMP); |
| 10069 | G_cg->note_pop(); |
| 10070 | |
| 10071 | /* this is the start of our implied 'catch' block */ |
| 10072 | G_cg->get_exc_table()->add_catch( |
| 10073 | start_ofs, end_ofs, VM_INVALID_OBJ, G_cs->get_ofs()); |
| 10074 | G_cg->note_push(); |
| 10075 | |
| 10076 | /* discard the exception */ |
| 10077 | G_cg->write_op(OPC_DISC); |
| 10078 | G_cg->note_pop(); |
| 10079 | |
| 10080 | /* generate the default value expression */ |
| 10081 | lcl->get_defval_expr()->gen_code(FALSE, FALSE); |
| 10082 | |
| 10083 | /* this is the common 'assign' branch point */ |
| 10084 | def_label_pos(asi_lbl); |
| 10085 | } |
| 10086 | |
| 10087 | /* assign the top-of-stack value to the local */ |
| 10088 | lcl->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", 0, |
| 10089 | FALSE, FALSE, 0); |
| 10090 | } |
| 10091 | else |
| 10092 | { |
| 10093 | /* it's a positional parameter - generate it */ |
| 10094 | dctx.gen_opt_positional(lcl); |
| 10095 | } |
| 10096 | } |
| 10097 | } |
| 10098 | |
| 10099 | /* |
| 10100 | * If this is a dynamic function (a DynamicFunc object), and it's |
| 10101 | * defined with the 'function' keyword (not 'method'), and it has a |
| 10102 | * 'self', we need to set up the method context to point to the |
| 10103 | * enclosing frame object's method context. |
| 10104 | */ |
| 10105 | if (G_cg->is_eval_for_dyn() |
| 10106 | && is_dyn_func_ |
| 10107 | && self_valid_ |
| 10108 | && (self_referenced_ || full_method_ctx_referenced_)) |
| 10109 | { |
| 10110 | /* |
| 10111 | * We need the method context. For dyanmic code, the invokee is |
| 10112 | * the DynamicFunc, and the DynamicFunc makes the context available |
| 10113 | * via its indexed value [1]. |
| 10114 | */ |
| 10115 | G_cg->write_op(OPC_PUSHCTXELE); |
| 10116 | G_cs->write(PUSHCTXELE_INVOKEE); |
| 10117 | CTPNConst::s_gen_code_int(1); |
| 10118 | G_cg->write_op(OPC_INDEX); |
| 10119 | |
| 10120 | /* we pushed the invokee and popped it again with the INDEX */ |
| 10121 | G_cg->note_push(); |
| 10122 | G_cg->note_pop(); |
| 10123 | |
| 10124 | /* |
| 10125 | * The invokee will store the appropriate object in its [1] |
| 10126 | * element, according to which type of method context we need. |
| 10127 | * Generate the appropriate 'set' for it. |
| 10128 | */ |
| 10129 | if (full_method_ctx_referenced_) |
| 10130 | { |
| 10131 | /* load the full method context */ |
| 10132 | G_cg->write_op(OPC_LOADCTX); |
| 10133 | } |
| 10134 | else |
| 10135 | { |
| 10136 | /* load the 'self' object */ |
| 10137 | G_cg->write_op(OPC_SETSELF); |
| 10138 | } |
| 10139 | |
| 10140 | /* that pops the context object */ |
| 10141 | G_cg->note_pop(); |
| 10142 | } |
| 10143 | |
| 10144 | /* |
| 10145 | * Generate code to initialize each enclosing-context-pointer local - |
| 10146 | * these variables allow us to find the context objects while we're |
| 10147 | * running inside this function. |
| 10148 | * |
| 10149 | * Before 3.1, we had to generate context level 1 last, because this |
| 10150 | * level sets 'self' for an anonymous function to the saved 'self' from |
| 10151 | * the context. Starting with 3.1, the stack frame has a separate |
| 10152 | * entry for the invokee, so we no longer conflate the anonymous |
| 10153 | * function object with 'self'. |
| 10154 | */ |
| 10155 | CTcCodeBodyCtx *cur_ctx; |
| 10156 | int ctx_idx; |
| 10157 | for (ctx_idx = 0, cur_ctx = ctx_head_ ; cur_ctx != 0 ; |
| 10158 | cur_ctx = cur_ctx->nxt_, ++ctx_idx) |
| 10159 | { |
| 10160 | /* |
| 10161 | * Get this context value, stored in invokee[n+2]. Note that the |
| 10162 | * context object indices start at 2 because the FUNCPTR value for |
| 10163 | * the code itself is at index 1. |
| 10164 | */ |
| 10165 | G_cg->write_op(OPC_PUSHCTXELE); |
| 10166 | G_cs->write(PUSHCTXELE_INVOKEE); |
| 10167 | CTPNConst::s_gen_code_int(ctx_idx + 2); |
| 10168 | G_cg->write_op(OPC_INDEX); |
| 10169 | |
| 10170 | /* |
| 10171 | * we pushed the object, then popped the object and index and |
| 10172 | * pushed the indexed value - this is a net of no change with one |
| 10173 | * maximum push |
| 10174 | */ |
| 10175 | G_cg->note_push(); |
| 10176 | G_cg->note_pop(); |
| 10177 | |
| 10178 | /* |
| 10179 | * If this is context level 1, and this context has a 'self', and |
| 10180 | * we need either 'self' or the full method context from the |
| 10181 | * lexically enclosing scope, generate code to load the self or the |
| 10182 | * full method context (as appropriate) from our local context. |
| 10183 | * |
| 10184 | * The enclosing method context is always stored in the context at |
| 10185 | * level 1, because this is inherently shared context for all |
| 10186 | * enclosed lexical scopes. We thus only have to worry about this |
| 10187 | * for context level 1. |
| 10188 | */ |
| 10189 | if (cur_ctx->level_ == 1 |
| 10190 | && !is_anon_method_ |
| 10191 | && self_valid_ |
| 10192 | && (self_referenced_ || full_method_ctx_referenced_)) |
| 10193 | { |
| 10194 | CTPNCodeBody *outer; |
| 10195 | |
| 10196 | /* |
| 10197 | * we just put our context object on the stack in preparation |
| 10198 | * for storing it - make a duplicate copy of it for our own |
| 10199 | * purposes |
| 10200 | */ |
| 10201 | G_cg->write_op(OPC_DUP); |
| 10202 | G_cg->note_push(); |
| 10203 | |
| 10204 | /* get the saved method context from the context object */ |
| 10205 | CTPNConst::s_gen_code_int(TCPRS_LOCAL_CTX_METHODCTX); |
| 10206 | G_cg->write_op(OPC_INDEX); |
| 10207 | |
| 10208 | /* |
| 10209 | * Load the context. We must check the outermost context to |
| 10210 | * determine what it stored, because we must load whatever it |
| 10211 | * stored. |
| 10212 | */ |
| 10213 | if ((outer = get_outermost_enclosing()) != 0 |
| 10214 | && outer->local_ctx_needs_full_method_ctx()) |
| 10215 | { |
| 10216 | /* load the full method context */ |
| 10217 | G_cg->write_op(OPC_LOADCTX); |
| 10218 | } |
| 10219 | else |
| 10220 | { |
| 10221 | /* load the 'self' object */ |
| 10222 | G_cg->write_op(OPC_SETSELF); |
| 10223 | } |
| 10224 | |
| 10225 | /* |
| 10226 | * we popped two values and pushed one in the INDEX, then |
| 10227 | * popped a value in the LOADCTX or SETSELF: the net is removal |
| 10228 | * of two elements and no additional maximum depth |
| 10229 | */ |
| 10230 | G_cg->note_pop(2); |
| 10231 | } |
| 10232 | |
| 10233 | /* store the context value in the appropriate local variable */ |
| 10234 | CTcSymLocal::s_gen_code_setlcl_stk(cur_ctx->var_num_, FALSE); |
| 10235 | } |
| 10236 | |
| 10237 | /* |
| 10238 | * if we created our own local context, and we have a 'self' object, |
| 10239 | * and we need access to the 'self' object or the full method context |
| 10240 | * from anonymous functions that refer to the local context, generate |
| 10241 | * code to store the appropriate data in the local context |
| 10242 | */ |
| 10243 | if (has_local_ctx_ && self_valid_ |
| 10244 | && (local_ctx_needs_self_ || local_ctx_needs_full_method_ctx_)) |
| 10245 | { |
| 10246 | /* |
| 10247 | * Check to see what we need. If we're marked as needing the full |
| 10248 | * method context, OR our outermost enclosing context has the full |
| 10249 | * context, we need to generate the full context. Intermediate |
| 10250 | * nesting levels of anonymous functions can need less context than |
| 10251 | * inner levels, but the context we actually generate at the outer |
| 10252 | * level depends on the innermost function's needs. So we simply |
| 10253 | * need to follow the lead of the outermost level, since that's |
| 10254 | * what the inner levels will all do. |
| 10255 | */ |
| 10256 | CTPNCodeBody *outer = get_outermost_enclosing(); |
| 10257 | if (local_ctx_needs_full_method_ctx_ |
| 10258 | || (outer != 0 && outer->local_ctx_needs_full_method_ctx())) |
| 10259 | { |
| 10260 | /* |
| 10261 | * we need the full method context - generate code to store it |
| 10262 | * and push a reference to it onto the stack |
| 10263 | */ |
| 10264 | G_cg->write_op(OPC_STORECTX); |
| 10265 | } |
| 10266 | else |
| 10267 | { |
| 10268 | /* we only need 'self' - push it */ |
| 10269 | G_cg->write_op(OPC_PUSHSELF); |
| 10270 | } |
| 10271 | |
| 10272 | /* we just pushed one value */ |
| 10273 | G_cg->note_push(); |
| 10274 | |
| 10275 | /* assign the value to the context variable */ |
| 10276 | if (local_ctx_var_ <= 255 && TCPRS_LOCAL_CTX_METHODCTX <= 255) |
| 10277 | { |
| 10278 | /* we can make the assignment with a single instruction */ |
| 10279 | G_cg->write_op(OPC_SETINDLCL1I8); |
| 10280 | G_cs->write((uchar)local_ctx_var_); |
| 10281 | G_cs->write(TCPRS_LOCAL_CTX_METHODCTX); |
| 10282 | |
| 10283 | /* that pops one value */ |
| 10284 | G_cg->note_pop(); |
| 10285 | } |
| 10286 | else |
| 10287 | { |
| 10288 | /* get the context object */ |
| 10289 | CTcSymLocal::s_gen_code_getlcl(local_ctx_var_, FALSE); |
| 10290 | |
| 10291 | /* store the data in the local context object */ |
| 10292 | CTPNConst::s_gen_code_int(TCPRS_LOCAL_CTX_METHODCTX); |
| 10293 | G_cg->write_op(OPC_SETIND); |
| 10294 | |
| 10295 | /* discard the indexed result */ |
| 10296 | G_cg->write_op(OPC_DISC); |
| 10297 | |
| 10298 | /* |
| 10299 | * the SETIND pops three values and pushes one, then we pop one |
| 10300 | * more with the DISC - this is a net three pops with no extra |
| 10301 | * maximum depth |
| 10302 | */ |
| 10303 | G_cg->note_pop(3); |
| 10304 | } |
| 10305 | } |
| 10306 | |
| 10307 | /* generate the compound statement, if we have one */ |
| 10308 | if (stm_ != 0) |
| 10309 | stm_->gen_code(TRUE, TRUE); |
| 10310 | |
| 10311 | #ifdef T3_DEBUG |
| 10312 | if (G_cg->get_sp_depth() != 0) |
| 10313 | { |
| 10314 | printf("---> stack depth is %d after block codegen (line %ld)!\n", |
| 10315 | G_cg->get_sp_depth(), end_linenum_); |
| 10316 | if (fixup_owner_sym_ != 0) |
| 10317 | printf("---> code block for %.*s\n", |
| 10318 | (int)fixup_owner_sym_->get_sym_len(), |
| 10319 | fixup_owner_sym_->get_sym()); |
| 10320 | } |
| 10321 | #endif |
| 10322 | |
| 10323 | /* close the method */ |
| 10324 | G_cg->close_method(local_cnt_, lcltab_, end_desc_, end_linenum_, |
| 10325 | &gen_ctx, named_arg_tables_); |
| 10326 | |
| 10327 | /* remember the head of the nested symbol table list */ |
| 10328 | first_nested_symtab_ = G_cs->get_first_frame(); |
| 10329 | |
| 10330 | /* |
| 10331 | * Generate debug records. If we're in debug mode, include the full |
| 10332 | * symbolic debugging information, including source line locations. |
| 10333 | * For release builds, include only the local variable symbols and |
| 10334 | * frame information; we need those even in release builds, for |
| 10335 | * reflection purposes. |
| 10336 | * |
| 10337 | * If we're generating this code for a debugger evaluation, omit |
| 10338 | * symbols. We don't need reflection support for debugger expressions, |
| 10339 | * since there doesn't seem to be any practical reason you'd need it |
| 10340 | * there. |
| 10341 | */ |
| 10342 | if (!G_cg->is_eval_for_debug()) |
| 10343 | build_debug_table(gen_ctx.method_ofs, G_debug); |
| 10344 | |
| 10345 | /* check for unreferenced labels and issue warnings */ |
| 10346 | check_unreferenced_labels(); |
| 10347 | |
| 10348 | /* show the disassembly of the code block if desired */ |
| 10349 | if (G_disasm_out != 0) |
| 10350 | show_disassembly(gen_ctx.method_ofs, |
| 10351 | gen_ctx.code_start_ofs, gen_ctx.code_end_ofs); |
| 10352 | |
| 10353 | /* clean up globals for the end of the method */ |
| 10354 | G_cg->close_method_cleanup(&gen_ctx); |
| 10355 | } |
| 10356 | |
| 10357 | /* |
| 10358 | * disassembly stream source implementation |
| 10359 | */ |
| 10360 | class CTcUnasSrcCodeBody: public CTcUnasSrc |
| 10361 | { |
| 10362 | public: |
| 10363 | CTcUnasSrcCodeBody(CTcCodeStream *str, |
| 10364 | unsigned long code_start_ofs, |
| 10365 | unsigned long code_end_ofs) |
| 10366 | { |
| 10367 | /* remember the stream */ |
| 10368 | str_ = str; |
| 10369 | |
| 10370 | /* start at the starting offset */ |
| 10371 | cur_ofs_ = code_start_ofs; |
| 10372 | |
| 10373 | /* remember the ending offset */ |
| 10374 | end_ofs_ = code_end_ofs; |
| 10375 | } |
| 10376 | |
| 10377 | /* read the next byte */ |
| 10378 | int next_byte(char *ch) |
| 10379 | { |
| 10380 | /* if there's anything left, return it */ |
| 10381 | if (cur_ofs_ < end_ofs_) |
| 10382 | { |
| 10383 | /* return the next byte */ |
| 10384 | *ch = str_->get_byte_at(cur_ofs_); |
| 10385 | ++cur_ofs_; |
| 10386 | return 0; |
| 10387 | } |
| 10388 | else |
| 10389 | { |
| 10390 | /* indicate end of file */ |
| 10391 | return 1; |
| 10392 | } |
| 10393 | } |
| 10394 | |
| 10395 | /* get the current offset */ |
| 10396 | ulong get_ofs() const { return cur_ofs_; } |
| 10397 | |
| 10398 | protected: |
| 10399 | /* code stream */ |
| 10400 | CTcCodeStream *str_; |
| 10401 | |
| 10402 | /* current offset */ |
| 10403 | unsigned long cur_ofs_; |
| 10404 | |
| 10405 | /* offset of end of code stream */ |
| 10406 | unsigned long end_ofs_; |
| 10407 | }; |
| 10408 | |
| 10409 | /* |
| 10410 | * Show the disassembly of this code block |
| 10411 | */ |
| 10412 | void CTPNCodeBody::show_disassembly(unsigned long start_ofs, |
| 10413 | unsigned long code_start_ofs, |
| 10414 | unsigned long code_end_ofs) |
| 10415 | { |
| 10416 | int argc; |
| 10417 | int locals; |
| 10418 | int total_stk; |
| 10419 | unsigned exc_rel; |
| 10420 | unsigned dbg_rel; |
| 10421 | |
| 10422 | /* first, dump the header */ |
| 10423 | argc = (unsigned char)G_cs->get_byte_at(start_ofs); |
| 10424 | locals = G_cs->readu2_at(start_ofs + 2); |
| 10425 | total_stk = G_cs->readu2_at(start_ofs + 4); |
| 10426 | exc_rel = G_cs->readu2_at(start_ofs + 6); |
| 10427 | dbg_rel = G_cs->readu2_at(start_ofs + 8); |
| 10428 | G_disasm_out->print("%8lx .code\n", start_ofs); |
| 10429 | G_disasm_out->print(" .argcount %d%s\n", |
| 10430 | (argc & 0x7f), |
| 10431 | (argc & 0x80) != 0 ? "+" : ""); |
| 10432 | G_disasm_out->print(" .locals %d\n", locals); |
| 10433 | G_disasm_out->print(" .maxstack %d\n", total_stk); |
| 10434 | |
| 10435 | /* set up a code stream reader and dump the code stream */ |
| 10436 | CTcUnasSrcCodeBody src(G_cs, code_start_ofs, code_end_ofs); |
| 10437 | CTcT3Unasm::disasm(&src, G_disasm_out); |
| 10438 | |
| 10439 | /* show the exception table, if there is one */ |
| 10440 | if (exc_rel != 0) |
| 10441 | { |
| 10442 | unsigned long exc_ofs; |
| 10443 | unsigned long exc_end_ofs; |
| 10444 | |
| 10445 | /* get the starting address */ |
| 10446 | exc_ofs = start_ofs + exc_rel; |
| 10447 | |
| 10448 | /* |
| 10449 | * get the length - it's the part up to the debug records, or the |
| 10450 | * part up to the current code offset if there are no debug records |
| 10451 | */ |
| 10452 | exc_end_ofs = (dbg_rel != 0 ? start_ofs + dbg_rel : G_cs->get_ofs()); |
| 10453 | |
| 10454 | /* show the table */ |
| 10455 | G_disasm_out->print(".exceptions\n"); |
| 10456 | CTcUnasSrcCodeBody exc_src(G_cs, exc_ofs, exc_end_ofs); |
| 10457 | CTcT3Unasm::show_exc_table(&exc_src, G_disasm_out, start_ofs); |
| 10458 | } |
| 10459 | |
| 10460 | /* add a blank line at the end */ |
| 10461 | G_disasm_out->print("\n"); |
| 10462 | } |
| 10463 | |
| 10464 | /* |
| 10465 | * Check for unreferenced local variables |
| 10466 | */ |
| 10467 | void CTPNCodeBody::check_locals() |
| 10468 | { |
| 10469 | CTcPrsSymtab *tab; |
| 10470 | |
| 10471 | /* check for unreferenced locals in each nested scope */ |
| 10472 | for (tab = first_nested_symtab_ ; tab != 0 ; tab = tab->get_list_next()) |
| 10473 | { |
| 10474 | /* check this table */ |
| 10475 | tab->check_unreferenced_locals(); |
| 10476 | } |
| 10477 | } |
| 10478 | |
| 10479 | /* |
| 10480 | * local symbol table enumerator for checking for parameter symbols that |
| 10481 | * belong in the local context |
| 10482 | */ |
| 10483 | void CTPNCodeBody::enum_for_param_ctx(void *, class CTcSymbol *sym) |
| 10484 | { |
| 10485 | /* if this is a local, check it further */ |
| 10486 | if (sym->get_type() == TC_SYM_LOCAL || sym->get_type() == TC_SYM_PARAM) |
| 10487 | { |
| 10488 | CTcSymLocal *lcl = (CTcSymLocal *)sym; |
| 10489 | |
| 10490 | /* |
| 10491 | * if it's a parameter, and it's also a context variable, its |
| 10492 | * value needs to be moved into the context |
| 10493 | */ |
| 10494 | if (lcl->is_param() && lcl->is_ctx_local()) |
| 10495 | { |
| 10496 | /* get the actual parameter value from the stack */ |
| 10497 | CTcSymLocal::s_gen_code_getlcl(lcl->get_var_num(), TRUE); |
| 10498 | |
| 10499 | /* |
| 10500 | * Store the value in the context variable. This is an implied |
| 10501 | * assignment equivalent to binding a formal to its actual |
| 10502 | * value. |
| 10503 | */ |
| 10504 | lcl->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", |
| 10505 | 0, TRUE, FALSE, 0); |
| 10506 | } |
| 10507 | } |
| 10508 | } |
| 10509 | |
| 10510 | /* |
| 10511 | * local symbol table enumerator for generating named argument bindings |
| 10512 | */ |
| 10513 | void CTPNCodeBody::enum_for_named_params(void *ctx, class CTcSymbol *sym) |
| 10514 | { |
| 10515 | /* |
| 10516 | * If this is a local, and it's a named parameter, generate code to |
| 10517 | * bind it. If it's optional and has a default value expression, skip |
| 10518 | * it on this pass, since we need to generate default expressions in |
| 10519 | * the order in which they appear in the argument list to ensure that |
| 10520 | * dependencies are resolved properly and side effects happen in the |
| 10521 | * proper order. |
| 10522 | */ |
| 10523 | if (sym->get_type() == TC_SYM_LOCAL) |
| 10524 | { |
| 10525 | /* get the symbol, properly cast */ |
| 10526 | CTcSymLocal *lcl = (CTcSymLocal *)sym; |
| 10527 | |
| 10528 | /* check what we have */ |
| 10529 | if (lcl->get_defval_expr() != 0) |
| 10530 | { |
| 10531 | /* |
| 10532 | * It's an optional parameter (positional or named) with a |
| 10533 | * default value. We can't generate the binding yet, since we |
| 10534 | * need to generate these in left-to-right order, and the table |
| 10535 | * enumeration is in hash-table order instead. So just add it |
| 10536 | * to the context list, which we'll sort into the proper |
| 10537 | * left-right sequence later. |
| 10538 | */ |
| 10539 | ((defval_ctx *)ctx)->add(lcl); |
| 10540 | } |
| 10541 | else if (lcl->is_named_param()) |
| 10542 | { |
| 10543 | /* |
| 10544 | * It's a named parameter - generate the binding code. If it's |
| 10545 | * optional, call t3GetNamedArg(name, nil) to use the default |
| 10546 | * nil value if it's not defined. If it's not optional, use |
| 10547 | * t3GetNamedArg(name) to throw an error on failure. |
| 10548 | */ |
| 10549 | int fargc = 1; |
| 10550 | if (lcl->is_opt_param()) |
| 10551 | { |
| 10552 | /* optional parameter - call t3GetNamedArg(argname, nil) */ |
| 10553 | G_cg->write_op(OPC_PUSHNIL); |
| 10554 | G_cg->note_push(); |
| 10555 | ++fargc; |
| 10556 | } |
| 10557 | |
| 10558 | /* push the parameter name and call the appropriate builtin */ |
| 10559 | CTPNConst::s_gen_code_str_by_mode(sym); |
| 10560 | call_internal_bif("t3GetNamedArg", |
| 10561 | TCERR_NAMED_PARAM_MISSING_FUNC, fargc); |
| 10562 | |
| 10563 | /* |
| 10564 | * Assign the result to the local. This is an implied |
| 10565 | * assignment equivalent to binding an actual argument value to |
| 10566 | * this formal. |
| 10567 | */ |
| 10568 | lcl->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", |
| 10569 | 0, FALSE, FALSE, 0); |
| 10570 | } |
| 10571 | else if (lcl->is_opt_param()) |
| 10572 | { |
| 10573 | /* |
| 10574 | * It's an optional parameter without a default value (we know |
| 10575 | * there's no default, since we handled that case above). |
| 10576 | * Generate code to load it if present. |
| 10577 | */ |
| 10578 | defval_ctx::gen_opt_positional(lcl); |
| 10579 | } |
| 10580 | } |
| 10581 | } |
| 10582 | |
| 10583 | /* |
| 10584 | * Add a named argument call table to the list. This is invoked each time |
| 10585 | * we generate a call that uses named parameters. We defer generation of |
| 10586 | * the argument name tables until the end of the method, to avoid having to |
| 10587 | * jump past inline tables while executing opcodes. |
| 10588 | */ |
| 10589 | CTcCodeLabel *CTPNCodeBody::add_named_arg_tab(const CTcNamedArgs *named_args) |
| 10590 | { |
| 10591 | /* allocate the tracking object */ |
| 10592 | CTcNamedArgTab *tab = new (G_prsmem) CTcNamedArgTab(named_args); |
| 10593 | |
| 10594 | /* link it into our list */ |
| 10595 | tab->nxt = named_arg_tables_; |
| 10596 | named_arg_tables_ = tab; |
| 10597 | |
| 10598 | /* return the code label for the table */ |
| 10599 | return tab->lbl; |
| 10600 | } |
| 10601 | |
| 10602 | /* ------------------------------------------------------------------------ */ |
| 10603 | /* |
| 10604 | * Debugger records and local symbol table |
| 10605 | */ |
| 10606 | |
| 10607 | /* local symbol enumeration callback context */ |
| 10608 | struct write_local_to_debug_frame_ctx |
| 10609 | { |
| 10610 | /* are we just counting the entries? */ |
| 10611 | int count_only; |
| 10612 | |
| 10613 | /* number of symbols written so far */ |
| 10614 | int count; |
| 10615 | }; |
| 10616 | |
| 10617 | /* |
| 10618 | * Callback for symbol table enumeration - write a local variable entry to |
| 10619 | * the code stream for a debug frame record. |
| 10620 | */ |
| 10621 | void CTPNCodeBody::write_local_to_debug_frame(void *ctx0, CTcSymbol *sym) |
| 10622 | { |
| 10623 | write_local_to_debug_frame_ctx *ctx; |
| 10624 | |
| 10625 | /* cast our context */ |
| 10626 | ctx = (write_local_to_debug_frame_ctx *)ctx0; |
| 10627 | |
| 10628 | /* write it out */ |
| 10629 | if (sym->write_to_debug_frame(ctx->count_only)) |
| 10630 | { |
| 10631 | /* we wrote the symbol - count it */ |
| 10632 | ++(ctx->count); |
| 10633 | } |
| 10634 | } |
| 10635 | |
| 10636 | /* |
| 10637 | * Build the debug information table for a code body |
| 10638 | */ |
| 10639 | void CTPNCodeBody::build_debug_table(ulong start_ofs, int include_lines) |
| 10640 | { |
| 10641 | size_t i; |
| 10642 | |
| 10643 | /* |
| 10644 | * Count up the frames that contain local variables. Frames that |
| 10645 | * contain no locals can be elided to reduce the size of the file, |
| 10646 | * since they carry no information. |
| 10647 | */ |
| 10648 | CTcPrsSymtab *frame; |
| 10649 | int frame_cnt; |
| 10650 | for (frame_cnt = 0, frame = G_cs->get_first_frame() ; frame != 0 ; |
| 10651 | frame = frame->get_list_next()) |
| 10652 | { |
| 10653 | /* enumerate the entries through our counting callback */ |
| 10654 | write_local_to_debug_frame_ctx cbctx; |
| 10655 | cbctx.count = 0; |
| 10656 | cbctx.count_only = TRUE; |
| 10657 | frame->enum_entries(&write_local_to_debug_frame, &cbctx); |
| 10658 | |
| 10659 | /* check to see if it has any locals */ |
| 10660 | if (cbctx.count != 0) |
| 10661 | { |
| 10662 | /* it has locals - renumber it to the current list position */ |
| 10663 | ++frame_cnt; |
| 10664 | frame->set_list_index(frame_cnt); |
| 10665 | } |
| 10666 | else |
| 10667 | { |
| 10668 | /* no locals - flag it as empty by setting its list index to 0 */ |
| 10669 | frame->set_list_index(0); |
| 10670 | } |
| 10671 | } |
| 10672 | |
| 10673 | /* |
| 10674 | * If we're not writing line records, and we don't have any frames with |
| 10675 | * local variables, skip writing the debug table. The debug table in |
| 10676 | * this case is for local variable reflection information only, so if |
| 10677 | * we don't have any locals, we don't need the table at all. |
| 10678 | */ |
| 10679 | if (!include_lines && frame_cnt == 0) |
| 10680 | return; |
| 10681 | |
| 10682 | /* fix up the debug record offset in the prolog to point here */ |
| 10683 | G_cs->write2_at(start_ofs + 8, G_cs->get_ofs() - start_ofs); |
| 10684 | |
| 10685 | /* |
| 10686 | * Write the debug table header. In the current VM version, this is |
| 10687 | * empty, so we have nothing to write. However, if we're dynamically |
| 10688 | * targeting a newer version, pad out any expected header space with |
| 10689 | * zeros. |
| 10690 | */ |
| 10691 | for (int j = G_sizes.dbg_hdr ; j > 0 ; --j) |
| 10692 | G_cs->write(0); |
| 10693 | |
| 10694 | /* |
| 10695 | * Add this offset to our list of line records. If we're creating an |
| 10696 | * object file, upon re-loading the object file, we'll need to go |
| 10697 | * through all of the line record tables and fix up the file references |
| 10698 | * to the final file numbering system at link time, so we need this |
| 10699 | * memory of where the line record are. We'll also need to fix up the |
| 10700 | * local variable name records to point to the consolidated set of |
| 10701 | * strings in the constant pool. |
| 10702 | */ |
| 10703 | G_cg->add_debug_line_table(G_cs->get_ofs()); |
| 10704 | |
| 10705 | /* if desired, write the source code location ("line") records */ |
| 10706 | if (include_lines) |
| 10707 | { |
| 10708 | /* write the number of line records */ |
| 10709 | G_cs->write2(G_cs->get_line_rec_count()); |
| 10710 | |
| 10711 | /* write the line records themselves */ |
| 10712 | for (i = 0 ; i < G_cs->get_line_rec_count() ; ++i) |
| 10713 | { |
| 10714 | /* get this record */ |
| 10715 | tcgen_line_t *rec = G_cs->get_line_rec(i); |
| 10716 | |
| 10717 | /* write the offset of the statement's first opcode */ |
| 10718 | G_cs->write2(rec->ofs); |
| 10719 | |
| 10720 | /* write the source file ID and line number */ |
| 10721 | G_cs->write2(rec->source_id); |
| 10722 | G_cs->write4(rec->source_line); |
| 10723 | |
| 10724 | /* |
| 10725 | * Get the frame for the line record. If the frame is empty, |
| 10726 | * use the parent frame instead, since we omit empty frames. |
| 10727 | */ |
| 10728 | CTcPrsSymtab *fr = rec->frame; |
| 10729 | while (fr != 0 && fr->get_list_index() == 0) |
| 10730 | fr = fr->get_parent(); |
| 10731 | |
| 10732 | /* write the frame ID */ |
| 10733 | G_cs->write2(fr == 0 ? 0 : fr->get_list_index()); |
| 10734 | |
| 10735 | /* add any padding expected in the target VM version */ |
| 10736 | for (int j = G_sizes.dbg_line - 10 ; j > 0 ; --j) |
| 10737 | G_cs->write(0); |
| 10738 | } |
| 10739 | } |
| 10740 | else |
| 10741 | { |
| 10742 | /* no line records - write zero as the line count */ |
| 10743 | G_cs->write2(0); |
| 10744 | } |
| 10745 | |
| 10746 | /* |
| 10747 | * write a placeholder pointer to the next byte after the end of the |
| 10748 | * frame table |
| 10749 | */ |
| 10750 | ulong post_ptr_ofs = G_cs->get_ofs(); |
| 10751 | G_cs->write2(0); |
| 10752 | |
| 10753 | /* write the frame count */ |
| 10754 | G_cs->write2(frame_cnt); |
| 10755 | |
| 10756 | /* |
| 10757 | * Write a placeholder frame index table. We will come back and fix up |
| 10758 | * this table as we actually write out the frames, but we don't |
| 10759 | * actually know how big the individual frame records will be yet, so |
| 10760 | * we can only write placeholders for them for now. First, note where |
| 10761 | * the frame index table begins. |
| 10762 | */ |
| 10763 | ulong index_ofs = G_cs->get_ofs(); |
| 10764 | |
| 10765 | /* write the placeholder index entries */ |
| 10766 | for (i = 0 ; i < (size_t)frame_cnt ; ++i) |
| 10767 | G_cs->write2(0); |
| 10768 | |
| 10769 | /* write the individual frames */ |
| 10770 | for (frame = G_cs->get_first_frame() ; frame != 0 ; |
| 10771 | frame = frame->get_list_next()) |
| 10772 | { |
| 10773 | /* |
| 10774 | * if this frame has a zero list index, it means that it contains |
| 10775 | * no writable symbols, so we can omit it from the debug table |
| 10776 | */ |
| 10777 | if (frame->get_list_index() == 0) |
| 10778 | continue; |
| 10779 | |
| 10780 | /* |
| 10781 | * go back and fill in the correct offset (from the entry itself) |
| 10782 | * in the index table entry for this frame |
| 10783 | */ |
| 10784 | G_cs->write2_at(index_ofs, G_cs->get_ofs() - index_ofs); |
| 10785 | |
| 10786 | /* move on to the next index entry */ |
| 10787 | index_ofs += 2; |
| 10788 | |
| 10789 | /* |
| 10790 | * Get the effective parent. If the actual parent doesn't have a |
| 10791 | * list index, it's empty, so we're not writing it. Find the |
| 10792 | * nearest parent of the parent that we'll actually write, and use |
| 10793 | * that as our effective parent. |
| 10794 | */ |
| 10795 | CTcPrsSymtab *par; |
| 10796 | for (par = frame->get_parent() ; |
| 10797 | par != 0 && par->get_list_index() == 0 ; |
| 10798 | par = par->get_parent()) ; |
| 10799 | |
| 10800 | /* write the ID of the enclosing frame */ |
| 10801 | G_cs->write2(par != 0 ? par->get_list_index() : 0); |
| 10802 | |
| 10803 | /* |
| 10804 | * write a placeholder for the count of the number of entries in |
| 10805 | * the frame, and remember where the placeholder is so we can come |
| 10806 | * back and fix it up later |
| 10807 | */ |
| 10808 | ulong count_ofs = G_cs->get_ofs(); |
| 10809 | G_cs->write2(0); |
| 10810 | |
| 10811 | /* add the bytecode range covered, if there's room in the format */ |
| 10812 | if (G_sizes.dbg_frame >= 8) |
| 10813 | { |
| 10814 | G_cs->write2(frame->get_start_ofs()); |
| 10815 | G_cs->write2(frame->get_end_ofs()); |
| 10816 | } |
| 10817 | |
| 10818 | /* pad out any extra space */ |
| 10819 | for (int j = G_sizes.dbg_frame - 8 ; j > 0 ; --j) |
| 10820 | G_cs->write(0); |
| 10821 | |
| 10822 | /* initialize the enumeration callback context */ |
| 10823 | write_local_to_debug_frame_ctx cbctx; |
| 10824 | cbctx.count = 0; |
| 10825 | cbctx.count_only = FALSE; |
| 10826 | |
| 10827 | /* write this frame table's entries */ |
| 10828 | frame->enum_entries(&write_local_to_debug_frame, &cbctx); |
| 10829 | |
| 10830 | /* go back and fix up the symbol count */ |
| 10831 | G_cs->write2_at(count_ofs, cbctx.count); |
| 10832 | } |
| 10833 | |
| 10834 | /* |
| 10835 | * go back and fill in the post-pointer offset - this is a pointer to |
| 10836 | * the next byte after the end of the frame table; write the offset |
| 10837 | * from the post-pointer field to the current location |
| 10838 | */ |
| 10839 | G_cs->write2_at(post_ptr_ofs, G_cs->get_ofs() - post_ptr_ofs); |
| 10840 | |
| 10841 | /* |
| 10842 | * write the required UINT4 zero value after the frame table - this |
| 10843 | * is a placeholder for future expansion (if we add more information |
| 10844 | * to the debug table later, this value will be non-zero to indicate |
| 10845 | * the presence of the additional information) |
| 10846 | */ |
| 10847 | G_cs->write4(0); |
| 10848 | } |
| 10849 | |
| 10850 | /* ------------------------------------------------------------------------ */ |
| 10851 | /* |
| 10852 | * 'return' statement |
| 10853 | */ |
| 10854 | |
| 10855 | /* |
| 10856 | * generate code |
| 10857 | */ |
| 10858 | void CTPNStmReturn::gen_code(int, int) |
| 10859 | { |
| 10860 | int val_on_stack; |
| 10861 | int need_gen; |
| 10862 | |
| 10863 | /* add a line record */ |
| 10864 | add_debug_line_rec(); |
| 10865 | |
| 10866 | /* presume we'll generate a value */ |
| 10867 | need_gen = TRUE; |
| 10868 | val_on_stack = FALSE; |
| 10869 | |
| 10870 | /* generate the return value expression, if appropriate */ |
| 10871 | if (expr_ != 0) |
| 10872 | { |
| 10873 | /* |
| 10874 | * it's an error if we're in a constructor, because a |
| 10875 | * constructor implicitly always returns 'self' |
| 10876 | */ |
| 10877 | if (G_cg->is_in_constructor()) |
| 10878 | log_error(TCERR_CONSTRUCT_CANNOT_RET_VAL); |
| 10879 | |
| 10880 | /* check for a constant expression */ |
| 10881 | if (expr_->is_const()) |
| 10882 | { |
| 10883 | switch(expr_->get_const_val()->get_type()) |
| 10884 | { |
| 10885 | case TC_CVT_NIL: |
| 10886 | case TC_CVT_TRUE: |
| 10887 | /* |
| 10888 | * we can use special constant return instructions for |
| 10889 | * these, so there's no need to generate the value |
| 10890 | */ |
| 10891 | need_gen = FALSE; |
| 10892 | break; |
| 10893 | |
| 10894 | default: |
| 10895 | /* |
| 10896 | * other types don't have constant-return opcodes, so we |
| 10897 | * must generate the expression code |
| 10898 | */ |
| 10899 | need_gen = TRUE; |
| 10900 | break; |
| 10901 | } |
| 10902 | } |
| 10903 | |
| 10904 | /* if necessary, generate the value */ |
| 10905 | if (need_gen) |
| 10906 | { |
| 10907 | int depth; |
| 10908 | |
| 10909 | /* note the initial stack depth */ |
| 10910 | depth = G_cg->get_sp_depth(); |
| 10911 | |
| 10912 | /* |
| 10913 | * Generate the value. We are obviously not discarding the |
| 10914 | * value, and since returning a value is equivalent to |
| 10915 | * assigning the value, we must use the stricter assignment |
| 10916 | * (not 'for condition') rules for logical expressions |
| 10917 | */ |
| 10918 | expr_->gen_code(FALSE, FALSE); |
| 10919 | |
| 10920 | /* note whether we actually left a value on the stack */ |
| 10921 | val_on_stack = (G_cg->get_sp_depth() > depth); |
| 10922 | } |
| 10923 | else |
| 10924 | { |
| 10925 | /* |
| 10926 | * we obviously aren't leaving a value on the stack if we |
| 10927 | * don't generate anything |
| 10928 | */ |
| 10929 | val_on_stack = FALSE; |
| 10930 | } |
| 10931 | } |
| 10932 | |
| 10933 | /* |
| 10934 | * Before we return, let any enclosing statements generate any code |
| 10935 | * necessary to leave their scope (in particular, we must invoke |
| 10936 | * 'finally' handlers in any enclosing 'try' blocks). |
| 10937 | * |
| 10938 | * Note that we generated the expression BEFORE we call any |
| 10939 | * 'finally' handlers. This is necessary because something we call |
| 10940 | * in the course of evaluating the return value could have thrown an |
| 10941 | * exception; if we were to call the 'finally' clauses before |
| 10942 | * generating the return value, we could invoke the 'finally' clause |
| 10943 | * twice (once explicitly, once in the handling of the thrown |
| 10944 | * exception), which would be incorrect. By generating the |
| 10945 | * 'finally' calls after the return expression, we're sure that the |
| 10946 | * 'finally' blocks are invoked only once - either through the |
| 10947 | * throw, or else now, after there's no more possibility of a |
| 10948 | * 'throw' before the return. |
| 10949 | */ |
| 10950 | if (G_cs->get_enclosing() != 0) |
| 10951 | { |
| 10952 | int did_save_retval; |
| 10953 | uint fin_ret_lcl; |
| 10954 | |
| 10955 | /* |
| 10956 | * if we're going to generate any subroutine calls, and we have |
| 10957 | * a return value on the stack, we need to save the return value |
| 10958 | * in a local to make sure the calculated value isn't affected |
| 10959 | * by the subroutine call |
| 10960 | */ |
| 10961 | if (val_on_stack |
| 10962 | && G_cs->get_enclosing()->will_gen_code_unwind_for_return() |
| 10963 | && G_cs->get_code_body() != 0) |
| 10964 | { |
| 10965 | /* allocate a local variable to save the return value */ |
| 10966 | fin_ret_lcl = G_cs->get_code_body()->alloc_fin_ret_lcl(); |
| 10967 | |
| 10968 | /* save the return value in a stack temporary for a moment */ |
| 10969 | CTcSymLocal::s_gen_code_setlcl_stk(fin_ret_lcl, FALSE); |
| 10970 | |
| 10971 | /* |
| 10972 | * note that we saved the return value, so we can retrieve |
| 10973 | * it later |
| 10974 | */ |
| 10975 | did_save_retval = TRUE; |
| 10976 | } |
| 10977 | else |
| 10978 | { |
| 10979 | /* note that we didn't save the return value */ |
| 10980 | did_save_retval = FALSE; |
| 10981 | } |
| 10982 | |
| 10983 | /* generate the unwind */ |
| 10984 | G_cs->get_enclosing()->gen_code_unwind_for_return(); |
| 10985 | |
| 10986 | /* if we saved the return value, retrieve it */ |
| 10987 | if (did_save_retval) |
| 10988 | CTcSymLocal::s_gen_code_getlcl(fin_ret_lcl, FALSE); |
| 10989 | } |
| 10990 | |
| 10991 | /* check for an expression to return */ |
| 10992 | if (G_cg->is_in_constructor()) |
| 10993 | { |
| 10994 | /* we're in a constructor - return 'self' */ |
| 10995 | G_cg->write_op(OPC_PUSHSELF); |
| 10996 | G_cg->write_op(OPC_RETVAL); |
| 10997 | } |
| 10998 | else if (expr_ == 0) |
| 10999 | { |
| 11000 | /* |
| 11001 | * there's no expression - generate a simple void return (but |
| 11002 | * explicitly return nil, so we don't return something left in |
| 11003 | * R0 from a previous function call we made) |
| 11004 | */ |
| 11005 | G_cg->write_op(OPC_RETNIL); |
| 11006 | } |
| 11007 | else |
| 11008 | { |
| 11009 | /* check for a constant expression */ |
| 11010 | if (expr_->is_const()) |
| 11011 | { |
| 11012 | switch(expr_->get_const_val()->get_type()) |
| 11013 | { |
| 11014 | case TC_CVT_NIL: |
| 11015 | /* generate a RETNIL instruction */ |
| 11016 | G_cg->write_op(OPC_RETNIL); |
| 11017 | break; |
| 11018 | |
| 11019 | case TC_CVT_TRUE: |
| 11020 | /* generate a RETTRUE instruction */ |
| 11021 | G_cg->write_op(OPC_RETTRUE); |
| 11022 | break; |
| 11023 | |
| 11024 | default: |
| 11025 | break; |
| 11026 | } |
| 11027 | } |
| 11028 | |
| 11029 | /* |
| 11030 | * if we needed code generation to evaluate the return value, we |
| 11031 | * now need to return the value |
| 11032 | */ |
| 11033 | if (need_gen) |
| 11034 | { |
| 11035 | /* |
| 11036 | * Other types don't have constant-return opcodes. We |
| 11037 | * already generated the expression value (before invoking |
| 11038 | * the enclosing 'finally' handlers, if any), so the value |
| 11039 | * is on the stack, and all we need to do is return it. |
| 11040 | * |
| 11041 | * If we didn't actually leave a value on the stack, we'll |
| 11042 | * just return nil. |
| 11043 | */ |
| 11044 | if (val_on_stack) |
| 11045 | { |
| 11046 | /* generate the return-value opcode */ |
| 11047 | G_cg->write_op(OPC_RETVAL); |
| 11048 | |
| 11049 | /* RETVAL removes an element from the stack */ |
| 11050 | G_cg->note_pop(); |
| 11051 | } |
| 11052 | else |
| 11053 | { |
| 11054 | /* |
| 11055 | * The depth didn't change - they must have evaluated an |
| 11056 | * expression involving a dstring or void function. |
| 11057 | * Return nil instead of the non-existent value. |
| 11058 | */ |
| 11059 | G_cg->write_op(OPC_RETNIL); |
| 11060 | } |
| 11061 | } |
| 11062 | } |
| 11063 | } |
| 11064 | |
| 11065 | /* ------------------------------------------------------------------------ */ |
| 11066 | /* |
| 11067 | * Static property initializer statement |
| 11068 | */ |
| 11069 | void CTPNStmStaticPropInit::gen_code(int, int) |
| 11070 | { |
| 11071 | int depth; |
| 11072 | |
| 11073 | /* add a line record */ |
| 11074 | add_debug_line_rec(); |
| 11075 | |
| 11076 | /* note the initial stack depth */ |
| 11077 | depth = G_cg->get_sp_depth(); |
| 11078 | |
| 11079 | /* generate the expression, keeping the generated value */ |
| 11080 | expr_->gen_code(FALSE, FALSE); |
| 11081 | |
| 11082 | /* ensure that we generated a value; if we didn't, push nil by default */ |
| 11083 | if (G_cg->get_sp_depth() <= depth) |
| 11084 | { |
| 11085 | /* push a default nil value */ |
| 11086 | G_cg->write_op(OPC_PUSHNIL); |
| 11087 | G_cg->note_push(); |
| 11088 | } |
| 11089 | |
| 11090 | /* |
| 11091 | * duplicate the value on the stack, so we can assign it to |
| 11092 | * initialize the property and also return it |
| 11093 | */ |
| 11094 | G_cg->write_op(OPC_DUP); |
| 11095 | G_cg->note_push(); |
| 11096 | |
| 11097 | /* write the SETPROPSELF to initialize the property */ |
| 11098 | G_cg->write_op(OPC_SETPROPSELF); |
| 11099 | G_cs->write_prop_id(prop_); |
| 11100 | |
| 11101 | /* SETPROPSELF removes the value */ |
| 11102 | G_cg->note_pop(); |
| 11103 | |
| 11104 | /* return the value (which we duplicated on the stack) */ |
| 11105 | G_cg->write_op(OPC_RETVAL); |
| 11106 | |
| 11107 | /* RETVAL removes the value */ |
| 11108 | G_cg->note_pop(); |
| 11109 | } |
| 11110 | |
| 11111 | |
| 11112 | /* ------------------------------------------------------------------------ */ |
| 11113 | /* |
| 11114 | * Object Definition Statement |
| 11115 | */ |
| 11116 | |
| 11117 | /* |
| 11118 | * generate code |
| 11119 | */ |
| 11120 | void CTPNStmObject::gen_code(int, int) |
| 11121 | { |
| 11122 | /* if this object has been replaced, don't generate any code for it */ |
| 11123 | if (replaced_) |
| 11124 | return; |
| 11125 | |
| 11126 | /* add an implicit constructor if necessary */ |
| 11127 | add_implicit_constructor(); |
| 11128 | |
| 11129 | /* get the appropriate stream for generating the data */ |
| 11130 | CTcDataStream *str = obj_sym_->get_stream(); |
| 11131 | |
| 11132 | /* clear the internal flags */ |
| 11133 | uint internal_flags = 0; |
| 11134 | |
| 11135 | /* |
| 11136 | * if we're a modified object, set the 'modified' flag in the object |
| 11137 | * header |
| 11138 | */ |
| 11139 | if (modified_) |
| 11140 | internal_flags |= TCT3_OBJ_MODIFIED; |
| 11141 | |
| 11142 | /* set the 'transient' flag if appropriate */ |
| 11143 | if (transient_) |
| 11144 | internal_flags |= TCT3_OBJ_TRANSIENT; |
| 11145 | |
| 11146 | /* clear the object flags */ |
| 11147 | uint obj_flags = 0; |
| 11148 | |
| 11149 | /* |
| 11150 | * If we're specifically marked as a 'class' object, or we're a |
| 11151 | * modified object, set the 'class' flag in the object flags. |
| 11152 | */ |
| 11153 | if (is_class_ || modified_) |
| 11154 | obj_flags |= TCT3_OBJFLG_CLASS; |
| 11155 | |
| 11156 | /* remember our starting offset in the object stream */ |
| 11157 | ulong start_ofs = str->get_ofs(); |
| 11158 | |
| 11159 | /* |
| 11160 | * store our stream offset in our defining symbol, for storage in |
| 11161 | * the object file |
| 11162 | */ |
| 11163 | obj_sym_->set_stream_ofs(start_ofs); |
| 11164 | |
| 11165 | /* write our internal flags */ |
| 11166 | str->write2(internal_flags); |
| 11167 | |
| 11168 | /* |
| 11169 | * First, write the per-object image file "OBJS" header - each |
| 11170 | * object starts with its object ID and the number of bytes in the |
| 11171 | * object's metaclass-specific data. For now, write zero as a |
| 11172 | * placeholder for our data size. Note that this is a |
| 11173 | * self-reference: it must be modified if the object is renumbered. |
| 11174 | */ |
| 11175 | str->write_obj_id_selfref(obj_sym_); |
| 11176 | str->write2(0); |
| 11177 | |
| 11178 | /* write a placeholder for the superclass count */ |
| 11179 | str->write2(0); |
| 11180 | |
| 11181 | /* write the fixed property count */ |
| 11182 | str->write2(proplist_.cnt_); |
| 11183 | |
| 11184 | /* write the object flags */ |
| 11185 | str->write2(obj_flags); |
| 11186 | |
| 11187 | /* |
| 11188 | * First, go through the superclass list and verify that each |
| 11189 | * superclass is actually an object. |
| 11190 | */ |
| 11191 | int bad_sc = FALSE; |
| 11192 | int sc_cnt = 0; |
| 11193 | for (CTPNSuperclass *sc = get_first_sc() ; sc != 0 ; sc = sc->nxt_) |
| 11194 | { |
| 11195 | /* look up the superclass in the global symbol table */ |
| 11196 | CTcSymObj *sc_sym = (CTcSymObj *)sc->get_sym(); |
| 11197 | |
| 11198 | /* make sure it's defined, and that it's really an object */ |
| 11199 | if (sc_sym == 0) |
| 11200 | { |
| 11201 | /* not defined */ |
| 11202 | log_error(TCERR_UNDEF_SYM_SC, |
| 11203 | (int)sc->get_sym_len(), sc->get_sym_txt(), |
| 11204 | (int)obj_sym_->get_sym_len(), obj_sym_->get_sym()); |
| 11205 | |
| 11206 | /* note that we have an invalid superclass */ |
| 11207 | bad_sc = TRUE; |
| 11208 | } |
| 11209 | else if (sc_sym->get_type() != TC_SYM_OBJ) |
| 11210 | { |
| 11211 | /* log an error */ |
| 11212 | log_error(TCERR_SC_NOT_OBJECT, |
| 11213 | (int)sc_sym->get_sym_len(), sc_sym->get_sym()); |
| 11214 | |
| 11215 | /* note that we have an invalid superclass */ |
| 11216 | bad_sc = TRUE; |
| 11217 | } |
| 11218 | else |
| 11219 | { |
| 11220 | /* count the superclass */ |
| 11221 | ++sc_cnt; |
| 11222 | |
| 11223 | /* write the superclass to the object header */ |
| 11224 | str->write_obj_id(sc_sym->get_obj_id()); |
| 11225 | } |
| 11226 | } |
| 11227 | |
| 11228 | /* |
| 11229 | * If we detected a 'bad template' error when we were parsing the |
| 11230 | * object definition, and all of our superclasses are valid, report the |
| 11231 | * template error. |
| 11232 | * |
| 11233 | * Do not report this error if we have any undefined or invalid |
| 11234 | * superclasses, because (1) we've already reported one error for this |
| 11235 | * object definition (the bad superclass error), and (2) the missing |
| 11236 | * template is likely just a consequence of the bad superclass, since |
| 11237 | * we can't have scanned the proper superclass's list of templates if |
| 11238 | * they didn't tell us the correct superclass to start with. When they |
| 11239 | * fix the superclass list and re-compile the code, it's likely that |
| 11240 | * this will fix the template problem as well, since we'll probably be |
| 11241 | * able to find the template give the corrected superclass list. |
| 11242 | * |
| 11243 | * If we found an undescribed class anywhere in our hierarchy, a |
| 11244 | * template simply cannot be used with this object; otherwise, the |
| 11245 | * error is that we failed to find a suitable template |
| 11246 | */ |
| 11247 | if (has_bad_template() && !bad_sc) |
| 11248 | log_error(has_undesc_sc() |
| 11249 | ? TCERR_OBJ_DEF_CANNOT_USE_TEMPLATE |
| 11250 | : TCERR_OBJ_DEF_NO_TEMPLATE); |
| 11251 | |
| 11252 | /* go back and write the superclass count to the header */ |
| 11253 | str->write2_at(start_ofs + TCT3_TADSOBJ_HEADER_OFS, sc_cnt); |
| 11254 | |
| 11255 | /* |
| 11256 | * Write the properties. We're required to write the properties in |
| 11257 | * sorted order of property ID, but we can't do that yet, because |
| 11258 | * the property ID's aren't finalized until after linking. For now, |
| 11259 | * just write them out in the order in which they were defined. |
| 11260 | */ |
| 11261 | for (CTPNObjProp *prop = proplist_.first_ ; prop != 0 ; prop = prop->nxt_) |
| 11262 | { |
| 11263 | /* make sure we have a valid property symbol */ |
| 11264 | if (prop->get_prop_sym() != 0) |
| 11265 | { |
| 11266 | /* write the property ID */ |
| 11267 | str->write_prop_id(prop->get_prop_sym()->get_prop()); |
| 11268 | |
| 11269 | /* generate code for the property */ |
| 11270 | prop->gen_code(FALSE, FALSE); |
| 11271 | } |
| 11272 | } |
| 11273 | |
| 11274 | /* |
| 11275 | * go back and write the size of our metaclass-specific data - this |
| 11276 | * goes at offset 4 in the T3 generic metaclass header |
| 11277 | */ |
| 11278 | str->write2_at(start_ofs + TCT3_META_HEADER_OFS + 4, |
| 11279 | str->get_ofs() - (start_ofs + TCT3_META_HEADER_OFS + 6)); |
| 11280 | } |
| 11281 | |
| 11282 | /* |
| 11283 | * Check for unreferenced local variables |
| 11284 | */ |
| 11285 | void CTPNStmObject::check_locals() |
| 11286 | { |
| 11287 | /* check for unreferenced locals for each property */ |
| 11288 | for (CTPNObjProp *prop = proplist_.first_ ; prop != 0 ; prop = prop->nxt_) |
| 11289 | prop->check_locals(); |
| 11290 | } |