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 | |
Chris Lattner | be343b3 | 2010-01-22 01:58:08 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCParser/AsmParser.h" |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSwitch.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" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Bill Wendling | 9bc0af8 | 2009-12-28 01:34:57 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SourceMgr.h" |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetAsmParser.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 32 | |
| 33 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 34 | |
Daniel Dunbar | 9186fa6 | 2010-07-01 20:41:56 +0000 | [diff] [blame^] | 35 | AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx, |
| 36 | MCStreamer &_Out, const MCAsmInfo &_MAI) |
| 37 | : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), CurBuffer(0) { |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 38 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 39 | |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 40 | // Debugging directives. |
| 41 | AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile); |
| 42 | AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine); |
| 43 | AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc); |
| 44 | } |
| 45 | |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 46 | AsmParser::~AsmParser() { |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 49 | void AsmParser::Warning(SMLoc L, const Twine &Msg) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 50 | PrintMessage(L, Msg.str(), "warning"); |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 53 | bool AsmParser::Error(SMLoc L, const Twine &Msg) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 54 | PrintMessage(L, Msg.str(), "error"); |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool AsmParser::TokError(const char *Msg) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 59 | PrintMessage(Lexer.getLoc(), Msg, "error"); |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 63 | void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg, |
| 64 | const char *Type) const { |
| 65 | SrcMgr.PrintMessage(Loc, Msg, Type); |
| 66 | } |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 67 | |
| 68 | bool AsmParser::EnterIncludeFile(const std::string &Filename) { |
| 69 | int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc()); |
| 70 | if (NewBuf == -1) |
| 71 | return true; |
Sean Callanan | 79036e4 | 2010-01-20 22:18:24 +0000 | [diff] [blame] | 72 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 73 | CurBuffer = NewBuf; |
| 74 | |
| 75 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | const AsmToken &AsmParser::Lex() { |
| 81 | const AsmToken *tok = &Lexer.Lex(); |
| 82 | |
| 83 | if (tok->is(AsmToken::Eof)) { |
| 84 | // If this is the end of an included file, pop the parent file off the |
| 85 | // include stack. |
| 86 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 87 | if (ParentIncludeLoc != SMLoc()) { |
| 88 | CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc); |
| 89 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), |
| 90 | ParentIncludeLoc.getPointer()); |
| 91 | tok = &Lexer.Lex(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (tok->is(AsmToken::Error)) |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 96 | PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error"); |
Sean Callanan | 79036e4 | 2010-01-20 22:18:24 +0000 | [diff] [blame] | 97 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 98 | return *tok; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 101 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 102 | // Create the initial section, if requested. |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 103 | // |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 104 | // FIXME: Target hook & command line option for initial section. |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 105 | if (!NoInitialTextSection) |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 106 | Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text", |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 107 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 108 | 0, SectionKind::getText())); |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 109 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 110 | // Prime the lexer. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 111 | Lex(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 112 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 113 | bool HadError = false; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 114 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 115 | AsmCond StartingCondState = TheCondState; |
| 116 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 117 | // While we have input, parse each statement. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 118 | while (Lexer.isNot(AsmToken::Eof)) { |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 119 | if (!ParseStatement()) continue; |
| 120 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 121 | // 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] | 122 | HadError = true; |
| 123 | EatToEndOfStatement(); |
| 124 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 125 | |
| 126 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 127 | TheCondState.Ignore != StartingCondState.Ignore) |
| 128 | return TokError("unmatched .ifs or .elses"); |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 130 | // Finalize the output stream if there are no errors and if the client wants |
| 131 | // us to. |
| 132 | if (!HadError && !NoFinalize) |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 133 | Out.Finish(); |
| 134 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 135 | return HadError; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 138 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 139 | void AsmParser::EatToEndOfStatement() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 140 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
| 141 | Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 142 | Lex(); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 143 | |
| 144 | // Eat EOL. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 145 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 146 | Lex(); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 150 | /// ParseParenExpr - Parse a paren expression and return it. |
| 151 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 152 | /// |
| 153 | /// parenexpr ::= expr) |
| 154 | /// |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 155 | bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 156 | if (ParseExpression(Res)) return true; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 157 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 158 | return TokError("expected ')' in parentheses expression"); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 159 | EndLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 160 | Lex(); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 161 | return false; |
| 162 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 164 | MCSymbol *AsmParser::CreateSymbol(StringRef Name) { |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 165 | // FIXME: Inline into callers. |
Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 166 | return Ctx.GetOrCreateSymbol(Name); |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 169 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 170 | /// primaryexpr ::= (parenexpr |
| 171 | /// primaryexpr ::= symbol |
| 172 | /// primaryexpr ::= number |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 173 | /// primaryexpr ::= '.' |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 174 | /// primaryexpr ::= ~,+,- primaryexpr |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 175 | bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 176 | switch (Lexer.getKind()) { |
| 177 | default: |
| 178 | return TokError("unknown token in expression"); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 179 | case AsmToken::Exclaim: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 180 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 181 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 182 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 183 | Res = MCUnaryExpr::CreateLNot(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 184 | return false; |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 185 | case AsmToken::String: |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 186 | case AsmToken::Identifier: { |
| 187 | // This is a symbol reference. |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 188 | std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@'); |
| 189 | MCSymbol *Sym = CreateSymbol(Split.first); |
| 190 | |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 191 | // Mark the symbol as used in an expression. |
| 192 | Sym->setUsedInExpr(true); |
| 193 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 194 | // Lookup the symbol variant if used. |
| 195 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 196 | if (Split.first.size() != getTok().getIdentifier().size()) |
| 197 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
| 198 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 199 | EndLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 200 | Lex(); // Eat identifier. |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 201 | |
| 202 | // If this is an absolute variable reference, substitute it now to preserve |
| 203 | // semantics in the face of reassignment. |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 204 | if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 205 | if (Variant) |
| 206 | return Error(EndLoc, "unexpected modified on variable reference"); |
| 207 | |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 208 | Res = Sym->getVariableValue(); |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
| 212 | // Otherwise create a symbol ref. |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 213 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 214 | return false; |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 215 | } |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 216 | case AsmToken::Integer: { |
| 217 | SMLoc Loc = getTok().getLoc(); |
| 218 | int64_t IntVal = getTok().getIntVal(); |
| 219 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 220 | EndLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 221 | Lex(); // Eat token. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 222 | // Look for 'b' or 'f' following an Integer as a directional label |
| 223 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 224 | StringRef IDVal = getTok().getString(); |
| 225 | if (IDVal == "f" || IDVal == "b"){ |
| 226 | MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal, |
| 227 | IDVal == "f" ? 1 : 0); |
| 228 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
| 229 | getContext()); |
| 230 | if(IDVal == "b" && Sym->isUndefined()) |
| 231 | return Error(Loc, "invalid reference to undefined symbol"); |
| 232 | EndLoc = Lexer.getLoc(); |
| 233 | Lex(); // Eat identifier. |
| 234 | } |
| 235 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 236 | return false; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 238 | case AsmToken::Dot: { |
| 239 | // This is a '.' reference, which references the current PC. Emit a |
| 240 | // temporary label to the streamer and refer to it. |
| 241 | MCSymbol *Sym = Ctx.CreateTempSymbol(); |
| 242 | Out.EmitLabel(Sym); |
| 243 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
| 244 | EndLoc = Lexer.getLoc(); |
| 245 | Lex(); // Eat identifier. |
| 246 | return false; |
| 247 | } |
| 248 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 249 | case AsmToken::LParen: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 250 | Lex(); // Eat the '('. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 251 | return ParseParenExpr(Res, EndLoc); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 252 | case AsmToken::Minus: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 253 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 254 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 255 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 256 | Res = MCUnaryExpr::CreateMinus(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 257 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 258 | case AsmToken::Plus: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 259 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 260 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 261 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 262 | Res = MCUnaryExpr::CreatePlus(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 263 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 264 | case AsmToken::Tilde: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 265 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 266 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 267 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 268 | Res = MCUnaryExpr::CreateNot(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 269 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 272 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 273 | bool AsmParser::ParseExpression(const MCExpr *&Res) { |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 274 | SMLoc EndLoc; |
| 275 | return ParseExpression(Res, EndLoc); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 278 | /// ParseExpression - Parse an expression and return it. |
| 279 | /// |
| 280 | /// expr ::= expr +,- expr -> lowest. |
| 281 | /// expr ::= expr |,^,&,! expr -> middle. |
| 282 | /// expr ::= expr *,/,%,<<,>> expr -> highest. |
| 283 | /// expr ::= primaryexpr |
| 284 | /// |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 285 | bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 286 | // Parse the expression. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 287 | Res = 0; |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 288 | if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc)) |
| 289 | return true; |
| 290 | |
| 291 | // Try to constant fold it up front, if possible. |
| 292 | int64_t Value; |
| 293 | if (Res->EvaluateAsAbsolute(Value)) |
| 294 | Res = MCConstantExpr::Create(Value, getContext()); |
| 295 | |
| 296 | return false; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 297 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 298 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 299 | bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 75f265f | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 300 | Res = 0; |
| 301 | return ParseParenExpr(Res, EndLoc) || |
| 302 | ParseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | c18274b | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 305 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 306 | const MCExpr *Expr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 307 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 308 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 309 | if (ParseExpression(Expr)) |
| 310 | return true; |
| 311 | |
Daniel Dunbar | e00b011 | 2009-10-16 01:57:52 +0000 | [diff] [blame] | 312 | if (!Expr->EvaluateAsAbsolute(Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 313 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 314 | |
| 315 | return false; |
| 316 | } |
| 317 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 318 | static unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 319 | MCBinaryExpr::Opcode &Kind) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 320 | switch (K) { |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 321 | default: |
| 322 | return 0; // not a binop. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 323 | |
| 324 | // Lowest Precedence: &&, || |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 325 | case AsmToken::AmpAmp: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 326 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 327 | return 1; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 328 | case AsmToken::PipePipe: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 329 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 330 | return 1; |
| 331 | |
| 332 | // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 333 | case AsmToken::Plus: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 334 | Kind = MCBinaryExpr::Add; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 335 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 336 | case AsmToken::Minus: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 337 | Kind = MCBinaryExpr::Sub; |
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::EqualEqual: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 340 | Kind = MCBinaryExpr::EQ; |
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::ExclaimEqual: |
| 343 | case AsmToken::LessGreater: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 344 | Kind = MCBinaryExpr::NE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 345 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 346 | case AsmToken::Less: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 347 | Kind = MCBinaryExpr::LT; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 348 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 349 | case AsmToken::LessEqual: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 350 | Kind = MCBinaryExpr::LTE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 351 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 352 | case AsmToken::Greater: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 353 | Kind = MCBinaryExpr::GT; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 354 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 355 | case AsmToken::GreaterEqual: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 356 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 357 | return 2; |
| 358 | |
| 359 | // Intermediate Precedence: |, &, ^ |
| 360 | // |
| 361 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 362 | case AsmToken::Pipe: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 363 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 364 | return 3; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 365 | case AsmToken::Caret: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 366 | Kind = MCBinaryExpr::Xor; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 367 | return 3; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 368 | case AsmToken::Amp: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 369 | Kind = MCBinaryExpr::And; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 370 | return 3; |
| 371 | |
| 372 | // Highest Precedence: *, /, %, <<, >> |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 373 | case AsmToken::Star: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 374 | Kind = MCBinaryExpr::Mul; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 375 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 376 | case AsmToken::Slash: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 377 | Kind = MCBinaryExpr::Div; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 378 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 379 | case AsmToken::Percent: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 380 | Kind = MCBinaryExpr::Mod; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 381 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 382 | case AsmToken::LessLess: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 383 | Kind = MCBinaryExpr::Shl; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 384 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 385 | case AsmToken::GreaterGreater: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 386 | Kind = MCBinaryExpr::Shr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 387 | return 4; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 393 | /// Res contains the LHS of the expression on input. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 394 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
| 395 | SMLoc &EndLoc) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 396 | while (1) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 397 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 398 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 399 | |
| 400 | // If the next token is lower precedence than we are allowed to eat, return |
| 401 | // successfully with what we ate already. |
| 402 | if (TokPrec < Precedence) |
| 403 | return false; |
| 404 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 405 | Lex(); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 406 | |
| 407 | // Eat the next primary expression. |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 408 | const MCExpr *RHS; |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 409 | if (ParsePrimaryExpr(RHS, EndLoc)) return true; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 410 | |
| 411 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 412 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 413 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 414 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 415 | if (TokPrec < NextTokPrec) { |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 416 | if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 419 | // Merge LHS and RHS according to operator. |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 420 | Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 424 | |
| 425 | |
| 426 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 427 | /// ParseStatement: |
| 428 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 429 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 430 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 431 | bool AsmParser::ParseStatement() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 432 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 01777ff | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 433 | Out.AddBlankLine(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 434 | Lex(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 435 | return false; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 436 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 437 | |
| 438 | // Statements always start with an identifier. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 439 | AsmToken ID = getTok(); |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 440 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 441 | StringRef IDVal; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 442 | int64_t LocalLabelVal = -1; |
| 443 | // GUESS allow an integer followed by a ':' as a directional local label |
| 444 | if (Lexer.is(AsmToken::Integer)) { |
| 445 | LocalLabelVal = getTok().getIntVal(); |
| 446 | if (LocalLabelVal < 0) { |
| 447 | if (!TheCondState.Ignore) |
| 448 | return TokError("unexpected token at start of statement"); |
| 449 | IDVal = ""; |
| 450 | } |
| 451 | else { |
| 452 | IDVal = getTok().getString(); |
| 453 | Lex(); // Consume the integer token to be used as an identifier token. |
| 454 | if (Lexer.getKind() != AsmToken::Colon) { |
| 455 | if (!TheCondState.Ignore) |
| 456 | return TokError("unexpected token at start of statement"); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | else if (ParseIdentifier(IDVal)) { |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 461 | if (!TheCondState.Ignore) |
| 462 | return TokError("unexpected token at start of statement"); |
| 463 | IDVal = ""; |
| 464 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 466 | // Handle conditional assembly here before checking for skipping. We |
| 467 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 468 | // example. |
| 469 | if (IDVal == ".if") |
| 470 | return ParseDirectiveIf(IDLoc); |
| 471 | if (IDVal == ".elseif") |
| 472 | return ParseDirectiveElseIf(IDLoc); |
| 473 | if (IDVal == ".else") |
| 474 | return ParseDirectiveElse(IDLoc); |
| 475 | if (IDVal == ".endif") |
| 476 | return ParseDirectiveEndIf(IDLoc); |
| 477 | |
| 478 | // If we are in a ".if 0" block, ignore this statement. |
| 479 | if (TheCondState.Ignore) { |
| 480 | EatToEndOfStatement(); |
| 481 | return false; |
| 482 | } |
| 483 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 484 | // FIXME: Recurse on local labels? |
| 485 | |
| 486 | // See what kind of statement we have. |
| 487 | switch (Lexer.getKind()) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 488 | case AsmToken::Colon: { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 489 | // identifier ':' -> Label. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 490 | Lex(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 491 | |
| 492 | // Diagnose attempt to use a variable as a label. |
| 493 | // |
| 494 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 495 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 496 | // implicitly marked as external. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 497 | MCSymbol *Sym; |
| 498 | if (LocalLabelVal == -1) |
| 499 | Sym = CreateSymbol(IDVal); |
| 500 | else |
| 501 | Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 502 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 503 | return Error(IDLoc, "invalid symbol redefinition"); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 504 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 505 | // Emit the label. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 506 | Out.EmitLabel(Sym); |
| 507 | |
Daniel Dunbar | 01777ff | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 508 | // Consume any end of statement token, if present, to avoid spurious |
| 509 | // AddBlankLine calls(). |
| 510 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 511 | Lex(); |
| 512 | if (Lexer.is(AsmToken::Eof)) |
| 513 | return false; |
| 514 | } |
| 515 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 516 | return ParseStatement(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 517 | } |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 518 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 519 | case AsmToken::Equal: |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 520 | // identifier '=' ... -> assignment statement |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 521 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 522 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 523 | return ParseAssignment(IDVal); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 524 | |
| 525 | default: // Normal instruction or directive. |
| 526 | break; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 530 | if (IDVal[0] == '.') { |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 531 | // FIXME: This should be driven based on a hash lookup and callback. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 532 | if (IDVal == ".section") |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 533 | return ParseDirectiveDarwinSection(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 534 | if (IDVal == ".text") |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 535 | // FIXME: This changes behavior based on the -static flag to the |
| 536 | // assembler. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 537 | return ParseDirectiveSectionSwitch("__TEXT", "__text", |
| 538 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 539 | if (IDVal == ".const") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 540 | return ParseDirectiveSectionSwitch("__TEXT", "__const"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 541 | if (IDVal == ".static_const") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 542 | return ParseDirectiveSectionSwitch("__TEXT", "__static_const"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 543 | if (IDVal == ".cstring") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 544 | return ParseDirectiveSectionSwitch("__TEXT","__cstring", |
| 545 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 546 | if (IDVal == ".literal4") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 547 | return ParseDirectiveSectionSwitch("__TEXT", "__literal4", |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 548 | MCSectionMachO::S_4BYTE_LITERALS, |
| 549 | 4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 550 | if (IDVal == ".literal8") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 551 | return ParseDirectiveSectionSwitch("__TEXT", "__literal8", |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 552 | MCSectionMachO::S_8BYTE_LITERALS, |
| 553 | 8); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 554 | if (IDVal == ".literal16") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 555 | return ParseDirectiveSectionSwitch("__TEXT","__literal16", |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 556 | MCSectionMachO::S_16BYTE_LITERALS, |
| 557 | 16); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 558 | if (IDVal == ".constructor") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 559 | return ParseDirectiveSectionSwitch("__TEXT","__constructor"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 560 | if (IDVal == ".destructor") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 561 | return ParseDirectiveSectionSwitch("__TEXT","__destructor"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 562 | if (IDVal == ".fvmlib_init0") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 563 | return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 564 | if (IDVal == ".fvmlib_init1") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 565 | return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1"); |
| 566 | |
| 567 | // FIXME: The assembler manual claims that this has the self modify code |
| 568 | // 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] | 569 | if (IDVal == ".symbol_stub") |
| 570 | return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub", |
| 571 | MCSectionMachO::S_SYMBOL_STUBS | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 572 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 573 | // FIXME: Different on PPC and ARM. |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 574 | 0, 16); |
| 575 | // FIXME: PowerPC only? |
| 576 | if (IDVal == ".picsymbol_stub") |
| 577 | return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub", |
| 578 | MCSectionMachO::S_SYMBOL_STUBS | |
| 579 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 580 | 0, 26); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 581 | if (IDVal == ".data") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 582 | return ParseDirectiveSectionSwitch("__DATA", "__data"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 583 | if (IDVal == ".static_data") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 584 | return ParseDirectiveSectionSwitch("__DATA", "__static_data"); |
| 585 | |
| 586 | // FIXME: The section names of these two are misspelled in the assembler |
| 587 | // manual. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 588 | if (IDVal == ".non_lazy_symbol_pointer") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 589 | return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr", |
| 590 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 591 | 4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 592 | if (IDVal == ".lazy_symbol_pointer") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 593 | return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr", |
| 594 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, |
| 595 | 4); |
| 596 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 597 | if (IDVal == ".dyld") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 598 | return ParseDirectiveSectionSwitch("__DATA", "__dyld"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 599 | if (IDVal == ".mod_init_func") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 600 | return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func", |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 601 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, |
| 602 | 4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 603 | if (IDVal == ".mod_term_func") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 604 | return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func", |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 605 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, |
| 606 | 4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 607 | if (IDVal == ".const_data") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 608 | return ParseDirectiveSectionSwitch("__DATA", "__const"); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 609 | |
| 610 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 611 | if (IDVal == ".objc_class") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 612 | return ParseDirectiveSectionSwitch("__OBJC", "__class", |
| 613 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 614 | if (IDVal == ".objc_meta_class") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 615 | return ParseDirectiveSectionSwitch("__OBJC", "__meta_class", |
| 616 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 617 | if (IDVal == ".objc_cat_cls_meth") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 618 | return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth", |
| 619 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 620 | if (IDVal == ".objc_cat_inst_meth") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 621 | return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth", |
| 622 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 623 | if (IDVal == ".objc_protocol") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 624 | return ParseDirectiveSectionSwitch("__OBJC", "__protocol", |
| 625 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 626 | if (IDVal == ".objc_string_object") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 627 | return ParseDirectiveSectionSwitch("__OBJC", "__string_object", |
| 628 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 629 | if (IDVal == ".objc_cls_meth") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 630 | return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth", |
| 631 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 632 | if (IDVal == ".objc_inst_meth") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 633 | return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth", |
| 634 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 635 | if (IDVal == ".objc_cls_refs") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 636 | return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs", |
| 637 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | |
| 638 | MCSectionMachO::S_LITERAL_POINTERS, |
| 639 | 4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 640 | if (IDVal == ".objc_message_refs") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 641 | return ParseDirectiveSectionSwitch("__OBJC", "__message_refs", |
| 642 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | |
| 643 | MCSectionMachO::S_LITERAL_POINTERS, |
| 644 | 4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 645 | if (IDVal == ".objc_symbols") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 646 | return ParseDirectiveSectionSwitch("__OBJC", "__symbols", |
| 647 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 648 | if (IDVal == ".objc_category") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 649 | return ParseDirectiveSectionSwitch("__OBJC", "__category", |
| 650 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 651 | if (IDVal == ".objc_class_vars") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 652 | return ParseDirectiveSectionSwitch("__OBJC", "__class_vars", |
| 653 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 654 | if (IDVal == ".objc_instance_vars") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 655 | return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars", |
| 656 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 657 | if (IDVal == ".objc_module_info") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 658 | return ParseDirectiveSectionSwitch("__OBJC", "__module_info", |
| 659 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 660 | if (IDVal == ".objc_class_names") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 661 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", |
| 662 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 663 | if (IDVal == ".objc_meth_var_types") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 664 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", |
| 665 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 666 | if (IDVal == ".objc_meth_var_names") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 667 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", |
| 668 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 669 | if (IDVal == ".objc_selector_strs") |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 670 | return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs", |
| 671 | MCSectionMachO::S_CSTRING_LITERALS); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 672 | |
Eric Christopher | c6177a4 | 2010-05-17 22:53:55 +0000 | [diff] [blame] | 673 | if (IDVal == ".tdata") |
| 674 | return ParseDirectiveSectionSwitch("__DATA", "__thread_data", |
| 675 | MCSectionMachO::S_THREAD_LOCAL_REGULAR); |
| 676 | if (IDVal == ".tlv") |
| 677 | return ParseDirectiveSectionSwitch("__DATA", "__thread_vars", |
| 678 | MCSectionMachO::S_THREAD_LOCAL_VARIABLES); |
| 679 | if (IDVal == ".thread_init_func") |
| 680 | return ParseDirectiveSectionSwitch("__DATA", "__thread_init", |
| 681 | MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS); |
| 682 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 683 | // Assembler features |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 684 | if (IDVal == ".set") |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 685 | return ParseDirectiveSet(); |
| 686 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 687 | // Data directives |
| 688 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 689 | if (IDVal == ".ascii") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 690 | return ParseDirectiveAscii(false); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 691 | if (IDVal == ".asciz") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 692 | return ParseDirectiveAscii(true); |
| 693 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 694 | if (IDVal == ".byte") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 695 | return ParseDirectiveValue(1); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 696 | if (IDVal == ".short") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 697 | return ParseDirectiveValue(2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 698 | if (IDVal == ".long") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 699 | return ParseDirectiveValue(4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 700 | if (IDVal == ".quad") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 701 | return ParseDirectiveValue(8); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 702 | |
| 703 | // FIXME: Target hooks for IsPow2. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 704 | if (IDVal == ".align") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 705 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 706 | if (IDVal == ".align32") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 707 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 708 | if (IDVal == ".balign") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 709 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 710 | if (IDVal == ".balignw") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 711 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 712 | if (IDVal == ".balignl") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 713 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 714 | if (IDVal == ".p2align") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 715 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 716 | if (IDVal == ".p2alignw") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 717 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 718 | if (IDVal == ".p2alignl") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 719 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 720 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 721 | if (IDVal == ".org") |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 722 | return ParseDirectiveOrg(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 723 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 724 | if (IDVal == ".fill") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 725 | return ParseDirectiveFill(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 726 | if (IDVal == ".space") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 727 | return ParseDirectiveSpace(); |
| 728 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 729 | // Symbol attribute directives |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 730 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 731 | if (IDVal == ".globl" || IDVal == ".global") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 732 | return ParseDirectiveSymbolAttribute(MCSA_Global); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 733 | if (IDVal == ".hidden") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 734 | return ParseDirectiveSymbolAttribute(MCSA_Hidden); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 735 | if (IDVal == ".indirect_symbol") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 736 | return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 737 | if (IDVal == ".internal") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 738 | return ParseDirectiveSymbolAttribute(MCSA_Internal); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 739 | if (IDVal == ".lazy_reference") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 740 | return ParseDirectiveSymbolAttribute(MCSA_LazyReference); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 741 | if (IDVal == ".no_dead_strip") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 742 | return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 743 | if (IDVal == ".private_extern") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 744 | return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 745 | if (IDVal == ".protected") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 746 | return ParseDirectiveSymbolAttribute(MCSA_Protected); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 747 | if (IDVal == ".reference") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 748 | return ParseDirectiveSymbolAttribute(MCSA_Reference); |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 749 | if (IDVal == ".type") |
| 750 | return ParseDirectiveELFType(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 751 | if (IDVal == ".weak") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 752 | return ParseDirectiveSymbolAttribute(MCSA_Weak); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 753 | if (IDVal == ".weak_definition") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 754 | return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 755 | if (IDVal == ".weak_reference") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 756 | return ParseDirectiveSymbolAttribute(MCSA_WeakReference); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 757 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 758 | if (IDVal == ".comm") |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 759 | return ParseDirectiveComm(/*IsLocal=*/false); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 760 | if (IDVal == ".lcomm") |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 761 | return ParseDirectiveComm(/*IsLocal=*/true); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 762 | if (IDVal == ".zerofill") |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 763 | return ParseDirectiveDarwinZerofill(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 764 | if (IDVal == ".desc") |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 765 | return ParseDirectiveDarwinSymbolDesc(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 766 | if (IDVal == ".lsym") |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 767 | return ParseDirectiveDarwinLsym(); |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 768 | if (IDVal == ".tbss") |
| 769 | return ParseDirectiveDarwinTBSS(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 770 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 771 | if (IDVal == ".subsections_via_symbols") |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 772 | return ParseDirectiveDarwinSubsectionsViaSymbols(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 773 | if (IDVal == ".abort") |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 774 | return ParseDirectiveAbort(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 775 | if (IDVal == ".include") |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 776 | return ParseDirectiveInclude(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 777 | if (IDVal == ".dump") |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 778 | return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 779 | if (IDVal == ".load") |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 780 | return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false); |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 781 | if (IDVal == ".secure_log_unique") |
| 782 | return ParseDirectiveDarwinSecureLogUnique(IDLoc); |
| 783 | if (IDVal == ".secure_log_reset") |
| 784 | return ParseDirectiveDarwinSecureLogReset(IDLoc); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 785 | |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 786 | // Look up the handler in the handler table, |
| 787 | bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal]; |
| 788 | if (Handler) |
| 789 | return (this->*Handler)(IDVal, IDLoc); |
| 790 | |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 791 | // Target hook for parsing target specific directives. |
| 792 | if (!getTargetParser().ParseDirective(ID)) |
| 793 | return false; |
| 794 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 795 | Warning(IDLoc, "ignoring directive for now"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 796 | EatToEndOfStatement(); |
| 797 | return false; |
| 798 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 799 | |
Chris Lattner | a7f1354 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 800 | // Canonicalize the opcode to lower case. |
| 801 | SmallString<128> Opcode; |
| 802 | for (unsigned i = 0, e = IDVal.size(); i != e; ++i) |
| 803 | Opcode.push_back(tolower(IDVal[i])); |
| 804 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 805 | SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; |
Chris Lattner | a7f1354 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 806 | bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc, |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 807 | ParsedOperands); |
| 808 | if (!HadError && Lexer.isNot(AsmToken::EndOfStatement)) |
| 809 | HadError = TokError("unexpected token in argument list"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 810 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 811 | // If parsing succeeded, match the instruction. |
| 812 | if (!HadError) { |
| 813 | MCInst Inst; |
| 814 | if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) { |
| 815 | // Emit the instruction on success. |
| 816 | Out.EmitInstruction(Inst); |
| 817 | } else { |
| 818 | // Otherwise emit a diagnostic about the match failure and set the error |
| 819 | // flag. |
| 820 | // |
| 821 | // FIXME: We should give nicer diagnostics about the exact failure. |
| 822 | Error(IDLoc, "unrecognized instruction"); |
| 823 | HadError = true; |
| 824 | } |
| 825 | } |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 826 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 827 | // If there was no error, consume the end-of-statement token. Otherwise this |
| 828 | // will be done by our caller. |
| 829 | if (!HadError) |
| 830 | Lex(); |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 831 | |
| 832 | // Free any parsed operands. |
| 833 | for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) |
| 834 | delete ParsedOperands[i]; |
| 835 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 836 | return HadError; |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 838 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 839 | bool AsmParser::ParseAssignment(const StringRef &Name) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 840 | // FIXME: Use better location, we should use proper tokens. |
| 841 | SMLoc EqualLoc = Lexer.getLoc(); |
| 842 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 843 | const MCExpr *Value; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 844 | if (ParseExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 845 | return true; |
| 846 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 847 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 848 | return TokError("unexpected token in assignment"); |
| 849 | |
| 850 | // Eat the end of statement marker. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 851 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 852 | |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 853 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 854 | // used as a symbol, or it is an absolute symbol). |
| 855 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 856 | if (Sym) { |
| 857 | // Diagnose assignment to a label. |
| 858 | // |
| 859 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 860 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 861 | if (Sym->isUndefined() && !Sym->isUsedInExpr()) |
| 862 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 863 | else if (!Sym->isUndefined() && !Sym->isAbsolute()) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 864 | return Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 865 | else if (!Sym->isVariable()) |
| 866 | return Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 867 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 868 | return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + |
| 869 | Name + "'"); |
| 870 | } else |
| 871 | Sym = CreateSymbol(Name); |
| 872 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 873 | // FIXME: Handle '.'. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 874 | |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 875 | Sym->setUsedInExpr(true); |
| 876 | |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 877 | // Do the assignment. |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 878 | Out.EmitAssignment(Sym, Value); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 879 | |
| 880 | return false; |
| 881 | } |
| 882 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 883 | /// ParseIdentifier: |
| 884 | /// ::= identifier |
| 885 | /// ::= string |
| 886 | bool AsmParser::ParseIdentifier(StringRef &Res) { |
| 887 | if (Lexer.isNot(AsmToken::Identifier) && |
| 888 | Lexer.isNot(AsmToken::String)) |
| 889 | return true; |
| 890 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 891 | Res = getTok().getIdentifier(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 892 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 893 | Lex(); // Consume the identifier token. |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 894 | |
| 895 | return false; |
| 896 | } |
| 897 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 898 | /// ParseDirectiveSet: |
| 899 | /// ::= .set identifier ',' expression |
| 900 | bool AsmParser::ParseDirectiveSet() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 901 | StringRef Name; |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 903 | if (ParseIdentifier(Name)) |
| 904 | return TokError("expected identifier after '.set' directive"); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 905 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 906 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 907 | return TokError("unexpected token in '.set'"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 908 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 909 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 910 | return ParseAssignment(Name); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 913 | /// ParseDirectiveSection: |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 914 | /// ::= .section identifier (',' identifier)* |
| 915 | /// FIXME: This should actually parse out the segment, section, attributes and |
| 916 | /// sizeof_stub fields. |
| 917 | bool AsmParser::ParseDirectiveDarwinSection() { |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 918 | SMLoc Loc = Lexer.getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 919 | |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 920 | StringRef SectionName; |
| 921 | if (ParseIdentifier(SectionName)) |
| 922 | return Error(Loc, "expected identifier after '.section' directive"); |
| 923 | |
| 924 | // Verify there is a following comma. |
| 925 | if (!Lexer.is(AsmToken::Comma)) |
| 926 | return TokError("unexpected token in '.section' directive"); |
| 927 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 928 | std::string SectionSpec = SectionName; |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 929 | SectionSpec += ","; |
| 930 | |
| 931 | // Add all the tokens until the end of the line, ParseSectionSpecifier will |
| 932 | // handle this. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 933 | StringRef EOL = Lexer.LexUntilEndOfStatement(); |
| 934 | SectionSpec.append(EOL.begin(), EOL.end()); |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 935 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 936 | Lex(); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 937 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 938 | return TokError("unexpected token in '.section' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 939 | Lex(); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 940 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 941 | |
| 942 | StringRef Segment, Section; |
| 943 | unsigned TAA, StubSize; |
| 944 | std::string ErrorStr = |
| 945 | MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section, |
| 946 | TAA, StubSize); |
| 947 | |
| 948 | if (!ErrorStr.empty()) |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 949 | return Error(Loc, ErrorStr.c_str()); |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 951 | // FIXME: Arch specific. |
Chris Lattner | f60e9bb | 2010-02-26 18:32:26 +0000 | [diff] [blame] | 952 | bool isText = Segment == "__TEXT"; // FIXME: Hack. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 953 | Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize, |
| 954 | isText ? SectionKind::getText() |
| 955 | : SectionKind::getDataRel())); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 956 | return false; |
| 957 | } |
| 958 | |
Chris Lattner | e15c2d7 | 2009-08-10 18:05:55 +0000 | [diff] [blame] | 959 | /// ParseDirectiveSectionSwitch - |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 960 | bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment, |
| 961 | const char *Section, |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 962 | unsigned TAA, unsigned Align, |
| 963 | unsigned StubSize) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 964 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 965 | return TokError("unexpected token in section switching directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 966 | Lex(); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 968 | // FIXME: Arch specific. |
Chris Lattner | f60e9bb | 2010-02-26 18:32:26 +0000 | [diff] [blame] | 969 | bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 970 | Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize, |
| 971 | isText ? SectionKind::getText() |
| 972 | : SectionKind::getDataRel())); |
Daniel Dunbar | 2330df6 | 2009-08-21 23:30:15 +0000 | [diff] [blame] | 973 | |
| 974 | // Set the implicit alignment, if any. |
| 975 | // |
| 976 | // FIXME: This isn't really what 'as' does; I think it just uses the implicit |
| 977 | // alignment on the section (e.g., if one manually inserts bytes into the |
| 978 | // section, then just issueing the section switch directive will not realign |
| 979 | // the section. However, this is arguably more reasonable behavior, and there |
| 980 | // is no good reason for someone to intentionally emit incorrectly sized |
| 981 | // values into the implicitly aligned sections. |
| 982 | if (Align) |
| 983 | Out.EmitValueToAlignment(Align, 0, 1, 0); |
| 984 | |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 985 | return false; |
| 986 | } |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 987 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 988 | bool AsmParser::ParseEscapedString(std::string &Data) { |
| 989 | assert(Lexer.is(AsmToken::String) && "Unexpected current token!"); |
| 990 | |
| 991 | Data = ""; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 992 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 993 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 994 | if (Str[i] != '\\') { |
| 995 | Data += Str[i]; |
| 996 | continue; |
| 997 | } |
| 998 | |
| 999 | // Recognize escaped characters. Note that this escape semantics currently |
| 1000 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 1001 | ++i; |
| 1002 | if (i == e) |
| 1003 | return TokError("unexpected backslash at end of string"); |
| 1004 | |
| 1005 | // Recognize octal sequences. |
| 1006 | if ((unsigned) (Str[i] - '0') <= 7) { |
| 1007 | // Consume up to three octal characters. |
| 1008 | unsigned Value = Str[i] - '0'; |
| 1009 | |
| 1010 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 1011 | ++i; |
| 1012 | Value = Value * 8 + (Str[i] - '0'); |
| 1013 | |
| 1014 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 1015 | ++i; |
| 1016 | Value = Value * 8 + (Str[i] - '0'); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | if (Value > 255) |
| 1021 | return TokError("invalid octal escape sequence (out of range)"); |
| 1022 | |
| 1023 | Data += (unsigned char) Value; |
| 1024 | continue; |
| 1025 | } |
| 1026 | |
| 1027 | // Otherwise recognize individual escapes. |
| 1028 | switch (Str[i]) { |
| 1029 | default: |
| 1030 | // Just reject invalid escape sequences for now. |
| 1031 | return TokError("invalid escape sequence (unrecognized character)"); |
| 1032 | |
| 1033 | case 'b': Data += '\b'; break; |
| 1034 | case 'f': Data += '\f'; break; |
| 1035 | case 'n': Data += '\n'; break; |
| 1036 | case 'r': Data += '\r'; break; |
| 1037 | case 't': Data += '\t'; break; |
| 1038 | case '"': Data += '"'; break; |
| 1039 | case '\\': Data += '\\'; break; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1046 | /// ParseDirectiveAscii: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1047 | /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1048 | bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1049 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1050 | for (;;) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1051 | if (Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1052 | return TokError("expected string in '.ascii' or '.asciz' directive"); |
| 1053 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1054 | std::string Data; |
| 1055 | if (ParseEscapedString(Data)) |
| 1056 | return true; |
| 1057 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 1058 | Out.EmitBytes(Data, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1059 | if (ZeroTerminated) |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 1060 | Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1061 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1062 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1063 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1064 | if (Lexer.is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1067 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1068 | return TokError("unexpected token in '.ascii' or '.asciz' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1069 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1073 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | /// ParseDirectiveValue |
| 1078 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 1079 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1080 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1081 | for (;;) { |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1082 | const MCExpr *Value; |
Bill Wendling | 9bc0af8 | 2009-12-28 01:34:57 +0000 | [diff] [blame] | 1083 | SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1084 | if (ParseExpression(Value)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1085 | return true; |
| 1086 | |
Daniel Dunbar | 414c0c4 | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 1087 | // Special case constant expressions to match code generator. |
| 1088 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) |
| 1089 | Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE); |
| 1090 | else |
| 1091 | Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1092 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1093 | if (Lexer.is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1094 | break; |
| 1095 | |
| 1096 | // FIXME: Improve diagnostic. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1097 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1098 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1099 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1103 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1104 | return false; |
| 1105 | } |
| 1106 | |
| 1107 | /// ParseDirectiveSpace |
| 1108 | /// ::= .space expression [ , expression ] |
| 1109 | bool AsmParser::ParseDirectiveSpace() { |
| 1110 | int64_t NumBytes; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1111 | if (ParseAbsoluteExpression(NumBytes)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1112 | return true; |
| 1113 | |
| 1114 | int64_t FillExpr = 0; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1115 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1116 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1117 | return TokError("unexpected token in '.space' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1118 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1119 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1120 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1121 | return true; |
| 1122 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1123 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1124 | return TokError("unexpected token in '.space' directive"); |
| 1125 | } |
| 1126 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1127 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1128 | |
| 1129 | if (NumBytes <= 0) |
| 1130 | return TokError("invalid number of bytes in '.space' directive"); |
| 1131 | |
| 1132 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 1133 | Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1134 | |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | /// ParseDirectiveFill |
| 1139 | /// ::= .fill expression , expression , expression |
| 1140 | bool AsmParser::ParseDirectiveFill() { |
| 1141 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1142 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1143 | return true; |
| 1144 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1145 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1146 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1147 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1148 | |
| 1149 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1150 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1151 | return true; |
| 1152 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1153 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1154 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1155 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1156 | |
| 1157 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1158 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1159 | return true; |
| 1160 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1161 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1162 | return TokError("unexpected token in '.fill' directive"); |
| 1163 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1164 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1165 | |
Daniel Dunbar | bc38ca7 | 2009-08-21 15:43:35 +0000 | [diff] [blame] | 1166 | if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) |
| 1167 | return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1168 | |
| 1169 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
Daniel Dunbar | 414c0c4 | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 1170 | Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1171 | |
| 1172 | return false; |
| 1173 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1174 | |
| 1175 | /// ParseDirectiveOrg |
| 1176 | /// ::= .org expression [ , expression ] |
| 1177 | bool AsmParser::ParseDirectiveOrg() { |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1178 | const MCExpr *Offset; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1179 | if (ParseExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1180 | return true; |
| 1181 | |
| 1182 | // Parse optional fill expression. |
| 1183 | int64_t FillExpr = 0; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1184 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1185 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1186 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1187 | Lex(); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1188 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1189 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1190 | return true; |
| 1191 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1192 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1193 | return TokError("unexpected token in '.org' directive"); |
| 1194 | } |
| 1195 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1196 | Lex(); |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1197 | |
| 1198 | // FIXME: Only limited forms of relocatable expressions are accepted here, it |
| 1199 | // has to be relative to the current section. |
| 1200 | Out.EmitValueToOffset(Offset, FillExpr); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1201 | |
| 1202 | return false; |
| 1203 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1204 | |
| 1205 | /// ParseDirectiveAlign |
| 1206 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
| 1207 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1208 | SMLoc AlignmentLoc = Lexer.getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1209 | int64_t Alignment; |
| 1210 | if (ParseAbsoluteExpression(Alignment)) |
| 1211 | return true; |
| 1212 | |
| 1213 | SMLoc MaxBytesLoc; |
| 1214 | bool HasFillExpr = false; |
| 1215 | int64_t FillExpr = 0; |
| 1216 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1217 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1218 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1219 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1220 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1221 | |
| 1222 | // The fill expression can be omitted while specifying a maximum number of |
| 1223 | // alignment bytes, e.g: |
| 1224 | // .align 3,,4 |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1225 | if (Lexer.isNot(AsmToken::Comma)) { |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1226 | HasFillExpr = true; |
| 1227 | if (ParseAbsoluteExpression(FillExpr)) |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1231 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1232 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1233 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1234 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1235 | |
| 1236 | MaxBytesLoc = Lexer.getLoc(); |
| 1237 | if (ParseAbsoluteExpression(MaxBytesToFill)) |
| 1238 | return true; |
| 1239 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1240 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1241 | return TokError("unexpected token in directive"); |
| 1242 | } |
| 1243 | } |
| 1244 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1245 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1246 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1247 | if (!HasFillExpr) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1248 | FillExpr = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Compute alignment in bytes. |
| 1251 | if (IsPow2) { |
| 1252 | // FIXME: Diagnose overflow. |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1253 | if (Alignment >= 32) { |
| 1254 | Error(AlignmentLoc, "invalid alignment value"); |
| 1255 | Alignment = 31; |
| 1256 | } |
| 1257 | |
Benjamin Kramer | 12fd767 | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 1258 | Alignment = 1ULL << Alignment; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1261 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1262 | if (MaxBytesLoc.isValid()) { |
| 1263 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1264 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
| 1265 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 0afb9f5 | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 1266 | MaxBytesToFill = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 1270 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
| 1271 | "has no effect"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1272 | MaxBytesToFill = 0; |
| 1273 | } |
| 1274 | } |
| 1275 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1276 | // Check whether we should use optimal code alignment for this .align |
| 1277 | // directive. |
| 1278 | // |
| 1279 | // FIXME: This should be using a target hook. |
| 1280 | bool UseCodeAlign = false; |
| 1281 | if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>( |
| 1282 | Out.getCurrentSection())) |
| 1283 | UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); |
| 1284 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 1285 | ValueSize == 1 && UseCodeAlign) { |
Kevin Enderby | d74acb0 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 1286 | Out.EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1287 | } else { |
Kevin Enderby | d74acb0 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 1288 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
| 1289 | Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1290 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1291 | |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1295 | /// ParseDirectiveSymbolAttribute |
| 1296 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1297 | bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1298 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1299 | for (;;) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1300 | StringRef Name; |
| 1301 | |
| 1302 | if (ParseIdentifier(Name)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1303 | return TokError("expected identifier in directive"); |
| 1304 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1305 | MCSymbol *Sym = CreateSymbol(Name); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1306 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1307 | Out.EmitSymbolAttribute(Sym, Attr); |
| 1308 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1309 | if (Lexer.is(AsmToken::EndOfStatement)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1310 | break; |
| 1311 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1312 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1313 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1314 | Lex(); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1318 | Lex(); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1321 | |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1322 | /// ParseDirectiveELFType |
| 1323 | /// ::= .type identifier , @attribute |
| 1324 | bool AsmParser::ParseDirectiveELFType() { |
| 1325 | StringRef Name; |
| 1326 | if (ParseIdentifier(Name)) |
| 1327 | return TokError("expected identifier in directive"); |
| 1328 | |
| 1329 | // Handle the identifier as the key symbol. |
| 1330 | MCSymbol *Sym = CreateSymbol(Name); |
| 1331 | |
| 1332 | if (Lexer.isNot(AsmToken::Comma)) |
| 1333 | return TokError("unexpected token in '.type' directive"); |
| 1334 | Lex(); |
| 1335 | |
| 1336 | if (Lexer.isNot(AsmToken::At)) |
| 1337 | return TokError("expected '@' before type"); |
| 1338 | Lex(); |
| 1339 | |
| 1340 | StringRef Type; |
| 1341 | SMLoc TypeLoc; |
| 1342 | |
| 1343 | TypeLoc = Lexer.getLoc(); |
| 1344 | if (ParseIdentifier(Type)) |
| 1345 | return TokError("expected symbol type in directive"); |
| 1346 | |
| 1347 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type) |
| 1348 | .Case("function", MCSA_ELF_TypeFunction) |
| 1349 | .Case("object", MCSA_ELF_TypeObject) |
| 1350 | .Case("tls_object", MCSA_ELF_TypeTLS) |
| 1351 | .Case("common", MCSA_ELF_TypeCommon) |
| 1352 | .Case("notype", MCSA_ELF_TypeNoType) |
| 1353 | .Default(MCSA_Invalid); |
| 1354 | |
| 1355 | if (Attr == MCSA_Invalid) |
| 1356 | return Error(TypeLoc, "unsupported attribute in '.type' directive"); |
| 1357 | |
| 1358 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1359 | return TokError("unexpected token in '.type' directive"); |
| 1360 | |
| 1361 | Lex(); |
| 1362 | |
| 1363 | Out.EmitSymbolAttribute(Sym, Attr); |
| 1364 | |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1368 | /// ParseDirectiveDarwinSymbolDesc |
| 1369 | /// ::= .desc identifier , expression |
| 1370 | bool AsmParser::ParseDirectiveDarwinSymbolDesc() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1371 | StringRef Name; |
| 1372 | if (ParseIdentifier(Name)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1373 | return TokError("expected identifier in directive"); |
| 1374 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1375 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1376 | MCSymbol *Sym = CreateSymbol(Name); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1377 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1378 | if (Lexer.isNot(AsmToken::Comma)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1379 | return TokError("unexpected token in '.desc' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1380 | Lex(); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1381 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1382 | int64_t DescValue; |
| 1383 | if (ParseAbsoluteExpression(DescValue)) |
| 1384 | return true; |
| 1385 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1386 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1387 | return TokError("unexpected token in '.desc' directive"); |
| 1388 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1389 | Lex(); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Set the n_desc field of this Symbol to this DescValue |
| 1392 | Out.EmitSymbolDesc(Sym, DescValue); |
| 1393 | |
| 1394 | return false; |
| 1395 | } |
| 1396 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1397 | /// ParseDirectiveComm |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1398 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
| 1399 | bool AsmParser::ParseDirectiveComm(bool IsLocal) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1400 | SMLoc IDLoc = Lexer.getLoc(); |
| 1401 | StringRef Name; |
| 1402 | if (ParseIdentifier(Name)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1403 | return TokError("expected identifier in directive"); |
| 1404 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1405 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1406 | MCSymbol *Sym = CreateSymbol(Name); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1407 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1408 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1409 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1410 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1411 | |
| 1412 | int64_t Size; |
| 1413 | SMLoc SizeLoc = Lexer.getLoc(); |
| 1414 | if (ParseAbsoluteExpression(Size)) |
| 1415 | return true; |
| 1416 | |
| 1417 | int64_t Pow2Alignment = 0; |
| 1418 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1419 | if (Lexer.is(AsmToken::Comma)) { |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1420 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1421 | Pow2AlignmentLoc = Lexer.getLoc(); |
| 1422 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 1423 | return true; |
Chris Lattner | 258281d | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 1424 | |
| 1425 | // If this target takes alignments in bytes (not log) validate and convert. |
| 1426 | if (Lexer.getMAI().getAlignmentIsInBytes()) { |
| 1427 | if (!isPowerOf2_64(Pow2Alignment)) |
| 1428 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 1429 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 1430 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1433 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1434 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1435 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1436 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1437 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1438 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 1439 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1440 | if (Size < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1441 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
| 1442 | "be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1443 | |
Eric Christopher | c260a3e | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 1444 | // NOTE: The alignment in the directive is a power of 2 value, the assembler |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1445 | // may internally end up wanting an alignment in bytes. |
| 1446 | // FIXME: Diagnose overflow. |
| 1447 | if (Pow2Alignment < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1448 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
| 1449 | "alignment, can't be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1450 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 1451 | if (!Sym->isUndefined()) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1452 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1453 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1454 | // '.lcomm' is equivalent to '.zerofill'. |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1455 | // 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] | 1456 | if (IsLocal) { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 1457 | Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss", |
| 1458 | MCSectionMachO::S_ZEROFILL, 0, |
| 1459 | SectionKind::getBSS()), |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1460 | Sym, Size, 1 << Pow2Alignment); |
| 1461 | return false; |
| 1462 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1463 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1464 | Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1465 | return false; |
| 1466 | } |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1467 | |
| 1468 | /// ParseDirectiveDarwinZerofill |
| 1469 | /// ::= .zerofill segname , sectname [, identifier , size_expression [ |
| 1470 | /// , align_expression ]] |
| 1471 | bool AsmParser::ParseDirectiveDarwinZerofill() { |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1472 | StringRef Segment; |
| 1473 | if (ParseIdentifier(Segment)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1474 | return TokError("expected segment name after '.zerofill' directive"); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1475 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1476 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1477 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1478 | Lex(); |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1479 | |
| 1480 | StringRef Section; |
| 1481 | if (ParseIdentifier(Section)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1482 | return TokError("expected section name after comma in '.zerofill' " |
| 1483 | "directive"); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1485 | // If this is the end of the line all that was wanted was to create the |
| 1486 | // the section but with no symbol. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1487 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1488 | // Create the zerofill section but no symbol |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 1489 | Out.EmitZerofill(Ctx.getMachOSection(Segment, Section, |
| 1490 | MCSectionMachO::S_ZEROFILL, 0, |
| 1491 | SectionKind::getBSS())); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1492 | return false; |
| 1493 | } |
| 1494 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1495 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1496 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1497 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1499 | SMLoc IDLoc = Lexer.getLoc(); |
| 1500 | StringRef IDStr; |
| 1501 | if (ParseIdentifier(IDStr)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1502 | return TokError("expected identifier in directive"); |
| 1503 | |
| 1504 | // handle the identifier as the key symbol. |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1505 | MCSymbol *Sym = CreateSymbol(IDStr); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1506 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1507 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1508 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1509 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1510 | |
| 1511 | int64_t Size; |
| 1512 | SMLoc SizeLoc = Lexer.getLoc(); |
| 1513 | if (ParseAbsoluteExpression(Size)) |
| 1514 | return true; |
| 1515 | |
| 1516 | int64_t Pow2Alignment = 0; |
| 1517 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1518 | if (Lexer.is(AsmToken::Comma)) { |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1519 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1520 | Pow2AlignmentLoc = Lexer.getLoc(); |
| 1521 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 1522 | return true; |
| 1523 | } |
| 1524 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1525 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1526 | return TokError("unexpected token in '.zerofill' directive"); |
| 1527 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1528 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1529 | |
| 1530 | if (Size < 0) |
| 1531 | return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less " |
| 1532 | "than zero"); |
| 1533 | |
Eric Christopher | c260a3e | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 1534 | // NOTE: The alignment in the directive is a power of 2 value, the assembler |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1535 | // may internally end up wanting an alignment in bytes. |
| 1536 | // FIXME: Diagnose overflow. |
| 1537 | if (Pow2Alignment < 0) |
| 1538 | return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, " |
| 1539 | "can't be less than zero"); |
| 1540 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 1541 | if (!Sym->isUndefined()) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1542 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1543 | |
Daniel Dunbar | bdee6df | 2009-08-27 23:58:10 +0000 | [diff] [blame] | 1544 | // Create the zerofill Symbol with Size and Pow2Alignment |
Daniel Dunbar | 2e15292 | 2009-08-28 05:48:29 +0000 | [diff] [blame] | 1545 | // |
| 1546 | // FIXME: Arch specific. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 1547 | Out.EmitZerofill(Ctx.getMachOSection(Segment, Section, |
| 1548 | MCSectionMachO::S_ZEROFILL, 0, |
| 1549 | SectionKind::getBSS()), |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1550 | Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1551 | |
| 1552 | return false; |
| 1553 | } |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1554 | |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1555 | /// ParseDirectiveDarwinTBSS |
| 1556 | /// ::= .tbss identifier, size, align |
| 1557 | bool AsmParser::ParseDirectiveDarwinTBSS() { |
| 1558 | SMLoc IDLoc = Lexer.getLoc(); |
| 1559 | StringRef Name; |
| 1560 | if (ParseIdentifier(Name)) |
| 1561 | return TokError("expected identifier in directive"); |
Eric Christopher | d04d98d | 2010-05-17 02:13:02 +0000 | [diff] [blame] | 1562 | |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1563 | // Handle the identifier as the key symbol. |
Eric Christopher | d04d98d | 2010-05-17 02:13:02 +0000 | [diff] [blame] | 1564 | MCSymbol *Sym = CreateSymbol(Name); |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1565 | |
| 1566 | if (Lexer.isNot(AsmToken::Comma)) |
| 1567 | return TokError("unexpected token in directive"); |
| 1568 | Lex(); |
| 1569 | |
| 1570 | int64_t Size; |
| 1571 | SMLoc SizeLoc = Lexer.getLoc(); |
| 1572 | if (ParseAbsoluteExpression(Size)) |
| 1573 | return true; |
| 1574 | |
| 1575 | int64_t Pow2Alignment = 0; |
| 1576 | SMLoc Pow2AlignmentLoc; |
| 1577 | if (Lexer.is(AsmToken::Comma)) { |
| 1578 | Lex(); |
| 1579 | Pow2AlignmentLoc = Lexer.getLoc(); |
| 1580 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 1581 | return true; |
| 1582 | } |
| 1583 | |
| 1584 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1585 | return TokError("unexpected token in '.tbss' directive"); |
| 1586 | |
| 1587 | Lex(); |
| 1588 | |
| 1589 | if (Size < 0) |
| 1590 | return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than" |
| 1591 | "zero"); |
| 1592 | |
| 1593 | // FIXME: Diagnose overflow. |
| 1594 | if (Pow2Alignment < 0) |
| 1595 | return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less" |
| 1596 | "than zero"); |
| 1597 | |
| 1598 | if (!Sym->isUndefined()) |
| 1599 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1600 | |
Eric Christopher | 4d01cbe | 2010-05-18 21:16:04 +0000 | [diff] [blame] | 1601 | Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss", |
| 1602 | MCSectionMachO::S_THREAD_LOCAL_ZEROFILL, |
| 1603 | 0, SectionKind::getThreadBSS()), |
| 1604 | Sym, Size, 1 << Pow2Alignment); |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1605 | |
| 1606 | return false; |
| 1607 | } |
| 1608 | |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1609 | /// ParseDirectiveDarwinSubsectionsViaSymbols |
| 1610 | /// ::= .subsections_via_symbols |
| 1611 | bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1612 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1613 | return TokError("unexpected token in '.subsections_via_symbols' directive"); |
| 1614 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1615 | Lex(); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1617 | Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1618 | |
| 1619 | return false; |
| 1620 | } |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1621 | |
| 1622 | /// ParseDirectiveAbort |
| 1623 | /// ::= .abort [ "abort_string" ] |
| 1624 | bool AsmParser::ParseDirectiveAbort() { |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1625 | // FIXME: Use loc from directive. |
| 1626 | SMLoc Loc = Lexer.getLoc(); |
| 1627 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1628 | StringRef Str = ""; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1629 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1630 | if (Lexer.isNot(AsmToken::String)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1631 | return TokError("expected string in '.abort' directive"); |
| 1632 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1633 | Str = getTok().getString(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1634 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1635 | Lex(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1638 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1639 | return TokError("unexpected token in '.abort' directive"); |
| 1640 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1641 | Lex(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1642 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1643 | // FIXME: Handle here. |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1644 | if (Str.empty()) |
| 1645 | Error(Loc, ".abort detected. Assembly stopping."); |
| 1646 | else |
| 1647 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1648 | |
| 1649 | return false; |
| 1650 | } |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1651 | |
| 1652 | /// ParseDirectiveLsym |
| 1653 | /// ::= .lsym identifier , expression |
| 1654 | bool AsmParser::ParseDirectiveDarwinLsym() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1655 | StringRef Name; |
| 1656 | if (ParseIdentifier(Name)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1657 | return TokError("expected identifier in directive"); |
| 1658 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1659 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1660 | MCSymbol *Sym = CreateSymbol(Name); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1661 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1662 | if (Lexer.isNot(AsmToken::Comma)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1663 | return TokError("unexpected token in '.lsym' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1664 | Lex(); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1665 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1666 | const MCExpr *Value; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1667 | if (ParseExpression(Value)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1668 | return true; |
| 1669 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1670 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1671 | return TokError("unexpected token in '.lsym' directive"); |
| 1672 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1673 | Lex(); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1674 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1675 | // We don't currently support this directive. |
| 1676 | // |
| 1677 | // FIXME: Diagnostic location! |
| 1678 | (void) Sym; |
| 1679 | return TokError("directive '.lsym' is unsupported"); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1680 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1681 | |
| 1682 | /// ParseDirectiveInclude |
| 1683 | /// ::= .include "filename" |
| 1684 | bool AsmParser::ParseDirectiveInclude() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1685 | if (Lexer.isNot(AsmToken::String)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1686 | return TokError("expected string in '.include' directive"); |
| 1687 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1688 | std::string Filename = getTok().getString(); |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1689 | SMLoc IncludeLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1690 | Lex(); |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1691 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1692 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1693 | return TokError("unexpected token in '.include' directive"); |
| 1694 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1695 | // Strip the quotes. |
| 1696 | Filename = Filename.substr(1, Filename.size()-2); |
| 1697 | |
| 1698 | // Attempt to switch the lexer to the included file before consuming the end |
| 1699 | // of statement to avoid losing it when we switch. |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 1700 | if (EnterIncludeFile(Filename)) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 1701 | PrintMessage(IncludeLoc, |
| 1702 | "Could not find include file '" + Filename + "'", |
| 1703 | "error"); |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1704 | return true; |
| 1705 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1706 | |
| 1707 | return false; |
| 1708 | } |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1709 | |
| 1710 | /// ParseDirectiveDarwinDumpOrLoad |
| 1711 | /// ::= ( .dump | .load ) "filename" |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1712 | bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1713 | if (Lexer.isNot(AsmToken::String)) |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1714 | return TokError("expected string in '.dump' or '.load' directive"); |
| 1715 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1716 | Lex(); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1717 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1718 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1719 | return TokError("unexpected token in '.dump' or '.load' directive"); |
| 1720 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1721 | Lex(); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1722 | |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1723 | // FIXME: If/when .dump and .load are implemented they will be done in the |
| 1724 | // the assembly parser and not have any need for an MCStreamer API. |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1725 | if (IsDump) |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1726 | Warning(IDLoc, "ignoring directive .dump for now"); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1727 | else |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1728 | Warning(IDLoc, "ignoring directive .load for now"); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1729 | |
| 1730 | return false; |
| 1731 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1732 | |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 1733 | /// ParseDirectiveDarwinSecureLogUnique |
| 1734 | /// ::= .secure_log_unique "log message" |
| 1735 | bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) { |
| 1736 | std::string LogMessage; |
| 1737 | |
| 1738 | if (Lexer.isNot(AsmToken::String)) |
| 1739 | LogMessage = ""; |
| 1740 | else{ |
| 1741 | LogMessage = getTok().getString(); |
| 1742 | Lex(); |
| 1743 | } |
| 1744 | |
| 1745 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1746 | return TokError("unexpected token in '.secure_log_unique' directive"); |
| 1747 | |
| 1748 | if (getContext().getSecureLogUsed() != false) |
| 1749 | return Error(IDLoc, ".secure_log_unique specified multiple times"); |
| 1750 | |
| 1751 | char *SecureLogFile = getContext().getSecureLogFile(); |
| 1752 | if (SecureLogFile == NULL) |
| 1753 | return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE " |
| 1754 | "environment variable unset."); |
| 1755 | |
| 1756 | raw_ostream *OS = getContext().getSecureLog(); |
| 1757 | if (OS == NULL) { |
| 1758 | std::string Err; |
| 1759 | OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append); |
| 1760 | if (!Err.empty()) { |
| 1761 | delete OS; |
| 1762 | return Error(IDLoc, Twine("can't open secure log file: ") + |
| 1763 | SecureLogFile + " (" + Err + ")"); |
| 1764 | } |
| 1765 | getContext().setSecureLog(OS); |
| 1766 | } |
| 1767 | |
| 1768 | int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc); |
| 1769 | *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":" |
| 1770 | << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":" |
| 1771 | << LogMessage + "\n"; |
| 1772 | |
| 1773 | getContext().setSecureLogUsed(true); |
| 1774 | |
| 1775 | return false; |
| 1776 | } |
| 1777 | |
| 1778 | /// ParseDirectiveDarwinSecureLogReset |
| 1779 | /// ::= .secure_log_reset |
| 1780 | bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) { |
| 1781 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1782 | return TokError("unexpected token in '.secure_log_reset' directive"); |
| 1783 | |
| 1784 | Lex(); |
| 1785 | |
| 1786 | getContext().setSecureLogUsed(false); |
| 1787 | |
| 1788 | return false; |
| 1789 | } |
| 1790 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1791 | /// ParseDirectiveIf |
| 1792 | /// ::= .if expression |
| 1793 | bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1794 | TheCondStack.push_back(TheCondState); |
| 1795 | TheCondState.TheCond = AsmCond::IfCond; |
| 1796 | if(TheCondState.Ignore) { |
| 1797 | EatToEndOfStatement(); |
| 1798 | } |
| 1799 | else { |
| 1800 | int64_t ExprValue; |
| 1801 | if (ParseAbsoluteExpression(ExprValue)) |
| 1802 | return true; |
| 1803 | |
| 1804 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1805 | return TokError("unexpected token in '.if' directive"); |
| 1806 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1807 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1808 | |
| 1809 | TheCondState.CondMet = ExprValue; |
| 1810 | TheCondState.Ignore = !TheCondState.CondMet; |
| 1811 | } |
| 1812 | |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | /// ParseDirectiveElseIf |
| 1817 | /// ::= .elseif expression |
| 1818 | bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { |
| 1819 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 1820 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 1821 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 1822 | " an .elseif"); |
| 1823 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 1824 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1825 | bool LastIgnoreState = false; |
| 1826 | if (!TheCondStack.empty()) |
| 1827 | LastIgnoreState = TheCondStack.back().Ignore; |
| 1828 | if (LastIgnoreState || TheCondState.CondMet) { |
| 1829 | TheCondState.Ignore = true; |
| 1830 | EatToEndOfStatement(); |
| 1831 | } |
| 1832 | else { |
| 1833 | int64_t ExprValue; |
| 1834 | if (ParseAbsoluteExpression(ExprValue)) |
| 1835 | return true; |
| 1836 | |
| 1837 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1838 | return TokError("unexpected token in '.elseif' directive"); |
| 1839 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1840 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1841 | TheCondState.CondMet = ExprValue; |
| 1842 | TheCondState.Ignore = !TheCondState.CondMet; |
| 1843 | } |
| 1844 | |
| 1845 | return false; |
| 1846 | } |
| 1847 | |
| 1848 | /// ParseDirectiveElse |
| 1849 | /// ::= .else |
| 1850 | bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1851 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1852 | return TokError("unexpected token in '.else' directive"); |
| 1853 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1854 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1855 | |
| 1856 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 1857 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 1858 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 1859 | ".elseif"); |
| 1860 | TheCondState.TheCond = AsmCond::ElseCond; |
| 1861 | bool LastIgnoreState = false; |
| 1862 | if (!TheCondStack.empty()) |
| 1863 | LastIgnoreState = TheCondStack.back().Ignore; |
| 1864 | if (LastIgnoreState || TheCondState.CondMet) |
| 1865 | TheCondState.Ignore = true; |
| 1866 | else |
| 1867 | TheCondState.Ignore = false; |
| 1868 | |
| 1869 | return false; |
| 1870 | } |
| 1871 | |
| 1872 | /// ParseDirectiveEndIf |
| 1873 | /// ::= .endif |
| 1874 | bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1875 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1876 | return TokError("unexpected token in '.endif' directive"); |
| 1877 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1878 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1879 | |
| 1880 | if ((TheCondState.TheCond == AsmCond::NoCond) || |
| 1881 | TheCondStack.empty()) |
| 1882 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 1883 | ".else"); |
| 1884 | if (!TheCondStack.empty()) { |
| 1885 | TheCondState = TheCondStack.back(); |
| 1886 | TheCondStack.pop_back(); |
| 1887 | } |
| 1888 | |
| 1889 | return false; |
| 1890 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1891 | |
| 1892 | /// ParseDirectiveFile |
| 1893 | /// ::= .file [number] string |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 1894 | bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1895 | // FIXME: I'm not sure what this is. |
| 1896 | int64_t FileNumber = -1; |
| 1897 | if (Lexer.is(AsmToken::Integer)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1898 | FileNumber = getTok().getIntVal(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1899 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1900 | |
| 1901 | if (FileNumber < 1) |
| 1902 | return TokError("file number less than one"); |
| 1903 | } |
| 1904 | |
| 1905 | if (Lexer.isNot(AsmToken::String)) |
| 1906 | return TokError("unexpected token in '.file' directive"); |
| 1907 | |
Chris Lattner | d32e803 | 2010-01-25 19:02:58 +0000 | [diff] [blame] | 1908 | StringRef Filename = getTok().getString(); |
| 1909 | Filename = Filename.substr(1, Filename.size()-2); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1910 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1911 | |
| 1912 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1913 | return TokError("unexpected token in '.file' directive"); |
| 1914 | |
Chris Lattner | d32e803 | 2010-01-25 19:02:58 +0000 | [diff] [blame] | 1915 | if (FileNumber == -1) |
| 1916 | Out.EmitFileDirective(Filename); |
| 1917 | else |
| 1918 | Out.EmitDwarfFileDirective(FileNumber, Filename); |
| 1919 | |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | |
| 1923 | /// ParseDirectiveLine |
| 1924 | /// ::= .line [number] |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 1925 | bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1926 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1927 | if (Lexer.isNot(AsmToken::Integer)) |
| 1928 | return TokError("unexpected token in '.line' directive"); |
| 1929 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1930 | int64_t LineNumber = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1931 | (void) LineNumber; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1932 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1933 | |
| 1934 | // FIXME: Do something with the .line. |
| 1935 | } |
| 1936 | |
| 1937 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 839348a | 2010-07-01 20:20:01 +0000 | [diff] [blame] | 1938 | return TokError("unexpected token in '.line' directive"); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1939 | |
| 1940 | return false; |
| 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | /// ParseDirectiveLoc |
| 1945 | /// ::= .loc number [number [number]] |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 1946 | bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1947 | if (Lexer.isNot(AsmToken::Integer)) |
| 1948 | return TokError("unexpected token in '.loc' directive"); |
| 1949 | |
| 1950 | // FIXME: What are these fields? |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1951 | int64_t FileNumber = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1952 | (void) FileNumber; |
| 1953 | // FIXME: Validate file. |
| 1954 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1955 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1956 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1957 | if (Lexer.isNot(AsmToken::Integer)) |
| 1958 | return TokError("unexpected token in '.loc' directive"); |
| 1959 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1960 | int64_t Param2 = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1961 | (void) Param2; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1962 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1963 | |
| 1964 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1965 | if (Lexer.isNot(AsmToken::Integer)) |
| 1966 | return TokError("unexpected token in '.loc' directive"); |
| 1967 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1968 | int64_t Param3 = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1969 | (void) Param3; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1970 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1971 | |
| 1972 | // FIXME: Do something with the .loc. |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1977 | return TokError("unexpected token in '.file' directive"); |
| 1978 | |
| 1979 | return false; |
| 1980 | } |
| 1981 | |