Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 1 | //===- MC-X86Specific.cpp - X86-Specific code for MC ----------------------===// |
| 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 implements X86-specific parsing, encoding and decoding stuff for |
| 11 | // MC. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AsmParser.h" |
Daniel Dunbar | bc9c647 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCInst.h" |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | /// X86Operand - Instances of this class represent one X86 machine instruction. |
| 22 | struct AsmParser::X86Operand { |
| 23 | enum { |
| 24 | Register, |
| 25 | Immediate, |
| 26 | Memory |
| 27 | } Kind; |
| 28 | |
| 29 | union { |
| 30 | struct { |
| 31 | unsigned RegNo; |
| 32 | } Reg; |
| 33 | |
| 34 | struct { |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 35 | MCValue Val; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 36 | } Imm; |
| 37 | |
| 38 | struct { |
| 39 | unsigned SegReg; |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 40 | MCValue Disp; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 41 | unsigned BaseReg; |
Daniel Dunbar | 3aafe4d | 2009-07-02 00:51:52 +0000 | [diff] [blame] | 42 | unsigned IndexReg; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 43 | unsigned Scale; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 44 | } Mem; |
| 45 | }; |
| 46 | |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 47 | unsigned getReg() const { |
| 48 | assert(Kind == Register && "Invalid access!"); |
| 49 | return Reg.RegNo; |
| 50 | } |
| 51 | |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 52 | static X86Operand CreateReg(unsigned RegNo) { |
| 53 | X86Operand Res; |
| 54 | Res.Kind = Register; |
| 55 | Res.Reg.RegNo = RegNo; |
| 56 | return Res; |
| 57 | } |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 58 | static X86Operand CreateImm(MCValue Val) { |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 59 | X86Operand Res; |
| 60 | Res.Kind = Immediate; |
| 61 | Res.Imm.Val = Val; |
| 62 | return Res; |
| 63 | } |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 64 | static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg, |
Daniel Dunbar | 3aafe4d | 2009-07-02 00:51:52 +0000 | [diff] [blame] | 65 | unsigned IndexReg, unsigned Scale) { |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 66 | // If there is no index register, we should never have a scale, and we |
| 67 | // should always have a scale (in {1,2,4,8}) if we do. |
| 68 | assert(((Scale == 0 && !IndexReg) || |
| 69 | (IndexReg && (Scale == 1 || Scale == 2 || |
| 70 | Scale == 4 || Scale == 8))) && |
| 71 | "Invalid scale!"); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 72 | X86Operand Res; |
| 73 | Res.Kind = Memory; |
| 74 | Res.Mem.SegReg = SegReg; |
| 75 | Res.Mem.Disp = Disp; |
| 76 | Res.Mem.BaseReg = BaseReg; |
Daniel Dunbar | 3aafe4d | 2009-07-02 00:51:52 +0000 | [diff] [blame] | 77 | Res.Mem.IndexReg = IndexReg; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 78 | Res.Mem.Scale = Scale; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 79 | return Res; |
| 80 | } |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 83 | bool AsmParser::ParseX86Register(X86Operand &Op) { |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 84 | assert(getLexer().is(AsmToken::Register) && "Invalid token kind!"); |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 85 | |
| 86 | // FIXME: Decode register number. |
| 87 | Op = X86Operand::CreateReg(123); |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 88 | getLexer().Lex(); // Eat register token. |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 93 | bool AsmParser::ParseX86Operand(X86Operand &Op) { |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 94 | switch (getLexer().getKind()) { |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 95 | default: |
| 96 | return ParseX86MemOperand(Op); |
Daniel Dunbar | c479a54 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 97 | case AsmToken::Register: |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 98 | // FIXME: if a segment register, this could either be just the seg reg, or |
| 99 | // the start of a memory operand. |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 100 | return ParseX86Register(Op); |
Daniel Dunbar | c479a54 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 101 | case AsmToken::Dollar: { |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 102 | // $42 -> immediate. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 103 | getLexer().Lex(); |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 104 | MCValue Val; |
| 105 | if (ParseRelocatableExpression(Val)) |
| 106 | return true; |
| 107 | Op = X86Operand::CreateImm(Val); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 108 | return false; |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 109 | } |
Daniel Dunbar | c479a54 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 110 | case AsmToken::Star: { |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 111 | getLexer().Lex(); // Eat the star. |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 112 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 113 | if (getLexer().is(AsmToken::Register)) { |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 114 | if (ParseX86Register(Op)) |
| 115 | return true; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 116 | } else if (ParseX86MemOperand(Op)) |
| 117 | return true; |
| 118 | |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 119 | // FIXME: Note the '*' in the operand for use by the matcher. |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// ParseX86MemOperand: segment: disp(basereg, indexreg, scale) |
| 126 | bool AsmParser::ParseX86MemOperand(X86Operand &Op) { |
| 127 | // FIXME: If SegReg ':' (e.g. %gs:), eat and remember. |
| 128 | unsigned SegReg = 0; |
| 129 | |
| 130 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 131 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
| 132 | // only way to do this without lookahead is to eat the ( and see what is after |
| 133 | // it. |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 134 | MCValue Disp = MCValue::get(0, 0, 0); |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 135 | if (getLexer().isNot(AsmToken::LParen)) { |
Daniel Dunbar | 4967dbd | 2009-06-30 23:02:44 +0000 | [diff] [blame] | 136 | if (ParseRelocatableExpression(Disp)) return true; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 137 | |
| 138 | // After parsing the base expression we could either have a parenthesized |
| 139 | // memory address or not. If not, return now. If so, eat the (. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 140 | if (getLexer().isNot(AsmToken::LParen)) { |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 141 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // Eat the '('. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 146 | getLexer().Lex(); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 147 | } else { |
| 148 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 149 | // so we have to eat the ( to see beyond it. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 150 | getLexer().Lex(); // Eat the '('. |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 152 | if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) { |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 153 | // Nothing to do here, fall into the code below with the '(' part of the |
| 154 | // memory operand consumed. |
| 155 | } else { |
| 156 | // It must be an parenthesized expression, parse it now. |
Daniel Dunbar | 5ebef27 | 2009-07-02 02:09:07 +0000 | [diff] [blame] | 157 | if (ParseParenRelocatableExpression(Disp)) |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 158 | return true; |
| 159 | |
| 160 | // After parsing the base expression we could either have a parenthesized |
| 161 | // memory address or not. If not, return now. If so, eat the (. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 162 | if (getLexer().isNot(AsmToken::LParen)) { |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 163 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // Eat the '('. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 168 | getLexer().Lex(); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 173 | // the rest of the memory operand. |
Daniel Dunbar | 3aafe4d | 2009-07-02 00:51:52 +0000 | [diff] [blame] | 174 | unsigned BaseReg = 0, IndexReg = 0, Scale = 0; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 176 | if (getLexer().is(AsmToken::Register)) { |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 177 | if (ParseX86Register(Op)) |
| 178 | return true; |
| 179 | BaseReg = Op.getReg(); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 182 | if (getLexer().is(AsmToken::Comma)) { |
| 183 | getLexer().Lex(); // Eat the comma. |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 184 | |
| 185 | // Following the comma we should have either an index register, or a scale |
| 186 | // value. We don't support the later form, but we want to parse it |
| 187 | // correctly. |
| 188 | // |
| 189 | // Not that even though it would be completely consistent to support syntax |
| 190 | // like "1(%eax,,1)", the assembler doesn't. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 191 | if (getLexer().is(AsmToken::Register)) { |
Daniel Dunbar | b336eac | 2009-07-02 01:58:24 +0000 | [diff] [blame] | 192 | if (ParseX86Register(Op)) |
| 193 | return true; |
| 194 | IndexReg = Op.getReg(); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 195 | Scale = 1; // If not specified, the scale defaults to 1. |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 197 | if (getLexer().isNot(AsmToken::RParen)) { |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 198 | // Parse the scale amount: |
| 199 | // ::= ',' [scale-expression] |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 200 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 201 | return true; |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 202 | getLexer().Lex(); // Eat the comma. |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 204 | if (getLexer().isNot(AsmToken::RParen)) { |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 205 | int64_t ScaleVal; |
| 206 | if (ParseAbsoluteExpression(ScaleVal)) |
| 207 | return true; |
| 208 | |
| 209 | // Validate the scale amount. |
| 210 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8) |
| 211 | return TokError("scale factor in address must be 1, 2, 4 or 8"); |
| 212 | Scale = (unsigned)ScaleVal; |
| 213 | } |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 214 | } |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 215 | } else if (getLexer().isNot(AsmToken::RParen)) { |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 216 | // Otherwise we have the unsupported form of a scale amount without an |
| 217 | // index. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 218 | SMLoc Loc = getLexer().getTok().getLoc(); |
Daniel Dunbar | b901d87 | 2009-07-02 02:26:39 +0000 | [diff] [blame] | 219 | |
| 220 | int64_t Value; |
| 221 | if (ParseAbsoluteExpression(Value)) |
| 222 | return true; |
| 223 | |
| 224 | return Error(Loc, "cannot have scale factor without index register"); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 229 | if (getLexer().isNot(AsmToken::RParen)) |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 230 | return TokError("unexpected token in memory operand"); |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 231 | getLexer().Lex(); // Eat the ')'. |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 232 | |
Daniel Dunbar | 3aafe4d | 2009-07-02 00:51:52 +0000 | [diff] [blame] | 233 | Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 237 | /// MatchX86Inst - Convert a parsed instruction name and operand list into a |
| 238 | /// concrete instruction. |
Daniel Dunbar | 9a7b61d | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 239 | static bool MatchX86Inst(const StringRef &Name, |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 240 | llvm::SmallVector<AsmParser::X86Operand, 3> &Operands, |
| 241 | MCInst &Inst) { |
| 242 | return false; |
| 243 | } |
| 244 | |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 245 | /// ParseX86InstOperands - Parse the operands of an X86 instruction and return |
| 246 | /// them as the operands of an MCInst. |
Daniel Dunbar | 9a7b61d | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 247 | bool AsmParser::ParseX86InstOperands(const StringRef &InstName, MCInst &Inst) { |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 248 | llvm::SmallVector<X86Operand, 3> Operands; |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 249 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 250 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 251 | // Read the first operand. |
| 252 | Operands.push_back(X86Operand()); |
| 253 | if (ParseX86Operand(Operands.back())) |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 254 | return true; |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | 4697897 | 2009-07-28 18:17:26 +0000 | [diff] [blame] | 256 | while (getLexer().is(AsmToken::Comma)) { |
| 257 | getLexer().Lex(); // Eat the comma. |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 258 | |
| 259 | // Parse and remember the operand. |
| 260 | Operands.push_back(X86Operand()); |
| 261 | if (ParseX86Operand(Operands.back())) |
| 262 | return true; |
| 263 | } |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 264 | } |
Daniel Dunbar | d1d1e83 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 265 | |
| 266 | return MatchX86Inst(InstName, Operands, Inst); |
Chris Lattner | e6c561b | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 267 | } |