Give the asmparser the ability to parse strings. Patch contributed by
authorChris Lattner <sabre@nondot.org>
Fri, 20 May 2005 03:25:47 +0000 (03:25 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 20 May 2005 03:25:47 +0000 (03:25 +0000)
Alexander Friedman

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22146 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AsmParser/Lexer.l
lib/AsmParser/Parser.cpp
lib/AsmParser/ParserInternals.h
lib/AsmParser/llvmAsmParser.y

index 12a430af36b21ed65a84d6e8aabc663796f10817..062e00c41971e947a147b6c89371a3a6eb0e12c7 100644 (file)
 #include <cctype>
 #include <cstdlib>
 
+void set_scan_file(FILE * F){
+  yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
+}
+void set_scan_string (const char * str) {
+  yy_scan_string (str);
+}
+
 #define RET_TOK(type, Enum, sym) \
   llvmAsmlval.type = Instruction::Enum; return sym
 
index 0111ea35df1d3e2c775fafa7268fddaae0717f43..7bb4f0a362f8a91644223863983d681e554109cb 100644 (file)
@@ -42,6 +42,10 @@ Module *llvm::ParseAssemblyFile(const std::string &Filename) {
   return Result;
 }
 
+Module *llvm::ParseAssemblyString(const char * AsmString, Module * M) {
+  return RunVMAsmParser(AsmString, M);
+}
+
 
 //===------------------------------------------------------------------------===
 //                              ParseException Class
index 20961357c88de919ddd4de16221dc0af7357f8ae..b21dea7a078fee17f174e49fb4409a5bc71d96a3 100644 (file)
 #include "llvm/Assembly/Parser.h"
 #include "llvm/ADT/StringExtras.h"
 
+
 // Global variables exported from the lexer...
-extern std::FILE *llvmAsmin;
+
 extern int llvmAsmlineno;
 
+extern std::string &llvmAsmTextin;
+
+// functions exported from the lexer
+void set_scan_file(FILE * F);
+void set_scan_string (const char * str);
+
 // Globals exported by the parser...
 extern char* llvmAsmtext;
 extern int   llvmAsmleng;
@@ -38,6 +45,9 @@ extern std::string CurFilename;
 class Module;
 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
 
+// Parse a string directly
+Module *RunVMAsmParser(const char * AsmString, Module * M);
+
 
 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
 // appropriate character.  If AllowNull is set to false, a \00 value will cause
index c459a0eec0e4c9516327a86af3f1942a4cfcde41..d73b008416ae4f3c5ea3640324eb5b0d493abd87 100644 (file)
@@ -719,26 +719,41 @@ static PATypeHolder HandleUpRefs(const Type *ty) {
 }
 
 
-//===----------------------------------------------------------------------===//
-//            RunVMAsmParser - Define an interface to this parser
-//===----------------------------------------------------------------------===//
-//
-Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
-  llvmAsmin = F;
-  CurFilename = Filename;
-  llvmAsmlineno = 1;      // Reset the current line number...
+// common code from the two 'RunVMAsmParser' functions
+ static Module * RunParser(Module * M) {
 
-  // Allocate a new module to read
-  CurModule.CurrentModule = new Module(Filename);
+  llvmAsmlineno = 1;      // Reset the current line number...
 
+  CurModule.CurrentModule = M;
   yyparse();       // Parse the file, potentially throwing exception
 
   Module *Result = ParserResult;
-
-  llvmAsmin = stdin;    // F is about to go away, don't use it anymore...
   ParserResult = 0;
 
   return Result;
+
+ }
+
+//===----------------------------------------------------------------------===//
+//            RunVMAsmParser - Define an interface to this parser
+//===----------------------------------------------------------------------===//
+//
+Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
+  set_scan_file(F);
+
+  CurFilename = Filename;
+  return RunParser(new Module(CurFilename));
+}
+
+Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
+  set_scan_string(AsmString);
+
+  CurFilename = "from_memory";
+  if (M == NULL) {
+    return RunParser(new Module (CurFilename));
+  } else {
+    return RunParser(M);
+  }
 }
 
 %}