scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 23495f800b903c0fe1261a385ed304994509f437
parent a06a212f80e6aa82aa335516bc5b4cbe17022625
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 22 Aug 2017 17:16:21 +0100

Chage makefile framework

At this moment there were several problems with the makefiles,
because we only could configure the archs and systems that
we want in the final build, and it meant that the final
target list was a cartesian product of both. This set
included non desired (and absurd) combinations like for example
z80-linux. Other problem was the impossibility of having
support for the new qbe support for aarch64.

This new framework uses a dep rule which generates makefiles
(makefiles are different files than Makefiles), and it helps
to avoid all the for loops that were needed for generating
all the requested architectures. All the needed information
is now in the leaf makefiles.

Diffstat:
Makefile | 84++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
cc1/Makefile | 42+++++++++++++++++++++---------------------
cc1/arch/amd64-sysv/arch.c | 223-------------------------------------------------------------------------------
cc1/arch/i386-sysv/arch.c | 224-------------------------------------------------------------------------------
cc1/arch/qbe/arch.c | 223-------------------------------------------------------------------------------
cc1/arch/z80/arch.c | 222-------------------------------------------------------------------------------
cc1/cpp.c | 5-----
cc1/gendep.sh | 27+++++++++++++++++++++++++++
cc1/target/amd64-sysv/arch.c | 222+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc1/target/i386-sysv/arch.c | 223+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc1/target/z80-scc/arch.c | 221+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/Makefile | 36+++++++++++++++++++++---------------
cc2/arch/amd64-sysv/arch.h | 1-
cc2/arch/amd64-sysv/cgen.c | 16----------------
cc2/arch/amd64-sysv/code.c | 211-------------------------------------------------------------------------------
cc2/arch/amd64-sysv/optm.c | 11-----------
cc2/arch/amd64-sysv/types.c | 94-------------------------------------------------------------------------------
cc2/arch/i386-sysv/arch.h | 1-
cc2/arch/i386-sysv/cgen.c | 16----------------
cc2/arch/i386-sysv/code.c | 209-------------------------------------------------------------------------------
cc2/arch/i386-sysv/optm.c | 11-----------
cc2/arch/i386-sysv/types.c | 95-------------------------------------------------------------------------------
cc2/arch/qbe/arch.h | 137-------------------------------------------------------------------------------
cc2/arch/qbe/cgen.c | 731-------------------------------------------------------------------------------
cc2/arch/qbe/code.c | 569-------------------------------------------------------------------------------
cc2/arch/qbe/optm.c | 58----------------------------------------------------------
cc2/arch/qbe/types.c | 94-------------------------------------------------------------------------------
cc2/arch/z80/arch.h | 1-
cc2/arch/z80/cgen.c | 68--------------------------------------------------------------------
cc2/arch/z80/code.c | 229-------------------------------------------------------------------------------
cc2/arch/z80/optm.c | 11-----------
cc2/arch/z80/types.c | 95-------------------------------------------------------------------------------
cc2/gendep.sh | 39+++++++++++++++++++++++++++++++++++++++
cc2/target/amd64-sysv/arch.h | 1+
cc2/target/amd64-sysv/cgen.c | 16++++++++++++++++
cc2/target/amd64-sysv/code.c | 211+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/amd64-sysv/optm.c | 11+++++++++++
cc2/target/amd64-sysv/target.mk | 6++++++
cc2/target/amd64-sysv/types.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/i386-sysv/arch.h | 1+
cc2/target/i386-sysv/cgen.c | 16++++++++++++++++
cc2/target/i386-sysv/code.c | 209+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/i386-sysv/optm.c | 11+++++++++++
cc2/target/i386-sysv/target.mk | 6++++++
cc2/target/i386-sysv/types.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/qbe_amd64-sysv/arch.h | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/qbe_amd64-sysv/cgen.c | 731+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/qbe_amd64-sysv/code.c | 569+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/qbe_amd64-sysv/optm.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/qbe_amd64-sysv/target.mk | 6++++++
cc2/target/qbe_amd64-sysv/types.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/z80-scc/arch.h | 1+
cc2/target/z80-scc/cgen.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/z80-scc/code.c | 229+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc2/target/z80-scc/optm.c | 11+++++++++++
cc2/target/z80-scc/target.mk | 7+++++++
cc2/target/z80-scc/types.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
config.mk | 28++++++++++++++++++----------
crt/Makefile | 11-----------
crt/amd64-sysv-linux/crt.s | 8--------
crt/amd64-sysv-openbsd/crt.s | 23-----------------------
crt/i386-sysv-linux/crt.s | 1-
crt/i386-sysv-openbsd/crt.s | 1-
crt/qbe-linux/crt.s | 8--------
crt/qbe-openbsd/crt.s | 23-----------------------
crt/z80-linux/crt.s | 1-
crt/z80-openbsd/crt.s | 1-
driver/Makefile | 7+++++++
driver/posix/Makefile | 33+++++++++++++++++++++------------
driver/posix/gendep.sh | 18++++++++++++++++++
driver/posix/scc.c | 27++++++++++++++++++++-------
inc/Makefile | 32++++++++++++++++++++++++++++++++
inc/cc.h | 4----
inc/sysincludes.def.h | 2+-
lib/Makefile | 20++++++++++----------
lib/c/Makefile | 7+++++++
lib/c/amd64-sysv-linux/Makefile | 6++++++
lib/c/common.mk | 19+++++++++++++++++++
lib/c/gendep.sh | 17+++++++++++++++++
lib/c/include/assert.h | 13+++++++++++++
lib/c/include/bits/amd64-sysv/arch/limits.h | 17+++++++++++++++++
lib/c/include/bits/amd64-sysv/arch/signal.h | 29+++++++++++++++++++++++++++++
lib/c/include/bits/amd64-sysv/arch/stddef.h | 16++++++++++++++++
lib/c/include/bits/amd64-sysv/arch/stdint.h | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/bits/amd64-sysv/arch/stdio.h | 15+++++++++++++++
lib/c/include/bits/amd64-sysv/arch/stdlib.h | 16++++++++++++++++
lib/c/include/bits/amd64-sysv/arch/string.h | 5+++++
lib/c/include/bits/amd64-sysv/arch/time.h | 8++++++++
lib/c/include/bits/i386-sysv/arch/limits.h | 17+++++++++++++++++
lib/c/include/bits/i386-sysv/arch/signal.h | 29+++++++++++++++++++++++++++++
lib/c/include/bits/i386-sysv/arch/stddef.h | 16++++++++++++++++
lib/c/include/bits/i386-sysv/arch/stdint.h | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/bits/i386-sysv/arch/stdio.h | 15+++++++++++++++
lib/c/include/bits/i386-sysv/arch/stdlib.h | 16++++++++++++++++
lib/c/include/bits/i386-sysv/arch/string.h | 5+++++
lib/c/include/bits/i386-sysv/arch/time.h | 8++++++++
lib/c/include/bits/z80-dos/arch/limits.h | 17+++++++++++++++++
lib/c/include/bits/z80-dos/arch/signal.h | 33+++++++++++++++++++++++++++++++++
lib/c/include/bits/z80-dos/arch/stddef.h | 16++++++++++++++++
lib/c/include/bits/z80-dos/arch/stdint.h | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/bits/z80-dos/arch/stdio.h | 15+++++++++++++++
lib/c/include/bits/z80-dos/arch/stdlib.h | 16++++++++++++++++
lib/c/include/bits/z80-dos/arch/string.h | 5+++++
lib/c/include/bits/z80-dos/arch/time.h | 8++++++++
lib/c/include/ctype.h | 49+++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/errno.h | 12++++++++++++
lib/c/include/limits.h | 9+++++++++
lib/c/include/locale.h | 45+++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/signal.h | 10++++++++++
lib/c/include/stdarg.h | 11+++++++++++
lib/c/include/stdbool.h | 10++++++++++
lib/c/include/stddef.h | 13+++++++++++++
lib/c/include/stdint.h | 7+++++++
lib/c/include/stdio.h | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/stdlib.h | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/include/string.h | 34++++++++++++++++++++++++++++++++++
lib/c/include/time.h | 38++++++++++++++++++++++++++++++++++++++
lib/c/obj.lst | 49+++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/src/Makefile | 24++++++++++++++++++++++++
lib/c/src/assert.c | 11+++++++++++
lib/c/src/atexit.c | 19+++++++++++++++++++
lib/c/src/atoi.c | 28++++++++++++++++++++++++++++
lib/c/src/calloc.c | 19+++++++++++++++++++
lib/c/src/ctype.c | 24++++++++++++++++++++++++
lib/c/src/exit.c | 15+++++++++++++++
lib/c/src/fprintf.c | 17+++++++++++++++++
lib/c/src/isalnum.c | 11+++++++++++
lib/c/src/isalpha.c | 11+++++++++++
lib/c/src/isascii.c | 11+++++++++++
lib/c/src/isblank.c | 7+++++++
lib/c/src/iscntrl.c | 11+++++++++++
lib/c/src/isdigit.c | 11+++++++++++
lib/c/src/isgraph.c | 11+++++++++++
lib/c/src/islower.c | 11+++++++++++
lib/c/src/isprint.c | 11+++++++++++
lib/c/src/ispunct.c | 11+++++++++++
lib/c/src/isspace.c | 11+++++++++++
lib/c/src/isupper.c | 11+++++++++++
lib/c/src/isxdigit.c | 11+++++++++++
lib/c/src/localeconv.c | 14++++++++++++++
lib/c/src/malloc.c | 154+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/src/malloc.h | 18++++++++++++++++++
lib/c/src/memchr.c | 14++++++++++++++
lib/c/src/memcmp.c | 14++++++++++++++
lib/c/src/memcpy.c | 15+++++++++++++++
lib/c/src/memmove.c | 20++++++++++++++++++++
lib/c/src/memset.c | 14++++++++++++++
lib/c/src/printf.c | 17+++++++++++++++++
lib/c/src/rand.c | 19+++++++++++++++++++
lib/c/src/realloc.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/c/src/setlocale.c | 18++++++++++++++++++
lib/c/src/strcat.c | 16++++++++++++++++
lib/c/src/strchr.c | 12++++++++++++
lib/c/src/strcmp.c | 12++++++++++++
lib/c/src/strcoll.c | 12++++++++++++
lib/c/src/strcpy.c | 14++++++++++++++
lib/c/src/strcspn.c | 20++++++++++++++++++++
lib/c/src/strlen.c | 14++++++++++++++
lib/c/src/strncat.c | 17+++++++++++++++++
lib/c/src/strncmp.c | 12++++++++++++
lib/c/src/strncpy.c | 16++++++++++++++++
lib/c/src/strpbrk.c | 19+++++++++++++++++++
lib/c/src/strrchr.c | 16++++++++++++++++
lib/c/src/strspn.c | 20++++++++++++++++++++
lib/c/src/strstr.c | 28++++++++++++++++++++++++++++
lib/c/src/strtok.c | 27+++++++++++++++++++++++++++
lib/c/src/strxfrm.c | 14++++++++++++++
lib/c/src/syscall.h | 11+++++++++++
lib/c/src/tolower.c | 11+++++++++++
lib/c/src/toupper.c | 11+++++++++++
lib/c/src/vfprintf.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/cc/Makefile | 20++++++++++++++++++++
lib/cc/debug.c | 21+++++++++++++++++++++
lib/cc/die.c | 21+++++++++++++++++++++
lib/cc/libdep.mk | 10++++++++++
lib/cc/newitem.c | 12++++++++++++
lib/cc/xcalloc.c | 14++++++++++++++
lib/cc/xmalloc.c | 14++++++++++++++
lib/cc/xrealloc.c | 14++++++++++++++
lib/cc/xstrdup.c | 13+++++++++++++
lib/crt/Makefile | 11+++++++++++
lib/crt/amd64-sysv-linux/crt.s | 8++++++++
lib/crt/amd64-sysv-openbsd/crt.s | 23+++++++++++++++++++++++
lib/crt/i386-sysv-linux/crt.s | 1+
lib/crt/i386-sysv-openbsd/crt.s | 1+
lib/debug.c | 21---------------------
lib/die.c | 21---------------------
lib/newitem.c | 12------------
lib/sys/Makefile | 13+++++++++++++
lib/sys/abi/amd64-sysv.sh | 15+++++++++++++++
lib/sys/amd64-sysv-linux/Makefile | 5+++++
lib/sys/amd64-sysv-linux/syscall.lst | 11+++++++++++
lib/sys/common.mk | 13+++++++++++++
lib/sys/i386-sysv-linux/Makefile | 4++++
lib/sys/i386-sysv-linux/syscall.lst | 0
lib/sys/libsys.mk | 24++++++++++++++++++++++++
lib/sys/qbe-linux/Makefile | 5+++++
lib/sys/qbe-linux/syscall.lst | 11+++++++++++
lib/sys/z80-linux/Makefile | 4++++
lib/sys/z80-linux/syscall.lst | 0
lib/xcalloc.c | 14--------------
lib/xmalloc.c | 14--------------
lib/xrealloc.c | 14--------------
lib/xstrdup.c | 13-------------
libc/include/assert.h | 13-------------
libc/include/bits/amd64-sysv/arch/limits.h | 17-----------------
libc/include/bits/amd64-sysv/arch/signal.h | 29-----------------------------
libc/include/bits/amd64-sysv/arch/stddef.h | 16----------------
libc/include/bits/amd64-sysv/arch/stdint.h | 117-------------------------------------------------------------------------------
libc/include/bits/amd64-sysv/arch/stdio.h | 15---------------
libc/include/bits/amd64-sysv/arch/stdlib.h | 16----------------
libc/include/bits/amd64-sysv/arch/string.h | 5-----
libc/include/bits/amd64-sysv/arch/time.h | 8--------
libc/include/bits/i386-sysv/arch/limits.h | 17-----------------
libc/include/bits/i386-sysv/arch/signal.h | 29-----------------------------
libc/include/bits/i386-sysv/arch/stddef.h | 16----------------
libc/include/bits/i386-sysv/arch/stdint.h | 117-------------------------------------------------------------------------------
libc/include/bits/i386-sysv/arch/stdio.h | 15---------------
libc/include/bits/i386-sysv/arch/stdlib.h | 16----------------
libc/include/bits/i386-sysv/arch/string.h | 5-----
libc/include/bits/i386-sysv/arch/time.h | 8--------
libc/include/bits/qbe/arch/limits.h | 17-----------------
libc/include/bits/qbe/arch/signal.h | 29-----------------------------
libc/include/bits/qbe/arch/stddef.h | 16----------------
libc/include/bits/qbe/arch/stdint.h | 117-------------------------------------------------------------------------------
libc/include/bits/qbe/arch/stdio.h | 15---------------
libc/include/bits/qbe/arch/stdlib.h | 16----------------
libc/include/bits/qbe/arch/string.h | 5-----
libc/include/bits/qbe/arch/time.h | 8--------
libc/include/bits/z80/arch/limits.h | 17-----------------
libc/include/bits/z80/arch/signal.h | 33---------------------------------
libc/include/bits/z80/arch/stddef.h | 16----------------
libc/include/bits/z80/arch/stdint.h | 117-------------------------------------------------------------------------------
libc/include/bits/z80/arch/stdio.h | 15---------------
libc/include/bits/z80/arch/stdlib.h | 16----------------
libc/include/bits/z80/arch/string.h | 5-----
libc/include/bits/z80/arch/time.h | 8--------
libc/include/ctype.h | 49-------------------------------------------------
libc/include/errno.h | 12------------
libc/include/limits.h | 9---------
libc/include/locale.h | 45---------------------------------------------
libc/include/signal.h | 10----------
libc/include/stdarg.h | 11-----------
libc/include/stdbool.h | 10----------
libc/include/stddef.h | 13-------------
libc/include/stdint.h | 7-------
libc/include/stdio.h | 102-------------------------------------------------------------------------------
libc/include/stdlib.h | 70----------------------------------------------------------------------
libc/include/string.h | 34----------------------------------
libc/include/time.h | 38--------------------------------------
libc/src/Makefile | 44--------------------------------------------
libc/src/assert.c | 11-----------
libc/src/atexit.c | 19-------------------
libc/src/atoi.c | 28----------------------------
libc/src/calloc.c | 19-------------------
libc/src/ctype.c | 24------------------------
libc/src/exit.c | 15---------------
libc/src/fprintf.c | 17-----------------
libc/src/isalnum.c | 11-----------
libc/src/isalpha.c | 11-----------
libc/src/isascii.c | 11-----------
libc/src/isblank.c | 7-------
libc/src/iscntrl.c | 11-----------
libc/src/isdigit.c | 11-----------
libc/src/isgraph.c | 11-----------
libc/src/islower.c | 11-----------
libc/src/isprint.c | 11-----------
libc/src/ispunct.c | 11-----------
libc/src/isspace.c | 11-----------
libc/src/isupper.c | 11-----------
libc/src/isxdigit.c | 11-----------
libc/src/localeconv.c | 14--------------
libc/src/malloc.c | 154-------------------------------------------------------------------------------
libc/src/malloc.h | 18------------------
libc/src/memchr.c | 14--------------
libc/src/memcmp.c | 14--------------
libc/src/memcpy.c | 15---------------
libc/src/memmove.c | 20--------------------
libc/src/memset.c | 14--------------
libc/src/printf.c | 17-----------------
libc/src/rand.c | 19-------------------
libc/src/realloc.c | 69---------------------------------------------------------------------
libc/src/setlocale.c | 18------------------
libc/src/strcat.c | 16----------------
libc/src/strchr.c | 12------------
libc/src/strcmp.c | 12------------
libc/src/strcoll.c | 12------------
libc/src/strcpy.c | 14--------------
libc/src/strcspn.c | 20--------------------
libc/src/strlen.c | 14--------------
libc/src/strncat.c | 17-----------------
libc/src/strncmp.c | 12------------
libc/src/strncpy.c | 16----------------
libc/src/strpbrk.c | 19-------------------
libc/src/strrchr.c | 16----------------
libc/src/strspn.c | 20--------------------
libc/src/strstr.c | 28----------------------------
libc/src/strtok.c | 27---------------------------
libc/src/strxfrm.c | 14--------------
libc/src/sys/amd64-sysv-linux/Makefile | 37-------------------------------------
libc/src/sys/amd64-sysv-linux/syscall.awk | 12------------
libc/src/sys/amd64-sysv-linux/syscall.lst | 11-----------
libc/src/sys/i386-sysv-linux/Makefile | 37-------------------------------------
libc/src/sys/i386-sysv-linux/syscall.lst | 0
libc/src/sys/qbe-linux/Makefile | 37-------------------------------------
libc/src/sys/qbe-linux/syscall.awk | 12------------
libc/src/sys/qbe-linux/syscall.lst | 11-----------
libc/src/sys/z80-linux/Makefile | 37-------------------------------------
libc/src/sys/z80-linux/syscall.lst | 0
libc/src/syscall.h | 11-----------
libc/src/tolower.c | 11-----------
libc/src/toupper.c | 11-----------
libc/src/vfprintf.c | 76----------------------------------------------------------------------------
tests/Makefile | 2+-
tests/execute/Makefile | 3+++
315 files changed, 6177 insertions(+), 6452 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,39 +4,65 @@ include config.mk -DIRS = lib cc1 cc2 crt driver/$(DRIVER) +DIRS = inc cc1 cc2 driver lib -all: scc-driver - for i in $(ARCHS); \ +all: + @set -e ;\ + trap 'rm -f bin/cc1-* bin/cc2-* bin/scc' 0 2 3; \ + if ! test -f bin/dep ;\ + then \ + echo Run make dep first >&2 ;\ + exit 1 ;\ + fi ;\ + pwd=$$PWD; \ + for i in $(DIRS); \ do \ - $(MAKE) $$i || exit; \ + cd $$i; \ + $(MAKE) $@; \ + cd $$pwd; \ done + cp cc1/cc1-* bin/ + cp cc2/cc2-* bin/ + cp driver/$(DRIVER)/scc bin/ -scc-driver: - cd driver/$(DRIVER)/ && $(MAKE) scc - ln -f driver/$(DRIVER)/scc bin/scc -$(ARCHS): +clean: + @set -e ;\ pwd=$$PWD; \ - for i in cc1 cc2; \ + for i in $(DIRS); \ do \ cd $$i; \ - ARCH=$@ $(MAKE) -e $$i-$@ || exit; \ + $(MAKE) $@; \ cd $$pwd; \ done - cd crt; \ - for i in $(SYSS); \ - do \ - ARCH=$@ SYS=$$i $(MAKE) -e || exit; \ - done; \ - cd $$pwd; - ln -f cc1/cc1-$@ bin/ - ln -f cc2/cc2-$@ bin/ + rm -f bin/cc1-* + rm -f bin/cc2-* + rm -f bin/scc -libc/src/libc.a: +distclean: + @set -e ;\ + pwd=$$PWD; \ + for i in $(DIRS); \ + do \ + cd $$i; \ + $(MAKE) $@; \ + cd $$pwd; \ + done + rm -f bin/cc1-* + rm -f bin/cc2-* + rm -f bin/scc + rm -f bin/dep -libc: libc/src/libc.a - cd libc/src && $(MAKE) -e CC=scc +dep: + @set -e;\ + pwd=$$PWD; \ + for i in $(DIRS); \ + do \ + cd $$i; \ + $(MAKE) $@; \ + cd $$pwd; \ + done + touch bin/dep tests: all cd tests && $(MAKE) -e all @@ -61,19 +87,3 @@ uninstall: rm -rf $(DESTDIR)/$(PREFIX)/lib/scc/ rm -f $(DESTDIR)/$(PREFIX)/bin/scc rm -f $(DESTDIR)/$(PREFIX)/bin/scpp - -clean-helper: - for i in $(DIRS); \ - do \ - (cd $$i && $(MAKE) -e clean || exit); \ - done - -clean: - for i in $(ARCHS); \ - do \ - ARCH=$$i $(MAKE) -e clean-helper || exit; \ - done - rm -f bin/cc* bin/scc - -distclean: clean - rm -f inc/sysincludes.h inc/syslibs.h inc/ldflags.h diff --git a/cc1/Makefile b/cc1/Makefile @@ -1,35 +1,35 @@ # See LICENSE file for copyright and license details. .POSIX: -CSTDINC = -I../inc/$(STD) +# Makefile is only used as a template for makefile. +# If you modify Makefile remember to run make dep include ../config.mk -OBJS = types.o decl.o lex.o error.o symbol.o main.o expr.o \ - code.o stmt.o cpp.o fold.o init.o builtin.o \ - arch/$(ARCH)/arch.o +MOREFLAGS = -I../inc/$(STD) +LIBDIR = ../lib/cc -all: cc1-$(ARCH) +OBJ = types.o decl.o lex.o error.o symbol.o main.o expr.o \ + code.o stmt.o cpp.o fold.o init.o builtin.o -cpp.o: stallman.msg -arch/$(ARCH)/arch.o: ../inc/sysincludes.h -$(OBJS): cc1.h ../inc/cc.h ../inc/$(STD)/cstd.h - -../inc/sysincludes.h: - cp -f ../inc/sysincludes.def.h ../inc/sysincludes.h +HDR = cc1.h ../inc/cc.h ../inc/$(STD)/cstd.h ../inc/sysincludes.h -../lib/libcc.a: - cd ../lib && $(MAKE) -e +all: -cc1-$(ARCH): $(OBJS) ../lib/libcc.a - $(CC) $(SCC_LDFLAGS) $(OBJS) ../lib/libcc.a -o $@ +cpp.o: stallman.msg -cpp: cc1-$(ARCH) - ln -f $< cpp +$(LIBDIR)/libcc.a: + cd $(LIBDIR) && $(MAKE) -test: - cd tests && ./chktest.sh *.c +dep: + ./gendep.sh $(TARGETS) clean: - rm -f $(OBJS) - rm -f cc1-* cpp + rm -f *.o + rm -f target/*/*.o + rm -f cc1-* + +distclean: clean + rm -f makefile + +include ../lib/cc/libdep.mk diff --git a/cc1/arch/amd64-sysv/arch.c b/cc1/arch/amd64-sysv/arch.c @@ -1,223 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc1/arch/amd64-sysv/arch.c"; -#include <stdio.h> - -#include "../../../inc/sysincludes.h" -#include "../../../inc/cc.h" -#include "../../cc1.h" - -#define RANK_BOOL 0 -#define RANK_SCHAR 1 -#define RANK_UCHAR 1 -#define RANK_CHAR 1 -#define RANK_SHORT 2 -#define RANK_USHORT 2 -#define RANK_INT 3 -#define RANK_UINT 3 -#define RANK_LONG 4 -#define RANK_ULONG 4 -#define RANK_LLONG 5 -#define RANK_ULLONG 5 -#define RANK_FLOAT 6 -#define RANK_DOUBLE 7 -#define RANK_LDOUBLE 8 - -/* - * Initializaion of type pointers were done with - * a C99 initilizator '... = &(Type) {...', but - * c compiler in Plan9 gives error with this - * syntax, so I have switched it to this ugly form - * I hope I will change it again in the future - */ - -static Type types[] = { - { /* 0 = voidtype */ - .op = VOID, - .letter = L_VOID, - }, - { /* 1 = pvoidtype */ - .op = PTR, - .letter = L_POINTER, - .prop = TDEFINED, - .type = &types[5], /* chartype */ - .size = 8, - .align = 8, - }, - { /* 2 = booltype */ - .op = INT, - .letter = L_BOOL, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_BOOL, - }, - { /* 3 = schartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_SCHAR, - }, - { /* 4 = uchartype */ - .op = INT, - .letter = L_UINT8, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_UCHAR, - }, - { /* 5 = chartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_CHAR, - }, - { /* 6 = ushortype */ - .op = INT, - .letter = L_UINT16, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 2, - .align = 2, - .n.rank = RANK_USHORT, - }, - { /* 7 = shortype */ - .op = INT, - .letter = L_INT16, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 2, - .align = 2, - .n.rank = RANK_SHORT, - }, - { /* 8 = uinttype */ - .op = INT, - .letter = L_UINT32, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_UINT, - }, - { /* 9 = inttype */ - .op = INT, - .letter = L_INT32, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 4, - .align = 4, - .n.rank = RANK_INT, - }, - { /* 10 = longtype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 8, - .n.rank = RANK_LONG, - }, - { /* 11 = ulongtype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_ULONG, - }, - { /* 12 = ullongtype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_ULLONG, - }, - { /* 13 = llongtype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 8, - .n.rank = RANK_LLONG, - }, - { /* 14 = floattype */ - .op = FLOAT, - .letter = L_FLOAT, - .prop = TDEFINED | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_FLOAT, - }, - { /* 15 = doubletype */ - .op = FLOAT, - .letter = L_DOUBLE, - .prop = TDEFINED | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_DOUBLE, - }, - { /* 16 = ldoubletype */ - .op = FLOAT, - .letter = L_LDOUBLE, - .prop = TDEFINED | TARITH, - .size = 16, - .align = 16, - .n.rank = RANK_LDOUBLE, - }, - { /* 17 = sizettype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_UINT, - }, - { /* 18 = ellipsis */ - .op = ELLIPSIS, - .letter = L_ELLIPSIS, - .prop = TDEFINED, - }, - { /* 19 = pdifftype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 8, - .n.rank = RANK_LONG, - }, - { /* 20 = va_type */ - .op = STRUCT, - .letter = L_VA_ARG, - .prop = TDEFINED, - .size = 24, - .align = 8, - }, -}; - -Type *voidtype = &types[0], *pvoidtype = &types[1], - *booltype = &types[2], *schartype = &types[3], - *uchartype = &types[4], *chartype = &types[5], - *ushortype = &types[6], *shortype = &types[7], - *uinttype = &types[8], *inttype = &types[9], - *longtype = &types[10], *ulongtype = &types[11], - *ullongtype = &types[12], *llongtype = &types[13], - *floattype = &types[14], *doubletype = &types[15], - *ldoubletype = &types[16], - *sizettype = &types[17], *pdifftype = &types[19], - *ellipsistype = &types[18], *va_type = &types[20], - *va_list_type; - -static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, - dummy1 = {.u.i = 1, .type = &types[9]}; -Symbol *zero = &dummy0, *one = &dummy1; - -void -iarch(void) -{ - va_list_type = mktype(va_type, ARY, 1, NULL); -} - -int -valid_va_list(Type *tp) -{ - return tp->op == PTR && eqtype(tp->type, va_type, 1); -} diff --git a/cc1/arch/i386-sysv/arch.c b/cc1/arch/i386-sysv/arch.c @@ -1,224 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc1/arch/i386-sysv/arch.c"; -#include <stdio.h> - -#include "../../../inc/sysincludes.h" -#include "../../../inc/cc.h" -#include "../../cc1.h" - -#define RANK_BOOL 0 -#define RANK_SCHAR 1 -#define RANK_UCHAR 1 -#define RANK_CHAR 1 -#define RANK_SHORT 2 -#define RANK_USHORT 2 -#define RANK_INT 3 -#define RANK_UINT 3 -#define RANK_LONG 4 -#define RANK_ULONG 4 -#define RANK_LLONG 5 -#define RANK_ULLONG 5 -#define RANK_FLOAT 6 -#define RANK_DOUBLE 7 -#define RANK_LDOUBLE 8 - -/* - * Initializaion of type pointers were done with - * a C99 initilizator '... = &(Type) {...', but - * c compiler in Plan9 gives error with this - * syntax, so I have switched it to this ugly form - * I hope I will change it again in the future - */ - -static Type types[] = { - { /* 0 = voidtype */ - .op = VOID, - .letter = L_VOID, - }, - { /* 1 = pvoidtype */ - .op = PTR, - .letter = L_POINTER, - .prop = TDEFINED, - .type = &types[5], /* chartype */ - .size = 4, - .align = 4, - }, - { /* 2 = booltype */ - .op = INT, - .letter = L_BOOL, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_BOOL, - }, - { /* 3 = schartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_SCHAR, - }, - { /* 4 = uchartype */ - .op = INT, - .letter = L_UINT8, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_UCHAR, - }, - { /* 5 = chartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_CHAR, - }, - { /* 6 = ushortype */ - .op = INT, - .letter = L_UINT16, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 2, - .align = 2, - .n.rank = RANK_USHORT, - }, - { /* 7 = shortype */ - .op = INT, - .letter = L_INT16, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 2, - .align = 2, - .n.rank = RANK_SHORT, - }, - { /* 8 = uinttype */ - .op = INT, - .letter = L_UINT32, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_UINT, - }, - { /* 9 = inttype */ - .op = INT, - .letter = L_INT32, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 4, - .align = 4, - .n.rank = RANK_INT, - }, - { /* 10 = longtype */ - .op = INT, - .letter = L_INT32, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 4, - .align = 4, - .n.rank = RANK_LONG, - }, - { /* 11 = ulongtype */ - .op = INT, - .letter = L_UINT32, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_ULONG, - }, - { /* 12 = ullongtype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 4, - .n.rank = RANK_ULLONG, - }, - { /* 13 = llongtype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 4, - .n.rank = RANK_LLONG, - }, - { /* 14 = floattype */ - .op = FLOAT, - .letter = L_FLOAT, - .prop = TDEFINED | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_FLOAT, - }, - { /* 15 = doubletype */ - .op = FLOAT, - .letter = L_DOUBLE, - .prop = TDEFINED | TARITH, - .size = 8, - .align = 4, - .n.rank = RANK_DOUBLE, - }, - { /* 16 = ldoubletype */ - .op = FLOAT, - .letter = L_LDOUBLE, - .prop = TDEFINED | TARITH, - .size = 12, - .align = 4, - .n.rank = RANK_LDOUBLE, - }, - { /* 17 = sizettype */ - .op = INT, - .letter = L_UINT32, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_UINT, - }, - { /* 18 = ellipsis */ - .op = ELLIPSIS, - .letter = L_ELLIPSIS, - .prop = TDEFINED, - }, - { /* 19 = pdifftype */ - .op = INT, - .letter = L_INT32, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 4, - .align = 4, - .n.rank = RANK_INT, - }, - { /* 20 = va_list_type */ - .op = PTR, - .letter = L_POINTER, - .prop = TDEFINED, - .size = 4, - .align = 4, - }, -}; - - -Type *voidtype = &types[0], *pvoidtype = &types[1], - *booltype = &types[2], *schartype = &types[3], - *uchartype = &types[4], *chartype = &types[5], - *ushortype = &types[6], *shortype = &types[7], - *uinttype = &types[8], *inttype = &types[9], - *longtype = &types[10], *ulongtype = &types[11], - *ullongtype = &types[12], *llongtype = &types[13], - *floattype = &types[14], *doubletype = &types[15], - *ldoubletype = &types[16], - *sizettype = &types[17], *pdifftype = &types[19], - *ellipsistype = &types[18], *va_list_type = &types[20]; - - - -static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, - dummy1 = {.u.i = 1, .type = &types[9]}; -Symbol *zero = &dummy0, *one = &dummy1; - -void -iarch(void) -{ -} - -int -valid_va_list(Type *tp) -{ - return eqtype(tp, va_list_type, 1); -} diff --git a/cc1/arch/qbe/arch.c b/cc1/arch/qbe/arch.c @@ -1,223 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc1/arch/qbe/arch.c"; -#include <stdio.h> - -#include "../../../inc/sysincludes.h" -#include "../../../inc/cc.h" -#include "../../cc1.h" - -#define RANK_BOOL 0 -#define RANK_SCHAR 1 -#define RANK_UCHAR 1 -#define RANK_CHAR 1 -#define RANK_SHORT 2 -#define RANK_USHORT 2 -#define RANK_INT 3 -#define RANK_UINT 3 -#define RANK_LONG 4 -#define RANK_ULONG 4 -#define RANK_LLONG 5 -#define RANK_ULLONG 5 -#define RANK_FLOAT 6 -#define RANK_DOUBLE 7 -#define RANK_LDOUBLE 8 - -/* - * Initializaion of type pointers were done with - * a C99 initilizator '... = &(Type) {...', but - * c compiler in Plan9 gives error with this - * syntax, so I have switched it to this ugly form - * I hope I will change it again in the future - */ - -static Type types[] = { - { /* 0 = voidtype */ - .op = VOID, - .letter = L_VOID, - }, - { /* 1 = pvoidtype */ - .op = PTR, - .letter = L_POINTER, - .prop = TDEFINED, - .type = &types[5], /* char type */ - .size = 8, - .align = 8, - }, - { /* 2 = booltype */ - .op = INT, - .letter = L_BOOL, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_BOOL, - }, - { /* 3 = schartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | SIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_SCHAR, - }, - { /* 4 = uchartype */ - .op = INT, - .letter = L_UINT8, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_UCHAR, - }, - { /* 5 = chartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_CHAR, - }, - { /* 6 = ushortype */ - .op = INT, - .letter = L_UINT16, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 2, - .align = 2, - .n.rank = RANK_USHORT, - }, - { /* 7 = shortype */ - .op = INT, - .letter = L_INT16, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 2, - .align = 2, - .n.rank = RANK_SHORT, - }, - { /* 8 = uinttype */ - .op = INT, - .letter = L_UINT32, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_UINT, - }, - { /* 9 = inttype */ - .op = INT, - .letter = L_INT32, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 4, - .align = 4, - .n.rank = RANK_INT, - }, - { /* 10 = longtype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 8, - .n.rank = RANK_LONG, - }, - { /* 11 = ulongtype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_ULONG, - }, - { /* 12 = ullongtype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_ULLONG, - }, - { /* 13 = llongtype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 8, - .n.rank = RANK_LLONG, - }, - { /* 14 = floattype */ - .op = FLOAT, - .letter = L_FLOAT, - .prop = TDEFINED | TARITH, - .size = 4, - .align = 4, - .n.rank = RANK_FLOAT, - }, - { /* 15 = doubletype */ - .op = FLOAT, - .letter = L_DOUBLE, - .prop = TDEFINED | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_DOUBLE, - }, - { /* 16 = ldoubletype */ - .op = FLOAT, - .letter = L_DOUBLE, - .prop = TDEFINED | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_DOUBLE, - }, - { /* 17 = sizettype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 8, - .n.rank = RANK_UINT, - }, - { /* 18 = ellipsis */ - .op = ELLIPSIS, - .letter = L_ELLIPSIS, - .prop = TDEFINED, - }, - { /* 19 = pdifftype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 8, - .n.rank = RANK_LONG, - }, - { /* 20 = va_type */ - .op = STRUCT, - .letter = L_VA_ARG, - .prop = TDEFINED, - .size = 24, - .align = 8, - }, -}; - -Type *voidtype = &types[0], *pvoidtype = &types[1], - *booltype = &types[2], *schartype = &types[3], - *uchartype = &types[4], *chartype = &types[5], - *ushortype = &types[6], *shortype = &types[7], - *uinttype = &types[8], *inttype = &types[9], - *longtype = &types[10], *ulongtype = &types[11], - *ullongtype = &types[12], *llongtype = &types[13], - *floattype = &types[14], *doubletype = &types[15], - *ldoubletype = &types[16], - *sizettype = &types[17], *pdifftype = &types[19], - *ellipsistype = &types[18], *va_type = &types[20], - *va_list_type; - -static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, - dummy1 = {.u.i = 1, .type = &types[9]}; -Symbol *zero = &dummy0, *one = &dummy1; - -void -iarch(void) -{ - va_list_type = mktype(va_type, ARY, 1, NULL); -} - -int -valid_va_list(Type *tp) -{ - return tp->op == PTR && eqtype(tp->type, va_type, 1); -} diff --git a/cc1/arch/z80/arch.c b/cc1/arch/z80/arch.c @@ -1,222 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc1/arch/z80/arch.c"; -#include <stdio.h> - -#include "../../../inc/sysincludes.h" -#include "../../../inc/cc.h" -#include "../../cc1.h" - -#define RANK_BOOL 0 -#define RANK_SCHAR 1 -#define RANK_UCHAR 1 -#define RANK_CHAR 1 -#define RANK_SHORT 2 -#define RANK_USHORT 2 -#define RANK_INT 3 -#define RANK_UINT 3 -#define RANK_LONG 4 -#define RANK_ULONG 4 -#define RANK_LLONG 5 -#define RANK_ULLONG 5 -#define RANK_FLOAT 6 -#define RANK_DOUBLE 7 -#define RANK_LDOUBLE 8 - -/* - * Initializaion of type pointers were done with - * a C99 initilizator '... = &(Type) {...', but - * c compiler in Plan9 gives error with this - * syntax, so I have switched it to this ugly form - * I hope I will change it again in the future - */ - -static Type types[] = { - { /* 0 = voidtype */ - .op = VOID, - .letter = L_VOID, - }, - { /* 1 = pvoidtype */ - .op = PTR, - .letter = L_POINTER, - .prop = TDEFINED, - .type = &types[5], /* char type */ - .size = 2, - .align = 2, - }, - { /* 2 = booltype */ - .op = INT, - .letter = L_BOOL, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_BOOL, - }, - { /* 3 = schartype */ - .op = INT, - .letter = L_INT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_SCHAR, - }, - { /* 4 = uchartype */ - .op = INT, - .letter = L_UINT8, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 1, - .align = 1, - .n.rank = RANK_UCHAR, - }, - { /* 5 = chartype */ - .op = INT, - .letter = L_UINT8, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 1, - .align = 1, - .n.rank = RANK_CHAR, - }, - { /* 6 = ushortype */ - .op = INT, - .letter = L_UINT16, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 2, - .align = 1, - .n.rank = RANK_USHORT, - }, - { /* 7 = shortype */ - .op = INT, - .letter = L_INT16, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 2, - .align = 1, - .n.rank = RANK_SHORT, - }, - { /* 8 = uinttype */ - .op = INT, - .letter = L_UINT16, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 2, - .align = 1, - .n.rank = RANK_UINT, - }, - { /* 9 = inttype */ - .op = INT, - .letter = L_INT16, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 2, - .align = 1, - .n.rank = RANK_INT, - }, - { /* 10 = longtype */ - .op = INT, - .letter = L_INT32, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 4, - .align = 1, - .n.rank = RANK_LONG, - }, - { /* 11 = ulongtype */ - .op = INT, - .letter = L_UINT32, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 4, - .align = 1, - .n.rank = RANK_ULONG, - }, - { /* 12 = ullongtype */ - .op = INT, - .letter = L_UINT64, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 8, - .align = 1, - .n.rank = RANK_ULLONG, - }, - { /* 13 = llongtype */ - .op = INT, - .letter = L_INT64, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 8, - .align = 1, - .n.rank = RANK_LLONG, - }, - { /* 14 = floattype */ - .op = FLOAT, - .letter = L_FLOAT, - .prop = TDEFINED | TARITH, - .size = 4, - .align = 1, - .n.rank = RANK_FLOAT, - }, - { /* 15 = doubletype */ - .op = FLOAT, - .letter = L_DOUBLE, - .prop = TDEFINED | TARITH, - .size = 8, - .align = 1, - .n.rank = RANK_DOUBLE, - }, - { /* 16 = ldoubletype */ - .op = FLOAT, - .letter = L_LDOUBLE, - .prop = TDEFINED | TARITH, - .size = 16, - .align = 1, - .n.rank = RANK_LDOUBLE, - }, - { /* 17 = sizettype */ - .op = INT, - .letter = L_UINT16, - .prop = TDEFINED | TINTEGER | TARITH, - .size = 2, - .align = 1, - .n.rank = RANK_UINT, - }, - { /* 18 = ellipsis */ - .op = ELLIPSIS, - .letter = L_ELLIPSIS, - .prop = TDEFINED, - }, - { /* 7 = pdifftype */ - .op = INT, - .letter = L_INT16, - .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, - .size = 2, - .align = 1, - .n.rank = RANK_SHORT, - }, - { /* 20 = va_list_type */ - .op = PTR, - .letter = L_POINTER, - .prop = TDEFINED, - .size = 2, - .align = 1, - } -}; - -Type *voidtype = &types[0], *pvoidtype = &types[1], - *booltype = &types[2], *schartype = &types[3], - *uchartype = &types[4], *chartype = &types[5], - *ushortype = &types[6], *shortype = &types[7], - *uinttype = &types[8], *inttype = &types[9], - *longtype = &types[10], *ulongtype = &types[11], - *ullongtype = &types[12], *llongtype = &types[13], - *floattype = &types[14], *doubletype = &types[15], - *ldoubletype = &types[16], - *sizettype = &types[17], *pdifftype = &types[19], - *ellipsistype = &types[18], *va_list_type = &types[20]; - - -static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, - dummy1 = {.u.i = 1, .type = &types[9]}; -Symbol *zero = &dummy0, *one = &dummy1; - -void -iarch(void) -{ -} - -int -valid_va_list(Type *tp) -{ - return eqtype(tp, va_list_type, 1); -} diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -11,7 +11,6 @@ static char sccsid[] = "@(#) ./cc1/cpp.c"; #include "../inc/cc.h" #include "cc1.h" -extern char *sysincludes[]; static char *argp, *macroname; static unsigned arglen; static unsigned ncmdlines; @@ -512,10 +511,6 @@ include(void) if (includefile(*bp, file, filelen)) goto its_done; } - for (bp = sysincludes; *bp; ++bp) { - if (includefile(*bp, file, filelen)) - goto its_done; - } cpperror("included file '%s' not found", file); its_done: diff --git a/cc1/gendep.sh b/cc1/gendep.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +rm -f makefile +trap "rm -f $$.mk" 0 2 3 + +re='\([^-]*\)-\([^-]*\)-\([^-]*\)-\([^-]*\)' + +echo $@ | +tr ' ' '\n' | +sed "s/$re/\1-\2/" | +sort -u | +(cat Makefile +echo +while read i +do + cat <<EOF +all: cc1-$i +OBJ-$i= \$(OBJ) target/$i/arch.o +\$(OBJ-$i): \$(HDR) +cc1-$i: \$(OBJ-$i) \$(LIBDIR)/libcc.a + \$(CC) \$(SCC_LDFLAGS) \$(OBJ-$i) -lcc -o \$@ + +EOF + echo +done) > $$.mk && mv $$.mk makefile diff --git a/cc1/target/amd64-sysv/arch.c b/cc1/target/amd64-sysv/arch.c @@ -0,0 +1,222 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc1/arch/amd64-sysv/arch.c"; +#include <stdio.h> + +#include "../../../inc/cc.h" +#include "../../cc1.h" + +#define RANK_BOOL 0 +#define RANK_SCHAR 1 +#define RANK_UCHAR 1 +#define RANK_CHAR 1 +#define RANK_SHORT 2 +#define RANK_USHORT 2 +#define RANK_INT 3 +#define RANK_UINT 3 +#define RANK_LONG 4 +#define RANK_ULONG 4 +#define RANK_LLONG 5 +#define RANK_ULLONG 5 +#define RANK_FLOAT 6 +#define RANK_DOUBLE 7 +#define RANK_LDOUBLE 8 + +/* + * Initializaion of type pointers were done with + * a C99 initilizator '... = &(Type) {...', but + * c compiler in Plan9 gives error with this + * syntax, so I have switched it to this ugly form + * I hope I will change it again in the future + */ + +static Type types[] = { + { /* 0 = voidtype */ + .op = VOID, + .letter = L_VOID, + }, + { /* 1 = pvoidtype */ + .op = PTR, + .letter = L_POINTER, + .prop = TDEFINED, + .type = &types[5], /* chartype */ + .size = 8, + .align = 8, + }, + { /* 2 = booltype */ + .op = INT, + .letter = L_BOOL, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 1, + .align = 1, + .n.rank = RANK_BOOL, + }, + { /* 3 = schartype */ + .op = INT, + .letter = L_INT8, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 1, + .align = 1, + .n.rank = RANK_SCHAR, + }, + { /* 4 = uchartype */ + .op = INT, + .letter = L_UINT8, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 1, + .align = 1, + .n.rank = RANK_UCHAR, + }, + { /* 5 = chartype */ + .op = INT, + .letter = L_INT8, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 1, + .align = 1, + .n.rank = RANK_CHAR, + }, + { /* 6 = ushortype */ + .op = INT, + .letter = L_UINT16, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 2, + .align = 2, + .n.rank = RANK_USHORT, + }, + { /* 7 = shortype */ + .op = INT, + .letter = L_INT16, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 2, + .align = 2, + .n.rank = RANK_SHORT, + }, + { /* 8 = uinttype */ + .op = INT, + .letter = L_UINT32, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 4, + .align = 4, + .n.rank = RANK_UINT, + }, + { /* 9 = inttype */ + .op = INT, + .letter = L_INT32, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 4, + .align = 4, + .n.rank = RANK_INT, + }, + { /* 10 = longtype */ + .op = INT, + .letter = L_INT64, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 8, + .align = 8, + .n.rank = RANK_LONG, + }, + { /* 11 = ulongtype */ + .op = INT, + .letter = L_UINT64, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 8, + .align = 8, + .n.rank = RANK_ULONG, + }, + { /* 12 = ullongtype */ + .op = INT, + .letter = L_UINT64, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 8, + .align = 8, + .n.rank = RANK_ULLONG, + }, + { /* 13 = llongtype */ + .op = INT, + .letter = L_INT64, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 8, + .align = 8, + .n.rank = RANK_LLONG, + }, + { /* 14 = floattype */ + .op = FLOAT, + .letter = L_FLOAT, + .prop = TDEFINED | TARITH, + .size = 4, + .align = 4, + .n.rank = RANK_FLOAT, + }, + { /* 15 = doubletype */ + .op = FLOAT, + .letter = L_DOUBLE, + .prop = TDEFINED | TARITH, + .size = 8, + .align = 8, + .n.rank = RANK_DOUBLE, + }, + { /* 16 = ldoubletype */ + .op = FLOAT, + .letter = L_LDOUBLE, + .prop = TDEFINED | TARITH, + .size = 16, + .align = 16, + .n.rank = RANK_LDOUBLE, + }, + { /* 17 = sizettype */ + .op = INT, + .letter = L_UINT64, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 8, + .align = 8, + .n.rank = RANK_UINT, + }, + { /* 18 = ellipsis */ + .op = ELLIPSIS, + .letter = L_ELLIPSIS, + .prop = TDEFINED, + }, + { /* 19 = pdifftype */ + .op = INT, + .letter = L_INT64, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 8, + .align = 8, + .n.rank = RANK_LONG, + }, + { /* 20 = va_type */ + .op = STRUCT, + .letter = L_VA_ARG, + .prop = TDEFINED, + .size = 24, + .align = 8, + }, +}; + +Type *voidtype = &types[0], *pvoidtype = &types[1], + *booltype = &types[2], *schartype = &types[3], + *uchartype = &types[4], *chartype = &types[5], + *ushortype = &types[6], *shortype = &types[7], + *uinttype = &types[8], *inttype = &types[9], + *longtype = &types[10], *ulongtype = &types[11], + *ullongtype = &types[12], *llongtype = &types[13], + *floattype = &types[14], *doubletype = &types[15], + *ldoubletype = &types[16], + *sizettype = &types[17], *pdifftype = &types[19], + *ellipsistype = &types[18], *va_type = &types[20], + *va_list_type; + +static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, + dummy1 = {.u.i = 1, .type = &types[9]}; +Symbol *zero = &dummy0, *one = &dummy1; + +void +iarch(void) +{ + va_list_type = mktype(va_type, ARY, 1, NULL); +} + +int +valid_va_list(Type *tp) +{ + return tp->op == PTR && eqtype(tp->type, va_type, 1); +} diff --git a/cc1/target/i386-sysv/arch.c b/cc1/target/i386-sysv/arch.c @@ -0,0 +1,223 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc1/arch/i386-sysv/arch.c"; +#include <stdio.h> + +#include "../../../inc/cc.h" +#include "../../cc1.h" + +#define RANK_BOOL 0 +#define RANK_SCHAR 1 +#define RANK_UCHAR 1 +#define RANK_CHAR 1 +#define RANK_SHORT 2 +#define RANK_USHORT 2 +#define RANK_INT 3 +#define RANK_UINT 3 +#define RANK_LONG 4 +#define RANK_ULONG 4 +#define RANK_LLONG 5 +#define RANK_ULLONG 5 +#define RANK_FLOAT 6 +#define RANK_DOUBLE 7 +#define RANK_LDOUBLE 8 + +/* + * Initializaion of type pointers were done with + * a C99 initilizator '... = &(Type) {...', but + * c compiler in Plan9 gives error with this + * syntax, so I have switched it to this ugly form + * I hope I will change it again in the future + */ + +static Type types[] = { + { /* 0 = voidtype */ + .op = VOID, + .letter = L_VOID, + }, + { /* 1 = pvoidtype */ + .op = PTR, + .letter = L_POINTER, + .prop = TDEFINED, + .type = &types[5], /* chartype */ + .size = 4, + .align = 4, + }, + { /* 2 = booltype */ + .op = INT, + .letter = L_BOOL, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 1, + .align = 1, + .n.rank = RANK_BOOL, + }, + { /* 3 = schartype */ + .op = INT, + .letter = L_INT8, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 1, + .align = 1, + .n.rank = RANK_SCHAR, + }, + { /* 4 = uchartype */ + .op = INT, + .letter = L_UINT8, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 1, + .align = 1, + .n.rank = RANK_UCHAR, + }, + { /* 5 = chartype */ + .op = INT, + .letter = L_INT8, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 1, + .align = 1, + .n.rank = RANK_CHAR, + }, + { /* 6 = ushortype */ + .op = INT, + .letter = L_UINT16, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 2, + .align = 2, + .n.rank = RANK_USHORT, + }, + { /* 7 = shortype */ + .op = INT, + .letter = L_INT16, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 2, + .align = 2, + .n.rank = RANK_SHORT, + }, + { /* 8 = uinttype */ + .op = INT, + .letter = L_UINT32, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 4, + .align = 4, + .n.rank = RANK_UINT, + }, + { /* 9 = inttype */ + .op = INT, + .letter = L_INT32, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 4, + .align = 4, + .n.rank = RANK_INT, + }, + { /* 10 = longtype */ + .op = INT, + .letter = L_INT32, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 4, + .align = 4, + .n.rank = RANK_LONG, + }, + { /* 11 = ulongtype */ + .op = INT, + .letter = L_UINT32, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 4, + .align = 4, + .n.rank = RANK_ULONG, + }, + { /* 12 = ullongtype */ + .op = INT, + .letter = L_UINT64, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 8, + .align = 4, + .n.rank = RANK_ULLONG, + }, + { /* 13 = llongtype */ + .op = INT, + .letter = L_INT64, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 8, + .align = 4, + .n.rank = RANK_LLONG, + }, + { /* 14 = floattype */ + .op = FLOAT, + .letter = L_FLOAT, + .prop = TDEFINED | TARITH, + .size = 4, + .align = 4, + .n.rank = RANK_FLOAT, + }, + { /* 15 = doubletype */ + .op = FLOAT, + .letter = L_DOUBLE, + .prop = TDEFINED | TARITH, + .size = 8, + .align = 4, + .n.rank = RANK_DOUBLE, + }, + { /* 16 = ldoubletype */ + .op = FLOAT, + .letter = L_LDOUBLE, + .prop = TDEFINED | TARITH, + .size = 12, + .align = 4, + .n.rank = RANK_LDOUBLE, + }, + { /* 17 = sizettype */ + .op = INT, + .letter = L_UINT32, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 4, + .align = 4, + .n.rank = RANK_UINT, + }, + { /* 18 = ellipsis */ + .op = ELLIPSIS, + .letter = L_ELLIPSIS, + .prop = TDEFINED, + }, + { /* 19 = pdifftype */ + .op = INT, + .letter = L_INT32, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 4, + .align = 4, + .n.rank = RANK_INT, + }, + { /* 20 = va_list_type */ + .op = PTR, + .letter = L_POINTER, + .prop = TDEFINED, + .size = 4, + .align = 4, + }, +}; + + +Type *voidtype = &types[0], *pvoidtype = &types[1], + *booltype = &types[2], *schartype = &types[3], + *uchartype = &types[4], *chartype = &types[5], + *ushortype = &types[6], *shortype = &types[7], + *uinttype = &types[8], *inttype = &types[9], + *longtype = &types[10], *ulongtype = &types[11], + *ullongtype = &types[12], *llongtype = &types[13], + *floattype = &types[14], *doubletype = &types[15], + *ldoubletype = &types[16], + *sizettype = &types[17], *pdifftype = &types[19], + *ellipsistype = &types[18], *va_list_type = &types[20]; + + + +static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, + dummy1 = {.u.i = 1, .type = &types[9]}; +Symbol *zero = &dummy0, *one = &dummy1; + +void +iarch(void) +{ +} + +int +valid_va_list(Type *tp) +{ + return eqtype(tp, va_list_type, 1); +} diff --git a/cc1/target/z80-scc/arch.c b/cc1/target/z80-scc/arch.c @@ -0,0 +1,221 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc1/arch/z80/arch.c"; +#include <stdio.h> + +#include "../../../inc/cc.h" +#include "../../cc1.h" + +#define RANK_BOOL 0 +#define RANK_SCHAR 1 +#define RANK_UCHAR 1 +#define RANK_CHAR 1 +#define RANK_SHORT 2 +#define RANK_USHORT 2 +#define RANK_INT 3 +#define RANK_UINT 3 +#define RANK_LONG 4 +#define RANK_ULONG 4 +#define RANK_LLONG 5 +#define RANK_ULLONG 5 +#define RANK_FLOAT 6 +#define RANK_DOUBLE 7 +#define RANK_LDOUBLE 8 + +/* + * Initializaion of type pointers were done with + * a C99 initilizator '... = &(Type) {...', but + * c compiler in Plan9 gives error with this + * syntax, so I have switched it to this ugly form + * I hope I will change it again in the future + */ + +static Type types[] = { + { /* 0 = voidtype */ + .op = VOID, + .letter = L_VOID, + }, + { /* 1 = pvoidtype */ + .op = PTR, + .letter = L_POINTER, + .prop = TDEFINED, + .type = &types[5], /* char type */ + .size = 2, + .align = 2, + }, + { /* 2 = booltype */ + .op = INT, + .letter = L_BOOL, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 1, + .align = 1, + .n.rank = RANK_BOOL, + }, + { /* 3 = schartype */ + .op = INT, + .letter = L_INT8, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 1, + .align = 1, + .n.rank = RANK_SCHAR, + }, + { /* 4 = uchartype */ + .op = INT, + .letter = L_UINT8, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 1, + .align = 1, + .n.rank = RANK_UCHAR, + }, + { /* 5 = chartype */ + .op = INT, + .letter = L_UINT8, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 1, + .align = 1, + .n.rank = RANK_CHAR, + }, + { /* 6 = ushortype */ + .op = INT, + .letter = L_UINT16, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 2, + .align = 1, + .n.rank = RANK_USHORT, + }, + { /* 7 = shortype */ + .op = INT, + .letter = L_INT16, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 2, + .align = 1, + .n.rank = RANK_SHORT, + }, + { /* 8 = uinttype */ + .op = INT, + .letter = L_UINT16, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 2, + .align = 1, + .n.rank = RANK_UINT, + }, + { /* 9 = inttype */ + .op = INT, + .letter = L_INT16, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 2, + .align = 1, + .n.rank = RANK_INT, + }, + { /* 10 = longtype */ + .op = INT, + .letter = L_INT32, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 4, + .align = 1, + .n.rank = RANK_LONG, + }, + { /* 11 = ulongtype */ + .op = INT, + .letter = L_UINT32, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 4, + .align = 1, + .n.rank = RANK_ULONG, + }, + { /* 12 = ullongtype */ + .op = INT, + .letter = L_UINT64, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 8, + .align = 1, + .n.rank = RANK_ULLONG, + }, + { /* 13 = llongtype */ + .op = INT, + .letter = L_INT64, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 8, + .align = 1, + .n.rank = RANK_LLONG, + }, + { /* 14 = floattype */ + .op = FLOAT, + .letter = L_FLOAT, + .prop = TDEFINED | TARITH, + .size = 4, + .align = 1, + .n.rank = RANK_FLOAT, + }, + { /* 15 = doubletype */ + .op = FLOAT, + .letter = L_DOUBLE, + .prop = TDEFINED | TARITH, + .size = 8, + .align = 1, + .n.rank = RANK_DOUBLE, + }, + { /* 16 = ldoubletype */ + .op = FLOAT, + .letter = L_LDOUBLE, + .prop = TDEFINED | TARITH, + .size = 16, + .align = 1, + .n.rank = RANK_LDOUBLE, + }, + { /* 17 = sizettype */ + .op = INT, + .letter = L_UINT16, + .prop = TDEFINED | TINTEGER | TARITH, + .size = 2, + .align = 1, + .n.rank = RANK_UINT, + }, + { /* 18 = ellipsis */ + .op = ELLIPSIS, + .letter = L_ELLIPSIS, + .prop = TDEFINED, + }, + { /* 7 = pdifftype */ + .op = INT, + .letter = L_INT16, + .prop = TDEFINED | TINTEGER | TARITH | TSIGNED, + .size = 2, + .align = 1, + .n.rank = RANK_SHORT, + }, + { /* 20 = va_list_type */ + .op = PTR, + .letter = L_POINTER, + .prop = TDEFINED, + .size = 2, + .align = 1, + } +}; + +Type *voidtype = &types[0], *pvoidtype = &types[1], + *booltype = &types[2], *schartype = &types[3], + *uchartype = &types[4], *chartype = &types[5], + *ushortype = &types[6], *shortype = &types[7], + *uinttype = &types[8], *inttype = &types[9], + *longtype = &types[10], *ulongtype = &types[11], + *ullongtype = &types[12], *llongtype = &types[13], + *floattype = &types[14], *doubletype = &types[15], + *ldoubletype = &types[16], + *sizettype = &types[17], *pdifftype = &types[19], + *ellipsistype = &types[18], *va_list_type = &types[20]; + + +static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, + dummy1 = {.u.i = 1, .type = &types[9]}; +Symbol *zero = &dummy0, *one = &dummy1; + +void +iarch(void) +{ +} + +int +valid_va_list(Type *tp) +{ + return eqtype(tp, va_list_type, 1); +} diff --git a/cc2/Makefile b/cc2/Makefile @@ -1,31 +1,37 @@ # See LICENSE file for copyright and license details. .POSIX: -CSTDINC = -I../inc/$(STD) +# Makefile is only used as a template for makefile. +# If you modify Makefile remember to run make dep include ../config.mk -OBJS = main.o parser.o peep.o symbol.o node.o code.o optm.o\ - arch/$(ARCH)/code.o arch/$(ARCH)/cgen.o \ - arch/$(ARCH)/types.o arch/$(ARCH)/optm.o +MOREFLAGS = -I../inc/$(STD) +LIBDIR = ../lib/cc -all: cc2-$(ARCH) +OBJ = main.o parser.o peep.o symbol.o node.o code.o optm.o +HDR = cc2.h ../inc/$(STD)/cstd.h ../inc/cc.h + +all: + +dep: + MKQBE=${MKQBE} ./gendep.sh $(TARGETS) + +$(LIBDIR)/libcc.a: + cd $(LIBDIR) && $(MAKE) main.o: error.h -$(OBJS): cc2.h ../inc/$(STD)/cstd.h ../inc/cc.h -arch/$(ARCH)/code.o: arch/$(ARCH)/arch.h -arch/$(ARCH)/cgen.o: arch/$(ARCH)/arch.h error.h: cc2.h rm -f $@; trap 'rm -f $$$$.h' EXIT INT QUIT ;\ awk -f generror.awk cc2.h > $$$$.h && mv $$$$.h $@ -../lib/libcc.a: - cd ../lib && $(MAKE) -e - -cc2-$(ARCH): $(OBJS) ../lib/libcc.a - $(CC) $(SCC_LDFLAGS) $(OBJS) ../lib/libcc.a -o $@ - clean: - rm -f $(OBJS) + rm -f *.o + rm -f target/*/*.o rm -f cc2-* error.h + +distclean: clean + rm -f makefile + +include ../lib/cc/libdep.mk diff --git a/cc2/arch/amd64-sysv/arch.h b/cc2/arch/amd64-sysv/arch.h @@ -1 +0,0 @@ -/* See LICENSE file for copyright and license details. */ diff --git a/cc2/arch/amd64-sysv/cgen.c b/cc2/arch/amd64-sysv/cgen.c @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/cgen.c"; - -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -cgen(Node *np) -{ -} - -Node * -sethi(Node *np) -{ -} diff --git a/cc2/arch/amd64-sysv/code.c b/cc2/arch/amd64-sysv/code.c @@ -1,211 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/code.c"; - -#include <stdio.h> -#include <stdlib.h> - -#include <cstd.h> -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -enum segment { - CODESEG, - DATASEG, - BSSSEG, - NOSEG -}; - -static int curseg = NOSEG; - -static void -segment(int seg) -{ - static char *txt[] = { - [CODESEG] = "\t.text\n", - [DATASEG] = "\t.data\n", - [BSSSEG] = "\t.bss\n", - }; - - if (seg == curseg) - return; - fputs(txt[seg], stdout); - curseg = seg; -} - -static char * -symname(Symbol *sym) -{ - static char name[INTIDENTSIZ+1]; - - if (sym->name) { - switch (sym->kind) { - case SEXTRN: - case SGLOB: - case SPRIV: - return sym->name; - } - } - - sprintf(name, ".L%d", sym->numid); - - return name; -} - -static void -emitconst(Node *np) -{ - switch (np->type.size) { - case 1: - printf("%d", (int) np->u.i & 0xFF); - break; - case 2: - printf("%d", (int) np->u.i & 0xFFFF); - break; - case 4: - printf("%ld", (long) np->u.i & 0xFFFFFFFF); - break; - case 8: - printf("%lld", (long long) np->u.i & 0xFFFFFFFF); - break; - default: - abort(); - } -} - -static void -emittree(Node *np) -{ - if (!np) - return; - - switch (np->op) { - case OSTRING: - printf("\"%s\"", np->u.s); - free(np->u.s); - np->u.s = NULL; - break; - case OCONST: - emitconst(np); - break; - case OADDR: - emittree(np->left); - break; - case OMEM: - fputs(symname(np->u.sym), stdout); - break; - default: - emittree(np->left); - printf(" %c ", np->op); - emittree(np->right); - break; - } -} -static void -size2asm(Type *tp) -{ - char *s; - - if (tp->flags & STRF) { - s = "\t.ascii\t"; - } else { - switch (tp->size) { - case 1: - s = "\t.byte\t"; - break; - case 2: - s = "\t.short\t"; - break; - case 4: - s = "\t.long\t"; - break; - case 8: - s = "\t.quad\t"; - break; - default: - s = "\t.space\t%lu,"; - break; - } - } - printf(s, tp->size); -} - - -void -data(Node *np) -{ - size2asm(&np->type); - emittree(np); - putchar('\n'); -} - -static void -label(Symbol *sym) -{ - int seg; - char *name = symname(sym); - Type *tp = &sym->type; - - if (sym->type.flags & FUNF) - seg = CODESEG; - else if (sym->type.flags & INITF) - seg = DATASEG; - else - seg = BSSSEG; - segment(seg); - - switch (sym->kind) { - case SEXTRN: - printf("\t.extern\t%s\n", name); - case SLOCAL: - return; - case SGLOB: - printf("\t.global\t%s\n", name); - if (seg == BSSSEG) - printf("\t.comm\t%s,%lu\n", name, tp->size); - break; - } - if (sym->type.align != 1) - printf("\t.align\t%lu\n", sym->type.align ); - printf("%s:\n", name); -} - -void -defglobal(Symbol *sym) -{ - label(sym); - if (sym->kind == SEXTRN || (sym->type.flags & INITF)) - return; - size2asm(&sym->type); - puts("0"); -} - -void -defvar(Symbol *sym) -{ -} - -void -defpar(Symbol *sym) -{ -} - -void -newfun(void) -{ -} - -void -writeout(void) -{ -} - -void -endinit(void) -{ -} - -void -getbblocks(void) -{ -} diff --git a/cc2/arch/amd64-sysv/optm.c b/cc2/arch/amd64-sysv/optm.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/optm.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -optm_dep(Node *np) -{ - return np; -} diff --git a/cc2/arch/amd64-sysv/types.c b/cc2/arch/amd64-sysv/types.c @@ -1,94 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/types.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - - -Type int8type = { - .flags = SIGNF | INTF, - .size = 1, - .align = 1 -}; - -Type int16type = { - .flags = SIGNF | INTF, - .size = 2, - .align = 2 -}; - -Type int32type = { - .flags = SIGNF | INTF, - .size = 4, - .align = 4 -}; - -Type int64type = { - .flags = SIGNF | INTF, - .size = 8, - .align = 8 -}; - -Type uint8type = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type uint16type = { - .flags = INTF, - .size = 2, - .align = 2 -}; - -Type uint32type = { - .flags = INTF, - .size = 4, - .align = 4 -}; - -Type uint64type = { - .flags = INTF, - .size = 8, - .align = 2 -}; - -Type ptrtype = { - .flags = INTF, - .size = 8, - .align = 8 -}; - -Type booltype = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type float32type = { - .flags = FLOATF, - .size = 4, - .align = 4 -}; - -Type float64type = { - .flags = FLOATF, - .size = 8, - .align = 8 -}; - -Type float80type = { - .flags = FLOATF, - .size = 16, - .align = 16 -}; - -Type voidtype = { - .size = 0, - .align = 0 -}; - -Type arg_type = { - .size = 24, - .align = 8 -}; diff --git a/cc2/arch/i386-sysv/arch.h b/cc2/arch/i386-sysv/arch.h @@ -1 +0,0 @@ -/* See LICENSE file for copyright and license details. */ diff --git a/cc2/arch/i386-sysv/cgen.c b/cc2/arch/i386-sysv/cgen.c @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/cgen.c"; - -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -cgen(Node *np) -{ -} - -Node * -sethi(Node *np) -{ -} diff --git a/cc2/arch/i386-sysv/code.c b/cc2/arch/i386-sysv/code.c @@ -1,209 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/code.c"; -#include <stdio.h> -#include <stdlib.h> - -#include <cstd.h> -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -enum segment { - CODESEG, - DATASEG, - BSSSEG, - NOSEG -}; - -static int curseg = NOSEG; - -static void -segment(int seg) -{ - static char *txt[] = { - [CODESEG] = "\t.text\n", - [DATASEG] = "\t.data\n", - [BSSSEG] = "\t.bss\n", - }; - - if (seg == curseg) - return; - fputs(txt[seg], stdout); - curseg = seg; -} - -static char * -symname(Symbol *sym) -{ - static char name[INTIDENTSIZ+1]; - - if (sym->name) { - switch (sym->kind) { - case SEXTRN: - case SGLOB: - case SPRIV: - return sym->name; - } - } - - sprintf(name, ".L%d", sym->numid); - - return name; -} - -static void -emitconst(Node *np) -{ - switch (np->type.size) { - case 1: - printf("%d", (int) np->u.i & 0xFF); - break; - case 2: - printf("%d", (int) np->u.i & 0xFFFF); - break; - case 4: - printf("%ld", (long) np->u.i & 0xFFFFFFFF); - break; - case 8: - printf("%lld", (long long) np->u.i & 0xFFFFFFFF); - break; - default: - abort(); - } -} - -static void -emittree(Node *np) -{ - if (!np) - return; - - switch (np->op) { - case OSTRING: - printf("\"%s\"", np->u.s); - free(np->u.s); - np->u.s = NULL; - break; - case OCONST: - emitconst(np); - break; - case OADDR: - emittree(np->left); - break; - case OMEM: - fputs(symname(np->u.sym), stdout); - break; - default: - emittree(np->left); - printf(" %c ", np->op); - emittree(np->right); - break; - } -} -static void -size2asm(Type *tp) -{ - char *s; - - if (tp->flags & STRF) { - s = "\t.ascii\t"; - } else { - switch (tp->size) { - case 1: - s = "\t.byte\t"; - break; - case 2: - s = "\t.short\t"; - break; - case 4: - s = "\t.long\t"; - break; - case 8: - s = "\t.quad\t"; - break; - default: - s = "\t.space\t%lu,"; - break; - } - } - printf(s, tp->size); -} - -void -data(Node *np) -{ - size2asm(&np->type); - emittree(np); - putchar('\n'); -} - -static void -label(Symbol *sym) -{ - int seg; - char *name = symname(sym); - Type *tp = &sym->type; - - if (sym->type.flags & FUNF) - seg = CODESEG; - else if (sym->type.flags & INITF) - seg = DATASEG; - else - seg = BSSSEG; - segment(seg); - - switch (sym->kind) { - case SEXTRN: - printf("\t.extern\t%s\n", name); - case SLOCAL: - return; - case SGLOB: - printf("\t.global\t%s\n", name); - if (seg == BSSSEG) - printf("\t.comm\t%s,%lu\n", name, tp->size); - break; - } - if (sym->type.align != 1) - printf("\t.align\t%lu\n", sym->type.align ); - printf("%s:\n", name); -} - -void -defglobal(Symbol *sym) -{ - label(sym); - if (sym->kind == SEXTRN || (sym->type.flags & INITF)) - return; - size2asm(&sym->type); - puts("0"); -} - -void -defpar(Symbol *sym) -{ -} - -void -defvar(Symbol *sym) -{ -} - -void -newfun(void) -{ -} - -void -writeout(void) -{ -} - -void -endinit(void) -{ -} - -void -getbblocks(void) -{ -} diff --git a/cc2/arch/i386-sysv/optm.c b/cc2/arch/i386-sysv/optm.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/optm.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -optm_dep(Node *np) -{ - return np; -} diff --git a/cc2/arch/i386-sysv/types.c b/cc2/arch/i386-sysv/types.c @@ -1,95 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/types.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - - -Type int8type = { - .flags = SIGNF | INTF, - .size = 1, - .align = 1 -}; - -Type int16type = { - .flags = SIGNF | INTF, - .size = 2, - .align = 2 -}; - -Type int32type = { - .flags = SIGNF | INTF, - .size = 4, - .align = 4 -}; - -Type int64type = { - .flags = SIGNF | INTF, - .size = 8, - .align = 4 -}; - -Type uint8type = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type uint16type = { - .flags = INTF, - .size = 2, - .align = 2 -}; - -Type uint32type = { - .flags = INTF, - .size = 4, - .align = 2 -}; - -Type uint64type = { - .flags = INTF, - .size = 8, - .align = 4 -}; - -Type ptrtype = { - .flags = INTF, - .size = 4, - .align = 4 -}; - -Type booltype = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type float32type = { - .flags = FLOATF, - .size = 4, - .align = 4 -}; - -Type float64type = { - .flags = FLOATF, - .size = 8, - .align = 4 -}; - -Type float80type = { - .flags = FLOATF, - .size = 12, - .align = 4 -}; - -Type voidtype = { - .size = 0, - .align = 0 -}; - -/* this type is not used in this architecture */ -Type arg_type = { - .size = 0, - .align = 0 -}; diff --git a/cc2/arch/qbe/arch.h b/cc2/arch/qbe/arch.h @@ -1,137 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -enum asmop { - ASNOP = 0, - ASSTB, - ASSTH, - ASSTW, - ASSTL, - ASSTM, - ASSTS, - ASSTD, - - ASLDSB, - ASLDUB, - ASLDSH, - ASLDUH, - ASLDSW, - ASLDUW, - ASLDL, - ASLDS, - ASLDD, - - ASADDW, - ASSUBW, - ASMULW, - ASMODW, - ASUMODW, - ASDIVW, - ASUDIVW, - ASSHLW, - ASSHRW, - ASUSHRW, - ASLTW, - ASULTW, - ASGTW, - ASUGTW, - ASLEW, - ASULEW, - ASGEW, - ASUGEW, - ASEQW, - ASNEW, - ASBANDW, - ASBORW, - ASBXORW, - - ASADDL, - ASSUBL, - ASMULL, - ASMODL, - ASUMODL, - ASDIVL, - ASUDIVL, - ASSHLL, - ASSHRL, - ASUSHRL, - ASLTL, - ASULTL, - ASGTL, - ASUGTL, - ASLEL, - ASULEL, - ASGEL, - ASUGEL, - ASEQL, - ASNEL, - ASBANDL, - ASBORL, - ASBXORL, - - ASADDS, - ASSUBS, - ASMULS, - ASDIVS, - ASLTS, - ASGTS, - ASLES, - ASGES, - ASEQS, - ASNES, - - ASADDD, - ASSUBD, - ASMULD, - ASDIVD, - ASLTD, - ASGTD, - ASLED, - ASGED, - ASEQD, - ASNED, - - ASEXTBW, - ASUEXTBW, - ASEXTBL, - ASUEXTBL, - ASEXTHW, - ASUEXTHW, - ASEXTHL, - ASUEXTHL, - ASEXTWL, - ASUEXTWL, - - ASSTOL, - ASSTOW, - ASDTOL, - ASDTOW, - - ASSWTOD, - ASSWTOS, - ASSLTOD, - ASSLTOS, - - ASEXTS, - ASTRUNCD, - - ASJMP, - ASBRANCH, - ASRET, - ASCALL, - ASCALLE, - ASCALLEX, - ASPAR, - ASPARE, - ASALLOC, - ASFORM, - - ASCOPYB, - ASCOPYH, - ASCOPYW, - ASCOPYL, - ASCOPYS, - ASCOPYD, - - ASVSTAR, - ASVARG, -}; diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -1,731 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/qbe/cgen.c"; - -#include <assert.h> -#include <stdlib.h> - -#include <cstd.h> -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -enum sflags { - ISTMP = 1, - ISCONS = 2 -}; - -static char opasmw[] = { - [OADD] = ASADDW, - [OSUB] = ASSUBW, - [OMUL] = ASMULW, - [OMOD] = ASMODW, - [ODIV] = ASDIVW, - [OSHL] = ASSHLW, - [OSHR] = ASSHRW, - [OLT] = ASLTW, - [OGT] = ASGTW, - [OLE] = ASLEW, - [OGE] = ASGEW, - [OEQ] = ASEQW, - [ONE] = ASNEW, - [OBAND] = ASBANDW, - [OBOR] = ASBORW, - [OBXOR] = ASBXORW, -}; - -static char opasml[] = { - [OADD] = ASADDL, - [OSUB] = ASSUBL, - [OMUL] = ASMULL, - [OMOD] = ASMODL, - [ODIV] = ASDIVL, - [OSHL] = ASSHLL, - [OSHR] = ASSHRL, - [OLT] = ASLTL, - [OGT] = ASGTL, - [OLE] = ASLEL, - [OGE] = ASGEL, - [OEQ] = ASEQL, - [ONE] = ASNEL, - [OBAND] = ASBANDL, - [OBOR] = ASBORL, - [OBXOR] = ASBXORL, -}; - -static char opasms[] = { - [OADD] = ASADDS, - [OSUB] = ASSUBS, - [OMUL] = ASMULS, - [ODIV] = ASDIVS, - [OLT] = ASLTS, - [OGT] = ASGTS, - [OLE] = ASLES, - [OGE] = ASGES, - [OEQ] = ASEQS, - [ONE] = ASNES, -}; -static char opasmd[] = { - [OADD] = ASADDD, - [OSUB] = ASSUBD, - [OMUL] = ASMULD, - [ODIV] = ASDIVD, - [OLT] = ASLTD, - [OGT] = ASGTD, - [OLE] = ASLED, - [OGE] = ASGED, - [OEQ] = ASEQD, - [ONE] = ASNED, -}; - -extern Type int32type, uint32type, ptrtype; - -static Node * -tmpnode(Node *np, Type *tp) -{ - char flags; - Symbol *sym; - - if (!np) - np = newnode(OTMP); - sym = getsym(TMPSYM); - sym->type = np->type = *tp; - flags = tp->flags & ~(PARF|INITF); - sym->type.flags = np->type.flags = flags; - sym->kind = STMP; - np->left = np->right = NULL; - np->u.sym = sym; - np->op = OTMP; - np->flags |= ISTMP; - return np; -} - -static Node * -load(Type *tp, Node *np, Node *new) -{ - int op; - int flags = tp->flags; - - if (flags & (AGGRF|FUNF)) { - *new = *np; - return new; - } - switch (tp->size) { - case 1: - op = ASLDSB; - break; - case 2: - op = ASLDSH; - break; - case 4: - op = (flags & FLOATF) ? ASLDS : ASLDSW; - break; - case 8: - op = (flags & FLOATF) ? ASLDD : ASLDL; - break; - default: - abort(); - } - /* - * unsigned version of operations are always +1 the - * signed version - */ - if ((flags & (INTF|SIGNF)) == INTF && tp->size < 8) - ++op; - - code(op, tmpnode(new, tp), np, NULL); - - return new; -} - -static Node *rhs(Node *np, Node *new); - -static Node * -cast(Type *td, Node *ns, Node *nd) -{ - Type *ts; - Node aux1, aux2; - int op, d_isint, s_isint; - - ts = &ns->type; - d_isint = (td->flags & INTF) != 0; - s_isint = (ts->flags & INTF) != 0; - - if (d_isint && s_isint) { - if (td->size <= ts->size) { - *nd = *ns; - return nd; - } - assert(td->size == 4 || td->size == 8); - switch (ts->size) { - case 1: - op = (td->size == 4) ? ASEXTBW : ASEXTBL; - break; - case 2: - op = (td->size == 4) ? ASEXTHW : ASEXTHL; - break; - case 4: - op = ASEXTWL; - break; - default: - abort(); - } - /* - * unsigned version of operations are always +1 the - * signed version - */ - op += (ts->flags & SIGNF) == 0; - } else if (d_isint) { - /* conversion from float to int */ - switch (ts->size) { - case 4: - op = (td->size == 8) ? ASSTOL : ASSTOW; - break; - case 8: - op = (td->size == 8) ? ASDTOL : ASDTOW; - break; - default: - abort(); - } - /* TODO: Add signess */ - } else if (s_isint) { - /* conversion from int to float */ - switch (ts->size) { - case 1: - case 2: - ts = (ts->flags&SIGNF) ? &int32type : &uint32type; - ns = cast(ts, ns, tmpnode(&aux2, ts)); - case 4: - op = (td->size == 8) ? ASSWTOD : ASSWTOS; - break; - case 8: - op = (td->size == 8) ? ASSLTOD : ASSLTOS; - break; - default: - abort(); - } - /* TODO: Add signess */ - } else { - /* conversion from float to float */ - op = (td->size == 4) ? ASEXTS : ASTRUNCD; - } - - code(op, tmpnode(nd, td), ns, NULL); - return nd; -} - -static Node *rhs(Node *np, Node *new); - -static Node * -call(Node *np, Node *fun, Node *ret) -{ - int n, op; - Type *tp; - Node aux, **q, *p, *pars[NR_FUNPARAM]; - - for (n = 0, p = np->right; p; p = p->right) - pars[n++] = rhs(p->left, newnode(OTMP)); - - tp = &np->type; - code(ASCALL, tmpnode(ret, tp), fun, NULL); - - for (q = pars; q < &pars[n]; ++q) { - op = (q == &pars[n-1]) ? ASPARE : ASPAR; - tmpnode(&aux, &(*q)->type); - code(op, NULL, *q, &aux); - } - code((np->op == OCALL) ? ASCALLE : ASCALLEX, NULL, NULL, NULL); - - return ret; -} - -static Node * -assign(Type *tp, Node *to, Node *from) -{ - int op; - - switch (tp->size) { - case 1: - op = ASSTB; - break; - case 2: - op = ASSTH; - break; - case 4: - op = (tp->flags & FLOATF) ? ASSTS : ASSTW; - break; - case 8: - op = (tp->flags & FLOATF) ? ASSTD : ASSTL; - break; - default: - op = ASSTM; - break; - } - code(op, to, from, NULL); - return from; -} - -static Node * -copy(Type *tp, Node *to, Node *from) -{ - int op; - - switch (tp->size) { - case 1: - op = ASCOPYB; - break; - case 2: - op = ASCOPYH; - break; - case 4: - op = (tp->flags & FLOATF) ? ASCOPYS : ASCOPYW; - break; - case 8: - op = (tp->flags & FLOATF) ? ASCOPYD : ASCOPYL; - break; - default: - /* TODO: Need to handle the general case */ - abort(); - } - code(op, to, from, NULL); - return from; -} - -/* TODO: Do field() transformation in sethi */ - -static Node * -field(Node *np, Node *ret, int islhs) -{ - Node base, node, off, add, *addr; - TUINT offset = np->right->u.sym->u.off; - - addr = rhs(np->left, &base); - - if (offset != 0) { - node.op = OADD; - node.type = ptrtype; - node.left = addr; - node.right = constnode(&off, offset, &ptrtype); - addr = rhs(&node, &add); - } - - if (islhs) - *ret = *addr; - else - load(&np->type, addr, ret); - - return ret; -} - -static Node * -lhs(Node *np, Node *new) -{ - switch (np->op) { - case OMEM: - case OAUTO: - *new = *np; - return new; - case OPTR: - return rhs(np->left, new); - case OFIELD: - return field(np, new, 1); - default: - abort(); - } -} - -static void -bool(Node *np, Symbol *true, Symbol *false) -{ - Node *l = np->left, *r = np->right; - Node ret, ifyes, ifno; - Symbol *label; - - switch (np->op) { - case ONEG: - bool(l, false, true); - break; - case OAND: - label = newlabel(); - bool(l, label, false); - setlabel(label); - bool(r, true, false); - break; - case OOR: - label = newlabel(); - bool(l, true, label); - setlabel(label); - bool(r, true, false); - break; - default: - label2node(&ifyes, true); - label2node(&ifno, false); - code(ASBRANCH, rhs(np, &ret), &ifyes, &ifno); - break; - } -} - -static Node * -ternary(Node *np, Node *ret) -{ - Node ifyes, ifno, phi, *colon, aux1, aux2, aux3; - - tmpnode(ret, &np->type); - label2node(&ifyes, NULL); - label2node(&ifno, NULL); - label2node(&phi, NULL); - - colon = np->right; - code(ASBRANCH, rhs(np->left, &aux1), &ifyes, &ifno); - - setlabel(ifyes.u.sym); - copy(&ret->type, ret, rhs(colon->left, &aux2)); - code(ASJMP, NULL, &phi, NULL); - - setlabel(ifno.u.sym); - copy(&ret->type, ret, rhs(colon->right, &aux3)); - setlabel(phi.u.sym); - - return ret; -} - -static Node * -function(void) -{ - Node aux; - Symbol *p; - - /* allocate stack space for parameters */ - for (p = locals; p && (p->type.flags & PARF) != 0; p = p->next) - code(ASALLOC, label2node(&aux, p), NULL, NULL); - - /* allocate stack space for local variables) */ - for ( ; p && p->id != TMPSYM; p = p->next) { - if (p->kind != SAUTO) - continue; - code(ASALLOC, label2node(&aux, p), NULL, NULL); - } - /* store formal parameters in parameters */ - for (p = locals; p; p = p->next) { - if ((p->type.flags & PARF) == 0) - break; - code(ASFORM, label2node(&aux, p), NULL, NULL); - } - return NULL; -} - -static void -swtch_if(Node *idx) -{ - Node aux1, aux2, *np; - Symbol *deflabel = NULL; - - for (;;) { - np = delstmt(); - setlabel(np->label); - - switch (np->op) { - case OESWITCH: - if (!deflabel) - deflabel = np->u.sym; - aux1.op = OJMP; - aux1.label = NULL; - aux1.u.sym = deflabel; - cgen(&aux1); - return; - case OCASE: - aux1 = *np; - aux1.op = OBRANCH; - aux1.label = NULL; - aux1.left = &aux2; - - aux2.op = OEQ; - aux2.type = idx->type; - aux2.left = np->left; - aux2.right = idx; - - cgen(&aux1); - break; - case ODEFAULT: - deflabel = np->u.sym; - break; - default: - abort(); - } - } -} - -static Node * -rhs(Node *np, Node *ret) -{ - Node aux1, aux2, *phi, *l = np->left, *r = np->right; - Type *tp; - int off, op; - char *tbl; - Symbol *true, *false; - - tp = &np->type; - - switch (np->op) { - case OBFUN: - return function(); - case ONOP: - case OBLOOP: - case OELOOP: - case OEFUN: - return NULL; - case OTMP: - case OCONST: - *ret = *np; - return np; - case OMEM: - case OAUTO: - return load(tp, np, ret); - case ONEG: - case OAND: - case OOR: - true = newlabel(); - false = newlabel(); - phi = label2node(&aux1, NULL); - tmpnode(ret, &int32type); - - bool(np, true, false); - - setlabel(true); - code(ASCOPYW, ret, constnode(&aux2, 1, &int32type), NULL); - code(ASJMP, NULL, phi, NULL); - - setlabel(false); - code(ASCOPYW, ret, constnode(&aux2, 0, &int32type), NULL); - - setlabel(phi->u.sym); - return ret; - case OMOD: - case OSHR: - assert(tp->flags & INTF); - case ODIV: - case OLT: - case OGT: - case OLE: - case OGE: - /* - * unsigned version of operations are always +1 the - * signed version - */ - off = (tp->flags & SIGNF) == 0; - goto binary; - case OSHL: - case OBAND: - case OBOR: - case OBXOR: - assert(tp->flags & INTF); - case OADD: - case OSUB: - case OMUL: - case OEQ: - case ONE: - off = 0; - binary: - if (l->complex >= r->complex) { - rhs(l, &aux1); - rhs(r, &aux2); - } else { - rhs(r, &aux2); - rhs(l, &aux1); - } - switch (tp->size) { - case 4: - tbl = (tp->flags & FLOATF) ? opasms : opasmw; - break; - case 8: - tbl = (tp->flags & FLOATF) ? opasmd : opasml; - break; - default: - abort(); - } - op = tbl[np->op] + off; - tmpnode(ret, tp); - code(op, ret, &aux1, &aux2); - return ret; - case OCALL: - case OCALLE: - if (l->op == OPTR) - l = rhs(l, &aux1); - return call(np, l, ret); - case OCAST: - return cast(tp, rhs(l, &aux1), ret); - case OASSIG: - /* TODO: Do this transformations in sethi */ - switch (np->u.subop) { - case OINC: - op = OADD; - goto post_oper; - case ODEC: - op = OSUB; - post_oper: - aux1.op = op; - aux1.left = rhs(l, ret); - aux1.right = r; - aux1.type = np->type; - rhs(&aux1, &aux2); - lhs(l, &aux1); - assign(tp, &aux1, &aux2); - break; - default: - aux2.type = np->type; - aux2.op = np->u.subop; - aux2.right = np->right; - aux2.left = np->left; - r = rhs(&aux2, &aux1); - Node aux3; - if (l->op == OCAST) { - aux3.type = l->left->type; - aux3.op = OCAST; - aux3.left = r; - aux3.right = NULL; - r = &aux3; - l = l->left; - } - case 0: - /* TODO: see what is the most difficult */ - lhs(l, &aux2); - rhs(r, ret); - return assign(tp, &aux2, ret); - } - return ret; - case OASK: - return ternary(np, ret); - case OCOMMA: - rhs(l, &aux1); - return rhs(r, ret); - case OPTR: - return load(tp, rhs(l, &aux1), ret); - case OADDR: - lhs(l, ret); - ret->type = *tp; - return ret; - case OFIELD: - return field(np, ret, 0); - case OBUILTIN: - switch (np->u.subop) { - case BVA_START: - l = rhs(l, &aux1); - code(ASVSTAR, NULL, l, NULL); - return NULL; - case BVA_END: - return NULL; - case BVA_ARG: - l = rhs(l, &aux1); - code(ASVARG, tmpnode(ret, tp), l, NULL); - return ret; - case BVA_COPY: - /* TODO */ - default: - abort(); - } - default: - abort(); - } - abort(); -} - -Node * -cgen(Node *np) -{ - Node aux, *p, *next; - - setlabel(np->label); - switch (np->op) { - case OJMP: - label2node(&aux, np->u.sym); - code(ASJMP, NULL, &aux, NULL); - break; - case OBRANCH: - next = np->next; - if (!next->label) - next->label = newlabel(); - bool(np->left, np->u.sym, next->label); - break; - case ORET: - p = (np->left) ? rhs(np->left, &aux) : NULL; - code(ASRET, NULL, p, NULL); - break; - case OBSWITCH: - p = rhs(np->left, &aux); - swtch_if(p); - break; - default: - rhs(np, &aux); - break; - } - return NULL; -} - -/* - * This is strongly influenced by - * http://plan9.bell-labs.com/sys/doc/compiler.ps (/sys/doc/compiler.ps) - * calculate addresability as follows - * AUTO => 11 value+fp - * REG => 11 reg - * STATIC => 11 (value) - * CONST => 11 $value - * These values of addressability are not used in the code generation. - * They are only used to calculate the Sethi-Ullman numbers. Since - * QBE is AMD64 targered we could do a better job there, and try to - * detect some of the complex addressing modes of these processors. - */ -Node * -sethi(Node *np) -{ - Node *lp, *rp; - - if (!np) - return np; - - np->complex = 0; - np->address = 0; - lp = np->left; - rp = np->right; - - switch (np->op) { - case OAUTO: - case OREG: - case OMEM: - case OCONST: - np->address = 11; - break; - case OCPL: - assert(np->type.flags & INTF); - np->op = OBXOR; - rp = constnode(NULL, ~(TUINT) 0, &np->type); - goto binary; - case OSNEG: - np->op = OSUB; - rp = lp; - lp = constnode(NULL, 0, &np->type); - if ((np->type.flags & INTF) == 0) - lp->u.f = 0.0; - default: - binary: - lp = sethi(lp); - rp = sethi(rp); - break; - } - np->left = lp; - np->right = rp; - - if (np->address > 10) - return np; - if (lp) - np->complex = lp->complex; - if (rp) { - int d = np->complex - rp->complex; - - if (d == 0) - ++np->complex; - else if (d < 0) - np->complex = rp->complex; - } - if (np->complex == 0) - ++np->complex; - return np; -} diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c @@ -1,569 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/qbe/code.c"; - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <cstd.h> -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -#define ADDR_LEN (INTIDENTSIZ+64) - -static void binary(void), unary(void), store(void), jmp(void), ret(void), - branch(void), call(void), ecall(void), param(void), - alloc(void), form2local(void), ldir(void), vastart(void), - vaarg(void); - -static struct opdata { - void (*fun)(void); - char *txt; - char letter; -} optbl [] = { - [ASLDSB] = {.fun = unary, .txt = "loadsb", .letter = 'w'}, - [ASLDUB] = {.fun = unary, .txt = "loadub", .letter = 'w'}, - [ASLDSH] = {.fun = unary, .txt = "loadsh", .letter = 'w'}, - [ASLDUH] = {.fun = unary, .txt = "loaduh", .letter = 'w'}, - [ASLDSW] = {.fun = unary, .txt = "loadsw", .letter = 'w'}, - [ASLDUW] = {.fun = unary, .txt = "loaduw", .letter = 'w'}, - [ASLDL] = {.fun = unary, .txt = "loadl", .letter = 'l'}, - [ASLDS] = {.fun = unary, .txt = "loads", .letter = 's'}, - [ASLDD] = {.fun = unary, .txt = "loadd", .letter = 'd'}, - - [ASCOPYB] = {.fun = unary, .txt = "copy", .letter = 'b'}, - [ASCOPYH] = {.fun = unary, .txt = "copy", .letter = 'h'}, - [ASCOPYW] = {.fun = unary, .txt = "copy", .letter = 'w'}, - [ASCOPYL] = {.fun = unary, .txt = "copy", .letter = 'l'}, - [ASCOPYS] = {.fun = unary, .txt = "copy", .letter = 's'}, - [ASCOPYD] = {.fun = unary, .txt = "copy", .letter = 'd'}, - - [ASSTB] = {.fun = store, .txt = "store", .letter = 'b'}, - [ASSTH] = {.fun = store, .txt = "store", .letter = 'h'}, - [ASSTW] = {.fun = store, .txt = "store", .letter = 'w'}, - [ASSTL] = {.fun = store, .txt = "store", .letter = 'l'}, - [ASSTM] = {.fun = ldir}, - [ASSTS] = {.fun = store, .txt = "store", .letter = 's'}, - [ASSTD] = {.fun = store, .txt = "store", .letter = 'd'}, - - [ASADDW] = {.fun = binary, .txt = "add", .letter = 'w'}, - [ASSUBW] = {.fun = binary, .txt = "sub", .letter = 'w'}, - [ASMULW] = {.fun = binary, .txt = "mul", .letter = 'w'}, - [ASMODW] = {.fun = binary, .txt = "rem", .letter = 'w'}, - [ASUMODW] = {.fun = binary, .txt = "urem", .letter = 'w'}, - [ASDIVW] = {.fun = binary, .txt = "div", .letter = 'w'}, - [ASUDIVW] = {.fun = binary, .txt = "udiv", .letter = 'w'}, - [ASSHLW] = {.fun = binary, .txt = "shl", .letter = 'w'}, - [ASSHRW] = {.fun = binary, .txt = "sar", .letter = 'w'}, - [ASUSHRW] = {.fun = binary, .txt = "shr", .letter = 'w'}, - [ASLTW] = {.fun = binary, .txt = "csltw", .letter = 'w'}, - [ASULTW] = {.fun = binary, .txt = "cultw", .letter = 'w'}, - [ASGTW] = {.fun = binary, .txt = "csgtw", .letter = 'w'}, - [ASUGTW] = {.fun = binary, .txt = "cugtw", .letter = 'w'}, - [ASLEW] = {.fun = binary, .txt = "cslew", .letter = 'w'}, - [ASULEW] = {.fun = binary, .txt = "culew", .letter = 'w'}, - [ASGEW] = {.fun = binary, .txt = "csgew", .letter = 'w'}, - [ASUGEW] = {.fun = binary, .txt = "cugew", .letter = 'w'}, - [ASEQW] = {.fun = binary, .txt = "ceqw", .letter = 'w'}, - [ASNEW] = {.fun = binary, .txt = "cnew", .letter = 'w'}, - [ASBANDW] = {.fun = binary, .txt = "and", .letter = 'w'}, - [ASBORW] = {.fun = binary, .txt = "or", .letter = 'w'}, - [ASBXORW] = {.fun = binary, .txt = "xor", .letter = 'w'}, - - [ASADDL] = {.fun = binary, .txt = "add", .letter = 'l'}, - [ASSUBL] = {.fun = binary, .txt = "sub", .letter = 'l'}, - [ASMULL] = {.fun = binary, .txt = "mul", .letter = 'l'}, - [ASMODL] = {.fun = binary, .txt = "rem", .letter = 'l'}, - [ASUMODL] = {.fun = binary, .txt = "urem", .letter = 'l'}, - [ASDIVL] = {.fun = binary, .txt = "div", .letter = 'l'}, - [ASUDIVL] = {.fun = binary, .txt = "udiv", .letter = 'l'}, - [ASSHLL] = {.fun = binary, .txt = "shl", .letter = 'l'}, - [ASSHRL] = {.fun = binary, .txt = "sar", .letter = 'l'}, - [ASUSHRL] = {.fun = binary, .txt = "shr", .letter = 'l'}, - [ASLTL] = {.fun = binary, .txt = "csltl", .letter = 'w'}, - [ASULTL] = {.fun = binary, .txt = "cultl", .letter = 'w'}, - [ASGTL] = {.fun = binary, .txt = "csgtl", .letter = 'w'}, - [ASUGTL] = {.fun = binary, .txt = "cugtl", .letter = 'w'}, - [ASLEL] = {.fun = binary, .txt = "cslel", .letter = 'w'}, - [ASULEL] = {.fun = binary, .txt = "culel", .letter = 'w'}, - [ASGEL] = {.fun = binary, .txt = "csgel", .letter = 'w'}, - [ASUGEL] = {.fun = binary, .txt = "cugel", .letter = 'w'}, - [ASEQL] = {.fun = binary, .txt = "ceql", .letter = 'w'}, - [ASNEL] = {.fun = binary, .txt = "cnel", .letter = 'w'}, - [ASBANDL] = {.fun = binary, .txt = "and", .letter = 'l'}, - [ASBORL] = {.fun = binary, .txt = "or", .letter = 'l'}, - [ASBXORL] = {.fun = binary, .txt = "xor", .letter = 'l'}, - - [ASADDS] = {.fun = binary, .txt = "add", .letter = 's'}, - [ASSUBS] = {.fun = binary, .txt = "sub", .letter = 's'}, - [ASMULS] = {.fun = binary, .txt = "mul", .letter = 's'}, - [ASDIVS] = {.fun = binary, .txt = "div", .letter = 's'}, - [ASLTS] = {.fun = binary, .txt = "clts", .letter = 'w'}, - [ASGTS] = {.fun = binary, .txt = "cgts", .letter = 'w'}, - [ASLES] = {.fun = binary, .txt = "cles", .letter = 'w'}, - [ASGES] = {.fun = binary, .txt = "cges", .letter = 'w'}, - [ASEQS] = {.fun = binary, .txt = "ceqs", .letter = 'w'}, - [ASNES] = {.fun = binary, .txt = "cnes", .letter = 'w'}, - - [ASADDD] = {.fun = binary, .txt = "add", .letter = 'd'}, - [ASSUBD] = {.fun = binary, .txt = "sub", .letter = 'd'}, - [ASMULD] = {.fun = binary, .txt = "mul", .letter = 'd'}, - [ASDIVD] = {.fun = binary, .txt = "div", .letter = 'd'}, - [ASLTD] = {.fun = binary, .txt = "cltd", .letter = 'w'}, - [ASGTD] = {.fun = binary, .txt = "cgtd", .letter = 'w'}, - [ASLED] = {.fun = binary, .txt = "cled", .letter = 'w'}, - [ASGED] = {.fun = binary, .txt = "cged", .letter = 'w'}, - [ASEQD] = {.fun = binary, .txt = "ceqd", .letter = 'w'}, - [ASNED] = {.fun = binary, .txt = "cned", .letter = 'w'}, - - [ASEXTBW] = {.fun = unary, .txt = "extsb", .letter = 'w'}, - [ASUEXTBW]= {.fun = unary, .txt = "extub", .letter = 'w'}, - [ASEXTBL] = {.fun = unary, .txt = "extsb", .letter = 'l'}, - [ASUEXTBL]= {.fun = unary, .txt = "extub", .letter = 'l'}, - [ASEXTHW] = {.fun = unary, .txt = "extsh", .letter = 'w'}, - [ASUEXTHW]= {.fun = unary, .txt = "extuh", .letter = 'w'}, - [ASEXTWL] = {.fun = unary, .txt = "extsw", .letter = 'l'}, - [ASUEXTWL]= {.fun = unary, .txt = "extuw", .letter = 'l'}, - - [ASSTOL] = {.fun = unary, .txt = "stosi", .letter = 'l'}, - [ASSTOW] = {.fun = unary, .txt = "stosi", .letter = 'w'}, - [ASDTOL] = {.fun = unary, .txt = "dtosi", .letter = 'l'}, - [ASDTOW] = {.fun = unary, .txt = "dtosi", .letter = 'w'}, - - [ASSWTOD] = {.fun = unary, .txt = "swtof", .letter = 'd'}, - [ASSWTOS] = {.fun = unary, .txt = "swtof", .letter = 's'}, - [ASSLTOD] = {.fun = unary, .txt = "sltof", .letter = 'd'}, - [ASSLTOS] = {.fun = unary, .txt = "sltof", .letter = 's'}, - - [ASEXTS] = {.fun = unary, .txt = "exts", .letter = 'd'}, - [ASTRUNCD] = {.fun = unary, .txt = "truncd", .letter = 's'}, - - [ASBRANCH] = {.fun = branch}, - [ASJMP] = {.fun = jmp}, - [ASRET] = {.fun = ret}, - [ASCALL] = {.fun = call}, - [ASCALLE] = {.fun = ecall, .txt = ")"}, - [ASCALLEX] = {.fun = ecall, .txt = ", ...)"}, - [ASPAR] = {.fun = param, .txt = "%s %s, "}, - [ASPARE] = {.fun = param, .txt = "%s %s"}, - [ASALLOC] = {.fun = alloc}, - [ASFORM] = {.fun = form2local}, - - [ASVSTAR] = {.fun = vastart}, - [ASVARG] = {.fun = vaarg}, -}; - -static char buff[ADDR_LEN]; -/* - * : is for user-defined Aggregate Types - * $ is for globals (represented by a pointer) - * % is for function-scope temporaries - * @ is for block labels - */ -static char -sigil(Symbol *sym) -{ - switch (sym->kind) { - case SEXTRN: - case SGLOB: - case SPRIV: - case SLOCAL: - return '$'; - case SAUTO: - case STMP: - return '%'; - case SLABEL: - return '@'; - default: - abort(); - } -} - -static char * -symname(Symbol *sym) -{ - char c = sigil(sym); - - if (sym->name) { - switch (sym->kind) { - case SEXTRN: - case SGLOB: - sprintf(buff, "%c%s", c, sym->name); - return buff; - case SLOCAL: - case SPRIV: - case SAUTO: - sprintf(buff, "%c%s.%u", c, sym->name, sym->id); - return buff; - default: - abort(); - } - } - sprintf(buff, "%c.%u", c, sym->numid); - - return buff; -} - -static void -emitconst(Node *np) -{ - switch (np->type.size) { - case 1: - printf("%d", (int) np->u.i & 0xFF); - break; - case 2: - printf("%d", (int) np->u.i & 0xFFFF); - break; - case 4: - printf("%ld", (long) np->u.i & 0xFFFFFFFF); - break; - case 8: - printf("%lld", (long long) np->u.i); - break; - default: - abort(); - } -} - -static void -emittree(Node *np) -{ - if (!np) - return; - - switch (np->op) { - case OSTRING: - printf("\"%s\"", np->u.s); - free(np->u.s); - np->u.s = NULL; - break; - case OCONST: - emitconst(np); - break; - case OADDR: - emittree(np->left); - break; - case OMEM: - fputs(symname(np->u.sym), stdout); - break; - default: - emittree(np->left); - printf(" %c ", np->op); - emittree(np->right); - break; - } -} - -static char * -size2asm(Type *tp) -{ - if (tp->flags & STRF) { - return "b"; - } else if (tp->flags & INTF) { - switch (tp->size) { - case 1: - return "b"; - case 2: - return "h"; - case 4: - return "w"; - case 8: - return "l"; - } - } else if (tp->flags & FLOATF) { - if (tp->size == 4) - return "s"; - else if (tp->size == 8) - return "d"; - } - abort(); -} - -void -defglobal(Symbol *sym) -{ - if (sym->kind == SEXTRN) - return; - if (sym->kind == SGLOB) - fputs("export ", stdout); - printf("data %s = {\n", symname(sym)); - if (sym->type.flags & INITF) - return; - printf("\tz\t%lu\n}\n", sym->type.size); -} - -void -defpar(Symbol *sym) -{ - sym->type.flags |= PARF; -} - -void -defvar(Symbol *sym) -{ - if (sym->kind == SREG) - sym->kind = SAUTO; -} - -void -data(Node *np) -{ - printf("\t%s\t", size2asm(&np->type)); - emittree(np); - putchar(','); - putchar('\n'); -} - -static char * -size2stack(Type *tp) -{ - if (tp->flags & INTF) { - switch (tp->size) { - case 1: - case 2: - case 4: - return "w"; - case 8: - return "l"; - } - } else if (tp->flags & FLOATF) { - if (tp->size == 4) - return "s"; - else if (tp->size == 8) - return "d"; - } else if (tp->size == 0) { - return "w"; - } - abort(); -} - -void -writeout(void) -{ - Symbol *p; - Type *tp; - char *sep, *name; - int haslabel = 0; - - if (!curfun) - return; - if (curfun->kind == SGLOB) - fputs("export ", stdout); - printf("function %s %s(", size2stack(&curfun->rtype), symname(curfun)); - - /* declare formal parameters */ - for (sep = "", p = locals; p; p = p->next, sep = ",") { - if ((p->type.flags & PARF) == 0) - break; - printf("%s%s %s.val", sep, size2stack(&p->type), symname(p)); - } - printf("%s)\n{\n", (curfun->type.flags&ELLIPS) ? ", ..." : ""); - - /* emit assembler instructions */ - for (pc = prog; pc; pc = pc->next) { - if (pc->label) { - haslabel = 1; - printf("%s\n", symname(pc->label)); - } - if (!pc->op) - continue; - if (pc->flags&BBENTRY && !haslabel) - printf("%s\n", symname(newlabel())); - (*optbl[pc->op].fun)(); - if (!pc->label) - haslabel = 0; - } - - puts("}"); -} - -static char * -addr2txt(Addr *a) -{ - switch (a->kind) { - case SCONST: - sprintf(buff, "%llu", (unsigned long long) a->u.i); - return buff; - case SAUTO: - case SLABEL: - case STMP: - case SGLOB: - case SEXTRN: - case SPRIV: - case SLOCAL: - return symname(a->u.sym); - default: - abort(); - } -} - -static void -binary(void) -{ - struct opdata *p = &optbl[pc->op]; - char to[ADDR_LEN], from1[ADDR_LEN], from2[ADDR_LEN]; - - strcpy(to, addr2txt(&pc->to)); - strcpy(from1, addr2txt(&pc->from1)); - strcpy(from2, addr2txt(&pc->from2)); - printf("\t%s =%c\t%s\t%s,%s\n", to, p->letter, p->txt, from1, from2); -} - -static void -ldir(void) -{ - struct opdata *p = &optbl[pc->op]; - char to[ADDR_LEN], from[ADDR_LEN]; - /* TODO: what type do we use for the size? */ - - /* TODO: it is pending */ -} - -static void -store(void) -{ - struct opdata *p = &optbl[pc->op]; - char to[ADDR_LEN], from[ADDR_LEN]; - - strcpy(to, addr2txt(&pc->to)); - strcpy(from, addr2txt(&pc->from1)); - printf("\t\t%s%c\t%s,%s\n", p->txt, p->letter, from, to); -} - -static void -unary(void) -{ - struct opdata *p = &optbl[pc->op]; - char to[ADDR_LEN], from[ADDR_LEN]; - - strcpy(to, addr2txt(&pc->to)); - strcpy(from, addr2txt(&pc->from1)); - printf("\t%s =%c\t%s\t%s\n", to, p->letter, p->txt, from); -} - -static void -call(void) -{ - struct opdata *p = &optbl[pc->op]; - char to[ADDR_LEN], from[ADDR_LEN]; - Symbol *sym = pc->to.u.sym; - - strcpy(to, addr2txt(&pc->to)); - strcpy(from, addr2txt(&pc->from1)); - printf("\t%s =%s\tcall\t%s(", - to, size2stack(&sym->type), from); -} - -static void -param(void) -{ - Symbol *sym = pc->from2.u.sym; - - printf(optbl[pc->op].txt, - size2stack(&sym->type), addr2txt(&pc->from1)); -} - -static void -ecall(void) -{ - struct opdata *p = &optbl[pc->op]; - - puts(p->txt); -} - -static void -ret(void) -{ - if (pc->from1.kind == SNONE) - puts("\t\tret"); - else - printf("\t\tret\t%s\n", addr2txt(&pc->from1)); -} - -static void -jmp(void) -{ - printf("\t\tjmp\t%s\n", addr2txt(&pc->from1)); -} - -static void -branch(void) -{ - char to[ADDR_LEN], from1[ADDR_LEN], from2[ADDR_LEN]; - - strcpy(to, addr2txt(&pc->to)); - strcpy(from1, addr2txt(&pc->from1)); - strcpy(from2, addr2txt(&pc->from2)); - printf("\t\tjnz\t%s,%s,%s\n", to, from1, from2); -} - -static void -vastart(void) -{ - printf("\t\tvastart %s\n", addr2txt(&pc->from1)); -} - -static void -vaarg(void) -{ - Symbol *sym = pc->to.u.sym; - Type *tp = &sym->type; - char to[ADDR_LEN], from[ADDR_LEN]; - - strcpy(to, addr2txt(&pc->to)); - strcpy(from, addr2txt(&pc->from1)); - printf("\t\t%s =%s vaarg %s\n", to, size2asm(tp), from); -} - -static void -alloc(void) -{ - Symbol *sym = pc->to.u.sym; - Type *tp = &sym->type; - extern Type ptrtype; - - printf("\t%s =%s\talloc%lu\t%lu\n", - symname(sym), size2asm(&ptrtype), tp->align+3 & ~3, tp->size); -} - -static void -form2local(void) -{ - Symbol *sym = pc->to.u.sym; - Type *tp = &sym->type; - char *name = symname(sym); - - printf("\t\tstore%s\t%s.val,%s\n", size2asm(tp), name, name); -} - -void -endinit(void) -{ - puts("}"); -} - -void -getbblocks(void) -{ - Inst *i; - - if (!prog) - return; - - prog->flags |= BBENTRY; - for (pc = prog; pc; pc = pc->next) { - switch (pc->op) { - case ASBRANCH: - i = pc->from2.u.sym->u.inst; - i->flags |= BBENTRY; - case ASJMP: - i = pc->from1.u.sym->u.inst; - i->flags |= BBENTRY; - case ASRET: - if (pc->next) - pc->next->flags |= BBENTRY; - break; - } - } -} diff --git a/cc2/arch/qbe/optm.c b/cc2/arch/qbe/optm.c @@ -1,58 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/qbe/optm.c"; - -#include <stddef.h> - -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -optm_dep(Node *np) -{ - int op = np->op; - Node *p, *dst, *next = np->next; - Symbol *sym, *osym; - - switch (op) { - case OEFUN: - /* - * In QBE we need at the end of a basic block - * a jump, so we have to ensure that the last - * statement of the function is a ret, a jmp - * or a branch. In the same way, QBE does - * not accept labels at the end of a function - * (ONOP is used for labels) so we have to add - * a ret there, and in the case of branches - * we need a label for the next statement - */ - op = (np->prev) ? np->prev->op : 0; - if (!op || op == ONOP || op == OBRANCH || (op != ORET && op != OJMP)) - addstmt(newnode(ORET), KEEPCUR); - break; - case OBRANCH: - if (!next->label) { - sym = getsym(TMPSYM); - sym->kind = SLABEL; - next->label = sym; - } - case OJMP: - for (;;) { - dst = np->u.sym->u.stmt; - if (dst->op != OJMP) - break; - np->u.sym = dst->u.sym; - } - for (p = np->next; p; p = p->next) { - if (p == dst) - return NULL; - if (p->op == ONOP || - p->op == OBLOOP || - p->op == OELOOP) { - continue; - } - break; - } - break; - } - return np; -} diff --git a/cc2/arch/qbe/types.c b/cc2/arch/qbe/types.c @@ -1,94 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/qbe/types.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - - -Type int8type = { - .flags = SIGNF | INTF, - .size = 1, - .align = 1 -}; - -Type int16type = { - .flags = SIGNF | INTF, - .size = 2, - .align = 2 -}; - -Type int32type = { - .flags = SIGNF | INTF, - .size = 4, - .align = 4 -}; - -Type int64type = { - .flags = SIGNF | INTF, - .size = 8, - .align = 8 -}; - -Type uint8type = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type uint16type = { - .flags = INTF, - .size = 2, - .align = 2 -}; - -Type uint32type = { - .flags = INTF, - .size = 4, - .align = 4 -}; - -Type uint64type = { - .flags = INTF, - .size = 8, - .align = 2 -}; - -Type ptrtype = { - .flags = INTF, - .size = 8, - .align = 8 -}; - -Type booltype = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type float32type = { - .flags = FLOATF, - .size = 4, - .align = 4 -}; - -Type float64type = { - .flags = FLOATF, - .size = 8, - .align = 8 -}; - -Type float80type = { - .flags = FLOATF, - .size = 16, - .align = 16 -}; - -Type voidtype = { - .size = 0, - .align = 0 -}; - -Type arg_type = { - .size = 24, - .align = 8 -}; diff --git a/cc2/arch/z80/arch.h b/cc2/arch/z80/arch.h @@ -1 +0,0 @@ -/* See LICENSE file for copyright and license details. */ diff --git a/cc2/arch/z80/cgen.c b/cc2/arch/z80/cgen.c @@ -1,68 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/z80/cgen.c"; - -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -cgen(Node *np) -{ -} - -/* - * This is strongly influenced by - * http://plan9.bell-labs.com/sys/doc/compiler.ps (/sys/doc/compiler.ps) - * calculate addresability as follows - * AUTO => 11 value+fp - * REG => 13 reg - * STATIC => 12 (value) - * CONST => 20 $value - */ -Node * -sethi(Node *np) -{ - Node *lp, *rp; - - if (!np) - return np; - - np->complex = 0; - np->address = 0; - lp = np->left; - rp = np->right; - switch (np->op) { - case OAUTO: - np->address = 11; - break; - case OREG: - np->address = 13; - break; - case OMEM: - np->address = 12; - break; - case OCONST: - np->address = 20; - break; - default: - sethi(lp); - sethi(rp); - break; - } - - if (np->address > 10) - return np; - if (lp) - np->complex = lp->complex; - if (rp) { - int d = np->complex - rp->complex; - - if (d == 0) - ++np->complex; - else if (d < 0) - np->complex = rp->complex; - } - if (np->complex == 0) - ++np->complex; - return np; -} diff --git a/cc2/arch/z80/code.c b/cc2/arch/z80/code.c @@ -1,229 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/z80/code.c"; - -#include <stdio.h> -#include <stdlib.h> - -#include <cstd.h> -#include "arch.h" -#include "../../../inc/cc.h" -#include "../../cc2.h" - -enum segment { - CODESEG, - DATASEG, - BSSSEG, - NOSEG -}; - -static int curseg = NOSEG; -static unsigned long offpar, offvar; - -static void -segment(int seg) -{ - static char *txt[] = { - [CODESEG] = "\tCSEG\n", - [DATASEG] = "\tDSEG\n", - [BSSSEG] = "\tASEG\n", - }; - - if (seg == curseg) - return; - fputs(txt[seg], stdout); - curseg = seg; -} - -static char * -symname(Symbol *sym) -{ - static char name[INTIDENTSIZ+1]; - - if (sym->name) { - switch (sym->kind) { - case SGLOB: - case SEXTRN: - snprintf(name, sizeof(name), "_%s", sym->name); - return name; - case SPRIV: - return sym->name; - } - } - - sprintf(name, ".%d", sym->numid); - - return name; -} - -static void -label(Symbol *sym) -{ - int seg; - char *name = symname(sym); - - if (sym->type.flags & FUNF) - seg = CODESEG; - else if (sym->type.flags & INITF) - seg = DATASEG; - else - seg = BSSSEG; - segment(seg); - - switch (sym->kind) { - case SEXTRN: - printf("\tEXTRN\t%s\n", name); - return; - case SGLOB: - printf("\tPUBLIC\t%s\n", name); - break; - } - - printf("%s:\n", name); -} - -static void -emitconst(Node *np) -{ - switch (np->type.size) { - case 1: - printf("%d", (int) np->u.i & 0xFF); - break; - case 2: - printf("%d", (int) np->u.i & 0xFFFF); - break; - case 4: - printf("%ld", (long) np->u.i & 0xFFFFFFFF); - break; - default: - abort(); - } -} - -static void -emittree(Node *np) -{ - if (!np) - return; - - switch (np->op) { - case OSTRING: - printf("\"%s\"", np->u.s); - free(np->u.s); - np->u.s = NULL; - break; - case OCONST: - emitconst(np); - break; - case OADDR: - emittree(np->left); - break; - case OMEM: - fputs(symname(np->u.sym), stdout); - break; - default: - emittree(np->left); - printf(" %c ", np->op); - emittree(np->right); - break; - } -} - -static void -size2asm(Type *tp) -{ - char *s; - - /* - * In z80 we can ignore the alignment - */ - if (tp->flags & STRF) { - s = "\tDB\t"; - } else { - switch (tp->size) { - case 1: - s = "\tDB\t"; - break; - case 2: - s = "\tDW\t"; - break; - case 4: - s = "\tDD\t"; - break; - default: - s = "\tDS\t%lu,"; - break; - } - } - printf(s, tp->size); -} - -void -newfun() -{ - offpar = offvar = 0; -} - -void -defpar(Symbol *sym) -{ - unsigned long align, size; - - if (sym->kind != SREG && sym->kind != SAUTO) - return; - align = sym->type.align; - size = sym->type.size; - - offpar -= align-1 & ~align; - sym->u.off = offpar; - offpar -= size; - sym->kind = SAUTO; -} - -void -defvar(Symbol *sym) -{ - unsigned long align, size; - - if (sym->kind != SREG && sym->kind != SAUTO) - return; - align = sym->type.align; - size = sym->type.size; - - offvar += align-1 & ~align; - sym->u.off = offvar; - offvar += size; - sym->kind = SAUTO; -} - -void -defglobal(Symbol *sym) -{ - label(sym); - if (sym->kind == SEXTRN || (sym->type.flags & INITF)) - return; - size2asm(&sym->type); - puts("0"); -} - -void -data(Node *np) -{ - size2asm(&np->type); - emittree(np); - putchar('\n'); -} - -void -writeout(void) -{ -} - -void -endinit(void) -{ -} - -void -getbblocks(void) -{ -} diff --git a/cc2/arch/z80/optm.c b/cc2/arch/z80/optm.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/z80/optm.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - -Node * -optm_dep(Node *np) -{ - return np; -} diff --git a/cc2/arch/z80/types.c b/cc2/arch/z80/types.c @@ -1,95 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./cc2/arch/z80/types.c"; - -#include "../../../inc/cc.h" -#include "../../cc2.h" - - -Type int8type = { - .flags = SIGNF | INTF, - .size = 1, - .align = 1 -}; - -Type int16type = { - .flags = SIGNF | INTF, - .size = 2, - .align = 1 -}; - -Type int32type = { - .flags = SIGNF | INTF, - .size = 4, - .align = 1 -}; - -Type int64type = { - .flags = SIGNF | INTF, - .size = 8, - .align = 1 -}; - -Type uint8type = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type uint16type = { - .flags = INTF, - .size = 2, - .align = 1 -}; - -Type uint32type = { - .flags = INTF, - .size = 4, - .align = 1 -}; - -Type uint64type = { - .flags = INTF, - .size = 8, - .align = 1 -}; - -Type ptrtype = { - .flags = INTF, - .size = 2, - .align = 1 -}; - -Type booltype = { - .flags = INTF, - .size = 1, - .align = 1 -}; - -Type float32type = { - .flags = FLOATF, - .size = 4, - .align = 1 -}; - -Type float64type = { - .flags = FLOATF, - .size = 4, - .align = 1 -}; - -Type float80type = { - .flags = FLOATF, - .size = 4, - .align = 1 -}; - -Type voidtype = { - .size = 0, - .align = 0 -}; - -/* this types is not going to be used in this arch */ -Type arg_type = { - .size = 0, - .align = 0 -}; diff --git a/cc2/gendep.sh b/cc2/gendep.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +gentarget() +{ + + cat <<EOF +include target/$1/target.mk +\$(OBJ-$1): \$(HDR) +all: cc2-$1 +cc2-$1: \$(OBJ-$1) \$(LIBDIR)/libcc.a + \$(CC) \$(SCC_LDFLAGS) \$(OBJ-$1) -lcc -o \$@ + +EOF +} + +set -e + +rm -f makefile +trap "rm -f $$.mk" 0 2 3 + +re='\([^-]*\)-\([^-]*\)-\([^-]*\)-\([^-]*\)' + +echo $@ | +tr ' ' '\n' | +sed "s/$re/\1-\2/" | +sort -u | +(cat Makefile +while read i +do + if test ${MKQBE-0} -eq 1 + then + case $i in + amd64-sysv) + gentarget qbe_$i + ;; + esac + fi + gentarget $i +done) > $$.mk && mv $$.mk makefile diff --git a/cc2/target/amd64-sysv/arch.h b/cc2/target/amd64-sysv/arch.h @@ -0,0 +1 @@ +/* See LICENSE file for copyright and license details. */ diff --git a/cc2/target/amd64-sysv/cgen.c b/cc2/target/amd64-sysv/cgen.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/cgen.c"; + +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +cgen(Node *np) +{ +} + +Node * +sethi(Node *np) +{ +} diff --git a/cc2/target/amd64-sysv/code.c b/cc2/target/amd64-sysv/code.c @@ -0,0 +1,211 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/code.c"; + +#include <stdio.h> +#include <stdlib.h> + +#include <cstd.h> +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +enum segment { + CODESEG, + DATASEG, + BSSSEG, + NOSEG +}; + +static int curseg = NOSEG; + +static void +segment(int seg) +{ + static char *txt[] = { + [CODESEG] = "\t.text\n", + [DATASEG] = "\t.data\n", + [BSSSEG] = "\t.bss\n", + }; + + if (seg == curseg) + return; + fputs(txt[seg], stdout); + curseg = seg; +} + +static char * +symname(Symbol *sym) +{ + static char name[INTIDENTSIZ+1]; + + if (sym->name) { + switch (sym->kind) { + case SEXTRN: + case SGLOB: + case SPRIV: + return sym->name; + } + } + + sprintf(name, ".L%d", sym->numid); + + return name; +} + +static void +emitconst(Node *np) +{ + switch (np->type.size) { + case 1: + printf("%d", (int) np->u.i & 0xFF); + break; + case 2: + printf("%d", (int) np->u.i & 0xFFFF); + break; + case 4: + printf("%ld", (long) np->u.i & 0xFFFFFFFF); + break; + case 8: + printf("%lld", (long long) np->u.i & 0xFFFFFFFF); + break; + default: + abort(); + } +} + +static void +emittree(Node *np) +{ + if (!np) + return; + + switch (np->op) { + case OSTRING: + printf("\"%s\"", np->u.s); + free(np->u.s); + np->u.s = NULL; + break; + case OCONST: + emitconst(np); + break; + case OADDR: + emittree(np->left); + break; + case OMEM: + fputs(symname(np->u.sym), stdout); + break; + default: + emittree(np->left); + printf(" %c ", np->op); + emittree(np->right); + break; + } +} +static void +size2asm(Type *tp) +{ + char *s; + + if (tp->flags & STRF) { + s = "\t.ascii\t"; + } else { + switch (tp->size) { + case 1: + s = "\t.byte\t"; + break; + case 2: + s = "\t.short\t"; + break; + case 4: + s = "\t.long\t"; + break; + case 8: + s = "\t.quad\t"; + break; + default: + s = "\t.space\t%lu,"; + break; + } + } + printf(s, tp->size); +} + + +void +data(Node *np) +{ + size2asm(&np->type); + emittree(np); + putchar('\n'); +} + +static void +label(Symbol *sym) +{ + int seg; + char *name = symname(sym); + Type *tp = &sym->type; + + if (sym->type.flags & FUNF) + seg = CODESEG; + else if (sym->type.flags & INITF) + seg = DATASEG; + else + seg = BSSSEG; + segment(seg); + + switch (sym->kind) { + case SEXTRN: + printf("\t.extern\t%s\n", name); + case SLOCAL: + return; + case SGLOB: + printf("\t.global\t%s\n", name); + if (seg == BSSSEG) + printf("\t.comm\t%s,%lu\n", name, tp->size); + break; + } + if (sym->type.align != 1) + printf("\t.align\t%lu\n", sym->type.align ); + printf("%s:\n", name); +} + +void +defglobal(Symbol *sym) +{ + label(sym); + if (sym->kind == SEXTRN || (sym->type.flags & INITF)) + return; + size2asm(&sym->type); + puts("0"); +} + +void +defvar(Symbol *sym) +{ +} + +void +defpar(Symbol *sym) +{ +} + +void +newfun(void) +{ +} + +void +writeout(void) +{ +} + +void +endinit(void) +{ +} + +void +getbblocks(void) +{ +} diff --git a/cc2/target/amd64-sysv/optm.c b/cc2/target/amd64-sysv/optm.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/optm.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +optm_dep(Node *np) +{ + return np; +} diff --git a/cc2/target/amd64-sysv/target.mk b/cc2/target/amd64-sysv/target.mk @@ -0,0 +1,6 @@ + +OBJ-amd64-sysv = $(OBJ) \ + target/amd64-sysv/cgen.o \ + target/amd64-sysv/optm.o \ + target/amd64-sysv/code.o \ + target/amd64-sysv/types.o diff --git a/cc2/target/amd64-sysv/types.c b/cc2/target/amd64-sysv/types.c @@ -0,0 +1,94 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/amd64-sysv/types.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + + +Type int8type = { + .flags = SIGNF | INTF, + .size = 1, + .align = 1 +}; + +Type int16type = { + .flags = SIGNF | INTF, + .size = 2, + .align = 2 +}; + +Type int32type = { + .flags = SIGNF | INTF, + .size = 4, + .align = 4 +}; + +Type int64type = { + .flags = SIGNF | INTF, + .size = 8, + .align = 8 +}; + +Type uint8type = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type uint16type = { + .flags = INTF, + .size = 2, + .align = 2 +}; + +Type uint32type = { + .flags = INTF, + .size = 4, + .align = 4 +}; + +Type uint64type = { + .flags = INTF, + .size = 8, + .align = 2 +}; + +Type ptrtype = { + .flags = INTF, + .size = 8, + .align = 8 +}; + +Type booltype = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type float32type = { + .flags = FLOATF, + .size = 4, + .align = 4 +}; + +Type float64type = { + .flags = FLOATF, + .size = 8, + .align = 8 +}; + +Type float80type = { + .flags = FLOATF, + .size = 16, + .align = 16 +}; + +Type voidtype = { + .size = 0, + .align = 0 +}; + +Type arg_type = { + .size = 24, + .align = 8 +}; diff --git a/cc2/target/i386-sysv/arch.h b/cc2/target/i386-sysv/arch.h @@ -0,0 +1 @@ +/* See LICENSE file for copyright and license details. */ diff --git a/cc2/target/i386-sysv/cgen.c b/cc2/target/i386-sysv/cgen.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/cgen.c"; + +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +cgen(Node *np) +{ +} + +Node * +sethi(Node *np) +{ +} diff --git a/cc2/target/i386-sysv/code.c b/cc2/target/i386-sysv/code.c @@ -0,0 +1,209 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/code.c"; +#include <stdio.h> +#include <stdlib.h> + +#include <cstd.h> +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +enum segment { + CODESEG, + DATASEG, + BSSSEG, + NOSEG +}; + +static int curseg = NOSEG; + +static void +segment(int seg) +{ + static char *txt[] = { + [CODESEG] = "\t.text\n", + [DATASEG] = "\t.data\n", + [BSSSEG] = "\t.bss\n", + }; + + if (seg == curseg) + return; + fputs(txt[seg], stdout); + curseg = seg; +} + +static char * +symname(Symbol *sym) +{ + static char name[INTIDENTSIZ+1]; + + if (sym->name) { + switch (sym->kind) { + case SEXTRN: + case SGLOB: + case SPRIV: + return sym->name; + } + } + + sprintf(name, ".L%d", sym->numid); + + return name; +} + +static void +emitconst(Node *np) +{ + switch (np->type.size) { + case 1: + printf("%d", (int) np->u.i & 0xFF); + break; + case 2: + printf("%d", (int) np->u.i & 0xFFFF); + break; + case 4: + printf("%ld", (long) np->u.i & 0xFFFFFFFF); + break; + case 8: + printf("%lld", (long long) np->u.i & 0xFFFFFFFF); + break; + default: + abort(); + } +} + +static void +emittree(Node *np) +{ + if (!np) + return; + + switch (np->op) { + case OSTRING: + printf("\"%s\"", np->u.s); + free(np->u.s); + np->u.s = NULL; + break; + case OCONST: + emitconst(np); + break; + case OADDR: + emittree(np->left); + break; + case OMEM: + fputs(symname(np->u.sym), stdout); + break; + default: + emittree(np->left); + printf(" %c ", np->op); + emittree(np->right); + break; + } +} +static void +size2asm(Type *tp) +{ + char *s; + + if (tp->flags & STRF) { + s = "\t.ascii\t"; + } else { + switch (tp->size) { + case 1: + s = "\t.byte\t"; + break; + case 2: + s = "\t.short\t"; + break; + case 4: + s = "\t.long\t"; + break; + case 8: + s = "\t.quad\t"; + break; + default: + s = "\t.space\t%lu,"; + break; + } + } + printf(s, tp->size); +} + +void +data(Node *np) +{ + size2asm(&np->type); + emittree(np); + putchar('\n'); +} + +static void +label(Symbol *sym) +{ + int seg; + char *name = symname(sym); + Type *tp = &sym->type; + + if (sym->type.flags & FUNF) + seg = CODESEG; + else if (sym->type.flags & INITF) + seg = DATASEG; + else + seg = BSSSEG; + segment(seg); + + switch (sym->kind) { + case SEXTRN: + printf("\t.extern\t%s\n", name); + case SLOCAL: + return; + case SGLOB: + printf("\t.global\t%s\n", name); + if (seg == BSSSEG) + printf("\t.comm\t%s,%lu\n", name, tp->size); + break; + } + if (sym->type.align != 1) + printf("\t.align\t%lu\n", sym->type.align ); + printf("%s:\n", name); +} + +void +defglobal(Symbol *sym) +{ + label(sym); + if (sym->kind == SEXTRN || (sym->type.flags & INITF)) + return; + size2asm(&sym->type); + puts("0"); +} + +void +defpar(Symbol *sym) +{ +} + +void +defvar(Symbol *sym) +{ +} + +void +newfun(void) +{ +} + +void +writeout(void) +{ +} + +void +endinit(void) +{ +} + +void +getbblocks(void) +{ +} diff --git a/cc2/target/i386-sysv/optm.c b/cc2/target/i386-sysv/optm.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/optm.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +optm_dep(Node *np) +{ + return np; +} diff --git a/cc2/target/i386-sysv/target.mk b/cc2/target/i386-sysv/target.mk @@ -0,0 +1,6 @@ + +OBJ-i386-sysv = $(OBJ) \ + target/i386-sysv/cgen.o \ + target/i386-sysv/optm.o \ + target/i386-sysv/code.o \ + target/i386-sysv/types.o diff --git a/cc2/target/i386-sysv/types.c b/cc2/target/i386-sysv/types.c @@ -0,0 +1,95 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/i386-sysv/types.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + + +Type int8type = { + .flags = SIGNF | INTF, + .size = 1, + .align = 1 +}; + +Type int16type = { + .flags = SIGNF | INTF, + .size = 2, + .align = 2 +}; + +Type int32type = { + .flags = SIGNF | INTF, + .size = 4, + .align = 4 +}; + +Type int64type = { + .flags = SIGNF | INTF, + .size = 8, + .align = 4 +}; + +Type uint8type = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type uint16type = { + .flags = INTF, + .size = 2, + .align = 2 +}; + +Type uint32type = { + .flags = INTF, + .size = 4, + .align = 2 +}; + +Type uint64type = { + .flags = INTF, + .size = 8, + .align = 4 +}; + +Type ptrtype = { + .flags = INTF, + .size = 4, + .align = 4 +}; + +Type booltype = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type float32type = { + .flags = FLOATF, + .size = 4, + .align = 4 +}; + +Type float64type = { + .flags = FLOATF, + .size = 8, + .align = 4 +}; + +Type float80type = { + .flags = FLOATF, + .size = 12, + .align = 4 +}; + +Type voidtype = { + .size = 0, + .align = 0 +}; + +/* this type is not used in this architecture */ +Type arg_type = { + .size = 0, + .align = 0 +}; diff --git a/cc2/target/qbe_amd64-sysv/arch.h b/cc2/target/qbe_amd64-sysv/arch.h @@ -0,0 +1,137 @@ +/* See LICENSE file for copyright and license details. */ + +enum asmop { + ASNOP = 0, + ASSTB, + ASSTH, + ASSTW, + ASSTL, + ASSTM, + ASSTS, + ASSTD, + + ASLDSB, + ASLDUB, + ASLDSH, + ASLDUH, + ASLDSW, + ASLDUW, + ASLDL, + ASLDS, + ASLDD, + + ASADDW, + ASSUBW, + ASMULW, + ASMODW, + ASUMODW, + ASDIVW, + ASUDIVW, + ASSHLW, + ASSHRW, + ASUSHRW, + ASLTW, + ASULTW, + ASGTW, + ASUGTW, + ASLEW, + ASULEW, + ASGEW, + ASUGEW, + ASEQW, + ASNEW, + ASBANDW, + ASBORW, + ASBXORW, + + ASADDL, + ASSUBL, + ASMULL, + ASMODL, + ASUMODL, + ASDIVL, + ASUDIVL, + ASSHLL, + ASSHRL, + ASUSHRL, + ASLTL, + ASULTL, + ASGTL, + ASUGTL, + ASLEL, + ASULEL, + ASGEL, + ASUGEL, + ASEQL, + ASNEL, + ASBANDL, + ASBORL, + ASBXORL, + + ASADDS, + ASSUBS, + ASMULS, + ASDIVS, + ASLTS, + ASGTS, + ASLES, + ASGES, + ASEQS, + ASNES, + + ASADDD, + ASSUBD, + ASMULD, + ASDIVD, + ASLTD, + ASGTD, + ASLED, + ASGED, + ASEQD, + ASNED, + + ASEXTBW, + ASUEXTBW, + ASEXTBL, + ASUEXTBL, + ASEXTHW, + ASUEXTHW, + ASEXTHL, + ASUEXTHL, + ASEXTWL, + ASUEXTWL, + + ASSTOL, + ASSTOW, + ASDTOL, + ASDTOW, + + ASSWTOD, + ASSWTOS, + ASSLTOD, + ASSLTOS, + + ASEXTS, + ASTRUNCD, + + ASJMP, + ASBRANCH, + ASRET, + ASCALL, + ASCALLE, + ASCALLEX, + ASPAR, + ASPARE, + ASALLOC, + ASFORM, + + ASCOPYB, + ASCOPYH, + ASCOPYW, + ASCOPYL, + ASCOPYS, + ASCOPYD, + + ASVSTAR, + ASVARG, +}; diff --git a/cc2/target/qbe_amd64-sysv/cgen.c b/cc2/target/qbe_amd64-sysv/cgen.c @@ -0,0 +1,731 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/qbe/cgen.c"; + +#include <assert.h> +#include <stdlib.h> + +#include <cstd.h> +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +enum sflags { + ISTMP = 1, + ISCONS = 2 +}; + +static char opasmw[] = { + [OADD] = ASADDW, + [OSUB] = ASSUBW, + [OMUL] = ASMULW, + [OMOD] = ASMODW, + [ODIV] = ASDIVW, + [OSHL] = ASSHLW, + [OSHR] = ASSHRW, + [OLT] = ASLTW, + [OGT] = ASGTW, + [OLE] = ASLEW, + [OGE] = ASGEW, + [OEQ] = ASEQW, + [ONE] = ASNEW, + [OBAND] = ASBANDW, + [OBOR] = ASBORW, + [OBXOR] = ASBXORW, +}; + +static char opasml[] = { + [OADD] = ASADDL, + [OSUB] = ASSUBL, + [OMUL] = ASMULL, + [OMOD] = ASMODL, + [ODIV] = ASDIVL, + [OSHL] = ASSHLL, + [OSHR] = ASSHRL, + [OLT] = ASLTL, + [OGT] = ASGTL, + [OLE] = ASLEL, + [OGE] = ASGEL, + [OEQ] = ASEQL, + [ONE] = ASNEL, + [OBAND] = ASBANDL, + [OBOR] = ASBORL, + [OBXOR] = ASBXORL, +}; + +static char opasms[] = { + [OADD] = ASADDS, + [OSUB] = ASSUBS, + [OMUL] = ASMULS, + [ODIV] = ASDIVS, + [OLT] = ASLTS, + [OGT] = ASGTS, + [OLE] = ASLES, + [OGE] = ASGES, + [OEQ] = ASEQS, + [ONE] = ASNES, +}; +static char opasmd[] = { + [OADD] = ASADDD, + [OSUB] = ASSUBD, + [OMUL] = ASMULD, + [ODIV] = ASDIVD, + [OLT] = ASLTD, + [OGT] = ASGTD, + [OLE] = ASLED, + [OGE] = ASGED, + [OEQ] = ASEQD, + [ONE] = ASNED, +}; + +extern Type int32type, uint32type, ptrtype; + +static Node * +tmpnode(Node *np, Type *tp) +{ + char flags; + Symbol *sym; + + if (!np) + np = newnode(OTMP); + sym = getsym(TMPSYM); + sym->type = np->type = *tp; + flags = tp->flags & ~(PARF|INITF); + sym->type.flags = np->type.flags = flags; + sym->kind = STMP; + np->left = np->right = NULL; + np->u.sym = sym; + np->op = OTMP; + np->flags |= ISTMP; + return np; +} + +static Node * +load(Type *tp, Node *np, Node *new) +{ + int op; + int flags = tp->flags; + + if (flags & (AGGRF|FUNF)) { + *new = *np; + return new; + } + switch (tp->size) { + case 1: + op = ASLDSB; + break; + case 2: + op = ASLDSH; + break; + case 4: + op = (flags & FLOATF) ? ASLDS : ASLDSW; + break; + case 8: + op = (flags & FLOATF) ? ASLDD : ASLDL; + break; + default: + abort(); + } + /* + * unsigned version of operations are always +1 the + * signed version + */ + if ((flags & (INTF|SIGNF)) == INTF && tp->size < 8) + ++op; + + code(op, tmpnode(new, tp), np, NULL); + + return new; +} + +static Node *rhs(Node *np, Node *new); + +static Node * +cast(Type *td, Node *ns, Node *nd) +{ + Type *ts; + Node aux1, aux2; + int op, d_isint, s_isint; + + ts = &ns->type; + d_isint = (td->flags & INTF) != 0; + s_isint = (ts->flags & INTF) != 0; + + if (d_isint && s_isint) { + if (td->size <= ts->size) { + *nd = *ns; + return nd; + } + assert(td->size == 4 || td->size == 8); + switch (ts->size) { + case 1: + op = (td->size == 4) ? ASEXTBW : ASEXTBL; + break; + case 2: + op = (td->size == 4) ? ASEXTHW : ASEXTHL; + break; + case 4: + op = ASEXTWL; + break; + default: + abort(); + } + /* + * unsigned version of operations are always +1 the + * signed version + */ + op += (ts->flags & SIGNF) == 0; + } else if (d_isint) { + /* conversion from float to int */ + switch (ts->size) { + case 4: + op = (td->size == 8) ? ASSTOL : ASSTOW; + break; + case 8: + op = (td->size == 8) ? ASDTOL : ASDTOW; + break; + default: + abort(); + } + /* TODO: Add signess */ + } else if (s_isint) { + /* conversion from int to float */ + switch (ts->size) { + case 1: + case 2: + ts = (ts->flags&SIGNF) ? &int32type : &uint32type; + ns = cast(ts, ns, tmpnode(&aux2, ts)); + case 4: + op = (td->size == 8) ? ASSWTOD : ASSWTOS; + break; + case 8: + op = (td->size == 8) ? ASSLTOD : ASSLTOS; + break; + default: + abort(); + } + /* TODO: Add signess */ + } else { + /* conversion from float to float */ + op = (td->size == 4) ? ASEXTS : ASTRUNCD; + } + + code(op, tmpnode(nd, td), ns, NULL); + return nd; +} + +static Node *rhs(Node *np, Node *new); + +static Node * +call(Node *np, Node *fun, Node *ret) +{ + int n, op; + Type *tp; + Node aux, **q, *p, *pars[NR_FUNPARAM]; + + for (n = 0, p = np->right; p; p = p->right) + pars[n++] = rhs(p->left, newnode(OTMP)); + + tp = &np->type; + code(ASCALL, tmpnode(ret, tp), fun, NULL); + + for (q = pars; q < &pars[n]; ++q) { + op = (q == &pars[n-1]) ? ASPARE : ASPAR; + tmpnode(&aux, &(*q)->type); + code(op, NULL, *q, &aux); + } + code((np->op == OCALL) ? ASCALLE : ASCALLEX, NULL, NULL, NULL); + + return ret; +} + +static Node * +assign(Type *tp, Node *to, Node *from) +{ + int op; + + switch (tp->size) { + case 1: + op = ASSTB; + break; + case 2: + op = ASSTH; + break; + case 4: + op = (tp->flags & FLOATF) ? ASSTS : ASSTW; + break; + case 8: + op = (tp->flags & FLOATF) ? ASSTD : ASSTL; + break; + default: + op = ASSTM; + break; + } + code(op, to, from, NULL); + return from; +} + +static Node * +copy(Type *tp, Node *to, Node *from) +{ + int op; + + switch (tp->size) { + case 1: + op = ASCOPYB; + break; + case 2: + op = ASCOPYH; + break; + case 4: + op = (tp->flags & FLOATF) ? ASCOPYS : ASCOPYW; + break; + case 8: + op = (tp->flags & FLOATF) ? ASCOPYD : ASCOPYL; + break; + default: + /* TODO: Need to handle the general case */ + abort(); + } + code(op, to, from, NULL); + return from; +} + +/* TODO: Do field() transformation in sethi */ + +static Node * +field(Node *np, Node *ret, int islhs) +{ + Node base, node, off, add, *addr; + TUINT offset = np->right->u.sym->u.off; + + addr = rhs(np->left, &base); + + if (offset != 0) { + node.op = OADD; + node.type = ptrtype; + node.left = addr; + node.right = constnode(&off, offset, &ptrtype); + addr = rhs(&node, &add); + } + + if (islhs) + *ret = *addr; + else + load(&np->type, addr, ret); + + return ret; +} + +static Node * +lhs(Node *np, Node *new) +{ + switch (np->op) { + case OMEM: + case OAUTO: + *new = *np; + return new; + case OPTR: + return rhs(np->left, new); + case OFIELD: + return field(np, new, 1); + default: + abort(); + } +} + +static void +bool(Node *np, Symbol *true, Symbol *false) +{ + Node *l = np->left, *r = np->right; + Node ret, ifyes, ifno; + Symbol *label; + + switch (np->op) { + case ONEG: + bool(l, false, true); + break; + case OAND: + label = newlabel(); + bool(l, label, false); + setlabel(label); + bool(r, true, false); + break; + case OOR: + label = newlabel(); + bool(l, true, label); + setlabel(label); + bool(r, true, false); + break; + default: + label2node(&ifyes, true); + label2node(&ifno, false); + code(ASBRANCH, rhs(np, &ret), &ifyes, &ifno); + break; + } +} + +static Node * +ternary(Node *np, Node *ret) +{ + Node ifyes, ifno, phi, *colon, aux1, aux2, aux3; + + tmpnode(ret, &np->type); + label2node(&ifyes, NULL); + label2node(&ifno, NULL); + label2node(&phi, NULL); + + colon = np->right; + code(ASBRANCH, rhs(np->left, &aux1), &ifyes, &ifno); + + setlabel(ifyes.u.sym); + copy(&ret->type, ret, rhs(colon->left, &aux2)); + code(ASJMP, NULL, &phi, NULL); + + setlabel(ifno.u.sym); + copy(&ret->type, ret, rhs(colon->right, &aux3)); + setlabel(phi.u.sym); + + return ret; +} + +static Node * +function(void) +{ + Node aux; + Symbol *p; + + /* allocate stack space for parameters */ + for (p = locals; p && (p->type.flags & PARF) != 0; p = p->next) + code(ASALLOC, label2node(&aux, p), NULL, NULL); + + /* allocate stack space for local variables) */ + for ( ; p && p->id != TMPSYM; p = p->next) { + if (p->kind != SAUTO) + continue; + code(ASALLOC, label2node(&aux, p), NULL, NULL); + } + /* store formal parameters in parameters */ + for (p = locals; p; p = p->next) { + if ((p->type.flags & PARF) == 0) + break; + code(ASFORM, label2node(&aux, p), NULL, NULL); + } + return NULL; +} + +static void +swtch_if(Node *idx) +{ + Node aux1, aux2, *np; + Symbol *deflabel = NULL; + + for (;;) { + np = delstmt(); + setlabel(np->label); + + switch (np->op) { + case OESWITCH: + if (!deflabel) + deflabel = np->u.sym; + aux1.op = OJMP; + aux1.label = NULL; + aux1.u.sym = deflabel; + cgen(&aux1); + return; + case OCASE: + aux1 = *np; + aux1.op = OBRANCH; + aux1.label = NULL; + aux1.left = &aux2; + + aux2.op = OEQ; + aux2.type = idx->type; + aux2.left = np->left; + aux2.right = idx; + + cgen(&aux1); + break; + case ODEFAULT: + deflabel = np->u.sym; + break; + default: + abort(); + } + } +} + +static Node * +rhs(Node *np, Node *ret) +{ + Node aux1, aux2, *phi, *l = np->left, *r = np->right; + Type *tp; + int off, op; + char *tbl; + Symbol *true, *false; + + tp = &np->type; + + switch (np->op) { + case OBFUN: + return function(); + case ONOP: + case OBLOOP: + case OELOOP: + case OEFUN: + return NULL; + case OTMP: + case OCONST: + *ret = *np; + return np; + case OMEM: + case OAUTO: + return load(tp, np, ret); + case ONEG: + case OAND: + case OOR: + true = newlabel(); + false = newlabel(); + phi = label2node(&aux1, NULL); + tmpnode(ret, &int32type); + + bool(np, true, false); + + setlabel(true); + code(ASCOPYW, ret, constnode(&aux2, 1, &int32type), NULL); + code(ASJMP, NULL, phi, NULL); + + setlabel(false); + code(ASCOPYW, ret, constnode(&aux2, 0, &int32type), NULL); + + setlabel(phi->u.sym); + return ret; + case OMOD: + case OSHR: + assert(tp->flags & INTF); + case ODIV: + case OLT: + case OGT: + case OLE: + case OGE: + /* + * unsigned version of operations are always +1 the + * signed version + */ + off = (tp->flags & SIGNF) == 0; + goto binary; + case OSHL: + case OBAND: + case OBOR: + case OBXOR: + assert(tp->flags & INTF); + case OADD: + case OSUB: + case OMUL: + case OEQ: + case ONE: + off = 0; + binary: + if (l->complex >= r->complex) { + rhs(l, &aux1); + rhs(r, &aux2); + } else { + rhs(r, &aux2); + rhs(l, &aux1); + } + switch (tp->size) { + case 4: + tbl = (tp->flags & FLOATF) ? opasms : opasmw; + break; + case 8: + tbl = (tp->flags & FLOATF) ? opasmd : opasml; + break; + default: + abort(); + } + op = tbl[np->op] + off; + tmpnode(ret, tp); + code(op, ret, &aux1, &aux2); + return ret; + case OCALL: + case OCALLE: + if (l->op == OPTR) + l = rhs(l, &aux1); + return call(np, l, ret); + case OCAST: + return cast(tp, rhs(l, &aux1), ret); + case OASSIG: + /* TODO: Do this transformations in sethi */ + switch (np->u.subop) { + case OINC: + op = OADD; + goto post_oper; + case ODEC: + op = OSUB; + post_oper: + aux1.op = op; + aux1.left = rhs(l, ret); + aux1.right = r; + aux1.type = np->type; + rhs(&aux1, &aux2); + lhs(l, &aux1); + assign(tp, &aux1, &aux2); + break; + default: + aux2.type = np->type; + aux2.op = np->u.subop; + aux2.right = np->right; + aux2.left = np->left; + r = rhs(&aux2, &aux1); + Node aux3; + if (l->op == OCAST) { + aux3.type = l->left->type; + aux3.op = OCAST; + aux3.left = r; + aux3.right = NULL; + r = &aux3; + l = l->left; + } + case 0: + /* TODO: see what is the most difficult */ + lhs(l, &aux2); + rhs(r, ret); + return assign(tp, &aux2, ret); + } + return ret; + case OASK: + return ternary(np, ret); + case OCOMMA: + rhs(l, &aux1); + return rhs(r, ret); + case OPTR: + return load(tp, rhs(l, &aux1), ret); + case OADDR: + lhs(l, ret); + ret->type = *tp; + return ret; + case OFIELD: + return field(np, ret, 0); + case OBUILTIN: + switch (np->u.subop) { + case BVA_START: + l = rhs(l, &aux1); + code(ASVSTAR, NULL, l, NULL); + return NULL; + case BVA_END: + return NULL; + case BVA_ARG: + l = rhs(l, &aux1); + code(ASVARG, tmpnode(ret, tp), l, NULL); + return ret; + case BVA_COPY: + /* TODO */ + default: + abort(); + } + default: + abort(); + } + abort(); +} + +Node * +cgen(Node *np) +{ + Node aux, *p, *next; + + setlabel(np->label); + switch (np->op) { + case OJMP: + label2node(&aux, np->u.sym); + code(ASJMP, NULL, &aux, NULL); + break; + case OBRANCH: + next = np->next; + if (!next->label) + next->label = newlabel(); + bool(np->left, np->u.sym, next->label); + break; + case ORET: + p = (np->left) ? rhs(np->left, &aux) : NULL; + code(ASRET, NULL, p, NULL); + break; + case OBSWITCH: + p = rhs(np->left, &aux); + swtch_if(p); + break; + default: + rhs(np, &aux); + break; + } + return NULL; +} + +/* + * This is strongly influenced by + * http://plan9.bell-labs.com/sys/doc/compiler.ps (/sys/doc/compiler.ps) + * calculate addresability as follows + * AUTO => 11 value+fp + * REG => 11 reg + * STATIC => 11 (value) + * CONST => 11 $value + * These values of addressability are not used in the code generation. + * They are only used to calculate the Sethi-Ullman numbers. Since + * QBE is AMD64 targered we could do a better job there, and try to + * detect some of the complex addressing modes of these processors. + */ +Node * +sethi(Node *np) +{ + Node *lp, *rp; + + if (!np) + return np; + + np->complex = 0; + np->address = 0; + lp = np->left; + rp = np->right; + + switch (np->op) { + case OAUTO: + case OREG: + case OMEM: + case OCONST: + np->address = 11; + break; + case OCPL: + assert(np->type.flags & INTF); + np->op = OBXOR; + rp = constnode(NULL, ~(TUINT) 0, &np->type); + goto binary; + case OSNEG: + np->op = OSUB; + rp = lp; + lp = constnode(NULL, 0, &np->type); + if ((np->type.flags & INTF) == 0) + lp->u.f = 0.0; + default: + binary: + lp = sethi(lp); + rp = sethi(rp); + break; + } + np->left = lp; + np->right = rp; + + if (np->address > 10) + return np; + if (lp) + np->complex = lp->complex; + if (rp) { + int d = np->complex - rp->complex; + + if (d == 0) + ++np->complex; + else if (d < 0) + np->complex = rp->complex; + } + if (np->complex == 0) + ++np->complex; + return np; +} diff --git a/cc2/target/qbe_amd64-sysv/code.c b/cc2/target/qbe_amd64-sysv/code.c @@ -0,0 +1,569 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/qbe/code.c"; + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <cstd.h> +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +#define ADDR_LEN (INTIDENTSIZ+64) + +static void binary(void), unary(void), store(void), jmp(void), ret(void), + branch(void), call(void), ecall(void), param(void), + alloc(void), form2local(void), ldir(void), vastart(void), + vaarg(void); + +static struct opdata { + void (*fun)(void); + char *txt; + char letter; +} optbl [] = { + [ASLDSB] = {.fun = unary, .txt = "loadsb", .letter = 'w'}, + [ASLDUB] = {.fun = unary, .txt = "loadub", .letter = 'w'}, + [ASLDSH] = {.fun = unary, .txt = "loadsh", .letter = 'w'}, + [ASLDUH] = {.fun = unary, .txt = "loaduh", .letter = 'w'}, + [ASLDSW] = {.fun = unary, .txt = "loadsw", .letter = 'w'}, + [ASLDUW] = {.fun = unary, .txt = "loaduw", .letter = 'w'}, + [ASLDL] = {.fun = unary, .txt = "loadl", .letter = 'l'}, + [ASLDS] = {.fun = unary, .txt = "loads", .letter = 's'}, + [ASLDD] = {.fun = unary, .txt = "loadd", .letter = 'd'}, + + [ASCOPYB] = {.fun = unary, .txt = "copy", .letter = 'b'}, + [ASCOPYH] = {.fun = unary, .txt = "copy", .letter = 'h'}, + [ASCOPYW] = {.fun = unary, .txt = "copy", .letter = 'w'}, + [ASCOPYL] = {.fun = unary, .txt = "copy", .letter = 'l'}, + [ASCOPYS] = {.fun = unary, .txt = "copy", .letter = 's'}, + [ASCOPYD] = {.fun = unary, .txt = "copy", .letter = 'd'}, + + [ASSTB] = {.fun = store, .txt = "store", .letter = 'b'}, + [ASSTH] = {.fun = store, .txt = "store", .letter = 'h'}, + [ASSTW] = {.fun = store, .txt = "store", .letter = 'w'}, + [ASSTL] = {.fun = store, .txt = "store", .letter = 'l'}, + [ASSTM] = {.fun = ldir}, + [ASSTS] = {.fun = store, .txt = "store", .letter = 's'}, + [ASSTD] = {.fun = store, .txt = "store", .letter = 'd'}, + + [ASADDW] = {.fun = binary, .txt = "add", .letter = 'w'}, + [ASSUBW] = {.fun = binary, .txt = "sub", .letter = 'w'}, + [ASMULW] = {.fun = binary, .txt = "mul", .letter = 'w'}, + [ASMODW] = {.fun = binary, .txt = "rem", .letter = 'w'}, + [ASUMODW] = {.fun = binary, .txt = "urem", .letter = 'w'}, + [ASDIVW] = {.fun = binary, .txt = "div", .letter = 'w'}, + [ASUDIVW] = {.fun = binary, .txt = "udiv", .letter = 'w'}, + [ASSHLW] = {.fun = binary, .txt = "shl", .letter = 'w'}, + [ASSHRW] = {.fun = binary, .txt = "sar", .letter = 'w'}, + [ASUSHRW] = {.fun = binary, .txt = "shr", .letter = 'w'}, + [ASLTW] = {.fun = binary, .txt = "csltw", .letter = 'w'}, + [ASULTW] = {.fun = binary, .txt = "cultw", .letter = 'w'}, + [ASGTW] = {.fun = binary, .txt = "csgtw", .letter = 'w'}, + [ASUGTW] = {.fun = binary, .txt = "cugtw", .letter = 'w'}, + [ASLEW] = {.fun = binary, .txt = "cslew", .letter = 'w'}, + [ASULEW] = {.fun = binary, .txt = "culew", .letter = 'w'}, + [ASGEW] = {.fun = binary, .txt = "csgew", .letter = 'w'}, + [ASUGEW] = {.fun = binary, .txt = "cugew", .letter = 'w'}, + [ASEQW] = {.fun = binary, .txt = "ceqw", .letter = 'w'}, + [ASNEW] = {.fun = binary, .txt = "cnew", .letter = 'w'}, + [ASBANDW] = {.fun = binary, .txt = "and", .letter = 'w'}, + [ASBORW] = {.fun = binary, .txt = "or", .letter = 'w'}, + [ASBXORW] = {.fun = binary, .txt = "xor", .letter = 'w'}, + + [ASADDL] = {.fun = binary, .txt = "add", .letter = 'l'}, + [ASSUBL] = {.fun = binary, .txt = "sub", .letter = 'l'}, + [ASMULL] = {.fun = binary, .txt = "mul", .letter = 'l'}, + [ASMODL] = {.fun = binary, .txt = "rem", .letter = 'l'}, + [ASUMODL] = {.fun = binary, .txt = "urem", .letter = 'l'}, + [ASDIVL] = {.fun = binary, .txt = "div", .letter = 'l'}, + [ASUDIVL] = {.fun = binary, .txt = "udiv", .letter = 'l'}, + [ASSHLL] = {.fun = binary, .txt = "shl", .letter = 'l'}, + [ASSHRL] = {.fun = binary, .txt = "sar", .letter = 'l'}, + [ASUSHRL] = {.fun = binary, .txt = "shr", .letter = 'l'}, + [ASLTL] = {.fun = binary, .txt = "csltl", .letter = 'w'}, + [ASULTL] = {.fun = binary, .txt = "cultl", .letter = 'w'}, + [ASGTL] = {.fun = binary, .txt = "csgtl", .letter = 'w'}, + [ASUGTL] = {.fun = binary, .txt = "cugtl", .letter = 'w'}, + [ASLEL] = {.fun = binary, .txt = "cslel", .letter = 'w'}, + [ASULEL] = {.fun = binary, .txt = "culel", .letter = 'w'}, + [ASGEL] = {.fun = binary, .txt = "csgel", .letter = 'w'}, + [ASUGEL] = {.fun = binary, .txt = "cugel", .letter = 'w'}, + [ASEQL] = {.fun = binary, .txt = "ceql", .letter = 'w'}, + [ASNEL] = {.fun = binary, .txt = "cnel", .letter = 'w'}, + [ASBANDL] = {.fun = binary, .txt = "and", .letter = 'l'}, + [ASBORL] = {.fun = binary, .txt = "or", .letter = 'l'}, + [ASBXORL] = {.fun = binary, .txt = "xor", .letter = 'l'}, + + [ASADDS] = {.fun = binary, .txt = "add", .letter = 's'}, + [ASSUBS] = {.fun = binary, .txt = "sub", .letter = 's'}, + [ASMULS] = {.fun = binary, .txt = "mul", .letter = 's'}, + [ASDIVS] = {.fun = binary, .txt = "div", .letter = 's'}, + [ASLTS] = {.fun = binary, .txt = "clts", .letter = 'w'}, + [ASGTS] = {.fun = binary, .txt = "cgts", .letter = 'w'}, + [ASLES] = {.fun = binary, .txt = "cles", .letter = 'w'}, + [ASGES] = {.fun = binary, .txt = "cges", .letter = 'w'}, + [ASEQS] = {.fun = binary, .txt = "ceqs", .letter = 'w'}, + [ASNES] = {.fun = binary, .txt = "cnes", .letter = 'w'}, + + [ASADDD] = {.fun = binary, .txt = "add", .letter = 'd'}, + [ASSUBD] = {.fun = binary, .txt = "sub", .letter = 'd'}, + [ASMULD] = {.fun = binary, .txt = "mul", .letter = 'd'}, + [ASDIVD] = {.fun = binary, .txt = "div", .letter = 'd'}, + [ASLTD] = {.fun = binary, .txt = "cltd", .letter = 'w'}, + [ASGTD] = {.fun = binary, .txt = "cgtd", .letter = 'w'}, + [ASLED] = {.fun = binary, .txt = "cled", .letter = 'w'}, + [ASGED] = {.fun = binary, .txt = "cged", .letter = 'w'}, + [ASEQD] = {.fun = binary, .txt = "ceqd", .letter = 'w'}, + [ASNED] = {.fun = binary, .txt = "cned", .letter = 'w'}, + + [ASEXTBW] = {.fun = unary, .txt = "extsb", .letter = 'w'}, + [ASUEXTBW]= {.fun = unary, .txt = "extub", .letter = 'w'}, + [ASEXTBL] = {.fun = unary, .txt = "extsb", .letter = 'l'}, + [ASUEXTBL]= {.fun = unary, .txt = "extub", .letter = 'l'}, + [ASEXTHW] = {.fun = unary, .txt = "extsh", .letter = 'w'}, + [ASUEXTHW]= {.fun = unary, .txt = "extuh", .letter = 'w'}, + [ASEXTWL] = {.fun = unary, .txt = "extsw", .letter = 'l'}, + [ASUEXTWL]= {.fun = unary, .txt = "extuw", .letter = 'l'}, + + [ASSTOL] = {.fun = unary, .txt = "stosi", .letter = 'l'}, + [ASSTOW] = {.fun = unary, .txt = "stosi", .letter = 'w'}, + [ASDTOL] = {.fun = unary, .txt = "dtosi", .letter = 'l'}, + [ASDTOW] = {.fun = unary, .txt = "dtosi", .letter = 'w'}, + + [ASSWTOD] = {.fun = unary, .txt = "swtof", .letter = 'd'}, + [ASSWTOS] = {.fun = unary, .txt = "swtof", .letter = 's'}, + [ASSLTOD] = {.fun = unary, .txt = "sltof", .letter = 'd'}, + [ASSLTOS] = {.fun = unary, .txt = "sltof", .letter = 's'}, + + [ASEXTS] = {.fun = unary, .txt = "exts", .letter = 'd'}, + [ASTRUNCD] = {.fun = unary, .txt = "truncd", .letter = 's'}, + + [ASBRANCH] = {.fun = branch}, + [ASJMP] = {.fun = jmp}, + [ASRET] = {.fun = ret}, + [ASCALL] = {.fun = call}, + [ASCALLE] = {.fun = ecall, .txt = ")"}, + [ASCALLEX] = {.fun = ecall, .txt = ", ...)"}, + [ASPAR] = {.fun = param, .txt = "%s %s, "}, + [ASPARE] = {.fun = param, .txt = "%s %s"}, + [ASALLOC] = {.fun = alloc}, + [ASFORM] = {.fun = form2local}, + + [ASVSTAR] = {.fun = vastart}, + [ASVARG] = {.fun = vaarg}, +}; + +static char buff[ADDR_LEN]; +/* + * : is for user-defined Aggregate Types + * $ is for globals (represented by a pointer) + * % is for function-scope temporaries + * @ is for block labels + */ +static char +sigil(Symbol *sym) +{ + switch (sym->kind) { + case SEXTRN: + case SGLOB: + case SPRIV: + case SLOCAL: + return '$'; + case SAUTO: + case STMP: + return '%'; + case SLABEL: + return '@'; + default: + abort(); + } +} + +static char * +symname(Symbol *sym) +{ + char c = sigil(sym); + + if (sym->name) { + switch (sym->kind) { + case SEXTRN: + case SGLOB: + sprintf(buff, "%c%s", c, sym->name); + return buff; + case SLOCAL: + case SPRIV: + case SAUTO: + sprintf(buff, "%c%s.%u", c, sym->name, sym->id); + return buff; + default: + abort(); + } + } + sprintf(buff, "%c.%u", c, sym->numid); + + return buff; +} + +static void +emitconst(Node *np) +{ + switch (np->type.size) { + case 1: + printf("%d", (int) np->u.i & 0xFF); + break; + case 2: + printf("%d", (int) np->u.i & 0xFFFF); + break; + case 4: + printf("%ld", (long) np->u.i & 0xFFFFFFFF); + break; + case 8: + printf("%lld", (long long) np->u.i); + break; + default: + abort(); + } +} + +static void +emittree(Node *np) +{ + if (!np) + return; + + switch (np->op) { + case OSTRING: + printf("\"%s\"", np->u.s); + free(np->u.s); + np->u.s = NULL; + break; + case OCONST: + emitconst(np); + break; + case OADDR: + emittree(np->left); + break; + case OMEM: + fputs(symname(np->u.sym), stdout); + break; + default: + emittree(np->left); + printf(" %c ", np->op); + emittree(np->right); + break; + } +} + +static char * +size2asm(Type *tp) +{ + if (tp->flags & STRF) { + return "b"; + } else if (tp->flags & INTF) { + switch (tp->size) { + case 1: + return "b"; + case 2: + return "h"; + case 4: + return "w"; + case 8: + return "l"; + } + } else if (tp->flags & FLOATF) { + if (tp->size == 4) + return "s"; + else if (tp->size == 8) + return "d"; + } + abort(); +} + +void +defglobal(Symbol *sym) +{ + if (sym->kind == SEXTRN) + return; + if (sym->kind == SGLOB) + fputs("export ", stdout); + printf("data %s = {\n", symname(sym)); + if (sym->type.flags & INITF) + return; + printf("\tz\t%lu\n}\n", sym->type.size); +} + +void +defpar(Symbol *sym) +{ + sym->type.flags |= PARF; +} + +void +defvar(Symbol *sym) +{ + if (sym->kind == SREG) + sym->kind = SAUTO; +} + +void +data(Node *np) +{ + printf("\t%s\t", size2asm(&np->type)); + emittree(np); + putchar(','); + putchar('\n'); +} + +static char * +size2stack(Type *tp) +{ + if (tp->flags & INTF) { + switch (tp->size) { + case 1: + case 2: + case 4: + return "w"; + case 8: + return "l"; + } + } else if (tp->flags & FLOATF) { + if (tp->size == 4) + return "s"; + else if (tp->size == 8) + return "d"; + } else if (tp->size == 0) { + return "w"; + } + abort(); +} + +void +writeout(void) +{ + Symbol *p; + Type *tp; + char *sep, *name; + int haslabel = 0; + + if (!curfun) + return; + if (curfun->kind == SGLOB) + fputs("export ", stdout); + printf("function %s %s(", size2stack(&curfun->rtype), symname(curfun)); + + /* declare formal parameters */ + for (sep = "", p = locals; p; p = p->next, sep = ",") { + if ((p->type.flags & PARF) == 0) + break; + printf("%s%s %s.val", sep, size2stack(&p->type), symname(p)); + } + printf("%s)\n{\n", (curfun->type.flags&ELLIPS) ? ", ..." : ""); + + /* emit assembler instructions */ + for (pc = prog; pc; pc = pc->next) { + if (pc->label) { + haslabel = 1; + printf("%s\n", symname(pc->label)); + } + if (!pc->op) + continue; + if (pc->flags&BBENTRY && !haslabel) + printf("%s\n", symname(newlabel())); + (*optbl[pc->op].fun)(); + if (!pc->label) + haslabel = 0; + } + + puts("}"); +} + +static char * +addr2txt(Addr *a) +{ + switch (a->kind) { + case SCONST: + sprintf(buff, "%llu", (unsigned long long) a->u.i); + return buff; + case SAUTO: + case SLABEL: + case STMP: + case SGLOB: + case SEXTRN: + case SPRIV: + case SLOCAL: + return symname(a->u.sym); + default: + abort(); + } +} + +static void +binary(void) +{ + struct opdata *p = &optbl[pc->op]; + char to[ADDR_LEN], from1[ADDR_LEN], from2[ADDR_LEN]; + + strcpy(to, addr2txt(&pc->to)); + strcpy(from1, addr2txt(&pc->from1)); + strcpy(from2, addr2txt(&pc->from2)); + printf("\t%s =%c\t%s\t%s,%s\n", to, p->letter, p->txt, from1, from2); +} + +static void +ldir(void) +{ + struct opdata *p = &optbl[pc->op]; + char to[ADDR_LEN], from[ADDR_LEN]; + /* TODO: what type do we use for the size? */ + + /* TODO: it is pending */ +} + +static void +store(void) +{ + struct opdata *p = &optbl[pc->op]; + char to[ADDR_LEN], from[ADDR_LEN]; + + strcpy(to, addr2txt(&pc->to)); + strcpy(from, addr2txt(&pc->from1)); + printf("\t\t%s%c\t%s,%s\n", p->txt, p->letter, from, to); +} + +static void +unary(void) +{ + struct opdata *p = &optbl[pc->op]; + char to[ADDR_LEN], from[ADDR_LEN]; + + strcpy(to, addr2txt(&pc->to)); + strcpy(from, addr2txt(&pc->from1)); + printf("\t%s =%c\t%s\t%s\n", to, p->letter, p->txt, from); +} + +static void +call(void) +{ + struct opdata *p = &optbl[pc->op]; + char to[ADDR_LEN], from[ADDR_LEN]; + Symbol *sym = pc->to.u.sym; + + strcpy(to, addr2txt(&pc->to)); + strcpy(from, addr2txt(&pc->from1)); + printf("\t%s =%s\tcall\t%s(", + to, size2stack(&sym->type), from); +} + +static void +param(void) +{ + Symbol *sym = pc->from2.u.sym; + + printf(optbl[pc->op].txt, + size2stack(&sym->type), addr2txt(&pc->from1)); +} + +static void +ecall(void) +{ + struct opdata *p = &optbl[pc->op]; + + puts(p->txt); +} + +static void +ret(void) +{ + if (pc->from1.kind == SNONE) + puts("\t\tret"); + else + printf("\t\tret\t%s\n", addr2txt(&pc->from1)); +} + +static void +jmp(void) +{ + printf("\t\tjmp\t%s\n", addr2txt(&pc->from1)); +} + +static void +branch(void) +{ + char to[ADDR_LEN], from1[ADDR_LEN], from2[ADDR_LEN]; + + strcpy(to, addr2txt(&pc->to)); + strcpy(from1, addr2txt(&pc->from1)); + strcpy(from2, addr2txt(&pc->from2)); + printf("\t\tjnz\t%s,%s,%s\n", to, from1, from2); +} + +static void +vastart(void) +{ + printf("\t\tvastart %s\n", addr2txt(&pc->from1)); +} + +static void +vaarg(void) +{ + Symbol *sym = pc->to.u.sym; + Type *tp = &sym->type; + char to[ADDR_LEN], from[ADDR_LEN]; + + strcpy(to, addr2txt(&pc->to)); + strcpy(from, addr2txt(&pc->from1)); + printf("\t\t%s =%s vaarg %s\n", to, size2asm(tp), from); +} + +static void +alloc(void) +{ + Symbol *sym = pc->to.u.sym; + Type *tp = &sym->type; + extern Type ptrtype; + + printf("\t%s =%s\talloc%lu\t%lu\n", + symname(sym), size2asm(&ptrtype), tp->align+3 & ~3, tp->size); +} + +static void +form2local(void) +{ + Symbol *sym = pc->to.u.sym; + Type *tp = &sym->type; + char *name = symname(sym); + + printf("\t\tstore%s\t%s.val,%s\n", size2asm(tp), name, name); +} + +void +endinit(void) +{ + puts("}"); +} + +void +getbblocks(void) +{ + Inst *i; + + if (!prog) + return; + + prog->flags |= BBENTRY; + for (pc = prog; pc; pc = pc->next) { + switch (pc->op) { + case ASBRANCH: + i = pc->from2.u.sym->u.inst; + i->flags |= BBENTRY; + case ASJMP: + i = pc->from1.u.sym->u.inst; + i->flags |= BBENTRY; + case ASRET: + if (pc->next) + pc->next->flags |= BBENTRY; + break; + } + } +} diff --git a/cc2/target/qbe_amd64-sysv/optm.c b/cc2/target/qbe_amd64-sysv/optm.c @@ -0,0 +1,58 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/qbe/optm.c"; + +#include <stddef.h> + +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +optm_dep(Node *np) +{ + int op = np->op; + Node *p, *dst, *next = np->next; + Symbol *sym, *osym; + + switch (op) { + case OEFUN: + /* + * In QBE we need at the end of a basic block + * a jump, so we have to ensure that the last + * statement of the function is a ret, a jmp + * or a branch. In the same way, QBE does + * not accept labels at the end of a function + * (ONOP is used for labels) so we have to add + * a ret there, and in the case of branches + * we need a label for the next statement + */ + op = (np->prev) ? np->prev->op : 0; + if (!op || op == ONOP || op == OBRANCH || (op != ORET && op != OJMP)) + addstmt(newnode(ORET), KEEPCUR); + break; + case OBRANCH: + if (!next->label) { + sym = getsym(TMPSYM); + sym->kind = SLABEL; + next->label = sym; + } + case OJMP: + for (;;) { + dst = np->u.sym->u.stmt; + if (dst->op != OJMP) + break; + np->u.sym = dst->u.sym; + } + for (p = np->next; p; p = p->next) { + if (p == dst) + return NULL; + if (p->op == ONOP || + p->op == OBLOOP || + p->op == OELOOP) { + continue; + } + break; + } + break; + } + return np; +} diff --git a/cc2/target/qbe_amd64-sysv/target.mk b/cc2/target/qbe_amd64-sysv/target.mk @@ -0,0 +1,6 @@ + +OBJ-qbe_amd64-sysv = $(OBJ) \ + target/qbe_amd64-sysv/cgen.o \ + target/qbe_amd64-sysv/optm.o \ + target/qbe_amd64-sysv/code.o \ + target/qbe_amd64-sysv/types.o diff --git a/cc2/target/qbe_amd64-sysv/types.c b/cc2/target/qbe_amd64-sysv/types.c @@ -0,0 +1,94 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/qbe/types.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + + +Type int8type = { + .flags = SIGNF | INTF, + .size = 1, + .align = 1 +}; + +Type int16type = { + .flags = SIGNF | INTF, + .size = 2, + .align = 2 +}; + +Type int32type = { + .flags = SIGNF | INTF, + .size = 4, + .align = 4 +}; + +Type int64type = { + .flags = SIGNF | INTF, + .size = 8, + .align = 8 +}; + +Type uint8type = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type uint16type = { + .flags = INTF, + .size = 2, + .align = 2 +}; + +Type uint32type = { + .flags = INTF, + .size = 4, + .align = 4 +}; + +Type uint64type = { + .flags = INTF, + .size = 8, + .align = 2 +}; + +Type ptrtype = { + .flags = INTF, + .size = 8, + .align = 8 +}; + +Type booltype = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type float32type = { + .flags = FLOATF, + .size = 4, + .align = 4 +}; + +Type float64type = { + .flags = FLOATF, + .size = 8, + .align = 8 +}; + +Type float80type = { + .flags = FLOATF, + .size = 16, + .align = 16 +}; + +Type voidtype = { + .size = 0, + .align = 0 +}; + +Type arg_type = { + .size = 24, + .align = 8 +}; diff --git a/cc2/target/z80-scc/arch.h b/cc2/target/z80-scc/arch.h @@ -0,0 +1 @@ +/* See LICENSE file for copyright and license details. */ diff --git a/cc2/target/z80-scc/cgen.c b/cc2/target/z80-scc/cgen.c @@ -0,0 +1,68 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/z80/cgen.c"; + +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +cgen(Node *np) +{ +} + +/* + * This is strongly influenced by + * http://plan9.bell-labs.com/sys/doc/compiler.ps (/sys/doc/compiler.ps) + * calculate addresability as follows + * AUTO => 11 value+fp + * REG => 13 reg + * STATIC => 12 (value) + * CONST => 20 $value + */ +Node * +sethi(Node *np) +{ + Node *lp, *rp; + + if (!np) + return np; + + np->complex = 0; + np->address = 0; + lp = np->left; + rp = np->right; + switch (np->op) { + case OAUTO: + np->address = 11; + break; + case OREG: + np->address = 13; + break; + case OMEM: + np->address = 12; + break; + case OCONST: + np->address = 20; + break; + default: + sethi(lp); + sethi(rp); + break; + } + + if (np->address > 10) + return np; + if (lp) + np->complex = lp->complex; + if (rp) { + int d = np->complex - rp->complex; + + if (d == 0) + ++np->complex; + else if (d < 0) + np->complex = rp->complex; + } + if (np->complex == 0) + ++np->complex; + return np; +} diff --git a/cc2/target/z80-scc/code.c b/cc2/target/z80-scc/code.c @@ -0,0 +1,229 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/z80/code.c"; + +#include <stdio.h> +#include <stdlib.h> + +#include <cstd.h> +#include "arch.h" +#include "../../../inc/cc.h" +#include "../../cc2.h" + +enum segment { + CODESEG, + DATASEG, + BSSSEG, + NOSEG +}; + +static int curseg = NOSEG; +static unsigned long offpar, offvar; + +static void +segment(int seg) +{ + static char *txt[] = { + [CODESEG] = "\tCSEG\n", + [DATASEG] = "\tDSEG\n", + [BSSSEG] = "\tASEG\n", + }; + + if (seg == curseg) + return; + fputs(txt[seg], stdout); + curseg = seg; +} + +static char * +symname(Symbol *sym) +{ + static char name[INTIDENTSIZ+1]; + + if (sym->name) { + switch (sym->kind) { + case SGLOB: + case SEXTRN: + snprintf(name, sizeof(name), "_%s", sym->name); + return name; + case SPRIV: + return sym->name; + } + } + + sprintf(name, ".%d", sym->numid); + + return name; +} + +static void +label(Symbol *sym) +{ + int seg; + char *name = symname(sym); + + if (sym->type.flags & FUNF) + seg = CODESEG; + else if (sym->type.flags & INITF) + seg = DATASEG; + else + seg = BSSSEG; + segment(seg); + + switch (sym->kind) { + case SEXTRN: + printf("\tEXTRN\t%s\n", name); + return; + case SGLOB: + printf("\tPUBLIC\t%s\n", name); + break; + } + + printf("%s:\n", name); +} + +static void +emitconst(Node *np) +{ + switch (np->type.size) { + case 1: + printf("%d", (int) np->u.i & 0xFF); + break; + case 2: + printf("%d", (int) np->u.i & 0xFFFF); + break; + case 4: + printf("%ld", (long) np->u.i & 0xFFFFFFFF); + break; + default: + abort(); + } +} + +static void +emittree(Node *np) +{ + if (!np) + return; + + switch (np->op) { + case OSTRING: + printf("\"%s\"", np->u.s); + free(np->u.s); + np->u.s = NULL; + break; + case OCONST: + emitconst(np); + break; + case OADDR: + emittree(np->left); + break; + case OMEM: + fputs(symname(np->u.sym), stdout); + break; + default: + emittree(np->left); + printf(" %c ", np->op); + emittree(np->right); + break; + } +} + +static void +size2asm(Type *tp) +{ + char *s; + + /* + * In z80 we can ignore the alignment + */ + if (tp->flags & STRF) { + s = "\tDB\t"; + } else { + switch (tp->size) { + case 1: + s = "\tDB\t"; + break; + case 2: + s = "\tDW\t"; + break; + case 4: + s = "\tDD\t"; + break; + default: + s = "\tDS\t%lu,"; + break; + } + } + printf(s, tp->size); +} + +void +newfun() +{ + offpar = offvar = 0; +} + +void +defpar(Symbol *sym) +{ + unsigned long align, size; + + if (sym->kind != SREG && sym->kind != SAUTO) + return; + align = sym->type.align; + size = sym->type.size; + + offpar -= align-1 & ~align; + sym->u.off = offpar; + offpar -= size; + sym->kind = SAUTO; +} + +void +defvar(Symbol *sym) +{ + unsigned long align, size; + + if (sym->kind != SREG && sym->kind != SAUTO) + return; + align = sym->type.align; + size = sym->type.size; + + offvar += align-1 & ~align; + sym->u.off = offvar; + offvar += size; + sym->kind = SAUTO; +} + +void +defglobal(Symbol *sym) +{ + label(sym); + if (sym->kind == SEXTRN || (sym->type.flags & INITF)) + return; + size2asm(&sym->type); + puts("0"); +} + +void +data(Node *np) +{ + size2asm(&np->type); + emittree(np); + putchar('\n'); +} + +void +writeout(void) +{ +} + +void +endinit(void) +{ +} + +void +getbblocks(void) +{ +} diff --git a/cc2/target/z80-scc/optm.c b/cc2/target/z80-scc/optm.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/z80/optm.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + +Node * +optm_dep(Node *np) +{ + return np; +} diff --git a/cc2/target/z80-scc/target.mk b/cc2/target/z80-scc/target.mk @@ -0,0 +1,7 @@ + +OBJ-z80-scc = $(OBJ) \ + target/z80-scc/cgen.o \ + target/z80-scc/optm.o \ + target/z80-scc/code.o \ + target/z80-scc/types.o + diff --git a/cc2/target/z80-scc/types.c b/cc2/target/z80-scc/types.c @@ -0,0 +1,95 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./cc2/arch/z80/types.c"; + +#include "../../../inc/cc.h" +#include "../../cc2.h" + + +Type int8type = { + .flags = SIGNF | INTF, + .size = 1, + .align = 1 +}; + +Type int16type = { + .flags = SIGNF | INTF, + .size = 2, + .align = 1 +}; + +Type int32type = { + .flags = SIGNF | INTF, + .size = 4, + .align = 1 +}; + +Type int64type = { + .flags = SIGNF | INTF, + .size = 8, + .align = 1 +}; + +Type uint8type = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type uint16type = { + .flags = INTF, + .size = 2, + .align = 1 +}; + +Type uint32type = { + .flags = INTF, + .size = 4, + .align = 1 +}; + +Type uint64type = { + .flags = INTF, + .size = 8, + .align = 1 +}; + +Type ptrtype = { + .flags = INTF, + .size = 2, + .align = 1 +}; + +Type booltype = { + .flags = INTF, + .size = 1, + .align = 1 +}; + +Type float32type = { + .flags = FLOATF, + .size = 4, + .align = 1 +}; + +Type float64type = { + .flags = FLOATF, + .size = 4, + .align = 1 +}; + +Type float80type = { + .flags = FLOATF, + .size = 4, + .align = 1 +}; + +Type voidtype = { + .size = 0, + .align = 0 +}; + +/* this types is not going to be used in this arch */ +Type arg_type = { + .size = 0, + .align = 0 +}; diff --git a/config.mk b/config.mk @@ -2,11 +2,21 @@ VERSION = 0.1 ## Customize below to fit your system -ARCHS = z80 i386-sysv amd64-sysv qbe -SYSS = linux openbsd -# default architecure used in nested makefiles -ARCH = qbe -SYS = linux +# TARGET is defined by backend-arch-abi-sys + +TARGETS = z80-scc-dos-com \ + amd64-sysv-linux-elf \ + i386-sysv-linux-elf \ + amd64-sysv-openbsd-elf + +# USEQBE selects QBE by default in the targets that support it +USEQBE = 1 + +#MKQBE enable build of +MKQBE = 1 + +TARGET = amd64-sys-linux-elf + DRIVER = posix # Can be c89 or c99 @@ -23,14 +33,11 @@ MANPREFIX = $(PREFIX)/share/man AS = as # for Plan9 add -D_SUSV2_SOURCE -SCC_CFLAGS = -DARCH=\"$(ARCH)\" \ - -DSYS=\"$(SYS)\" \ - $(CSTDINC) \ - -DPREFIX=\"$(PREFIX)\" \ +SCC_CFLAGS = $(MOREFLAGS) \ -g \ $(CFLAGS) -SCC_LDFLAGS = $(LDFLAGS) +SCC_LDFLAGS = -L$(LIBDIR)/ $(LDFLAGS) .s.o: $(AS) $< -o $@ @@ -40,3 +47,4 @@ SCC_LDFLAGS = $(LDFLAGS) .c: $(CC) $(SCC_CFLAGS) $(SCC_LDFLAGS) -o $@ $< + diff --git a/crt/Makefile b/crt/Makefile @@ -1,11 +0,0 @@ -.POSIX: - -include ../config.mk - -all: crt-$(ARCH)-$(SYS).o - -crt-$(ARCH)-$(SYS).o: $(ARCH)-$(SYS)/crt.o - ln -f $(ARCH)-$(SYS)/crt.o $@ - -clean: - rm -f */crt.o crt-*.o diff --git a/crt/amd64-sysv-linux/crt.s b/crt/amd64-sysv-linux/crt.s @@ -1,8 +0,0 @@ - .file "crt.s" - - .text - .global _start -_start: - call main - movl %eax, %edi - call exit diff --git a/crt/amd64-sysv-openbsd/crt.s b/crt/amd64-sysv-openbsd/crt.s @@ -1,23 +0,0 @@ - .file "crt.s" - - .section ".note.openbsd.ident", "a" - .p2align 2 - .long 8 - .long 4 - .long 1 - .ascii "OpenBSD\0" - .long 0 - .p2align 2 - - .text - .align 8 - .global _start -_start: - call main - movl %eax, %edi - call exit - - .data - .global __guard_local -__guard_local: - .long 0 diff --git a/crt/i386-sysv-linux/crt.s b/crt/i386-sysv-linux/crt.s @@ -1 +0,0 @@ -/* TODO */ diff --git a/crt/i386-sysv-openbsd/crt.s b/crt/i386-sysv-openbsd/crt.s @@ -1 +0,0 @@ -/* TODO */ diff --git a/crt/qbe-linux/crt.s b/crt/qbe-linux/crt.s @@ -1,8 +0,0 @@ - .file "crt.s" - - .text - .global _start -_start: - call main - movl %eax, %edi - call exit diff --git a/crt/qbe-openbsd/crt.s b/crt/qbe-openbsd/crt.s @@ -1,23 +0,0 @@ - .file "crt.s" - - .section ".note.openbsd.ident", "a" - .p2align 2 - .long 8 - .long 4 - .long 1 - .ascii "OpenBSD\0" - .long 0 - .p2align 2 - - .text - .align 8 - .global _start -_start: - call main - movl %eax, %edi - call exit - - .data - .global __guard_local -__guard_local: - .long 0 diff --git a/crt/z80-linux/crt.s b/crt/z80-linux/crt.s @@ -1 +0,0 @@ -/* TODO */ diff --git a/crt/z80-openbsd/crt.s b/crt/z80-openbsd/crt.s @@ -1 +0,0 @@ -/* TODO */ diff --git a/driver/Makefile b/driver/Makefile @@ -0,0 +1,7 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../config.mk + +all dep clean distclean: + cd $(DRIVER) && $(MAKE) $@ diff --git a/driver/posix/Makefile b/driver/posix/Makefile @@ -3,24 +3,33 @@ include ../../config.mk -OBJS = scc.o +LIBDIR = ../../lib/cc -all: scc +OBJ = scc.o +HDR = config.h \ + ../../inc/cc.h \ + ../../inc/arg.h \ + ../../inc/syslibs.h \ + ../../inc/ldflags.h -$(OBJS): ../../inc/cc.h ../../inc/arg.h ../../inc/syslibs.h ../../inc/ldflags.h +all: scc -scc: $(OBJS) ../../lib/libcc.a - $(CC) $(SCC_LDFLAGS) $(OBJS) ../../lib/libcc.a -o $@ +dep: + PREFIX=$(PREFIX) USEQBE=$(USEQBE) ./gendep.sh $(TARGET) -../../inc/syslibs.h: ../../inc/syslibs.def.h - cp -f ../../inc/syslibs.def.h ../../inc/syslibs.h +$(OBJ): $(HDR) -../../inc/ldflags.h: ../../inc/ldflags.def.h - cp -f ../../inc/ldflags.def.h ../../inc/ldflags.h +scc: $(OBJ) $(LIBDIR)/libcc.a + $(CC) $(SCC_LDFLAGS) $(OBJ) -lcc -o $@ -../../lib/libcc.a: - cd ../../lib && $(MAKE) -e +$(LIBDIR)/libcc.a: + cd $(LIBDIR) && $(MAKE) clean: - rm -f $(OBJS) + rm -f $(OBJ) rm -f scc + +distclean: clean + rm -f config.h + +include ../../lib/cc/libdep.mk diff --git a/driver/posix/gendep.sh b/driver/posix/gendep.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e + +rm -f config.h +trap "rm -f $$.h" 0 2 3 + +PREFIX=${PREFIX-$HOME} +USEQBE=${USEQBE:-1} + +echo $@ | +(IFS=- read arch abi sys format r +echo \#define PREFIX \"$PREFIX\" +echo \#define USEQBE $USEQBE +echo \#define ARCH \"$arch\" +echo \#define SYS \"$sys\" +echo \#define ABI \"$abi\" +echo \#define FORMAT \"$format\") > $$.h && mv $$.h config.h diff --git a/driver/posix/scc.c b/driver/posix/scc.c @@ -14,6 +14,7 @@ static char sccsid[] = "@(#) ./driver/posix/scc.c"; #include <stdlib.h> #include <string.h> +#include "config.h" #include "../../inc/arg.h" #include "../../inc/cc.h" #include "../../inc/syslibs.h" @@ -53,11 +54,12 @@ static struct tool { }; char *argv0; -static char *arch, *sys, *execpath, *objfile, *outfile; +static char *arch, *sys, *abi, *format; +static char *execpath, *objfile, *outfile; static char *tmpdir; static size_t tmpdirln; static struct items objtmp, objout; -static int Mflag, Eflag, Sflag, cflag, dflag, kflag, sflag; +static int Mflag, Eflag, Sflag, cflag, dflag, kflag, sflag, Qflag = USEQBE; static int devnullfd; extern int failure; @@ -99,7 +101,7 @@ static int inittool(int tool) { struct tool *t = &tools[tool]; - char *crt; + char *crt, *fmt; int n; if (t->init) @@ -108,7 +110,8 @@ inittool(int tool) switch (tool) { case CC1: /* FALLTHROUGH */ case CC2: - n = snprintf(t->bin, sizeof(t->bin), "%s-%s", t->cmd, arch); + fmt = (Qflag && tool == CC2) ? "%s-qbe_%s-%s" : "%s-%s-%s"; + n = snprintf(t->bin, sizeof(t->bin), fmt, t->cmd, arch, abi); if (n < 0 || n >= sizeof(t->bin)) die("scc: target tool name too long"); @@ -359,10 +362,10 @@ buildfile(char *file, int tool) nexttool = CC2; break; case CC2: - if (!arch || strcmp(arch, "qbe")) - nexttool = (Sflag || kflag) ? TEEAS : AS; - else + if (Qflag) nexttool = kflag ? TEEQBE : QBE; + else + nexttool = (Sflag || kflag) ? TEEAS : AS; break; case TEEQBE: nexttool = QBE; @@ -445,6 +448,10 @@ main(int argc, char *argv[]) arch = ARCH; if (!(sys = getenv("SYS"))) sys = SYS; + if (!(abi = getenv("ABI"))) + abi = ABI; + if (!(format = getenv("FORMAT"))) + format = FORMAT; if (!(execpath = getenv("SCCEXECPATH"))) execpath = PREFIX "/libexec/scc"; @@ -513,6 +520,12 @@ main(int argc, char *argv[]) case 'w': addarg(CC1, "-w"); break; + case 'q': + Qflag = 0; + break; + case 'Q': + Qflag = 1; + break; case '-': fprintf(stderr, "scc: ignored parameter --%s\n", EARGF(usage())); diff --git a/inc/Makefile b/inc/Makefile @@ -0,0 +1,32 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../config.mk + +HDR = ldflags.h sysincludes.h syslibs.h + +all: + +dep: $(HDR) + +ldflags.h: + rm -f $@ ; \ + trap "rm -f $$$$.h" 0 2 3; \ + cat < ldflags.def.h > $$$$.h; \ + mv $$$$.h $@ + +sysincludes.h: + rm -f $@ ; \ + trap "rm -f $$$$.h" 0 2 3; \ + sed 's,PREFIX,"$(PREFIX)",g' < sysincludes.def.h > $$$$.h && \ + mv $$$$.h $@ + +syslibs.h: + rm -f $@ ; \ + trap "rm -f $$$$.h" 0 2 3; \ + sed 's,PREFIX,"$(PREFIX)",g' < syslibs.def.h > $$$$.h && \ + mv $$$$.h $@ + +clean: +distclean: + rm -f $(HDR) diff --git a/inc/cc.h b/inc/cc.h @@ -11,10 +11,6 @@ extern int debug; #define DBGON() #endif -#ifndef PREFIX -#define PREFIX "/usr/local/" -#endif - #define TINT long long #define TUINT unsigned long long #define TFLOAT double diff --git a/inc/sysincludes.def.h b/inc/sysincludes.def.h @@ -1,6 +1,6 @@ char *sysincludes[] = { PREFIX "/include/scc/" , - PREFIX "/include/scc/bits/" ARCH "/", + PREFIX "/include/scc/bits/" TARGET "/", /* configure below your standard sys include paths */ PREFIX "/include/", PREFIX "/local/include/", diff --git a/lib/Makefile b/lib/Makefile @@ -1,15 +1,15 @@ # See LICENSE file for copyright and license details. .POSIX: -include ../config.mk - -OBJS = debug.o die.o newitem.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o - -all: libcc.a +include ../config.mk -libcc.a: $(OBJS) - ar $(ARFLAGS) $@ $? - ranlib $@ +DIRS = cc -clean: - rm -f *.o *.a +all dep clean distclean: + @pwd=$$PWD; \ + for i in $(DIRS); \ + do \ + cd $$i; \ + $(MAKE) $@; \ + cd $$pwd; \ + done diff --git a/lib/c/Makefile b/lib/c/Makefile @@ -0,0 +1,7 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../config.mk + +all dep clean distclean: + cd src && CC=scc $(MAKE) -e $@ diff --git a/lib/c/amd64-sysv-linux/Makefile b/lib/c/amd64-sysv-linux/Makefile @@ -0,0 +1,6 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../../config.mk +include ../common.mk + diff --git a/lib/c/common.mk b/lib/c/common.mk @@ -0,0 +1,19 @@ +# See LICENSE file for copyright and license details. + +SRC=$(OBJ:.o=.c) + +all: libc.a + +libc.a: $(OBJ) + $(AR) $(ARFLAGS) $@ $? + ranlib $@ + +dep: + ../gendep.sh + +clean: + rm -f $(OBJ) + +distclean: clean + rm -f $(SRC) + rm -f makefile diff --git a/lib/c/gendep.sh b/lib/c/gendep.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +sed 's/\.o/.c/' ../obj.lst | +while read src +do + echo '#include "../src/'$src'"' > $src +done + +(cat Makefile +echo +printf "OBJ =" +while read i +do + printf " %s" $i +done < ../obj.lst +echo) > makefile + diff --git a/lib/c/include/assert.h b/lib/c/include/assert.h @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _ASSERT_H +#define _ASSERT_H + +void __assert(char *exp, char *file, long line); + +#ifndef NDEBUG +# define assert(exp) ((exp) ? (void) 0 : __assert(#exp, __FILE__, __LINE__)) +#else +# define assert(exp) ((void)0) +#endif + +#endif diff --git a/lib/c/include/bits/amd64-sysv/arch/limits.h b/lib/c/include/bits/amd64-sysv/arch/limits.h @@ -0,0 +1,17 @@ +#define SCHAR_MAX 0x7F +#define SCHAR_MIN (-SCHAR_MIN-1) +#define CHAR_MAX 0x7F +#define CHAR_MIN (-CHAR_MAX-1) +#define UCHAR_MAX 0xFF +#define SHRT_MAX 0x7FFF +#define SHRT_MIN (-SHRT_MAX-1) +#define USHRT_MAX 0xFFFF +#define INT_MAX 0x7FFFFFFF +#define INT_MIN (-INT_MAX-1) +#define UINT_MAX 0xFFFFFFFF +#define LONG_MAX 0x7FFFFFFFFFFFFFFF +#define LONG_MIN (-LONG_MAX-1) +#define ULONG_MAX 0xFFFFFFFFFFFFFFFF +#define LLONG_MAX 0x7FFFFFFFFFFFFFFF +#define LLONG_MIN (-LLONG_MAX-1) +#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/lib/c/include/bits/amd64-sysv/arch/signal.h b/lib/c/include/bits/amd64-sysv/arch/signal.h @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ + +typedef int sig_atomic_t; + +#define SIG_ERR -1 +#define SIG_DFL 0 +#define SIG_IGN 1 + +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGABRT 6 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGCHLD 17 +#define SIGCONT 18 +#define SIGSTOP 19 +#define SIGSSTP 20 +#define SIGTTIN 21 +#define SIGTTOU 22 + +#define __NR_SIGNALS 23 diff --git a/lib/c/include/bits/amd64-sysv/arch/stddef.h b/lib/c/include/bits/amd64-sysv/arch/stddef.h @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +#ifndef _WCHAR_T +typedef int wchar_t; +#define _WCHAR_T +#endif + +#ifndef _PTRDIFF_T +typedef long ptrdiff_t; +#define _PTRDIFF_T +#endif diff --git a/lib/c/include/bits/amd64-sysv/arch/stdint.h b/lib/c/include/bits/amd64-sysv/arch/stdint.h @@ -0,0 +1,117 @@ +/* See LICENSE file for copyright and license details. */ + +#define INT8_MAX 0x7F +#define INT8_MIN (-INT8_MAX-1) +#define UINT8_MAX 0xFF + +#define INT16_MAX 0x7FFF +#define INT16_MIN (-INT16_MAX-1) +#define UINT16_MAX 0xFFFF + +#define INT32_MAX 0x7FFFFFFF +#define INT32_MIN (-INT32_MAX-1) +#define UINT32_MAX 0xFFFFFFFF + +#define INT64_MAX 0x7FFFFFFFFFFFFFFF +#define INT64_MIN (-INT64_MAX-1) +#define UINT64_MAX 0xFFFFFFFFFFFFFFFF + +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define UINT_LEAST8_MAX UINT8_MAX + +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define UINT_LEAST16_MAX UINT16_MAX + +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define UINT_LEAST32_MAX UINT32_MAX + +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +#define INT_FAST8_MIN INT32_MIN +#define INT_FAST8_MAX INT32_MAX +#define UINT_FAST8_MAX UINT32_MAX + +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST16_MAX INT32_MAX +#define UINT_FAST16_MAX UINT32_MAX + +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define UINT_FAST32_MAX UINT32_MAX + +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST64_MAX UINT64_MAX + +#define INTPTR_MIN INT64_MIN +#define INTPTR_MAX INT64_MAX +#define UINTPTR_MAX UINT64_MAX + +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +#define PTRDIFF_MIN INT64_MIN +#define PTRDIFF_MAX INT64_MAX + +#define SIG_ATOMIC_MIN INT32_MIN +#define SIG_ATOMIC_MAX INT32_MAX + +#define SIZE_MAX UINT64_MAX + +#define WCHAR_MIN INT32_MIN +#define WCHAR_MAX INT32_MAX + +#define INT8_C(x) x +#define INT16_C(x) x +#define INT32_C(x) x +#define INT64_C(x) x ## L + +#define UINT8_C(x) x +#define UINT16_C(x) x +#define UINT32_C(x) x ## U +#define UINT64_C(x) x ## UL + +#define INTMAX_C(x) x ## L +#define UINTMAX_C(x) x ## UL + +typedef signed char int8_t; +typedef short int16_t; +typedef int int32_t; +typedef long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned uint32_t; +typedef unsigned long uint64_t; + +typedef signed char int8_least_t; +typedef short int16_least_t; +typedef int int32_least_t; +typedef long int64_least_t; + +typedef unsigned char uint8_least_t; +typedef unsigned short uint16_least_t; +typedef unsigned uint32_least_t; +typedef unsigned long uint64_least_t; + +typedef int int8_fast_t; +typedef int int16_fast_t; +typedef int int32_fast_t; +typedef long int64_fast_t; + +typedef unsigned uint8_fast_t; +typedef unsigned uint16_fast_t; +typedef unsigned uint32_fast_t; +typedef unsigned long uint64_fast_t; + +typedef long intptr_t; +typedef unsigned long uintptr_t; + +typedef long intmax_t; +typedef unsigned long uintmax_t; diff --git a/lib/c/include/bits/amd64-sysv/arch/stdio.h b/lib/c/include/bits/amd64-sysv/arch/stdio.h @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +#define BUFSIZ 512 +#define FILENAME_MAX 256 +#define FOPEN_MAX 16 + +#define TMP_MAX 25 +#define L_tmpnam 256 + +typedef int fpos_t; diff --git a/lib/c/include/bits/amd64-sysv/arch/stdlib.h b/lib/c/include/bits/amd64-sysv/arch/stdlib.h @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + +#ifndef _WCHAR_T +typedef int wchar_t; +#define _WCHAR_T +#endif + +#define _ALIGNTYPE long double diff --git a/lib/c/include/bits/amd64-sysv/arch/string.h b/lib/c/include/bits/amd64-sysv/arch/string.h @@ -0,0 +1,5 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#endif diff --git a/lib/c/include/bits/amd64-sysv/arch/time.h b/lib/c/include/bits/amd64-sysv/arch/time.h @@ -0,0 +1,8 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +typedef long int time_t; diff --git a/lib/c/include/bits/i386-sysv/arch/limits.h b/lib/c/include/bits/i386-sysv/arch/limits.h @@ -0,0 +1,17 @@ +#define SCHAR_MAX 0x7F +#define SCHAR_MIN (-SCHAR_MIN-1) +#define CHAR_MAX 0x7F +#define CHAR_MIN (-CHAR_MAX-1) +#define UCHAR_MAX 0xFF +#define SHRT_MAX 0x7FFF +#define SHRT_MIN (-SHRT_MAX-1) +#define USHRT_MAX 0xFFFF +#define INT_MAX 0x7FFFFFFF +#define INT_MIN (-INT_MAX-1) +#define UINT_MAX 0xFFFFFFFF +#define LONG_MAX 0x7FFFFFFF +#define LONG_MIN (-LONG_MAX-1) +#define ULONG_MAX 0xFFFFFFFF +#define LLONG_MAX 0x7FFFFFFFFFFFFFFF +#define LLONG_MIN (-LLONG_MAX-1) +#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/lib/c/include/bits/i386-sysv/arch/signal.h b/lib/c/include/bits/i386-sysv/arch/signal.h @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ + +typedef int sig_atomic_t; + +#define SIG_ERR -1 +#define SIG_DFL 0 +#define SIG_IGN 1 + +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGABRT 6 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGCHLD 17 +#define SIGCONT 18 +#define SIGSTOP 19 +#define SIGSSTP 20 +#define SIGTTIN 21 +#define SIGTTOU 22 + +#define __NR_SIGNALS 23 diff --git a/lib/c/include/bits/i386-sysv/arch/stddef.h b/lib/c/include/bits/i386-sysv/arch/stddef.h @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +#ifndef _WCHAR_T +typedef int wchar_t; +#define _WCHAR_T +#endif + +#ifndef _PTRDIFF_T +typedef long ptrdiff_t; +#define _PTRDIFF_T +#endif diff --git a/lib/c/include/bits/i386-sysv/arch/stdint.h b/lib/c/include/bits/i386-sysv/arch/stdint.h @@ -0,0 +1,117 @@ +/* See LICENSE file for copyright and license details. */ + +#define INT8_MAX 0x7F +#define INT8_MIN (-INT8_MAX-1) +#define UINT8_MAX 0xFF + +#define INT16_MAX 0x7FFF +#define INT16_MIN (-INT16_MAX-1) +#define UINT16_MAX 0xFFFF + +#define INT32_MAX 0x7FFFFFFF +#define INT32_MIN (-INT32_MAX-1) +#define UINT32_MAX 0xFFFFFFFF + +#define INT64_MAX 0x7FFFFFFFFFFFFFFF +#define INT64_MIN (-INT64_MAX-1) +#define UINT64_MAX 0xFFFFFFFFFFFFFFFF + +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define UINT_LEAST8_MAX UINT8_MAX + +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define UINT_LEAST16_MAX UINT16_MAX + +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define UINT_LEAST32_MAX UINT32_MAX + +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +#define INT_FAST8_MIN INT32_MIN +#define INT_FAST8_MAX INT32_MAX +#define UINT_FAST8_MAX UINT32_MAX + +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST16_MAX INT32_MAX +#define UINT_FAST16_MAX UINT32_MAX + +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define UINT_FAST32_MAX UINT32_MAX + +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST64_MAX UINT64_MAX + +#define INTPTR_MIN INT32_MIN +#define INTPTR_MAX INT32_MAX +#define UINTPTR_MAX UINT32_MAX + +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +#define PTRDIFF_MIN INT32_MIN +#define PTRDIFF_MAX INT32_MAX + +#define SIG_ATOMIC_MIN INT32_MIN +#define SIG_ATOMIC_MAX INT32_MAX + +#define SIZE_MAX UINT32_MAX + +#define WCHAR_MIN INT32_MIN +#define WCHAR_MAX INT32_MAX + +#define INT8_C(x) x +#define INT16_C(x) x +#define INT32_C(x) x +#define INT64_C(x) x ## LL + +#define UINT8_C(x) x +#define UINT16_C(x) x +#define UINT32_C(x) x ## U +#define UINT64_C(x) x ## ULL + +#define INTMAX_C(x) x ## LL +#define UINTMAX_C(x) x ## ULL + +typedef signed char int8_t; +typedef short int16_t; +typedef int int32_t; +typedef long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned uint32_t; +typedef unsigned long long uint64_t; + +typedef signed char int8_least_t; +typedef short int16_least_t; +typedef int int32_least_t; +typedef long long int64_least_t; + +typedef unsigned char uint8_least_t; +typedef unsigned short uint16_least_t; +typedef unsigned uint32_least_t; +typedef unsigned long long uint64_least_t; + +typedef int int8_fast_t; +typedef int int16_fast_t; +typedef int int32_fast_t; +typedef long long int64_fast_t; + +typedef unsigned uint8_fast_t; +typedef unsigned uint16_fast_t; +typedef unsigned uint32_fast_t; +typedef unsigned long long uint64_fast_t; + +typedef int intptr_t; +typedef unsigned uintptr_t; + +typedef long long intmax_t; +typedef unsigned long long uintmax_t; diff --git a/lib/c/include/bits/i386-sysv/arch/stdio.h b/lib/c/include/bits/i386-sysv/arch/stdio.h @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +#define BUFSIZ 512 +#define FILENAME_MAX 256 +#define FOPEN_MAX 16 + +#define TMP_MAX 25 +#define L_tmpnam 256 + +typedef long fpos_t; diff --git a/lib/c/include/bits/i386-sysv/arch/stdlib.h b/lib/c/include/bits/i386-sysv/arch/stdlib.h @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + +#ifndef _WCHAR_T +typedef int wchar_t; +#define _WCHAR_T +#endif + +#define _ALIGNTYPE long double diff --git a/lib/c/include/bits/i386-sysv/arch/string.h b/lib/c/include/bits/i386-sysv/arch/string.h @@ -0,0 +1,5 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#endif diff --git a/lib/c/include/bits/i386-sysv/arch/time.h b/lib/c/include/bits/i386-sysv/arch/time.h @@ -0,0 +1,8 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned long size_t; +#define _SIZET +#endif + +typedef long int time_t; diff --git a/lib/c/include/bits/z80-dos/arch/limits.h b/lib/c/include/bits/z80-dos/arch/limits.h @@ -0,0 +1,17 @@ +#define SCHAR_MAX 0x7F +#define SCHAR_MIN (-SCHAR_MIN-1) +#define CHAR_MAX 0xFF +#define CHAR_MIN 0 +#define UCHAR_MAX 0xFF +#define SHRT_MAX 0x7FFF +#define SHRT_MIN (-SHRT_MAX-1) +#define USHRT_MAX 0xFFFF +#define INT_MAX 0x7FFF +#define INT_MIN (-INT_MAX-1) +#define UINT_MAX 0xFFFF +#define LONG_MAX 0x7FFFFFFF +#define LONG_MIN (-LONG_MAX-1) +#define ULONG_MAX 0xFFFFFFFF +#define LLONG_MAX 0x7FFFFFFFFFFFFFFF +#define LLONG_MIN (-LLONG_MAX-1) +#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/lib/c/include/bits/z80-dos/arch/signal.h b/lib/c/include/bits/z80-dos/arch/signal.h @@ -0,0 +1,33 @@ +/* See LICENSE file for copyright and license details. */ + +typedef char sig_atomic_t; + +#define SIG_ERR -1 +#define SIG_DFL 0 +#define SIG_IGN 1 + +#define SIG_ERR -1 +#define SIG_DFL 0 +#define SIG_IGN 1 + +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGABRT 6 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGCHLD 17 +#define SIGCONT 18 +#define SIGSTOP 19 +#define SIGSSTP 20 +#define SIGTTIN 21 +#define SIGTTOU 22 + +#define __NR_SIGNALS 23 diff --git a/lib/c/include/bits/z80-dos/arch/stddef.h b/lib/c/include/bits/z80-dos/arch/stddef.h @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned size_t; +#define _SIZET +#endif + +#ifndef _WCHAR_T +typedef short wchar_t; +#define _WCHAR_T +#endif + +#ifndef _PTRDIFF_T +typedef short ptrdiff_t; +#define _PTRDIFF_T +#endif diff --git a/lib/c/include/bits/z80-dos/arch/stdint.h b/lib/c/include/bits/z80-dos/arch/stdint.h @@ -0,0 +1,117 @@ +/* See LICENSE file for copyright and license details. */ + +#define INT8_MAX 0x7F +#define INT8_MIN (-INT8_MAX-1) +#define UINT8_MAX 0xFF + +#define INT16_MAX 0x7FFF +#define INT16_MIN (-INT16_MAX-1) +#define UINT16_MAX 0xFFFF + +#define INT32_MAX 0x7FFFFFFF +#define INT32_MIN (-INT32_MAX-1) +#define UINT32_MAX 0xFFFFFFFF + +#define INT64_MAX 0x7FFFFFFFFFFFFFFF +#define INT64_MIN (-INT64_MAX-1) +#define UINT64_MAX 0xFFFFFFFFFFFFFFFF + +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define UINT_LEAST8_MAX UINT8_MAX + +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define UINT_LEAST16_MAX UINT16_MAX + +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define UINT_LEAST32_MAX UINT32_MAX + +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +#define INT_FAST8_MIN INT16_MIN +#define INT_FAST8_MAX INT16_MAX +#define UINT_FAST8_MAX UINT16_MAX + +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST16_MAX INT16_MAX +#define UINT_FAST16_MAX UINT16_MAX + +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define UINT_FAST32_MAX UINT32_MAX + +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST64_MAX UINT64_MAX + +#define INTPTR_MIN INT16_MIN +#define INTPTR_MAX INT16_MAX +#define UINTPTR_MAX UINT16_MAX + +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +#define PTRDIFF_MIN INT16_MIN +#define PTRDIFF_MAX INT16_MAX + +#define SIG_ATOMIC_MIN INT16_MIN +#define SIG_ATOMIC_MAX INT16_MAX + +#define SIZE_MAX UINT64_MAX + +#define WCHAR_MIN INT16_MIN +#define WCHAR_MAX INT16_MAX + +#define INT8_C(x) x +#define INT16_C(x) x +#define INT32_C(x) x ## L +#define INT64_C(x) x ## LL + +#define UINT8_C(x) x +#define UINT16_C(x) x ## U +#define UINT32_C(x) x ## UL +#define UINT64_C(x) x ## ULL + +#define INTMAX_C(x) x ## LL +#define UINTMAX_C(x) x ## ULL + +typedef signed char int8_t; +typedef int int16_t; +typedef long int32_t; +typedef long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned uint16_t; +typedef unsigned long uint32_t; +typedef unsigned long long uint64_t; + +typedef signed char int8_least_t; +typedef int int16_least_t; +typedef long int32_least_t; +typedef long long int64_least_t; + +typedef unsigned char uint8_least_t; +typedef unsigned uint16_least_t; +typedef unsigned long uint32_least_t; +typedef unsigned long long uint64_least_t; + +typedef signed char int8_fast_t; +typedef int int16_fast_t; +typedef long int32_fast_t; +typedef long long int64_fast_t; + +typedef unsigned char uint8_fast_t; +typedef unsigned uint16_fast_t; +typedef unsigned long uint32_fast_t; +typedef unsigned long long uint64_fast_t; + +typedef int intptr_t; +typedef unsigned uintptr_t; + +typedef long long intmax_t; +typedef unsigned long long uintmax_t; diff --git a/lib/c/include/bits/z80-dos/arch/stdio.h b/lib/c/include/bits/z80-dos/arch/stdio.h @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned size_t; +#define _SIZET +#endif + +#define BUFSIZ 512 +#define FILENAME_MAX 256 +#define FOPEN_MAX 16 + +#define TMP_MAX 25 +#define L_tmpnam 256 + +typedef long fpos_t; diff --git a/lib/c/include/bits/z80-dos/arch/stdlib.h b/lib/c/include/bits/z80-dos/arch/stdlib.h @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned size_t; +#define _SIZET +#endif + +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + +#ifndef _WCHAR_T +typedef short wchar_t; +#define _WCHAR_T +#endif + +#define _ALIGNTYPE int diff --git a/lib/c/include/bits/z80-dos/arch/string.h b/lib/c/include/bits/z80-dos/arch/string.h @@ -0,0 +1,5 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned size_t; +#endif diff --git a/lib/c/include/bits/z80-dos/arch/time.h b/lib/c/include/bits/z80-dos/arch/time.h @@ -0,0 +1,8 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef _SIZET +typedef unsigned size_t; +#define _SIZET +#endif + +typedef long time_t; diff --git a/lib/c/include/ctype.h b/lib/c/include/ctype.h @@ -0,0 +1,49 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _CTYPE_H +#define _CTYPE_H + +extern int isalnum(int c); +extern int isalpha(int c); +extern int islower(int c); +extern int isupper(int c); +extern int isdigit(int c); +extern int isxdigit(int c); +extern int iscntrl(int c); +extern int isgraph(int c); +extern int isspace(int c); +extern int isblank(int c); +extern int isprint(int c); +extern int ispunct(int c); +extern int tolower(int c); +extern int toupper(int c); + +#ifdef __USE_MACROS + +#define _U 0x01 /* upper */ +#define _L 0x02 /* lower */ +#define _D 0x04 /* digit */ +#define _C 0x08 /* cntrl */ +#define _P 0x10 /* punct */ +#define _S 0x20 /* white space (space/lf/tab) */ +#define _X 0x40 /* hex char */ +#define _SP 0x80 /* hard space (0x20) */ + +extern unsigned char __ctype[]; + +#define isalnum(c) (__ctype[(unsigned char) c] & (_U|_L|_D)) +#define isalpha(c) (__ctype[(unsigned char) c] & (_U|_L)) +#define iscntrl(c) (__ctype[(unsigned char) c] & (_C)) +#define isdigit(c) (__ctype[(unsigned char) c] & (_D)) +#define isgraph(c) (__ctype[(unsigned char) c] & (_P|_U|_L|_D)) +#define islower(c) (__ctype[(unsigned char) c] & (_L)) +#define isprint(c) (__ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP)) +#define ispunct(c) (__ctype[(unsigned char) c] & (_P)) +#define isspace(c) (__ctype[(unsigned char) c] & (_S)) +#define isupper(c) (__ctype[(unsigned char) c] & (_U)) +#define isxdigit(c) (__ctype[(unsigned char) c] & (_D|_X)) + +#define isascii(c) (((unsigned) c)<=0x7f) + +#endif + +#endif diff --git a/lib/c/include/errno.h b/lib/c/include/errno.h @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _ERRNO_H +#define _ERRNO_H + +#define EDOM 1 +#define EILSEQ 2 +#define ERANGE 3 +#define ENOMEN 4 + +extern int errno; + +#endif diff --git a/lib/c/include/limits.h b/lib/c/include/limits.h @@ -0,0 +1,9 @@ +#ifndef _LIMITS_H +#define _LIMITS_H + +#include <arch/limits.h> + +#define CHAR_BIT 8 +#define MB_LEN_MAX 1 + +#endif diff --git a/lib/c/include/locale.h b/lib/c/include/locale.h @@ -0,0 +1,45 @@ +#ifndef _LOCALE_H +#define _LOCALE_H + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#define LC_ALL 0 +#define LC_COLLATE 1 +#define LC_CTYPE 2 +#define LC_MONETARY 3 +#define LC_NUMERIC 4 +#define LC_TIME 5 + +struct lconv { + char *decimal_point; + char *thousands_sep; + char *grouping; + char *mon_decimal_point; + char *mon_thousands_sep; + char *mon_grouping; + char *positive_sign; + char *negative_sign; + char *currency_symbol; + char *int_curr_symbol; + char frac_digits; + char p_cs_precedes; + char n_cs_precedes; + char p_sep_by_space; + char n_sep_by_space; + char p_sign_posn; + char n_sign_posn; + char int_frac_digits; + char int_p_cs_precedes; + char int_n_cs_precedes; + char int_p_sep_by_space; + char int_n_sep_by_space; + char int_p_sign_posn; + char int_n_sign_posn; +}; + +extern char *setlocale(int category, const char *locale); +extern struct lconv *localeconv(void); + +#endif diff --git a/lib/c/include/signal.h b/lib/c/include/signal.h @@ -0,0 +1,10 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _SIGNAL_H +#define _SIGNAL_H + +#include <arch/signal.h> + +void ( *signal(int signum, void (*handler)(int)) ) (int); +int raise(int sig); + +#endif diff --git a/lib/c/include/stdarg.h b/lib/c/include/stdarg.h @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STDARG_H +#define _STDARG_H + +#define va_list __builtin_va_list +#define va_start(ap, last) __builtin_va_start(ap, last) +#define va_end(ap) __builtin_va_end(ap) +#define va_copy(to, from) __builtin_va_copy(to, from) +#define va_arg(to, type) __builtin_va_arg(to, type) + +#endif diff --git a/lib/c/include/stdbool.h b/lib/c/include/stdbool.h @@ -0,0 +1,10 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +#define bool _Bool +#define true ((bool) 1) +#define false ((bool) 0) +#define __bool_true_false_are_defined 1 + +#endif diff --git a/lib/c/include/stddef.h b/lib/c/include/stddef.h @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STDDEF_H +#define _STDDEF_H + +#include <arch/stddef.h> + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#define offsetof(st, m) ((size_t)&(((st *)0)->m)) + +#endif diff --git a/lib/c/include/stdint.h b/lib/c/include/stdint.h @@ -0,0 +1,7 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STDINT_H_ +#define _STDINT_H_ + +#include <arch/stdint.h> + +#endif diff --git a/lib/c/include/stdio.h b/lib/c/include/stdio.h @@ -0,0 +1,102 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STDIO_H +#define _STDIO_H + +#include <arch/stdio.h> + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#define EOF -1 +#define _IOFBF 0 +#define _IOLBF 1 +#define _IONBF 2 +#define SEEK_CUR 0 +#define SEEK_END 1 +#define SEEK_SET 2 + +typedef struct { + int fd; /* file descriptor */ + char flags; /* bits for must free buffer on close, line-buffered */ + char state; /* last operation was read, write, position, error, eof */ + char *buf; /* pointer to i/o buffer */ + char *rp; /* read pointer (or write end-of-buffer) */ + char *wp; /* write pointer (or read end-of-buffer) */ + char *lp; /* actual write pointer used when line-buffering */ + size_t len; /* actual length of buffer */ + char unbuf[1]; /* tiny buffer for unbuffered io */ +} FILE; + +extern FILE _IO_stream[FOPEN_MAX]; + +#define stderr (&_IO_stream[2]) +#define stdin (&_IO_stream[0]) +#define stdout (&_IO_stream[1]) + +extern int remove(const char *filename); +extern int rename(const char *old, const char *new); +extern FILE *tmpfile(void); +extern char *tmpnam(char *s); +extern int fclose(FILE *fp); +extern int fflush(FILE *fp); +extern FILE *fopen(const char * restrict fname, const char * restrict mode); +extern FILE *freopen(const char * restrict fname, const char * restrict mode, + FILE * restrict fp); +extern void setbuf(FILE * restrict fp, char * restrict buf); +extern int setvbuf(FILE * restrict fp, + char * restrict buf, int mode, size_t size); +extern int fprintf(FILE * restrict fp, const char * restrict fmt, ...); +extern int fscanf(FILE * restrict fp, const char * restrict fmt, ...); +extern int printf(const char * restrict fmt, ...); +extern int scanf(const char * restrict fmt, ...); +extern int snprintf(char * restrict s, + size_t n, const char * restrict fmt, ...); +extern int sprintf(char * restrict s, const char * restrict fmt, ...); +extern int sscanf(const char * restrict s, const char * restrict fmt, ...); + +#ifdef _STDARG_H +extern int vfprintf(FILE * restrict fp, + const char * restrict fmt, va_list arg); +extern int vfscanf(FILE * restrict fp, + const char * restrict fmt, va_list arg); +extern int vprintf(const char * restrict fmt, va_list arg); +extern int vscanf(const char * restrict fmt, va_list arg); +extern int vsnprintf(char * restrict s, size_t n, const char * restrict fmt, + va_list arg); +extern int vsprintf(char * restrict s, + const char * restrict fmt, va_list arg); +extern int vsscanf(const char * restrict s, + const char * restrict fmt, va_list arg); +#endif + +extern int fgetc(FILE *fp); +extern char *fgets(char * restrict s, int n, FILE * restrict fp); +extern int fputc(int c, FILE *fp); +extern int fputs(const char * restrict s, FILE * restrict fp); +extern int getc(FILE *fp); +extern int getchar(void); +extern char *gets(char *s); +extern int putc(int c, FILE *fp); +extern int putchar(int c); +extern int puts(const char *s); +extern int ungetc(int c, FILE *fp); +extern size_t fread(void * restrict ptr, size_t size, size_t nmemb, + FILE * restrict fp); +extern size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, + FILE * restrict fp); +extern int fgetpos(FILE * restrict fp, fpos_t * restrict pos); +extern int fseek(FILE *fp, long int offset, int whence); +extern int fsetpos(FILE *fp, const fpos_t *pos); +extern long int ftell(FILE *fp); +extern void rewind(FILE *fp); +extern void clearerr(FILE *fp); +extern int feof(FILE *fp); +extern int ferror(FILE *fp); +extern void perror(const char *s); + +#ifdef __USE_MACROS +#define printf(...) fprintf(stdout, __VA_ARGS__) +#endif + +#endif diff --git a/lib/c/include/stdlib.h b/lib/c/include/stdlib.h @@ -0,0 +1,70 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STDLIB_H +#define _STDLIB_H + +#include <arch/stdlib.h> + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#define _ATEXIT_MAX 32 + +#define MB_CUR_MAX 1 +#define RAND_MAX 32767 + +typedef struct { + int quot, rem; +} div_t; + +typedef struct { + long quot, rem; +} ldiv_t; + +typedef struct { + long long quot, rem; +} lldiv_t; + +extern double atof(const char *nptr); +extern int atoi(const char *nptr); +extern long int atol(const char *nptr); +extern long long int atoll(const char *nptr); +extern double strtod(const char * restrict nptr, char ** restrict endptr); +extern float strtof(const char * restrict nptr, char ** restrict endptr); +extern long double strtold(const char * restrict nptr, char ** restrict endptr); +extern long int strtol(const char * restrict nptr, char ** restrict endptr, int base); +extern long long int strtoll(const char * restrict nptr, char ** restrict endptr, + int base); +extern unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, + int base); +extern unsigned long long int strtoull(const char * restrict nptr, + char ** restrict endptr, int base); +extern int rand(void); +extern void srand(unsigned int seed); +extern void *calloc(size_t nmemb, size_t size); +extern void free(void *ptr); +extern void *malloc(size_t size); +extern void *realloc(void *ptr, size_t size); +extern void abort(void); +extern int atexit(void (*func)(void)); +extern void exit(int status); +extern void _Exit(int status); +extern char *getenv(const char *name); +extern int system(const char *string); +extern void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)); +extern void qsort(void *base, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)); +extern int abs(int j); +extern long int labs(long int j); +extern long long int llabs(long long int j); +extern div_t div(int numer, int denom); +extern ldiv_t ldiv(long int numer, long int denom); +extern lldiv_t lldiv(long long int numer, long long int denom); +extern int mblen(const char *s, size_t n); +extern int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n); +extern int wctomb(char *s, wchar_t wchar); +extern size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); +extern size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); + +#endif diff --git a/lib/c/include/string.h b/lib/c/include/string.h @@ -0,0 +1,34 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef _STRING_H +#define _STRING_H + +#include <arch/string.h> + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +extern void *memcpy(void * restrict s1, const void * restrict s2, size_t n); +extern void *memmove(void *s1, const void *s2, size_t n); +extern char *strcpy(char * restrict s1, const char * restrict s2); +extern char *strncpy(char * restrict s1, const char * restrict s2, size_t n); +extern char *strcat(char * restrict s1, const char * restrict s2); +extern char *strncat(char * restrict s1, const char * restrict s2, size_t n); +extern int memcmp(const void *s1, const void *s2, size_t n); +extern int strcmp(const char *s1, const char *s2); +extern int strcoll(const char *s1, const char *s2); +extern int strncmp(const char *s1, const char *s2, size_t n); +extern size_t strxfrm(char * restrict s1, const char * restrict s2, size_t n); +extern void *memchr(const void *s, int c, size_t n); +extern char *strchr(const char *s, int c); +extern size_t strcspn(const char *s1, const char *s2); +extern char *strpbrk(const char *s1, const char *s2); +extern char *strrchr(const char *s, int c); +extern size_t strspn(const char *s1, const char *s2); +extern char *strstr(const char *s1, const char *s2); +extern char *strtok(char * restrict s1, const char * restrict s2); +extern void *memset(void *s, int c, size_t n); +extern char *strerror(int errnum); +extern size_t strlen(const char *s); + +#endif diff --git a/lib/c/include/time.h b/lib/c/include/time.h @@ -0,0 +1,38 @@ +#ifndef _TIME_H +#define _TIME_H + +#include <arch/time.h> + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#define CLOCKS_PER_SEC 1000000 + +typedef long int clock_t; + +struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mday; + int tm_mon; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; +}; + +extern clock_t clock(void); +extern double difftime(time_t time1, time_t time0); +extern time_t mktime(struct tm *timeptr); +extern time_t time(time_t *timer); +extern char *asctime(const struct tm *timeptr); +extern char *ctime(const time_t *timer); +extern struct tm *gmtime(const time_t *timer); +extern struct tm *localtime(const time_t *timer); +extern size_t strftime(char * restrict s, size_t maxsize, + const char * restrict format, + const struct tm * restrict timeptr); + +#endif diff --git a/lib/c/obj.lst b/lib/c/obj.lst @@ -0,0 +1,49 @@ +assert.o +strcpy.o +strcmp.o +strlen.o +strchr.o +strrchr.o +strcat.o +strncmp.o +strncpy.o +strncat.o +strcoll.o +strxfrm.o +strstr.o +strspn.o +strcspn.o +strpbrk.o +strtok.o +memset.o +memcpy.o +memmove.o +memcmp.o +memchr.o +isalnum.o +isalpha.o +isascii.o +isblank.o +iscntrl.o +isdigit.o +isgraph.o +islower.o +isprint.o +ispunct.o +isspace.o +isupper.o +isxdigit.o +toupper.o +tolower.o +ctype.o +setlocale.o +localeconv.o +atoi.o +atexit.o +exit.o +printf.o +fprintf.o +vfprintf.o +realloc.o +calloc.o +malloc.o diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -0,0 +1,24 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../../config.mk + +OBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \ + strrchr.o strcat.o strncmp.o strncpy.o strncat.o strcoll.o \ + strxfrm.o strstr.o strspn.o strcspn.o strpbrk.o strtok.o \ + memset.o memcpy.o memmove.o memcmp.o memchr.o \ + isalnum.o isalpha.o isascii.o isblank.o iscntrl.o isdigit.o \ + isgraph.o islower.o isprint.o ispunct.o isspace.o isupper.o \ + isxdigit.o toupper.o tolower.o ctype.o setlocale.o \ + localeconv.o atoi.o atexit.o exit.o \ + printf.o fprintf.o vfprintf.o \ + realloc.o calloc.o malloc.o + +all: $(ARCH)-libc.a + +clean distclean: + rm -f *.o *-libc.a + +$(ARCH)-libc.a: $(OBJ) + $(AR) $(ARFLAGS) $@ $? + ranlib $@ diff --git a/lib/c/src/assert.c b/lib/c/src/assert.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#include <assert.h> +#include <stdlib.h> +#include <stdio.h> + +void __assert(char *exp, char *file, long line) +{ + fprintf(stderr, "%s:%ld: assertion failed '%s'\n", file, line, exp); + abort(); +} diff --git a/lib/c/src/atexit.c b/lib/c/src/atexit.c @@ -0,0 +1,19 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#include <errno.h> +#undef atexit + +extern void (*_exitf[_ATEXIT_MAX])(void); +extern unsigned _exitn; + +int +atexit(void (*fun)(void)) +{ + if (_exitn == _ATEXIT_MAX) { + errno = ENOMEN; + return -1; + } + _exitf[_exitn++] = fun; + return 0; +} diff --git a/lib/c/src/atoi.c b/lib/c/src/atoi.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ + +#include <ctype.h> +#include <stdlib.h> +#undef atoi + +int +atoi(const char *s) +{ + int n, sign = -1; + + while(isspace(*s)) + ++s; + + switch(*s) { + case '-': + sign = 1; + case '+': + ++s; + } + + /* Compute n as a negative number to avoid overflow on INT_MIN */ + for (n = 0; isdigit(*s); ++s) + n = 10*n - (*s - '0'); + + return sign * n; +} + diff --git a/lib/c/src/calloc.c b/lib/c/src/calloc.c @@ -0,0 +1,19 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#include <string.h> + +void * +calloc(size_t nmemb, size_t size) +{ + size_t nbytes; + void *mem; + + if (!nmemb || !size || nmemb > (size_t)-1/size) + return NULL; + + nbytes = nmemb * size; + if ((mem = malloc(nbytes)) == NULL) + return NULL; + return memset(mem, 0, nbytes); +} diff --git a/lib/c/src/ctype.c b/lib/c/src/ctype.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef ctype + +unsigned char __ctype[255] = { + _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ + _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ + _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ + _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ + _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ + _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ + _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ + _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ + _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ + _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ + _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ + _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ + _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ + _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ + _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ + _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ +}; diff --git a/lib/c/src/exit.c b/lib/c/src/exit.c @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#undef exit + +void (*_exitf[_ATEXIT_MAX])(void); +unsigned _exitn; + +void +exit(int status) +{ + while (_exitn > 0) + (*_exitf[--_exitn])(); + _Exit(status); +} diff --git a/lib/c/src/fprintf.c b/lib/c/src/fprintf.c @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdarg.h> +#include <stdio.h> +#undef fprintf + +int +fprintf(FILE * restrict fp, const char * restrict fmt, ...) +{ + va_list va; + int cnt; + + va_start(va, fmt); + cnt = vfprintf(fp, fmt, va); + va_end(va); + return cnt; +} diff --git a/lib/c/src/isalnum.c b/lib/c/src/isalnum.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isalnum + +int +isalnum(int c) +{ + return __ctype[(unsigned char) c] & (_U|_L|_D); +} diff --git a/lib/c/src/isalpha.c b/lib/c/src/isalpha.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isalpha + +int +isalpha(int c) +{ + return __ctype[(unsigned char) c] & (_U|_L); +} diff --git a/lib/c/src/isascii.c b/lib/c/src/isascii.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isascii + +int +isascii(int c) +{ + return c <= 0x7f; +} diff --git a/lib/c/src/isblank.c b/lib/c/src/isblank.c @@ -0,0 +1,7 @@ +/* See LICENSE file for copyright and license details. */ + +int +isblank(int c) +{ + return (c == ' ') || (c == '\t'); +} diff --git a/lib/c/src/iscntrl.c b/lib/c/src/iscntrl.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef iscntrl + +int +iscntrl(int c) +{ + return __ctype[(unsigned char) c] & (_C); +} diff --git a/lib/c/src/isdigit.c b/lib/c/src/isdigit.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isdigit + +int +isdigit(int c) +{ + return __ctype[(unsigned char) c] & (_D); +} diff --git a/lib/c/src/isgraph.c b/lib/c/src/isgraph.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isgraph + +int +isgraph(int c) +{ + return __ctype[(unsigned char) c] & (_P|_U|_L|_D); +} diff --git a/lib/c/src/islower.c b/lib/c/src/islower.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef islower + +int +islower(int c) +{ + return __ctype[(unsigned char) c] & _L; +} diff --git a/lib/c/src/isprint.c b/lib/c/src/isprint.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isprint + +int +isprint(int c) +{ + return __ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP); +} diff --git a/lib/c/src/ispunct.c b/lib/c/src/ispunct.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef ispunct + +int +ispunct(int c) +{ + return __ctype[(unsigned char) c] & (_P); +} diff --git a/lib/c/src/isspace.c b/lib/c/src/isspace.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isspace + +int +isspace(int c) +{ + return __ctype[(unsigned char) c] & _S; +} diff --git a/lib/c/src/isupper.c b/lib/c/src/isupper.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isupper + +int +isupper(int c) +{ + return __ctype[(unsigned char) c] & _U; +} diff --git a/lib/c/src/isxdigit.c b/lib/c/src/isxdigit.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef isxdigit + +int +isxdigit(int c) +{ + return __ctype[(unsigned char) c] & (_D|_X); +} diff --git a/lib/c/src/localeconv.c b/lib/c/src/localeconv.c @@ -0,0 +1,14 @@ +#include <locale.h> +#include <limits.h> +#undef localeconv + +struct lconv * +localeconv(void) +{ + static struct lconv lc = { ".", "", "", "", "", "", "", "", "", "", + CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, + CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, + CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, + CHAR_MAX, CHAR_MAX }; + return &lc; +} diff --git a/lib/c/src/malloc.c b/lib/c/src/malloc.c @@ -0,0 +1,154 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "malloc.h" +#include "syscall.h" + +#define MAXADDR ((char *)-1) +#define ERRADDR ((char *)-1) + +extern char end[]; +static Header base = { .h.next = &base }; +static Header *freep = &base; +static char *heap = end; + +/* + * Run over the free list looking for the nearest previous + * block. There are two possible results: end of the list + * or an intermediary block. + */ +void * +_prevchunk(Header *hp) +{ + Header *p; + + for (p = freep; ;p = p->h.next) { + /* hp between p and p->h.next? */ + if (p < hp && hp < p->h.next) + break; + /* p before hp and hp at the end of list? */ + if (p->h.next <= p && (hp < p->h.next || hp > p)) + break; + } + return p; +} + +/* + * Get the previous block and try to merge + * with next and previous blocks + */ +void +free(void *mem) +{ + Header *hp, *prev; + + if (!mem) + return; + + hp = (Header *) mem - 1; + prev = _prevchunk(hp); + + /* join to next */ + if (hp + hp->h.size == prev->h.next) { + hp->h.size += prev->h.next->h.size; + hp->h.next = prev->h.next->h.next; + } else { + hp->h.next = prev->h.next; + } + + /* join to previous */ + if (prev + prev->h.size == hp) { + prev->h.size += hp->h.size; + prev->h.next = hp->h.next; + } else { + prev->h.next = hp; + } + + freep = prev; +} + +static void * +sbrk(uintptr_t inc) +{ + char *new, *old = heap; + + if (old >= MAXADDR - inc) + return ERRADDR; + + new = old + inc; + if (_brk(new) < 0) + return ERRADDR; + heap = new; + + return old; +} + +static Header * +morecore(size_t nunits) +{ + char *rawmem; + Header *hp; + + if (nunits < NALLOC) + nunits = NALLOC; + + rawmem = sbrk(nunits * sizeof(Header)); + if (rawmem == ERRADDR) + return NULL; + + hp = (Header*)rawmem; + hp->h.size = nunits; + + /* integrate new memory into the list */ + free(hp + 1); + + return freep; +} + +/* + * Run over the list of free blocks trying to find a block + * big enough for nbytes. If the block fit perfectly with + * the required size then we only have to unlink + * the block. Otherwise we have to split the block and + * return the right part. If we run over the full list + * without a fit then we have to require more memory + * + * ______________________________________ + * ___________./______________________________________\_____ + * ...| in | | | in | |.....| in | | | |.... + * ...| use | | | use | |.....| use | | | |.... + * ___|______|___|.____|_____|._|_____|______|._|.___|.|____ + * \__/ \_________/ \_____________/ \/ \__/ + */ +void * +malloc(size_t nbytes) +{ + Header *cur, *prev; + size_t nunits; + + /* 1 unit for header plus enough units to fit nbytes */ + nunits = (nbytes+sizeof(Header)-1) / sizeof(Header) + 1; + + for (prev = freep; ; prev = cur) { + cur = prev->h.next; + if (cur->h.size >= nunits) { + if (cur->h.size == nunits) { + prev->h.next = cur->h.next; + } else { + cur->h.size -= nunits; + cur += cur->h.size; + cur->h.size = nunits; + } + freep = prev; + return cur + 1; + } + + if (cur == freep) { + if ((cur = morecore(nunits)) == NULL) + return NULL; + } + } +} diff --git a/lib/c/src/malloc.h b/lib/c/src/malloc.h @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> + +/* minimum amount of required units */ +#define NALLOC 10000 + +typedef union header Header; +union header { + struct hdr { + Header *next; + size_t size; + } h; + /* most restrictive type fixes the union size for alignment */ + _ALIGNTYPE most; +}; + +extern void *_prevchunk(Header *hp); diff --git a/lib/c/src/memchr.c b/lib/c/src/memchr.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef memchr + +void * +memchr(const void *s, int c, size_t n) +{ + unsigned char *bp = (char *) s; + + while (n > 0 && *bp++ != c) + --n; + return (n == 0) ? NULL : bp-1; +} diff --git a/lib/c/src/memcmp.c b/lib/c/src/memcmp.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef memcmp + +int +memcmp(const void *s1, const void *s2, size_t n) +{ + char *s = (char *) s1, *t = (char *) s2; + + while (n > 0 && *s == *t) + --n, ++s, ++t; + return n ? (*s - *t) : 0; +} diff --git a/lib/c/src/memcpy.c b/lib/c/src/memcpy.c @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef memcpy + +void * +memcpy(void * restrict dst, const void * restrict src, size_t n) +{ + char *s1 = dst; + const char *s2 = src; + + while (n-- > 0) + *s1++ = *s2++; + return dst; +} diff --git a/lib/c/src/memmove.c b/lib/c/src/memmove.c @@ -0,0 +1,20 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef memmove + +void * +memmove(void *dst, const void *src, size_t n) +{ + char *d = dst, *s = (char *) src; + + if (d < s) { + while (n-- > 0) + *d++ = *s++; + } else { + s += n-1, d += n-1; + while (n-- > 0) + *d-- = *s--; + } + return dst; +} diff --git a/lib/c/src/memset.c b/lib/c/src/memset.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef memset + +void * +memset(void *s, int c, size_t n) +{ + char *m = s; + + while (n-- > 0) + *m++ = c; + return s; +} diff --git a/lib/c/src/printf.c b/lib/c/src/printf.c @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdarg.h> +#include <stdio.h> +#undef printf + +int +printf(const char * restrict fmt, ...) +{ + int cnt; + va_list va; + + va_start(va, fmt); + cnt = vfprintf(stdin, fmt, va); + va_end(va); + return cnt; +} diff --git a/lib/c/src/rand.c b/lib/c/src/rand.c @@ -0,0 +1,19 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#undef rand + +static int next; + +void +srand(unsigned seed) +{ + next = seed; +} + +int +rand(void) /* RAND_MAX assumed to be 32767. */ +{ + next = next * 1103515245 + 12345; + return (unsigned)(next/65536) % 32768; +} diff --git a/lib/c/src/realloc.c b/lib/c/src/realloc.c @@ -0,0 +1,69 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#include <string.h> + +#include "malloc.h" + +void * +realloc(void *ptr, size_t nbytes) +{ + Header *oh, *prev, *next, *new; + size_t nunits, avail, onbytes, n; + + if (!nbytes) + return NULL; + + if (!ptr) + return malloc(nbytes); + + nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1; + oh = (Header*)ptr - 1; + + if (oh->h.size == nunits) + return ptr; + + new = oh + nunits; + + if (nunits < oh->h.size - 1) { + new->h.size = oh->h.size - nunits; + oh->h.size = nunits; + free(new + 1); + return oh; + } + + prev = _prevchunk(oh); + + if (oh + oh->h.size == prev->h.next) { + /* + * if there is free space adjacent + * to the current memory + */ + next = prev->h.next; + avail = oh->h.size + next->h.size; + + if (avail == nunits) { + oh->h.size = nunits; + prev->h.next = next->h.next; + return oh; + } + + if (avail > nunits) { + oh->h.size = nunits; + prev->h.next = new; + new->h.next = next; + new->h.size = avail - nunits; + return oh; + } + } + + onbytes = (oh->h.size - 1) * sizeof(Header); + if ((new = malloc(nbytes)) == NULL) + return NULL; + + n = (onbytes > nbytes) ? nbytes : onbytes; + memcpy(new, ptr, n); + free(ptr); + + return new; +} diff --git a/lib/c/src/setlocale.c b/lib/c/src/setlocale.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ + +#include <locale.h> +#include <stddef.h> +#undef setlocale + +char * +setlocale(int category, const char *locale) +{ + if (category > LC_TIME || category < LC_ALL) + return NULL; + if (!locale || + locale[0] == '\0' || + locale[0] == 'C' && locale[1] == '\0') { + return "C"; + } + return NULL; +} diff --git a/lib/c/src/strcat.c b/lib/c/src/strcat.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strcat + +char * +strcat(char * restrict dst, const char * restrict src) +{ + char *ret = dst; + + while (*dst) + ++dst; + while (*dst++ = *src++) + /* nothing */; + return ret; +} diff --git a/lib/c/src/strchr.c b/lib/c/src/strchr.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strchr + +char * +strchr(const char *s, int c) +{ + while (*s && *s != c) + ++s; + return (*s == c) ? (char *)s : NULL; +} diff --git a/lib/c/src/strcmp.c b/lib/c/src/strcmp.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strcmp + +int +strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s2 && *s1 == *s2) + ++s1, ++s2; + return *(unsigned char *)s1 - *(unsigned char *)s2; +} diff --git a/lib/c/src/strcoll.c b/lib/c/src/strcoll.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strcoll + +int +strcoll(const char *s1, const char *s2) +{ + while (*s1 && *s2 && *s1 == *s2) + ++s1, ++s2; + return *(unsigned char *) s1 - *(unsigned char *) s2; +} diff --git a/lib/c/src/strcpy.c b/lib/c/src/strcpy.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strcpy + +char * +strcpy(char * restrict dst, const char * restrict src) +{ + char *ret = dst; + + while (*dst++ = *src++) + /* nothing */; + return ret; +} diff --git a/lib/c/src/strcspn.c b/lib/c/src/strcspn.c @@ -0,0 +1,20 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strcspn + +size_t +strcspn(const char *s1, const char *s2) +{ + size_t n; + int c; + const char *p; + + for (n = 0; c = *s1++; ++n) { + for (p = s2; *p && *p != c; ++p) + /* nothing */; + if (*p == c) + break; + } + return n; +} diff --git a/lib/c/src/strlen.c b/lib/c/src/strlen.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strlen + +size_t +strlen(const char *s) +{ + const char *t; + + for (t = s; *t; ++t) + /* nothing */; + return t - s; +} diff --git a/lib/c/src/strncat.c b/lib/c/src/strncat.c @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strncat + +char * +strncat(char * restrict dst, const char * restrict src, size_t n) +{ + char *ret = dst; + + while (*dst) + ++dst; + while (n-- > 0 && *src) + *dst++ = *src++; + *dst = '\0'; + return ret; +} diff --git a/lib/c/src/strncmp.c b/lib/c/src/strncmp.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strncmp + +int +strncmp(const char *s1, const char *s2, size_t n) +{ + for (; n && *s1 && *s2 && *s1 == *s2; --n, ++s1, ++s2); + /* nothing */; + return n ? (*(unsigned char *)s1 - *(unsigned char *)s2) : 0; +} diff --git a/lib/c/src/strncpy.c b/lib/c/src/strncpy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strncpy + +char * +strncpy(char * restrict dst, const char * restrict src, size_t n) +{ + char *ret = dst; + + for (; n > 0 && *src; --n) + *dst++ = *src++; + while (n-- > 0) + *dst++ = '\0'; + return ret; +} diff --git a/lib/c/src/strpbrk.c b/lib/c/src/strpbrk.c @@ -0,0 +1,19 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strpbrk + +char * +strpbrk(const char *s1, const char *s2) +{ + int c; + const char *p; + + for (; c = *s1; ++s1) { + for (p = s2; *p && *p != c; ++p) + /* nothing */; + if (*p == c) + return s1; + } + return NULL; +} diff --git a/lib/c/src/strrchr.c b/lib/c/src/strrchr.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strrchr + +char * +strrchr(const char *s, int c) +{ + const char *t = s; + + while (*t) + ++t; + while (t > s && *t != c) + --t; + return (*t == c) ? (char *)t : NULL; +} diff --git a/lib/c/src/strspn.c b/lib/c/src/strspn.c @@ -0,0 +1,20 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strspn + +size_t +strspn(const char *s1, const char *s2) +{ + size_t n; + int c; + const char *p; + + for (n = 0; c = *s1++; ++n) { + for (p = s2; *p && *p != c; ++p) + /* nothing */; + if (*p == '\0') + break; + } + return n; +} diff --git a/lib/c/src/strstr.c b/lib/c/src/strstr.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strstr + +char * +strstr(const char *s1, const char *s2) +{ + const char *p, *q; + int c; + + c = *s2++; + if (c == '\0') + return (char *) s1; + + while (*s1) { + if (*s1 != c) { + ++s1; + } else { + p = s1++; + for (q = s2; *q && *s1 == *q; ++s1, ++q) + /* nothing */; + if (*q == '\0') + return (char *) p; + } + } + return NULL; +} diff --git a/lib/c/src/strtok.c b/lib/c/src/strtok.c @@ -0,0 +1,27 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strtok + +char * +strtok(char * restrict s, const char * restrict delim) +{ + static char *line; + + if (s) + line = s; + if (!s && !line) + return NULL; + + s = line + strspn(line, delim); + if (*s == '\0') + return line = NULL; + + line = s + strcspn(s, delim); + if (*line != '\0') + *line++ = '\0'; + else + line = NULL; + + return s; +} diff --git a/lib/c/src/strxfrm.c b/lib/c/src/strxfrm.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string.h> +#undef strxfrm + +size_t +strxfrm(char * restrict dst, const char * restrict src, size_t n) +{ + size_t len = strlen(src); + + if (len < n) + strcpy(dst, src); + return len; +} diff --git a/lib/c/src/syscall.h b/lib/c/src/syscall.h @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +extern void *_brk(void *addr); +extern int _open(char *path, int flags, int perm); +extern int _close(int fd); +extern int _read(int fd, void *buf, size_t n); +extern int _write(int fd, void *buf, size_t n); +extern int _lseek(int fd, long off, int whence); +extern void _Exit(int status); +extern int raise(int sig); +extern void (*signal(int sig, void (*func)(int)))(int); diff --git a/lib/c/src/tolower.c b/lib/c/src/tolower.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef tolower + +int +tolower(int c) +{ + return (isupper(c)) ? c | 0x20 : c; +} diff --git a/lib/c/src/toupper.c b/lib/c/src/toupper.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#define __USE_MACROS +#include <ctype.h> +#undef toupper + +int +toupper(int c) +{ + return (islower(c)) ? c & ~0x20 : c; +} diff --git a/lib/c/src/vfprintf.c b/lib/c/src/vfprintf.c @@ -0,0 +1,76 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdarg.h> +#include <stdio.h> +#undef vfprintf + +static int +printn2(FILE * restrict fp, unsigned n, int base) +{ + unsigned t; + int cnt = 0; + static char digits[] = "0123456789ABCDEF"; + + if ((t = n / base) != 0) + cnt += printn2(fp, t, base); + putc(digits[n % base], fp); + return cnt + 1; +} + +static int +printn(FILE * restrict fp, int n, int b, int sign) +{ + int cnt = 0; + + if (sign && n < 0) { + n = -n; + putc('-', fp); + ++cnt; + } + cnt += printn2(fp, n, b); + return cnt; +} + +int +vfprintf(FILE * restrict fp, const char *fmt, va_list va) +{ + int c, base, sign, cnt = 0; + char *s; + + while ((c = *fmt++) != '\0') { + if (c == '%') { + sign = 0; + switch (*fmt++) { + case '%': + c = '%'; + break; + case 'c': + c = va_arg(va, int); + break; + case 'o': + base = 8; + goto numeric; + case 'd': + sign = 1; + base = 10; + goto numeric; + case 'x': + base = 16; + numeric: + c = va_arg(va, int); + cnt += printn(fp, c, base, sign); + continue; + case 's': + s = va_arg(va, char *); + while ((c = *s++) != '\0') + putc(c, fp); + /* passthrou */ + default: + continue; + } + } + putc(c, fp); + ++cnt; + } + return cnt; +} diff --git a/lib/cc/Makefile b/lib/cc/Makefile @@ -0,0 +1,20 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../config.mk + +LIBDIR = ./ + +all: libcc.a + +libcc.a: + ar $(ARFLAGS) $@ $? + ranlib $@ + +dep: + +clean: + rm -f *.o *.a + +distclean: clean +include libdep.mk diff --git a/lib/cc/debug.c b/lib/cc/debug.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./lib/debug.c"; +#include <stdarg.h> +#include <stdio.h> + +#include "../../inc/cc.h" + +int debug; + +void +dbg(const char *fmt, ...) +{ + if (!debug) + return; + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + putc('\n', stderr); + va_end(va); + return; +} diff --git a/lib/cc/die.c b/lib/cc/die.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./lib/die.c"; +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> + +#include "../../inc/cc.h" + +int failure; + +void +die(const char *fmt, ...) +{ + failure = 1; + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + putc('\n', stderr); + va_end(va); + exit(1); +} diff --git a/lib/cc/libdep.mk b/lib/cc/libdep.mk @@ -0,0 +1,10 @@ + +LIB-OBJ = $(LIBDIR)/debug.o \ + $(LIBDIR)/die.o \ + $(LIBDIR)/newitem.o \ + $(LIBDIR)/xcalloc.o \ + $(LIBDIR)/xmalloc.o \ + $(LIBDIR)/xrealloc.o \ + $(LIBDIR)/xstrdup.o \ + +$(LIBDIR)/libcc.a: $(LIB-OBJ) diff --git a/lib/cc/newitem.c b/lib/cc/newitem.c @@ -0,0 +1,12 @@ +#include "../../inc/cc.h" + +void +newitem(struct items *items, char *item) +{ + if ((items->n + 1) < items->n) + die("newitem: overflow (%u + 1)", items->n); + + items->s = xrealloc(items->s, (items->n + 1) * sizeof(char **)); + items->s[items->n++] = item; +} + diff --git a/lib/cc/xcalloc.c b/lib/cc/xcalloc.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./lib/xcalloc.c"; +#include <stdlib.h> +#include "../../inc/cc.h" + +void * +xcalloc(size_t n, size_t size) +{ + void *p = calloc(n, size); + + if (!p) + die("out of memory"); + return p; +} diff --git a/lib/cc/xmalloc.c b/lib/cc/xmalloc.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./lib/xmalloc.c"; +#include <stdlib.h> +#include "../../inc/cc.h" + +void * +xmalloc(size_t size) +{ + void *p = malloc(size); + + if (!p) + die("out of memory"); + return p; +} diff --git a/lib/cc/xrealloc.c b/lib/cc/xrealloc.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./lib/xrealloc.c"; +#include <stdlib.h> +#include "../../inc/cc.h" + +void * +xrealloc(void *buff, size_t size) +{ + void *p = realloc(buff, size); + + if (!p) + die("out of memory"); + return p; +} diff --git a/lib/cc/xstrdup.c b/lib/cc/xstrdup.c @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +static char sccsid[] = "@(#) ./lib/xstrdup.c"; +#include <string.h> +#include "../../inc/cc.h" + +char * +xstrdup(const char *s) +{ + size_t len = strlen(s) + 1; + char *p = xmalloc(len); + + return memcpy(p, s, len); +} diff --git a/lib/crt/Makefile b/lib/crt/Makefile @@ -0,0 +1,11 @@ +.POSIX: + +include ../../config.mk + +all: crt-$(ARCH)-$(SYS).o + +crt-$(ARCH)-$(SYS).o: $(ARCH)-$(SYS)/crt.o + ln -f $(ARCH)-$(SYS)/crt.o $@ + +clean: + rm -f */crt.o crt-*.o diff --git a/lib/crt/amd64-sysv-linux/crt.s b/lib/crt/amd64-sysv-linux/crt.s @@ -0,0 +1,8 @@ + .file "crt.s" + + .text + .global _start +_start: + call main + movl %eax, %edi + call exit diff --git a/lib/crt/amd64-sysv-openbsd/crt.s b/lib/crt/amd64-sysv-openbsd/crt.s @@ -0,0 +1,23 @@ + .file "crt.s" + + .section ".note.openbsd.ident", "a" + .p2align 2 + .long 8 + .long 4 + .long 1 + .ascii "OpenBSD\0" + .long 0 + .p2align 2 + + .text + .align 8 + .global _start +_start: + call main + movl %eax, %edi + call exit + + .data + .global __guard_local +__guard_local: + .long 0 diff --git a/lib/crt/i386-sysv-linux/crt.s b/lib/crt/i386-sysv-linux/crt.s @@ -0,0 +1 @@ +/* TODO */ diff --git a/lib/crt/i386-sysv-openbsd/crt.s b/lib/crt/i386-sysv-openbsd/crt.s @@ -0,0 +1 @@ +/* TODO */ diff --git a/lib/debug.c b/lib/debug.c @@ -1,21 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./lib/debug.c"; -#include <stdarg.h> -#include <stdio.h> - -#include "../inc/cc.h" - -int debug; - -void -dbg(const char *fmt, ...) -{ - if (!debug) - return; - va_list va; - va_start(va, fmt); - vfprintf(stderr, fmt, va); - putc('\n', stderr); - va_end(va); - return; -} diff --git a/lib/die.c b/lib/die.c @@ -1,21 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./lib/die.c"; -#include <stdarg.h> -#include <stdlib.h> -#include <stdio.h> - -#include "../inc/cc.h" - -int failure; - -void -die(const char *fmt, ...) -{ - failure = 1; - va_list va; - va_start(va, fmt); - vfprintf(stderr, fmt, va); - putc('\n', stderr); - va_end(va); - exit(1); -} diff --git a/lib/newitem.c b/lib/newitem.c @@ -1,12 +0,0 @@ -#include "../inc/cc.h" - -void -newitem(struct items *items, char *item) -{ - if ((items->n + 1) < items->n) - die("newitem: overflow (%u + 1)", items->n); - - items->s = xrealloc(items->s, (items->n + 1) * sizeof(char **)); - items->s[items->n++] = item; -} - diff --git a/lib/sys/Makefile b/lib/sys/Makefile @@ -0,0 +1,13 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../config.mk + +all clean distclean: + pwd=$$PWD ;\ + for i in $(SYSS); \ + do \ + cd $$i; \ + $(MAKE) -e SYS=$i $@ ;\ + cd $$pwd; \ + done diff --git a/lib/sys/abi/amd64-sysv.sh b/lib/sys/abi/amd64-sysv.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +awk '/^#/ {next} + {name=$2 ".s" + printf ".global %s\n" \ + "%s:\n" \ + "\tmovq\t$%d,%%rax\n" \ + "\tsyscall\n" \ + "\tret\n", $2, $2, $1 > name + close(name)}' syscall.lst diff --git a/lib/sys/amd64-sysv-linux/Makefile b/lib/sys/amd64-sysv-linux/Makefile @@ -0,0 +1,5 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../common.mk +ABI = amd64-sysv diff --git a/lib/sys/amd64-sysv-linux/syscall.lst b/lib/sys/amd64-sysv-linux/syscall.lst @@ -0,0 +1,11 @@ +#number name +0 _read +1 _write +2 _open +3 _close +3 _lseek +12 _brk +13 _rt_sigaction +38 _getpid +60 _Exit +32 _kill diff --git a/lib/sys/common.mk b/lib/sys/common.mk @@ -0,0 +1,13 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../../config.mk + +all clean distclean: system.mk + $(MAKE) ABI=$(ABI) -f ../libsys.mk $@ + +system.mk: syscall.lst + rm -f $@; trap "rm -f $$$$.mk" 0 2 3; \ + awk 'BEGIN{printf "OBJ = "} \ + ! /^#/ {printf "%s.o ", $$2}' syscall.lst > $$$$.mk && \ + mv $$$$.mk $@ diff --git a/lib/sys/i386-sysv-linux/Makefile b/lib/sys/i386-sysv-linux/Makefile @@ -0,0 +1,4 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../Makefile.sys diff --git a/lib/sys/i386-sysv-linux/syscall.lst b/lib/sys/i386-sysv-linux/syscall.lst diff --git a/lib/sys/libsys.mk b/lib/sys/libsys.mk @@ -0,0 +1,24 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../../../config.mk +include system.mk + +ASM = $(OBJ:.o=.s) + +all: libsys.a + +libsys.a: $(OBJ) + $(AR) $(ARFLAGS) $@ $? + ranlib $@ + +$(ASM): syscall.lst + ../abi/$(ABI).sh + +clean: + rm -f $(ASM) + rm -f $(OBJ) + rm -f libsys.a + +distclean: clean + rm -f system.mk diff --git a/lib/sys/qbe-linux/Makefile b/lib/sys/qbe-linux/Makefile @@ -0,0 +1,5 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../common.mk +ABI=amd64-sysv diff --git a/lib/sys/qbe-linux/syscall.lst b/lib/sys/qbe-linux/syscall.lst @@ -0,0 +1,11 @@ +#number name +0 _read +1 _write +2 _open +3 _close +3 _lseek +12 _brk +13 _rt_sigaction +38 _getpid +60 _Exit +32 _kill diff --git a/lib/sys/z80-linux/Makefile b/lib/sys/z80-linux/Makefile @@ -0,0 +1,4 @@ +# See LICENSE file for copyright and license details. +.POSIX: + +include ../Makefile.sys diff --git a/lib/sys/z80-linux/syscall.lst b/lib/sys/z80-linux/syscall.lst diff --git a/lib/xcalloc.c b/lib/xcalloc.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./lib/xcalloc.c"; -#include <stdlib.h> -#include "../inc/cc.h" - -void * -xcalloc(size_t n, size_t size) -{ - void *p = calloc(n, size); - - if (!p) - die("out of memory"); - return p; -} diff --git a/lib/xmalloc.c b/lib/xmalloc.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./lib/xmalloc.c"; -#include <stdlib.h> -#include "../inc/cc.h" - -void * -xmalloc(size_t size) -{ - void *p = malloc(size); - - if (!p) - die("out of memory"); - return p; -} diff --git a/lib/xrealloc.c b/lib/xrealloc.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./lib/xrealloc.c"; -#include <stdlib.h> -#include "../inc/cc.h" - -void * -xrealloc(void *buff, size_t size) -{ - void *p = realloc(buff, size); - - if (!p) - die("out of memory"); - return p; -} diff --git a/lib/xstrdup.c b/lib/xstrdup.c @@ -1,13 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -static char sccsid[] = "@(#) ./lib/xstrdup.c"; -#include <string.h> -#include "../inc/cc.h" - -char * -xstrdup(const char *s) -{ - size_t len = strlen(s) + 1; - char *p = xmalloc(len); - - return memcpy(p, s, len); -} diff --git a/libc/include/assert.h b/libc/include/assert.h @@ -1,13 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _ASSERT_H -#define _ASSERT_H - -void __assert(char *exp, char *file, long line); - -#ifndef NDEBUG -# define assert(exp) ((exp) ? (void) 0 : __assert(#exp, __FILE__, __LINE__)) -#else -# define assert(exp) ((void)0) -#endif - -#endif diff --git a/libc/include/bits/amd64-sysv/arch/limits.h b/libc/include/bits/amd64-sysv/arch/limits.h @@ -1,17 +0,0 @@ -#define SCHAR_MAX 0x7F -#define SCHAR_MIN (-SCHAR_MIN-1) -#define CHAR_MAX 0x7F -#define CHAR_MIN (-CHAR_MAX-1) -#define UCHAR_MAX 0xFF -#define SHRT_MAX 0x7FFF -#define SHRT_MIN (-SHRT_MAX-1) -#define USHRT_MAX 0xFFFF -#define INT_MAX 0x7FFFFFFF -#define INT_MIN (-INT_MAX-1) -#define UINT_MAX 0xFFFFFFFF -#define LONG_MAX 0x7FFFFFFFFFFFFFFF -#define LONG_MIN (-LONG_MAX-1) -#define ULONG_MAX 0xFFFFFFFFFFFFFFFF -#define LLONG_MAX 0x7FFFFFFFFFFFFFFF -#define LLONG_MIN (-LLONG_MAX-1) -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/libc/include/bits/amd64-sysv/arch/signal.h b/libc/include/bits/amd64-sysv/arch/signal.h @@ -1,29 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -typedef int sig_atomic_t; - -#define SIG_ERR -1 -#define SIG_DFL 0 -#define SIG_IGN 1 - -#define SIGHUP 1 -#define SIGINT 2 -#define SIGQUIT 3 -#define SIGILL 4 -#define SIGABRT 6 -#define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 -#define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 -#define SIGTERM 15 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGSSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 - -#define __NR_SIGNALS 23 diff --git a/libc/include/bits/amd64-sysv/arch/stddef.h b/libc/include/bits/amd64-sysv/arch/stddef.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#ifndef _WCHAR_T -typedef int wchar_t; -#define _WCHAR_T -#endif - -#ifndef _PTRDIFF_T -typedef long ptrdiff_t; -#define _PTRDIFF_T -#endif diff --git a/libc/include/bits/amd64-sysv/arch/stdint.h b/libc/include/bits/amd64-sysv/arch/stdint.h @@ -1,117 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define INT8_MAX 0x7F -#define INT8_MIN (-INT8_MAX-1) -#define UINT8_MAX 0xFF - -#define INT16_MAX 0x7FFF -#define INT16_MIN (-INT16_MAX-1) -#define UINT16_MAX 0xFFFF - -#define INT32_MAX 0x7FFFFFFF -#define INT32_MIN (-INT32_MAX-1) -#define UINT32_MAX 0xFFFFFFFF - -#define INT64_MAX 0x7FFFFFFFFFFFFFFF -#define INT64_MIN (-INT64_MAX-1) -#define UINT64_MAX 0xFFFFFFFFFFFFFFFF - -#define INT_LEAST8_MIN INT8_MIN -#define INT_LEAST8_MAX INT8_MAX -#define UINT_LEAST8_MAX UINT8_MAX - -#define INT_LEAST16_MIN INT16_MIN -#define INT_LEAST16_MAX INT16_MAX -#define UINT_LEAST16_MAX UINT16_MAX - -#define INT_LEAST32_MIN INT32_MIN -#define INT_LEAST32_MAX INT32_MAX -#define UINT_LEAST32_MAX UINT32_MAX - -#define INT_LEAST64_MIN INT64_MIN -#define INT_LEAST64_MAX INT64_MAX -#define UINT_LEAST64_MAX UINT64_MAX - -#define INT_FAST8_MIN INT32_MIN -#define INT_FAST8_MAX INT32_MAX -#define UINT_FAST8_MAX UINT32_MAX - -#define INT_FAST16_MIN INT32_MIN -#define INT_FAST16_MAX INT32_MAX -#define UINT_FAST16_MAX UINT32_MAX - -#define INT_FAST32_MIN INT32_MIN -#define INT_FAST32_MAX INT32_MAX -#define UINT_FAST32_MAX UINT32_MAX - -#define INT_FAST64_MIN INT64_MIN -#define INT_FAST64_MAX INT64_MAX -#define UINT_FAST64_MAX UINT64_MAX - -#define INTPTR_MIN INT64_MIN -#define INTPTR_MAX INT64_MAX -#define UINTPTR_MAX UINT64_MAX - -#define INTMAX_MIN INT64_MIN -#define INTMAX_MAX INT64_MAX -#define UINTMAX_MAX UINT64_MAX - -#define PTRDIFF_MIN INT64_MIN -#define PTRDIFF_MAX INT64_MAX - -#define SIG_ATOMIC_MIN INT32_MIN -#define SIG_ATOMIC_MAX INT32_MAX - -#define SIZE_MAX UINT64_MAX - -#define WCHAR_MIN INT32_MIN -#define WCHAR_MAX INT32_MAX - -#define INT8_C(x) x -#define INT16_C(x) x -#define INT32_C(x) x -#define INT64_C(x) x ## L - -#define UINT8_C(x) x -#define UINT16_C(x) x -#define UINT32_C(x) x ## U -#define UINT64_C(x) x ## UL - -#define INTMAX_C(x) x ## L -#define UINTMAX_C(x) x ## UL - -typedef signed char int8_t; -typedef short int16_t; -typedef int int32_t; -typedef long int64_t; - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned uint32_t; -typedef unsigned long uint64_t; - -typedef signed char int8_least_t; -typedef short int16_least_t; -typedef int int32_least_t; -typedef long int64_least_t; - -typedef unsigned char uint8_least_t; -typedef unsigned short uint16_least_t; -typedef unsigned uint32_least_t; -typedef unsigned long uint64_least_t; - -typedef int int8_fast_t; -typedef int int16_fast_t; -typedef int int32_fast_t; -typedef long int64_fast_t; - -typedef unsigned uint8_fast_t; -typedef unsigned uint16_fast_t; -typedef unsigned uint32_fast_t; -typedef unsigned long uint64_fast_t; - -typedef long intptr_t; -typedef unsigned long uintptr_t; - -typedef long intmax_t; -typedef unsigned long uintmax_t; diff --git a/libc/include/bits/amd64-sysv/arch/stdio.h b/libc/include/bits/amd64-sysv/arch/stdio.h @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#define BUFSIZ 512 -#define FILENAME_MAX 256 -#define FOPEN_MAX 16 - -#define TMP_MAX 25 -#define L_tmpnam 256 - -typedef int fpos_t; diff --git a/libc/include/bits/amd64-sysv/arch/stdlib.h b/libc/include/bits/amd64-sysv/arch/stdlib.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#define EXIT_FAILURE 1 -#define EXIT_SUCCESS 0 - -#ifndef _WCHAR_T -typedef int wchar_t; -#define _WCHAR_T -#endif - -#define _ALIGNTYPE long double diff --git a/libc/include/bits/amd64-sysv/arch/string.h b/libc/include/bits/amd64-sysv/arch/string.h @@ -1,5 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#endif diff --git a/libc/include/bits/amd64-sysv/arch/time.h b/libc/include/bits/amd64-sysv/arch/time.h @@ -1,8 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -typedef long int time_t; diff --git a/libc/include/bits/i386-sysv/arch/limits.h b/libc/include/bits/i386-sysv/arch/limits.h @@ -1,17 +0,0 @@ -#define SCHAR_MAX 0x7F -#define SCHAR_MIN (-SCHAR_MIN-1) -#define CHAR_MAX 0x7F -#define CHAR_MIN (-CHAR_MAX-1) -#define UCHAR_MAX 0xFF -#define SHRT_MAX 0x7FFF -#define SHRT_MIN (-SHRT_MAX-1) -#define USHRT_MAX 0xFFFF -#define INT_MAX 0x7FFFFFFF -#define INT_MIN (-INT_MAX-1) -#define UINT_MAX 0xFFFFFFFF -#define LONG_MAX 0x7FFFFFFF -#define LONG_MIN (-LONG_MAX-1) -#define ULONG_MAX 0xFFFFFFFF -#define LLONG_MAX 0x7FFFFFFFFFFFFFFF -#define LLONG_MIN (-LLONG_MAX-1) -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/libc/include/bits/i386-sysv/arch/signal.h b/libc/include/bits/i386-sysv/arch/signal.h @@ -1,29 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -typedef int sig_atomic_t; - -#define SIG_ERR -1 -#define SIG_DFL 0 -#define SIG_IGN 1 - -#define SIGHUP 1 -#define SIGINT 2 -#define SIGQUIT 3 -#define SIGILL 4 -#define SIGABRT 6 -#define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 -#define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 -#define SIGTERM 15 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGSSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 - -#define __NR_SIGNALS 23 diff --git a/libc/include/bits/i386-sysv/arch/stddef.h b/libc/include/bits/i386-sysv/arch/stddef.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#ifndef _WCHAR_T -typedef int wchar_t; -#define _WCHAR_T -#endif - -#ifndef _PTRDIFF_T -typedef long ptrdiff_t; -#define _PTRDIFF_T -#endif diff --git a/libc/include/bits/i386-sysv/arch/stdint.h b/libc/include/bits/i386-sysv/arch/stdint.h @@ -1,117 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define INT8_MAX 0x7F -#define INT8_MIN (-INT8_MAX-1) -#define UINT8_MAX 0xFF - -#define INT16_MAX 0x7FFF -#define INT16_MIN (-INT16_MAX-1) -#define UINT16_MAX 0xFFFF - -#define INT32_MAX 0x7FFFFFFF -#define INT32_MIN (-INT32_MAX-1) -#define UINT32_MAX 0xFFFFFFFF - -#define INT64_MAX 0x7FFFFFFFFFFFFFFF -#define INT64_MIN (-INT64_MAX-1) -#define UINT64_MAX 0xFFFFFFFFFFFFFFFF - -#define INT_LEAST8_MIN INT8_MIN -#define INT_LEAST8_MAX INT8_MAX -#define UINT_LEAST8_MAX UINT8_MAX - -#define INT_LEAST16_MIN INT16_MIN -#define INT_LEAST16_MAX INT16_MAX -#define UINT_LEAST16_MAX UINT16_MAX - -#define INT_LEAST32_MIN INT32_MIN -#define INT_LEAST32_MAX INT32_MAX -#define UINT_LEAST32_MAX UINT32_MAX - -#define INT_LEAST64_MIN INT64_MIN -#define INT_LEAST64_MAX INT64_MAX -#define UINT_LEAST64_MAX UINT64_MAX - -#define INT_FAST8_MIN INT32_MIN -#define INT_FAST8_MAX INT32_MAX -#define UINT_FAST8_MAX UINT32_MAX - -#define INT_FAST16_MIN INT32_MIN -#define INT_FAST16_MAX INT32_MAX -#define UINT_FAST16_MAX UINT32_MAX - -#define INT_FAST32_MIN INT32_MIN -#define INT_FAST32_MAX INT32_MAX -#define UINT_FAST32_MAX UINT32_MAX - -#define INT_FAST64_MIN INT64_MIN -#define INT_FAST64_MAX INT64_MAX -#define UINT_FAST64_MAX UINT64_MAX - -#define INTPTR_MIN INT32_MIN -#define INTPTR_MAX INT32_MAX -#define UINTPTR_MAX UINT32_MAX - -#define INTMAX_MIN INT64_MIN -#define INTMAX_MAX INT64_MAX -#define UINTMAX_MAX UINT64_MAX - -#define PTRDIFF_MIN INT32_MIN -#define PTRDIFF_MAX INT32_MAX - -#define SIG_ATOMIC_MIN INT32_MIN -#define SIG_ATOMIC_MAX INT32_MAX - -#define SIZE_MAX UINT32_MAX - -#define WCHAR_MIN INT32_MIN -#define WCHAR_MAX INT32_MAX - -#define INT8_C(x) x -#define INT16_C(x) x -#define INT32_C(x) x -#define INT64_C(x) x ## LL - -#define UINT8_C(x) x -#define UINT16_C(x) x -#define UINT32_C(x) x ## U -#define UINT64_C(x) x ## ULL - -#define INTMAX_C(x) x ## LL -#define UINTMAX_C(x) x ## ULL - -typedef signed char int8_t; -typedef short int16_t; -typedef int int32_t; -typedef long long int64_t; - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned uint32_t; -typedef unsigned long long uint64_t; - -typedef signed char int8_least_t; -typedef short int16_least_t; -typedef int int32_least_t; -typedef long long int64_least_t; - -typedef unsigned char uint8_least_t; -typedef unsigned short uint16_least_t; -typedef unsigned uint32_least_t; -typedef unsigned long long uint64_least_t; - -typedef int int8_fast_t; -typedef int int16_fast_t; -typedef int int32_fast_t; -typedef long long int64_fast_t; - -typedef unsigned uint8_fast_t; -typedef unsigned uint16_fast_t; -typedef unsigned uint32_fast_t; -typedef unsigned long long uint64_fast_t; - -typedef int intptr_t; -typedef unsigned uintptr_t; - -typedef long long intmax_t; -typedef unsigned long long uintmax_t; diff --git a/libc/include/bits/i386-sysv/arch/stdio.h b/libc/include/bits/i386-sysv/arch/stdio.h @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#define BUFSIZ 512 -#define FILENAME_MAX 256 -#define FOPEN_MAX 16 - -#define TMP_MAX 25 -#define L_tmpnam 256 - -typedef long fpos_t; diff --git a/libc/include/bits/i386-sysv/arch/stdlib.h b/libc/include/bits/i386-sysv/arch/stdlib.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#define EXIT_FAILURE 1 -#define EXIT_SUCCESS 0 - -#ifndef _WCHAR_T -typedef int wchar_t; -#define _WCHAR_T -#endif - -#define _ALIGNTYPE long double diff --git a/libc/include/bits/i386-sysv/arch/string.h b/libc/include/bits/i386-sysv/arch/string.h @@ -1,5 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#endif diff --git a/libc/include/bits/i386-sysv/arch/time.h b/libc/include/bits/i386-sysv/arch/time.h @@ -1,8 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -typedef long int time_t; diff --git a/libc/include/bits/qbe/arch/limits.h b/libc/include/bits/qbe/arch/limits.h @@ -1,17 +0,0 @@ -#define SCHAR_MAX 0x7F -#define SCHAR_MIN (-SCHAR_MIN-1) -#define CHAR_MAX 0x7F -#define CHAR_MIN (-CHAR_MAX-1) -#define UCHAR_MAX 0xFF -#define SHRT_MAX 0x7FFF -#define SHRT_MIN (-SHRT_MAX-1) -#define USHRT_MAX 0xFFFF -#define INT_MAX 0x7FFFFFFF -#define INT_MIN (-INT_MAX-1) -#define UINT_MAX 0xFFFFFFFF -#define LONG_MAX 0x7FFFFFFFFFFFFFFF -#define LONG_MIN (-LONG_MAX-1) -#define ULONG_MAX 0xFFFFFFFFFFFFFFFF -#define LLONG_MAX 0x7FFFFFFFFFFFFFFF -#define LLONG_MIN (-LLONG_MAX-1) -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/libc/include/bits/qbe/arch/signal.h b/libc/include/bits/qbe/arch/signal.h @@ -1,29 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -typedef int sig_atomic_t; - -#define SIG_ERR -1 -#define SIG_DFL 0 -#define SIG_IGN 1 - -#define SIGHUP 1 -#define SIGINT 2 -#define SIGQUIT 3 -#define SIGILL 4 -#define SIGABRT 6 -#define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 -#define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 -#define SIGTERM 15 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGSSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 - -#define __NR_SIGNALS 23 diff --git a/libc/include/bits/qbe/arch/stddef.h b/libc/include/bits/qbe/arch/stddef.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#ifndef _WCHAR_T -typedef int wchar_t; -#define _WCHAR_T -#endif - -#ifndef _PTRDIFF_T -typedef long ptrdiff_t; -#define _PTRDIFF_T -#endif diff --git a/libc/include/bits/qbe/arch/stdint.h b/libc/include/bits/qbe/arch/stdint.h @@ -1,117 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define INT8_MAX 0x7F -#define INT8_MIN (-INT8_MAX-1) -#define UINT8_MAX 0xFF - -#define INT16_MAX 0x7FFF -#define INT16_MIN (-INT16_MAX-1) -#define UINT16_MAX 0xFFFF - -#define INT32_MAX 0x7FFFFFFF -#define INT32_MIN (-INT32_MAX-1) -#define UINT32_MAX 0xFFFFFFFF - -#define INT64_MAX 0x7FFFFFFFFFFFFFFF -#define INT64_MIN (-INT64_MAX-1) -#define UINT64_MAX 0xFFFFFFFFFFFFFFFF - -#define INT_LEAST8_MIN INT8_MIN -#define INT_LEAST8_MAX INT8_MAX -#define UINT_LEAST8_MAX UINT8_MAX - -#define INT_LEAST16_MIN INT16_MIN -#define INT_LEAST16_MAX INT16_MAX -#define UINT_LEAST16_MAX UINT16_MAX - -#define INT_LEAST32_MIN INT32_MIN -#define INT_LEAST32_MAX INT32_MAX -#define UINT_LEAST32_MAX UINT32_MAX - -#define INT_LEAST64_MIN INT64_MIN -#define INT_LEAST64_MAX INT64_MAX -#define UINT_LEAST64_MAX UINT64_MAX - -#define INT_FAST8_MIN INT32_MIN -#define INT_FAST8_MAX INT32_MAX -#define UINT_FAST8_MAX UINT32_MAX - -#define INT_FAST16_MIN INT32_MIN -#define INT_FAST16_MAX INT32_MAX -#define UINT_FAST16_MAX UINT32_MAX - -#define INT_FAST32_MIN INT32_MIN -#define INT_FAST32_MAX INT32_MAX -#define UINT_FAST32_MAX UINT32_MAX - -#define INT_FAST64_MIN INT64_MIN -#define INT_FAST64_MAX INT64_MAX -#define UINT_FAST64_MAX UINT64_MAX - -#define INTPTR_MIN INT64_MIN -#define INTPTR_MAX INT64_MAX -#define UINTPTR_MAX UINT64_MAX - -#define INTMAX_MIN INT64_MIN -#define INTMAX_MAX INT64_MAX -#define UINTMAX_MAX UINT64_MAX - -#define PTRDIFF_MIN INT64_MIN -#define PTRDIFF_MAX INT64_MAX - -#define SIG_ATOMIC_MIN INT32_MIN -#define SIG_ATOMIC_MAX INT32_MAX - -#define SIZE_MAX UINT64_MAX - -#define WCHAR_MIN INT32_MIN -#define WCHAR_MAX INT32_MAX - -#define INT8_C(x) x -#define INT16_C(x) x -#define INT32_C(x) x -#define INT64_C(x) x ## L - -#define UINT8_C(x) x -#define UINT16_C(x) x -#define UINT32_C(x) x ## U -#define UINT64_C(x) x ## UL - -#define INTMAX_C(x) x ## L -#define UINTMAX_C(x) x ## UL - -typedef signed char int8_t; -typedef short int16_t; -typedef int int32_t; -typedef long int64_t; - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned uint32_t; -typedef unsigned long uint64_t; - -typedef signed char int8_least_t; -typedef short int16_least_t; -typedef int int32_least_t; -typedef long int64_least_t; - -typedef unsigned char uint8_least_t; -typedef unsigned short uint16_least_t; -typedef unsigned uint32_least_t; -typedef unsigned long uint64_least_t; - -typedef int int8_fast_t; -typedef int int16_fast_t; -typedef int int32_fast_t; -typedef long int64_fast_t; - -typedef unsigned uint8_fast_t; -typedef unsigned uint16_fast_t; -typedef unsigned uint32_fast_t; -typedef unsigned long uint64_fast_t; - -typedef long intptr_t; -typedef unsigned long uintptr_t; - -typedef long intmax_t; -typedef unsigned long uintmax_t; diff --git a/libc/include/bits/qbe/arch/stdio.h b/libc/include/bits/qbe/arch/stdio.h @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#define BUFSIZ 512 -#define FILENAME_MAX 256 -#define FOPEN_MAX 16 - -#define TMP_MAX 25 -#define L_tmpnam 256 - -typedef int fpos_t; diff --git a/libc/include/bits/qbe/arch/stdlib.h b/libc/include/bits/qbe/arch/stdlib.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -#define EXIT_FAILURE 1 -#define EXIT_SUCCESS 0 - -#ifndef _WCHAR_T -typedef int wchar_t; -#define _WCHAR_T -#endif - -#define _ALIGNTYPE long double diff --git a/libc/include/bits/qbe/arch/string.h b/libc/include/bits/qbe/arch/string.h @@ -1,5 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#endif diff --git a/libc/include/bits/qbe/arch/time.h b/libc/include/bits/qbe/arch/time.h @@ -1,8 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned long size_t; -#define _SIZET -#endif - -typedef long int time_t; diff --git a/libc/include/bits/z80/arch/limits.h b/libc/include/bits/z80/arch/limits.h @@ -1,17 +0,0 @@ -#define SCHAR_MAX 0x7F -#define SCHAR_MIN (-SCHAR_MIN-1) -#define CHAR_MAX 0xFF -#define CHAR_MIN 0 -#define UCHAR_MAX 0xFF -#define SHRT_MAX 0x7FFF -#define SHRT_MIN (-SHRT_MAX-1) -#define USHRT_MAX 0xFFFF -#define INT_MAX 0x7FFF -#define INT_MIN (-INT_MAX-1) -#define UINT_MAX 0xFFFF -#define LONG_MAX 0x7FFFFFFF -#define LONG_MIN (-LONG_MAX-1) -#define ULONG_MAX 0xFFFFFFFF -#define LLONG_MAX 0x7FFFFFFFFFFFFFFF -#define LLONG_MIN (-LLONG_MAX-1) -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/libc/include/bits/z80/arch/signal.h b/libc/include/bits/z80/arch/signal.h @@ -1,33 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -typedef char sig_atomic_t; - -#define SIG_ERR -1 -#define SIG_DFL 0 -#define SIG_IGN 1 - -#define SIG_ERR -1 -#define SIG_DFL 0 -#define SIG_IGN 1 - -#define SIGHUP 1 -#define SIGINT 2 -#define SIGQUIT 3 -#define SIGILL 4 -#define SIGABRT 6 -#define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 -#define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 -#define SIGTERM 15 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGSSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 - -#define __NR_SIGNALS 23 diff --git a/libc/include/bits/z80/arch/stddef.h b/libc/include/bits/z80/arch/stddef.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned size_t; -#define _SIZET -#endif - -#ifndef _WCHAR_T -typedef short wchar_t; -#define _WCHAR_T -#endif - -#ifndef _PTRDIFF_T -typedef short ptrdiff_t; -#define _PTRDIFF_T -#endif diff --git a/libc/include/bits/z80/arch/stdint.h b/libc/include/bits/z80/arch/stdint.h @@ -1,117 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define INT8_MAX 0x7F -#define INT8_MIN (-INT8_MAX-1) -#define UINT8_MAX 0xFF - -#define INT16_MAX 0x7FFF -#define INT16_MIN (-INT16_MAX-1) -#define UINT16_MAX 0xFFFF - -#define INT32_MAX 0x7FFFFFFF -#define INT32_MIN (-INT32_MAX-1) -#define UINT32_MAX 0xFFFFFFFF - -#define INT64_MAX 0x7FFFFFFFFFFFFFFF -#define INT64_MIN (-INT64_MAX-1) -#define UINT64_MAX 0xFFFFFFFFFFFFFFFF - -#define INT_LEAST8_MIN INT8_MIN -#define INT_LEAST8_MAX INT8_MAX -#define UINT_LEAST8_MAX UINT8_MAX - -#define INT_LEAST16_MIN INT16_MIN -#define INT_LEAST16_MAX INT16_MAX -#define UINT_LEAST16_MAX UINT16_MAX - -#define INT_LEAST32_MIN INT32_MIN -#define INT_LEAST32_MAX INT32_MAX -#define UINT_LEAST32_MAX UINT32_MAX - -#define INT_LEAST64_MIN INT64_MIN -#define INT_LEAST64_MAX INT64_MAX -#define UINT_LEAST64_MAX UINT64_MAX - -#define INT_FAST8_MIN INT16_MIN -#define INT_FAST8_MAX INT16_MAX -#define UINT_FAST8_MAX UINT16_MAX - -#define INT_FAST16_MIN INT16_MIN -#define INT_FAST16_MAX INT16_MAX -#define UINT_FAST16_MAX UINT16_MAX - -#define INT_FAST32_MIN INT32_MIN -#define INT_FAST32_MAX INT32_MAX -#define UINT_FAST32_MAX UINT32_MAX - -#define INT_FAST64_MIN INT64_MIN -#define INT_FAST64_MAX INT64_MAX -#define UINT_FAST64_MAX UINT64_MAX - -#define INTPTR_MIN INT16_MIN -#define INTPTR_MAX INT16_MAX -#define UINTPTR_MAX UINT16_MAX - -#define INTMAX_MIN INT64_MIN -#define INTMAX_MAX INT64_MAX -#define UINTMAX_MAX UINT64_MAX - -#define PTRDIFF_MIN INT16_MIN -#define PTRDIFF_MAX INT16_MAX - -#define SIG_ATOMIC_MIN INT16_MIN -#define SIG_ATOMIC_MAX INT16_MAX - -#define SIZE_MAX UINT64_MAX - -#define WCHAR_MIN INT16_MIN -#define WCHAR_MAX INT16_MAX - -#define INT8_C(x) x -#define INT16_C(x) x -#define INT32_C(x) x ## L -#define INT64_C(x) x ## LL - -#define UINT8_C(x) x -#define UINT16_C(x) x ## U -#define UINT32_C(x) x ## UL -#define UINT64_C(x) x ## ULL - -#define INTMAX_C(x) x ## LL -#define UINTMAX_C(x) x ## ULL - -typedef signed char int8_t; -typedef int int16_t; -typedef long int32_t; -typedef long long int64_t; - -typedef unsigned char uint8_t; -typedef unsigned uint16_t; -typedef unsigned long uint32_t; -typedef unsigned long long uint64_t; - -typedef signed char int8_least_t; -typedef int int16_least_t; -typedef long int32_least_t; -typedef long long int64_least_t; - -typedef unsigned char uint8_least_t; -typedef unsigned uint16_least_t; -typedef unsigned long uint32_least_t; -typedef unsigned long long uint64_least_t; - -typedef signed char int8_fast_t; -typedef int int16_fast_t; -typedef long int32_fast_t; -typedef long long int64_fast_t; - -typedef unsigned char uint8_fast_t; -typedef unsigned uint16_fast_t; -typedef unsigned long uint32_fast_t; -typedef unsigned long long uint64_fast_t; - -typedef int intptr_t; -typedef unsigned uintptr_t; - -typedef long long intmax_t; -typedef unsigned long long uintmax_t; diff --git a/libc/include/bits/z80/arch/stdio.h b/libc/include/bits/z80/arch/stdio.h @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned size_t; -#define _SIZET -#endif - -#define BUFSIZ 512 -#define FILENAME_MAX 256 -#define FOPEN_MAX 16 - -#define TMP_MAX 25 -#define L_tmpnam 256 - -typedef long fpos_t; diff --git a/libc/include/bits/z80/arch/stdlib.h b/libc/include/bits/z80/arch/stdlib.h @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned size_t; -#define _SIZET -#endif - -#define EXIT_FAILURE 1 -#define EXIT_SUCCESS 0 - -#ifndef _WCHAR_T -typedef short wchar_t; -#define _WCHAR_T -#endif - -#define _ALIGNTYPE int diff --git a/libc/include/bits/z80/arch/string.h b/libc/include/bits/z80/arch/string.h @@ -1,5 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned size_t; -#endif diff --git a/libc/include/bits/z80/arch/time.h b/libc/include/bits/z80/arch/time.h @@ -1,8 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#ifndef _SIZET -typedef unsigned size_t; -#define _SIZET -#endif - -typedef long time_t; diff --git a/libc/include/ctype.h b/libc/include/ctype.h @@ -1,49 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _CTYPE_H -#define _CTYPE_H - -extern int isalnum(int c); -extern int isalpha(int c); -extern int islower(int c); -extern int isupper(int c); -extern int isdigit(int c); -extern int isxdigit(int c); -extern int iscntrl(int c); -extern int isgraph(int c); -extern int isspace(int c); -extern int isblank(int c); -extern int isprint(int c); -extern int ispunct(int c); -extern int tolower(int c); -extern int toupper(int c); - -#ifdef __USE_MACROS - -#define _U 0x01 /* upper */ -#define _L 0x02 /* lower */ -#define _D 0x04 /* digit */ -#define _C 0x08 /* cntrl */ -#define _P 0x10 /* punct */ -#define _S 0x20 /* white space (space/lf/tab) */ -#define _X 0x40 /* hex char */ -#define _SP 0x80 /* hard space (0x20) */ - -extern unsigned char __ctype[]; - -#define isalnum(c) (__ctype[(unsigned char) c] & (_U|_L|_D)) -#define isalpha(c) (__ctype[(unsigned char) c] & (_U|_L)) -#define iscntrl(c) (__ctype[(unsigned char) c] & (_C)) -#define isdigit(c) (__ctype[(unsigned char) c] & (_D)) -#define isgraph(c) (__ctype[(unsigned char) c] & (_P|_U|_L|_D)) -#define islower(c) (__ctype[(unsigned char) c] & (_L)) -#define isprint(c) (__ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP)) -#define ispunct(c) (__ctype[(unsigned char) c] & (_P)) -#define isspace(c) (__ctype[(unsigned char) c] & (_S)) -#define isupper(c) (__ctype[(unsigned char) c] & (_U)) -#define isxdigit(c) (__ctype[(unsigned char) c] & (_D|_X)) - -#define isascii(c) (((unsigned) c)<=0x7f) - -#endif - -#endif diff --git a/libc/include/errno.h b/libc/include/errno.h @@ -1,12 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _ERRNO_H -#define _ERRNO_H - -#define EDOM 1 -#define EILSEQ 2 -#define ERANGE 3 -#define ENOMEN 4 - -extern int errno; - -#endif diff --git a/libc/include/limits.h b/libc/include/limits.h @@ -1,9 +0,0 @@ -#ifndef _LIMITS_H -#define _LIMITS_H - -#include <arch/limits.h> - -#define CHAR_BIT 8 -#define MB_LEN_MAX 1 - -#endif diff --git a/libc/include/locale.h b/libc/include/locale.h @@ -1,45 +0,0 @@ -#ifndef _LOCALE_H -#define _LOCALE_H - -#ifndef NULL -#define NULL ((void *) 0) -#endif - -#define LC_ALL 0 -#define LC_COLLATE 1 -#define LC_CTYPE 2 -#define LC_MONETARY 3 -#define LC_NUMERIC 4 -#define LC_TIME 5 - -struct lconv { - char *decimal_point; - char *thousands_sep; - char *grouping; - char *mon_decimal_point; - char *mon_thousands_sep; - char *mon_grouping; - char *positive_sign; - char *negative_sign; - char *currency_symbol; - char *int_curr_symbol; - char frac_digits; - char p_cs_precedes; - char n_cs_precedes; - char p_sep_by_space; - char n_sep_by_space; - char p_sign_posn; - char n_sign_posn; - char int_frac_digits; - char int_p_cs_precedes; - char int_n_cs_precedes; - char int_p_sep_by_space; - char int_n_sep_by_space; - char int_p_sign_posn; - char int_n_sign_posn; -}; - -extern char *setlocale(int category, const char *locale); -extern struct lconv *localeconv(void); - -#endif diff --git a/libc/include/signal.h b/libc/include/signal.h @@ -1,10 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _SIGNAL_H -#define _SIGNAL_H - -#include <arch/signal.h> - -void ( *signal(int signum, void (*handler)(int)) ) (int); -int raise(int sig); - -#endif diff --git a/libc/include/stdarg.h b/libc/include/stdarg.h @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STDARG_H -#define _STDARG_H - -#define va_list __builtin_va_list -#define va_start(ap, last) __builtin_va_start(ap, last) -#define va_end(ap) __builtin_va_end(ap) -#define va_copy(to, from) __builtin_va_copy(to, from) -#define va_arg(to, type) __builtin_va_arg(to, type) - -#endif diff --git a/libc/include/stdbool.h b/libc/include/stdbool.h @@ -1,10 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STDBOOL_H -#define _STDBOOL_H - -#define bool _Bool -#define true ((bool) 1) -#define false ((bool) 0) -#define __bool_true_false_are_defined 1 - -#endif diff --git a/libc/include/stddef.h b/libc/include/stddef.h @@ -1,13 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STDDEF_H -#define _STDDEF_H - -#include <arch/stddef.h> - -#ifndef NULL -#define NULL ((void *) 0) -#endif - -#define offsetof(st, m) ((size_t)&(((st *)0)->m)) - -#endif diff --git a/libc/include/stdint.h b/libc/include/stdint.h @@ -1,7 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STDINT_H_ -#define _STDINT_H_ - -#include <arch/stdint.h> - -#endif diff --git a/libc/include/stdio.h b/libc/include/stdio.h @@ -1,102 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STDIO_H -#define _STDIO_H - -#include <arch/stdio.h> - -#ifndef NULL -#define NULL ((void *) 0) -#endif - -#define EOF -1 -#define _IOFBF 0 -#define _IOLBF 1 -#define _IONBF 2 -#define SEEK_CUR 0 -#define SEEK_END 1 -#define SEEK_SET 2 - -typedef struct { - int fd; /* file descriptor */ - char flags; /* bits for must free buffer on close, line-buffered */ - char state; /* last operation was read, write, position, error, eof */ - char *buf; /* pointer to i/o buffer */ - char *rp; /* read pointer (or write end-of-buffer) */ - char *wp; /* write pointer (or read end-of-buffer) */ - char *lp; /* actual write pointer used when line-buffering */ - size_t len; /* actual length of buffer */ - char unbuf[1]; /* tiny buffer for unbuffered io */ -} FILE; - -extern FILE _IO_stream[FOPEN_MAX]; - -#define stderr (&_IO_stream[2]) -#define stdin (&_IO_stream[0]) -#define stdout (&_IO_stream[1]) - -extern int remove(const char *filename); -extern int rename(const char *old, const char *new); -extern FILE *tmpfile(void); -extern char *tmpnam(char *s); -extern int fclose(FILE *fp); -extern int fflush(FILE *fp); -extern FILE *fopen(const char * restrict fname, const char * restrict mode); -extern FILE *freopen(const char * restrict fname, const char * restrict mode, - FILE * restrict fp); -extern void setbuf(FILE * restrict fp, char * restrict buf); -extern int setvbuf(FILE * restrict fp, - char * restrict buf, int mode, size_t size); -extern int fprintf(FILE * restrict fp, const char * restrict fmt, ...); -extern int fscanf(FILE * restrict fp, const char * restrict fmt, ...); -extern int printf(const char * restrict fmt, ...); -extern int scanf(const char * restrict fmt, ...); -extern int snprintf(char * restrict s, - size_t n, const char * restrict fmt, ...); -extern int sprintf(char * restrict s, const char * restrict fmt, ...); -extern int sscanf(const char * restrict s, const char * restrict fmt, ...); - -#ifdef _STDARG_H -extern int vfprintf(FILE * restrict fp, - const char * restrict fmt, va_list arg); -extern int vfscanf(FILE * restrict fp, - const char * restrict fmt, va_list arg); -extern int vprintf(const char * restrict fmt, va_list arg); -extern int vscanf(const char * restrict fmt, va_list arg); -extern int vsnprintf(char * restrict s, size_t n, const char * restrict fmt, - va_list arg); -extern int vsprintf(char * restrict s, - const char * restrict fmt, va_list arg); -extern int vsscanf(const char * restrict s, - const char * restrict fmt, va_list arg); -#endif - -extern int fgetc(FILE *fp); -extern char *fgets(char * restrict s, int n, FILE * restrict fp); -extern int fputc(int c, FILE *fp); -extern int fputs(const char * restrict s, FILE * restrict fp); -extern int getc(FILE *fp); -extern int getchar(void); -extern char *gets(char *s); -extern int putc(int c, FILE *fp); -extern int putchar(int c); -extern int puts(const char *s); -extern int ungetc(int c, FILE *fp); -extern size_t fread(void * restrict ptr, size_t size, size_t nmemb, - FILE * restrict fp); -extern size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, - FILE * restrict fp); -extern int fgetpos(FILE * restrict fp, fpos_t * restrict pos); -extern int fseek(FILE *fp, long int offset, int whence); -extern int fsetpos(FILE *fp, const fpos_t *pos); -extern long int ftell(FILE *fp); -extern void rewind(FILE *fp); -extern void clearerr(FILE *fp); -extern int feof(FILE *fp); -extern int ferror(FILE *fp); -extern void perror(const char *s); - -#ifdef __USE_MACROS -#define printf(...) fprintf(stdout, __VA_ARGS__) -#endif - -#endif diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h @@ -1,70 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STDLIB_H -#define _STDLIB_H - -#include <arch/stdlib.h> - -#ifndef NULL -#define NULL ((void *) 0) -#endif - -#define _ATEXIT_MAX 32 - -#define MB_CUR_MAX 1 -#define RAND_MAX 32767 - -typedef struct { - int quot, rem; -} div_t; - -typedef struct { - long quot, rem; -} ldiv_t; - -typedef struct { - long long quot, rem; -} lldiv_t; - -extern double atof(const char *nptr); -extern int atoi(const char *nptr); -extern long int atol(const char *nptr); -extern long long int atoll(const char *nptr); -extern double strtod(const char * restrict nptr, char ** restrict endptr); -extern float strtof(const char * restrict nptr, char ** restrict endptr); -extern long double strtold(const char * restrict nptr, char ** restrict endptr); -extern long int strtol(const char * restrict nptr, char ** restrict endptr, int base); -extern long long int strtoll(const char * restrict nptr, char ** restrict endptr, - int base); -extern unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, - int base); -extern unsigned long long int strtoull(const char * restrict nptr, - char ** restrict endptr, int base); -extern int rand(void); -extern void srand(unsigned int seed); -extern void *calloc(size_t nmemb, size_t size); -extern void free(void *ptr); -extern void *malloc(size_t size); -extern void *realloc(void *ptr, size_t size); -extern void abort(void); -extern int atexit(void (*func)(void)); -extern void exit(int status); -extern void _Exit(int status); -extern char *getenv(const char *name); -extern int system(const char *string); -extern void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, - int (*compar)(const void *, const void *)); -extern void qsort(void *base, size_t nmemb, size_t size, - int (*compar)(const void *, const void *)); -extern int abs(int j); -extern long int labs(long int j); -extern long long int llabs(long long int j); -extern div_t div(int numer, int denom); -extern ldiv_t ldiv(long int numer, long int denom); -extern lldiv_t lldiv(long long int numer, long long int denom); -extern int mblen(const char *s, size_t n); -extern int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n); -extern int wctomb(char *s, wchar_t wchar); -extern size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); -extern size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); - -#endif diff --git a/libc/include/string.h b/libc/include/string.h @@ -1,34 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef _STRING_H -#define _STRING_H - -#include <arch/string.h> - -#ifndef NULL -#define NULL ((void *) 0) -#endif - -extern void *memcpy(void * restrict s1, const void * restrict s2, size_t n); -extern void *memmove(void *s1, const void *s2, size_t n); -extern char *strcpy(char * restrict s1, const char * restrict s2); -extern char *strncpy(char * restrict s1, const char * restrict s2, size_t n); -extern char *strcat(char * restrict s1, const char * restrict s2); -extern char *strncat(char * restrict s1, const char * restrict s2, size_t n); -extern int memcmp(const void *s1, const void *s2, size_t n); -extern int strcmp(const char *s1, const char *s2); -extern int strcoll(const char *s1, const char *s2); -extern int strncmp(const char *s1, const char *s2, size_t n); -extern size_t strxfrm(char * restrict s1, const char * restrict s2, size_t n); -extern void *memchr(const void *s, int c, size_t n); -extern char *strchr(const char *s, int c); -extern size_t strcspn(const char *s1, const char *s2); -extern char *strpbrk(const char *s1, const char *s2); -extern char *strrchr(const char *s, int c); -extern size_t strspn(const char *s1, const char *s2); -extern char *strstr(const char *s1, const char *s2); -extern char *strtok(char * restrict s1, const char * restrict s2); -extern void *memset(void *s, int c, size_t n); -extern char *strerror(int errnum); -extern size_t strlen(const char *s); - -#endif diff --git a/libc/include/time.h b/libc/include/time.h @@ -1,38 +0,0 @@ -#ifndef _TIME_H -#define _TIME_H - -#include <arch/time.h> - -#ifndef NULL -#define NULL ((void *) 0) -#endif - -#define CLOCKS_PER_SEC 1000000 - -typedef long int clock_t; - -struct tm { - int tm_sec; - int tm_min; - int tm_hour; - int tm_mday; - int tm_mon; - int tm_year; - int tm_wday; - int tm_yday; - int tm_isdst; -}; - -extern clock_t clock(void); -extern double difftime(time_t time1, time_t time0); -extern time_t mktime(struct tm *timeptr); -extern time_t time(time_t *timer); -extern char *asctime(const struct tm *timeptr); -extern char *ctime(const time_t *timer); -extern struct tm *gmtime(const time_t *timer); -extern struct tm *localtime(const time_t *timer); -extern size_t strftime(char * restrict s, size_t maxsize, - const char * restrict format, - const struct tm * restrict timeptr); - -#endif diff --git a/libc/src/Makefile b/libc/src/Makefile @@ -1,44 +0,0 @@ -# See LICENSE file for copyright and license details. -.POSIX: - -include ../../config.mk - -SYSDIR = sys/$(ARCH)-$(SYS) -SYSINC = $(SYSDIR)/system.mk - -SYSINCLUDE = /dev/null -include $(SYSINCLUDE) - -LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \ - strrchr.o strcat.o strncmp.o strncpy.o strncat.o strcoll.o \ - strxfrm.o strstr.o strspn.o strcspn.o strpbrk.o strtok.o \ - memset.o memcpy.o memmove.o memcmp.o memchr.o \ - isalnum.o isalpha.o isascii.o isblank.o iscntrl.o isdigit.o \ - isgraph.o islower.o isprint.o ispunct.o isspace.o isupper.o \ - isxdigit.o toupper.o tolower.o ctype.o setlocale.o \ - localeconv.o atoi.o atexit.o exit.o \ - printf.o fprintf.o vfprintf.o \ - realloc.o calloc.o malloc.o -LIBCSYSOBJ = $(SYSOBJ:%=$(SYSDIR)/%) -OBJ = $(LIBCOBJ) $(LIBCSYSOBJ) - -all: all.bootstrap -all.start: libc.a - -clean: clean.bootstrap -clean.start: - rm -f *.o libc.a - cd $(SYSDIR) && $(MAKE) clean - -all.bootstrap clean.bootstrap: $(SYSINC) - $(MAKE) -e SYSINCLUDE="$(SYSINC)" $(@:%.bootstrap=%.start) - -$(SYSINC): $(SYSDIR)/Makefile $(SYSDIR)/syscall.lst - cd $(SYSDIR) && $(MAKE) -e system.mk - -libc.a: $(SYSINC) $(OBJ) - $(AR) $(ARFLAGS) $@ $(OBJ) - ranlib $@ - -$(LIBCSYSOBJ): - cd $(SYSDIR) && $(MAKE) diff --git a/libc/src/assert.c b/libc/src/assert.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <assert.h> -#include <stdlib.h> -#include <stdio.h> - -void __assert(char *exp, char *file, long line) -{ - fprintf(stderr, "%s:%ld: assertion failed '%s'\n", file, line, exp); - abort(); -} diff --git a/libc/src/atexit.c b/libc/src/atexit.c @@ -1,19 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdlib.h> -#include <errno.h> -#undef atexit - -extern void (*_exitf[_ATEXIT_MAX])(void); -extern unsigned _exitn; - -int -atexit(void (*fun)(void)) -{ - if (_exitn == _ATEXIT_MAX) { - errno = ENOMEN; - return -1; - } - _exitf[_exitn++] = fun; - return 0; -} diff --git a/libc/src/atoi.c b/libc/src/atoi.c @@ -1,28 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <ctype.h> -#include <stdlib.h> -#undef atoi - -int -atoi(const char *s) -{ - int n, sign = -1; - - while(isspace(*s)) - ++s; - - switch(*s) { - case '-': - sign = 1; - case '+': - ++s; - } - - /* Compute n as a negative number to avoid overflow on INT_MIN */ - for (n = 0; isdigit(*s); ++s) - n = 10*n - (*s - '0'); - - return sign * n; -} - diff --git a/libc/src/calloc.c b/libc/src/calloc.c @@ -1,19 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdlib.h> -#include <string.h> - -void * -calloc(size_t nmemb, size_t size) -{ - size_t nbytes; - void *mem; - - if (!nmemb || !size || nmemb > (size_t)-1/size) - return NULL; - - nbytes = nmemb * size; - if ((mem = malloc(nbytes)) == NULL) - return NULL; - return memset(mem, 0, nbytes); -} diff --git a/libc/src/ctype.c b/libc/src/ctype.c @@ -1,24 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef ctype - -unsigned char __ctype[255] = { - _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ - _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ - _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ - _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ - _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ - _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ - _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ - _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ - _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ - _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ - _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ - _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ - _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ - _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ - _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ - _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ -}; diff --git a/libc/src/exit.c b/libc/src/exit.c @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdlib.h> -#undef exit - -void (*_exitf[_ATEXIT_MAX])(void); -unsigned _exitn; - -void -exit(int status) -{ - while (_exitn > 0) - (*_exitf[--_exitn])(); - _Exit(status); -} diff --git a/libc/src/fprintf.c b/libc/src/fprintf.c @@ -1,17 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdarg.h> -#include <stdio.h> -#undef fprintf - -int -fprintf(FILE * restrict fp, const char * restrict fmt, ...) -{ - va_list va; - int cnt; - - va_start(va, fmt); - cnt = vfprintf(fp, fmt, va); - va_end(va); - return cnt; -} diff --git a/libc/src/isalnum.c b/libc/src/isalnum.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isalnum - -int -isalnum(int c) -{ - return __ctype[(unsigned char) c] & (_U|_L|_D); -} diff --git a/libc/src/isalpha.c b/libc/src/isalpha.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isalpha - -int -isalpha(int c) -{ - return __ctype[(unsigned char) c] & (_U|_L); -} diff --git a/libc/src/isascii.c b/libc/src/isascii.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isascii - -int -isascii(int c) -{ - return c <= 0x7f; -} diff --git a/libc/src/isblank.c b/libc/src/isblank.c @@ -1,7 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -int -isblank(int c) -{ - return (c == ' ') || (c == '\t'); -} diff --git a/libc/src/iscntrl.c b/libc/src/iscntrl.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef iscntrl - -int -iscntrl(int c) -{ - return __ctype[(unsigned char) c] & (_C); -} diff --git a/libc/src/isdigit.c b/libc/src/isdigit.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isdigit - -int -isdigit(int c) -{ - return __ctype[(unsigned char) c] & (_D); -} diff --git a/libc/src/isgraph.c b/libc/src/isgraph.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isgraph - -int -isgraph(int c) -{ - return __ctype[(unsigned char) c] & (_P|_U|_L|_D); -} diff --git a/libc/src/islower.c b/libc/src/islower.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef islower - -int -islower(int c) -{ - return __ctype[(unsigned char) c] & _L; -} diff --git a/libc/src/isprint.c b/libc/src/isprint.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isprint - -int -isprint(int c) -{ - return __ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP); -} diff --git a/libc/src/ispunct.c b/libc/src/ispunct.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef ispunct - -int -ispunct(int c) -{ - return __ctype[(unsigned char) c] & (_P); -} diff --git a/libc/src/isspace.c b/libc/src/isspace.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isspace - -int -isspace(int c) -{ - return __ctype[(unsigned char) c] & _S; -} diff --git a/libc/src/isupper.c b/libc/src/isupper.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isupper - -int -isupper(int c) -{ - return __ctype[(unsigned char) c] & _U; -} diff --git a/libc/src/isxdigit.c b/libc/src/isxdigit.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef isxdigit - -int -isxdigit(int c) -{ - return __ctype[(unsigned char) c] & (_D|_X); -} diff --git a/libc/src/localeconv.c b/libc/src/localeconv.c @@ -1,14 +0,0 @@ -#include <locale.h> -#include <limits.h> -#undef localeconv - -struct lconv * -localeconv(void) -{ - static struct lconv lc = { ".", "", "", "", "", "", "", "", "", "", - CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, - CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, - CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, - CHAR_MAX, CHAR_MAX }; - return &lc; -} diff --git a/libc/src/malloc.c b/libc/src/malloc.c @@ -1,154 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#include "malloc.h" -#include "syscall.h" - -#define MAXADDR ((char *)-1) -#define ERRADDR ((char *)-1) - -extern char end[]; -static Header base = { .h.next = &base }; -static Header *freep = &base; -static char *heap = end; - -/* - * Run over the free list looking for the nearest previous - * block. There are two possible results: end of the list - * or an intermediary block. - */ -void * -_prevchunk(Header *hp) -{ - Header *p; - - for (p = freep; ;p = p->h.next) { - /* hp between p and p->h.next? */ - if (p < hp && hp < p->h.next) - break; - /* p before hp and hp at the end of list? */ - if (p->h.next <= p && (hp < p->h.next || hp > p)) - break; - } - return p; -} - -/* - * Get the previous block and try to merge - * with next and previous blocks - */ -void -free(void *mem) -{ - Header *hp, *prev; - - if (!mem) - return; - - hp = (Header *) mem - 1; - prev = _prevchunk(hp); - - /* join to next */ - if (hp + hp->h.size == prev->h.next) { - hp->h.size += prev->h.next->h.size; - hp->h.next = prev->h.next->h.next; - } else { - hp->h.next = prev->h.next; - } - - /* join to previous */ - if (prev + prev->h.size == hp) { - prev->h.size += hp->h.size; - prev->h.next = hp->h.next; - } else { - prev->h.next = hp; - } - - freep = prev; -} - -static void * -sbrk(uintptr_t inc) -{ - char *new, *old = heap; - - if (old >= MAXADDR - inc) - return ERRADDR; - - new = old + inc; - if (_brk(new) < 0) - return ERRADDR; - heap = new; - - return old; -} - -static Header * -morecore(size_t nunits) -{ - char *rawmem; - Header *hp; - - if (nunits < NALLOC) - nunits = NALLOC; - - rawmem = sbrk(nunits * sizeof(Header)); - if (rawmem == ERRADDR) - return NULL; - - hp = (Header*)rawmem; - hp->h.size = nunits; - - /* integrate new memory into the list */ - free(hp + 1); - - return freep; -} - -/* - * Run over the list of free blocks trying to find a block - * big enough for nbytes. If the block fit perfectly with - * the required size then we only have to unlink - * the block. Otherwise we have to split the block and - * return the right part. If we run over the full list - * without a fit then we have to require more memory - * - * ______________________________________ - * ___________./______________________________________\_____ - * ...| in | | | in | |.....| in | | | |.... - * ...| use | | | use | |.....| use | | | |.... - * ___|______|___|.____|_____|._|_____|______|._|.___|.|____ - * \__/ \_________/ \_____________/ \/ \__/ - */ -void * -malloc(size_t nbytes) -{ - Header *cur, *prev; - size_t nunits; - - /* 1 unit for header plus enough units to fit nbytes */ - nunits = (nbytes+sizeof(Header)-1) / sizeof(Header) + 1; - - for (prev = freep; ; prev = cur) { - cur = prev->h.next; - if (cur->h.size >= nunits) { - if (cur->h.size == nunits) { - prev->h.next = cur->h.next; - } else { - cur->h.size -= nunits; - cur += cur->h.size; - cur->h.size = nunits; - } - freep = prev; - return cur + 1; - } - - if (cur == freep) { - if ((cur = morecore(nunits)) == NULL) - return NULL; - } - } -} diff --git a/libc/src/malloc.h b/libc/src/malloc.h @@ -1,18 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdlib.h> - -/* minimum amount of required units */ -#define NALLOC 10000 - -typedef union header Header; -union header { - struct hdr { - Header *next; - size_t size; - } h; - /* most restrictive type fixes the union size for alignment */ - _ALIGNTYPE most; -}; - -extern void *_prevchunk(Header *hp); diff --git a/libc/src/memchr.c b/libc/src/memchr.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef memchr - -void * -memchr(const void *s, int c, size_t n) -{ - unsigned char *bp = (char *) s; - - while (n > 0 && *bp++ != c) - --n; - return (n == 0) ? NULL : bp-1; -} diff --git a/libc/src/memcmp.c b/libc/src/memcmp.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef memcmp - -int -memcmp(const void *s1, const void *s2, size_t n) -{ - char *s = (char *) s1, *t = (char *) s2; - - while (n > 0 && *s == *t) - --n, ++s, ++t; - return n ? (*s - *t) : 0; -} diff --git a/libc/src/memcpy.c b/libc/src/memcpy.c @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef memcpy - -void * -memcpy(void * restrict dst, const void * restrict src, size_t n) -{ - char *s1 = dst; - const char *s2 = src; - - while (n-- > 0) - *s1++ = *s2++; - return dst; -} diff --git a/libc/src/memmove.c b/libc/src/memmove.c @@ -1,20 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef memmove - -void * -memmove(void *dst, const void *src, size_t n) -{ - char *d = dst, *s = (char *) src; - - if (d < s) { - while (n-- > 0) - *d++ = *s++; - } else { - s += n-1, d += n-1; - while (n-- > 0) - *d-- = *s--; - } - return dst; -} diff --git a/libc/src/memset.c b/libc/src/memset.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef memset - -void * -memset(void *s, int c, size_t n) -{ - char *m = s; - - while (n-- > 0) - *m++ = c; - return s; -} diff --git a/libc/src/printf.c b/libc/src/printf.c @@ -1,17 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdarg.h> -#include <stdio.h> -#undef printf - -int -printf(const char * restrict fmt, ...) -{ - int cnt; - va_list va; - - va_start(va, fmt); - cnt = vfprintf(stdin, fmt, va); - va_end(va); - return cnt; -} diff --git a/libc/src/rand.c b/libc/src/rand.c @@ -1,19 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdlib.h> -#undef rand - -static int next; - -void -srand(unsigned seed) -{ - next = seed; -} - -int -rand(void) /* RAND_MAX assumed to be 32767. */ -{ - next = next * 1103515245 + 12345; - return (unsigned)(next/65536) % 32768; -} diff --git a/libc/src/realloc.c b/libc/src/realloc.c @@ -1,69 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdlib.h> -#include <string.h> - -#include "malloc.h" - -void * -realloc(void *ptr, size_t nbytes) -{ - Header *oh, *prev, *next, *new; - size_t nunits, avail, onbytes, n; - - if (!nbytes) - return NULL; - - if (!ptr) - return malloc(nbytes); - - nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1; - oh = (Header*)ptr - 1; - - if (oh->h.size == nunits) - return ptr; - - new = oh + nunits; - - if (nunits < oh->h.size - 1) { - new->h.size = oh->h.size - nunits; - oh->h.size = nunits; - free(new + 1); - return oh; - } - - prev = _prevchunk(oh); - - if (oh + oh->h.size == prev->h.next) { - /* - * if there is free space adjacent - * to the current memory - */ - next = prev->h.next; - avail = oh->h.size + next->h.size; - - if (avail == nunits) { - oh->h.size = nunits; - prev->h.next = next->h.next; - return oh; - } - - if (avail > nunits) { - oh->h.size = nunits; - prev->h.next = new; - new->h.next = next; - new->h.size = avail - nunits; - return oh; - } - } - - onbytes = (oh->h.size - 1) * sizeof(Header); - if ((new = malloc(nbytes)) == NULL) - return NULL; - - n = (onbytes > nbytes) ? nbytes : onbytes; - memcpy(new, ptr, n); - free(ptr); - - return new; -} diff --git a/libc/src/setlocale.c b/libc/src/setlocale.c @@ -1,18 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <locale.h> -#include <stddef.h> -#undef setlocale - -char * -setlocale(int category, const char *locale) -{ - if (category > LC_TIME || category < LC_ALL) - return NULL; - if (!locale || - locale[0] == '\0' || - locale[0] == 'C' && locale[1] == '\0') { - return "C"; - } - return NULL; -} diff --git a/libc/src/strcat.c b/libc/src/strcat.c @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strcat - -char * -strcat(char * restrict dst, const char * restrict src) -{ - char *ret = dst; - - while (*dst) - ++dst; - while (*dst++ = *src++) - /* nothing */; - return ret; -} diff --git a/libc/src/strchr.c b/libc/src/strchr.c @@ -1,12 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strchr - -char * -strchr(const char *s, int c) -{ - while (*s && *s != c) - ++s; - return (*s == c) ? (char *)s : NULL; -} diff --git a/libc/src/strcmp.c b/libc/src/strcmp.c @@ -1,12 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strcmp - -int -strcmp(const char *s1, const char *s2) -{ - while (*s1 && *s2 && *s1 == *s2) - ++s1, ++s2; - return *(unsigned char *)s1 - *(unsigned char *)s2; -} diff --git a/libc/src/strcoll.c b/libc/src/strcoll.c @@ -1,12 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strcoll - -int -strcoll(const char *s1, const char *s2) -{ - while (*s1 && *s2 && *s1 == *s2) - ++s1, ++s2; - return *(unsigned char *) s1 - *(unsigned char *) s2; -} diff --git a/libc/src/strcpy.c b/libc/src/strcpy.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strcpy - -char * -strcpy(char * restrict dst, const char * restrict src) -{ - char *ret = dst; - - while (*dst++ = *src++) - /* nothing */; - return ret; -} diff --git a/libc/src/strcspn.c b/libc/src/strcspn.c @@ -1,20 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strcspn - -size_t -strcspn(const char *s1, const char *s2) -{ - size_t n; - int c; - const char *p; - - for (n = 0; c = *s1++; ++n) { - for (p = s2; *p && *p != c; ++p) - /* nothing */; - if (*p == c) - break; - } - return n; -} diff --git a/libc/src/strlen.c b/libc/src/strlen.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strlen - -size_t -strlen(const char *s) -{ - const char *t; - - for (t = s; *t; ++t) - /* nothing */; - return t - s; -} diff --git a/libc/src/strncat.c b/libc/src/strncat.c @@ -1,17 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strncat - -char * -strncat(char * restrict dst, const char * restrict src, size_t n) -{ - char *ret = dst; - - while (*dst) - ++dst; - while (n-- > 0 && *src) - *dst++ = *src++; - *dst = '\0'; - return ret; -} diff --git a/libc/src/strncmp.c b/libc/src/strncmp.c @@ -1,12 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strncmp - -int -strncmp(const char *s1, const char *s2, size_t n) -{ - for (; n && *s1 && *s2 && *s1 == *s2; --n, ++s1, ++s2); - /* nothing */; - return n ? (*(unsigned char *)s1 - *(unsigned char *)s2) : 0; -} diff --git a/libc/src/strncpy.c b/libc/src/strncpy.c @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strncpy - -char * -strncpy(char * restrict dst, const char * restrict src, size_t n) -{ - char *ret = dst; - - for (; n > 0 && *src; --n) - *dst++ = *src++; - while (n-- > 0) - *dst++ = '\0'; - return ret; -} diff --git a/libc/src/strpbrk.c b/libc/src/strpbrk.c @@ -1,19 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strpbrk - -char * -strpbrk(const char *s1, const char *s2) -{ - int c; - const char *p; - - for (; c = *s1; ++s1) { - for (p = s2; *p && *p != c; ++p) - /* nothing */; - if (*p == c) - return s1; - } - return NULL; -} diff --git a/libc/src/strrchr.c b/libc/src/strrchr.c @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strrchr - -char * -strrchr(const char *s, int c) -{ - const char *t = s; - - while (*t) - ++t; - while (t > s && *t != c) - --t; - return (*t == c) ? (char *)t : NULL; -} diff --git a/libc/src/strspn.c b/libc/src/strspn.c @@ -1,20 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strspn - -size_t -strspn(const char *s1, const char *s2) -{ - size_t n; - int c; - const char *p; - - for (n = 0; c = *s1++; ++n) { - for (p = s2; *p && *p != c; ++p) - /* nothing */; - if (*p == '\0') - break; - } - return n; -} diff --git a/libc/src/strstr.c b/libc/src/strstr.c @@ -1,28 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strstr - -char * -strstr(const char *s1, const char *s2) -{ - const char *p, *q; - int c; - - c = *s2++; - if (c == '\0') - return (char *) s1; - - while (*s1) { - if (*s1 != c) { - ++s1; - } else { - p = s1++; - for (q = s2; *q && *s1 == *q; ++s1, ++q) - /* nothing */; - if (*q == '\0') - return (char *) p; - } - } - return NULL; -} diff --git a/libc/src/strtok.c b/libc/src/strtok.c @@ -1,27 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strtok - -char * -strtok(char * restrict s, const char * restrict delim) -{ - static char *line; - - if (s) - line = s; - if (!s && !line) - return NULL; - - s = line + strspn(line, delim); - if (*s == '\0') - return line = NULL; - - line = s + strcspn(s, delim); - if (*line != '\0') - *line++ = '\0'; - else - line = NULL; - - return s; -} diff --git a/libc/src/strxfrm.c b/libc/src/strxfrm.c @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <string.h> -#undef strxfrm - -size_t -strxfrm(char * restrict dst, const char * restrict src, size_t n) -{ - size_t len = strlen(src); - - if (len < n) - strcpy(dst, src); - return len; -} diff --git a/libc/src/sys/amd64-sysv-linux/Makefile b/libc/src/sys/amd64-sysv-linux/Makefile @@ -1,37 +0,0 @@ -# See LICENSE file for copyright and license details. -.POSIX: - -include ../../../../config.mk - -SYSTEMINC = /dev/null -include $(SYSTEMINC) -SYSGENINC = /dev/null -include $(SYSGENINC) - -GENASM = $(GENOBJ:.o=.s) -OBJ = - -all: all.bootstrap -all.start: $(SYSOBJ) - -clean: clean.bootstrap -clean.start: - rm -f *.o $(GENASM) system.mk sysgen.mk - -all.bootstrap clean.bootstrap: system.mk sysgen.mk - $(MAKE) -e SYSTEMINC=system.mk SYSGENINC=sysgen.mk \ - $(@:%.bootstrap=%.start) - -system.mk: Makefile syscall.lst - printf '%s ' 'SYSOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - $(OBJ) \ - > $@ - -sysgen.mk: syscall.lst - printf '%s ' 'GENOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - > $@ - -$(GENASM): syscall.lst - awk -f syscall.awk < syscall.lst diff --git a/libc/src/sys/amd64-sysv-linux/syscall.awk b/libc/src/sys/amd64-sysv-linux/syscall.awk @@ -1,12 +0,0 @@ -# This job is very easy because app and kernel ABI are identical -# until the 4th parameter, so we only have to set the syscall -# number in rax - -/^#/ {next} - {name=$2 ".s" - printf ".global %s\n" \ - "%s:\n" \ - "\tmovq\t$%d,%%rax\n" \ - "\tsyscall\n" \ - "\tret\n", $2, $2, $1 > name - close(name)} diff --git a/libc/src/sys/amd64-sysv-linux/syscall.lst b/libc/src/sys/amd64-sysv-linux/syscall.lst @@ -1,11 +0,0 @@ -#number name -0 _read -1 _write -2 _open -3 _close -3 _lseek -12 _brk -13 _rt_sigaction -38 _getpid -60 _Exit -32 _kill diff --git a/libc/src/sys/i386-sysv-linux/Makefile b/libc/src/sys/i386-sysv-linux/Makefile @@ -1,37 +0,0 @@ -# See LICENSE file for copyright and license details. -.POSIX: - -include ../../../../config.mk - -SYSTEMINC = /dev/null -include $(SYSTEMINC) -SYSGENINC = /dev/null -include $(SYSGENINC) - -GENASM = $(GENOBJ:.o=.s) -OBJ = - -all: all.bootstrap -all.start: $(SYSOBJ) - -clean: clean.bootstrap -clean.start: - rm -f *.o $(GENASM) system.mk sysgen.mk - -all.bootstrap clean.bootstrap: system.mk sysgen.mk - $(MAKE) -e SYSTEMINC=system.mk SYSGENINC=sysgen.mk \ - $(@:%.bootstrap=%.start) - -system.mk: Makefile syscall.lst - printf '%s ' 'SYSOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - $(OBJ) \ - > $@ - -sysgen.mk: syscall.lst - printf '%s ' 'GENOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - > $@ - -$(GENASM): syscall.lst - awk -f syscall.awk < syscall.lst diff --git a/libc/src/sys/i386-sysv-linux/syscall.lst b/libc/src/sys/i386-sysv-linux/syscall.lst diff --git a/libc/src/sys/qbe-linux/Makefile b/libc/src/sys/qbe-linux/Makefile @@ -1,37 +0,0 @@ -# See LICENSE file for copyright and license details. -.POSIX: - -include ../../../../config.mk - -SYSTEMINC = /dev/null -include $(SYSTEMINC) -SYSGENINC = /dev/null -include $(SYSGENINC) - -GENASM = $(GENOBJ:.o=.s) -OBJ = - -all: all.bootstrap -all.start: $(SYSOBJ) - -clean: clean.bootstrap -clean.start: - rm -f *.o $(GENASM) system.mk sysgen.mk - -all.bootstrap clean.bootstrap: system.mk sysgen.mk - $(MAKE) -e SYSTEMINC=system.mk SYSGENINC=sysgen.mk \ - $(@:%.bootstrap=%.start) - -system.mk: Makefile syscall.lst - printf '%s ' 'SYSOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - $(OBJ) \ - > $@ - -sysgen.mk: syscall.lst - printf '%s ' 'GENOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - > $@ - -$(GENASM): syscall.lst - awk -f syscall.awk < syscall.lst diff --git a/libc/src/sys/qbe-linux/syscall.awk b/libc/src/sys/qbe-linux/syscall.awk @@ -1,12 +0,0 @@ -# This job is very easy because app and kernel ABI are identical -# until the 4th parameter, so we only have to set the syscall -# number in rax - -/^#/ {next} - {name=$2 ".s" - printf ".global %s\n" \ - "%s:\n" \ - "\tmovq\t$%d,%%rax\n" \ - "\tsyscall\n" \ - "\tret\n", $2, $2, $1 > name - close(name)} diff --git a/libc/src/sys/qbe-linux/syscall.lst b/libc/src/sys/qbe-linux/syscall.lst @@ -1,11 +0,0 @@ -#number name -0 _read -1 _write -2 _open -3 _close -3 _lseek -12 _brk -13 _rt_sigaction -38 _getpid -60 _Exit -32 _kill diff --git a/libc/src/sys/z80-linux/Makefile b/libc/src/sys/z80-linux/Makefile @@ -1,37 +0,0 @@ -# See LICENSE file for copyright and license details. -.POSIX: - -include ../../../../config.mk - -SYSTEMINC = /dev/null -include $(SYSTEMINC) -SYSGENINC = /dev/null -include $(SYSGENINC) - -GENASM = $(GENOBJ:.o=.s) -OBJ = - -all: all.bootstrap -all.start: $(SYSOBJ) - -clean: clean.bootstrap -clean.start: - rm -f *.o $(GENASM) system.mk sysgen.mk - -all.bootstrap clean.bootstrap: system.mk sysgen.mk - $(MAKE) -e SYSTEMINC=system.mk SYSGENINC=sysgen.mk \ - $(@:%.bootstrap=%.start) - -system.mk: Makefile syscall.lst - printf '%s ' 'SYSOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - $(OBJ) \ - > $@ - -sysgen.mk: syscall.lst - printf '%s ' 'GENOBJ =' \ - "$$(awk '/^#/{next}{printf "%s.o ", $$2}' syscall.lst)" \ - > $@ - -$(GENASM): syscall.lst - awk -f syscall.awk < syscall.lst diff --git a/libc/src/sys/z80-linux/syscall.lst b/libc/src/sys/z80-linux/syscall.lst diff --git a/libc/src/syscall.h b/libc/src/syscall.h @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -extern void *_brk(void *addr); -extern int _open(char *path, int flags, int perm); -extern int _close(int fd); -extern int _read(int fd, void *buf, size_t n); -extern int _write(int fd, void *buf, size_t n); -extern int _lseek(int fd, long off, int whence); -extern void _Exit(int status); -extern int raise(int sig); -extern void (*signal(int sig, void (*func)(int)))(int); diff --git a/libc/src/tolower.c b/libc/src/tolower.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef tolower - -int -tolower(int c) -{ - return (isupper(c)) ? c | 0x20 : c; -} diff --git a/libc/src/toupper.c b/libc/src/toupper.c @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define __USE_MACROS -#include <ctype.h> -#undef toupper - -int -toupper(int c) -{ - return (islower(c)) ? c & ~0x20 : c; -} diff --git a/libc/src/vfprintf.c b/libc/src/vfprintf.c @@ -1,76 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <stdarg.h> -#include <stdio.h> -#undef vfprintf - -static int -printn2(FILE * restrict fp, unsigned n, int base) -{ - unsigned t; - int cnt = 0; - static char digits[] = "0123456789ABCDEF"; - - if ((t = n / base) != 0) - cnt += printn2(fp, t, base); - putc(digits[n % base], fp); - return cnt + 1; -} - -static int -printn(FILE * restrict fp, int n, int b, int sign) -{ - int cnt = 0; - - if (sign && n < 0) { - n = -n; - putc('-', fp); - ++cnt; - } - cnt += printn2(fp, n, b); - return cnt; -} - -int -vfprintf(FILE * restrict fp, const char *fmt, va_list va) -{ - int c, base, sign, cnt = 0; - char *s; - - while ((c = *fmt++) != '\0') { - if (c == '%') { - sign = 0; - switch (*fmt++) { - case '%': - c = '%'; - break; - case 'c': - c = va_arg(va, int); - break; - case 'o': - base = 8; - goto numeric; - case 'd': - sign = 1; - base = 10; - goto numeric; - case 'x': - base = 16; - numeric: - c = va_arg(va, int); - cnt += printn(fp, c, base, sign); - continue; - case 's': - s = va_arg(va, char *); - while ((c = *s++) != '\0') - putc(c, fp); - /* passthrou */ - default: - continue; - } - } - putc(c, fp); - ++cnt; - } - return cnt; -} diff --git a/tests/Makefile b/tests/Makefile @@ -1,5 +1,5 @@ .POSIX: -all clean: +all clean dep distclean: cd error && make -e $@ cd execute && make -e $@ diff --git a/tests/execute/Makefile b/tests/execute/Makefile @@ -6,3 +6,6 @@ tests: CFLAGS='' SCCEXECPATH=../../bin PATH=../../bin:$$PATH ./chktest.sh < scc-tests.lst clean: rm -f *.as *.o *.ir *.qbe *core test.log + +distclean: clean +dep: