-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathyat.c
326 lines (281 loc) · 7.73 KB
/
yat.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// yat.c - Utility for testing y64 assembler.
// author: Lewis Cheng
// date: 2012/4/21
// update: 2012/4/22 by Lewis Cheng
// update: 2012/5/7 by Shida Min
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int make_y64sim()
{
return system("make > /dev/null");
}
#define COMMAND_BUFFER_SIZE 1024
static char cmdbuf[COMMAND_BUFFER_SIZE];
static int make_app_stu(const char *name,int steps)
{
if(steps)
sprintf(cmdbuf, "cd y64-app-bin; ../y64sim %s.bin %d > %s.sim", name,steps,name);
else
sprintf(cmdbuf, "cd y64-app-bin; ../y64sim %s.bin > %s.sim", name,name);
return system(cmdbuf);
}
static int make_app_base(const char *name,int steps)
{
if(steps)
sprintf(cmdbuf, "cd y64-base; ./y64asm-base %s.ys; ./y64sim-base %s.bin %d > %s.sim.base", name,name,steps,name);
else
sprintf(cmdbuf, "cd y64-base; ./y64asm-base %s.ys; ./y64sim-base %s.bin > %s.sim.base",name,name,name);
return system(cmdbuf);
}
static int diff_app(const char *name)
{
sprintf(cmdbuf, " diff ./y64-base/%s.sim.base ./y64-app-bin/%s.sim", name, name);
return system(cmdbuf);
}
static int make_ins_stu(const char *name,int steps)
{
if(steps){
sprintf(cmdbuf, "cd y64-ins-bin; ../y64sim %s.bin %d > %s.sim", name,steps,name);
}
else
sprintf(cmdbuf, "cd y64-ins-bin; make %s.sim > /dev/null", name);
return system(cmdbuf);
}
static int make_ins_base(const char *name,int steps)
{
if(steps)
sprintf(cmdbuf, "cd y64-base; ./y64asm-base %s.ys; ./y64sim-base %s.bin %d > %s.sim.base", name, name,steps,name);
else
sprintf(cmdbuf, "cd y64-base; ./y64asm-base %s.ys; ./y64sim-base %s.bin > %s.sim.base",name,name,name);
return system(cmdbuf);
}
static int diff_ins(const char *name)
{
sprintf(cmdbuf, " diff ./y64-base/%s.sim.base ./y64-ins-bin/%s.sim", name, name);
return system(cmdbuf);
}
static int ins_pass_count;
static int ins_test_count;
static int app_test_count;
static int app_pass_count;
// test a uniterm, either an instruction or an error-handling case.
static void test_ins_bin(const char *name,int steps)
{
// test an instruction
ins_test_count++;
printf("[ Testing instruction: %s ]\n", name);
if (!make_ins_base(name,steps) && !make_ins_stu(name,steps) && !diff_ins(name)) {
ins_pass_count++;
printf("[ Result: Pass ]\n");
} else {
printf("[ Result: Fail ]\n");
}
}
static char *uni_list[] = {
"halt",
"nop",
"rrmovq",
"cmovle",
"cmovl",
"cmove",
"cmovne",
"cmovge",
"cmovg",
"irmovq",
"rmmovq",
"mrmovq",
"addq",
"subq",
"andq",
"xorq",
"jmp",
"jle",
"jl",
"je",
"jne",
"jge",
"jg",
"call",
"ret",
"pushq",
"popq",
"byte",
"word",
"long",
"quad",
"pos",
"align",
NULL
};
static void test_all_ins_bin()
{
char **p = uni_list;
while (*p)
test_ins_bin(*p++,0);
}
static void test_app_bin(const char *name,int steps)
{
++app_test_count;
printf("[ Testing application: %s ]\n", name);
if (!make_app_base(name,steps) && !make_app_stu(name,steps) && !diff_app(name)) {
++app_pass_count;
printf("[ Result: Pass ]\n");
} else {
printf("[ Result: Fail ]\n");
}
}
static char *app_list[] = {
"abs-asum-cmov",
"abs-asum-jmp",
"asumr",
"asum",
"cjr",
"j-cc",
"poptest",
"prog10",
"prog1",
"prog2",
"prog3",
"prog4",
"prog5",
"prog6",
"prog7",
"prog8",
"prog9",
"pushquestion",
"pushtest",
"ret-hazard",
NULL
};
static void test_all_app_bin()
{
// compare all .bin and .yo files
char **p = app_list;
while (*p)
test_app_bin(*p++,0);
}
static int get_correct(const char*name,int steps)
{
if(steps)
sprintf(cmdbuf, "cd y64-base; ./y64asm-base %s.ys; ./y64sim-base %s.bin %d",name,name,steps);
else
sprintf(cmdbuf, "cd y64-base; make %s.sim; cat %s.sim",name,name);
return system(cmdbuf);
}
#define SCORE_PER_INS 1.0
#define SCORE_PER_APP 2.0
static void print_result()
{
double ins_gained_score = ins_pass_count * SCORE_PER_INS;
double ins_full_score = ins_test_count * SCORE_PER_INS;
if (ins_test_count)
printf("Score for instructions:\t\t%5.2f/%5.2f\n", ins_gained_score, ins_full_score);
double app_gained_score = app_pass_count * SCORE_PER_APP;
double app_full_score = app_test_count * SCORE_PER_APP;
if (app_test_count)
printf("Score for applications:\t\t%5.2f/%5.2f\n", app_gained_score, app_full_score);
double total_gained_score = ins_gained_score + app_gained_score;
double total_full_score = ins_full_score + app_full_score;
if (ins_test_count && app_test_count)
printf("Total score:\t\t\t%5.2f/%5.2f\n", total_gained_score, total_full_score);
}
static void print_usage()
{
printf("Usage: yat -c <name> [max_steps]\n"
" Or: yat -s <name> [max_steps]\n"
" Or: yat -S\n"
" Or: yat -a <name> [max_steps]\n"
" Or: yat -A\n"
" Or: yat -F\n\n"
"Option specification:\n"
" [max_steps] limit the steps to observe the intermediate result\n"
" -c get the correct status of registers and memory\n"
" (e.g. yat -c prog9 4)\n"
" -s test single instruction in ./y64-ins-bin/<name>.bin,\n"
" (e.g. yat -s rrmovl)\n"
" -S test all instructions\n"
" -a test single application in ./y64-app-bin/<name>.bin\n"
" (e.g. yat -a asum)\n"
" -A test all applications\n"
" -F test instructions and applications, and get final score\n"
" -h print this message\n");
}
static void clean_up()
{
system("make clean > /dev/null");
system("cd y64-ins-bin; make clean > /dev/null");
system("cd y64-app-bin; make clean > /dev/null");
system("cd y64-base; make clean > /dev/null");
}
int main(int argc, char *argv[])
{
int stuff = 0;
if (argc < 2)
stuff = 1;
else if (!strcmp(argv[1], "-h"))
stuff = 1;
else if (!strcmp(argv[1], "-s"))
stuff = 2;
else if (!strcmp(argv[1], "-S"))
stuff = 3;
else if (!strcmp(argv[1], "-a"))
stuff = 4;
else if (!strcmp(argv[1], "-A"))
stuff = 5;
else if (!strcmp(argv[1], "-F"))
stuff = 6;
else if (!strcmp(argv[1], "-c"))
stuff = 7;
if (stuff == 0) {
fprintf(stderr, "yat: Invalid option %s\n", argv[1]);
return 1;
}
if (stuff == 1) {
print_usage();
return 0;
}
if (make_y64sim()) {
fprintf(stderr, "yat: Cannot make y64sim, go check y64sim.c or y64sim.h\n");
return 1;
}
if (stuff == 2) {
if (argc == 3) {
test_ins_bin(argv[2],0);
} else if (argc == 4){
int step = atoi(argv[3]);
test_ins_bin(argv[2],step);
} else {
fprintf(stderr, "yat: You have to specify the arguments\n");
return 1;
}
} else if (stuff == 4) {
if (argc == 3)
test_app_bin(argv[2],0);
else if(argc == 4){
int step = atoi(argv[3]);
test_app_bin(argv[2],step);
}
else{
fprintf(stderr, "yat: You have to specify the arguments\n");
return 1;
}
} else if (stuff == 3) {
test_all_ins_bin();
} else if (stuff == 5) {
test_all_app_bin();
} else if (stuff == 6) {
test_all_ins_bin();
test_all_app_bin();
} else if (stuff == 7) {
if(argc == 3)
get_correct(argv[2],0);
else if (argc == 4){
int step = atoi(argv[3]);
get_correct(argv[2],step);
}
}
clean_up();
print_result();
return 0;
}