Debian Codegraph @

terps/tads/tads3/tcpnbase.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/TCPNBASE.H,v 1.3 1999/07/11 00:46:53 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1999, 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 tcpn.h - Parse Node - base class
12 Function
13 Defines the target-independent base class for parse nodes
14 Notes
15 All expression parse nodes are derived from the target-specific
16 subclass of this class. The target-independent base class is
17 CTcPrsNodeBase; the target-specific class is CTcPrsNode.
18 Modified
19 05/10/99 MJRoberts - Creation
20 */
21
22 #ifndef TCPN_H
23 #define TCPN_H
24
25 #include "vmhash.h"
26
27 /* ------------------------------------------------------------------------ */
28 /*
29 * Parse Tree Allocation Object. This is a base class that can be used
30 * for tree objects that are to be allocated from the parser node pool.
31 */
32 class CTcPrsAllocObj
33 {
34 public:
35 /*
36 * Override operator new() - allocate all parse node objects out of
37 * the parse node pool.
38 */
39 void *operator new(size_t siz);
40 };
41
42
43 /* ------------------------------------------------------------------------ */
44 /*
45 * Parse Tree Expression Node - base class. As we parse an expression,
46 * we build a tree of these objects to describe the source code.
47 *
48 * This class is subclassed for each type of parsing node: each type of
49 * statement has a node type, some statements have helper node types for
50 * parts of statements, and each expression operator has a node type.
51 * These subclasses contain the information specific to the type of
52 * parsing construct represented.
53 *
54 * Each parsing subclass is then further subclassed for each target
55 * architecture. This final subclass contains the code generator for
56 * the node in the target architecture.
57 *
58 * The target-independent base version of each subclass is called
59 * CTPNXxxBase. The target-specific subclass derived from this base
60 * class is CTPNXxx. For example, the final subclass for constant
61 * nodes, which is derived from the target-independent base class
62 * CTPNConstBase, is CTPNConst. (Note that each target uses the same
63 * name for the final subclass, so we can only link one target
64 * architecture into a given build of the compiler. Each additional
65 * target requires a separate compiler executable with the appropriate
66 * CTPNConst classes linked in.)
67 */
68 class CTcPrsNodeBase: public CTcPrsAllocObj
69 {
70 public:
71 /*
72 * Generate code for the expression for the target architecture.
73 * This method is defined only by the final target-specific
74 * subclasses.
75 *
76 * This method is used to generate code to evaluate the expression
77 * as an rvalue.
78 *
79 * If 'discard' is true, it indicates that any value yielded by the
80 * expression will not be used, in which case the generated code
81 * need not leave the result of the expression on the stack. We can
82 * generate code more efficiently for certain types of expressions
83 * when we know that we're evaluating them only for side effects.
84 * For example, an assignment expression has a result value, but
85 * this value need not be pushed onto the stack if it will simply be
86 * discarded. Also, an operator like "+" that has no side effects
87 * of its own can merely evaluate its operands for their side
88 * effects, but need not compute its own result if that result would
89 * simply be discarded.
90 *
91 * If 'for_condition' is true, it indicates that the result of the
92 * expression will be used directly for a conditional of some kind
93 * (for a "?:" operator, an "if" statement, a "while" statement, or
94 * the like). In some cases, we can avoid extra conversions to some
95 * values when they're going to be used directly for a comparison;
96 * for example, the "&&" operator must return a true/nil value, but
97 * the code generator may be able to avoid the extra conversion when
98 * the value will be used for an "if" statement's conditional value.
99 */
100 virtual void gen_code(int discard, int for_condition) = 0;
101
102 /*
103 * Get the constant value of the parse node, if available. Most
104 * parse nodes have no constant value, so by default this returns
105 * null. Only constant parse nodes can provide a constant value, so
106 * they should override this.
107 */
108 virtual class CTcConstVal *get_const_val() { return 0; }
109
110 /* determine if the node has a constant value */
111 int is_const() { return get_const_val() != 0; }
112
113 /* determine if I have a given constant integer value */
114 int is_const_int(int val)
115 {
116 return (is_const()
117 && get_const_val()->get_type() == TC_CVT_INT
118 && get_const_val()->get_val_int() == val);
119 }
120
121 /*
122 * Set the constant value of the parse node from that of another
123 * node. The caller must already have checked that this node and
124 * the value being assigned are both valid constant values.
125 */
126 void set_const_val(class CTcPrsNode *src)
127 {
128 /* set my constant value from the source's constant value */
129 get_const_val()->set(((CTcPrsNodeBase *)src)->get_const_val());
130 }
131
132 /*
133 * Is this value guaranteed to be a boolean value? Certain operators
134 * produce boolean results regardless of the operands. In particular,
135 * the comparison operators (==, !=, >, <, >=, <=) and the logical NOT
136 * operator (!) always produce boolean results.
137 */
138 virtual int is_bool() const { return FALSE; }
139
140 /*
141 * Check to see if this expression can possibly be a valid lvalue.
142 * Return true if so, false if not. This check is made before
143 * symbol resolution; when it is not certain whether or not a symbol
144 * expression can be an lvalue, assume it can be at this point. By
145 * default, we'll return false; operator nodes whose result can be
146 * used as an lvalue should override this to return true.
147 */
148 virtual int check_lvalue() const { return FALSE; }
149
150 /*
151 * Check to see if this expression is an valid lvalue, after
152 * resolving symbols in the given scope. Returns true if so, false
153 * if not.
154 */
155 virtual int check_lvalue_resolved(class CTcPrsSymtab *symtab) const
156 { return FALSE; }
157
158 /*
159 * Check to see if this expression can possibly be a valid address
160 * value, so that the address-of ("&") operator can be applied.
161 * Returns true if it is possible, false if not. The only type of
162 * expression whose address can be taken is a simple symbol. The
163 * address of a symbol can be taken only if the symbol is a function
164 * or property name, but we won't know this at parse time, so we'll
165 * indicate that any symbol is acceptable. By default, this returns
166 * false, since the address of most expressions cannot be taken.
167 */
168 virtual int has_addr() const { return FALSE; }
169
170 /*
171 * Check to see if this expression is an address expression of some
172 * kind (i.e., of class CTPNAddrBase, or of a class derived from
173 * CTPNAddrBase). Returns true if so, false if not.
174 */
175 virtual int is_addr() const { return FALSE; }
176
177 /*
178 * Determine if this node is of type double-quoted string (dstring).
179 * Returns true if so, false if not. By default, we return false.
180 */
181 virtual int is_dstring() const { return FALSE; }
182
183 /*
184 * Determine if this node contains a dstring expression. This returns
185 * true if the node is a dstring, a dstring embedding, or a comma node
186 * containing a dstring and/or a dstring embedding.
187 */
188 virtual int is_dstring_expr() const { return FALSE; }
189
190 /*
191 * Determine if this is a simple assignment operator node. Returns
192 * true if so, false if not. By default, we return false.
193 */
194 virtual int is_simple_asi() const { return FALSE; }
195
196 /*
197 * Determine if this node yields a value when evaluated. Returns
198 * true if so, false if not. When it cannot be determined at
199 * compile-time whether or not the node has a value (for example,
200 * for a call to a pointer to a function whose return type is not
201 * declared), this should indicate that a value is returned.
202 *
203 * Most nodes yield a value when executed, so we'll return true by
204 * default.
205 */
206 virtual int has_return_value() const { return TRUE; }
207
208 /*
209 * Determine if this node yields a return value when called as a
210 * function. We assume by default that it does.
211 */
212 virtual int has_return_value_on_call() const { return TRUE; }
213
214 /*
215 * Get the text of the symbol for this node, if any. If the node is
216 * not some kind of symbol node, this returns null.
217 */
218 virtual const textchar_t *get_sym_text() const { return 0; }
219 virtual size_t get_sym_text_len() const { return 0; }
220
221 /* check for a match to the given symbol text */
222 virtual int sym_text_matches(const char *str, size_t len) const
223 { return FALSE; }
224
225 /*
226 * Fold constant expressions, given a finished symbol table. We do
227 * most of our constant folding during the initial parsing, but some
228 * constant folding must wait until the symbol table is finished; in
229 * particular, we can't figure out what to do with symbols until we
230 * know what the symbols mean.
231 *
232 * For most nodes, this function should merely recurse into subnodes
233 * and fold constants. Nodes that are affected by symbol
234 * resolution, directly or indirectly, should override this.
235 *
236 * For example, a list can change from unknown to constant during
237 * this operation. If the list contains a symbol, the list will
238 * initially be set to unknown, since the symbol could turn out to
239 * be a property evaluation, which would be non-constant, or an
240 * object name, which would be constant.
241 *
242 * Returns the folded version of the node, or simply 'this' if no
243 * folding takes place.
244 */
245 virtual class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab) = 0;
246
247 /*
248 * generate a constant value node for the address of this node;
249 * returns null if the symbol has no address
250 */
251 virtual class CTcPrsNode *fold_addr_const(class CTcPrsSymtab *)
252 {
253 /* by default, we have no address */
254 return 0;
255 }
256
257 /*
258 * Adjust the expression for use as a dynamic (run-time) compilation
259 * expression expression. Dynamic expressions include debugger
260 * expression evaluations and the run-time "eval()" facility.
261 *
262 * Code generation for dynamic expressions is somewhat different than
263 * for normal expressions. This routine should allocate and return a
264 * new node, if necessary, for dynamic use. If no changes are
265 * necessary, simply return 'this'.
266 *
267 * If 'speculative' is true, the expression is being evaluated
268 * speculatively by the debugger. This means that the user hasn't
269 * explicitly asked for the expression to be evaluated, but rather the
270 * debugger is making a guess that the expression might be of interest
271 * to the user and is making an unsolicited attempt to offer it to the
272 * user. Because the debugger is only guessing that the expression is
273 * interesting, the expression must not be evaluated if it has any side
274 * effects at all.
275 */
276 virtual class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
277 };
278
279 /* ------------------------------------------------------------------------ */
280 /*
281 * Symbol Table Entry. Each symbol has an entry in one of the symbol
282 * tables:
283 *
284 * - The global symbol table contains object, property, and built-in
285 * functions from the default function set.
286 *
287 * - Local symbol tables contain local variables and parameters. Local
288 * tables have block-level scope.
289 *
290 * - Label symbol tables contain code labels (for "goto" statements).
291 * Label tables have function-level or method-level scope.
292 */
293
294 /*
295 * Basic symbol table entry. The target
296 */
297 class CTcSymbolBase: public CVmHashEntryCS
298 {
299 public:
300 CTcSymbolBase(const char *str, size_t len, int copy, tc_symtype_t typ)
301 : CVmHashEntryCS(str, len, copy)
302 {
303 typ_ = typ;
304 }
305
306 /* allocate symbol entries from the parser memory pool */
307 void *operator new(size_t siz);
308
309 /* get the symbol type */
310 tc_symtype_t get_type() const { return typ_; }
311
312 /* get the symbol text and length */
313 const char *get_sym() const { return getstr(); }
314 size_t get_sym_len() const { return getlen(); }
315
316 /*
317 * Generate a constant value node for this symbol, if possible;
318 * returns null if the symbol does not evaluate to a compile-time
319 * constant value. An object name, for example, evaluates to a
320 * compile-time constant equal to the object reference; a property
321 * name, in contrast, is (when not qualified by another operator) an
322 * invocation of the property, hence must be executed at run time,
323 * hence is not a compile-time constant.
324 */
325 virtual class CTcPrsNode *fold_constant()
326 {
327 /* by default, a symbol's value is not a constant */
328 return 0;
329 }
330
331 /*
332 * generate a constant value node for the address of this symbol;
333 * returns null if the symbol has no address
334 */
335 virtual class CTcPrsNode *fold_addr_const()
336 {
337 /* by default, a symbol has no address */
338 return 0;
339 }
340
341 /* determine if this symbol can be used as an lvalue */
342 virtual int check_lvalue() const { return FALSE; }
343
344 /* determine if this symbol can have its address taken */
345 virtual int has_addr() const { return FALSE; }
346
347 /* determine if I have a return value when evaluated */
348 virtual int has_return_value_on_call() const { return TRUE; }
349
350 /*
351 * Write the symbol to a symbol export file. By default, we'll
352 * write the type and symbol name to the file. Some subclasses
353 * might wish to override this to write additional data, or to write
354 * something different or nothing at all (for example, built-in
355 * function symbols are not written to a symbol export file).
356 *
357 * When a subclass does override this, it must write the type as a
358 * UINT2 value as the first thing written to the file. The generic
359 * file reader switches on this type code to determine what to call
360 * to load the entry, then calls the subclass-specific loader to do
361 * the actual work.
362 *
363 * Returns true if we wrote the symbol to the file, false if not.
364 * (False doesn't indicate an error - it indicates that we chose not
365 * to store the symbol because the symbol is not of a type that we
366 * want to put in the export file.)
367 */
368 virtual int write_to_sym_file(class CVmFile *fp);
369
370 /* write the symbol name (with a UINT2 length prefix) to a file */
371 int write_name_to_file(class CVmFile *fp);
372
373 /*
374 * Write the symbol to an object file. By default, we'll write the
375 * type and symbol name to the file. Some subclasses might wish to
376 * override this to write additional data, or to write something
377 * different or nothing at all (for example, built-in function
378 * symbols are not written to an object file).
379 *
380 * When a subclass does override this, it must write the type as a
381 * UINT2 value as the first thing written to the file. The generic
382 * file reader switches on this type code to determine what to call
383 * to load the entry, then calls the subclass-specific loader to do
384 * the actual work.
385 *
386 * Returns true if we wrote the symbol to the file, false if not.
387 * (False doesn't indicate an error - it indicates that we chose not
388 * to store the symbol because the symbol is not of a type that we
389 * want to put in the export file.)
390 */
391 virtual int write_to_obj_file(class CVmFile *fp);
392
393 /*
394 * Write the symbol's cross references to the object file. This can
395 * write references to other symbols by storing the other symbol's
396 * index in the object file. Most symbols don't have any cross
397 * references, so this does nothing by default.
398 *
399 * If this writes anything, the first thing written must be a UINT4
400 * giving the object file index of this symbol. On loading, we'll
401 * read this and look up the loaded symbol.
402 */
403 virtual int write_refs_to_obj_file(class CVmFile *) { return FALSE; }
404
405 /*
406 * perform basic writing to a file - this performs common work that
407 * can be used for object or symbol files
408 */
409 int write_to_file_gen(CVmFile *fp);
410
411 /*
412 * Read a symbol from a symbol file, returning the new symbol
413 */
414 static class CTcSymbol *read_from_sym_file(class CVmFile *fp);
415
416 /*
417 * Load a symbol from an object file. Stores the symbol in the
418 * global symbol table, and fills in the appropriate translation
419 * mapping table when necessary. Returns zero on success; logs
420 * error messages and return non-zero on failure.
421 */
422 static int load_from_obj_file(class CVmFile *fp,
423 const textchar_t *fname,
424 tctarg_obj_id_t *obj_xlat,
425 tctarg_prop_id_t *prop_xlat,
426 ulong *enum_xlat);
427
428 /*
429 * Load references from the object file - reads the information that
430 * write_refs_to_obj_file() wrote, except that the caller will have
431 * read the first UINT4 giving the symbol's object file index before
432 * calling this routine.
433 */
434 virtual void load_refs_from_obj_file(class CVmFile *,
435 const textchar_t * /*obj_fname*/,
436 tctarg_obj_id_t * /*obj_xlat*/,
437 tctarg_prop_id_t * /*prop_xlat*/)
438 {
439 /* by default, do nothing */
440 }
441
442 /*
443 * Log an object file loading conflict with this symbol. The given
444 * type is the new type found in the object file of the given name.
445 */
446 void log_objfile_conflict(const textchar_t *fname, tc_symtype_t new_type)
447 const;
448
449 /*
450 * Get a pointer to the head of the fixup list for this symbol.
451 * Symbols such as functions that keep a list of fixups for
452 * references to the symbol must override this to provide a fixup
453 * list head; by default, symbols keep no fixup list, so we'll just
454 * return null.
455 */
456 virtual struct CTcAbsFixup **get_fixup_list_anchor() { return 0; }
457
458 /*
459 * Set my code stream anchor object. By default, symbols don't keep
460 * track of any stream anchors. Symbols that refer to code or data
461 * stream locations directly must keep an anchor, since they must
462 * keep track of their fixup list in order to fix up generated
463 * references to the symbol. This must be overridden by any
464 * subclasses that keep anchors.
465 */
466 virtual void set_anchor(struct CTcStreamAnchor *) { }
467
468 /*
469 * Determine if this symbol is external and unresolved. By default,
470 * a symbol cannot be external at all, so this will return false.
471 * Subclasses for symbol types that can be external should override
472 * this to return true if the symbol is an unresolved external
473 * reference.
474 */
475 virtual int is_unresolved_extern() const { return FALSE; }
476
477 /*
478 * Mark the symbol as referenced. Some symbol types keep track of
479 * whether they've been referenced or not; those types can override
480 * this to keep track. This method is called each time the symbol
481 * is found in the symbol table via the find() or find_or_def()
482 * methods. By default, we do nothing.
483 */
484 virtual void mark_referenced() { }
485
486 /*
487 * Apply internal fixups. If the symbol keeps its own internal
488 * fixup information, it can translate the fixups here. By default,
489 * this does nothing.
490 */
491 virtual void apply_internal_fixups() { }
492
493 /*
494 * Build dictionary entries for this symbol. Most symbols do
495 * nothing here; objects which can have associated vocabulary words
496 * should insert their vocabulary into the dictionary.
497 */
498 virtual void build_dictionary() { }
499
500 /*
501 * Create a new "context variable" version of this symbol for use in
502 * an anonymous function. This is only needed for symbols that can
503 * exist in a local scope.
504 */
505 virtual class CTcSymbol *new_ctx_var() const { return 0; }
506
507 /*
508 * Apply context variable conversion. If this symbol has not been
509 * referenced, this should simply remove the symbol from the symbol
510 * table. Otherwise, this should apply the necessary conversions to
511 * the original symbol from which this symbol was created to ensure
512 * that the original and this symbol share a context variable slot.
513 *
514 * Returns true if a conversion was performed (i.e., the symbol was
515 * referenced), false if not.
516 */
517 virtual int apply_ctx_var_conv(class CTcPrsSymtab *,
518 class CTPNCodeBody *)
519 { return FALSE; }
520
521 /*
522 * Finalize context variable conversion. This should do nothing if
523 * the variable hasn't already been notified that it's a context
524 * variable (how this happens varies by symbol type - see locals in
525 * particular). This is called with the variable's own scope active
526 * in the parser, so the final variable assignments for the symbol
527 * can be made.
528 */
529 virtual void finish_ctx_var_conv() { }
530
531 /*
532 * Check for local references. For variables that can exist in
533 * local scope, such as locals, this will be called when all of the
534 * code for the scope has been parsed; this should check to see if
535 * the symbol has been referenced in the scope, and display an
536 * appropriate warning message if not.
537 */
538 virtual void check_local_references() { }
539
540 /*
541 * Add an entry for this symbol to a "runtime symbol table," which is
542 * a symbol table that we can pass to the interpreter. This must be
543 * overridden by each symbol type for each target architecture,
544 * because the nature of the runtime symbol table varies by target
545 * architecture.
546 *
547 * By default, this does nothing. Symbol types that don't need to
548 * generate runtime symbol table entries don't need to override this.
549 */
550 virtual void add_runtime_symbol(class CVmRuntimeSymbols *) { }
551
552 protected:
553 /*
554 * Base routine to read from a symbol file - reads the symbol name.
555 * Returns a pointer to the symbol name (stored in tokenizer memory
556 * that will remain valid throughout the compilation) on success; on
557 * failure, logs an error and returns null.
558 */
559 static const char *base_read_from_sym_file(class CVmFile *fp);
560
561 /* symbol type */
562 tc_symtype_t typ_;
563 };
564
565 #endif /* TCPN_H */