Class LlvmirGenerator
java.lang.Object
LlvmirGenerator
LLVM IR Generator from yaLcc parse tree
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intUsed by newLine to indent linesprivate intlabel counterprivate StringBuilderprivate intunamed variable counter (temporary value)Map of named var and their identifier -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringcompOpToLlvmOp(String compOp) generateLLVMIR(ParseTree treeNode) generate LLVM IR code from a ParseTree root programprivate StringgetOrNewI32(String varName) Retrieve the varId of a named var.private voidheader()Header write at the begining of the LLVM IR codeprivate Stringload a named var into a new unamed var.private voidAssign a value or ExprArith to a named i32private voidnewCodeBranch(ParseTree treeNode) Recursive method to chenerate code by reading treeprivate StringNew condition.private StringnewCondAtom(ParseTree treeNode) private StringnewCondComp(ParseTree treeNode) Write a new comparison in llvm ir.private StringnewCondImpl(ParseTree treeNode) Create a new cond impl.private StringnewExprAddSub(ParseTree treeNode) Write a new expr arithmetic add or sub in llvm ir.private StringnewExprArith(ParseTree treeNode) Write a new expr arithmetic in llvm ir.private StringnewExprMulDiv(ParseTree treeNode) private StringnewExprPrimary(ParseTree treeNode) private StringnewExprUnary(ParseTree treeNode) WriteExprPrimary if needed. private voidStrat of an if statement.private StringRead a int in input and return the name of the new unamed register holding it.private voidnewInstructions(ParseTree treeNode) Generate instruction recursivelyprivate StringnewLabel()Create new label name.private voidWrite a indented line in the LLVM IR code.private voidWrite a indented line in LLVM IR codeprivate voidGenerate output for the grammar:private StringCreate a new unique unamed variable container.private voidprivate voidstoreInNamI32(String varName, String unamedI32Id) store an unamed var into a named var.
-
Field Details
-
llvmirCode
-
var
Map of named var and their identifier -
unamedVarCounter
private int unamedVarCounterunamed variable counter (temporary value) -
labelCounter
private int labelCounterlabel counter -
indentLevel
private int indentLevelUsed by newLine to indent lines
-
-
Constructor Details
-
LlvmirGenerator
-
-
Method Details
-
newLine
Write a indented line in LLVM IR code- Parameters:
line- string to write in llvm ir code.indentLevel- indent level of llvm code.
-
newLine
Write a indented line in the LLVM IR code.- Parameters:
line- string to write in llvm ir code
-
newLabel
Create new label name. Will not put "%" before the string (important) -
newUnamedI32Id
Create a new unique unamed variable container. "%" in the begining is part of the string at every call need it.- Returns:
-
header
private void header()Header write at the begining of the LLVM IR code -
generateLLVMIR
generate LLVM IR code from a ParseTree root program- Parameters:
treeNode- ParseTree root node.- Returns:
- String of generated LLVM IR code.
-
newCodeBranch
Recursive method to chenerate code by reading tree -
getOrNewI32
Retrieve the varId of a named var. Create it inplace if not existant as named var are global, no need to look for scope (and there is a garbage collector). -
loadI32
load a named var into a new unamed var. Create named var if non existant -
storeInNamI32
store an unamed var into a named var. Create named var if non existant -
newInstructions
Generate instruction recursively- Parameters:
node-
-
newAssign
Assign a value or ExprArith to a named i32- Parameters:
treeNode-
-
newIf
Strat of an if statement.- Parameters:
treeNode- if PaserTree node.
-
newCond
New condition. Value of the result is stored in a unamed i32.- Parameters:
treeNode- node of Cond ParseTree- Returns:
- String i32 identifier of the result.
-
newCondImpl
Create a new cond impl. Store the result in variable en return the identifier (%).- Parameters:
treeNode-- Returns:
- the unamed i32 id of the result.
-
newCondAtom
-
compOpToLlvmOp
-
newCondComp
Write a new comparison in llvm ir.- Parameters:
treeNode- node of the comparison.- Returns:
- String id of the unamed i32 holding the result.
-
newWhile
-
newOutput
Generate output for the grammar: -
newInput
Read a int in input and return the name of the new unamed register holding it. < Input > ==> Input([VarName]) -
newExprArith
Write a new expr arithmetic in llvm ir.- Parameters:
treeNode- ParseTree node of the expr arithm.- Returns:
- String id of the unamed i32 holding the result.
-
newExprAddSub
Write a new expr arithmetic add or sub in llvm ir. ExprAddSub => ExprMulDiv { (+|-) ExprMulDiv }*- Parameters:
treeNode- ParseTree node of the ExprAddSub- Returns:
- String of the unamed i32 id containing the result.
-
newExprMulDiv
-
newExprUnary
WriteExprPrimary if needed. - Parameters:
treeNode- [TODO:parameter]- Returns:
- string of unamed i32 id containing the (not ExprPrimary) (if MINUS).
-
newExprPrimary
-