/*
 * Copyright (C) 2009-2011 Mathias Doenitz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.parboiled.transform.process;

import com.github.parboiled1.grappa.annotations.WillBeFinal;
import com.google.common.base.Preconditions;
import me.qmx.jitescript.util.CodegenUtils;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.parboiled.Rule;
import org.parboiled.annotations.Label;
import org.parboiled.transform.ParserClassNode;
import org.parboiled.transform.RuleMethod;

import javax.annotation.Nonnull;

import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.IFNULL;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;

/**
 * Adds automatic labelling code before the return instruction.
 */
@WillBeFinal(version = "1.1")
public class LabellingGenerator
    implements RuleMethodProcessor
{

    @Override
    public boolean appliesTo(@Nonnull final ParserClassNode classNode,
        @Nonnull final RuleMethod method)
    {
        Preconditions.checkNotNull(classNode, "classNode");
        Preconditions.checkNotNull(method, "method");
        return !method.hasDontLabelAnnotation();
    }

    @Override
    public void process(@Nonnull final ParserClassNode classNode,
        @Nonnull final RuleMethod method)
        throws Exception
    {
        Preconditions.checkNotNull(classNode, "classNode");
        Preconditions.checkNotNull(method, "method");
        // super methods have flag moved to the overriding method
        Preconditions.checkState(!method.isSuperMethod());

        final InsnList instructions = method.instructions;

        AbstractInsnNode ret = instructions.getLast();
        while (ret.getOpcode() != ARETURN)
            ret = ret.getPrevious();


        final LabelNode isNullLabel = new LabelNode();
        // stack: <rule>
        instructions.insertBefore(ret, new InsnNode(DUP));
        // stack: <rule> :: <rule>
        instructions.insertBefore(ret, new JumpInsnNode(IFNULL, isNullLabel));
        // stack: <rule>
        instructions.insertBefore(ret, new LdcInsnNode(getLabelText(method)));
        // stack: <rule> :: <labelText>
        instructions.insertBefore(ret, new MethodInsnNode(INVOKEINTERFACE,
            CodegenUtils.p(Rule.class), "label",
            CodegenUtils.sig(Rule.class, String.class), true));
        // stack: <rule>
        instructions.insertBefore(ret, isNullLabel);
        // stack: <rule>
    }

    public static String getLabelText(final RuleMethod method) {
        if (method.visibleAnnotations == null)
            return method.name;

        AnnotationNode annotation;
        for (final Object annotationObj: method.visibleAnnotations) {
            annotation = (AnnotationNode) annotationObj;
            if (!annotation.desc.equals(CodegenUtils.ci(Label.class)))
                continue;
            if (annotation.values == null)
                continue;
            Preconditions.checkState("value".equals(annotation.values.get(0)));
            final String labelValue = (String) annotation.values.get(1);
            return labelValue.isEmpty() ? method.name : labelValue;
        }
        return method.name;
    }
}
@@ -680,31 +680,27 @@
 ppa.
-annotations.WillBeFinal
+transform.CodeBlock
 ;%0Aim
@@ -954,186 +954,13 @@
 ree.
-InsnNode;%0Aimport org.objectweb.asm.tree.JumpInsnNode;%0Aimport org.objectweb.asm.tree.LabelNode;%0Aimport org.objectweb.asm.tree.LdcInsnNode;%0Aimport org.objectweb.asm.tree.MethodInsn
+Label
 Node
@@ -1206,158 +1206,8 @@
 URN;
-%0Aimport static org.objectweb.asm.Opcodes.DUP;%0Aimport static org.objectweb.asm.Opcodes.IFNULL;%0Aimport static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
 %0A%0A/*
@@ -1280,44 +1280,20 @@
  */%0A
-@WillBeFinal(version = %221.1%22)%0A
 public
+ final
  cla
@@ -2136,16 +2136,20 @@
 Node ret
+Insn
  = instr
@@ -2185,16 +2185,20 @@
 ile (ret
+Insn
 .getOpco
@@ -2233,14 +2233,22 @@
  ret
+Insn
  = ret
+Insn
 .get
@@ -2260,17 +2260,16 @@
 ous();%0A%0A
-%0A
         
@@ -2284,23 +2284,17 @@
 belNode 
-isNullL
+l
 abel = n
@@ -2321,197 +2321,102 @@
     
-// stack: %3Crule%3E%0A        instructions.insertBefore(ret, new InsnNode(DUP));%0A        // stack: %3Crule%3E :: %3Crule%3E%0A        instructions.insertBefore(ret, new JumpInsnNode(IFNULL, isNullL
+final CodeBlock block = CodeBlock.newCodeBlock()%0A            .dup()%0A            .ifnull(l
 abel)
-);
 %0A   
@@ -2424,79 +2424,16 @@
     
-// stack: %3Crule%3E%0A        instructions.insertBefore(ret, new LdcInsnNode
+    .ldc
 (get
@@ -2450,18 +2450,16 @@
 method))
-);
 %0A       
@@ -2463,127 +2463,29 @@
     
-// stack: %3Crule%3E :: %3ClabelText%3E%0A        instructions.insertBefore(ret, new MethodInsnNode(INVOKEINTERFACE,%0A            
+    .invokeinterface(
 Code
@@ -2517,16 +2517,20 @@
 label%22,%0A
+    
         
@@ -2579,42 +2579,37 @@
 ass)
-, true));%0A        // stack: %3Crule%3E
+)%0A            .label(label);%0A
 %0A   
@@ -2646,48 +2646,42 @@
 (ret
-, isNullLabel);%0A        // stack: %3Crule%3E
+Insn, block.getInstructionList());
 %0A   
@@ -2864,16 +2864,17 @@
 tation;%0A
+%0A
         
@@ -2992,16 +2992,17 @@
 ionObj;%0A
+%0A
         
@@ -3082,32 +3082,33 @@
       continue;%0A
+%0A
             if (
@@ -3156,24 +3156,25 @@
   continue;%0A
+%0A
             
@@ -3392,16 +3392,25 @@
       %7D%0A
+        %0A
         

use CodeBlock