blob: 08db8a1f1c6a91f9f8d450e7c00a48682ad6f730 [file] [log] [blame]
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst 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
Daniel Dunbar0b0441e2009-07-18 23:03:22 +000010#include "X86.h"
Daniel Dunbar78929e52009-07-20 20:01:54 +000011#include "llvm/ADT/SmallVector.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000012#include "llvm/ADT/Twine.h"
Daniel Dunbard80432a2009-07-28 20:47:52 +000013#include "llvm/MC/MCAsmLexer.h"
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +000014#include "llvm/MC/MCAsmParser.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000015#include "llvm/MC/MCValue.h"
16#include "llvm/Support/SourceMgr.h"
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000017#include "llvm/Target/TargetRegistry.h"
18#include "llvm/Target/TargetAsmParser.h"
19using namespace llvm;
20
21namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000022class X86Operand;
23
24class X86ATTAsmParser : public TargetAsmParser {
25 MCAsmParser &Parser;
26
27private:
28 bool MatchInstruction(const StringRef &Name,
Chris Lattnereb46b492009-07-29 06:29:53 +000029 SmallVectorImpl<X86Operand> &Operands,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000030 MCInst &Inst);
31
32 MCAsmParser &getParser() const { return Parser; }
33
34 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
35
36 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
37
38 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
39
40 bool ParseRegister(X86Operand &Op);
41
42 bool ParseOperand(X86Operand &Op);
43
44 bool ParseMemOperand(X86Operand &Op);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000045
46 /// @name Auto-generated Match Functions
47 /// {
48
49 bool MatchRegisterName(const StringRef &Name, unsigned &RegNo);
50
51 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000052
53public:
54 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
55 : TargetAsmParser(T), Parser(_Parser) {}
56
57 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
58};
Chris Lattnere54532b2009-07-29 06:33:53 +000059
60} // end anonymous namespace
61
62
63namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000064
65/// X86Operand - Instances of this class represent a parsed X86 machine
66/// instruction.
67struct X86Operand {
68 enum {
69 Register,
70 Immediate,
71 Memory
72 } Kind;
73
74 union {
75 struct {
76 unsigned RegNo;
77 } Reg;
78
79 struct {
80 MCValue Val;
81 } Imm;
82
83 struct {
84 unsigned SegReg;
85 MCValue Disp;
86 unsigned BaseReg;
87 unsigned IndexReg;
88 unsigned Scale;
89 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +000090 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000091
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000092 unsigned getReg() const {
93 assert(Kind == Register && "Invalid access!");
94 return Reg.RegNo;
95 }
Daniel Dunbard80432a2009-07-28 20:47:52 +000096
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000097 static X86Operand CreateReg(unsigned RegNo) {
98 X86Operand Res;
99 Res.Kind = Register;
100 Res.Reg.RegNo = RegNo;
101 return Res;
102 }
103 static X86Operand CreateImm(MCValue Val) {
104 X86Operand Res;
105 Res.Kind = Immediate;
106 Res.Imm.Val = Val;
107 return Res;
108 }
109 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
110 unsigned IndexReg, unsigned Scale) {
111 // If there is no index register, we should never have a scale, and we
112 // should always have a scale (in {1,2,4,8}) if we do.
113 assert(((Scale == 0 && !IndexReg) ||
114 (IndexReg && (Scale == 1 || Scale == 2 ||
115 Scale == 4 || Scale == 8))) &&
116 "Invalid scale!");
117 X86Operand Res;
118 Res.Kind = Memory;
119 Res.Mem.SegReg = SegReg;
120 Res.Mem.Disp = Disp;
121 Res.Mem.BaseReg = BaseReg;
122 Res.Mem.IndexReg = IndexReg;
123 Res.Mem.Scale = Scale;
124 return Res;
125 }
126};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000127
Chris Lattnere54532b2009-07-29 06:33:53 +0000128} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000129
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000130
131bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Chris Lattnere54532b2009-07-29 06:33:53 +0000132 const AsmToken &Tok = getLexer().getTok();
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000133 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000134
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000135 // FIXME: Validate register for the current architecture; we have to do
136 // validation later, so maybe there is no need for this here.
137 unsigned RegNo;
138 assert(Tok.getString().startswith("%") && "Invalid register name!");
139 if (MatchRegisterName(Tok.getString().substr(1), RegNo))
140 return Error(Tok.getLoc(), "invalid register name");
141
142 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000143 getLexer().Lex(); // Eat register token.
144
145 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000146}
147
Daniel Dunbar78929e52009-07-20 20:01:54 +0000148bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000149 switch (getLexer().getKind()) {
150 default:
151 return ParseMemOperand(Op);
152 case AsmToken::Register:
153 // FIXME: if a segment register, this could either be just the seg reg, or
154 // the start of a memory operand.
155 return ParseRegister(Op);
156 case AsmToken::Dollar: {
157 // $42 -> immediate.
158 getLexer().Lex();
159 MCValue Val;
160 if (getParser().ParseRelocatableExpression(Val))
161 return true;
162 Op = X86Operand::CreateImm(Val);
163 return false;
164 }
Chris Lattnere54532b2009-07-29 06:33:53 +0000165 case AsmToken::Star:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000166 getLexer().Lex(); // Eat the star.
167
168 if (getLexer().is(AsmToken::Register)) {
169 if (ParseRegister(Op))
170 return true;
171 } else if (ParseMemOperand(Op))
172 return true;
173
174 // FIXME: Note the '*' in the operand for use by the matcher.
175 return false;
176 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000177}
178
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000179/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
180bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
181 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
182 unsigned SegReg = 0;
183
184 // We have to disambiguate a parenthesized expression "(4+5)" from the start
185 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
186 // only way to do this without lookahead is to eat the ( and see what is after
187 // it.
188 MCValue Disp = MCValue::get(0, 0, 0);
189 if (getLexer().isNot(AsmToken::LParen)) {
190 if (getParser().ParseRelocatableExpression(Disp)) return true;
191
192 // After parsing the base expression we could either have a parenthesized
193 // memory address or not. If not, return now. If so, eat the (.
194 if (getLexer().isNot(AsmToken::LParen)) {
195 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
196 return false;
197 }
198
199 // Eat the '('.
200 getLexer().Lex();
201 } else {
202 // Okay, we have a '('. We don't know if this is an expression or not, but
203 // so we have to eat the ( to see beyond it.
204 getLexer().Lex(); // Eat the '('.
205
206 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
207 // Nothing to do here, fall into the code below with the '(' part of the
208 // memory operand consumed.
209 } else {
210 // It must be an parenthesized expression, parse it now.
211 if (getParser().ParseParenRelocatableExpression(Disp))
212 return true;
213
214 // After parsing the base expression we could either have a parenthesized
215 // memory address or not. If not, return now. If so, eat the (.
216 if (getLexer().isNot(AsmToken::LParen)) {
217 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
218 return false;
219 }
220
221 // Eat the '('.
222 getLexer().Lex();
223 }
224 }
225
226 // If we reached here, then we just ate the ( of the memory operand. Process
227 // the rest of the memory operand.
228 unsigned BaseReg = 0, IndexReg = 0, Scale = 0;
229
230 if (getLexer().is(AsmToken::Register)) {
231 if (ParseRegister(Op))
232 return true;
233 BaseReg = Op.getReg();
234 }
235
236 if (getLexer().is(AsmToken::Comma)) {
237 getLexer().Lex(); // Eat the comma.
238
239 // Following the comma we should have either an index register, or a scale
240 // value. We don't support the later form, but we want to parse it
241 // correctly.
242 //
243 // Not that even though it would be completely consistent to support syntax
244 // like "1(%eax,,1)", the assembler doesn't.
245 if (getLexer().is(AsmToken::Register)) {
246 if (ParseRegister(Op))
247 return true;
248 IndexReg = Op.getReg();
249 Scale = 1; // If not specified, the scale defaults to 1.
250
251 if (getLexer().isNot(AsmToken::RParen)) {
252 // Parse the scale amount:
253 // ::= ',' [scale-expression]
254 if (getLexer().isNot(AsmToken::Comma))
255 return true;
256 getLexer().Lex(); // Eat the comma.
257
258 if (getLexer().isNot(AsmToken::RParen)) {
259 SMLoc Loc = getLexer().getTok().getLoc();
260
261 int64_t ScaleVal;
262 if (getParser().ParseAbsoluteExpression(ScaleVal))
263 return true;
264
265 // Validate the scale amount.
266 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
267 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
268 Scale = (unsigned)ScaleVal;
269 }
270 }
271 } else if (getLexer().isNot(AsmToken::RParen)) {
272 // Otherwise we have the unsupported form of a scale amount without an
273 // index.
274 SMLoc Loc = getLexer().getTok().getLoc();
275
276 int64_t Value;
277 if (getParser().ParseAbsoluteExpression(Value))
278 return true;
279
280 return Error(Loc, "cannot have scale factor without index register");
281 }
282 }
283
284 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
285 if (getLexer().isNot(AsmToken::RParen))
286 return Error(getLexer().getTok().getLoc(),
287 "unexpected token in memory operand");
288 getLexer().Lex(); // Eat the ')'.
289
290 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
291 return false;
292}
293
294bool
295X86ATTAsmParser::MatchInstruction(const StringRef &Name,
Chris Lattnereb46b492009-07-29 06:29:53 +0000296 SmallVectorImpl<X86Operand> &Operands,
Daniel Dunbar78929e52009-07-20 20:01:54 +0000297 MCInst &Inst) {
298 return false;
299}
300
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000301bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Chris Lattnereb46b492009-07-29 06:29:53 +0000302 SmallVector<X86Operand, 3> Operands;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000303
304 if (getLexer().isNot(AsmToken::EndOfStatement)) {
305 // Read the first operand.
306 Operands.push_back(X86Operand());
307 if (ParseOperand(Operands.back()))
308 return true;
309
310 while (getLexer().is(AsmToken::Comma)) {
311 getLexer().Lex(); // Eat the comma.
312
313 // Parse and remember the operand.
314 Operands.push_back(X86Operand());
315 if (ParseOperand(Operands.back()))
316 return true;
317 }
318 }
319
Daniel Dunbar78929e52009-07-20 20:01:54 +0000320 return MatchInstruction(Name, Operands, Inst);
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000321}
322
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000323// Force static initialization.
324extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000325 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
326 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000327}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000328
329#include "X86GenAsmMatcher.inc"