From 6184febcda9b97cd053b2e7557a681a2d78625c8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 20 May 2005 03:25:47 +0000 Subject: [PATCH] Give the asmparser the ability to parse strings. Patch contributed by Alexander Friedman git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22146 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/Lexer.l | 7 ++++++ lib/AsmParser/Parser.cpp | 4 ++++ lib/AsmParser/ParserInternals.h | 12 +++++++++- lib/AsmParser/llvmAsmParser.y | 39 +++++++++++++++++++++++---------- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lib/AsmParser/Lexer.l b/lib/AsmParser/Lexer.l index 12a430af36b..062e00c4197 100644 --- a/lib/AsmParser/Lexer.l +++ b/lib/AsmParser/Lexer.l @@ -32,6 +32,13 @@ #include #include +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 diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp index 0111ea35df1..7bb4f0a362f 100644 --- a/lib/AsmParser/Parser.cpp +++ b/lib/AsmParser/Parser.cpp @@ -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 diff --git a/lib/AsmParser/ParserInternals.h b/lib/AsmParser/ParserInternals.h index 20961357c88..b21dea7a078 100644 --- a/lib/AsmParser/ParserInternals.h +++ b/lib/AsmParser/ParserInternals.h @@ -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 @@ 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 diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y index c459a0eec0e..d73b008416a 100644 --- a/lib/AsmParser/llvmAsmParser.y +++ b/lib/AsmParser/llvmAsmParser.y @@ -719,26 +719,41 @@ static PATypeHolder HandleUpRefs(const Type *ty) { } +// common code from the two 'RunVMAsmParser' functions + static Module * RunParser(Module * M) { + + llvmAsmlineno = 1; // Reset the current line number... + + CurModule.CurrentModule = M; + yyparse(); // Parse the file, potentially throwing exception + + Module *Result = ParserResult; + ParserResult = 0; + + return Result; + + } + //===----------------------------------------------------------------------===// // RunVMAsmParser - Define an interface to this parser //===----------------------------------------------------------------------===// // Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) { - llvmAsmin = F; + set_scan_file(F); + CurFilename = Filename; - llvmAsmlineno = 1; // Reset the current line number... + return RunParser(new Module(CurFilename)); +} - // Allocate a new module to read - CurModule.CurrentModule = new Module(Filename); +Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) { + set_scan_string(AsmString); - 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; + CurFilename = "from_memory"; + if (M == NULL) { + return RunParser(new Module (CurFilename)); + } else { + return RunParser(M); + } } %}