makedoscalls.awk revision 1.1 1 1.1 itohy #! /usr/bin/awk -f
2 1.1 itohy #
3 1.1 itohy # create DOS call interface from dos.h
4 1.1 itohy #
5 1.1 itohy # written by Yasha (ITOH Yasufumi)
6 1.1 itohy # public domain
7 1.1 itohy #
8 1.1 itohy # $NetBSD: makedoscalls.awk,v 1.1 1998/09/01 19:53:26 itohy Exp $
9 1.1 itohy
10 1.1 itohy BEGIN {
11 1.1 itohy errno_nomem = 8 # errno for "Cannot allocate memory"
12 1.1 itohy argsiz["l"] = 4; argsiz["w"] = 2
13 1.1 itohy argsiz["lb31"] = 4; argsiz["wb8"] = 2; argsiz["wb15"] = 2
14 1.1 itohy }
15 1.1 itohy
16 1.1 itohy $1 == "/*" && $2 ~ /^ff[0-9a-f][0-9a-f]$/ {
17 1.1 itohy funcnam=""
18 1.1 itohy dosno=$2
19 1.1 itohy narg=0
20 1.1 itohy ncarg=0 # number of 32bit C function argument
21 1.1 itohy argbyte=0
22 1.1 itohy opt_e=0
23 1.1 itohy e_strict=0
24 1.1 itohy e_alloc=0
25 1.1 itohy e_proc=0
26 1.1 itohy svreg=0
27 1.1 itohy noret=0
28 1.1 itohy super=0
29 1.1 itohy super_jsr=0
30 1.1 itohy alias=""
31 1.1 itohy for (i = 3; i <= NF && $i != "*/" && $i != ";"; i++) {
32 1.1 itohy arg[narg] = $i
33 1.1 itohy narg++
34 1.1 itohy if (argsiz[$i])
35 1.1 itohy ncarg++
36 1.1 itohy }
37 1.1 itohy if ($i == ";") {
38 1.1 itohy # process opts
39 1.1 itohy for (i++; i <= NF && $i != "*/"; i++) {
40 1.1 itohy if ($i == "e")
41 1.1 itohy opt_e = 1
42 1.1 itohy else if ($i == "estrct") {
43 1.1 itohy opt_e = 1
44 1.1 itohy e_strict = 1
45 1.1 itohy } else if ($i == "ep") {
46 1.1 itohy opt_e = 1
47 1.1 itohy e_proc = 1
48 1.1 itohy } else if ($i == "ealloc") {
49 1.1 itohy opt_e = 1
50 1.1 itohy e_alloc = 1
51 1.1 itohy } else if ($i == "sv")
52 1.1 itohy svreg = 1
53 1.1 itohy else if ($i == "noret")
54 1.1 itohy noret = 1
55 1.1 itohy else if ($i == "alias") {
56 1.1 itohy i++
57 1.1 itohy alias = $i
58 1.1 itohy } else if ($i == "super")
59 1.1 itohy super = 1
60 1.1 itohy else if ($i == "super_jsr")
61 1.1 itohy super_jsr = 1
62 1.1 itohy else {
63 1.1 itohy print FILENAME ":" NR ": unknown opt", $i
64 1.1 itohy exit(1)
65 1.1 itohy }
66 1.1 itohy }
67 1.1 itohy }
68 1.1 itohy if ($i != "*/") {
69 1.1 itohy print FILENAME ":" NR ": malformed input line:" $0
70 1.1 itohy exit(1)
71 1.1 itohy }
72 1.1 itohy # find out func name
73 1.1 itohy printf "|"
74 1.1 itohy for (i++; i <= NF; i++) {
75 1.1 itohy printf " %s", $i
76 1.1 itohy if ($i ~ /^\**DOS_[A-Z0-9_]*$/) {
77 1.1 itohy funcnam = $i
78 1.1 itohy while (funcnam ~ /^\*/)
79 1.1 itohy funcnam = substr(funcnam, 2, length(funcnam) -1)
80 1.1 itohy }
81 1.1 itohy }
82 1.1 itohy print ""
83 1.1 itohy if (!funcnam) {
84 1.1 itohy print FILENAME ":" NR ": can't find function name"
85 1.1 itohy exit(1)
86 1.1 itohy }
87 1.1 itohy
88 1.1 itohy # output assembly code
89 1.1 itohy print "\t.text\n\t.even"
90 1.1 itohy print "\t.globl\t_" funcnam
91 1.1 itohy if (alias) {
92 1.1 itohy print "\t.globl\t_" alias
93 1.1 itohy }
94 1.1 itohy print "_" funcnam ":"
95 1.1 itohy if (alias) print "_" alias ":"
96 1.1 itohy
97 1.1 itohy if (svreg) print "\tmoveml\td2-d7/a2-a6,sp@-"
98 1.1 itohy
99 1.1 itohy # PUSH ARGS
100 1.1 itohy argoff = ncarg * 4
101 1.1 itohy if (svreg)
102 1.1 itohy argoff += 4 * 11
103 1.1 itohy for (i = narg - 1; i >= 0; i--) {
104 1.1 itohy a = arg[i]
105 1.1 itohy asz = argsiz[a]
106 1.1 itohy if (asz) {
107 1.1 itohy if (a == "l") {
108 1.1 itohy # optimize with movem
109 1.1 itohy if (arg[i-1] == "l" && arg[i-2] == "l") {
110 1.1 itohy if (arg[i-3] == "l") {
111 1.1 itohy print "\tmoveml\tsp@(" argoff - 12 "),d0-d1/a0-a1"
112 1.1 itohy print "\tmoveml\td0-d1/a0-a1,sp@-"
113 1.1 itohy asz = 16
114 1.1 itohy i -= 3
115 1.1 itohy } else if (arg[i-3] == "w") {
116 1.1 itohy print "\tmoveml\tsp@(" argoff - 12 "),d0-d1/a0-a1"
117 1.1 itohy print "\tmoveml\td1/a0-a1,sp@-"
118 1.1 itohy print "\tmovew\td0,sp@-"
119 1.1 itohy asz = 14
120 1.1 itohy i -= 3
121 1.1 itohy } else {
122 1.1 itohy print "\tmoveml\tsp@(" argoff - 8 "),d0-d1/a0"
123 1.1 itohy print "\tmoveml\td0-d1/a0,sp@-"
124 1.1 itohy asz = 12
125 1.1 itohy i -= 2
126 1.1 itohy }
127 1.1 itohy } else {
128 1.1 itohy print "\tmovel\tsp@(" argoff "),sp@-"
129 1.1 itohy }
130 1.1 itohy } else if (a == "w")
131 1.1 itohy print "\tmovew\tsp@(" argoff + 2 "),sp@-"
132 1.1 itohy else if (a == "lb31") {
133 1.1 itohy print "\tmovel\tsp@(" argoff "),d0"
134 1.1 itohy print "\tbset\t#31,d0"
135 1.1 itohy print "\tmovel\td0,sp@-"
136 1.1 itohy } else if (a == "wb8") {
137 1.1 itohy print "\tmovew\tsp@(" argoff + 2 "),d0"
138 1.1 itohy print "\torw\t#0x100,d0"
139 1.1 itohy print "\tmovew\td0,sp@-"
140 1.1 itohy } else if (a == "wb15") {
141 1.1 itohy print "\tmovew\tsp@(" argoff + 2 "),d0"
142 1.1 itohy print "\torw\t#0x8000,d0"
143 1.1 itohy print "\tmovew\td0,sp@-"
144 1.1 itohy } else {
145 1.1 itohy print "??? unknown type"
146 1.1 itohy exit(1)
147 1.1 itohy }
148 1.1 itohy
149 1.1 itohy if (asz == 2)
150 1.1 itohy argoff -= 2
151 1.1 itohy } else if (a ~ /^[0-9][0-9]*\.w$/) {
152 1.1 itohy asz = 2
153 1.1 itohy argoff += 2
154 1.1 itohy val = substr(a, 1, length(a) - 2)
155 1.1 itohy if (val == 0)
156 1.1 itohy print "\tclrw\tsp@-"
157 1.1 itohy else
158 1.1 itohy print "\tmovew\t#" val ",sp@-"
159 1.1 itohy } else if (a ~ /^[0-9][0-9]*\.l$/) {
160 1.1 itohy asz = 4;
161 1.1 itohy argoff += 4
162 1.1 itohy val = substr(a, 1, length(a) - 2)
163 1.1 itohy if (val == 0)
164 1.1 itohy print "\tclrl\tsp@-"
165 1.1 itohy else if (val <= 32767)
166 1.1 itohy print "\tpea\t" val ":w"
167 1.1 itohy else
168 1.1 itohy print "\tmovel\t#" val ",sp@-"
169 1.1 itohy } else if (a == "drvctrl" && narg == 1) {
170 1.1 itohy # only for DOS_DRVCTRL
171 1.1 itohy asz = 2
172 1.1 itohy print "\tmoveb\tsp@(7),d0"
173 1.1 itohy print "\tlslw\t#8,d0"
174 1.1 itohy print "\tmoveb\tsp@(11),d0"
175 1.1 itohy print "\tmovew\td0,sp@-"
176 1.1 itohy } else if (a == "super" && narg == 1) {
177 1.1 itohy # only for DOS_SUPER
178 1.1 itohy print "\tmoveal\tsp@+,a1"
179 1.1 itohy } else {
180 1.1 itohy print FILENAME ":" NR ": unknown arg type:", a
181 1.1 itohy exit(1)
182 1.1 itohy }
183 1.1 itohy argbyte += asz
184 1.1 itohy }
185 1.1 itohy
186 1.1 itohy if (super_jsr) {
187 1.1 itohy print "\tmoveal\tsp@(" argoff + 8 "),a0 | inregs"
188 1.1 itohy print "\tmoveml\ta0@,d0-d7/a0-a6"
189 1.1 itohy }
190 1.1 itohy
191 1.1 itohy if (dosno ~ /^ff[8a]./) {
192 1.1 itohy if (dosno ~ /^..8./)
193 1.1 itohy v2dosno = "ff5" substr(dosno, 4, 1)
194 1.1 itohy else
195 1.1 itohy v2dosno = "ff7" substr(dosno, 4, 1)
196 1.1 itohy print "\tcmpiw #0x200+14,__vernum+2 | 2.14"
197 1.1 itohy # print "\tbcss\tLv2doscall"
198 1.1 itohy print "\tbcss\t2f"
199 1.1 itohy print "\t.word\t0x" dosno
200 1.1 itohy if (!noret)
201 1.1 itohy # print "\tbras\tLedoscall"
202 1.1 itohy print "\tbras\t3f"
203 1.1 itohy # print "Lv2doscall:"
204 1.1 itohy print "2:"
205 1.1 itohy print "\t.word\t0x" v2dosno
206 1.1 itohy if (!noret)
207 1.1 itohy # print "Ledoscall:"
208 1.1 itohy print "3:"
209 1.1 itohy } else {
210 1.1 itohy print "\t.word\t0x" dosno
211 1.1 itohy }
212 1.1 itohy
213 1.1 itohy # no postprocess needed for dead calls
214 1.1 itohy if (noret)
215 1.1 itohy next
216 1.1 itohy
217 1.1 itohy if (super_jsr) {
218 1.1 itohy print "\tmovel\ta6,sp@"
219 1.1 itohy print "\tmoveal\tsp@(" argoff + 12 "),a6 | outregs"
220 1.1 itohy print "\tmovel\tsp@+,a6@(" 4 * 14 ")"
221 1.1 itohy print "\tmoveml\td0-d7/a0-a5,a6@"
222 1.1 itohy } else if (argbyte > 0) {
223 1.1 itohy # POP ARGS
224 1.1 itohy if (argbyte <= 8)
225 1.1 itohy print "\taddql\t#" argbyte ",sp"
226 1.1 itohy else
227 1.1 itohy print "\tlea\tsp@(" argbyte "),sp"
228 1.1 itohy }
229 1.1 itohy
230 1.1 itohy if (svreg) print "\tmoveml\tsp@+,d2-d7/a2-a6"
231 1.1 itohy if (opt_e) {
232 1.1 itohy if (e_strict) {
233 1.1 itohy print "\tcmpil\t#0xffffff00,d0"
234 1.1 itohy print "\tbcc\tDOS_CERROR"
235 1.1 itohy } else {
236 1.1 itohy print "\ttstl\td0"
237 1.1 itohy if (super) {
238 1.1 itohy # print "\tbpls\tLnoerr"
239 1.1 itohy print "\tbpls\t5f"
240 1.1 itohy print "\tnegl\td0"
241 1.1 itohy print "\tmovel\td0,_dos_errno"
242 1.1 itohy print "\tnegl\td0"
243 1.1 itohy # print "Lnoerr:"
244 1.1 itohy print "5:"
245 1.1 itohy } else if (e_alloc) {
246 1.1 itohy # print "\tbpls\tLnoerr"
247 1.1 itohy print "\tbpls\t5f"
248 1.1 itohy print "\tmovel\t#" errno_nomem ",_dos_errno"
249 1.1 itohy # print "Lnoerr:"
250 1.1 itohy print "5:"
251 1.1 itohy } else if (e_proc) {
252 1.1 itohy print "\tbmi\tDOS_PRCERROR"
253 1.1 itohy } else
254 1.1 itohy print "\tbmi\tDOS_CERROR"
255 1.1 itohy }
256 1.1 itohy }
257 1.1 itohy if (super)
258 1.1 itohy print "\tjmp\ta1@"
259 1.1 itohy else
260 1.1 itohy print "\trts"
261 1.1 itohy }
262