altos/stmf0: Add a comment about the requirements for using ao_flash_stm
[fw/altos] / src / lisp / ao_lisp.h
index 6122a2edf5f6406ad991afe10a29bc459ecad3f2..ea3d2a0905f8d5e8e9deab93523404f3bd0ecb61 100644 (file)
 //#include <stdio.h>
 #include <ao_lisp_os.h>
 
+typedef uint16_t       ao_poly;
+typedef int16_t                ao_signed_poly;
+
+#ifdef AO_LISP_SAVE
+
+struct ao_lisp_os_save {
+       ao_poly         atoms;
+       ao_poly         globals;
+       uint16_t        const_checksum;
+       uint16_t        const_checksum_inv;
+};
+
+#define AO_LISP_POOL_EXTRA     (sizeof(struct ao_lisp_os_save))
+#define AO_LISP_POOL   ((int) (AO_LISP_POOL_TOTAL - AO_LISP_POOL_EXTRA))
+
+int
+ao_lisp_os_save(void);
+
+int
+ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
+
+int
+ao_lisp_os_restore(void);
+
+#endif
+
 #ifdef AO_LISP_MAKE_CONST
 #define AO_LISP_POOL_CONST     16384
 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
@@ -36,19 +62,25 @@ extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
 #define _ao_lisp_atom_cdr      _atom("cdr")
 #define _ao_lisp_atom_cons     _atom("cons")
 #define _ao_lisp_atom_last     _atom("last")
+#define _ao_lisp_atom_length   _atom("length")
 #define _ao_lisp_atom_cond     _atom("cond")
 #define _ao_lisp_atom_lambda   _atom("lambda")
 #define _ao_lisp_atom_led      _atom("led")
 #define _ao_lisp_atom_delay    _atom("delay")
+#define _ao_lisp_atom_pack     _atom("pack")
+#define _ao_lisp_atom_unpack   _atom("unpack")
+#define _ao_lisp_atom_flush    _atom("flush")
 #define _ao_lisp_atom_eval     _atom("eval")
 #define _ao_lisp_atom_read     _atom("read")
 #define _ao_lisp_atom_eof      _atom("eof")
+#define _ao_lisp_atom_save     _atom("save")
+#define _ao_lisp_atom_restore  _atom("restore")
 #else
 #include "ao_lisp_const.h"
 #ifndef AO_LISP_POOL
-#define AO_LISP_POOL   16384
+#define AO_LISP_POOL   3072
 #endif
-extern uint8_t         ao_lisp_pool[AO_LISP_POOL];
+extern uint8_t         ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA];
 #endif
 
 /* Primitive types */
@@ -81,9 +113,6 @@ extern uint16_t              ao_lisp_top;
 
 extern uint8_t         ao_lisp_exception;
 
-typedef uint16_t       ao_poly;
-typedef int16_t                ao_signed_poly;
-
 static inline int
 ao_lisp_is_const(ao_poly poly) {
        return poly & AO_LISP_CONST;
@@ -95,26 +124,11 @@ ao_lisp_is_const(ao_poly poly) {
 #define AO_LISP_IS_CONST(a)    (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
 #define AO_LISP_IS_POOL(a)     (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
 
-static inline void *
-ao_lisp_ref(ao_poly poly) {
-       if (poly == 0xBEEF)
-               ao_lisp_abort();
-       if (poly == AO_LISP_NIL)
-               return NULL;
-       if (poly & AO_LISP_CONST)
-               return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
-       return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
-}
+void *
+ao_lisp_ref(ao_poly poly);
 
-static inline ao_poly
-ao_lisp_poly(const void *addr, ao_poly type) {
-       const uint8_t   *a = addr;
-       if (a == NULL)
-               return AO_LISP_NIL;
-       if (AO_LISP_IS_CONST(a))
-               return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
-       return (a - AO_LISP_POOL_BASE) | type;
-}
+ao_poly
+ao_lisp_poly(const void *addr, ao_poly type);
 
 struct ao_lisp_type {
        int     (*size)(void *addr);
@@ -215,6 +229,7 @@ enum ao_lisp_builtin_id {
        builtin_cdr,
        builtin_cons,
        builtin_last,
+       builtin_length,
        builtin_quote,
        builtin_set,
        builtin_setq,
@@ -233,8 +248,13 @@ enum ao_lisp_builtin_id {
        builtin_greater,
        builtin_less_equal,
        builtin_greater_equal,
+       builtin_pack,
+       builtin_unpack,
+       builtin_flush,
        builtin_delay,
        builtin_led,
+       builtin_save,
+       builtin_restore,
        _builtin_last
 };
 
@@ -409,6 +429,9 @@ ao_lisp_cons_print(ao_poly);
 void
 ao_lisp_cons_patom(ao_poly);
 
+int
+ao_lisp_cons_length(struct ao_lisp_cons *cons);
+
 /* string */
 extern const struct ao_lisp_type ao_lisp_string_type;
 
@@ -421,6 +444,12 @@ ao_lisp_string_copy(char *a);
 char *
 ao_lisp_string_cat(char *a, char *b);
 
+ao_poly
+ao_lisp_string_pack(struct ao_lisp_cons *cons);
+
+ao_poly
+ao_lisp_string_unpack(char *a);
+
 void
 ao_lisp_string_print(ao_poly s);
 
@@ -466,6 +495,12 @@ ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
 
 /* eval */
 
+void
+ao_lisp_eval_clear_globals(void);
+
+int
+ao_lisp_eval_restart(void);
+
 ao_poly
 ao_lisp_eval(ao_poly p);
 
@@ -510,8 +545,8 @@ ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
 struct ao_lisp_frame *
 ao_lisp_frame_new(int num);
 
-struct ao_lisp_frame *
-ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
+int
+ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
 
 void
 ao_lisp_frame_print(ao_poly p);
@@ -538,8 +573,15 @@ ao_poly
 ao_lisp_macro(struct ao_lisp_cons *cons);
 
 ao_poly
-ao_lisp_lambda_eval(struct ao_lisp_lambda *lambda,
-                   struct ao_lisp_cons *cons);
+ao_lisp_lambda_eval(void);
+
+/* save */
+
+ao_poly
+ao_lisp_save(struct ao_lisp_cons *cons);
+
+ao_poly
+ao_lisp_restore(struct ao_lisp_cons *cons);
 
 /* error */