
Removed counter field from prelex, using the operand field instead to provide the index into the fxmap. This follows other uses within the compiler where we use the operand field as a little place for state that is used within a single pass. This has a few advantages. First, it keeps the record a little smaller. Second, it means that the prelex numbering can start from 0 for each compilation unit, which should help keep the numbers for the fxmap a bit smaller in longer running sessions with multiple calls to the compiler. Finally, it avoids adding to the burden of the tc-mutex, since within the pass it is safe for us to set the prelexes, since only the instance of the pass holding this block of code has a handle on it. As part of this change prelex-counter is now defined in cptypes and the operand is cleared after the variables go out of scope. base-lang.ss Fixed the highest-set-bit function in fxmap so that it will work in the 32-bit versions of Chez Scheme. The fxsrl by 32 raises an exception, and was leading to tests to fail in 32-bit mode. fxmap.ss Restructured predicate-implies? so that it uses committed choice instead of uncommitted choice in comparing x and y. Basically, this means, instead of doing: (or (and (predicate-1? x) (predicate-1? y) ---) (and (predicate-2? x) (predicate-2? y) ---) ...) we now do: (cond [(predicate-1? x) (and (predicate-1? y) ---)] [(predicate-2? x) (and (predicate-2? y) ---)] ...) This avoids running predicates on x that we know will fail because an earlier predicate matches, generally getting out of the predicate faster. This did require a little restructuring, because in some cases x was dominant and in other cases y was dominant. This is now restructured with y dominate, after the eq? and x 'bottom check. Replaced let-values calls with cata-morphism syntax, including removal of maps that built up a list of values that then needed to be separated out with (map car ...) (map cadr ...) etc. calls. This avoid building up structures we don't need, since the nanopass framework will generate a mutltivalued let for these situations. The if clause in cptypes/raw now uses types1 (the result of the recursive call on e1) in place of the incoming types clause when processing the e2 or e3 expressions in the cases where e1 is known statically to produce either a false or non-false value. Fixed a bug with directly-applied variable arity lambda. The original code marked all directly-applied variable arity lambda's as producing bottom, because it was chacking for the interface to be equal to the number of arguments. However, variable arity functions are represented with a negative number. For instance, the original code would transform the expression: (begin ((lambda (a . b) (set! t (cons* b a t))) 'a 'b 'c) t) to ((lambda (a . b) (set! t (cons* b a t))) 'a 'b 'c) anticipating that the call would raise an error, however, it is a perfectly valid (if some what unusual) expression. I tried to come up with a test for this, however, without building something fairly complicated, it is difficult to get past cp0 without cp0 turning it into something like: (let ([b (list 'b 'c)]) (set! t (cons* b 'a t)) t) Fixed make-time, time-second-set!, and time-second to indicate that second can be an exact-integer, since it is not really restricted to the fixnum range (and if fact we even test this fact in the mats on 32-bit machines). primdata.ss Changed check of prelex-was-assigned (which is not reliably on the input to any give pass) with prelex-assigned, which should always have an accurate, if conservative, value in it. Added enable-type-recovery parameter to allow the type recover to be turned on and off, and added cptype to the cp0 not run path that runs cpletrec, so that cptypes can be run independent of cp0. This is helpful for testing and allows us to benefit from type recovery, even in cases where we do not want cp0 to perform any inlining. compile.ss, front.ss, primdata.ss Stylistic changes, mostly for consistency with other parts of the compiler, though I'm not married to these changes if you'd really prefer to keep things the way the are. 1. clauses of define-record type now use parenthesis instead of square brackets. 2. indented by 2 spaces where things were only indented by one space 3. define, let, define-pass, nanopass pass productions clauses, now use parenthesis for outer markers instead of square brackets. fxmap.ss, original commit: 5c6c5a534ff708d4bff23f6fd48fe6726a5c4e05
261 lines
9.1 KiB
Scheme
261 lines
9.1 KiB
Scheme
;;; base-lang.ss
|
|
;;; Copyright 1984-2017 Cisco Systems, Inc.
|
|
;;;
|
|
;;; Licensed under the Apache License, Version 2.0 (the "License");
|
|
;;; you may not use this file except in compliance with the License.
|
|
;;; You may obtain a copy of the License at
|
|
;;;
|
|
;;; http://www.apache.org/licenses/LICENSE-2.0
|
|
;;;
|
|
;;; Unless required by applicable law or agreed to in writing, software
|
|
;;; distributed under the License is distributed on an "AS IS" BASIS,
|
|
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
;;; See the License for the specific language governing permissions and
|
|
;;; limitations under the License.
|
|
|
|
(module (Lsrc Lsrc? Ltype Ltype? unparse-Ltype unparse-Lsrc count-Lsrc
|
|
lookup-primref primref? primref-name primref-level primref-flags primref-arity primref-signatures
|
|
sorry! make-preinfo preinfo? preinfo-lambda? preinfo-sexpr preinfo-sexpr-set! preinfo-src
|
|
make-preinfo-lambda preinfo-lambda-name preinfo-lambda-name-set! preinfo-lambda-flags preinfo-lambda-libspec
|
|
prelex? make-prelex prelex-name prelex-name-set! prelex-flags prelex-flags-set!
|
|
prelex-source prelex-operand prelex-operand-set! prelex-uname make-prelex*
|
|
target-fixnum? target-bignum?)
|
|
|
|
(module (lookup-primref primref? primref-name primref-flags primref-arity primref-signatures primref-level)
|
|
(include "primref.ss")
|
|
|
|
(define $lookup-primref
|
|
(lambda (level name)
|
|
(unless (symbol? name)
|
|
(sorry! 'lookup-primref "invalid primitive name ~s" name))
|
|
(or ($sgetprop name
|
|
(case level
|
|
[(2) '*prim2*]
|
|
[(3) '*prim3*]
|
|
[else ($oops 'lookup-primref "invalid level ~s" level)])
|
|
#f)
|
|
($oops 'lookup-primref "unrecognized prim ~s" name))))
|
|
|
|
(define-syntax lookup-primref
|
|
(lambda (x)
|
|
(define exact-integer?
|
|
(lambda (x)
|
|
(and (integer? x) (exact? x))))
|
|
(define constant-level&name
|
|
(lambda (level name)
|
|
(unless (and (exact-integer? level) (memv level '(2 3)))
|
|
(syntax-error x (format "invalid level ~s" level)))
|
|
(unless (symbol? name)
|
|
(syntax-error x (format "invalid name ~s" name)))
|
|
(let ([primref ($sgetprop name (if (eqv? level 2) '*prim2* '*prim3*) #f)])
|
|
(unless primref (syntax-error x (format "unknown primitive ~s" name)))
|
|
#`'#,primref)))
|
|
(define constant-name
|
|
(lambda (?level name)
|
|
(unless (symbol? name)
|
|
(syntax-error x (format "invalid name ~s" name)))
|
|
(let ([primref2 ($sgetprop name '*prim2* #f)]
|
|
[primref3 ($sgetprop name '*prim3* #f)])
|
|
(unless (and primref2 primref3)
|
|
(syntax-error x (format "unknown primitive ~s" name)))
|
|
#`(let ([level #,?level])
|
|
(case level
|
|
[(2) '#,primref2]
|
|
[(3) '#,primref3]
|
|
[else (sorry! 'lookup-primref "invalid level ~s" level)])))))
|
|
(syntax-case x (quote)
|
|
[(_ (quote level) (quote name))
|
|
(constant-level&name (datum level) (datum name))]
|
|
[(_ level (quote name))
|
|
(exact-integer? (datum level))
|
|
(constant-level&name (datum level) (datum name))]
|
|
[(_ ?level (quote name))
|
|
(constant-name #'?level (datum name))]
|
|
[(k ?level ?name) #'($lookup-primref ?level ?name)]))))
|
|
|
|
(module (prelex? make-prelex
|
|
prelex-name prelex-name-set!
|
|
prelex-flags prelex-flags-set!
|
|
prelex-source
|
|
prelex-operand prelex-operand-set!
|
|
prelex-uname)
|
|
(define-record-type prelex
|
|
(nongenerative #{prelex grpmhtzqa9bflxfggfu6pp-2})
|
|
(sealed #t)
|
|
(fields (mutable name) (mutable flags) source (mutable operand) (mutable $uname))
|
|
(protocol
|
|
(lambda (new)
|
|
(lambda (name flags source operand)
|
|
(new name flags source operand #f)))))
|
|
(define prelex-uname
|
|
(lambda (id)
|
|
(or (prelex-$uname id)
|
|
(let ([uname (gensym (symbol->string (prelex-name id)))])
|
|
(with-tc-mutex
|
|
(or (prelex-$uname id)
|
|
(begin (prelex-$uname-set! id uname) uname)))))))
|
|
(record-writer (record-type-descriptor prelex)
|
|
(lambda (x p wr)
|
|
(fprintf p "~s" (prelex-name x)))))
|
|
|
|
(define make-prelex*
|
|
(case-lambda
|
|
[() (make-prelex (gensym) 0 #f #f)]
|
|
[(name) (make-prelex name 0 #f #f)]))
|
|
|
|
; TODO: use sorry! where appropriate
|
|
(define sorry!
|
|
(lambda (who str . arg*)
|
|
($oops 'compiler-internal "~@[~a: ~]~?" who str arg*)))
|
|
|
|
(define maybe-source-object?
|
|
(lambda (x)
|
|
(or (eq? x #f) (source-object? x))))
|
|
|
|
(define rcd?
|
|
(lambda (x)
|
|
(or (record-constructor-descriptor? x) #t))) ; rcd should be retricted to rcd or ctrcd
|
|
|
|
(define exact-integer?
|
|
(lambda (x)
|
|
(and (integer? x) (exact? x))))
|
|
|
|
(meta-cond
|
|
[(= (constant fixnum-bits) (fixnum-width))
|
|
(define target-fixnum? fixnum?)
|
|
(define target-bignum? bignum?)]
|
|
[(< (constant fixnum-bits) (fixnum-width))
|
|
(define target-fixnum?
|
|
(lambda (x)
|
|
(and (fixnum? x)
|
|
(fx<= (constant most-negative-fixnum) x (constant most-positive-fixnum)))))
|
|
(define target-bignum?
|
|
(lambda (x)
|
|
(or (bignum? x)
|
|
(and (fixnum? x)
|
|
(not (fx<= (constant most-negative-fixnum) x (constant most-positive-fixnum)))))))]
|
|
[else
|
|
(define target-fixnum?
|
|
(lambda (x)
|
|
(or (fixnum? x)
|
|
(and (bignum? x)
|
|
(<= (constant most-negative-fixnum) x (constant most-positive-fixnum))))))
|
|
(define target-bignum?
|
|
(lambda (x)
|
|
(and (bignum? x)
|
|
(not (<= (constant most-negative-fixnum) x (constant most-positive-fixnum))))))])
|
|
|
|
(define $prelex?
|
|
(lambda (x)
|
|
(prelex? x)))
|
|
|
|
(define datum?
|
|
(lambda (x)
|
|
#t))
|
|
|
|
(define convention?
|
|
(lambda (x)
|
|
(or (eq? x #f) (symbol? x))))
|
|
|
|
(define-record-type preinfo
|
|
(nongenerative #{preinfo e23pkvo5btgapnzomqgegm-2})
|
|
(fields src (mutable sexpr))
|
|
(protocol
|
|
(lambda (new)
|
|
(case-lambda
|
|
[() (new #f #f)]
|
|
[(src) (new src #f)]
|
|
[(src sexpr) (new src sexpr)]))))
|
|
|
|
(define-record-type preinfo-lambda
|
|
(nongenerative #{preinfo-lambda e23pkvo5btgapnzomqgegm-4})
|
|
(parent preinfo)
|
|
(sealed #t)
|
|
(fields libspec (mutable name) flags)
|
|
(protocol
|
|
(lambda (pargs->new)
|
|
(case-lambda
|
|
[() ((pargs->new) #f #f 0)]
|
|
[(src) ((pargs->new src) #f #f 0)]
|
|
[(src sexpr) ((pargs->new src sexpr) #f #f 0)]
|
|
[(src sexpr libspec) ((pargs->new src sexpr) libspec #f 0)]
|
|
[(src sexpr libspec name) ((pargs->new src sexpr) libspec name 0)]
|
|
[(src sexpr libspec name flags) ((pargs->new src sexpr) libspec name flags)]))))
|
|
|
|
; language of foreign types
|
|
(define-language Ltype
|
|
(nongenerative-id #{Ltype czp82kxwe75y4e18-1})
|
|
(terminals
|
|
(exact-integer (bits))
|
|
($ftd (ftd)))
|
|
(Type (t)
|
|
(fp-integer bits)
|
|
(fp-unsigned bits)
|
|
(fp-void)
|
|
(fp-scheme-object)
|
|
(fp-u8*)
|
|
(fp-u16*)
|
|
(fp-u32*)
|
|
(fp-fixnum)
|
|
(fp-double-float)
|
|
(fp-single-float)
|
|
(fp-ftd ftd)
|
|
(fp-ftd& ftd)))
|
|
|
|
(define arity?
|
|
(lambda (x)
|
|
(or (eq? x #f)
|
|
(for-all fixnum? x))))
|
|
|
|
(define maybe-string? (lambda (x) (or (eq? x #f) (string? x))))
|
|
|
|
; source language used by the passes leading up to the compiler or interpreter
|
|
(define-language Lsrc
|
|
(nongenerative-id #{Lsrc czsa1fcfzdeh493n-2})
|
|
(terminals
|
|
(preinfo (preinfo))
|
|
($prelex (x))
|
|
(datum (d))
|
|
(record-type-descriptor (rtd))
|
|
(rcd (rcd))
|
|
(source-object (src))
|
|
(maybe-source-object (maybe-src))
|
|
(Ltype (arg-type result-type)) => unparse-Ltype
|
|
(fixnum (interface index flags level))
|
|
(arity (arity))
|
|
(box (box))
|
|
(convention (conv))
|
|
(maybe-string (name))
|
|
(symbol (sym type))
|
|
(primref (pr)))
|
|
(Expr (e body rtd-expr)
|
|
pr
|
|
(moi)
|
|
(ref maybe-src x) => x
|
|
(quote d)
|
|
(if e0 e1 e2)
|
|
(seq e0 e1)
|
|
(set! maybe-src x e) => (set! x e)
|
|
(pariah)
|
|
(case-lambda preinfo cl ...) => (case-lambda cl ...)
|
|
(letrec ([x* e*] ...) body)
|
|
(letrec* ([x* e*] ...) body)
|
|
(call preinfo e0 e1 ...) => (e0 e1 ...)
|
|
(record-type rtd e)
|
|
(record-cd rcd rtd-expr e)
|
|
(immutable-list (e* ...) e)
|
|
(record rtd rtd-expr e* ...)
|
|
(record-ref rtd type index e)
|
|
(record-set! rtd type index e1 e2)
|
|
(cte-optimization-loc box e)
|
|
(foreign conv name e (arg-type* ...) result-type)
|
|
(fcallable conv e (arg-type* ...) result-type)
|
|
(profile src) => (profile)
|
|
; used only in cpvalid
|
|
(cpvalid-defer e))
|
|
(CaseLambdaClause (cl)
|
|
(clause (x* ...) interface body) => [(x* ...) interface body]))
|
|
|
|
(define-language-node-counter count-Lsrc Lsrc)
|
|
)
|