hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit cae8360ac9e7b5358c2eb38c43051c3052fbba81
parent e35c32485d2690e671c93d0ad7ef63652cc19798
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  4 Feb 2021 13:03:36 -0500

Write script to generate stdlib.mk

Diffstat:
MMakefile | 2+-
Agen-stdlib | 168+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amk/gen-stdlib.sh | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amk/stdlib.mk | 171+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dstdlib.mk | 177-------------------------------------------------------------------------------
5 files changed, 399 insertions(+), 178 deletions(-)

diff --git a/Makefile b/Makefile @@ -14,7 +14,7 @@ hare: @printf 'AS\t$@\n' @$(AS) -g -o $@ $< -include stdlib.mk +include mk/stdlib.mk hare_srcs=\ main.ha diff --git a/gen-stdlib b/gen-stdlib @@ -0,0 +1,168 @@ +#!/bin/sh +# The purpose of this script is to generate make targets for the Hare standard +# library. If you are adding new modules to the standard library, write a +# function for that module (e.g. encoding_utf8), and call the following helper +# commands: +# +# gen_srcs module::name list.ha of.ha sources.ha +# gen_ssa module::name list of module::names yours depends on +# gen_lib module::name +# +# Then add your module to the list of modules at the bottom, making sure it +# comes after any of its dependencies. +# +# Then run ./gen-stdlib > mk/stdlib.mk to generate new rules. + +srcdir="$(dirname "$0")" +eval ". $srcdir/mk/gen-stdlib.sh" +cache=HARECACHE + +rt() { + # This one is complicated, don't use it as a reference for other modules + printf '# rt\n' + printf '%s\n' 'rtscript=$(STDLIB)/rt/hare.sc' + gen_srcs rt \ + '$(PLATFORM)/abort.ha' \ + '$(PLATFORM)/env.ha' \ + '$(PLATFORM)/errno.ha' \ + '$(PLATFORM)/start.ha' \ + '$(PLATFORM)/syscallno$(ARCH).ha' \ + '$(PLATFORM)/syscalls.ha' \ + '$(PLATFORM)/segmalloc.ha' \ + malloc.ha \ + memcpy.ha \ + memset.ha \ + strcmp.ha \ + start-test.ha + gen_ssa rt + cat <<EOF +\$($cache)/rt/syscall.o: \$(STDLIB)/rt/\$(PLATFORM)/syscall\$(ARCH).s + @printf 'AS \t\$@\n' + @mkdir -p \$(HARECACHE)/rt + @as -o \$@ \$< + +\$($cache)/rt/start.o: \$(STDLIB)/rt/\$(PLATFORM)/start.s + @printf 'AS \t\$@\n' + @mkdir -p \$($cache)/rt + @as -o \$@ \$< + +\$($cache)/rt/rt.a: \$($cache)/rt/rt.o \$($cache)/rt/syscall.o + @printf 'AR\t\$@\n' + @\$(AR) -csr \$@ \$($cache)/rt/rt.o \$($cache)/rt/syscall.o + +stdlib_rt=\$($cache)/rt/rt.a +stdlib_start=\$($cache)/rt/start.o + +EOF +} + +ascii() { + printf '# ascii\n' + gen_srcs ascii \ + ctype.ha + gen_ssa ascii + gen_lib ascii +} + +bytes() { + printf '# bytes\n' + gen_srcs bytes \ + copy.ha \ + equal.ha \ + index.ha \ + reverse.ha + gen_ssa bytes + gen_lib bytes +} + +types() { + printf '# types\n' + gen_srcs types \ + limits.ha \ + classes.ha \ + 'arch$(ARCH).ha' + gen_ssa types + gen_lib types +} + +strconv() { + printf '# strconv\n' + gen_srcs strconv \ + itos.ha \ + utos.ha \ + numeric.ha + gen_ssa strconv types + gen_lib strconv +} + +io() { + # XXX: Sort me only after fixing forward references to alias types + # Sort it to see what the problem is if you don't understand + printf '# io\n' + gen_srcs io \ + types.ha \ + copy.ha \ + println.ha \ + stream.ha + gen_ssa io + gen_lib io +} + +encoding_utf8() { + # XXX: Also has ordering issues + printf '# encoding::utf8\n' + gen_srcs encoding::utf8 \ + rune.ha \ + decode.ha \ + encode.ha + gen_ssa encoding::utf8 types + gen_lib encoding::utf8 +} + +strings() { + printf '# strings\n' + gen_srcs strings \ + cstrings.ha \ + dup.ha \ + iter.ha \ + utf8.ha + gen_ssa strings encoding::utf8 types + gen_lib strings +} + +os() { + printf '# os\n' + gen_srcs os \ + '$(PLATFORM)/environ.ha' \ + '$(PLATFORM)/errors.ha' \ + '$(PLATFORM)/exit.ha' \ + '$(PLATFORM)/fdstream.ha' \ + '$(PLATFORM)/open.ha' \ + '$(PLATFORM)/stdfd.ha' \ + environ.ha \ + stdfd.ha + gen_ssa os strings types + gen_lib os +} + +fmt() { + printf '# fmt\n' + gen_srcs fmt \ + fmt.ha + gen_ssa fmt io os strconv strings types + gen_lib fmt +} + +printf '# This file is generated by the gen-stdlib script, do not edit it by hand\n\n' + +# Keep me ordered with respect to dependencies +rt +ascii +bytes +types +strconv +io +encoding_utf8 +strings +os +fmt diff --git a/mk/gen-stdlib.sh b/mk/gen-stdlib.sh @@ -0,0 +1,59 @@ +mod_path() { + printf '%s\n' "$1" | tr -s '::' '/' +} +mod_file() { + printf '%s\n' "$1" | tr -s '::' '.' +} +mod_var() { + printf '%s\n' "$1" | tr -s '::' '_' +} + +gen_srcs() { + mod="$1" + path="$(mod_path "$mod")" + var="$(mod_var "$mod")" + shift + printf 'lib%s_srcs= \\\n' "$var" + while [ $# -ne 0 ] + do + if [ $# -eq 1 ] + then + printf '\t$(STDLIB)/%s/%s \n\n' "$path" "$1" + else + printf '\t$(STDLIB)/%s/%s \\\n' "$path" "$1" + fi + shift + done +} + +gen_ssa() { + mod="$1" + path=$(mod_path "$mod") + file=$(mod_file "$mod") + var=$(mod_var "$mod") + shift + + printf "\$($cache)/$path/$file.ssa: \$(lib${var}_srcs) \$(stdlib_rt)" + for dep in $* + do + printf ' $(stdlib_%s)' "$(mod_var "$dep")" + done + printf '\n' + + cat <<EOF + @printf 'HAREC \t\$@\n' + @mkdir -p \$($cache)/$path + @\$(HAREC) \$(HAREFLAGS) -o \$@ -N$mod \\ + -t\$($cache)/$path/$file.td \$(lib${var}_srcs) + +EOF +} + +gen_lib() { + mod="$1" + path=$(mod_path "$mod") + file=$(mod_file "$mod") + var=$(mod_var "$mod") + printf "stdlib_$var=\$($cache)/$path/$file.o\n\n" +} + diff --git a/mk/stdlib.mk b/mk/stdlib.mk @@ -0,0 +1,171 @@ +# This file is generated by the gen-stdlib script, do not edit it by hand + +# rt +rtscript=$(STDLIB)/rt/hare.sc +librt_srcs= \ + $(STDLIB)/rt/$(PLATFORM)/abort.ha \ + $(STDLIB)/rt/$(PLATFORM)/env.ha \ + $(STDLIB)/rt/$(PLATFORM)/errno.ha \ + $(STDLIB)/rt/$(PLATFORM)/start.ha \ + $(STDLIB)/rt/$(PLATFORM)/syscallno$(ARCH).ha \ + $(STDLIB)/rt/$(PLATFORM)/syscalls.ha \ + $(STDLIB)/rt/$(PLATFORM)/segmalloc.ha \ + $(STDLIB)/rt/malloc.ha \ + $(STDLIB)/rt/memcpy.ha \ + $(STDLIB)/rt/memset.ha \ + $(STDLIB)/rt/strcmp.ha \ + $(STDLIB)/rt/start-test.ha + +$(HARECACHE)/rt/rt.ssa: $(librt_srcs) $(stdlib_rt) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/rt + @$(HAREC) $(HAREFLAGS) -o $@ -Nrt \ + -t$(HARECACHE)/rt/rt.td $(librt_srcs) + +$(HARECACHE)/rt/syscall.o: $(STDLIB)/rt/$(PLATFORM)/syscall$(ARCH).s + @printf 'AS \t$@\n' + @mkdir -p $(HARECACHE)/rt + @as -o $@ $< + +$(HARECACHE)/rt/start.o: $(STDLIB)/rt/$(PLATFORM)/start.s + @printf 'AS \t$@\n' + @mkdir -p $(HARECACHE)/rt + @as -o $@ $< + +$(HARECACHE)/rt/rt.a: $(HARECACHE)/rt/rt.o $(HARECACHE)/rt/syscall.o + @printf 'AR\t$@\n' + @$(AR) -csr $@ $(HARECACHE)/rt/rt.o $(HARECACHE)/rt/syscall.o + +stdlib_rt=$(HARECACHE)/rt/rt.a +stdlib_start=$(HARECACHE)/rt/start.o + +# ascii +libascii_srcs= \ + $(STDLIB)/ascii/ctype.ha + +$(HARECACHE)/ascii/ascii.ssa: $(libascii_srcs) $(stdlib_rt) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/ascii + @$(HAREC) $(HAREFLAGS) -o $@ -Nascii \ + -t$(HARECACHE)/ascii/ascii.td $(libascii_srcs) + +stdlib_ascii=$(HARECACHE)/ascii/ascii.o + +# bytes +libbytes_srcs= \ + $(STDLIB)/bytes/copy.ha \ + $(STDLIB)/bytes/equal.ha \ + $(STDLIB)/bytes/index.ha \ + $(STDLIB)/bytes/reverse.ha + +$(HARECACHE)/bytes/bytes.ssa: $(libbytes_srcs) $(stdlib_rt) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/bytes + @$(HAREC) $(HAREFLAGS) -o $@ -Nbytes \ + -t$(HARECACHE)/bytes/bytes.td $(libbytes_srcs) + +stdlib_bytes=$(HARECACHE)/bytes/bytes.o + +# types +libtypes_srcs= \ + $(STDLIB)/types/limits.ha \ + $(STDLIB)/types/classes.ha \ + $(STDLIB)/types/arch$(ARCH).ha + +$(HARECACHE)/types/types.ssa: $(libtypes_srcs) $(stdlib_rt) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/types + @$(HAREC) $(HAREFLAGS) -o $@ -Ntypes \ + -t$(HARECACHE)/types/types.td $(libtypes_srcs) + +stdlib_types=$(HARECACHE)/types/types.o + +# strconv +libstrconv_srcs= \ + $(STDLIB)/strconv/itos.ha \ + $(STDLIB)/strconv/utos.ha \ + $(STDLIB)/strconv/numeric.ha + +$(HARECACHE)/strconv/strconv.ssa: $(libstrconv_srcs) $(stdlib_rt) $(stdlib_types) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/strconv + @$(HAREC) $(HAREFLAGS) -o $@ -Nstrconv \ + -t$(HARECACHE)/strconv/strconv.td $(libstrconv_srcs) + +stdlib_strconv=$(HARECACHE)/strconv/strconv.o + +# io +libio_srcs= \ + $(STDLIB)/io/types.ha \ + $(STDLIB)/io/copy.ha \ + $(STDLIB)/io/println.ha \ + $(STDLIB)/io/stream.ha + +$(HARECACHE)/io/io.ssa: $(libio_srcs) $(stdlib_rt) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/io + @$(HAREC) $(HAREFLAGS) -o $@ -Nio \ + -t$(HARECACHE)/io/io.td $(libio_srcs) + +stdlib_io=$(HARECACHE)/io/io.o + +# encoding::utf8 +libencoding_utf8_srcs= \ + $(STDLIB)/encoding/utf8/rune.ha \ + $(STDLIB)/encoding/utf8/decode.ha \ + $(STDLIB)/encoding/utf8/encode.ha + +$(HARECACHE)/encoding/utf8/encoding.utf8.ssa: $(libencoding_utf8_srcs) $(stdlib_rt) $(stdlib_types) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/encoding/utf8 + @$(HAREC) $(HAREFLAGS) -o $@ -Nencoding::utf8 \ + -t$(HARECACHE)/encoding/utf8/encoding.utf8.td $(libencoding_utf8_srcs) + +stdlib_encoding_utf8=$(HARECACHE)/encoding/utf8/encoding.utf8.o + +# strings +libstrings_srcs= \ + $(STDLIB)/strings/cstrings.ha \ + $(STDLIB)/strings/dup.ha \ + $(STDLIB)/strings/iter.ha \ + $(STDLIB)/strings/utf8.ha + +$(HARECACHE)/strings/strings.ssa: $(libstrings_srcs) $(stdlib_rt) $(stdlib_encoding_utf8) $(stdlib_types) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/strings + @$(HAREC) $(HAREFLAGS) -o $@ -Nstrings \ + -t$(HARECACHE)/strings/strings.td $(libstrings_srcs) + +stdlib_strings=$(HARECACHE)/strings/strings.o + +# os +libos_srcs= \ + $(STDLIB)/os/$(PLATFORM)/environ.ha \ + $(STDLIB)/os/$(PLATFORM)/errors.ha \ + $(STDLIB)/os/$(PLATFORM)/exit.ha \ + $(STDLIB)/os/$(PLATFORM)/fdstream.ha \ + $(STDLIB)/os/$(PLATFORM)/open.ha \ + $(STDLIB)/os/$(PLATFORM)/stdfd.ha \ + $(STDLIB)/os/environ.ha \ + $(STDLIB)/os/stdfd.ha + +$(HARECACHE)/os/os.ssa: $(libos_srcs) $(stdlib_rt) $(stdlib_strings) $(stdlib_types) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/os + @$(HAREC) $(HAREFLAGS) -o $@ -Nos \ + -t$(HARECACHE)/os/os.td $(libos_srcs) + +stdlib_os=$(HARECACHE)/os/os.o + +# fmt +libfmt_srcs= \ + $(STDLIB)/fmt/fmt.ha + +$(HARECACHE)/fmt/fmt.ssa: $(libfmt_srcs) $(stdlib_rt) $(stdlib_io) $(stdlib_os) $(stdlib_strconv) $(stdlib_strings) $(stdlib_types) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/fmt + @$(HAREC) $(HAREFLAGS) -o $@ -Nfmt \ + -t$(HARECACHE)/fmt/fmt.td $(libfmt_srcs) + +stdlib_fmt=$(HARECACHE)/fmt/fmt.o + diff --git a/stdlib.mk b/stdlib.mk @@ -1,177 +0,0 @@ -# TODO: Write a script to generate this file - -# rt -rtscript=$(STDLIB)/rt/hare.sc -librt_srcs=$(STDLIB)/rt/$(PLATFORM)/abort.ha \ - $(STDLIB)/rt/$(PLATFORM)/env.ha \ - $(STDLIB)/rt/$(PLATFORM)/errno.ha \ - $(STDLIB)/rt/$(PLATFORM)/start.ha \ - $(STDLIB)/rt/$(PLATFORM)/syscallno$(ARCH).ha \ - $(STDLIB)/rt/$(PLATFORM)/syscalls.ha \ - $(STDLIB)/rt/$(PLATFORM)/segmalloc.ha \ - $(STDLIB)/rt/malloc.ha \ - $(STDLIB)/rt/memcpy.ha \ - $(STDLIB)/rt/memset.ha \ - $(STDLIB)/rt/start.ha \ - $(STDLIB)/rt/strcmp.ha - -$(HARECACHE)/rt/rt.ssa: $(librt_srcs) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/rt - @$(HAREC) $(HAREFLAGS) -o $@ -Nrt -t$(HARECACHE)/rt/rt.td $(librt_srcs) - -$(HARECACHE)/rt/start.o: $(STDLIB)/rt/$(PLATFORM)/start.s - @printf 'AS \t$@\n' - @mkdir -p $(HARECACHE)/rt - @as -o $@ $< - -$(HARECACHE)/rt/syscall.o: $(STDLIB)/rt/$(PLATFORM)/syscall$(ARCH).s - @printf 'AS \t$@\n' - @mkdir -p $(HARECACHE)/rt - @as -o $@ $< - -$(HARECACHE)/rt/rt.a: $(HARECACHE)/rt/rt.o $(HARECACHE)/rt/syscall.o - @printf 'AR\t$@\n' - @$(AR) -csr $@ $(HARECACHE)/rt/rt.o $(HARECACHE)/rt/syscall.o - -stdlib_rt=$(HARECACHE)/rt/rt.a -stdlib_start=$(HARECACHE)/rt/start.o - -# ascii -libascii_srcs=\ - $(STDLIB)/ascii/ctype.ha - -$(HARECACHE)/ascii/ascii.ssa: $(libascii_srcs) $(stdlib_rt) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/ascii - @$(HAREC) $(HAREFLAGS) -o $@ -Nascii -t$(HARECACHE)/ascii/ascii.td $(libascii_srcs) - -stdlib_ascii=$(HARECACHE)/ascii/ascii.o - -# bytes -libbytes_srcs=\ - $(STDLIB)/bytes/copy.ha \ - $(STDLIB)/bytes/equal.ha \ - $(STDLIB)/bytes/index.ha \ - $(STDLIB)/bytes/reverse.ha - -$(HARECACHE)/bytes/bytes.ssa: $(libbytes_srcs) $(stdlib_rt) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/bytes - @$(HAREC) $(HAREFLAGS) -o $@ -Nbytes -t$(HARECACHE)/bytes/bytes.td $(libbytes_srcs) - -stdlib_bytes=$(HARECACHE)/bytes/bytes.o - -# types -libtypes_srcs=\ - $(STDLIB)/types/limits.ha \ - $(STDLIB)/types/classes.ha \ - $(STDLIB)/types/arch$(ARCH).ha - -$(HARECACHE)/types/types.ssa: $(libtypes_srcs) $(stdlib_rt) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/types - @$(HAREC) $(HAREFLAGS) -o $@ -Ntypes -t$(HARECACHE)/types/types.td $(libtypes_srcs) - -stdlib_types=$(HARECACHE)/types/types.o - -# strconv -libstrconv_srcs=\ - $(STDLIB)/strconv/itos.ha \ - $(STDLIB)/strconv/utos.ha \ - $(STDLIB)/strconv/numeric.ha - -$(HARECACHE)/strconv/strconv.ssa: $(libstrconv_srcs) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/strconv - @$(HAREC) $(HAREFLAGS) -o $@ -Nstrconv -t$(HARECACHE)/strconv/strconv.td $(libstrconv_srcs) - -$(HARECACHE)/strconv/strconv.ssa: $(stdlib_rt) $(stdlib_bytes) $(stdlib_types) - -stdlib_strconv=$(HARECACHE)/strconv/strconv.o - -# io -# XXX: Sort me only after fixing forward references to alias types -# Sort it to see what the problem is if you don't understand -libio_srcs=\ - $(STDLIB)/io/types.ha \ - $(STDLIB)/io/copy.ha \ - $(STDLIB)/io/println.ha \ - $(STDLIB)/io/stream.ha - -$(HARECACHE)/io/io.ssa: $(libio_srcs) $(stdlib_rt) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/io - @$(HAREC) $(HAREFLAGS) -o $@ -Nio -t$(HARECACHE)/io/io.td $(libio_srcs) - -stdlib_io=$(HARECACHE)/io/io.o - -# encoding/utf8 -# XXX: Also has ordering issues -libencoding_utf8_srcs=\ - $(STDLIB)/encoding/utf8/rune.ha \ - $(STDLIB)/encoding/utf8/decode.ha \ - $(STDLIB)/encoding/utf8/encode.ha - -libencoding_utf8_deps=$(stdlib_rt) $(stdlib_types) - -$(HARECACHE)/encoding/utf8/encoding.utf8.ssa: $(libencoding_utf8_srcs) $(libencoding_utf8_deps) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/encoding/utf8 - @$(HAREC) $(HAREFLAGS) -o $@ -Nencoding::utf8 -t$(HARECACHE)/encoding/utf8/encoding.utf8.td $(libencoding_utf8_srcs) - -stdlib_encoding_utf8=$(HARECACHE)/encoding/utf8/encoding.utf8.o - -# strings -libstrings_srcs=\ - $(STDLIB)/strings/cstrings.ha \ - $(STDLIB)/strings/dup.ha \ - $(STDLIB)/strings/iter.ha \ - $(STDLIB)/strings/utf8.ha - -libstrings_deps=$(stdlib_rt) $(stdlib_types) $(stdlib_encoding_utf8) - -$(HARECACHE)/strings/strings.ssa: $(libstrings_srcs) $(libstrings_deps) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/strings - @$(HAREC) $(HAREFLAGS) -o $@ -Nstrings -t$(HARECACHE)/strings/strings.td $(libstrings_srcs) - -stdlib_strings=$(HARECACHE)/strings/strings.o - -# os -libos_srcs=\ - $(STDLIB)/os/$(PLATFORM)/environ.ha \ - $(STDLIB)/os/$(PLATFORM)/errors.ha \ - $(STDLIB)/os/$(PLATFORM)/exit.ha \ - $(STDLIB)/os/$(PLATFORM)/fdstream.ha \ - $(STDLIB)/os/$(PLATFORM)/open.ha \ - $(STDLIB)/os/$(PLATFORM)/stdfd.ha \ - $(STDLIB)/os/environ.ha \ - $(STDLIB)/os/stdfd.ha - -libos_deps=$(stdlib_rt) $(stdlib_strings) $(stdlib_types) - -$(HARECACHE)/os/os.ssa: $(libos_srcs) $(libos_deps) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/os - @$(HAREC) $(HAREFLAGS) -o $@ -Nos -t$(HARECACHE)/os/os.td $(libos_srcs) - -stdlib_os=$(HARECACHE)/os/os.o - -# fmt -libfmt_srcs=\ - $(STDLIB)/fmt/fmt.ha - -libfmt_deps=$(stdlib_rt) \ - $(stdlib_io) \ - $(stdlib_os) \ - $(stdlib_strconv) \ - $(stdlib_strings) \ - $(stdlib_types) - -$(HARECACHE)/fmt/fmt.ssa: $(libfmt_srcs) $(libfmt_deps) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/fmt - @$(HAREC) $(HAREFLAGS) -o $@ -Nfmt -t$(HARECACHE)/fmt/fmt.td $(libfmt_srcs) - -stdlib_fmt=$(HARECACHE)/fmt/fmt.o