Commits

David Lin  committed 2071497

Diagnostic Working...

  • Participants
  • Parent commits 5469f48

Comments (0)

Files changed (7)

File lab_4/b98902077/compiler/Context.cpp

 #include "Context.h"
-
-#include "YYParser.hpp"
+#include "YYTypes.h"
 
 #include <llvm/Analysis/Verifier.h>
 #include <llvm/Support/raw_os_ostream.h>
 {
   
   Context::Context(MemoryBuffer* buffer)
-  : m_TokenExtractor(buffer), m_Builder(m_Context)
+  : m_TokenExtractor(buffer, m_SourceMgr), m_Builder(m_Context)
   {
     // Prepare Source Code
     m_SourceMgr.AddNewSourceBuffer(buffer, llvm::SMLoc());
   
   // ======================================================================
   
-  int Context::_lex(YYSTYPE* lvalp)
+  int Context::_lex(YYSTYPE* lvalp, YYLTYPE* llocp)
   {
     // Next Token!
     m_TokenExtractor.next();
     // Initialize Default Value
     lvalp->null = NULL;
     
+    // Get Location
+    llocp->startPtr = m_TokenExtractor.tokenLocStart().getPointer();
+    llocp->endPtr = m_TokenExtractor.tokenLocEnd().getPointer();
+    
+    //
+    //std::cerr << "Lex: " << m_TokenExtractor.tokenData().str() << " : ";
+    //std::cerr << "(" << (int)m_TokenExtractor.tokenCode() << ") ";
+    //std::cerr << (void*) llocp->startPtr << ", ";
+    //std::cerr << (void*) llocp->endPtr << "\n";
+    
     // Determine Token Code
     switch (m_TokenExtractor.tokenCode())
     {
     return true;
   }
   
+  void Context::_error(YYLTYPE* llocp, const Twine& msg)
+  {
+    SourceLocation startLoc = SourceLocation::getFromPointer(llocp->startPtr);
+    SourceLocation endLoc = SourceLocation::getFromPointer(llocp->endPtr);
+    llvm::SMRange range(startLoc, endLoc) ;
+    
+    m_SourceMgr.PrintMessage(startLoc, llvm::SourceMgr::DK_Error, msg, 
+                             llvm::ArrayRef<llvm::SMRange>( range ));
+    
+    std::exit(1);
+  }
+  
 }

File lab_4/b98902077/compiler/Context.h

     llvm::Module* getModule(){ return m_Module; }
     
   public: // for YYParser.y
+    // Error
+    void _error(YYLTYPE* llocp, const Twine& msg);
+    
     // Lex
-    int _lex(YYSTYPE* lvalp);
+    int _lex(YYSTYPE* lvalp, YYLTYPE* llocp);
     
     // Code Generation
     void _codegen_done();

File lab_4/b98902077/compiler/Makefile

 CXXFLAGS += -Wall
 CXXFLAGS += -D__STDC_LIMIT_MACROS
 CXXFLAGS += -D__STDC_CONSTANT_MACROS
-#CXXFLAGS += -g
+CXXFLAGS += -g
 
 SYSNAME := $(shell uname -s)
 

File lab_4/b98902077/compiler/TokenExtractor.h

 #include <llvm/ADT/StringRef.h>
 #include <llvm/Support/MemoryBuffer.h>
 #include <llvm/Support/SMLoc.h>
+#include <llvm/Support/SourceMgr.h>
+#include <llvm/ADT/Twine.h>
+#include <llvm/ADT/ArrayRef.h>
 
 #include <fstream>
 
 {
   using llvm::MemoryBuffer;
   using llvm::StringRef;
+  using llvm::Twine;
   typedef llvm::SMLoc SourceLocation;
-  typedef llvm::SMRange SourceRange;
   
   /// @enum Token Code
   struct TokenCode
   class TokenExtractor
   {
   public:
-    explicit TokenExtractor(MemoryBuffer* buffer);
+    explicit TokenExtractor(MemoryBuffer* buffer, llvm::SourceMgr& mgr);
     ~TokenExtractor();
     
     bool next();
     
     StringRef tokenData() const;
     TokenCode::Type tokenCode() const;
-    SourceRange tokenRange() const;
+    SourceLocation tokenLocStart() const;
+    SourceLocation tokenLocEnd() const;
     
   private:
     MemoryBuffer* m_Buffer;
+    llvm::SourceMgr& m_SrcMgr;
     
   private:
     void fsmInit();

File lab_4/b98902077/compiler/TokenExtractor.rl

   
   void TokenExtractor::fsmInit()
   {
+    // Init Start State
     m_FsmState = %%{ write start; }%% ;
     
+    // Init Token Information
     m_TokenStart = NULL;
     m_TokenEnd = NULL;
     m_TokenCode = TokenCode::None;
     
+    // Init Read Pointer
     m_FsmCurr = m_Buffer->getBufferStart();
   }
   
     const char *eof = pe;
     TokenCode::Type tc = TokenCode::None;
     
+    // Run Finite State Machine
     %% write exec;
     
     m_FsmState = cs;
-    m_FsmCurr = te;
-    m_TokenStart = ts;
-    m_TokenEnd = te;
     m_TokenCode = tc;
     
     if (fsmError() || tc == TokenCode::Error)
     {
-      m_TokenStart = NULL;
-      m_TokenEnd = NULL;
+      m_FsmCurr = p;
+      m_TokenStart = ( ts == NULL ? p : ts );
+      m_TokenEnd = p;
       m_TokenCode = TokenCode::Error;
       return false;
     }
+    else if (tc != TokenCode::None)// OK
+    {
+      m_FsmCurr = te;
+      m_TokenStart = ts;
+      m_TokenEnd = te;
+    }
+    else
+    {
+      m_FsmCurr = pe;
+      m_TokenStart = pe;
+      m_TokenEnd = pe;
+    }
     
     return tc != TokenCode::None;
   }
   
   // ===============================================================================
   
-  TokenExtractor::TokenExtractor(MemoryBuffer* buffer)
-  : m_Buffer(buffer), m_TokenStart(NULL), m_TokenEnd(NULL)
+  TokenExtractor::TokenExtractor(MemoryBuffer* buffer, llvm::SourceMgr& mgr)
+  : m_Buffer(buffer), m_SrcMgr(mgr), m_TokenStart(NULL), m_TokenEnd(NULL)
   {
     assert((buffer != NULL) && "MemoryBuffer should be valid");
     
     return m_TokenCode;
   }
   
-  SourceRange TokenExtractor::tokenRange() const
+  SourceLocation TokenExtractor::tokenLocStart() const
+  {
+    return SourceLocation::getFromPointer(m_TokenStart);
+  }
+  
+  SourceLocation TokenExtractor::tokenLocEnd() const
   {
-    SourceLocation start = SourceLocation::getFromPointer(m_TokenStart);
-    SourceLocation end = SourceLocation::getFromPointer(m_TokenEnd - 1);
-    return SourceRange(start, end);
+    return SourceLocation::getFromPointer(m_TokenEnd - 1);
   }
   
 }

File lab_4/b98902077/compiler/YYParser.y

 %{
 
 #include "Context.h"
+#include "YYTypes.h"
 
 #include <llvm/Support/SMLoc.h>
 
 #include <iostream>
 
-// Location Tracking
-
-struct YYLTYPE
-{
-  const char* startPtr;
-  const char* endPtr;
-  // [startPtr, endPtr], not [startPtr, endPtr)
-};
-
-#define YYLTYPE YYLTYPE
-
-#define YYLLOC_DEFAULT(curr, rhs, n) \
-  do \
-  { \
-    if( n > 0 ) \
-    { \
-      (curr).startPtr = YYRHSLOC((rhs), 1).startPtr; \
-      (curr).endPtr = YYRHSLOC((rhs), (n)).endPtr; \
-    } \
-    else \
-    { \
-      (curr) = YYRHSLOC((rhs), 0); \
-    } \
-  } \
-  while(0)
-
 // Token Information
 
 union YYSTYPE;
 //
 static int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, cmm::Context& context)
 {
-  int result = context._lex(lvalp);
-  
-  if (result == YX_ERROR)
-    std::cerr << "Lexical Error" << std::endl;
+  int result = context._lex(lvalp, llocp);
   
   return result;
 }
 //
 static void yyerror(YYLTYPE *llocp, cmm::Context& context, const char* msg)
 {
-  std::cerr << msg << std::endl;
+  context._error(llocp, msg);
 }
 
 //

File lab_4/b98902077/compiler/YYTypes.h

+#pragma once
+
+// Location Tracking
+
+struct YYLTYPE
+{
+  const char* startPtr;
+  const char* endPtr;
+  // [startPtr, endPtr], not [startPtr, endPtr)
+};
+
+#define YYLTYPE YYLTYPE
+
+#define YYLLOC_DEFAULT(curr, rhs, n) \
+do \
+{ \
+  if( n > 0 ) \
+  { \
+    (curr).startPtr = YYRHSLOC((rhs), 1).startPtr; \
+    (curr).endPtr = YYRHSLOC((rhs), (n)).endPtr; \
+  } \
+  else \
+  { \
+    (curr) = YYRHSLOC((rhs), 0); \
+  } \
+} \
+while(0)
+
+// Token Code
+
+#include "YYParser.hpp"