Package clojure.asm.commons

Examples of clojure.asm.commons.GeneratorAdapter


  abstract Type[] getArgTypes();

  public void emit(ObjExpr fn, ClassVisitor cv){
    Method m = new Method(getMethodName(), getReturnType(), getArgTypes());

    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC,
                                                m,
                                                null,
                                                //todo don't hardwire this
                                                EXCEPTION_TYPES,
                                                cv);
    gen.visitCode();

    Label loopLabel = gen.mark();
    gen.visitLineNumber(line, loopLabel);
    try
      {
      Var.pushThreadBindings(RT.map(LOOP_LABEL, loopLabel, METHOD, this));

      body.emit(C.RETURN, fn, gen);
      Label end = gen.mark();
      gen.visitLocalVariable("this", "Ljava/lang/Object;", null, loopLabel, end, 0);
      for(ISeq lbs = argLocals.seq(); lbs != null; lbs = lbs.next())
        {
        LocalBinding lb = (LocalBinding) lbs.first();
        gen.visitLocalVariable(lb.name, "Ljava/lang/Object;", null, loopLabel, end, lb.idx);
        }
      }
    finally
      {
      Var.popThreadBindings();
      }

    gen.returnValue();
    //gen.visitMaxs(1, 1);
    gen.endMethod();
  }
View Full Code Here


  }

  public void doEmitStatic(ObjExpr fn, ClassVisitor cv){
    Method ms = new Method("invokeStatic", getReturnType(), argtypes);

    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                                                ms,
                                                null,
                                                //todo don't hardwire this
                                                EXCEPTION_TYPES,
                                                cv);
    gen.visitCode();
    Label loopLabel = gen.mark();
    gen.visitLineNumber(line, loopLabel);
    try
      {
      Var.pushThreadBindings(RT.map(LOOP_LABEL, loopLabel, METHOD, this));
      emitBody(objx, gen, retClass, body);

      Label end = gen.mark();
      for(ISeq lbs = argLocals.seq(); lbs != null; lbs = lbs.next())
        {
        LocalBinding lb = (LocalBinding) lbs.first();
        gen.visitLocalVariable(lb.name, argtypes[lb.idx].getDescriptor(), null, loopLabel, end, lb.idx);
        }
      }
    finally
      {
      Var.popThreadBindings();
      }

    gen.returnValue();
    //gen.visitMaxs(1, 1);
    gen.endMethod();

  //generate the regular invoke, calling the static method
    Method m = new Method(getMethodName(), OBJECT_TYPE, getArgTypes());

    gen = new GeneratorAdapter(ACC_PUBLIC,
                               m,
                               null,
                               //todo don't hardwire this
                               EXCEPTION_TYPES,
                               cv);
    gen.visitCode();
    for(int i = 0; i < argtypes.length; i++)
      {
      gen.loadArg(i);
      HostExpr.emitUnboxArg(fn, gen, argclasses[i]);
      }
    gen.invokeStatic(objx.objtype, ms);
    gen.box(getReturnType());


    gen.returnValue();
    //gen.visitMaxs(1, 1);
    gen.endMethod();

  }
View Full Code Here

      for(Class retType : e.getValue())
        {
             Method meth = new Method(m.getName(), Type.getType(retType), argTypes);

        GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC + ACC_BRIDGE,
                                                meth,
                                                null,
                                                //todo don't hardwire this
                                                EXCEPTION_TYPES,
                                                cv);
        gen.visitCode();
        gen.loadThis();
        gen.loadArgs();
        gen.invokeInterface(Type.getType(m.getDeclaringClass()),target);
        gen.returnValue();
        gen.endMethod();
        }
      }
  }
View Full Code Here

      {
      extypes = new Type[exclasses.length];
      for(int i=0;i<exclasses.length;i++)
        extypes[i] = Type.getType(exclasses[i]);
      }
    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC,
                                                m,
                                                null,
                                                extypes,
                                                cv);
    addAnnotation(gen,methodMeta);
    for(int i = 0; i < parms.count(); i++)
      {
      IPersistentMap meta = RT.meta(parms.nth(i));
      addParameterAnnotation(gen, meta, i);
      }
    gen.visitCode();

    Label loopLabel = gen.mark();

    gen.visitLineNumber(line, loopLabel);
    try
      {
      Var.pushThreadBindings(RT.map(LOOP_LABEL, loopLabel, METHOD, this));

      emitBody(objx, gen, retClass, body);
      Label end = gen.mark();
      gen.visitLocalVariable("this", obj.objtype.getDescriptor(), null, loopLabel, end, 0);
      for(ISeq lbs = argLocals.seq(); lbs != null; lbs = lbs.next())
        {
        LocalBinding lb = (LocalBinding) lbs.first();
        gen.visitLocalVariable(lb.name, argTypes[lb.idx-1].getDescriptor(), null, loopLabel, end, lb.idx);
        }
      }
    finally
      {
      Var.popThreadBindings();
      }

    gen.returnValue();
    //gen.visitMaxs(1, 1);
    gen.endMethod();
  }
View Full Code Here

            , lb.name, OBJECT_TYPE.getDescriptor(), null, null);
      }

    //ctor that takes closed-overs and does nothing
    Method m = new Method("<init>", Type.VOID_TYPE, ret.ctorTypes());
    GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC,
                                                    m,
                                                    null,
                                                    null,
                                                    cv);
    ctorgen.visitCode();
    ctorgen.loadThis();
    ctorgen.invokeConstructor(Type.getObjectType(superName), voidctor);
    ctorgen.returnValue();
    ctorgen.endMethod();

    if(ret.altCtorDrops > 0)
      {
      Type[] ctorTypes = ret.ctorTypes();
      Type[] altCtorTypes = new Type[ctorTypes.length-ret.altCtorDrops];
      for(int i=0;i<altCtorTypes.length;i++)
        altCtorTypes[i] = ctorTypes[i];
      Method alt = new Method("<init>", Type.VOID_TYPE, altCtorTypes);
      ctorgen = new GeneratorAdapter(ACC_PUBLIC,
                              alt,
                              null,
                              null,
                              cv);
      ctorgen.visitCode();
      ctorgen.loadThis();
      ctorgen.loadArgs();
      for(int i=0;i<ret.altCtorDrops;i++)
        ctorgen.visitInsn(Opcodes.ACONST_NULL);

      ctorgen.invokeConstructor(Type.getObjectType(COMPILE_STUB_PREFIX + "/" + ret.internalName),
                                new Method("<init>", Type.VOID_TYPE, ctorTypes));

      ctorgen.returnValue();
      ctorgen.endMethod();
      }
    //end of class
    cv.visitEnd();

    byte[] bytecode = cw.toByteArray();
View Full Code Here

  protected void emitStatics(ClassVisitor cv) {
    if(this.isDeftype())
      {
      //getBasis()
      Method meth = Method.getMethod("clojure.lang.IPersistentVector getBasis()");
      GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                        meth,
                        null,
                        null,
                        cv);
      emitValue(hintedFields, gen);
      gen.returnValue();
      gen.endMethod();

      if (this.isDeftype() && this.fields.count() > this.hintedFields.count())
        {
        //create(IPersistentMap)
        String className = name.replace('.', '/');
View Full Code Here

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = cw;
    cv.visit(V1_5, ACC_PUBLIC + ACC_SUPER, objx.internalName, null, "java/lang/Object", null);

    //static load method
    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                                                Method.getMethod("void load ()"),
                                                null,
                                                null,
                                                cv);
    gen.visitCode();

    for(Object r = LispReader.read(pushbackReader, false, EOF, false); r != EOF;
        r = LispReader.read(pushbackReader, false, EOF, false))
      {
        LINE_AFTER.set(pushbackReader.getLineNumber());
        compile1(gen, objx, r);
        LINE_BEFORE.set(pushbackReader.getLineNumber());
      }
    //end of load
    gen.returnValue();
    gen.endMethod();

    //static fields for constants
    for(int i = 0; i < objx.constants.count(); i++)
      {
      cv.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, objx.constantName(i), objx.constantType(i).getDescriptor(),
                    null, null);
      }

    final int INITS_PER = 100;
    int numInits =  objx.constants.count() / INITS_PER;
    if(objx.constants.count() % INITS_PER != 0)
      ++numInits;

    for(int n = 0;n<numInits;n++)
      {
      GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                                                        Method.getMethod("void __init" + n + "()"),
                                                        null,
                                                        null,
                                                        cv);
      clinitgen.visitCode();
      try
        {
        Var.pushThreadBindings(RT.map(RT.PRINT_DUP, RT.T));

        for(int i = n*INITS_PER; i < objx.constants.count() && i < (n+1)*INITS_PER; i++)
          {
          objx.emitValue(objx.constants.nth(i), clinitgen);
          clinitgen.checkCast(objx.constantType(i));
          clinitgen.putStatic(objx.objtype, objx.constantName(i), objx.constantType(i));
          }
        }
      finally
        {
        Var.popThreadBindings();
        }
      clinitgen.returnValue();
      clinitgen.endMethod();
      }

    //static init for constants, keywords and vars
    GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                                                      Method.getMethod("void <clinit> ()"),
                                                      null,
                                                      null,
                                                      cv);
    clinitgen.visitCode();
    Label startTry = clinitgen.newLabel();
    Label endTry = clinitgen.newLabel();
    Label end = clinitgen.newLabel();
    Label finallyLabel = clinitgen.newLabel();

//    if(objx.constants.count() > 0)
//      {
//      objx.emitConstants(clinitgen);
//      }
    for(int n = 0;n<numInits;n++)
      clinitgen.invokeStatic(objx.objtype, Method.getMethod("void __init" + n + "()"));

    clinitgen.push(objx.internalName.replace('/','.'));
    clinitgen.invokeStatic(CLASS_TYPE, Method.getMethod("Class forName(String)"));
    clinitgen.invokeVirtual(CLASS_TYPE,Method.getMethod("ClassLoader getClassLoader()"));
    clinitgen.invokeStatic(Type.getType(Compiler.class), Method.getMethod("void pushNSandLoader(ClassLoader)"));
    clinitgen.mark(startTry);
    clinitgen.invokeStatic(objx.objtype, Method.getMethod("void load()"));
    clinitgen.mark(endTry);
    clinitgen.invokeStatic(VAR_TYPE, Method.getMethod("void popThreadBindings()"));
    clinitgen.goTo(end);

    clinitgen.mark(finallyLabel);
    //exception should be on stack
    clinitgen.invokeStatic(VAR_TYPE, Method.getMethod("void popThreadBindings()"));
    clinitgen.throwException();
    clinitgen.mark(end);
    clinitgen.visitTryCatchBlock(startTry, endTry, finallyLabel, null);

    //end of static init
    clinitgen.returnValue();
    clinitgen.endMethod();

    //end of class
    cv.visitEnd();

    writeClassFile(objx.internalName, cw.toByteArray());
View Full Code Here

  abstract Type[] getArgTypes();

  public void emit(ObjExpr fn, ClassVisitor cv){
    Method m = new Method(getMethodName(), getReturnType(), getArgTypes());

    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC,
                                                m,
                                                null,
                                                //todo don't hardwire this
                                                EXCEPTION_TYPES,
                                                cv);
    gen.visitCode();
    Label loopLabel = gen.mark();
    gen.visitLineNumber(line, loopLabel);
    try
      {
      Var.pushThreadBindings(RT.map(LOOP_LABEL, loopLabel, METHOD, this));
      body.emit(C.RETURN, fn, gen);
      Label end = gen.mark();
      gen.visitLocalVariable("this", "Ljava/lang/Object;", null, loopLabel, end, 0);
      for(ISeq lbs = argLocals.seq(); lbs != null; lbs = lbs.next())
        {
        LocalBinding lb = (LocalBinding) lbs.first();
        gen.visitLocalVariable(lb.name, "Ljava/lang/Object;", null, loopLabel, end, lb.idx);
        }
      }
    finally
      {
      Var.popThreadBindings();
      }

    gen.returnValue();
    //gen.visitMaxs(1, 1);
    gen.endMethod();
  }
View Full Code Here

            , lb.name, OBJECT_TYPE.getDescriptor(), null, null);
      }

    //ctor that takes closed-overs and does nothing
    Method m = new Method("<init>", Type.VOID_TYPE, ret.ctorTypes());
    GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC,
                                                    m,
                                                    null,
                                                    null,
                                                    cv);
    ctorgen.visitCode();
    ctorgen.loadThis();
    ctorgen.invokeConstructor(Type.getObjectType(superName), voidctor);
    ctorgen.returnValue();
    ctorgen.endMethod();

    if(ret.altCtorDrops > 0)
      {
      Type[] ctorTypes = ret.ctorTypes();
      Type[] altCtorTypes = new Type[ctorTypes.length-ret.altCtorDrops];
      for(int i=0;i<altCtorTypes.length;i++)
        altCtorTypes[i] = ctorTypes[i];
      Method alt = new Method("<init>", Type.VOID_TYPE, altCtorTypes);
      ctorgen = new GeneratorAdapter(ACC_PUBLIC,
                              alt,
                              null,
                              null,
                              cv);
      ctorgen.visitCode();
      ctorgen.loadThis();
      ctorgen.loadArgs();
      for(int i=0;i<ret.altCtorDrops;i++)
        ctorgen.visitInsn(Opcodes.ACONST_NULL);

      ctorgen.invokeConstructor(Type.getObjectType(COMPILE_STUB_PREFIX + "/" + ret.internalName),
                                new Method("<init>", Type.VOID_TYPE, ctorTypes));

      ctorgen.returnValue();
      ctorgen.endMethod();
      }
    //end of class
    cv.visitEnd();

    byte[] bytecode = cw.toByteArray();
View Full Code Here

      for(Class retType : e.getValue())
        {
             Method meth = new Method(m.getName(), Type.getType(retType), argTypes);

        GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC + ACC_BRIDGE,
                                                meth,
                                                null,
                                                //todo don't hardwire this
                                                EXCEPTION_TYPES,
                                                cv);
        gen.visitCode();
        gen.loadThis();
        gen.loadArgs();
        gen.invokeInterface(Type.getType(m.getDeclaringClass()),target);
        gen.returnValue();
        gen.endMethod();
        }
      }
  }
View Full Code Here

TOP

Related Classes of clojure.asm.commons.GeneratorAdapter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.