-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20001111-1.c
More file actions
226 lines (210 loc) · 4.73 KB
/
20001111-1.c
File metadata and controls
226 lines (210 loc) · 4.73 KB
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
#include "cod.h"
#undef NDEBUG
#include "assert.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <setjmp.h>
/*
* Original test was:
*/
//
// static int next_buffer = 0;
// void bar (void);
//
// static int t = 1, u = 0;
//
// long
// foo (unsigned int offset)
// {
// unsigned i, buffer;
// int x;
// char *data;
//
// i = u;
// if (i)
// return i * 0xce2f;
//
// buffer = next_buffer;
// data = buffer * 0xce2f;
// for (i = 0; i < 2; i++)
// bar ();
// buffer = next_buffer;
// return buffer * 0xce2f + offset;
//
// }
//
// void
// bar (void)
// {
// }
//
// int
// main ()
// {
// if (foo (3) != 3)
// abort ();
// next_buffer = 1;
// if (foo (2) != 0xce2f + 2)
// abort ();
// exit (0);
// }
int exit_value = 0; /* success */
jmp_buf env;
void
my_abort()
{
exit_value = 1; /* failure */
longjmp(env, 1);
}
void
test_exit(int value)
{
if (value != 0) {
exit_value = value;
}
longjmp(env, 1);
}
FILE *test_output = NULL;
int verbose = 0;
int test_printf(const char *format, ...)
{
int ret;
va_list args;
va_start(args, format);
if (test_output == NULL) {
test_output = fopen("20001111-1.c.output", "w");
}
if (verbose) vprintf(format, args);
ret = vfprintf(test_output, format, args);
va_end(args);
return ret;
}
int
main(int argc, char**argv)
{
while (argc > 1) {
if (strcmp(argv[1], "-v") == 0) {
verbose++;
}
argc--; argv++;
}
cod_extern_entry externs[] =
{
{"foo", (void*)(long)-1},
{"bar", (void*)(long)-1},
{"main", (void*)(long)-1},
{"abort", (void*)my_abort},
{"exit", (void*)test_exit},
{"test_printf", (void*)test_printf},
{"printf", (void*)printf},
{(void*)0, (void*)0}
};
char extern_string[] = "\n\
long foo (unsigned int offset);\n\
void bar ();\n\
int main ();\n\
void exit(int value);\n\
void abort();\n\
int test_printf(const char *format, ...);\n\
int printf(const char *format, ...);";
char *global_decls[] = {
"static int next_buffer = 0;\n\
void bar (void);\n\
\n\
static int t = 1, u = 0;",
""};
char *func_decls[] = {
"long foo (unsigned int offset);",
"void bar ();",
"int main ();",
""};
char *func_bodies[] = {
/* body for foo */
"{\n\
unsigned i, buffer;\n\
int x;\n\
char *data;\n\
\n\
i = u;\n\
if (i)\n\
return i * 0xce2f;\n\
\n\
buffer = next_buffer;\n\
data = buffer * 0xce2f;\n\
for (i = 0; i < 2; i++)\n\
bar ();\n\
buffer = next_buffer;\n\
return buffer * 0xce2f + offset;\n\
\n\
}",
/* body for bar */
"{\n\
}",
/* body for main */
"{\n\
if (foo (3) != 3)\n\
abort ();\n\
next_buffer = 1;\n\
if (foo (2) != 0xce2f + 2)\n\
abort ();\n\
exit (0);\n\
}",
""};
int i;
cod_code gen_code[3];
cod_parse_context context;
for (i=0; i < 3; i++) {
int j;
if (verbose) {
printf("Working on subroutine %s\n", externs[i].extern_name);
}
if (i==0) {
context = new_cod_parse_context();
cod_assoc_externs(context, externs);
for (j=0; j < sizeof(global_decls)/sizeof(global_decls[0])-1; j++) {
cod_parse_for_globals(global_decls[j], context);
}
cod_parse_for_context(extern_string, context);
} else {
cod_extern_entry single_extern[2];
single_extern[0] = externs[i-1];
single_extern[1].extern_name = NULL;
single_extern[1].extern_value = NULL;
cod_assoc_externs(context, single_extern);
cod_parse_for_context(func_decls[i-1], context);
}
cod_subroutine_declaration(func_decls[i], context);
gen_code[i] = cod_code_gen(func_bodies[i], context);
externs[i].extern_value = (void*) gen_code[i]->func;
if (i == 2) {
int (*func)() = (int(*)()) externs[i].extern_value;
if (setjmp(env) == 0) {
func();
}
if (exit_value != 0) {
printf("Test ./generated/20001111-1.c failed\n");
exit(exit_value);
}
} else {
context = cod_copy_globals(context);
}
}
if (test_output) {
/* there was output, test expected */
fclose(test_output);
int ret = system("cmp 20001111-1.c.output /Users/eisen/prog/gcc-3.3.1-3/gcc/testsuite/gcc.expect-torture/execute/20001111-1.expect");
ret = ret >> 8;
if (ret == 1) {
printf("Test ./generated/20001111-1.c failed, output differs\n");
exit(1);
}
if (ret != 0) {
printf("Test ./generated/20001111-1.c failed, output missing\n");
exit(1);
}
}
if (verbose) printf("Test ./generated/20001111-1.c Succeeded\n");
return 0;
}