llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Macro expansion fails with "expected absolute expression"

Open isanbard opened this issue 7 years ago • 10 comments

Bugzilla Link 41825
Resolution FIXED
Resolved on Nov 18, 2019 09:29
Version trunk
OS All
CC @jcai19,@jyknight,@kbeyls,@nickdesaulniers,@ostannard,@smithp35,@stephenhines

Extended Description

This is seen when compiling Linux with the integrated assembler. It was first seen in AArch64, but isn't specific to that platform.

$ cat bad.s .macro jump_slot jumptable, index, label .if (. - \jumptable) != 4 * (\index) .error "Jump slot index mismatch" .endif b \label .endm

.text jumptable: jump_slot jumptable, 0, realtime

$ clang -cc1as -triple aarch64-grtev4-linux-gnu -target-cpu generic -target-feature +neon -mrelocation-model static -o /dev/null bad.s :1:5: error: expected absolute expression .if (. - jumptable) != 4 * (0) ^ bad.s:10:3: note: while in macro instantiation jump_slot jumptable, 0, realtime ^ :2:1: error: Jump slot index mismatch .error "Jump slot index mismatch" ^ bad.s:10:3: note: while in macro instantiation jump_slot jumptable, 0, realtime ^

isanbard avatar May 10 '19 08:05 isanbard

Fixed: https://reviews.llvm.org/rGbf142fc43347d8a35a71f46f7dda7e2a0a992e0d

nickdesaulniers avatar Nov 18 '19 17:11 nickdesaulniers

jyknight has also authored https://reviews.llvm.org/D70062 referencing this issue.

Yes this is a better solution, I have removed the redundant code from my patch.

jcai19 avatar Nov 11 '19 18:11 jcai19

jyknight has also authored https://reviews.llvm.org/D70062 referencing this issue.

nickdesaulniers avatar Nov 11 '19 18:11 nickdesaulniers

Different from a similar issue llvm/llvm-bugzilla-archive#43759 , which is caused by .if comparing two symbols in consecutive fragments, this issue is due to MC parses a .if statement before a real MCDataFragment created. Symbols are created and placed in a MCDummyFragment, and re-associated with a real fragment when created. If a .if statement is the first to parse within a code fragment, then MC will fail to evaluate the difference of their offsets since the symbols are still in the dummy fragment. I tried to solve this problem when symbols are emitted, which however caused regression and the reason was not easy to identify. So eventually I had to fix the issue when the comparison happens, in a similar way to how I fix llvm/llvm-bugzilla-archive#43759 at https://reviews.llvm.org/D69411. Feel free to suggest a better fix. Thanks.

jcai19 avatar Nov 09 '19 18:11 jcai19

I see what you mean. Now that I think about it I don't think that there is going to be an easy way of solving this in MC. The largest problem with .if is that it can affect the layout itself, for example:

label: relaxable-relative-branch-to label2: .if (condition) one instruction .else many instructions .endif label2:

if the condition passes then a small branch instruction can be used, if it fails then a larger relaxed one might be needed, which might then change the value of condition.

I'm thinking that the closest MC is likely to get to being able to evaluate

    .text

jumptable: .if (. - jumptable) is if the label is in the same non relaxable Fragment so that the .if can be resolved without affecting the . - jumptable.

I don't think that this can be accomplished by the existing evaluate expression code though.

smithp35 avatar May 21 '19 09:05 smithp35

It's just that the parser doesn't have access to the MCAsmLayout object and I'm not sure how to pass it to the parser.

isanbard avatar May 20 '19 19:05 isanbard

Not that I know of sorry. The only thing I could think to try was: .globl offset .equ offset, (. - jumptable) .if (offset != 0) sadly MC still thinks offset is not absolute.

Looking at the MCExpr code I think that this is because the code for .if uses evaluateAsAbsolute(Expr);

There are alternative forms of evaluate that include MCAsmLayout which I think MCExpr needs to know that . - jumptable is a constant.

smithp35 avatar May 16 '19 09:05 smithp35

Is there an easy way to convince MC that this is an absolute expression?

isanbard avatar May 15 '19 20:05 isanbard

This will reproduce without a macro and on any target .text jumptable: .if (. - jumptable) != 0 .error "1" .endif nop

The .if directive is using parseAbsoluteExpression(). What needs to be worked out is whether (. - jumptable) != 0 is not considered absolute. Given that jumptable is before the directive I think that this can evaluate to an absolute constant.

smithp35 avatar May 13 '19 17:05 smithp35

assigned to @jyknight

isanbard avatar May 10 '19 08:05 isanbard