List of usage examples for org.objectweb.asm Opcodes GETFIELD
int GETFIELD
To view the source code for org.objectweb.asm Opcodes GETFIELD.
Click Source Link
From source file:jpcsp.Allegrex.compiler.CompilerContext.java
License:Open Source License
@Override public void prepareVcrCcForStore(int cc) { if (preparedRegisterForStore < 0) { loadVcr();/*w w w. j a v a2 s . co m*/ mv.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(Vcr.class), "cc", "[Z"); loadImm(cc); preparedRegisterForStore = cc; } }
From source file:jpcsp.Allegrex.compiler.CompilerContext.java
License:Open Source License
@Override public void storeVcrCc(int cc) { if (preparedRegisterForStore == cc) { mv.visitInsn(Opcodes.BASTORE);//from w w w .j a v a2 s .c o m preparedRegisterForStore = -1; } else { loadVcr(); mv.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(Vcr.class), "cc", "[Z"); mv.visitInsn(Opcodes.SWAP); loadImm(cc); mv.visitInsn(Opcodes.SWAP); mv.visitInsn(Opcodes.BASTORE); } }
From source file:jpcsp.Allegrex.compiler.CompilerContext.java
License:Open Source License
private void startPfxCompiled(VfpuPfxState vfpuPfxState, String name, String descriptor, String internalName, boolean isFloat) { if (vfpuPfxState.isUnknown()) { if (interpretPfxLabel == null) { interpretPfxLabel = new Label(); }//from w w w .jav a 2s. c om loadVcr(); mv.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(Vcr.class), name, descriptor); mv.visitFieldInsn(Opcodes.GETFIELD, internalName, "enabled", "Z"); mv.visitJumpInsn(Opcodes.IFNE, interpretPfxLabel); } }
From source file:lapin.comp.asm.ASMByteCodeGenerator.java
License:Open Source License
private void generateCall(CallableInfo ci, Env env) { /*/*from w w w .ja v a2s .com*/ * local variables * <Callable#call> * 0: this * 1: args (list of arguments) * 2: env * * <Callable0#call0> * 0: this * 1: env * * <Callable1#call1> * 0: this * 1: arg0 * 2: env * * <Callable2#call2> * 0: this * 1: arg0 * 2: arg1 * 3: env * * ... * */ MethodVisitor mv = _cw.visitMethod( ci.mi.implCallable() ? Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL : Opcodes.ACC_FINAL, ci.mi.name(), Type.getMethodDescriptor(ci.retType, ci.paramTypes), null, null); // instruction list int len = ci.mi.instLen(); // label Label label = null; // generate code for (int i = 0; i < len; i++) { Object inst = ci.mi.getInst(i); if (Logger.tracelevelp(env)) Logger.trace("[asm:gen]" + i + ":\t~S", Lists.list(inst), env); // inst is symbol // -> convert tag (Symbol) to label (ASMe Label object) if (Data.isSymbol(inst)) { Symbol tag = Data.symbol(inst); Label l = (Label) ci.labelTable.get(tag); if (l == null) { throw new NotReachedException("label is null: ~S.", Lists.list(tag)); } else if (l != label) { mv.visitLabel(l); label = l; if (Logger.tracelevelp(env)) Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S", Lists.list(tag, l), env); } else { if (Logger.tracelevelp(env)) Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S" + " (dup)", Lists.list(tag, l), env); } continue; } // inst must be the form of (id <arg1> <arg2> ....) Object id = Lists.car(inst); if (id == Insts.CONST) { /* push const on the stack. */ Object obj = Lists.cadr(inst); String val = Data.string(constTable.get(obj)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val, TYPE_OBJECT.getDescriptor()); } else if (id == Insts.VAR) { /* push var on the stack */ Object var = Lists.cadr(inst); String val = Data.string(varTable.get(var)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val, TYPE_SYMBOL.getDescriptor()); } else if (id == Insts.LAMBDA_LIST) { /* push lambdaList on the stack */ Object var = Lists.cadr(inst); /* push _ll_<i> on the stack */ String val = Data.string(llTable.get(var)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val, TYPE_LAMBDA_LIST.getDescriptor()); } else if (id == Insts.ENV_GET) { /* env.get */ Class type = Data.javaClass(Lists.cadr(inst)); if (type.equals(int.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getInt", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL })); } else if (type.equals(double.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getDouble", Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL })); } else if (type.equals(char.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getChar", Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL })); } else if (Object.class.isAssignableFrom(type)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "get", Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL })); } else { throw new NotReachedException("unsupported type: ~S.", Lists.list(type)); } } else if (id == Insts.ENV_SET) { /* env.set */ Class type = Data.javaClass(Lists.cadr(inst)); if (type.equals(int.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE })); } else if (type.equals(double.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE })); } else if (type.equals(char.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE })); } else if (Object.class.isAssignableFrom(type)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT })); } else { throw new NotReachedException("unsupported type: ~S.", Lists.list(type)); } } else if (id == Insts.ENV_BIND) { /* env.bind */ Class type = Data.javaClass(Lists.cadr(inst)); if (type.equals(int.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE })); } else if (type.equals(double.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE })); } else if (type.equals(char.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE })); } else if (Object.class.isAssignableFrom(type)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT })); } else { throw new NotReachedException("unsupported type: ~S.", Lists.list(type)); } } else if (id == Insts.ENV_UNBIND) { /* env.unbind */ Class type = Data.javaClass(Lists.cadr(inst)); if (type.equals(int.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindInt", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL })); } else if (type.equals(double.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindDouble", Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL })); } else if (type.equals(char.class)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindChar", Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL })); } else if (Object.class.isAssignableFrom(type)) { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbind", Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL })); } else { throw new NotReachedException("unsupported type: ~S.", Lists.list(type)); } } else if (id == Insts.ENV_CHILD) { /* env.child */ Object oldEnvVar = Lists.cadr(inst); Object newEnvVar = Lists.caddr(inst); Object oldSlot = Lists.cadr(oldEnvVar); Object newSlot = Lists.cadr(newEnvVar); int oldLocal = Data.fixnum(ci.localTable.get(oldSlot)).intValue(); int newLocal = Data.fixnum(ci.localTable.get(newSlot)).intValue(); if (Logger.tracelevelp(env)) Logger.trace("[asm:gen]" + i + ":\tenv-child: local ~S -> ~S", Lists.list(Data.toFixnum(oldLocal), Data.toFixnum(newLocal)), env); mv.visitVarInsn(Opcodes.ALOAD, oldLocal); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "child", Type.getMethodDescriptor(TYPE_ENV, TYPE_NO_ARGS)); mv.visitVarInsn(Opcodes.ASTORE, newLocal); } else if (id == Insts.CALL) { /* funcall */ int nargs = Data.fixnum(Lists.cadr(inst)).intValue(); String className = "lapin.eval.Funcall"; String methodName = nargs < 0 ? "funcall" : "funcall" + nargs; Class rType = Object.class; Class[] pTypes; if (nargs < 0) { pTypes = new Class[] { Function.class, Object.class, // list of args Env.class }; } else { pTypes = new Class[nargs + 2]; pTypes[0] = Function.class; for (int j = 0; j < nargs; j++) { pTypes[j + 1] = Object.class; } pTypes[nargs + 1] = Env.class; } Type retType = Type.getType(rType); Type[] paramTypes = new Type[pTypes.length]; for (int j = 0; j < pTypes.length; j++) paramTypes[j] = Type.getType(pTypes[j]); mv.visitMethodInsn(Opcodes.INVOKESTATIC, toInternalName(className), methodName, Type.getMethodDescriptor(retType, paramTypes)); } else if (id == Insts.CALL_DIRECT) { /* * public class Foo * extends CompiledExpr implements Callable2 { * public Object call2(Object arg0, Object arg1, Env env) { * ... * } * } */ MethodInfo mi = (MethodInfo) Lists.cadr(inst); String className = mi.classInfo().classname(); int nargs = mi.nargs(); boolean rest = mi.rest(); String methodName = mi.name(); Class rType = mi.retType(); Class[] pTypes = mi.paramTypes(); Type retType = Type.getType(rType); Type[] paramTypes = new Type[pTypes.length]; for (int j = 0; j < pTypes.length; j++) paramTypes[j] = Type.getType(pTypes[j]); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, toInternalName(className), methodName, Type.getMethodDescriptor(retType, paramTypes)); } else if (id == Insts.COMPILED_EXPR) { /* * public class Foo extends CompiledExpr { * static public Foo SELF; * ... * } */ String className = Data.string(Lists.cadr(inst)); String fieldName = "SELF"; String typeName = className; mv.visitFieldInsn(Opcodes.GETSTATIC, toInternalName(className), fieldName, toTypeDescriptor(typeName)); } else if (id == Insts.RETURN) { /* return */ Class type = Data.javaClass(Lists.cadr(inst)); if (type.equals(int.class) || type.equals(short.class) || type.equals(byte.class) || type.equals(char.class)) { mv.visitInsn(Opcodes.IRETURN); } else if (type.equals(long.class)) { mv.visitInsn(Opcodes.LRETURN); } else if (type.equals(float.class)) { mv.visitInsn(Opcodes.FRETURN); } else if (type.equals(double.class)) { mv.visitInsn(Opcodes.DRETURN); } else if (type.equals(void.class)) { //mv.visitInsn(Opcodes.RETURN); throw new NotReachedException("unsupported returnType: ~S.", Lists.list(type)); } else { mv.visitInsn(Opcodes.ARETURN); } } else if (id == Insts.IFEQ) { /* conditional jump */ Symbol tag = Data.symbol(Lists.cadr(inst)); Label l = (Label) ci.labelTable.get(tag); if (l == null) { throw new NotReachedException("label not found: ~S.", Lists.list(tag)); } mv.visitJumpInsn(Opcodes.IFEQ, l); } else if (id == Insts.IFNE) { /* conditional jump */ Symbol tag = Data.symbol(Lists.cadr(inst)); Label l = (Label) ci.labelTable.get(tag); if (l == null) { throw new NotReachedException("label not found: ~S.", Lists.list(tag)); } mv.visitJumpInsn(Opcodes.IFNE, l); } else if (id == Insts.GOTO) { /* jump */ Symbol tag = Data.symbol(Lists.cadr(inst)); Label l = (Label) ci.labelTable.get(tag); if (l == null) { throw new NotReachedException("label not found: ~S.", Lists.list(tag)); } mv.visitJumpInsn(Opcodes.GOTO, l); } else if (id == Insts.LOAD) { /* local -> stack */ Object localVar = Lists.cadr(inst); Object slot = Lists.cadr(localVar); Class type = Data.javaClass(Lists.caddr(localVar)); int local = Data.fixnum(ci.localTable.get(slot)).intValue(); int op = Type.getType(type).getOpcode(Opcodes.ILOAD); if (Logger.tracelevelp(env)) Logger.trace("[asm:gen]" + i + ":\tload: local=~S type=~S", Lists.list(Data.toFixnum(local), type), env); mv.visitVarInsn(op, local); } else if (id == Insts.STORE) { /* stack -> local */ Object localVar = Lists.cadr(inst); Object slot = Lists.cadr(localVar); Class type = Data.javaClass(Lists.caddr(localVar)); int local = Data.fixnum(ci.localTable.get(slot)).intValue(); int op = Type.getType(type).getOpcode(Opcodes.ISTORE); if (Logger.tracelevelp(env)) Logger.trace("[asm:gen]" + i + ":\tstore: local=~S type=~S", Lists.list(Data.toFixnum(local), type), env); mv.visitVarInsn(op, local); } else if (id == Insts.POP) { /* pop a value and discard it */ Class type = Data.javaClass(Lists.cadr(inst)); int op; switch (Classes.sizeOf(type)) { case 1: op = Opcodes.POP; break; case 2: op = Opcodes.POP2; break; default: throw new NotReachedException("unsupported type: ~S.", Lists.list(type)); } mv.visitInsn(op); } else if (id == Insts.DUP) { /* peek a value and duplicate it */ Class type = Data.javaClass(Lists.cadr(inst)); int op; switch (Classes.sizeOf(type)) { case 1: op = Opcodes.DUP; break; case 2: op = Opcodes.DUP2; break; default: throw new NotReachedException("unsupported type: ~S.", Lists.list(type)); } mv.visitInsn(op); } else if (id == Insts.PUSH) { /* push a constant */ Object val = Lists.cadr(inst); if (Data.isJavaBoolean(val)) { if (Data.javaBoolean(val).booleanValue()) mv.visitInsn(Opcodes.ICONST_1); else mv.visitInsn(Opcodes.ICONST_0); } else if (val instanceof Byte || val instanceof Short || val instanceof Integer) { int n = Data.javaNumber(val).intValue(); if (n == -1) mv.visitInsn(Opcodes.ICONST_M1); else if (n == 0) mv.visitInsn(Opcodes.ICONST_0); else if (n == 1) mv.visitInsn(Opcodes.ICONST_1); else if (n == 2) mv.visitInsn(Opcodes.ICONST_2); else if (n == 3) mv.visitInsn(Opcodes.ICONST_3); else if (n == 4) mv.visitInsn(Opcodes.ICONST_4); else if (n == 5) mv.visitInsn(Opcodes.ICONST_5); else if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE) mv.visitIntInsn(Opcodes.BIPUSH, n); else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE) mv.visitIntInsn(Opcodes.SIPUSH, n); else mv.visitLdcInsn(Data.toFixnum(n)); } else if (val instanceof Long) { long n = Data.javaNumber(val).longValue(); if (n == 0L) mv.visitInsn(Opcodes.LCONST_0); else if (n == 1L) mv.visitInsn(Opcodes.LCONST_1); else mv.visitLdcInsn(val); } else if (val instanceof Float) { float n = Data.javaNumber(val).floatValue(); if (n == 0.0f) mv.visitInsn(Opcodes.FCONST_0); else if (n == 1.0f) mv.visitInsn(Opcodes.FCONST_1); else if (n == 2.0f) mv.visitInsn(Opcodes.FCONST_2); else mv.visitLdcInsn(val); } else if (val instanceof Double) { double n = Data.javaNumber(val).doubleValue(); if (n == 0.0) mv.visitInsn(Opcodes.DCONST_0); else if (n == 1.0) mv.visitInsn(Opcodes.DCONST_1); else mv.visitLdcInsn(val); } else if (Data.isCharacter(val)) { Character c = Data.character(val); int n = (int) c.charValue(); if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE) mv.visitIntInsn(Opcodes.BIPUSH, n); else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE) mv.visitIntInsn(Opcodes.SIPUSH, n); else mv.visitLdcInsn(Data.toFixnum(n)); } else if (Data.isString(val)) { mv.visitLdcInsn(val); } else { throw new NotReachedException("cannot push: ~S.", Lists.list(val)); } } else if (id == Insts.GET) { Field f = Data.javaField(Lists.cadr(inst)); String fieldName = f.getName(); Class c = f.getDeclaringClass(); String className = c.getName(); Class t = f.getType(); String typeName = t.getName(); boolean isStatic = Classes.isStatic(f); int op = isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD; mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName)); } else if (id == Insts.PUT) { Field f = Data.javaField(Lists.cadr(inst)); String fieldName = f.getName(); Class c = f.getDeclaringClass(); String className = c.getName(); Class t = f.getType(); String typeName = t.getName(); boolean isStatic = Classes.isStatic(f); int op = isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD; mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName)); } else if (id == Insts.INVOKE) { Method m = Data.javaMethod(Lists.cadr(inst)); String methodName = m.getName(); Class c = m.getDeclaringClass(); String className = c.getName(); Class rType = m.getReturnType(); Class[] pTypes = m.getParameterTypes(); if (rType.equals(void.class)) { throw new NotReachedException("unsupported returnType: ~S.", Lists.list(rType)); } Type retType = Type.getType(rType); Type[] paramTypes = new Type[pTypes.length]; for (int j = 0; j < pTypes.length; j++) paramTypes[j] = Type.getType(pTypes[j]); boolean isStatic = Classes.isStatic(m); boolean isInterface = c.isInterface(); int op = isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL; mv.visitMethodInsn(op, toInternalName(className), methodName, Type.getMethodDescriptor(retType, paramTypes)); } else if (id == Insts.CHECKCAST) { Class c = Data.javaClass(Lists.cadr(inst)); Type t = Type.getType(c); mv.visitTypeInsn(Opcodes.CHECKCAST, t.getInternalName()); } else if (id == Insts.THROW) { mv.visitInsn(Opcodes.ATHROW); } else if (id == Insts.CATCH) { Symbol tagS = Data.symbol(Lists.cadr(inst)); Symbol tagE = Data.symbol(Lists.caddr(inst)); Symbol tagH = Data.symbol(Lists.cadddr(inst)); String className; if (Lists.isEnd(Lists.cddddr(inst))) { className = null; } else { Class c = Data.javaClass(Lists.car(Lists.cddddr(inst))); className = toInternalName(c.getName()); } Label labelS = (Label) ci.labelTable.get(tagS); if (labelS == null) { throw new NotReachedException("label not found: ~S.", Lists.list(tagS)); } Label labelE = (Label) ci.labelTable.get(tagE); if (labelE == null) { throw new NotReachedException("label not found: ~S.", Lists.list(tagE)); } Label labelH = (Label) ci.labelTable.get(tagH); if (labelH == null) { throw new NotReachedException("label not found: ~S.", Lists.list(tagH)); } mv.visitTryCatchBlock(labelS, labelE, labelH, className); } //else if (id == Insts.CATCH_FROM || // id == Insts.CATCH_TO || // id == Insts.CATCH_HANDLER) { // /* nothing emitted */ // continue; //} else { throw new NotReachedException("unknown inst: ~S.", Lists.list(inst)); } } mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:local.edg.ClassDbVisitor.java
License:Open Source License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { currentMethod.setEmpty(false);/*from w w w . j ava2s . c o m*/ Class c; Field f; switch (opcode) { case Opcodes.GETFIELD: case Opcodes.GETSTATIC: c = getClass(owner); f = getField(c, name); f.setDesc(desc); // read after write is not a real read // if (!currentMethod.hasWrite(f)){ currentMethod.addRead(f); // } break; case Opcodes.PUTFIELD: case Opcodes.PUTSTATIC: c = getClass(owner); f = getField(c, name); f.setDesc(desc); currentMethod.addWrite(f); break; } }
From source file:lucee.transformer.bytecode.BytecodeFactory.java
License:Open Source License
@Override public void registerKey(Context c, Expression name, boolean doUpperCase) throws TransformerException { BytecodeContext bc = (BytecodeContext) c; if (name instanceof Literal) { Literal l = (Literal) name;/* ww w.j a v a 2 s .c om*/ LitString ls = name instanceof LitString ? (LitString) l : c.getFactory().createLitString(l.getString()); if (doUpperCase) { ls = ls.duplicate(); ls.upperCase(); } String key = KeyConstants.getFieldName(ls.getString()); if (key != null) { bc.getAdapter().getStatic(KEY_CONSTANTS, key, Types.COLLECTION_KEY); return; } int index = bc.registerKey(ls); bc.getAdapter().visitVarInsn(Opcodes.ALOAD, 0); bc.getAdapter().visitFieldInsn(Opcodes.GETFIELD, bc.getClassName(), "keys", Types.COLLECTION_KEY_ARRAY.toString()); bc.getAdapter().push(index); bc.getAdapter().visitInsn(Opcodes.AALOAD); //ExpressionUtil.writeOutSilent(lit,bc, Expression.MODE_REF); //bc.getAdapter().invokeStatic(Page.KEY_IMPL, Page.KEY_INTERN); return; } name.writeOut(bc, Expression.MODE_REF); bc.getAdapter().invokeStatic(Page.KEY_IMPL, INIT); //bc.getAdapter().invokeStatic(Types.CASTER, TO_KEY); return; }
From source file:lucee.transformer.bytecode.expression.var.Variable.java
License:Open Source License
public static void registerKey(BytecodeContext bc, Expression name, boolean doUpperCase) throws BytecodeException { if (name instanceof Literal) { Literal l = (Literal) name;/* w w w. j a v a 2 s . co m*/ LitString ls = name instanceof LitString ? (LitString) l : LitString.toLitString(l.getString()); if (doUpperCase) { ls = ls.duplicate(); ls.upperCase(); } String key = KeyConstants.getFieldName(ls.getString()); if (key != null) { bc.getAdapter().getStatic(KEY_CONSTANTS, key, Types.COLLECTION_KEY); return; } int index = bc.registerKey(ls); bc.getAdapter().visitVarInsn(Opcodes.ALOAD, 0); bc.getAdapter().visitFieldInsn(Opcodes.GETFIELD, bc.getClassName(), "keys", Types.COLLECTION_KEY_ARRAY.toString()); bc.getAdapter().push(index); bc.getAdapter().visitInsn(Opcodes.AALOAD); return; } name.writeOut(bc, MODE_REF); bc.getAdapter().invokeStatic(Page.KEY_IMPL, INIT); //bc.getAdapter().invokeStatic(Types.CASTER, TO_KEY); return; }
From source file:lucee.transformer.bytecode.Page.java
License:Open Source License
/** * result byte code as binary array//from www . ja va 2s . com * @param classFile * @return byte code * @throws IOException * @throws TemplateException */ public byte[] execute(PageSource source, Resource classFile) throws BytecodeException { /* // this is done that the Page can be executed more than once if(initFunctions==null) initFunctions=(ArrayList<IFunction>) functions.clone(); else functions=initFunctions; if(initThreads==null) initThreads=(ArrayList<TagThread>) threads.clone(); else threads=initThreads; methodCount=0; off=0; staticTextLocation=null; print.e(this.functions); print.e(this.threads);*/ Resource p = classFile.getParentResource().getRealResource(classFile.getName() + ".txt"); List<LitString> keys = new ArrayList<LitString>(); ClassWriter cw = ASMUtil.getClassWriter(); ArrayList<String> imports = new ArrayList<String>(); getImports(imports, this); // parent String parent = "lucee/runtime/PagePlus"; if (isComponent()) parent = "lucee/runtime/ComponentPage"; else if (isInterface()) parent = "lucee/runtime/InterfacePage"; cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, name, null, parent, null); cw.visitSource(this.pageSource.getPhyscalFile().getAbsolutePath(), "rel:" + this.pageSource.getFullRealpath()); // when adding more use ; as delimiter // // static constructor //GeneratorAdapter statConstrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,STATIC_CONSTRUCTOR,null,null,cw); BytecodeContext statConstr = null;//new BytecodeContext(null,null,this,externalizer,keys,cw,name,statConstrAdapter,STATIC_CONSTRUCTOR,writeLog(),suppressWSbeforeArg); // constructor GeneratorAdapter constrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_PS, null, null, cw); BytecodeContext constr = new BytecodeContext(source, null, null, this, keys, cw, name, constrAdapter, CONSTRUCTOR_PS, writeLog(), suppressWSbeforeArg, output); constrAdapter.loadThis(); Type t = Types.PAGE_PLUS; if (isComponent()) t = Types.COMPONENT_PAGE; else if (isInterface()) t = Types.INTERFACE_PAGE; constrAdapter.invokeConstructor(t, CONSTRUCTOR); // call _init() constrAdapter.visitVarInsn(Opcodes.ALOAD, 0); constrAdapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, constr.getClassName(), "initKeys", "()V"); // private static ImportDefintion[] test=new ImportDefintion[]{...}; { FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "imports", "[Llucee/runtime/component/ImportDefintion;", null, null); fv.visitEnd(); constrAdapter.visitVarInsn(Opcodes.ALOAD, 0); ArrayVisitor av = new ArrayVisitor(); av.visitBegin(constrAdapter, Types.IMPORT_DEFINITIONS, imports.size()); int index = 0; Iterator<String> it = imports.iterator(); while (it.hasNext()) { av.visitBeginItem(constrAdapter, index++); constrAdapter.push(it.next()); ASMConstants.NULL(constrAdapter); constrAdapter.invokeStatic(Types.IMPORT_DEFINITIONS_IMPL, ID_GET_INSTANCE); av.visitEndItem(constrAdapter); } av.visitEnd(); constrAdapter.visitFieldInsn(Opcodes.PUTFIELD, name, "imports", "[Llucee/runtime/component/ImportDefintion;"); } // getVersion GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, VERSION, null, null, cw); adapter.push(version); adapter.returnValue(); adapter.endMethod(); // public ImportDefintion[] getImportDefintions() if (imports.size() > 0) { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, GET_IMPORT_DEFINITIONS, null, null, cw); adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitFieldInsn(Opcodes.GETFIELD, name, "imports", "[Llucee/runtime/component/ImportDefintion;"); adapter.returnValue(); adapter.endMethod(); } else { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, GET_IMPORT_DEFINITIONS, null, null, cw); adapter.visitInsn(Opcodes.ICONST_0); adapter.visitTypeInsn(Opcodes.ANEWARRAY, "lucee/runtime/component/ImportDefintion"); adapter.returnValue(); adapter.endMethod(); } // getSourceLastModified adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, LAST_MOD, null, null, cw); adapter.push(lastModifed); adapter.returnValue(); adapter.endMethod(); // getCompileTime adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, COMPILE_TIME, null, null, cw); adapter.push(System.currentTimeMillis()); adapter.returnValue(); adapter.endMethod(); // newInstance/initComponent/call if (isComponent()) { Tag component = getComponent(); writeOutNewComponent(statConstr, constr, keys, cw, component); writeOutInitComponent(statConstr, constr, keys, cw, component); } else if (isInterface()) { Tag interf = getInterface(); writeOutNewInterface(statConstr, constr, keys, cw, interf); writeOutInitInterface(statConstr, constr, keys, cw, interf); } else { writeOutCall(statConstr, constr, keys, cw); } // udfCall Function[] functions = getFunctions(); ConditionVisitor cv; DecisionIntVisitor div; // less/equal than 10 functions if (isInterface()) { } else if (functions.length <= 10) { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null, new Type[] { Types.THROWABLE }, cw); BytecodeContext bc = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, UDF_CALL, writeLog(), suppressWSbeforeArg, output); if (functions.length == 0) { } else if (functions.length == 1) { ExpressionUtil.visitLine(bc, functions[0].getStart()); functions[0].getBody().writeOut(bc); ExpressionUtil.visitLine(bc, functions[0].getEnd()); } else writeOutUdfCallInner(bc, functions, 0, functions.length); adapter.visitInsn(Opcodes.ACONST_NULL); adapter.returnValue(); adapter.endMethod(); } // more than 10 functions else { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null, new Type[] { Types.THROWABLE }, cw); BytecodeContext bc = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, UDF_CALL, writeLog(), suppressWSbeforeArg, output); cv = new ConditionVisitor(); cv.visitBefore(); int count = 0; for (int i = 0; i < functions.length; i += 10) { cv.visitWhenBeforeExpr(); div = new DecisionIntVisitor(); div.visitBegin(); adapter.loadArg(2); div.visitLT(); adapter.push(i + 10); div.visitEnd(bc); cv.visitWhenAfterExprBeforeBody(bc); adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitVarInsn(Opcodes.ALOAD, 1); adapter.visitVarInsn(Opcodes.ALOAD, 2); adapter.visitVarInsn(Opcodes.ILOAD, 3); adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, createFunctionName(++count), "(Llucee/runtime/PageContext;Llucee/runtime/type/UDF;I)Ljava/lang/Object;"); adapter.visitInsn(Opcodes.ARETURN);//adapter.returnValue(); cv.visitWhenAfterBody(bc); } cv.visitAfter(bc); adapter.visitInsn(Opcodes.ACONST_NULL); adapter.returnValue(); adapter.endMethod(); count = 0; Method innerCall; for (int i = 0; i < functions.length; i += 10) { innerCall = new Method(createFunctionName(++count), Types.OBJECT, new Type[] { Types.PAGE_CONTEXT, USER_DEFINED_FUNCTION, Types.INT_VALUE }); adapter = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, innerCall, null, new Type[] { Types.THROWABLE }, cw); writeOutUdfCallInner( new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, innerCall, writeLog(), suppressWSbeforeArg, output), functions, i, i + 10 > functions.length ? functions.length : i + 10); adapter.visitInsn(Opcodes.ACONST_NULL); adapter.returnValue(); adapter.endMethod(); } } // threadCall TagThread[] threads = getThreads(); if (true) { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, THREAD_CALL, null, new Type[] { Types.THROWABLE }, cw); if (threads.length > 0) writeOutThreadCallInner(new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, THREAD_CALL, writeLog(), suppressWSbeforeArg, output), threads, 0, threads.length); //adapter.visitInsn(Opcodes.ACONST_NULL); adapter.returnValue(); adapter.endMethod(); } // udfDefaultValue // less/equal than 10 functions if (isInterface()) { } else if (functions.length <= 10) { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_DEFAULT_VALUE, null, new Type[] { Types.PAGE_EXCEPTION }, cw); if (functions.length > 0) writeUdfDefaultValueInner( new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output), functions, 0, functions.length); adapter.loadArg(DEFAULT_VALUE); adapter.returnValue(); adapter.endMethod(); } else { adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_DEFAULT_VALUE, null, new Type[] { Types.PAGE_EXCEPTION }, cw); BytecodeContext bc = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output); cv = new ConditionVisitor(); cv.visitBefore(); int count = 0; for (int i = 0; i < functions.length; i += 10) { cv.visitWhenBeforeExpr(); div = new DecisionIntVisitor(); div.visitBegin(); adapter.loadArg(1); div.visitLT(); adapter.push(i + 10); div.visitEnd(bc); cv.visitWhenAfterExprBeforeBody(bc); adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitVarInsn(Opcodes.ALOAD, 1); adapter.visitVarInsn(Opcodes.ILOAD, 2); adapter.visitVarInsn(Opcodes.ILOAD, 3); adapter.visitVarInsn(Opcodes.ALOAD, 4); adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "udfDefaultValue" + (++count), "(Llucee/runtime/PageContext;IILjava/lang/Object;)Ljava/lang/Object;"); adapter.visitInsn(Opcodes.ARETURN);//adapter.returnValue(); cv.visitWhenAfterBody(bc); } cv.visitAfter(bc); adapter.visitInsn(Opcodes.ACONST_NULL); adapter.returnValue(); adapter.endMethod(); count = 0; Method innerDefaultValue; for (int i = 0; i < functions.length; i += 10) { innerDefaultValue = new Method("udfDefaultValue" + (++count), Types.OBJECT, new Type[] { Types.PAGE_CONTEXT, Types.INT_VALUE, Types.INT_VALUE, Types.OBJECT }); adapter = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, innerDefaultValue, null, new Type[] { Types.PAGE_EXCEPTION }, cw); writeUdfDefaultValueInner( new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, innerDefaultValue, writeLog(), suppressWSbeforeArg, output), functions, i, i + 10 > functions.length ? functions.length : i + 10); adapter.loadArg(DEFAULT_VALUE); //adapter.visitInsn(Opcodes.ACONST_NULL); adapter.returnValue(); adapter.endMethod(); } } // register fields { GeneratorAdapter aInit = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, _INIT, null, null, cw); BytecodeContext bcInit = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, aInit, _INIT, writeLog(), suppressWSbeforeArg, output); registerFields(bcInit, keys); aInit.returnValue(); aInit.endMethod(); } //setPageSource(pageSource); constrAdapter.visitVarInsn(Opcodes.ALOAD, 0); constrAdapter.visitVarInsn(Opcodes.ALOAD, 1); constrAdapter.invokeVirtual(t, SET_PAGE_SOURCE); constrAdapter.returnValue(); constrAdapter.endMethod(); return cw.toByteArray(); }
From source file:lucee.transformer.bytecode.util.ASMUtil.java
License:Open Source License
private static void createProperty(ClassWriter cw, String classType, ASMProperty property) throws PageException { String name = property.getName(); Type type = property.getASMType(); Class clazz = property.getClazz(); cw.visitField(Opcodes.ACC_PRIVATE, name, type.toString(), null, null).visitEnd(); int load = loadFor(type); //int sizeOf=sizeOf(type); // get<PropertyName>():<type> Type[] types = new Type[0]; Method method = new Method((clazz == boolean.class ? "get" : "get") + StringUtil.ucFirst(name), type, types);//from w w w. j av a 2s .c om GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw); Label start = new Label(); adapter.visitLabel(start); adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitFieldInsn(Opcodes.GETFIELD, classType, name, type.toString()); adapter.returnValue(); Label end = new Label(); adapter.visitLabel(end); adapter.visitLocalVariable("this", "L" + classType + ";", null, start, end, 0); adapter.visitEnd(); adapter.endMethod(); // set<PropertyName>(object):void types = new Type[] { type }; method = new Method("set" + StringUtil.ucFirst(name), Types.VOID, types); adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw); start = new Label(); adapter.visitLabel(start); adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitVarInsn(load, 1); adapter.visitFieldInsn(Opcodes.PUTFIELD, classType, name, type.toString()); adapter.visitInsn(Opcodes.RETURN); end = new Label(); adapter.visitLabel(end); adapter.visitLocalVariable("this", "L" + classType + ";", null, start, end, 0); adapter.visitLocalVariable(name, type.toString(), null, start, end, 1); //adapter.visitMaxs(0, 0);//.visitMaxs(sizeOf+1, sizeOf+1);// hansx adapter.visitEnd(); adapter.endMethod(); }
From source file:lucee.transformer.bytecode.util.JavaProxyFactory.java
License:Open Source License
private static void _createMethod(ClassWriter cw, Map<String, Class> mDone, Method src, String className) throws IOException { Class<?>[] classArgs = src.getParameterTypes(); Class<?> classRtn = src.getReturnType(); String str = src.getName() + "(" + Reflector.getDspMethods(classArgs) + ")"; Class rtnClass = mDone.get(str); if (rtnClass != null) { if (rtnClass != classRtn) throw new IOException("there is a conflict with method [" + str + "], this method is declared more than once with different return types."); return;/*from w ww . ja v a 2 s .c om*/ } mDone.put(str, classRtn); Type[] typeArgs = ASMUtil.toTypes(classArgs); Type typeRtn = Type.getType(classRtn); org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(src.getName(), typeRtn, typeArgs); GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null, cw); //BytecodeContext bc = new BytecodeContext(statConstr,constr,null,null,keys,cw,className,adapter,method,writeLog); Label start = adapter.newLabel(); adapter.visitLabel(start); //JavaProxy.call(cfc,"add",new Object[]{arg0}) // config adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitFieldInsn(Opcodes.GETFIELD, className, "config", CONFIG_WEB_NAME); // cfc adapter.visitVarInsn(Opcodes.ALOAD, 0); adapter.visitFieldInsn(Opcodes.GETFIELD, className, "cfc", COMPONENT_NAME); // name adapter.push(src.getName()); // arguments ArrayVisitor av = new ArrayVisitor(); av.visitBegin(adapter, Types.OBJECT, typeArgs.length); for (int y = 0; y < typeArgs.length; y++) { av.visitBeginItem(adapter, y); adapter.loadArg(y); if (classArgs[y] == boolean.class) adapter.invokeStatic(JAVA_PROXY, _BOOLEAN); else if (classArgs[y] == byte.class) adapter.invokeStatic(JAVA_PROXY, _BYTE); else if (classArgs[y] == char.class) adapter.invokeStatic(JAVA_PROXY, _CHAR); else if (classArgs[y] == double.class) adapter.invokeStatic(JAVA_PROXY, _DOUBLE); else if (classArgs[y] == float.class) adapter.invokeStatic(JAVA_PROXY, _FLOAT); else if (classArgs[y] == int.class) adapter.invokeStatic(JAVA_PROXY, _INT); else if (classArgs[y] == long.class) adapter.invokeStatic(JAVA_PROXY, _LONG); else if (classArgs[y] == short.class) adapter.invokeStatic(JAVA_PROXY, _SHORT); else { adapter.invokeStatic(JAVA_PROXY, _OBJECT); } av.visitEndItem(adapter); } av.visitEnd(); adapter.invokeStatic(JAVA_PROXY, CALL); //JavaProxy.to...(...); int rtn = Opcodes.IRETURN; if (classRtn == boolean.class) adapter.invokeStatic(JAVA_PROXY, TO_BOOLEAN); else if (classRtn == byte.class) adapter.invokeStatic(JAVA_PROXY, TO_BYTE); else if (classRtn == char.class) adapter.invokeStatic(JAVA_PROXY, TO_CHAR); else if (classRtn == double.class) { rtn = Opcodes.DRETURN; adapter.invokeStatic(JAVA_PROXY, TO_DOUBLE); } else if (classRtn == float.class) { rtn = Opcodes.FRETURN; adapter.invokeStatic(JAVA_PROXY, TO_FLOAT); } else if (classRtn == int.class) adapter.invokeStatic(JAVA_PROXY, TO_INT); else if (classRtn == long.class) { rtn = Opcodes.LRETURN; adapter.invokeStatic(JAVA_PROXY, TO_LONG); } else if (classRtn == short.class) adapter.invokeStatic(JAVA_PROXY, TO_SHORT); else if (classRtn == void.class) { rtn = Opcodes.RETURN; adapter.pop(); } else { rtn = Opcodes.ARETURN; adapter.checkCast(typeRtn); } /*mv = cw.visitMethod(ACC_PUBLIC, "add", "(Ljava/lang/Object;)Z", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(20, l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, "Test", "cfc", "Ljava/lang/Object;"); mv.visitLdcInsn("add"); mv.visitInsn(ICONST_1); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKESTATIC, "JavaProxy", "call", "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitMethodInsn(INVOKESTATIC, "JavaProxy", "toBoolean", "(Ljava/lang/Object;)Z"); mv.visitInsn(IRETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "LTest;", null, l0, l1, 0); mv.visitLocalVariable("arg0", "Ljava/lang/Object;", null, l0, l1, 1); mv.visitMaxs(6, 2); mv.visitEnd();*/ adapter.visitInsn(rtn); adapter.endMethod(); }