Esempio n. 1
0
  private static void catchBody(
      BytecodeContext bc,
      GeneratorAdapter adapter,
      Catch ct,
      int pe,
      int lRef,
      boolean caugth,
      boolean store)
      throws BytecodeException {
    // pc.setCatch(pe,true);
    adapter.loadArg(0);
    adapter.loadLocal(pe);
    adapter.push(caugth);
    adapter.push(store);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.SET_CATCH3);

    // ref=
    ct.name.writeOut(bc, Expression.MODE_REF);
    adapter.storeLocal(lRef);

    adapter.loadLocal(lRef);
    adapter.loadArg(0);
    adapter.loadLocal(pe); // (...,pe.getCatchBlock(pc))
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_EXCEPTION, GET_CATCH_BLOCK);
    adapter.invokeInterface(Types.REFERENCE, SET);
    adapter.pop();

    ct.body.writeOut(bc);
  }
Esempio n. 2
0
  @Override
  public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {

    int form = VALUE;
    int type = STRING;
    if (name instanceof Variable && !((Variable) name).fromHash()) {
      GeneratorAdapter adapter = bc.getAdapter();
      String[] arr = VariableString.variableToStringArray((Variable) name, true);
      if (arr.length > 1) {
        form = ARRAY;
        ArrayVisitor av = new ArrayVisitor();
        av.visitBegin(adapter, Types.STRING, arr.length);
        for (int y = 0; y < arr.length; y++) {
          av.visitBeginItem(adapter, y);
          adapter.push(varKeyUpperCase ? arr[y].toUpperCase() : arr[y]);
          av.visitEndItem(bc.getAdapter());
        }
        av.visitEnd();
      } else {
        // VariableString.toExprString(name).writeOut(bc, MODE_REF);
        String str = VariableString.variableToString((Variable) name, true);
        name = LitString.toExprString(varKeyUpperCase ? str.toUpperCase() : str);
        type = Variable.registerKey(bc, VariableString.toExprString(name)) ? KEY : STRING;
      }
    } else {
      // CastString.toExprString(name).writeOut(bc, MODE_REF);
      type = Variable.registerKey(bc, CastString.toExprString(name)) ? KEY : STRING;
    }
    // name.writeOut(bc, MODE_REF);
    super._writeOut(bc, MODE_REF);
    // bc.getAdapter().push(variableString);
    bc.getAdapter().invokeStatic(TYPE_FUNCTION_VALUE, NEW_INSTANCE[type][form]);
    return Types.FUNCTION_VALUE;
  }
Esempio n. 3
0
  private static byte[] visitEnd() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, RubyIDClassName, null, "java/lang/Object", null);
    Method staticBlock = Method.getMethod("void <clinit> ()V");
    GeneratorAdapter staticBlockMg =
        new GeneratorAdapter(Opcodes.ACC_STATIC, staticBlock, null, null, cw);

    for (Map.Entry<String, String> e : idMap.entrySet()) {
      cw.visitField(
          Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,
          e.getValue(),
          Types.RUBY_ID_TYPE.getDescriptor(),
          null,
          null);

      staticBlockMg.push(e.getKey());
      staticBlockMg.invokeStatic(
          Type.getType(RubyID.class),
          Method.getMethod("com.xruby.runtime.lang.RubyID intern(String)"));
      staticBlockMg.putStatic(
          Type.getType("L" + RubyIDClassName + ";"), e.getValue(), Types.RUBY_ID_TYPE);
    }

    staticBlockMg.returnValue();
    staticBlockMg.endMethod();
    cw.visitEnd();

    return cw.toByteArray();
  }
Esempio n. 4
0
  @Override
  void write(
      final CompilerSettings settings,
      final Definition definition,
      final GeneratorAdapter adapter) {
    final Sort sort = actual.sort;

    switch (sort) {
      case STRING:
        adapter.push((String) constant);
        break;
      case DOUBLE:
        adapter.push((double) constant);
        break;
      case FLOAT:
        adapter.push((float) constant);
        break;
      case LONG:
        adapter.push((long) constant);
        break;
      case INT:
        adapter.push((int) constant);
        break;
      case CHAR:
        adapter.push((char) constant);
        break;
      case SHORT:
        adapter.push((short) constant);
        break;
      case BYTE:
        adapter.push((byte) constant);
        break;
      case BOOL:
        if (tru != null && (boolean) constant) {
          adapter.goTo(tru);
        } else if (fals != null && !(boolean) constant) {
          adapter.goTo(fals);
        } else if (tru == null && fals == null) {
          adapter.push((boolean) constant);
        }

        break;
      default:
        throw new IllegalStateException(error("Illegal tree structure."));
    }

    if (sort != Sort.BOOL) {
      WriterUtility.writeBranch(adapter, tru, fals);
    }
  }
Esempio n. 5
0
  public static void main(final String args[]) throws Exception {

    // Generates the bytecode corresponding to the following Java class:
    //
    // public class Example {
    // public static void main (String[] args) {
    // System.out.println("Hello world!");
    // }
    // }

    // creates a ClassWriter for the Example public class,
    // which inherits from Object
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);

    // creates a MethodWriter for the (implicit) constructor
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    // pushes the 'this' variable
    mw.visitVarInsn(ALOAD, 0);
    // invokes the super class constructor
    mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of one stack element and one local variable
    mw.visitMaxs(1, 1);
    mw.visitEnd();

    // creates a MethodWriter for the 'main' method
    mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
    // pushes the 'out' field (of type PrintStream) of the System class
    mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    // pushes the "Hello World!" String constant
    mw.visitLdcInsn("Hello world!");
    // invokes the 'println' method (defined in the PrintStream class)
    mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of two stack elements and two local
    // variables
    mw.visitMaxs(2, 2);
    mw.visitEnd();

    // gets the bytecode of the Example class, and loads it dynamically
    byte[] code = cw.toByteArray();

    FileOutputStream fos = new FileOutputStream("Example.class");
    fos.write(code);
    fos.close();

    Helloworld loader = new Helloworld();
    Class<?> exampleClass = loader.defineClass("Example", code, 0, code.length);

    // uses the dynamically generated class to print 'Helloworld'
    exampleClass.getMethods()[0].invoke(null, new Object[] {null});

    // ------------------------------------------------------------------------
    // Same example with a GeneratorAdapter (more convenient but slower)
    // ------------------------------------------------------------------------

    cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);

    // creates a GeneratorAdapter for the (implicit) constructor
    Method m = Method.getMethod("void <init> ()");
    GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
    mg.loadThis();
    mg.invokeConstructor(Type.getType(Object.class), m);
    mg.returnValue();
    mg.endMethod();

    // creates a GeneratorAdapter for the 'main' method
    m = Method.getMethod("void main (String[])");
    mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
    mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class));
    mg.push("Hello world!");
    mg.invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println (String)"));
    mg.returnValue();
    mg.endMethod();

    cw.visitEnd();

    code = cw.toByteArray();
    loader = new Helloworld();
    exampleClass = loader.defineClass("Example", code, 0, code.length);

    // uses the dynamically generated class to print 'Helloworld'
    exampleClass.getMethods()[0].invoke(null, new Object[] {null});
  }
Esempio n. 6
0
 private void forInit(GeneratorAdapter adapter, int start, boolean isLocal) {
   i = adapter.newLocal(Types.INT_VALUE);
   if (isLocal) adapter.loadLocal(start, Types.INT_VALUE);
   else adapter.push(start);
   adapter.visitVarInsn(ISTORE, i);
 }
Esempio n. 7
0
  private void _writeOutCatch(BytecodeContext bc, int lRef, int lThrow) throws BytecodeException {
    GeneratorAdapter adapter = bc.getAdapter();
    int pe = adapter.newLocal(Types.PAGE_EXCEPTION);

    // instance of Abort
    Label abortEnd = new Label();
    adapter.loadLocal(lThrow);
    adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
    // adapter.instanceOf(Types.ABORT);
    adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
    adapter.loadLocal(lThrow);
    adapter.throwException();
    adapter.visitLabel(abortEnd);

    // PageExceptionImpl old=pc.getCatch();
    int old = adapter.newLocal(Types.PAGE_EXCEPTION);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.GET_CATCH);
    adapter.storeLocal(old);

    // cast to PageException  Caster.toPagException(t);
    adapter.loadLocal(lThrow);
    adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);

    // PageException pe=...
    adapter.storeLocal(pe);

    // catch loop
    Label endAllIf = new Label();
    Iterator<Catch> it = catches.iterator();
    Catch ctElse = null;
    while (it.hasNext()) {
      Catch ct = it.next();
      // store any for else
      if (ct.type != null
          && ct.type instanceof LitString
          && ((LitString) ct.type).getString().equalsIgnoreCase("any")) {
        ctElse = ct;
        continue;
      }

      ExpressionUtil.visitLine(bc, ct.line);

      // pe.typeEqual(type)
      if (ct.type == null) {
        LitBoolean.TRUE.writeOut(bc, Expression.MODE_VALUE);
      } else {
        adapter.loadLocal(pe);
        ct.type.writeOut(bc, Expression.MODE_REF);
        adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
      }

      Label endIf = new Label();
      adapter.ifZCmp(Opcodes.IFEQ, endIf);

      catchBody(bc, adapter, ct, pe, lRef, true, true);

      adapter.visitJumpInsn(Opcodes.GOTO, endAllIf);
      adapter.visitLabel(endIf);
    }

    if (ctElse != null) {
      catchBody(bc, adapter, ctElse, pe, lRef, true, true);
    } else {
      // pc.setCatch(pe,true);
      adapter.loadArg(0);
      adapter.loadLocal(pe);
      adapter.push(false);
      adapter.push(false);
      adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.SET_CATCH3);

      adapter.loadLocal(pe);
      adapter.throwException();
    }
    adapter.visitLabel(endAllIf);

    // PageExceptionImpl old=pc.setCatch(old);
    adapter.loadArg(0);
    adapter.loadLocal(old);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.SET_CATCH_PE);
  }
Esempio n. 8
0
  public static byte[] createPojo(
      String className, ASMProperty[] properties, Class parent, Class[] interfaces, String srcName)
      throws PageException {
    className = className.replace('.', '/');
    className = className.replace('\\', '/');
    className = ListUtil.trim(className, "/");
    String[] inter = null;
    if (interfaces != null) {
      inter = new String[interfaces.length];
      for (int i = 0; i < inter.length; i++) {
        inter[i] = interfaces[i].getName().replace('.', '/');
      }
    }
    // CREATE CLASS
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(
        Opcodes.V1_6,
        Opcodes.ACC_PUBLIC,
        className,
        null,
        parent.getName().replace('.', '/'),
        inter);
    String md5;
    try {
      md5 = createMD5(properties);
    } catch (Throwable t) {
      md5 = "";
      t.printStackTrace();
    }

    FieldVisitor fv =
        cw.visitField(
            Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
            "_md5_",
            "Ljava/lang/String;",
            null,
            md5);
    fv.visitEnd();

    // Constructor
    GeneratorAdapter adapter =
        new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_OBJECT, null, null, cw);
    adapter.loadThis();
    adapter.invokeConstructor(toType(parent, true), CONSTRUCTOR_OBJECT);
    adapter.returnValue();
    adapter.endMethod();

    // properties
    for (int i = 0; i < properties.length; i++) {
      createProperty(cw, className, properties[i]);
    }

    // complexType src
    if (!StringUtil.isEmpty(srcName)) {
      GeneratorAdapter _adapter =
          new GeneratorAdapter(
              Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
              _SRC_NAME,
              null,
              null,
              cw);
      _adapter.push(srcName);
      _adapter.returnValue();
      _adapter.endMethod();
    }

    cw.visitEnd();
    return cw.toByteArray();
  }
Esempio n. 9
0
 @Override
 public void compile(GeneratorAdapter ga, int narg) {
   ga.push(value);
 }