Changeset 739 for trunk/rphp

Show
Ignore:
Timestamp:
11/21/08 13:12:06 (7 weeks ago)
Author:
weyrick
Message:

some real error handling

Location:
trunk/rphp
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • trunk/rphp/compiler/CMakeLists.txt

    r729 r739  
    8181  pLinkTarget.cpp 
    8282  pLibTargets.cpp 
     83  pCompileError.cpp 
    8384  ${RPHP_LLVM_DEP_FILES} 
    8485  ${LLVM_LIBS_INTERPRETER_OBJECTS} 
  • trunk/rphp/compiler/pAST.h

    r734 r739  
    168168class var: public expr { 
    169169 
    170     const pSourceRange name_; 
    171  
    172 public: 
    173     var(const pSourceRange& name): expr(varKind), name_(name) { } 
     170    pIdentString name_; 
     171 
     172public: 
     173    var(const pSourceRange& name): expr(varKind), name_(name.begin(), name.end()) { } 
    174174 
    175175    pIdentString name(void) const { 
    176         return pIdentString(name_.begin(), name_.end()); 
     176        return name_; 
    177177    } 
    178178 
     
    200200class functionInvoke: public expr { 
    201201 
    202     const pSourceRange name_; 
     202    pIdentString name_; 
    203203    expressionList argList_; 
    204204 
     
    206206    functionInvoke(const pSourceRange& name, expressionList* argList): 
    207207        expr(functionInvokeKind), 
    208         name_(name), 
     208        name_(name.begin(), name.end()), 
    209209        argList_(*argList) 
    210210    { 
     
    220220 
    221221    pIdentString name(void) const { 
    222         return pIdentString(name_.begin(), name_.end()); 
     222        return name_; 
    223223    } 
    224224    expressionList& argList(void) { return argList_; } 
  • trunk/rphp/compiler/pDriver.cpp

    r732 r739  
    152152    l.dumpTokens(); 
    153153    delete source; 
    154      
     154 
    155155} 
    156156 
  • trunk/rphp/compiler/pGenSupport.cpp

    r735 r739  
    2828#include <fstream> 
    2929 
     30#include "pCompileError.h" 
    3031#include "pGenSupport.h" 
    3132 
     
    3940} 
    4041 
    41 bool pGenSupport::writeBitcode(llvm::Module* m, std::string outFile) { 
     42void pGenSupport::writeBitcode(llvm::Module* m, std::string outFile) { 
    4243 
    4344    assert(m != NULL); 
    4445    assert(outFile.length() > 0); 
    4546 
    46     // TODO: real error handling 
    4747    std::ofstream OS(outFile.c_str(), std::ios_base::out|std::ios::trunc|std::ios::binary); 
    4848    if (!OS.fail()) { 
    4949        llvm::WriteBitcodeToFile(m, OS); 
    50         return true; 
    5150    } 
    5251    else { 
    53         return false; 
     52        throw pCompileError("unable to write bitcode file ["+outFile+"]"); 
    5453    } 
    5554 
     
    6261    llvm::MemoryBuffer* mb = llvm::MemoryBuffer::getFile(fileName.c_str(), &errMsg); 
    6362    if (errMsg.length()) { 
    64         // TODO: errors 
    65         std::cerr << "error loading runtime IR file [" << fileName << "]: " << errMsg << std::endl; 
    66         exit(1); 
     63        throw pCompileError("error loading runtime IR file [" + fileName + "]: " + errMsg); 
    6764    } 
    6865 
    6966    llvm::ModuleProvider* mp = llvm::getBitcodeModuleProvider(mb, &errMsg); 
    7067    if (errMsg.length()) { 
    71         // TODO: errors 
    72         std::cerr << "error parsing bitcode file [" << fileName << "]: " << errMsg << std::endl; 
    73         exit(1); 
     68        throw pCompileError("error parsing bitcode file [" + fileName + "]: " + errMsg); 
    7469    } 
    7570 
     
    8681llvm::Module* pGenSupport::getRuntimeIR() { 
    8782 
    88     // TODO: ouch! 
     83    // TODO: needs to be a command line option with a good default 
    8984    return readBitcode("../runtime-ir/irRuntime.ir"); 
    9085 
  • trunk/rphp/compiler/pGenSupport.h

    r680 r739  
    3535public: 
    3636    static std::string mangleModuleName(std::string moduleName); 
    37     static bool writeBitcode(llvm::Module* m, std::string outFile); 
     37    static void writeBitcode(llvm::Module* m, std::string outFile); 
    3838    static llvm::Module* readBitcode(std::string fileName); 
    3939    static llvm::Module* getRuntimeIR(); 
  • trunk/rphp/compiler/pGenerator.cpp

    r734 r739  
    3131#include <llvm/Linker.h> 
    3232 
     33#include "pCompileError.h" 
    3334#include "pGenerator.h" 
    3435#include "pCompileTarget.h" 
     
    6566    l.LinkInModule(irMod, &errMsg); 
    6667    if (errMsg.length()) { 
    67         // TODO: errors 
    68         std::cerr << "error linking in runtime ir: " << errMsg << std::endl; 
    69         exit(1); 
     68        throw pCompileError("error linking in runtime IR [" + errMsg + "]"); 
    7069    } 
    7170 
     
    261260 
    262261    // gen lval 
     262    pIdentString name("lVal"); 
     263    if (n->lVal()->getKind() == AST::varKind) { 
     264        AST::var* l = static_cast<AST::var*>(n->lVal()); 
     265        name = l->name(); 
     266    } 
    263267    visit(n->lVal()); 
    264268    Value* lVal = valueStack_.back(); 
     
    268272    assert(f != NULL); 
    269273 
    270     currentBlock_.CreateCall2(f, lVal, rVal); 
     274    currentBlock_.CreateCall2(f, lVal, rVal, name.c_str()); 
    271275 
    272276} 
  • trunk/rphp/compiler/pGenerator.h

    r722 r739  
    4040namespace rphp { 
    4141 
     42// TODO: llvm has structures for this. use those? 
    4243typedef boost::unordered_map<pIdentString, llvm::Value*> symbolTableType; 
    4344 
  • trunk/rphp/compiler/pModule.cpp

    r738 r739  
    4343{ 
    4444 
    45     // TODO: error handling 
    4645    source_ = new pSourceFile(file); 
    4746    parser::parseSourceFile(this); 
     
    115114} 
    116115 
    117 bool pModule::writeBitcode(pFileNameString fileName) { 
     116void pModule::writeBitcode(pFileNameString fileName) { 
    118117 
    119     return pGenSupport::writeBitcode(llvmModule_, source_->fileName()); 
     118    pGenSupport::writeBitcode(llvmModule_, source_->fileName()); 
    120119 
    121120} 
  • trunk/rphp/compiler/pModule.h

    r732 r739  
    7171    void applyVisitor(AST::baseVisitor* v); 
    7272    bool lowerToIR(pCompileTarget* target); 
    73     bool writeBitcode(pFileNameString fileName); 
     73    void writeBitcode(pFileNameString fileName); 
    7474    void setLLVMModuleOwnership(bool v) { llvmModuleOwner_ = v; } 
    7575    llvm::Module* getLLVMModule() { return llvmModule_; } 
  • trunk/rphp/compiler/pSourceFile.cpp

    r738 r739  
    3030#include <stdio.h> 
    3131 
     32#include "pCompileError.h" 
    3233#include "pSourceFile.h" 
    3334 
     
    4041    std::ifstream instream(file_.get<0>().c_str()); 
    4142    if (!instream.is_open()) { 
    42         std::cerr << "Couldn't open file: " << file_.get<0>() << std::endl; 
    43         exit(-1); 
     43        throw pCompileError("couldn't open file [" + file_.get<0>() + "]"); 
    4444    } 
    4545    instream.unsetf(std::ios::skipws); 
     
    6666        UnicodeString ubuffer(rawBuffer.data(), rawBuffer.length(), file_.get<1>().c_str()); 
    6767        if (ubuffer.isBogus()) { 
    68             std::cerr << "Could not perform character conversion in file: " << file_.get<0>() << " from charset: " << file_.get<1>() << std::endl; 
    69             exit(-1); 
     68            throw pCompileError("could not perform character conversion in file [" + file_.get<0>() + "] from charset [" + file_.get<1>() + "]"); 
    7069        } 
    7170 
  • trunk/rphp/compiler/pStandAloneTargets.cpp

    r722 r739  
    3434#include <llvm/System/Program.h> 
    3535 
     36#include "pCompileError.h" 
    3637#include "pGenSupport.h" 
    3738#include "pStandAloneTargets.h" 
     
    123124    sys::Path ld = sys::Program::FindProgramByName("llvm-ld"); 
    124125    if (ld.isEmpty()) { 
    125         // TODO: error handling 
    126         std::cerr << "unable to link: llvm-ld not found" << std::endl; 
    127         return; 
     126        throw pCompileError("unable to link: llvm-ld not found"); 
    128127    } 
    129128 
     
    163162 
    164163    if (R != 0) { 
    165         // TODO: error handling 
    166         std::cerr << errMsg << std::endl; 
    167         return; 
     164        throw pCompileError(errMsg); 
    168165    } 
    169166 
  • trunk/rphp/frontend/cli/main.cpp

    r732 r739  
    7272    } 
    7373    else if (dumpToks) { 
    74         driver.dumpTokens(boost::make_tuple(inputFile, encoding)); 
     74        try { 
     75            driver.dumpTokens(boost::make_tuple(inputFile, encoding)); 
     76        } 
     77        catch (std::exception& e) { 
     78            std::cerr << e.what() << std::endl; 
     79            return 1; 
     80        } 
     81        return 0; 
    7582    } 
    7683    else if (dumpAST) { 
    77         driver.dumpAST(boost::make_tuple(inputFile, encoding)); 
     84        try { 
     85            driver.dumpAST(boost::make_tuple(inputFile, encoding)); 
     86        } 
     87        catch (std::exception& e) { 
     88            std::cerr << e.what() << std::endl; 
     89            return 1; 
     90        } 
     91        return 0; 
    7892    } 
    7993    else if (dumpIR) { 
    80         driver.dumpIR(boost::make_tuple(inputFile, encoding)); 
     94        try { 
     95            driver.dumpIR(boost::make_tuple(inputFile, encoding)); 
     96        } 
     97        catch (std::exception& e) { 
     98            std::cerr << e.what() << std::endl; 
     99            return 1; 
     100        } 
     101        return 0; 
    81102    } 
    82103    else if (dumpPre) { 
    83         driver.dumpPre(boost::make_tuple(inputFile, encoding)); 
     104        try { 
     105            driver.dumpPre(boost::make_tuple(inputFile, encoding)); 
     106        } 
     107        catch (std::exception& e) { 
     108            std::cerr << e.what() << std::endl; 
     109            return 1; 
     110        } 
     111        return 0; 
    84112    } 
    85113    else {