LambLisp 01 Red Fox Alpha
From AI to Actuator
Loading...
Searching...
No Matches
ll_platform_generic.h
1// Copyright 2026 by Frobenius Norm LLC 2026-05-16
2// Free for non-commercial use. Commercial use requires a license.
3#ifndef LL_PLATFORM_GENERIC_H
4#define LL_PLATFORM_GENERIC_H
5
6#include <stdio.h>
7#include <stdint.h>
8#include <stdarg.h>
9#include <setjmp.h>
10#include <type_traits>
11#include <new>
12#include "assert.h"
13#include "unistd.h"
14
15// Default all LL_ feature flags to 0 so they can be used as C++ values, not just #if guards.
17#ifndef LL_ESP_ARDUINO
18#define LL_ESP_ARDUINO 0
19#endif
20
21#ifndef LL_ARDUINO
22#define LL_ARDUINO 0
23#endif
24
25#ifndef LL_POSIX
26#define LL_POSIX 0
27#endif
28
32#ifndef LL_WASM
33#define LL_WASM 0
34#endif
35#ifndef LL_WASI
36#define LL_WASI 0
37#endif
38#ifndef LL_EMSCRIPTEN
39#define LL_EMSCRIPTEN 0
40#endif
41
44#ifndef LL_WINDOWS
45#define LL_WINDOWS 0
46#endif
47#ifndef LL_WIFI
48#define LL_WIFI 0
49#endif
50#ifndef LL_LITTLEFS
51#define LL_LITTLEFS 0
52#endif
53
54#ifndef LL_GCCHK_VISITED_GROWBY
55 #if LL_ESP32
56 #define LL_GCCHK_VISITED_GROWBY 4096
57 #define LL_GCCHK_STACK_INIT 256
58 #define LL_GCCHK_STACK_GROWBY 8
59 #define LL_GCCHK_STACK_CAP 8192
60 #define LL_GCCHK_VECSTACK_INIT 64
61 #define LL_GCCHK_VECSTACK_GROWBY 8
62 #define LL_GCCHK_VECSTACK_CAP 1024
63 #else
64 #define LL_GCCHK_VISITED_GROWBY (-1)
65 #define LL_GCCHK_STACK_INIT 256
66 #define LL_GCCHK_STACK_GROWBY 256
67 #define LL_GCCHK_STACK_CAP (1<<20)
68 #define LL_GCCHK_VECSTACK_INIT 64
69 #define LL_GCCHK_VECSTACK_GROWBY 64
70 #define LL_GCCHK_VECSTACK_CAP (1<<16)
71 #endif
72#endif
73
74#ifndef LL_EVAL_STACK_BUDGET
75 #if LL_WASM
81 #define LL_EVAL_STACK_BUDGET (3UL * 1024 * 1024)
82 #elif LL_POSIX
83 #define LL_EVAL_STACK_BUDGET (6UL * 1024 * 1024)
84 #else
85 #define LL_EVAL_STACK_BUDGET (40UL * 1024)
86 #endif
87#endif
88
97#ifndef LL_EVAL_STACK_BUDGET_MIN
98#define LL_EVAL_STACK_BUDGET_MIN (16UL * 1024)
99#endif
100
104#ifndef LL_XTENSA
105#define LL_XTENSA 0
106#endif
107#ifndef LL_RISCV32
108#define LL_RISCV32 0
109#endif
110
112#ifndef LL_NCG_DIRAM_POOL
113 #if LL_ESP32 && LL_XTENSA
114 #define LL_NCG_DIRAM_POOL 1
115 #else
116 #define LL_NCG_DIRAM_POOL 0
117 #endif
118#endif
119
120#ifndef LL_NCG_RISCV_POOL
121 #if LL_RISCV32 && ((defined(LL_C5_PMP_RWX) && LL_C5_PMP_RWX) || \
122 (defined(LL_C5_NCG_ARENA) && LL_C5_NCG_ARENA))
123 #define LL_NCG_RISCV_POOL 1
124 #else
125 #define LL_NCG_RISCV_POOL 0
126 #endif
127#endif
128
130#ifndef LL_NCG_EXEC_FLOOR_DEFAULT
131 #define LL_NCG_EXEC_FLOOR_DEFAULT 24576
132#endif
133
134#ifndef LL_NCG_PSRAM_EXEC
135 #if defined(BOARD_HAS_PSRAM) && (LL_ESP32S3 || LL_RISCV32)
136 #define LL_NCG_PSRAM_EXEC 1
137 #else
138 #define LL_NCG_PSRAM_EXEC 0
139 #endif
140#endif
141
143#ifndef LL_EXTRAM_SEPARATE_IBUS
144 #if LL_ESP32 && LL_XTENSA
145 #define LL_EXTRAM_SEPARATE_IBUS 1
146 #else
147 #define LL_EXTRAM_SEPARATE_IBUS 0
148 #endif
149#endif
150
163#ifndef LL_NCG_PSRAM_ALIAS
164 #if LL_NCG_PSRAM_EXEC && LL_ESP32S3
165 #define LL_NCG_PSRAM_ALIAS 1
166 #else
167 #define LL_NCG_PSRAM_ALIAS 0
168 #endif
169#endif
170
173#ifndef LL_WIFI_RESERVE_BYTES
174 #define LL_WIFI_RESERVE_BYTES (48UL * 1024)
175#endif
176
196#ifndef LL_GC_BUDGET_WHEN_MAXED
197#define LL_GC_BUDGET_WHEN_MAXED 1
198#endif
199
200#ifndef LL_NCG_EXEC_POOL_BYTES
201 #if LL_NCG_RISCV_POOL
202 #define LL_NCG_EXEC_POOL_BYTES (32UL * 1024)
203 #elif LL_ESP32S3
204 #define LL_NCG_EXEC_POOL_BYTES (32UL * 1024)
205 #elif LL_ESP32
207 #define LL_NCG_EXEC_POOL_BYTES (32UL * 1024)
208 #else
209 #define LL_NCG_EXEC_POOL_BYTES (32UL * 1024)
210 #endif
211#endif
212
214#ifndef LL_ROOTSTACK_MAX
215 #if LL_WASM
217 #define LL_ROOTSTACK_MAX 131072
218 #elif LL_POSIX
219 #define LL_ROOTSTACK_MAX 262144
220 #else
221 #define LL_ROOTSTACK_MAX 65536
222 #endif
223#endif
224
227#ifndef LL_ROOTSTACK_SLACK
228#define LL_ROOTSTACK_SLACK 256
229
245#ifndef LL_ROOTSTACK_INIT
246 #define LL_ROOTSTACK_INIT 256
247#endif
248#ifndef LL_MARKSTACK_INIT
249 #define LL_MARKSTACK_INIT 256
250#endif
251#endif
252
254#ifndef LL_EVAL_ROOT_DEPTH_MAX
255#define LL_EVAL_ROOT_DEPTH_MAX (LL_ROOTSTACK_MAX - 536)
256#endif
257
269
295#ifndef LL_ROOT_HIGH_WATER
296 #if LL_POSIX
297 #define LL_ROOT_HIGH_WATER 1
298 #else
299 #define LL_ROOT_HIGH_WATER 0
300 #endif
301#endif
302
317#ifndef LL_HAVE_BACKTRACE
318 #if defined(__GLIBC__) && !LL_ESP32
319 #define LL_HAVE_BACKTRACE 1
320 #else
321 #define LL_HAVE_BACKTRACE 0
322 #endif
323#endif
324
325
326#ifndef LL_I2C
327#define LL_I2C 0
328#endif
329#ifndef LL_COMMONIO
330#define LL_COMMONIO 0
331#endif
332#ifndef LL_ESP32
333#define LL_ESP32 0
334#endif
335#ifndef LL_COMPLEX
336#define LL_COMPLEX 0
337#endif
338#ifndef LL_BIGNUM
339#define LL_BIGNUM 0
340#endif
341#ifndef LL_BIGNUM_LIMB_BITS
342#define LL_BIGNUM_LIMB_BITS 32
343#endif
345#ifndef LL_BIGNUM_STRICT_RT
346#define LL_BIGNUM_STRICT_RT 0
347#endif
348
349#ifndef LL_MODBUS
350#define LL_MODBUS 0
351#endif
352#ifndef LL_PROFIBUS
353#define LL_PROFIBUS 0
354#endif
355#ifndef LL_PROFINET
356#define LL_PROFINET 0
357#endif
358#ifndef LL_CUDA
359#define LL_CUDA 0
360#endif
361#ifndef LL_HIP
362#define LL_HIP 0
363#endif
364
365// Board-specific flags (set by platformio.ini build_flags per env)
366#ifndef LL_ESP32_S3_DEVKIT_C
367#define LL_ESP32_S3_DEVKIT_C 0
368#endif
369#ifndef LL_ESP32S3_N8R2
370#define LL_ESP32S3_N8R2 0
371#endif
372#ifndef LL_Freenove_4WD_Car_Kit_ESP32
373#define LL_Freenove_4WD_Car_Kit_ESP32 0
374#endif
375#ifndef LL_Freenove_ESP32_WROVER
376#define LL_Freenove_ESP32_WROVER 0
377#endif
378#ifndef LL_ESP32_N4R4
379#define LL_ESP32_N4R4 0
380#endif
381#ifndef LL_AMD64
382#define LL_AMD64 0
383#endif
384#ifndef LL_X86_64
385#define LL_X86_64 0
386#endif
387#ifndef LL_AARCH64
388#define LL_AARCH64 0
389#endif
390#ifndef LL_ARM64
391#define LL_ARM64 0
392#endif
393#ifndef LL_WASM32
394#define LL_WASM32 0
395#endif
396
412#ifndef LL_NETWORK
413 #if LL_WASM
414 #define LL_NETWORK 0
415 #else
416 #define LL_NETWORK ((LL_WIFI) || (LL_POSIX))
417 #endif
418#endif
421#define LL_POSIX_NET ((LL_POSIX) && (LL_NETWORK))
422
423#ifndef LL_TERMIOS
424 #if LL_WASM
425 #define LL_TERMIOS 0
426 #else
427 #define LL_TERMIOS (LL_POSIX)
428 #endif
429#endif
430
437#ifndef LL_NCG
438 #if LL_WASM
439 #define LL_NCG 0
440 #else
441 #define LL_NCG 1
442 #endif
443#endif
444
468#ifndef LL_AUTOCOMPILE
469 #define LL_AUTOCOMPILE 1
470#endif
471
472
491#ifndef LL_XTENSA
492#define LL_XTENSA 0
493#endif
494#ifndef LL_RISCV32
495#define LL_RISCV32 0
496#endif
497
498
501#if LL_ESP_ARDUINO
502#include "Arduino.h"
503#endif
504
508#if LL_ARDUINO
509#include <string.h>
510
511unsigned long millis();
512unsigned long micros();
525void delay_ms(unsigned long ms);
526
527static void delay(unsigned long ms) { delay_ms(ms); }
528
534#ifndef HIGH
535#define HIGH 1
536#define LOW 0
537#endif
538#ifndef INPUT
539#define INPUT 0x0
540#define OUTPUT 0x1
541#define INPUT_PULLUP 0x2
542#endif
543
544void pinMode(int pin, int mode);
545void digitalWrite(int pin, int val);
546int digitalRead(int pin);
547void analogWrite(int pin, int val);
548int analogRead(int pin);
549void tone(int pin, unsigned int freq);
550void tone(int pin, unsigned int freq, unsigned long dur);
551void noTone(int pin);
552void delayMicroseconds(unsigned long us);
553
556int ll_llarduino_pin_mode(int pin);
557int ll_llarduino_pin_duty(int pin);
558int ll_llarduino_pin_tone(int pin);
559int ll_llarduino_npins();
560const char *ll_llarduino_backend();
561
562typedef uint8_t byte;
563
584class LL_String {
585public:
586
587 LL_String() { _init(); set(""); }
588 LL_String(const char *other) { _init(); set(other); }
589 LL_String(const char *other, int n) { _init(); set(other, n); }
590 LL_String(const LL_String &other) { _init(); set(other.ptr, other.len); }
591
592 ~LL_String() { if (ptr) { delete[] ptr; ptr = 0; } len = cap = 0; }
593
594 void set(const char *s) { set(s, s ? (int) strlen(s) : 0); }
595
596 void set(const char *s, int n) {
597 if (!s || n <= 0) { _ensure(0); ptr[0] = '\0'; len = 0; return; }
598 if (ptr && s >= ptr && s <= ptr + len) {
599 const int off = (int) (s - ptr);
600 memmove(ptr, ptr + off, (size_t) n);
601 }
602 else {
603 _ensure(n);
604 memmove(ptr, s, (size_t) n);
605 }
606 ptr[n] = '\0';
607 len = n;
608 }
609
610 LL_String concat(const char *s1, const char *s2) {
611 LL_String r;
612 const int n1 = s1 ? (int) strlen(s1) : 0;
613 const int n2 = s2 ? (int) strlen(s2) : 0;
614 r._ensure(n1 + n2);
615 if (n1) memmove(r.ptr, s1, (size_t) n1);
616 if (n2) memmove(r.ptr + n1, s2, (size_t) n2);
617 r.ptr[n1 + n2] = '\0';
618 r.len = n1 + n2;
619 return r;
620 }
621
622 LL_String &append(const char *s1) {
623 if (s1 && *s1) {
624 const int n1 = (int) strlen(s1);
625 const bool self = (ptr && s1 >= ptr && s1 <= ptr + len);
626 const int off = self ? (int) (s1 - ptr) : 0;
627 _ensure(len + n1);
628 memmove(ptr + len, self ? ptr + off : s1, (size_t) n1);
629 len += n1;
630 ptr[len] = '\0';
631 }
632 return *this;
633 }
634
637 LL_String &append(char c) {
638 _ensure(len + 1);
639 ptr[len++] = c;
640 ptr[len] = '\0';
641 return *this;
642 }
643
644 LL_String substring(int start, int len_) { return LL_String(&(ptr[start]), len_); }
645
646 LL_String &operator=(const char *other) { set(other); return *this; }
647 LL_String &operator=(const LL_String &other) { if (this != &other) set(other.ptr, other.len); return *this; }
648
649 bool operator==(const char *other) { return strcmp(ptr, other) == 0; }
650 bool operator!=(const char *other) { return strcmp(ptr, other) != 0; }
651 bool operator==(const LL_String &other) { return strcmp(ptr, other.ptr) == 0; }
652 bool operator!=(const LL_String &other) { return strcmp(ptr, other.ptr) != 0; }
653 char &operator[](int ix) { return ptr[ix]; }
654
655 LL_String operator+(const char *other) { return concat(ptr, other); }
656 LL_String operator+(const LL_String &other) { return concat(ptr, other.ptr); }
657 LL_String operator+(char c) { char cstr[2]; cstr[0] = c; cstr[1] = '\0'; return concat(ptr, cstr); }
658
659 LL_String &operator+=(const char *other) { return append(other); }
660 LL_String &operator+=(const LL_String &other) { return append(other.ptr); }
661 LL_String &operator+=(char c) { return append(c); }
662
663 char *c_str() const { return ptr; }
664 int length() const { return len; }
665
666private:
667 void _init() { ptr = 0; len = 0; cap = 0; }
668
671 void _ensure(int need) {
672 if (cap >= need + 1) return;
673 int ncap = cap ? cap : 16;
674 while (ncap < need + 1) ncap += ncap;
676 char *np = new char[ncap];
677 if (ptr) { memmove(np, ptr, (size_t) len + 1); delete[] ptr; }
678 else { np[0] = '\0'; }
679 ptr = np;
680 cap = ncap;
681 }
682
683 char *ptr;
684 int len;
685 int cap;
686};
687
688typedef LL_String String;
689#endif
690
693#define NOTUSED __attribute__((__unused__))
694#define INLINE __attribute__((__inline__))
695#define NOINLINE __attribute__((__noinline__))
696#define CHECKPRINTF __attribute__((format(printf, 1, 2)))
697#define CHECKPRINTF_pos2 __attribute__((format(printf, 2, 3)))
698
703
711#if LL_ARDUINO
725class Print {
726public:
727 virtual ~Print() {}
728 virtual size_t write(uint8_t c) = 0;
729 virtual size_t write(const uint8_t *buf, size_t n);
730 size_t write(const char *s);
731 size_t print(char c);
732 size_t print(const char *s);
733 size_t print(const String &s);
734 size_t print(int n, int base = 10);
735 size_t print(long n, int base = 10);
736 size_t print(unsigned long n, int base = 10);
737 size_t print(double v, int digits = 2);
738 size_t println();
739 size_t println(char c);
740 size_t println(const char *s);
741 size_t println(const String &s);
742 size_t println(int n, int base = 10);
743 size_t println(long n, int base = 10);
744 size_t println(unsigned long n, int base = 10);
745 size_t println(double v, int digits = 2);
746 size_t printf(const char *fmt, ...) CHECKPRINTF_pos2;
747};
748
749class Stream : public Print {
750public:
751 virtual int available() = 0;
752 virtual int read() = 0;
753 virtual int peek() = 0;
754 virtual void flush() {}
755};
756
759class LLArduinoSerial : public Stream {
760 int _peeked;
761public:
762 LLArduinoSerial() : _peeked(-1) {}
763 void begin(unsigned long baud = 115200);
764 void end();
765 int available() override;
766 int availableForWrite();
767 int read() override;
768 int peek() override;
769 void flush() override;
770 size_t write(uint8_t c) override;
771 using Print::write;
772};
773
774extern LLArduinoSerial Serial;
775
776
777#endif // LL_ARDUINO -- Print/Stream/Serial
778
782#if LL_ARDUINO || LL_ESP_ARDUINO
783const char *ll_llarduino_fmt_int(long v, int base);
784const char *ll_llarduino_fmt_real(double v, int digits);
785const char *ll_llarduino_eol();
786#endif
787
788#define CHECKPRINTF_pos3 __attribute__((format(printf, 3, 4)))
789#define CHECKPRINTF_pos4 __attribute__((format(printf, 4, 5)))
790
791// On Arduino/ESP32, IRAM_ATTR and DRAM_ATTR are already defined by Arduino.h (esp_attr.h).
792// On other platforms (Linux, desktop simulation) define them as empty so the same source compiles.
793#if !defined(IRAM_ATTR)
794#define IRAM_ATTR
795#endif
796#if !defined(DRAM_ATTR)
797#define DRAM_ATTR
798#endif
799
838#ifndef LL_STDIO_RX_BUFSIZE
839#define LL_STDIO_RX_BUFSIZE 0
840#endif
841
842const unsigned long toString_MAX_LENGTH = 8192;
845const unsigned long LL_ERRFORM_MAX = 400;
852const unsigned long LL_ENVFRAME_MAX = 32;
869const unsigned long LL_PRINT_LIST_MAX = 4096;
870String toString(const char *fmt, ...) CHECKPRINTF;
872
873
874#define ME(_me_) NOTUSED const char me[] = _me_
875#define ll_isdef(sym) (#sym[0])
876
877void global_printf(const char *fmt, ...);
878
893void global_spill_begin();
894
908void global_term_quiet(bool echo, bool logs);
909
920#if LL_EMSCRIPTEN
921void global_term_push_line(const char *s);
922#endif
923
925
944
945typedef unsigned char Byte_t;
946typedef bool Bool_t;
959typedef char Char_t;
960typedef int32_t ll_codepoint_t;
961
962typedef Char_t const *Charst_t;
963typedef Byte_t const *Bytest_t;
964typedef Char_t *CharVec_t;
965typedef Byte_t *ByteVec_t;
966
985void *ll_payload_alloc_bytes(size_t nbytes);
986void ll_payload_free(void *p);
987
989template <typename T> static inline T *ll_payload_new(size_t n)
990{
991 return (T *) ll_payload_alloc_bytes(n * sizeof(T));
992}
994
1028void *ll_fastram_alloc_bytes(size_t nbytes);
1029void ll_fastram_free(void *p);
1031
1054typedef uintptr_t Word_t;
1055typedef void *Ptr_t;
1056static_assert(sizeof(Word_t) >= sizeof(void *),
1057 "Word_t must hold a pointer; LLP64 (Windows x64) breaks "
1058 "the old `unsigned long` spelling because long is 32 bits there and pointers are 64.");
1059
1060typedef int32_t LL_int32;
1061
1070
1073inline int ll_u8_seqlen(unsigned char b0)
1074{
1075 if (b0 < 0x80) return 1;
1076 if ((b0 & 0xE0) == 0xC0) return 2;
1077 if ((b0 & 0xF0) == 0xE0) return 3;
1078 if ((b0 & 0xF8) == 0xF0) return 4;
1079 return 1;
1080}
1081
1084inline ll_codepoint_t ll_u8_decode(const char *p, int *nb)
1085{
1086 unsigned char b0 = (unsigned char) p[0];
1087 int n = ll_u8_seqlen(b0);
1088 if (nb) *nb = n;
1089 if (n == 1) return (ll_codepoint_t) b0;
1090 ll_codepoint_t cp = b0 & (0xFF >> (n + 1));
1091 for (int i = 1; i < n; i++) {
1092 unsigned char c = (unsigned char) p[i];
1093 if ((c & 0xC0) != 0x80) { if (nb) *nb = 1; return (ll_codepoint_t) b0; }
1094 cp = (cp << 6) | (c & 0x3F);
1095 }
1096 return cp;
1097}
1098
1100inline int ll_u8_encode(ll_codepoint_t cp, char *out)
1101{
1102 if (cp < 0x80) { out[0] = (char) cp; return 1; }
1103 if (cp < 0x800) { out[0] = (char)(0xC0 | (cp >> 6)); out[1] = (char)(0x80 | (cp & 0x3F)); return 2; }
1104 if (cp < 0x10000) { out[0] = (char)(0xE0 | (cp >> 12)); out[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
1105 out[2] = (char)(0x80 | (cp & 0x3F)); return 3; }
1106 out[0] = (char)(0xF0 | (cp >> 18)); out[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
1107 out[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); out[3] = (char)(0x80 | (cp & 0x3F)); return 4;
1108}
1109
1111inline LL_int32 ll_u8_strlen(const char *s)
1112{
1113 LL_int32 n = 0;
1114 for (const char *p = s; *p; n++) p += ll_u8_seqlen((unsigned char) *p);
1115 return n;
1116}
1117
1124inline LL_int32 ll_u8_strlen_n(const char *s, LL_int32 nbytes)
1125{
1126 LL_int32 n = 0;
1127 for (LL_int32 i = 0; i < nbytes; n++) i += ll_u8_seqlen((unsigned char) s[i]);
1128 return n;
1129}
1130
1133inline const char *ll_u8_index_n(const char *s, LL_int32 nbytes, LL_int32 k)
1134{
1135 LL_int32 i = 0;
1136 for (LL_int32 c = 0; c < k; c++) {
1137 if (i >= nbytes) return 0;
1138 i += ll_u8_seqlen((unsigned char) s[i]);
1139 }
1140 return (i <= nbytes) ? (s + i) : 0;
1141}
1142
1145inline const char *ll_u8_index(const char *s, LL_int32 k)
1146{
1147 const char *p = s;
1148 for (LL_int32 i = 0; i < k; i++) {
1149 if (!*p) return 0;
1150 p += ll_u8_seqlen((unsigned char) *p);
1151 }
1152 return p;
1153}
1155
1156typedef int64_t LL_int64;
1157typedef float LL_float32;
1158typedef double LL_float64;
1159
1160static_assert(sizeof(Word_t) == sizeof(Ptr_t), "Word_t size must be == Ptr_t size\n");
1161static_assert(sizeof(Word_t) >= sizeof(LL_int32), "Word_t size must be >= LL_int32 size\n");
1162static_assert(sizeof(LL_float32) == 4, "LL_float32 must be 4 bytes\n");
1163static_assert(sizeof(LL_float64) == 8, "LL_float64 must be 8 bytes\n");
1165
1166//The AsciiConverter class mitigates printf formatting problems caused by different int sizes on different processors, when using printf-style format string (%u, %d %lu %ld etc).
1167class AsciiConverter {
1168public:
1169
1170 char *dec(Word_t n) {
1171 char *ptr = slot_end();
1172
1173 *ptr = '\0';
1174 do {
1175 Word_t d = n % 10;
1176 n /= 10;
1177 char c = d + '0';
1178 *--ptr = c;
1179 } while (n);
1180 return ptr;
1181 }
1182
1183 char *dec(LL_int32 n) {
1184 if (n >= 0) return dec((Word_t) n);
1185 if (n == INT32_MIN) return dec((LL_int64) n);
1186 char *ptr = dec(-n);
1187 *--ptr = '-';
1188 return ptr;
1189 }
1190
1205 template <typename T>
1206 typename std::enable_if<std::is_same<T, int>::value && !std::is_same<LL_int32, int>::value,
1207 char *>::type
1208 dec(T n) { return dec((LL_int32) n); }
1209
1210 char *dec(LL_int64 n) {
1211 // Full 64-bit signed decimal; avoids conflict with dec(Word_t) on 64-bit platforms.
1212 char *ptr = slot_end();
1213 bool neg = (n < 0);
1214 uint64_t uv = neg ? (n == INT64_MIN ? (uint64_t) 9223372036854775808ULL : (uint64_t) -n)
1215 : (uint64_t) n;
1216 do { *--ptr = (char)('0' + (uv % 10)); uv /= 10; } while (uv);
1217 if (neg) *--ptr = '-';
1218 return ptr;
1219 }
1220
1221 char *hex(Word_t n) {
1222 char *ptr = slot_end();
1223 LL_int32 nibs = sizeof(Word_t) * 2;
1224
1225 *ptr = '\0';
1226 while (nibs--) {
1227 Word_t d = n & 0x0f;
1228 n >>= 4;
1229 char c = (d < 10) ? (d + '0') : (d - 10 + 'a');
1230 *--ptr = c;
1231 }
1232 return ptr;
1233
1234 }
1235
1236 char *dec(LL_float32 n) {
1237 char *b = slot_base();
1238 snprintf(b, SLOT, "%f", (LL_float64) n);
1239 return b;
1240 }
1241
1242private:
1245
1278 static const LL_int32 SLOTS = 16;
1279 static const LL_int32 SLOT = 64;
1280 static_assert(SLOT >= 3 + 20, "dec(LL_int64): sign + 20 digits + NUL");
1281 static_assert(SLOT >= 2 + sizeof(Word_t) * 2, "hex(Word_t): nibbles + NUL");
1282 static_assert(SLOT >= 48, "dec(LL_float32): %f of 3.4e38 needs 47 chars");
1283 char buf[SLOTS][SLOT];
1284 LL_int32 next = 0;
1285 char *slot_base() { next = (next + 1) % SLOTS; return buf[next]; }
1286 char *slot_end() { char *b = slot_base(); b[SLOT - 1] = '\0'; return &b[SLOT - 1]; }
1287};
1288
1289extern AsciiConverter ascii;
1290
1291class LambPlatform {
1292public:
1293
1296 LambPlatform() {}
1297 ~LambPlatform() { end(); }
1298
1299 void begin(void);
1300 void loop(void);
1301 void end(void);
1302
1303 void reboot(void);
1304
1305 const char *name();
1306 void identification(void);
1307
1308 LL_int32 free_heap();
1319 LL_int32 cpu_mhz();
1329 LL_int32 free_internal();
1330 LL_int32 free_dma();
1331 LL_int32 largest_free_internal();
1332 LL_int32 largest_free_dma();
1333 LL_int32 cell_pool_free();
1334
1337
1354 void *cell_block_alloc(unsigned long nbytes);
1355 void cell_block_free(void *blk);
1356
1359
1366 const char *mem_placement(void *p);
1367
1370
1380 LL_int64 fs_total();
1381 LL_int64 fs_used();
1382 LL_int32 free_stack();
1383 Bool_t heap_integrity_check(Bool_t complain=false);
1384
1386 LL_float32 rand11() {
1387 const int max_int = (~((unsigned int) 0)) >> 1;
1388 const int min_int = -max_int - 1;
1389 const LL_float32 min = (LL_float32) min_int;
1390
1391 LL_int32 n;
1392 rand((byte *) &n, sizeof(n));
1393 return (n / min);
1394 }
1395
1396 LL_float32 rand01() { return (rand11() + 1.0) / 2.0; }
1397
1398 void rand(byte *buf, LL_int32 len);
1399 void rand11(LL_float32 *buf, LL_int32 n) { while (n--) *buf++ = rand11(); }
1400 void rand01(LL_float32 *buf, LL_int32 n) { while (n--) *buf++ = rand01(); }
1401
1402 LL_int32 loop_elapsed_ms() { return millis() - loop_start_ms; }
1403 LL_int32 loop_elapsed_us() { return micros() - loop_start_us; }
1404
1406
1407private:
1408 LL_int32 loop_start_ms;
1409 LL_int32 loop_start_us;
1410};
1411
1412extern LambPlatform lambPlatform;
1413
1416#if LL_LITTLEFS
1417#include "LittleFS.h"
1418typedef File File_Native;
1419#endif
1420
1421#if LL_POSIX
1422#include <stdio.h>
1423typedef FILE* File_Native;
1424#endif
1426
1437class LL_File {
1438public:
1439
1440 LL_File();
1441 ~LL_File();
1442
1443 bool isOpen() { return _path != ""; }
1444 int read(void);
1445 int write(byte b);
1446 int seek(unsigned long target, int whence=SEEK_SET);
1447 int tell();
1448 int size();
1449 int close();
1450
1451 int read(byte *b, int n) {
1452 LL_int32 nread = 0;
1453 while (n--) {
1454 int ch = read();
1455 if (ch == EOF) return nread;
1456 b[nread++] = ch;
1457 }
1458 return nread;
1459 }
1460
1461 int read(char *s, int n) { return read((byte *) s, n); }
1462 int write(const byte *b, int n) { while (n--) write(*b++); return n; }
1463 int write(const char *s, int n) { while (n--) write((byte) *s++); return n; }
1464 int peek();
1465
1466 File_Native _theFile;
1467 String _path;
1468 String _mode;
1469
1470private:
1471
1472};
1473
1474
1477
1504bool ll_fastram_reserve(const char *tag, size_t bytes);
1505
1510
1527bool ll_fastram_reserve_pinned(const char *tag, size_t bytes);
1528
1530void ll_fastram_release(const char *tag);
1531
1533void ll_fastram_release_unclaimed();
1535
1542
1543// Declared here rather than in a Windows-only header; the implementation file is the only place
1544// Win32 is named.
1547#if LL_WINDOWS
1548
1559size_t ll_win_self_exe_path(char *buf, size_t cap);
1560
1568int ll_win_mkdir(const char *path, int mode);
1569
1583int ll_win_set_system_time(long long utc_seconds);
1584
1585#endif // LL_WINDOWS
1586
1588
1610
1611// Every LambLisp element takes this interface from the generic platform rather than from a
1612// driver-specific header.
1615class Lamb; // the operations below take `Lamb &`; function pointers need only an incomplete type
1616
1624#ifndef LL_PANEL_ST7789_EYE
1625 #define LL_PANEL_ST7789_EYE 0
1626#endif
1627
1633#ifndef LL_PANEL_ST7701_AITRIP
1634 #define LL_PANEL_ST7701_AITRIP 0
1635#endif
1636
1641#if LL_PANEL_ST7789_EYE || LL_PANEL_ST7701_AITRIP
1642 #define LL_PANEL 1
1643#else
1644 #define LL_PANEL 0
1645#endif
1646
1648enum LL_PanelModel {
1649 LL_PANEL_PUSH = 0,
1650 LL_PANEL_SCAN = 1
1651};
1652
1654struct LL_Panel {
1655 const char *name;
1656 int w, h;
1657 LL_PanelModel model;
1658 bool (*init)(Lamb &lamb);
1659 void (*fill)(Lamb &lamb, int x, int y, int w, int h, uint16_t color);
1660 void (*blit)(Lamb &lamb, int x, int y, int w, int h, const uint16_t *src);
1661 uint16_t *(*fb)(void);
1662 void (*set_brightness)(Lamb &lamb, int percent);
1663 void (*on_off)(Lamb &lamb, bool on);
1664};
1665
1667const LL_Panel *ll_panel_active(void);
1668
1670void ll_panel_register(const LL_Panel *p);
1671
1673bool ll_panel_ready(void);
1674
1679void ll_panel_blit_rgb565(Lamb &lamb, int x, int y, int w, int h, const uint16_t *src);
1680
1682
1690public:
1691 LL_File *open(const char *path, const char *mode);
1692 bool exists(const char *path);
1693 int rm(const char *path);
1694 int mv(const char *from, const char *to);
1695 int mkdir(const char *path);
1696
1697private:
1698};
1699
1700extern LL_File_System ll_file_system;
1701
1705#if LL_WIRE
1706#include "Wire.h"
1707extern TwoWire *LL_Wire;
1708#endif
1709
1710#if LL_WIFI
1711#include "WiFi.h"
1712#include "WiFiClientSecure.h"
1713extern WiFiClass *LL_WiFi;
1714#endif
1716
1724public:
1725 int setTxBufferSize(int n);
1726 int setRxBufferSize(int n);
1727
1734 bool set_input_callback(void (*cb)());
1735
1736 void begin(void) { begin(115200); }
1737 void begin(unsigned long baudrate);
1738 void end();
1739 int available(void);
1740 int availableForWrite(void);
1741 int read(void);
1742 int write(uint8_t c);
1743 int write(char c) { return write((uint8_t) c); }
1744
1745 void flush(void);
1746
1747 int read(byte *buf, int max) {
1748 int nread = 0;
1749 while (max--) {
1750 int b = read();
1751 if (b == EOF) break;
1752 *buf++ = b;
1753 }
1754 return nread;
1755 }
1756
1757 int read(char *s, int max) { return read((uint8_t *) s, max); }
1758 int write(const char *s, int n) { return write((uint8_t *) s, n); }
1759 int write(const byte *b, size_t n) { int i=n; while (i--) write(*b++); return n; }
1760 int write(const char *s) { int i=0; while (*s) { write(*s++); i++; } return i; }
1761
1762 operator bool() { return true; }
1763};
1764
1765extern LambStdioClass LambStdio;
1766
1767typedef byte uuid_t[16];
1768
1770#define ll_once(_once_something) do { \
1771 static bool _visited_ = false; \
1772 if (!_visited_) { \
1773 _visited_ = true; \
1774 { _once_something; } \
1775 } \
1776 } while (0) \
1777 //
1778//
1779
1781#define ll_every(_every_so_often_ms, _every_something_to_do) do { \
1782 static unsigned long _every_next = 0; \
1783 unsigned long _every_now = millis(); \
1784 if (_every_now >= _every_next) { \
1785 { _every_something_to_do; } \
1786 _every_next = _every_now + (_every_so_often_ms); \
1787 } \
1788 } while (0) \
1789 //
1790//
1791
1820void embedded_debug_catcher();
1821#define ll_debug_catcher embedded_debug_catcher()
1822//#define ll_debug_catcher ll_term.flush()
1823
1824// ── Settings file paths ──────────────────────────────────────────────────────
1825#if LL_LITTLEFS
1826#define LAMB_SETTINGS_PATH "/Settings-Lamb.scm"
1827#define SETTINGS_PATH "/Settings.scm"
1828#else
1829#define LAMB_SETTINGS_PATH "Settings-Lamb.scm"
1830#define SETTINGS_PATH "Settings.scm"
1831#endif
1832
1833// ── Pre-allocation settings ──────────────────────────────────────────────────
1840static inline LL_int32 ll_round_up_1k(LL_int32 n)
1841{
1842 if (n <= 0) return n;
1843 if (n > (LL_int32) 0x7FFFFC00) return (LL_int32) 0x7FFFFC00;
1844 return (n + 1023) & ~((LL_int32) 1023);
1845}
1846
1847struct LambPreSettings {
1851 LL_int32 cell_block_size = 0;
1852 LL_int32 extension_block_size = 8 * 1024;
1859 LL_int32 gc_total_marked = 0;
1869 LL_int32 gc_pool_target = 0;
1870 LL_int32 ncg_frame_pool_size = 16;
1877 LL_int32 pool_reserve_pct = 25;
1878 LL_int32 pool_reserve_bytes = 0;
1900 LL_int32 gc_persist = 1;
1916 LL_int32 sint_in_iram = 1;
1917 LL_int32 rootstack_in_iram = 1;
1923 LL_int32 rootstack_max = 0;
1924 LL_int32 rootstack_init = 0;
1925 LL_int32 markstack_init = 0;
1931 LL_int32 va_list_max = (LL_int32) toString_MAX_LENGTH;
1940 LL_int32 boot_pending = 0;
1941 void load();
1952 bool apply_key(const char *key, long val);
1953};
1954
1957void ll_pre_settings_write(LL_int32 boot_pending);
1959void ll_settings_boot_ok(void);
1960
1969extern LL_int32 _ll_settings_writes_done;
1970extern LL_int32 _ll_settings_writes_skipped;
1971
1972extern LambPreSettings ll_pre_settings;
1973
1997extern LL_int32 ll_cell_block_size_effective;
1998
2005extern LL_int32 ll_va_list_max;
2006
2010#define ll_try try
2011
2025extern const void *ll_catch_last_err;
2026extern int ll_catch_repeats;
2027#define LL_CATCH_REPEAT_LIMIT 4
2028
2054#define LL_ERR_IS_USER_RAISE(__e__) \
2055 ((__e__)->type() == Cell::T_ERROR && strcmp((__e__)->error_get_chars(), "__raise__") == 0)
2056
2080#define ll_catch(__code_before_rethrow__) \
2081 catch (std::bad_alloc &) { \
2082 Sexpr_t __err__ = NIL->mk_error("%s out of memory (C++ allocation failed)", me); \
2083 ll_catch_last_err = (const void *) __err__; \
2084 ll_catch_repeats = 1; \
2085 global_printf("\r[%d] %s ll_catch(): %s\n", millis(), me, __err__->error_get_chars()); \
2086 ll_debug_catcher; \
2087 __code_before_rethrow__; \
2088 throw __err__; \
2089 } \
2090 catch (Sexpr_t __err__) { \
2091 if (__err__->type() != Cell::T_ERROR) \
2092 throw NIL->mk_error("ll_catch() BUG in %s bad type %s", me, __err__->dump().c_str()); \
2093 \
2094 if (!LL_ERR_IS_USER_RAISE(__err__)) { \
2095 if ((const void *) __err__ != ll_catch_last_err) { \
2096 ll_catch_last_err = (const void *) __err__; \
2097 ll_catch_repeats = 0; \
2098 } \
2099 if (++ll_catch_repeats <= LL_CATCH_REPEAT_LIMIT) \
2100 global_printf("\r[%d] %s ll_catch(): %s\n", millis(), me, __err__->error_get_chars()); \
2101 else if (ll_catch_repeats == LL_CATCH_REPEAT_LIMIT + 1) \
2102 global_printf("\r[%d] %s ll_catch(): ... same error still unwinding; further frames suppressed\n", millis(), me); \
2103 } \
2104 \
2105 ll_debug_catcher; \
2106 __code_before_rethrow__; \
2107 throw __err__; \
2108 } \
2109 //
2110
2111#define ll_catch_terminal \
2112 catch (std::bad_alloc &) { \
2113 global_printf("\r[%d] %s ll_catch_terminal: out of memory (C++ allocation failed)\n", \
2114 millis(), me); \
2115 ll_debug_catcher; \
2116 } \
2117 catch (Sexpr_t __err__) { \
2118 \
2119 \
2122 if (LL_ERR_IS_USER_RAISE(__err__)) \
2123 global_printf("\r[%d] %s ll_catch_terminal: UNCAUGHT raise: %s\n", \
2124 millis(), me, __err__->error_get_irritants()->str().c_str()); \
2125 \
2127 else if (__err__->type() != Cell::T_ERROR) \
2128 global_printf("\r[%d] %s ll_catch_terminal: non-error type %d\n", \
2129 millis(), me, __err__->type()); \
2130 else \
2131 global_printf("\r[%d] %s ll_catch_terminal: %s\n", \
2132 millis(), me, __err__->error_get_chars()); \
2133 ll_debug_catcher; \
2134 } \
2135 //
2137
2149#ifdef __cplusplus
2150const char *const *ll_feature_names(int *count);
2151#endif
2152
2163#ifdef __cplusplus
2164bool ll_term_ansi(void);
2165void ll_term_ansi_set(bool on);
2166bool ll_term_ansi_detect(void);
2167#if LL_WINDOWS
2168int ll_win_enable_vt(void);
2169void ll_win_yield_ms(unsigned ms);
2172#endif
2173#endif
2176
2177
2178#endif
Definition ll_platform_generic.h:759
Definition ll_platform_generic.h:1689
Definition ll_platform_generic.h:1437
Definition ll_platform_generic.h:584
LL_String & append(char c)
Definition ll_platform_generic.h:637
void set(const char *s, int n)
Definition ll_platform_generic.h:596
LL_String & append(const char *s1)
Definition ll_platform_generic.h:622
int length() const
O(1): a stored length, not a strlen per call.
Definition ll_platform_generic.h:664
Definition ll_platform_generic.h:1723
bool set_input_callback(void(*cb)())
JIT exception-escape context; opaque to library users (ll_vm_internal.h).
Definition LambLisp.h:1207
Definition ll_platform_generic.h:725
int peek() override
One byte of pushback, which is all Stream promises. LambStdioClass has no peek of its own.
Definition ll_llarduino_print.cpp:130
int ll_llarduino_pin_mode(int pin)
Definition ll_llarduino_null.cpp:250
size_t println()
CRLF, as Arduino does – not "\n".
Definition ll_llarduino_print.cpp:82
size_t printf(const char *fmt,...) CHECKPRINTF_pos2
HardwareSerial::printf, used by LL itself.
Definition ll_llarduino_print.cpp:93
void pinMode(int pin, int mode)
Public Arduino surface – one line each, straight through the table.
Definition ll_llarduino_null.cpp:241
int available() override
Definition ll_llarduino_print.cpp:117
WiFiClass * LL_WiFi
Definition ll_xmop3_WiFi.cpp:31
const char *const * ll_feature_names(int *count)
The feature identifiers reported by (features) and matched by cond-expand.
Definition ll_xmop3_platform_generic.cpp:531
void ll_wifi_boot_reserve()
Definition ll_xmop3_WiFi.cpp:587
A board's panel: geometry, model, and the five operations. See the note above on NULL.
Definition ll_platform_generic.h:1654
const char * name
"st7789-240x240-spi3", "co5300-410x502-qspi", "rgb565-480x480-par16"
Definition ll_platform_generic.h:1655
bool(* init)(Lamb &lamb)
bring up bus + controller; idempotent
Definition ll_platform_generic.h:1658
LL_PanelModel model
PUSH or SCAN – branch on this, not on the name.
Definition ll_platform_generic.h:1657
void(* set_brightness)(Lamb &lamb, int percent)
0..100, or NULL if the panel has none
Definition ll_platform_generic.h:1662
int h
panel geometry in pixels
Definition ll_platform_generic.h:1656
void(* blit)(Lamb &lamb, int x, int y, int w, int h, const uint16_t *src)
PUSH only; NULL on SCAN.
Definition ll_platform_generic.h:1660
void(* fill)(Lamb &lamb, int x, int y, int w, int h, uint16_t color)
filled rect, RGB565
Definition ll_platform_generic.h:1659
void(* on_off)(Lamb &lamb, bool on)
display on/off, or NULL
Definition ll_platform_generic.h:1663