Skip to content

Commit

Permalink
Import the 6809 Compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Toomey committed Jul 18, 2024
1 parent 26b6e0f commit 17876ff
Show file tree
Hide file tree
Showing 402 changed files with 19,140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 63_QBE/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,5 @@ pretty good.
I would like to learn about the concepts that QBE embodies, e.g.
SSA and register allocation. So, perhaps I'll go off and do some
research and write that up.

[Next step](../64_6809_Target/Readme.md)
61 changes: 61 additions & 0 deletions 64_6809_Target/6809triple_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
#
# Build the 6809 compiler binaries. Also create front-end shell
# scripts so that we can run them as if they were native programs.
#
# Do this twice to run the triple test on the 6809 compiler binaries.
#
make clean l1dirs.h
mkdir L1
wcc -o L1/wcc wcc.c
cc -o L1/cpeep cpeep.c
wcc -m6809 -o L1/_cscan scan.c misc.c
wcc -m6809 -o L1/_detok detok.c tstring.c
wcc -m6809 -o L1/_detree -DDETREE detree.c misc.c tree.c
wcc -m6809 -o L1/_desym desym.c
wcc -m6809 -o L1/_cparse6809 -DWRITESYMS decl.c expr.c misc.c opt.c \
parse.c stmt.c sym.c tree.c targ6809.c tstring.c types.c
wcc -m6809 -o L1/_cgen6809 -DSPLITSWITCH cg6809.c cgen.c gen.c misc.c sym.c \
targ6809.c tree.c types.c
rm -f l1dirs.h dirs.h

# Make the front-end shell scripts
dir=`pwd`/L1
for i in cscan detok detree desym cparse6809 cgen6809
do cat << EOF > L1/$i
#!/bin/sh
exec emu6809 $dir/_$i \$*
EOF

chmod +x L1/$i
done

# exit 0

# Now we do it all again for L2
make l2dirs.h
mkdir L2
wcc -o L2/wcc wcc.c
cc -o L2/cpeep cpeep.c
L1/wcc -m6809 -v -o L2/_cscan scan.c misc.c
L1/wcc -m6809 -v -o L2/_detok detok.c tstring.c
L1/wcc -m6809 -v -o L2/_detree -DDETREE detree.c misc.c tree.c
L1/wcc -m6809 -v -o L2/_desym desym.c
L1/wcc -m6809 -v -o L2/_cparse6809 -DWRITESYMS decl.c expr.c misc.c opt.c \
parse.c stmt.c sym.c tree.c targ6809.c tstring.c types.c
L1/wcc -m6809 -v -o L2/_cgen6809 -DSPLITSWITCH cg6809.c cgen.c gen.c misc.c sym.c \
targ6809.c tree.c types.c
rm -f l2dirs.h dirs.h

# Make the front-end shell scripts
dir=`pwd`/L2
for i in cscan detok detree desym cparse6809 cgen6809
do cat << EOF > L2/$i
#!/bin/sh
exec emu6809 $dir/_$i \$*
EOF

chmod +x L2/$i
done

exit 0
198 changes: 198 additions & 0 deletions 64_6809_Target/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# Define the location of the include directory
# and the location to install the compiler binary.
# You will need to make TOPDIR and make it writable by you.
#
TOPDIR=/opt/wcc
BINDIR=$(TOPDIR)/bin
INCQBEDIR=$(TOPDIR)/include/qbe
INC6809DIR=$(TOPDIR)/include/6809
LIB6809DIR=$(TOPDIR)/lib/6809

CFLAGS=-g -Wall
# CFLAGS+= --coverage
# then gcov *gcno

# Header files and C files for the QBE and 6809 parser phase
#
PARSEH= cg.h data.h decl.h defs.h expr.h gen.h misc.h opt.h \
parse.h stmt.h sym.h target.h tree.h types.h
PARSEC6809= decl.c expr.c misc.c opt.c parse.c stmt.c sym.c tree.c \
targ6809.c tstring.c types.c
PARSECQBE= decl.c expr.c misc.c opt.c parse.c stmt.c sym.c tree.c \
targqbe.c tstring.c types.c

# Header files and C files for the QBE and 6809 code generator phase
#
GENH= cg.h data.h defs.h gen.h misc.h sym.h target.h tree.h types.h
GENC6809= cg6809.c cgen.c gen.c misc.c sym.c targ6809.c tree.c types.c
GENCQBE= cgqbe.c cgen.c gen.c misc.c sym.c targqbe.c tree.c types.c

# These executables are compiled by the existing C compiler on your system.
#
all: wcc cscan detok detree desym cpeep \
cparse6809 cgen6809 cparseqbe cgenqbe

wcc: wcc.c wcc.h l0dirs.h
cc -o wcc $(CFLAGS) wcc.c

cscan: scan.c defs.h misc.h misc.c
cc -o cscan $(CFLAGS) scan.c misc.c

cpeep: cpeep.c
cc -o cpeep $(CFLAGS) cpeep.c

cparse6809: $(PARSEC6809) $(PARSEH)
cc -o cparse6809 $(CFLAGS) -DWRITESYMS $(PARSEC6809)

cgen6809: $(GENC6809) $(GENH)
cc -o cgen6809 $(CFLAGS) $(GENC6809)

cparseqbe: $(PARSECQBE) $(PARSEH)
cc -o cparseqbe $(CFLAGS) -DWRITESYMS $(PARSECQBE)

cgenqbe: $(GENCQBE) $(GENH)
cc -o cgenqbe $(CFLAGS) $(GENCQBE)

desym: desym.c defs.h types.h
cc -o desym $(CFLAGS) desym.c

detok: detok.c tstring.c defs.h
cc -o detok $(CFLAGS) detok.c tstring.c

detree: detree.c misc.c tree.c misc.h defs.h tree.h
cc -o detree $(CFLAGS) -DDETREE detree.c misc.c tree.c

l0dirs.h:
echo "#define TOPDIR \"$(TOPDIR)\"" > l0dirs.h
echo "#define INCQBEDIR \"$(INCQBEDIR)\"" >> l0dirs.h
echo "#define INC6809DIR \"$(INC6809DIR)\"" >> l0dirs.h
echo "#define BINDIR \"$(BINDIR)\"" >> l0dirs.h
echo "#define LIB6809DIR \"$(LIB6809DIR)\"" >> l0dirs.h
cp l0dirs.h dirs.h

# Install the compiler built by the external compiler
#
install: all
@if [ ! -d $(TOPDIR) ]; then echo "$(TOPDIR) doesn't exit, create it writeable by you"; exit 1; fi
mkdir -p $(INC6809DIR)
mkdir -p $(INCQBEDIR)
mkdir -p $(BINDIR)
mkdir -p $(LIB6809DIR)
rsync -a --exclude RCS include/qbe/. $(INCQBEDIR)
rsync -a --exclude RCS include/6809/. $(INC6809DIR)
rsync -a --exclude Makefile --exclude RCS --exclude crt0.s \
lib/6809/. $(LIB6809DIR)
cp wcc cscan detok detree desym cpeep \
cparse6809 cgen6809 \
cparseqbe cgenqbe $(BINDIR)

# These rules are for the compiler to build itself.
# Use the L1 directory to hold the binaries.
#
l1dirs.h: TOPDIR= `pwd`
l1dirs.h: BINDIR= `pwd`/L1

l1dirs.h:
echo "#define TOPDIR \"$(TOPDIR)\"" > l1dirs.h
echo "#define INCQBEDIR \"$(INCQBEDIR)\"" >> l1dirs.h
echo "#define INC6809DIR \"$(INC6809DIR)\"" >> l1dirs.h
echo "#define BINDIR \"$(BINDIR)\"" >> l1dirs.h
echo "#define LIB6809DIR \"$(LIB6809DIR)\"" >> l1dirs.h
cp l1dirs.h dirs.h

# These executables are compiled by our own compiler
#
l1bins: install L1/wcc L1/cscan L1/cparseqbe L1/cgenqbe \
L1/desym L1/detok L1/detree

L1/wcc: wcc.c wcc.h l1dirs.h
mkdir -p L1
wcc -o L1/wcc wcc.c

L1/cscan: scan.c defs.h misc.h misc.c
wcc -o L1/cscan scan.c misc.c

L1/cparseqbe: $(PARSECQBE) $(PARSEH)
wcc -o L1/cparseqbe -DWRITESYMS $(PARSECQBE)

L1/cgenqbe: $(GENCQBE) $(GENH)
wcc -o L1/cgenqbe $(GENCQBE)

L1/desym: desym.c defs.h types.h
wcc -o L1/desym desym.c

L1/detok: detok.c tstring.c defs.h
wcc -o L1/detok detok.c tstring.c

L1/detree: detree.c misc.c tree.c misc.h defs.h tree.h
wcc -o L1/detree -DDETREE detree.c misc.c tree.c

# These rules are for the compiler to build itself a second time.
# If the binaries match those built the first time, we know that
# the compiler can successfully compile itself.
# Use the L2 directory to hold the binaries.
#
l2dirs.h: TOPDIR= `pwd`
l2dirs.h: BINDIR= `pwd`/L2

l2dirs.h:
echo "#define TOPDIR \"$(TOPDIR)\"" > l2dirs.h
echo "#define INCQBEDIR \"$(INCQBEDIR)\"" >> l2dirs.h
echo "#define INC6809DIR \"$(INC6809DIR)\"" >> l2dirs.h
echo "#define BINDIR \"$(BINDIR)\"" >> l2dirs.h
echo "#define LIB6809DIR \"$(LIB6809DIR)\"" >> l2dirs.h
cp l2dirs.h dirs.h

# These executables are compiled by our own compiler
#
l2bins: l1bins L2/wcc L2/cscan L2/cparseqbe L2/cgenqbe \
L2/desym L2/detok L2/detree

L2/wcc: wcc.c wcc.h l2dirs.h
mkdir -p L2
L1/wcc -o L2/wcc wcc.c

L2/cscan: scan.c defs.h misc.h misc.c
L1/wcc -o L2/cscan scan.c misc.c

L2/cparseqbe: $(PARSECQBE) $(PARSEH)
L1/wcc -o L2/cparseqbe -DWRITESYMS $(PARSECQBE)

L2/cgenqbe: $(GENCQBE) $(GENH)
L1/wcc -o L2/cgenqbe $(GENCQBE)

L2/desym: desym.c defs.h types.h
L1/wcc -o L2/desym desym.c

L2/detok: detok.c tstring.c defs.h
L1/wcc -o L2/detok detok.c tstring.c

L2/detree: detree.c misc.c tree.c misc.h defs.h tree.h
L1/wcc -o L2/detree -DDETREE detree.c misc.c tree.c

# Do the triple test: build the compiler with the external compiler,
# build the compiler with itself and then use this compiler to
# build the compiler again. The binaries should be identical. Note
# that the `wcc` binaries are different because the TOPDIR is different.
#
triple: l2bins
md5sum L1/* L2/* | sort

# Clean up all versions of the compiler
clean:
rm -f wcc cscan detok detree desym cpeep \
cparse6809 cgen6809 \
cparseqbe cgenqbe
rm -f *.o *.s out a.out dirs.h l?dirs.h *.gc??
rm -rf L1 L2

# Run the tests with the compiler built with the external compiler
#
test: install tests/runtests
(cd tests; chmod +x runtests; ./runtests qbe)

tests: test

# Run the 6809 tests
6test: install tests/runtests
(cd tests; chmod +x runtests; ./runtests 6809)
25 changes: 25 additions & 0 deletions 64_6809_Target/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Warren's In-progress Work on a 6809 C Compiler

Basic instructions (for now)!

- Make sure `as6809` and `ld6809` are on your `PATH`
- untar `optwcc.tgz` in `/opt`
- If you want the amd64 backend, you'll need to install QBE:
https://c9x.me/compile/
- You will need `/opt/wcc/bin/` on your `PATH`, or at least
`/opt/wcc/bin/wcc` on your `PATH`. I usually symlink this
so I can type `wcc` and get it to run.

Now you should be able to `make; make install` which builds
the compiler with the 6809 and QBE backends and installs
them in `/opt/wcc/bin`. The header files are copied into
`/opt/wcc/include` and the 6809 `crt0.o` into `/opt/wcc/lib/6809`.

Now you should be able to:

- `make test` to run the amd64 tests (assuming you have QBE)
- `make 6test` to run the 6809 tests
- `make triple` to compile the amd64 compiler with itself
and check that the binary checksums are the same.

`make clean` should clean up everything.
Loading

0 comments on commit 17876ff

Please sign in to comment.