Package clojure.asm.commons

Examples of clojure.asm.commons.GeneratorAdapter


  public void emit(FnExpr fn, ClassVisitor cv){
    Method m = new Method(isVariadic() ? "doInvoke" : "invoke",
                          OBJECT_TYPE, ARG_TYPES[numParams()]);

    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC,
                                                m,
                                                null,
                                                EXCEPTION_TYPES,
                                                cv);
    gen.visitCode();
    Label loopLabel = gen.mark();
    gen.visitLineNumber(line, loopLabel);
    try
      {
      Var.pushThreadBindings(RT.map(LOOP_LABEL, loopLabel));
      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.rest())
        {
        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


      cv.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, munge(v.sym.toString()),
                    VAR_TYPE.getDescriptor(), null, null);
      }
      */
    //static init for constants, keywords and vars
    GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                                                      Method.getMethod("void <clinit> ()"),
                                                      null,
                                                      null,
                                                      cv);
    clinitgen.visitCode();
    clinitgen.visitLineNumber(line, clinitgen.mark());
//    Label begin = clinitgen.newLabel();
//    Label end = clinitgen.newLabel();

    if(constants.count() > 0)
      {
//    clinitgen.mark(begin);
      clinitgen.visitLdcInsn(fntype);
      clinitgen.invokeVirtual(CLASS_TYPE, getClassLoaderMethod);
      clinitgen.checkCast(DYNAMIC_CLASSLOADER_TYPE);
      clinitgen.push(constantsID);
      clinitgen.invokeVirtual(DYNAMIC_CLASSLOADER_TYPE, getConstantsMethod);

      for(int i = 0; i < constants.count(); i++)
        {
        clinitgen.dup();
        clinitgen.push(i);
        clinitgen.arrayLoad(OBJECT_TYPE);
        clinitgen.checkCast(constantType(i));
        clinitgen.putStatic(fntype, constantName(i), constantType(i));
        }
      }
//    for(ISeq s = RT.keys(keywords); s != null; s = s.rest())
//      {
//      Keyword k = (Keyword) s.first();
//      clinitgen.push(k.sym.ns);
//      clinitgen.push(k.sym.name);
//      clinitgen.invokeStatic(KEYWORD_TYPE, kwintern);
//      clinitgen.putStatic(fntype, munge(k.sym.toString()), KEYWORD_TYPE);
//      }
//    for(ISeq s = RT.keys(vars); s != null; s = s.rest())
//      {
//      Var v = (Var) s.first();
//      clinitgen.push(v.ns.name.name);
//      clinitgen.invokeStatic(SYMBOL_TYPE, symcreate);
//      clinitgen.push(v.sym.name);
//      clinitgen.invokeStatic(SYMBOL_TYPE, symcreate);
//      clinitgen.invokeStatic(VAR_TYPE, varintern);
//      clinitgen.putStatic(fntype, munge(v.sym.toString()), VAR_TYPE);
//      }
//    clinitgen.mark(end);
//    clinitgen.visitLocalVariable("constants", "[Ljava/lang/Object;", null, begin, end, 0);
    clinitgen.returnValue();

    clinitgen.endMethod();
//    clinitgen.visitMaxs(1, 1);
    //instance fields for closed-overs
    for(ISeq s = RT.keys(closes); s != null; s = s.rest())
      {
      LocalBinding lb = (LocalBinding) s.first();
      cv.visitField(ACC_PUBLIC + ACC_FINAL, lb.name, OBJECT_TYPE.getDescriptor(), null, null);
      }
    //ctor that takes closed-overs and inits base + fields
    Method m = new Method("<init>", Type.VOID_TYPE, ARG_TYPES[closes.count()]);
    GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC,
                                                    m,
                                                    null,
                                                    null,
                                                    cv);
    ctorgen.visitCode();
    ctorgen.visitLineNumber(line, ctorgen.mark());
    ctorgen.loadThis();
    if(isVariadic()) //RestFn ctor takes reqArity arg
      {
      ctorgen.push(variadicMethod.reqParms.count());
      ctorgen.invokeConstructor(restFnType, restfnctor);
      }
    else
      ctorgen.invokeConstructor(aFnType, afnctor);
    int a = 1;
    for(ISeq s = RT.keys(closes); s != null; s = s.rest(), ++a)
      {
      LocalBinding lb = (LocalBinding) s.first();
      ctorgen.loadThis();
      ctorgen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ILOAD), a);
      ctorgen.putField(fntype, lb.name, OBJECT_TYPE);
      }
    ctorgen.returnValue();
    //  ctorgen.visitMaxs(1, 1);
    ctorgen.endMethod();

    //override of invoke/doInvoke for each method
    for(ISeq s = RT.seq(methods); s != null; s = s.rest())
      {
      FnMethod method = (FnMethod) s.first();
View Full Code Here

      cv.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, munge(v.sym.toString()),
                    VAR_TYPE.getDescriptor(), null, null);
      }
      */
    //static init for constants, keywords and vars
    GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC,
                                                      Method.getMethod("void <clinit> ()"),
                                                      null,
                                                      null,
                                                      cv);
    clinitgen.visitCode();
    clinitgen.visitLineNumber(line, clinitgen.mark());
//    Label begin = clinitgen.newLabel();
//    Label end = clinitgen.newLabel();

    if(constants.count() > 0)
      {
//    clinitgen.mark(begin);
      clinitgen.visitLdcInsn(fntype);
      clinitgen.invokeVirtual(CLASS_TYPE, getClassLoaderMethod);
      clinitgen.checkCast(DYNAMIC_CLASSLOADER_TYPE);
      clinitgen.push(constantsID);
      clinitgen.invokeVirtual(DYNAMIC_CLASSLOADER_TYPE, getConstantsMethod);

      for(int i = 0; i < constants.count(); i++)
        {
        clinitgen.dup();
        clinitgen.push(i);
        clinitgen.arrayLoad(OBJECT_TYPE);
        clinitgen.checkCast(constantType(i));
        clinitgen.putStatic(fntype, constantName(i), constantType(i));
        }
      }
//    for(ISeq s = RT.keys(keywords); s != null; s = s.rest())
//      {
//      Keyword k = (Keyword) s.first();
//      clinitgen.push(k.sym.ns);
//      clinitgen.push(k.sym.name);
//      clinitgen.invokeStatic(KEYWORD_TYPE, kwintern);
//      clinitgen.putStatic(fntype, munge(k.sym.toString()), KEYWORD_TYPE);
//      }
//    for(ISeq s = RT.keys(vars); s != null; s = s.rest())
//      {
//      Var v = (Var) s.first();
//      clinitgen.push(v.ns.name.name);
//      clinitgen.invokeStatic(SYMBOL_TYPE, symcreate);
//      clinitgen.push(v.sym.name);
//      clinitgen.invokeStatic(SYMBOL_TYPE, symcreate);
//      clinitgen.invokeStatic(VAR_TYPE, varintern);
//      clinitgen.putStatic(fntype, munge(v.sym.toString()), VAR_TYPE);
//      }
//    clinitgen.mark(end);
//    clinitgen.visitLocalVariable("constants", "[Ljava/lang/Object;", null, begin, end, 0);
    clinitgen.returnValue();

    clinitgen.endMethod();
//    clinitgen.visitMaxs(1, 1);
    //instance fields for closed-overs
    for(ISeq s = RT.keys(closes); s != null; s = s.rest())
      {
      LocalBinding lb = (LocalBinding) s.first();
      if(lb.getPrimitiveType() != null)
        cv.visitField(ACC_PUBLIC + ACC_FINAL, lb.name, Type.getType(lb.getPrimitiveType()).getDescriptor(),
                      null, null);
      else
        cv.visitField(ACC_PUBLIC + ACC_FINAL, lb.name, OBJECT_TYPE.getDescriptor(), null, null);
      }
    //ctor that takes closed-overs and inits base + fields
//    Method m = new Method("<init>", Type.VOID_TYPE, ARG_TYPES[closes.count()]);
    Method m = new Method("<init>", Type.VOID_TYPE, ctorTypes());
    GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC,
                                                    m,
                                                    null,
                                                    null,
                                                    cv);
    Label start = ctorgen.newLabel();
    Label end = ctorgen.newLabel();
    ctorgen.visitCode();
    ctorgen.visitLineNumber(line, ctorgen.mark());
    ctorgen.visitLabel(start);
    ctorgen.loadThis();
    if(isVariadic()) //RestFn ctor takes reqArity arg
      {
      ctorgen.push(variadicMethod.reqParms.count());
      ctorgen.invokeConstructor(restFnType, restfnctor);
      }
    else
      ctorgen.invokeConstructor(aFnType, afnctor);
    int a = 1;
    for(ISeq s = RT.keys(closes); s != null; s = s.rest(), ++a)
      {
      LocalBinding lb = (LocalBinding) s.first();
      ctorgen.loadThis();
      Class primc = lb.getPrimitiveType();
      if(primc != null)
        {
        ctorgen.visitVarInsn(Type.getType(primc).getOpcode(Opcodes.ILOAD), a);
        ctorgen.putField(fntype, lb.name, Type.getType(primc));
        if(primc == Long.TYPE || primc == Double.TYPE)
          ++a;
        }
      else
        {
        ctorgen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ILOAD), a);
        ctorgen.putField(fntype, lb.name, OBJECT_TYPE);
        }
      }
    ctorgen.visitLabel(end);
//    a = 1;
//    for(ISeq s = RT.keys(closes); s != null; s = s.rest(), ++a)
//      {
//      LocalBinding lb = (LocalBinding) s.first();
//      Class primc = lb.getPrimitiveType();
//      if(primc != null)
//        {
//        ctorgen.visitLocalVariable(lb.name, Type.getDescriptor(primc), null, start, end, a);
//        if(primc == Long.TYPE || primc == Double.TYPE)
//          ++a;
//        }
//      else
//        {
//        ctorgen.visitLocalVariable(lb.name, "Ljava/lang/Object;", null, start, end, a);
//        }
//      }
    ctorgen.returnValue();
    //  ctorgen.visitMaxs(1, 1);
    ctorgen.endMethod();

    //override of invoke/doInvoke for each method
    for(ISeq s = RT.seq(methods); s != null; s = s.rest())
      {
      FnMethod method = (FnMethod) s.first();
View Full Code Here

  public void emit(FnExpr fn, ClassVisitor cv){
    Method m = new Method(isVariadic() ? "doInvoke" : "invoke",
                          OBJECT_TYPE, ARG_TYPES[numParams()]);

    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC,
                                                m,
                                                null,
                                                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.rest())
        {
        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 doEmitPrim(ObjExpr fn, ClassVisitor cv){
    Method ms = new Method("invokePrim", getReturnType(), argtypes);

    GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC + ACC_FINAL,
                                                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();
      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, argtypes[lb.idx-1].getDescriptor(), null, loopLabel, end, lb.idx);
        }
      }
    catch(Exception e)
      {
      throw Util.sneakyThrow(e);
      }
    finally
      {
      Var.popThreadBindings();
      }

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

  //generate the regular invoke, calling the prim 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();
    gen.loadThis();
    for(int i = 0; i < argtypes.length; i++)
      {
      gen.loadArg(i);
      HostExpr.emitUnboxArg(fn, gen, argclasses[i]);
      }
    gen.invokeInterface(Type.getType("L"+prim+";"), ms);
    gen.box(getReturnType());


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

  }
View Full Code Here

  }
  public void doEmit(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

  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

    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());
        COLUMN_AFTER.set(pushbackReader.getColumnNumber());
        compile1(gen, objx, r);
        LINE_BEFORE.set(pushbackReader.getLineNumber());
        COLUMN_BEFORE.set(pushbackReader.getColumnNumber());
      }
    //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

      {
      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);
        }
      }
    catch(Exception e)
      {
      throw Util.sneakyThrow(e);
      }
    finally
      {
      Var.popThreadBindings();
      }

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

      method.emit(this, cv);
      }

    if(isVariadic())
      {
      GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC,
                                                  Method.getMethod("int getRequiredArity()"),
                                                  null,
                                                  null,
                                                  cv);
      gen.visitCode();
      gen.push(variadicMethod.reqParms.count());
      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.