From 8bed92bef959ebe5d112b888e76a20bfdbeb3d6f Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sun, 4 Sep 2022 16:58:11 -0700 Subject: [PATCH] Start of adding code word support. --- Makefile | 2 +- common/calls.h | 3 ++ common/code.fs | 58 +++++++++++++++++++++++++++++++++ common/forth_namespace_tests.fs | 5 +++ esp32/builtins.h | 1 + esp32/print-builtins.cpp | 1 + esp32/sim_main.cpp | 2 ++ examples/code_x64.fs | 12 +++++++ posix/posix.fs | 1 + 9 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 common/code.fs create mode 100644 examples/code_x64.fs diff --git a/Makefile b/Makefile index 1a0d46f..d2354f3 100644 --- a/Makefile +++ b/Makefile @@ -211,7 +211,7 @@ COMMON_PHASE1e = common/comments.fs \ common/floats.fs \ common/structures.fs -COMMON_PHASE2 = common/utils.fs common/locals.fs +COMMON_PHASE2 = common/utils.fs common/code.fs common/locals.fs COMMON_FILETOOLS = common/tasks.fs common/streams.fs \ common/filetools.fs common/including.fs \ diff --git a/common/calls.h b/common/calls.h index a917f6e..fa4fe83 100644 --- a/common/calls.h +++ b/common/calls.h @@ -25,6 +25,9 @@ typedef cell_t (CALLTYPE *call_t)(); #define ct0 ((call_t) n0) #define CALLING_OPCODE_LIST \ + YV(internals, CALLCODE, float *t_fp = fp; DUP; \ + sp = (cell_t *) (*(call_t*) (w + sizeof(cell_t)))(sp, &t_fp); \ + fp = t_fp; DROP) \ YV(internals, CALL0, n0 = ct0()) \ YV(internals, CALL1, n0 = ct0(n1); --sp) \ YV(internals, CALL2, n0 = ct0(n2, n1); sp -= 2) \ diff --git a/common/code.fs b/common/code.fs new file mode 100644 index 0000000..f26cc00 --- /dev/null +++ b/common/code.fs @@ -0,0 +1,58 @@ +\ Copyright 2022 Bradley D. Nelson +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +( Lazy loaded code words ) +: asm r| + +also forth definitions +vocabulary asm asm definitions +also internals + +variable code-start +variable code-at + +DEFINED? posix [IF] +also posix +: reserve ( n -- ) + 0 swap PROT_READ PROT_WRITE PROT_EXEC or or MAP_ANONYMOUS -1 0 mmap code-start ! ; +previous +4096 reserve +[THEN] + +DEFINED? esp [IF] +also esp +: reserve ( n -- ) MALLOC_CAP_EXEC heap_caps_malloc code-start ! ; +previous +1024 reserve +[THEN] + +code-start code-at ! + +: chere ( -- a ) code-at @ ; +: callot ( n -- ) code-at +! ; +: code, ( n -- ) chere ! cell callot ; +: code1, ( n -- ) chere c! 1 callot ; +: code2, ( n -- ) chere w! 2 callot ; +: code4, ( n -- ) chere l! 4 callot ; +: end-code previous ; + +also forth definitions + +: code ( "name" ) create ['] callcode @ latestxt ! + code-at @ latestxt cell+ ! also asm ; + +previous previous previous +asm + +| evaluate ; diff --git a/common/forth_namespace_tests.fs b/common/forth_namespace_tests.fs index b9aa6fb..6214a93 100644 --- a/common/forth_namespace_tests.fs +++ b/common/forth_namespace_tests.fs @@ -479,8 +479,13 @@ e: check-filetools check-tasks ;e +e: check-asm + out: asm +;e + e: check-phase2 check-locals + check-asm check-utils ;e diff --git a/esp32/builtins.h b/esp32/builtins.h index 89eaa5b..363d496 100644 --- a/esp32/builtins.h +++ b/esp32/builtins.h @@ -56,6 +56,7 @@ static cell_t ResizeFile(cell_t fd, cell_t size); OPTIONAL_RMT_SUPPORT \ OPTIONAL_OLED_SUPPORT \ OPTIONAL_SPI_FLASH_SUPPORT \ + CALLING_OPCODE_LIST \ FLOATING_POINT_LIST #define REQUIRED_MEMORY_SUPPORT \ diff --git a/esp32/print-builtins.cpp b/esp32/print-builtins.cpp index eba45ff..d98e75c 100644 --- a/esp32/print-builtins.cpp +++ b/esp32/print-builtins.cpp @@ -19,6 +19,7 @@ #define SIM_PRINT_ONLY #define ENABLE_OLED_SUPPORT #include "esp32/options.h" +#define CALLING_OPCODE_LIST #define FLOATING_POINT_LIST #define USER_WORDS #include "builtins.h" diff --git a/esp32/sim_main.cpp b/esp32/sim_main.cpp index 2198ac0..ffd45d9 100644 --- a/esp32/sim_main.cpp +++ b/esp32/sim_main.cpp @@ -17,6 +17,7 @@ #include "common/tier1_opcodes.h" #include "common/tier2_opcodes.h" #include "common/floats.h" +#include "common/calls.h" #include "common/calling.h" #define SIM_HEAP_SIZE (100 * 1024 + 1024 * 1024) @@ -25,6 +26,7 @@ static cell_t *simulated(cell_t *sp, const char *op); #define PLATFORM_OPCODE_LIST \ PLATFORM_SIMULATED_OPCODE_LIST \ + CALLING_OPCODE_LIST \ FLOATING_POINT_LIST #include "gen/esp32_sim_opcodes.h" diff --git a/examples/code_x64.fs b/examples/code_x64.fs new file mode 100644 index 0000000..4468dfc --- /dev/null +++ b/examples/code_x64.fs @@ -0,0 +1,12 @@ +#! /usr/bin/env ueforth + +asm forth + +code my2* + $48 code1, $89 code1, $f8 code1, ( mov %rdi, %rax ) + $48 code1, $d1 code1, $27 code1, ( shlq [%rdi] ) + $c3 code1, ( ret ) +end-code + +123 my2* . cr +bye diff --git a/posix/posix.fs b/posix/posix.fs index 8eb1137..10fe30e 100644 --- a/posix/posix.fs +++ b/posix/posix.fs @@ -48,6 +48,7 @@ z" wait" 1 sysfunc wait z" waitpid" 3 sysfunc waitpid z" mmap" 6 sysfunc mmap z" munmap" 2 sysfunc munmap +z" mprotect" 3 sysfunc mprotect z" unlink" 1 sysfunc unlink z" rename" 2 sysfunc rename z" malloc" 1 sysfunc malloc