scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

gen.awk (2140B)


      1 
      2 BEGIN		{
      3 		FS = "\t"
      4 		printf "#include \"../../../inc/scc.h\"\n"\
      5 		       "#include \"../../as.h\"\n"\
      6 		       "#include \"../" family "/proc.h\"\n"
      7 		nop = 0; nvar = 0
      8 }
      9 		{sub(/#.*/,"")}
     10 
     11 $6 !~ cpu	{next}
     12 
     13 /^$/		{next}
     14 
     15 		{
     16 		if (opstart[$1] == 0) {
     17 			opstart[$1] = nvar
     18 			opnames[nop++] = $1
     19 		}
     20 		opcount[$1]++
     21 		opargs[nvar] = $2
     22 		opsize[nvar] = $3
     23 		opbytes[nvar] = ($4 == "none") ? "" : $4
     24 		opformat[nvar++] = $5
     25 		formats[$5] = 1
     26 }
     27 END		{
     28 		for (i in formats)
     29 			printf "Format %s;\n", i
     30 
     31 		printf "int nr_ins = %d;\n\n", nop
     32 		print "struct ins instab[] = {"
     33 		for (i = 0; i < nop; i++) {
     34 			n = opnames[i]
     35 			start = opstart[n]
     36 			end = start + opcount[n]
     37 			printf "\t{.str = \"%s\", .begin = %d, .end = %d},\n",
     38 			       n, start, end | "sort"
     39 		}
     40 		close("sort")
     41 		printf "};\n\n"
     42 
     43 		print "struct op optab[] = {"
     44 		for (i = 0; i < nvar; i++) {
     45 			printf "\t{\n" \
     46 			       "\t\t.size = %d,\n"\
     47 			       "\t\t.format = %s,\n",
     48 			       opsize[i], opformat[i]
     49 
     50 			if (opbytes[i] != "")
     51 				printf "\t\t.bytes = (unsigned char []) {%s},\n",
     52 				        opbytes[i]
     53 
     54 			a = str2args(opargs[i])
     55 			if (a != "")
     56 				printf "\t\t.args = (unsigned char []) {%s}\n", a
     57 
     58 			print "\t},"
     59 		}
     60 		print "};"
     61 }
     62 
     63 function str2args(s, args, i, out, n)
     64 {
     65 	n = split(s, args, /,/)
     66 	if (n == 0 || args[1] == "none")
     67 		return ""
     68 	for (i = 1; i <= n; i++) {
     69 		a = args[i]
     70 		if (match(a, /^imm8/)) {
     71 			out = out "AIMM8"
     72 		} else if (match(a, /^imm16/)) {
     73 			out = out "AIMM16"
     74 		} else if (match(a, /^imm32/)) {
     75 			out = out "AIMM32"
     76 		} else if (match(a, /^imm64/)) {
     77 			out = "AIMM64"
     78 		} else if (match(a, /^reg_p/)) {
     79 			out = out "AREG_PCLASS"
     80 		} else if (match(a, /^reg_q/)) {
     81 			out = out "AREG_QCLASS"
     82 		} else if (match(a, /^reg_r/)) {
     83 			out = out "AREG_RCLASS"
     84 		} else if (match(a, /^regA/)) {
     85 			out = out "AREG_A"
     86 		} else if (match(a, /^indir_HL/)) {
     87 			out = out "AINDER_HL"
     88 		} else {
     89 			print "wrong arg", a
     90 			exit 1
     91 		}
     92 		a = substr(a, RLENGTH+1)
     93 		if (a ~ /^\+$/) {
     94 			return out "|AREP"
     95 		} else if (a != "") {
     96 			print "wrong arg", a > "/dev/stderr"
     97 			exit 1
     98 		}
     99 		out = out ","
    100 	}
    101 	out = out "0"
    102 
    103 	return out
    104 }