public final class CodeGenerator extends Object
Responsible for compiling the declarations, statements and expression found in a WhileyFile into WyIL declarations and bytecode blocks. For example:
type nat is (int x) where x >= 0
function f(nat x) -> int:
return x-1
The code generator is responsible for generating the code for the constraint
on nat, as well as compiling the function's statements into
their corresponding WyIL bytecodes. For example, the code generated
constraint on type nat would look like this:
type nat is int
where:
load x
const 0
ifge goto exit
fail("type constraint not satisfied")
.exit:
This WyIL bytecode simply compares the local variable x against 0. Here, x
represents the value held in a variable of type nat. If the
constraint fails, then the given message is printed.| Constructor and Description |
|---|
CodeGenerator(CompileTask builder)
Construct a code generator object for translating WhileyFiles into
WyilFiles.
|
| Modifier and Type | Method and Description |
|---|---|
static String |
freshLabel() |
WyilFile |
generate(WhileyFile whileyFile,
wyfs.lang.Path.Entry<WyilFile> target)
Generate a WyilFile from a given WhileyFile by translating all of the
declarations, statements and expressions into WyIL declarations and
bytecode blocks.
|
wyc.builder.CodeGenerator.FlowResult |
generateAndCondition(Expr.BinOp condition,
wyc.builder.CodeGenerator.EnclosingScope scope)
Translate a source-level conjunction into a sequence of WyIL bytecodes.
|
int |
generateAsStmt(Expr.FunctionOrMethodCall expr,
wyc.builder.CodeGenerator.EnclosingScope scope)
Generate an invoke expression as a statement.
|
int |
generateAsStmt(Expr.IndirectFunctionOrMethodCall expr,
wyc.builder.CodeGenerator.EnclosingScope scope)
Generate an indirect invoke expression as a statement.
|
wyc.builder.CodeGenerator.FlowResult |
generateCondition(Expr condition,
wyc.builder.CodeGenerator.EnclosingScope scope)
Translate a source-level conditional expression into WyIL bytecodes,
using a given scope mapping named variables to locations.
|
int |
generateExpression(Expr expression,
wyc.builder.CodeGenerator.EnclosingScope scope)
Translate a source-level expression into a WYIL bytecode block, using a
given environment mapping named variables to registers.
|
int |
generateFunctionOrMethodCall(Expr.FunctionOrMethodCall expr,
wyc.builder.CodeGenerator.EnclosingScope scope) |
int |
generateIndirectFunctionOrMethodCall(Expr.IndirectFunctionOrMethodCall expr,
wyc.builder.CodeGenerator.EnclosingScope scope) |
wyc.builder.CodeGenerator.FlowResult |
generateIsCondition(Expr.BinOp condition,
wyc.builder.CodeGenerator.EnclosingScope scope)
Translate a source-level type test.
|
int[] |
generateMultipleReturns(List<Expr> expressions,
wyc.builder.CodeGenerator.EnclosingScope scope)
Translate a source-level expression into a WyIL bytecode block, using a
given environment mapping named variables to registers.
|
wyc.builder.CodeGenerator.FlowResult |
generateNotCondition(Expr.UnOp condition,
wyc.builder.CodeGenerator.EnclosingScope scope) |
wyc.builder.CodeGenerator.FlowResult |
generateOrCondition(Expr.BinOp condition,
wyc.builder.CodeGenerator.EnclosingScope scope)
Translate a source-level disjunction into a sequence of WyIL bytecodes.
|
List<Integer> |
toIntegerList(int... items) |
public CodeGenerator(CompileTask builder)
builder - The enclosing builder instance which provides access to the
global namespace.public WyilFile generate(WhileyFile whileyFile, wyfs.lang.Path.Entry<WyilFile> target)
whileyFile - The WhileyFile to be translated.public int generateAsStmt(Expr.FunctionOrMethodCall expr, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
expr - The expression to be translated as a statementscope - The enclosing scope of the expressionwybs.util.ResolveErrorpublic int generateAsStmt(Expr.IndirectFunctionOrMethodCall expr, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
expr - The expression to be translated as a statementscope - The enclosing scope of the expressionwybs.util.ResolveErrorpublic wyc.builder.CodeGenerator.FlowResult generateCondition(Expr condition, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
condition - scope - wybs.util.ResolveErrorpublic wyc.builder.CodeGenerator.FlowResult generateAndCondition(Expr.BinOp condition, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
x is int && x >= 0Here, the true scope coming out of the lhs will identify
x with type int. This is necessary for the rhs
to make sense. Observe that this is exploiting the fact that operators
have short circuiting behaviour in Whiley.condition - Condition being translatedscope - Enclosing scope going into this condition.wybs.util.ResolveErrorpublic wyc.builder.CodeGenerator.FlowResult generateOrCondition(Expr.BinOp condition, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
x is null || x >= 0Here, assume x is declared with type
int|null. Then, the
false scope coming out of the lhs will identify
x with type int. This is necessary for the rhs
to make sense. Observe that this is exploiting the fact that operators
have short circuiting behaviour in Whiley.condition - Condition being translatedscope - Enclosing scope going into this condition.wybs.util.ResolveErrorpublic wyc.builder.CodeGenerator.FlowResult generateIsCondition(Expr.BinOp condition, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
x is intAssum
x is declared with type int|null. Then on
the true branch x has type int&(int|null) which
reduces to int. And, on the false branch, x has
type !int&(int|null) which reduces to null.condition - scope - wybs.util.ResolveErrorpublic wyc.builder.CodeGenerator.FlowResult generateNotCondition(Expr.UnOp condition, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
wybs.util.ResolveErrorpublic int[] generateMultipleReturns(List<Expr> expressions, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
expression - scope - wybs.util.ResolveErrorpublic int generateExpression(Expr expression, wyc.builder.CodeGenerator.EnclosingScope scope)
expression - --- Source-level expression to be translatedscope - --- Enclosing scope of the conditionpublic int generateFunctionOrMethodCall(Expr.FunctionOrMethodCall expr, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
wybs.util.ResolveErrorpublic int generateIndirectFunctionOrMethodCall(Expr.IndirectFunctionOrMethodCall expr, wyc.builder.CodeGenerator.EnclosingScope scope) throws wybs.util.ResolveError
wybs.util.ResolveErrorpublic static String freshLabel()
Copyright © 2017. All rights reserved.