Package anvil.codec

Examples of anvil.codec.ConstantPool


 
  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    if (_class instanceof NativeJava) {
      if (hasSplices()) {
        code.getstatic(pool.addFieldRef(_class.getTypeRef(pool),
          "__class__", "Lanvil/script/compiler/NativeClass;"));
        code.aload_first();
        context.compileArgumentList(getChilds(0));
        code.invokevirtual(pool.addMethodRef(
          "anvil/script/compiler/NativeClass", "newInstance",
          "(Lanvil/script/Context;[Lanvil/core/Any;)Lanvil/core/Any;"));
         
       } else {
         context.compileCall(_constructor, getChilds(0));
        
       }
      
    } else {
      int classindex = _class.getTypeRef(pool);
      boolean has_splices = hasSplices();
      if (has_splices) {
        code.getstatic(pool.addFieldRef(classindex, "m_"+_constructor.getName(), "Lanvil/script/Function;"));
        code.aload_first();
      }
      code.anew(classindex);
      code.dup();
      ClassType[] clazz_parents = _class.getEnclosingClasses();
      int n = clazz_parents.length;
      for(int i=0; i<n; i++) {
        context.accessInstance(_context, clazz_parents[i]);
      }
      code.invokespecial(_class.getConstructorReference(pool));
      if (has_splices) {
        context.compileArgumentList(getChilds(0));
        code.invokeinterface(pool.addInterfaceMethodRef("anvil/script/Function", "execute",
          "(Lanvil/script/Context;Lanvil/core/Any;[Lanvil/core/Any;)Lanvil/core/Any;"));
      } else {
        context.compileArgumentList(_constructor, getChilds(0));
        code.invokevirtual(_constructor.getTypeRef(pool));
      }
View Full Code Here


    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

    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();
View Full Code Here

 

  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;
View Full Code Here

 
  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++) {
View Full Code Here

    Code code = getCode();
    if (context == null || context == target) {
      code.self();
      return;
    }
    ConstantPool pool = code.getPool();
    ClassType[] parents = context.getEnclosingClasses();
    int n = parents.length;
    for(int i=0; i<n; i++) {
      ClassType parent = parents[i];
      if (parent == target) {
        code.self();
        code.getfield(pool.addFieldRef(context.getTypeRef(pool), "this$"+i,
          'L'+parent.getDescriptor()+';'));
        return;
      }
    }
    code.self();
View Full Code Here

 
 
  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    int clazz = pool.addClass("anvil/core/AnyList");
    int n = childs();
    code.anew(clazz);
    code.dup();
    code.iconst(n);
    code.anewarray(context.TYPE_ANY);
    for(int i=0; i<n; i++) {
      code.dup();
      code.iconst(i);
      getChild(i).compile(context, GET);
      code.aastore();
    }
    code.invokespecial(pool.addMethodRef(clazz, "<init>",
      "([Lanvil/core/Any;)V"));
    if (operation == context.GET_BOOLEAN) {
      context.any2boolean();
    }   
  }
View Full Code Here


  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    final Node child = _child;
    switch(child.typeOf()) {
    case Node.EXPR_VARIABLE:
      {
        final int tmp = code.addLocal();
        child.compile(context, GET);
        code.astore(tmp);
        child.compile(context, new Node() {
          public void compile(ByteCompiler context, int operation)
          {
            Code code_ = context.getCode();
            code_.aload(tmp);
            code_.invokevirtual(code_.getPool().addMethodRef(context.TYPE_ANY, "decrease", "()Lanvil/core/Any;"));
          }     
        });
        code.pop();
        code.aload(tmp);
        code.endLocal(tmp);
      }
      break;
     
    case Node.EXPR_ATTRIBUTE:
      {
        AttributeNode attr = (AttributeNode)child;
        attr.getChild().compile(context, GET);
        code.astring(attr.getAttribute());
        code.aload_first();
        code.invokestatic(pool.addMethodRef(context.TYPE_CONTEXT, "postdec",
          "(Lanvil/core/Any;Ljava/lang/String;Lanvil/script/Context;)Lanvil/core/Any;"));
      }
      break;
   
    case Node.EXPR_REFERENCE:
      {
        ReferenceNode ref = (ReferenceNode)child;
        ref.getLeft().compile(context, GET);
        ref.getRight().compile(context, GET);
        code.aload_first();
        code.invokestatic(pool.addMethodRef(context.TYPE_CONTEXT, "postdec",
          "(Lanvil/core/Any;Lanvil/core/Any;Lanvil/script/Context;)Lanvil/core/Any;"));
      }
      break;

    default:
View Full Code Here


  public void compileDescriptor(ByteCompiler context)
  {
    ClassRoom clazz = context.getClassRoom();
    ConstantPool pool = context.getPool();
    Parameter[] params = _params;
    int size = _size;
    int count = size;
    for(int i=0; i<size; i++) {
      if (params[i].value != null) {
View Full Code Here


  public void compileRef(ByteCompiler context)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    code.getstatic(pool.addFieldRef(context.TYPE_MODULE,
      "f_"+_function.getName(), "Lanvil/script/Function;"));
  }
View Full Code Here

TOP

Related Classes of anvil.codec.ConstantPool

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.