blob: 12e9f937fc2ce097705b45883754c4300c0f2c73 [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,
29 llvm::SmallVector<X86Operand, 3> &Operands,
30 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);
45
46public:
47 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
48 : TargetAsmParser(T), Parser(_Parser) {}
49
50 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
51};
52
53/// X86Operand - Instances of this class represent a parsed X86 machine
54/// instruction.
55struct X86Operand {
56 enum {
57 Register,
58 Immediate,
59 Memory
60 } Kind;
61
62 union {
63 struct {
64 unsigned RegNo;
65 } Reg;
66
67 struct {
68 MCValue Val;
69 } Imm;
70
71 struct {
72 unsigned SegReg;
73 MCValue Disp;
74 unsigned BaseReg;
75 unsigned IndexReg;
76 unsigned Scale;
77 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +000078 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000079
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000080 unsigned getReg() const {
81 assert(Kind == Register && "Invalid access!");
82 return Reg.RegNo;
83 }
Daniel Dunbard80432a2009-07-28 20:47:52 +000084
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000085 static X86Operand CreateReg(unsigned RegNo) {
86 X86Operand Res;
87 Res.Kind = Register;
88 Res.Reg.RegNo = RegNo;
89 return Res;
90 }
91 static X86Operand CreateImm(MCValue Val) {
92 X86Operand Res;
93 Res.Kind = Immediate;
94 Res.Imm.Val = Val;
95 return Res;
96 }
97 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
98 unsigned IndexReg, unsigned Scale) {
99 // If there is no index register, we should never have a scale, and we
100 // should always have a scale (in {1,2,4,8}) if we do.
101 assert(((Scale == 0 && !IndexReg) ||
102 (IndexReg && (Scale == 1 || Scale == 2 ||
103 Scale == 4 || Scale == 8))) &&
104 "Invalid scale!");
105 X86Operand Res;
106 Res.Kind = Memory;
107 Res.Mem.SegReg = SegReg;
108 Res.Mem.Disp = Disp;
109 Res.Mem.BaseReg = BaseReg;
110 Res.Mem.IndexReg = IndexReg;
111 Res.Mem.Scale = Scale;
112 return Res;
113 }
114};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000115
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000116}
Daniel Dunbard80432a2009-07-28 20:47:52 +0000117
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000118//
119
120bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
121 assert(getLexer().is(AsmToken::Register) && "Invalid token kind!");
122
123 // FIXME: Decode register number.
124 Op = X86Operand::CreateReg(123);
125 getLexer().Lex(); // Eat register token.
126
127 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000128}
129
Daniel Dunbar78929e52009-07-20 20:01:54 +0000130bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000131 switch (getLexer().getKind()) {
132 default:
133 return ParseMemOperand(Op);
134 case AsmToken::Register:
135 // FIXME: if a segment register, this could either be just the seg reg, or
136 // the start of a memory operand.
137 return ParseRegister(Op);
138 case AsmToken::Dollar: {
139 // $42 -> immediate.
140 getLexer().Lex();
141 MCValue Val;
142 if (getParser().ParseRelocatableExpression(Val))
143 return true;
144 Op = X86Operand::CreateImm(Val);
145 return false;
146 }
147 case AsmToken::Star: {
148 getLexer().Lex(); // Eat the star.
149
150 if (getLexer().is(AsmToken::Register)) {
151 if (ParseRegister(Op))
152 return true;
153 } else if (ParseMemOperand(Op))
154 return true;
155
156 // FIXME: Note the '*' in the operand for use by the matcher.
157 return false;
158 }
159 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000160}
161
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000162/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
163bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
164 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
165 unsigned SegReg = 0;
166
167 // We have to disambiguate a parenthesized expression "(4+5)" from the start
168 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
169 // only way to do this without lookahead is to eat the ( and see what is after
170 // it.
171 MCValue Disp = MCValue::get(0, 0, 0);
172 if (getLexer().isNot(AsmToken::LParen)) {
173 if (getParser().ParseRelocatableExpression(Disp)) return true;
174
175 // After parsing the base expression we could either have a parenthesized
176 // memory address or not. If not, return now. If so, eat the (.
177 if (getLexer().isNot(AsmToken::LParen)) {
178 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
179 return false;
180 }
181
182 // Eat the '('.
183 getLexer().Lex();
184 } else {
185 // Okay, we have a '('. We don't know if this is an expression or not, but
186 // so we have to eat the ( to see beyond it.
187 getLexer().Lex(); // Eat the '('.
188
189 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
190 // Nothing to do here, fall into the code below with the '(' part of the
191 // memory operand consumed.
192 } else {
193 // It must be an parenthesized expression, parse it now.
194 if (getParser().ParseParenRelocatableExpression(Disp))
195 return true;
196
197 // After parsing the base expression we could either have a parenthesized
198 // memory address or not. If not, return now. If so, eat the (.
199 if (getLexer().isNot(AsmToken::LParen)) {
200 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
201 return false;
202 }
203
204 // Eat the '('.
205 getLexer().Lex();
206 }
207 }
208
209 // If we reached here, then we just ate the ( of the memory operand. Process
210 // the rest of the memory operand.
211 unsigned BaseReg = 0, IndexReg = 0, Scale = 0;
212
213 if (getLexer().is(AsmToken::Register)) {
214 if (ParseRegister(Op))
215 return true;
216 BaseReg = Op.getReg();
217 }
218
219 if (getLexer().is(AsmToken::Comma)) {
220 getLexer().Lex(); // Eat the comma.
221
222 // Following the comma we should have either an index register, or a scale
223 // value. We don't support the later form, but we want to parse it
224 // correctly.
225 //
226 // Not that even though it would be completely consistent to support syntax
227 // like "1(%eax,,1)", the assembler doesn't.
228 if (getLexer().is(AsmToken::Register)) {
229 if (ParseRegister(Op))
230 return true;
231 IndexReg = Op.getReg();
232 Scale = 1; // If not specified, the scale defaults to 1.
233
234 if (getLexer().isNot(AsmToken::RParen)) {
235 // Parse the scale amount:
236 // ::= ',' [scale-expression]
237 if (getLexer().isNot(AsmToken::Comma))
238 return true;
239 getLexer().Lex(); // Eat the comma.
240
241 if (getLexer().isNot(AsmToken::RParen)) {
242 SMLoc Loc = getLexer().getTok().getLoc();
243
244 int64_t ScaleVal;
245 if (getParser().ParseAbsoluteExpression(ScaleVal))
246 return true;
247
248 // Validate the scale amount.
249 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
250 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
251 Scale = (unsigned)ScaleVal;
252 }
253 }
254 } else if (getLexer().isNot(AsmToken::RParen)) {
255 // Otherwise we have the unsupported form of a scale amount without an
256 // index.
257 SMLoc Loc = getLexer().getTok().getLoc();
258
259 int64_t Value;
260 if (getParser().ParseAbsoluteExpression(Value))
261 return true;
262
263 return Error(Loc, "cannot have scale factor without index register");
264 }
265 }
266
267 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
268 if (getLexer().isNot(AsmToken::RParen))
269 return Error(getLexer().getTok().getLoc(),
270 "unexpected token in memory operand");
271 getLexer().Lex(); // Eat the ')'.
272
273 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
274 return false;
275}
276
277bool
278X86ATTAsmParser::MatchInstruction(const StringRef &Name,
Daniel Dunbar78929e52009-07-20 20:01:54 +0000279 llvm::SmallVector<X86Operand, 3> &Operands,
280 MCInst &Inst) {
281 return false;
282}
283
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000284bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Daniel Dunbar78929e52009-07-20 20:01:54 +0000285 llvm::SmallVector<X86Operand, 3> Operands;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000286
287 if (getLexer().isNot(AsmToken::EndOfStatement)) {
288 // Read the first operand.
289 Operands.push_back(X86Operand());
290 if (ParseOperand(Operands.back()))
291 return true;
292
293 while (getLexer().is(AsmToken::Comma)) {
294 getLexer().Lex(); // Eat the comma.
295
296 // Parse and remember the operand.
297 Operands.push_back(X86Operand());
298 if (ParseOperand(Operands.back()))
299 return true;
300 }
301 }
302
Daniel Dunbar78929e52009-07-20 20:01:54 +0000303 return MatchInstruction(Name, Operands, Inst);
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000304}
305
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000306// Force static initialization.
307extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000308 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
309 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000310}