-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
122 lines (108 loc) · 2.8 KB
/
table.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
#include <stdio.h> /* for fprintf */
#include <string.h> /* for strncmp */
#include "table.h"
#include "consts.h"
#include "types.h"
#include "as.h"
/* all labels declared or defined
* in a source file */
static label_t labels[MAX_LABELS];
static int free_label_index = 0;
/************************************************
* NAME: init_labels
* DESCRIPTION: init the labels table
***********************************************/
void init_labels(void)
{
free_label_index = 0;
}
/************************************************
* NAME: validate_labels
* RETURN VALUE: 1 on error, 0 on success
* DESCRIPTION: validate that all entry and
* regular labels are defined and
* that all extern labels are not
* defined
***********************************************/
int validate_labels(void)
{
int i;
int failed = 0;
for (i = 0; i < free_label_index; i++)
{
label_t *label = &labels[i];
switch (label->type) {
case REGULAR: /* FALLTHROUGH */
case ENTRY:
if (!label->has_address) {
fprintf(stderr, "label isn't defined: %s\n", label->name);
failed = 1;
}
break;
case EXTERNAL:
if (label->has_address) {
fprintf(stderr, "externl label defined: %s\n", label->name);
failed = 1;
}
break;
}
}
return failed;
}
/************************************************
* NAME: loop_labels
* PARAMS: fun - the function to invoke
* DESCRIPTION: inovke a given function on all
* labels
***********************************************/
void loop_labels(void (*fun)(label_t *))
{
int i;
for (i = 0; i < free_label_index; i++)
{
fun(&labels[i]);
}
}
/************************************************
* NAME: install_label
* PARAMS: name - the name of the label to be
* installed
* label - a pointer to the installed
* label
* RETURN VALUE: 1 on error, 0 on success
* DESCRIPTION: install a given label
***********************************************/
int install_label(char *name, label_t **label)
{
/* check that there is a free space for the label */
if (free_label_index == MAX_LABELS) {
parse_error("too many labels defined");
return 1;
}
/* check if label already exist */
if (lookup_label(name)) {
parse_error("label already defined");
return 1;
}
*label = &labels[free_label_index++];
memmove((*label)->name, name, MAX_LABEL_LENGTH);
return 0;
}
/************************************************
* NAME: install_label
* PARAMS: name - the name of the label to be
* looked up
* RETURN VALUE: 1 on error, 0 on success
* DESCRIPTION: lookup for a label with a given
* name
***********************************************/
label_t* lookup_label(char *name)
{
int i;
for (i = 0; i < free_label_index; i++) {
if (strncmp(labels[i].name, name, strlen(name)) == 0) {
return &labels[i];
}
}
return NULL;
}