| Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// | 
|  | 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 class implements the parser for assembly files. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
|  | 14 | #include "AsmParser.h" | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 15 |  | 
| Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" | 
| Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" | 
| Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCExpr.h" | 
| Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInst.h" | 
| Chris Lattner | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionMachO.h" | 
| Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCStreamer.h" | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSymbol.h" | 
| Daniel Dunbar | c18274b | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCValue.h" | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 25 | #include "llvm/Support/SourceMgr.h" | 
|  | 26 | #include "llvm/Support/raw_ostream.h" | 
| Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetAsmParser.h" | 
| Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 28 | using namespace llvm; | 
|  | 29 |  | 
| Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 30 | // Mach-O section uniquing. | 
|  | 31 | // | 
|  | 32 | // FIXME: Figure out where this should live, it should be shared by | 
|  | 33 | // TargetLoweringObjectFile. | 
|  | 34 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; | 
|  | 35 |  | 
|  | 36 | AsmParser::~AsmParser() { | 
|  | 37 | // If we have the MachO uniquing map, free it. | 
|  | 38 | delete (MachOUniqueMapTy*)SectionUniquingMap; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | const MCSection *AsmParser::getMachOSection(const StringRef &Segment, | 
|  | 42 | const StringRef &Section, | 
|  | 43 | unsigned TypeAndAttributes, | 
|  | 44 | unsigned Reserved2, | 
|  | 45 | SectionKind Kind) const { | 
|  | 46 | // We unique sections by their segment/section pair.  The returned section | 
|  | 47 | // may not have the same flags as the requested section, if so this should be | 
|  | 48 | // diagnosed by the client as an error. | 
|  | 49 |  | 
|  | 50 | // Create the map if it doesn't already exist. | 
|  | 51 | if (SectionUniquingMap == 0) | 
|  | 52 | SectionUniquingMap = new MachOUniqueMapTy(); | 
|  | 53 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap; | 
|  | 54 |  | 
|  | 55 | // Form the name to look up. | 
|  | 56 | SmallString<64> Name; | 
|  | 57 | Name += Segment; | 
|  | 58 | Name.push_back(','); | 
|  | 59 | Name += Section; | 
|  | 60 |  | 
|  | 61 | // Do the lookup, if we have a hit, return it. | 
|  | 62 | const MCSectionMachO *&Entry = Map[Name.str()]; | 
|  | 63 |  | 
|  | 64 | // FIXME: This should validate the type and attributes. | 
|  | 65 | if (Entry) return Entry; | 
|  | 66 |  | 
|  | 67 | // Otherwise, return a new section. | 
|  | 68 | return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, | 
|  | 69 | Reserved2, Kind, Ctx); | 
|  | 70 | } | 
|  | 71 |  | 
| Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 72 | void AsmParser::Warning(SMLoc L, const Twine &Msg) { | 
|  | 73 | Lexer.PrintMessage(L, Msg.str(), "warning"); | 
| Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 76 | bool AsmParser::Error(SMLoc L, const Twine &Msg) { | 
|  | 77 | Lexer.PrintMessage(L, Msg.str(), "error"); | 
| Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 78 | return true; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | bool AsmParser::TokError(const char *Msg) { | 
| Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 82 | Lexer.PrintMessage(Lexer.getLoc(), Msg, "error"); | 
| Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 83 | return true; | 
|  | 84 | } | 
|  | 85 |  | 
| Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 86 | bool AsmParser::Run() { | 
| Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 87 | // Create the initial section. | 
|  | 88 | // | 
|  | 89 | // FIXME: Support -n. | 
|  | 90 | // FIXME: Target hook & command line option for initial section. | 
|  | 91 | Out.SwitchSection(getMachOSection("__TEXT", "__text", | 
|  | 92 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, | 
|  | 93 | 0, SectionKind())); | 
|  | 94 |  | 
|  | 95 |  | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 96 | // Prime the lexer. | 
|  | 97 | Lexer.Lex(); | 
|  | 98 |  | 
| Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 99 | bool HadError = false; | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 100 |  | 
| Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 101 | AsmCond StartingCondState = TheCondState; | 
|  | 102 |  | 
| Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 103 | // While we have input, parse each statement. | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 104 | while (Lexer.isNot(AsmToken::Eof)) { | 
| Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 105 | // Handle conditional assembly here before calling ParseStatement() | 
|  | 106 | if (Lexer.getKind() == AsmToken::Identifier) { | 
|  | 107 | // If we have an identifier, handle it as the key symbol. | 
|  | 108 | AsmToken ID = Lexer.getTok(); | 
|  | 109 | SMLoc IDLoc = ID.getLoc(); | 
|  | 110 | StringRef IDVal = ID.getString(); | 
|  | 111 |  | 
|  | 112 | if (IDVal == ".if" || | 
|  | 113 | IDVal == ".elseif" || | 
|  | 114 | IDVal == ".else" || | 
|  | 115 | IDVal == ".endif") { | 
|  | 116 | if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc)) | 
|  | 117 | continue; | 
|  | 118 | HadError = true; | 
|  | 119 | EatToEndOfStatement(); | 
|  | 120 | continue; | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 | if (TheCondState.Ignore) { | 
|  | 124 | EatToEndOfStatement(); | 
|  | 125 | continue; | 
|  | 126 | } | 
|  | 127 |  | 
| Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 128 | if (!ParseStatement()) continue; | 
|  | 129 |  | 
| Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 130 | // We had an error, remember it and recover by skipping to the next line. | 
| Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 131 | HadError = true; | 
|  | 132 | EatToEndOfStatement(); | 
|  | 133 | } | 
| Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 134 |  | 
|  | 135 | if (TheCondState.TheCond != StartingCondState.TheCond || | 
|  | 136 | TheCondState.Ignore != StartingCondState.Ignore) | 
|  | 137 | return TokError("unmatched .ifs or .elses"); | 
| Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 138 |  | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 139 | if (!HadError) | 
|  | 140 | Out.Finish(); | 
|  | 141 |  | 
| Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 142 | return HadError; | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 145 | /// ParseConditionalAssemblyDirectives - parse the conditional assembly | 
|  | 146 | /// directives | 
|  | 147 | bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive, | 
|  | 148 | SMLoc DirectiveLoc) { | 
|  | 149 | if (Directive == ".if") | 
|  | 150 | return ParseDirectiveIf(DirectiveLoc); | 
|  | 151 | if (Directive == ".elseif") | 
|  | 152 | return ParseDirectiveElseIf(DirectiveLoc); | 
|  | 153 | if (Directive == ".else") | 
|  | 154 | return ParseDirectiveElse(DirectiveLoc); | 
|  | 155 | if (Directive == ".endif") | 
|  | 156 | return ParseDirectiveEndIf(DirectiveLoc); | 
|  | 157 | return true; | 
|  | 158 | } | 
|  | 159 |  | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 160 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. | 
|  | 161 | void AsmParser::EatToEndOfStatement() { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 162 | while (Lexer.isNot(AsmToken::EndOfStatement) && | 
|  | 163 | Lexer.isNot(AsmToken::Eof)) | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 164 | Lexer.Lex(); | 
|  | 165 |  | 
|  | 166 | // Eat EOL. | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 167 | if (Lexer.is(AsmToken::EndOfStatement)) | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 168 | Lexer.Lex(); | 
|  | 169 | } | 
|  | 170 |  | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 171 |  | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 172 | /// ParseParenExpr - Parse a paren expression and return it. | 
|  | 173 | /// NOTE: This assumes the leading '(' has already been consumed. | 
|  | 174 | /// | 
|  | 175 | /// parenexpr ::= expr) | 
|  | 176 | /// | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 177 | bool AsmParser::ParseParenExpr(const MCExpr *&Res) { | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 178 | if (ParseExpression(Res)) return true; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 179 | if (Lexer.isNot(AsmToken::RParen)) | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 180 | return TokError("expected ')' in parentheses expression"); | 
|  | 181 | Lexer.Lex(); | 
|  | 182 | return false; | 
|  | 183 | } | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 184 |  | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 185 | MCSymbol *AsmParser::CreateSymbol(StringRef Name) { | 
|  | 186 | if (MCSymbol *S = Ctx.LookupSymbol(Name)) | 
|  | 187 | return S; | 
|  | 188 |  | 
|  | 189 | // If the label starts with L it is an assembler temporary label. | 
|  | 190 | if (Name.startswith("L")) | 
|  | 191 | return Ctx.CreateTemporarySymbol(Name); | 
|  | 192 |  | 
|  | 193 | return Ctx.CreateSymbol(Name); | 
|  | 194 | } | 
|  | 195 |  | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 196 | /// ParsePrimaryExpr - Parse a primary expression and return it. | 
|  | 197 | ///  primaryexpr ::= (parenexpr | 
|  | 198 | ///  primaryexpr ::= symbol | 
|  | 199 | ///  primaryexpr ::= number | 
|  | 200 | ///  primaryexpr ::= ~,+,- primaryexpr | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 201 | bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) { | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 202 | switch (Lexer.getKind()) { | 
|  | 203 | default: | 
|  | 204 | return TokError("unknown token in expression"); | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 205 | case AsmToken::Exclaim: | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 206 | Lexer.Lex(); // Eat the operator. | 
|  | 207 | if (ParsePrimaryExpr(Res)) | 
|  | 208 | return true; | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 209 | Res = MCUnaryExpr::CreateLNot(Res, getContext()); | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 210 | return false; | 
| Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 211 | case AsmToken::String: | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 212 | case AsmToken::Identifier: { | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 213 | // This is a label, this should be parsed as part of an expression, to | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 214 | // handle things like LFOO+4. | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 215 | MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier()); | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 216 |  | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 217 | Res = MCSymbolRefExpr::Create(Sym, getContext()); | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 218 | Lexer.Lex(); // Eat identifier. | 
|  | 219 | return false; | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 220 | } | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 221 | case AsmToken::Integer: | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 222 | Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext()); | 
| Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 223 | Lexer.Lex(); // Eat token. | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 224 | return false; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 225 | case AsmToken::LParen: | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 226 | Lexer.Lex(); // Eat the '('. | 
|  | 227 | return ParseParenExpr(Res); | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 228 | case AsmToken::Minus: | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 229 | Lexer.Lex(); // Eat the operator. | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 230 | if (ParsePrimaryExpr(Res)) | 
|  | 231 | return true; | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 232 | Res = MCUnaryExpr::CreateMinus(Res, getContext()); | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 233 | return false; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 234 | case AsmToken::Plus: | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 235 | Lexer.Lex(); // Eat the operator. | 
|  | 236 | if (ParsePrimaryExpr(Res)) | 
|  | 237 | return true; | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 238 | Res = MCUnaryExpr::CreatePlus(Res, getContext()); | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 239 | return false; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 240 | case AsmToken::Tilde: | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 241 | Lexer.Lex(); // Eat the operator. | 
|  | 242 | if (ParsePrimaryExpr(Res)) | 
|  | 243 | return true; | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 244 | Res = MCUnaryExpr::CreateNot(Res, getContext()); | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 245 | return false; | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 246 | } | 
|  | 247 | } | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 248 |  | 
|  | 249 | /// ParseExpression - Parse an expression and return it. | 
|  | 250 | /// | 
|  | 251 | ///  expr ::= expr +,- expr          -> lowest. | 
|  | 252 | ///  expr ::= expr |,^,&,! expr      -> middle. | 
|  | 253 | ///  expr ::= expr *,/,%,<<,>> expr  -> highest. | 
|  | 254 | ///  expr ::= primaryexpr | 
|  | 255 | /// | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 256 | bool AsmParser::ParseExpression(const MCExpr *&Res) { | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 257 | Res = 0; | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 258 | return ParsePrimaryExpr(Res) || | 
|  | 259 | ParseBinOpRHS(1, Res); | 
| Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 260 | } | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 261 |  | 
| Daniel Dunbar | c18274b | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 262 | bool AsmParser::ParseParenExpression(const MCExpr *&Res) { | 
|  | 263 | if (ParseParenExpr(Res)) | 
|  | 264 | return true; | 
|  | 265 |  | 
|  | 266 | return false; | 
|  | 267 | } | 
|  | 268 |  | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 269 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 270 | const MCExpr *Expr; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 271 |  | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 272 | SMLoc StartLoc = Lexer.getLoc(); | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 273 | if (ParseExpression(Expr)) | 
|  | 274 | return true; | 
|  | 275 |  | 
|  | 276 | if (!Expr->EvaluateAsAbsolute(Ctx, Res)) | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 277 | return Error(StartLoc, "expected absolute expression"); | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 278 |  | 
|  | 279 | return false; | 
|  | 280 | } | 
|  | 281 |  | 
| Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 282 | bool AsmParser::ParseRelocatableExpression(MCValue &Res) { | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 283 | const MCExpr *Expr; | 
| Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 284 |  | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 285 | SMLoc StartLoc = Lexer.getLoc(); | 
| Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 286 | if (ParseExpression(Expr)) | 
|  | 287 | return true; | 
|  | 288 |  | 
|  | 289 | if (!Expr->EvaluateAsRelocatable(Ctx, Res)) | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 290 | return Error(StartLoc, "expected relocatable expression"); | 
| Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 291 |  | 
|  | 292 | return false; | 
|  | 293 | } | 
|  | 294 |  | 
| Daniel Dunbar | 2c3f00c | 2009-07-02 02:09:07 +0000 | [diff] [blame] | 295 | bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) { | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 296 | const MCExpr *Expr; | 
| Daniel Dunbar | 2c3f00c | 2009-07-02 02:09:07 +0000 | [diff] [blame] | 297 |  | 
|  | 298 | SMLoc StartLoc = Lexer.getLoc(); | 
|  | 299 | if (ParseParenExpr(Expr)) | 
|  | 300 | return true; | 
|  | 301 |  | 
|  | 302 | if (!Expr->EvaluateAsRelocatable(Ctx, Res)) | 
|  | 303 | return Error(StartLoc, "expected relocatable expression"); | 
|  | 304 |  | 
|  | 305 | return false; | 
|  | 306 | } | 
|  | 307 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 308 | static unsigned getBinOpPrecedence(AsmToken::TokenKind K, | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 309 | MCBinaryExpr::Opcode &Kind) { | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 310 | switch (K) { | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 311 | default: | 
|  | 312 | return 0;    // not a binop. | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 313 |  | 
|  | 314 | // Lowest Precedence: &&, || | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 315 | case AsmToken::AmpAmp: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 316 | Kind = MCBinaryExpr::LAnd; | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 317 | return 1; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 318 | case AsmToken::PipePipe: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 319 | Kind = MCBinaryExpr::LOr; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 320 | return 1; | 
|  | 321 |  | 
|  | 322 | // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 323 | case AsmToken::Plus: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 324 | Kind = MCBinaryExpr::Add; | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 325 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 326 | case AsmToken::Minus: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 327 | Kind = MCBinaryExpr::Sub; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 328 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 329 | case AsmToken::EqualEqual: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 330 | Kind = MCBinaryExpr::EQ; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 331 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 332 | case AsmToken::ExclaimEqual: | 
|  | 333 | case AsmToken::LessGreater: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 334 | Kind = MCBinaryExpr::NE; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 335 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 336 | case AsmToken::Less: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 337 | Kind = MCBinaryExpr::LT; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 338 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 339 | case AsmToken::LessEqual: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 340 | Kind = MCBinaryExpr::LTE; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 341 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 342 | case AsmToken::Greater: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 343 | Kind = MCBinaryExpr::GT; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 344 | return 2; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 345 | case AsmToken::GreaterEqual: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 346 | Kind = MCBinaryExpr::GTE; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 347 | return 2; | 
|  | 348 |  | 
|  | 349 | // Intermediate Precedence: |, &, ^ | 
|  | 350 | // | 
|  | 351 | // FIXME: gas seems to support '!' as an infix operator? | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 352 | case AsmToken::Pipe: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 353 | Kind = MCBinaryExpr::Or; | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 354 | return 3; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 355 | case AsmToken::Caret: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 356 | Kind = MCBinaryExpr::Xor; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 357 | return 3; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 358 | case AsmToken::Amp: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 359 | Kind = MCBinaryExpr::And; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 360 | return 3; | 
|  | 361 |  | 
|  | 362 | // Highest Precedence: *, /, %, <<, >> | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 363 | case AsmToken::Star: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 364 | Kind = MCBinaryExpr::Mul; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 365 | return 4; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 366 | case AsmToken::Slash: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 367 | Kind = MCBinaryExpr::Div; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 368 | return 4; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 369 | case AsmToken::Percent: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 370 | Kind = MCBinaryExpr::Mod; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 371 | return 4; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 372 | case AsmToken::LessLess: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 373 | Kind = MCBinaryExpr::Shl; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 374 | return 4; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 375 | case AsmToken::GreaterGreater: | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 376 | Kind = MCBinaryExpr::Shr; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 377 | return 4; | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 378 | } | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 |  | 
|  | 382 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. | 
|  | 383 | /// Res contains the LHS of the expression on input. | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 384 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) { | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 385 | while (1) { | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 386 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 387 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 388 |  | 
|  | 389 | // If the next token is lower precedence than we are allowed to eat, return | 
|  | 390 | // successfully with what we ate already. | 
|  | 391 | if (TokPrec < Precedence) | 
|  | 392 | return false; | 
|  | 393 |  | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 394 | Lexer.Lex(); | 
|  | 395 |  | 
|  | 396 | // Eat the next primary expression. | 
| Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 397 | const MCExpr *RHS; | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 398 | if (ParsePrimaryExpr(RHS)) return true; | 
|  | 399 |  | 
|  | 400 | // If BinOp binds less tightly with RHS than the operator after RHS, let | 
|  | 401 | // the pending operator take RHS as its LHS. | 
| Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 402 | MCBinaryExpr::Opcode Dummy; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 403 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 404 | if (TokPrec < NextTokPrec) { | 
|  | 405 | if (ParseBinOpRHS(Precedence+1, RHS)) return true; | 
|  | 406 | } | 
|  | 407 |  | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 408 | // Merge LHS and RHS according to operator. | 
| Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 409 | Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); | 
| Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 410 | } | 
|  | 411 | } | 
|  | 412 |  | 
| Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 413 |  | 
|  | 414 |  | 
|  | 415 |  | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 416 | /// ParseStatement: | 
|  | 417 | ///   ::= EndOfStatement | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 418 | ///   ::= Label* Directive ...Operands... EndOfStatement | 
|  | 419 | ///   ::= Label* Identifier OperandList* EndOfStatement | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 420 | bool AsmParser::ParseStatement() { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 421 | if (Lexer.is(AsmToken::EndOfStatement)) { | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 422 | Lexer.Lex(); | 
|  | 423 | return false; | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 424 | } | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 425 |  | 
|  | 426 | // Statements always start with an identifier. | 
| Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 427 | AsmToken ID = Lexer.getTok(); | 
|  | 428 | SMLoc IDLoc = ID.getLoc(); | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 429 | StringRef IDVal; | 
|  | 430 | if (ParseIdentifier(IDVal)) | 
|  | 431 | return TokError("unexpected token at start of statement"); | 
|  | 432 |  | 
|  | 433 | // FIXME: Recurse on local labels? | 
|  | 434 |  | 
|  | 435 | // See what kind of statement we have. | 
|  | 436 | switch (Lexer.getKind()) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 437 | case AsmToken::Colon: { | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 438 | // identifier ':'   -> Label. | 
|  | 439 | Lexer.Lex(); | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 440 |  | 
|  | 441 | // Diagnose attempt to use a variable as a label. | 
|  | 442 | // | 
|  | 443 | // FIXME: Diagnostics. Note the location of the definition as a label. | 
|  | 444 | // FIXME: This doesn't diagnose assignment to a symbol which has been | 
|  | 445 | // implicitly marked as external. | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 446 | MCSymbol *Sym = CreateSymbol(IDVal); | 
| Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 447 | if (!Sym->isUndefined()) | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 448 | return Error(IDLoc, "invalid symbol redefinition"); | 
| Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 449 |  | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 450 | // Emit the label. | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 451 | Out.EmitLabel(Sym); | 
|  | 452 |  | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 453 | return ParseStatement(); | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 454 | } | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 455 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 456 | case AsmToken::Equal: | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 457 | // identifier '=' ... -> assignment statement | 
|  | 458 | Lexer.Lex(); | 
|  | 459 |  | 
|  | 460 | return ParseAssignment(IDVal, false); | 
|  | 461 |  | 
|  | 462 | default: // Normal instruction or directive. | 
|  | 463 | break; | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
|  | 466 | // Otherwise, we have a normal instruction or directive. | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 467 | if (IDVal[0] == '.') { | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 468 | // FIXME: This should be driven based on a hash lookup and callback. | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 469 | if (IDVal == ".section") | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 470 | return ParseDirectiveDarwinSection(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 471 | if (IDVal == ".text") | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 472 | // FIXME: This changes behavior based on the -static flag to the | 
|  | 473 | // assembler. | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 474 | return ParseDirectiveSectionSwitch("__TEXT", "__text", | 
|  | 475 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 476 | if (IDVal == ".const") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 477 | return ParseDirectiveSectionSwitch("__TEXT", "__const"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 478 | if (IDVal == ".static_const") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 479 | return ParseDirectiveSectionSwitch("__TEXT", "__static_const"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 480 | if (IDVal == ".cstring") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 481 | return ParseDirectiveSectionSwitch("__TEXT","__cstring", | 
|  | 482 | MCSectionMachO::S_CSTRING_LITERALS); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 483 | if (IDVal == ".literal4") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 484 | return ParseDirectiveSectionSwitch("__TEXT", "__literal4", | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 485 | MCSectionMachO::S_4BYTE_LITERALS, | 
|  | 486 | 4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 487 | if (IDVal == ".literal8") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 488 | return ParseDirectiveSectionSwitch("__TEXT", "__literal8", | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 489 | MCSectionMachO::S_8BYTE_LITERALS, | 
|  | 490 | 8); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 491 | if (IDVal == ".literal16") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 492 | return ParseDirectiveSectionSwitch("__TEXT","__literal16", | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 493 | MCSectionMachO::S_16BYTE_LITERALS, | 
|  | 494 | 16); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 495 | if (IDVal == ".constructor") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 496 | return ParseDirectiveSectionSwitch("__TEXT","__constructor"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 497 | if (IDVal == ".destructor") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 498 | return ParseDirectiveSectionSwitch("__TEXT","__destructor"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 499 | if (IDVal == ".fvmlib_init0") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 500 | return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 501 | if (IDVal == ".fvmlib_init1") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 502 | return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1"); | 
|  | 503 |  | 
|  | 504 | // FIXME: The assembler manual claims that this has the self modify code | 
|  | 505 | // flag, at least on x86-32, but that does not appear to be correct. | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 506 | if (IDVal == ".symbol_stub") | 
|  | 507 | return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub", | 
|  | 508 | MCSectionMachO::S_SYMBOL_STUBS | | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 509 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, | 
|  | 510 | // FIXME: Different on PPC and ARM. | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 511 | 0, 16); | 
|  | 512 | // FIXME: PowerPC only? | 
|  | 513 | if (IDVal == ".picsymbol_stub") | 
|  | 514 | return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub", | 
|  | 515 | MCSectionMachO::S_SYMBOL_STUBS | | 
|  | 516 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, | 
|  | 517 | 0, 26); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 518 | if (IDVal == ".data") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 519 | return ParseDirectiveSectionSwitch("__DATA", "__data"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 520 | if (IDVal == ".static_data") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 521 | return ParseDirectiveSectionSwitch("__DATA", "__static_data"); | 
|  | 522 |  | 
|  | 523 | // FIXME: The section names of these two are misspelled in the assembler | 
|  | 524 | // manual. | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 525 | if (IDVal == ".non_lazy_symbol_pointer") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 526 | return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr", | 
|  | 527 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, | 
|  | 528 | 4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 529 | if (IDVal == ".lazy_symbol_pointer") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 530 | return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr", | 
|  | 531 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, | 
|  | 532 | 4); | 
|  | 533 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 534 | if (IDVal == ".dyld") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 535 | return ParseDirectiveSectionSwitch("__DATA", "__dyld"); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 536 | if (IDVal == ".mod_init_func") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 537 | return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func", | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 538 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, | 
|  | 539 | 4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 540 | if (IDVal == ".mod_term_func") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 541 | return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func", | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 542 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, | 
|  | 543 | 4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 544 | if (IDVal == ".const_data") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 545 | return ParseDirectiveSectionSwitch("__DATA", "__const"); | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 546 |  | 
|  | 547 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 548 | if (IDVal == ".objc_class") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 549 | return ParseDirectiveSectionSwitch("__OBJC", "__class", | 
|  | 550 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 551 | if (IDVal == ".objc_meta_class") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 552 | return ParseDirectiveSectionSwitch("__OBJC", "__meta_class", | 
|  | 553 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 554 | if (IDVal == ".objc_cat_cls_meth") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 555 | return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth", | 
|  | 556 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 557 | if (IDVal == ".objc_cat_inst_meth") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 558 | return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth", | 
|  | 559 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 560 | if (IDVal == ".objc_protocol") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 561 | return ParseDirectiveSectionSwitch("__OBJC", "__protocol", | 
|  | 562 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 563 | if (IDVal == ".objc_string_object") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 564 | return ParseDirectiveSectionSwitch("__OBJC", "__string_object", | 
|  | 565 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 566 | if (IDVal == ".objc_cls_meth") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 567 | return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth", | 
|  | 568 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 569 | if (IDVal == ".objc_inst_meth") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 570 | return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth", | 
|  | 571 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 572 | if (IDVal == ".objc_cls_refs") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 573 | return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs", | 
|  | 574 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | | 
|  | 575 | MCSectionMachO::S_LITERAL_POINTERS, | 
|  | 576 | 4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 577 | if (IDVal == ".objc_message_refs") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 578 | return ParseDirectiveSectionSwitch("__OBJC", "__message_refs", | 
|  | 579 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | | 
|  | 580 | MCSectionMachO::S_LITERAL_POINTERS, | 
|  | 581 | 4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 582 | if (IDVal == ".objc_symbols") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 583 | return ParseDirectiveSectionSwitch("__OBJC", "__symbols", | 
|  | 584 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 585 | if (IDVal == ".objc_category") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 586 | return ParseDirectiveSectionSwitch("__OBJC", "__category", | 
|  | 587 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 588 | if (IDVal == ".objc_class_vars") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 589 | return ParseDirectiveSectionSwitch("__OBJC", "__class_vars", | 
|  | 590 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 591 | if (IDVal == ".objc_instance_vars") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 592 | return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars", | 
|  | 593 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 594 | if (IDVal == ".objc_module_info") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 595 | return ParseDirectiveSectionSwitch("__OBJC", "__module_info", | 
|  | 596 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 597 | if (IDVal == ".objc_class_names") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 598 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", | 
|  | 599 | MCSectionMachO::S_CSTRING_LITERALS); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 600 | if (IDVal == ".objc_meth_var_types") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 601 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", | 
|  | 602 | MCSectionMachO::S_CSTRING_LITERALS); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 603 | if (IDVal == ".objc_meth_var_names") | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 604 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", | 
|  | 605 | MCSectionMachO::S_CSTRING_LITERALS); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 606 | if (IDVal == ".objc_selector_strs") | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 607 | return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs", | 
|  | 608 | MCSectionMachO::S_CSTRING_LITERALS); | 
| Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 609 |  | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 610 | // Assembler features | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 611 | if (IDVal == ".set") | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 612 | return ParseDirectiveSet(); | 
|  | 613 |  | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 614 | // Data directives | 
|  | 615 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 616 | if (IDVal == ".ascii") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 617 | return ParseDirectiveAscii(false); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 618 | if (IDVal == ".asciz") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 619 | return ParseDirectiveAscii(true); | 
|  | 620 |  | 
|  | 621 | // FIXME: Target hooks for size? Also for "word", "hword". | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 622 | if (IDVal == ".byte") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 623 | return ParseDirectiveValue(1); | 
| Daniel Dunbar | 1e840b2 | 2009-08-11 04:44:00 +0000 | [diff] [blame] | 624 | if (IDVal == ".short" || IDVal == ".word") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 625 | return ParseDirectiveValue(2); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 626 | if (IDVal == ".long") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 627 | return ParseDirectiveValue(4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 628 | if (IDVal == ".quad") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 629 | return ParseDirectiveValue(8); | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 630 |  | 
|  | 631 | // FIXME: Target hooks for IsPow2. | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 632 | if (IDVal == ".align") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 633 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 634 | if (IDVal == ".align32") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 635 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 636 | if (IDVal == ".balign") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 637 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 638 | if (IDVal == ".balignw") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 639 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 640 | if (IDVal == ".balignl") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 641 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 642 | if (IDVal == ".p2align") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 643 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 644 | if (IDVal == ".p2alignw") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 645 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 646 | if (IDVal == ".p2alignl") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 647 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); | 
|  | 648 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 649 | if (IDVal == ".org") | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 650 | return ParseDirectiveOrg(); | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 651 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 652 | if (IDVal == ".fill") | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 653 | return ParseDirectiveFill(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 654 | if (IDVal == ".space") | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 655 | return ParseDirectiveSpace(); | 
|  | 656 |  | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 657 | // Symbol attribute directives | 
| Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 658 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 659 | if (IDVal == ".globl" || IDVal == ".global") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 660 | return ParseDirectiveSymbolAttribute(MCStreamer::Global); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 661 | if (IDVal == ".hidden") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 662 | return ParseDirectiveSymbolAttribute(MCStreamer::Hidden); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 663 | if (IDVal == ".indirect_symbol") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 664 | return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 665 | if (IDVal == ".internal") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 666 | return ParseDirectiveSymbolAttribute(MCStreamer::Internal); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 667 | if (IDVal == ".lazy_reference") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 668 | return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 669 | if (IDVal == ".no_dead_strip") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 670 | return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 671 | if (IDVal == ".private_extern") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 672 | return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 673 | if (IDVal == ".protected") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 674 | return ParseDirectiveSymbolAttribute(MCStreamer::Protected); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 675 | if (IDVal == ".reference") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 676 | return ParseDirectiveSymbolAttribute(MCStreamer::Reference); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 677 | if (IDVal == ".weak") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 678 | return ParseDirectiveSymbolAttribute(MCStreamer::Weak); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 679 | if (IDVal == ".weak_definition") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 680 | return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 681 | if (IDVal == ".weak_reference") | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 682 | return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference); | 
|  | 683 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 684 | if (IDVal == ".comm") | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 685 | return ParseDirectiveComm(/*IsLocal=*/false); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 686 | if (IDVal == ".lcomm") | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 687 | return ParseDirectiveComm(/*IsLocal=*/true); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 688 | if (IDVal == ".zerofill") | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 689 | return ParseDirectiveDarwinZerofill(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 690 | if (IDVal == ".desc") | 
| Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 691 | return ParseDirectiveDarwinSymbolDesc(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 692 | if (IDVal == ".lsym") | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 693 | return ParseDirectiveDarwinLsym(); | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 694 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 695 | if (IDVal == ".subsections_via_symbols") | 
| Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 696 | return ParseDirectiveDarwinSubsectionsViaSymbols(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 697 | if (IDVal == ".abort") | 
| Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 698 | return ParseDirectiveAbort(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 699 | if (IDVal == ".include") | 
| Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 700 | return ParseDirectiveInclude(); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 701 | if (IDVal == ".dump") | 
| Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 702 | return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true); | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 703 | if (IDVal == ".load") | 
| Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 704 | return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false); | 
| Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 705 |  | 
| Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 706 | // Debugging directives | 
|  | 707 |  | 
|  | 708 | if (IDVal == ".file") | 
|  | 709 | return ParseDirectiveFile(IDLoc); | 
|  | 710 | if (IDVal == ".line") | 
|  | 711 | return ParseDirectiveLine(IDLoc); | 
|  | 712 | if (IDVal == ".loc") | 
|  | 713 | return ParseDirectiveLoc(IDLoc); | 
|  | 714 |  | 
| Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 715 | Warning(IDLoc, "ignoring directive for now"); | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 716 | EatToEndOfStatement(); | 
|  | 717 | return false; | 
|  | 718 | } | 
| Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 719 |  | 
| Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 720 | MCInst Inst; | 
| Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 721 | if (getTargetParser().ParseInstruction(IDVal, Inst)) | 
| Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 722 | return true; | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 723 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 724 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 725 | return TokError("unexpected token in argument list"); | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 726 |  | 
|  | 727 | // Eat the end of statement marker. | 
|  | 728 | Lexer.Lex(); | 
|  | 729 |  | 
|  | 730 | // Instruction is good, process it. | 
| Daniel Dunbar | 0eebb05 | 2009-07-01 06:35:48 +0000 | [diff] [blame] | 731 | Out.EmitInstruction(Inst); | 
| Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 732 |  | 
|  | 733 | // Skip to end of line for now. | 
| Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 734 | return false; | 
|  | 735 | } | 
| Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 736 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 737 | bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) { | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 738 | // FIXME: Use better location, we should use proper tokens. | 
|  | 739 | SMLoc EqualLoc = Lexer.getLoc(); | 
|  | 740 |  | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 741 | MCValue Value; | 
|  | 742 | if (ParseRelocatableExpression(Value)) | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 743 | return true; | 
|  | 744 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 745 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 746 | return TokError("unexpected token in assignment"); | 
|  | 747 |  | 
|  | 748 | // Eat the end of statement marker. | 
|  | 749 | Lexer.Lex(); | 
|  | 750 |  | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 751 | // Diagnose assignment to a label. | 
|  | 752 | // | 
|  | 753 | // FIXME: Diagnostics. Note the location of the definition as a label. | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 754 | // FIXME: Handle '.'. | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 755 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 756 | MCSymbol *Sym = CreateSymbol(Name); | 
| Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 757 | if (!Sym->isUndefined() && !Sym->isAbsolute()) | 
|  | 758 | return Error(EqualLoc, "symbol has already been defined"); | 
| Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 759 |  | 
|  | 760 | // Do the assignment. | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 761 | Out.EmitAssignment(Sym, Value, IsDotSet); | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 762 |  | 
|  | 763 | return false; | 
|  | 764 | } | 
|  | 765 |  | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 766 | /// ParseIdentifier: | 
|  | 767 | ///   ::= identifier | 
|  | 768 | ///   ::= string | 
|  | 769 | bool AsmParser::ParseIdentifier(StringRef &Res) { | 
|  | 770 | if (Lexer.isNot(AsmToken::Identifier) && | 
|  | 771 | Lexer.isNot(AsmToken::String)) | 
|  | 772 | return true; | 
|  | 773 |  | 
|  | 774 | Res = Lexer.getTok().getIdentifier(); | 
|  | 775 |  | 
|  | 776 | Lexer.Lex(); // Consume the identifier token. | 
|  | 777 |  | 
|  | 778 | return false; | 
|  | 779 | } | 
|  | 780 |  | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 781 | /// ParseDirectiveSet: | 
|  | 782 | ///   ::= .set identifier ',' expression | 
|  | 783 | bool AsmParser::ParseDirectiveSet() { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 784 | StringRef Name; | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 785 |  | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 786 | if (ParseIdentifier(Name)) | 
|  | 787 | return TokError("expected identifier after '.set' directive"); | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 788 |  | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 789 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 790 | return TokError("unexpected token in '.set'"); | 
|  | 791 | Lexer.Lex(); | 
|  | 792 |  | 
|  | 793 | return ParseAssignment(Name, true); | 
|  | 794 | } | 
|  | 795 |  | 
| Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 796 | /// ParseDirectiveSection: | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 797 | ///   ::= .section identifier (',' identifier)* | 
|  | 798 | /// FIXME: This should actually parse out the segment, section, attributes and | 
|  | 799 | /// sizeof_stub fields. | 
|  | 800 | bool AsmParser::ParseDirectiveDarwinSection() { | 
| Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 801 | SMLoc Loc = Lexer.getLoc(); | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 802 |  | 
| Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 803 | StringRef SectionName; | 
|  | 804 | if (ParseIdentifier(SectionName)) | 
|  | 805 | return Error(Loc, "expected identifier after '.section' directive"); | 
|  | 806 |  | 
|  | 807 | // Verify there is a following comma. | 
|  | 808 | if (!Lexer.is(AsmToken::Comma)) | 
|  | 809 | return TokError("unexpected token in '.section' directive"); | 
|  | 810 |  | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 811 | std::string SectionSpec = SectionName; | 
| Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 812 | SectionSpec += ","; | 
|  | 813 |  | 
|  | 814 | // Add all the tokens until the end of the line, ParseSectionSpecifier will | 
|  | 815 | // handle this. | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 816 | StringRef EOL = Lexer.LexUntilEndOfStatement(); | 
|  | 817 | SectionSpec.append(EOL.begin(), EOL.end()); | 
| Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 818 |  | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 819 | Lexer.Lex(); | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 820 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 821 | return TokError("unexpected token in '.section' directive"); | 
|  | 822 | Lexer.Lex(); | 
|  | 823 |  | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 824 |  | 
|  | 825 | StringRef Segment, Section; | 
|  | 826 | unsigned TAA, StubSize; | 
|  | 827 | std::string ErrorStr = | 
|  | 828 | MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section, | 
|  | 829 | TAA, StubSize); | 
|  | 830 |  | 
|  | 831 | if (!ErrorStr.empty()) | 
| Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 832 | return Error(Loc, ErrorStr.c_str()); | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 833 |  | 
| Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 834 | // FIXME: Arch specific. | 
| Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 835 | Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, | 
|  | 836 | SectionKind())); | 
| Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 837 | return false; | 
|  | 838 | } | 
|  | 839 |  | 
| Chris Lattner | e15c2d7 | 2009-08-10 18:05:55 +0000 | [diff] [blame] | 840 | /// ParseDirectiveSectionSwitch - | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 841 | bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment, | 
|  | 842 | const char *Section, | 
| Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 843 | unsigned TAA, unsigned Align, | 
|  | 844 | unsigned StubSize) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 845 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 846 | return TokError("unexpected token in section switching directive"); | 
|  | 847 | Lexer.Lex(); | 
|  | 848 |  | 
| Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 849 | // FIXME: Arch specific. | 
| Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 850 | Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, | 
|  | 851 | SectionKind())); | 
| Daniel Dunbar | 2330df6 | 2009-08-21 23:30:15 +0000 | [diff] [blame] | 852 |  | 
|  | 853 | // Set the implicit alignment, if any. | 
|  | 854 | // | 
|  | 855 | // FIXME: This isn't really what 'as' does; I think it just uses the implicit | 
|  | 856 | // alignment on the section (e.g., if one manually inserts bytes into the | 
|  | 857 | // section, then just issueing the section switch directive will not realign | 
|  | 858 | // the section. However, this is arguably more reasonable behavior, and there | 
|  | 859 | // is no good reason for someone to intentionally emit incorrectly sized | 
|  | 860 | // values into the implicitly aligned sections. | 
|  | 861 | if (Align) | 
|  | 862 | Out.EmitValueToAlignment(Align, 0, 1, 0); | 
|  | 863 |  | 
| Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 864 | return false; | 
|  | 865 | } | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 866 |  | 
| Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 867 | bool AsmParser::ParseEscapedString(std::string &Data) { | 
|  | 868 | assert(Lexer.is(AsmToken::String) && "Unexpected current token!"); | 
|  | 869 |  | 
|  | 870 | Data = ""; | 
|  | 871 | StringRef Str = Lexer.getTok().getStringContents(); | 
|  | 872 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { | 
|  | 873 | if (Str[i] != '\\') { | 
|  | 874 | Data += Str[i]; | 
|  | 875 | continue; | 
|  | 876 | } | 
|  | 877 |  | 
|  | 878 | // Recognize escaped characters. Note that this escape semantics currently | 
|  | 879 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. | 
|  | 880 | ++i; | 
|  | 881 | if (i == e) | 
|  | 882 | return TokError("unexpected backslash at end of string"); | 
|  | 883 |  | 
|  | 884 | // Recognize octal sequences. | 
|  | 885 | if ((unsigned) (Str[i] - '0') <= 7) { | 
|  | 886 | // Consume up to three octal characters. | 
|  | 887 | unsigned Value = Str[i] - '0'; | 
|  | 888 |  | 
|  | 889 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { | 
|  | 890 | ++i; | 
|  | 891 | Value = Value * 8 + (Str[i] - '0'); | 
|  | 892 |  | 
|  | 893 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { | 
|  | 894 | ++i; | 
|  | 895 | Value = Value * 8 + (Str[i] - '0'); | 
|  | 896 | } | 
|  | 897 | } | 
|  | 898 |  | 
|  | 899 | if (Value > 255) | 
|  | 900 | return TokError("invalid octal escape sequence (out of range)"); | 
|  | 901 |  | 
|  | 902 | Data += (unsigned char) Value; | 
|  | 903 | continue; | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | // Otherwise recognize individual escapes. | 
|  | 907 | switch (Str[i]) { | 
|  | 908 | default: | 
|  | 909 | // Just reject invalid escape sequences for now. | 
|  | 910 | return TokError("invalid escape sequence (unrecognized character)"); | 
|  | 911 |  | 
|  | 912 | case 'b': Data += '\b'; break; | 
|  | 913 | case 'f': Data += '\f'; break; | 
|  | 914 | case 'n': Data += '\n'; break; | 
|  | 915 | case 'r': Data += '\r'; break; | 
|  | 916 | case 't': Data += '\t'; break; | 
|  | 917 | case '"': Data += '"'; break; | 
|  | 918 | case '\\': Data += '\\'; break; | 
|  | 919 | } | 
|  | 920 | } | 
|  | 921 |  | 
|  | 922 | return false; | 
|  | 923 | } | 
|  | 924 |  | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 925 | /// ParseDirectiveAscii: | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 926 | ///   ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 927 | bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 928 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 929 | for (;;) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 930 | if (Lexer.isNot(AsmToken::String)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 931 | return TokError("expected string in '.ascii' or '.asciz' directive"); | 
|  | 932 |  | 
| Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 933 | std::string Data; | 
|  | 934 | if (ParseEscapedString(Data)) | 
|  | 935 | return true; | 
|  | 936 |  | 
|  | 937 | Out.EmitBytes(Data); | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 938 | if (ZeroTerminated) | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 939 | Out.EmitBytes(StringRef("\0", 1)); | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 940 |  | 
|  | 941 | Lexer.Lex(); | 
|  | 942 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 943 | if (Lexer.is(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 944 | break; | 
|  | 945 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 946 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 947 | return TokError("unexpected token in '.ascii' or '.asciz' directive"); | 
|  | 948 | Lexer.Lex(); | 
|  | 949 | } | 
|  | 950 | } | 
|  | 951 |  | 
|  | 952 | Lexer.Lex(); | 
|  | 953 | return false; | 
|  | 954 | } | 
|  | 955 |  | 
|  | 956 | /// ParseDirectiveValue | 
|  | 957 | ///  ::= (.byte | .short | ... ) [ expression (, expression)* ] | 
|  | 958 | bool AsmParser::ParseDirectiveValue(unsigned Size) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 959 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 960 | for (;;) { | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 961 | MCValue Expr; | 
|  | 962 | if (ParseRelocatableExpression(Expr)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 963 | return true; | 
|  | 964 |  | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 965 | Out.EmitValue(Expr, Size); | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 966 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 967 | if (Lexer.is(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 968 | break; | 
|  | 969 |  | 
|  | 970 | // FIXME: Improve diagnostic. | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 971 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 972 | return TokError("unexpected token in directive"); | 
|  | 973 | Lexer.Lex(); | 
|  | 974 | } | 
|  | 975 | } | 
|  | 976 |  | 
|  | 977 | Lexer.Lex(); | 
|  | 978 | return false; | 
|  | 979 | } | 
|  | 980 |  | 
|  | 981 | /// ParseDirectiveSpace | 
|  | 982 | ///  ::= .space expression [ , expression ] | 
|  | 983 | bool AsmParser::ParseDirectiveSpace() { | 
|  | 984 | int64_t NumBytes; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 985 | if (ParseAbsoluteExpression(NumBytes)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 986 | return true; | 
|  | 987 |  | 
|  | 988 | int64_t FillExpr = 0; | 
|  | 989 | bool HasFillExpr = false; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 990 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 991 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 992 | return TokError("unexpected token in '.space' directive"); | 
|  | 993 | Lexer.Lex(); | 
|  | 994 |  | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 995 | if (ParseAbsoluteExpression(FillExpr)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 996 | return true; | 
|  | 997 |  | 
|  | 998 | HasFillExpr = true; | 
|  | 999 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1000 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1001 | return TokError("unexpected token in '.space' directive"); | 
|  | 1002 | } | 
|  | 1003 |  | 
|  | 1004 | Lexer.Lex(); | 
|  | 1005 |  | 
|  | 1006 | if (NumBytes <= 0) | 
|  | 1007 | return TokError("invalid number of bytes in '.space' directive"); | 
|  | 1008 |  | 
|  | 1009 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. | 
|  | 1010 | for (uint64_t i = 0, e = NumBytes; i != e; ++i) | 
|  | 1011 | Out.EmitValue(MCValue::get(FillExpr), 1); | 
|  | 1012 |  | 
|  | 1013 | return false; | 
|  | 1014 | } | 
|  | 1015 |  | 
|  | 1016 | /// ParseDirectiveFill | 
|  | 1017 | ///  ::= .fill expression , expression , expression | 
|  | 1018 | bool AsmParser::ParseDirectiveFill() { | 
|  | 1019 | int64_t NumValues; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1020 | if (ParseAbsoluteExpression(NumValues)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1021 | return true; | 
|  | 1022 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1023 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1024 | return TokError("unexpected token in '.fill' directive"); | 
|  | 1025 | Lexer.Lex(); | 
|  | 1026 |  | 
|  | 1027 | int64_t FillSize; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1028 | if (ParseAbsoluteExpression(FillSize)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1029 | return true; | 
|  | 1030 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1031 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1032 | return TokError("unexpected token in '.fill' directive"); | 
|  | 1033 | Lexer.Lex(); | 
|  | 1034 |  | 
|  | 1035 | int64_t FillExpr; | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1036 | if (ParseAbsoluteExpression(FillExpr)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1037 | return true; | 
|  | 1038 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1039 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1040 | return TokError("unexpected token in '.fill' directive"); | 
|  | 1041 |  | 
|  | 1042 | Lexer.Lex(); | 
|  | 1043 |  | 
| Daniel Dunbar | bc38ca7 | 2009-08-21 15:43:35 +0000 | [diff] [blame] | 1044 | if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) | 
|  | 1045 | return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); | 
| Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1046 |  | 
|  | 1047 | for (uint64_t i = 0, e = NumValues; i != e; ++i) | 
|  | 1048 | Out.EmitValue(MCValue::get(FillExpr), FillSize); | 
|  | 1049 |  | 
|  | 1050 | return false; | 
|  | 1051 | } | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1052 |  | 
|  | 1053 | /// ParseDirectiveOrg | 
|  | 1054 | ///  ::= .org expression [ , expression ] | 
|  | 1055 | bool AsmParser::ParseDirectiveOrg() { | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1056 | MCValue Offset; | 
|  | 1057 | if (ParseRelocatableExpression(Offset)) | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1058 | return true; | 
|  | 1059 |  | 
|  | 1060 | // Parse optional fill expression. | 
|  | 1061 | int64_t FillExpr = 0; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1062 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1063 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1064 | return TokError("unexpected token in '.org' directive"); | 
|  | 1065 | Lexer.Lex(); | 
|  | 1066 |  | 
| Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1067 | if (ParseAbsoluteExpression(FillExpr)) | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1068 | return true; | 
|  | 1069 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1070 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1071 | return TokError("unexpected token in '.org' directive"); | 
|  | 1072 | } | 
|  | 1073 |  | 
|  | 1074 | Lexer.Lex(); | 
| Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1075 |  | 
|  | 1076 | // FIXME: Only limited forms of relocatable expressions are accepted here, it | 
|  | 1077 | // has to be relative to the current section. | 
|  | 1078 | Out.EmitValueToOffset(Offset, FillExpr); | 
| Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1079 |  | 
|  | 1080 | return false; | 
|  | 1081 | } | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1082 |  | 
|  | 1083 | /// ParseDirectiveAlign | 
|  | 1084 | ///  ::= {.align, ...} expression [ , expression [ , expression ]] | 
|  | 1085 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { | 
| Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1086 | SMLoc AlignmentLoc = Lexer.getLoc(); | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1087 | int64_t Alignment; | 
|  | 1088 | if (ParseAbsoluteExpression(Alignment)) | 
|  | 1089 | return true; | 
|  | 1090 |  | 
|  | 1091 | SMLoc MaxBytesLoc; | 
|  | 1092 | bool HasFillExpr = false; | 
|  | 1093 | int64_t FillExpr = 0; | 
|  | 1094 | int64_t MaxBytesToFill = 0; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1095 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1096 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1097 | return TokError("unexpected token in directive"); | 
|  | 1098 | Lexer.Lex(); | 
|  | 1099 |  | 
|  | 1100 | // The fill expression can be omitted while specifying a maximum number of | 
|  | 1101 | // alignment bytes, e.g: | 
|  | 1102 | //  .align 3,,4 | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1103 | if (Lexer.isNot(AsmToken::Comma)) { | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1104 | HasFillExpr = true; | 
|  | 1105 | if (ParseAbsoluteExpression(FillExpr)) | 
|  | 1106 | return true; | 
|  | 1107 | } | 
|  | 1108 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1109 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1110 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1111 | return TokError("unexpected token in directive"); | 
|  | 1112 | Lexer.Lex(); | 
|  | 1113 |  | 
|  | 1114 | MaxBytesLoc = Lexer.getLoc(); | 
|  | 1115 | if (ParseAbsoluteExpression(MaxBytesToFill)) | 
|  | 1116 | return true; | 
|  | 1117 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1118 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1119 | return TokError("unexpected token in directive"); | 
|  | 1120 | } | 
|  | 1121 | } | 
|  | 1122 |  | 
|  | 1123 | Lexer.Lex(); | 
|  | 1124 |  | 
|  | 1125 | if (!HasFillExpr) { | 
|  | 1126 | // FIXME: Sometimes fill with nop. | 
|  | 1127 | FillExpr = 0; | 
|  | 1128 | } | 
|  | 1129 |  | 
|  | 1130 | // Compute alignment in bytes. | 
|  | 1131 | if (IsPow2) { | 
|  | 1132 | // FIXME: Diagnose overflow. | 
| Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1133 | if (Alignment >= 32) { | 
|  | 1134 | Error(AlignmentLoc, "invalid alignment value"); | 
|  | 1135 | Alignment = 31; | 
|  | 1136 | } | 
|  | 1137 |  | 
|  | 1138 | Alignment = 1 << Alignment; | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1139 | } | 
|  | 1140 |  | 
| Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1141 | // Diagnose non-sensical max bytes to align. | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1142 | if (MaxBytesLoc.isValid()) { | 
|  | 1143 | if (MaxBytesToFill < 1) { | 
| Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1144 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " | 
|  | 1145 | "many bytes, ignoring maximum bytes expression"); | 
| Daniel Dunbar | 0afb9f5 | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 1146 | MaxBytesToFill = 0; | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1147 | } | 
|  | 1148 |  | 
|  | 1149 | if (MaxBytesToFill >= Alignment) { | 
| Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 1150 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " | 
|  | 1151 | "has no effect"); | 
| Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1152 | MaxBytesToFill = 0; | 
|  | 1153 | } | 
|  | 1154 | } | 
|  | 1155 |  | 
|  | 1156 | // FIXME: Target specific behavior about how the "extra" bytes are filled. | 
|  | 1157 | Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill); | 
|  | 1158 |  | 
|  | 1159 | return false; | 
|  | 1160 | } | 
|  | 1161 |  | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1162 | /// ParseDirectiveSymbolAttribute | 
|  | 1163 | ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] | 
|  | 1164 | bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1165 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1166 | for (;;) { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1167 | StringRef Name; | 
|  | 1168 |  | 
|  | 1169 | if (ParseIdentifier(Name)) | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1170 | return TokError("expected identifier in directive"); | 
|  | 1171 |  | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1172 | MCSymbol *Sym = CreateSymbol(Name); | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1173 |  | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1174 | Out.EmitSymbolAttribute(Sym, Attr); | 
|  | 1175 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1176 | if (Lexer.is(AsmToken::EndOfStatement)) | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1177 | break; | 
|  | 1178 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1179 | if (Lexer.isNot(AsmToken::Comma)) | 
| Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1180 | return TokError("unexpected token in directive"); | 
|  | 1181 | Lexer.Lex(); | 
|  | 1182 | } | 
|  | 1183 | } | 
|  | 1184 |  | 
|  | 1185 | Lexer.Lex(); | 
|  | 1186 | return false; | 
|  | 1187 | } | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1188 |  | 
| Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1189 | /// ParseDirectiveDarwinSymbolDesc | 
|  | 1190 | ///  ::= .desc identifier , expression | 
|  | 1191 | bool AsmParser::ParseDirectiveDarwinSymbolDesc() { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1192 | StringRef Name; | 
|  | 1193 | if (ParseIdentifier(Name)) | 
| Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1194 | return TokError("expected identifier in directive"); | 
|  | 1195 |  | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1196 | // Handle the identifier as the key symbol. | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1197 | MCSymbol *Sym = CreateSymbol(Name); | 
| Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1198 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1199 | if (Lexer.isNot(AsmToken::Comma)) | 
| Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1200 | return TokError("unexpected token in '.desc' directive"); | 
|  | 1201 | Lexer.Lex(); | 
|  | 1202 |  | 
|  | 1203 | SMLoc DescLoc = Lexer.getLoc(); | 
|  | 1204 | int64_t DescValue; | 
|  | 1205 | if (ParseAbsoluteExpression(DescValue)) | 
|  | 1206 | return true; | 
|  | 1207 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1208 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1209 | return TokError("unexpected token in '.desc' directive"); | 
|  | 1210 |  | 
|  | 1211 | Lexer.Lex(); | 
|  | 1212 |  | 
|  | 1213 | // Set the n_desc field of this Symbol to this DescValue | 
|  | 1214 | Out.EmitSymbolDesc(Sym, DescValue); | 
|  | 1215 |  | 
|  | 1216 | return false; | 
|  | 1217 | } | 
|  | 1218 |  | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1219 | /// ParseDirectiveComm | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1220 | ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] | 
|  | 1221 | bool AsmParser::ParseDirectiveComm(bool IsLocal) { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1222 | SMLoc IDLoc = Lexer.getLoc(); | 
|  | 1223 | StringRef Name; | 
|  | 1224 | if (ParseIdentifier(Name)) | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1225 | return TokError("expected identifier in directive"); | 
|  | 1226 |  | 
| Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1227 | // Handle the identifier as the key symbol. | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1228 | MCSymbol *Sym = CreateSymbol(Name); | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1229 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1230 | if (Lexer.isNot(AsmToken::Comma)) | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1231 | return TokError("unexpected token in directive"); | 
|  | 1232 | Lexer.Lex(); | 
|  | 1233 |  | 
|  | 1234 | int64_t Size; | 
|  | 1235 | SMLoc SizeLoc = Lexer.getLoc(); | 
|  | 1236 | if (ParseAbsoluteExpression(Size)) | 
|  | 1237 | return true; | 
|  | 1238 |  | 
|  | 1239 | int64_t Pow2Alignment = 0; | 
|  | 1240 | SMLoc Pow2AlignmentLoc; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1241 | if (Lexer.is(AsmToken::Comma)) { | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1242 | Lexer.Lex(); | 
|  | 1243 | Pow2AlignmentLoc = Lexer.getLoc(); | 
|  | 1244 | if (ParseAbsoluteExpression(Pow2Alignment)) | 
|  | 1245 | return true; | 
|  | 1246 | } | 
|  | 1247 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1248 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1249 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1250 |  | 
|  | 1251 | Lexer.Lex(); | 
|  | 1252 |  | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1253 | // NOTE: a size of zero for a .comm should create a undefined symbol | 
|  | 1254 | // but a size of .lcomm creates a bss symbol of size zero. | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1255 | if (Size < 0) | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1256 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " | 
|  | 1257 | "be less than zero"); | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1258 |  | 
|  | 1259 | // NOTE: The alignment in the directive is a power of 2 value, the assember | 
|  | 1260 | // may internally end up wanting an alignment in bytes. | 
|  | 1261 | // FIXME: Diagnose overflow. | 
|  | 1262 | if (Pow2Alignment < 0) | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1263 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " | 
|  | 1264 | "alignment, can't be less than zero"); | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1265 |  | 
| Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 1266 | if (!Sym->isUndefined()) | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1267 | return Error(IDLoc, "invalid symbol redefinition"); | 
|  | 1268 |  | 
| Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1269 | // '.lcomm' is equivalent to '.zerofill'. | 
| Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1270 | // Create the Symbol as a common or local common with Size and Pow2Alignment | 
| Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1271 | if (IsLocal) { | 
| Daniel Dunbar | e6cdbf2 | 2009-08-28 05:48:46 +0000 | [diff] [blame] | 1272 | Out.EmitZerofill(getMachOSection("__DATA", "__bss", | 
|  | 1273 | MCSectionMachO::S_ZEROFILL, 0, | 
|  | 1274 | SectionKind()), | 
| Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1275 | Sym, Size, 1 << Pow2Alignment); | 
|  | 1276 | return false; | 
|  | 1277 | } | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1278 |  | 
| Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1279 | Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); | 
| Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1280 | return false; | 
|  | 1281 | } | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1282 |  | 
|  | 1283 | /// ParseDirectiveDarwinZerofill | 
|  | 1284 | ///  ::= .zerofill segname , sectname [, identifier , size_expression [ | 
|  | 1285 | ///      , align_expression ]] | 
|  | 1286 | bool AsmParser::ParseDirectiveDarwinZerofill() { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1287 | // FIXME: Handle quoted names here. | 
|  | 1288 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1289 | if (Lexer.isNot(AsmToken::Identifier)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1290 | return TokError("expected segment name after '.zerofill' directive"); | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1291 | StringRef Segment = Lexer.getTok().getString(); | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1292 | Lexer.Lex(); | 
|  | 1293 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1294 | if (Lexer.isNot(AsmToken::Comma)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1295 | return TokError("unexpected token in directive"); | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1296 | Lexer.Lex(); | 
|  | 1297 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1298 | if (Lexer.isNot(AsmToken::Identifier)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1299 | return TokError("expected section name after comma in '.zerofill' " | 
|  | 1300 | "directive"); | 
| Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1301 | StringRef Section = Lexer.getTok().getString(); | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1302 | Lexer.Lex(); | 
|  | 1303 |  | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1304 | // If this is the end of the line all that was wanted was to create the | 
|  | 1305 | // the section but with no symbol. | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1306 | if (Lexer.is(AsmToken::EndOfStatement)) { | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1307 | // Create the zerofill section but no symbol | 
| Daniel Dunbar | 2e15292 | 2009-08-28 05:48:29 +0000 | [diff] [blame] | 1308 | Out.EmitZerofill(getMachOSection(Segment, Section, | 
|  | 1309 | MCSectionMachO::S_ZEROFILL, 0, | 
|  | 1310 | SectionKind())); | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1311 | return false; | 
|  | 1312 | } | 
|  | 1313 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1314 | if (Lexer.isNot(AsmToken::Comma)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1315 | return TokError("unexpected token in directive"); | 
|  | 1316 | Lexer.Lex(); | 
|  | 1317 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1318 | if (Lexer.isNot(AsmToken::Identifier)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1319 | return TokError("expected identifier in directive"); | 
|  | 1320 |  | 
|  | 1321 | // handle the identifier as the key symbol. | 
|  | 1322 | SMLoc IDLoc = Lexer.getLoc(); | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1323 | MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString()); | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1324 | Lexer.Lex(); | 
|  | 1325 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1326 | if (Lexer.isNot(AsmToken::Comma)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1327 | return TokError("unexpected token in directive"); | 
|  | 1328 | Lexer.Lex(); | 
|  | 1329 |  | 
|  | 1330 | int64_t Size; | 
|  | 1331 | SMLoc SizeLoc = Lexer.getLoc(); | 
|  | 1332 | if (ParseAbsoluteExpression(Size)) | 
|  | 1333 | return true; | 
|  | 1334 |  | 
|  | 1335 | int64_t Pow2Alignment = 0; | 
|  | 1336 | SMLoc Pow2AlignmentLoc; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1337 | if (Lexer.is(AsmToken::Comma)) { | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1338 | Lexer.Lex(); | 
|  | 1339 | Pow2AlignmentLoc = Lexer.getLoc(); | 
|  | 1340 | if (ParseAbsoluteExpression(Pow2Alignment)) | 
|  | 1341 | return true; | 
|  | 1342 | } | 
|  | 1343 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1344 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1345 | return TokError("unexpected token in '.zerofill' directive"); | 
|  | 1346 |  | 
|  | 1347 | Lexer.Lex(); | 
|  | 1348 |  | 
|  | 1349 | if (Size < 0) | 
|  | 1350 | return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less " | 
|  | 1351 | "than zero"); | 
|  | 1352 |  | 
|  | 1353 | // NOTE: The alignment in the directive is a power of 2 value, the assember | 
|  | 1354 | // may internally end up wanting an alignment in bytes. | 
|  | 1355 | // FIXME: Diagnose overflow. | 
|  | 1356 | if (Pow2Alignment < 0) | 
|  | 1357 | return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, " | 
|  | 1358 | "can't be less than zero"); | 
|  | 1359 |  | 
| Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 1360 | if (!Sym->isUndefined()) | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1361 | return Error(IDLoc, "invalid symbol redefinition"); | 
|  | 1362 |  | 
| Daniel Dunbar | bdee6df | 2009-08-27 23:58:10 +0000 | [diff] [blame] | 1363 | // Create the zerofill Symbol with Size and Pow2Alignment | 
| Daniel Dunbar | 2e15292 | 2009-08-28 05:48:29 +0000 | [diff] [blame] | 1364 | // | 
|  | 1365 | // FIXME: Arch specific. | 
|  | 1366 | Out.EmitZerofill(getMachOSection(Segment, Section, | 
|  | 1367 | MCSectionMachO::S_ZEROFILL, 0, | 
|  | 1368 | SectionKind()), | 
| Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1369 | Sym, Size, 1 << Pow2Alignment); | 
| Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1370 |  | 
|  | 1371 | return false; | 
|  | 1372 | } | 
| Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1373 |  | 
|  | 1374 | /// ParseDirectiveDarwinSubsectionsViaSymbols | 
|  | 1375 | ///  ::= .subsections_via_symbols | 
|  | 1376 | bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1377 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1378 | return TokError("unexpected token in '.subsections_via_symbols' directive"); | 
|  | 1379 |  | 
|  | 1380 | Lexer.Lex(); | 
|  | 1381 |  | 
| Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 1382 | Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols); | 
| Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1383 |  | 
|  | 1384 | return false; | 
|  | 1385 | } | 
| Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1386 |  | 
|  | 1387 | /// ParseDirectiveAbort | 
|  | 1388 | ///  ::= .abort [ "abort_string" ] | 
|  | 1389 | bool AsmParser::ParseDirectiveAbort() { | 
| Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1390 | // FIXME: Use loc from directive. | 
|  | 1391 | SMLoc Loc = Lexer.getLoc(); | 
|  | 1392 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1393 | StringRef Str = ""; | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1394 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1395 | if (Lexer.isNot(AsmToken::String)) | 
| Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1396 | return TokError("expected string in '.abort' directive"); | 
|  | 1397 |  | 
| Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1398 | Str = Lexer.getTok().getString(); | 
| Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1399 |  | 
|  | 1400 | Lexer.Lex(); | 
|  | 1401 | } | 
|  | 1402 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1403 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1404 | return TokError("unexpected token in '.abort' directive"); | 
|  | 1405 |  | 
|  | 1406 | Lexer.Lex(); | 
|  | 1407 |  | 
| Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1408 | // FIXME: Handle here. | 
| Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1409 | if (Str.empty()) | 
|  | 1410 | Error(Loc, ".abort detected. Assembly stopping."); | 
|  | 1411 | else | 
|  | 1412 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); | 
| Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1413 |  | 
|  | 1414 | return false; | 
|  | 1415 | } | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1416 |  | 
|  | 1417 | /// ParseDirectiveLsym | 
|  | 1418 | ///  ::= .lsym identifier , expression | 
|  | 1419 | bool AsmParser::ParseDirectiveDarwinLsym() { | 
| Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1420 | StringRef Name; | 
|  | 1421 | if (ParseIdentifier(Name)) | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1422 | return TokError("expected identifier in directive"); | 
|  | 1423 |  | 
| Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1424 | // Handle the identifier as the key symbol. | 
| Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1425 | MCSymbol *Sym = CreateSymbol(Name); | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1426 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1427 | if (Lexer.isNot(AsmToken::Comma)) | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1428 | return TokError("unexpected token in '.lsym' directive"); | 
|  | 1429 | Lexer.Lex(); | 
|  | 1430 |  | 
|  | 1431 | MCValue Expr; | 
|  | 1432 | if (ParseRelocatableExpression(Expr)) | 
|  | 1433 | return true; | 
|  | 1434 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1435 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1436 | return TokError("unexpected token in '.lsym' directive"); | 
|  | 1437 |  | 
|  | 1438 | Lexer.Lex(); | 
|  | 1439 |  | 
| Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1440 | // We don't currently support this directive. | 
|  | 1441 | // | 
|  | 1442 | // FIXME: Diagnostic location! | 
|  | 1443 | (void) Sym; | 
|  | 1444 | return TokError("directive '.lsym' is unsupported"); | 
| Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1445 | } | 
| Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1446 |  | 
|  | 1447 | /// ParseDirectiveInclude | 
|  | 1448 | ///  ::= .include "filename" | 
|  | 1449 | bool AsmParser::ParseDirectiveInclude() { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1450 | if (Lexer.isNot(AsmToken::String)) | 
| Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1451 | return TokError("expected string in '.include' directive"); | 
|  | 1452 |  | 
| Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1453 | std::string Filename = Lexer.getTok().getString(); | 
| Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1454 | SMLoc IncludeLoc = Lexer.getLoc(); | 
| Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1455 | Lexer.Lex(); | 
|  | 1456 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1457 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1458 | return TokError("unexpected token in '.include' directive"); | 
|  | 1459 |  | 
| Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1460 | // Strip the quotes. | 
|  | 1461 | Filename = Filename.substr(1, Filename.size()-2); | 
|  | 1462 |  | 
|  | 1463 | // Attempt to switch the lexer to the included file before consuming the end | 
|  | 1464 | // of statement to avoid losing it when we switch. | 
|  | 1465 | if (Lexer.EnterIncludeFile(Filename)) { | 
|  | 1466 | Lexer.PrintMessage(IncludeLoc, | 
|  | 1467 | "Could not find include file '" + Filename + "'", | 
|  | 1468 | "error"); | 
|  | 1469 | return true; | 
|  | 1470 | } | 
| Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1471 |  | 
|  | 1472 | return false; | 
|  | 1473 | } | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1474 |  | 
|  | 1475 | /// ParseDirectiveDarwinDumpOrLoad | 
|  | 1476 | ///  ::= ( .dump | .load ) "filename" | 
| Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1477 | bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) { | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1478 | if (Lexer.isNot(AsmToken::String)) | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1479 | return TokError("expected string in '.dump' or '.load' directive"); | 
|  | 1480 |  | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1481 | Lexer.Lex(); | 
|  | 1482 |  | 
| Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1483 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1484 | return TokError("unexpected token in '.dump' or '.load' directive"); | 
|  | 1485 |  | 
|  | 1486 | Lexer.Lex(); | 
|  | 1487 |  | 
| Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1488 | // FIXME: If/when .dump and .load are implemented they will be done in the | 
|  | 1489 | // the assembly parser and not have any need for an MCStreamer API. | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1490 | if (IsDump) | 
| Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1491 | Warning(IDLoc, "ignoring directive .dump for now"); | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1492 | else | 
| Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1493 | Warning(IDLoc, "ignoring directive .load for now"); | 
| Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1494 |  | 
|  | 1495 | return false; | 
|  | 1496 | } | 
| Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1497 |  | 
|  | 1498 | /// ParseDirectiveIf | 
|  | 1499 | /// ::= .if expression | 
|  | 1500 | bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { | 
|  | 1501 | // Consume the identifier that was the .if directive | 
|  | 1502 | Lexer.Lex(); | 
|  | 1503 |  | 
|  | 1504 | TheCondStack.push_back(TheCondState); | 
|  | 1505 | TheCondState.TheCond = AsmCond::IfCond; | 
|  | 1506 | if(TheCondState.Ignore) { | 
|  | 1507 | EatToEndOfStatement(); | 
|  | 1508 | } | 
|  | 1509 | else { | 
|  | 1510 | int64_t ExprValue; | 
|  | 1511 | if (ParseAbsoluteExpression(ExprValue)) | 
|  | 1512 | return true; | 
|  | 1513 |  | 
|  | 1514 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1515 | return TokError("unexpected token in '.if' directive"); | 
|  | 1516 |  | 
|  | 1517 | Lexer.Lex(); | 
|  | 1518 |  | 
|  | 1519 | TheCondState.CondMet = ExprValue; | 
|  | 1520 | TheCondState.Ignore = !TheCondState.CondMet; | 
|  | 1521 | } | 
|  | 1522 |  | 
|  | 1523 | return false; | 
|  | 1524 | } | 
|  | 1525 |  | 
|  | 1526 | /// ParseDirectiveElseIf | 
|  | 1527 | /// ::= .elseif expression | 
|  | 1528 | bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { | 
|  | 1529 | if (TheCondState.TheCond != AsmCond::IfCond && | 
|  | 1530 | TheCondState.TheCond != AsmCond::ElseIfCond) | 
|  | 1531 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " | 
|  | 1532 | " an .elseif"); | 
|  | 1533 | TheCondState.TheCond = AsmCond::ElseIfCond; | 
|  | 1534 |  | 
|  | 1535 | // Consume the identifier that was the .elseif directive | 
|  | 1536 | Lexer.Lex(); | 
|  | 1537 |  | 
|  | 1538 | bool LastIgnoreState = false; | 
|  | 1539 | if (!TheCondStack.empty()) | 
|  | 1540 | LastIgnoreState = TheCondStack.back().Ignore; | 
|  | 1541 | if (LastIgnoreState || TheCondState.CondMet) { | 
|  | 1542 | TheCondState.Ignore = true; | 
|  | 1543 | EatToEndOfStatement(); | 
|  | 1544 | } | 
|  | 1545 | else { | 
|  | 1546 | int64_t ExprValue; | 
|  | 1547 | if (ParseAbsoluteExpression(ExprValue)) | 
|  | 1548 | return true; | 
|  | 1549 |  | 
|  | 1550 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1551 | return TokError("unexpected token in '.elseif' directive"); | 
|  | 1552 |  | 
|  | 1553 | Lexer.Lex(); | 
|  | 1554 | TheCondState.CondMet = ExprValue; | 
|  | 1555 | TheCondState.Ignore = !TheCondState.CondMet; | 
|  | 1556 | } | 
|  | 1557 |  | 
|  | 1558 | return false; | 
|  | 1559 | } | 
|  | 1560 |  | 
|  | 1561 | /// ParseDirectiveElse | 
|  | 1562 | /// ::= .else | 
|  | 1563 | bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { | 
|  | 1564 | // Consume the identifier that was the .else directive | 
|  | 1565 | Lexer.Lex(); | 
|  | 1566 |  | 
|  | 1567 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1568 | return TokError("unexpected token in '.else' directive"); | 
|  | 1569 |  | 
|  | 1570 | Lexer.Lex(); | 
|  | 1571 |  | 
|  | 1572 | if (TheCondState.TheCond != AsmCond::IfCond && | 
|  | 1573 | TheCondState.TheCond != AsmCond::ElseIfCond) | 
|  | 1574 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " | 
|  | 1575 | ".elseif"); | 
|  | 1576 | TheCondState.TheCond = AsmCond::ElseCond; | 
|  | 1577 | bool LastIgnoreState = false; | 
|  | 1578 | if (!TheCondStack.empty()) | 
|  | 1579 | LastIgnoreState = TheCondStack.back().Ignore; | 
|  | 1580 | if (LastIgnoreState || TheCondState.CondMet) | 
|  | 1581 | TheCondState.Ignore = true; | 
|  | 1582 | else | 
|  | 1583 | TheCondState.Ignore = false; | 
|  | 1584 |  | 
|  | 1585 | return false; | 
|  | 1586 | } | 
|  | 1587 |  | 
|  | 1588 | /// ParseDirectiveEndIf | 
|  | 1589 | /// ::= .endif | 
|  | 1590 | bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { | 
|  | 1591 | // Consume the identifier that was the .endif directive | 
|  | 1592 | Lexer.Lex(); | 
|  | 1593 |  | 
|  | 1594 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1595 | return TokError("unexpected token in '.endif' directive"); | 
|  | 1596 |  | 
|  | 1597 | Lexer.Lex(); | 
|  | 1598 |  | 
|  | 1599 | if ((TheCondState.TheCond == AsmCond::NoCond) || | 
|  | 1600 | TheCondStack.empty()) | 
|  | 1601 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " | 
|  | 1602 | ".else"); | 
|  | 1603 | if (!TheCondStack.empty()) { | 
|  | 1604 | TheCondState = TheCondStack.back(); | 
|  | 1605 | TheCondStack.pop_back(); | 
|  | 1606 | } | 
|  | 1607 |  | 
|  | 1608 | return false; | 
|  | 1609 | } | 
| Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1610 |  | 
|  | 1611 | /// ParseDirectiveFile | 
|  | 1612 | /// ::= .file [number] string | 
|  | 1613 | bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) { | 
|  | 1614 | // FIXME: I'm not sure what this is. | 
|  | 1615 | int64_t FileNumber = -1; | 
|  | 1616 | if (Lexer.is(AsmToken::Integer)) { | 
|  | 1617 | FileNumber = Lexer.getTok().getIntVal(); | 
|  | 1618 | Lexer.Lex(); | 
|  | 1619 |  | 
|  | 1620 | if (FileNumber < 1) | 
|  | 1621 | return TokError("file number less than one"); | 
|  | 1622 | } | 
|  | 1623 |  | 
|  | 1624 | if (Lexer.isNot(AsmToken::String)) | 
|  | 1625 | return TokError("unexpected token in '.file' directive"); | 
|  | 1626 |  | 
|  | 1627 | StringRef FileName = Lexer.getTok().getString(); | 
|  | 1628 | Lexer.Lex(); | 
|  | 1629 |  | 
|  | 1630 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1631 | return TokError("unexpected token in '.file' directive"); | 
|  | 1632 |  | 
|  | 1633 | // FIXME: Do something with the .file. | 
|  | 1634 |  | 
|  | 1635 | return false; | 
|  | 1636 | } | 
|  | 1637 |  | 
|  | 1638 | /// ParseDirectiveLine | 
|  | 1639 | /// ::= .line [number] | 
|  | 1640 | bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) { | 
|  | 1641 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1642 | if (Lexer.isNot(AsmToken::Integer)) | 
|  | 1643 | return TokError("unexpected token in '.line' directive"); | 
|  | 1644 |  | 
|  | 1645 | int64_t LineNumber = Lexer.getTok().getIntVal(); | 
|  | 1646 | (void) LineNumber; | 
|  | 1647 | Lexer.Lex(); | 
|  | 1648 |  | 
|  | 1649 | // FIXME: Do something with the .line. | 
|  | 1650 | } | 
|  | 1651 |  | 
|  | 1652 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1653 | return TokError("unexpected token in '.file' directive"); | 
|  | 1654 |  | 
|  | 1655 | return false; | 
|  | 1656 | } | 
|  | 1657 |  | 
|  | 1658 |  | 
|  | 1659 | /// ParseDirectiveLoc | 
|  | 1660 | /// ::= .loc number [number [number]] | 
|  | 1661 | bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) { | 
|  | 1662 | if (Lexer.isNot(AsmToken::Integer)) | 
|  | 1663 | return TokError("unexpected token in '.loc' directive"); | 
|  | 1664 |  | 
|  | 1665 | // FIXME: What are these fields? | 
|  | 1666 | int64_t FileNumber = Lexer.getTok().getIntVal(); | 
|  | 1667 | (void) FileNumber; | 
|  | 1668 | // FIXME: Validate file. | 
|  | 1669 |  | 
|  | 1670 | Lexer.Lex(); | 
|  | 1671 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1672 | if (Lexer.isNot(AsmToken::Integer)) | 
|  | 1673 | return TokError("unexpected token in '.loc' directive"); | 
|  | 1674 |  | 
|  | 1675 | int64_t Param2 = Lexer.getTok().getIntVal(); | 
|  | 1676 | (void) Param2; | 
|  | 1677 | Lexer.Lex(); | 
|  | 1678 |  | 
|  | 1679 | if (Lexer.isNot(AsmToken::EndOfStatement)) { | 
|  | 1680 | if (Lexer.isNot(AsmToken::Integer)) | 
|  | 1681 | return TokError("unexpected token in '.loc' directive"); | 
|  | 1682 |  | 
|  | 1683 | int64_t Param3 = Lexer.getTok().getIntVal(); | 
|  | 1684 | (void) Param3; | 
|  | 1685 | Lexer.Lex(); | 
|  | 1686 |  | 
|  | 1687 | // FIXME: Do something with the .loc. | 
|  | 1688 | } | 
|  | 1689 | } | 
|  | 1690 |  | 
|  | 1691 | if (Lexer.isNot(AsmToken::EndOfStatement)) | 
|  | 1692 | return TokError("unexpected token in '.file' directive"); | 
|  | 1693 |  | 
|  | 1694 | return false; | 
|  | 1695 | } | 
|  | 1696 |  |