Examples of Code


Examples of anvil.codec.Code

    Integer slot = (Integer)_reflections.get(classname);
    if (slot == null) {
      slot = new Integer(_reflections.size());
      _reflections.put(classname, slot);
    }
    Code code = getCode();
    ConstantPool pool = getPool();
    int javafield = pool.addFieldRef(TYPE_MODULE, "java$"+slot,
      "Lanvil/core/reflect/Reflection;");
    int contextclass = pool.addClass(TYPE_CONTEXT);
    code.getstatic(javafield);
    Source isnull = code.if_null();
    code.getstatic(javafield);
    Source notnull = code.go_to();
    isnull.bind();
    code.aload_first();
    code.astring(classname);
    code.invokevirtual(pool.addMethodRef(contextclass, "reflect", "(Ljava/lang/String;)Lanvil/core/reflect/Reflection;"));
    code.dup();
    code.putstatic(javafield);
    notnull.bind();
    code.popop();
    return this;
  }
View Full Code Here

Examples of anvil.codec.Code

 

  public ByteCompiler location(Location location)
  {
    if (_debug && location != null) {
      Code code = getCode();
      Method method = code.getMethod();
      if (method.getClassRoom().getStatic() != method) {
        code.aload_first();
        code.iconst(location.getLine());
        code.invokevirtual(getPool().addMethodRef("anvil/script/StackFrameStack",
          "setLine", "(I)V"));
      }
    }
    return this;
  }
View Full Code Here

Examples of anvil.codec.Code

  }

 
  public boolean compile()
  {
    Code code;
   
    String classname = _envelope.getClassname();
    Address address = _envelope.getAddress();
    String pathinfo = address.getPathinfo();
    String classsig = _envelope.getDescriptor();
    TYPE_MODULE = classsig;
   
    ClassRoom clazz = new ClassRoom();
    pushClass(clazz);
    clazz.setClassname(classsig, null);
    clazz.setSuperClassname("anvil/script/compiler/CompiledModule");
    clazz.setAccessFlags(ACC_SUPER|ACC_PUBLIC);
    ConstantPool pool = clazz.getPool();

    _f_const = clazz.createField("_const", "[Lanvil/core/Any;", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);
    _f_text = clazz.createField("_text", "[[B", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);
    _f_switch = clazz.createField("_switch", "[Lanvil/java/util/Hashlist;", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);

    Method _m_const = clazz.createMethod("_const", "()V", ACC_PUBLIC|ACC_STATIC);
    Method _m_text = clazz.createMethod("_text", "()V", ACC_PUBLIC|ACC_STATIC);
    Method _m_switch = clazz.createMethod("_switch", "()V", ACC_PUBLIC|ACC_STATIC);
    Method _m_symbols = clazz.createMethod("_symbols", "()V", ACC_PUBLIC|ACC_STATIC);
    //Method _m_imports = clazz.createMethod("_imports", "()V", ACC_PUBLIC|ACC_STATIC);
   
    code = clazz.getStatic().getCode();
    //code.println("MODULE-START:"+classsig);
    code.invokestatic(_m_const.getIndex());
    code.invokestatic(_m_text.getIndex());
    code.invokestatic(_m_switch.getIndex());
    code.invokestatic(_m_symbols.getIndex());
    ((anvil.script.statements.ModuleStatement)getModule()).compile(this);
    //code.println("MODULE-END:"+classsig);
    code.vreturn();
   
    /* constants */
    code = _m_const.getCode();
    if (_constants.size() > 0) {
      code.iconst(_constants.size());
      code.anewarray(TYPE_ANY);
      Enumeration e = _constants.keys();
      for(int i=0; e.hasMoreElements(); i++) {
        code.dup();
        code.iconst(i);
        ((Any)e.nextElement()).toCode(code);
        code.aastore();
      }
      code.putstatic(_f_const);
    }
    code.vreturn();
   
    /* texts */
    code = _m_text.getCode();
    int getBytes = pool.addMethodRef("anvil/util/Conversions", "getBytes", "(Ljava/lang/String;)[B");
    if (_texts.size() > 0) {
      int size = _texts.size();
      code.iconst(size);
      code.anewarray("[[B", 1);
      Enumeration e = _texts.keys();
      for(int i=0; e.hasMoreElements(); i++) {
        code.dup();
        code.iconst(i);
        code.astring((String)e.nextElement());
        code.invokestatic(getBytes);
        code.aastore();
      }
      code.putstatic(_f_text);
    }
    code.vreturn();
   
    /* switchtables */
    code = _m_switch.getCode();
    int n = _switches.size();
    if (_switches.size() > 0 ) {
      int hashlistclazz = pool.addClass("anvil/java/util/Hashlist");
      int hashlistctor = pool.addMethodRef(hashlistclazz, "<init>", "()V");
      int hashlistadd = pool.addMethodRef(hashlistclazz, "add",
        "(Ljava/lang/Object;Ljava/lang/Object;)Lanvil/java/util/Hashlist;");
      int intclazz = pool.addClass("java/lang/Integer");
      int intclazzctor = pool.addMethodRef(intclazz, "<init>", "(I)V");
      code.iconst(n);
      code.anewarray(hashlistclazz);
      for(int i=0; i<n; i++) {
        code.dup();
        code.iconst(i);
        code.anew(hashlistclazz);
        code.dup();
        code.invokespecial(hashlistctor);
        int c = 0;
        Enumeration keys = (Enumeration)_switches.elementAt(i);
        while(keys.hasMoreElements()) {
          Any key = (Any)keys.nextElement();
          if (key != SwitchStatement.DEFAULT_MARKER) {
            key.toCode(code);
            code.anew(intclazz);
            code.dup();
            code.iconst(c++);
            code.invokespecial(intclazzctor);
            code.invokevirtual(hashlistadd);
          }
        }
        code.aastore();
      }
      code.putstatic(_f_switch);
    }
    code.vreturn();

    /* methodindices */
    code = _m_symbols.getCode();
    if (_symbols.size() > 0) {
      int register = pool.addMethodRef(pool.addClass("anvil/core/Register"),
        "register", "(Ljava/lang/String;)I");
      BindingEnumeration e = _symbols.keysAndElements();
      while(e.hasMoreElements()) {
        int index = ((Integer)e.nextKey()).intValue();
        String name = (String)e.nextElement();
        Field field = clazz.createField("r_"+name, "I", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);
        code.astring(name);
        code.invokestatic(register);
        code.putstatic(field);
      }
    }
    code.vreturn();
   
    /* reflectionfields */
    if (_reflections.size() > 0) {
      BindingEnumeration e = _reflections.keysAndElements();
      while(e.hasMoreElements()) {
View Full Code Here

Examples of anvil.codec.Code

  private static class ParentNode extends Node
  {
   
    public void compile(ByteCompiler context, int operation)
    {
      Code code = context.getCode();
      ConstantPool pool = code.getPool();
      code.aload_first();
      code.invokevirtual(pool.addMethodRef(context.TYPE_CONTEXT, "frame",
        "()Lanvil/script/StackFrame;"));
      code.invokevirtual(pool.addMethodRef("anvil/script/StackFrame", "peek",
        "()Lanvil/core/Any;"));
    }
View Full Code Here

Examples of anvil.codec.Code

  private class FrameNode extends Node
  {
   
    public void compile(ByteCompiler context, int operation)
    {
      Code code = context.getCode();
      ConstantPool pool = code.getPool();
      code.aload(_framelocal);
    }
View Full Code Here

Examples of anvil.codec.Code

  }
 

  public void compileArgumentList(CompilableFunction function, Node[] parameters, int contextindex)
  {
    Code code = getCode();
    ConstantPool pool = getPool();
    int max = (parameters != null) ? parameters.length : 0;
    int c = 0;
    int n = function.getParameterCount();
    for(int i=0; i<n; i++) {
      Any defaultValue = function.getParameterDefault(i);
      switch(function.getParameterType(i)) {
      case CompilableFunction.PARAMETER_ANY:
        if (c<max) {
          parameters[c++].compile(this, Node.GET);
        } else {
          if (defaultValue != null) {
            constant(defaultValue, true);
          } else {
            code.aconst_null();
          }
        }
        break;

      case CompilableFunction.PARAMETER_OBJECT:
        if (c<max) {
          parameters[c++].compile(this, Node.GET);
          code.invokevirtual(pool.addMethodRef(TYPE_ANY,
            "toObject", "()Ljava/lang/Object;"));
        } else {
          if (defaultValue != null) {
            constant(defaultValue, true);
            code.invokevirtual(pool.addMethodRef(TYPE_ANY,
              "toObject", "()Ljava/lang/Object;"));
          } else {
            code.aconst_null();
          }
        }
        break;

      case CompilableFunction.PARAMETER_BOOLEAN:
        if (c<max) {
          Node node = parameters[c++];
          if (node.isConstant()) {
            code.iconst(node.eval().toBoolean());
          } else {
            node.compile(this, Node.GET_BOOLEAN);
          }
        } else {
          if (defaultValue != null) {
            code.iconst(defaultValue.toBoolean());
          } else {
            code.iconst(false);
          }
        }
        break;

      case CompilableFunction.PARAMETER_INT:
        if (c<max) {
          Node node = parameters[c++];
          if (node.isConstant()) {
            code.iconst(node.eval().toInt());
          } else {
            node.compile(this, Node.GET);
            code.invokevirtual(pool.addMethodRef(TYPE_ANY,
              "toInt", "()I"));           
          }
        } else {
          if (defaultValue != null) {
            code.iconst(defaultValue.toInt());
          } else {
            code.iconst(0);
          }
        }
        break;

      case CompilableFunction.PARAMETER_LONG:
        if (c<max) {
          Node node = parameters[c++];
          if (node.isConstant()) {
            code.lconst(node.eval().toLong());
          } else {
            node.compile(this, Node.GET);
            code.invokevirtual(pool.addMethodRef(TYPE_ANY,
              "toLong", "()J"))
          }
        } else {
          if (defaultValue != null) {
            code.lconst(defaultValue.toLong());
          } else {
            code.lconst(0);
          }
        }
        break;

      case CompilableFunction.PARAMETER_DOUBLE:
        if (c<max) {
          Node node = parameters[c++];
          if (node.isConstant()) {
            code.dconst(node.eval().toDouble());
          } else {
            node.compile(this, Node.GET);
            code.invokevirtual(pool.addMethodRef(TYPE_ANY,
              "toDouble", "()D"));
          }
        } else {
          if (defaultValue != null) {
            code.dconst(defaultValue.toDouble());
          } else {
            code.dconst(0.0);
          }
        }
        break;

      case CompilableFunction.PARAMETER_STRING:
        if (c<max) {
          Node node = parameters[c++];
          if (node.isConstant()) {
            code.astring(node.eval().toString());
          } else {
            node.compile(this, Node.GET);
            code.invokevirtual(pool.addMethodRef(TYPE_OBJECT,
              "toString", "()Ljava/lang/String;"));
          }
        } else {
          if (defaultValue != null) {
            code.astring(defaultValue.toString());
          } else {
            code.aconst_null();
          }
        }
        break;

      case CompilableFunction.PARAMETER_ARRAY:
        {
          int arrayclazz = pool.addClass("anvil/core/Array");
          int appendmethod = pool.addMethodRef(arrayclazz, "append",
            "(Lanvil/core/Any;)Lanvil/core/Array;");
          code.anew(arrayclazz);
          code.dup();
          code.invokespecial(pool.addMethodRef(arrayclazz, "<init>", "()V"));
          for(; c < max; c++) {
            code.dup();
            parameters[c].compile(this, Node.GET);
            code.invokevirtual(appendmethod);
          }
        }
        break;

      case CompilableFunction.PARAMETER_ANYLIST:
      case CompilableFunction.PARAMETER_REST:
        if (c >= max) {
          code.getstatic(pool.addFieldRef(TYPE_ANY, "EMPTY_TUPLE",
            "Lanvil/core/AnyTuple;"));
        } else {
          int tupleclazz = pool.addClass("anvil/core/AnyTuple");
          int appendmethod = pool.addMethodRef(tupleclazz, "append",
            "(Lanvil/core/Any;)Lanvil/core/Array;");
          code.anew(tupleclazz);
          code.dup();
          code.iconst(max - c);
          code.anewarray(TYPE_ANY);
          int index = 0;
          for(; c < max; c++) {
            code.dup();
            code.iconst(index++);
            parameters[c].compile(this, Node.GET);
            code.aastore();
          }
          code.invokespecial(pool.addMethodRef(tupleclazz, "<init>",
            "([Lanvil/core/Any;)V"));
        }
        break;

      case CompilableFunction.PARAMETER_LIST:
        int len = max - c;
        if (len <= 0) {
          code.getstatic(pool.addFieldRef(TYPE_ANY, "ARRAY0",
            "[Lanvil/core/Any;"));
        } else {
          code.iconst(len);
          code.anewarray(TYPE_ANY);
          int index = 0;
          for(; c < max; c++) {
            code.dup();
            code.iconst(index++);
            parameters[c].compile(this, Node.GET);
            code.aastore();
          }
        }
        break;

      case CompilableFunction.PARAMETER_CONTEXT:
        if (contextindex == -1) {
          code.aload_first();
        } else {
          code.aload(contextindex);
        }
        break;
      }
    }
  }
View Full Code Here

Examples of anvil.codec.Code

  }

 
  public void compileArgumentList(Node[] parameters)
  {
    Code code = getCode();
    ConstantPool pool = getPool();
   
    int n = (parameters != null) ? parameters.length : 0;
    if (n == 0) {
      code.getstatic(pool.addFieldRef(TYPE_ANY, "ARRAY0",
        "[Lanvil/core/Any;"));
      return;
    }

    boolean hasSplices = false;
    for(int i=0; i<n; i++) {
      if (parameters[i].typeOf() == Node.EXPR_SPLICE) {
        hasSplices = true;
        break;
      }
    }

    if (hasSplices) {
      int clazz = pool.addClass("anvil/script/ParameterList");
      int splicemethod = pool.addMethodRef(clazz, "splice",
        "(Lanvil/core/Any;)Lanvil/script/ParameterList;");
      int addmethod = pool.addMethodRef(clazz, "add",
        "(Lanvil/core/Any;)Lanvil/script/ParameterList;");
      code.anew(clazz);
      code.dup();
      code.iconst(n);
      code.invokespecial(pool.addMethodRef(clazz, "<init>", "(I)V"));
      for(int i=0; i<n; i++) {
        parameters[i].compile(this, Node.GET);
        if (parameters[i].typeOf() == Node.EXPR_SPLICE) {
          code.invokevirtual(splicemethod);
        } else {
          code.invokevirtual(addmethod);
        }
      }
      code.invokevirtual(pool.addMethodRef(clazz, "toArray",
        "()[Lanvil/core/Any;"));
    } else {
      code.iconst(n);
      code.anewarray(TYPE_ANY);
      for(int i=0; i<n; i++) {
        code.dup();
        code.iconst(i);
        parameters[i].compile(this, Node.GET);
        code.aastore();
      }
    }
  } 
View Full Code Here

Examples of anvil.codec.Code

  }


  public void any2boolean()
  {
    Code code = getCode();
    code.invokevirtual(code.getPool().addMethodRef("anvil/core/Any", "toBoolean", "()Z"));
  }
View Full Code Here

Examples of anvil.codec.Code

  }


  public void boolean2any()
  {
    Code code = getCode();
    code.invokestatic(code.getPool().addMethodRef(
      "anvil/script/compiler/CompiledModule", "b2a", "(Z)Lanvil/core/Any;"));
  }
View Full Code Here

Examples of anvil.codec.Code



  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    int clazz = pool.addClass("anvil/core/Array");
    code.anew(clazz);
    code.dup();
    code.invokespecial(pool.addMethodRef(clazz, "<init>", "()V"));
    int append1 = pool.addMethodRef(clazz, "append",
          "(Lanvil/core/Any;)Lanvil/core/Array;");
    int append2 = pool.addMethodRef(clazz, "append",
          "(Lanvil/core/Any;Lanvil/core/Any;)Lanvil/core/Array;");
    int n = childs();
    for(int i=0; i<n; i++) {
      Node node = getChild(i);
      if (node.typeOf() == Node.EXPR_MAPPING) {
        MappingNode mapnode = (MappingNode)node;
        mapnode.getLeft().compile(context, GET);
        mapnode.getRight().compile(context, GET);
        code.invokevirtual(append2);
      } else {
        node.compile(context, GET);
        code.invokevirtual(append1);
      }
    }
    if (operation == GET_BOOLEAN) {
      context.any2boolean();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.