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