summaryrefslogtreecommitdiff
path: root/sys/kern/gendevcalls.awk
blob: b6e3f12b95f7b64667d1d57bd8221e40dbc01516 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
#! /usr/bin/awk -f
#	$NetBSD: gendevcalls.awk,v 1.1 2021/09/15 17:25:14 thorpej Exp $
#
# Copyright (c) 2021 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Jason R. Thorpe.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#
# Parses a device call definition file and produces argument and
# binding structures.
#

function emit_binding(field) {
	printf("union %s_binding {\n", call_name_ub)
	printf("\tstruct device_call_generic generic;\n")
	if (field != "") {
		printf("\tstruct {\n")
		printf("\t\tconst char *name;\n")
		printf("\t\tstruct %s_args *args;\n", call_name_ub)
		printf("\t} %s;\n", field);
	}
	printf("};\n")
}

function emit_name_macro() {
	printf("\n")
	printf("#define %s_STR \"%s\"\n", call_name_ub_uc, call_name)
}

function emit_invoke_macro(field, marg, carg) {
	printf("\n")
	printf("#define %s%s \\\n", call_name_ub_uc, marg)
	printf("\t&((const union %s_binding){ \\\n", call_name_ub)
	printf("\t\t.%s.name = \"%s\", \\\n", field, call_name)
	printf("\t\t.%s.args = %s, \\\n", field, carg)
	printf("\t})\n")
}

function start_decl(arg) {
	if (state == "expecting-subsystem") {
		print "must declare a subsystem before declaring a call " \
		    "at line " NR \
		    > "/dev/stderr"
		exit 1
	}

	if (state != "expecting-decl-start") {
		print "unexpected start of declaration at line " NR \
		    > "/dev/stderr"
		exit 1
	}

	call_name = arg

	if (index(call_name, call_name_prefix) != 1) {
		printf("method name '%s' at line %d must begin with '%s'\n", \
		    call_name, NR, call_name_prefix) \
		    > "/dev/stderr"
		exit 1
	}

	call_name_ub = arg
	gsub("\-", "_", call_name_ub)
	call_name_ub_uc = toupper(call_name_ub)
}

NR == 1 {
	VERSION = $0
	gsub("\\$", "", VERSION)
	gsub(/ $/, "", VERSION)

	printf("/*\t$NetBSD" "$\t*/\n\n")
	printf("/*\n")
	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n")
	printf(" *\n")
	printf(" * generated from:\n")
	printf(" *\t%s\n", VERSION)
	printf(" */\n")

	subsystem = ""
	state = "expecting-subsystem"

	next
}
#
# Subystem declaration.  We use this to generate header file guards,
# as well as to sanity-check the method names when they are declared.
#
state == "expecting-subsystem" && \
/^subsystem[ \t]+[a-zA-Z]+[a-zA-Z0-9\-]*[ \t]*;[ \t]*$/ {
	subsystem = $2

	# strip the trailing ;
	gsub(";$", "", subsystem)

	subsystem_ub = subsystem
	gsub("\-", "_", subsystem_ub)
	subsystem_ub_uc = toupper(subsystem_ub)

	# now tack on a trailing - for sanity checking method
	# names later.
	call_name_prefix = subsystem "-"

	# Emit the leading header guard.
	printf("#ifndef _%s_CALLS_H_\n", subsystem_ub_uc)
	printf("#define _%s_CALLS_H_\n", subsystem_ub_uc)

	# Pull in <sys/device.h> for 'struct device_call_generic'.
	printf("\n#include <sys/device.h>\n")

	state = "expecting-decl-start"

	next
}
#
# Beginning of a call-with-arguments declaration.  Gather up the various
# forms of the method name and emit the beginning of the structure declaration.
#
/^[a-zA-Z]+[a-zA-Z0-9\-]*[ \t]+{[ \t]*$/ {
	start_decl($1)

	# Emit the args structure declaration.
	printf("struct %s_args {\n", call_name_ub)

	state = "expecting-decl-end"

	next
}
#
# A call-without-arguments declaration.
#
/^[a-zA-Z]+[a-zA-Z0-9\-]*[ \t]*;[ \t]*$/ {
	# strip the trailing ;
	call_name = $1
	gsub(";$", "", call_name)

	start_decl(call_name)

	emit_binding("")
	emit_name_macro()
	emit_invoke_macro("generic", "", "NULL")

	next
}
#
# End of a declaration.  Wrap up the structure declaration and emit
# the binding information.
#
/^}[ \t]*;[ \t]*$/ {
	if (state != "expecting-decl-end") {
		print "unexpected end of declaration at line " NR \
		    > "/dev/stderr"
		exit 1
	}

	# Terminate the args structure declaration.
	printf("};\n")

	printf("\n")
	emit_binding("binding")
	emit_name_macro()
	emit_invoke_macro("binding", "(_args_)", "(_args_)")

	state = "expecting-decl-start"

	next
}
#
# Default action is to simply emit the line as it exists in the source
# file.
#
{
	print $0
}
END {
	if (state != "expecting-decl-start") {
		print "unexpected end of file at line " NR \
		    > "/dev/stderr"
		exit 1
	}

	# Emit the trailing header guard.
	printf("\n#endif /* _%s_CALLS_H_ */\n", subsystem_ub_uc)
}