From a1a934397d13acd9027ce7198b2b8c71c6c8b59d Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Mon, 2 Jan 2023 21:08:53 -0800 Subject: [PATCH 1/6] Fix branch decoding. --- esp32/xtensa-assembler.fs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/esp32/xtensa-assembler.fs b/esp32/xtensa-assembler.fs index 5b1eca0..fcf79aa 100644 --- a/esp32/xtensa-assembler.fs +++ b/esp32/xtensa-assembler.fs @@ -28,21 +28,26 @@ vocabulary xtensa xtensa definitions numeric operand im : imm4 im im im im ; : imm8 imm4 imm4 ; -: imm12 imm4 imm4 imm4 ; : imm16 imm8 imm8 ; : sr imm8 ; -( Offsets for J ) +( Offsets for J and branches ) : >ofs ( n -- n ) chere - 4 - ; -: ofs. ( n -- ) 18 sextend address @ + 4 + . ; -' >ofs ' ofs. operand ofs -: offset 18 for aft ofs then next ; +: ofs8. ( n -- ) 8 sextend address @ + 4 + . ; +: ofs12. ( n -- ) 12 sextend address @ + 4 + . ; +: ofs18. ( n -- ) 18 sextend address @ + 4 + . ; +' >ofs ' ofs8. operand ofs8 +' >ofs ' ofs12. operand ofs12 +' >ofs ' ofs18. operand ofs18 +: offset8 8 for aft ofs8 then next ; +: offset12 12 for aft ofs12 then next ; +: offset18 18 for aft ofs18 then next ; ( Offsets for CALL* ) : >cofs ( n -- n ) chere - 2 rshift 1- ; : cofs. ( n -- ) 18 sextend 1+ 2 lshift address @ 3 invert and + . ; ' >cofs ' cofs. operand cofs -: coffset 18 for aft cofs then next ; +: coffset18 18 for aft cofs then next ; ( Frame size of ENTRY ) : >entry12 ( n -- n ) 3 rshift ; @@ -119,27 +124,27 @@ $4 ALU2 XORB, $8 ALU2 MULL, $a ALU2 MULUH, $b ALU2 MULSH, $c ALU2 QUOU, $d ALU2 QUOS, $e ALU2 REMU, $f ALU2 REMS, -: BRANCH1 imm8 4 bits ssss tttt o l l l OP ; +: BRANCH1 offset8 4 bits ssss tttt o l l l OP ; $0 BRANCH1 BNONE, $1 BRANCH1 BEQ, $2 BRANCH1 BLT, $3 BRANCH1 BLTU, $4 BRANCH1 BALL, $5 BRANCH1 BBC, -imm8 o l l b ssss bbbb o l l l OP BBCI, +offset8 o l l b ssss bbbb o l l l OP BBCI, $8 BRANCH1 BANY, $9 BRANCH1 BNE, $a BRANCH1 BGE, $b BRANCH1 BGEU, $c BRANCH1 BNALL, $d BRANCH1 BBS, -imm8 l l l b ssss bbbb o l l l OP BBSI, +offset8 l l l b ssss bbbb o l l l OP BBSI, -: BRANCH2 imm12 ssss 4 bits o l l o OP ; -: BRANCH2a imm8 rrrr ssss 4 bits o l l o OP ; +: BRANCH2 offset12 ssss 4 bits o l l o OP ; +: BRANCH2a offset8 rrrr ssss 4 bits o l l o OP ; : BRANCH2e entry12 ssss 4 bits o l l o OP ; ( $0 J, ) $1 BRANCH2 BEQZ, $2 BRANCH2a BEQI, $3 BRANCH2e ENTRY, ( $4 J, ) $5 BRANCH2 BNEZ, $6 BRANCH2a BNEI, ( BRANCH2b's ) ( $8 J, ) $9 BRANCH2 BLTZ, $a BRANCH2a BLTI, $b BRANCH2a BLTUI, ( $c J, ) $d BRANCH2 BGEZ, $e BRANCH2a BGEI, $f BRANCH2a BGEUI, -offset o o o l l o OP J, -: BRANCH2b imm8 4 bits ssss o l l l o l l o OP ; +offset18 o o o l l o OP J, +: BRANCH2b offset8 4 bits ssss o l l l o l l o OP ; $0 BRANCH2b BF, $1 BRANCH2b BT, $8 BRANCH2b LOOP, $9 BRANCH2b LOOPNEZ, $a BRANCH2b LOOPGTZ, -: CALLOP coffset 2 bits o l o l OP ; +: CALLOP coffset18 2 bits o l o l OP ; 0 CALLOP CALL0, 1 CALLOP CALL4, 2 CALLOP CALL8, 3 CALLOP CALL12, : CALLXOP o o o o o o o o o o o o ssss l l 2 bits o o o o OP ; From 3d1cf4b93cda8c8e31631ba174722c629da7b5ad Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Tue, 3 Jan 2023 21:01:51 -0800 Subject: [PATCH 2/6] Making [IF] [THEN] work at console, bumping version. --- Makefile | 2 +- common/conditionals.fs | 7 +++++-- common/core.h | 3 +++ web/web.template.js | 3 +++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index c3e0647..70473e4 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -VERSION=7.0.7.4 +VERSION=7.0.7.5 STABLE_VERSION=7.0.6.19 OLD_STABLE_VERSION=7.0.5.4 REVISION=$(shell git rev-parse HEAD | head -c 20) diff --git a/common/conditionals.fs b/common/conditionals.fs index f876235..a123919 100644 --- a/common/conditionals.fs +++ b/common/conditionals.fs @@ -15,9 +15,12 @@ ( Interpret time conditionals ) : DEFINED? ( "name" -- xt|0 ) - bl parse find state @ if aliteral then ; immediate + begin bl parse dup 0= while 2drop refill 0= throw repeat + find state @ if aliteral then ; immediate defer [SKIP] -: [THEN] ; : [ELSE] [SKIP] ; : [IF] 0= if [SKIP] then ; +: [THEN] ; immediate +: [ELSE] [SKIP] ; immediate +: [IF] 0= if [SKIP] then ; immediate : [SKIP]' 0 begin postpone defined? dup if dup ['] [IF] = if swap 1+ swap then dup ['] [ELSE] = if swap dup 0 <= if 2drop exit then swap then diff --git a/common/core.h b/common/core.h index d49ac96..6b3a24e 100644 --- a/common/core.h +++ b/common/core.h @@ -109,6 +109,9 @@ static cell_t same(const char *a, const char *b, cell_t len) { } static cell_t find(const char *name, cell_t len) { + if (len == 0) { + return 0; + } for (cell_t ***voc = g_sys->context; *voc; ++voc) { cell_t xt = (cell_t) **voc; while (xt) { diff --git a/web/web.template.js b/web/web.template.js index 99859d1..ed8fe49 100644 --- a/web/web.template.js +++ b/web/web.template.js @@ -117,6 +117,9 @@ function BUILTIN_CODE(i) { } function Find(name) { + if (name.length === 0) { + return 0; + } name = name.toUpperCase(); var raw = unescape(encodeURIComponent(name)); for (var voc = i32[g_sys_context>>2]; i32[voc>>2]; voc += 4) { From b155e49a6327a2dfc282343e540d38366d7e6996 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Wed, 4 Jan 2023 21:17:49 -0800 Subject: [PATCH 3/6] Intercept division by zero with a fault handler. --- common/interp.h | 1 + 1 file changed, 1 insertion(+) diff --git a/common/interp.h b/common/interp.h index c12851d..f9364e1 100644 --- a/common/interp.h +++ b/common/interp.h @@ -57,6 +57,7 @@ static cell_t *forth_run(cell_t *init_rp) { sigaction(SIGSEGV, &sa, 0); sigaction(SIGBUS, &sa, 0); sigaction(SIGINT, &sa, 0); + sigaction(SIGFPE, &sa, 0); #endif return 0; } From 600e82d67e30c8bb2f67e35f14147849ea7d976c Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Wed, 4 Jan 2023 21:45:26 -0800 Subject: [PATCH 4/6] Add fault handling for ESP32, ESP32-S2, and ESP32-S3. Also bump the version. --- Makefile | 2 +- common/interp.h | 41 +++++++++++++++++++++++++++++++++++------ esp32/builtins.h | 2 +- esp32/options.h | 5 +++++ esp32/sim_main.cpp | 3 +++ 5 files changed, 45 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 70473e4..621e794 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -VERSION=7.0.7.5 +VERSION=7.0.7.6 STABLE_VERSION=7.0.6.19 OLD_STABLE_VERSION=7.0.5.4 REVISION=$(shell git rev-parse HEAD | head -c 20) diff --git a/common/interp.h b/common/interp.h index f9364e1..3eb9fd4 100644 --- a/common/interp.h +++ b/common/interp.h @@ -12,19 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifdef HAS_SIGNALS -#include -#include +#if defined(HAS_SIGNALS) || defined(ENABLE_ESP32_FORTH_FAULT_HANDLING) +# include +#endif +#if defined(HAS_SIGNALS) +# include #endif #define JMPW goto **(void **) w #define NEXT w = *ip++; JMPW #define ADDROF(x) (&& OP_ ## x) -#ifdef HAS_SIGNALS +#if defined(HAS_SIGNALS) || defined(ENABLE_ESP32_FORTH_FAULT_HANDLING) static __thread jmp_buf g_forth_fault; static __thread int g_forth_signal; +#endif +#if defined(HAS_SIGNALS) static void forth_signal_handler(int sig) { g_forth_signal = sig; sigset_t ss; @@ -34,6 +38,17 @@ static void forth_signal_handler(int sig) { } #endif +#if defined(ENABLE_ESP32_FORTH_FAULT_HANDLING) +# include "soc/soc.h" +# include +static __thread uint32_t g_forth_setlevel; +static void IRAM_ATTR forth_exception_handler(XtExcFrame *frame) { + g_forth_signal = frame->exccause; + XTOS_RESTORE_INTLEVEL(g_forth_setlevel); + longjmp(g_forth_fault, 1); +} +#endif + static cell_t *forth_run(cell_t *init_rp) { static const BUILTIN_WORD builtins[] = { #define Z(flags, name, op, code) \ @@ -50,7 +65,7 @@ static cell_t *forth_run(cell_t *init_rp) { if (!init_rp) { g_sys->DOCREATE_OP = ADDROF(DOCREATE); g_sys->builtins = builtins; -#ifdef HAS_SIGNALS +#if defined(HAS_SIGNALS) struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = forth_signal_handler; @@ -58,13 +73,27 @@ static cell_t *forth_run(cell_t *init_rp) { sigaction(SIGBUS, &sa, 0); sigaction(SIGINT, &sa, 0); sigaction(SIGFPE, &sa, 0); +#endif +#if defined(ENABLE_ESP32_FORTH_FAULT_HANDLING) + xt_set_exception_handler(EXCCAUSE_LOAD_STORE_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_PRIVILEGED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_UNALIGNED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_DIVIDE_BY_ZERO, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_INSTR_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_ILLEGAL, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_LOAD_PROHIBITED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_STORE_PROHIBITED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_INSTR_PROHIBITED, forth_exception_handler); + uint32_t default_setlevel = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); + XTOS_RESTORE_INTLEVEL(default_setlevel); + g_forth_setlevel = default_setlevel; #endif return 0; } register cell_t *ip, *rp, *sp, tos, w; register float *fp, ft; rp = init_rp; UNPARK; -#ifdef HAS_SIGNALS +#if defined(HAS_SIGNALS) || defined(ENABLE_ESP32_FORTH_FAULT_HANDLING) if (setjmp(g_forth_fault)) { rp = *g_sys->throw_handler; *g_sys->throw_handler = (cell_t *) *rp--; diff --git a/esp32/builtins.h b/esp32/builtins.h index dbb6910..c7b3fe0 100644 --- a/esp32/builtins.h +++ b/esp32/builtins.h @@ -86,7 +86,7 @@ static cell_t ResizeFile(cell_t fd, cell_t size); #define REQUIRED_SYSTEM_SUPPORT \ X("MS-TICKS", MS_TICKS, PUSH millis()) \ XV(internals, "RAW-YIELD", RAW_YIELD, yield()) \ - XV(internals, "RAW-TERMINATE", RAW_TERMINATE, exit(n0)) + XV(internals, "RAW-TERMINATE", RAW_TERMINATE, ESP.restart()) #define REQUIRED_SERIAL_SUPPORT \ XV(serial, "Serial.begin", SERIAL_BEGIN, Serial.begin(tos); DROP) \ diff --git a/esp32/options.h b/esp32/options.h index ef76adc..4816751 100644 --- a/esp32/options.h +++ b/esp32/options.h @@ -52,6 +52,11 @@ # define ENABLE_RMT_SUPPORT #endif +// ESP32-C3 doesn't support fault handling yet. +#if !defined(CONFIG_IDF_TARGET_ESP32C3) +# define ENABLE_ESP32_FORTH_FAULT_HANDLING +#endif + // Uncomment this #define for OLED Support. // You will need to install these libraries from the Library Manager: // Adafruit SSD1306 diff --git a/esp32/sim_main.cpp b/esp32/sim_main.cpp index f495572..c979c0e 100644 --- a/esp32/sim_main.cpp +++ b/esp32/sim_main.cpp @@ -39,6 +39,9 @@ PLATFORM_SIMULATED_OPCODE_LIST #define heap_caps_get_largest_free_block(x) SIM_HEAP_SIZE #define heap_caps_get_free_size(x) SIM_HEAP_SIZE +// Fault handling can't work in the simulator for now. +#undef ENABLE_ESP32_FORTH_FAULT_HANDLING + #include "common/bits.h" #include "common/core.h" #include "common/interp.h" From 106fdcb63d199b7c63e7dd91d11be5c93a2095b0 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sat, 14 Jan 2023 15:05:44 -0800 Subject: [PATCH 5/6] Shunt out more things for camera + bluetooth when not there. --- esp32/bindings.fs | 12 +++++++++--- esp32/bterm.fs | 4 ++++ esp32/camera.fs | 3 +++ esp32/options.h | 3 ++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/esp32/bindings.fs b/esp32/bindings.fs index 2a209a2..980fc12 100644 --- a/esp32/bindings.fs +++ b/esp32/bindings.fs @@ -143,9 +143,15 @@ vocabulary rtos rtos definitions transfer rtos-builtins forth definitions -vocabulary bluetooth bluetooth definitions -transfer bluetooth-builtins -forth definitions +DEFINED? SerialBT.new [IF] + vocabulary bluetooth bluetooth definitions + transfer bluetooth-builtins + forth definitions +[ELSE] + internals definitions + transfer bluetooth-builtins + forth definitions +[THEN] vocabulary oled oled definitions transfer oled-builtins diff --git a/esp32/bterm.fs b/esp32/bterm.fs index befd9c1..5982813 100644 --- a/esp32/bterm.fs +++ b/esp32/bterm.fs @@ -12,6 +12,8 @@ \ See the License for the specific language governing permissions and \ limitations under the License. +DEFINED? bluetooth [IF] + ( Lazy loaded Bluetooth Serial Terminal ) : bterm r| vocabulary bterm bterm definitions @@ -34,3 +36,5 @@ name 0 bt SerialBT.begin drop only forth definitions bterm 500 ms bt-on | evaluate ; + +[THEN] diff --git a/esp32/camera.fs b/esp32/camera.fs index 3c0aef8..fbf73e3 100644 --- a/esp32/camera.fs +++ b/esp32/camera.fs @@ -14,6 +14,7 @@ internals definitions transfer camera-builtins +DEFINED? esp_camera_init [IF] forth definitions ( Lazy loaded camera handling for ESP32-CAM ) @@ -124,4 +125,6 @@ forth definitions camera | evaluate ; +[ELSE] +forth definitions [THEN] diff --git a/esp32/options.h b/esp32/options.h index 4816751..ad445d9 100644 --- a/esp32/options.h +++ b/esp32/options.h @@ -68,9 +68,10 @@ // camera support and BluetoothSerial. // ESP32-CAM always have PSRAM, but so do WROVER boards, // so this isn't an ideal indicator. +// Also limiting to ESP32 classic only, as these can't be ESP32-CAM. // Some boards (e.g. ESP32-S2-WROVER) don't seem to have // built the serial library, so check if its enabled as well. -#if defined(BOARD_HAS_PSRAM) || defined(SIM_PRINT_ONLY) +#if (defined(CONFIG_IDF_TARGET_ESP32) && defined(BOARD_HAS_PSRAM)) || defined(SIM_PRINT_ONLY) # define ENABLE_CAMERA_SUPPORT # if (defined(CONFIG_BT_ENABLED) && \ defined(CONFIG_BLUEDROID_ENABLED)) || \ From 3ba96a284f2e2fd0608e9a7708967b82711c1696 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sat, 14 Jan 2023 15:06:25 -0800 Subject: [PATCH 6/6] Bump version. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 621e794..a97d38c 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -VERSION=7.0.7.6 +VERSION=7.0.7.7 STABLE_VERSION=7.0.6.19 OLD_STABLE_VERSION=7.0.5.4 REVISION=$(shell git rev-parse HEAD | head -c 20)