]> gitweb.factorcode.org Git - factor.git/commitdiff
more compiler work, a few java factor fixes
authorSlava Pestov <slava@factorcode.org>
Tue, 7 Sep 2004 02:39:12 +0000 (02:39 +0000)
committerSlava Pestov <slava@factorcode.org>
Tue, 7 Sep 2004 02:39:12 +0000 (02:39 +0000)
33 files changed:
TODO.FACTOR.txt
factor/FactorLib.java
factor/compiler/FlowObject.java
factor/jedit/FactorPlugin.java
library/compiler/assembler.factor [new file with mode: 0644]
library/compiler/assembly-x86.factor [new file with mode: 0644]
library/compiler/compiler.factor [new file with mode: 0644]
library/cross-compiler.factor
library/platform/jvm/sbuf.factor
library/platform/jvm/stream.factor
library/platform/native/boot-stage2.factor
library/platform/native/compiler.factor [deleted file]
library/platform/native/debugger.factor
library/platform/native/init.factor
library/platform/native/primitives.factor
library/platform/native/words.factor
library/sbuf.factor
library/strings.factor
native/arithmetic.c
native/compiler.c
native/compiler.h
native/error.c
native/error.h
native/gc.c
native/image.c
native/memory.c
native/memory.h
native/primitives.c
native/primitives.h
native/sbuf.c
native/sbuf.h
native/string.c
native/string.h

index 9083cd685fcf9f15989f769e5a8338d27288fc07..acf294b0c5cf25eeeb6305f57e2fc1b6fa3c32a5 100644 (file)
@@ -4,11 +4,16 @@
 - plugin should not exit jEdit on fatal errors\r
 - wordpreview: don't show for string literals and comments\r
 - alist -vs- assoc terminology\r
+- NPE in activate()/deactivate()\r
+- write-icon kind of messy; " " should be output by the listener\r
+- f usages. --> don't print all words\r
+- file responder: don't show full path in title\r
+\r
 - clean up listener's action popups\r
 - jedit ==> jedit-word, jedit takes a file name\r
 - introduce ifte* and ?str-head/?str-tail where appropriate\r
 - namespace clone drops static var bindings\r
-<kc5tja> The binary register ordering, from 0 to 7, is EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI.\r
+- when running (inf, .factor-rc not loaded\r
 \r
 + bignums:\r
 \r
@@ -44,7 +49,6 @@
 \r
 + listener/plugin:\r
 \r
-- NPE in activate()/deactivate()\r
 - NPE in ErrorHighlight\r
 - some way to not have previous definitions from a source file\r
   clutter the namespace\r
 \r
 + misc:\r
 \r
-- write-icon kind of messy; " " should be output by the listener\r
-- f usages. --> don't print all words\r
-- pipe support\r
-- telnetd: init-history\r
-- str-reverse primitive\r
 - some way to run httpd from command line\r
 - don't rehash strings on every startup\r
 - 'cascading' styles\r
index 03ae9fe3c81f2e002f373c716454e088d32c2512..59dfda749bd627a6ec6a35426e0b45447f6bb98e 100644 (file)
@@ -216,7 +216,12 @@ public class FactorLib
                                break;
                        buf.append((char)b);
                }
-               return buf.toString();
+
+               /* EOF? */
+               if(b == -1 && buf.length() == 0)
+                       return null;
+               else
+                       return buf.toString();
        } //}}}
 
        //{{{ readCount() method
index 6b47f99044db6d624430805d4070100969a6e358..a92b78eeafc70925b7b50980b878b6241ebf6f94 100644 (file)
@@ -173,11 +173,25 @@ implements Constants, FactorExternalizable, PublicCloneable
                }
                else
                {
-                       mw.visitMethodInsn(INVOKESTATIC,
-                               "factor/FactorJava",
-                               methodName,
-                               "(Ljava/lang/Object;)"
-                               + FactorJava.javaClassToVMClass(type));
+                       String signature;
+                       if(type.isArray())
+                       {
+                               signature = "(Ljava/lang/Object;)"
+                                       + "[Ljava/lang/Object;";
+                       }
+                       else
+                       {
+                               signature = "(Ljava/lang/Object;)"
+                                       + FactorJava.javaClassToVMClass(type);
+                       }
+                       mw.visitMethodInsn(INVOKESTATIC,"factor/FactorJava",
+                               methodName,signature);
+                       /* if(type.isArray())
+                       {
+                               mw.visitTypeInsn(CHECKCAST,
+                                       type.getName()
+                                       .replace('.','/'));
+                       } */
                }
        } //}}}
 
index cffd7927d8fdefdea73c015f67f476e99a6f23a0..6f47c65905f141a372712b08d14dd21d203bca7a 100644 (file)
@@ -309,6 +309,7 @@ public class FactorPlugin extends EditPlugin
 
                Buffer buffer = view.getBuffer();
                int lastUseOffset = 0;
+               boolean trailingNewline = false;
 
                for(int i = 0; i < buffer.getLineCount(); i++)
                {
@@ -325,12 +326,17 @@ public class FactorPlugin extends EditPlugin
                                        lastUseOffset = buffer.getLineEndOffset(i-1) - 1;
                        }
                        else
+                       {
+                               trailingNewline = true;
                                break;
+                       }
                }
 
                String decl = "USE: " + vocab;
                if(lastUseOffset != 0)
                        decl = "\n" + decl;
+               if(trailingNewline)
+                       decl = decl + "\n";
                buffer.insert(lastUseOffset,decl);
                showStatus(view,"inserted-use",decl);
        } //}}}
diff --git a/library/compiler/assembler.factor b/library/compiler/assembler.factor
new file mode 100644 (file)
index 0000000..6411bf0
--- /dev/null
@@ -0,0 +1,58 @@
+! :folding=indent:collapseFolds=1:
+
+! $Id$
+!
+! Copyright (C) 2004 Slava Pestov.
+! 
+! Redistribution and use in source and binary forms, with or without
+! modification, are permitted provided that the following conditions are met:
+! 
+! 1. Redistributions of source code must retain the above copyright notice,
+!    this list of conditions and the following disclaimer.
+! 
+! 2. Redistributions in binary form must reproduce the above copyright notice,
+!    this list of conditions and the following disclaimer in the documentation
+!    and/or other materials provided with the distribution.
+! 
+! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+IN: compiler
+USE: math
+USE: kernel
+USE: stack
+
+: cell 4 ;
+: literal-table 1024 cell * ;
+
+: init-assembler ( -- )
+    compiled-offset literal-table + set-compiled-offset ;
+
+: intern-literal ( obj -- lit# )
+    address-of
+    literal-top set-compiled-cell
+    literal-top dup cell + set-literal-top ;
+
+: compile-byte ( n -- )
+    compiled-offset set-compiled-byte
+    compiled-offset 1 + set-compiled-offset ;
+
+: compile-cell ( n -- )
+    compiled-offset set-compiled-cell
+    compiled-offset cell + set-compiled-offset ;
+
+: DATASTACK ( -- ptr )
+    #! A pointer to a pointer to the datastack top.
+    11 getenv ;
+
+: CALLSTACK ( -- ptr )
+    #! A pointer to a pointer to the callstack top.
+    12 getenv ;
diff --git a/library/compiler/assembly-x86.factor b/library/compiler/assembly-x86.factor
new file mode 100644 (file)
index 0000000..01beb90
--- /dev/null
@@ -0,0 +1,106 @@
+! :folding=indent:collapseFolds=1:
+
+! $Id$
+!
+! Copyright (C) 2004 Slava Pestov.
+! 
+! Redistribution and use in source and binary forms, with or without
+! modification, are permitted provided that the following conditions are met:
+! 
+! 1. Redistributions of source code must retain the above copyright notice,
+!    this list of conditions and the following disclaimer.
+! 
+! 2. Redistributions in binary form must reproduce the above copyright notice,
+!    this list of conditions and the following disclaimer in the documentation
+!    and/or other materials provided with the distribution.
+! 
+! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+IN: compiler
+USE: kernel
+USE: compiler
+USE: math
+USE: stack
+
+: EAX 0 ;
+: ECX 1 ;
+: EDX 2 ;
+: EBX 3 ;
+: ESP 4 ;
+: EBP 5 ;
+: ESI 6 ;
+: EDI 7 ;
+
+: PUSH ( reg -- )
+    HEX: 50 + compile-byte ;
+
+: POP ( reg -- )
+    HEX: 58 + compile-byte ;
+
+: I>R ( imm reg -- )
+    #! MOV <imm> TO <reg>
+    HEX: b8 + compile-byte  compile-cell ;
+
+: [I]>R ( imm reg -- )
+    #! MOV INDIRECT <imm> TO <reg>
+    HEX: a1 + compile-byte  compile-cell ;
+
+: I>[R] ( imm reg -- )
+    #! MOV <imm> TO INDIRECT <reg>
+    HEX: c7 compile-byte  compile-byte  compile-cell ;
+
+: [R]>R ( reg reg -- )
+    #! MOV INDIRECT <reg> TO <reg>.
+    HEX: 8b compile-byte  swap 3 shift bitor compile-byte ;
+
+: R>[R] ( reg reg -- )
+    #! MOV <reg> TO INDIRECT <reg>.
+    HEX: 89 compile-byte  swap 3 shift bitor compile-byte ;
+
+: I+[I] ( imm addr -- )
+    #! ADD <imm> TO ADDRESS <addr>
+    HEX: 81 compile-byte
+    HEX: 05 compile-byte
+    compile-cell
+    compile-cell ;
+
+: LITERAL ( cell -- )
+    #! Push literal on data stack.
+    #! Assume that it is ok to clobber EAX without saving.
+    DATASTACK EAX [I]>R
+    EAX I>[R]
+    4 DATASTACK I+[I] ;
+
+: [LITERAL] ( cell -- )
+    #! Push literal on data stack by following an indirect
+    #! pointer.
+    ECX PUSH
+    ( cell -- ) ECX I>R
+    ECX ECX [R]>R
+    DATASTACK EAX [I]>R
+    ECX EAX R>[R]
+    4 DATASTACK I+[I]
+    ECX POP ;
+
+: (JMP) ( xt opcode -- )
+    #! JMP, CALL insn is 5 bytes long
+    #! addr is relative to *after* insn
+    compile-byte  compiled-offset 4 + - compile-cell ;
+
+: JMP ( -- )
+    HEX: e9 (JMP) ;
+
+: CALL ( -- )
+    HEX: e8 (JMP) ;
+
+: RET ( -- )
+    HEX: c3 compile-byte ;
diff --git a/library/compiler/compiler.factor b/library/compiler/compiler.factor
new file mode 100644 (file)
index 0000000..14fde7d
--- /dev/null
@@ -0,0 +1,88 @@
+! :folding=indent:collapseFolds=1:
+
+! $Id$
+!
+! Copyright (C) 2004 Slava Pestov.
+! 
+! Redistribution and use in source and binary forms, with or without
+! modification, are permitted provided that the following conditions are met:
+! 
+! 1. Redistributions of source code must retain the above copyright notice,
+!    this list of conditions and the following disclaimer.
+! 
+! 2. Redistributions in binary form must reproduce the above copyright notice,
+!    this list of conditions and the following disclaimer in the documentation
+!    and/or other materials provided with the distribution.
+! 
+! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+IN: compiler
+USE: math
+USE: stack
+USE: lists
+USE: combinators
+USE: words
+USE: namespaces
+USE: unparser
+USE: errors
+USE: strings
+USE: logic
+USE: kernel
+USE: vectors
+
+: compile-word ( word -- )
+    #! Compile a JMP at the end (tail call optimization)
+    word-xt "compile-last" get [ JMP ] [ CALL ] ifte ;
+
+: compile-literal ( obj -- )
+    dup fixnum? [
+        address-of LITERAL
+    ] [
+        intern-literal [LITERAL]
+    ] ifte ;
+
+: commit-literals ( -- )
+    "compile-datastack" get dup [ compile-literal ] vector-each
+    0 swap set-vector-length ;
+
+: postpone ( obj -- )
+    "compile-datastack" get vector-push ;
+
+: compile-atom ( obj -- )
+    [
+        [ word? ] [ commit-literals compile-word ]
+        [ drop t ] [ postpone ]
+    ] cond ;
+
+: compile-loop ( quot -- )
+    dup [
+        unswons
+        over not "compile-last" set
+        compile-atom
+        compile-loop
+    ] [
+        commit-literals drop RET
+    ] ifte ;
+
+: compile-quot ( quot -- xt )
+    [
+        "compile-last" off
+        10 <vector> "compile-datastack" set
+        compiled-offset swap compile-loop
+    ] with-scope ;
+
+: compile ( word -- )
+    intern dup word-parameter compile-quot swap set-word-xt ;
+
+: call-xt ( xt -- )
+    #! For testing.
+    0 f f <word> [ set-word-xt ] keep execute ;
index 390976ecbbf7d63076460ff68edb3fbf9627288a..def7a9f474423ad822de399512b8cdb31b2d85df 100644 (file)
@@ -42,9 +42,12 @@ USE: vectors
 USE: words
 
 IN: compiler
-DEFER: compile-byte
-DEFER: compile-cell
-DEFER: compile-offset
+DEFER: set-compiled-byte
+DEFER: set-compiled-cell
+DEFER: compiled-offset
+DEFER: set-compiled-offset
+DEFER: literal-top
+DEFER: set-literal-top
 
 IN: kernel
 DEFER: getenv
@@ -54,6 +57,7 @@ DEFER: room
 DEFER: os-env
 DEFER: type-of
 DEFER: size-of
+DEFER: address-of
 DEFER: dump
 
 IN: strings
@@ -150,6 +154,7 @@ IN: cross-compiler
         str-hashcode
         index-of*
         substring
+        str-reverse
         sbuf?
         <sbuf>
         sbuf-length
@@ -277,9 +282,13 @@ IN: cross-compiler
         dump
         cwd
         cd
-        compile-byte
-        compile-cell
-        compile-offset
+        set-compiled-byte
+        set-compiled-cell
+        compiled-offset
+        set-compiled-offset
+        literal-top
+        set-literal-top
+        address-of
     ] [
         swap succ tuck primitive,
     ] each drop ;
index ea12ab098e27ec6963af87effcaaeb99587f29ec..e82c0e9acc0226ce8074f8561dbab89f410ddb52 100644 (file)
@@ -58,3 +58,7 @@ USE: stack
 : sbuf-reverse ( sbuf -- )
     #! Destructively reverse a string buffer.
     [ ] "java.lang.StringBuffer" "reverse" jinvoke drop ;
+
+DEFER: str>sbuf
+: str-reverse ( str -- str )
+    str>sbuf dup sbuf-reverse sbuf>str ;
index 12eac0509cb51d18e14ee48fe6e5871d54a1a439..4f21d9c3f92fc6ceaf6b41083fe95ea0921e8945 100644 (file)
@@ -112,8 +112,8 @@ USE: strings
     #! java.io.OutputStream out. The streams are wrapped in
     #! buffered streams.
     <stream> [
-        <bout> "out" set
-        <bin> "in" set
+        "out" set
+        "in" set
         ( -- string )
         [ <byte-stream>/freadln ] "freadln" set
         ( count -- string )
@@ -191,12 +191,12 @@ USE: strings
     <char-stream> ;
 
 : <filebr> ( path -- stream )
-    [ "java.lang.String" ] "java.io.FileInputStream" jnew
+    [ "java.lang.String" ] "java.io.FileInputStream" jnew <bin>
     f
     <byte-stream> ;
 
 : <filebw> ( path -- stream )
-    [ "java.lang.String" ] "java.io.FileOutputStream" jnew
+    [ "java.lang.String" ] "java.io.FileOutputStream" jnew <bout>
     f swap
     <byte-stream> ;
 
@@ -232,8 +232,8 @@ USE: strings
 : <socket-stream> ( socket -- stream )
     #! Wraps a socket inside a byte-stream.
     dup
-    [ [ ] "java.net.Socket" "getInputStream"  jinvoke ]
-    [ [ ] "java.net.Socket" "getOutputStream" jinvoke ]
+    [ [ ] "java.net.Socket" "getInputStream"  jinvoke <bin> ]
+    [ [ ] "java.net.Socket" "getOutputStream" jinvoke <bout> ]
     cleave
     <byte-stream> [
         dup >str "client" set "socket" set
index 07448c7ff7eca0e0acdd3e1fb45c6d2fbf036c32..201b5160255c24a6cacc6fb71bfc44e0e0abdfb2 100644 (file)
@@ -109,7 +109,6 @@ USE: stdio
     "/library/telnetd.factor"
     "/library/inferior.factor"
     "/library/platform/native/profiler.factor"
-    "/library/platform/native/compiler.factor"
 
     "/library/image.factor"
     "/library/cross-compiler.factor"
@@ -132,6 +131,10 @@ USE: stdio
     "/library/jedit/jedit-remote.factor"
     "/library/jedit/jedit.factor"
 
+    "/library/compiler/assembler.factor"
+    "/library/compiler/assembly-x86.factor"
+    "/library/compiler/compiler.factor"
+
     "/library/platform/native/primitives.factor"
 
     "/library/init.factor"
diff --git a/library/platform/native/compiler.factor b/library/platform/native/compiler.factor
deleted file mode 100644 (file)
index ba0286e..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-IN: compiler
-USE: math
-USE: stack
-USE: lists
-USE: combinators
-USE: words
-USE: namespaces
-USE: unparser
-USE: errors
-USE: strings
-USE: logic
-USE: kernel
-
-: DATASTACK
-    #! A pointer to a pointer to the datastack top.
-    11 getenv ;
-
-: EAX 0 ;
-: ECX 1 ;
-: EDX 2 ;
-: EBX 3 ;
-: ESP 4 ;
-: EBP 5 ;
-: ESI 6 ;
-: EDI 7 ;
-
-: I>R ( imm reg -- )
-    #! MOV <imm> TO <reg>
-    HEX: a1 + compile-byte  compile-cell ;
-
-: I>[R] ( imm reg -- )
-    #! MOV <imm> TO ADDRESS <reg>
-    HEX: c7 compile-byte  compile-byte  compile-cell ;
-
-: I+[I] ( imm addr -- )
-    #! ADD <imm> TO ADDRESS <addr>
-    HEX: 81 compile-byte
-    HEX: 05 compile-byte
-    compile-cell
-    compile-cell ;
-
-: LITERAL ( cell -- )
-    #! Push literal on data stack.
-    DATASTACK EAX I>R  EAX I>[R] 4 DATASTACK I+[I] ;
-
-: (JMP) ( xt opcode -- )
-    #! JMP, CALL insn is 5 bytes long
-    #! addr is relative to *after* insn
-    compile-byte  compile-offset 4 + - compile-cell ;
-
-: JMP HEX: e9 (JMP) ;
-: CALL HEX: e8 (JMP) ;
-: RET HEX: c3 compile-byte ;
-
-: compile-word ( word -- )
-    #! Compile a JMP at the end (tail call optimization)
-    word-xt "compile-last" get [ JMP ] [ CALL ] ifte ;
-
-: compile-fixnum ( n -- )
-    3 shift 7 bitnot bitand  LITERAL ;
-
-: compile-atom ( obj -- )
-    [
-        [ fixnum? ] [ compile-fixnum ]
-        [ word? ] [ compile-word ]
-        [ drop t ] [ "Cannot compile " swap unparse cat2 throw ]
-    ] cond ;
-
-: compile-loop ( quot -- )
-    dup [
-        unswons
-        over not "compile-last" set
-        compile-atom
-        compile-loop
-    ] [
-        drop RET
-    ] ifte ;
-
-: compile-quot ( quot -- xt )
-    [
-        "compile-last" off
-        compile-offset swap compile-loop
-    ] with-scope ;
-
-: compile ( word -- )
-    intern dup word-parameter compile-quot swap set-word-xt ;
-
-: call-xt ( xt -- )
-    #! For testing.
-    0 f f <word> [ set-word-xt ] keep execute ;
index 09a17db0d5961245eac58bf354c4b365e6886c36..5b43b6344df238baba18b455037e6a1469e7eb34 100644 (file)
@@ -92,6 +92,9 @@ USE: words
 : bad-primitive-error ( obj -- )
     "Bad primitive number: " write . ;
 
+: c-string-error ( obj -- )
+    "Cannot convert to C string: " write . ;
+
 : kernel-error. ( obj n -- str )
     {
         expired-port-error
@@ -108,6 +111,7 @@ USE: words
         profiling-disabled-error
         negative-array-size-error
         bad-primitive-error
+        c-string-error
     } vector-nth execute ;
 
 : kernel-error? ( obj -- ? )
index b5629fd0bceb4e827afe010e42d25455058992b4..5014a752a0e43cf23be710f670c18c741582ee59 100644 (file)
@@ -27,6 +27,7 @@
 
 IN: init
 USE: combinators
+USE: compiler
 USE: errors
 USE: kernel
 USE: namespaces
index ddec07aa65bbd4f768e325130c80f4ff205efd3a..1f7c881beaadd9deb437fdce9b267125fc018716 100644 (file)
@@ -40,6 +40,7 @@ USE: stack
 USE: vectors
 USE: words
 USE: unparser
+USE: compiler
 
 [
     [ execute                | " word -- " ]
@@ -189,6 +190,12 @@ USE: unparser
     [ dump                   | " obj -- " ]
     [ cwd                    | " -- dir " ]
     [ cd                     | " dir -- " ]
+    [ set-compiled-byte      | " n ptr -- " ]
+    [ set-compiled-cell      | " n ptr -- " ]
+    [ compiled-offset        | " -- ptr " ]
+    [ set-compiled-offset    | " ptr -- " ]
+    [ literal-top            | " -- ptr " ]
+    [ set-literal-top        | " ptr -- " ]
 ] [
     unswons "stack-effect" swap set-word-property
 ] each
index f2a62c326a50387332adf324ee76512776cac9c1..c25f0a6f1db327e5db25192ddd5a98a21022ea7b 100644 (file)
@@ -58,7 +58,6 @@ USE: stack
     global [ "last-word" set ] bind ;
 
 : define-compound ( word def -- )
-    #! Define a compound word at runtime.
     over set-word-parameter
     1 swap set-word-primitive ;
 
index 9e724952eaab5cc8515d32b05ec3f3fba5fb3fdf..145ca7255ef6c9815185568ac04844437ea47bc6 100644 (file)
@@ -101,6 +101,3 @@ USE: stack
 : split-n ( n str -- list )
     #! Split a string into n-character chunks.
     [, 0 -rot (split-n) ,] ;
-
-: str-reverse ( str -- str )
-    str>sbuf dup sbuf-reverse sbuf>str ;
index 902fcd8afc326779bfc09709f169a68d173909d8..501d1750a16da3a45712a990e02577206e25f87c 100644 (file)
@@ -124,9 +124,6 @@ USE: stack
     dupd str-tail? dup [ nip t ] [ drop f ] ifte ;
 
 : split1 ( string split -- before after )
-    #! The car of the pair is the string up to the first
-    #! occurrence of split; the cdr is the remainder of
-    #! the string.
     2dup index-of dup -1 = [
         2drop f
     ] [
index f729f7c265fc7e4a63fba425393c51cc796ecca0..c1d2a6c1ddd7063bff1ae7f2472fabeb21875027 100644 (file)
@@ -27,6 +27,7 @@ CELL to_cell(CELL x)
                return s48_bignum_to_long(untag_bignum(x));
        default:
                type_error(INTEGER_TYPE,x);
+               return 0;
        }
 }
 
index 749756509513f1f684304b0a23b67c981d7ebe41..13b088802cbfb014a48b229b5a9b503565d15137 100644 (file)
@@ -3,21 +3,61 @@
 void init_compiler(void)
 {
        init_zone(&compiling,COMPILE_ZONE_SIZE);
+       literal_top = compiling.base;
 }
 
-void primitive_compile_byte(void)
+void check_compiled_offset(CELL offset)
 {
-       bput(compiling.here,to_fixnum(dpop()));
-       compiling.here++;
+       if(offset < compiling.base || offset >= compiling.limit)
+               range_error(F,offset,compiling.limit);
 }
 
-void primitive_compile_cell(void)
+void primitive_set_compiled_byte(void)
 {
-       put(compiling.here,to_cell(dpop()));
-       compiling.here += sizeof(CELL);
+       CELL offset = to_cell(dpop());
+       BYTE b = to_fixnum(dpop());
+       check_compiled_offset(offset);
+       bput(offset,b);
 }
 
-void primitive_compile_offset(void)
+void primitive_set_compiled_cell(void)
+{
+       CELL offset = to_cell(dpop());
+       CELL c = to_fixnum(dpop());
+       check_compiled_offset(offset);
+       put(offset,c);
+}
+
+void primitive_compiled_offset(void)
 {
        dpush(tag_integer(compiling.here));
 }
+
+void primitive_set_compiled_offset(void)
+{
+       CELL offset = to_cell(dpop());
+       check_compiled_offset(offset);
+       compiling.here = offset;
+}
+
+void primitive_literal_top(void)
+{
+       dpush(tag_integer(literal_top));
+}
+
+void primitive_set_literal_top(void)
+{
+       CELL offset = to_cell(dpop());
+       check_compiled_offset(offset);
+       literal_top = offset;
+}
+
+void collect_literals(void)
+{
+       CELL i = compiling.base;
+       while(i < literal_top)
+       {
+               copy_object((CELL*)i);
+               i += CELLS;
+       }
+}
index 58beb1bd9741a0a025cce8fcc73e38dbd4f825dd..539b53aa9688169c6f834060ad079b19c946a750 100644 (file)
@@ -1,6 +1,11 @@
 ZONE compiling;
+CELL literal_top;
 
 void init_compiler(void);
-void primitive_compile_byte(void);
-void primitive_compile_cell(void);
-void primitive_compile_offset(void);
+void primitive_set_compiled_byte(void);
+void primitive_set_compiled_cell(void);
+void primitive_compiled_offset(void);
+void primitive_set_compiled_offset(void);
+void primitive_literal_top(void);
+void primitive_set_literal_top(void);
+void collect_literals(void);
index f6f650751744f65aa10069a8d8177c41c9c11d37..f0a77ee833ed4b819b70c2f326f7ff9d9c2190e3 100644 (file)
@@ -50,6 +50,7 @@ void general_error(CELL error, CELL tagged)
                        fprintf(stderr,"Got type #%ld\n",type_of(
                                untag_cons(tagged)->cdr));
                }
+               fflush(stderr);
                exit(1);
        }
        throw_error(c);
index 71be1c72028bad081e1e74f98866649d6355f2ac..5718d1414b3f7ffa59e1076547e20654b0807334 100644 (file)
@@ -12,6 +12,7 @@
 #define ERROR_PROFILING_DISABLED (11<<3)
 #define ERROR_NEGATIVE_ARRAY_SIZE (12<<3)
 #define ERROR_BAD_PRIMITIVE (13<<3)
+#define ERROR_C_STRING (14<<3)
 
 void fatal_error(char* msg, CELL tagged);
 void critical_error(char* msg, CELL tagged);
index a7e5e13575ca8c9e969c94ee1c4d53d344ac84a6..7401d72b159c8188d894c9d33fd9720b8bc6bbb3 100644 (file)
@@ -143,6 +143,8 @@ void primitive_gc(void)
        scan = active.here = active.base;
        collect_roots();
        collect_io_tasks();
+       /* collect literal objects referenced from compiled code */
+       collect_literals();
        while(scan < active.here)
        {
                gc_debug("scan loop",scan);
index 7a585f13ace4215d8b37d9c2e7f04544fb4b3d97..f526352ed86fbc2671f555d21c6ef22ea2d052ba 100644 (file)
@@ -6,7 +6,7 @@ void load_image(char* filename)
        HEADER h;
        CELL size;
        
-       fprintf(stderr,"Loading %s...",filename);
+       printf("Loading %s...",filename);
 
        file = fopen(filename,"rb");
        if(file == NULL)
@@ -30,7 +30,7 @@ void load_image(char* filename)
        active.here = active.base + h.size;
        fclose(file);
 
-       fprintf(stderr," relocating...");
+       printf(" relocating...");
        fflush(stdout);
 
        clear_environment();
@@ -40,7 +40,8 @@ void load_image(char* filename)
 
        relocate(h.relocation_base);
 
-       fprintf(stderr," done\n");
+       printf(" done\n");
+       fflush(stdout);
 }
 
 bool save_image(char* filename)
index a73d28eea6d98b5c1c5af8f2865b9899f1ddde51..38da000eaec67a0bd314d7b9a96767b1630a8a0a 100644 (file)
@@ -111,3 +111,8 @@ void primitive_allot_profiling(void)
        }
 #endif
 }
+
+void primitive_address_of(void)
+{
+       dpush(tag_object(s48_ulong_to_bignum(dpop())));
+}
index 74a2ead4092c8baf24d6df082ec9f8503846d233..8af98fcd0fe7f758917e8a33cec698e7e3dd56fe 100644 (file)
@@ -69,3 +69,4 @@ bool in_zone(ZONE* z, CELL pointer);
 
 void primitive_room(void);
 void primitive_allot_profiling(void);
+void primitive_address_of(void);
index d41bce9b2daf5b0613199cba8ff4426a995ffc16..7effab30c655d7e5be2f8b87f364f186569349df 100644 (file)
@@ -26,6 +26,7 @@ XT primitives[] = {
        primitive_string_hashcode,
        primitive_index_of,
        primitive_substring,
+       primitive_string_reverse,
        primitive_sbufp,
        primitive_sbuf,
        primitive_sbuf_length,
@@ -153,9 +154,13 @@ XT primitives[] = {
        primitive_dump,
        primitive_cwd,
        primitive_cd,
-       primitive_compile_byte,
-       primitive_compile_cell,
-       primitive_compile_offset
+       primitive_set_compiled_byte,
+       primitive_set_compiled_cell,
+       primitive_compiled_offset,
+       primitive_set_compiled_offset,
+       primitive_literal_top,
+       primitive_set_literal_top,
+       primitive_address_of
 };
 
 CELL primitive_to_xt(CELL primitive)
index 0171be5678e33e2c4c196680e8274d6591d25e68..5543503c54bb6aa672993b4367ba5e7006985741 100644 (file)
@@ -1,4 +1,4 @@
 extern XT primitives[];
-#define PRIMITIVE_COUNT 155
+#define PRIMITIVE_COUNT 160
 
 CELL primitive_to_xt(CELL primitive);
index 3642124c38394bf2a11bc88bf81db6caf6425036..d21eca9c083df7d432d5b0e0beb284b2e71b7616 100644 (file)
@@ -101,32 +101,18 @@ void primitive_sbuf_append(void)
        }
 }
 
-STRING* sbuf_to_string(SBUF* sbuf)
-{
-       STRING* string = allot_string(sbuf->top);
-       memcpy(string + 1,sbuf->string + 1,sbuf->top * CHARS);
-       hash_string(string);
-       return string;
-}
-
 void primitive_sbuf_to_string(void)
 {
-       drepl(tag_object(sbuf_to_string(untag_sbuf(dpeek()))));
+       SBUF* sbuf = untag_sbuf(dpeek());
+       STRING* s = string_clone(sbuf->string,sbuf->top);
+       hash_string(s);
+       drepl(tag_object(s));
 }
 
 void primitive_sbuf_reverse(void)
 {
        SBUF* sbuf = untag_sbuf(dpop());
-       int i, j;
-       CHAR ch1, ch2;
-       for(i = 0; i < sbuf->top / 2; i++)
-       {
-               j = sbuf->top - i - 1;
-               ch1 = string_nth(sbuf->string,i);
-               ch2 = string_nth(sbuf->string,j);
-               set_string_nth(sbuf->string,j,ch1);
-               set_string_nth(sbuf->string,i,ch2);
-       }
+       string_reverse(sbuf->string,sbuf->top);
 }
 
 void primitive_sbuf_clone(void)
index 149cf0eec675912051b308ad0e684c0ea5684711..1da90fc4c82c27e70025c80d0ed475ed079a1874 100644 (file)
@@ -25,7 +25,6 @@ void set_sbuf_nth(SBUF* sbuf, CELL index, CHAR value);
 void primitive_set_sbuf_nth(void);
 void sbuf_append_string(SBUF* sbuf, STRING* string);
 void primitive_sbuf_append(void);
-STRING* sbuf_to_string(SBUF* sbuf);
 void primitive_sbuf_to_string(void);
 void primitive_sbuf_reverse(void);
 void primitive_sbuf_clone(void);
index c1164301045abaed1f42b43ae3110f8a78c13d0f..294a7ade583f4a3890da5e2d7eb4973462a91c1a 100644 (file)
@@ -80,7 +80,12 @@ BYTE* to_c_string(STRING* s)
        BYTE* c_str = (BYTE*)(_c_str + 1);
        
        for(i = 0; i < s->capacity; i++)
+       {
+               CHAR ch = string_nth(s,i);
+               if(ch == '\0' || ch > 255)
+                       general_error(ERROR_C_STRING,tag_object(s));
                c_str[i] = string_nth(s,i);
+       }
 
        c_str[s->capacity] = '\0';
 
@@ -259,3 +264,45 @@ void primitive_substring(void)
        CELL start = to_fixnum(dpop());
        dpush(tag_object(substring(start,end,string)));
 }
+
+/* DESTRUCTIVE - don't use with user-visible strings */
+void string_reverse(STRING* s, int len)
+{
+       int i, j;
+       CHAR ch1, ch2;
+       for(i = 0; i < len / 2; i++)
+       {
+               j = len - i - 1;
+               ch1 = string_nth(s,i);
+               ch2 = string_nth(s,j);
+               set_string_nth(s,j,ch1);
+               set_string_nth(s,i,ch2);
+       }
+}
+
+/* Doesn't rehash the string! */
+STRING* string_clone(STRING* s, int len)
+{
+       STRING* copy = allot_string(len);
+       memcpy(copy + 1,s + 1,len * CHARS);
+       return copy;
+}
+
+void primitive_string_reverse(void)
+{
+       STRING* s = untag_string(dpeek());
+       s = string_clone(s,s->capacity);
+       string_reverse(s,s->capacity);
+       hash_string(s);
+       drepl(tag_object(s));
+}
+
+STRING* fixup_untagged_string(STRING* str)
+{
+       return (STRING*)((CELL)str + (active.base - relocation_base));
+}
+
+STRING* copy_untagged_string(STRING* str)
+{
+       return copy_untagged_object(str,SSIZE(str));
+}
index 308cd69dfc0dd5aab37fe3c6d50185ea7a35813b..9e53c9e2d40e1578ffcbb771d742d8f824560c32 100644 (file)
@@ -46,13 +46,8 @@ void primitive_string_eq(void);
 void primitive_string_hashcode(void);
 void primitive_index_of(void);
 void primitive_substring(void);
-
-INLINE STRING* fixup_untagged_string(STRING* str)
-{
-       return (STRING*)((CELL)str + (active.base - relocation_base));
-}
-
-INLINE STRING* copy_untagged_string(STRING* str)
-{
-       return copy_untagged_object(str,SSIZE(str));
-}
+void string_reverse(STRING* s, int len);
+STRING* string_clone(STRING* s, int len);
+void primitive_string_reverse(void);
+STRING* fixup_untagged_string(STRING* str);
+STRING* copy_untagged_string(STRING* str);