-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_pointer_arithmetic.c
More file actions
196 lines (182 loc) · 4.32 KB
/
19_pointer_arithmetic.c
File metadata and controls
196 lines (182 loc) · 4.32 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
#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:
*/
// #include <stdio.h>
//
// int main()
// {
// int a;
// int *b;
// int *c;
//
// a = 42;
// b = &a;
// c = NULL;
//
// printf("%d\n", *b);
//
// if (b == NULL)
// printf("b is NULL\n");
// else
// printf("b is not NULL\n");
//
// if (c == NULL)
// printf("c is NULL\n");
// else
// printf("c is not NULL\n");
//
// return 0;
// }
//
// /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
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("19_pointer_arithmetic.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[] =
{
{"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\
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[] = {
"#include <stdio.h>",
""};
char *func_decls[] = {
"int main();",
""};
char *func_bodies[] = {
/* body for main */
"{\n\
int a;\n\
int *b;\n\
int *c;\n\
\n\
a = 42;\n\
b = &a;\n\
c = (void*)0;\n\
\n\
test_printf(\"%d\\n\", *b);\n\
\n\
if (b == NULL)\n\
test_printf(\"b is NULL\\n\");\n\
else\n\
test_printf(\"b is not NULL\\n\");\n\
\n\
if (c == NULL)\n\
test_printf(\"c is NULL\\n\");\n\
else\n\
test_printf(\"c is not NULL\\n\");\n\
\n\
return 0;\n\
}",
""};
int i;
cod_code gen_code[1];
cod_parse_context context;
for (i=0; i < 1; 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 == 0) {
int (*func)() = (int(*)()) externs[i].extern_value;
if (setjmp(env) == 0) {
func();
}
if (exit_value != 0) {
printf("Test ./generated/19_pointer_arithmetic.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 19_pointer_arithmetic.c.output /Users/eisen/prog/tinycc/tests/tests2/19_pointer_arithmetic.expect");
ret = ret >> 8;
if (ret == 1) {
printf("Test ./generated/19_pointer_arithmetic.c failed, output differs\n");
exit(1);
}
if (ret != 0) {
printf("Test ./generated/19_pointer_arithmetic.c failed, output missing\n");
exit(1);
}
}
if (verbose) printf("Test ./generated/19_pointer_arithmetic.c Succeeded\n");
return 0;
}