Home | History | Annotate | Line # | Download | only in libiocs
makeiocscalls.awk revision 1.1
      1 #! /usr/bin/awk -f
      2 #
      3 #	create IOCS call interface from iocs.h
      4 #
      5 #	written by Yasha (ITOH Yasufumi)
      6 #	public domain
      7 #
      8 #	$NetBSD: makeiocscalls.awk,v 1.1 1998/09/01 19:53:54 itohy Exp $
      9 
     10 BEGIN {
     11 	argsiz["l"] = 4; argsiz["w"] = 2
     12 	argsiz["lb31"] = 4; argsiz["wb8"] = 2
     13 
     14 	for (i = 0; i < 16; i++) {
     15 		reg = substr("d0d1d2d3d4d5d6d7a0a1a2a3a4a5a6a7", i*2+1, 2)
     16 		regno[reg] = i
     17 	}
     18 }
     19 
     20 $1 == "/*" && ($2 ~ /^[0-9a-f][0-9a-f]$/ || $2 == "(none)") {
     21 	funcnam=""
     22 	iocsno=$2
     23 	narg=0
     24 	retd2=0
     25 	err_d0=0
     26 	noret=0
     27 	c_md=0
     28 	b_super=0
     29 	sp_regst=0
     30 	b_curmod = 0
     31 	b_curpat = 0
     32 	b_scroll = 0
     33 	iocs_trap15=0
     34 	for (i = 3; i <= NF && $i != "*/" && $i != ";"; i++) {
     35 		arg[narg] = $i
     36 		narg++
     37 	}
     38 	if ($i == ";") {
     39 		# process opts
     40 		for (i++; i <= NF && $i != "*/"; i++) {
     41 			if ($i == "retd2")
     42 				retd2 = 1
     43 			else if ($i == "err_d0")
     44 				err_d0 = 1
     45 			else if ($i == "noret")
     46 				noret = 1
     47 			else if ($i == "c_md")
     48 				c_md = 1
     49 			else if ($i == "b_super")
     50 				b_super = 1
     51 			else if ($i == "sp_regst")
     52 				sp_regst = 1
     53 			else if ($i == "b_curmod")
     54 				b_curmod = 1
     55 			else if ($i == "b_curpat")
     56 				b_curpat = 1
     57 			else if ($i == "b_scroll")
     58 				b_scroll = 1
     59 			else if ($i == "trap15")
     60 				iocs_trap15 = 1
     61 			else {
     62 				print FILENAME ":" NR ": unknown opt", $i
     63 				exit(1)
     64 			}
     65 		}
     66 	}
     67 	if ($i != "*/") {
     68 		print FILENAME ":" NR ": malformed input line:" $0
     69 		exit(1)
     70 	}
     71 
     72 	# find out func name
     73 	printf "|"
     74 	for (i++; i <= NF; i++) {
     75 		printf " %s", $i
     76 		if ($i ~ /^\**IOCS_[A-Z0-9_]*$/) {
     77 			funcnam = $i
     78 			while (funcnam ~ /^\*/)
     79 				funcnam = substr(funcnam, 2, length(funcnam) -1)
     80 		}
     81 	}
     82 	print ""
     83 	if (!funcnam) {
     84 		print FILENAME ":" NR ": can't find function name"
     85 		exit(1)
     86 	}
     87 
     88 	# output assembly code
     89 	print "\t.text\n\t.even"
     90 	print "\t.globl\t_" funcnam
     91 	print "_" funcnam ":"
     92 
     93 	# SAVE REGISTERS
     94 	for (i = 0; i < 16; i++) {
     95 		savereg[i] = 0
     96 	}
     97 	for (i = 0; i < narg; i++) {
     98 		r = arg[i]
     99 		if (r ~ /^o[ad][0-7]$/)
    100 			r = substr(r, 2, 2)
    101 		else if (r ~ /^d[0-7]=/)
    102 			r = substr(r, 1, 2)
    103 		if (r != "d0" && !regno[r]) {
    104 			print FILENAME ":" NR ": unknown arg type:", arg[i]
    105 			exit(1)
    106 		}
    107 		if (r !~ /^[da][01]$/)		# may not be saved
    108 			savereg[regno[r]] = r
    109 	}
    110 	# count reg to save
    111 	nsave = 0
    112 	for (i = 0; i < 16; i++) {
    113 		if (savereg[i])
    114 			nsave++
    115 	}
    116 
    117 	if (iocs_trap15) {
    118 		print "\tmoveml\td2-d7/a2-a6,sp@-"
    119 		nsave = 11
    120 	} else if (nsave == 1 || nsave == 2){
    121 		# use movel
    122 		for (i = 0; i < 16; i++) {
    123 			if (savereg[i])
    124 				print "\tmovel\t" savereg[i] ",sp@-"
    125 		}
    126 	} else if (nsave > 2) {
    127 		# use moveml
    128 		saveregs = ""
    129 		for (i = 0; i < 16; i++) {
    130 			if (savereg[i])
    131 				saveregs = saveregs "/" savereg[i]
    132 		}
    133 		saveregs = substr(saveregs, 2, length(saveregs) - 1)
    134 		print "\tmoveml\t" saveregs ",sp@-"
    135 	}
    136 
    137 	# LOAD ARGS
    138 	# XXX this should be more intelligent
    139 	argoff = nsave * 4 + 4
    140 	# input arguments for IOCS call
    141 	iarg = ""
    142 	niarg = 0
    143 	iarg_incorder = 1
    144 	immarg = ""
    145 	nimmarg = 0
    146 	for (i = 0; i < narg && arg[i] ~ /^[ad]/; i++) {
    147 		a = arg[i]
    148 		if (a ~ /^d[1-7]=[0-9][0-9]*$/) {
    149 			immarg = immarg " " a
    150 			nimmarg++
    151 		} else {
    152 			if (iarg) {
    153 				if (regno[a1] >= regno[a])
    154 					iarg_incorder = 0
    155 			}
    156 			a1 = a
    157 			iarg = iarg "/" a
    158 			niarg++
    159 		}
    160 	}
    161 	oarg = ""
    162 	noarg = 0
    163 	for ( ; i < narg; i++) {
    164 		if (arg[i] ~ /^[o]d[0-7]/) {
    165 			oarg = oarg " " arg[i]
    166 			noarg++
    167 		} else {
    168 			print "unknown arg:", arg[i]
    169 			exit(1)
    170 		}
    171 	}
    172 	# remove leading char
    173 	iarg = substr(iarg, 2, length(iarg) - 1);
    174 	immarg = substr(immarg, 2, length(immarg) - 1);
    175 	oarg = substr(oarg, 2, length(oarg) - 1);
    176 	# load input args
    177 	if (niarg == 0)
    178 		;
    179 	else if (niarg == 1 && iarg !~ /\=/) {
    180 		print "\tmovel\tsp@(" argoff ")," iarg	"\t| 1arg"
    181 	} else if (iarg_incorder && iarg !~ /\=/) {
    182 		print "\tmoveml\tsp@(" argoff ")," iarg	"\t| inc order"
    183 	} else if (iarg == "a1/d1") {
    184 		print "\tmoveal\tsp@(" argoff "),a1"
    185 		print "\tmovel\tsp@(" argoff + 4 "),d1"
    186 	} else if (iarg == "d1/a1/d2") {
    187 		print "\tmoveml\tsp@(" argoff "),d1-d2/a1"
    188 		print "\texg\td2,a1"
    189 	} else if (iarg == "a1/a2/d1") {
    190 		print "\tmoveml\tsp@(" argoff "),a1/a2"
    191 		print "\tmovel\tsp@(" argoff + 8 "),d1"
    192 	} else if (iarg == "a1/a2/d1/d2") {
    193 		print "\tmoveml\tsp@(" argoff "),d1-d2/a1-a2"
    194 		print "\texg\td1,a1"
    195 		print "\texg\td2,a2"
    196 	} else if (iarg == "a1/d1/d2") {
    197 		print "\tmoveml\tsp@(" argoff "),d0-d2"
    198 		print "\tmovel\td0,a1"
    199 	} else if (iarg == "d1=bb") {
    200 		print "\tmoveq\t#0,d1"
    201 		print "\tmoveb\tsp@(" argoff + 3 "),d1"
    202 		print "\tlslw\t#8,d1"
    203 		print "\tmoveb\tsp@(" argoff + 7 "),d1"
    204 		niarg = 2
    205 	} else if (iarg == "d1=ww") {
    206 		print "\tmovew\tsp@(" argoff + 2 "),d1"
    207 		print "\tswap\td1"
    208 		print "\tmovew\tsp@(" argoff + 6 "),d1"
    209 		niarg = 2
    210 	} else if (iarg == "d1=hsv") {
    211 		print "\tmoveb\tsp@(" argoff + 3 "),d1"
    212 		print "\tswap\td1"
    213 		print "\tmoveb\tsp@(" argoff + 7 "),d1"
    214 		print "\tlslw\t#8,d1"
    215 		print "\tmoveb\tsp@(" argoff + 11 "),d1"
    216 		print "\tandl\t#0x00ff1f1f,d1"
    217 		niarg = 3
    218 	} else if (iarg == "a1/d1=bb") {
    219 		print "\tmoveal\tsp@(" argoff "),a1"
    220 		print "\tmoveq\t#0,d1"
    221 		print "\tmoveb\tsp@(" argoff + 7 "),d1"
    222 		print "\tlslw\t#8,d1"
    223 		print "\tmoveb	sp@(" argoff + 11 "),d1"
    224 		niarg = 3
    225 	} else if (iarg == "d1/d2=ww") {
    226 		print "\tmovel\tsp@(" argoff "),d1"
    227 		print "\tmovew\tsp@(" argoff + 6 "),d2"
    228 		print "\tswap\td2"
    229 		print "\tmovew\tsp@(" argoff + 10 "),d2"
    230 		niarg = 3
    231 	} else if (iarg == "d1=ww/a1") {
    232 		print "\tmoveml\tsp@(" argoff "),d0-d1/a1"
    233 		print "\tswap\td1"
    234 		print "\tmovew\td0,d1"
    235 		print "\tswap\td1"
    236 		niarg = 3
    237 	} else if (iarg == "d1=ww/d2=ww") {
    238 		print "\tmoveml\tsp@(" argoff "),d1-d2"
    239 		print "\tswap\td1"
    240 		print "\tmovew\td2,d1"
    241 		print "\tmovew\tsp@(" argoff + 10 "),d2"
    242 		print "\tswap\td2"
    243 		print "\tmovew\tsp@(" argoff + 14 "),d2"
    244 		niarg = 4
    245 	} else {
    246 		print "unsupported iarg:", iarg
    247 		exit(1)
    248 	}
    249 	argoff += niarg * 4
    250 
    251 	if (sp_regst) {
    252 		print "\tandl\t#0x80000000,d1"
    253 		print "\tmoveb\td0,d1"
    254 	}
    255 
    256 	if (b_curmod) {
    257 		print "\tmoveq\t#1,d0"
    258 		print "\tcmpl\td1,d0"
    259 #		print "\tbcss\tLerr"
    260 		print "\tbcss\t6f"
    261 	}
    262 
    263 	if (b_curpat) {
    264 		print "\ttstw\td2"
    265 #		print "\tbeqs\tLerr"
    266 		print "\tbeqs\t6f"
    267 	}
    268 
    269 	if (b_super) {
    270 		print "\tmoval\tsp@+,a0"
    271 		print "\tmoval\tsp@,a1"
    272 	}
    273 
    274 	# load imm args
    275 	if (nimmarg) {
    276 		for (i = 0; i < narg && arg[i] ~ /^[ad]/; i++) {
    277 			a = arg[i]
    278 			if (a ~ /^d[1-7]=[0-9][0-9]*$/) {
    279 				r = substr(a, 1, 2)
    280 				v = substr(a, 4, length(a)-3)
    281 				print "\tmoveq\t#" v "," r
    282 			}
    283 		}
    284 	}
    285 
    286 	if (c_md) {
    287 		# -1: flush(3), -2: set default(2), other: set by the value(4)
    288 		print "\tmovel\td2,d0"
    289 		print "\taddql\t#1,d0"
    290 		print "\tbeqs\tLcachemd"
    291 		print "\tmoveq\t#2,d1"
    292 		print "\taddql\t#1,d0"
    293 		print "\tbnes\tLcachemd"
    294 		print "\tmoveq\t#4,d1"
    295 		print "Lcachemd:"
    296 	}
    297 
    298 	if (b_scroll) {
    299 		# d1 has 16
    300 		print "\tcmpl\td1,d2"
    301 		print "\tbcss\tLscriocs"
    302 		print "\tmovel\td2,d1"
    303 		print "Lscriocs:"
    304 	}
    305 
    306 	if (iocs_trap15) {
    307 		print "\tmoveal\tsp@(" argoff "),a0	| inregs"
    308 		print "\tmoveml\ta0@,d0-d7/a1-a6"
    309 		argoff += 4
    310 	}
    311 
    312 	if (iocsno != "(none)") {
    313 		if (iocsno ~ /^[89abcdef]./)
    314 			iocsno = "ffffff" iocsno
    315 		print "\tmoveq\t#0x" iocsno ",d0"
    316 	}
    317 	print "\ttrap\t#15"
    318 
    319 	if (iocs_trap15) {
    320 		print "\tmoveal\tsp@(" argoff "),a0	| outregs"
    321 		print "\tmoveml\td0-d7/a1-a6,a0@"
    322 	}
    323 
    324 	if (err_d0 && noarg) {
    325 		print "\ttstl\td0"
    326 #		print "\tbnes\tLerr"
    327 		print "\tbnes\t6f"
    328 	}
    329 
    330 	# SAVERESULTS
    331 	# XXX this should be more intelligent
    332 	if (noarg == 0)
    333 		;
    334 	else if (oarg == "od2") {
    335 		print "\tmoveal\tsp@(" argoff "),a0"
    336 		argoff += 4
    337 		print "\tmovel\td2,a0@"
    338 	} else if (oarg == "od1 od2 od0") {
    339 		print "\tmoveml\tsp@(" argoff "),a0/a1"
    340 		argoff += 8
    341 		print "\tmovel\td1,a0@"
    342 		print "\tmovel\td2,a1@"
    343 		print "\tmoveal\tsp@(" argoff "),a0"
    344 		argoff += 4
    345 		print "\tmovel\td0,a0@"
    346 	} else if (oarg == "od2 od3") {
    347 		print "\tmoveml\tsp@(" argoff "),a0/a1"
    348 		argoff += 8
    349 		print "\tmovel\td2,a0@"
    350 		print "\tmovel\td3,a1@"
    351 	} else if (oarg == "od2 od3 od4 od5") {
    352 		print "\tmoveml\tsp@(" argoff "),a0/a1"
    353 		argoff += 8
    354 		print "\tmovel\td2,a0@"
    355 		print "\tmovel\td3,a1@"
    356 		print "\tmoveml\tsp@(" argoff "),a0/a1"
    357 		argoff += 8
    358 		print "\tmovel\td4,a0@"
    359 		print "\tmovel\td5,a1@"
    360 	} else {
    361 		print "unsupported oarg:", oarg
    362 		exit(1)
    363 	}
    364 
    365 	if ((err_d0 && noarg) || b_curmod || b_curpat)
    366 #		print "Lerr:"
    367 		print "6:"
    368 
    369 	# return value
    370 	if (retd2)
    371 		print "\tmovel\td2,d0"
    372 
    373 	# RESTORE REGISTERS
    374 	if (iocs_trap15) {
    375 		print "\tmoveml\tsp@+,d2-d7/a2-a6"
    376 	} else if (nsave == 1 || nsave == 2){
    377 		# use movel
    378 		for (i = 16 - 1; i >= 0; i--) {
    379 			if (savereg[i])
    380 				print "\tmovel\tsp@+," savereg[i]
    381 		}
    382 	} else if (nsave > 2) {
    383 		# use moveml
    384 		print "\tmoveml\tsp@+," saveregs
    385 	}
    386 
    387 
    388 	if (b_super)
    389 		print "\tjmp\ta0@"
    390 	else if (!noret)
    391 		print "\trts"
    392 }
    393