huff-rs icon indicating copy to clipboard operation
huff-rs copied to clipboard

Detect duplicate destination labels

Open erhant opened this issue 2 years ago • 2 comments

Huff (v0.3.2) allows one to place multiple destinations for the same label:

#define macro MAIN() = takes(0) returns(0) {
    label jump
    label: 0x00
    label: 0x00
}

It apparently chooses the position of the last one as the jump target, as seen by the runtime code (result of huffc -r) of the code above:

// 610006565b5f5b5f

/* 00 */ PUSH2 0x0006
/* 03 */ JUMP
/* 04 */ JUMPDEST
/* 05 */ PUSH0
/* 06 */ JUMPDEST
/* 07 */ PUSH0

Although an unlikely bug, it would do no harm to catch this bug at compile time.

erhant avatar Sep 16 '23 14:09 erhant

I definitely think the compiler should raise an error here, duplicate label definitions should not be allowed. Huff also won't throw an error if you accidentally declare a duplicate label as so:

#define macro A() = takes(0) returns(0) {
    first:
        sub
}

#define macro B() = takes(0) returns(0) {
    first:
        add
}

#define macro MAIN() = takes(0) returns(0) {
    A()
    B()
    first jump
}

Philogy avatar Oct 03 '23 14:10 Philogy

Indeed, I would even go further and say it would be better if we could have scoped labels:

#define macro INNER() = {
    loop:
        // ...
        loop jumpi
}

#define macro OUTER() = {
    loop:
        // ...
        INNER()
        // ....
        loop jumpi
}

In the example above, the loop label of INNER macro will be confused with that of the OUTER, but they could perhaps be handled within the scope of the macro that they are referenced in, or a macro-specific suffix/prefix could be added to labels to get that scoping effect maybe?

NOTE: This cost me so many hours during the huffathon 🥲

erhant avatar Oct 03 '23 14:10 erhant