Class LlvmirGenerator

java.lang.Object
LlvmirGenerator

public class LlvmirGenerator extends Object
LLVM IR Generator from yaLcc parse tree
  • Field Details

    • llvmirCode

      private StringBuilder llvmirCode
    • var

      private HashMap<String,String> var
      Map of named var and their identifier
    • unamedVarCounter

      private int unamedVarCounter
      unamed variable counter (temporary value)
    • labelCounter

      private int labelCounter
      label counter
    • indentLevel

      private int indentLevel
      Used by newLine to indent lines
  • Constructor Details

    • LlvmirGenerator

      public LlvmirGenerator(ParseTree parseTree)
  • Method Details

    • newLine

      private void newLine(String line, int indentLevel)
      Write a indented line in LLVM IR code
      Parameters:
      line - string to write in llvm ir code.
      indentLevel - indent level of llvm code.
    • newLine

      private void newLine(String line)
      Write a indented line in the LLVM IR code.
      Parameters:
      line - string to write in llvm ir code
    • newLabel

      private String newLabel()
      Create new label name. Will not put "%" before the string (important)
    • newUnamedI32Id

      private String 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

      public String generateLLVMIR(ParseTree treeNode)
      generate LLVM IR code from a ParseTree root program
      Parameters:
      treeNode - ParseTree root node.
      Returns:
      String of generated LLVM IR code.
    • newCodeBranch

      private void newCodeBranch(ParseTree treeNode)
      Recursive method to chenerate code by reading tree
    • getOrNewI32

      private String getOrNewI32(String varName)
      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

      private String loadI32(String varName)
      load a named var into a new unamed var. Create named var if non existant
    • storeInNamI32

      private void storeInNamI32(String varName, String unamedI32Id)
      store an unamed var into a named var. Create named var if non existant
    • newInstructions

      private void newInstructions(ParseTree treeNode)
      Generate instruction recursively
      Parameters:
      node -
    • newAssign

      private void newAssign(ParseTree treeNode)
      Assign a value or ExprArith to a named i32
      Parameters:
      treeNode -
    • newIf

      private void newIf(ParseTree treeNode)
      Strat of an if statement.
      Parameters:
      treeNode - if PaserTree node.
    • newCond

      private String newCond(ParseTree treeNode)
      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

      private String newCondImpl(ParseTree treeNode)
      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

      private String newCondAtom(ParseTree treeNode)
    • compOpToLlvmOp

      private String compOpToLlvmOp(String compOp)
    • newCondComp

      private String newCondComp(ParseTree treeNode)
      Write a new comparison in llvm ir.
      Parameters:
      treeNode - node of the comparison.
      Returns:
      String id of the unamed i32 holding the result.
    • newWhile

      private void newWhile(ParseTree treeNode)
    • newOutput

      private void newOutput(ParseTree treeNode)
      Generate output for the grammar: ==>Print([VarName])
    • newInput

      private String newInput(ParseTree treeNode)
      Read a int in input and return the name of the new unamed register holding it. < Input > ==> Input([VarName])
    • newExprArith

      private String newExprArith(ParseTree treeNode)
      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

      private String newExprAddSub(ParseTree treeNode)
      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

      private String newExprMulDiv(ParseTree treeNode)
    • newExprUnary

      private String newExprUnary(ParseTree treeNode)
      Write ExprPrimary if needed.
      Parameters:
      treeNode - [TODO:parameter]
      Returns:
      string of unamed i32 id containing the (not ExprPrimary) (if MINUS).
    • newExprPrimary

      private String newExprPrimary(ParseTree treeNode)