blob: c203d2c4817bc9281f407206d0668384b8319b97 [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/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/ADT/StringRef.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000021#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,
Alex Lorenz5022f6b2015-08-13 23:10:16 +000033 Newline,
Alex Lorenz91370c52015-06-22 20:37:46 +000034
Alex Lorenzf3db51de2015-06-23 16:35:26 +000035 // Tokens with no info.
36 comma,
37 equal,
Alex Lorenz12b554e2015-06-24 17:34:58 +000038 underscore,
Alex Lorenz2eacca82015-07-13 23:24:34 +000039 colon,
Alex Lorenz4af7e612015-08-03 23:08:19 +000040 coloncolon,
Matthias Braun333e4682016-07-26 21:49:34 +000041 dot,
Alex Lorenz35e44462015-07-22 17:58:46 +000042 exclaim,
Alex Lorenzdeb53492015-07-28 17:28:03 +000043 lparen,
44 rparen,
Alex Lorenzf9a2b122015-08-14 18:57:24 +000045 lbrace,
46 rbrace,
Alex Lorenz5672a892015-08-05 22:26:15 +000047 plus,
48 minus,
Quentin Colombet5e63e782016-03-08 02:00:43 +000049 less,
50 greater,
Alex Lorenzf3db51de2015-06-23 16:35:26 +000051
Alex Lorenzcb268d42015-07-06 23:07:26 +000052 // Keywords
53 kw_implicit,
54 kw_implicit_define,
Alex Lorenze66a7cc2015-08-19 18:55:47 +000055 kw_def,
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +000056 kw_dead,
Justin Lebaradbf09e2016-09-11 01:38:58 +000057 kw_dereferenceable,
Alex Lorenz495ad872015-07-08 21:23:34 +000058 kw_killed,
Alex Lorenz4d026b892015-07-08 23:58:31 +000059 kw_undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +000060 kw_internal,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +000061 kw_early_clobber,
Alex Lorenz90752582015-08-05 17:41:17 +000062 kw_debug_use,
Alex Lorenz5ef93b02015-08-19 19:05:34 +000063 kw_tied_def,
Alex Lorenze5a44662015-07-17 00:24:15 +000064 kw_frame_setup,
Alex Lorenz46d760d2015-07-22 21:15:11 +000065 kw_debug_location,
Alex Lorenz577d2712015-08-14 21:55:58 +000066 kw_cfi_same_value,
Alex Lorenz8cfc6862015-07-23 23:09:07 +000067 kw_cfi_offset,
Alex Lorenz5b0d5f62015-07-27 20:39:03 +000068 kw_cfi_def_cfa_register,
Alex Lorenzf4baeb52015-07-21 22:28:27 +000069 kw_cfi_def_cfa_offset,
Alex Lorenzb1393232015-07-29 18:57:23 +000070 kw_cfi_def_cfa,
Alex Lorenzdeb53492015-07-28 17:28:03 +000071 kw_blockaddress,
Tim Northover6b3bd612016-07-29 20:32:59 +000072 kw_intrinsic,
Alex Lorenzef5c1962015-07-28 23:02:45 +000073 kw_target_index,
Alex Lorenzad156fb2015-07-31 20:49:21 +000074 kw_half,
75 kw_float,
76 kw_double,
77 kw_x86_fp80,
78 kw_fp128,
79 kw_ppc_fp128,
Alex Lorenz49873a82015-08-06 00:44:07 +000080 kw_target_flags,
Alex Lorenza518b792015-08-04 00:24:45 +000081 kw_volatile,
Alex Lorenz10fd0382015-08-06 16:49:30 +000082 kw_non_temporal,
Alex Lorenzdc8de2a2015-08-06 16:55:53 +000083 kw_invariant,
Alex Lorenz61420f72015-08-07 20:48:30 +000084 kw_align,
Alex Lorenz46e95582015-08-12 20:44:16 +000085 kw_stack,
Alex Lorenzd858f872015-08-12 21:00:22 +000086 kw_got,
Alex Lorenz4be56e92015-08-12 21:11:08 +000087 kw_jump_table,
Alex Lorenz91097a32015-08-12 20:33:26 +000088 kw_constant_pool,
Alex Lorenz0d009642015-08-20 00:12:57 +000089 kw_call_entry,
Alex Lorenzb97c9ef2015-08-10 23:24:42 +000090 kw_liveout,
Alex Lorenz5022f6b2015-08-13 23:10:16 +000091 kw_address_taken,
92 kw_landing_pad,
93 kw_liveins,
94 kw_successors,
Tim Northoverde3aea0412016-08-17 20:25:25 +000095 kw_floatpred,
96 kw_intpred,
Alex Lorenzcb268d42015-07-06 23:07:26 +000097
Alex Lorenza617c912015-08-17 22:05:15 +000098 // Named metadata keywords
99 md_tbaa,
Alex Lorenza16f6242015-08-17 22:06:40 +0000100 md_alias_scope,
Alex Lorenz03e940d2015-08-17 22:08:02 +0000101 md_noalias,
Alex Lorenzeb625682015-08-17 22:09:52 +0000102 md_range,
Reid Kleckner6d353342017-08-23 20:31:27 +0000103 md_diexpr,
Alex Lorenza617c912015-08-17 22:05:15 +0000104
Alex Lorenz91370c52015-06-22 20:37:46 +0000105 // Identifier tokens
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000106 Identifier,
Alex Lorenz05e38822015-08-05 18:52:21 +0000107 IntegerType,
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000108 NamedRegister,
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000109 MachineBasicBlockLabel,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000110 MachineBasicBlock,
Tim Northoverbd505462016-07-22 16:59:52 +0000111 PointerType,
Tim Northover62ae5682016-07-20 19:09:30 +0000112 ScalarType,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000113 StackObject,
114 FixedStackObject,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000115 NamedGlobalValue,
116 GlobalValue,
Alex Lorenz6ede3742015-07-21 16:59:53 +0000117 ExternalSymbol,
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000118
119 // Other tokens
Alex Lorenz53464512015-07-10 22:51:20 +0000120 IntegerLiteral,
Alex Lorenzad156fb2015-07-31 20:49:21 +0000121 FloatingPointLiteral,
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000122 HexLiteral,
Alex Lorenz31d70682015-07-15 23:38:35 +0000123 VirtualRegister,
Alex Lorenzab980492015-07-20 20:51:18 +0000124 ConstantPoolItem,
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000125 JumpTableIndex,
Alex Lorenzdeb53492015-07-28 17:28:03 +0000126 NamedIRBlock,
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000127 IRBlock,
Alex Lorenz4af7e612015-08-03 23:08:19 +0000128 NamedIRValue,
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000129 IRValue,
Matthias Braunb74eb412016-03-28 18:18:46 +0000130 QuotedIRValue, // `<constant value>`
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000131 SubRegisterIndex,
132 StringConstant
Alex Lorenz91370c52015-06-22 20:37:46 +0000133 };
134
135private:
136 TokenKind Kind;
137 StringRef Range;
Alex Lorenz3fb77682015-08-06 23:17:42 +0000138 StringRef StringValue;
139 std::string StringValueStorage;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000140 APSInt IntVal;
Alex Lorenz91370c52015-06-22 20:37:46 +0000141
142public:
Alex Lorenz3fb77682015-08-06 23:17:42 +0000143 MIToken() : Kind(Error) {}
Alex Lorenz970c12e2015-08-05 17:35:55 +0000144
Alex Lorenz3fb77682015-08-06 23:17:42 +0000145 MIToken &reset(TokenKind Kind, StringRef Range);
Alex Lorenz91370c52015-06-22 20:37:46 +0000146
Alex Lorenz3fb77682015-08-06 23:17:42 +0000147 MIToken &setStringValue(StringRef StrVal);
148 MIToken &setOwnedStringValue(std::string StrVal);
149 MIToken &setIntegerValue(APSInt IntVal);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000150
Alex Lorenz91370c52015-06-22 20:37:46 +0000151 TokenKind kind() const { return Kind; }
152
153 bool isError() const { return Kind == Error; }
154
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000155 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
156
157 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
158
Alex Lorenz12b554e2015-06-24 17:34:58 +0000159 bool isRegister() const {
Alex Lorenz53464512015-07-10 22:51:20 +0000160 return Kind == NamedRegister || Kind == underscore ||
161 Kind == VirtualRegister;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000162 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000163
Alex Lorenzcb268d42015-07-06 23:07:26 +0000164 bool isRegisterFlag() const {
Alex Lorenz495ad872015-07-08 21:23:34 +0000165 return Kind == kw_implicit || Kind == kw_implicit_define ||
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000166 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
167 Kind == kw_undef || Kind == kw_internal ||
168 Kind == kw_early_clobber || Kind == kw_debug_use;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000169 }
170
Alex Lorenz10fd0382015-08-06 16:49:30 +0000171 bool isMemoryOperandFlag() const {
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000172 return Kind == kw_volatile || Kind == kw_non_temporal ||
Geoff Berry6748abe2017-07-13 02:28:54 +0000173 Kind == kw_dereferenceable || Kind == kw_invariant ||
174 Kind == StringConstant;
Alex Lorenz10fd0382015-08-06 16:49:30 +0000175 }
Alex Lorenza518b792015-08-04 00:24:45 +0000176
Alex Lorenz91370c52015-06-22 20:37:46 +0000177 bool is(TokenKind K) const { return Kind == K; }
178
179 bool isNot(TokenKind K) const { return Kind != K; }
180
181 StringRef::iterator location() const { return Range.begin(); }
182
Alex Lorenz3fb77682015-08-06 23:17:42 +0000183 StringRef range() const { return Range; }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000184
Alex Lorenz970c12e2015-08-05 17:35:55 +0000185 /// Return the token's string value.
Alex Lorenz3fb77682015-08-06 23:17:42 +0000186 StringRef stringValue() const { return StringValue; }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000187
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000188 const APSInt &integerValue() const { return IntVal; }
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000189
190 bool hasIntegerValue() const {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000191 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000192 Kind == MachineBasicBlockLabel || Kind == StackObject ||
193 Kind == FixedStackObject || Kind == GlobalValue ||
194 Kind == VirtualRegister || Kind == ConstantPoolItem ||
Alex Lorenzdd13be02015-08-19 23:31:05 +0000195 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000196 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000197};
198
199/// Consume a single machine instruction token in the given source and return
200/// the remaining source string.
201StringRef lexMIToken(
202 StringRef Source, MIToken &Token,
203 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
204
205} // end namespace llvm
206
207#endif