! Copyright (C) 2004, 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. IN: compiler USING: inference kernel lists math namespaces words strings errors prettyprint kernel-internals ; ! The linear IR is close to assembly language. It also resembles ! Forth code in some sense. It exists so that pattern matching ! optimization can be performed against it. ! Linear IR nodes. This is in addition to the symbols already ! defined in inference vocab. SYMBOL: #push-immediate SYMBOL: #push-indirect SYMBOL: #replace-immediate SYMBOL: #replace-indirect SYMBOL: #jump-t ( branch if top of stack is true ) SYMBOL: #jump-t-label ( branch if top of stack is true ) SYMBOL: #jump-f ( branch if top of stack is false ) SYMBOL: #jump-f-label ( branch if top of stack is false ) SYMBOL: #jump ( tail-call ) SYMBOL: #jump-label ( tail-call ) SYMBOL: #return-to ( push addr on C stack ) ! dispatch is linearized as dispatch followed by a #target or ! #target-label for each dispatch table entry. The dispatch ! table terminates with #end-dispatch. The linearizer ensures ! the correct number of #targets is emitted. SYMBOL: #target ( part of jump table ) SYMBOL: #target-label SYMBOL: #end-dispatch : linear, ( node -- ) #! Add a node to the linear IR. [ node-op get node-param get ] bind cons , ; : >linear ( node -- ) #! Dataflow OPs have a linearizer word property. This #! quotation is executed to convert the node into linear #! form. "linearizer" [ linear, ] apply-dataflow ; : (linearize) ( dataflow -- ) [ >linear ] each ; : linearize ( dataflow -- linear ) #! Transform dataflow IR into linear IR. This strips out #! stack flow information, flattens conditionals into #! jumps and labels, and turns dataflow IR nodes into #! lists where the first element is an operation, and the #! rest is arguments. [ (linearize) ] make-list ; : immediate? ( obj -- ? ) #! fixnums and f have a pointerless representation, and #! are compiled immediately. Everything else can be moved #! by GC, and is indexed through a table. dup fixnum? swap f eq? or ; #push [ [ node-param get ] bind dup immediate? #push-immediate #push-indirect ? swons , ] "linearizer" set-word-property :