Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why Code#compare parameter order is reversed after generated dex? #180

Open
wuyr opened this issue Jan 16, 2022 · 2 comments
Open

Why Code#compare parameter order is reversed after generated dex? #180

wuyr opened this issue Jan 16, 2022 · 2 comments

Comments

@wuyr
Copy link

wuyr commented Jan 16, 2022

I have the following code:

Local local1 = methodCodeBlock.newLocal(TypeId.INT);
Local parameter = methodCodeBlock.getParameter(0, TypeId.INT);
            
...
     
methodCodeBlock.compare(Comparison.LT, label, local1, parameter);

The expected result should be:

if(local1 >= parameter)
...

But when I invoke the DexMaker#generate, the result I see is:

if(parameter >= local1)
...

Is this a bug? Or am I using it incorrectly? If I use it incorrectly, can someone tell me how to use it correctly? Thank a lot!
Is this project still alive? The last update I saw was last May

@wuyr
Copy link
Author

wuyr commented Jan 16, 2022

I got it, when local1 value = 0, the result order is always reversed. how can i fix this?

@kkoser
Copy link
Contributor

kkoser commented Mar 10, 2022

Hey @wuyr sorry I'm getting back so late - I think this is working as expected. writing a comparison of compare(LT, local1, param) is the java equivalent of

if (local1 < param)
which could also be rewritten as

if (param >= local1)

I tried writing a test case for this with the local having a value of 0, and it seems to work correctly - lmk if you're seeing different, or if I'm misunderstanding your use case. Thanks!

    @Test
    public void testIntCompare() throws Exception {
        /*
         * public static bool call(int b) {
         *   return 0 < b
         * }
         */
        MethodId<?, Boolean> methodId = GENERATED.getMethod(
                TypeId.BOOLEAN, "call", TypeId.INT);
        Code code = dexMaker.declare(methodId, PUBLIC | STATIC);
        Local<Integer> localA = code.newLocal(TypeId.INT);
        Local<Integer> localB = code.getParameter(0, TypeId.INT);
        Local<Boolean> result = code.newLocal(TypeId.get(boolean.class));
        Label afterIf = new Label();
        Label ifBody = new Label();
        code.loadConstant(localA, 0);
        code.compare(Comparison.LT, ifBody, localA, localB);
        code.jump(afterIf);

        code.mark(ifBody);
        code.loadConstant(result, true);
        code.returnValue(result);

        code.mark(afterIf);
        code.loadConstant(result, false);
        code.returnValue(result);
        Method method = getMethod();


        assertEquals(true, method.invoke(null, 5));
        assertEquals(false, method.invoke(null, -5));
        assertEquals(false, method.invoke(null, 0));
    }```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants