Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Detect duplicate destination labels #295

Open
erhant opened this issue Sep 16, 2023 · 2 comments
Open

Detect duplicate destination labels #295

erhant opened this issue Sep 16, 2023 · 2 comments

Comments

@erhant
Copy link

erhant commented Sep 16, 2023

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.

@Philogy
Copy link
Contributor

Philogy commented Oct 3, 2023

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
}

@erhant
Copy link
Author

erhant commented Oct 3, 2023

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 🥲

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

No branches or pull requests

2 participants