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