-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler_utils.c
95 lines (80 loc) · 2.13 KB
/
assembler_utils.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
#include "header.h"
boolean find_label(Control *ctrl, string s) {
string token;
string end = QM(LABEL_END);
if (!strchr(s, LABEL_END) || !(token = strtok(s, end)))
/*There is no label*/
return FALSE;
/*Detect instruction*/
if (isReservedWord(ctrl, token, In) != EOIL) {
add_error(ctrl, "Illegal label: can not be a reserved assembly word "
"(instruction).\n");
return FALSE;
}
if (edge_case_string(ctrl, token)) {
return FALSE;
}
/*Check if label is legal*/
if (!isalpha(*token))
add_error(ctrl, "Illegal label: must start with a letter.\n");
if (strlen(token) > MAX_LABEL)
add_error(ctrl, "Illegal label: too long. Max label length is " Q(
MAX_LABEL) ".\n");
if (isReservedWord(ctrl, token, Op) != EOCL) {
add_error(
ctrl,
"Illegal label: can not be a reserved assembly word (operation).\n");
return FALSE;
}
if (isReservedWord(ctrl, token, Re) != EORL) {
add_error(
ctrl,
"Illegal label: can not be a reserved assembly word (register).\n");
return FALSE;
}
for (; *token; token++) {
if (!isalnum(*token)) {
add_error(
ctrl,
"Illegal label: can not contain non alphanumeric characters.\n");
return FALSE;
}
}
return !is_empty_list(&ctrl->errors_array);
}
int isReservedWord(Control *ctrl, string s, int EOL) {
int i = 0;
string *pt;
switch (EOL) {
case Op:
EOL = EOCL;
pt = ctrl->ops_array;
break;
case In:
EOL = EOIL;
pt = ctrl->instnames_array;
break;
case Re:
EOL = EORL;
pt = ctrl->regs_array;
break;
}
for (i = 0; i < EOL && strcmp(s, *(pt + i)); i++)
;
return i;
}
boolean edge_case_string(Control *ctrl, string token) {
int len = strlen(ctrl->instnames_array[i_string]);
return !strncmp(token, ctrl->instnames_array[i_string], len);
}
int trim(string s) {
/*
Description: Trims blank chars from start of s.
Input: String s.
Output: Number of blanks to trim (to be used as addition to a pointer).
*/
int i;
for (i = 0; isspace(*(s + i)) && *(s + i) != '\n'; i++)
;
return i;
}