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

Simplify idiomatic for loop generation

Move the complexity from the fresh property into the printer and only ask xsmith about choices that matter.
parent a1ee0386
......@@ -158,10 +158,9 @@
;; 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
;; a value for use by children
[loop : Loop])
[ForLoop Expr ([initial : Literal]
[loopvar : GlobalSet]
[loopbody : Expr])
#:prop choice-weight 30]
)
......@@ -188,41 +187,11 @@
#;[GlobalSet (hash 'name (fresh-var-name "$global_"))]
;;todo Implement these with the upcoming custom name changes
;; 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 'LiteralIntThirtyTwo)
'name loopvar-name
'expr (make-fresh-node 'LiteralIntThirtyTwo)))] ;;dummy leaf node
[zero (make-fresh-node 'LiteralIntThirtyTwo
(hash 'v 0))]
[one (make-fresh-node 'LiteralIntThirtyTwo
(hash 'v 1))]
[loopvar-get1 (make-fresh-node 'GlobalGet
(hash 'name loopvar-name))]
[loopvar-get2 (make-fresh-node 'GlobalGet
(hash 'name loopvar-name))]
[subtraction (make-fresh-node 'Subtraction
(hash 'l loopvar-get1
'r one))]
[comparison (make-fresh-node 'GreaterThanSigned
(hash 'l loopvar-get2
'r zero))]
[branch (make-fresh-node 'BranchIf
(hash 'cond comparison
'targetindex 0 ;; Target the loop in order to loop
;; Specify the type manually to avoid cyclic dependenices
'targettype 'Loop))]
;; 'val is the (randomly generated) loop body
[loop-body (make-fresh-node 'GlobalSet
(hash 'val subtraction
'name loopvar-name
'expr branch))]
[loop (make-fresh-node 'Loop
(hash 'expr loop-body))])
(hash 'loopvar loopvar
'loop loop))]
;; Let the initial value and loop body be handled by the default
[ForLoop (hash 'loopvar (make-fresh-node 'GlobalSet
(hash 'val (make-fresh-node 'LiteralIntThirtyTwo) ;;dummy
'name (binding-name (send this xsmith_get-reference-for-child! i32 #t))
'expr (make-fresh-node 'LiteralIntThirtyTwo))))] ;;dummy
)
(add-att-rule
......@@ -234,8 +203,7 @@
(if (eq? c (ast-child 'cond n))
(+ 0 depth)
(+ 1 depth))))]
[Func (λ (n c) (+ 1 (att-value 'nesting-level (ast-parent n))))]
)
[Func (λ (n c) (+ 1 (att-value 'nesting-level (ast-parent n))))])
;; ints and floats
......@@ -290,8 +258,9 @@
'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))]]
[ForLoop [(fresh-type-variable) (λ (n t) (hash 'initial i32
'loopvar i32
'loopbody 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
......@@ -503,11 +472,22 @@
'(loop (result i32))
($xsmith_render-node (ast-child 'expr n))
'(end)))]
[ForLoop (λ (n)
(append
($xsmith_render-node (ast-child 'val (ast-child 'loopvar n)))
($xsmith_render-node (ast-child 'initial n))
`(global.set ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
`(loop (result i32))
`(global.get ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
'(i32.const 1)
'(i32.sub)
`(global.set ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
($xsmith_render-node (ast-child 'loop n))))]
;; loop body here
($xsmith_render-node (ast-child 'loopbody n))
`(global.get ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
'(i32.const 0)
'(i32.ge_s)
'(br_if 0)))]
[Branch (λ (n)
(append
($xsmith_render-node (ast-child 'val n))
......@@ -596,8 +576,8 @@
[GreaterThanUnsigned (λ (n) 'i32.gt_u)]
[LessThanOrEqualSigned (λ (n) 'i32.le_s)]
[LessThanOrEqualUnsigned (λ (n) 'i32.le_u)]
[GreaterThanOrEqualSigned (λ (n) 'i32.gt_s)]
[GreaterThanOrEqualUnsigned (λ (n) 'i32.gt_u)]
[GreaterThanOrEqualSigned (λ (n) 'i32.ge_s)]
[GreaterThanOrEqualUnsigned (λ (n) 'i32.ge_u)]
[CountLeadingZero (λ (n) 'i32.clz)]
[CountTrailingZero (λ (n) 'i32.ctz)]
[NonZeroBits (λ (n) 'i32.popcnt)]
......
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