| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2010 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 | vmdynfunc.cpp - DynamicFunc implementation |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 12/13/09 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "os.h" |
| 29 | #include "charmap.h" |
| 30 | #include "vmdynfunc.h" |
| 31 | #include "vmerr.h" |
| 32 | #include "vmerrnum.h" |
| 33 | #include "vmtype.h" |
| 34 | #include "vmglob.h" |
| 35 | #include "vmobj.h" |
| 36 | #include "vmundo.h" |
| 37 | #include "vmmcreg.h" |
| 38 | #include "vmfile.h" |
| 39 | #include "vmbif.h" |
| 40 | #include "vmmeta.h" |
| 41 | #include "vmstack.h" |
| 42 | #include "vmrun.h" |
| 43 | #include "vmlst.h" |
| 44 | #include "vmstr.h" |
| 45 | #include "vmrun.h" |
| 46 | #include "vmgram.h" |
| 47 | #include "vmdict.h" |
| 48 | #include "vmtobj.h" |
| 49 | #include "vmpredef.h" |
| 50 | #include "vmimport.h" |
| 51 | #include "vmfunc.h" |
| 52 | #include "vmfref.h" |
| 53 | #include "utf8.h" |
| 54 | #include "vmop.h" |
| 55 | #include "tctarg.h" |
| 56 | #include "tcglob.h" |
| 57 | #include "tctok.h" |
| 58 | #include "tcprs.h" |
| 59 | #include "tcmain.h" |
| 60 | #include "resload.h" |
| 61 | #include "tchost.h" |
| 62 | #include "vmhost.h" |
| 63 | #include "tcgen.h" |
| 64 | #include "vmimage.h" |
| 65 | #include "vmpool.h" |
| 66 | |
| 67 | |
| 68 | |
| 69 | /* ------------------------------------------------------------------------ */ |
| 70 | /* |
| 71 | * Allocate an extension structure |
| 72 | */ |
| 73 | vm_dynfunc_ext *vm_dynfunc_ext::alloc_ext( |
| 74 | VMG_ CVmDynamicFunc *self, size_t bytecode_len, int obj_ref_cnt) |
| 75 | { |
| 76 | /* |
| 77 | * Calculate how much space we need: we need the base structure, plus |
| 78 | * space for the object references, plus the dynamic object header, and |
| 79 | * the byte code array. |
| 80 | */ |
| 81 | size_t siz = sizeof(vm_dynfunc_ext) |
| 82 | + (obj_ref_cnt - 1)*sizeof_field(vm_dynfunc_ext, obj_refs[0]) |
| 83 | + VMCO_PREFIX_LENGTH |
| 84 | + bytecode_len; |
| 85 | |
| 86 | /* allocate the memory */ |
| 87 | vm_dynfunc_ext *ext = (vm_dynfunc_ext *)G_mem->get_var_heap()->alloc_mem( |
| 88 | siz, self); |
| 89 | |
| 90 | /* remember the sizes */ |
| 91 | ext->obj_ref_cnt = obj_ref_cnt; |
| 92 | ext->bytecode_len = bytecode_len; |
| 93 | |
| 94 | /* we don't have any source text yet */ |
| 95 | ext->src.set_nil(); |
| 96 | |
| 97 | /* we don't have a method context object yet */ |
| 98 | ext->method_ctx.set_nil(); |
| 99 | |
| 100 | /* return the new extension */ |
| 101 | return ext; |
| 102 | } |
| 103 | |
| 104 | /* ------------------------------------------------------------------------ */ |
| 105 | /* |
| 106 | * DynamicFunc statics |
| 107 | */ |
| 108 | |
| 109 | /* metaclass registration object */ |
| 110 | static CVmMetaclassDynamicFunc metaclass_reg_obj; |
| 111 | CVmMetaclass *CVmDynamicFunc::metaclass_reg_ = &metaclass_reg_obj; |
| 112 | |
| 113 | /* function table */ |
| 114 | int (CVmDynamicFunc::*CVmDynamicFunc::func_table_[])( |
| 115 | VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc) = |
| 116 | { |
| 117 | &CVmDynamicFunc::getp_undef, |
| 118 | &CVmDynamicFunc::getp_get_source |
| 119 | }; |
| 120 | |
| 121 | /* ------------------------------------------------------------------------ */ |
| 122 | /* |
| 123 | * DynamicFunc metaclass implementation |
| 124 | */ |
| 125 | |
| 126 | /* |
| 127 | * construction |
| 128 | */ |
| 129 | CVmDynamicFunc::CVmDynamicFunc(VMG_ vm_obj_id_t self, const vm_val_t *src, |
| 130 | size_t bytecode_len, int obj_ref_cnt) |
| 131 | { |
| 132 | /* allocate our extension structure */ |
| 133 | vm_dynfunc_ext *ext = vm_dynfunc_ext::alloc_ext( |
| 134 | vmg_ this, bytecode_len, obj_ref_cnt); |
| 135 | |
| 136 | /* store it in the object */ |
| 137 | ext_ = (char *)ext; |
| 138 | |
| 139 | /* save the source value */ |
| 140 | if (src != 0) |
| 141 | ext->src = *src; |
| 142 | |
| 143 | /* set up the code object prefix header */ |
| 144 | vmb_put_objid(ext->get_prefix_ptr(), self); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * create |
| 149 | */ |
| 150 | vm_obj_id_t CVmDynamicFunc::create(VMG_ int in_root_set, |
| 151 | vm_obj_id_t globals, |
| 152 | vm_obj_id_t locals, |
| 153 | vm_obj_id_t macros, |
| 154 | const vm_val_t *srcval, |
| 155 | const char *src, size_t src_len) |
| 156 | { |
| 157 | /* get the compiler interface */ |
| 158 | CVmDynamicCompiler *comp = CVmDynamicCompiler::get(vmg0_); |
| 159 | |
| 160 | /* compile the code and create a code object */ |
| 161 | CVmDynCompResults results; |
| 162 | vm_obj_id_t id = comp->compile( |
| 163 | vmg_ in_root_set, globals, locals, macros, |
| 164 | srcval, src, src_len, DCModeAuto, 0, &results); |
| 165 | |
| 166 | /* if an error occurred, throw the error */ |
| 167 | if (id == VM_INVALID_OBJ) |
| 168 | results.throw_error(vmg0_); |
| 169 | |
| 170 | /* return the new ID */ |
| 171 | return id; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* |
| 176 | * create |
| 177 | */ |
| 178 | vm_obj_id_t CVmDynamicFunc::create(VMG_ int in_root_set, |
| 179 | vm_obj_id_t globals, vm_obj_id_t locals, |
| 180 | vm_obj_id_t macros, |
| 181 | const vm_val_t *src) |
| 182 | { |
| 183 | /* get the source value as a string */ |
| 184 | const char *src_txt = src->get_as_string(vmg0_); |
| 185 | |
| 186 | /* if it's not a string value, it's an error */ |
| 187 | if (src_txt == 0) |
| 188 | err_throw(VMERR_BAD_TYPE_BIF); |
| 189 | |
| 190 | /* create the object */ |
| 191 | return create(vmg_ in_root_set, globals, locals, macros, |
| 192 | src, src_txt + VMB_LEN, vmb_get_len(src_txt)); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Create dynamically using stack arguments. This takes the source code as |
| 197 | * a string, compiles it, and creates a DynamicFunc encapsulating the byte |
| 198 | * code. |
| 199 | */ |
| 200 | vm_obj_id_t CVmDynamicFunc::create_from_stack( |
| 201 | VMG_ const uchar **pc_ptr, uint argc) |
| 202 | { |
| 203 | /* check arguments */ |
| 204 | if (argc < 1 || argc > 4) |
| 205 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 206 | |
| 207 | /* get the source code argument (leave on stack for gc protection) */ |
| 208 | const vm_val_t *src = G_stk->get(0); |
| 209 | |
| 210 | /* get the global symbol table object, if any */ |
| 211 | vm_obj_id_t globals = VM_INVALID_OBJ; |
| 212 | if (argc >= 2) |
| 213 | { |
| 214 | /* it has to be an object or nil */ |
| 215 | if (G_stk->get(1)->typ == VM_OBJ) |
| 216 | globals = G_stk->get(1)->val.obj; |
| 217 | else if (G_stk->get(1)->typ != VM_NIL) |
| 218 | err_throw(VMERR_BAD_TYPE_BIF); |
| 219 | } |
| 220 | |
| 221 | /* get the local symbol table object, if any */ |
| 222 | vm_obj_id_t locals = VM_INVALID_OBJ; |
| 223 | if (argc >= 3) |
| 224 | { |
| 225 | /* it has to be an object or nil */ |
| 226 | if (G_stk->get(2)->typ == VM_OBJ) |
| 227 | locals = G_stk->get(2)->val.obj; |
| 228 | else if (G_stk->get(2)->typ != VM_NIL) |
| 229 | err_throw(VMERR_BAD_TYPE_BIF); |
| 230 | } |
| 231 | |
| 232 | /* check for macros */ |
| 233 | vm_obj_id_t macros = VM_INVALID_OBJ; |
| 234 | if (argc >= 4) |
| 235 | { |
| 236 | /* it has to be an object or nil */ |
| 237 | if (G_stk->get(3)->typ == VM_OBJ) |
| 238 | macros = G_stk->get(3)->val.obj; |
| 239 | else if (G_stk->get(3)->typ != VM_NIL) |
| 240 | err_throw(VMERR_BAD_TYPE_BIF); |
| 241 | } |
| 242 | |
| 243 | /* allocate the object ID and create the object */ |
| 244 | vm_obj_id_t id = create(vmg_ FALSE, globals, locals, macros, src); |
| 245 | |
| 246 | /* discard arguments */ |
| 247 | G_stk->discard(argc); |
| 248 | |
| 249 | /* return the new ID */ |
| 250 | return id; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * notify of deletion |
| 255 | */ |
| 256 | void CVmDynamicFunc::notify_delete(VMG_ int /*in_root_set*/) |
| 257 | { |
| 258 | /* free our additional data, if we have any */ |
| 259 | if (ext_ != 0) |
| 260 | G_mem->get_var_heap()->free_mem(ext_); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Retrieve a DynamicFunc ID from a bytecode method prefix |
| 265 | */ |
| 266 | vm_obj_id_t CVmDynamicFunc::get_obj_from_prefix(VMG_ const uchar *p) |
| 267 | { |
| 268 | /* |
| 269 | * A DynamicFunc stores a method header prefix just before the start of |
| 270 | * the header, and the prefix stores the associated DynamicFunc ID. |
| 271 | * Read the prefix header. |
| 272 | * |
| 273 | * (Technically, this has some risk of reading outside the bounds of |
| 274 | * valid program memory, because we don't know the origin of 'p'. |
| 275 | * Callers should never attempt calling this routine with arbitrary |
| 276 | * pointers. Only pass in pointers that are known to refer to valid |
| 277 | * method headers, AND that aren't part of the static Code Pool. The |
| 278 | * only other kind of valid method header pointer is a DynamicFunc, so |
| 279 | * as long as callers obey these rules this dereference should be |
| 280 | * safe.) |
| 281 | */ |
| 282 | vm_obj_id_t id = vmb_get_objid((const char *)p - VMCO_PREFIX_LENGTH); |
| 283 | |
| 284 | /* verify with the memory manager that this is a valid object ID */ |
| 285 | if (!G_obj_table->is_obj_id_valid(id)) |
| 286 | return VM_INVALID_OBJ; |
| 287 | |
| 288 | /* it's a valid object; make sure it's really of type DynamicFunc */ |
| 289 | if (!is_dynfunc_obj(vmg_ id)) |
| 290 | return VM_INVALID_OBJ; |
| 291 | |
| 292 | /* |
| 293 | * It's a valid DynamicFunc: verify that it's really the one it claims |
| 294 | * to be. If it is, the object's bytecode pointer should match 'p'. |
| 295 | */ |
| 296 | CVmDynamicFunc *co = (CVmDynamicFunc *)vm_objp(vmg_ id); |
| 297 | if (co->get_ext()->get_bytecode_ptr() == (const char *)p) |
| 298 | { |
| 299 | /* we have a winner! */ |
| 300 | return id; |
| 301 | } |
| 302 | |
| 303 | /* it's not our object */ |
| 304 | return VM_INVALID_OBJ; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * get a property |
| 309 | */ |
| 310 | int CVmDynamicFunc::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 311 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 312 | uint *argc) |
| 313 | { |
| 314 | uint func_idx; |
| 315 | |
| 316 | /* translate the property into a function vector index */ |
| 317 | func_idx = G_meta_table |
| 318 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 319 | |
| 320 | /* call the appropriate function */ |
| 321 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 322 | { |
| 323 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 324 | return TRUE; |
| 325 | } |
| 326 | |
| 327 | /* inherit default handling from our base class */ |
| 328 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Invoke |
| 333 | */ |
| 334 | int CVmDynamicFunc::get_invoker(VMG_ vm_val_t *val) |
| 335 | { |
| 336 | /* |
| 337 | * Return a pointer to the start of our byte code. The actual method |
| 338 | * header starts after our prefix. |
| 339 | */ |
| 340 | if (val != 0) |
| 341 | val->set_codeptr(get_ext()->get_bytecode_ptr()); |
| 342 | |
| 343 | /* we're invokable */ |
| 344 | return TRUE; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Index the object. |
| 349 | * |
| 350 | * A DynamicFunc is an invokee, and as such it can be referenced by |
| 351 | * generated code for special internal operations. We use indexing to |
| 352 | * retrieve special information: |
| 353 | * |
| 354 | * [1] is the 'self' object or method context object for the enclosing |
| 355 | * stack frame. The compiler uses this to establish the method context for |
| 356 | * a dynamic method that's compiled in the context of an enclosing frame, |
| 357 | * in analogy to an anonymous function that accesses its enclosing lexical |
| 358 | * scope's method context. |
| 359 | */ |
| 360 | int CVmDynamicFunc::index_val_q(VMG_ vm_val_t *result, |
| 361 | vm_obj_id_t self, |
| 362 | const vm_val_t *index_val) |
| 363 | { |
| 364 | /* check the type */ |
| 365 | if (index_val->typ == VM_INT) |
| 366 | { |
| 367 | /* it's an integer index - check the value */ |
| 368 | switch (index_val->val.intval) |
| 369 | { |
| 370 | case 1: |
| 371 | /* return our method context */ |
| 372 | *result = get_ext()->method_ctx; |
| 373 | return TRUE; |
| 374 | |
| 375 | default: |
| 376 | /* other values are not allowed */ |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* if we didn't handle it already, it's an invalid index */ |
| 382 | err_throw(VMERR_INDEX_OUT_OF_RANGE); |
| 383 | AFTER_ERR_THROW(return TRUE;) |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * load from an image file |
| 388 | */ |
| 389 | void CVmDynamicFunc::load_from_image(VMG_ vm_obj_id_t self, |
| 390 | const char *ptr, size_t siz) |
| 391 | { |
| 392 | /* load our image data */ |
| 393 | load_image_data(vmg_ self, ptr, siz); |
| 394 | |
| 395 | /* |
| 396 | * save our image data pointer in the object table, so that we can |
| 397 | * access it (without storing it ourselves) during a reload |
| 398 | */ |
| 399 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * reload from the image file |
| 404 | */ |
| 405 | void CVmDynamicFunc::reload_from_image(VMG_ vm_obj_id_t self, |
| 406 | const char *ptr, size_t siz) |
| 407 | { |
| 408 | /* load our image data */ |
| 409 | load_image_data(vmg_ self, ptr, siz); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * load or reload data from the image |
| 414 | */ |
| 415 | void CVmDynamicFunc::load_image_data(VMG_ vm_obj_id_t self, |
| 416 | const char *ptr, size_t siz) |
| 417 | { |
| 418 | /* free our existing extension, if we have one */ |
| 419 | if (ext_ != 0) |
| 420 | G_mem->get_var_heap()->free_mem(ext_); |
| 421 | |
| 422 | /* read the bytecode length */ |
| 423 | size_t bytecode_len = osrp2(ptr); |
| 424 | ptr += 2; |
| 425 | |
| 426 | /* read the object reference count */ |
| 427 | int obj_ref_cnt = osrp2(ptr); |
| 428 | ptr += 2; |
| 429 | |
| 430 | /* allocate the extension */ |
| 431 | vm_dynfunc_ext *ext = vm_dynfunc_ext::alloc_ext( |
| 432 | vmg_ this, bytecode_len, obj_ref_cnt); |
| 433 | ext_ = (char *)ext; |
| 434 | |
| 435 | /* save the method context object */ |
| 436 | vmb_get_dh(ptr, &ext->method_ctx); |
| 437 | ptr += VMB_DATAHOLDER; |
| 438 | |
| 439 | /* save the source string value */ |
| 440 | vmb_get_dh(ptr, &ext->src); |
| 441 | ptr += VMB_DATAHOLDER; |
| 442 | |
| 443 | /* set up the dynamic code prefix header */ |
| 444 | vmb_put_objid(ext->get_prefix_ptr(), self); |
| 445 | |
| 446 | /* copy the byte code */ |
| 447 | memcpy(ext->get_bytecode_ptr(), ptr, bytecode_len); |
| 448 | ptr += bytecode_len; |
| 449 | |
| 450 | /* load the object reference offsets */ |
| 451 | for (int i = 0 ; i < obj_ref_cnt ; ++i, ptr += 2) |
| 452 | ext->obj_refs[i] = osrp2(ptr); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | /* |
| 457 | * save to a file |
| 458 | */ |
| 459 | void CVmDynamicFunc::save_to_file(VMG_ class CVmFile *fp) |
| 460 | { |
| 461 | char buf[VMB_DATAHOLDER]; |
| 462 | vm_dynfunc_ext *ext = get_ext(); |
| 463 | |
| 464 | /* write our source code length */ |
| 465 | fp->write_uint2(ext->bytecode_len); |
| 466 | |
| 467 | /* write our object reference count */ |
| 468 | fp->write_uint2(ext->obj_ref_cnt); |
| 469 | |
| 470 | /* write the method context object */ |
| 471 | vmb_put_dh(buf, &ext->method_ctx); |
| 472 | fp->write_bytes(buf, VMB_DATAHOLDER); |
| 473 | |
| 474 | /* write our source object value */ |
| 475 | vmb_put_dh(buf, &ext->src); |
| 476 | fp->write_bytes(buf, VMB_DATAHOLDER); |
| 477 | |
| 478 | /* write the bytecode data */ |
| 479 | fp->write_bytes(ext->get_bytecode_ptr(), ext->bytecode_len); |
| 480 | |
| 481 | /* write the object reference list */ |
| 482 | for (int i = 0 ; i < ext->obj_ref_cnt ; ++i) |
| 483 | fp->write_uint2(ext->obj_refs[i]); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * restore from a file |
| 488 | */ |
| 489 | void CVmDynamicFunc::restore_from_file(VMG_ vm_obj_id_t self, |
| 490 | CVmFile *fp, CVmObjFixup *fixups) |
| 491 | { |
| 492 | char buf[VMB_DATAHOLDER]; |
| 493 | |
| 494 | /* free our existing extension, if we have one */ |
| 495 | if (ext_ != 0) |
| 496 | G_mem->get_var_heap()->free_mem(ext_); |
| 497 | |
| 498 | /* read the bytecode length and object reference count */ |
| 499 | size_t bytecode_len = fp->read_uint2(); |
| 500 | int obj_ref_cnt = fp->read_uint2(); |
| 501 | |
| 502 | /* allocate the extension */ |
| 503 | vm_dynfunc_ext *ext = vm_dynfunc_ext::alloc_ext( |
| 504 | vmg_ this, bytecode_len, obj_ref_cnt); |
| 505 | ext_ = (char *)ext; |
| 506 | |
| 507 | /* read the method context object */ |
| 508 | fp->read_bytes(buf, VMB_DATAHOLDER); |
| 509 | fixups->fix_dh(vmg_ buf); |
| 510 | vmb_get_dh(buf, &ext->method_ctx); |
| 511 | |
| 512 | /* read the source object */ |
| 513 | fp->read_bytes(buf, VMB_DATAHOLDER); |
| 514 | fixups->fix_dh(vmg_ buf); |
| 515 | vmb_get_dh(buf, &ext->src); |
| 516 | |
| 517 | /* set up the dynamic object code prefix header */ |
| 518 | vmb_put_objid(ext->get_prefix_ptr(), self); |
| 519 | |
| 520 | /* read the bytecode */ |
| 521 | char *bc = ext->get_bytecode_ptr(); |
| 522 | fp->read_bytes(bc, bytecode_len); |
| 523 | |
| 524 | /* read the object references, fixing each one up in the bytecode */ |
| 525 | for (int i = 0 ; i < obj_ref_cnt ; ++i) |
| 526 | { |
| 527 | /* read and save the offset of this reference */ |
| 528 | uint ofs = ext->obj_refs[i] = fp->read_uint2(); |
| 529 | |
| 530 | /* fix up the object ID in the bytecode data */ |
| 531 | fixups->fix_vmb_obj(vmg_ bc + ofs); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * mark references |
| 537 | */ |
| 538 | void CVmDynamicFunc::mark_refs(VMG_ uint state) |
| 539 | { |
| 540 | /* |
| 541 | * Get my extension, and only proceed if it's non-empty. For most |
| 542 | * object types this isn't an issue, because the only time we create |
| 543 | * empty objects of most types is during loading, when gc is disabled. |
| 544 | * But we do create long-lived empty code objects in the course of |
| 545 | * compilation, because we need a placeholder for the object ID ahead |
| 546 | * of the compilation. |
| 547 | */ |
| 548 | vm_dynfunc_ext *ext = get_ext(); |
| 549 | if (ext != 0) |
| 550 | { |
| 551 | /* mark my context object as referenced */ |
| 552 | if (ext->method_ctx.typ == VM_OBJ) |
| 553 | G_obj_table->mark_all_refs(ext->method_ctx.val.obj, state); |
| 554 | |
| 555 | /* mark my source code string as referenced */ |
| 556 | if (ext->src.typ == VM_OBJ) |
| 557 | G_obj_table->mark_all_refs(ext->src.val.obj, state); |
| 558 | |
| 559 | /* mark each object reference in the bytecode stream */ |
| 560 | char *bc = ext->get_bytecode_ptr(); |
| 561 | for (int i = 0 ; i < ext->obj_ref_cnt ; ++i) |
| 562 | { |
| 563 | /* |
| 564 | * Read the object ID from the bytecode. The obj_refs[] array |
| 565 | * entry isn't the actual reference, but rather the offset of |
| 566 | * the reference in the bytecode stream. The reference itself |
| 567 | * is a portable OBJECT_ID field. |
| 568 | */ |
| 569 | vm_obj_id_t ref = vmb_get_objid(bc + ext->obj_refs[i]); |
| 570 | G_obj_table->mark_all_refs(ref, state); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /* |
| 577 | * property evaluator - get the source code string |
| 578 | */ |
| 579 | int CVmDynamicFunc::getp_get_source(VMG_ vm_obj_id_t self, |
| 580 | vm_val_t *retval, uint *argc) |
| 581 | { |
| 582 | /* check arguments */ |
| 583 | static CVmNativeCodeDesc desc(0); |
| 584 | if (get_prop_check_argc(retval, argc, &desc)) |
| 585 | return TRUE; |
| 586 | |
| 587 | /* return the source code object */ |
| 588 | *retval = get_ext()->src; |
| 589 | |
| 590 | /* handled */ |
| 591 | return TRUE; |
| 592 | } |
| 593 | |
| 594 | /* ------------------------------------------------------------------------ */ |
| 595 | /* |
| 596 | * Custom host interface. This host interface captures error messages |
| 597 | * generated during compilation so that we can return them to the program. |
| 598 | */ |
| 599 | class CTcHostIfcDynComp: public CTcHostIfc |
| 600 | { |
| 601 | public: |
| 602 | CTcHostIfcDynComp() |
| 603 | { |
| 604 | /* allocate an initial buffer */ |
| 605 | erralo_ = 1024; |
| 606 | errmsg_ = (char *)t3malloc(erralo_); |
| 607 | |
| 608 | /* initially clear the message buffer */ |
| 609 | reset_messages(); |
| 610 | } |
| 611 | |
| 612 | ~CTcHostIfcDynComp() |
| 613 | { |
| 614 | /* free our buffer */ |
| 615 | t3free(errmsg_); |
| 616 | } |
| 617 | |
| 618 | /* clear the message buffer */ |
| 619 | void reset_messages() |
| 620 | { |
| 621 | errmsg_[0] = '\0'; |
| 622 | p_ = errmsg_; |
| 623 | } |
| 624 | |
| 625 | /* get the text of the first error message since we cleared the buffer */ |
| 626 | const char *get_error_msg() const { return errmsg_; } |
| 627 | |
| 628 | /* |
| 629 | * CTcHostIfc interface |
| 630 | */ |
| 631 | |
| 632 | /* display an informational message */ |
| 633 | virtual void v_print_msg(const char *, va_list) |
| 634 | { |
| 635 | /* ignore informational messages */ |
| 636 | } |
| 637 | |
| 638 | /* display a process step message */ |
| 639 | virtual void v_print_step(const char *, va_list) |
| 640 | { |
| 641 | /* ignore process step messages */ |
| 642 | } |
| 643 | |
| 644 | /* display an error message */ |
| 645 | virtual void v_print_err(const char *msg, va_list args) |
| 646 | { |
| 647 | /* format the message */ |
| 648 | char *f = t3vsprintf_alloc(msg, args); |
| 649 | if (f == 0) |
| 650 | return; |
| 651 | |
| 652 | /* get the length */ |
| 653 | size_t len = strlen(f); |
| 654 | |
| 655 | /* expand our buffer if necessary */ |
| 656 | size_t curlen = p_ - errmsg_; |
| 657 | size_t need = curlen + len + 1; |
| 658 | if (need > erralo_) |
| 659 | { |
| 660 | erralo_ = need + 1024; |
| 661 | char *newbuf = (char *)t3realloc(errmsg_, erralo_); |
| 662 | if (newbuf == 0) |
| 663 | return; |
| 664 | |
| 665 | /* switch to the new buffer */ |
| 666 | errmsg_ = newbuf; |
| 667 | p_ = errmsg_ + curlen; |
| 668 | } |
| 669 | |
| 670 | /* append the message */ |
| 671 | memcpy(p_, f, len + 1); |
| 672 | |
| 673 | /* bump the write pointer */ |
| 674 | p_ += len; |
| 675 | |
| 676 | /* done with the allocated message text */ |
| 677 | t3free(f); |
| 678 | } |
| 679 | |
| 680 | private: |
| 681 | /* our error message buffer */ |
| 682 | char *errmsg_; |
| 683 | |
| 684 | /* allocated buffer size */ |
| 685 | size_t erralo_; |
| 686 | |
| 687 | /* pointer to next available byte of message buffer */ |
| 688 | char *p_; |
| 689 | }; |
| 690 | |
| 691 | |
| 692 | /* ------------------------------------------------------------------------ */ |
| 693 | /* |
| 694 | * Dynamic compiler symbol table interface. This provides the compiler |
| 695 | * with global symbols specified via an indexable object. This is usually |
| 696 | * a LookupTable, but any object that can be indexed by symbol name string |
| 697 | * will work. The actual compiler symbols retrieved from |
| 698 | * t3GetGlobalSymbols() can be used, or the program can provide its own |
| 699 | * custom symbol table. |
| 700 | * |
| 701 | * We implement the symbol table as a view of the user-code object, with |
| 702 | * caching in our own hash table. To match the interface, we have to |
| 703 | * create a CTcSymbol object for each symbol we successfully look up. |
| 704 | * Rather than creating redundant copies of these objects, we cache them in |
| 705 | * our own hash table. |
| 706 | */ |
| 707 | class CVmDynFuncSymtab: public CTcPrsSymtab |
| 708 | { |
| 709 | public: |
| 710 | /* initialize with a source object */ |
| 711 | CVmDynFuncSymtab(VMG_ vm_obj_id_t globals, vm_obj_id_t locals) |
| 712 | : CTcPrsSymtab(0) |
| 713 | { |
| 714 | /* remember the globals */ |
| 715 | gptr_ = VMGLOB_ADDR; |
| 716 | |
| 717 | /* remember my symbol table objects */ |
| 718 | globals_ = globals; |
| 719 | locals_ = locals; |
| 720 | } |
| 721 | |
| 722 | /* find a symbol - implementation of CTcPrsDbgSymtab interface */ |
| 723 | virtual CTcSymbol *find_direct(const textchar_t *sym, size_t len) |
| 724 | { |
| 725 | /* first, check the cache to see if we've already looked it up */ |
| 726 | CTcSymbol *ret = CTcPrsSymtab::find_direct(sym, len); |
| 727 | if (ret != 0) |
| 728 | return ret; |
| 729 | |
| 730 | /* |
| 731 | * Didn't find it - check the user-code tables. |
| 732 | */ |
| 733 | |
| 734 | /* try our local symbol table */ |
| 735 | ret = find_local(sym, len); |
| 736 | |
| 737 | /* if that didn't work, try our global symbol table */ |
| 738 | if (ret == 0) |
| 739 | ret = find_global(sym, len); |
| 740 | |
| 741 | /* if we found a symbol, add it to the cache */ |
| 742 | if (ret != 0) |
| 743 | CTcPrsSymtab::add_entry(ret); |
| 744 | |
| 745 | /* return the symbol */ |
| 746 | return ret; |
| 747 | } |
| 748 | |
| 749 | /* add an entry */ |
| 750 | virtual void add_entry(CTcSymbol *sym) |
| 751 | { |
| 752 | /* flag an error */ |
| 753 | G_tok->log_error(TCERR_RT_CANNOT_DEFINE_GLOBALS, |
| 754 | (int)sym->get_sym_len(), sym->get_sym()); |
| 755 | } |
| 756 | |
| 757 | private: |
| 758 | /* look up a symbol in our local symbol table */ |
| 759 | CTcSymbol *find_local(const textchar_t *sym, size_t len) |
| 760 | { |
| 761 | /* if there's no user-code table, there's nothing to search */ |
| 762 | if (locals_ == VM_INVALID_OBJ) |
| 763 | return 0; |
| 764 | |
| 765 | /* establish access to globals */ |
| 766 | VMGLOB_PTR(gptr_); |
| 767 | |
| 768 | /* |
| 769 | * if it's a StackFrameDesc object, look up the symbol; otherwise |
| 770 | * try to treat it as a list-like object, and check each element of |
| 771 | * the list |
| 772 | */ |
| 773 | if (CVmObjFrameDesc::is_framedesc_obj(vmg_ locals_)) |
| 774 | { |
| 775 | /* look up the value in our single table */ |
| 776 | return find_local(sym, len, locals_); |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | /* assume it's a list - set up an object value */ |
| 781 | vm_val_t lt; |
| 782 | lt.set_obj(locals_); |
| 783 | |
| 784 | /* check each list element */ |
| 785 | int n = lt.ll_length(vmg0_); |
| 786 | for (int i = 1 ; i <= n ; ++i) |
| 787 | { |
| 788 | /* get this element */ |
| 789 | vm_val_t ele; |
| 790 | lt.ll_index(vmg_ &ele, i); |
| 791 | |
| 792 | /* if it's a StackFrameDesc object, check it */ |
| 793 | if (ele.typ == VM_OBJ |
| 794 | && CVmObjFrameDesc::is_framedesc_obj(vmg_ ele.val.obj)) |
| 795 | { |
| 796 | /* look up the value in this table */ |
| 797 | CTcSymbol *ret = find_local(sym, len, ele.val.obj); |
| 798 | |
| 799 | /* if we found it, return the value */ |
| 800 | if (ret != 0) |
| 801 | return ret; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /* we didn't find a value */ |
| 806 | return 0; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* find a symbol in a given local table */ |
| 811 | CTcSymbol *find_local(const textchar_t *sym, size_t len, vm_obj_id_t fref) |
| 812 | { |
| 813 | /* establish access to globals */ |
| 814 | VMGLOB_PTR(gptr_); |
| 815 | |
| 816 | /* get the frame descriptor object */ |
| 817 | CVmObjFrameDesc *fp = (CVmObjFrameDesc *)vm_objp(vmg_ fref); |
| 818 | |
| 819 | /* look up the symbol */ |
| 820 | CVmDbgFrameSymPtr info; |
| 821 | if (fp->find_local(vmg_ sym, len, &info)) |
| 822 | { |
| 823 | /* return a new dynamic local symbol */ |
| 824 | return new CTcSymDynLocal( |
| 825 | sym, len, FALSE, fp->get_frame_ref_id(), |
| 826 | fp->get_frame_ref(vmg0_)->get_var_frame_index(&info), |
| 827 | info.is_ctx_local() ? info.get_ctx_arr_idx() : 0); |
| 828 | } |
| 829 | |
| 830 | /* didn't find it - return failure */ |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | /* look up a symbol in our global table */ |
| 835 | CTcSymbol *find_global(const textchar_t *sym, size_t len) |
| 836 | { |
| 837 | /* if there's no user-code table, there's nothing to search */ |
| 838 | if (globals_ == VM_INVALID_OBJ) |
| 839 | return 0; |
| 840 | |
| 841 | /* presume we won't find a symbol */ |
| 842 | CTcSymbol *ret = 0; |
| 843 | |
| 844 | /* establish access to globals */ |
| 845 | VMGLOB_PTR(gptr_); |
| 846 | |
| 847 | /* create a string for the symbol name; stack it for gc protection */ |
| 848 | vm_val_t symstr; |
| 849 | symstr.set_obj(CVmObjString::create(vmg_ FALSE, sym, len)); |
| 850 | G_stk->push(&symstr); |
| 851 | |
| 852 | /* look up the symbol string in the table */ |
| 853 | vm_val_t symval; |
| 854 | vm_objp(vmg_ globals_)->index_val_ov(vmg_ &symval, globals_, &symstr); |
| 855 | G_stk->push(&symval); |
| 856 | |
| 857 | /* check what we found */ |
| 858 | switch (symval.typ) |
| 859 | { |
| 860 | case VM_OBJ: |
| 861 | /* it's an object - map it to CTcSymObj or CTcSymMetaclass */ |
| 862 | if (CVmObjClass::is_intcls_obj(vmg_ symval.val.obj)) |
| 863 | { |
| 864 | /* |
| 865 | * it's an IntrinsicClass object - this represents a |
| 866 | * metaclass, so map it to a CTcSymMetaclass symbol |
| 867 | */ |
| 868 | CVmObjClass *cl = (CVmObjClass *)vm_objp(vmg_ symval.val.obj); |
| 869 | ret = new CTcSymMetaclass(sym, len, FALSE, |
| 870 | cl->get_meta_idx(), |
| 871 | symval.val.obj); |
| 872 | } |
| 873 | else if (vm_objp(vmg_ symval.val.obj)->get_invoker(vmg_ 0)) |
| 874 | { |
| 875 | /* |
| 876 | * it's an invokable object - create a function-like object |
| 877 | * symbol for it |
| 878 | */ |
| 879 | ret = new CTcSymFuncObj(sym, len, FALSE, symval.val.obj, |
| 880 | FALSE, TC_META_UNKNOWN, 0); |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | /* |
| 885 | * It's not an IntrinsicClass object, so it's a regular |
| 886 | * CTcSymObj symbol. Check for special metaclasses that |
| 887 | * the compiler is aware of. |
| 888 | */ |
| 889 | tc_metaclass_t meta = TC_META_UNKNOWN; |
| 890 | |
| 891 | if (CVmObjTads::is_tadsobj_obj(vmg_ symval.val.obj)) |
| 892 | meta = TC_META_TADSOBJ; |
| 893 | else if (CVmObjGramProd::is_gramprod_obj(vmg_ symval.val.obj)) |
| 894 | meta = TC_META_GRAMPROD; |
| 895 | else if (CVmObjDict::is_dictionary_obj(vmg_ symval.val.obj)) |
| 896 | meta = TC_META_DICT; |
| 897 | else if (CVmObjIntClsMod::is_intcls_mod_obj(vmg_ symval.val.obj)) |
| 898 | meta = TC_META_ICMOD; |
| 899 | |
| 900 | /* create the object symbol */ |
| 901 | ret = new CTcSymObj(sym, len, FALSE, symval.val.obj, |
| 902 | FALSE, meta, 0); |
| 903 | } |
| 904 | break; |
| 905 | |
| 906 | case VM_PROP: |
| 907 | /* it's a property ID - map to a CTcSymProp */ |
| 908 | ret = new CTcSymProp(sym, len, FALSE, symval.val.prop); |
| 909 | break; |
| 910 | |
| 911 | case VM_FUNCPTR: |
| 912 | /* it's a function pointer */ |
| 913 | { |
| 914 | /* get the method header, for the parameter information */ |
| 915 | CVmFuncPtr fp(vmg_ symval.val.ofs); |
| 916 | |
| 917 | /* set up the resolved function symbol */ |
| 918 | ret = new CTcSymFunc( |
| 919 | sym, len, FALSE, |
| 920 | fp.get_min_argc(), fp.get_opt_argc(), |
| 921 | fp.is_varargs(), TRUE, |
| 922 | FALSE, FALSE, FALSE, TRUE); |
| 923 | |
| 924 | /* we know the absolute code address - set it */ |
| 925 | ((CTcSymFunc *)ret)->set_abs_addr(symval.val.ofs); |
| 926 | } |
| 927 | break; |
| 928 | |
| 929 | case VM_ENUM: |
| 930 | /* enumerated constant */ |
| 931 | ret = new CTcSymEnum(sym, len, FALSE, |
| 932 | symval.val.enumval, FALSE); |
| 933 | break; |
| 934 | |
| 935 | case VM_BIFPTR: |
| 936 | /* built-in function pointer */ |
| 937 | ret = 0; |
| 938 | { |
| 939 | /* decode the function pointer */ |
| 940 | ushort set_idx = symval.val.bifptr.set_idx; |
| 941 | ushort func_idx = symval.val.bifptr.func_idx; |
| 942 | |
| 943 | /* get the function descriptor */ |
| 944 | const vm_bif_desc *desc = |
| 945 | G_bif_table->get_desc(set_idx, func_idx); |
| 946 | |
| 947 | /* if there's a valid descriptor, set up the symbol */ |
| 948 | if (desc != 0 && desc->func != 0) |
| 949 | { |
| 950 | /* found it - set up the symbol */ |
| 951 | ret = new CTcSymBif(sym, len, FALSE, |
| 952 | set_idx, func_idx, TRUE, |
| 953 | desc->min_argc, |
| 954 | desc->min_argc + desc->opt_argc, |
| 955 | desc->varargs); |
| 956 | } |
| 957 | } |
| 958 | break; |
| 959 | |
| 960 | default: |
| 961 | /* |
| 962 | * it's either not in the table or it's not a type we can map |
| 963 | * to a compiler symbol - return 'not found' |
| 964 | */ |
| 965 | ret = 0; |
| 966 | break; |
| 967 | } |
| 968 | |
| 969 | /* done with the gc protection */ |
| 970 | G_stk->discard(2); |
| 971 | |
| 972 | /* return the symbol */ |
| 973 | return ret; |
| 974 | } |
| 975 | |
| 976 | /* VM globals */ |
| 977 | vm_globals *gptr_; |
| 978 | |
| 979 | /* the local and global symbol table objects */ |
| 980 | vm_obj_id_t globals_; |
| 981 | vm_obj_id_t locals_; |
| 982 | }; |
| 983 | |
| 984 | |
| 985 | /* ------------------------------------------------------------------------ */ |
| 986 | /* |
| 987 | * Dynamic compiler macro table. This works a lot like our global symbol |
| 988 | * table, to provide program-defined macros to the tokenizer during dynamic |
| 989 | * compilation. |
| 990 | */ |
| 991 | class CVmDynFuncMacros: public CTcMacroTable |
| 992 | { |
| 993 | public: |
| 994 | CVmDynFuncMacros(VMG_ vm_obj_id_t tab) |
| 995 | : hash_(512, new CVmHashFuncCS(), TRUE) |
| 996 | { |
| 997 | /* remember the globals */ |
| 998 | globals_ = VMGLOB_ADDR; |
| 999 | |
| 1000 | /* remember my symbol table object */ |
| 1001 | tab_ = tab; |
| 1002 | } |
| 1003 | |
| 1004 | /* find an entry */ |
| 1005 | CVmHashEntry *find(const char *sym, size_t len) |
| 1006 | { |
| 1007 | CVmHashEntry *entry; |
| 1008 | |
| 1009 | /* first, check to see if it's our underlying table */ |
| 1010 | entry = hash_.find(sym, len); |
| 1011 | if (entry != 0) |
| 1012 | return entry; |
| 1013 | |
| 1014 | /* if there's no user-code table, fail */ |
| 1015 | if (tab_ == VM_INVALID_OBJ) |
| 1016 | return 0; |
| 1017 | |
| 1018 | /* establish access to globals */ |
| 1019 | VMGLOB_PTR(globals_); |
| 1020 | |
| 1021 | /* create a string for the symbol name; stack it for gc protection */ |
| 1022 | vm_val_t symstr; |
| 1023 | symstr.set_obj(CVmObjString::create(vmg_ FALSE, sym, len)); |
| 1024 | G_stk->push(&symstr); |
| 1025 | |
| 1026 | /* look up the symbol string in the table */ |
| 1027 | vm_val_t symval; |
| 1028 | vm_objp(vmg_ tab_)->index_val_ov(vmg_ &symval, tab_, &symstr); |
| 1029 | G_stk->push(&symval); |
| 1030 | |
| 1031 | /* it has to be a list, and it has to have at least three elements */ |
| 1032 | const char *lst = symval.get_as_list(vmg0_); |
| 1033 | if (lst != 0 && vmb_get_len(lst) >= 3) |
| 1034 | { |
| 1035 | vm_val_t ele, subele; |
| 1036 | const char *exp, *arglst; |
| 1037 | uint flags = 0; |
| 1038 | int argc; |
| 1039 | const char **argv = 0; |
| 1040 | size_t *argvlen = 0; |
| 1041 | int i; |
| 1042 | |
| 1043 | /* the first element is the expansion string */ |
| 1044 | CVmObjList::index_list(vmg_ &ele, lst, 1); |
| 1045 | if ((exp = ele.get_as_string(vmg0_)) == 0) |
| 1046 | goto done; |
| 1047 | |
| 1048 | /* the second element is the argument list */ |
| 1049 | CVmObjList::index_list(vmg_ &ele, lst, 2); |
| 1050 | if ((arglst = ele.get_as_list(vmg0_)) == 0) |
| 1051 | goto done; |
| 1052 | |
| 1053 | /* figure the argument count */ |
| 1054 | argc = vmb_get_len(arglst); |
| 1055 | |
| 1056 | /* build the argument vector */ |
| 1057 | if (argc != 0) |
| 1058 | { |
| 1059 | /* allocate the string and length arrays */ |
| 1060 | argv = new const char*[argc]; |
| 1061 | argvlen = new size_t[argc]; |
| 1062 | |
| 1063 | /* populate the arrays */ |
| 1064 | for (i = 0 ; i < argc ; ++i) |
| 1065 | { |
| 1066 | /* retrieve this argument name string */ |
| 1067 | CVmObjList::index_list(vmg_ &subele, arglst, i+1); |
| 1068 | const char *n = subele.get_as_string(vmg0_); |
| 1069 | if (n == 0) |
| 1070 | goto done; |
| 1071 | |
| 1072 | /* add it to the string and length arrays */ |
| 1073 | argv[i] = n + VMB_LEN; |
| 1074 | argvlen[i] = vmb_get_len(n); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /* the third element is the flags */ |
| 1079 | CVmObjList::index_list(vmg_ &ele, lst, 3); |
| 1080 | if (!ele.is_numeric(vmg0_)) |
| 1081 | goto done; |
| 1082 | flags = ele.num_to_int(vmg0_); |
| 1083 | |
| 1084 | /* create the entry */ |
| 1085 | entry = new CTcHashEntryPpDefine( |
| 1086 | sym, len, TRUE, (flags & 1) != 0, argc, (flags & 2) != 0, |
| 1087 | argv, argvlen, exp + VMB_LEN, vmb_get_len(exp)); |
| 1088 | |
| 1089 | /* add the entry to our cache */ |
| 1090 | hash_.add(entry); |
| 1091 | |
| 1092 | done: |
| 1093 | /* free the argument list */ |
| 1094 | if (argv != 0) |
| 1095 | delete [] argv; |
| 1096 | if (argvlen != 0) |
| 1097 | delete [] argvlen; |
| 1098 | } |
| 1099 | |
| 1100 | /* done with our gc protection */ |
| 1101 | G_stk->discard(2); |
| 1102 | |
| 1103 | /* return what we found */ |
| 1104 | return entry; |
| 1105 | } |
| 1106 | |
| 1107 | /* add an entry - ignore */ |
| 1108 | void add(CVmHashEntry *) { } |
| 1109 | |
| 1110 | /* remove an entry - ignore */ |
| 1111 | void remove(CVmHashEntry *) { } |
| 1112 | |
| 1113 | /* enumerate entries - ignore */ |
| 1114 | void enum_entries(void (*)(void *, class CVmHashEntry *), void *) { } |
| 1115 | |
| 1116 | /* dump to a file - ignore */ |
| 1117 | void debug_dump() { } |
| 1118 | |
| 1119 | private: |
| 1120 | /* VM globals */ |
| 1121 | vm_globals *globals_; |
| 1122 | |
| 1123 | /* the user LookupTable object */ |
| 1124 | vm_obj_id_t tab_; |
| 1125 | |
| 1126 | /* our cache of symbols we've already translated */ |
| 1127 | CVmHashTable hash_; |
| 1128 | }; |
| 1129 | |
| 1130 | |
| 1131 | /* ------------------------------------------------------------------------ */ |
| 1132 | /* |
| 1133 | * Grammar production alternative parser - compiler interface for dynamic |
| 1134 | * compilation |
| 1135 | */ |
| 1136 | class CTcGramAltFuncsDyn: public CTcGramAltFuncs |
| 1137 | { |
| 1138 | public: |
| 1139 | /* initialize with the parser object */ |
| 1140 | CTcGramAltFuncsDyn(CTcPrsSymtab *symtab) { this->symtab = symtab; } |
| 1141 | |
| 1142 | /* look up a property - look up or add to the global symbols */ |
| 1143 | CTcSymProp *look_up_prop(const CTcToken *tok, int show_err) |
| 1144 | { |
| 1145 | /* look up the symbol */ |
| 1146 | CTcSymProp *prop = (CTcSymProp *)symtab->find( |
| 1147 | tok->get_text(), tok->get_text_len()); |
| 1148 | |
| 1149 | /* if it's already defined, make sure it's a property */ |
| 1150 | if (prop != 0) |
| 1151 | { |
| 1152 | /* make sure it's a property */ |
| 1153 | if (prop->get_type() != TC_SYM_PROP) |
| 1154 | { |
| 1155 | /* log an error if appropriate */ |
| 1156 | if (show_err) |
| 1157 | { |
| 1158 | G_tok->log_error( |
| 1159 | TCERR_REDEF_AS_PROP, |
| 1160 | (int)tok->get_text_len(), tok->get_text()); |
| 1161 | } |
| 1162 | |
| 1163 | /* we can't use the symbol, so forget it */ |
| 1164 | prop = 0; |
| 1165 | } |
| 1166 | } |
| 1167 | else |
| 1168 | { |
| 1169 | /* |
| 1170 | * undefined - we can't add it for dynamic compilation, so log |
| 1171 | * an error |
| 1172 | */ |
| 1173 | if (show_err) |
| 1174 | { |
| 1175 | G_tok->log_error( |
| 1176 | TCERR_UNDEF_SYM, |
| 1177 | (int)tok->get_text_len(), tok->get_text()); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /* return the property */ |
| 1182 | return prop; |
| 1183 | } |
| 1184 | |
| 1185 | /* declare a grammar production symbol */ |
| 1186 | CTcGramProdEntry *declare_gramprod(const char *txt, size_t len) |
| 1187 | { |
| 1188 | /* |
| 1189 | * we can't create symbols in the dynamic compiler - log an error |
| 1190 | * and return failure |
| 1191 | */ |
| 1192 | G_tok->log_error(TCERR_UNDEF_SYM, (int)len, txt); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Check an enum for use as a production token. For dynamic |
| 1198 | * compilation, we don't enforce the 'enum token' requirement that the |
| 1199 | * static compiler applies, because we don't have any information on |
| 1200 | * the 'enum token' attribute in the basic run-time symbol table. |
| 1201 | */ |
| 1202 | void check_enum_tok(class CTcSymEnum *) { } |
| 1203 | |
| 1204 | /* |
| 1205 | * EOF in the middle of an alternative - when we're parsing a |
| 1206 | * stand-alone grammar rule list, the rule list is the whole input |
| 1207 | * text, so EOF just means we've reached the end |
| 1208 | */ |
| 1209 | void on_eof(int * /*err*/) { } |
| 1210 | |
| 1211 | /* the parser's global symbol table */ |
| 1212 | CTcPrsSymtab *symtab; |
| 1213 | }; |
| 1214 | |
| 1215 | |
| 1216 | /* ------------------------------------------------------------------------ */ |
| 1217 | /* |
| 1218 | * Dynamic compiler interface to the VM. |
| 1219 | */ |
| 1220 | class CVmDynFuncVMIfc: public CTcVMIfc |
| 1221 | { |
| 1222 | public: |
| 1223 | CVmDynFuncVMIfc(VMG0_) |
| 1224 | { |
| 1225 | /* remember the globals */ |
| 1226 | vmg = VMGLOB_ADDR; |
| 1227 | |
| 1228 | /* we haven't saved any objects on the stack yet */ |
| 1229 | gc_cnt = 0; |
| 1230 | } |
| 1231 | |
| 1232 | /* create a live object in the VM */ |
| 1233 | virtual tctarg_obj_id_t new_obj(tctarg_obj_id_t cls) |
| 1234 | { |
| 1235 | /* establish access to globals */ |
| 1236 | VMGLOB_PTR(vmg); |
| 1237 | |
| 1238 | /* create a TadsObject instance */ |
| 1239 | int argc = 0; |
| 1240 | if (cls != VM_INVALID_OBJ) |
| 1241 | { |
| 1242 | G_stk->push_obj(vmg_ cls); |
| 1243 | ++argc; |
| 1244 | } |
| 1245 | vm_obj_id_t id = CVmObjTads::create_from_stack(vmg_ 0, argc); |
| 1246 | |
| 1247 | /* push it for gc protection */ |
| 1248 | G_stk->push_check()->set_obj(id); |
| 1249 | ++gc_cnt; |
| 1250 | |
| 1251 | /* return the new ID */ |
| 1252 | return id; |
| 1253 | } |
| 1254 | |
| 1255 | /* validate an object value */ |
| 1256 | virtual int validate_obj(tctarg_obj_id_t obj) |
| 1257 | { |
| 1258 | VMGLOB_PTR(vmg); |
| 1259 | return G_obj_table->is_obj_id_valid(obj); |
| 1260 | } |
| 1261 | |
| 1262 | /* add a property value to an object */ |
| 1263 | virtual void set_prop(tctarg_obj_id_t obj, tctarg_prop_id_t prop, |
| 1264 | const CTcConstVal *c) |
| 1265 | { |
| 1266 | /* establish access to globals */ |
| 1267 | VMGLOB_PTR(vmg); |
| 1268 | |
| 1269 | /* set up a value based on the constant value */ |
| 1270 | vm_val_t val; |
| 1271 | switch (c->get_type()) |
| 1272 | { |
| 1273 | case TC_CVT_TRUE: |
| 1274 | val.set_true(); |
| 1275 | break; |
| 1276 | |
| 1277 | case TC_CVT_INT: |
| 1278 | val.set_int(c->get_val_int()); |
| 1279 | break; |
| 1280 | |
| 1281 | case TC_CVT_SSTR: |
| 1282 | val.set_obj(CVmObjString::create( |
| 1283 | vmg_ FALSE, c->get_val_str(), c->get_val_str_len())); |
| 1284 | break; |
| 1285 | |
| 1286 | case TC_CVT_OBJ: |
| 1287 | val.set_obj(c->get_val_obj()); |
| 1288 | break; |
| 1289 | |
| 1290 | case TC_CVT_PROP: |
| 1291 | val.set_propid((vm_prop_id_t)c->get_val_prop()); |
| 1292 | break; |
| 1293 | |
| 1294 | case TC_CVT_ENUM: |
| 1295 | val.set_enum(c->get_val_enum()); |
| 1296 | break; |
| 1297 | |
| 1298 | case TC_CVT_FLOAT: |
| 1299 | val.set_obj(CVmObjBigNum::create( |
| 1300 | vmg_ FALSE, c->get_val_float(), c->get_val_float_len())); |
| 1301 | break; |
| 1302 | |
| 1303 | case TC_CVT_LIST: |
| 1304 | case TC_CVT_FUNCPTR: |
| 1305 | case TC_CVT_ANONFUNCPTR: |
| 1306 | // $$$ implement later if necessary |
| 1307 | val.set_nil(); |
| 1308 | break; |
| 1309 | |
| 1310 | case TC_CVT_NIL: |
| 1311 | case TC_CVT_UNK: |
| 1312 | default: |
| 1313 | val.set_nil(); |
| 1314 | break; |
| 1315 | } |
| 1316 | |
| 1317 | /* set the property in the object */ |
| 1318 | vm_objp(vmg_ obj)->set_prop(vmg_ G_undo, obj, prop, &val); |
| 1319 | } |
| 1320 | |
| 1321 | /* validate a property value */ |
| 1322 | virtual int validate_prop(tctarg_prop_id_t prop) |
| 1323 | { |
| 1324 | VMGLOB_PTR(vmg); |
| 1325 | return prop <= G_image_loader->get_last_prop(vmg0_); |
| 1326 | } |
| 1327 | |
| 1328 | /* validate a built-in function pointer */ |
| 1329 | virtual int validate_bif(uint set_index, uint func_index) |
| 1330 | { |
| 1331 | VMGLOB_PTR(vmg); |
| 1332 | return G_bif_table->validate_entry(set_index, func_index); |
| 1333 | } |
| 1334 | |
| 1335 | /* validate a constant pool list address */ |
| 1336 | virtual int validate_pool_str(uint32_t ofs) |
| 1337 | { |
| 1338 | /* establish globals */ |
| 1339 | VMGLOB_PTR(vmg); |
| 1340 | |
| 1341 | /* validate the starting offset and full length prefix */ |
| 1342 | if (!G_const_pool->validate_ofs(ofs) |
| 1343 | || !G_const_pool->validate_ofs(ofs+1)) |
| 1344 | return FALSE; |
| 1345 | |
| 1346 | /* get the length */ |
| 1347 | size_t len = vmb_get_len(G_const_pool->get_ptr(ofs)); |
| 1348 | |
| 1349 | /* make sure the end of the string is valid as well */ |
| 1350 | if (!G_const_pool->validate_ofs(ofs + VMB_LEN + len - 1)) |
| 1351 | return FALSE; |
| 1352 | |
| 1353 | /* success */ |
| 1354 | return TRUE; |
| 1355 | } |
| 1356 | |
| 1357 | /* validate a constant pool string address */ |
| 1358 | virtual int validate_pool_list(uint32_t ofs) |
| 1359 | { |
| 1360 | /* establish globals */ |
| 1361 | VMGLOB_PTR(vmg); |
| 1362 | |
| 1363 | /* validate the starting offset and full length prefix */ |
| 1364 | if (!G_const_pool->validate_ofs(ofs) |
| 1365 | || !G_const_pool->validate_ofs(ofs+1)) |
| 1366 | return FALSE; |
| 1367 | |
| 1368 | /* get the length */ |
| 1369 | const char *p = G_const_pool->get_ptr(ofs); |
| 1370 | size_t len = vmb_get_len(p); |
| 1371 | p += VMB_LEN; |
| 1372 | |
| 1373 | /* make sure the end of the string is valid as well */ |
| 1374 | if (!G_const_pool->validate_ofs( |
| 1375 | ofs + VMB_LEN + (len*VMB_DATAHOLDER) - 1)) |
| 1376 | return FALSE; |
| 1377 | |
| 1378 | /* validate that each element is valid */ |
| 1379 | for ( ; len != 0 ; p += VMB_DATAHOLDER, --len) |
| 1380 | { |
| 1381 | if (*p < 0 || *p >= (int)VM_FIRST_INVALID_TYPE) |
| 1382 | return FALSE; |
| 1383 | } |
| 1384 | |
| 1385 | /* success */ |
| 1386 | return TRUE; |
| 1387 | } |
| 1388 | |
| 1389 | /* discard items pushed on the stack for gc protection */ |
| 1390 | void discard() |
| 1391 | { |
| 1392 | VMGLOB_PTR(vmg); |
| 1393 | G_stk->discard(gc_cnt); |
| 1394 | } |
| 1395 | |
| 1396 | protected: |
| 1397 | /* number of objects we've pushed for gc protection */ |
| 1398 | int gc_cnt; |
| 1399 | |
| 1400 | /* VM globals */ |
| 1401 | vm_globals *vmg; |
| 1402 | }; |
| 1403 | |
| 1404 | |
| 1405 | /* ------------------------------------------------------------------------ */ |
| 1406 | /* |
| 1407 | * Dynamic compiler results object |
| 1408 | */ |
| 1409 | |
| 1410 | /* throw an error based on the results */ |
| 1411 | void CVmDynCompResults::throw_error(VMG0_) |
| 1412 | { |
| 1413 | /* push the message text as the exception constructor argument */ |
| 1414 | if (msgbuf != 0) |
| 1415 | { |
| 1416 | /* push the message as a string object */ |
| 1417 | G_stk->push()->set_obj(CVmObjString::create( |
| 1418 | vmg_ FALSE, msgbuf, strlen(msgbuf))); |
| 1419 | |
| 1420 | /* done with the message buffer */ |
| 1421 | free_msgbuf(); |
| 1422 | } |
| 1423 | else |
| 1424 | { |
| 1425 | /* no message - push a nil argument */ |
| 1426 | G_stk->push()->set_nil(); |
| 1427 | } |
| 1428 | |
| 1429 | /* throw the error */ |
| 1430 | G_interpreter->throw_new_class(vmg_ G_predef->compiler_exc, 1, |
| 1431 | "code compilation failed"); |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | /* ------------------------------------------------------------------------ */ |
| 1436 | /* |
| 1437 | * Dynamic Compiler Interface |
| 1438 | */ |
| 1439 | |
| 1440 | |
| 1441 | /* |
| 1442 | * Construction |
| 1443 | */ |
| 1444 | CVmDynamicCompiler::CVmDynamicCompiler(VMG0_) |
| 1445 | { |
| 1446 | /* create our host interface */ |
| 1447 | hostifc_ = new CTcHostIfcDynComp(); |
| 1448 | |
| 1449 | /* initialize the compiler */ |
| 1450 | CTcMain::init(hostifc_, G_host_ifc->get_sys_res_loader(), "utf8"); |
| 1451 | |
| 1452 | /* |
| 1453 | * set up the compiler size globals from the corresponding load image |
| 1454 | * size records, so that dynamically generated byte code conforms to |
| 1455 | * the image file settings |
| 1456 | */ |
| 1457 | G_sizes.mhdr = G_interpreter->get_funchdr_size(); |
| 1458 | G_sizes.exc_entry = G_exc_entry_size; |
| 1459 | G_sizes.dbg_hdr = G_dbg_hdr_size; |
| 1460 | G_sizes.dbg_line = G_line_entry_size; |
| 1461 | G_sizes.lcl_hdr = G_dbg_lclsym_hdr_size; |
| 1462 | G_sizes.dbg_fmt_vsn = G_dbg_fmt_vsn; |
| 1463 | G_sizes.dbg_frame = G_dbg_frame_size; |
| 1464 | |
| 1465 | /* point the compiler to the loaded metaclass table */ |
| 1466 | G_metaclass_tab = G_meta_table; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * Destruction |
| 1471 | */ |
| 1472 | CVmDynamicCompiler::~CVmDynamicCompiler() |
| 1473 | { |
| 1474 | /* terminate the compiler */ |
| 1475 | CTcMain::terminate(); |
| 1476 | |
| 1477 | /* delete our host interface */ |
| 1478 | delete hostifc_; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Get or create the global singleton instance. |
| 1483 | */ |
| 1484 | CVmDynamicCompiler *CVmDynamicCompiler::get(VMG0_) |
| 1485 | { |
| 1486 | /* if there's no compiler object, create one */ |
| 1487 | if (G_dyncomp == 0) |
| 1488 | G_dyncomp = new CVmDynamicCompiler(vmg0_); |
| 1489 | |
| 1490 | /* return the instance */ |
| 1491 | return G_dyncomp; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Compile source code |
| 1496 | */ |
| 1497 | vm_obj_id_t CVmDynamicCompiler::compile( |
| 1498 | VMG_ int in_root_set, |
| 1499 | vm_obj_id_t globals, vm_obj_id_t locals, vm_obj_id_t macros, |
| 1500 | const vm_val_t *srcval, const char *src, size_t srclen, |
| 1501 | CVmDynCompMode mode, CVmDynCompDebug *dbg, CVmDynCompResults *results) |
| 1502 | { |
| 1503 | vm_obj_id_t coid = 0; |
| 1504 | CTcPrsSymtab *old_global_symtab; |
| 1505 | CTcMacroTable *old_macros; |
| 1506 | CVmDynFuncMacros *new_macros = 0; |
| 1507 | int pushcnt = 0; |
| 1508 | CTcPrsNode *node = 0; |
| 1509 | int frame_has_self = FALSE; |
| 1510 | CVmObjFrameDesc *framedesc = 0; |
| 1511 | CVmObjFrameRef *frameref = 0; |
| 1512 | |
| 1513 | /* |
| 1514 | * save the parser memory pool state, so we can reset it when we're |
| 1515 | * done (this allows us to discard any parser memory we allocate while |
| 1516 | * we're working - we only need it while compiling, and can discard it |
| 1517 | * when we're done) |
| 1518 | */ |
| 1519 | tcprsmem_state_t prsmem_state; |
| 1520 | G_prsmem->save_state(&prsmem_state); |
| 1521 | |
| 1522 | /* |
| 1523 | * Set the local symbol table context. If the caller provided a |
| 1524 | * debugger context, use the debugger symbol table; otherwise there's |
| 1525 | * no local symbol context. |
| 1526 | */ |
| 1527 | CTcPrsDbgSymtab *old_symtab = G_prs->set_debug_symtab( |
| 1528 | dbg != 0 ? dbg->symtab : 0); |
| 1529 | |
| 1530 | /* don't allow #define or other directives for dynamic compilation */ |
| 1531 | G_tok->enable_pp(FALSE); |
| 1532 | |
| 1533 | /* enable the special debugger syntax if it's for the debugger */ |
| 1534 | G_prs->set_debug_expr(dbg != 0); |
| 1535 | |
| 1536 | /* set up a dynamic VM interface for the compiler */ |
| 1537 | CVmDynFuncVMIfc vmifc Pvmg0_P; |
| 1538 | G_vmifc = &vmifc; |
| 1539 | |
| 1540 | /* |
| 1541 | * If there's no debugger context, set up the global symbol table and |
| 1542 | * the new macro table. |
| 1543 | */ |
| 1544 | if (dbg == 0) |
| 1545 | { |
| 1546 | /* install a new symbol table; remember the old one to restore */ |
| 1547 | old_global_symtab = G_prs->set_global_symtab( |
| 1548 | new CVmDynFuncSymtab(vmg_ globals, locals)); |
| 1549 | |
| 1550 | /* install a new macro table; remember the old one to restore */ |
| 1551 | old_macros = G_tok->set_defines_table( |
| 1552 | new_macros = new CVmDynFuncMacros(vmg_ macros)); |
| 1553 | } |
| 1554 | |
| 1555 | /* presume no error will occur */ |
| 1556 | int errcode = 0, errflag = FALSE; |
| 1557 | results->err = 0; |
| 1558 | results->free_msgbuf(); |
| 1559 | |
| 1560 | /* presume we won't generate a statement node */ |
| 1561 | CTPNCodeBody *cb = 0; |
| 1562 | |
| 1563 | /* catch any errors that occur during parsing or code generation */ |
| 1564 | err_try |
| 1565 | { |
| 1566 | /* |
| 1567 | * If there's a local stack frame object, note if it has a method |
| 1568 | * context ('self', etc - it's sufficient to check that 'self' is |
| 1569 | * not nil). If so, code defined with 'function()' uses the method |
| 1570 | * context of the enclosing stack frame, in analogy to an anonymous |
| 1571 | * function using its enclosing lexical scope's method context. If |
| 1572 | * we have multiple stack frames, consider only the innermost. |
| 1573 | */ |
| 1574 | if (locals != VM_INVALID_OBJ) |
| 1575 | { |
| 1576 | /* presume we have just a single frame object */ |
| 1577 | vm_obj_id_t frame = locals; |
| 1578 | |
| 1579 | /* |
| 1580 | * if it's not in fact a stack frame object, and it's |
| 1581 | * list-like, it's a collection of frames ordered from |
| 1582 | * innermost to outermost: so retrieve the first element to get |
| 1583 | * the innermost frame |
| 1584 | */ |
| 1585 | CVmObject *localp = vm_objp(vmg_ locals); |
| 1586 | if (!CVmObjFrameDesc::is_framedesc_obj(vmg_ locals) |
| 1587 | && localp->is_listlike(vmg_ locals) |
| 1588 | && localp->ll_length(vmg_ locals) > 0) |
| 1589 | { |
| 1590 | /* |
| 1591 | * it's a list - retrieve its first element; if that's an |
| 1592 | * object, use it as the frame object |
| 1593 | */ |
| 1594 | vm_val_t ele, v1; |
| 1595 | v1.set_int(1); |
| 1596 | localp->index_val_ov(vmg_ &ele, locals, &v1); |
| 1597 | if (ele.typ == VM_OBJ) |
| 1598 | frame = ele.val.obj; |
| 1599 | } |
| 1600 | |
| 1601 | /* if we now have a frame object, check its 'self' */ |
| 1602 | if (CVmObjFrameDesc::is_framedesc_obj(vmg_ frame)) |
| 1603 | { |
| 1604 | /* get the frame object, suitably cast */ |
| 1605 | framedesc = (CVmObjFrameDesc *)vm_objp(vmg_ frame); |
| 1606 | frameref = framedesc->get_frame_ref(vmg0_); |
| 1607 | |
| 1608 | /* get 'self' from the frame */ |
| 1609 | vm_val_t self; |
| 1610 | frameref->get_self(vmg_ &self); |
| 1611 | |
| 1612 | /* if that's not nil, we have a method context */ |
| 1613 | frame_has_self = (self.typ != VM_NIL); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | /* reset the compiler error counters for the new parse */ |
| 1618 | G_tcmain->reset_error_counts(); |
| 1619 | |
| 1620 | /* clear any old error messages in our host interface */ |
| 1621 | hostifc_->reset_messages(); |
| 1622 | |
| 1623 | /* point the tokenizer to the source string */ |
| 1624 | G_tok->set_source_buf(src, srclen); |
| 1625 | |
| 1626 | /* read the first token */ |
| 1627 | G_tok->next(); |
| 1628 | |
| 1629 | /* parse the source code */ |
| 1630 | switch (mode) |
| 1631 | { |
| 1632 | case DCModeExpression: |
| 1633 | /* parse an expression */ |
| 1634 | G_prs->set_local_symtab(0); |
| 1635 | if ((node = G_prs->parse_expr()) != 0) |
| 1636 | { |
| 1637 | /* check some extra information for the debugger */ |
| 1638 | // $$$ does this need to be deferred until after folding |
| 1639 | // constants and adjust_for_dyn??? |
| 1640 | if (dbg != 0 && node != 0) |
| 1641 | { |
| 1642 | /* the debugger wants to know if it's an lvalue */ |
| 1643 | dbg->is_lval = node->check_lvalue_resolved( |
| 1644 | G_prs->get_global_symtab()); |
| 1645 | } |
| 1646 | |
| 1647 | /* wrap it in a 'return' and a code body */ |
| 1648 | cb = new CTPNCodeBody(G_prs->get_global_symtab(), 0, |
| 1649 | new CTPNStmReturn(node), |
| 1650 | 0, 0, FALSE, FALSE, 0, 0, |
| 1651 | dbg != 0 ? dbg->self_valid : FALSE, 0); |
| 1652 | |
| 1653 | /* mark it as a dynamic function */ |
| 1654 | cb->set_dyn_func(TRUE); |
| 1655 | } |
| 1656 | break; |
| 1657 | |
| 1658 | case DCModeAuto: |
| 1659 | /* |
| 1660 | * Auto-sensing mode - parse a function or expression based on |
| 1661 | * what the string looks like. |
| 1662 | * |
| 1663 | * If the first token is 'function', we have a function |
| 1664 | * definition of the form "function(args) { body }". This is |
| 1665 | * compiled as an ordinary function with no name. |
| 1666 | * |
| 1667 | * If the first token is 'method', we have a method definition |
| 1668 | * of the form "method(args) { body }". This is compiled as a |
| 1669 | * floating method definition with no name. |
| 1670 | * |
| 1671 | * Otherwise, we have an expression. We compile this as an |
| 1672 | * expression, then wrap it in a "return" statement and a |
| 1673 | * function code body with zero arguments. |
| 1674 | */ |
| 1675 | if (G_tok->cur() == TOKT_FUNCTION) |
| 1676 | { |
| 1677 | /* skip the 'function' token */ |
| 1678 | G_tok->next(); |
| 1679 | |
| 1680 | /* parse the code body */ |
| 1681 | cb = G_prs->parse_code_body(FALSE, FALSE, frame_has_self, |
| 1682 | 0, 0, 0, 0, 0, 0, &errflag, 0, |
| 1683 | TCPRS_CB_NORMAL, 0, 0, 0, 0); |
| 1684 | |
| 1685 | /* mark it as a dynamic function */ |
| 1686 | cb->set_dyn_func(TRUE); |
| 1687 | } |
| 1688 | else if (G_tok->cur() == TOKT_METHOD) |
| 1689 | { |
| 1690 | /* skip the 'method' token */ |
| 1691 | G_tok->next(); |
| 1692 | |
| 1693 | /* parse the code body */ |
| 1694 | cb = G_prs->parse_code_body(FALSE, FALSE, TRUE, |
| 1695 | 0, 0, 0, 0, 0, 0, &errflag, 0, |
| 1696 | TCPRS_CB_NORMAL, 0, 0, 0, 0); |
| 1697 | |
| 1698 | /* mark it as a dynamic method */ |
| 1699 | cb->set_dyn_method(TRUE); |
| 1700 | } |
| 1701 | else |
| 1702 | { |
| 1703 | /* parse an expression */ |
| 1704 | G_prs->set_local_symtab(0); |
| 1705 | if ((node = G_prs->parse_expr()) != 0) |
| 1706 | { |
| 1707 | /* wrap it in a 'return' an a zero-argument code body */ |
| 1708 | cb = new CTPNCodeBody(G_prs->get_global_symtab(), 0, |
| 1709 | new CTPNStmReturn(node), |
| 1710 | 0, 0, FALSE, FALSE, 0, 0, |
| 1711 | dbg != 0 ? dbg->self_valid : FALSE, |
| 1712 | 0); |
| 1713 | |
| 1714 | /* mark it as a dynamic function */ |
| 1715 | cb->set_dyn_func(TRUE); |
| 1716 | } |
| 1717 | } |
| 1718 | break; |
| 1719 | |
| 1720 | case DCModeGramAlt: |
| 1721 | /* parse a grammar alternative list */ |
| 1722 | { |
| 1723 | /* set up a dummy symbol to represent the match object */ |
| 1724 | CTcSymObj *gram_obj = new CTcSymObj( |
| 1725 | ".anon", 5, FALSE, VM_INVALID_OBJ, |
| 1726 | FALSE, TC_META_TADSOBJ, 0); |
| 1727 | |
| 1728 | /* set up a production to hold the list */ |
| 1729 | CTcGramProdEntry *prod = |
| 1730 | new (G_prsmem) CTcGramProdEntry(gram_obj); |
| 1731 | |
| 1732 | /* parse the alternative list */ |
| 1733 | CTcGramPropArrows arrows; |
| 1734 | CTcGramAltFuncsDyn funcs(G_prs->get_global_symtab()); |
| 1735 | G_prs->parse_gram_alts( |
| 1736 | &errflag, gram_obj, prod, &arrows, &funcs); |
| 1737 | |
| 1738 | /* |
| 1739 | * if the parse was successful, send the parsed data back |
| 1740 | * to the caller |
| 1741 | */ |
| 1742 | if (!errflag && G_tcmain->get_error_count() == 0) |
| 1743 | results->save_grammar(vmg_ prod->get_alt_head(), &arrows); |
| 1744 | } |
| 1745 | break; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * if we didn't get a parse node out of the deal, or any compiler |
| 1750 | * errors occurred, fail |
| 1751 | */ |
| 1752 | if (cb == 0 || errflag || G_tcmain->get_error_count() != 0) |
| 1753 | goto compilation_done; |
| 1754 | |
| 1755 | /* assign an object ID for CodeBody for the main code we parsed */ |
| 1756 | coid = vm_new_id(vmg_ in_root_set, TRUE, FALSE); |
| 1757 | cb->set_dyn_obj_id(coid); |
| 1758 | |
| 1759 | /* |
| 1760 | * create a dummy object for it - we'll replace it with a real |
| 1761 | * object when actually generating the code |
| 1762 | */ |
| 1763 | new (vmg_ coid) CVmDynamicFunc(); |
| 1764 | |
| 1765 | /* save it on the stack for GC protection */ |
| 1766 | G_stk->push_check()->set_obj(coid); |
| 1767 | ++pushcnt; |
| 1768 | |
| 1769 | /* |
| 1770 | * Allocate object IDs for the nested code bodies. Each nested |
| 1771 | * code body requires its own separate CodeBody object. Nested |
| 1772 | * code bodies are typically referenced from the main code body, |
| 1773 | * and might cross-reference one another, so in order to generate |
| 1774 | * code for any of the code bodies, we have to pre-assign an object |
| 1775 | * ID to *all* code bodies first. |
| 1776 | */ |
| 1777 | for (CTPNStmTop *ts = G_prs->get_first_nested_stm() ; ts != 0 ; |
| 1778 | ts = ts->get_next_stm_top()) |
| 1779 | { |
| 1780 | /* allocate and assign the object ID */ |
| 1781 | vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE); |
| 1782 | new (vmg_ id) CVmDynamicFunc(); |
| 1783 | ts->set_dyn_obj_id(id); |
| 1784 | |
| 1785 | /* save it on the stack for GC protection */ |
| 1786 | G_stk->push_check()->set_obj(id); |
| 1787 | ++pushcnt; |
| 1788 | } |
| 1789 | |
| 1790 | /* generate the code for the main object */ |
| 1791 | if (!gen_code_body(vmg_ cb, srcval, dbg)) |
| 1792 | goto compilation_done; |
| 1793 | |
| 1794 | /* |
| 1795 | * If necessary, store the method context in the object. This is |
| 1796 | * required if this is defined with 'function' rather than |
| 1797 | * 'method', and it references 'self' or other method context |
| 1798 | * variables, and there's a local frame to use for the method |
| 1799 | * context. |
| 1800 | */ |
| 1801 | if (frame_has_self |
| 1802 | && cb->is_dyn_func() |
| 1803 | && (cb->self_referenced() || cb->full_method_ctx_referenced())) |
| 1804 | { |
| 1805 | /* |
| 1806 | * We need the method context. The context is simply the |
| 1807 | * 'self' object if the rest of the context wasn't referenced, |
| 1808 | * otherwise it's the context object suitable for the LOADCTX |
| 1809 | * opcode. |
| 1810 | */ |
| 1811 | vm_val_t mctx; |
| 1812 | if (cb->full_method_ctx_referenced()) |
| 1813 | { |
| 1814 | /* we need the full method context object for LOADCTX */ |
| 1815 | frameref->create_loadctx_obj(vmg_ &mctx); |
| 1816 | } |
| 1817 | else |
| 1818 | { |
| 1819 | /* the context is simply the 'self' object */ |
| 1820 | frameref->get_self(vmg_ &mctx); |
| 1821 | } |
| 1822 | |
| 1823 | /* save the method context in the DynamicFunc */ |
| 1824 | ((CVmDynamicFunc *)vm_objp(vmg_ coid))->set_method_ctx(&mctx); |
| 1825 | } |
| 1826 | |
| 1827 | /* generate each nested statement code body */ |
| 1828 | for (CTPNStmTop *ts = G_prs->get_first_nested_stm() ; ts != 0 ; |
| 1829 | ts = ts->get_next_stm_top()) |
| 1830 | { |
| 1831 | if (!gen_code_body(vmg_ ts, 0, dbg)) |
| 1832 | goto compilation_done; |
| 1833 | } |
| 1834 | |
| 1835 | compilation_done: ; |
| 1836 | } |
| 1837 | err_catch(exc) |
| 1838 | { |
| 1839 | /* note the error code */ |
| 1840 | errcode = exc->get_error_code(); |
| 1841 | |
| 1842 | /* if there were no logged errors, use the error from the exception */ |
| 1843 | if (G_tcmain->get_error_count() == 0) |
| 1844 | { |
| 1845 | /* delete any old message text */ |
| 1846 | results->free_msgbuf(); |
| 1847 | |
| 1848 | /* format the message into the result object */ |
| 1849 | const char *msg = tcerr_get_msg(errcode, FALSE); |
| 1850 | results->msgbuf = err_format_msg(msg, exc); |
| 1851 | } |
| 1852 | |
| 1853 | /* we don't have a valid object */ |
| 1854 | coid = VM_INVALID_OBJ; |
| 1855 | } |
| 1856 | err_end; |
| 1857 | |
| 1858 | /* if a compilation error occurred, return failure */ |
| 1859 | if (errcode != 0 || errflag) |
| 1860 | { |
| 1861 | /* parsing failed - stop now */ |
| 1862 | coid = VM_INVALID_OBJ; |
| 1863 | goto done; |
| 1864 | } |
| 1865 | else if (G_tcmain->get_error_count() != 0 || coid == VM_INVALID_OBJ) |
| 1866 | { |
| 1867 | /* parse errors were reported - retrieve the first and stop */ |
| 1868 | errcode = G_tcmain->get_first_error(); |
| 1869 | coid = VM_INVALID_OBJ; |
| 1870 | goto done; |
| 1871 | } |
| 1872 | |
| 1873 | done: |
| 1874 | /* restore the original symbol table and macros in the parser */ |
| 1875 | G_prs->set_debug_symtab(old_symtab); |
| 1876 | |
| 1877 | /* reset the code generator streams */ |
| 1878 | G_ds->reset(); |
| 1879 | G_cs->reset(); |
| 1880 | G_os->reset(); |
| 1881 | |
| 1882 | /* restore the old global symbol and macro tables */ |
| 1883 | if (dbg == 0) |
| 1884 | { |
| 1885 | G_prs->set_global_symtab(old_global_symtab); |
| 1886 | G_tok->set_defines_table(old_macros); |
| 1887 | } |
| 1888 | |
| 1889 | /* delete the macro table if we created one */ |
| 1890 | if (new_macros != 0) |
| 1891 | delete new_macros; |
| 1892 | |
| 1893 | /* reset the parser and tokenizer */ |
| 1894 | G_prs->reset(); |
| 1895 | G_tok->reset(); |
| 1896 | |
| 1897 | /* we're done with the compiler's working state */ |
| 1898 | G_prsmem->reset(&prsmem_state); |
| 1899 | |
| 1900 | /* |
| 1901 | * if there were logged compiler errors, and we haven't already |
| 1902 | * returned an exception message, return the captured compiler error |
| 1903 | * messages from the host interface |
| 1904 | */ |
| 1905 | if (G_tcmain->get_error_count() != 0 && results->msgbuf == 0) |
| 1906 | results->msgbuf = lib_copy_str(hostifc_->get_error_msg()); |
| 1907 | |
| 1908 | /* return the error code result */ |
| 1909 | results->err = errcode; |
| 1910 | |
| 1911 | /* discard our GC protection, plus the vmifc's */ |
| 1912 | G_stk->discard(pushcnt); |
| 1913 | vmifc.discard(); |
| 1914 | |
| 1915 | /* return the DynamicFunc we created, if any */ |
| 1916 | return coid; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * Generate code for a parsed code body. Parsing a single block of source |
| 1921 | * code might yield multiple parsed code bodies, because anonymous |
| 1922 | * functions and other nested structures require separate top-level code |
| 1923 | * bodies. |
| 1924 | */ |
| 1925 | int CVmDynamicCompiler::gen_code_body( |
| 1926 | VMG_ CTPNStmTop *node, const vm_val_t *srcval, CVmDynCompDebug *dbg) |
| 1927 | { |
| 1928 | int i; |
| 1929 | CTcIdFixup *fixup; |
| 1930 | |
| 1931 | /* clear the object fixup list - we want just this object's fixups */ |
| 1932 | G_objfixup = 0; |
| 1933 | |
| 1934 | /* our code will start at the current code stream write pointer */ |
| 1935 | ulong start_ofs = G_cs->get_ofs(); |
| 1936 | |
| 1937 | /* fold constants */ |
| 1938 | if (node->fold_constants(G_prs->get_global_symtab()) == 0) |
| 1939 | return FALSE; |
| 1940 | |
| 1941 | /* |
| 1942 | * Make adjustments for dynamic compilation. If the caller provided |
| 1943 | * debugger evaluation options, use the dynamic adjustment options |
| 1944 | * specified there; otherwise use defaults. |
| 1945 | */ |
| 1946 | tcpn_dyncomp_info di; |
| 1947 | if (node->adjust_for_dyn(dbg != 0 ? &dbg->di : &di) == 0) |
| 1948 | return FALSE; |
| 1949 | |
| 1950 | /* |
| 1951 | * Set the code generation mode. If we have debugger options, set |
| 1952 | * debug mode; otherwise set dynamic compilation mode. |
| 1953 | */ |
| 1954 | if (dbg != 0) |
| 1955 | G_cg->set_debug_eval(dbg->di.speculative, |
| 1956 | dbg->di.stack_level); |
| 1957 | else |
| 1958 | G_cg->set_dyn_eval(); |
| 1959 | |
| 1960 | /* |
| 1961 | * Keep object fixups - we need these to find object references in the |
| 1962 | * generated bytecode. We don't need property or enum fixups, since |
| 1963 | * we're building against a loaded image file with all of those types |
| 1964 | * already resolved. |
| 1965 | */ |
| 1966 | G_keep_objfixups = TRUE; |
| 1967 | |
| 1968 | /* make the global symbol table active for code generation */ |
| 1969 | G_cs->set_symtab(G_prs->get_global_symtab()); |
| 1970 | |
| 1971 | /* generate the code */ |
| 1972 | node->gen_code(FALSE, FALSE); |
| 1973 | |
| 1974 | /* if any errors occurred during code generation, return failure */ |
| 1975 | if (G_tcmain->get_error_count() != 0) |
| 1976 | return FALSE; |
| 1977 | |
| 1978 | /* |
| 1979 | * Go through the object fixup list and count up references to objects |
| 1980 | * that aren't in the root set. We need to keep a list of these |
| 1981 | * references in the generated code object, because the code object |
| 1982 | * needs to manage them like any other object manages its live |
| 1983 | * references: mark them during garbage collection, fix them up during |
| 1984 | * save/restore, etc. |
| 1985 | */ |
| 1986 | int refcnt = 0; |
| 1987 | for (fixup = G_objfixup ; fixup != 0 ; fixup = fixup->nxt_) |
| 1988 | { |
| 1989 | /* if it refers to a non-root-set object, count it */ |
| 1990 | if (!G_obj_table->is_obj_in_root_set((vm_obj_id_t)fixup->id_)) |
| 1991 | ++refcnt; |
| 1992 | } |
| 1993 | |
| 1994 | /* figure the bytecode length for allocating the DynamicFunc instance */ |
| 1995 | size_t bytecode_rem = (size_t)(G_cs->get_ofs() - start_ofs); |
| 1996 | |
| 1997 | /* the caller already allocated our object ID - retrieve it */ |
| 1998 | vm_obj_id_t coid = node->get_dyn_obj_id(); |
| 1999 | |
| 2000 | /* create the C++ object */ |
| 2001 | CVmDynamicFunc *co = new (vmg_ coid) CVmDynamicFunc( |
| 2002 | vmg_ coid, srcval, bytecode_rem, refcnt); |
| 2003 | |
| 2004 | /* save the object reference list */ |
| 2005 | for (i = 0, fixup = G_objfixup ; fixup != 0 ; fixup = fixup->nxt_) |
| 2006 | { |
| 2007 | /* if it refers to a non-root-set object, save it */ |
| 2008 | if (!G_obj_table->is_obj_in_root_set((vm_obj_id_t)fixup->id_)) |
| 2009 | co->get_ext()->obj_refs[i++] = fixup->ofs_ - start_ofs; |
| 2010 | } |
| 2011 | |
| 2012 | /* copy the code */ |
| 2013 | char *bc = co->get_ext()->get_bytecode_ptr(); |
| 2014 | for (size_t blkofs = 0 ; bytecode_rem != 0 ; ) |
| 2015 | { |
| 2016 | /* get the next block */ |
| 2017 | ulong blklen; |
| 2018 | const char *blkp = G_cs->get_block_ptr( |
| 2019 | start_ofs + blkofs, bytecode_rem, &blklen); |
| 2020 | |
| 2021 | /* copy the data */ |
| 2022 | memcpy(bc + blkofs, blkp, blklen); |
| 2023 | |
| 2024 | /* advance past the copied data */ |
| 2025 | blkofs += blklen; |
| 2026 | bytecode_rem -= blklen; |
| 2027 | } |
| 2028 | |
| 2029 | /* success */ |
| 2030 | return TRUE; |
| 2031 | } |
| 2032 |