1 /*
2  * t7 - not actually a regression test.
3  *   subtest 0 - this code was created to try to verify the handling of different types of
4  *       static and dynamic arrays
5  *       Added a bit of code that dumps out types that are passed into routines like submit.
6  *   subtest 1 - this code really has to be run in verbose mode to see its effects.  Created to debug
7  *       "const int blah = 5;" sorts of things.
8  *
9  *     no success or failure is associated with the execution of this file.
10  */
11 #include "cod.h"
12 #include <stdlib.h>
13 #ifdef HAVE_MALLOC_H
14 #include "malloc.h"
15 #endif
16 #include "assert.h"
17 #include <stdio.h>
18 #include <string.h>
19 
20 static int verbose = 0;
21 #ifdef NO_EMULATION
22 #define GEN_PARSE_CONTEXT(x) \
23 x = new_cod_parse_context();
24 #define EC_param0
25 #define EC_param1
26 #else
27 #define GEN_PARSE_CONTEXT(x) \
28 x = new_cod_parse_context();\
29 cod_add_param("ec", "cod_exec_context", 0, x);
30 #define EC_param0 ec
31 #define EC_param1 ec,
32 #endif
33 
34 typedef struct _multi_array_rec {
35     long	ifield;
36     double	double_array[2][2][2][2];
37     int		(*int_array)[2];
38     int		(*int_array2)[4];
39     int		(*int_array3)[4][4];
40 } multi_array_rec, *multi_array_rec_ptr;
41 
42 FMField multi_array_flds[] = {
43     {"ifield", "integer", sizeof(int), FMOffset(multi_array_rec_ptr, ifield)},
44     {"double_array", "float[2][2][2][2]", sizeof(double),
45      FMOffset(multi_array_rec_ptr, double_array)},
46     {"int_array", "integer[ifield][2]", sizeof(int),
47      FMOffset(multi_array_rec_ptr, int_array)},
48     {"int_array2", "integer[2][ifield]", sizeof(int),
49      FMOffset(multi_array_rec_ptr, int_array2)},
50     {"int_array3", "integer[ifield][ifield][ifield]", sizeof(int),
51     FMOffset(multi_array_rec_ptr, int_array3)},
52     {(char *) 0, (char *) 0, 0, 0}
53 };
54 
55 
56 typedef struct _complex_rec {
57     double r;
58     double i;
59 } complex, *complex_ptr;
60 
61 typedef struct _nested_rec {
62     complex item;
63 } nested, *nested_ptr;
64 
65 static FMField nested_field_list[] =
66 {
67     {"item", "complex", sizeof(complex), FMOffset(nested_ptr, item)},
68     {NULL, NULL, 0, 0}
69 };
70 
71 static FMField complex_field_list[] =
72 {
73     {"r", "double", sizeof(double), FMOffset(complex_ptr, r)},
74     {"i", "double", sizeof(double), FMOffset(complex_ptr, i)},
75     {NULL, NULL, 0, 0}
76 };
77 
78 typedef struct _simple_rec {
79     int integer_field;
80     short short_field;
81     long long_field;
82     nested nested_field;
83     double double_field;
84     char char_field;
85     int scan_sum;
86 } simple_rec, *simple_rec_ptr;
87 
88 static FMField simple_field_list[] =
89 {
90     {"integer_field", "integer",
91      sizeof(int), FMOffset(simple_rec_ptr, integer_field)},
92     {"short_field", "integer",
93      sizeof(short), FMOffset(simple_rec_ptr, short_field)},
94     {"long_field", "integer",
95      sizeof(long), FMOffset(simple_rec_ptr, long_field)},
96     {"nested_field", "nested",
97      sizeof(nested), FMOffset(simple_rec_ptr, nested_field)},
98     {"double_field", "float",
99      sizeof(double), FMOffset(simple_rec_ptr, double_field)},
100     {"char_field", "char",
101      sizeof(char), FMOffset(simple_rec_ptr, char_field)},
102     {"scan_sum", "integer",
103      sizeof(int), FMOffset(simple_rec_ptr, scan_sum)},
104     {NULL, NULL, 0, 0}
105 };
106 
107 int
submit(cod_exec_context ec,int port,void * data,void * type_info)108 submit(cod_exec_context ec, int port, void *data, void *type_info)
109 {
110     FMStructDescList formats = (FMStructDescList) type_info;
111     printf("In submit, ec is %p, port is %d, data is %p, type_info is %p\n",
112 	   ec, port, data, type_info);
113     while (formats[0].format_name != NULL) {
114 	FMFieldList tmp = formats[0].field_list;
115 	printf("Format \"%s\" - \n", formats[0].format_name);
116 	while(tmp[0].field_name != NULL) {
117 	    printf("{%s, %s, %d, %d},\n", tmp[0].field_name,
118 		   tmp[0].field_type, tmp[0].field_size, tmp[0].field_offset);
119 	    tmp++;
120 	}
121 	formats++;
122     }
123     return 1;
124 }
125 
126 int
main(int argc,char ** argv)127 main(int argc, char **argv)
128 {
129     int test_num = 0;
130     int run_only = -1;
131     while (argc > 1) {
132 	if (strcmp(argv[1], "-v") == 0) {
133 	    verbose++;
134 	} else if (strcmp(argv[1], "-o") == 0) {
135 	    sscanf(argv[2], "%d", &run_only);
136 	    argc--; argv++;
137 	}
138 	argc--; argv++;
139     }
140     if ((run_only == -1) || (run_only == test_num)) {
141 	static char extern_string[] = "int printf(string format, ...);\n\
142 	int submit(cod_exec_context ec, int port, void *d, cod_type_spec dt);";
143 	static cod_extern_entry externs[] =
144 	    {
145 		{"printf", (void*)(long)printf},
146 		{"submit", (void*)(long)submit},
147 		{(void*)0, (void*)0}
148 	    };
149 	static char code[] = "{\n\
150     submit(1, input);\n		 \
151      }";
152 
153 /*
154 
155 */
156     int i, j, k, l;
157     cod_code gen_code;
158     cod_exec_context ec;
159     void (*func)(cod_exec_context, void*);
160 
161     multi_array_rec multi_array;
162 
163     cod_parse_context context = new_cod_parse_context();
164 
165     multi_array.ifield = 4;
166     for (i = 0; i < 2; i++) {
167 	for (j = 0; j < 2; j++) {
168 	    for (k = 0; k < 2; k++) {
169 		for (l = 0; l < 2; l++) {
170 		    multi_array.double_array[i][j][k][l] =
171 			1000*i + 100*j + 10*k +l;
172 		}
173 	    }
174 	}
175     }
176     multi_array.int_array = malloc(2*4*sizeof(int));
177     for (i = 0; i < 4; i++) {
178 	for (j = 0; j < 2; j++) {
179 	    multi_array.int_array[i][j] = 1000*i + 100*j;
180 	}
181     }
182     multi_array.int_array2 = malloc(4*2*sizeof(int));
183     for (i = 0; i < 2; i++) {
184 	for (j = 0; j < 4; j++) {
185 	    printf("element [%d][%d] is at addr %p\n", i, j, &multi_array.int_array2[i][j]);
186 	    multi_array.int_array2[i][j] = 1000*i + 100*j;
187 	}
188     }
189     multi_array.int_array3 = malloc(4*4*4*sizeof(int));
190     for (i = 0; i < 4; i++) {
191 	for (j = 0; j < 4; j++) {
192 	    for (k = 0; k < 4; k++) {
193 		multi_array.int_array3[i][j][k] = 1000*i + 100*j + 10*k;
194 	    }
195 	}
196     }
197 
198 
199     cod_assoc_externs(context, externs);
200     cod_parse_for_context(extern_string, context);
201 
202     cod_add_simple_struct_type("multi_array", multi_array_flds, context);
203     cod_add_simple_struct_type("nested", nested_field_list, context);
204     cod_add_simple_struct_type("complex", complex_field_list, context);
205     cod_add_simple_struct_type("simple", simple_field_list, context);
206     cod_subroutine_declaration("void proc(cod_exec_context ec, simple *input)", context);
207 
208     gen_code = cod_code_gen(code, context);
209     func = (void (*)(cod_exec_context, void*))(long)gen_code->func;
210 
211     cod_dump(gen_code);
212     ec = cod_create_exec_context(gen_code);
213     printf("Main ec is %lx\n", (long) ec);
214     func(ec, &multi_array);
215     cod_exec_context_free(ec);
216     cod_code_free(gen_code);
217     cod_free_parse_context(context);
218 /*    if ((data.num_points != 1) || (data.image_data[0].num_points != 1))
219       return 1;*/
220     return 0;
221     }
222     test_num++;
223     if ((run_only == -1) || (run_only == test_num)) {
224 	/* test 1 */
225 	char code_string[] = "\
226 {\n\
227     const int j = 4;\n				\
228     const long k = 10;\n				\
229     const short l = 3;\n					\
230 \n\
231     return j + j;\n\
232 }";
233 
234 	cod_parse_context context;
235 	cod_exec_context ec;
236 	cod_code gen_code;
237 	long (*func)();
238 	long result;
239 
240 	GEN_PARSE_CONTEXT(context);
241 	gen_code = cod_code_gen(code_string, context);
242 	ec = cod_create_exec_context(gen_code);
243 	func = (long(*)()) (long) gen_code->func;
244 	if (verbose) cod_dump(gen_code);
245 	result = func(EC_param0);
246 	assert(result == 8);
247 	cod_code_free(gen_code);
248 	cod_exec_context_free(ec);
249 	cod_free_parse_context(context);
250     }
251     test_num++; /* 2 */
252 }
253