Exemple #1
0
 /**
  * Add appropriate CodeFragement which will be translated to machine instruction to the fragments
  * in codegen *
  */
 public void emitCode() {
   if (value.length() > 255) {
     System.err.println("CODEGEN PROBLEM: TEXT CONSTANT BEYOND RECOMMENDED LIMIT of 255");
   }
   for (int g = this.value.length() - 1; g >= 0; g--) {
     CodeGen.addCodeFragment("INSTRUCTION", (short) value.charAt(g), null, "PUSH");
   }
 }
  @Override
  public void codeGen(CodeGen codeGen) {
    LabelInstruction falseLabel = new LabelInstruction("FalseBranchLabel");
    LabelInstruction endLabel = new LabelInstruction("EndIfLabel");

    condition.codeGen(codeGen);

    Instruction pushInstr = new PushLabelInstruction(falseLabel.getName());
    codeGen.generateCode(pushInstr);

    Instruction bfInstruction = new Instruction(Machine.BF, "BF");
    codeGen.generateCode(bfInstruction);

    ListIterator<Stmt> trueStatements = this.whenTrue.listIterator();
    ListIterator<Stmt> falseStatements = this.whenFalse.listIterator();

    while (trueStatements.hasNext()) {
      Stmt statement = trueStatements.next();
      statement.codeGen(codeGen);
    }

    Instruction pushInstr2 = new PushLabelInstruction(endLabel.getName());
    codeGen.generateCode(pushInstr2);

    Instruction brInstr = new Instruction(Machine.BR, "BR");
    codeGen.generateCode(brInstr);

    codeGen.generateCode(falseLabel);

    while (falseStatements.hasNext()) {
      Stmt statement = falseStatements.next();
      statement.codeGen(codeGen);
    }

    codeGen.generateCode(endLabel);
  }