blob: b1c052261582b75d1cf4c13fef7e09f412ea4e22 [file] [log] [blame]
Alex Lorenz91370c52015-06-22 20:37:46 +00001//===- MILexer.h - Lexer for machine instructions -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the function that lexes the machine instruction source
11// string.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16#define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17
Alex Lorenz240fc1e2015-06-23 23:42:28 +000018#include "llvm/ADT/APSInt.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000019#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include <functional>
22
23namespace llvm {
24
25class Twine;
26
27/// A token produced by the machine instruction lexer.
28struct MIToken {
29 enum TokenKind {
30 // Markers
31 Eof,
32 Error,
33
Alex Lorenzf3db51de2015-06-23 16:35:26 +000034 // Tokens with no info.
35 comma,
36 equal,
Alex Lorenz12b554e2015-06-24 17:34:58 +000037 underscore,
Alex Lorenz2eacca82015-07-13 23:24:34 +000038 colon,
Alex Lorenzf3db51de2015-06-23 16:35:26 +000039
Alex Lorenzcb268d42015-07-06 23:07:26 +000040 // Keywords
41 kw_implicit,
42 kw_implicit_define,
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +000043 kw_dead,
Alex Lorenz495ad872015-07-08 21:23:34 +000044 kw_killed,
Alex Lorenz4d026b892015-07-08 23:58:31 +000045 kw_undef,
Alex Lorenzcb268d42015-07-06 23:07:26 +000046
Alex Lorenz91370c52015-06-22 20:37:46 +000047 // Identifier tokens
Alex Lorenzf3db51de2015-06-23 16:35:26 +000048 Identifier,
Alex Lorenz240fc1e2015-06-23 23:42:28 +000049 NamedRegister,
Alex Lorenz33f0aef2015-06-26 16:46:11 +000050 MachineBasicBlock,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000051 NamedGlobalValue,
52 GlobalValue,
Alex Lorenz240fc1e2015-06-23 23:42:28 +000053
54 // Other tokens
Alex Lorenz53464512015-07-10 22:51:20 +000055 IntegerLiteral,
Alex Lorenz31d70682015-07-15 23:38:35 +000056 VirtualRegister,
57 JumpTableIndex
Alex Lorenz91370c52015-06-22 20:37:46 +000058 };
59
60private:
61 TokenKind Kind;
Alex Lorenz33f0aef2015-06-26 16:46:11 +000062 unsigned StringOffset;
Alex Lorenz91370c52015-06-22 20:37:46 +000063 StringRef Range;
Alex Lorenz240fc1e2015-06-23 23:42:28 +000064 APSInt IntVal;
Alex Lorenz91370c52015-06-22 20:37:46 +000065
66public:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000067 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
68 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
Alex Lorenz91370c52015-06-22 20:37:46 +000069
Alex Lorenz33f0aef2015-06-26 16:46:11 +000070 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
71 unsigned StringOffset = 0)
72 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
Alex Lorenz240fc1e2015-06-23 23:42:28 +000073
Alex Lorenz91370c52015-06-22 20:37:46 +000074 TokenKind kind() const { return Kind; }
75
76 bool isError() const { return Kind == Error; }
77
Alex Lorenz12b554e2015-06-24 17:34:58 +000078 bool isRegister() const {
Alex Lorenz53464512015-07-10 22:51:20 +000079 return Kind == NamedRegister || Kind == underscore ||
80 Kind == VirtualRegister;
Alex Lorenz12b554e2015-06-24 17:34:58 +000081 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +000082
Alex Lorenzcb268d42015-07-06 23:07:26 +000083 bool isRegisterFlag() const {
Alex Lorenz495ad872015-07-08 21:23:34 +000084 return Kind == kw_implicit || Kind == kw_implicit_define ||
Alex Lorenz4d026b892015-07-08 23:58:31 +000085 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
Alex Lorenzcb268d42015-07-06 23:07:26 +000086 }
87
Alex Lorenz91370c52015-06-22 20:37:46 +000088 bool is(TokenKind K) const { return Kind == K; }
89
90 bool isNot(TokenKind K) const { return Kind != K; }
91
92 StringRef::iterator location() const { return Range.begin(); }
93
Alex Lorenz33f0aef2015-06-26 16:46:11 +000094 StringRef stringValue() const { return Range.drop_front(StringOffset); }
Alex Lorenz240fc1e2015-06-23 23:42:28 +000095
96 const APSInt &integerValue() const { return IntVal; }
Alex Lorenz33f0aef2015-06-26 16:46:11 +000097
98 bool hasIntegerValue() const {
Alex Lorenz5d6108e2015-06-26 22:56:48 +000099 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
Alex Lorenz31d70682015-07-15 23:38:35 +0000100 Kind == GlobalValue || Kind == VirtualRegister ||
101 Kind == JumpTableIndex;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000102 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000103};
104
105/// Consume a single machine instruction token in the given source and return
106/// the remaining source string.
107StringRef lexMIToken(
108 StringRef Source, MIToken &Token,
109 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
110
111} // end namespace llvm
112
113#endif