diff --git a/ueforth/Makefile b/ueforth/Makefile index da789f5..d13a44e 100644 --- a/ueforth/Makefile +++ b/ueforth/Makefile @@ -3,17 +3,44 @@ GEN = $(OUT)/gen RES = $(OUT)/resources WEB = $(OUT)/web POSIX = $(OUT)/posix +WINDOWS = $(OUT)/windows ARDUINO = $(OUT)/arduino -CFLAGS=-O2 -Wall -Werror -I ./ -I $(OUT) +CFLAGS = -Wall -Werror \ + -O2 \ + -fno-exceptions \ + -s \ + -fomit-frame-pointer \ + -fno-stack-protector \ + -fno-ident -Wl,--build-id=none \ + -ffunction-sections -fdata-sections \ + -Wl,--gc-sections \ + -fmerge-all-constants \ + -I ./ -I $(OUT) +STRIP_ARGS = -S \ + --strip-unneeded \ + --remove-section=.note.gnu.gold-version \ + --remove-section=.comment \ + --remove-section=.note \ + --remove-section=.note.gnu.build-id \ + --remove-section=.note.ABI-tag LIBS=-ldl +WIN_ARCH=i686 +WINFLAGS = -mwindows -luser32 + TARGETS = $(WEB)/terminal.html \ $(WEB)/ueforth.js \ $(POSIX)/ueforth \ + $(WINDOWS)/uEforth.exe \ $(ARDUINO)/ueforth.ino -all: $(TARGETS) $(RES)/eforth.ico tests +all: $(TARGETS) tests + +clean: + rm -rf $(OUT) + +# ---- TESTS ---- tests: core_test @@ -22,6 +49,8 @@ core_test: $(POSIX)/ueforth common/core_test.fs \ echo "include common/core_test.fs" | $< | \ diff - common/core_test.fs.golden +# ---- GENERATED ---- + $(GEN): mkdir -p $@ @@ -29,6 +58,10 @@ POSIX_BOOT = common/boot.fs posix/posix.fs $(GEN)/posix_boot.h: common/source_to_string.js $(POSIX_BOOT) | $(GEN) echo "ok" | cat $(POSIX_BOOT) - | $< boot >$@ +WINDOWS_BOOT = common/boot.fs +$(GEN)/windows_boot.h: common/source_to_string.js $(WINDOWS_BOOT) | $(GEN) + echo "ok" | cat $(WINDOWS_BOOT) - | $< boot >$@ + ARDUINO_BOOT = common/boot.fs $(GEN)/arduino_boot.h: common/source_to_string.js $(ARDUINO_BOOT) | $(GEN) echo "ok" | cat $(ARDUINO_BOOT) - | $< boot >$@ @@ -42,6 +75,8 @@ $(GEN)/web_cases.js: $(GEN)/dump_web_opcodes | $(GEN) $(GEN)/web_dict.js: $(GEN)/dump_web_opcodes | $(GEN) $< dict >$@ +# ---- RESOURCES ---- + $(RES): mkdir -p $@ @@ -65,6 +100,13 @@ ICON_SIZES = $(RES)/eforth256x256.png \ $(RES)/eforth.ico: $(ICON_SIZES) convert $^ $< $@ +WINDOWS_RESOURCES = $(RES)/ueforth_res.o + +$(WINDOWS_RESOURCES): windows/ueforth.rc $(RES)/eforth.ico + $(WIN_ARCH)-w64-mingw32-windres $< $@ + +# ---- WEB ---- + $(WEB): mkdir -p $(WEB) @@ -79,6 +121,8 @@ $(WEB)/ueforth.js: \ $(GEN)/web_cases.js | $(WEB) $^ >$@ +# ---- POSIX ---- + $(POSIX): mkdir -p $@ @@ -89,6 +133,22 @@ $(POSIX)/ueforth: \ $(GEN)/posix_boot.h | $(POSIX) $(CC) $(CFLAGS) $< -o $@ $(LIBS) +# ---- WINDOWS ---- + +$(WINDOWS): + mkdir -p $@ + +$(WINDOWS)/uEforth.exe: \ + windows/windows_main.c \ + common/opcodes.h \ + common/core.h \ + $(GEN)/windows_boot.h \ + $(WINDOWS_RESOURCES) | $(WINDOWS) + $(WIN_ARCH)-w64-mingw32-gcc \ + $(CFLAGS) $(WINFLAGS) $< $(WINDOWS_RESOURCES) -o $@ + +# ---- ARDUINO ---- + $(ARDUINO): mkdir -p $@ @@ -100,5 +160,3 @@ $(ARDUINO)/ueforth.ino: \ $(GEN)/arduino_boot.h | $(ARDUINO) $^ >$@ -clean: - rm -rf $(OUT) diff --git a/ueforth/common/calling.h b/ueforth/common/calling.h new file mode 100644 index 0000000..1a6b21b --- /dev/null +++ b/ueforth/common/calling.h @@ -0,0 +1,20 @@ +#define CALLING_OPCODE_LIST \ + X("CALL0", OP_CALL0, tos = ((cell_t (*)()) tos)()) \ + X("CALL1", OP_CALL1, tos = ((cell_t (*)()) tos)(*sp); --sp) \ + X("CALL2", OP_CALL2, tos = ((cell_t (*)()) tos)(sp[-1], *sp); sp -= 2) \ + X("CALL3", OP_CALL3, tos = ((cell_t (*)()) tos)(sp[-2], sp[-1], *sp); sp -= 3) \ + X("CALL4", OP_CALL4, tos = ((cell_t (*)()) tos)(sp[-3], sp[-2], sp[-1], \ + *sp); sp -= 4) \ + X("CALL5", OP_CALL5, tos = ((cell_t (*)()) tos)(sp[-4], sp[-3], sp[-2], \ + sp[-1], *sp); sp -= 5) \ + X("CALL6", OP_CALL6, tos = ((cell_t (*)()) tos)(sp[-5], sp[-4], sp[-3], \ + sp[-2], sp[-1], *sp); sp -= 6) \ + X("CALL7", OP_CALL7, tos = ((cell_t (*)()) tos)(sp[-6], sp[-5], sp[-4], \ + sp[-3], sp[-2], sp[-1], *sp); sp -= 7) \ + X("CALL8", OP_CALL8, tos = ((cell_t (*)()) tos)(sp[-7], sp[-6], sp[-5], \ + sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 8) \ + X("CALL9", OP_CALL9, tos = ((cell_t (*)()) tos)(sp[-8], sp[-7], sp[-6], \ + sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 9) \ + X("CALL10", OP_CALL10, tos = ((cell_t (*)()) tos)(sp[-9], sp[-8], sp[-7], \ + sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 10) \ + diff --git a/ueforth/posix/posix_main.c b/ueforth/posix/posix_main.c index 59c32b9..c4bb3d2 100644 --- a/ueforth/posix/posix_main.c +++ b/ueforth/posix/posix_main.c @@ -2,37 +2,21 @@ #include #include "common/opcodes.h" +#include "common/calling.h" +#define HEAP_SIZE (10 * 1024 * 1024) #define STACK_SIZE (16 * 1024) #define PLATFORM_OPCODE_LIST \ X("DLSYM", OP_DLSYM, tos = (cell_t) dlsym((void *) *sp, (void *) tos); --sp) \ - X("CALL0", OP_CALL0, tos = ((cell_t (*)()) tos)()) \ - X("CALL1", OP_CALL1, tos = ((cell_t (*)()) tos)(*sp); --sp) \ - X("CALL2", OP_CALL2, tos = ((cell_t (*)()) tos)(sp[-1], *sp); sp -= 2) \ - X("CALL3", OP_CALL3, tos = ((cell_t (*)()) tos)(sp[-2], sp[-1], *sp); sp -= 3) \ - X("CALL4", OP_CALL4, tos = ((cell_t (*)()) tos)(sp[-3], sp[-2], sp[-1], \ - *sp); sp -= 4) \ - X("CALL5", OP_CALL5, tos = ((cell_t (*)()) tos)(sp[-4], sp[-3], sp[-2], \ - sp[-1], *sp); sp -= 5) \ - X("CALL6", OP_CALL6, tos = ((cell_t (*)()) tos)(sp[-5], sp[-4], sp[-3], \ - sp[-2], sp[-1], *sp); sp -= 6) \ - X("CALL7", OP_CALL7, tos = ((cell_t (*)()) tos)(sp[-6], sp[-5], sp[-4], \ - sp[-3], sp[-2], sp[-1], *sp); sp -= 7) \ - X("CALL8", OP_CALL8, tos = ((cell_t (*)()) tos)(sp[-7], sp[-6], sp[-5], \ - sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 8) \ - X("CALL9", OP_CALL9, tos = ((cell_t (*)()) tos)(sp[-8], sp[-7], sp[-6], \ - sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 9) \ - X("CALL10", OP_CALL10, tos = ((cell_t (*)()) tos)(sp[-9], sp[-8], sp[-7], \ - sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 10) \ + CALLING_OPCODE_LIST \ #include "common/core.h" #include "gen/posix_boot.h" -#define HEAP_SIZE (10 * 1024 * 1024) - int main(int argc, char *argv[]) { void *heap = mmap(0, HEAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); ueforth(argc, argv, heap, boot, sizeof(boot)); + return 1; } diff --git a/ueforth/windows/ueforth.rc b/ueforth/windows/ueforth.rc new file mode 100644 index 0000000..4039ca6 --- /dev/null +++ b/ueforth/windows/ueforth.rc @@ -0,0 +1 @@ +IDI_MAIN_ICON ICON "out/resources/eforth.ico" diff --git a/ueforth/windows/windows_main.c b/ueforth/windows/windows_main.c new file mode 100644 index 0000000..9ea758f --- /dev/null +++ b/ueforth/windows/windows_main.c @@ -0,0 +1,25 @@ +#include "windows.h" + +#include "common/opcodes.h" +#include "common/calling.h" + +#define HEAP_SIZE (10 * 1024 * 1024) +#define STACK_SIZE (16 * 1024) + +#define PLATFORM_OPCODE_LIST \ + X("GETPROCADDRES", OP_GETPROCADDRESS, \ + tos = (cell_t) GetProcAddress((HMODULE) *sp, (LPCSTR) tos); --sp) \ + CALLING_OPCODE_LIST \ + +#include "common/core.h" + +#include "gen/windows_boot.h" + +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, + PSTR pCmdLine, int nCmdShow) { + void *heap = VirtualAlloc( + NULL, HEAP_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); + ueforth(0, 0, heap, boot, sizeof(boot)); + return 1; +} +