! Copyright (C) 2004, 2006 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays generic hashtables inference kernel math namespaces sequences words ; IN: compiler GENERIC: stack-reserve* M: object stack-reserve* drop 0 ; : stack-reserve ( node -- ) 0 swap [ stack-reserve* max ] each-node ; 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 end-basic-block ; ! 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 -- ) [ init-templates swap >r { } make r> linearized get set-hash ] with-node-iterator ; inline : linearize-1 ( word node -- ) swap [ dup stack-reserve %prologue , 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 ) end-basic-block tail-call? [ %jump , f ] [ %call , iterate-next ] if ; : rename-label ( label -- label )