-
Notifications
You must be signed in to change notification settings - Fork 15
/
label.c
71 lines (61 loc) · 1.28 KB
/
label.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
/*
* Labels look like symbols except that they don't really behave
* like them in terms of scope. Handle them on their own
*/
#include <stdint.h>
#include "compiler.h"
#define L_DECLARED 0x8000
struct label {
uint16_t name;
uint16_t line;
};
struct label labels[MAXLABEL];
struct label *labelp;
void init_labels(void)
{
labelp = labels;
}
static void new_label(unsigned n)
{
if (labelp == &labels[MAXLABEL])
fatal("too many goto labels");
labelp->name = n;
labelp->line = line_num;
labelp++;
}
static struct label *find_label(register unsigned n)
{
register struct label *p = labels;
n &= 0x7FFF;
while(p < labelp) {
if (n == (p->name & 0x7FFF))
return p;
p++;
}
return NULL;
}
void use_label(unsigned n)
{
if (find_label(n))
return;
new_label(n & 0x7FFF);
}
void add_label(unsigned n)
{
register struct label *l = find_label(n);
if (l && (l->name & L_DECLARED))
error("duplicate label");
if (l == NULL)
new_label(n | L_DECLARED);
else
l->name |= L_DECLARED;
}
void check_labels(void)
{
register struct label *p = labels;
while(p < labelp) {
if (!(p->name & L_DECLARED))
errorline(p->line, "unknown label");
p++;
}
}