## The PolyML.Codetree structure

The CodeTree sub-structure contains functions that construct and operate on the intermediate code structure of the Poly/ML compiler. It is intended for compilers for languages other than Standard ML to target the back-end.

The intermediate code-tree is untyped and almost no checking is performed on it. It is very easy to cause the compiler or garbage-collector to crash and a failure could occur at some random point.

    structure CodeTree :
      sig

        type codeBinding
        type codetree
        type machineWord

        val mkConstant: machineWord -> codetree
        val mkLoadArgument: int -> codetree
        val mkLoadClosure: int -> codetree
        val mkLoadLocal: int -> codetree

        val mkEnv: codeBinding list * codetree -> codetree

        val mkFunction: codetree * int * string * codetree list * int -> codetree
        val mkInlineFunction: codetree * int * string * codetree list * int -> codetree
        val mkCall: codetree * codetree list -> codetree

        val mkTuple: codetree list -> codetree
        val mkInd: int * codetree -> codetree

        val mkIf: codetree * codetree * codetree -> codetree

        val mkBeginLoop: codetree * (int * codetree) list -> codetree
        val mkLoop: codetree list -> codetree
        val mkWhile: codetree * codetree -> codetree

        val mkRaise: codetree -> codetree
        val mkHandle: codetree * codetree -> codetree
        val Ldexc: codetree

        val mkDec: int * codetree -> codeBinding
        val mkMutualDecs: (int * codetree) list -> codeBinding
        val mkNullDec: codetree -> codeBinding

        val rtsFunction: int -> codetree

        val pretty: codetree -> pretty

        val genCode: codetree * int -> unit -> codetree
        val evalue: codetree -> machineWord option

        val encodeBinary: codetree -> Word8Vector.vector
        val decodeBinary: Word8Vector.vector -> codetree

        val unsafeMakeFunctor:
           string * NameSpace.signatureVal * NameSpace.signatureVal * codetree -> NameSpace.functorVal
      end

    val encodeBinary : codetree -> Word8Vector.vector

This encodes the code-tree as a byte vector. It is intended to allow compiled code to be exported and subsequently imported by decodeBinary. There are a number of restrictions on the code-tree to allow it to be exported, primarily that it is fully self-contained. It is really only suitable for use with the code of a functor.

    val decodeBinary : Word8Vector.vector -> codetree

This function imports a code-tree that has been encoded with encodeBinary.

    val unsafeMakeFunctor:
           string * NameSpace.signatureVal * NameSpace.signatureVal * codetree -> NameSpace.functorVal

This function can be used to create a functor from code and signature information.

    val genCode: codetree * int -> unit -> codetree

The genCode function compiles code and returns a function that, when called, will execute the compiled code. genCode(c, nBindings) takes the codetree c and an integer nBindings which is the number of binding addresses used in the top-level tree, or more specifically at least one more than the maximum binding address used. Binding addresses used within functions are not included in this; they are counted within their respective function. The result is a function of type unit -> codetree which when called executes the code. The result is a codetree. Currently this will always be a Constant node whose value can be extracted by evalue.

    val evalue : codetree -> machineWord option

The evalue function extracts the value from a Constant node. If the argument is not a Constant node the result will be NONE.

    val pretty: codetree -> pretty

This function formats the code-tree as a pretty data structure. It can then be printed using PolyML.prettyPrint or incorporated into another pretty structure.

    val ZZZ: int ref

This function does ZZZ

    val ZZZ: int ref

This function does ZZZ

    val ZZZ: int ref

This function does ZZZ