blob: 771f6047e5c9a68015fa09906b97df476bf4c3eb [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 Lorenze5a44662015-07-17 00:24:15 +000046 kw_frame_setup,
Alex Lorenzf4baeb52015-07-21 22:28:27 +000047 kw_cfi_def_cfa_offset,
Alex Lorenzcb268d42015-07-06 23:07:26 +000048
Alex Lorenz91370c52015-06-22 20:37:46 +000049 // Identifier tokens
Alex Lorenzf3db51de2015-06-23 16:35:26 +000050 Identifier,
Alex Lorenz240fc1e2015-06-23 23:42:28 +000051 NamedRegister,
Alex Lorenz33f0aef2015-06-26 16:46:11 +000052 MachineBasicBlock,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000053 StackObject,
54 FixedStackObject,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000055 NamedGlobalValue,
Alex Lorenzb29554d2015-07-20 20:31:01 +000056 QuotedNamedGlobalValue,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000057 GlobalValue,
Alex Lorenz6ede3742015-07-21 16:59:53 +000058 ExternalSymbol,
59 QuotedExternalSymbol,
Alex Lorenz240fc1e2015-06-23 23:42:28 +000060
61 // Other tokens
Alex Lorenz53464512015-07-10 22:51:20 +000062 IntegerLiteral,
Alex Lorenz31d70682015-07-15 23:38:35 +000063 VirtualRegister,
Alex Lorenzab980492015-07-20 20:51:18 +000064 ConstantPoolItem,
Alex Lorenz31d70682015-07-15 23:38:35 +000065 JumpTableIndex
Alex Lorenz91370c52015-06-22 20:37:46 +000066 };
67
68private:
69 TokenKind Kind;
Alex Lorenz33f0aef2015-06-26 16:46:11 +000070 unsigned StringOffset;
Alex Lorenz91370c52015-06-22 20:37:46 +000071 StringRef Range;
Alex Lorenz240fc1e2015-06-23 23:42:28 +000072 APSInt IntVal;
Alex Lorenz91370c52015-06-22 20:37:46 +000073
74public:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000075 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
76 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
Alex Lorenz91370c52015-06-22 20:37:46 +000077
Alex Lorenz33f0aef2015-06-26 16:46:11 +000078 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
79 unsigned StringOffset = 0)
80 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
Alex Lorenz240fc1e2015-06-23 23:42:28 +000081
Alex Lorenz91370c52015-06-22 20:37:46 +000082 TokenKind kind() const { return Kind; }
83
84 bool isError() const { return Kind == Error; }
85
Alex Lorenz12b554e2015-06-24 17:34:58 +000086 bool isRegister() const {
Alex Lorenz53464512015-07-10 22:51:20 +000087 return Kind == NamedRegister || Kind == underscore ||
88 Kind == VirtualRegister;
Alex Lorenz12b554e2015-06-24 17:34:58 +000089 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +000090
Alex Lorenzcb268d42015-07-06 23:07:26 +000091 bool isRegisterFlag() const {
Alex Lorenz495ad872015-07-08 21:23:34 +000092 return Kind == kw_implicit || Kind == kw_implicit_define ||
Alex Lorenz4d026b892015-07-08 23:58:31 +000093 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
Alex Lorenzcb268d42015-07-06 23:07:26 +000094 }
95
Alex Lorenz91370c52015-06-22 20:37:46 +000096 bool is(TokenKind K) const { return Kind == K; }
97
98 bool isNot(TokenKind K) const { return Kind != K; }
99
100 StringRef::iterator location() const { return Range.begin(); }
101
Alex Lorenz6ede3742015-07-21 16:59:53 +0000102 bool isStringValueQuoted() const {
103 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol;
104 }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000105
106 /// Return the token's raw string value.
107 ///
108 /// If the string value is quoted, this method returns that quoted string as
109 /// it is, without unescaping the string value.
110 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
111
112 /// Return token's string value.
113 ///
114 /// Expects the string value to be unquoted.
115 StringRef stringValue() const {
116 assert(!isStringValueQuoted() && "String value is quoted");
117 return Range.drop_front(StringOffset);
118 }
119
120 /// Unescapes the token's string value.
121 ///
122 /// Expects the string value to be quoted.
123 void unescapeQuotedStringValue(std::string &Str) const;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000124
125 const APSInt &integerValue() const { return IntVal; }
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000126
127 bool hasIntegerValue() const {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000128 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000129 Kind == StackObject || Kind == FixedStackObject ||
Alex Lorenz31d70682015-07-15 23:38:35 +0000130 Kind == GlobalValue || Kind == VirtualRegister ||
Alex Lorenzab980492015-07-20 20:51:18 +0000131 Kind == ConstantPoolItem || Kind == JumpTableIndex;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000132 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000133};
134
135/// Consume a single machine instruction token in the given source and return
136/// the remaining source string.
137StringRef lexMIToken(
138 StringRef Source, MIToken &Token,
139 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
140
141} // end namespace llvm
142
143#endif