Sign in Join
Wiki copytemporary

Symbol tables: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Symbol tables

OldNewLine
1+back to contents [[FPC internals]]
2+
3+# Symbol tables
4+
5+## Architecture
6+
7+> **Warning:** Last updated for FPC version 1.0.x
8+
9+The symbol table contains all definitions for all symbols in the compiler. It also contains all type information for all symbols encountered during the parsing process. All symbols and definitions are streamable, and are used within PPU files to avoid recompiling everything to verify if all symbols are valid.
10+
11+There are different types of symbol tables, all of which maybe active at one time or another depending on the context of the parser.
12+
13+An architectural overview of the interaction between the symbol tables, the symbol entries and the definition entries is displayed in figure 4.1.
14+
15+As can be seen, the symbol table entries in the symbol table are done using the fast hashing algorithm with a hash dictionary.
16+
17+## The Symbol table object
18+
19+> **Warning:** Last updated for FPC version 2.5.1
20+
21+All symbol tables in the compiler are from this type of object, which contains fields for the total size of the data in the symbol table, and methods to read and write the symbol table into a stream. The start of the linked list of active symbol tables is the **symtablestack** variable.
22+
23+<http://www.pjh2.de/fpc/CompilerInternalsFigure04.png>
24+
25+``` pascal
26+type
27+ TSymTable = class
28+ name: pshortstring; // uppercased realname
29+ realname : pshortstring; // used to generate long symbol names (like symble.name + '.' + sym.name)
30+ DefList : TFPObjectList; // list of definintions
31+ SymList : TFPHashObjectList; // list of symbols
32+ defowner : TDefEntry; // The owner definition. Valud for records, objects and enumerations.
33+ moduleid : longint; // unit index
34+ refcount : smallint; // count of references. if few objects shares the same
35+ // symbol table - they add a new reference instead of
36+ // full copying the symbols
37+ currentvisibility : tvisibility; // current visibility of symtable - used while parsing object members
38+ // to put them into symtable with the correct visibility
39+ currentlyoptional : boolean; // used while parsing of objc protocol
40+ symtablelevel : byte; // level of symtable, used for nested procedures
41+ symtabletype : TSymtabletype; // Indicates the type of this symbol table (2).
42+ end;
43+```
44+
45+The type of possible symbol tables are shown in the following table:
46+
47+| Field | Description |
48+|----|----|
49+| abstractsymtable | Default value when the symbol table is created and its type is not defined. Used for debugging purposes |
50+| WithSymTable | All symbols accessed in a with statement |
51+| StaticSymTable | Contains unit implementation or program symbols |
52+| GlobalSymTable | Contains unit interface symbols |
53+| ObjectSymTable | Contains all symbols within an object/class/interface/objc class and other object types statement |
54+| RecordSymTable | Contains all symbols within a record statement |
55+| LocalSymTable | Hold symbols for all local variables of a routine |
56+| ParaSymTable | Holds symbols for all parameters of a routine (the actual parameter declaration symbols) |
57+| Stt_ExceptSymTable | Contains all exception symbols defined in the except block |
58+| exportedmacrosymtable | Holds all exported macros |
59+| localmacrosymtable | Holds all macros currently in scope |
60+| enumsymtable | Contains all enumeration elements symbols of an enumeration |
61+
62+## Inserting symbols into a symbol table
63+
64+*(last updated for fpc version 1.0.x)*
65+
66+To add a symbol into a specific symbol table, that’s symbol table’s Insert method is called, which in turns call the Insert_In_Data method of that symbol. Insert_In_Data, depending on the symbol type, adjusts the alignment and sizes of the data and actually creates the data entry in the correct segment.
67+
68+<http://www.pjh2.de/fpc/CompilerInternalsFigure05.png>
69+
70+## Symbol table interface
71+
72+*(last updated for fpc version 1.0.x)*
73+
74+### Routines
75+
76+#### Search_a_Symtable
77+
78+| | |
79+|----|----|
80+| Declaration: | <b>`function`</b>` Search_a_Symtable(`<b>`const`</b>` Symbol: String; SymTableType: TSymTableType): PSym;` |
81+| Description: | Search for a symbol Symbol in a specified symbol table SymTableType. Returns NIL if the symbol table is not found, and also if the symbol cannot be found in the desired symbol table. |
82+
83+#### GetSym
84+
85+| | |
86+|----|----|
87+| Declaration: | <b>`procedure`</b>` GetSym(`<b>`const`</b>` S: StringId; NotFoundError: Boolean);` |
88+| Description: | Search all the active symbol tables for the symbol s,setting the global variable SrSym to the found symbol, or to nil if the symbol was not found. notfounderror should be set to TRUE if the routine must give out an error when the symbol is not found. |
89+
90+#### GlobalDef
91+
92+| | |
93+|----|----|
94+| Declaration: | <b>`function`</b>` GlobalDef(`<b>`const`</b>` S: String): PDef;` |
95+| Description: | Returns a pointer to the definition of the fully qualified type symbol S, or NIL if not found. Notes: It is fully qualified, in that the symbol system.byte, for example, will be fully resolved to a unit and byte type component The symbol must have a global scope, and it must be a type symbol, otherwise NIL will be returned. |
96+
97+### Variables
98+
99+#### SrSym
100+
101+| | |
102+|--------------|-------------------------------------------------------------|
103+| Declaration: | <b>`var`</b>` SrSym: PSym;` |
104+| Description: | This points to the symbol entry found, when calling getsym. |
105+
106+#### SrSymTable
107+
108+| | |
109+|----|----|
110+| Declaration: | <b>`var`</b>` SrSymTable: PSymTable;` |
111+| Description: | This points to the symbol table of the symbol SrSym when calling GetSym. |
112+
113+Next chapter: [[Symbol entries]]
114+
115+---
116+*From the Free Pascal wiki, page "Symbol tables", as it stood on 2020-12-24 after a change by E-ric, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history