blob: f72897a5217bcb16f7f9c0c4f876f7fbe06b5b97 [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 Lorenz4af7e612015-08-03 23:08:19 +000039 coloncolon,
Alex Lorenz35e44462015-07-22 17:58:46 +000040 exclaim,
Alex Lorenzdeb53492015-07-28 17:28:03 +000041 lparen,
42 rparen,
Alex Lorenzf3db51de2015-06-23 16:35:26 +000043
Alex Lorenzcb268d42015-07-06 23:07:26 +000044 // Keywords
45 kw_implicit,
46 kw_implicit_define,
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +000047 kw_dead,
Alex Lorenz495ad872015-07-08 21:23:34 +000048 kw_killed,
Alex Lorenz4d026b892015-07-08 23:58:31 +000049 kw_undef,
Alex Lorenze5a44662015-07-17 00:24:15 +000050 kw_frame_setup,
Alex Lorenz46d760d2015-07-22 21:15:11 +000051 kw_debug_location,
Alex Lorenz8cfc6862015-07-23 23:09:07 +000052 kw_cfi_offset,
Alex Lorenz5b0d5f62015-07-27 20:39:03 +000053 kw_cfi_def_cfa_register,
Alex Lorenzf4baeb52015-07-21 22:28:27 +000054 kw_cfi_def_cfa_offset,
Alex Lorenzb1393232015-07-29 18:57:23 +000055 kw_cfi_def_cfa,
Alex Lorenzdeb53492015-07-28 17:28:03 +000056 kw_blockaddress,
Alex Lorenzef5c1962015-07-28 23:02:45 +000057 kw_target_index,
Alex Lorenzad156fb2015-07-31 20:49:21 +000058 kw_half,
59 kw_float,
60 kw_double,
61 kw_x86_fp80,
62 kw_fp128,
63 kw_ppc_fp128,
Alex Lorenza518b792015-08-04 00:24:45 +000064 kw_volatile,
Alex Lorenzcb268d42015-07-06 23:07:26 +000065
Alex Lorenz91370c52015-06-22 20:37:46 +000066 // Identifier tokens
Alex Lorenzf3db51de2015-06-23 16:35:26 +000067 Identifier,
Alex Lorenz240fc1e2015-06-23 23:42:28 +000068 NamedRegister,
Alex Lorenz33f0aef2015-06-26 16:46:11 +000069 MachineBasicBlock,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000070 StackObject,
71 FixedStackObject,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000072 NamedGlobalValue,
73 GlobalValue,
Alex Lorenz6ede3742015-07-21 16:59:53 +000074 ExternalSymbol,
Alex Lorenz240fc1e2015-06-23 23:42:28 +000075
76 // Other tokens
Alex Lorenz53464512015-07-10 22:51:20 +000077 IntegerLiteral,
Alex Lorenzad156fb2015-07-31 20:49:21 +000078 FloatingPointLiteral,
Alex Lorenz31d70682015-07-15 23:38:35 +000079 VirtualRegister,
Alex Lorenzab980492015-07-20 20:51:18 +000080 ConstantPoolItem,
Alex Lorenz8a1915b2015-07-27 22:42:41 +000081 JumpTableIndex,
Alex Lorenzdeb53492015-07-28 17:28:03 +000082 NamedIRBlock,
Alex Lorenz8a1915b2015-07-27 22:42:41 +000083 IRBlock,
Alex Lorenz4af7e612015-08-03 23:08:19 +000084 NamedIRValue,
Alex Lorenz91370c52015-06-22 20:37:46 +000085 };
86
87private:
88 TokenKind Kind;
Alex Lorenz33f0aef2015-06-26 16:46:11 +000089 unsigned StringOffset;
Alex Lorenz970c12e2015-08-05 17:35:55 +000090 bool HasStringValue;
Alex Lorenz91370c52015-06-22 20:37:46 +000091 StringRef Range;
Alex Lorenz970c12e2015-08-05 17:35:55 +000092 std::string StringValue;
Alex Lorenz240fc1e2015-06-23 23:42:28 +000093 APSInt IntVal;
Alex Lorenz91370c52015-06-22 20:37:46 +000094
95public:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000096 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
Alex Lorenz970c12e2015-08-05 17:35:55 +000097 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
98 Range(Range) {}
99
100 MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
101 unsigned StringOffset = 0)
102 : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
103 Range(Range), StringValue(std::move(StringValue)) {}
Alex Lorenz91370c52015-06-22 20:37:46 +0000104
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000105 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
106 unsigned StringOffset = 0)
Alex Lorenz970c12e2015-08-05 17:35:55 +0000107 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
108 Range(Range), IntVal(IntVal) {}
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000109
Alex Lorenz91370c52015-06-22 20:37:46 +0000110 TokenKind kind() const { return Kind; }
111
112 bool isError() const { return Kind == Error; }
113
Alex Lorenz12b554e2015-06-24 17:34:58 +0000114 bool isRegister() const {
Alex Lorenz53464512015-07-10 22:51:20 +0000115 return Kind == NamedRegister || Kind == underscore ||
116 Kind == VirtualRegister;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000117 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000118
Alex Lorenzcb268d42015-07-06 23:07:26 +0000119 bool isRegisterFlag() const {
Alex Lorenz495ad872015-07-08 21:23:34 +0000120 return Kind == kw_implicit || Kind == kw_implicit_define ||
Alex Lorenz4d026b892015-07-08 23:58:31 +0000121 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000122 }
123
Alex Lorenza518b792015-08-04 00:24:45 +0000124 bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
125
Alex Lorenz91370c52015-06-22 20:37:46 +0000126 bool is(TokenKind K) const { return Kind == K; }
127
128 bool isNot(TokenKind K) const { return Kind != K; }
129
130 StringRef::iterator location() const { return Range.begin(); }
131
Alex Lorenzb29554d2015-07-20 20:31:01 +0000132 /// Return the token's raw string value.
133 ///
134 /// If the string value is quoted, this method returns that quoted string as
135 /// it is, without unescaping the string value.
136 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
137
Alex Lorenz970c12e2015-08-05 17:35:55 +0000138 /// Return the token's string value.
Alex Lorenzb29554d2015-07-20 20:31:01 +0000139 StringRef stringValue() const {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000140 return HasStringValue ? StringRef(StringValue)
141 : Range.drop_front(StringOffset);
Alex Lorenzb29554d2015-07-20 20:31:01 +0000142 }
143
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000144 const APSInt &integerValue() const { return IntVal; }
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000145
146 bool hasIntegerValue() const {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000147 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000148 Kind == StackObject || Kind == FixedStackObject ||
Alex Lorenz31d70682015-07-15 23:38:35 +0000149 Kind == GlobalValue || Kind == VirtualRegister ||
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000150 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
151 Kind == IRBlock;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000152 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000153};
154
155/// Consume a single machine instruction token in the given source and return
156/// the remaining source string.
157StringRef lexMIToken(
158 StringRef Source, MIToken &Token,
159 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
160
161} // end namespace llvm
162
163#endif