llvm.org GIT mirror llvm / ac57f35
MIR Serialization: Serialize immediate machine operands. 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 Alex Lorenz 5 years ago
5 changed file(s) with 79 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
3232
3333 bool isEOF() const { return Ptr == End; }
3434
35 char peek() const { return isEOF() ? 0 : *Ptr; }
35 char peek(unsigned I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
3636
3737 void advance() { ++Ptr; }
3838
7676 return C;
7777 }
7878
79 static Cursor lexIntegerLiteral(Cursor C, MIToken &Token) {
80 auto Range = C;
81 C.advance();
82 while (isdigit(C.peek()))
83 C.advance();
84 StringRef StrVal = Range.upto(C);
85 Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
86 return C;
87 }
88
7989 static MIToken::TokenKind symbolToken(char C) {
8090 switch (C) {
8191 case ',':
108118 return lexIdentifier(C, Token).remaining();
109119 if (Char == '%')
110120 return lexPercent(C, Token).remaining();
121 if (isdigit(Char) || (Char == '-' && isdigit(C.peek(1))))
122 return lexIntegerLiteral(C, Token).remaining();
111123 MIToken::TokenKind Kind = symbolToken(Char);
112124 if (Kind != MIToken::Error)
113125 return lexSymbol(C, Kind, Token).remaining();
1414 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
1515 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
1616
17 #include "llvm/ADT/APSInt.h"
1718 #include "llvm/ADT/StringRef.h"
1819 #include "llvm/ADT/STLExtras.h"
1920 #include
3536
3637 // Identifier tokens
3738 Identifier,
38 NamedRegister
39 NamedRegister,
40
41 // Other tokens
42 IntegerLiteral
3943 };
4044
4145 private:
4246 TokenKind Kind;
4347 StringRef Range;
48 APSInt IntVal;
4449
4550 public:
4651 MIToken(TokenKind Kind, StringRef Range) : Kind(Kind), Range(Range) {}
52
53 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal)
54 : Kind(Kind), Range(Range), IntVal(IntVal) {}
4755
4856 TokenKind kind() const { return Kind; }
4957
5866 StringRef::iterator location() const { return Range.begin(); }
5967
6068 StringRef stringValue() const { return Range; }
69
70 const APSInt &integerValue() const { return IntVal; }
6171 };
6272
6373 /// Consume a single machine instruction token in the given source and return
5656
5757 bool parseRegister(unsigned &Reg);
5858 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
59 bool parseImmediateOperand(MachineOperand &Dest);
5960 bool parseMachineOperand(MachineOperand &Dest);
6061
6162 private:
196197 return false;
197198 }
198199
200 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
201 assert(Token.is(MIToken::IntegerLiteral));
202 const APSInt &Int = Token.integerValue();
203 if (Int.getMinSignedBits() > 64)
204 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
205 llvm_unreachable("Can't parse large integer literals yet!");
206 Dest = MachineOperand::CreateImm(Int.getExtValue());
207 lex();
208 return false;
209 }
210
199211 bool MIParser::parseMachineOperand(MachineOperand &Dest) {
200212 switch (Token.kind()) {
201213 case MIToken::NamedRegister:
202214 return parseRegisterOperand(Dest);
215 case MIToken::IntegerLiteral:
216 return parseImmediateOperand(Dest);
203217 case MIToken::Error:
204218 return true;
205219 default:
158158 printReg(Op.getReg(), OS, TRI);
159159 // TODO: Print sub register.
160160 break;
161 case MachineOperand::MO_Immediate:
162 OS << Op.getImm();
163 break;
161164 default:
162165 // TODO: Print the other machine operands.
163166 llvm_unreachable("Can't print this machine operand at the moment");
0 # RUN: llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s
1 # This test ensures that the MIR parser parses immediate machine operands.
2
3 --- |
4
5 define i32 @foo() {
6 entry:
7 ret i32 42
8 }
9
10 define i32 @bar() {
11 entry:
12 ret i32 -11
13 }
14
15 ...
16 ---
17 # CHECK: name: foo
18 name: foo
19 body:
20 - name: entry
21 instructions:
22 # CHECK: - '%eax = MOV32ri 42'
23 # CHECK-NEXT: - 'RETQ %eax'
24 - '%eax = MOV32ri 42'
25 - 'RETQ %eax'
26 ...
27 ---
28 # CHECK: name: bar
29 name: bar
30 body:
31 - name: entry
32 instructions:
33 # CHECK: - '%eax = MOV32ri -11'
34 # CHECK-NEXT: - 'RETQ %eax'
35 - '%eax = MOV32ri -11'
36 - 'RETQ %eax'
37 ...