From e9a60eb4987a096df10de0442af1e2929bc32547 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sat, 13 Feb 2010 01:28:07 +0000 Subject: [PATCH] MC/AsmParser: Attempt to constant fold expressions up-front. This ensures we avoid fixups for obvious cases like '-(16)'. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96064 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/MCParser/AsmParser.cpp | 12 ++++++++++-- test/MC/AsmParser/X86/x86_32-new-encoder.s | 6 ++++++ test/MC/AsmParser/X86/x86_operands.s | 18 +++++++++--------- test/MC/AsmParser/conditional_asm.s | 2 +- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/MC/MCParser/AsmParser.cpp b/lib/MC/MCParser/AsmParser.cpp index d5bc396103f..51ad5d18f3b 100644 --- a/lib/MC/MCParser/AsmParser.cpp +++ b/lib/MC/MCParser/AsmParser.cpp @@ -325,9 +325,17 @@ bool AsmParser::ParseExpression(const MCExpr *&Res) { /// expr ::= primaryexpr /// bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) { + // Parse the expression. Res = 0; - return ParsePrimaryExpr(Res, EndLoc) || - ParseBinOpRHS(1, Res, EndLoc); + if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc)) + return true; + + // Try to constant fold it up front, if possible. + int64_t Value; + if (Res->EvaluateAsAbsolute(Value)) + Res = MCConstantExpr::Create(Value, getContext()); + + return false; } bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { diff --git a/test/MC/AsmParser/X86/x86_32-new-encoder.s b/test/MC/AsmParser/X86/x86_32-new-encoder.s index a440a253df5..efac73f845e 100644 --- a/test/MC/AsmParser/X86/x86_32-new-encoder.s +++ b/test/MC/AsmParser/X86/x86_32-new-encoder.s @@ -28,3 +28,9 @@ swapgs // CHECK: swapgs // CHECK: encoding: [0x0f,0x01,0xf8] + +// CHECK: movl %eax, 16(%ebp) # encoding: [0x89,0x45,0x10] + movl %eax, 16(%ebp) +// CHECK: movl %eax, -16(%ebp) # encoding: [0x89,0x45,0xf0] + movl %eax, -16(%ebp) + diff --git a/test/MC/AsmParser/X86/x86_operands.s b/test/MC/AsmParser/X86/x86_operands.s index 8e6543e7bd6..edddd1fc449 100644 --- a/test/MC/AsmParser/X86/x86_operands.s +++ b/test/MC/AsmParser/X86/x86_operands.s @@ -5,28 +5,28 @@ # Immediates # CHECK: addl $1, %eax addl $1, %eax -# CHECK: addl $1+2, %eax +# CHECK: addl $3, %eax addl $(1+2), %eax # CHECK: addl $a, %eax addl $a, %eax -# CHECK: addl $1+2, %eax +# CHECK: addl $3, %eax addl $1 + 2, %eax # Disambiguation - # FIXME: Add back when we can match this. - #addl $1, 4+4 - # FIXME: Add back when we can match this. - #addl $1, (4+4) -# CHECK: addl $1, 4+4(%eax) +# CHECK: addl $1, 8 + addl $1, 4+4 +# CHECK: addl $1, 8 + addl $1, (4+4) +# CHECK: addl $1, 8(%eax) addl $1, 4+4(%eax) -# CHECK: addl $1, 4+4(%eax) +# CHECK: addl $1, 8(%eax) addl $1, (4+4)(%eax) # CHECK: addl $1, 8(%eax) addl $1, 8(%eax) # CHECK: addl $1, (%eax) addl $1, (%eax) -# CHECK: addl $1, 4+4(,%eax) +# CHECK: addl $1, 8(,%eax) addl $1, (4+4)(,%eax) # Indirect Memory Operands diff --git a/test/MC/AsmParser/conditional_asm.s b/test/MC/AsmParser/conditional_asm.s index b8a514fb4fa..f619ef9bb42 100644 --- a/test/MC/AsmParser/conditional_asm.s +++ b/test/MC/AsmParser/conditional_asm.s @@ -1,6 +1,6 @@ # RUN: llvm-mc -triple i386-unknown-unknown %s -I %p | FileCheck %s -# CHECK: .byte 1+1 +# CHECK: .byte 2 .if 1+2 .if 1-1 .byte 1