Commit a5fa7e15 authored by Guy Watson's avatar Guy Watson
Browse files

Add explicit types for i32, i64, f32, and f64

parent 02e2ce2d
......@@ -125,7 +125,7 @@ Type conversions
[ ] f32.convert_i64_u
// The following apply to only their own type
[ ] i32.wrap_i64
[ ] i64.extern_i32_s
[ ] i64.extend_i32_s
[ ] i64.extend_i32_u
[ ] f32.demote_f64
[ ] f64.promote_f32
......
......@@ -50,8 +50,14 @@
[localcount = (random 1 10)])]
[Expr #f ()
#:prop may-be-generated #f]
[LiteralInt Expr ([v = (random 10000)])
#:prop choice-weight 3]
[LiteralIntThirtyTwo Expr ([v = (random -1000 1000)]) ;;todo add command line feature for the whole range
#:prop choice-weight 3]
[LiteralIntSixtyFour Expr ([v = (random -1000 1000)])
#:prop choice-weight 3]
[LiteralFloatThirtyTwo Expr ([v = (+ (random -1000 1000) (random))])
#:prop choice-weight 3]
[LiteralFloatSixtyFour Expr ([v = (+ (random -1000 1000) (random))])
#:prop choice-weight 3]
[Noop Expr ([expr : Expr])]
[Binop Expr ([l : Expr] [r : Expr]) ;; use l and r here
#:prop choice-weight 30 ;; default is 10
......@@ -106,17 +112,17 @@
[targetnode]
[targetindex])
#:prop choice-weight 10]
[MemStore Expr ([address : LiteralInt]
[MemStore Expr ([address : LiteralIntThirtyTwo]
[value : Expr]
[offset = (abs (- (random 20000) 10000))] ;; This probably has some bias towards 0
[alignment = (random 4)]
[expr : Expr])
#:prop choice-weight 3]
[MemLoad Expr ([address : LiteralInt]
[MemLoad Expr ([address : LiteralIntThirtyTwo]
[offset = (abs (- (random 20000) 10000))]
[alignment = (random 4)])
#:prop choice-weight 3]
[LocalGet Expr ([index])] ;; Change these over to the xsmith reference system once random reference increases are implemented by William
[LocalGet Expr ([index])] ;;todo Change these over to the xsmith reference system once random reference increases are implemented by William
[LocalSet Expr ([val : Expr]
[index]
[expr : Expr])]
......@@ -124,7 +130,7 @@
[index])]
[GlobalDeclaration #f ([name]
[type]
[initialvalue : LiteralInt])
[initialvalue : LiteralIntThirtyTwo])
#:prop binder-info (name type definition)]
[GlobalGet Expr ([name])
#:prop reference-info (read name)
......@@ -135,6 +141,19 @@
#:prop reference-info (write name #:unifies val)
#:prop choice-weight 4]
;; Type conversions
[TypeConversion Expr ([expr : Expr])
#:prop choice-weight 20
#:prop may-be-generated #f]
[Truncate TypeConversion ([sign = (< (random) 0.5)])] ;; float -> int
[Convert TypeConversion ([sign = (< (random) 0.5)])] ;; int -> float
[Wrap TypeConversion ()] ;; i64 -> i32
[Extend TypeConversion ([sign = (< (random) 0.5)])] ;; i32 -> i64
[Demote TypeConversion ()] ;; f64 -> f32
[Promote TypeConversion ()] ;; f32 -> f64
[Reinterpret TypeConversion ()] ;; float -> int, or int -> float, but only with the same bit-width
;; Idiomatic Generation
[ForLoop Expr ([loopvar : GlobalSet] ;; This will be the inital value set before entering the loop
;; Rendering for this node is restricted: it will not produce
......@@ -170,12 +189,12 @@
;; The problem here is that I can't seem to make-fresh-node inside of fresh
[ForLoop (let* ([loopvar-name (binding-name (send this xsmith_get-reference-for-child! int #t))]
[loopvar (make-fresh-node 'GlobalSet
(hash 'val (make-fresh-node 'LiteralInt)
(hash 'val (make-fresh-node 'LiteralIntThirtyTwo)
'name loopvar-name
'expr (make-fresh-node 'LiteralInt)))] ;;dummy leaf node
[zero (make-fresh-node 'LiteralInt
'expr (make-fresh-node 'LiteralIntThirtyTwo)))] ;;dummy leaf node
[zero (make-fresh-node 'LiteralIntThirtyTwo
(hash 'v 0))]
[one (make-fresh-node 'LiteralInt
[one (make-fresh-node 'LiteralIntThirtyTwo
(hash 'v 1))]
[loopvar-get1 (make-fresh-node 'GlobalGet
(hash 'name loopvar-name))]
......@@ -215,52 +234,103 @@
[Func (λ (n c) (+ 1 (att-value 'nesting-level (ast-parent n))))]
)
;; ints and floats
;; Use these with (fresh-subtype-of ___)
;; For 32 and 64 bit types, use (fresh-type-variable i32 f32)
(define int (base-type 'int))
(define float (base-type 'float))
;; All the base types of WebAssembly, with supertypes of int and float
(define i32 (base-type 'i32 int))
(define i64 (base-type 'i64 int))
(define f32 (base-type 'f32 float))
(define f64 (base-type 'f64 float))
(define (no-child-types)
(λ (n t) (hash)))
(add-prop
wasm-like type-info
[Program [(fresh-type-variable)
[Program [i32
(λ (n t)
(hash 'Func (fresh-type-variable)
'globals (λ (cn) (fresh-type-variable))))]]
[GlobalDeclaration [int (λ (n t) (hash 'initialvalue int))]]
(hash 'Func i32
'globals (fresh-type-variable)))]]
;;todo I want the globals to possibly have different types
;;'globals (λ (child-node) (fresh-type-variable))))]]
[GlobalDeclaration [(fresh-type-variable)
(λ (n t) (hash 'initialvalue t))]]
[Func [(fresh-type-variable)
(λ (n t)
(hash 'root (fresh-type-variable)))]]
[LiteralInt [int (no-child-types)]]
[Noop [int (λ (n t) (hash 'expr int))]]
[Binop [(fresh-type-variable int #|float|#)
(hash 'root t))]]
[LiteralIntThirtyTwo [i32
(no-child-types)]]
[LiteralIntSixtyFour [i64
(no-child-types)]]
[LiteralFloatThirtyTwo [f32
(no-child-types)]]
[LiteralFloatSixtyFour [f64
(no-child-types)]]
[Noop [(fresh-type-variable) (λ (n t) (hash 'expr t))]] ;;todo This is wierd....
[Binop [(fresh-type-variable)
(λ (n t)
(hash 'l t
'r t))]]
[Unop [(fresh-type-variable int)
[Unop [(fresh-type-variable)
(λ (n t)
(hash 'expr t))]]
[IfElse [int
[IfElse [(fresh-type-variable)
(λ (n t)
(hash 'cond int
'then int
'else int))]]
[If [int
(hash 'cond i32
'then t
'else t))]]
[If [(fresh-type-variable)
(λ (n t)
(hash 'cond int
'then int))]]
[Block [int (λ (n t) (hash 'expr int))]]
[Loop [int (λ (n t) (hash 'expr int))]]
[ForLoop [int (λ (n t) (hash 'loopvar int
'loop int))]]
[Branch [int (λ (n t) (hash 'val int))]]
[BranchIf [int (λ (n t) (hash 'cond int
'val int))]]
[MemStore [int (λ (n t) (hash 'address int
'value int
'expr int))]]
[MemLoad [int (λ (n t) (hash 'address int))]]
[LocalGet [int (no-child-types)]]
[LocalSet [int (λ (n t) (hash 'val int
'expr int))]]
[LocalTee [int (λ (n t) (hash 'val int))]]
[GlobalGet [int (no-child-types)]]
[GlobalSet [int (λ (n t) (hash 'val int
'expr int))]]
(hash 'cond i32
'then t))]]
[Block [(fresh-type-variable) (λ (n t) (hash 'expr t))]]
[Loop [(fresh-type-variable) (λ (n t) (hash 'expr t))]]
[ForLoop [(fresh-type-variable) (λ (n t) (hash 'loopvar i32
'loop t))]]
[Branch [(fresh-type-variable) (λ (n t) (hash 'val t))]] ;;todo triple check branch interactions here
[BranchIf [(fresh-type-variable)
(λ (n t) (hash 'cond i32
'val t))]]
[MemStore [(fresh-type-variable)
(λ (n t) (hash 'address i32
'value t
'expr t))]]
[MemLoad [(fresh-type-variable) (λ (n t) (hash 'address i32))]]
[LocalGet [i32 (no-child-types)]]
[LocalSet [i32 (λ (n t) (hash 'val i32
'expr i32))]]
[LocalTee [i32 (λ (n t) (hash 'val i32))]]
[GlobalGet [(fresh-type-variable) (no-child-types)]]
[GlobalSet [(fresh-type-variable) (λ (n t) (hash 'val t
'expr t))]]
;;Type conversions
[Truncate [(fresh-subtype-of int)
(λ (n t)
(hash 'expr (fresh-subtype-of float)))]]
[Convert [(fresh-subtype-of float)
(λ (n t)
(hash 'expr (fresh-subtype-of int)))]]
[Wrap [i32 (λ (n t)
(hash 'expr i64))]]
[Extend [i64 (λ (n t)
(hash 'expr i32))]]
[Demote [f32 (λ (n t)
(hash 'expr f64))]]
[Promote [f64 (λ (n t)
(hash 'expr f32))]]
[Reinterpret [(fresh-type-variable i32 i64 f32 f64) ;;constrain to concrete types
(λ (n t) ;; int to float or float to int, but only in the same bitwidth
(begin
(printf (format "node: ~a, type: ~a" n t))
(case t
[(i32) (hash 'expr f32)]
[(f32) (hash 'expr i32)]
[(i64) (hash 'expr f64)]
[(f64) (hash 'expr i64)])))]]
)
;; Define structured control instruction property
......@@ -277,9 +347,7 @@
[IfElse (λ (n) #t)]
[If (λ (n) #t)]
[Block (λ (n) #t)]
[Loop (λ (n) #t)]
;;[ForLoop (λ (n) #t)]
)
[Loop (λ (n) #t)])
(define (get-nesting-types node)
(let ([type (node-type node)])
......@@ -337,13 +405,6 @@
(random (ast-child 'localcount (get-func-node n))))
;; This creates a base type, like a leaf of a grammar
(define int (base-type 'int))
(define (no-child-types)
(λ (n t) (hash)))
(add-prop
wasm-like
render-node-info
......@@ -375,7 +436,10 @@
[Func (λ (n) `(func (export "_func") (result i32)
(local ,@(make-list (ast-child 'localcount n) 'i32))
,@($xsmith_render-node (ast-child 'root n))))]
[LiteralInt (λ (n) (list 'i32.const (ast-child 'v n)))]
[LiteralIntThirtyTwo (λ (n) (list 'FIXMEEEE (ast-child 'v n)))] ;; todo fix
[LiteralIntSixtyFour (λ (n) (list 'FIXMEEEE (ast-child 'v n)))] ;; todo fix
[LiteralFloatThirtyTwo (λ (n) (list 'FIXMEEEE (ast-child 'v n)))] ;; todo fix
[LiteralFloatSixtyFour (λ (n) (list 'FIXMEEEE (ast-child 'v n)))] ;; todo fix
[Noop (λ (n) (append
'(nop)
($xsmith_render-node (ast-child 'expr n))))]
......@@ -456,13 +520,36 @@
($xsmith_render-node (ast-child 'val n))
`(global.set ,(string->symbol (format "$~a" (ast-child 'name n))))
($xsmith_render-node (ast-child 'expr n))))]
[Truncate (λ (n)
(append
'(FIXME)))]
[Convert (λ (n)
(append
'(FIXME)))]
[Wrap (λ (n)
(append
'(FIXME)))]
[Extend (λ (n)
(append
'(FIXME)))]
[Demote (λ (n)
(append
'(FIXME)))]
[Promote (λ (n)
(append
'(FIXME)))]
[Reinterpret (λ (n)
(append
'(FIXME)))]
)
(add-prop
wasm-like
render-hole-info
[#f (λ (hole) `(hole!!! <,(ast-node-type hole)>))])
[#f (λ (n)
(append
`(hole!!! <,(ast-node-type n)>)))])
(add-att-rule
wasm-like math-op
......@@ -502,7 +589,7 @@
(assemble-spec-components webassembly wasm-like)
(xsmith-command-line (λ () (parameterize ([current-xsmith-type-constructor-thunks
(list (λ () int))])
(list (λ () i32))])
(webassembly-generate-ast 'Program)))
#:comment-wrap (λ (lines)
(string-join
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment