scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

mktbl.awk (2140B)


      1 
      2 BEGIN		{
      3 		nvar=0
      4 		printf "#include <scc/mach.h>\n"\
      5 		       "#include <scc/scc.h>\n"\
      6 		       "#include \"../as.h\"\n"\
      7 		       "#include \"proc.h\"\n"
      8 
      9 		rules = "opers.dat"
     10 		while (getline < rules > 0) {
     11 			regex[++nregs] = $1
     12 			value[nregs] = $2
     13 		}
     14 		close(rules)
     15 }
     16 		{sub(/#.*/,"")}
     17 
     18 $7 !~ cpu	{next}
     19 
     20 /^$/		{next}
     21 
     22 		{
     23 		if (op[$1] == 0) {
     24 			op[$1] = 1
     25 			opstart[$1] = nvar
     26 			opnames[nop++] = $1
     27 		}
     28 		opcount[$1]++
     29 		opargs[nvar] = $3
     30 		opsize[nvar] = $4
     31 		opbytes[nvar] = ($5 == "none") ? "" : $5
     32 		opformat[nvar++] = $6
     33 		formats[$6] = 1
     34 }
     35 
     36 END		{
     37 		for (i in formats)
     38 			printf "Format %s;\n", i
     39 
     40 		print "struct ins instab[] = {"
     41 		for (i = 0; i < nop; i++) {
     42 			n = opnames[i]
     43 			start = opstart[n]
     44 			end = start + opcount[n]
     45 			printf "\t{.str = \"%s\", .begin = %d, .end = %d},\n",
     46 			       n, start, end
     47 		}
     48 		printf "};\n\n"
     49 
     50 		for (i = 0; i < nop; i++)
     51 			print opnames[i] | "../lexh"
     52 		close("../lexh")
     53 
     54 		print "struct op optab[] = {"
     55 		for (i = 0; i < nvar; i++) {
     56 			printf "\t/* %d */\n", i
     57 			printf "\t{\n" \
     58 			       "\t\t.size = %d,\n"\
     59 			       "\t\t.format = %s,\n",
     60 			       opsize[i], opformat[i]
     61 
     62 			if (opbytes[i] != "")
     63 				printf "\t\t.bytes = (unsigned char [%d]) {%s},\n",
     64 				        opsize[i],
     65 				        opbytes[i]
     66 
     67 			a = str2args(opargs[i])
     68 			if (a != "")
     69 				printf "\t\t.args = (unsigned char []) {%s}\n", a
     70 
     71 			print "\t},"
     72 		}
     73 		print "};"
     74 }
     75 
     76 function str2args(s, args, i, j, out, n, found)
     77 {
     78 	n = split(s, args, /,/)
     79 	if (n == 0 || args[1] == "none")
     80 		return ""
     81 	for (i = 1; i <= n; i++) {
     82 		a = args[i]
     83 		found = 0
     84 
     85 		if (a ~ /\?$/)
     86 			out = out "AOPT ,"
     87 		else if (a ~ /\+$/)
     88 			out = out "AREP ,"
     89 
     90 		for (j = 1; j <= nregs; j++) {
     91 			if (match(a, "^" regex[j])) {
     92 				out = out value[j]
     93 				found = 1
     94 				break
     95 			}
     96 		}
     97 
     98 		if (!found) {
     99 			print FILENAME ":" NR ":" \
    100 			      $0 ":wrong arg in opcode list", a > "/dev/stderr"
    101 			exit 1
    102 		}
    103 
    104 		a = substr(a, RLENGTH+1)
    105 		sub(/\?$/, "", a)
    106 		sub(/\+$/, "", a)
    107 		if (a != "") {
    108 			print FILENAME ":" NR ":" \
    109 			      $0 ": trailing chars: ", a > "/dev/stderr"
    110 			exit 1
    111 		}
    112 		out = out ","
    113 	}
    114 	out = out "0"
    115 
    116 	return out
    117 }