Index: lib/CodeGen/MIRParser/MILexer.h =================================================================== --- lib/CodeGen/MIRParser/MILexer.h +++ lib/CodeGen/MIRParser/MILexer.h @@ -16,6 +16,7 @@ #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/APSInt.h" #include namespace llvm { @@ -35,16 +36,23 @@ // Identifier tokens Identifier, - NamedRegister + NamedRegister, + + // Other tokens + IntegerLiteral }; private: TokenKind Kind; StringRef Range; + APSInt IntVal; public: MIToken(TokenKind Kind, StringRef Range) : Kind(Kind), Range(Range) {} + MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal) + : Kind(Kind), Range(Range), IntVal(IntVal) {} + TokenKind kind() const { return Kind; } bool isError() const { return Kind == Error; } @@ -58,6 +66,8 @@ StringRef::iterator location() const { return Range.begin(); } StringRef stringValue() const { return Range; } + + const APSInt &integerValue() const { return IntVal; } }; /// Consume a single machine instruction token in the given source and return Index: lib/CodeGen/MIRParser/MILexer.cpp =================================================================== --- lib/CodeGen/MIRParser/MILexer.cpp +++ lib/CodeGen/MIRParser/MILexer.cpp @@ -35,6 +35,8 @@ char peek() const { return isEOF() ? 0 : *Ptr; } + char peekNext() const { return (Ptr + 1) >= End ? 0 : *(Ptr + 1); } + void advance() { ++Ptr; } StringRef remaining() const { return StringRef(Ptr, End - Ptr); } @@ -77,6 +79,32 @@ return C; } +static APSInt toInteger(StringRef Str) { + assert(!Str.empty() && "Integer literal string must not be empty"); + unsigned NumBits = ((Str.size() * 64) / 19) + 2; + APInt Tmp(NumBits, Str, /*Radix=*/10); + if (Str[0] == '-') { + unsigned MinBits = Tmp.getMinSignedBits(); + if (MinBits > 0 && MinBits < NumBits) + Tmp = Tmp.trunc(MinBits); + return APSInt(Tmp, /*IsUnsigned=*/false); + } + unsigned ActiveBits = Tmp.getActiveBits(); + if (ActiveBits > 0 && ActiveBits < NumBits) + Tmp = Tmp.trunc(ActiveBits); + return APSInt(Tmp, /*IsUnsigned=*/true); +} + +static Cursor lexIntegerLiteral(Cursor C, MIToken &Token) { + auto Range = C; + C.advance(); + while (isdigit(C.peek())) + C.advance(); + StringRef StrVal = Range.upto(C); + Token = MIToken(MIToken::IntegerLiteral, StrVal, toInteger(StrVal)); + return C; +} + static MIToken::TokenKind symbolToken(char C) { switch (C) { case ',': @@ -109,6 +137,8 @@ return lexIdentifier(C, Token).remaining(); if (Char == '%') return lexPercent(C, Token).remaining(); + if (isdigit(Char) || (Char == '-' && isdigit(C.peekNext()))) + return lexIntegerLiteral(C, Token).remaining(); MIToken::TokenKind Kind = symbolToken(Char); if (Kind != MIToken::Error) return lexSymbol(C, Kind, Token).remaining(); Index: lib/CodeGen/MIRParser/MIParser.cpp =================================================================== --- lib/CodeGen/MIRParser/MIParser.cpp +++ lib/CodeGen/MIRParser/MIParser.cpp @@ -57,6 +57,7 @@ bool parseRegister(unsigned &Reg); bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false); + bool parseImmediateOperand(MachineOperand &Dest); bool parseMachineOperand(MachineOperand &Dest); private: @@ -197,10 +198,23 @@ return false; } +bool MIParser::parseImmediateOperand(MachineOperand &Dest) { + assert(Token.is(MIToken::IntegerLiteral)); + const APSInt &Int = Token.integerValue(); + if (Int.getMinSignedBits() > 64) + // TODO: Replace this with an error when we can parse CIMM Machine Operands. + llvm_unreachable("Can't parse large integer literals yet!"); + Dest = MachineOperand::CreateImm(Int.getExtValue()); + lex(); + return false; +} + bool MIParser::parseMachineOperand(MachineOperand &Dest) { switch (Token.kind()) { case MIToken::NamedRegister: return parseRegisterOperand(Dest); + case MIToken::IntegerLiteral: + return parseImmediateOperand(Dest); case MIToken::Error: return true; default: Index: lib/CodeGen/MIRPrinter.cpp =================================================================== --- lib/CodeGen/MIRPrinter.cpp +++ lib/CodeGen/MIRPrinter.cpp @@ -159,6 +159,9 @@ printReg(Op.getReg(), OS, TRI); // TODO: Print sub register. break; + case MachineOperand::MO_Immediate: + OS << Op.getImm(); + break; default: // TODO: Print the other machine operands. llvm_unreachable("Can't print this machine operand at the moment"); Index: test/CodeGen/MIR/X86/immediate-operands.mir =================================================================== --- /dev/null +++ test/CodeGen/MIR/X86/immediate-operands.mir @@ -0,0 +1,38 @@ +# RUN: llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s +# This test ensures that the MIR parser parses immediate machine operands. + +--- | + + define i32 @foo() { + entry: + ret i32 42 + } + + define i32 @bar() { + entry: + ret i32 -11 + } + +... +--- +# CHECK: name: foo +name: foo +body: + - name: entry + instructions: + # CHECK: - '%eax = MOV32ri 42' + # CHECK-NEXT: - 'RETQ %eax' + - '%eax = MOV32ri 42' + - 'RETQ %eax' +... +--- +# CHECK: name: bar +name: bar +body: + - name: entry + instructions: + # CHECK: - '%eax = MOV32ri -11' + # CHECK-NEXT: - 'RETQ %eax' + - '%eax = MOV32ri -11' + - 'RETQ %eax' +...