Commit 17bf60bf authored by Guy Watson's avatar Guy Watson
Browse files

Add idiomatic for loop generation

parent 11d8cf7c
#lang racket/base
#lang xsmith/private/base
;; -*- mode: Racket -*-
;;
;; Copyright (c) 2019 The University of Utah
......@@ -138,19 +138,25 @@
[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]) ;; this iter is controlled by 'fresh', and will produce an
;; expression for the result of the loop
#:prop choice-weight 10000]
[loop : Loop])
#:prop choice-weight 30]
)
(add-prop
wasm-like
fresh
[Branch (match-let ([(cons index node) (choose-br-target (current-hole))])
(hash 'targetindex index 'targetnode node))]
[BranchIf (match-let ([(cons index node) (choose-br-target (current-hole))])
(hash 'targetindex index 'targetnode node))]
[Branch (if (ast-has-parent? current-hole) ;; Only check for targets if we are attached to the tree
;; For example: generating a branch instruction for the
;; for loop below while not connected to the tree. We
;; must manually specify the target
(match-let ([(cons index node) (choose-br-target (current-hole))])
(hash 'targetindex index 'targettype (node-type node)))
(hash))]
[BranchIf (if (ast-has-parent? current-hole)
(match-let ([(cons index node) (choose-br-target (current-hole))])
(hash 'targetindex index 'targettype (node-type node)))
(hash))]
[LocalGet (hash 'index (choose-local-target (current-hole)))]
[LocalSet (hash 'index (choose-local-target (current-hole)))]
[LocalTee (hash 'index (choose-local-target (current-hole)))]
......@@ -161,13 +167,11 @@
;; The problem here is that I can't seem to make-fresh-node inside of fresh
[ForLoop (let* ([_ (printf "Started fresh for loop\n")]
[loopvar-name (binding-name (send this xsmith_get-reference-for-child! int #t))]
[proto-loopvar (make-fresh-node 'GlobalSet
(hash 'val (make-fresh-node 'LiteralInt)
'name loopvar-name
'expr (make-fresh-node 'LiteralInt)))] ;;dummy leaf node
[_ (printf (format "Chose loopvar name: ~a\n" loopvar-name))]
[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)
'name loopvar-name
'expr (make-fresh-node 'LiteralInt)))] ;;dummy leaf node
[zero (make-fresh-node 'LiteralInt
(hash 'v 0))]
[one (make-fresh-node 'LiteralInt
......@@ -184,16 +188,18 @@
'r zero))]
[branch (make-fresh-node 'BranchIf
(hash 'cond comparison
'targetindex 0))] ;; Target the loop in order to loop
;; 'val is the loop body
[proto-loop (make-fresh-node 'GlobalSet
(hash 'val subtraction
'name loopvar-name
'expr branch))]
[_ (printf "Finished fresh for loop\n")])
(hash 'loopvar proto-loopvar
'loop (make-fresh-node 'Loop
(hash 'expr proto-loop))))]
'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 'e loop-body))])
(hash 'loopvar loopvar
'loop loop))]
)
(add-att-rule
......@@ -239,7 +245,7 @@
[Block [int (λ (n t) (hash 'e int))]]
[Loop [int (λ (n t) (hash 'e int))]]
[ForLoop [int (λ (n t) (hash 'loopvar int
'e int))]]
'loop int))]]
[Branch [int (λ (n t) (hash 'val int))]]
[BranchIf [int (λ (n t) (hash 'cond int
'val int))]]
......@@ -271,7 +277,7 @@
[If (λ (n) #t)]
[Block (λ (n) #t)]
[Loop (λ (n) #t)]
[ForLoop (λ (n) #t)]
;;[ForLoop (λ (n) #t)]
)
(define (get-nesting-types node)
......@@ -405,12 +411,9 @@
'(end)))]
[ForLoop (λ (n)
(append
(render-node (ast-child 'val (ast-child 'loopvar n)))
`(global.set ,(string->symbol (format "$~a" (ast-child 'name (ast-child 'loopvar n)))))
'(loop (result i32))
(render-node (ast-child 'iter n))
'(end)))]
(render-node (ast-child 'loop n))))]
[Branch (λ (n)
(append
(render-node (ast-child 'val n))
......
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