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

Render the proper types instead of default i32

Adds two convenience methods: get-base-type-name and combine symbols

get-base-type-name attempts to fully concretize a type variable before getting its name

combine-symbols takes in two symbols and glues them together. This is useful for making instructions like 'i32.add', where the 'i32' could be any type
parent b1d474d9
......@@ -376,6 +376,21 @@
(define (choose-local-target n)
(random (ast-child 'localcount (get-func-node n))))
;; Convenience function for getting the name of the type
;; This function checks to see if a type can be concretized, then does so and
;; returns the symbol of the type
(define (get-base-type-name n)
(let ([type (att-value 'xsmith_type n)])
(if (concrete-type? type)
(base-type-name (concretize-type type))
(eprintf "Non-concrete type encountered in renderer"))))
;;Combines symbols: useful for printing things like 'i32.add'
(define (combine-symbols a b)
(let* ([a-string (symbol->string a)]
[b-string (symbol->string b)])
(string->symbol (format "~a~a" a-string b-string))))
(add-prop
wasm-like
......@@ -386,7 +401,7 @@
(import "env" "__memory_base" (global $mem_base i32))
,@(map (λ (global)
`(global ,(string->symbol (format "$~a" (ast-child 'name global)))
(mut i32)
(mut ,(get-base-type-name global))
,($xsmith_render-node (ast-child 'initialvalue global))))
(reverse (ast-children (ast-child 'globals n))))
,($xsmith_render-node (ast-child 'Func n))
......@@ -406,7 +421,7 @@
'(call $addToCrc)))
(reverse (ast-children (ast-child 'globals n))))))))]
[Func (λ (n) `(func (export "_func") (result i32)
(local ,@(make-list (ast-child 'localcount n) 'i32))
(local ,@(make-list (ast-child 'localcount n) 'i32)) ;;todo convert to new method
,@($xsmith_render-node (ast-child 'root n))))]
[LiteralIntThirtyTwo (λ (n) (list 'i32.const (ast-child 'v n)))]
[LiteralIntSixtyFour (λ (n) (list 'i64.const (ast-child 'v n)))]
......@@ -425,7 +440,7 @@
[IfElse (λ (n)
(append
($xsmith_render-node (ast-child 'cond n))
'(if (result i32))
`(if (result ,(get-base-type-name n)))
($xsmith_render-node (ast-child 'then n))
'(else)
($xsmith_render-node (ast-child 'else n))
......@@ -433,17 +448,17 @@
[If (λ (n)
(append
($xsmith_render-node (ast-child 'cond n))
'(if (result i32))
`(if (result ,(get-base-type-name n)))
($xsmith_render-node (ast-child 'then n))
'(end)))]
[Block (λ (n)
(append
'(block (result i32))
`(block (result ,(get-base-type-name n)))
($xsmith_render-node (ast-child 'expr n))
'(end)))]
[Loop (λ (n)
(append
'(loop (result i32))
`(loop (result ,(get-base-type-name n)))
($xsmith_render-node (ast-child 'expr n))
'(end)))]
......@@ -451,7 +466,7 @@
(append
($xsmith_render-node (ast-child 'initial n))
`(global.set ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
`(loop (result i32))
`(loop (result ,(get-base-type-name n)))
`(global.get ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
'(i32.const 1)
'(i32.sub)
......@@ -475,14 +490,20 @@
(append
($xsmith_render-node (ast-child 'address n))
($xsmith_render-node (ast-child 'value n))
`(i32.store ,(string->symbol (format "offset=~a" (ast-child 'offset n)))
,(string->symbol (format "align=~a" (expt 2 (ast-child 'alignment n)))))
`(,(combine-symbols
(get-base-type-name n)
'.store)
,(string->symbol (format "offset=~a" (ast-child 'offset n)))
,(string->symbol (format "align=~a" (expt 2 (ast-child 'alignment n)))))
($xsmith_render-node (ast-child 'expr n))))]
[MemLoad (λ (n)
(append
($xsmith_render-node (ast-child 'address n))
`(i32.load ,(string->symbol (format "offset=~a" (ast-child 'offset n)))
,(string->symbol (format "align=~a" (expt 2 (ast-child 'alignment n)))))))]
`(,(combine-symbols
(get-base-type-name n)
'.load)
,(string->symbol (format "offset=~a" (ast-child 'offset n)))
,(string->symbol (format "align=~a" (expt 2 (ast-child 'alignment n)))))))]
[LocalGet (λ (n)
(append
`(local.get ,(ast-child 'index n))))]
......@@ -535,6 +556,9 @@
'(FIXME)))]
)
;; Convenience function to get the type of the node
;; Checks that the type is a valid leaf node type, and converts
;; the result into a symbol for ease of use in the renderer
(add-prop
wasm-like
......@@ -545,35 +569,35 @@
(add-att-rule
wasm-like math-op
[Equal (λ (n) 'i32.eq)]
[NotEqual (λ (n) 'i32.ne)]
[EqualZero (λ (n) 'i32.eqz)]
[Addition (λ (n) 'i32.add)]
[Subtraction (λ (n) 'i32.sub)]
[Multiplication (λ (n) 'i32.mul)]
[DivisionSigned (λ (n) 'i32.div_s)]
[DivisionUnsigned (λ (n) 'i32.div_u)]
[LessThanSigned (λ (n) 'i32.lt_s)]
[LessThanUnsigned (λ (n) 'i32.lt_u)]
[GreaterThanSigned (λ (n) 'i32.gt_s)]
[GreaterThanUnsigned (λ (n) 'i32.gt_u)]
[LessThanOrEqualSigned (λ (n) 'i32.le_s)]
[LessThanOrEqualUnsigned (λ (n) 'i32.le_u)]
[GreaterThanOrEqualSigned (λ (n) 'i32.ge_s)]
[GreaterThanOrEqualUnsigned (λ (n) 'i32.ge_u)]
[CountLeadingZero (λ (n) 'i32.clz)]
[CountTrailingZero (λ (n) 'i32.ctz)]
[NonZeroBits (λ (n) 'i32.popcnt)]
[RemainderSigned (λ (n) 'i32.rem_s)]
[RemainderUnsigned (λ (n) 'i32.rem_u)]
[And (λ (n) 'i32.and)]
[Or (λ (n) 'i32.or)]
[Xor (λ (n) 'i32.xor)]
[ShiftLeft (λ (n) 'i32.shl)]
[ShiftRightSigned (λ (n) 'i32.shr_s)]
[ShiftRightUnsigned (λ (n) 'i32.shr_u)]
[RotateLeft (λ (n) 'i32.rotl)]
[RotateRight (λ (n) 'i32.rotr)]
[Equal (λ (n) (combine-symbols (get-base-type-name) '.eq))]
[NotEqual (λ (n) (combine-symbols (get-base-type-name n) '.ne))]
[EqualZero (λ (n) (combine-symbols (get-base-type-name n) '.eqz))]
[Addition (λ (n) (combine-symbols (get-base-type-name n) '.add))]
[Subtraction (λ (n) (combine-symbols (get-base-type-name n) '.sub))]
[Multiplication (λ (n) (combine-symbols (get-base-type-name n) '.mul))]
[DivisionSigned (λ (n) (combine-symbols (get-base-type-name n) '.div_s))]
[DivisionUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.div_u))]
[LessThanSigned (λ (n) (combine-symbols (get-base-type-name n) '.lt_s))]
[LessThanUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.lt_u))]
[GreaterThanSigned (λ (n) (combine-symbols (get-base-type-name n) '.gt_s))]
[GreaterThanUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.gt_u))]
[LessThanOrEqualSigned (λ (n) (combine-symbols (get-base-type-name n) '.le_s))]
[LessThanOrEqualUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.le_u))]
[GreaterThanOrEqualSigned (λ (n) (combine-symbols (get-base-type-name n) '.ge_s))]
[GreaterThanOrEqualUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.ge_u))]
[CountLeadingZero (λ (n) (combine-symbols (get-base-type-name n) '.clz))]
[CountTrailingZero (λ (n) (combine-symbols (get-base-type-name n) '.ctz))]
[NonZeroBits (λ (n) (combine-symbols (get-base-type-name n) '.popcnt))]
[RemainderSigned (λ (n) (combine-symbols (get-base-type-name n) '.rem_s))]
[RemainderUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.rem_u))]
[And (λ (n) (combine-symbols (get-base-type-name n) '.and))]
[Or (λ (n) (combine-symbols (get-base-type-name n) '.or))]
[Xor (λ (n) (combine-symbols (get-base-type-name n) '.xor))]
[ShiftLeft (λ (n) (combine-symbols (get-base-type-name n) '.shl))]
[ShiftRightSigned (λ (n) (combine-symbols (get-base-type-name n) '.shr_s))]
[ShiftRightUnsigned (λ (n) (combine-symbols (get-base-type-name n) '.shr_u))]
[RotateLeft (λ (n) (combine-symbols (get-base-type-name n) '.rotl))]
[RotateRight (λ (n) (combine-symbols (get-base-type-name n) '.rotr))]
)
......
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