forked from shamilcm/spsil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spsil.h
188 lines (165 loc) · 3.46 KB
/
spsil.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include<string.h>
#define LEGAL 0
extern int linecount;
int flag_alias=LEGAL;
struct define
{
char *name;
int value;
struct define *next;
};
struct tree
{
char nodetype; /* +,-,*,/,%,=,<,>,!
?-if statement
c-number, i-identifier, r-read, p-print,
n-nonterminal, e-double equals, l-lessthan or equals
g-greaterthan or equals w-while
b-boolean constants
a-AND o-OR x-NOT
*/
char *name;
int value;
struct define *entry;
struct tree *ptr1,*ptr2,*ptr3;
};
struct define *define_root=NULL;
FILE *fp;
char alias_table[8][30];
struct define* lookup_constant(char *name)
{
struct define *temp=define_root;
while(temp!=NULL)
{
if(strcmp(name,temp->name)==0)
return temp;
temp=temp->next;
}
return NULL;
}
int lookup_alias(char *name)
{
int i=0;
while(i<8)
{
if(strcmp(alias_table[i],name)==0)
return(i+8);
i++;
}
return(-1);
}
void insert_constant(char *name,int value)
{
struct define * temp;
temp=lookup_constant(name);
if(temp==NULL)
{
temp=malloc(sizeof(struct define));
temp->name=name;
temp->value=value;
temp->next=define_root;
define_root=temp;
}
else
{
printf("\n%d: Multiple definition of symbolic contant %s !!\n",linecount,name);
exit(0);
}
}
void insert_alias(char *name,int value)
{
struct define * temp;
if(flag_alias!=LEGAL)
{
printf("\n%d: Aliasing cannot be done within if and while!!\n",linecount);
exit(0);
}
if(!(value>=8 && value <=15))
{
printf("\n%d: Only kernel registers R8 to R15 can be aliased!!\n",linecount);
exit(0);
}
temp=lookup_constant(name);
if(temp==NULL)
strcpy(alias_table[value-8],name);
else
{
printf("\n%d: Alias name %s already used as symbolic contant!!\n",linecount,name);
exit(0);
}
}
void add_predefined_constants()
{
struct define * temp;
char name[15];
strcpy(name,"SCRATCHPAD");
if(lookup_constant(name)==NULL)
insert_constant(name,256);
strcpy(name,"PAGE_TABLE");
if(lookup_constant(name)==NULL)
insert_constant(name,512);
strcpy(name,"MEM LIST");
if(lookup_constant(name)==NULL)
insert_constant(name,576);
strcpy(name,"FILE TABLE");
if(lookup_constant(name)==NULL)
insert_constant(name,640);
strcpy(name,"READY LIST");
if(lookup_constant(name)==NULL)
insert_constant(name,736);
strcpy(name,"PROC TABLE");
if(lookup_constant(name)==NULL)
insert_constant(name,767);
strcpy(name,"FAT");
if(lookup_constant(name)==NULL)
insert_constant(name,1024);
strcpy(name,"DISK LIST");
if(lookup_constant(name)==NULL)
insert_constant(name,1536);
strcpy(name,"USER PROG");
if(lookup_constant(name)==NULL)
insert_constant(name,1792);
strcpy(name,"INTERRUPT");
if(lookup_constant(name)==NULL)
insert_constant(name,13824);
}
struct tree * create_nonterm_node(char *name,struct tree *a,struct tree *b)
{
struct tree *temp=malloc(sizeof(struct tree));
temp->nodetype='n';
temp->name=name;
temp->entry=NULL;
temp->ptr1=a;
temp->ptr2=b;
temp->ptr3=NULL;
return temp;
}
struct tree * create_tree(struct tree *a,struct tree *b,struct tree *c,struct tree *d)
{
a->ptr1=b;
a->ptr2=c;
a->ptr3=d;
}
struct tree * substitute_id(struct tree *id)
{
struct define *temp;
int n;
temp=lookup_constant(id->name);
if(temp!=NULL)
{
id->nodetype='c';
id->name=NULL;
id->value=temp->value;
return(id);
}
n=lookup_alias(id->name);
if(n<0)
{
printf("\n%d: Unknown identifier %s used!!\n",linecount,id->name);
exit(0);
}
id->nodetype='R';
id->name=NULL;
id->value=n;
return(id);
}