List of usage examples for org.objectweb.asm MethodVisitor visitVarInsn
public void visitVarInsn(final int opcode, final int var)
From source file:de.zib.sfs.instrument.MappedByteBufferAdapter.java
License:BSD License
@Override protected void initializeFields(MethodVisitor constructorMV, String constructorDesc) { // fromFileChannel = false; constructorMV.visitVarInsn(Opcodes.ALOAD, 0); constructorMV.visitInsn(Opcodes.ICONST_0); constructorMV.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(MappedByteBuffer.class), "fromFileChannel", Type.getDescriptor(Boolean.TYPE)); }
From source file:de.zib.sfs.instrument.MappedByteBufferAdapter.java
License:BSD License
@Override protected void appendWrappedMethods(ClassVisitor visitor) { // public void setFromFileChannel(boolean fromFileChannel) { MethodVisitor setFromFileChannelMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "setFromFileChannel", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), null, null); setFromFileChannelMV.visitCode();// w w w .j a v a 2 s . c o m // this.fromFileChannel = fromFileChannel; setFromFileChannelMV.visitVarInsn(Opcodes.ALOAD, 0); setFromFileChannelMV.visitVarInsn(Opcodes.ILOAD, 1); setFromFileChannelMV.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(MappedByteBuffer.class), "fromFileChannel", Type.getDescriptor(Boolean.TYPE)); // } setFromFileChannelMV.visitInsn(Opcodes.RETURN); setFromFileChannelMV.visitMaxs(0, 0); setFromFileChannelMV.visitEnd(); // public boolean isFromFileChannel() { MethodVisitor isFromFileChannelMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "isFromFileChannel", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), null, null); isFromFileChannelMV.visitCode(); // return fromFileChannel; // } isFromFileChannelMV.visitVarInsn(Opcodes.ALOAD, 0); isFromFileChannelMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class), "fromFileChannel", Type.getDescriptor(Boolean.TYPE)); isFromFileChannelMV.visitInsn(Opcodes.IRETURN); isFromFileChannelMV.visitMaxs(0, 0); isFromFileChannelMV.visitEnd(); // public void setFileDescriptor(FileDescriptor fileDescriptor) { MethodVisitor settFileDescriptorMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "setFileDescriptor", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)), null, null); settFileDescriptorMV.visitCode(); // this.fileDescriptor = fileDescriptor; settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0); settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 1); settFileDescriptorMV.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(MappedByteBuffer.class), "fileDescriptor", Type.getDescriptor(FileDescriptor.class)); // } settFileDescriptorMV.visitInsn(Opcodes.RETURN); settFileDescriptorMV.visitMaxs(0, 0); settFileDescriptorMV.visitEnd(); // public FileDescriptor getFileDescriptor() { MethodVisitor getFileDescriptorMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "getFileDescriptor", Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), null, null); getFileDescriptorMV.visitCode(); // return getFileDescriptorImpl(); // } getFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0); getFileDescriptorMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName, "getFileDescriptorImpl", Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), false); getFileDescriptorMV.visitInsn(Opcodes.ARETURN); getFileDescriptorMV.visitMaxs(0, 0); getFileDescriptorMV.visitEnd(); // protected FileDescriptor getFileDescriptorImpl() { MethodVisitor getFileDescriptorImplMV = visitor.visitMethod(Opcodes.ACC_PROTECTED, "getFileDescriptorImpl", Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), null, null); getFileDescriptorImplMV.visitCode(); // return fileDescriptor; // } getFileDescriptorImplMV.visitVarInsn(Opcodes.ALOAD, 0); getFileDescriptorImplMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class), "fileDescriptor", Type.getDescriptor(FileDescriptor.class)); getFileDescriptorImplMV.visitInsn(Opcodes.ARETURN); getFileDescriptorImplMV.visitMaxs(0, 0); getFileDescriptorImplMV.visitEnd(); visitor.visitEnd(); }
From source file:de.zib.sfs.instrument.ZipFileAdapter.java
License:BSD License
@Override public void visitEnd() { if (!this.skip.contains(OperationCategory.ZIP)) { // public void close() { MethodVisitor closeMethodMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC, "close", Type.getMethodDescriptor(Type.VOID_TYPE), null, new String[] { Type.getInternalName(IOException.class) }); closeMethodMV.visitCode();//from w ww. j a v a 2 s . co m // ZipFileCallback.closeCallback(jzfile); closeMethodMV.visitVarInsn(Opcodes.ALOAD, 0); closeMethodMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(ZipFile.class), "jzfile", Type.getDescriptor(Long.TYPE)); closeMethodMV.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(ZipFileCallback.class), "closeCallback", Type.getMethodDescriptor(Type.VOID_TYPE, Type.LONG_TYPE), false); // methodPrefixclose(); closeMethodMV.visitVarInsn(Opcodes.ALOAD, 0); closeMethodMV.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(ZipFile.class), this.methodPrefix + "close", Type.getMethodDescriptor(Type.VOID_TYPE), false); // } closeMethodMV.visitInsn(Opcodes.RETURN); closeMethodMV.visitMaxs(0, 0); closeMethodMV.visitEnd(); } this.cv.visitEnd(); }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(ClassDeclarationNode classDecl) { // prolog/*from w ww . j av a2 s.c o m*/ cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); programName = classDecl.getID().getName(); fullPath = classPackage + "/" + classDecl.getID().getName(); cw.visit(V1_8, ACC_PUBLIC + ACC_STATIC, fullPath, null, "java/lang/Object", null); cw.visitSource(classDecl.getID().getName() + ".java", null); FieldVisitor fv; List<Symbol> classSymbols = new ArrayList<Symbol>(); classSymbols.addAll(SymbolTableManager.getInstance().getClassSymbolManager(programName) .getCurrentSymbolTable().getSymbols()); for (ASTNode property : classDecl.getProperties()) { classSymbols.add(((PropertyNode) property).getID().symbol); } for (Symbol symbol : classSymbols) { String type; if (symbol.getType() == DijkstraType.ARRAY) { ArraySymbol as = (ArraySymbol) symbol; if (as.getArrayType() == DijkstraType.INT) { type = "[J"; } else if (as.getArrayType() == DijkstraType.FLOAT) { type = "[D"; } else { type = "[Z"; } } else { if (symbol.getType() == DijkstraType.INT) { type = "J"; } else if (symbol.getType() == DijkstraType.FLOAT) { type = "D"; } else { type = "Z"; } } fv = cw.visitField(ACC_PUBLIC, symbol.getId(), type, null, null); fv.visitEnd(); } final StringBuilder sig = new StringBuilder(); sig.append('('); for (Symbol property : ((ClassSymbol) classDecl.getID().symbol).getPropertySymbols()) { if (property.getType() == DijkstraType.INT) { sig.append('J'); } else if (property.getType() == DijkstraType.FLOAT) { sig.append('D'); } else { sig.append('Z'); } } sig.append(")V"); mvStack.push(cw.visitMethod(ACC_PUBLIC, "<init>", sig.toString(), null, null)); MethodVisitor mv = mvStack.peek(); mv.visitCode(); final Label startLabel = new Label(); mv.visitLabel(startLabel); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); int localAddr = 1; for (Symbol param : ((ClassSymbol) classDecl.getID().symbol).getPropertySymbols()) { mv.visitVarInsn(ALOAD, 0); if (param.getType() == DijkstraType.INT) { mv.visitVarInsn(LLOAD, localAddr); mv.visitFieldInsn(PUTFIELD, fullPath, param.getId(), "J"); localAddr += 2; } else if (param.getType() == DijkstraType.FLOAT) { mv.visitVarInsn(DLOAD, localAddr); mv.visitFieldInsn(PUTFIELD, fullPath, param.getId(), "D"); localAddr += 2; } else { mv.visitVarInsn(ILOAD, localAddr); mv.visitFieldInsn(PUTFIELD, fullPath, param.getId(), "Z"); localAddr += 1; } } processingClassFields = true; inClass = true; for (ASTNode body : classDecl.getDeclarations()) { body.accept(this); } inClass = false; processingClassFields = false; mv.visitInsn(RETURN); final Label endLabel = new Label(); mv.visitLabel(endLabel); mv.visitLocalVariable("this", "L" + fullPath + ";", null, startLabel, endLabel, 0); int paramLoc = 1; for (Symbol param : ((ClassSymbol) classDecl.getID().symbol).getPropertySymbols()) { if (param.getType() == DijkstraType.INT) { mv.visitLocalVariable(param.getId(), "J", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else if (param.getType() == DijkstraType.FLOAT) { mv.visitLocalVariable(param.getId(), "D", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else { mv.visitLocalVariable(param.getId(), "Z", null, startLabel, endLabel, paramLoc); paramLoc += 1; } } mv.visitMaxs(0, 0); mv.visitEnd(); mvStack.pop(); inClass = true; for (ASTNode body : classDecl.getDeclarations()) { body.accept(this); } inClass = false; // Actual end of generation cw.visitEnd(); return cw.toByteArray(); }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
/** * Generate the program prolog, then visit the children, then generate * the program end.// w w w . j a va2 s . co m * @see dijkstra.ast.ASTVisitor#visit(dijkstra.ast.ASTNodeFactory.ProgramNode) */ public byte[] visit(ProgramNode program) { // prolog cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); programName = program.programName; fullPath = classPackage + "/" + program.programName; cw.visit(V1_8, ACC_PUBLIC + ACC_STATIC, fullPath, null, "java/lang/Object", null); cw.visitSource(program.programName + ".java", null); FieldVisitor fv; for (Symbol symbol : SymbolTableManager.getInstance().getCurrentSymbolTable().getSymbols()) { String type; if (symbol.getType() == DijkstraType.ARRAY) { ArraySymbol as = (ArraySymbol) symbol; if (as.getArrayType() == DijkstraType.INT) { type = "[J"; } else if (as.getArrayType() == DijkstraType.FLOAT) { type = "[D"; } else if (as.getArrayType() == DijkstraType.BOOLEAN) { type = "[Z"; } else { type = "L" + classPackage + "/" + ((ObjectSymbol) symbol).getObjectType() + ";"; } } else { if (symbol.getType() == DijkstraType.INT) { type = "J"; } else if (symbol.getType() == DijkstraType.FLOAT) { type = "D"; } else if (symbol.getType() == DijkstraType.BOOLEAN) { type = "Z"; } else { type = "L" + classPackage + "/" + ((ObjectSymbol) symbol).getObjectType() + ";"; } } fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, symbol.getId(), type, null, null); fv.visitEnd(); } mvStack.push(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null)); MethodVisitor mv = mvStack.peek(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); mvStack.pop(); // Start the main() method mvStack.push(cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null)); mv = mvStack.peek(); mv.visitCode(); visitChildren(program); // program end // End of main mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); // Actual end of generation cw.visitEnd(); return cw.toByteArray(); }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(AttributeNode node) { final MethodVisitor mv = mvStack.peek(); final String objectType = ((ObjectSymbol) node.getObj().symbol).getObjectType(); if (reading) { if (node.getObj().symbol.isGlobal() && !inClass) { mv.visitFieldInsn(GETSTATIC, fullPath, node.getObj().getName(), "L" + classPackage + "/" + ((ObjectSymbol) node.getObj().symbol).getObjectType() + ";"); } else if (node.getObj().symbol.isGlobal() && inClass) { mv.visitFieldInsn(GETFIELD, fullPath, node.getObj().getName(), "L" + classPackage + "/" + ((ObjectSymbol) node.getObj().symbol).getObjectType() + ";"); } else {// w w w . j av a 2 s. c o m mv.visitVarInsn(ALOAD, node.getObj().getAddress()); } if (node.getType() == DijkstraType.INT) { mv.visitFieldInsn(GETFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "J"); } else if (node.getType() == DijkstraType.FLOAT) { mv.visitFieldInsn(GETFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "D"); } else if (node.getType() == DijkstraType.BOOLEAN) { mv.visitFieldInsn(GETFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "Z"); } else { mv.visitFieldInsn(GETFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "L" + classPackage + "/" + ((ObjectSymbol) node.getObj().symbol).getObjectType() + ";"); } } else { if (node.getType() == DijkstraType.INT) { mv.visitFieldInsn(PUTFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "J"); } else if (node.getType() == DijkstraType.FLOAT) { mv.visitFieldInsn(PUTFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "D"); } else if (node.getType() == DijkstraType.BOOLEAN) { mv.visitFieldInsn(PUTFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "Z"); } else { mv.visitFieldInsn(PUTFIELD, classPackage + "/" + objectType, node.getAttribute().getName(), "L" + classPackage + "/" + ((ObjectSymbol) node.getObj().symbol).getObjectType() + ";"); } } return null; }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(VariableDeclarationNode decl) { if (!mvStack.isEmpty() && !processingClassFields) { final MethodVisitor mv = mvStack.peek(); for (IDNode id : decl.getIdList()) { if (id.getType() == DijkstraType.INT) { mv.visitLdcInsn(new Long(0)); if (id.symbol.isGlobal()) { mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "J"); } else { mv.visitVarInsn(LSTORE, id.getAddress()); }//w w w . ja v a 2s. com } else if (id.getType() == DijkstraType.FLOAT) { mv.visitLdcInsn(new Double(0)); if (id.symbol.isGlobal()) { mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "D"); } else { mv.visitVarInsn(DSTORE, id.getAddress()); } } else if (id.getType() == DijkstraType.BOOLEAN) { // boolean mv.visitInsn(ICONST_0); if (id.symbol.isGlobal()) { mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "Z"); } else { mv.visitVarInsn(ISTORE, id.getAddress()); } } } } else if (processingClassFields) { final MethodVisitor mv = mvStack.peek(); } return null; }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(ArrayDeclarationNode decl) { if (!mvStack.isEmpty() && !processingClassFields) { final MethodVisitor mv = mvStack.peek(); for (IDNode id : decl.getIdList()) { decl.arrayLength.accept(this); if (decl.arrayLength.type == DijkstraType.INT) { mv.visitInsn(L2I);//from ww w .j a va2 s . c om } else { mv.visitInsn(D2I); } ArraySymbol symbol = ((ArraySymbol) id.symbol); if (id.symbol.isGlobal()) { if (symbol.getArrayType() == DijkstraType.INT) { mv.visitIntInsn(NEWARRAY, T_LONG); mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "[J"); } else if (symbol.getArrayType() == DijkstraType.FLOAT) { mv.visitIntInsn(NEWARRAY, T_DOUBLE); mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "[D"); } else if (symbol.getArrayType() == DijkstraType.BOOLEAN) { mv.visitIntInsn(NEWARRAY, T_BOOLEAN); mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "[Z"); } } else { if (symbol.getArrayType() == DijkstraType.INT) { mv.visitIntInsn(NEWARRAY, T_LONG); mv.visitVarInsn(ASTORE, id.getAddress()); } else if (symbol.getArrayType() == DijkstraType.FLOAT) { mv.visitIntInsn(NEWARRAY, T_DOUBLE); mv.visitVarInsn(ASTORE, id.getAddress()); } else if (symbol.getArrayType() == DijkstraType.BOOLEAN) { // boolean mv.visitIntInsn(NEWARRAY, T_BOOLEAN); mv.visitVarInsn(ASTORE, id.getAddress()); } } } } else if (processingClassFields) { final MethodVisitor mv = mvStack.peek(); for (IDNode id : decl.getIdList()) { mv.visitVarInsn(ALOAD, 0); decl.arrayLength.accept(this); if (decl.arrayLength.type == DijkstraType.INT) { mv.visitInsn(L2I); } else { mv.visitInsn(D2I); } ArraySymbol symbol = ((ArraySymbol) id.symbol); if (symbol.getArrayType() == DijkstraType.INT) { mv.visitIntInsn(NEWARRAY, T_LONG); mv.visitFieldInsn(PUTFIELD, fullPath, id.getName(), "[J"); } else if (symbol.getArrayType() == DijkstraType.FLOAT) { mv.visitIntInsn(NEWARRAY, T_DOUBLE); mv.visitFieldInsn(PUTFIELD, fullPath, id.getName(), "[D"); } else if (symbol.getArrayType() == DijkstraType.BOOLEAN) { mv.visitIntInsn(NEWARRAY, T_BOOLEAN); mv.visitFieldInsn(PUTFIELD, fullPath, id.getName(), "[Z"); } } } return null; }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
/** * Call the runtime input for the ID and then store the result in the * @see dijkstra.ast.ASTVisitor#visit(dijkstra.ast.ASTNodeFactory.InputNode) *//*from w w w .j a va2 s . c o m*/ public byte[] visit(InputNode input) { final MethodVisitor mv = mvStack.peek(); for (IDNode id : input.getIDs()) { mv.visitLdcInsn(id.token.getText()); // Name of the variable if (id.getType() == DijkstraType.INT) { mv.visitMethodInsn(INVOKESTATIC, "dijkstra/runtime/DijkstraRuntime", "inputLong", "(Ljava/lang/String;)J", false); if (id.symbol.isGlobal()) { mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "J"); } else { mv.visitVarInsn(LSTORE, id.getAddress()); } } else if (id.getType() == DijkstraType.FLOAT) { mv.visitMethodInsn(INVOKESTATIC, "dijkstra/runtime/DijkstraRuntime", "inputDouble", "(Ljava/lang/String;)D", false); if (id.symbol.isGlobal()) { mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "D"); } else { mv.visitVarInsn(DSTORE, id.getAddress()); } } else { mv.visitMethodInsn(INVOKESTATIC, "dijkstra/runtime/DijkstraRuntime", "inputBoolean", "(Ljava/lang/String;)Z", false); if (id.symbol.isGlobal()) { mv.visitFieldInsn(PUTSTATIC, fullPath, id.getName(), "Z"); } else { mv.visitVarInsn(ISTORE, id.getAddress()); } } } return null; }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(ArrayAccessorNode arrayAccessor) { final MethodVisitor mv = mvStack.peek(); final DijkstraType arrayType = ((ArraySymbol) arrayAccessor.getId().symbol).getArrayType(); if (reading) { if (arrayAccessor.getId().symbol.isGlobal() && !inClass) { if (arrayType == DijkstraType.INT) { mv.visitFieldInsn(GETSTATIC, fullPath, arrayAccessor.getId().getName(), "[J"); } else if (arrayType == DijkstraType.FLOAT) { mv.visitFieldInsn(GETSTATIC, fullPath, arrayAccessor.getId().getName(), "[D"); } else { mv.visitFieldInsn(GETSTATIC, fullPath, arrayAccessor.getId().getName(), "[Z"); }/*w w w .j a v a2 s . c o m*/ } else if (arrayAccessor.getId().symbol.isGlobal() && inClass) { if (arrayType == DijkstraType.INT) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, fullPath, arrayAccessor.getId().getName(), "[J"); } else if (arrayType == DijkstraType.FLOAT) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, fullPath, arrayAccessor.getId().getName(), "[D"); } else { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, fullPath, arrayAccessor.getId().getName(), "[Z"); } } else { mv.visitVarInsn(ALOAD, arrayAccessor.getId().getAddress()); } arrayAccessor.getExpression().accept(this); if (arrayAccessor.getExpression().getType() == DijkstraType.FLOAT) { mv.visitInsn(D2I); } else { mv.visitInsn(L2I); } if (arrayType == DijkstraType.INT) { mv.visitInsn(LALOAD); } else if (arrayType == DijkstraType.FLOAT) { mv.visitInsn(DALOAD); } else { mv.visitInsn(BALOAD); } } else { if (arrayType == DijkstraType.INT) { mv.visitInsn(LASTORE); } else if (arrayType == DijkstraType.FLOAT) { mv.visitInsn(DASTORE); } else { mv.visitInsn(BASTORE); } } return null; }