ref: 69ba15ff2d2a095092354dcbf0125457b7857b89
parent: 3102bca6cc3346fec2531740b1af9bb3729ebc3e
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Dec 18 15:34:30 EST 2017
[as] Read gen.awk rules from a file In this way the same gen.awk can be used from different architectures without repeating any code.
--- a/as/target/amd64/target.mk
+++ b/as/target/amd64/target.mk
@@ -3,7 +3,7 @@
$(AMD64_LST): target/x86/proc.h
-target/amd64/instbl.c: target/gen.awk target/x86/x86.dat
+target/amd64/instbl.c: target/gen.awk target/x86/x86.dat target/x86/rules.dat
set -e ;\
rm -f $@;\
trap "rm -f $$$$.c" 0 2 3; \
--- a/as/target/gen.awk
+++ b/as/target/gen.awk
@@ -5,6 +5,13 @@
"#include \"../../as.h\"\n"\
"#include \"../" family "/proc.h\"\n"
nop = 0; nvar = 0
+
+ rules = "target/" family "/rules.dat"
+ while (getline < rules > 0) {
+ regex[++nregs] = $1
+ value[nregs] = $2
+ }
+ close "target/rules.awk"
}
{sub(/#.*/,"")}
@@ -24,6 +31,7 @@
opformat[nvar++] = $5
formats[$5] = 1
}
+
END {
for (i in formats)
printf "Format %s;\n", i
@@ -60,7 +68,7 @@
print "};"
}
-function str2args(s, args, i, out, n)
+function str2args(s, args, i, j, out, n, found)
{
n = split(s, args, /,/)
if (n == 0 || args[1] == "none")
@@ -67,84 +75,21 @@
return ""
for (i = 1; i <= n; i++) {
a = args[i]
- if (match(a, /^imm8/)) {
- out = out "AIMM8"
- } else if (match(a, /^imm16/)) {
- out = out "AIMM16"
- } else if (match(a, /^imm32/)) {
- out = out "AIMM32"
- } else if (match(a, /^imm64/)) {
- out = out "AIMM64"
- } else if (match(a, /^imm3/)) {
- out = out "AIMM3"
- } else if (match(a, /^rst/)) {
- out = out "ARST"
- } else if (match(a, /^\(IY\+n\)/)) {
- out = out "AINDEX_IY"
- } else if (match(a, /^\(IX\+n\)/)) {
- out = out "AINDEX_IX"
- } else if (match(a, /^ss/)) {
- out = out "AREG_SSCLASS"
- } else if (match(a, /^cc/)) {
- out = out "AREG_CCCLASS"
- } else if (match(a, /^dd/)) {
- out = out "AREG_DDCLASS"
- } else if (match(a, /^qq/)) {
- out = out "AREG_QQCLASS"
- } else if (match(a, /^rr/)) {
- out = out "AREG_RRCLASS"
- } else if (match(a, /^pp/)) {
- out = out "AREG_PPCLASS"
- } else if (match(a, /^p/)) {
- out = out "AREG_PCLASS"
- } else if (match(a, /^q/)) {
- out = out "AREG_QCLASS"
- } else if (match(a, /^r/)) {
- out = out "AREG_RCLASS"
- } else if (match(a, /^R/)) {
- out = out "AREG_R"
- } else if (match(a, /^\(DE\)/)) {
- out = out "AINDER_DE"
- } else if (match(a, /^\(BC\)/)) {
- out = out "AINDER_BC"
- } else if (match(a, /^\(HL\)/)) {
- out = out "AINDER_HL"
- } else if (match(a, /^\(SP\)/)) {
- out = out "AINDER_SP"
- } else if (match(a, /^\(C\)/)) {
- out = out "AINDER_C"
- } else if (match(a, /^SP/)) {
- out = out "AREG_SP"
- } else if (match(a, /^HL/)) {
- out = out "AREG_HL"
- } else if (match(a, /^DE/)) {
- out = out "AREG_DE"
- } else if (match(a, /^IX/)) {
- out = out "AREG_IX"
- } else if (match(a, /^IY/)) {
- out = out "AREG_IY"
- } else if (match(a, /^AF'/)) {
- out = out "AREG_AF_"
- } else if (match(a, /^AF/)) {
- out = out "AREG_AF"
- } else if (match(a, /^A/)) {
- out = out "AREG_A"
- } else if (match(a, /^F/)) {
- out = out "AREG_F"
- } else if (match(a, /^\(n\)/)) {
- out = out "ADIRECT"
- } else if (match(a, /^I/)) {
- out = out "AREG_I"
- } else if (match(a, /^0/)) {
- out = out "AZERO"
- } else if (match(a, /^sym/)) {
- out = out "ASYM"
- } else if (match(a, /^string/)) {
- out = out "ASTR"
- } else {
- print FILENAME ":" NR ":" $0 ":wrong arg", a > "/dev/stderr"
+ found = 0
+ for (j = 1; j <= nregs; j++) {
+ if (match(a, regex[j])) {
+ out = out value[j]
+ found = 1
+ break
+ }
+ }
+
+ if (!found) {
+ print FILENAME ":" NR ":" \
+ $0 ":wrong arg", a > "/dev/stderr"
exit 1
}
+
a = substr(a, RLENGTH+1)
if (a ~ /^\+$/) {
return out "|AREP"
@@ -151,7 +96,8 @@
} else if (a ~ /^\?$/) {
return out "|AOPT"
} else if (a != "") {
- print FILENAME ":" NR ":" $0 ":wrong arg", a > "/dev/stderr"
+ print FILENAME ":" NR ":" \
+ $0 ": trailing chars: ", a > "/dev/stderr"
exit 1
}
out = out ","
--- /dev/null
+++ b/as/target/x80/rules.dat
@@ -1,0 +1,37 @@
+^imm8 AIMM8
+^imm16 AIMM16
+^imm32 AIMM32
+^imm64 AIMM64
+^imm3 AIMM3
+^rst ARST
+^\(IY\+n\) AINDEX_IY
+^\(IX\+n\) AINDEX_IX
+^ss AREG_SSCLASS
+^cc AREG_CCCLASS
+^dd AREG_DDCLASS
+^qq AREG_QQCLASS
+^rr AREG_RRCLASS
+^pp AREG_PPCLASS
+^p AREG_PCLASS
+^q AREG_QCLASS
+^r AREG_RCLASS
+^R AREG_R
+^\(DE\) AINDER_DE
+^\(BC\) AINDER_BC
+^\(HL\) AINDER_HL
+^\(SP\) AINDER_SP
+^\(C\) AINDER_C
+^SP AREG_SP
+^HL AREG_HL
+^DE AREG_DE
+^IX AREG_IX
+^IY AREG_IY
+^AF' AREG_AF_
+^AF AREG_AF
+^A AREG_A
+^F AREG_F
+^\(n\) ADIRECT
+^I AREG_I
+^0 AZERO
+^sym ASYM
+^string ASTR
--- /dev/null
+++ b/as/target/x86/rules.dat
@@ -1,0 +1,7 @@
+^imm8 AIMM8
+^imm16 AIMM16
+^imm32 AIMM32
+^imm64 AIMM64
+^\(n\) ADIRECT
+^sym ASYM
+^string ASTR
--- a/as/target/z80/target.mk
+++ b/as/target/z80/target.mk
@@ -3,7 +3,7 @@
$(Z80_LST): target/x80/proc.h
-target/z80/instbl.c: target/gen.awk target/x80/x80.dat
+target/z80/instbl.c: target/gen.awk target/x80/x80.dat target/x80/rules.dat
set -e ;\
rm -f $@;\
trap "rm -f $$$$.c" 0 2 3; \