| 1 | /* $Header$ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmrunsym.h - run-time global symbol table |
| 12 | Function |
| 13 | Defines a symbol table structure that allows us to convey the global |
| 14 | symbols from the compiler or image loader to the interpreter. |
| 15 | |
| 16 | When the compiler runs pre-initialization, or when the image loader finds |
| 17 | debug symbols in a program, the interpreter builds a LookupTable object |
| 18 | containing the global symbol table. This provides a "reflection" mechanism |
| 19 | that lets the running program inspect its own symbol table. |
| 20 | |
| 21 | Because we have two very different ways of getting the symbol information |
| 22 | -- from the compiler, or from the image file's debug records -- we define |
| 23 | this class as an intermediary that provides a common mechanism for |
| 24 | conveying the information to the interpreter. Note that, when the |
| 25 | information is coming from the compiler, there's usually not a debug symbol |
| 26 | table in the image file, because the compiler usually only runs |
| 27 | pre-initialization on programs compiled for release (i.e., with no debug |
| 28 | information), so we can't count on debug records in the image file as a |
| 29 | common conveyance mechanism. |
| 30 | |
| 31 | This is a very simple linked list storage class, because we have no need |
| 32 | to search this symbol table. The only things we do with this type of |
| 33 | symbol table are to load it with all of the symbols from the compiler or |
| 34 | image file's debug records, and then enumerate all of our symbols to build |
| 35 | a run-time LookupTable. |
| 36 | |
| 37 | One final note: we could conceivably avoid having this other data |
| 38 | representation by having the compiler build a LookupTable directly and |
| 39 | storing it in the image file. However, this would put the LookupTable into |
| 40 | the root set, so it could never be deleted. By building the LookupTable |
| 41 | dynamically during pre-initialization, the table will be automatically |
| 42 | garbage collected before the image file is finalized if the program doesn't |
| 43 | retain a reference to it in a location accessible from the root set. This |
| 44 | allows the program to control the presence of this extra information in a |
| 45 | very natural way. |
| 46 | Notes |
| 47 | |
| 48 | Modified |
| 49 | 02/17/01 MJRoberts - Creation |
| 50 | */ |
| 51 | |
| 52 | #ifndef VMRUNSYM_H |
| 53 | #define VMRUNSYM_H |
| 54 | |
| 55 | #include "vmtype.h" |
| 56 | |
| 57 | class CVmRuntimeSymbols |
| 58 | { |
| 59 | public: |
| 60 | /* construct */ |
| 61 | CVmRuntimeSymbols() |
| 62 | { |
| 63 | /* nothing in the list yet - empty the list and clear the count */ |
| 64 | head_ = tail_ = 0; |
| 65 | cnt_ = 0; |
| 66 | } |
| 67 | |
| 68 | /* delete */ |
| 69 | ~CVmRuntimeSymbols(); |
| 70 | |
| 71 | /* add a symbol */ |
| 72 | void add_sym(const char *sym, size_t len, const vm_val_t *val); |
| 73 | |
| 74 | /* add a macro definition */ |
| 75 | struct vm_runtime_sym *add_macro(const char *sym, size_t len, |
| 76 | size_t explen, unsigned int flags, |
| 77 | int argc, size_t arglen); |
| 78 | |
| 79 | /* get the first symbol */ |
| 80 | struct vm_runtime_sym *get_head() const { return head_; } |
| 81 | |
| 82 | /* get the number of symbols */ |
| 83 | size_t get_sym_count() const { return cnt_; } |
| 84 | |
| 85 | /* find an object name - returns null if the object isn't found */ |
| 86 | const char *find_obj_name(VMG_ vm_obj_id_t obj, size_t *name_len) const |
| 87 | { |
| 88 | vm_val_t val; |
| 89 | |
| 90 | /* set up an object value */ |
| 91 | val.set_obj(obj); |
| 92 | |
| 93 | /* find the value */ |
| 94 | return find_val_name(vmg_ &val, name_len); |
| 95 | } |
| 96 | |
| 97 | /* find a property name - returns null if not found */ |
| 98 | const char *find_prop_name(VMG_ vm_prop_id_t prop, size_t *name_len) const |
| 99 | { |
| 100 | vm_val_t val; |
| 101 | |
| 102 | /* set up a property ID value */ |
| 103 | val.set_propid(prop); |
| 104 | |
| 105 | /* find the value */ |
| 106 | return find_val_name(vmg_ &val, name_len); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Find the name for a value. Fills in the length pointer with the |
| 111 | * length of the return string, which is not null-terminated. |
| 112 | * Returns null if no such value is present in the table. |
| 113 | */ |
| 114 | const char *find_val_name(VMG_ const vm_val_t *val, |
| 115 | size_t *name_len) const; |
| 116 | |
| 117 | protected: |
| 118 | /* head and tail of symbol list */ |
| 119 | struct vm_runtime_sym *head_; |
| 120 | struct vm_runtime_sym *tail_; |
| 121 | |
| 122 | /* number of symbols in the list */ |
| 123 | size_t cnt_; |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * A Symbol |
| 128 | */ |
| 129 | struct vm_runtime_sym |
| 130 | { |
| 131 | /* next symbol in the list */ |
| 132 | vm_runtime_sym *nxt; |
| 133 | |
| 134 | /* the symbol name */ |
| 135 | char *sym; |
| 136 | |
| 137 | /* the length of the name */ |
| 138 | size_t len; |
| 139 | |
| 140 | /* the value of the symbol */ |
| 141 | vm_val_t val; |
| 142 | |
| 143 | /* for a macro, the flags from the MACR record */ |
| 144 | unsigned int macro_flags; |
| 145 | |
| 146 | /* for a macro, the expansion of the macro */ |
| 147 | char *macro_expansion; |
| 148 | size_t macro_exp_len; |
| 149 | |
| 150 | /* for a macro, the number of formal parameters */ |
| 151 | int macro_argc; |
| 152 | |
| 153 | /* for a macro, the argument list */ |
| 154 | char **macro_args; |
| 155 | |
| 156 | /* |
| 157 | * Commit storage for a macro argument. Arguments MUST be stored |
| 158 | * sequentially, starting from number 0. The caller must |
| 159 | * null-terminate each name, AND must include the null byte in the |
| 160 | * 'len' value. |
| 161 | */ |
| 162 | void commit_macro_arg(int i, size_t len) |
| 163 | { |
| 164 | /* if this isn't the last argument, set the next allocation pointer */ |
| 165 | if (i + 1 < macro_argc) |
| 166 | macro_args[i + 1] = macro_args[i] + len; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | #endif /* VMRUNSYM_H */ |
| 171 |