Ignore:
Timestamp:
Mar 1, 2017, 4:17:24 PM (2 years ago)
Author:
nmedfort
Message:

Progress on parenthesis matching example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/array-test.cpp

    r5337 r5353  
    8888        body.createAssign(pending_lparen, body.createAnd(pscan, lparen));
    8989        // Mark any opening paren without a matching closer as an error.
    90         body.createAssign(accumulated_errors, body.createOr(accumulated_errors, body.createAtEOF(pscan)));
     90        PabloAST * unmatched_lparen = body.createAtEOF(pscan, "unmatched_lparen");
     91        body.createAssign(accumulated_errors, body.createOr(accumulated_errors, unmatched_lparen));
    9192
    9293        body.createAssign(body.createExtract(matches, index), closed);
     
    102103}
    103104
    104 
    105105Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
    106106
    107     Type * byteStreamTy = iBuilder->getStreamSetTy(1, 2);
     107    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 8);
    108108
    109109    Module * const mod = iBuilder->getModule();
    110    
     110
    111111    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", iBuilder->getVoidTy(), byteStreamTy->getPointerTo(), iBuilder->getSizeTy(), nullptr));
    112112    main->setCallingConv(CallingConv::C);
     
    117117    Value * const fileSize = &*(args++);
    118118    fileSize->setName("fileSize");
     119
     120    const unsigned segmentSize = codegen::SegmentSize;
     121    const unsigned bufferSegments = codegen::BufferSegments;
    119122   
    120     ExternalFileBuffer ByteStream(iBuilder, byteStreamTy);
    121     SingleBlockBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8, 1));
    122     ExpandableBuffer matches(iBuilder, iBuilder->getStreamSetTy(count, 1), 2);
    123     SingleBlockBuffer errors(iBuilder, iBuilder->getStreamTy());
    124 
    125     MMapSourceKernel mmapK(iBuilder);
     123    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
     124
     125    kernel::MMapSourceKernel mmapK(iBuilder, segmentSize);
    126126    mmapK.generateKernel({}, {&ByteStream});
    127127    mmapK.setInitialArguments({fileSize});
    128    
    129     S2PKernel s2pk(iBuilder);
     128
     129    CircularBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8), segmentSize * bufferSegments);
     130
     131    kernel::S2PKernel  s2pk(iBuilder);
    130132    s2pk.generateKernel({&ByteStream}, {&BasisBits});
    131133
     
    136138    generate(&bm);
    137139    pablo_function_passes(&bm);
     140
     141    ExpandableBuffer matches(iBuilder, iBuilder->getStreamSetTy(count), segmentSize * bufferSegments);
     142    SingleBlockBuffer errors(iBuilder, iBuilder->getStreamTy());
    138143
    139144    bm.generateKernel({&BasisBits}, {&matches, &errors});
     
    162167    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
    163168
    164     llvm::Function * f = pipeline(idb, 4);
     169    llvm::Function * f = pipeline(idb, 3);
    165170
    166171    verifyModule(*M, &dbgs());
     
    175180}
    176181
    177 void run(MatchParens f, const std::string & fileName) {
     182template <typename T>
     183std::ostream & operator<< (std::ostream& out, const std::vector<T>& v) {
     184  if ( !v.empty() ) {
     185    out << '[';
     186    std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
     187    out << "\b\b]";
     188  }
     189  return out;
     190}
     191
     192void check(const char * byteStream, const size_t fileSize) {
     193
     194    std::vector<size_t> unmatched_left;
     195    std::vector<size_t> unmatched_right;
     196    size_t maxDepth = 0;
     197    for (size_t i = 0; i < fileSize; ++i) {
     198        switch (byteStream[i]) {
     199            case '(':
     200                unmatched_left.push_back(i);
     201                maxDepth = std::max<size_t>(maxDepth, unmatched_left.size());
     202                break;
     203            case ')':
     204                if (unmatched_left.empty()) {
     205                    unmatched_right.push_back(i);
     206                } else {
     207                    unmatched_left.pop_back();
     208                }
     209            default:
     210                break;
     211        }
     212    }
     213
     214    std::cerr << "maximum depth:        " << maxDepth << "\n"
     215                 "invalid left parens:  " << unmatched_left << "\n"
     216                 "invalid right parens: " << unmatched_right <<
     217                 std::endl;
     218}
     219
     220
     221void run(MatchParens match, const std::string & fileName) {
    178222    const boost::filesystem::path file(fileName);
    179223    if (exists(file)) {
     
    186230            mappedFile.open(fileName);
    187231            char * fileBuffer = const_cast<char *>(mappedFile.data());
    188             f(fileBuffer, fileSize);
     232            check(fileBuffer, fileSize);
     233            match(fileBuffer, fileSize);
    189234            mappedFile.close();
    190235        }
Note: See TracChangeset for help on using the changeset viewer.