Skip to content

Commit

Permalink
VM: Write restores and literal matches to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpfard committed Aug 18, 2020
1 parent 4181ccd commit 5e9411f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
76 changes: 56 additions & 20 deletions src/vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,17 @@ class Builder {

// crucial library functions
{

auto dfnty = llvm::FunctionType::get(
llvm::Type::getVoidTy(module.TheContext),
{ llvm::Type::getInt32Ty(module.TheContext),
llvm::PointerType::getInt8PtrTy(module.TheContext, 0),
llvm::PointerType::getInt8PtrTy(module.TheContext, 0),
llvm::PointerType::getInt8PtrTy(module.TheContext, 0) },
false);
module.nlex_debug = module.mkfunc(true, "__nlex_produce_debug", false,
false, true, dfnty);

auto nlex_fed_string = module.nlex_fed_string = module.createGlobal(
llvm::PointerType::get(llvm::Type::getInt8Ty(module.TheContext), 0),
llvm::Constant::getNullValue(llvm::PointerType::get(
Expand Down Expand Up @@ -1085,6 +1096,18 @@ class Builder {
// nlex_restore - restore position from passed in pointer
BB = llvm::BasicBlock::Create(module.TheContext, "", module.nlex_restore);
builder.SetInsertPoint(BB);
if (module.debug_mode) {
builder.CreateCall(
module.nlex_debug,
{
llvm::ConstantInt::get(
llvm::Type::getInt32Ty(module.TheContext),
5), // Restore
builder.CreateLoad(nlex_fed_string),
module.nlex_restore->arg_begin(),
get_or_create_tag(""),
});
}
builder.CreateStore(module.nlex_restore->arg_begin(), nlex_fed_string);
builder.CreateStore(
llvm::ConstantInt::get(llvm::Type::getInt32Ty(module.TheContext), 0),
Expand Down Expand Up @@ -1293,16 +1316,6 @@ class Builder {
_6);
phi->addIncoming(sel, _8);
builder.CreateRet(phi);

auto dfnty = llvm::FunctionType::get(
llvm::Type::getVoidTy(module.TheContext),
{ llvm::Type::getInt32Ty(module.TheContext),
llvm::PointerType::getInt8PtrTy(module.TheContext, 0),
llvm::PointerType::getInt8PtrTy(module.TheContext, 0),
llvm::PointerType::getInt8PtrTy(module.TheContext, 0) },
false);
module.nlex_debug = module.mkfunc(true, "__nlex_produce_debug", false,
false, true, dfnty);
}
// Create a stopword remover if any stopwords are present
if (lexer_stuff.stopwords.size() > 0) {
Expand Down Expand Up @@ -1487,13 +1500,27 @@ class Builder {
auto [nodes, nodebb, offsetidx] = queue.front();
queue.pop();
builder.SetInsertPoint(nodebb);
auto next_position = builder.CreateInBoundsGEP(
strv,
{ llvm::ConstantInt::get(
llvm::Type::getInt32Ty(module.TheContext), offsetidx) });
if (module.debug_mode) {
builder.CreateCall(
module.nlex_debug,
{
llvm::ConstantInt::get(
llvm::Type::getInt32Ty(module.TheContext),
9), // Literal check
next_position,
builder.CreateLoad(
module.last_backtrack_branch_position),
get_or_create_tag(std::to_string(offsetidx)),
});
}
auto swinst = builder.CreateSwitch(
builder.CreateLoad(builder.CreateInBoundsGEP(
strv,
{ llvm::ConstantInt::get(
llvm::Type::getInt32Ty(module.TheContext), offsetidx) })),
builder.CreateLoad(next_position),
start, nodes->elements.size());
for (auto [c, node] : *nodes) {
for (auto& [c, node] : *nodes) {
// EOW - tag matches, emit new tag and return
auto& tag = node->metadata.first;
if (c == 0) {
Expand All @@ -1508,6 +1535,19 @@ class Builder {
for (auto c : node->metadata.second)
module.add_char_to_token(c, builder);

if (module.debug_mode) {
builder.CreateCall(
module.nlex_debug,
{
llvm::ConstantInt::get(
llvm::Type::getInt32Ty(module.TheContext),
8), // EOW
next_position,
builder.CreateLoad(
module.last_backtrack_branch_position),
get_or_create_tag(node->metadata.second),
});
}
// set tag
builder.CreateStore(get_or_create_tag(tag), module.last_tag);
// set matched flag
Expand All @@ -1520,11 +1560,7 @@ class Builder {
// increment nlex_fed
builder.CreateCall(
module.nlex_restore,
{ builder.CreateInBoundsGEP(
builder.CreateCall(module.nlex_current_p),
{ llvm::ConstantInt::get(
llvm::Type::getInt32Ty(module.TheContext),
offsetidx) }) });
{ next_position });
builder.CreateStore(builder.CreateCall(module.nlex_current_p, {}),
module.last_final_state_position);
builder.CreateStore(
Expand Down
7 changes: 5 additions & 2 deletions wrapper/c/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ char const* name) {
case 2: act = "succeed"; break;
case 3: act = "jump"; break;
case 4: act = "backtrack"; break;
case 5: act = "restore"; break;
case 8: act = "EOW literal"; break;
case 9: act = "Check Literal"; break;
default: act = "?"; break;
}
printf("[DEBUG] [%.*s](%#x) %s in %p from regex [%s] :: %p\n", 1+
__nlex_utf8_length(*position), position, *position, act, position, name, data);
auto length = __nlex_utf8_length(*position);
printf("[DEBUG] [%.*s](%#p+%d) %s in %p from regex [%s] :: %p\n", 1 + length, position, position, length, act, position, name, data);
}


Expand Down

0 comments on commit 5e9411f

Please sign in to comment.