scc

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

mktbl.awk (2089B)


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