! Copyright (C) 2004, 2006 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays compiler-backend generic hashtables inference kernel math namespaces sequences words ; IN: compiler-frontend ! On PowerPC and AMD64, we use a stack discipline whereby ! stack frames are used to hold parameters. We need to compute ! the stack frame size to compile the prologue on entry to a ! word. 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 ; ! 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 { } 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 ) tail-call? [ %jump , f ] [ %call , iterate-next ] if ; : rename-label ( label -- label )