dotgnu-libjit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Libjit-developers] simple loop?


From: jhanebach
Subject: [Libjit-developers] simple loop?
Date: Tue, 8 Feb 2005 17:43:36 -0500


Hi,

I'm trying to get a feel of libjit and cannot figure out how to create a simple loop :(
My lame attempt: (VC 7.1 on WinXP)

        jit_context_t context;
        jit_type_t params[3];
        jit_type_t signature;
        jit_function_t function;
        jit_value_t x, x1, y, z, one, zero;
        jit_value_t temp1, temp2, i, j, i1, j1;
        jit_int arg1, arg2, arg3;
        jit_label_t outer = jit_label_undefined;
        jit_label_t inner = jit_label_undefined;
        void *args[3];
        jit_int result;
        FILE * output;
        jit_int  data[20] = {17, 2, 3, 4, 5, 6, 7, 8, 9, 10,  /* input*/
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* output */
        /* Create a context to hold the JIT's primary state */
        context = jit_context_create();

        /* Lock the context while we build and compile the function */
        jit_context_build_start(context);

        /* Build the function signature */
        params[0] = jit_type_create_pointer(jit_type_int, 1);
        params[1] = jit_type_int;
        params[2] = jit_type_int;
        signature = jit_type_create_signature
                (jit_abi_cdecl, jit_type_int, params, 3, 1);

        /* Create the function object */
        function = jit_function_create(context, signature);
        output = fopen("output.txt", "w");
        /* Construct the function body */
        x = jit_value_get_param(function, 0);
        y = jit_value_get_param(function, 1);
        z = jit_value_get_param(function, 2);
        i = jit_value_create(function, jit_type_int);
        j = jit_value_create(function, jit_type_int);
        zero = jit_value_create_nint_constant(function, jit_type_int, 0);
        one  = jit_value_create_nint_constant(function, jit_type_int, 1);
        x1 = jit_insn_add(function, x, y);         // x1 = index into output part of data
        i1 = jit_insn_dup(function, z);             // init value for outer loop counter
        jit_insn_label(function, &outer);
        j = jit_insn_dup(function, y);                 // reset inner loop counter
       
        jit_insn_label(function, &inner);
        j1 = jit_insn_dup(function, j);                  
        // decrement inner loop counter - but doesn't generate what I expected it to :(
        j = jit_insn_sub(function, j1, one);  
        temp2 = jit_insn_load_elem(function, x, j, jit_type_int); // get the input element
        temp1 = jit_insn_load_elem(function, x1, j, jit_type_int); // and the output element
       
        temp1 = jit_insn_add(function, temp1, temp2); // add them together
        jit_insn_store_elem(function, x1, j, temp1); // and store in output
   
        jit_insn_branch_if(function, j, &inner);  // if j != 0 continue inner loop
        i1 = jit_insn_dup(function, i);
        // decrement i (still no luck)
        i = jit_insn_sub(function, i1, one);
        jit_insn_branch_if(function, i, &outer);  // if i + 0 continue outer loop

        jit_insn_return(function, zero);
        jit_dump_function(output, function, "before_compile");
        /* Compile the function */
        jit_function_compile(function);

        /* Unlock the context */
        jit_context_build_end(context);

        jit_dump_function(output, function, "after_compile");
        /* Execute the function and print the result */
        fclose(output);
        arg1 = 3;
        arg2 = 10;
        arg3 = 1000;
        args[0] = &data;
        args[1] = &arg2;
        args[2] = &arg3;
        jit_function_apply(function, args, &result);
   
        /* Clean up */
        jit_context_destroy(context);

crashes inside  jit_function_apply and the two dumps generate following output:

function before_compile(i1 : ptr, i2 : int, i3 : int) : int
        incoming_frame_posn(i1, 8)
        incoming_frame_posn(i2, 12)
        incoming_frame_posn(i3, 16)
        i11 = i1 + i2
        i12 = i3
.L0:
        i13 = i2
.L1:
        i14 = i13
        i15 = i14 - 1
        i16 = load_element_int(i1, i15)
        i17 = load_element_int(i11, i15)
        i18 = i17 + i16
        store_element_int(i11, i15, i18)
        if itrue(i15) then goto .L1
.L2:
        i19 = i7
        i20 = i19 - 1
        if itrue(i20) then goto .L0
.L3:
        return_int(0)
        ends_in_dead
end

function after_compile(ptr, int, int) : int
end

First problem - function after compile appears to be empty !?
second problem - my loop counters don't really get decremented

Can anybody give me an insight into what I'm doing wrong?
TIA

Jack
reply via email to

[Prev in Thread] Current Thread [Next in Thread]