Home | History | Annotate | Line # | Download | only in libdos
      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.4  itohy #	written by ITOH Yasufumi
      6  1.1  itohy #	public domain
      7  1.1  itohy #
      8  1.4  itohy #	$NetBSD: makedoscalls.awk,v 1.4 2011/02/21 02:31:59 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.2  itohy 	print "#include \"dos_asm.h\""
     15  1.1  itohy }
     16  1.1  itohy 
     17  1.1  itohy $1 == "/*" && $2 ~ /^ff[0-9a-f][0-9a-f]$/ {
     18  1.1  itohy 	funcnam=""
     19  1.1  itohy 	dosno=$2
     20  1.2  itohy 	ptrval=0
     21  1.1  itohy 	narg=0
     22  1.1  itohy 	ncarg=0		# number of 32bit C function argument
     23  1.1  itohy 	argbyte=0
     24  1.1  itohy 	opt_e=0
     25  1.1  itohy 	e_strict=0
     26  1.1  itohy 	e_alloc=0
     27  1.1  itohy 	e_proc=0
     28  1.1  itohy 	svreg=0
     29  1.1  itohy 	noret=0
     30  1.1  itohy 	super=0
     31  1.1  itohy 	super_jsr=0
     32  1.1  itohy 	alias=""
     33  1.1  itohy 	for (i = 3; i <= NF && $i != "*/" && $i != ";"; i++) {
     34  1.1  itohy 		arg[narg] = $i
     35  1.1  itohy 		narg++
     36  1.1  itohy 		if (argsiz[$i])
     37  1.1  itohy 			ncarg++
     38  1.1  itohy 	}
     39  1.1  itohy 	if ($i == ";") {
     40  1.1  itohy 		# process opts
     41  1.1  itohy 		for (i++; i <= NF && $i != "*/"; i++) {
     42  1.1  itohy 			if ($i == "e")
     43  1.1  itohy 				opt_e = 1
     44  1.1  itohy 			else if ($i == "estrct") {
     45  1.1  itohy 				opt_e = 1
     46  1.1  itohy 				e_strict = 1
     47  1.1  itohy 			} else if ($i == "ep") {
     48  1.1  itohy 				opt_e = 1
     49  1.1  itohy 				e_proc = 1
     50  1.1  itohy 			} else if ($i == "ealloc") {
     51  1.1  itohy 				opt_e = 1
     52  1.1  itohy 				e_alloc = 1
     53  1.1  itohy 			} else if ($i == "sv")
     54  1.1  itohy 				svreg = 1
     55  1.1  itohy 			else if ($i == "noret")
     56  1.1  itohy 				noret = 1
     57  1.1  itohy 			else if ($i == "alias") {
     58  1.1  itohy 				i++
     59  1.1  itohy 				alias = $i
     60  1.1  itohy 			} else if ($i == "super")
     61  1.1  itohy 				super = 1
     62  1.1  itohy 			else if ($i == "super_jsr")
     63  1.1  itohy 				super_jsr = 1
     64  1.1  itohy 			else {
     65  1.1  itohy 				print FILENAME ":" NR ": unknown opt", $i
     66  1.1  itohy 				exit(1)
     67  1.1  itohy 			}
     68  1.1  itohy 		}
     69  1.1  itohy 	}
     70  1.1  itohy 	if ($i != "*/") {
     71  1.1  itohy 		print FILENAME ":" NR ": malformed input line:" $0
     72  1.1  itohy 		exit(1)
     73  1.1  itohy 	}
     74  1.1  itohy 	# find out func name
     75  1.1  itohy 	printf "|"
     76  1.1  itohy 	for (i++; i <= NF; i++) {
     77  1.1  itohy 		printf " %s", $i
     78  1.1  itohy 		if ($i ~ /^\**DOS_[A-Z0-9_]*$/) {
     79  1.1  itohy 			funcnam = $i
     80  1.2  itohy 			while (funcnam ~ /^\*/) {
     81  1.1  itohy 				funcnam = substr(funcnam, 2, length(funcnam) -1)
     82  1.2  itohy 				ptrval = 1
     83  1.2  itohy 			}
     84  1.1  itohy 		}
     85  1.1  itohy 	}
     86  1.1  itohy 	print ""
     87  1.1  itohy 	if (!funcnam) {
     88  1.1  itohy 		print FILENAME ":" NR ": can't find function name"
     89  1.1  itohy 		exit(1)
     90  1.1  itohy 	}
     91  1.1  itohy 
     92  1.1  itohy 	# output assembly code
     93  1.2  itohy 	print "ENTRY_NOPROFILE(" funcnam ")"
     94  1.1  itohy 	if (alias) {
     95  1.2  itohy 		print "GLOBAL(" alias ")"
     96  1.1  itohy 	}
     97  1.2  itohy 	if (svreg)	print "\tmoveml\t%d2-%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.2  itohy 						print "\tmoveml\t%sp@(" argoff - 12 "),%d0-%d1/%a0-%a1"
    112  1.2  itohy 						print "\tmoveml\t%d0-%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.2  itohy 						print "\tmoveml\t%sp@(" argoff - 12 "),%d0-%d1/%a0-%a1"
    117  1.2  itohy 						print "\tmoveml\t%d1/%a0-%a1,%sp@-"
    118  1.2  itohy 						print "\tmovew\t%d0,%sp@-"
    119  1.1  itohy 						asz = 14
    120  1.1  itohy 						i -= 3
    121  1.1  itohy 					} else {
    122  1.2  itohy 						print "\tmoveml\t%sp@(" argoff - 8 "),%d0-%d1/%a0"
    123  1.2  itohy 						print "\tmoveml\t%d0-%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.2  itohy 					print "\tmovel\t%sp@(" argoff "),%sp@-"
    129  1.1  itohy 				}
    130  1.1  itohy 			} else if (a == "w")
    131  1.2  itohy 				print "\tmovew\t%sp@(" argoff + 2 "),%sp@-"
    132  1.1  itohy 			else if (a == "lb31") {
    133  1.2  itohy 				print "\tmovel\t%sp@(" argoff "),%d0"
    134  1.2  itohy 				print "\tbset\t#31,%d0"
    135  1.2  itohy 				print "\tmovel\t%d0,%sp@-"
    136  1.1  itohy 			} else if (a == "wb8") {
    137  1.2  itohy 				print "\tmovew\t%sp@(" argoff + 2 "),%d0"
    138  1.2  itohy 				print "\torw\t#0x100,%d0"
    139  1.2  itohy 				print "\tmovew\t%d0,%sp@-"
    140  1.1  itohy 			} else if (a == "wb15") {
    141  1.2  itohy 				print "\tmovew\t%sp@(" argoff + 2 "),%d0"
    142  1.2  itohy 				print "\torw\t#0x8000,%d0"
    143  1.2  itohy 				print "\tmovew\t%d0,%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.2  itohy 				print "\tclrw\t%sp@-"
    157  1.1  itohy 			else
    158  1.2  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.2  itohy 				print "\tclrl\t%sp@-"
    165  1.1  itohy 			else if (val <= 32767)
    166  1.1  itohy 				print "\tpea\t" val ":w"
    167  1.1  itohy 			else
    168  1.2  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.2  itohy 			print "\tmoveb\t%sp@(7),%d0"
    173  1.2  itohy 			print "\tlslw\t#8,%d0"
    174  1.2  itohy 			print "\tmoveb\t%sp@(11),%d0"
    175  1.2  itohy 			print "\tmovew\t%d0,%sp@-"
    176  1.1  itohy 		} else if (a == "super" && narg == 1) {
    177  1.1  itohy 			# only for DOS_SUPER
    178  1.2  itohy 			print "\tmoveal\t%sp@+,%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.2  itohy 		print "\tmoveal\t%sp@(" argoff + 8 "),%a0	| inregs"
    188  1.2  itohy 		print "\tmoveml\t%a0@,%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.2  itohy 		print "\tcmpiw	#0x200+14,_C_LABEL(_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.2  itohy 		print "\tmovel\t%a6,%sp@"
    219  1.2  itohy 		print "\tmoveal\t%sp@(" argoff + 12 "),%a6	| outregs"
    220  1.2  itohy 		print "\tmovel\t%sp@+,%a6@(" 4 * 14 ")"
    221  1.2  itohy 		print "\tmoveml\t%d0-%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.2  itohy 			print "\taddql\t#" argbyte ",%sp"
    226  1.1  itohy 		else
    227  1.2  itohy 			print "\tlea\t%sp@(" argbyte "),%sp"
    228  1.1  itohy 	}
    229  1.1  itohy 
    230  1.2  itohy 	if (svreg)	print "\tmoveml\t%sp@+,%d2-%d7/%a2-%a6"
    231  1.3  itohy 	if (ptrval)	print "#ifdef __SVR4_ABI__\n\tmoveal\t%d0,%a0\n#endif"
    232  1.1  itohy 	if (opt_e) {
    233  1.1  itohy 		if (e_strict) {
    234  1.2  itohy 			print "\tcmpil\t#0xffffff00,%d0"
    235  1.2  itohy 			print "\tbcc\tCERROR"
    236  1.1  itohy 		} else  {
    237  1.2  itohy 			print "\ttstl\t%d0"
    238  1.1  itohy 			if (super) {
    239  1.1  itohy #				print "\tbpls\tLnoerr"
    240  1.1  itohy 				print "\tbpls\t5f"
    241  1.2  itohy 				print "\tnegl\t%d0"
    242  1.2  itohy 				print "\tmovel\t%d0,_C_LABEL(dos_errno)"
    243  1.2  itohy 				print "\tnegl\t%d0"
    244  1.1  itohy #				print "Lnoerr:"
    245  1.1  itohy 				print "5:"
    246  1.1  itohy 			} else if (e_alloc) {
    247  1.1  itohy #				print "\tbpls\tLnoerr"
    248  1.1  itohy 				print "\tbpls\t5f"
    249  1.2  itohy 				print "\tmovel\t#" errno_nomem ",_C_LABEL(dos_errno)"
    250  1.1  itohy #				print "Lnoerr:"
    251  1.1  itohy 				print "5:"
    252  1.1  itohy 			} else if (e_proc) {
    253  1.2  itohy 				print "\tbmi\tPRCERROR"
    254  1.1  itohy 			} else
    255  1.2  itohy 				print "\tbmi\tCERROR"
    256  1.1  itohy 		}
    257  1.1  itohy 	}
    258  1.1  itohy 	if (super)
    259  1.2  itohy 		print "\tjmp\t%a1@"
    260  1.3  itohy 	else
    261  1.1  itohy 		print "\trts"
    262  1.1  itohy }
    263