Macro expansion fails with "expected absolute expression"
| 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
Fixed: https://reviews.llvm.org/rGbf142fc43347d8a35a71f46f7dda7e2a0a992e0d
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.
jyknight has also authored https://reviews.llvm.org/D70062 referencing this issue.
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.
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.
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.
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.
Is there an easy way to convince MC that this is an absolute expression?
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.
assigned to @jyknight