📄 symtab.h
字号:
////////////////////////////////////////////////////////////////////////////////// symtab.[h,cc] -- symbol table routines and classes//#ifndef SYMTAB_H#define SYMTAB_H#include "scanner.h"#define MAX_SCOPES 1000#define INPUT_NAME "INPUT" // pre-defined symbol#define OUTPUT_NAME "OUTPUT" // "typedef int scope_number_t; // 0..MAX_SCOPES////////// Forward referencesclass symbol_code_t; // codegen.hclass symbol_entry_t;class se_constant_t;class se_variable_t;class se_procedure_t;////////////////////////////////////////////////////////////////////////////////// Symbol Table Entry Classes://// Symbol table entry classes represent all possible types of entries into// the symbol table. All entries must be members of a concrete type which is// subclassed from the abstract type symbol_entry_t.//// Each subclass of symbol_entry_t must implement the following functions:// * constructor (in symtab.cc)// * allocate_space: (in codegen.cc)// - Allocates any space either in the stack frame or globally which// will be needed to represent the symbol in the compiled program.// * generate_data: (in codegen.cc)// - Outputs any pseudo symbols or other code for creating the symbol// in the output assembly file. Normally just something like (.dword)// * generate_code: (in codegen.cc)// - Generate any inline code needed to access a symbol.// * dump_object_data: (in symtab.cc)// - Dump all useful debugging information about a symbol table entry// to the display.class symbol_entry_t{protected: string_number_t name; scope_number_t scope; symbol_entry_t * next_in_scope; bool used; // Use the identifier's value? location_t declaration; symbol_code_t * code_gen_info; // see codegen.hpublic: symbol_entry_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc); virtual void allocate_space(void); virtual void generate_data(void); virtual symbol_code_t * generate_code(void); virtual void dump_object_data(void) const; virtual void end_of_scope_check(void) const; const symbol_code_t * get_code_gen_info(void); symbol_entry_t * next(void) const; string_number_t get_name(void) const; scope_number_t get_scope(void) const; location_t declaration_location(void) const; // the following three routines also check to make sure the desired // operation (assignment, reading, calling) is appropriate for their // type of object virtual void set_assigned(location_t loc); virtual void set_used(location_t loc); virtual void set_called(location_t loc); //////// // A few friend functions which are static to symtab.cc // friend symbol_entry_t *search_hash_table(scope_number_t scope_num, string_number_t symbol, symbol_entry_t *entry);};class se_constant_t : public symbol_entry_t{protected: int value; // constant value.public: se_constant_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc, int const_value); virtual void allocate_space(void); virtual void generate_data(void); virtual void dump_object_data(void) const; virtual void end_of_scope_check(void) const; virtual void set_used(location_t loc); int get_value(void) const;};class se_variable_t : public symbol_entry_t{protected: bool assigned; // value was set?public: se_variable_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc); virtual void allocate_space(void); virtual void generate_data(void); virtual void dump_object_data(void) const; virtual void end_of_scope_check(void) const; virtual void set_assigned(location_t loc); virtual void set_used(location_t loc);};class se_procedure_t : public symbol_entry_t{protected: scope_number_t sub_scope; bool leaf; // doesn't call anything bool has_local_vars; // needs activation recordpublic: se_procedure_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc, scope_number_t sub); virtual void allocate_space(void); virtual void generate_data(void); virtual void dump_object_data(void) const; virtual void end_of_scope_check(void) const; scope_number_t get_sub_scope(void) const; bool is_leaf(void) const; void not_leaf(void); bool has_locals(void) const; void set_locals(void); virtual void set_called(location_t loc);};extern se_variable_t * input_symbol;extern se_variable_t * output_symbol;// Symbol table entries for predefined input and output variables.extern scope_number_t predefined_scope;// Input and output are in this scope.extern scope_number_t global_scope;// Global variables are in this scope.void init_symbol_table(void);// Creates pre-defined scope and its contents, initializes internal tables.scope_number_t new_scope(void);// Create a new scope and return its scope number.void open_scope(scope_number_t scope);// Push scope onto the stack of scopes in which to look things up.void close_scope(void);// Pop one entry off the stack of scopes in which to look things up.int scope_depth(scope_number_t scope);// Determine how many levels out from the current scope the specified// scope lies -- i.e. how many times one has to dereference the static// chain to find variables in that scope.symbol_entry_t * symbol_look_up(string_number_t symbol);// Look symbol up in the symbol table, obeying PL/0 scope rules,// and return a pointer to its symbol table record; return nil if not found.symbol_entry_t * first_in_scope(scope_number_t scope);// Return head of list of symbols in given scope.scope_number_t last_scope(void);// Return number of last scope in program.void dump_symbol_table(void);// For debugging, dump the contents of the symbol table.#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -