Goto Chapter: Top 1 2 3 Ind
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 

2 Improving and extending the compiler
 2.1 Logic
 2.2 Enhanced syntax trees
 2.3 Iterating over a syntax tree
 2.4 Tools
 2.5 Compilation steps

2 Improving and extending the compiler

The easiest way to extend the compiler is by adding more logic to it, see 2.1. For writing logic functions you also have to iterate over enhanced syntax trees, see 2.2 and 2.3. You might also want to use available tools, see 2.4. If you want to improve an existing compilation step or add a completely new one, see 2.5.

For debugging you can:

2.1 Logic

Warning: When writing logic functions and templates keep in mind that wrapped arguments are outlined, see CapJitOutlinedWrappedArguments (2.5-19). This means that for example a logic template of the form CreateCapCategoryObjectWithAttributes( cat, attr, MyFunction( x ) ) will never match because MyFunction( x ) is outlined to a local variable.

2.1-1 CapJitAddLogicFunction
‣ CapJitAddLogicFunction( func )( function )

Adds the function func to the list of logic functions. For a list of pre-installed logic functions, which can be used as guiding examples, see CompilerForCAP/gap/Logic.gi. Technically, func should accept an (enhanced) syntax tree and return an (enhanced) syntax tree. Semantically, func should use some kind of "logic" to transform the tree. For example, func could look for calls of CallFuncList and replace them by calls to the actual function. Note: Often it is easier to use a logic template (see CapJitAddLogicTemplate (2.1-2)) than a logic function.

2.1-2 CapJitAddLogicTemplate
‣ CapJitAddLogicTemplate( template )( function )

Adds the logic template template to the list of logic templates. For a list of pre-installed logic templates, which can be used as guiding examples, see CompilerForCAP/gap/LogicTemplates.gi. Logic templates are records with the following entries:

Semantics: src_template is a piece of code which should be replaced by dst_template:

Notes:

2.2 Enhanced syntax trees

To simplify the handling of syntax trees, the CAP compiler enhances syntax trees in the following ways:

2.2-1 ENHANCED_SYNTAX_TREE
‣ ENHANCED_SYNTAX_TREE( func )( function )

Returns: a record

Returns an enhanced syntax tree of the plain function func (see above). If the option globalize_hvars is set to true, higher variables pointing to variables in the environment of func are assigned to global variables and referenced via these global variables in the tree. Otherwise, an error is thrown if such higher variables exist. If a list is given as the option given_arguments, references to the i-th argument of the function are replaced by references to a global variable with the value of given_arguments[i] (only in case this position is bound).

2.2-2 ENHANCED_SYNTAX_TREE_CODE
‣ ENHANCED_SYNTAX_TREE_CODE( tree )( function )

Returns: a function

Converts the enhanced syntax tree tree to a function.

2.3 Iterating over a syntax tree

2.3-1 CapJitIterateOverTree
‣ CapJitIterateOverTree( tree, pre_func, result_func, additional_arguments_func, additional_arguments )( function )

Returns: see description

Iterates recursively over a syntax tree and calls pre_func and result_func for each node. Overview:

Details:

Note: This function on its own does not modify the tree. However, you can make modifications in pre_func, result_func, and additional_arguments_func. If you do not want to make these modifcations in-place, you can replace a (sub-)tree by a modified version in pre_func and combine the modified (sub-)trees again using CapJitResultFuncCombineChildren (2.4-3) as result_func.

2.3-2 CapJitIterateOverTreeWithCachedBindingResults
‣ CapJitIterateOverTreeWithCachedBindingResults( tree, pre_func, result_func, additional_arguments_func, additional_arguments )( function )

Same input and output as CapJitIterateOverTree (2.3-1), but the results of bindings are cached and

WARNING: Calls to CapJitIterateOverTreeWithCachedBindingResults must not be nested if this results in a function being visited twice.

2.4 Tools

2.4-1 CapJitPrettyPrintSyntaxTree
‣ CapJitPrettyPrintSyntaxTree( tree )( function )

Displays an enhanced syntax tree in a more useful way. For example, prints the type of a node on top.

2.4-2 CapJitIsCallToGlobalFunction
‣ CapJitIsCallToGlobalFunction( tree, condition )( function )

Returns: a boolean

Checks if tree is an EXPR_FUNCCALL with funcref EXPR_GVAR such that gvar fulfills condition. If condition is a string, gvar must be equal to the string. Othwerwise, condition must be a function returning a boolean when applied to gvar.

2.4-3 CapJitResultFuncCombineChildren
‣ CapJitResultFuncCombineChildren( tree, result, additional_arguments )( function )

Returns: a list or a record

Can be used as a result_func for CapJitIterateOverTree (2.3-1). Replaces tree.(key) (resp. tree[key]) by result.(key) (resp. result[key]) for all keys of children of tree and returns the result. See CapJitIterateOverTree (2.3-1) for more details.

2.4-4 CapJitContainsRefToFVAROutsideOfFuncStack
‣ CapJitContainsRefToFVAROutsideOfFuncStack( tree, initial_func_id_stack )( function )

Returns: a boolean

Checks if tree (with function ID stack initial_func_id_stack) contains an FVAR which references a function outside of its function stack. Such a tree is not well-defined.

2.4-5 CapJitGetOrCreateGlobalVariable
‣ CapJitGetOrCreateGlobalVariable( value )( function )

Returns: a string

Assigns value to a global variable and returns the name of the global variable. If value has already been assigned to a global variable by this function before, simply returns the name of that global variable.

2.4-6 CapJitFindNodeDeep
‣ CapJitFindNodeDeep( tree, condition_func )( function )

Returns: a list or fail

Finds a node in tree for which condition_func returns true and returns the path of this node. For each node, condition_func is called with the node and current path as arguments, and must return a boolean. If multiple nodes are found, children are preferred over their parents (i.e. a "deep" node is returned). If no node can be found, fail is returned.

2.4-7 CapJitFindNodes
‣ CapJitFindNodes( tree, condition_func )( function )

Returns: a list

Finds all nodes in tree for which condition_func returns true. For each node, condition_func is called with the node and current path as arguments, and must return a boolean. Returns a list of paths of nodes for which this call yields true.

2.4-8 CapJitGetNodeByPath
‣ CapJitGetNodeByPath( tree, path )( function )

Returns: a record

Gets the node of tree with path path. Throws an error if no such node exists.

2.4-9 CapJitRemovedReturnFail
‣ CapJitRemovedReturnFail( tree )( function )

Returns: a record

Removes removes any statement of the form if condition then return fail; fi; (or similar) from the statements of a tree of type EXPR_FUNC. Throws an error if it cannot find such a statement.

2.4-10 CapJitPrettyPrintFunction
‣ CapJitPrettyPrintFunction( func )( function )

Returns: a string

Pretty prints the function func and returns the result. func must be a regular function, i.e. not an operation or a kernel function.

2.4-11 CapJitCopyWithNewFunctionIDs
‣ CapJitCopyWithNewFunctionIDs( tree )( function )

Returns: a record

Returns a structural copy of the enhanced syntax tree which is 1:1 except that all functions have new, unused IDs.

2.4-12 CapJitIsEqualForEnhancedSyntaxTrees
‣ CapJitIsEqualForEnhancedSyntaxTrees( tree1, tree2 )( function )

Returns: a boolean

Returns true if the enhanced syntax trees tree1 and tree2 are equal up to:

2.4-13 CapJitAddBinding
‣ CapJitAddBinding( bindings, name, value )( function )

Returns: a record

Adds a binding for name with value value to a syntax tree bindings of type FVAR_BINDING_SEQ.

2.4-14 CapJitValueOfBinding
‣ CapJitValueOfBinding( bindings, name )( function )

Returns: a record

Gets the value of the binding named name from a syntax tree bindings of type FVAR_BINDING_SEQ.

2.4-15 CapJitUnbindBinding
‣ CapJitUnbindBinding( bindings, name )( function )

Unbinds the the binding named name from a syntax tree bindings of type FVAR_BINDING_SEQ.

2.4-16 CapJitReplacedEXPR_REF_FVARByValue
‣ CapJitReplacedEXPR_REF_FVARByValue( tree, func_id, name, value )( function )

Returns: a record

Replaces all subtrees of the enhanced syntax tree tree which are of type EXPR_REF_FVAR with given func_id and name by value.

2.4-17 CapJitGetNextUnusedVariableID
‣ CapJitGetNextUnusedVariableID( func )( function )

Returns: an integer

Returns the minimal positive integer \(n\) such that no name of a local variable of the function func (given as an enhanced syntax tree) ends with Concatenation( "_", String( \(m\) ) ) for any \(m \geq n\).

2.4-18 CapJitDataTypeIsNestedListOfSimpleLiterals
‣ CapJitDataTypeIsNestedListOfSimpleLiterals( data_type )( function )

Returns whether the data type is a nested list of integers, booleans, strings, or characters. The nesting can be arbitrarily deep, including the degenerate case of no list at all.

2.4-19 PrintWithCurrentlyCompiledFunctionLocation
‣ PrintWithCurrentlyCompiledFunctionLocation( args... )( function )

Prints args... followed by the location of the currently compiled function.

2.4-20 DisplayWithCurrentlyCompiledFunctionLocation
‣ DisplayWithCurrentlyCompiledFunctionLocation( obj )( function )

Displays obj followed by the location of the currently compiled function.

2.4-21 ErrorWithCurrentlyCompiledFunctionLocation
‣ ErrorWithCurrentlyCompiledFunctionLocation( args... )( function )

Prints args... as an error followed by the location of the currently compiled function.

2.4-22 EvalStringStrict
‣ EvalStringStrict( string )( function )

Same as EvalString, but enters a break-loop when encountering syntax errors.

2.4-23 ConcatenationOfStringsAsEnumerationWithAnd
‣ ConcatenationOfStringsAsEnumerationWithAnd( list_of_strings )( function )

Concatenates [ "apples", "bananas", "oranges" ] as "apples, bananas, and oranges".

2.5 Compilation steps

2.5-1 CapJitCleanedUpHoistedAndDeduplicatedExpressions
‣ CapJitCleanedUpHoistedAndDeduplicatedExpressions( tree )( function )

Returns: a record

Cleans up variable names after hoisting and deduplication:

2.5-2 CapJitAppliedCompilerHints
‣ CapJitAppliedCompilerHints( tree, category )( function )

Returns: a record

Applies all compiler hints (see 1.6) to tree.

2.5-3 CapJitReplacedGlobalVariablesByCategoryAttributes
‣ CapJitReplacedGlobalVariablesByCategoryAttributes( tree, category )( function )

Returns: a record

Applies the compiler hint category_attribute_names (see 1.6) to tree. Assumes that category is the first argument of the function defined by tree.

2.5-4 CapJitReplacedSourceAndRangeAttributes
‣ CapJitReplacedSourceAndRangeAttributes( tree, category )( function )

Returns: a record

Applies the compiler hint source_and_range_attributes_from_morphism_attribute (see 1.6) to tree.

2.5-5 CapJitDeduplicatedExpressions
‣ CapJitDeduplicatedExpressions( tree )( function )

Returns: a record

Deduplicates expressions occuring at least twice in the enhanced syntax tree tree.

2.5-6 CapJitDroppedHandledEdgeCases
‣ CapJitDroppedHandledEdgeCases( tree )( function )

Returns: a record

Idea: If the same edge case is handled multiple times in the tree by checking a condition and returning a value, all condition checks except the first can be dropped. Details: Keeps a record of conditions which immediately lead to a return, i.e., statements of the form if condition1 then return value; fi;. If another statement of the form if condition2 then return value; fi; is found later in the tree and if condition2 = true implies condition1 = true, the second statement is dropped.

2.5-7 CapJitDroppedUnusedBindings
‣ CapJitDroppedUnusedBindings( tree )( function )

Returns: a record

Drops bindings (and names) of variables in functions in tree which are never referenced.

2.5-8 CapJitHoistedExpressions
‣ CapJitHoistedExpressions( tree )( function )

Returns: a record

Hoists expressions which are part of but indepedent of inner functions to outer functions. Assumes that there are no unused bindings (i.e., you should use CapJitDroppedUnusedBindings (2.5-7) first).

2.5-9 CapJitHoistedBindings
‣ CapJitHoistedBindings( tree )( function )

Returns: a record

Hoists bindings which are part of but indepedent of inner functions to outer functions. Assumes that there are no unused bindings (i.e., you should use CapJitDroppedUnusedBindings (2.5-7) first).

2.5-10 CapJitInferredDataTypes
‣ CapJitInferredDataTypes( tree )( function )

Returns: a record

Tries to infer the data types of expressions in the enhanced syntax tree tree of a function with data types. A data type is a record with component filter (a GAP filter) and, depending on the filter, additional components:

2.5-11 CapJitInlinedArguments
‣ CapJitInlinedArguments( tree )( function )

Returns: a record

Example: transforms (function(x) return x; end)(1) into (function() local x; x := 1; return x; end)(). Details: Searches for function calls of resolved functions. Assigns the argument values to local variables at the beginning of the function, and drops the arguments (i.e., makes the function a 0-ary function).

2.5-12 CapJitInlinedBindings
‣ CapJitInlinedBindings( tree )( function )

Returns: a record

Example: transforms function() local x; x := 1; return x^2; end into function() return 1^2; end(). Details: Replaces references to local variables of a function by the value of the corresponding binding of the function. If the option inline_var_refs_only is set to true, this is only done if the value is a reference to a (local or global) variable. If the option inline_fully is NOT set to true, wrapped arguments are not inlined (see CapJitOutlinedWrappedArguments (2.5-19)). Simple values like integers are always inlined. Also drops the inlined bindings.

2.5-13 CapJitInlinedBindingsToVariableReferences
‣ CapJitInlinedBindingsToVariableReferences( tree )( function )

Returns: a record

Short hand for CapJitInlinedBindings( tree : inline_var_refs_only := true ).

2.5-14 CapJitInlinedBindingsFully
‣ CapJitInlinedBindingsFully( tree )( function )

Returns: a record

Short hand for CapJitInlinedBindings( tree : inline_fully := true ).

2.5-15 CapJitInlinedFunctionCalls
‣ CapJitInlinedFunctionCalls( tree )( function )

Returns: a record

Example: transforms function() local x; x := (y -> y + 2)(1); return x; end into function() local x, y, r; y := 1; r := y + 2; x := r; return x; end. Details: Searches for function calls of a resolved function in the right hand side of a variable assignment or a return statement. Inserts the body of the function right before the variable assignment / return statement to avoid the function call. Assumes that arguments of function calls are inlined (i.e., you should use CapJitInlinedArguments (2.5-11) first). Due to the nature of a return statement breaking the execution and having no goto keyword in GAP, only functions

and not containing other return statements can be inlined.

2.5-16 CapJitInlinedSimpleFunctionCalls
‣ CapJitInlinedSimpleFunctionCalls( tree )( function )

Returns: a record

Replaces function calls of the form (function() return value; end)() by value. Assumes that arguments of function calls are inlined (i.e., you should use CapJitInlinedArguments (2.5-11) first).

2.5-17 CapJitAppliedLogic
‣ CapJitAppliedLogic( tree )( function )

Returns: a record

Applies all logic functions (see CapJitAddLogicFunction (2.1-1)) and logic templates (see CapJitAppliedLogicTemplates (2.5-18)) to tree.

2.5-18 CapJitAppliedLogicTemplates
‣ CapJitAppliedLogicTemplates( tree )( function )

Returns: a record

Applies all logic templates (see CapJitAddLogicTemplate (2.1-2)) to tree.

2.5-19 CapJitOutlinedWrappedArguments
‣ CapJitOutlinedWrappedArguments( tree )( function )

Returns: a record

Outlines wrapped arguments to local variables. This includes:

2.5-20 CapJitResolvedGlobalVariables
‣ CapJitResolvedGlobalVariables( tree )( function )

Returns: a record

Resolves global variables (except those which are listed in CAP_JIT_NON_RESOLVABLE_GLOBAL_VARIABLE_NAMES):

2.5-21 CapJitResolvedOperations
‣ CapJitResolvedOperations( tree )( function )

Returns: a record

Tries to resolve operations in tree:

 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 
Goto Chapter: Top 1 2 3 Ind

generated by GAPDoc2HTML