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