scc

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

commit 33d749d24c838c86f206c8d1bf69ee47ea3f28f4
parent 417a9c29842043f30884372e9efa88035d8c8266
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 18 Dec 2017 20:34:30 +0100

[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.

Diffstat:
Mas/target/amd64/target.mk | 2+-
Mas/target/gen.awk | 102+++++++++++++++++++------------------------------------------------------------
Aas/target/x80/rules.dat | 37+++++++++++++++++++++++++++++++++++++
Aas/target/x86/rules.dat | 7+++++++
Mas/target/z80/target.mk | 2+-
5 files changed, 70 insertions(+), 80 deletions(-)

diff --git a/as/target/amd64/target.mk b/as/target/amd64/target.mk @@ -3,7 +3,7 @@ AMD64_LST = target/amd64/instbl.o target/amd64/ins.o target/amd64/proc.o $(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; \ diff --git a/as/target/gen.awk b/as/target/gen.awk @@ -5,6 +5,13 @@ BEGIN { "#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 @@ $6 !~ cpu {next} opformat[nvar++] = $5 formats[$5] = 1 } + END { for (i in formats) printf "Format %s;\n", i @@ -60,98 +68,36 @@ END { 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") 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" } 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 "," diff --git a/as/target/x80/rules.dat b/as/target/x80/rules.dat @@ -0,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 diff --git a/as/target/x86/rules.dat b/as/target/x86/rules.dat @@ -0,0 +1,7 @@ +^imm8 AIMM8 +^imm16 AIMM16 +^imm32 AIMM32 +^imm64 AIMM64 +^\(n\) ADIRECT +^sym ASYM +^string ASTR diff --git a/as/target/z80/target.mk b/as/target/z80/target.mk @@ -3,7 +3,7 @@ Z80_LST = target/z80/instbl.o target/z80/ins.o target/z80/proc.o target/x80/ins. $(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; \