xref: /netbsd-src/sys/arch/luna68k/dev/xplx/mkdefs.awk (revision 8b8575d933fe91428231bb68e1efabe962f2b668)
1#
2# Copyright (c) 2018 Tetsuya Isaki. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24
25BEGIN {
26	print "/* generated by mkdefs.awk */"
27	print "#ifndef XPLX_DEFINE"
28	print "#define XPLX_DEFINE"
29}
30
31# hex to decimal
32function h2d(s,   i,c,n,rv)
33{
34	rv = 0
35	for (i = 1; i <= length(s); i++) {
36		c = toupper(substr(s, i, 1))
37		n = index("0123456789ABCDEF", c)
38		if (n == 0) {
39			error = 1
40			exit error
41		}
42		rv = rv * 16 + n - 1
43	}
44	return rv
45}
46
47# Global label
48/::/ {
49	print "/* " $0 " */"
50	for (i = 1; i <= NF; i++) {
51		f = $(i)
52		if (f ~ /::/) {
53			break;
54		}
55	}
56	sub(/::/, "", f)
57	sub(/:/, "", $1)
58	printf("#define %s 0x%s\n", f, $1);
59
60	k = "GLOBAL_"
61	keys[""] = k
62	v = h2d($1)
63	while (values["", v] != "") v++;
64	values["", v] = f
65	counts[""]++
66}
67
68$2 ~ /#define/ {
69	printf("%s %s %s\n", $2, $3, $4);
70
71	# multiple if statements
72	# because match() returns index/length by global variables
73	if (match($3, /^XPLX_R_/)) {
74		k = substr($3, RSTART, RLENGTH)
75	} else
76	if (match($3, /^DEVID_/)) {
77		k = substr($3, RSTART, RLENGTH)
78	} else
79	if (match($3, /^[^_]+_(CMD|ENC)_/)) {
80		k = substr($3, RSTART, RLENGTH)
81	} else {
82		next
83	}
84	sub(k, "", $3)
85	keys[k] = k
86	values[k, $4] = $3
87	counts[k]++
88}
89
90END {
91	print "#endif /* !XPLX_DEFINE */"
92}
93