|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.farrago.fennel.calc.CalcProgramBuilder
public class CalcProgramBuilder
Constructs a calculator assembly language program based upon a series of calls made by the client.
If you want multi-line programs, call
setSeparator(System.getProperty("line.separator")
.
See tracer
.
Nested Class Summary | |
---|---|
(package private) static class |
CalcProgramBuilder.BoolInstructionDef
|
(package private) static class |
CalcProgramBuilder.BoolNativeInstructionDef
|
(package private) static class |
CalcProgramBuilder.ComparisonInstructionDef
|
static class |
CalcProgramBuilder.ExtInstrDef
Defines an extended instruction. |
static class |
CalcProgramBuilder.ExtInstrSizeDef
Defines an extended instruction with name depending on the number or operands. |
static class |
CalcProgramBuilder.FunctionCall
Holds and represents the parameters for a call to an operator |
(package private) class |
CalcProgramBuilder.Instruction
Represents an instruction and its operands |
(package private) static class |
CalcProgramBuilder.InstructionDef
Definition for an instruction. |
(package private) static class |
CalcProgramBuilder.IntegralNativeInstructionDef
|
(package private) static class |
CalcProgramBuilder.IntegralNativeShift
|
(package private) static class |
CalcProgramBuilder.JumpInstructionDef
|
static class |
CalcProgramBuilder.Line
Reference to a line number |
private static class |
CalcProgramBuilder.LiteralPair
A key-value pair class to hold 1) Value of a literal and 2) Type of a literal For use in a hashtable |
(package private) static class |
CalcProgramBuilder.NativeInstructionDef
|
(package private) static interface |
CalcProgramBuilder.Operand
Represents an Operand in an opereation e.g. |
static class |
CalcProgramBuilder.OpType
Enumeration of the types supported by the calculator. |
(package private) static class |
CalcProgramBuilder.PointerBoolInstructionDef
|
static class |
CalcProgramBuilder.RegisterDescriptor
|
protected class |
CalcProgramBuilder.RegisterSets
A place holder to hold defined register sets |
static class |
CalcProgramBuilder.RegisterSetType
Enumeration of register types |
Constructor Summary | |
---|---|
CalcProgramBuilder()
Creates a CalcProgramBuilder. |
Method Summary | |
---|---|
protected void |
addComment(String comment)
Adds an comment string to the last instruction. |
protected void |
addInstruction(CalcProgramBuilder.InstructionDef instrDef,
CalcProgramBuilder.Operand... operands)
For internal use only. |
protected void |
addJumpBooleanWithCondition(CalcProgramBuilder.JumpInstructionDef op,
int line,
CalcReg reg)
|
void |
addJumpFalse(int line,
CalcReg reg)
|
void |
addJumpNotNull(int line,
CalcReg reg)
|
void |
addJumpNull(int line,
CalcReg reg)
|
void |
addJumpTrue(int line,
CalcReg reg)
Adds an condtional JMP instruction. |
void |
addLabel(String label)
|
void |
addLabelJump(String label)
|
void |
addLabelJumpFalse(String label,
CalcReg reg)
|
void |
addLabelJumpNotNull(String label,
CalcReg reg)
|
void |
addLabelJumpNull(String label,
CalcReg reg)
|
void |
addLabelJumpTrue(String label,
CalcReg reg)
|
void |
addRef(CalcReg outputRegister,
CalcReg src)
Adds a REF instruction. |
void |
addReturn()
|
protected void |
assertIsNativeType(CalcReg reg)
Asserts input is of native type except booleans |
protected void |
assertIsVarchar(CalcReg reg)
Asserts input is of type CalcProgramBuilder.OpType.Varchar |
protected void |
assertNotDivideByZero(CalcReg reg)
Asserts that Register is nothing of the following, all at once of Literal type of Integer type its value is zero |
protected void |
assertOperandsNotNull(CalcProgramBuilder.Operand[] operands)
Asserts that all operands are not null. |
protected void |
assertRegisterBool(CalcReg reg)
Asserts that the register is declared as CalcProgramBuilder.OpType.Bool |
protected void |
assertRegisterInteger(CalcReg reg)
Asserts that the register is declared as integer. |
protected void |
assertRegisterIsPointer(CalcReg reg)
Asserts that the register is declared as a pointer. |
protected void |
assertRegisterLiteral(CalcReg reg)
Asserts that the register is declared as CalcProgramBuilder.RegisterSetType.Literal |
protected void |
assertRegisterNotConstant(CalcReg reg)
Asserts that the register is not declared as CalcProgramBuilder.RegisterSetType.Literal or CalcProgramBuilder.RegisterSetType.Input . |
private void |
bindReferences()
Replaces a label with a line number |
void |
clear()
Re-initializes a CalcProgramBuilder. |
protected void |
compilationAssert(boolean cond)
|
protected void |
compilationAssert(boolean cond,
String msg)
|
(package private) static String |
formatComment(String comment)
Formats a "logical" comment into a "physical" comment the calculator recognises. |
private void |
generateRegDeclarations(PrintWriter writer,
CalcProgramBuilder.RegisterSetType registerSetType)
|
private void |
generateRegValues(PrintWriter writer)
|
int |
getCurrentLineNumber()
Returns the number of existing lines of instructions - 1 |
protected void |
getInstructions(PrintWriter writer)
See getProgram() |
String |
getProgram()
A program consists of two parts, its instructions and the user defined registers with its value. |
(package private) CalcReg |
getRegister(int ordinal,
CalcProgramBuilder.RegisterSetType registerType)
Returns the register of a given ordinal and type. |
protected void |
getRegisterSetsLayout(PrintWriter writer)
Outputs register declarations. |
CalcReg |
newBoolLiteral(boolean b)
|
CalcReg |
newDoubleLiteral(double d)
|
CalcReg |
newFloatLiteral(float f)
|
CalcReg |
newInput(CalcProgramBuilder.OpType type,
int storageBytes)
Creates a register in the input set and returns its reference |
CalcReg |
newInput(CalcProgramBuilder.RegisterDescriptor desc)
|
CalcReg |
newInt4Literal(int i)
|
CalcReg |
newInt8Literal(long i)
|
private CalcReg |
newLiteral(CalcProgramBuilder.OpType type,
Object value,
int storageBytes)
Generates a reference to a constant register. |
CalcReg |
newLiteral(CalcProgramBuilder.RegisterDescriptor desc,
Object value)
|
CalcReg |
newLocal(CalcProgramBuilder.OpType type,
int storageBytes)
Creates a register in the local set and returns its reference |
CalcReg |
newLocal(CalcProgramBuilder.RegisterDescriptor desc)
Creates a register in the local set and returns its reference |
CalcReg |
newOutput(CalcProgramBuilder.OpType type,
int storageBytes)
Generates a reference to an output register. |
CalcReg |
newOutput(CalcProgramBuilder.RegisterDescriptor desc)
Generates a reference to an output register. |
CalcReg |
newStatus(CalcProgramBuilder.OpType type,
int storageBytes)
Creates a register in the status set and returns its reference |
CalcReg |
newUint4Literal(BigInteger i)
|
CalcReg |
newUint8Literal(BigInteger i)
|
CalcReg |
newVarbinaryLiteral(byte[] bytes)
|
CalcReg |
newVarcharLiteral(String s)
Generates a reference to a string literal. |
CalcReg |
newVarcharLiteral(String s,
int length)
Generates a reference to a string literal. |
private void |
optimize()
Tries to optimize the program. |
private String |
prettyPrint(String program)
Introduces NL's for prettiness. |
private void |
printOperands(PrintWriter writer,
CalcProgramBuilder.Operand[] operands)
|
void |
setOutputComments(boolean outputComments)
|
void |
setSeparator(String separator)
Sets the separator between instructions in the generated program. |
static int |
stringByteCount(int i)
Returns the number of bytes storage required for a string. |
static int |
stringByteCount(String s)
Returns the number of bytes storage required for a string. |
private void |
validate()
Validates the program. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String NL
public static final String SEPARATOR_SEMICOLON
public static final String SEPARATOR_NEWLINE
public static final String SEPARATOR_SEMICOLON_NEWLINE
private static final Logger tracer
private static final BigInteger Uint4_MAX
private static final BigInteger Uint8_MAX
public static final CalcProgramBuilder.InstructionDef refInstruction
public static final CalcProgramBuilder.JumpInstructionDef jumpInstruction
public static final CalcProgramBuilder.JumpInstructionDef jumpTrueInstruction
public static final CalcProgramBuilder.JumpInstructionDef jumpFalseInstruction
public static final CalcProgramBuilder.JumpInstructionDef jumpNullInstruction
public static final CalcProgramBuilder.JumpInstructionDef jumpNotNullInstruction
public static final CalcProgramBuilder.InstructionDef returnInstruction
public static final CalcProgramBuilder.InstructionDef nativeAdd
public static final CalcProgramBuilder.InstructionDef pointerAdd
public static final CalcProgramBuilder.InstructionDef boolAnd
public static final CalcProgramBuilder.InstructionDef integralNativeAnd
public static final CalcProgramBuilder.InstructionDef cast
public static final CalcProgramBuilder.InstructionDef call
public static final CalcProgramBuilder.InstructionDef nativeDiv
public static final CalcProgramBuilder.InstructionDef boolNativeEqual
public static final CalcProgramBuilder.InstructionDef boolEqual
public static final CalcProgramBuilder.InstructionDef pointerBoolEqual
public static final CalcProgramBuilder.InstructionDef boolGreaterThan
public static final CalcProgramBuilder.InstructionDef boolNativeGreaterThan
public static final CalcProgramBuilder.InstructionDef pointerBoolGreaterThan
public static final CalcProgramBuilder.InstructionDef boolGreaterOrEqualThan
public static final CalcProgramBuilder.InstructionDef boolNativeGreaterOrEqualThan
public static final CalcProgramBuilder.InstructionDef pointerBoolGreaterOrEqualThan
public static final CalcProgramBuilder.InstructionDef boolNativeIsNull
public static final CalcProgramBuilder.InstructionDef boolIsNull
public static final CalcProgramBuilder.InstructionDef pointerBoolIsNull
public static final CalcProgramBuilder.InstructionDef boolNativeIsNotNull
public static final CalcProgramBuilder.InstructionDef boolIsNotNull
public static final CalcProgramBuilder.InstructionDef pointerBoolIsNotNull
public static final CalcProgramBuilder.InstructionDef boolLessThan
public static final CalcProgramBuilder.InstructionDef boolNativeLessThan
public static final CalcProgramBuilder.InstructionDef pointerBoolLessThan
public static final CalcProgramBuilder.InstructionDef boolLessOrEqualThan
public static final CalcProgramBuilder.InstructionDef boolNativeLessOrEqualThan
public static final CalcProgramBuilder.InstructionDef pointerBoolLessOrEqualThan
public static final CalcProgramBuilder.InstructionDef nativeMinus
public static final CalcProgramBuilder.InstructionDef integralNativeMod
public static final CalcProgramBuilder.InstructionDef move
public static final CalcProgramBuilder.InstructionDef boolMove
public static final CalcProgramBuilder.InstructionDef nativeMove
public static final CalcProgramBuilder.InstructionDef pointerMove
public static final CalcProgramBuilder.InstructionDef integralNativeMul
public static final CalcProgramBuilder.InstructionDef boolNot
public static final CalcProgramBuilder.InstructionDef boolNativeNotEqual
public static final CalcProgramBuilder.InstructionDef boolNotEqual
public static final CalcProgramBuilder.InstructionDef pointerBoolNotEqual
public static final CalcProgramBuilder.InstructionDef boolOr
public static final CalcProgramBuilder.InstructionDef integralNativeOr
public static final CalcProgramBuilder.InstructionDef nativeNeg
public static final CalcProgramBuilder.InstructionDef raise
public static final CalcProgramBuilder.InstructionDef round
public static final CalcProgramBuilder.InstructionDef integralNativeShiftLeft
public static final CalcProgramBuilder.InstructionDef integralNativeShiftRight
public static final CalcProgramBuilder.InstructionDef integralNativeXor
public static final CalcProgramBuilder.InstructionDef[] allInstrDefs
protected String separator
protected final List<CalcProgramBuilder.Instruction> instructions
protected CalcProgramBuilder.RegisterSets registerSets
protected final Map<CalcProgramBuilder.LiteralPair,CalcReg> literals
protected final Map<String,Integer> labels
private boolean outputComments
Constructor Detail |
---|
public CalcProgramBuilder()
Method Detail |
---|
public void clear()
public void setSeparator(String separator)
';'
or '\n'
or ";\n"
public void setOutputComments(boolean outputComments)
protected void compilationAssert(boolean cond, String msg)
protected void compilationAssert(boolean cond)
public String getProgram()
getRegisterSetsLayout(java.io.PrintWriter)
getInstructions(java.io.PrintWriter)
private String prettyPrint(String program)
CalcReg getRegister(int ordinal, CalcProgramBuilder.RegisterSetType registerType)
private void bindReferences()
public int getCurrentLineNumber()
protected void getRegisterSetsLayout(PrintWriter writer)
O vc,20; I vc,30; C u1, u1, vc,22, vc,12, vc,30, s4; V 1, 0, 'ISO-8859-1', 'WILMA', 'ISO-8859-1$en', 0; L vc,30, u1, s4; S u1;
writer
- private void generateRegDeclarations(PrintWriter writer, CalcProgramBuilder.RegisterSetType registerSetType)
private void generateRegValues(PrintWriter writer)
protected void getInstructions(PrintWriter writer)
getProgram()
writer
- private void optimize()
private void validate()
private void printOperands(PrintWriter writer, CalcProgramBuilder.Operand[] operands)
public CalcReg newOutput(CalcProgramBuilder.OpType type, int storageBytes)
public CalcReg newOutput(CalcProgramBuilder.RegisterDescriptor desc)
private CalcReg newLiteral(CalcProgramBuilder.OpType type, Object value, int storageBytes)
type
- CalcProgramBuilder.OpType
Type of valuevalue
- Valuepublic CalcReg newLiteral(CalcProgramBuilder.RegisterDescriptor desc, Object value)
public CalcReg newBoolLiteral(boolean b)
public CalcReg newInt4Literal(int i)
public CalcReg newInt8Literal(long i)
public CalcReg newUint4Literal(BigInteger i)
public CalcReg newUint8Literal(BigInteger i)
public CalcReg newFloatLiteral(float f)
public CalcReg newDoubleLiteral(double d)
public CalcReg newVarbinaryLiteral(byte[] bytes)
public CalcReg newVarcharLiteral(String s)
public CalcReg newVarcharLiteral(String s, int length)
public static int stringByteCount(String s)
public static int stringByteCount(int i)
public CalcReg newInput(CalcProgramBuilder.OpType type, int storageBytes)
public CalcReg newInput(CalcProgramBuilder.RegisterDescriptor desc)
public CalcReg newLocal(CalcProgramBuilder.OpType type, int storageBytes)
public CalcReg newLocal(CalcProgramBuilder.RegisterDescriptor desc)
public CalcReg newStatus(CalcProgramBuilder.OpType type, int storageBytes)
protected void addInstruction(CalcProgramBuilder.InstructionDef instrDef, CalcProgramBuilder.Operand... operands)
instruction.add(builder, operand0, operand1, ...)
.
instrDef
- Instruction defnoperands
- Operandsprotected void addComment(String comment)
formatComment(java.lang.String)
comment
- static String formatComment(String comment)
formatCommnet("yo wassup?")
outputs' /* yo wassup? */'
NB the inital space.
protected void assertRegisterNotConstant(CalcReg reg)
CalcProgramBuilder.RegisterSetType.Literal
or CalcProgramBuilder.RegisterSetType.Input
.
reg
- Registerprotected void assertRegisterLiteral(CalcReg reg)
CalcProgramBuilder.RegisterSetType.Literal
reg
- Registerprotected void assertRegisterBool(CalcReg reg)
CalcProgramBuilder.OpType.Bool
reg
- Registerprotected void assertRegisterIsPointer(CalcReg reg)
CalcProgramBuilder.OpType.Varchar
,CalcProgramBuilder.OpType.Varbinary
reg
- Registerprotected void assertRegisterInteger(CalcReg reg)
CalcProgramBuilder.OpType.Int4
, CalcProgramBuilder.OpType.Int8
, CalcProgramBuilder.OpType.Uint4
,CalcProgramBuilder.OpType.Uint8
reg
- Registerprotected void assertNotDivideByZero(CalcReg reg)
reg
- Registerprotected void assertIsNativeType(CalcReg reg)
reg
- Registerprotected void assertIsVarchar(CalcReg reg)
CalcProgramBuilder.OpType.Varchar
reg
- Registerprotected void assertOperandsNotNull(CalcProgramBuilder.Operand[] operands)
operands
- Operandspublic void addRef(CalcReg outputRegister, CalcReg src)
protected void addJumpBooleanWithCondition(CalcProgramBuilder.JumpInstructionDef op, int line, CalcReg reg)
public void addJumpTrue(int line, CalcReg reg)
line
if the
value in reg
is TRUE.
public void addJumpFalse(int line, CalcReg reg)
public void addJumpNull(int line, CalcReg reg)
public void addJumpNotNull(int line, CalcReg reg)
public void addReturn()
public void addLabelJump(String label)
public void addLabelJumpTrue(String label, CalcReg reg)
public void addLabelJumpFalse(String label, CalcReg reg)
public void addLabelJumpNull(String label, CalcReg reg)
public void addLabelJumpNotNull(String label, CalcReg reg)
public void addLabel(String label)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |