module Analysis: sig
.. end
Create an optimized AST from the parsing phase AST
Pass 1:
- resolve all variable references, including closure variables
- process imports and add declarations to AST
- builds runtime AST
- convert template definitions / instructions
- evaluate operations on constants and replace with value in AST
- determine if variables are initialized with a constant value (rather than an expression),
if a variable is written after being declared and if it is ever read after being declared
- determine if a function is inlineable
Pass 2:
The second pass replaces all non function variables whose value have not been modified
with a constant value, and evaluates operations on constants , eliminates assignment
statements on constant values when the variable is not reassigned and not written,
inlines functions
Author(s): Tony BenBrahim < tony.benbrahim at gmail.com >
val check_errors : Environment.analysis_env -> unit
Prints all errors in an analysis environment and raises FatalExit if there are errors
Raises FatalExit
if there are errors in the environment
Returns unit
env
: analysis environment
val check_warnings : Environment.analysis_env -> Environment.analysis_env
Generates additional warnings about unused variables, then prints all warnings
Returns analysis environment with newly added warnings
env
: analysis environment
val print_name_info : Environment.analysis_env -> unit
Prints information about names found during analysis
Returns unit
env
: analysis environment
FIRST PASS
- resolve all variable references, including closure variables
- process imports and add declarations to AST
- builds runtime AST
- convert template definitions / instructions
- evaluate operations on constants and replace with value in AST
- determine if variables are initialized with a constant value (rather than an expression)
- determine if a variable is written after being declared and if it is ever read after being declared
- determine if a function is inlineable
exception TemplateError of string
internal exception to signal an error in template processing.
val check_template_nesting : Ast.template_spec list ->
(string, Environment.label_pos) Hashtbl.t * (string * int) list
Checks for invalid nesting in a template specification
Returns an list of tuples containing the label and line offset where conflicts where found
*
template_spec
: the template spec to check
val generate_template_instr_function : string * string list * Ast.replacement_spec list * (string * int) ->
Environment.analysis_env -> Ast.runtime_statement * Environment.analysis_env
Generate a set of statements corresponding to a template instruction
Returns a runtime statement for the instruction defining a function
instruction
: instruction AST
env
: runtime environment
val filter_imported_ast : Ast.statement list -> Ast.statement list
Filters an ast, returning only a list of declaration and import statement
Returns a statement list containing only declarations and imports
stmts
: the statement list to process
val analyze_variables : Environment.analysis_env ->
Ast.statement -> Ast.runtime_statement * Environment.analysis_env
find declarations and resolve references to variables. Since declarations are
visible in the entire scope in which they are defined, and not just after they are
declared, a breadth first search is necessary before recursively processing children
statements
Returns an ast where all variables have been resolved to an absolute location, either
on a stack or in the global heap and an environment containing information about all
variables
env
: an analysis environment
ast
: the intermediate ast
SECOND PASS
- replace all constant declarations with Noop
- replace all constant variables with their value
- replace all constant expressions with the computed value
- replace all calls to inlineable functions with an expression
val inline_expr_replace : int ->
int ->
Ast.runtime_expression list ->
Ast.runtime_expression -> Ast.runtime_expression
replaces an expression from an inlined function with the corresponding
values from a function call expression list
Returns the inline expression with the arguments replacing the former local args
depth
: the stack depth, for sanity checking
numargs
: the number of arguments
expr
: the inline expression
val replace_constant : Environment.analysis_env ->
int list -> Ast.runtime_expression -> Ast.runtime_expression
Replace non modified variables with their declared value
Returns an expression with constant variables replaced by their value
env
: analysis environment
inline_uids
: list of inlined functions to avoid recursively inlining recursive inlinable functions
val pass2 : Environment.analysis_env -> Ast.runtime_statement -> Ast.runtime_statement
Looks for expressions where constants can be substituted
env
: analysis environment
val analyze : Ast.statement -> Ast.runtime_statement * Environment.analysis_env
Analyzes an AST, generates a runtime AST
Returns a tuple of the runtime AST and analysis environment
ast
: a parsing AST