📄 codegeneration.jrag
字号:
public void StringLiteral.emitPushConstant(CodeGeneration gen) { StringLiteral.push(gen, getLITERAL()); } public void NullLiteral.emitPushConstant(CodeGeneration gen) { gen.emit(Bytecode.ACONST_NULL); } public void BooleanLiteral.emitPushConstant(CodeGeneration gen) { BooleanLiteral.push(gen, constant().booleanValue()); } public void ASTNode.error() { Throwable t = new Throwable(); StackTraceElement[] ste = new Throwable().getStackTrace(); String s = ste[1].toString(); throw new Error(s+" Cannot create bytecode for:"+getClass().getName()); } public void Constant.createBCode(CodeGeneration gen) { if(this instanceof ConstantInt) IntegerLiteral.push(gen, intValue()); else if(this instanceof ConstantLong) LongLiteral.push(gen, longValue()); else if(this instanceof ConstantFloat) FloatingPointLiteral.push(gen, floatValue()); else if(this instanceof ConstantDouble) DoubleLiteral.push(gen, doubleValue()); else if(this instanceof ConstantChar) IntegerLiteral.push(gen, intValue()); else if(this instanceof ConstantBoolean) BooleanLiteral.push(gen, booleanValue()); else if(this instanceof ConstantString) StringLiteral.push(gen, stringValue()); } // return public void TypeDecl.emitReturn(CodeGeneration gen) { error(); } public void VoidType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.RETURN);} public void PrimitiveType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.IRETURN);} public void LongType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.LRETURN);} public void FloatType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.FRETURN);} public void DoubleType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.DRETURN);} public void ReferenceType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.ARETURN);} public void NullType.emitReturn(CodeGeneration gen) { gen.emit(Bytecode.ARETURN);} syn byte TypeDecl.arrayLoad() { throw new Error("Cannot create array load for TypeDecl"); } eq ReferenceType.arrayLoad() = Bytecode.AALOAD; eq IntType.arrayLoad() = Bytecode.IALOAD; eq LongType.arrayLoad() = Bytecode.LALOAD; eq FloatType.arrayLoad() = Bytecode.FALOAD; eq DoubleType.arrayLoad() = Bytecode.DALOAD; eq ByteType.arrayLoad() = Bytecode.BALOAD; eq CharType.arrayLoad() = Bytecode.CALOAD; eq ShortType.arrayLoad() = Bytecode.SALOAD; eq BooleanType.arrayLoad() = Bytecode.BALOAD; public void TypeDecl.emitLoadLocal(CodeGeneration gen, int pos) {error();} public void PrimitiveType.emitLoadLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+1); if(pos == 0) gen.emit(Bytecode.ILOAD_0); else if(pos == 1) gen.emit(Bytecode.ILOAD_1); else if(pos == 2) gen.emit(Bytecode.ILOAD_2); else if(pos == 3) gen.emit(Bytecode.ILOAD_3); else if(pos < 256) gen.emit(Bytecode.ILOAD).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.ILOAD).add2(pos); } public void LongType.emitLoadLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+2); if(pos == 0) gen.emit(Bytecode.LLOAD_0); else if(pos == 1) gen.emit(Bytecode.LLOAD_1); else if(pos == 2) gen.emit(Bytecode.LLOAD_2); else if(pos == 3) gen.emit(Bytecode.LLOAD_3); else if(pos < 256) gen.emit(Bytecode.LLOAD).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.LLOAD).add2(pos); } public void FloatType.emitLoadLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+1); if(pos == 0) gen.emit(Bytecode.FLOAD_0); else if(pos == 1) gen.emit(Bytecode.FLOAD_1); else if(pos == 2) gen.emit(Bytecode.FLOAD_2); else if(pos == 3) gen.emit(Bytecode.FLOAD_3); else if(pos < 256) gen.emit(Bytecode.FLOAD).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.FLOAD).add2(pos); } public void DoubleType.emitLoadLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+2); if(pos == 0) gen.emit(Bytecode.DLOAD_0); else if(pos == 1) gen.emit(Bytecode.DLOAD_1); else if(pos == 2) gen.emit(Bytecode.DLOAD_2); else if(pos == 3) gen.emit(Bytecode.DLOAD_3); else if(pos < 256) gen.emit(Bytecode.DLOAD).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.DLOAD).add2(pos); } public void ReferenceType.emitLoadLocal(CodeGeneration gen, int pos) { gen.emitLoadReference(pos); } public void NullType.emitLoadLocal(CodeGeneration gen, int pos) { gen.emitLoadReference(pos); } public void FieldDeclaration.emitLoadField(CodeGeneration gen, TypeDecl typeDecl) { if(hostType().isArrayDecl() && name().equals("length")) { gen.emit(Bytecode.ARRAYLENGTH); return; } String classname = typeDecl.constantPoolName(); String desc = type().typeDescriptor(); String name = name(); int index = gen.constantPool().addFieldref(classname, name, desc); if(isStatic()) gen.emit(Bytecode.GETSTATIC, type().variableSize()).add2(index); else gen.emit(Bytecode.GETFIELD, type().variableSize() - 1).add2(index); } // emit store public void Expr.emitStore(CodeGeneration gen) { error("emitStore called with " + getClass().getName()); } public void AbstractDot.emitStore(CodeGeneration gen) { lastAccess().emitStore(gen); } public void VarAccess.emitStore(CodeGeneration gen) { Variable v = decl(); if(v instanceof VariableDeclaration) { VariableDeclaration decl = (VariableDeclaration)v; if(isDUbefore(v)) gen.addLocalVariableEntryAtCurrentPC(decl.name(), decl.type().typeDescriptor(), decl.localNum(), decl.variableScopeEndLabel(gen)); decl.type().emitStoreLocal(gen, decl.localNum()); } else if(v instanceof ParameterDeclaration) { ParameterDeclaration decl = (ParameterDeclaration)v; decl.type().emitStoreLocal(gen, decl.localNum()); } else if(v instanceof FieldDeclaration) { FieldDeclaration f = (FieldDeclaration)v; if(f.isPrivate() && !hostType().hasField(v.name())) f.createAccessorWrite(fieldQualifierType()).emitInvokeMethod(gen, fieldQualifierType()); else f.emitStoreField(gen, fieldQualifierType()); } } public void ArrayAccess.emitStore(CodeGeneration gen) { gen.emit(type().arrayStore()); } syn byte TypeDecl.arrayStore() { throw new Error("Cannot create array load for TypeDecl"); } eq ReferenceType.arrayStore() = Bytecode.AASTORE; eq IntType.arrayStore() = Bytecode.IASTORE; eq LongType.arrayStore() = Bytecode.LASTORE; eq FloatType.arrayStore() = Bytecode.FASTORE; eq DoubleType.arrayStore() = Bytecode.DASTORE; eq ByteType.arrayStore() = Bytecode.BASTORE; eq CharType.arrayStore() = Bytecode.CASTORE; eq ShortType.arrayStore() = Bytecode.SASTORE; eq BooleanType.arrayStore() = Bytecode.BASTORE; public void FieldDeclaration.emitStoreField(CodeGeneration gen, TypeDecl typeDecl) { String classname = typeDecl.constantPoolName(); String desc = type().typeDescriptor(); String name = name(); int index = gen.constantPool().addFieldref(classname, name, desc); if(isStatic()) gen.emit(Bytecode.PUTSTATIC, -type().variableSize()).add2(index); else gen.emit(Bytecode.PUTFIELD, -type().variableSize() - 1).add2(index); } public void TypeDecl.emitStoreLocal(CodeGeneration gen, int pos) {error();} public void PrimitiveType.emitStoreLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+1); if(pos == 0) gen.emit(Bytecode.ISTORE_0); else if(pos == 1) gen.emit(Bytecode.ISTORE_1); else if(pos == 2) gen.emit(Bytecode.ISTORE_2); else if(pos == 3) gen.emit(Bytecode.ISTORE_3); else if(pos < 256) gen.emit(Bytecode.ISTORE).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.ISTORE).add2(pos); } public void LongType.emitStoreLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+2); if(pos == 0) gen.emit(Bytecode.LSTORE_0); else if(pos == 1) gen.emit(Bytecode.LSTORE_1); else if(pos == 2) gen.emit(Bytecode.LSTORE_2); else if(pos == 3) gen.emit(Bytecode.LSTORE_3); else if(pos < 256) gen.emit(Bytecode.LSTORE).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.LSTORE).add2(pos); } public void FloatType.emitStoreLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+1); if(pos == 0) gen.emit(Bytecode.FSTORE_0); else if(pos == 1) gen.emit(Bytecode.FSTORE_1); else if(pos == 2) gen.emit(Bytecode.FSTORE_2); else if(pos == 3) gen.emit(Bytecode.FSTORE_3); else if(pos < 256) gen.emit(Bytecode.FSTORE).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.FSTORE).add2(pos); } public void DoubleType.emitStoreLocal(CodeGeneration gen, int pos) { gen.maxLocals = Math.max(gen.maxLocals, pos+2); if(pos == 0) gen.emit(Bytecode.DSTORE_0); else if(pos == 1) gen.emit(Bytecode.DSTORE_1); else if(pos == 2) gen.emit(Bytecode.DSTORE_2); else if(pos == 3) gen.emit(Bytecode.DSTORE_3); else if(pos < 256) gen.emit(Bytecode.DSTORE).add(pos); else gen.emit(Bytecode.WIDE).emit(Bytecode.DSTORE).add2(pos); } public void ReferenceType.emitStoreLocal(CodeGeneration gen, int pos) { gen.emitStoreReference(pos); } public void NullType.emitStoreLocal(CodeGeneration gen, int pos) { gen.emitStoreReference(pos); } // exceptions inh TypeDecl TryStmt.typeThrowable(); public void TryStmt.emitExceptionHandler(CodeGeneration gen) { // add 1 to stack depth gen.changeStackDepth(1); int num = localNum(); gen.emitStoreReference(num); gen.emitJsr(label_finally_block()); gen.emitLoadReference(num); gen.emit(Bytecode.ATHROW); } public void TryStmt.emitFinallyBlock(CodeGeneration gen) { // add 1 to stack depth gen.changeStackDepth(1); int num = localNum()+1; gen.emitStoreReference(num); getFinally().createBCode(gen); if(num < 256) gen.emit(Bytecode.RET).add(num); else gen.emit(Bytecode.WIDE).emit(Bytecode.RET).add2(num); } public void SynchronizedStmt.emitMonitorEnter(CodeGeneration gen) { gen.emitDup(); int num = localNum(); gen.emitStoreReference(num); gen.emit(Bytecode.MONITORENTER); } public void SynchronizedStmt.emitExceptionHandler(CodeGeneration gen) { // add 1 to stack depth gen.changeStackDepth(1); int num = localNum() + 1; gen.emitStoreReference(num); gen.emitJsr(label_finally_block()); gen.emitLoadReference(num); gen.emit(Bytecode.ATHROW); } public void SynchronizedStmt.emitFinallyBlock(CodeGeneration gen) { // add 1 to stack depth gen.changeStackDepth(1); int num = localNum() + 2; gen.emitStoreReference(num); gen.emitLoadReference(localNum()); // monitor gen.emit(Bytecode.MONITOREXIT); gen.emit(Bytecode.RET).add(num); } // dup public void TypeDecl.emitDup(CodeGeneration gen) { gen.emit(Bytecode.DUP); } public void VoidType.emitDup(CodeGeneration gen) { } public void DoubleType.emitDup(CodeGeneration gen) { gen.emit(Bytecode.DUP2); } public void LongType.emitDup(CodeGeneration gen) { gen.emit(Bytecode.DUP2); } public void TypeDecl.emitDup_x1(CodeGeneration gen) { gen.emit(Bytecode.DUP_X1); } public void VoidType.emitDup_x1(CodeGeneration gen) { } public void DoubleType.emitDup_x1(CodeGeneration gen) { gen.emit(Bytecode.DUP2_X1); } public void LongType.emitDup_x1(CodeGeneration gen) { gen.emit(Bytecode.DUP2_X1); } public void TypeDecl.emitDup_x2(CodeGeneration gen) { gen.emit(Bytecode.DUP_X2); } public void VoidType.emitDup_x2(CodeGeneration gen) { } public void DoubleType.emitDup_x2(CodeGeneration gen) { gen.emit(Bytecode.DUP2_X2); } public void LongType.emitDup_x2(CodeGeneration gen) { gen.emit(Bytecode.DUP2_X2); } public void TypeDecl.emitPop(CodeGeneration gen) { gen.emit(Bytecode.POP); } public void VoidType.emitPop(CodeGeneration gen) { } public void DoubleType.emitPop(CodeGeneration gen) { gen.emit(Bytecode.POP2); } public void LongType.emitPop(CodeGeneration gen) { gen.emit(Bytecode.POP2); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -