-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.lex
241 lines (230 loc) · 8.61 KB
/
decode.lex
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
%{
#include "instr.h"
#include "data.h"
#include "interrupt.h"
#define BLANK_THRESHOLD 10
#define YY_INPUT(buf,result,max_size) \
{ \
int len; \
bzero(buf,max_size); \
strcpy(buf, instruction); \
len = strlen(buf); \
result = len; \
}
char tempbuf[16];
int tempnum;
int blank_count;
void decode_indexed_addr(char location[],char index[]);
%}
%option noyywrap
%%
START { blank_count=0; return(START);}
MOV { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(MOV); }
ADD { yylval.flag = ADD; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
SUB { yylval.flag = SUB; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
MUL { yylval.flag = MUL; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
DIV { yylval.flag = DIV; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
MOD { yylval.flag = MOD; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
INR { yylval.flag = INR; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
DCR { yylval.flag = DCR; yylval.flag2 = ILLREG; blank_count=0; return(ARITH); }
LT { yylval.flag = LT; yylval.flag2 = ILLREG; blank_count=0; return(LOGIC); }
GT { yylval.flag = GT; yylval.flag2 = ILLREG; blank_count=0; return(LOGIC); }
EQ { yylval.flag = EQ; yylval.flag2 = ILLREG; blank_count=0; return(LOGIC); }
NE { yylval.flag = NE; yylval.flag2 = ILLREG; blank_count=0; return(LOGIC); }
GE { yylval.flag = GE; yylval.flag2 = ILLREG; blank_count=0; return(LOGIC); }
LE { yylval.flag = LE; yylval.flag2 = ILLREG; blank_count=0; return(LOGIC); }
JZ { yylval.flag = JZ; yylval.flag2 = ILLREG; blank_count=0; return(BRANCH); }
JNZ { yylval.flag = JNZ; yylval.flag2 = ILLREG; blank_count=0; return(BRANCH); }
JMP { yylval.flag = JMP; yylval.flag2 = ILLREG; blank_count=0; return(BRANCH); }
PUSH { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(PUSH); }
POP { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(POP); }
CALL { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(CALL); }
RET { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(RET); }
IN { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(IN); }
OUT { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(OUT); }
LOAD { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(LOAD); }
STORE { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(STORE); }
HALT { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(HALT); }
INT { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(INT); }
END { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(END); }
BRKP { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(BRKP); }
IRET { yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(IRET);}
PORT {yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(PORT);}
INA {yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(INA);}
READA {yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(READA);}
STOREA {yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(STOREA);}
ENCRYPT {yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(ENCRYPT);}
RESTORE {yylval.flag = 0; yylval.flag2 = ILLREG; blank_count=0; return(RESTORE);}
SP { yylval.flag = SP; yylval.flag2 = ILLREG; blank_count=0; return(SP_REG); }
BP { yylval.flag = BP; yylval.flag2 = ILLREG; blank_count=0; return(BP_REG); }
IP { yylval.flag = IP; yylval.flag2 = ILLREG; blank_count=0; return(IP_REG); }
PTBR { yylval.flag = PTBR; yylval.flag2 = ILLREG; blank_count=0; return(PTBR_REG); }
PTLR { yylval.flag = PTLR; yylval.flag2 = ILLREG; blank_count=0; return(PTLR_REG); }
EIP {yylval.flag = EIP; yylval.flag2 = ILLREG; blank_count = 0; return(EIP);}
EC {yylval.flag = EC; yylval.flag2 = ILLREG; blank_count = 0; return(EC);}
EPN {yylval.flag = EPN; yylval.flag2 = ILLREG; blank_count = 0; return(EPN);}
EMA {yylval.flag = EMA; yylval.flag2 = ILLREG; blank_count = 0; return(EMA);}
R[0-9]+ {
yylval.flag = REG;
yytext++;
tempnum = atoi(yytext);
blank_count=0;
if(tempnum > 7)
return ILLREG;
yylval.flag2 = tempnum + R0;
return(tempnum + R0);
}
P[0-3] {
yylval.flag = PORT;
yytext++;
tempnum = atoi(yytext);
yylval.flag2 = tempnum + P0;
return yylval.flag2;
}
\[SP\] { yylval.flag = MEM_SP; yylval.flag2 = ILLREG; blank_count=0; return(SP_REG); }
\[BP\] { yylval.flag = MEM_BP; yylval.flag2 = ILLREG; blank_count=0; return(BP_REG); }
\[IP\] { yylval.flag = MEM_IP; yylval.flag2 = ILLREG; blank_count=0; return(IP_REG); } //error: Is this needed.
\[PTBR\] { yylval.flag = MEM_PTBR; yylval.flag2 = ILLREG; blank_count=0; return(PTBR_REG); }
\[PTLR\] { yylval.flag = MEM_PTLR; yylval.flag2 = ILLREG; blank_count=0; return(PTLR_REG); }
\[R[0-9]+\] {
yylval.flag = MEM_REG; yylval.flag2 = ILLREG;
yytext[yyleng-1]='\0';
yytext=yytext+2;
tempnum = atoi(yytext);
blank_count=0;
if(tempnum > 7)
return ILLREG;
return(tempnum + R0);
}
-?[0-9]+ { yylval.flag = NUM; yylval.flag2 = ILLREG; blank_count=0; return(atoi(yytext)); }
\[[0-9]+\] {
yylval.flag = MEM_DIR; yylval.flag2 = ILLREG;
yytext[yyleng-1]='\0';
yytext++;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]R[0-9]+ {
yylval.flag = MEM_DIR_REG;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
tempnum = atoi(tempbuf);
if(tempnum > 7)
yylval.flag2 = ILLREG;
else
yylval.flag2 = tempnum + R0;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]SP {
yylval.flag = MEM_DIR_SP;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = SP_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]BP {
yylval.flag = MEM_DIR_BP;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = BP_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]IP {
yylval.flag = MEM_DIR_IP;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = IP_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]PTBR {
yylval.flag = MEM_DIR_PTBR;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = PTBR_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]PTLR {
yylval.flag = MEM_DIR_PTLR;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = PTLR_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]EIP { //Do we really need this feature for these exception registers
yylval.flag = MEM_DIR_EIP;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = EIP_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]EC {
yylval.flag = MEM_DIR_EC;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = EC_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]EPN {
yylval.flag = MEM_DIR_EPN;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = EPN_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]EMA {
yylval.flag = MEM_DIR_EMA;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = EMA_REG;
blank_count=0; return(atoi(yytext));
}
\[-?[0-9]+\]-?[0-9]+ {
yylval.flag = MEM_DIR_IN;
yytext++;
decode_indexed_addr(yytext,tempbuf); //Not at all tested. Vulnerable ***
yylval.flag2 = atoi(tempbuf);
blank_count=0; return(atoi(yytext));
}
\"[^"]* {
if(yytext[yyleng-1] == '\\')
yymore();
else
{
yytext[yyleng]='\0';
yytext++;
strcpy(yylval.data,yytext);
yylval.flag = STRING; yylval.flag2 = ILLREG;
blank_count=0; return(0);
}
}
[\n\t ] {blank_count++;
if(blank_count == BLANK_THRESHOLD)
return(ILLTOKEN);
}
\/\/.* {blank_count=0;}
[,] ;
. {blank_count=0; return(ILLTOKEN);}
%%
/*
Splits the token of the form "[location]index" to location and index
*/
void decode_indexed_addr(char location[],char index[]) //Not at all tested. Vulnerable ***
{
int i,flag,j;
for(i=0,j=0,flag=0;location[i]!='\0';i++,j++)
{
if(location[i] == ']')
{
flag = 1;
j=0;
location[i]='\0';
if(location[i+1] == 'R' || location[i+1] == 'S' || location[i+1] == 'T')
{
i++;
location[i]='\0';
}
}
if(flag == 1)
index[j] = location[i];
}
index[j]='\0';
}