! Copyright (C) 2004, 2006 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays compiler-backend hashtables inference kernel namespaces sequences words ; IN: compiler-frontend SYMBOL: node-stack : >node node-stack get push ; : node> node-stack get pop ; : node@ node-stack get peek ; DEFER: iterate-nodes : iterate-children ( quot -- ) node@ node-children [ swap iterate-nodes ] each ; : iterate-next ( -- node ) node@ node-successor ; : iterate-nodes ( node quot -- ) over [ [ swap >node call node> drop ] keep over [ iterate-nodes ] [ 2drop ] if ] [ 2drop ] if ; inline : with-node-iterator ( quot -- ) [ V{ } clone node-stack set call ] with-scope ; inline DEFER: #terminal? PREDICATE: #merge #terminal-merge node-successor #terminal? ; UNION: #terminal POSTPONE: f #return #values #terminal-merge ; : tail-call? ( -- ? ) node-stack get [ node-successor ] map [ #terminal? ] all? ; GENERIC: linearize* ( node -- next ) : linearize-child ( node -- ) [ node@ linearize* ] iterate-nodes ; ! A map from words to linear IR. SYMBOL: linearized ! Renamed labels. To avoid problems with labels with the same ! name in different scopes. SYMBOL: renamed-labels : make-linear ( word quot -- ) [ swap >r [ %prologue , call ] { } make r> linearized get set-hash ] with-node-iterator ; inline : linearize-1 ( word node -- ) swap [ linearize-child ] make-linear ; : init-linearizer ( -- ) H{ } clone linearized set H{ } clone renamed-labels set ; : linearize ( word dataflow -- linearized ) #! Outputs a hashtable mapping from labels to their #! respective linear IR. init-linearizer linearize-1 linearized get ; M: node linearize* ( node -- next ) drop iterate-next ; : linearize-call ( label -- next ) tail-call? [ %jump , f ] [ %call , iterate-next ] if ; : rename-label ( label -- label )