guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 02/05: Add disjoint syntax object type


From: Andy Wingo
Subject: [Guile-commits] 02/05: Add disjoint syntax object type
Date: Tue, 28 Mar 2017 15:28:29 -0400 (EDT)

wingo pushed a commit to branch master
in repository guile.

commit 64c5cc58fced3092f17639bbbddb46c1bae974c8
Author: Andy Wingo <address@hidden>
Date:   Fri Mar 24 11:17:26 2017 +0100

    Add disjoint syntax object type
    
    * libguile/Makefile.am (address@hidden@_la_SOURCES):
      (DOT_X_FILES, DOT_DOC_FILES, noinst_HEADERS): Add syntax.c and
      syntax.h.
    * libguile/evalext.c (scm_self_evaluating_p):
    * libguile/goops.c (class_syntax, scm_class_of, scm_goops_early_init):
    * libguile/init.c (scm_init_guile):
    * libguile/print.c (iprin1):
    * libguile/tags.h (scm_tc7_syntax):
    * module/oop/goops.scm (<syntax>):
    * module/system/base/types.scm (%tc7-syntax, cell->object):
    * module/system/vm/disassembler.scm (code-annotation): Wire up the new
      data type.
    * libguile/syntax.c:
    * libguile/syntax.h: New files.
    * module/ice-9/boot-9.scm: Move new definitions to (system syntax
      internal).
    * module/system/syntax.scm (print-syntax): New helper.
    * module/system/vm/assembler.scm (statically-allocatable?)
      (intern-constant, link-data): Arrange to be able to write syntax
      objects into images.
    * module/language/cps/types.scm (&syntax): New type.  Remove
      &hash-table; it was never detected, an internal binding, and we need
      the bit to avoid going into bignum territory.
---
 libguile/Makefile.am              |   4 ++
 libguile/evalext.c                |   1 +
 libguile/goops.c                  |   4 ++
 libguile/init.c                   |   2 +
 libguile/print.c                  |   4 ++
 libguile/syntax.c                 | 120 ++++++++++++++++++++++++++++++++++++++
 libguile/syntax.h                 |  34 +++++++++++
 libguile/tags.h                   |   2 +-
 module/ice-9/boot-9.scm           |   9 ++-
 module/language/cps/types.scm     |   6 +-
 module/oop/goops.scm              |   3 +-
 module/system/base/types.scm      |   6 ++
 module/system/syntax.scm          |   9 ++-
 module/system/vm/assembler.scm    |  24 +++++++-
 module/system/vm/disassembler.scm |   1 +
 15 files changed, 221 insertions(+), 8 deletions(-)

diff --git a/libguile/Makefile.am b/libguile/Makefile.am
index 142e739..2214a4a 100644
--- a/libguile/Makefile.am
+++ b/libguile/Makefile.am
@@ -212,6 +212,7 @@ address@hidden@_la_SOURCES =                                
\
        strports.c                              \
        struct.c                                \
        symbols.c                               \
+       syntax.c                                \
        threads.c                               \
        throw.c                                 \
        trees.c                                 \
@@ -316,6 +317,7 @@ DOT_X_FILES =                                       \
        strports.x                              \
        struct.x                                \
        symbols.x                               \
+       syntax.x                                \
        threads.x                               \
        throw.x                                 \
        trees.x                                 \
@@ -418,6 +420,7 @@ DOT_DOC_FILES =                             \
        strports.doc                            \
        struct.doc                              \
        symbols.doc                             \
+       syntax.doc                              \
        threads.doc                             \
        throw.doc                               \
        trees.doc                               \
@@ -509,6 +512,7 @@ noinst_HEADERS = conv-integer.i.c conv-uinteger.i.c         
\
                  posix-w32.h                                   \
                 private-options.h                              \
                 ports-internal.h                               \
+                syntax.h                                       \
                 weak-list.h
 
 # vm instructions
diff --git a/libguile/evalext.c b/libguile/evalext.c
index 48d9a17..33205a2 100644
--- a/libguile/evalext.c
+++ b/libguile/evalext.c
@@ -82,6 +82,7 @@ SCM_DEFINE (scm_self_evaluating_p, "self-evaluating?", 1, 0, 
0,
        case scm_tc7_dynamic_state:
         case scm_tc7_frame:
         case scm_tc7_keyword:
+        case scm_tc7_syntax:
         case scm_tc7_vm_cont:
        case scm_tc7_number:
        case scm_tc7_string:
diff --git a/libguile/goops.c b/libguile/goops.c
index 8ed0f60..a158a1c 100644
--- a/libguile/goops.c
+++ b/libguile/goops.c
@@ -110,6 +110,7 @@ static SCM class_applicable_struct_class;
 static SCM class_applicable_struct_with_setter_class;
 static SCM class_number, class_list;
 static SCM class_keyword;
+static SCM class_syntax;
 static SCM class_atomic_box;
 static SCM class_port, class_input_output_port;
 static SCM class_input_port, class_output_port;
@@ -227,6 +228,8 @@ SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
          return class_frame;
         case scm_tc7_keyword:
          return class_keyword;
+        case scm_tc7_syntax:
+         return class_syntax;
         case scm_tc7_atomic_box:
          return class_atomic_box;
         case scm_tc7_vm_cont:
@@ -1002,6 +1005,7 @@ SCM_DEFINE (scm_sys_goops_early_init, 
"%goops-early-init", 0, 0, 0,
   class_dynamic_state = scm_variable_ref (scm_c_lookup ("<dynamic-state>"));
   class_frame = scm_variable_ref (scm_c_lookup ("<frame>"));
   class_keyword = scm_variable_ref (scm_c_lookup ("<keyword>"));
+  class_syntax = scm_variable_ref (scm_c_lookup ("<syntax>"));
   class_atomic_box = scm_variable_ref (scm_c_lookup ("<atomic-box>"));
   class_vm_cont = scm_variable_ref (scm_c_lookup ("<vm-continuation>"));
   class_bytevector = scm_variable_ref (scm_c_lookup ("<bytevector>"));
diff --git a/libguile/init.c b/libguile/init.c
index 1a6f599..b046685 100644
--- a/libguile/init.c
+++ b/libguile/init.c
@@ -124,6 +124,7 @@
 #include "libguile/strports.h"
 #include "libguile/struct.h"
 #include "libguile/symbols.h"
+#include "libguile/syntax.h"
 #include "libguile/throw.h"
 #include "libguile/arrays.h"
 #include "libguile/trees.h"
@@ -507,6 +508,7 @@ scm_i_init_guile (void *base)
   scm_init_evalext ();
   scm_init_debug ();   /* Requires macro smobs */
   scm_init_simpos ();
+  scm_init_syntax ();
 #if HAVE_MODULES
   scm_init_dynamic_linking (); /* Requires smob_prehistory */
 #endif
diff --git a/libguile/print.c b/libguile/print.c
index 9669dcf..7667d24 100644
--- a/libguile/print.c
+++ b/libguile/print.c
@@ -46,6 +46,7 @@
 #include "libguile/ports-internal.h"
 #include "libguile/strings.h"
 #include "libguile/strports.h"
+#include "libguile/syntax.h"
 #include "libguile/vectors.h"
 #include "libguile/numbers.h"
 #include "libguile/vm.h"
@@ -716,6 +717,9 @@ iprin1 (SCM exp, SCM port, scm_print_state *pstate)
           scm_puts ("#:", port);
           scm_iprin1 (scm_keyword_to_symbol (exp), port, pstate);
           break;
+        case scm_tc7_syntax:
+         scm_i_syntax_print (exp, port, pstate);
+          break;
        case scm_tc7_atomic_box:
          scm_i_atomic_box_print (exp, port, pstate);
          break;
diff --git a/libguile/syntax.c b/libguile/syntax.c
new file mode 100644
index 0000000..df12c69
--- /dev/null
+++ b/libguile/syntax.c
@@ -0,0 +1,120 @@
+/* Copyright (C) 2017 Free Software Foundation, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "libguile/_scm.h"
+#include "libguile/keywords.h"
+#include "libguile/ports.h"
+#include "libguile/syntax.h"
+#include "libguile/validate.h"
+
+
+
+static int
+scm_is_syntax (SCM x)
+{
+  return SCM_HAS_TYP7 (x, scm_tc7_syntax);
+}
+
+#define SCM_VALIDATE_SYNTAX(pos, scm) \
+  SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_syntax, "syntax object")
+
+SCM_DEFINE (scm_syntax_p, "syntax?", 1, 0, 0,
+            (SCM obj),
+           "Return @code{#t} if the argument @var{obj} is a syntax object,\n"
+            "else @code{#f}.")
+#define FUNC_NAME s_scm_syntax_p
+{
+  return scm_from_bool (scm_is_syntax (obj));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_make_syntax, "make-syntax", 3, 0, 0,
+           (SCM exp, SCM wrap, SCM module),
+           "Make a new syntax object.")
+#define FUNC_NAME s_scm_make_syntax
+{
+  return scm_double_cell (scm_tc7_syntax, SCM_UNPACK (exp),
+                          SCM_UNPACK (wrap), SCM_UNPACK (module));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_syntax_expression, "syntax-expression", 1, 0, 0,
+           (SCM obj),
+           "Return the expression contained in the syntax object @var{obj}.")
+#define FUNC_NAME s_scm_syntax_expression
+{
+  SCM_VALIDATE_SYNTAX (1, obj);
+  return SCM_CELL_OBJECT_1 (obj);
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_syntax_wrap, "syntax-wrap", 1, 0, 0,
+           (SCM obj),
+           "Return the wrap contained in the syntax object @var{obj}.")
+#define FUNC_NAME s_scm_syntax_wrap
+{
+  SCM_VALIDATE_SYNTAX (1, obj);
+  return SCM_CELL_OBJECT_2 (obj);
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_syntax_module, "syntax-module", 1, 0, 0,
+           (SCM obj),
+           "Return the module info contained in the syntax object @var{obj}.")
+#define FUNC_NAME s_scm_syntax_module
+{
+  SCM_VALIDATE_SYNTAX (1, obj);
+  return SCM_CELL_OBJECT_3 (obj);
+}
+#undef FUNC_NAME
+
+static SCM print_syntax_var;
+
+static void
+init_print_syntax_var (void)
+{
+  print_syntax_var =
+    scm_c_private_variable ("system syntax", "print-syntax");
+}
+
+void
+scm_i_syntax_print (SCM obj, SCM port, scm_print_state *pstate)
+{
+  static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
+  scm_i_pthread_once (&once, init_print_syntax_var);
+  scm_call_2 (scm_variable_ref (print_syntax_var), obj, port);
+}
+
+void
+scm_init_syntax ()
+{
+#include "libguile/syntax.x"
+}
+
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/
diff --git a/libguile/syntax.h b/libguile/syntax.h
new file mode 100644
index 0000000..7fdfd28
--- /dev/null
+++ b/libguile/syntax.h
@@ -0,0 +1,34 @@
+#ifndef SCM_SYNTAX_H
+#define SCM_SYNTAX_H
+
+/* Copyright (C) 2017 Free Software Foundation, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include "libguile/__scm.h"
+
+SCM_INTERNAL SCM scm_syntax_p (SCM obj);
+SCM_INTERNAL SCM scm_make_syntax (SCM exp, SCM wrap, SCM module);
+SCM_INTERNAL SCM scm_syntax_expression (SCM obj);
+SCM_INTERNAL SCM scm_syntax_wrap (SCM obj);
+SCM_INTERNAL SCM scm_syntax_module (SCM obj);
+
+SCM_INTERNAL void scm_i_syntax_print (SCM obj, SCM port,
+                                      scm_print_state *pstate);
+SCM_INTERNAL void scm_init_syntax (void);
+
+#endif  /* SCM_SYNTAX_H */
diff --git a/libguile/tags.h b/libguile/tags.h
index 8f44d96..3a01a15 100644
--- a/libguile/tags.h
+++ b/libguile/tags.h
@@ -416,7 +416,7 @@ typedef union SCM { struct { scm_t_bits n; } n; } SCM;
 #define scm_tc7_frame          0x2f
 #define scm_tc7_keyword                0x35
 #define scm_tc7_atomic_box     0x37
-#define scm_tc7_unused_3d      0x3d
+#define scm_tc7_syntax         0x3d
 #define scm_tc7_unused_3f      0x3f
 #define scm_tc7_program                0x45
 #define scm_tc7_vm_cont                0x47
diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index 07d357d..be890fa 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -4087,10 +4087,15 @@ when none is available, reading FILE-NAME with READER."
     (module-export! to ids))
 
   (steal-bindings! the-root-module (resolve-module '(system syntax internal))
-                   '(syntax-local-binding
+                   '(syntax?
+                     syntax-local-binding
                      %syntax-module
                      syntax-locally-bound-identifiers
-                     syntax-session-id)))
+                     syntax-session-id
+                     make-syntax
+                     syntax-expression
+                     syntax-wrap
+                     syntax-module)))
 
 
 
diff --git a/module/language/cps/types.scm b/module/language/cps/types.scm
index fd592ea..8464a65 100644
--- a/module/language/cps/types.scm
+++ b/module/language/cps/types.scm
@@ -84,6 +84,7 @@
   #:use-module (language cps intset)
   #:use-module (rnrs bytevectors)
   #:use-module (srfi srfi-11)
+  #:use-module ((system syntax internal) #:select (syntax?))
   #:export (;; Specific types.
             &exact-integer
             &flonum
@@ -112,7 +113,7 @@
             &bytevector
             &bitvector
             &array
-            &hash-table
+            &syntax
 
             ;; Union types.
             &number &real
@@ -169,7 +170,7 @@
   &bytevector
   &bitvector
   &array
-  &hash-table
+  &syntax
 
   &f64
   &u64
@@ -348,6 +349,7 @@ minimum, and maximum."
    ((bytevector? val) (return &bytevector (bytevector-length val)))
    ((bitvector? val) (return &bitvector (bitvector-length val)))
    ((array? val) (return &array (array-rank val)))
+   ((syntax? val) (return &syntax 0))
    ((not (variable-bound? (make-variable val))) (return &unbound #f))
 
    (else (error "unhandled constant" val))))
diff --git a/module/oop/goops.scm b/module/oop/goops.scm
index b7d980d..a469180 100644
--- a/module/oop/goops.scm
+++ b/module/oop/goops.scm
@@ -62,7 +62,7 @@
             <boolean> <char> <list> <pair> <null> <string> <symbol>
             <vector> <bytevector> <uvec> <foreign> <hashtable>
             <fluid> <dynamic-state> <frame> <vm> <vm-continuation>
-            <keyword> <atomic-box>
+            <keyword> <syntax> <atomic-box>
 
             ;; Numbers.
             <number> <complex> <real> <integer> <fraction>
@@ -1009,6 +1009,7 @@ slots as we go."
 (define-standard-class <integer> (<real>))
 (define-standard-class <fraction> (<real>))
 (define-standard-class <keyword> (<top>))
+(define-standard-class <syntax> (<top>))
 (define-standard-class <atomic-box> (<top>))
 (define-standard-class <unknown> (<top>))
 (define-standard-class <procedure> (<applicable>)
diff --git a/module/system/base/types.scm b/module/system/base/types.scm
index 652c922..53a3dbe 100644
--- a/module/system/base/types.scm
+++ b/module/system/base/types.scm
@@ -23,6 +23,7 @@
   #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-60)
+  #:use-module (system syntax internal)
   #:use-module (ice-9 match)
   #:use-module (ice-9 iconv)
   #:use-module (ice-9 format)
@@ -254,6 +255,7 @@ the matching bits, possibly with bitwise operations to 
extract it from BITS."
 (define %tc7-dynamic-state #x2d)
 (define %tc7-frame #x2f)
 (define %tc7-keyword #x35)
+(define %tc7-syntax #x3d)
 (define %tc7-program #x45)
 (define %tc7-vm-continuation #x47)
 (define %tc7-bytevector #x4d)
@@ -464,6 +466,10 @@ using BACKEND."
            (make-pointer address))
           (((_ & #x7f = %tc7-keyword) symbol)
            (symbol->keyword (cell->object symbol backend)))
+          (((_ & #x7f = %tc7-syntax) expression wrap module)
+           (make-syntax (cell->object expression backend)
+                        (cell->object wrap backend)
+                        (cell->object module backend)))
           (((_ & #x7f = %tc7-vm-continuation))
            (inferior-object 'vm-continuation address))
           (((_ & #x7f = %tc7-weak-set))
diff --git a/module/system/syntax.scm b/module/system/syntax.scm
index 9d6bc57..34fadb3 100644
--- a/module/system/syntax.scm
+++ b/module/system/syntax.scm
@@ -20,7 +20,14 @@
 
 (define-module (system syntax)
   #:use-module (system syntax internal)
-  #:re-export (syntax-local-binding
+  #:re-export (syntax?
+               syntax-local-binding
                (%syntax-module . syntax-module)
                syntax-locally-bound-identifiers
                syntax-session-id))
+
+;; Used by syntax.c.
+(define (print-syntax obj port)
+  ;; FIXME: Use syntax->datum instad of syntax-expression, when
+  ;; syntax->datum can operate on new syntax objects.
+  (format port "#<syntax ~s>" (syntax-expression obj)))
diff --git a/module/system/vm/assembler.scm b/module/system/vm/assembler.scm
index 9ac3fa6..56c33be 100644
--- a/module/system/vm/assembler.scm
+++ b/module/system/vm/assembler.scm
@@ -47,6 +47,7 @@
   #:use-module (system vm dwarf)
   #:use-module (system vm elf)
   #:use-module (system vm linker)
+  #:use-module (system syntax internal)
   #:use-module (language bytecode)
   #:use-module (rnrs bytevectors)
   #:use-module (ice-9 binary-ports)
@@ -1017,7 +1018,8 @@ immediate, and @code{#f} otherwise."
   "Return @code{#t} if a non-immediate constant can be allocated
 statically, and @code{#f} if it would need some kind of runtime
 allocation."
-  (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x) (array? x)))
+  (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x)
+      (array? x) (syntax? x)))
 
 (define (intern-constant asm obj)
   "Add an object to the constant table, and return a label that can be
@@ -1045,6 +1047,10 @@ table, its existing label is used directly."
                 (append-reverse (field label (1+ i) (vector-ref obj i))
                                 inits))
             (reverse inits))))
+     ((syntax? obj)
+      (append (field label 1 (syntax-expression obj))
+              (field label 2 (syntax-wrap obj))
+              (field label 3 (syntax-module obj))))
      ((stringbuf? obj) '())
      ((static-procedure? obj)
       `((static-patch! ,label 1 ,(static-procedure-code obj))))
@@ -1181,6 +1187,7 @@ returned instead."
 ;(define-tc7-macro-assembler br-if-dynamic-state 45)
 ;(define-tc7-macro-assembler br-if-frame 47)
 (define-tc7-macro-assembler br-if-keyword #x35)
+;(define-tc7-macro-assembler br-if-syntax #x3d)
 ;(define-tc7-macro-assembler br-if-vm 55)
 ;(define-tc7-macro-assembler br-if-vm-cont 71)
 ;(define-tc7-macro-assembler br-if-rtl-program 69)
@@ -1391,6 +1398,7 @@ should be .data or .rodata), and return the resulting 
linker object.
   (define tc7-narrow-stringbuf tc7-stringbuf)
   (define tc7-wide-stringbuf (+ tc7-stringbuf stringbuf-wide-flag))
   (define tc7-ro-string (+ 21 #x200))
+  (define tc7-syntax #x3d)
   (define tc7-program 69)
   (define tc7-bytevector 77)
   (define tc7-bitvector 95)
@@ -1415,6 +1423,8 @@ should be .data or .rodata), and return the resulting 
linker object.
         (* 2 word-size))
        ((simple-vector? x)
         (* (1+ (vector-length x)) word-size))
+       ((syntax? x)
+        (* 4 word-size))
        ((simple-uniform-vector? x)
         (* 4 word-size))
        ((uniform-vector-backing-store? x)
@@ -1519,6 +1529,18 @@ should be .data or .rodata), and return the resulting 
linker object.
        ((keyword? obj)
         (write-placeholder asm buf pos))
 
+       ((syntax? obj)
+        (case word-size
+          ((4) (bytevector-u32-set! buf pos tc7-syntax endianness))
+          ((8) (bytevector-u64-set! buf pos tc7-syntax endianness))
+          (else (error "bad word size")))
+        (write-constant-reference buf (+ pos (* 1 word-size))
+                                  (syntax-expression obj))
+        (write-constant-reference buf (+ pos (* 2 word-size))
+                                  (syntax-wrap obj))
+        (write-constant-reference buf (+ pos (* 3 word-size))
+                                  (syntax-module obj)))
+
        ((number? obj)
         (write-placeholder asm buf pos))
 
diff --git a/module/system/vm/disassembler.scm 
b/module/system/vm/disassembler.scm
index b6f4f78..4db4a03 100644
--- a/module/system/vm/disassembler.scm
+++ b/module/system/vm/disassembler.scm
@@ -210,6 +210,7 @@ address of that offset."
                         ((13) "vector?")
                         ((15) "string?")
                         ((53) "keyword?")
+                        ((#x3d) "syntax?")
                         ((77) "bytevector?")
                         ((95) "bitvector?")
                         (else (number->string tc7)))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]