Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.Code


      {
         MethodRecord pMeth = iter.next();
         // _logger.log(Level.INFO, "Marking sub method " + pMeth.iMethod.getName());
         pMeth.iClassRecord.markMethod(pMeth, false);
      }
      Code pCodeAttrib = iMethod.getCode();
      if (pCodeAttrib == null) return;
      byte [] aCode = pCodeAttrib.getCode();
      if (aCode == null) return;
      CodeUtilities pUtils = new CodeUtilities(iMethod.getName().toString(),
         aClassFile, aBinary);
     
      pUtils.processCalls(aCode, aClassFile, aBinary);
View Full Code Here


  };
   * @throws IOException
 
  */
  private void bytecode(Set<String> incSet, Method method) throws IOException {
    Code code= method.getCode();
    methodName= getMethodSignature("1 bc "+jc.getClassName()+"."+method.getName()+" "+method.getSignature());
    if (code == null) {
      // is abstract or native method
      outc.println("*/");
      if (method.isNative()) {
                totalMethods++;
                totalBClength+=3;

                if (methodName.endsWith("_clinitHaikuMagic_V")) {
                    conditions.add(Condition.CLINIT.desc("found clinitHaikuMagic() in: "+classname));
                    outc.printf("\n");
                    for (Clinit desc : Clinit.set()) {
                        String include=desc.getClassName().replace('.', '/');
                        outc.println("// clinit           "+include);
                        includeThis(incSet, include);
                        //outc.printf("extern const ByteCode %s__clinit__V;\n", mangle(desc.getClassName()));
                    }
                    /**
                        typedef struct {
                            uint8_t max_stack; int8_t purLocals; uint8_t purParams;
                       
                            OP_bc oph9; OPadr fh9;  // <clinit>
                            OP_bc oph12; OPadr fh12;  // <clinit>
                            OP_bc oph15; OPadr fh15;  // <clinit>
                            OP_bc oph18; OPadr fh18;  // <clinit>
                            OP_bc op0;                                                             // 0:    return
                        }            haiku_avr_lib_arduino_HaikuMicroKernelEx_clinitHaikuMagic_V_t;
                        extern const haiku_avr_lib_arduino_HaikuMicroKernelEx_clinitHaikuMagic_V_t haiku_avr_lib_arduino_HaikuMicroKernelEx_clinitHaikuMagic_V;

                    public static synchronized void clinitHaikuMagic()
                    Code(max_stack = 0, max_locals = 0, code_length = 1)
                    * /
                    #undef  JMETHOD
                    #define JMETHOD haiku_avr_lib_arduino_HaikuMicroKernelEx_clinitHaikuMagic_V
                    const           haiku_avr_lib_arduino_HaikuMicroKernelEx_clinitHaikuMagic_V_t JMETHOD PROGMEM ={
                    0+0 +2,    0,    0,    // max_stack, purLocals, purParams

                    OP_INVOKESTATIC,     ADR(haiku_avr_AVRDefines__clinit__V),  // haiku.avr.AVRDefines.<clinit>
                    OP_INVOKESTATIC,     ADR(java_lang_Math__clinit__V),    // java.lang.Math.<clinit>
                    OP_INVOKESTATIC,     ADR(haikuvm_bench_JUnit1__clinit__V),  // haikuvm.bench.JUnit1.<clinit>
                    OP_INVOKESTATIC,     ADR(java_lang_Double__clinit__V),  // java.lang.Double.<clinit>
                    OP_RETURN,                                                             // 0:    return
                    };
                    */

                    outh.printf("typedef struct {\n");
                    outh.printf("    uint8_t max_stack; int8_t purLocals; uint8_t purParams;\n");
                    for (Clinit desc : Clinit.set()) {
                        totalBClength+=3;
                        outh.printf("\tOP_bc oph%d; OPadr fh%d;  // <clinit>\n", totalBClength, totalBClength);
                    }
                    outh.printf("    OP_bc op0;                                                             //       return\n");
                    outh.printf("}            %s_t;\n", methodName, methodName);
                    outh.printf("extern const %s_t %s;\n", methodName, methodName);
                    outh.printf("\n");
                   
                    outc.printf("const           %s_t %s PROGMEM ={\n", methodName, methodName);
                    outc.printf("2, 0, 0,    // max_stack, purLocals, purParams\n");
                    outc.printf("\n");
                    for (Clinit desc : Clinit.set()) {
                        outc.printf("OP_INVOKESTATIC,     ADR(%s__clinit__V),   // %s.<clinit>\n", mangle(desc.getClassName()), desc.getClassName());
                        BC2IDX.put("INVOKESTATIC");
                    }
                    outc.printf("OP_RETURN,                                                             //       return\n");
                    outc.printf("};\n");
                } else {
                    outh.printf("#ifndef %s\n", "native_"+methodName);
                    outh.printf("extern const NativCode %s;\n", methodName);
                    outh.printf("extern void            %s(void);\n",    "native_"+methodName);
                    outh.printf("#endif\n");
                    outh.printf("\n");
                   
                    outc.printf("#ifndef %s\n", "native_"+methodName);
                    outc.printf("const NativCode %s PROGMEM ={0xff, &%s};\n", methodName, "native_"+methodName);
                    outc.printf("#endif\n");
                   
                    totalBClength+=2;
                    haikuJNIc.nativeInfo(classname, method, methodName);
                }
      }
      return;
    }

    int remap[]=makeArgumentReMap(method);
    int start=0, end;
    String str =code.toString(true);
    byte bc[]=code.getCode();
    totalBClength+=bc.length+3 + 2 /*for pointer in functionTable*/;
    list=str.split("\n");
    for (int k = 0; k < list.length; k++) {
      String line=list[k];
      if (line.matches("^\\d+:.*")) {
        start=Integer.parseInt(line.replaceAll(":.*", ""));
                end=bc.length;
                if (k+1<list.length) {
                    try {
                        end = Integer.parseInt(list[k+1].replaceAll(":.*", ""));
                    } catch (NumberFormatException e) {
                    }
                }
        if (start==0) {
          outc.println("*/");
         
          outh.printf("typedef struct {\n");
          outh.printf("\tuint8_t max_stack; int8_t purLocals; uint8_t purParams;\n");
          outh.printf("\n");



          totalMethods++;
          outc.printf("#undef  JMETHOD\n");
          outc.printf("#define JMETHOD %s\n", methodName);
         
          /*
           * max_stack: max inner method working stack usage (without purLocals and purParams)
           * purParams: only stack usage of parameters (without purLocals)
           * purLocals: all local variables of the method (without purParams)
           *
           * Comment:
           * getlocal(idx) addresses the stack beginning with the first (leftmost) Parameter position getLocal(0)
           *   It covers both parameters and local variables.
           */
          outc.printf("const           %s_t JMETHOD PROGMEM ={\n", methodName);
          outc.printf("%d+(2)+%d, %4d, %4d,    // MaxLocals+(lsp+pc)+MaxStack, purLocals, purParams\n", code.getMaxLocals(), code.getMaxStack(), code.getMaxLocals()-getParams(method), getParams(method));
          outc.printf("\n");
          if (methodName.endsWith("_clinitHaikuMagic_V")) {
                        conditions.add(Condition.CLINIT.desc("found clinitHaikuMagic() in: "+classname));
            for (Clinit desc : Clinit.set()) {
              totalBClength+=3;
              outh.printf("\tOP_bc oph%d; OPadr fh%d;  // <clinit>\n", totalBClength, totalBClength);
              outc.printf("OP_INVOKESTATIC,     ADR(%s__clinit__V),  // %s.<clinit>\n", mangle(desc.getClassName()), desc.getClassName());
              BC2IDX.put("INVOKESTATIC");
            }
          }
          pc+=4;
        }
        String[] token= list[k].split("[ \t,]+");
        if (token.length<=2 || !supressPanicSupport(token[2])) {
          String bcode=token[1];
          if (bcode.equals("wide")) {
            k++;         
            end = Integer.parseInt(list[k+1].replaceAll(":.*", ""));
          }
          outh.println("\t"+compileJ2Typedef(list[k], bc, start, end));
          outc.println(compileJ2Image(list[k], bc, start, end, remap));
        }
        start=999;
      } else {
        if (start==0) outc.println(line);
      }
    }
    outh.printf("}            %s_t;\n", methodName);
    outh.printf("extern const %s_t %s;\n", methodName, methodName);
    outh.printf("\n");
    outc.printf("};\n");

   
    CodeException[] et = code.getExceptionTable();
    ConstantPool cp=jc.getConstantPool();
    Constant[] cons=cp.getConstantPool();

    for (int i = 0; i < et.length; i++) {
      int ct=et[i].getCatchType();
View Full Code Here

          new Clinit(classname, level.length());
        }
              if (method.isNative() ) {
                  continue;
              }
        Code code= method.getCode();
        String str =code.toString(true);
        String[] list=str.split("\n");
        for (int k = 0; k < list.length; k++) {
          String jcode=list[k];
          String[] token= jcode.split("[ \t,]+", 9);
          if (token[0].endsWith(":")) { //[39:, iconst_0]
View Full Code Here

      Method method=methods[i];
      outc.println();
      outc.println("/**");
      outc.println(method);
 
      Code code = method.getCode();
      if (code != null) { // Non-abstract method
        String str =code.toString(false);
        list=str.split("\n");
        for (int j = 0; j < list.length; j++) {
          String line=list[j];
          if (line.matches("^\\d+:.*")) {
            outc.println("*/");       
 
            access = "access__"+Utility.accessToString(method.getAccessFlags()).replace(' ', '_')+"_";
            String thisParam="+1";
            if (access.indexOf("static")>=0) {
              thisParam="";
            }
            methodName= process(jc.getClassName()+"."+method.getName())+", "+process(method.getSignature());
            String methodDesc=new Formatter().format("%s, %s, %d, %d, %d, %d%s", access, methodName, code.getMaxStack(), code.getMaxLocals(), code.getCode().length, method.getArgumentTypes().length, thisParam).toString();
            String meth[]=methodName.split("[, ]+");
            totalBClength+=code.getCode().length;
            if (methodName.indexOf(" main,")>=0) {
              haikuConfigh.printf("methodExtern(%s)\n", methodDesc);
              //haikuInit.printf("  {2, \"%s\", methodName(%s)}, // main\n", classname, methodDesc);
            }
            if (thisParam.length()>0) {
View Full Code Here

        replaceWith = replacer;
    }
   
    @Override
    public Code patch(Code c, CPoolMap map){
        Code newCode = map.applyTo(replaceWith, newCPs);
        // Retain old attributes for now.
        newCode.setAttributes(c.getAttributes());
        // Set all old code CPs to 0.
        for(CodePointHook cph : oldCPs)
            cph.codePoint = 0;
        return newCode;
    }
View Full Code Here

     * @param patches The already applied attribute patches.
     * @param map The cpool map.
     */
    public static void generate(Attribute[] old, Attribute[] new_,
            List<AttributePatch> patches, CPoolMap map){
        Code oldCode = null;
        for(Attribute a : old){
            if(a instanceof Code){
                oldCode = (Code)a;
                break;
            }
        }
        Code newCode = null;
        for(Attribute a : new_){
            if(a instanceof Code){
                newCode = (Code)a;
                break;
            }
View Full Code Here

   
    @Override
    public List<Attribute> patch(List<Attribute> attribs, CPoolMap map){
        for(int i = 0; i < attribs.size(); i++){
            if(attribs.get(i) instanceof Code){
                Code newCode = this.patch((Code)attribs.get(i), map);
                // Aaand apply attribs patch...
                List<Attribute> codeAttribs = new ArrayList<Attribute>(
                        Arrays.asList(newCode.getAttributes()));
                codeAttribs = attributesPatch.patch(codeAttribs, map);
                newCode.setAttributes(codeAttribs.toArray(
                        new Attribute[codeAttribs.size()]));
                // Aaand add and return.
                attribs.set(i, newCode);
                return attribs;
            }
View Full Code Here

          String lvtname = ((ConstantUtf8) cp.getConstant(lvt.getNameIndex())).getBytes();
          if (! lvtname.equals("LocalVariableTable")){
            throw new ClassConstraintException("The LocalVariableTable attribute '"+tostring(lvt)+"' is not correctly named 'LocalVariableTable' but '"+lvtname+"'.");
          }

          Code code = obj;

          //In JustIce, the check for correct offsets into the code array is delayed to Pass 3a.
          LocalVariable[] localvariables = lvt.getLocalVariableTable();

          for (int i=0; i<localvariables.length; i++){
            checkIndex(lvt, localvariables[i].getNameIndex(), CONST_Utf8);
            String localname = ((ConstantUtf8) cp.getConstant(localvariables[i].getNameIndex())).getBytes();
            if (!validJavaIdentifier(localname)){
              throw new ClassConstraintException("LocalVariableTable '"+tostring(lvt)+"' references a local variable by the name '"+localname+"' which is not a legal Java simple name.");
            }

            checkIndex(lvt, localvariables[i].getSignatureIndex(), CONST_Utf8);
            String localsig  = ((ConstantUtf8) (cp.getConstant(localvariables[i].getSignatureIndex()))).getBytes(); // Local signature(=descriptor)
            Type t;
            try{
              t = Type.getType(localsig);
            }
            catch (ClassFormatException cfe){
              throw new ClassConstraintException("Illegal descriptor (==signature) '"+localsig+"' used by LocalVariable '"+tostring(localvariables[i])+"' referenced by '"+tostring(lvt)+"'.", cfe);
            }
            int localindex = localvariables[i].getIndex();
            if ( ( (t==Type.LONG || t==Type.DOUBLE)? localindex+1:localindex) >= code.getMaxLocals()){
              throw new ClassConstraintException("LocalVariableTable attribute '"+tostring(lvt)+"' references a LocalVariable '"+tostring(localvariables[i])+"' with an index that exceeds the surrounding Code attribute's max_locals value of '"+code.getMaxLocals()+"'.");
            }

            try{
              localVariablesInfos[method_number].add(localindex, localname, localvariables[i].getStartPC(), localvariables[i].getLength(), t);
            }
View Full Code Here

                + "</A></H4>");
        /* Handle different attributes
         */
        switch (tag) {
            case ATTR_CODE:
                Code c = (Code) attribute;
                // Some directly printable values
                file.print("<UL><LI>Maximum stack size = " + c.getMaxStack()
                        + "</LI>\n<LI>Number of local variables = " + c.getMaxLocals()
                        + "</LI>\n<LI><A HREF=\"" + class_name + "_code.html#method"
                        + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
                // Get handled exceptions and list them
                CodeException[] ce = c.getExceptionTable();
                int len = ce.length;
                if (len > 0) {
                    file.print("<P><B>Exceptions handled</B><UL>");
                    for (int i = 0; i < len; i++) {
                        int catch_type = ce[i].getCatchType(); // Index in constant pool
View Full Code Here

            if (i < args.length - 1) {
                file.print(",&nbsp;");
            }
        }
        file.println(")</B></P>");
        Code c = null;
        byte[] code = null;
        if (attributes.length > 0) {
            file.print("<H4>Attributes</H4><UL>\n");
            for (int i = 0; i < attributes.length; i++) {
                byte tag = attributes[i].getTag();
                if (tag != ATTR_UNKNOWN) {
                    file.print("<LI><A HREF=\"" + class_name + "_attributes.html#method"
                            + method_number + "@" + i + "\" TARGET=Attributes>"
                            + ATTRIBUTE_NAMES[tag] + "</A></LI>\n");
                } else {
                    file.print("<LI>" + attributes[i] + "</LI>");
                }
                if (tag == ATTR_CODE) {
                    c = (Code) attributes[i];
                    Attribute[] attributes2 = c.getAttributes();
                    code = c.getCode();
                    file.print("<UL>");
                    for (int j = 0; j < attributes2.length; j++) {
                        tag = attributes2[j].getTag();
                        file.print("<LI><A HREF=\"" + class_name + "_attributes.html#" + "method"
                                + method_number + "@" + i + "@" + j + "\" TARGET=Attributes>"
View Full Code Here

TOP

Related Classes of org.apache.bcel.classfile.Code

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.