466301 {
466302 if (did_onepass_)
466303 return onepass_nodes_.data() != NULL;
466304 did_onepass_ = true;
466305
466306 if (start() == 0)
466307 return false;
466308
466309
466310
466311
466312
466313 int maxnodes = 2 + inst_count(kInstByteRange);
466315 if (maxnodes >= 65000 || dfa_mem_ / 4 / statesize < maxnodes)
466316 return false;
466317
466318
466319
466320
466321 int stacksize = inst_count(kInstCapture) +
466322 inst_count(kInstEmptyWidth) +
466323 inst_count(kInstNop) + 1;
466324 PODArray<InstCond> stack(stacksize);
466325
466326 int size = this->size();
466327 PODArray<int> nodebyid(size);
466328 memset(nodebyid.data(), 0xFF, size*sizeof nodebyid[0]);
466329
466330
466331
466332
466333 std::vector<uint8_t> nodes;
466334
466335 Instq tovisit(size), workq(size);
466336 AddQ(&tovisit, start());
466337 nodebyid[start()] = 0;
466338 int nalloc = 1;
466339 nodes.insert(nodes.end(), statesize, 0);
466340 for (Instq::iterator it = tovisit.begin(); it != tovisit.end(); ++it) {
466341 int id = *it;
466342 int nodeindex = nodebyid[id];
466343 OneState* node = IndexToNode(nodes.data(), statesize, nodeindex);
466344
466345
466346
466347 for (int b = 0; b < bytemap_range_; b++)
466348 node->action[b] = kImpossible;
466349 node->matchcond = kImpossible;
466350
466351 workq.clear();
466352 bool matched = false;
466353 int nstack = 0;
466354 stack[nstack].id = id;
466355 stack[nstack++].cond = 0;
466356 while (nstack > 0) {
466357 int id = stack[--nstack].id;
466358 uint32_t cond = stack[nstack].cond;
466359
466360 Loop:
466361 Prog::Inst* ip = inst(id);
466362 switch (ip->opcode()) {
466363 default:
466364 LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
466365 break;
466366
466367 case kInstAltMatch:
466368
466369
466370 DCHECK(!ip->last());
466371
466372 if (!AddQ(&workq, id+1))
466373 goto fail;
466374 id = id+1;
466375 goto Loop;
466376
466377 case kInstByteRange: {
466378 int nextindex = nodebyid[ip->out()];
466379 if (nextindex == -1) {
466380 if (nalloc >= maxnodes) {
466381 goto fail;
466382 }
466383 nextindex = nalloc;
466384 AddQ(&tovisit, ip->out());
466385 nodebyid[ip->out()] = nalloc;
466386 nalloc++;
466387 nodes.insert(nodes.end(), statesize, 0);
466388
466389 node = IndexToNode(nodes.data(), statesize, nodeindex);
466390 }
466391 for (int c = ip->lo(); c <= ip->hi(); c++) {
466392 int b = bytemap_[c];
466393
466394 while (c < 256-1 && bytemap_[c+1] == b)
466395 c++;
466397 uint32_t newact = (nextindex << kIndexShift) | cond;
466398 if (matched)
466399 newact |= kMatchWins;
466400 if ((act & kImpossible) == kImpossible) {
466401 node->action[b] = newact;
466402 } else if (act != newact) {
466403 goto fail;
466404 }
466405 }
466406 if (ip->foldcase()) {
466407 Rune lo = std::max<Rune>(ip->lo(), 'a') + 'A' - 'a';
466408 Rune hi = std::min<Rune>(ip->hi(), 'z') + 'A' - 'a';
466409 for (int c = lo; c <= hi; c++) {
466410 int b = bytemap_[c];
466411
466412 while (c < 256-1 && bytemap_[c+1] == b)
466413 c++;
466415 uint32_t newact = (nextindex << kIndexShift) | cond;
466416 if (matched)
466417 newact |= kMatchWins;
466418 if ((act & kImpossible) == kImpossible) {
466419 node->action[b] = newact;
466420 } else if (act != newact) {
466421 goto fail;
466422 }
466423 }
466424 }
466425
466426 if (ip->last())
466427 break;
466428
466429 if (!AddQ(&workq, id+1))
466430 goto fail;
466431 id = id+1;
466432 goto Loop;
466433 }
466434
466435 case kInstCapture:
466436 case kInstEmptyWidth:
466437 case kInstNop:
466438 if (!ip->last()) {
466439
466440 if (!AddQ(&workq, id+1))
466441 goto fail;
466442 stack[nstack].id = id+1;
466443 stack[nstack++].cond = cond;
466444 }
466445
466446 if (ip->opcode() == kInstCapture && ip->cap() < kMaxCap)
466447 cond |= (1 << kCapShift) << ip->cap();
466448 if (ip->opcode() == kInstEmptyWidth)
466449 cond |= ip->empty();
466450
466451
466452
466453
466454
466455
466456
466457
466458 if (!AddQ(&workq, ip->out())) {
466459 goto fail;
466460 }
466461 id = ip->out();
466462 goto Loop;
466463
466464 case kInstMatch:
466465 if (matched) {
466466
466467 goto fail;
466468 }
466469 matched = true;
466470 node->matchcond = cond;
466471
466472 if (ip->last())
466473 break;
466474
466475 if (!AddQ(&workq, id+1))
466476 goto fail;
466477 id = id+1;
466478 goto Loop;
466479
466480 case kInstFail:
466481 break;
466482 }
466483 }
466484 }
466485
466486 dfa_mem_ -= nalloc*statesize;
466487 onepass_nodes_ = PODArray<uint8_t>(nalloc*statesize);
466488 memmove(onepass_nodes_.data(), nodes.data(), nalloc*statesize);
466489 return true;
466490
466491fail:
466492 return false;
466493}