• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Rob Clark <robdclark@gmail.com>
3  * Copyright (C) 2010-2011 Marcin Koƛcielnicki <koriakin@0x04.net>
4  * Copyright (C) 2010 Luca Barbieri <luca@luca-barbieri.com>
5  * Copyright (C) 2010 Marcin Slusarz <marcin.slusarz@gmail.com>
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 /* modified version of headergen which uses enums and inline fxns for
29  * type safety.. based on original headergen
30  */
31 
32 #include "rnn.h"
33 #include "util.h"
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <inttypes.h>
38 #include <time.h>
39 #include <ctype.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
44 #include <assert.h>
45 
46 struct rnndelem **elems = NULL;
47 int elemsnum = 0;
48 int elemsmax = 0;
49 
50 char **offsetfns = NULL;
51 int offsetfnsnum = 0;
52 int offsetfnsmax = 0;
53 
54 int startcol = 64;
55 
56 struct fout {
57 	char *name;
58 	FILE *file;
59 	char *guard;
60 };
61 
62 struct fout *fouts = 0;
63 int foutsnum = 0;
64 int foutsmax = 0;
65 
66 static bool no_asserts = false;
67 
seekcol(FILE * f,int src,int dst)68 static void seekcol (FILE *f, int src, int dst) {
69 	if (dst <= src)
70 		fprintf (f, "\t");
71 	else {
72 		int n = dst/8 - src/8;
73 		if (n) {
74 			while (n--)
75 				fprintf (f, "\t");
76 			n = dst&7;
77 		} else
78 			n = dst-src;
79 		while (n--)
80 			fprintf (f, " ");
81 	}
82 }
83 
findfout(char * file)84 static FILE *findfout (char *file) {
85 	int i;
86 	for (i = 0; i < foutsnum; i++)
87 		if (!strcmp(fouts[i].name, file))
88 			break;
89 	if (i == foutsnum) {
90 		fprintf (stderr, "AIII, didn't open file %s.\n", file);
91 		exit(1);
92 	}
93 	return fouts[i].file;
94 }
95 
printdef(char * name,char * suf,int type,uint64_t val,char * file)96 static void printdef (char *name, char *suf, int type, uint64_t val, char *file) {
97 	FILE *dst = findfout(file);
98 	int len;
99 	if (suf)
100 		fprintf (dst, "#define %s__%s%n", name, suf, &len);
101 	else
102 		fprintf (dst, "#define %s%n", name, &len);
103 	if (type == 0 && val > 0xffffffffull)
104 		seekcol (dst, len, startcol-8);
105 	else
106 		seekcol (dst, len, startcol);
107 	switch (type) {
108 		case 0:
109 			if (val > 0xffffffffull)
110 				fprintf (dst, "0x%016"PRIx64"ULL\n", val);
111 			else
112 				fprintf (dst, "0x%08"PRIx64"\n", val);
113 			break;
114 		case 1:
115 			fprintf (dst, "%"PRIu64"\n", val);
116 			break;
117 	}
118 }
119 
printvalue(struct rnnvalue * val,int shift)120 static void printvalue (struct rnnvalue *val, int shift) {
121 	if (val->varinfo.dead)
122 		return;
123 	if (val->valvalid)
124 		printdef (val->fullname, 0, 0, val->value << shift, val->file);
125 }
126 
127 static void printbitfield (struct rnnbitfield *bf, int shift);
128 
printtypeinfo(struct rnntypeinfo * ti,struct rnnbitfield * bf,char * prefix,char * file)129 static void printtypeinfo (struct rnntypeinfo *ti, struct rnnbitfield *bf,
130 		char *prefix, char *file) {
131 	FILE *dst = findfout(file);
132 	enum rnnttype intype = ti->type;
133 	char *typename = NULL;
134 	uint32_t mask = typeinfo_mask(ti);
135 	uint32_t width = 1 + ti->high - ti->low;
136 
137 	/* for fixed point, input type (arg to fxn) is float: */
138 	if ((ti->type == RNN_TTYPE_FIXED) || (ti->type == RNN_TTYPE_UFIXED))
139 		intype = RNN_TTYPE_FLOAT;
140 
141 	/* for toplevel register (ie. not bitfield), only generate accessor
142 	 * fxn for special cases (float, shr, min/max, etc):
143 	 */
144 	if (bf || ti->shr || ti->minvalid || ti->maxvalid || ti->alignvalid ||
145 			ti->radixvalid || (intype == RNN_TTYPE_FLOAT)) {
146 		switch (intype) {
147 		case RNN_TTYPE_HEX:
148 		case RNN_TTYPE_UINT:
149 		case RNN_TTYPE_A3XX_REGID:
150 			typename = "uint32_t";
151 			break;
152 		case RNN_TTYPE_INT:
153 			typename = "int32_t";
154 			break;
155 		case RNN_TTYPE_FLOAT:
156 			typename = "float";
157 			break;
158 		case RNN_TTYPE_ENUM:
159 			asprintf(&typename, "enum %s", ti->name);
160 			break;
161 		default:
162 			break;
163 		}
164 	}
165 
166 	/* for boolean, just generate a #define flag.. rather than inline fxn */
167 	if (bf && (intype == RNN_TTYPE_BOOLEAN)) {
168 		printdef(bf->fullname, 0, 0, mask, file);
169 		return;
170 	}
171 
172 	if (typename) {
173 		printdef(prefix, "MASK", 0, mask, file);
174 		printdef(prefix, "SHIFT", 1, ti->low, file);
175 
176 		fprintf(dst, "static inline uint32_t %s(%s val)\n", prefix, typename);
177 		fprintf(dst, "{\n");
178 
179 		if ((ti->minvalid || ti->maxvalid || ti->alignvalid) && !no_asserts) {
180 			fprintf(dst, "\tassert(1");
181 			if (ti->minvalid)
182 				fprintf(dst, " && (val >= %"PRIu64")", ti->min);
183 			if (ti->maxvalid)
184 				fprintf(dst, " && (val <= %"PRIu64")", ti->max);
185 			if (ti->alignvalid)
186 				fprintf(dst, " && !(val %% %"PRIu64")", ti->align);
187 			fprintf(dst, ");\n");
188 		}
189 
190 		if (ti->shr && !no_asserts) {
191 			fprintf(dst, "\tassert(!(val & 0x%x));\n", (1 << ti->shr) - 1);
192 		}
193 
194 		fprintf(dst, "\treturn ((");
195 
196 		if (ti->type == RNN_TTYPE_FIXED) {
197 			fprintf(dst, "((int32_t)(val * %d.0))", (1 << ti->radix));
198 		} else if (ti->type == RNN_TTYPE_UFIXED) {
199 			fprintf(dst, "((uint32_t)(val * %d.0))", (1 << ti->radix));
200 		} else if (ti->type == RNN_TTYPE_FLOAT) {
201 			if (width == 32)
202 				fprintf(dst, "fui(val)");
203 			else if (width == 16)
204 				fprintf(dst, "_mesa_float_to_half(val)");
205 			else
206 				assert(!"invalid float size");
207 		} else {
208 			fprintf(dst, "val");
209 		}
210 
211 		if (ti->shr)
212 			fprintf(dst, " >> %d", ti->shr);
213 
214 		fprintf(dst, ") << %s__SHIFT) & %s__MASK;\n", prefix, prefix);
215 		fprintf(dst, "}\n");
216 
217 		if (intype == RNN_TTYPE_ENUM)
218 			free(typename);
219 	}
220 
221 	int i;
222 	for (i = 0; i < ti->valsnum; i++)
223 		printvalue(ti->vals[i], ti->low);
224 	for (i = 0; i < ti->bitfieldsnum; i++)
225 		printbitfield(ti->bitfields[i], ti->low);
226 }
227 
printbitfield(struct rnnbitfield * bf,int shift)228 static void printbitfield (struct rnnbitfield *bf, int shift) {
229 	if (bf->varinfo.dead)
230 		return;
231 	printtypeinfo (&bf->typeinfo, bf, bf->fullname, bf->file);
232 }
233 
printdelem(struct rnndelem * elem,uint64_t offset)234 static void printdelem (struct rnndelem *elem, uint64_t offset) {
235 	int use_offset_fxn;
236 	char *offsetfn = NULL;
237 
238 	if (elem->varinfo.dead)
239 		return;
240 
241 	use_offset_fxn = elem->offsets || elem->doffset || elem->doffsets;
242 	assert((!!elem->offsets + !!elem->doffset + !!elem->doffsets) <= 1);
243 
244 	if (use_offset_fxn)
245 		asprintf(&offsetfn, "__offset_%s", elem->name);
246 
247 	if (elem->length != 1) {
248 		ADDARRAY(elems, elem);
249 		ADDARRAY(offsetfns, offsetfn);
250 	}
251 
252 	if (elem->name) {
253 		char *regname;
254 		asprintf(&regname, "REG_%s", elem->fullname);
255 		if (elemsnum) {
256 			int len;
257 			FILE *dst = findfout(elem->file);
258 			int i;
259 
260 			if (use_offset_fxn) {
261 				fprintf(dst, "static inline uint32_t %s(", offsetfn);
262 				if (elem->index)
263 					fprintf(dst, "enum %s", elem->index->name);
264 				else
265 					fprintf(dst, "uint32_t");
266 				fprintf(dst, " idx)\n");
267 				fprintf(dst, "{\n");
268 				if (elem->doffset) {
269 					fprintf(dst, "\treturn (%s) + (%#" PRIx64 "*idx);\n", elem->doffset, elem->stride);
270 				} else {
271 					int valuesnum = elem->doffsets ? elem->doffsetsnum : elem->offsetsnum;
272 
273 					fprintf(dst, "\tswitch (idx) {\n");
274 					for (i = 0; i < valuesnum; i++) {
275 						struct rnnvalue *val = NULL;
276 						fprintf(dst, "\t\tcase ");
277 						if (elem->index) {
278 							int j;
279 							for (j = 0; j < elem->index->valsnum; j++) {
280 								if (elem->index->vals[j]->value == i) {
281 									val = elem->index->vals[j];
282 									break;
283 								}
284 							}
285 						}
286 						if (val) {
287 							fprintf(dst, "%s", val->name);
288 						} else {
289 							fprintf(dst, "%d", i);
290 						}
291 						if (elem->offsets) {
292 							fprintf(dst, ": return 0x%08"PRIx64";\n", elem->offsets[i]);
293 						} else {
294 							fprintf(dst, ": return (%s);\n", elem->doffsets[i]);
295 						}
296 					}
297 					fprintf(dst, "\t\tdefault: return INVALID_IDX(idx);\n");
298 					fprintf(dst, "\t}\n");
299 				}
300 				fprintf(dst, "}\n");
301 			}
302 			fprintf (dst, "static inline uint32_t %s(", regname);
303 			for (i = 0; i < elemsnum; i++) {
304 				if (i)
305 					fprintf(dst, ", ");
306 				if (elems[i]->index)
307 					fprintf(dst, "enum %s ", elems[i]->index->name);
308 				else
309 					fprintf(dst, "uint32_t ");
310 				fprintf (dst, "i%d%n", i, &len);
311 			}
312 			fprintf (dst, ") { return ");
313 			fprintf (dst, "0x%08"PRIx64"", offset + elem->offset);
314 			for (i = 0; i < elemsnum; i++) {
315 				if (offsetfns[i])
316 					fprintf(dst, " + %s(i%d)", offsetfns[i], i);
317 				else
318 					fprintf (dst, " + %#" PRIx64 "*i%d", elems[i]->stride, i);
319 			}
320 			fprintf (dst, "; }\n");
321 		} else
322 			printdef (regname, 0, 0, offset + elem->offset, elem->file);
323 
324 		free(regname);
325 /*
326 		if (elem->stride)
327 			printdef (elem->fullname, "ESIZE", 0, elem->stride, elem->file);
328 		if (elem->length != 1)
329 			printdef (elem->fullname, "LEN", 0, elem->length, elem->file);
330 */
331 		printtypeinfo (&elem->typeinfo, NULL, elem->fullname, elem->file);
332 	}
333 	fprintf (findfout(elem->file), "\n");
334 	int j;
335 	for (j = 0; j < elem->subelemsnum; j++) {
336 		printdelem(elem->subelems[j], offset + elem->offset);
337 	}
338 	if (elem->length != 1) {
339 		elemsnum--;
340 		offsetfnsnum--;
341 	}
342 	free(offsetfn);
343 }
344 
print_file_info_(FILE * dst,struct stat * sb,struct tm * tm)345 static void print_file_info_(FILE *dst, struct stat* sb, struct tm* tm)
346 {
347 	char timestr[64];
348 	strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
349 	fprintf(dst, "(%7Lu bytes, from %s)\n", (unsigned long long)sb->st_size, timestr);
350 }
351 
print_file_info(FILE * dst,const char * file)352 static void print_file_info(FILE *dst, const char* file)
353 {
354 	struct stat sb;
355 	struct tm tm;
356 	stat(file, &sb);
357 	gmtime_r(&sb.st_mtime, &tm);
358 	print_file_info_(dst, &sb, &tm);
359 }
360 
printhead(struct fout f,struct rnndb * db)361 static void printhead(struct fout f, struct rnndb *db) {
362 	int i, j;
363 	struct stat sb;
364 	struct tm tm;
365 	stat(f.name, &sb);
366 	gmtime_r(&sb.st_mtime, &tm);
367 	fprintf (f.file, "#ifndef %s\n", f.guard);
368 	fprintf (f.file, "#define %s\n", f.guard);
369 	fprintf (f.file, "\n");
370 	fprintf(f.file,
371 		"/* Autogenerated file, DO NOT EDIT manually!\n"
372 		"\n"
373 		"This file was generated by the rules-ng-ng headergen tool in this git repository:\n"
374 		"http://github.com/freedreno/envytools/\n"
375 		"git clone https://github.com/freedreno/envytools.git\n"
376 		"\n"
377 		"The rules-ng-ng source files this header was generated from are:\n");
378 	unsigned maxlen = 0;
379 	for(i = 0; i < db->filesnum; ++i) {
380 		unsigned len = strlen(db->files[i]);
381 		if(len > maxlen)
382 			maxlen = len;
383 	}
384 	for(i = 0; i < db->filesnum; ++i) {
385 		unsigned len = strlen(db->files[i]);
386 		fprintf(f.file, "- %s%*s ", db->files[i], maxlen - len, "");
387 		print_file_info(f.file, db->files[i]);
388 	}
389 	fprintf(f.file,
390 		"\n"
391 		"Copyright (C) ");
392 	if(db->copyright.firstyear && db->copyright.firstyear < (1900 + tm.tm_year))
393 		fprintf(f.file, "%u-", db->copyright.firstyear);
394 	fprintf(f.file, "%u", 1900 + tm.tm_year);
395 	if(db->copyright.authorsnum) {
396 		fprintf(f.file, " by the following authors:");
397 		for(i = 0; i < db->copyright.authorsnum; ++i) {
398 			fprintf(f.file, "\n- ");
399 			if(db->copyright.authors[i]->name)
400 				fprintf(f.file, "%s", db->copyright.authors[i]->name);
401 			if(db->copyright.authors[i]->email)
402 				fprintf(f.file, " <%s>", db->copyright.authors[i]->email);
403 			if(db->copyright.authors[i]->nicknamesnum) {
404 				for(j = 0; j < db->copyright.authors[i]->nicknamesnum; ++j) {
405 					fprintf(f.file, "%s%s", (j ? ", " : " ("), db->copyright.authors[i]->nicknames[j]);
406 				}
407 				fprintf(f.file, ")");
408 			}
409 		}
410 	}
411 	fprintf(f.file, "\n");
412 	if(db->copyright.license)
413 		fprintf(f.file, "\n%s\n", db->copyright.license);
414 	fprintf(f.file, "*/\n\n\n");
415 }
416 
main(int argc,char ** argv)417 int main(int argc, char **argv) {
418 	char *file;
419 	struct rnndb *db;
420 	int i, j;
421 
422 	if (argc < 2) {
423 		fprintf(stderr, "Usage:\n\theadergen database-file\n");
424 		exit(1);
425 	}
426 
427 	if ((argc >= 3) && !strcmp(argv[1], "--no-asserts")) {
428 		no_asserts = true;
429 		file = argv[2];
430 	} else {
431 		file = argv[1];
432 	}
433 
434 	rnn_init();
435 	db = rnn_newdb();
436 	rnn_parsefile (db, file);
437 	rnn_prepdb (db);
438 	for(i = 0; i < db->filesnum; ++i) {
439 		char *dstname = malloc(strlen(db->files[i]) + 3);
440 		char *pretty;
441 		strcpy(dstname, db->files[i]);
442 		strcat(dstname, ".h");
443 		struct fout f = { db->files[i], fopen(dstname, "w") };
444 		if (!f.file) {
445 			perror(dstname);
446 			exit(1);
447 		}
448 		free(dstname);
449 		pretty = strrchr(f.name, '/');
450 		if (pretty)
451 			pretty += 1;
452 		else
453 			pretty = f.name;
454 		f.guard = strdup(pretty);
455 		for (j = 0; j < strlen(f.guard); j++)
456 			if (isalnum(f.guard[j]))
457 				f.guard[j] = toupper(f.guard[j]);
458 			else
459 				f.guard[j] = '_';
460 		ADDARRAY(fouts, f);
461 		printhead(f, db);
462 	}
463 
464 	for (i = 0; i < db->enumsnum; i++) {
465 		FILE *dst = NULL;
466 		int j;
467 		for (j = 0; j < db->enums[i]->valsnum; j++) {
468 			if (!dst) {
469 				dst = findfout(db->enums[i]->vals[j]->file);
470 				fprintf(dst, "enum %s {\n", db->enums[i]->name);
471 			}
472 			if (0xffff0000 & db->enums[i]->vals[j]->value)
473 				fprintf(dst, "\t%s = 0x%08"PRIx64",\n", db->enums[i]->vals[j]->name,
474 						db->enums[i]->vals[j]->value);
475 			else
476 				fprintf(dst, "\t%s = %"PRIu64",\n", db->enums[i]->vals[j]->name,
477 						db->enums[i]->vals[j]->value);
478 		}
479 		if (dst) {
480 			fprintf(dst, "};\n\n");
481 		}
482 	}
483 	for (i = 0; i < db->bitsetsnum; i++) {
484 		if (db->bitsets[i]->isinline)
485 			continue;
486 		int j;
487 		for (j = 0; j < db->bitsets[i]->bitfieldsnum; j++)
488 			printbitfield (db->bitsets[i]->bitfields[j], 0);
489 	}
490 	for (i = 0; i < db->domainsnum; i++) {
491 		if (db->domains[i]->size)
492 			printdef (db->domains[i]->fullname, "SIZE", 0, db->domains[i]->size, db->domains[i]->file);
493 		int j;
494 		for (j = 0; j < db->domains[i]->subelemsnum; j++) {
495 			printdelem(db->domains[i]->subelems[j], 0);
496 		}
497 	}
498 	for(i = 0; i < foutsnum; ++i) {
499 		fprintf (fouts[i].file, "\n#endif /* %s */\n", fouts[i].guard);
500 	}
501 	return db->estatus;
502 }
503