⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createbcode.jrag

📁 JDK1.4编译器后端
💻 JRAG
📖 第 1 页 / 共 4 页
字号:
  public void ExprStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    getExpr().createBCode(gen);    if(needsPop())      getExpr().type().emitPop(gen);  }  syn lazy DefaultCase SwitchStmt.defaultCase() {    for(int i= 0; i < getBlock().getNumStmt(); i++) {      if(getBlock().getStmt(i) instanceof DefaultCase)        return (DefaultCase)getBlock().getStmt(i);    }    return null;  }  syn lazy int SwitchStmt.end_label() = hostType().constantPool().newLabel();  public void SwitchStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    int cond_label = hostType().constantPool().newLabel();    int switch_label = hostType().constantPool().newLabel();    gen.emitGoto(cond_label);    getBlock().createBCode(gen);    if(canCompleteNormally())      gen.emitGoto(end_label());    gen.addLabel(cond_label);    getExpr().createBCode(gen);    TreeMap map = new TreeMap();    for(int i = 0; i < getBlock().getNumStmt(); i++) {      if(getBlock().getStmt(i) instanceof ConstCase) {        ConstCase ca = (ConstCase)getBlock().getStmt(i);        map.put(new Integer(ca.getValue().constant().intValue()), ca);      }            }    long low = map.isEmpty() ? 0 : ((Integer)map.firstKey()).intValue();    long high = map.isEmpty() ? 0 : ((Integer)map.lastKey()).intValue();    long tableSwitchSize = 8L + (high - low + 1L) * 4L;    long lookupSwitchSize = 4L + map.size() * 8L;    gen.addLabel(switch_label);    if(tableSwitchSize < lookupSwitchSize) {      gen.emit(Bytecode.TABLESWITCH);      int pad = emitPad(gen);      int defaultOffset = defaultOffset(gen, switch_label);      if(defaultOffset == 0) {        defaultOffset = 1 + pad + 4 + 4 + 4 + 4 * (int)(high - low + 1);      }      gen.add4(defaultOffset);      gen.add4((int)low);      gen.add4((int)high);      for(long i = low; i <= high; i++) {        ConstCase ca = (ConstCase)map.get(new Integer((int)i));        if(ca != null) {          int offset = gen.addressOf(ca.label(gen))            - gen.addressOf(switch_label);          gen.add4(offset);        }        else {          gen.add4(defaultOffset);        }      }    }    else {      gen.emit(Bytecode.LOOKUPSWITCH);      int pad = emitPad(gen);      int defaultOffset = defaultOffset(gen, switch_label);      if(defaultOffset == 0) {        defaultOffset = 1 + pad + 4 + 4 + 8 * numCase();      }      gen.add4(defaultOffset);      gen.add4(map.size());      for(Iterator iter = map.values().iterator(); iter.hasNext(); ) {        ConstCase ca = (ConstCase)iter.next();        gen.add4(ca.getValue().constant().intValue());        int offset = gen.addressOf(ca.label(gen))          - gen.addressOf(switch_label);        gen.add4(offset);      }    }    gen.addLabel(end_label());  }  syn int SwitchStmt.numCase() {    int result = 0;    for(int i = 0; i < getBlock().getNumStmt(); i++)      if(getBlock().getStmt(i) instanceof Case)        result++;    return result;  }  private int SwitchStmt.emitPad(CodeGeneration gen) {    int pad = (4 - (gen.pos() % 4)) % 4;    for(int i = 0; i < pad; i++)      gen.emit(Bytecode.NOP);    if(gen.pos() % 4 != 0)      throw new Error("Switch not at 4-byte boundary:" + gen.pos());    return pad;  }  private int SwitchStmt.defaultOffset(CodeGeneration gen, int switch_label) {    boolean hasDefault = defaultCase() != null;    if(hasDefault) {      int offset = gen.addressOf(defaultCase().label(gen))        - gen.addressOf(switch_label);      return offset;    }    return 0;  }  syn lazy int Case.label(CodeGeneration gen) = hostType().constantPool().newLabel();  public void Case.createBCode(CodeGeneration gen) {    gen.addLabel(label(gen));  }  syn lazy int IfStmt.else_branch_label() = hostType().constantPool().newLabel();  syn lazy int IfStmt.then_branch_label() = hostType().constantPool().newLabel();  public void IfStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    int elseBranch = else_branch_label();    int thenBranch = then_branch_label();    int endBranch = hostType().constantPool().newLabel();    getCondition().emitEvalBranch(gen);    gen.addLabel(thenBranch);    //if(getCondition().canBeTrue()) {      getThen().createBCode(gen);      if(getThen().canCompleteNormally() && hasElse() /*&& getCondition().canBeFalse()*/)        gen.emitGoto(endBranch);    //}    gen.addLabel(elseBranch);    if(hasElse() /*&& getCondition().canBeFalse()*/)      getElse().createBCode(gen);    gen.addLabel(endBranch);  }  syn lazy int WhileStmt.cond_label() = hostType().constantPool().newLabel();  syn lazy int WhileStmt.end_label() = hostType().constantPool().newLabel();  syn lazy int WhileStmt.stmt_label() = hostType().constantPool().newLabel();  public void WhileStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    gen.addLabel(cond_label());    getCondition().emitEvalBranch(gen);    gen.addLabel(stmt_label());    if(getCondition().canBeTrue()) {      getStmt().createBCode(gen);      if(getStmt().canCompleteNormally())        gen.emitGoto(cond_label());    }    gen.addLabel(end_label());  }  syn lazy int DoStmt.begin_label() = hostType().constantPool().newLabel();  syn lazy int DoStmt.cond_label() = hostType().constantPool().newLabel();  syn lazy int DoStmt.end_label() = hostType().constantPool().newLabel();  public void DoStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    gen.addLabel(begin_label());    getStmt().createBCode(gen);    gen.addLabel(cond_label());    getCondition().emitEvalBranch(gen);    gen.addLabel(end_label());  }  syn lazy int ForStmt.cond_label() = hostType().constantPool().newLabel();  syn lazy int ForStmt.begin_label() = hostType().constantPool().newLabel();  syn lazy int ForStmt.update_label() = hostType().constantPool().newLabel();  syn lazy int ForStmt.end_label() = hostType().constantPool().newLabel();  public void ForStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    for (int i=0; i<getNumInitStmt(); i++) {      getInitStmt(i).createBCode(gen);    }    gen.addLabel(cond_label());    getCondition().emitEvalBranch(gen);    if(getCondition().canBeTrue()) {      gen.addLabel(begin_label());      getStmt().createBCode(gen);      gen.addLabel(update_label());	      for (int i=0; i<getNumUpdateStmt(); i++)        getUpdateStmt(i).createBCode(gen);      gen.emitGoto(cond_label());    }    if(canCompleteNormally()) {      gen.addLabel(end_label());    }    gen.addVariableScopeLabel(variableScopeEndLabel(gen));  }    syn int Stmt.break_label() {    throw new UnsupportedOperationException("Can not break at this statement of type " + getClass().getName());  }  eq ForStmt.break_label() = end_label();  eq WhileStmt.break_label() = end_label();  eq DoStmt.break_label() = end_label();  eq LabeledStmt.break_label() = end_label();  eq SwitchStmt.break_label() = end_label();  eq TryStmt.break_label() =  label_finally();  public void BreakStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    for(Iterator iter = finallyList().iterator(); iter.hasNext(); ) {      FinallyHost stmt = (FinallyHost)iter.next();      gen.emitJsr(stmt.label_finally_block());    }    gen.emitGoto(targetStmt().break_label());  }  syn int Stmt.continue_label() {    throw new UnsupportedOperationException("Can not continue at this statement");  }  eq ForStmt.continue_label() = update_label();  eq WhileStmt.continue_label() = cond_label();  eq DoStmt.continue_label() = cond_label();  eq LabeledStmt.continue_label() = getStmt().continue_label();  eq TryStmt.continue_label() = label_finally();  public void ContinueStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    for(Iterator iter = finallyList().iterator(); iter.hasNext(); ) {      FinallyHost stmt = (FinallyHost)iter.next();      gen.emitJsr(stmt.label_finally_block());    }    gen.emitGoto(targetStmt().continue_label());  }  public void ReturnStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    if(hasResult()) {      TypeDecl type = null;      BodyDecl b = enclosingBodyDecl();      if(b instanceof MethodDecl) {        type = ((MethodDecl)b).type();      }      else {        throw new Error("Can not create code that returns value within non method");      }      getResult().createBCode(gen);      getResult().type().emitCastTo(gen, type);      if(!finallyList().isEmpty()) {        type.emitStoreLocal(gen, resultSaveLocalNum());      }      for(Iterator iter = finallyList().iterator(); iter.hasNext(); ) {        FinallyHost stmt = (FinallyHost)iter.next();        gen.emitJsr(stmt.label_finally_block());      }      if(!finallyList().isEmpty()) {        type.emitLoadLocal(gen, resultSaveLocalNum());      }      type.emitReturn(gen);    }    else {      for(Iterator iter = finallyList().iterator(); iter.hasNext(); ) {        FinallyHost stmt = (FinallyHost)iter.next();        gen.emitJsr(stmt.label_finally_block());      }      gen.emitReturn();    }  }  public void ThrowStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    getExpr().createBCode(gen);    gen.emitThrow();  }  syn lazy int FinallyHost.label_finally_block();  syn lazy int TryStmt.label_begin() = hostType().constantPool().newLabel();  syn lazy int TryStmt.label_block_end() = hostType().constantPool().newLabel();  syn lazy int TryStmt.label_end() = hostType().constantPool().newLabel();  syn lazy int TryStmt.label_finally() = hostType().constantPool().newLabel();  eq TryStmt.label_finally_block() = hostType().constantPool().newLabel();  syn lazy int TryStmt.label_exception_handler() = hostType().constantPool().newLabel();  syn lazy int TryStmt.label_catch_end() = hostType().constantPool().newLabel();  public void TryStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    gen.addLabel(label_begin());    getBlock().createBCode(gen);    if(getBlock().canCompleteNormally())      gen.emitGoto(label_finally());    gen.addLabel(label_block_end());    for(int i = 0; i < getNumCatchClause(); i++) {      getCatchClause(i).createBCode(gen);      if(getCatchClause(i).getBlock().canCompleteNormally()) {        if(!hasFinally())          gen.emitGoto(label_finally());        else          gen.emitGoto(label_catch_end());      }    }        gen.addLabel(label_catch_end());    if(hasFinally() && getNumCatchClause() > 0) {      gen.emitJsr(label_finally_block());      if(canCompleteNormally())        gen.emitGoto(label_end());    }        gen.addLabel(label_finally());    if(hasFinally()) {      if(getBlock().canCompleteNormally()) {        gen.emitJsr(label_finally_block());        if(canCompleteNormally())          gen.emitGoto(label_end());      }      gen.addLabel(label_exception_handler());      emitExceptionHandler(gen);      gen.addLabel(label_finally_block());      emitFinallyBlock(gen);    }    gen.addLabel(label_end());    gen.createExceptionTable(this);  }  syn lazy int CatchClause.label() = hostType().constantPool().newLabel();  public void CatchClause.createBCode(CodeGeneration gen) {    gen.addLabel(label());    // add 1 to stack depth    gen.changeStackDepth(1);    getParameter().type().emitStoreLocal(gen, getParameter().localNum());    getBlock().createBCode(gen);  }  syn lazy int SynchronizedStmt.label_begin() = hostType().constantPool().newLabel();  syn lazy int SynchronizedStmt.label_end() = hostType().constantPool().newLabel();  syn lazy int SynchronizedStmt.label_finally() = hostType().constantPool().newLabel();  eq SynchronizedStmt.label_finally_block() = hostType().constantPool().newLabel();  syn lazy int SynchronizedStmt.label_exception_handler() = hostType().constantPool().newLabel();  public void SynchronizedStmt.createBCode(CodeGeneration gen) {    super.createBCode(gen);    getExpr().createBCode(gen);    emitMonitorEnter(gen);    gen.addLabel(label_begin());    getBlock().createBCode(gen);    gen.addLabel(label_finally());    if(getBlock().canCompleteNormally()) {      gen.emitJsr(label_finally_block());      gen.emitGoto(label_end());    }    gen.addLabel(label_exception_handler());    emitExceptionHandler(gen);    gen.addLabel(label_finally_block());    emitFinallyBlock(gen);    gen.addLabel(label_end());    gen.createExceptionTable(this);  }  public void AssertStmt.createBCode(CodeGeneration gen) {    throw new UnsupportedOperationException("Assert not implemented yet");  }  public void LocalClassDeclStmt.createBCode(CodeGeneration gen) {  }  public void ClassAccess.createBCode(CodeGeneration gen) {    if(prevExpr().type().isPrimitiveType() || prevExpr().type().isVoid()) {      TypeDecl typeDecl = lookupType("java.lang", prevExpr().type().primitiveClassName());      SimpleSet c = typeDecl.memberFields("TYPE");      FieldDeclaration f = (FieldDeclaration)c.iterator().next();      f.emitLoadField(gen, typeDecl);    }    else {      FieldDeclaration f = hostType().topLevelType().createStaticClassField(prevExpr().type().referenceClassFieldName());      // add method to perform lookup as a side-effect      MethodDecl m = hostType().topLevelType().createStaticClassMethod();      int next_label = hostType().constantPool().newLabel();      int end_label = hostType().constantPool().newLabel();      f.emitLoadField(gen, hostType());      gen.emitBranchNonNull(next_label);            // emit string literal            StringLiteral.push(gen, prevExpr().type().jvmName());      m.emitInvokeMethod(gen, hostType());      gen.emitDup();      f.emitStoreField(gen, hostType());      gen.emitGoto(end_label);      gen.addLabel(next_label);      gen.changeStackDepth(-1);      f.emitLoadField(gen, hostType());      gen.addLabel(end_label);    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -