1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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 class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "AsmParser.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/raw_ostream.h"
19 bool AsmParser::Error(SMLoc L, const char *Msg) {
20 Lexer.PrintMessage(L, Msg);
24 bool AsmParser::TokError(const char *Msg) {
25 Lexer.PrintMessage(Lexer.getLoc(), Msg);
29 bool AsmParser::Run() {
33 while (Lexer.isNot(asmtok::Eof))
40 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
41 void AsmParser::EatToEndOfStatement() {
42 while (Lexer.isNot(asmtok::EndOfStatement) &&
43 Lexer.isNot(asmtok::Eof))
47 if (Lexer.is(asmtok::EndOfStatement))
52 struct AsmParser::X86Operand {
65 // FIXME: Should be a general expression.
71 int64_t Disp; // FIXME: Should be a general expression.
78 static X86Operand CreateReg(unsigned RegNo) {
81 Res.Reg.RegNo = RegNo;
84 static X86Operand CreateImm(int64_t Val) {
90 static X86Operand CreateMem(unsigned SegReg, int64_t Disp, unsigned BaseReg,
91 unsigned Scale, unsigned ScaleReg) {
94 Res.Mem.SegReg = SegReg;
96 Res.Mem.BaseReg = BaseReg;
97 Res.Mem.Scale = Scale;
98 Res.Mem.ScaleReg = ScaleReg;
103 bool AsmParser::ParseX86Operand(X86Operand &Op) {
104 switch (Lexer.getKind()) {
106 return ParseX86MemOperand(Op);
107 case asmtok::Register:
108 // FIXME: Decode reg #.
109 // FIXME: if a segment register, this could either be just the seg reg, or
110 // the start of a memory operand.
111 Op = X86Operand::CreateReg(0);
112 Lexer.Lex(); // Eat register.
114 case asmtok::Dollar: {
118 if (ParseExpression(Val))
119 return TokError("expected integer constant");
120 Op = X86Operand::CreateReg(Val);
127 // Note that these are both "dereferenced".
131 /// ParseX86MemOperand: segment: disp(basereg, indexreg, scale)
132 bool AsmParser::ParseX86MemOperand(X86Operand &Op) {
133 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
137 // We have to disambiguate a parenthesized expression "(4+5)" from the start
138 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
139 // only way to do this without lookahead is to eat the ( and see what is after
142 if (Lexer.isNot(asmtok::LParen)) {
143 if (ParseExpression(Disp)) return true;
145 // After parsing the base expression we could either have a parenthesized
146 // memory address or not. If not, return now. If so, eat the (.
147 if (Lexer.isNot(asmtok::LParen)) {
148 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
155 // Okay, we have a '('. We don't know if this is an expression or not, but
156 // so we have to eat the ( to see beyond it.
157 Lexer.Lex(); // Eat the '('.
159 if (Lexer.is(asmtok::Register) || Lexer.is(asmtok::Comma)) {
160 // Nothing to do here, fall into the code below with the '(' part of the
161 // memory operand consumed.
163 // FIXME: Call ParseParenExpression with the leading ( consumed.
164 return TokError("FIXME: Paren expr not implemented yet!");
168 // If we reached here, then we just ate the ( of the memory operand. Process
169 // the rest of the memory operand.
170 unsigned BaseReg = 0, ScaleReg = 0, Scale = 0;
172 if (Lexer.is(asmtok::Register)) {
173 BaseReg = 123; // FIXME: decode reg #
174 Lexer.Lex(); // eat the register.
177 if (Lexer.is(asmtok::Comma)) {
178 Lexer.Lex(); // eat the comma.
180 if (Lexer.is(asmtok::Register)) {
181 ScaleReg = 123; // FIXME: decode reg #
182 Lexer.Lex(); // eat the register.
183 Scale = 1; // If not specified, the scale defaults to 1.
186 if (Lexer.is(asmtok::Comma)) {
187 Lexer.Lex(); // eat the comma.
189 // If present, get and validate scale amount.
190 if (Lexer.is(asmtok::IntVal)) {
191 int64_t ScaleVal = Lexer.getCurIntVal();
192 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
193 return TokError("scale factor in address must be 1, 2, 4 or 8");
194 Lexer.Lex(); // eat the scale.
195 Scale = (unsigned)ScaleVal;
200 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
201 if (Lexer.isNot(asmtok::RParen))
202 return TokError("unexpected token in memory operand");
203 Lexer.Lex(); // Eat the ')'.
205 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, Scale, ScaleReg);
210 /// ParseExpression - Parse an expression and return it.
211 /// FIXME: This should handle real expressions, we do something trivial for now.
212 bool AsmParser::ParseExpression(int64_t &Res) {
213 switch (Lexer.getKind()) {
215 return TokError("unknown token in expression");
216 case asmtok::Identifier:
217 // This is a label, this should be parsed as part of an expression, to
218 // handle things like LFOO+4
220 Lexer.Lex(); // Eat identifier.
223 Res = Lexer.getCurIntVal();
224 Lexer.Lex(); // Eat identifier.
233 /// ::= EndOfStatement
234 /// ::= Label* Directive ...Operands... EndOfStatement
235 /// ::= Label* Identifier OperandList* EndOfStatement
236 bool AsmParser::ParseStatement() {
237 switch (Lexer.getKind()) {
239 return TokError("unexpected token at start of statement");
240 case asmtok::EndOfStatement:
243 case asmtok::Identifier:
245 // TODO: Recurse on local labels etc.
248 // If we have an identifier, handle it as the key symbol.
249 SMLoc IDLoc = Lexer.getLoc();
250 std::string IDVal = Lexer.getCurStrVal();
252 // Consume the identifier, see what is after it.
253 if (Lexer.Lex() == asmtok::Colon) {
254 // identifier ':' -> Label.
256 return ParseStatement();
259 // Otherwise, we have a normal instruction or directive.
260 if (IDVal[0] == '.') {
261 Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
262 EatToEndOfStatement();
266 // If it's an instruction, parse an operand list.
267 std::vector<X86Operand> Operands;
269 // Read the first operand, if present. Note that we require a newline at the
270 // end of file, so we don't have to worry about Eof here.
271 if (Lexer.isNot(asmtok::EndOfStatement)) {
273 if (ParseX86Operand(Op))
275 Operands.push_back(Op);
278 while (Lexer.is(asmtok::Comma)) {
279 Lexer.Lex(); // Eat the comma.
281 // Parse and remember the operand.
283 if (ParseX86Operand(Op))
285 Operands.push_back(Op);
288 if (Lexer.isNot(asmtok::EndOfStatement))
289 return TokError("unexpected token in operand list");
291 // Eat the end of statement marker.
294 // Instruction is good, process it.
295 outs() << "Found instruction: " << IDVal << " with " << Operands.size()
298 // Skip to end of line for now.