[ELF] Fix precedence of == and != in expressions

In GNU ld, the == and != operators have lower precedence than < > <= >=.
This behavior matches C.
This commit is contained in:
Fangrui Song 2022-06-25 13:47:32 -07:00
parent 3d37e785c7
commit d479b2e4db
2 changed files with 10 additions and 9 deletions

View File

@ -636,14 +636,15 @@ void ScriptParser::readTarget() {
static int precedence(StringRef op) {
return StringSwitch<int>(op)
.Cases("*", "/", "%", 8)
.Cases("+", "-", 7)
.Cases("<<", ">>", 6)
.Cases("<", "<=", ">", ">=", "==", "!=", 5)
.Case("&", 4)
.Case("|", 3)
.Case("&&", 2)
.Case("||", 1)
.Cases("*", "/", "%", 10)
.Cases("+", "-", 9)
.Cases("<<", ">>", 8)
.Cases("<", "<=", ">", ">=", 7)
.Cases("==", "!=", 6)
.Case("&", 5)
.Case("|", 4)
.Case("&&", 3)
.Case("||", 2)
.Default(-1);
}

View File

@ -20,7 +20,7 @@ SECTIONS {
greater = 0 > 1 ? 1 : 2;
greatereq = 1 >= 1 ? 1 : 2;
eq = 1 == 1 ? 1 : 2;
neq = 1 != 1 ? 1 : 2;
neq = (1 != 1 <= 1) ? 1 : 2;
plusassign = 1;
plusassign += 2;
unary = -1 + 3;