
org.parboiled.transform.process.CachingGenerator Maven / Gradle / Ivy
/*
* Copyright (c) 2009-2010 Ken Wenzel and Mathias Doenitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.parboiled.transform.process;
import com.github.parboiled1.grappa.matchers.base.Matcher;
import com.github.parboiled1.grappa.matchers.wrap.ProxyMatcher;
import com.github.parboiled1.grappa.transform.CodeBlock;
import com.google.common.base.Preconditions;
import me.qmx.jitescript.util.CodegenUtils;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.LabelNode;
import org.parboiled.Rule;
import org.parboiled.transform.CacheArguments;
import org.parboiled.transform.ParserClassNode;
import org.parboiled.transform.RuleMethod;
import javax.annotation.Nonnull;
import java.util.HashMap;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ARETURN;
/**
* Wraps the method code with caching and proxying constructs.
*/
public final class CachingGenerator
implements RuleMethodProcessor
{
private ParserClassNode classNode;
private RuleMethod method;
private InsnList instructions;
private AbstractInsnNode retInsn;
private String cacheFieldName;
@Override
public boolean appliesTo(@Nonnull final ParserClassNode classNode,
@Nonnull final RuleMethod method)
{
Preconditions.checkNotNull(classNode, "classNode");
Preconditions.checkNotNull(method, "method");
return method.hasCachedAnnotation();
}
@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());
this.classNode = classNode;
this.method = method;
instructions = method.instructions;
retInsn = instructions.getLast();
while (retInsn.getOpcode() != ARETURN)
retInsn = retInsn.getPrevious();
CodeBlock block;
block = CodeBlock.newCodeBlock();
generateCacheHitReturn(block);
generateStoreNewProxyMatcher(block);
instructions.insert(block.getInstructionList());
block = CodeBlock.newCodeBlock();
generateArmProxyMatcher(block);
generateStoreInCache(block);
instructions.insertBefore(retInsn, block.getInstructionList());
}
// if ( != null) return ;
private void generateCacheHitReturn(final CodeBlock block)
{
generateGetFromCache(block);
final LabelNode cacheMiss = new LabelNode();
block.dup()
.ifnull(cacheMiss)
.areturn()
.label(cacheMiss)
.pop();
}
private void generateGetFromCache(final CodeBlock block)
{
final Type[] paramTypes = Type.getArgumentTypes(method.desc);
cacheFieldName = findUnusedCacheFieldName();
// if we have no parameters we use a simple Rule field as cache,
// otherwise a HashMap
final String cacheFieldDesc = paramTypes.length == 0
? CodegenUtils.ci(Rule.class)
: CodegenUtils.ci(HashMap.class);
final FieldNode field = new FieldNode(ACC_PRIVATE, cacheFieldName,
cacheFieldDesc, null, null);
classNode.fields.add(field);
block.aload(0).getfield(classNode.name, cacheFieldName, cacheFieldDesc);
if (paramTypes.length == 0)
return; // if we have no parameters we are done
// generate: if ( == null) = new HashMap
© 2015 - 2025 Weber Informatics LLC | Privacy Policy