<commit_before>package org.deuce.transform.asm;

import org.deuce.objectweb.asm.AnnotationVisitor;
import org.deuce.objectweb.asm.Attribute;
import org.deuce.objectweb.asm.Label;
import org.deuce.objectweb.asm.MethodAdapter;
import org.deuce.objectweb.asm.MethodVisitor;
import org.deuce.objectweb.asm.Opcodes;
import org.deuce.objectweb.asm.Type;
import org.deuce.objectweb.asm.commons.Method;
import org.deuce.transaction.AbstractContext;
import org.deuce.transform.asm.type.TypeCodeResolver;
import org.deuce.transform.asm.type.TypeCodeResolverFactory;

public class AtomicMethod extends MethodAdapter implements Opcodes{

	private int retries = 7; // TODO set default
	
	final private String className;
	final private String methodName;
	final private TypeCodeResolver returnReolver;
	final private TypeCodeResolver[] argumentReolvers;
	final private boolean isStatic;
	final private int variablesSize;
	final private Method newMethod; 

	public AtomicMethod(MethodVisitor mv, String className, String methodName,
			String descriptor, Method newMethod, boolean isStatic) {
		super(mv);
		this.className = className;
		this.methodName = methodName;
		this.newMethod = newMethod;
		this.isStatic = isStatic;

		Type returnType = Type.getReturnType(descriptor);
		Type[] argumentTypes = Type.getArgumentTypes(descriptor);

		returnReolver = TypeCodeResolverFactory.getReolver(returnType);
		argumentReolvers = new TypeCodeResolver[ argumentTypes.length];
		for( int i=0; i< argumentTypes.length ; ++i) {
			argumentReolvers[ i] = TypeCodeResolverFactory.getReolver( argumentTypes[ i]);
		}
		variablesSize = variablesSize( argumentReolvers, isStatic);
	}

	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		return super.visitAnnotation(desc, visible);
	}

	@Override
	public AnnotationVisitor visitAnnotationDefault() {
		return super.visitAnnotationDefault();
	}

	@Override
	public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
		return super.visitParameterAnnotation(parameter, desc, visible);
	}

	@Override
	public void visitAttribute(Attribute attr) {
		super.visitAttribute(attr);
	}

	@Override
	public void visitCode() {

		final int indexIndex = variablesSize; // i
		final int contextIndex = indexIndex + 1; // context
		final int resultIndex = returnReolver == null ? contextIndex : contextIndex + 1;
		final int throwableIndex = resultIndex + (returnReolver == null ? 0 : returnReolver.extendLocals()) + 1;

		Label l0 = new Label();
		Label l1 = new Label();
		mv.visitTryCatchBlock(l0, l1, l1, null); // try{

		mv.visitIntInsn(SIPUSH, retries); // for( int i =retries; ; ...) 
		mv.visitVarInsn(ISTORE, indexIndex);
		Label l2 = new Label();
		mv.visitJumpInsn(GOTO, l2);

		Label l3 = getContext(contextIndex); // AbstractContext context = AbstractContext.getInstance();

		if( returnReolver != null) { // result = null;
			mv.visitInsn(returnReolver.nullValueCode());
			mv.visitVarInsn(returnReolver.storeCode(), resultIndex);
		}

		// -------------- result = foo( context, ...)  --------------- 

		mv.visitLabel(l0);
		if( !isStatic) // load this id not static
			mv.visitVarInsn(ALOAD, 0);

		// load the rest of the arguments
		int local = isStatic ? 0 : 1;
		for( int i=0 ; i < argumentReolvers.length ; ++i) { 
			mv.visitVarInsn(argumentReolvers[i].loadCode(), local);
			local += argumentReolvers[i].extendLocals();
		}
		
		mv.visitVarInsn(ALOAD, contextIndex); // load the context
		
		if( isStatic)
			mv.visitMethodInsn(INVOKESTATIC, className, methodName, newMethod.getDescriptor()); // ... = foo( ...
		else
			mv.visitMethodInsn(INVOKEVIRTUAL, className, methodName, newMethod.getDescriptor()); // ... = foo( ...

		if( returnReolver != null) {
			mv.visitVarInsn(returnReolver.storeCode(), resultIndex); // result = ...
		}

		Label l4 = new Label();
		mv.visitJumpInsn(GOTO, l4);
		mv.visitLabel(l1);
		mv.visitVarInsn(ASTORE, throwableIndex); // store the throwable
		mv.visitVarInsn(ALOAD, contextIndex); // context.commit()
		mv.visitMethodInsn(INVOKEVIRTUAL, "org/deuce/transaction/AbstractContext", "commit", "()Z");
		Label l5 = new Label();
		mv.visitJumpInsn(IFNE, l5); //if( !context.commit())
		Label l6 = new Label();
		mv.visitJumpInsn(GOTO, l6); // continue;
		mv.visitLabel(l5);
		mv.visitVarInsn(ALOAD, throwableIndex); // load the throwable for re-throw
		mv.visitInsn(ATHROW);<commit_msg>Issue 13 Add +1 to the arguments load to move to the next argument on arguments load.<commit_after>package org.deuce.transform.asm;

import org.deuce.objectweb.asm.AnnotationVisitor;
import org.deuce.objectweb.asm.Attribute;
import org.deuce.objectweb.asm.Label;
import org.deuce.objectweb.asm.MethodAdapter;
import org.deuce.objectweb.asm.MethodVisitor;
import org.deuce.objectweb.asm.Opcodes;
import org.deuce.objectweb.asm.Type;
import org.deuce.objectweb.asm.commons.Method;
import org.deuce.transaction.AbstractContext;
import org.deuce.transform.asm.type.TypeCodeResolver;
import org.deuce.transform.asm.type.TypeCodeResolverFactory;

public class AtomicMethod extends MethodAdapter implements Opcodes{

	private int retries = 7; // TODO set default
	
	final private String className;
	final private String methodName;
	final private TypeCodeResolver returnReolver;
	final private TypeCodeResolver[] argumentReolvers;
	final private boolean isStatic;
	final private int variablesSize;
	final private Method newMethod; 

	public AtomicMethod(MethodVisitor mv, String className, String methodName,
			String descriptor, Method newMethod, boolean isStatic) {
		super(mv);
		this.className = className;
		this.methodName = methodName;
		this.newMethod = newMethod;
		this.isStatic = isStatic;

		Type returnType = Type.getReturnType(descriptor);
		Type[] argumentTypes = Type.getArgumentTypes(descriptor);

		returnReolver = TypeCodeResolverFactory.getReolver(returnType);
		argumentReolvers = new TypeCodeResolver[ argumentTypes.length];
		for( int i=0; i< argumentTypes.length ; ++i) {
			argumentReolvers[ i] = TypeCodeResolverFactory.getReolver( argumentTypes[ i]);
		}
		variablesSize = variablesSize( argumentReolvers, isStatic);
	}

	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		return super.visitAnnotation(desc, visible);
	}

	@Override
	public AnnotationVisitor visitAnnotationDefault() {
		return super.visitAnnotationDefault();
	}

	@Override
	public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
		return super.visitParameterAnnotation(parameter, desc, visible);
	}

	@Override
	public void visitAttribute(Attribute attr) {
		super.visitAttribute(attr);
	}

	@Override
	public void visitCode() {

		final int indexIndex = variablesSize; // i
		final int contextIndex = indexIndex + 1; // context
		final int resultIndex = returnReolver == null ? contextIndex : contextIndex + 1;
		final int throwableIndex = resultIndex + (returnReolver == null ? 0 : returnReolver.extendLocals()) + 1;

		Label l0 = new Label();
		Label l1 = new Label();
		mv.visitTryCatchBlock(l0, l1, l1, null); // try{

		mv.visitIntInsn(SIPUSH, retries); // for( int i =retries; ; ...) 
		mv.visitVarInsn(ISTORE, indexIndex);
		Label l2 = new Label();
		mv.visitJumpInsn(GOTO, l2);

		Label l3 = getContext(contextIndex); // AbstractContext context = AbstractContext.getInstance();

		if( returnReolver != null) { // result = null;
			mv.visitInsn(returnReolver.nullValueCode());
			mv.visitVarInsn(returnReolver.storeCode(), resultIndex);
		}

		// -------------- result = foo( context, ...)  --------------- 

		mv.visitLabel(l0);
		if( !isStatic) // load this id if not static
			mv.visitVarInsn(ALOAD, 0);

		// load the rest of the arguments
		int local = isStatic ? 0 : 1;
		for( int i=0 ; i < argumentReolvers.length ; ++i) { 
			mv.visitVarInsn(argumentReolvers[i].loadCode(), local);
			local += (argumentReolvers[i].extendLocals() + 1); // move to the next argument
		}
		
		mv.visitVarInsn(ALOAD, contextIndex); // load the context
		
		if( isStatic)
			mv.visitMethodInsn(INVOKESTATIC, className, methodName, newMethod.getDescriptor()); // ... = foo( ...
		else
			mv.visitMethodInsn(INVOKEVIRTUAL, className, methodName, newMethod.getDescriptor()); // ... = foo( ...

		if( returnReolver != null) 
			mv.visitVarInsn(returnReolver.storeCode(), resultIndex); // result = ...

		Label l4 = new Label();
		mv.visitJumpInsn(GOTO, l4);
		mv.visitLabel(l1);
		mv.visitVarInsn(ASTORE, throwableIndex); // store the throwable
		mv.visitVarInsn(ALOAD, contextIndex); // context.commit()
		mv.visitMethodInsn(INVOKEVIRTUAL, "org/deuce/transaction/AbstractContext", "commit", "()Z");
		Label l5 = new Label();
		mv.visitJumpInsn(IFNE, l5); //if( !context.commit())
		Label l6 = new Label();
		mv.visitJumpInsn(GOTO, l6); // continue;
		mv.visitLabel(l5);
		mv.visitVarInsn(ALOAD, throwableIndex); // load the throwable for re-throw
		mv.visitInsn(ATHROW);
{'fraction_non_alphanumeric': 0.1247626493912655, 'fraction_numerical': 0.006478275438400536, 'mean_word_length': 4.4664224664224665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2885079', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2773, 'n_words': 670}