doing the typechecking on consts up front, to avoid the weirdness in dealing with Any later on.
This commit is contained in:
parent
12dfe2caa0
commit
ce48679f73
38
compiler/compiler-helper.rkt
Normal file
38
compiler/compiler-helper.rkt
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(provide ensure-const-value)
|
||||||
|
|
||||||
|
(define (ensure-const-value x)
|
||||||
|
(cond
|
||||||
|
[(symbol? x)
|
||||||
|
x]
|
||||||
|
[(boolean? x)
|
||||||
|
x]
|
||||||
|
[(string? x)
|
||||||
|
x]
|
||||||
|
[(number? x)
|
||||||
|
x]
|
||||||
|
[(void? x)
|
||||||
|
x]
|
||||||
|
[(null? x)
|
||||||
|
x]
|
||||||
|
[(char? x)
|
||||||
|
x]
|
||||||
|
[(bytes? x)
|
||||||
|
x]
|
||||||
|
[(path? x)
|
||||||
|
x]
|
||||||
|
[(pair? x)
|
||||||
|
(begin (ensure-const-value (car x))
|
||||||
|
(ensure-const-value (cdr x))
|
||||||
|
x)]
|
||||||
|
[(vector? x)
|
||||||
|
(begin (for-each ensure-const-value (vector->list x)))
|
||||||
|
x]
|
||||||
|
[(box? x)
|
||||||
|
(ensure-const-value (unbox x))
|
||||||
|
x]
|
||||||
|
[else
|
||||||
|
(error 'ensure-const-value "Not a const value: ~s\n" x)]))
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,13 @@
|
||||||
racket/bool
|
racket/bool
|
||||||
racket/list
|
racket/list
|
||||||
racket/match)
|
racket/match)
|
||||||
|
|
||||||
(require/typed "../logger.rkt"
|
(require/typed "../logger.rkt"
|
||||||
[log-debug (String -> Void)])
|
[log-debug (String -> Void)])
|
||||||
|
|
||||||
|
(require/typed "compiler-helper.rkt"
|
||||||
|
[ensure-const-value (Any -> const-value)])
|
||||||
|
|
||||||
(provide (rename-out [-compile compile])
|
(provide (rename-out [-compile compile])
|
||||||
compile-general-procedure-call
|
compile-general-procedure-call
|
||||||
append-instruction-sequences)
|
append-instruction-sequences)
|
||||||
|
@ -450,7 +454,8 @@
|
||||||
(end-with-linkage linkage
|
(end-with-linkage linkage
|
||||||
cenv
|
cenv
|
||||||
(append-instruction-sequences
|
(append-instruction-sequences
|
||||||
(make-AssignImmediateStatement target (make-Const (Constant-v exp)))
|
(make-AssignImmediateStatement target (make-Const
|
||||||
|
(ensure-const-value (Constant-v exp))))
|
||||||
singular-context-check))))
|
singular-context-check))))
|
||||||
|
|
||||||
|
|
||||||
|
@ -1208,7 +1213,7 @@
|
||||||
(map (lambda: ([e : Expression])
|
(map (lambda: ([e : Expression])
|
||||||
(cond
|
(cond
|
||||||
[(Constant? e)
|
[(Constant? e)
|
||||||
(make-Const (Constant-v e))]
|
(make-Const (ensure-const-value (Constant-v e)))]
|
||||||
[(LocalRef? e)
|
[(LocalRef? e)
|
||||||
(make-EnvLexicalReference (LocalRef-depth e)
|
(make-EnvLexicalReference (LocalRef-depth e)
|
||||||
(LocalRef-unbox? e))]
|
(LocalRef-unbox? e))]
|
||||||
|
@ -1655,7 +1660,7 @@
|
||||||
'?]))]
|
'?]))]
|
||||||
|
|
||||||
[(Constant? exp)
|
[(Constant? exp)
|
||||||
(make-Const (Constant-v exp))]
|
(make-Const (ensure-const-value (Constant-v exp)))]
|
||||||
|
|
||||||
[(PrimitiveKernelValue? exp)
|
[(PrimitiveKernelValue? exp)
|
||||||
exp]
|
exp]
|
||||||
|
|
|
@ -64,12 +64,27 @@
|
||||||
#:transparent)
|
#:transparent)
|
||||||
|
|
||||||
|
|
||||||
|
(define-type const-value
|
||||||
|
(Rec C
|
||||||
|
(U Symbol
|
||||||
|
String
|
||||||
|
Number
|
||||||
|
Boolean
|
||||||
|
Void
|
||||||
|
Null
|
||||||
|
Char
|
||||||
|
Bytes
|
||||||
|
Path
|
||||||
|
(Pairof C C)
|
||||||
|
(Vectorof C)
|
||||||
|
(Boxof C))))
|
||||||
|
|
||||||
|
|
||||||
(define-struct: Label ([name : Symbol])
|
(define-struct: Label ([name : Symbol])
|
||||||
#:transparent)
|
#:transparent)
|
||||||
(define-struct: Reg ([name : AtomicRegisterSymbol])
|
(define-struct: Reg ([name : AtomicRegisterSymbol])
|
||||||
#:transparent)
|
#:transparent)
|
||||||
(define-struct: Const ([const : Any])
|
(define-struct: Const ([const : const-value])
|
||||||
#:transparent)
|
#:transparent)
|
||||||
|
|
||||||
;; Limited arithmetic on OpArgs
|
;; Limited arithmetic on OpArgs
|
||||||
|
|
|
@ -109,7 +109,7 @@
|
||||||
;; fixme: use js->string
|
;; fixme: use js->string
|
||||||
(: assemble-const (Const -> String))
|
(: assemble-const (Const -> String))
|
||||||
(define (assemble-const stmt)
|
(define (assemble-const stmt)
|
||||||
(let: loop : String ([val : Any (Const-const stmt)])
|
(let: loop : String ([val : const-value (Const-const stmt)])
|
||||||
(cond [(symbol? val)
|
(cond [(symbol? val)
|
||||||
(format "RUNTIME.makeSymbol(~s)" (symbol->string val))]
|
(format "RUNTIME.makeSymbol(~s)" (symbol->string val))]
|
||||||
[(pair? val)
|
[(pair? val)
|
||||||
|
@ -136,13 +136,15 @@
|
||||||
[(path? val)
|
[(path? val)
|
||||||
(format "RUNTIME.makePath(~s)"
|
(format "RUNTIME.makePath(~s)"
|
||||||
(path->string val))]
|
(path->string val))]
|
||||||
#;[(vector? val)
|
[(vector? val)
|
||||||
(format "RUNTIME.makeVector(~s)"
|
(format "RUNTIME.makeVector(~s)"
|
||||||
(string-join (for/list ([elt (vector->list val)])
|
(string-join (for/list ([elt (vector->list val)])
|
||||||
(loop elt))
|
(loop elt))
|
||||||
","))]
|
","))]
|
||||||
[else
|
[(box? val)
|
||||||
(error 'assemble-const "Unsupported datum ~s" val)])))
|
(format "RUNTIME.makeBox(~s)"
|
||||||
|
(loop (unbox val)))])))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(: assemble-listof-assembled-values ((Listof String) -> String))
|
(: assemble-listof-assembled-values ((Listof String) -> String))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user