David Lin avatar David Lin committed 52b97dc

Diagnostic System

Comments (0)

Files changed (3)

lab_4/b98902077/compiler/Context.cpp

     m_Builder.CreateRet(_get_int_constant("0"));
   }
   
-  llvm::Value* Context::_codegen_arith(char op, llvm::Value* a, llvm::Value* b)
+  llvm::Value* Context::_codegen_arith
+  (char op, llvm::Value* a, llvm::Value* b, const YYLTYPE& opLoc)
   {
     // according to op, determine instruction
     switch(op)
     }
     
     {
-      std::cerr << "Invalid Binary Arithmetic Expression" << std::endl;
-      std::abort();
+      _error(opLoc, "Invalid Binary Arithmetic Expression");
     }
     
     return NULL;
   }
   
-  llvm::Value* Context::_codegen_arith(char op, llvm::Value* a)
+  llvm::Value* Context::_codegen_arith
+  (char op, llvm::Value* a, const YYLTYPE& opLoc)
   {
     // according to op, determine instruction
     switch(op)
     }
     
     {
-      std::cerr << "Invalid Unary Arithmetic Expression" << std::endl;
-      std::abort();
+      _error(opLoc, "Invalid Unary Arithmetic Expression");
     }
     
     return NULL;
     m_LoopDepth.pop_back();
   }
   
-  void Context::_codegen_new_var(const char* name)
+  void Context::_codegen_new_var(const char* name, const YYLTYPE& loc)
   {
     VarTable* vtable = m_VarTableList.back();
     VarTable::iterator it = vtable->find(name);
     }
     else // duplicate name is not allowed
     {
-      std::cerr << "duplicate name \"" << name << "\" in Declaration\n";
-      std::abort();
+      _error(loc, Twine("Duplicate name \"") + name + "\" in declaration");
     }
   }
   
   llvm::Value* Context::_codegen_find_var
-  (const char* name, const std::vector<llvm::Value*>* dims)
+  (const char* name, const DimensionVector* dims, const YYLTYPE& loc)
   {
     // Find a variable
     for (std::vector<VarTable*>::reverse_iterator 
         {
           if (dims != NULL)
           {
-            std::cerr << "local variable should not have dimension\n";
-            std::abort();
+            _error(loc, "local variable should not have dimension");
           }
           
           return var.address;
         {
           if (dims == NULL || dims->size() != var.dims.size())
           {
-            std::cerr << "dimension of the array does not match.\n";
-            std::abort();
+            _error(loc, "the dimension of the array does not match");
           }
           
           llvm::Value* localval = var.address;
     }
     
     // Not Found
-    std::cerr << "Cannot find variable with the name \"" << name << "\"\n";
-    std::abort();
+    _error(loc, Twine("Cannot find variable with the name \"") + name +"\"");
     return NULL;
   }
   
     m_DeclDims = dims;
   }
   
-  void Context::_codegen_break()
+  void Context::_codegen_break(const YYLTYPE& loc)
   {
     unsigned long depth = m_BlockDepth;
     
     if (m_LoopDepth.empty())
     {
-      std::cerr << "The break statement must be used in loop" << std::endl;
-      std::abort();
+      _error(loc, "The break statement must be used in loop");
     }
     
     // Clean up resources
     m_Builder.SetInsertPoint(block);
   }
   
-  void Context::_codegen_continue()
+  void Context::_codegen_continue(const YYLTYPE& loc)
   {
     unsigned long depth = m_BlockDepth;
     
     if (m_LoopDepth.empty())
     {
-      std::cerr << "The continue statement must be used in loop" << std::endl;
-      std::abort();
+      _error(loc, "The continue statement must be used in loop");
     }
     
     // Clean up resources
     return true;
   }
   
-  void Context::_error(YYLTYPE* llocp, const Twine& msg)
+  void Context::_error(const YYLTYPE& llocp, const Twine& msg)
   {
-    SourceLocation startLoc = SourceLocation::getFromPointer(llocp->startPtr);
-    SourceLocation endLoc = SourceLocation::getFromPointer(llocp->endPtr);
+    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, 
+    m_SourceMgr.PrintMessage(endLoc, llvm::SourceMgr::DK_Error, msg, 
                              llvm::ArrayRef<llvm::SMRange>( range ));
     
     std::exit(1);

lab_4/b98902077/compiler/Context.h

 namespace cmm
 {
   
+  typedef std::vector<llvm::Value*> DimensionVector;
+  
   class Context
   {
   public:
     
   public: // for YYParser.y
     // Error
-    void _error(YYLTYPE* llocp, const Twine& msg);
+    void _error(const YYLTYPE& llocp, const Twine& msg);
     
     // Lex
     int _lex(YYSTYPE* lvalp, YYLTYPE* llocp);
     // Code Generation
     void _codegen_done();
     
-    llvm::Value* _codegen_arith(char op, llvm::Value* a, llvm::Value* b);
-    llvm::Value* _codegen_arith(char op, llvm::Value* a);
+    llvm::Value* _codegen_arith
+    (char op, llvm::Value* a, llvm::Value* b, const YYLTYPE& opLoc);
+    
+    llvm::Value* _codegen_arith
+    (char op, llvm::Value* a, const YYLTYPE& opLoc);
+    
     llvm::Value* _codegen_cmpeq(llvm::Value* a, llvm::Value* b);
     llvm::Value* _codegen_cmpne(llvm::Value* a, llvm::Value* b);
     llvm::Value* _codegen_cmplt(llvm::Value* a, llvm::Value* b);
     void _codegen_while_body(llvm::Value* cond);
     void _codegen_endwhile();
     
-    void _codegen_new_var(const char* name);
+    void _codegen_new_var(const char* name, const YYLTYPE& loc);
     
     llvm::Value* _codegen_find_var
-    (const char* name, const std::vector<llvm::Value*>* dims);
+    (const char* name, const DimensionVector* dims, const YYLTYPE& loc);
     
     llvm::Value* _codegen_load(llvm::Value* ptr);
     void _codegen_store(llvm::Value* ptr, llvm::Value* expr);
     void _codegen_decl_nodim();
     void _codegen_decl_dims(const std::vector<llvm::Value*>& dims);
     
-    void _codegen_break();
-    void _codegen_continue();
+    void _codegen_break(const YYLTYPE& loc);
+    void _codegen_continue(const YYLTYPE& loc);
     
   private:
     int _lex_word(YYSTYPE* lvalp);
     // Working Block
     llvm::IRBuilder<> m_Builder;
     std::vector< llvm::BasicBlock* > m_LazyBlock;
-    std::vector< llvm::Value* > m_DeclDims;
+    DimensionVector m_DeclDims;
     unsigned long m_BlockDepth;
     
     // Loop Info

lab_4/b98902077/compiler/YYParser.y

   void* null;
   const char* name;
   llvm::Value* value;
-  std::vector<llvm::Value*>* pDims;
+  cmm::DimensionVector* pDims;
   std::vector<const char*>* pNames;
 }
 
 declArray: dims { context._codegen_decl_dims(*($1)); delete $1; } ;
 
 // Identifier Sequence
-declNames: declNames "," YX_ID { context._codegen_new_var($3); }
-         | YX_ID { context._codegen_new_var($1); }
+declNames: declNames "," YX_ID { context._codegen_new_var($3, @3); }
+         | YX_ID { context._codegen_new_var($1, @1); }
          ;
 
 // Dimension Sequence
 dims: dims "[" arithExpr "]" { $1->push_back($3); $$ = $1; }
-    | "[" arithExpr "]" { $$ = new std::vector<llvm::Value*>(1, $2); }
+    | "[" arithExpr "]" { $$ = new cmm::DimensionVector(1, $2); }
     ;
 
 // Statement: Variable Assignment
     ;
 
 // Statement: break the while loop
-stmt: "break" ";" { context._codegen_break(); } ;
+stmt: "break" ";" { context._codegen_break(@1); } ;
 
 // Statement: continue the while loop
-stmt: "continue" ";" { context._codegen_continue(); } ;
+stmt: "continue" ";" { context._codegen_continue(@1); } ;
 
 // Statement: read from input
 stmt: "scan" "(" readVarList ")" ";";
          ;
 
 // Variable Access
-var: YX_ID { $$ = context._codegen_find_var($1, NULL); }
-   | YX_ID dims { $$ = context._codegen_find_var($1, $2); delete $2; }
+var: YX_ID { $$ = context._codegen_find_var($1, NULL, @1); }
+   | YX_ID dims { $$ = context._codegen_find_var($1, $2, @1); delete $2; }
    ;
 
 // Arithmetic Expression (Binary)
-arithExpr: arithExpr "+" arithExpr {$$ = context._codegen_arith('+',$1,$3);}
-         | arithExpr "-" arithExpr {$$ = context._codegen_arith('-',$1,$3);}
-         | arithExpr "*" arithExpr {$$ = context._codegen_arith('*',$1,$3);}
-         | arithExpr "/" arithExpr {$$ = context._codegen_arith('/',$1,$3);}
+arithExpr: arithExpr "+" arithExpr {$$ = context._codegen_arith('+',$1,$3,@2);}
+         | arithExpr "-" arithExpr {$$ = context._codegen_arith('-',$1,$3,@2);}
+         | arithExpr "*" arithExpr {$$ = context._codegen_arith('*',$1,$3,@2);}
+         | arithExpr "/" arithExpr {$$ = context._codegen_arith('/',$1,$3,@2);}
          ;
 
 // Arithmetic Expression (Unary)
-arithExpr: "+" arithExpr %prec YX_UNARY {$$ = context._codegen_arith('+',$2);}
-         | "-" arithExpr %prec YX_UNARY {$$ = context._codegen_arith('-',$2);}
+arithExpr: "+" arithExpr %prec YX_UNARY {$$=context._codegen_arith('+',$2,@1);}
+         | "-" arithExpr %prec YX_UNARY {$$=context._codegen_arith('-',$2,@1);}
          | var    { $$ = context._codegen_load($1); }
          | YX_NUM { $$ = $1; }
          | "(" arithExpr ")" { $$ = $2; }
          ;
 
 // Logical Expression (Logic Operation)
-logicExpr: logicExpr "||" logicExpr {$$ = context._codegen_arith('|',$1,$3);}
-         | logicExpr "&&" logicExpr {$$ = context._codegen_arith('&',$1,$3);}
-         | "!" logicExpr            {$$ = context._codegen_arith('!',$2);}
+logicExpr: logicExpr "||" logicExpr {$$=context._codegen_arith('|',$1,$3,@2);}
+         | logicExpr "&&" logicExpr {$$=context._codegen_arith('&',$1,$3,@2);}
+         | "!" logicExpr            {$$ = context._codegen_arith('!',$2,@1);}
          | "[" logicExpr "]" { $$ = $2; }
          ;
 
 //
 static void yyerror(YYLTYPE *llocp, cmm::Context& context, const char* msg)
 {
-  context._error(llocp, msg);
+  context._error(*llocp, msg);
 }
 
 //
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.