1 //===- MC-X86Specific.cpp - X86-Specific code for MC ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements X86-specific parsing, encoding and decoding stuff for
13 //===----------------------------------------------------------------------===//
15 #include "AsmParser.h"
16 #include "llvm/MC/MCInst.h"
19 /// X86Operand - Instances of this class represent one X86 machine instruction.
20 struct AsmParser::X86Operand {
45 static X86Operand CreateReg(unsigned RegNo) {
48 Res.Reg.RegNo = RegNo;
51 static X86Operand CreateImm(MCValue Val) {
57 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
58 unsigned IndexReg, unsigned Scale) {
61 Res.Mem.SegReg = SegReg;
63 Res.Mem.BaseReg = BaseReg;
64 Res.Mem.IndexReg = IndexReg;
65 Res.Mem.Scale = Scale;
70 bool AsmParser::ParseX86Operand(X86Operand &Op) {
71 switch (Lexer.getKind()) {
73 return ParseX86MemOperand(Op);
74 case asmtok::Register:
75 // FIXME: Decode reg #.
76 // FIXME: if a segment register, this could either be just the seg reg, or
77 // the start of a memory operand.
78 Op = X86Operand::CreateReg(123);
79 Lexer.Lex(); // Eat register.
81 case asmtok::Dollar: {
85 if (ParseRelocatableExpression(Val))
87 Op = X86Operand::CreateImm(Val);
91 Lexer.Lex(); // Eat the star.
93 if (Lexer.is(asmtok::Register)) {
94 Op = X86Operand::CreateReg(123);
95 Lexer.Lex(); // Eat register.
96 } else if (ParseX86MemOperand(Op))
99 // FIXME: Note that these are 'dereferenced' so that clients know the '*' is
106 /// ParseX86MemOperand: segment: disp(basereg, indexreg, scale)
107 bool AsmParser::ParseX86MemOperand(X86Operand &Op) {
108 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
111 // We have to disambiguate a parenthesized expression "(4+5)" from the start
112 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
113 // only way to do this without lookahead is to eat the ( and see what is after
115 MCValue Disp = MCValue::get(0, 0, 0);
116 if (Lexer.isNot(asmtok::LParen)) {
117 if (ParseRelocatableExpression(Disp)) return true;
119 // After parsing the base expression we could either have a parenthesized
120 // memory address or not. If not, return now. If so, eat the (.
121 if (Lexer.isNot(asmtok::LParen)) {
122 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
129 // Okay, we have a '('. We don't know if this is an expression or not, but
130 // so we have to eat the ( to see beyond it.
131 Lexer.Lex(); // Eat the '('.
133 if (Lexer.is(asmtok::Register) || Lexer.is(asmtok::Comma)) {
134 // Nothing to do here, fall into the code below with the '(' part of the
135 // memory operand consumed.
137 // It must be an parenthesized expression, parse it now.
138 if (ParseRelocatableExpression(Disp))
141 // After parsing the base expression we could either have a parenthesized
142 // memory address or not. If not, return now. If so, eat the (.
143 if (Lexer.isNot(asmtok::LParen)) {
144 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
153 // If we reached here, then we just ate the ( of the memory operand. Process
154 // the rest of the memory operand.
155 unsigned BaseReg = 0, IndexReg = 0, Scale = 0;
157 if (Lexer.is(asmtok::Register)) {
158 BaseReg = 123; // FIXME: decode reg #
159 Lexer.Lex(); // eat the register.
162 if (Lexer.is(asmtok::Comma)) {
163 Lexer.Lex(); // eat the comma.
165 if (Lexer.is(asmtok::Register)) {
166 IndexReg = 123; // FIXME: decode reg #
167 Lexer.Lex(); // eat the register.
168 Scale = 1; // If not specified, the scale defaults to 1.
171 if (Lexer.is(asmtok::Comma)) {
172 Lexer.Lex(); // eat the comma.
174 // If present, get and validate scale amount.
175 if (Lexer.is(asmtok::IntVal)) {
176 int64_t ScaleVal = Lexer.getCurIntVal();
177 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
178 return TokError("scale factor in address must be 1, 2, 4 or 8");
179 Lexer.Lex(); // eat the scale.
180 Scale = (unsigned)ScaleVal;
185 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
186 if (Lexer.isNot(asmtok::RParen))
187 return TokError("unexpected token in memory operand");
188 Lexer.Lex(); // Eat the ')'.
190 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
194 /// MatchX86Inst - Convert a parsed instruction name and operand list into a
195 /// concrete instruction.
196 static bool MatchX86Inst(const char *Name,
197 llvm::SmallVector<AsmParser::X86Operand, 3> &Operands,
202 /// ParseX86InstOperands - Parse the operands of an X86 instruction and return
203 /// them as the operands of an MCInst.
204 bool AsmParser::ParseX86InstOperands(const char *InstName, MCInst &Inst) {
205 llvm::SmallVector<X86Operand, 3> Operands;
207 if (Lexer.isNot(asmtok::EndOfStatement)) {
208 // Read the first operand.
209 Operands.push_back(X86Operand());
210 if (ParseX86Operand(Operands.back()))
213 while (Lexer.is(asmtok::Comma)) {
214 Lexer.Lex(); // Eat the comma.
216 // Parse and remember the operand.
217 Operands.push_back(X86Operand());
218 if (ParseX86Operand(Operands.back()))
223 return MatchX86Inst(InstName, Operands, Inst);