diff --git a/.gitignore b/.gitignore index 9bb5861..57e4c63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o secd +secd_debug diff --git a/Makefile b/Makefile index 85ca0a5..b4c16f7 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,19 @@ .POSIX: -.PHONY: all clean +.PHONY: all debug run clean DEPS=src/main.o src/util.o +LIBS=-lgc +DBGFLAGS=-Og -g +DBGBIN=secd_debug all: secd secd: $(DEPS) - $(CC) $(CFLAGS) $(DEPS) -o secd + $(CC) $(LIBS) $(CFLAGS) $(DEPS) -o secd + +$(DBGBIN): $(DEPS) + $(CC) $(LIBS) $(DBGFLAGS) -DENABLE_DBG_H -o secd_debug + +debug: $(DBGBIN) run: secd @./secd diff --git a/README.md b/README.md index fdf426f..64a5dd4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # SECD This is a (wip) implementation of a SECD machine written in C. For more information on what a SECD machine is, and what the goals of this project is, read [my blogpost on secd machines](https://depsterr.com/blog/2021/secd1.html). + +## DEPENDS + +* Bohem-Demer-Weiser garbage collector [web](https://www.hboehm.info/gc/) [git](https://github.com/ivmai/bdwgc) diff --git a/doc/ideas.md b/doc/ideas.md new file mode 100644 index 0000000..436fff9 --- /dev/null +++ b/doc/ideas.md @@ -0,0 +1,49 @@ +# Goals + +* Be an efficent compilation target for typed lambda calculi + +* Be substantially easier to translate to than machine code. + +* Run cross-platform on as many operating systems and architectures as possible. (bare minimum 32-bit). + +* Allow for code completely independent of the host machine. + +* Leave memory managment out of the question for the compiler writer. + +* Provide significant IO functionality. + +* Have methods for representing algebraic data types built into the machine. + +* Provide efficient native methods for manipulating commonly used types + * Strings (don't represent as a list of characters, god) + * Characters + * Integers + * Floating point numbers + * Raw bytes + * Lists (cons linked list), efficient map and foldr builtin would be really nice + * Arrays (constant lookup continous memory) + +# Non-Goals + +* Safely error out or leave catchable exceptions when instruction is called with wrongful operands. + +* Dynamic linking, creating object files with polymorphic types. + +* Lazy evaluation. + +# Nicities (plausible) + +* Provide a compiler from a simply typed lambda calculus, coupled with a small standard library which would + confirm type safety of all used instructions. Maybe go even further? System F? + +* Maybe some vector operations? You could reasonably operate on chunks of the stack. + +# Nicities (dreaming) + +* Provide more complete IO functionality, such as networking, etc. (this is not likely to happen) + +* Provide some sort of FFI (this is not likely to happen) + +* It's possible one might be able to JIT compile some stuff? + +# Ideas diff --git a/src/main.c b/src/main.c index 5cc36eb..0f1d4c7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,23 @@ #include +#include +#include + +#include + #include "dbg.h" +#include "util.h" int main(int argc, char** argv) { + GC_INIT(); + + if (argc < 2) { + die("Not enough args"); + } + + size_t size; + uint8_t* buf; + + buf = readFile(argv[1], &size); + + fwrite(buf, 1, size, stdout); } diff --git a/src/util.c b/src/util.c index 9b16e60..b764a65 100644 --- a/src/util.c +++ b/src/util.c @@ -13,7 +13,7 @@ // print message to stderr and then exit void die(const char* s) { - fprintf(stderr, "FATAL ERROR: %s.\n", s); + fprintf(stderr, "ERROR: %s.\n", s); exit(1); } @@ -23,27 +23,6 @@ void diePerror(const char* s) { exit(1); } -// malloc which dies on failure -void* xmalloc(size_t bytes) { - void* p = malloc(bytes); - GUARD(p); - return p; -} - -// realloc which dies on failure -void* xrealloc(void* p, size_t bytes) { - p = realloc(p, bytes); - GUARD(p); - return p; -} - -// free which is garantueed null pointer safe -void xfree(void* p) { - if (p) { - free(p); - } -} - // read the file at the given path and return a byte buffer // pointing to the contents of the file, the length of which // is stored in rsize @@ -75,7 +54,8 @@ uint8_t* readFile(char* path, size_t* rsize) { rewind(fp); - buf = xmalloc(size); + buf = malloc(size); + GUARD(buf); GUARD(fread(buf, 1, size, fp) == size); @@ -92,6 +72,6 @@ void freeFile(uint8_t* buf, size_t size) { #ifdef POSIX SGUARD(munmap(buf, size)); #else - xfree(buf); + free(buf); #endif } diff --git a/src/util.h b/src/util.h index 01c2700..a0465b9 100644 --- a/src/util.h +++ b/src/util.h @@ -12,4 +12,9 @@ #define WINDOWS #endif +void die(const char* s); +void diePerror(const char* s); +uint8_t* readFile(char* path, size_t* size); +void freeFile(uint8_t* buf, size_t size); + #endif