/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.xreate;

import com.google.common.collect.Maps;
import java.math.BigInteger;
import org.jllvm.*;
import org.jllvm.bindings.*;
import org.junit.Test;
/**
 *
 * @author pgess
 */
public class TestRunner2 {
    
    protected LLVMModule module;

    public void setModule(LLVMModule module){
        this.module = module;
    }
    
    public void constructAndRun() throws Exception{
        boolean isNative = LLVMTargetData.initializeNativeTarget();
         module = new LLVMModule("xreate_first_test");
        
        LLVMValue func = constructBody();
        runBody(func);
    }
    
    public LLVMValue constructBody() throws Exception{
        LLVMConstantInteger a = LLVMConstantInteger.constantInteger(LLVMIntegerType.i32, 10, true);
        LLVMConstantInteger b = LLVMConstantInteger.constantInteger(LLVMIntegerType.i32, 20, true);
        LLVMConstantInteger c = LLVMConstantInteger.constantInteger(LLVMIntegerType.i32, 3, true);
        LLVMInstructionBuilder builder = new LLVMInstructionBuilder();
        
        LLVMInstruction i = new LLVMAddInstruction(builder, a, b, false, "a+b");
        i = new LLVMMultiplyInstruction(builder, c, i, false, "c * (a + b)");
        
        //LLVMModule module;  //  module = 
        LLVMFunctionType func_test1_type;
        
      
        
        LLVMType[] types = new LLVMType[0];
        //types[0] = new LLVMVoidType();
        
        func_test1_type = new LLVMFunctionType(LLVMIntegerType.i32, types, false);
        
        LLVMFunction test1 = new LLVMFunction(module, "test1", func_test1_type);
        LLVMBasicBlock entry = test1.appendBasicBlock("entry");
        builder.positionBuilderAtEnd(entry);
        
        new LLVMReturnInstruction(builder, i);
        
        SWIGTYPE_p_p_char outerrs = ExecutionEngine.new_StringArray(1);
        int x = Analysis.LLVMVerifyFunction(test1.getInstance(), LLVMVerifierFailureAction.LLVMReturnStatusAction);
        x = Analysis.LLVMVerifyModule(module.getInstance(), LLVMVerifierFailureAction.LLVMReturnStatusAction, outerrs);
        
       ExecutionEngine.delete_StringArray(outerrs); outerrs = null;
     
       return test1;
    }
    
    public void runBody(LLVMValue test1 ){
        
        
        SWIGTYPE_p_p_LLVMOpaqueExecutionEngine engines = ExecutionEngine.new_LLVMExecutionEngineRefArray(1);
        SWIGTYPE_p_p_char outerrs = ExecutionEngine.new_StringArray(1);
		int success = ExecutionEngine.LLVMCreateJITCompilerForModule(engines,module.getInstance(), 0, outerrs);
		String outerr = ExecutionEngine.StringArray_getitem(outerrs,0);
		ExecutionEngine.delete_StringArray(outerrs); outerrs = null;
                SWIGTYPE_p_LLVMOpaqueExecutionEngine instance_ = ExecutionEngine.LLVMExecutionEngineRefArray_getitem(engines,0);
		ExecutionEngine.delete_LLVMExecutionEngineRefArray(engines); engines = null;
         
       /*
         LLVMJitCompiler engine = new LLVMJitCompiler(module);
                
                LLVMGenericValue res = engine.runFunction(test1, args);
                
                //
                BigInteger result = ExecutionEngine.LLVMGenericValueToInt(res.getInstance(), 0);
       */

                LLVMGenericValue[] args = new LLVMGenericValue[0];
                
                
                SWIGTYPE_p_p_LLVMOpaqueGenericValue arg_array = ExecutionEngine.new_LLVMGenericValueRefArray(args.length);
		for(int ii=0;ii<args.length;ii++)
			ExecutionEngine.LLVMGenericValueRefArray_setitem(arg_array,ii,args[ii].getInstance());
                
		SWIGTYPE_p_LLVMOpaqueGenericValue res = ExecutionEngine.LLVMRunFunction(instance_,test1.getInstance(),args.length,arg_array);
		ExecutionEngine.delete_LLVMGenericValueRefArray(arg_array);
                
                BigInteger result = ExecutionEngine.LLVMGenericValueToInt(res, 0);
        //LLVMGenericInt result = (LLVMGenericInt) new LLVMGenericValue(res);
                
                
                /*
        LLVMJitCompiler program = new LLVMJitCompiler (module);
        LLVMGenericInt result = (LLVMGenericInt) program.runFunction(test1, new LLVMGenericValue[0]);
                */
        
        
        int r = result.intValue();
        
       System.out.println(r);
    }
    
    static {
        System.loadLibrary("jllvm");
    }
}
