forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsym.c
138 lines (118 loc) · 3.86 KB
/
sym.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "defs.h"
#include "data.h"
#include "decl.h"
// Symbol table functions
// Copyright (c) 2019 Warren Toomey, GPL3
// Determine if the symbol s is in the global symbol table.
// Return its slot position or -1 if not found.
// Skip C_PARAM entries
int findglob(char *s) {
int i;
for (i = 0; i < Globs; i++) {
if (Symtable[i].class == C_PARAM) continue;
if (*s == *Symtable[i].name && !strcmp(s, Symtable[i].name))
return (i);
}
return (-1);
}
// Get the position of a new global symbol slot, or die
// if we've run out of positions.
static int newglob(void) {
int p;
if ((p = Globs++) >= Locls)
fatal("Too many global symbols");
return (p);
}
// Determine if the symbol s is in the local symbol table.
// Return its slot position or -1 if not found.
int findlocl(char *s) {
int i;
for (i = Locls + 1; i < NSYMBOLS; i++) {
if (*s == *Symtable[i].name && !strcmp(s, Symtable[i].name))
return (i);
}
return (-1);
}
// Get the position of a new local symbol slot, or die
// if we've run out of positions.
static int newlocl(void) {
int p;
if ((p = Locls--) <= Globs)
fatal("Too many local symbols");
return (p);
}
// Clear all the entries in the
// local symbol table
void freeloclsyms(void) {
Locls = NSYMBOLS - 1;
}
// Update a symbol at the given slot number in the symbol table. Set up its:
// + type: char, int etc.
// + structural type: var, function, array etc.
// + size: number of elements
// + endlabel: if this is a function
// + posn: Position information for local symbols
static void updatesym(int slot, char *name, int type, int stype,
int class, int endlabel, int size, int posn) {
if (slot < 0 || slot >= NSYMBOLS)
fatal("Invalid symbol slot number in updatesym()");
Symtable[slot].name = strdup(name);
Symtable[slot].type = type;
Symtable[slot].stype = stype;
Symtable[slot].class = class;
Symtable[slot].endlabel = endlabel;
Symtable[slot].size = size;
Symtable[slot].posn = posn;
}
// Add a global symbol to the symbol table. Set up its:
// + type: char, int etc.
// + structural type: var, function, array etc.
// + size: number of elements
// + endlabel: if this is a function
// Return the slot number in the symbol table
int addglob(char *name, int type, int stype, int endlabel, int size) {
int slot;
// If this is already in the symbol table, return the existing slot
if ((slot = findglob(name)) != -1)
return (slot);
// Otherwise get a new slot, fill it in and
// return the slot number
slot = newglob();
updatesym(slot, name, type, stype, C_GLOBAL, endlabel, size, 0);
genglobsym(slot);
return (slot);
}
// Add a local symbol to the symbol table. Set up its:
// + type: char, int etc.
// + structural type: var, function, array etc.
// + size: number of elements
// + isparam: if true, this is a parameter to the function
// Return the slot number in the symbol table, -1 if a duplicate entry
int addlocl(char *name, int type, int stype, int isparam, int size) {
int localslot, globalslot;
// If this is already in the symbol table, return an error
if ((localslot = findlocl(name)) != -1)
return (-1);
// Otherwise get a new symbol slot and a position for this local.
// Update the local symbol table entry. If this is a parameter,
// also create a global C_PARAM entry to build the function's prototype.
localslot = newlocl();
if (isparam) {
updatesym(localslot, name, type, stype, C_PARAM, 0, size, 0);
globalslot = newglob();
updatesym(globalslot, name, type, stype, C_PARAM, 0, size, 0);
} else {
updatesym(localslot, name, type, stype, C_LOCAL, 0, size, 0);
}
// Return the local symbol's slot
return (localslot);
}
// Determine if the symbol s is in the symbol table.
// Return its slot position or -1 if not found.
int findsymbol(char *s) {
int slot;
slot = findlocl(s);
if (slot == -1)
slot = findglob(s);
return (slot);
}