implement .include in the lexer/parser instead of passing it into the streamer.
[oota-llvm.git] / tools / llvm-mc / AsmParser.cpp
index a2cdea4c4dc03af5eb7c582007a377dabbedd2a9..68eb9d56c07b8ab0a6f98d70e84a2373e402bd95 100644 (file)
@@ -526,6 +526,21 @@ bool AsmParser::ParseStatement() {
       return ParseDirectiveComm(/*IsLocal=*/true);
     if (!strcmp(IDVal, ".zerofill"))
       return ParseDirectiveDarwinZerofill();
+    if (!strcmp(IDVal, ".desc"))
+      return ParseDirectiveDarwinSymbolDesc();
+    if (!strcmp(IDVal, ".lsym"))
+      return ParseDirectiveDarwinLsym();
+
+    if (!strcmp(IDVal, ".subsections_via_symbols"))
+      return ParseDirectiveDarwinSubsectionsViaSymbols();
+    if (!strcmp(IDVal, ".abort"))
+      return ParseDirectiveAbort();
+    if (!strcmp(IDVal, ".include"))
+      return ParseDirectiveInclude();
+    if (!strcmp(IDVal, ".dump"))
+      return ParseDirectiveDarwinDumpOrLoad(/*IsDump=*/true);
+    if (!strcmp(IDVal, ".load"))
+      return ParseDirectiveDarwinDumpOrLoad(/*IsLoad=*/false);
 
     Warning(IDLoc, "ignoring directive for now");
     EatToEndOfStatement();
@@ -904,6 +919,37 @@ bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
   return false;  
 }
 
+/// ParseDirectiveDarwinSymbolDesc
+///  ::= .desc identifier , expression
+bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
+  if (Lexer.isNot(asmtok::Identifier))
+    return TokError("expected identifier in directive");
+  
+  // handle the identifier as the key symbol.
+  SMLoc IDLoc = Lexer.getLoc();
+  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
+  Lexer.Lex();
+
+  if (Lexer.isNot(asmtok::Comma))
+    return TokError("unexpected token in '.desc' directive");
+  Lexer.Lex();
+
+  SMLoc DescLoc = Lexer.getLoc();
+  int64_t DescValue;
+  if (ParseAbsoluteExpression(DescValue))
+    return true;
+
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.desc' directive");
+  
+  Lexer.Lex();
+
+  // Set the n_desc field of this Symbol to this DescValue
+  Out.EmitSymbolDesc(Sym, DescValue);
+
+  return false;
+}
+
 /// ParseDirectiveComm
 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
@@ -1052,3 +1098,122 @@ bool AsmParser::ParseDirectiveDarwinZerofill() {
 
   return false;
 }
+
+/// ParseDirectiveDarwinSubsectionsViaSymbols
+///  ::= .subsections_via_symbols
+bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.subsections_via_symbols' directive");
+  
+  Lexer.Lex();
+
+  Out.SubsectionsViaSymbols();
+
+  return false;
+}
+
+/// ParseDirectiveAbort
+///  ::= .abort [ "abort_string" ]
+bool AsmParser::ParseDirectiveAbort() {
+  const char *Str = NULL;
+  if (Lexer.isNot(asmtok::EndOfStatement)) {
+    if (Lexer.isNot(asmtok::String))
+      return TokError("expected string in '.abort' directive");
+    
+    Str = Lexer.getCurStrVal();
+
+    Lexer.Lex();
+  }
+
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.abort' directive");
+  
+  Lexer.Lex();
+
+  Out.AbortAssembly(Str);
+
+  return false;
+}
+
+/// ParseDirectiveLsym
+///  ::= .lsym identifier , expression
+bool AsmParser::ParseDirectiveDarwinLsym() {
+  if (Lexer.isNot(asmtok::Identifier))
+    return TokError("expected identifier in directive");
+  
+  // handle the identifier as the key symbol.
+  SMLoc IDLoc = Lexer.getLoc();
+  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
+  Lexer.Lex();
+
+  if (Lexer.isNot(asmtok::Comma))
+    return TokError("unexpected token in '.lsym' directive");
+  Lexer.Lex();
+
+  MCValue Expr;
+  if (ParseRelocatableExpression(Expr))
+    return true;
+
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.lsym' directive");
+  
+  Lexer.Lex();
+
+  // Create the Sym with the value of the Expr
+  Out.EmitLocalSymbol(Sym, Expr);
+
+  return false;
+}
+
+/// ParseDirectiveInclude
+///  ::= .include "filename"
+bool AsmParser::ParseDirectiveInclude() {
+  if (Lexer.isNot(asmtok::String))
+    return TokError("expected string in '.include' directive");
+  
+  std::string Filename = Lexer.getCurStrVal();
+  SMLoc IncludeLoc = Lexer.getLoc();
+  Lexer.Lex();
+
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.include' directive");
+  
+  // Strip the quotes.
+  Filename = Filename.substr(1, Filename.size()-2);
+  
+  // Attempt to switch the lexer to the included file before consuming the end
+  // of statement to avoid losing it when we switch.
+  if (Lexer.EnterIncludeFile(Filename)) {
+    Lexer.PrintMessage(IncludeLoc,
+                       "Could not find include file '" + Filename + "'",
+                       "error");
+    return true;
+  }
+
+  return false;
+}
+
+/// ParseDirectiveDarwinDumpOrLoad
+///  ::= ( .dump | .load ) "filename"
+bool AsmParser::ParseDirectiveDarwinDumpOrLoad(bool IsDump) {
+  const char *Str;
+
+  if (Lexer.isNot(asmtok::String))
+    return TokError("expected string in '.dump' or '.load' directive");
+  
+  Str = Lexer.getCurStrVal();
+
+  Lexer.Lex();
+
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.dump' or '.load' directive");
+  
+  Lexer.Lex();
+
+  if (IsDump)
+    Out.DumpSymbolsandMacros(Str);
+  else
+    Out.LoadSymbolsandMacros(Str);
+
+  return false;
+}