MIR Serialization: Serialize immediate machine operands.
authorAlex Lorenz <arphaman@gmail.com>
Tue, 23 Jun 2015 23:42:28 +0000 (23:42 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Tue, 23 Jun 2015 23:42:28 +0000 (23:42 +0000)
Reviewers: Duncan P. N. Exon Smith

Differential Revision: http://reviews.llvm.org/D10573

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240481 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/MIRParser/MILexer.cpp
lib/CodeGen/MIRParser/MILexer.h
lib/CodeGen/MIRParser/MIParser.cpp
lib/CodeGen/MIRPrinter.cpp
test/CodeGen/MIR/X86/immediate-operands.mir [new file with mode: 0644]

index 1cc5956d9fb8700c8f0b6a571ec1561e2731a11d..b836221d9f08ff195966cdaa6256492a5370dce7 100644 (file)
@@ -33,7 +33,7 @@ public:
 
   bool isEOF() const { return Ptr == End; }
 
-  char peek() const { return isEOF() ? 0 : *Ptr; }
+  char peek(unsigned I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
 
   void advance() { ++Ptr; }
 
@@ -77,6 +77,16 @@ static Cursor lexPercent(Cursor C, MIToken &Token) {
   return C;
 }
 
+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, APSInt(StrVal));
+  return C;
+}
+
 static MIToken::TokenKind symbolToken(char C) {
   switch (C) {
   case ',':
@@ -109,6 +119,8 @@ StringRef llvm::lexMIToken(
     return lexIdentifier(C, Token).remaining();
   if (Char == '%')
     return lexPercent(C, Token).remaining();
+  if (isdigit(Char) || (Char == '-' && isdigit(C.peek(1))))
+    return lexIntegerLiteral(C, Token).remaining();
   MIToken::TokenKind Kind = symbolToken(Char);
   if (Kind != MIToken::Error)
     return lexSymbol(C, Kind, Token).remaining();
index 24cbf7d72f3fb0b729660bc72f8c0b2b1ff13707..df8b6cb2026ef3bc5b5b8c3e0a9491c0efc8a247 100644 (file)
@@ -15,6 +15,7 @@
 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
 
+#include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/STLExtras.h"
 #include <functional>
@@ -36,16 +37,23 @@ struct MIToken {
 
     // 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; }
@@ -59,6 +67,8 @@ public:
   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 029732a853e45308f6c6db9043c4838d73c1577b..33f306945cb0d7c9bb7749a5df9538d5ed7d03a8 100644 (file)
@@ -57,6 +57,7 @@ public:
 
   bool parseRegister(unsigned &Reg);
   bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
+  bool parseImmediateOperand(MachineOperand &Dest);
   bool parseMachineOperand(MachineOperand &Dest);
 
 private:
@@ -197,10 +198,23 @@ bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
   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 5dd38cf9491a9bfb8315b2370ef8f4bd4cbebb20..6d62b0ad85b3dda31c36a522faec864e94573430 100644 (file)
@@ -159,6 +159,9 @@ void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
     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");
diff --git a/test/CodeGen/MIR/X86/immediate-operands.mir b/test/CodeGen/MIR/X86/immediate-operands.mir
new file mode 100644 (file)
index 0000000..6cb3152
--- /dev/null
@@ -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'
+...