Unexpected Behavior?

15 views
Skip to first unread message

Grant Posner

unread,
Oct 10, 2013, 1:49:32 PM10/10/13
to asmji...@googlegroups.com
When I run this example:

#include <stdio.h>
#include <AsmJit.h>

using namespace AsmJit;

void doSomething(int a, int b)
{
    printf("a = %d, b = %d\n", a, b);
}

void main()
{
    // create compiler
    Compiler c;

    // create a function to generate code for
    EFunction* func = c.newFunction(CALL_CONV_DEFAULT, FunctionBuilder0<int>());
    // attempt to generate without prologue/epilogue
    func->setHint(FUNCTION_HINT_NAKED, true);

    // call doSomething(5, 10);
    ECall* func_call = c.call(&doSomething);
    func_call->setPrototype(CALL_CONV_DEFAULT, FunctionBuilder2<Void, int, int>());
    GPVar var_a(c.newGP(VARIABLE_TYPE_INT32));
    GPVar var_b(c.newGP(VARIABLE_TYPE_INT32));
    c.mov(var_a, imm(5));
    c.mov(var_b, imm(10));
    func_call->setArgument(0, var_a);
    func_call->setArgument(1, var_b);

    // now finish and compile the function
    c.endFunction();
    CompiledWord word = function_cast<CompiledWord>(c.make());

    doSomething(5, 10); // a = 5, b = 10
    (*word)(); // a = -858993460, b = -858993460 <-- what's going on here?!?
}


I get strange values when doSomething is called from the generated assembly. Am I doing something wrong? And if so, how do I fix it?

Thanks!
Grant Posner

Petr Kobalíček

unread,
Oct 11, 2013, 5:22:07 AM10/11/13
to asmjit-dev
Hi Grant,

I looked at your sample at least 4 times and it seemed right, but then I realized where is the problem. When you use c.call() the "Call" node is already added to the compiler and any other instruction will be added after it. To fix your sample you have to change the order a bit:

#include <stdio.h>
#include <AsmJit.h>

using namespace AsmJit;

void doSomething(int a, int b)
{
    printf("a = %d, b = %d\n", a, b);
}

void main()
{
    Compiler c;


    EFunction* func = c.newFunction(CALL_CONV_DEFAULT, FunctionBuilder0<int>());
    func->setHint(FUNCTION_HINT_NAKED, true);

    // Prepare arguments first.
    GPVar var_a(c.newGP(VARIABLE_TYPE_INT32));
    GPVar var_b(c.newGP(VARIABLE_TYPE_INT32));
    c.mov(var_a, imm(5));
    c.mov(var_b, imm(10));

    // Call the function.
    ECall* func_call = c.call(&doSomething);
    func_call->setPrototype(CALL_CONV_DEFAULT, FunctionBuilder2<Void, int, int>());
    func_call->setArgument(0, var_a);
    func_call->setArgument(1, var_b);

    // now finish and compile the function
    c.endFunction();
    CompiledWord word = function_cast<CompiledWord>(c.make());

    doSomething(5, 10); // a = 5, b = 10
    (*word)(); // a = -858993460, b = -858993460 <-- what's going on here?!?
}

This sample should work as expected.

Best,
Petr

--
 
---
You received this message because you are subscribed to the Google Groups "asmjit-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asmjit-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Grant Posner

unread,
Oct 11, 2013, 9:20:26 AM10/11/13
to asmji...@googlegroups.com
Wow. Now that you explain it, it seems obvious what the problem was... thank you so much (again)! Pretty soon, I'll have a working JIT-compiled language using AsmJit!
 
Thanks,
Grant Posner
Reply all
Reply to author
Forward
0 new messages