[llvm-commits] CVS: llvm/lib/AsmParser/Lexer.l Parser.cpp ParserInternals.h llvmAsmParser.y

Chris Lattner lattner at cs.uiuc.edu
Thu May 19 20:25:58 PDT 2005



Changes in directory llvm/lib/AsmParser:

Lexer.l updated: 1.62 -> 1.63
Parser.cpp updated: 1.15 -> 1.16
ParserInternals.h updated: 1.40 -> 1.41
llvmAsmParser.y updated: 1.224 -> 1.225
---
Log message:

Give the asmparser the ability to parse strings.  Patch contributed by
Alexander Friedman


---
Diffs of the changes:  (+49 -13)

 Lexer.l           |    7 +++++++
 Parser.cpp        |    4 ++++
 ParserInternals.h |   12 +++++++++++-
 llvmAsmParser.y   |   39 +++++++++++++++++++++++++++------------
 4 files changed, 49 insertions(+), 13 deletions(-)


Index: llvm/lib/AsmParser/Lexer.l
diff -u llvm/lib/AsmParser/Lexer.l:1.62 llvm/lib/AsmParser/Lexer.l:1.63
--- llvm/lib/AsmParser/Lexer.l:1.62	Tue May 10 17:02:28 2005
+++ llvm/lib/AsmParser/Lexer.l	Thu May 19 22:25:47 2005
@@ -32,6 +32,13 @@
 #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: llvm/lib/AsmParser/Parser.cpp
diff -u llvm/lib/AsmParser/Parser.cpp:1.15 llvm/lib/AsmParser/Parser.cpp:1.16
--- llvm/lib/AsmParser/Parser.cpp:1.15	Thu Apr 21 16:10:11 2005
+++ llvm/lib/AsmParser/Parser.cpp	Thu May 19 22:25:47 2005
@@ -42,6 +42,10 @@
   return Result;
 }
 
+Module *llvm::ParseAssemblyString(const char * AsmString, Module * M) {
+  return RunVMAsmParser(AsmString, M);
+}
+
 
 //===------------------------------------------------------------------------===
 //                              ParseException Class


Index: llvm/lib/AsmParser/ParserInternals.h
diff -u llvm/lib/AsmParser/ParserInternals.h:1.40 llvm/lib/AsmParser/ParserInternals.h:1.41
--- llvm/lib/AsmParser/ParserInternals.h:1.40	Thu Apr 21 16:10:11 2005
+++ llvm/lib/AsmParser/ParserInternals.h	Thu May 19 22:25:47 2005
@@ -22,10 +22,17 @@
 #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 @@
 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: llvm/lib/AsmParser/llvmAsmParser.y
diff -u llvm/lib/AsmParser/llvmAsmParser.y:1.224 llvm/lib/AsmParser/llvmAsmParser.y:1.225
--- llvm/lib/AsmParser/llvmAsmParser.y:1.224	Tue May 10 17:02:28 2005
+++ llvm/lib/AsmParser/llvmAsmParser.y	Thu May 19 22:25:47 2005
@@ -719,26 +719,41 @@
 }
 
 
-//===----------------------------------------------------------------------===//
-//            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);
+  }
 }
 
 %}






More information about the llvm-commits mailing list