Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Do not generate YYDEBUG statements for unused states labels.
Currently DFA states with unused labels still have their own index, but
this will change soon and there won't be any state number to pass to
YYDEBUG. This patch prepares the code by removing YYDEBUG statements for
states with unused labels.

One exception is the initial label: it may be unused (if there are no
transitions into the initial state), but still have a valid index. Keep
YYDEBUG statements for the initial label as they may be useful.
  • Loading branch information
skvadrik committed Jan 2, 2022
1 parent 705e687 commit a46f01e
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 610 deletions.
10 changes: 9 additions & 1 deletion src/codegen/code.h
@@ -1,6 +1,7 @@
#ifndef _RE2C_CODEGEN_CODE_
#define _RE2C_CODEGEN_CODE_

#include <assert.h>
#include <stddef.h>
#include "src/util/c99_stdint.h"
#include <string.h>
Expand Down Expand Up @@ -69,10 +70,16 @@ class Scratchbuf {
Scratchbuf& u64(uint64_t u) { os << u; return *this; }
Scratchbuf& str(const std::string &s) { os << s; return *this; }
Scratchbuf& cstr(const char *s) { os << s; return *this; }
Scratchbuf& label(const Label &l) { os << l.index; return *this; }
Scratchbuf& u32_width(uint32_t u, int width);
Scratchbuf& yybm_char(uint32_t u, const opt_t *opts, int width);
Scratchbuf& exact_uint(size_t width);

Scratchbuf& label(const Label &l, bool check_used = false) {
// TODO: enforce `check_used` by default.
assert((l.used || !check_used) && l.index != Label::NONE);
os << l.index;
return *this;
}
};

template<typename T>
Expand Down Expand Up @@ -802,6 +809,7 @@ std::string vartag_expr(tagver_t ver, const opt_t *opts,
const std::set<tagver_t> &mtagvers);
void output_version_time(std::ostream &os, const opt_t *opts);
void gen_peek_expr(std::ostream &os, const opt_t *opts);
void gen_yydebug(Output &output, const Label *label, CodeList *stmts);

} // namespace re2c

Expand Down
33 changes: 21 additions & 12 deletions src/codegen/gen_dfa.cc
Expand Up @@ -22,21 +22,30 @@

namespace re2c {

static void emit_state(Output &output, const State *s, CodeList *stmts)
{
void gen_yydebug(Output &output, const Label *label, CodeList *stmts) {
const opt_t *opts = output.block().opts;
code_alc_t &alc = output.allocator;
Scratchbuf &o = output.scratchbuf;
Scratchbuf &buf = output.scratchbuf;

if (!opts->loop_switch) {
append(stmts, code_nlabel(alc, s->label));
}
if (!opts->dFlag) return;

if (opts->dFlag && (s->action.type != Action::INITIAL)) {
o.str(opts->yydebug).cstr("(").label(*s->label).cstr(", ");
gen_peek_expr(o.stream(), opts);
o.cstr(")");
append(stmts, code_stmt(alc, o.flush()));
// The label may be unused but still have a valid index (one such example is the
// initial label in goto/label mode). It still needs an YYDEBUG statement.
buf.str(opts->yydebug).cstr("(").label(*label, /*check_used*/ false).cstr(", ");
gen_peek_expr(buf.stream(), opts);
buf.cstr(")");
append(stmts, code_stmt(output.allocator, buf.flush()));
}

static void emit_state(Output &output, const State *state, CodeList *stmts) {
// If state label is unused, we should not generate it.
// Nor can we emit an YYDEBUG statement, as there is no state number to pass to it.
if (!state->label->used) return;

if (!output.block().opts->loop_switch) {
append(stmts, code_nlabel(output.allocator, state->label));
}
if (state->action.type != Action::INITIAL) {
gen_yydebug(output, state->label, stmts);
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/codegen/gen_state.cc
Expand Up @@ -70,11 +70,7 @@ void emit_action(Output &output, const DFA &dfa, const State *s, CodeList *stmts
append(stmts, code_skip(alc));
}
append(stmts, code_nlabel(alc, dfa.initial_label));
if (opts->dFlag) {
text = o.str(opts->yydebug).cstr("(").label(*dfa.initial_label)
.cstr(", *").str(opts->yycursor).cstr(")").flush();
append(stmts, code_stmt(alc, text));
}
gen_yydebug(output, dfa.initial_label, stmts);
gen_fill_and_label(output, stmts, dfa, s);
if (backup) {
append(stmts, code_backup(alc));
Expand Down
6 changes: 0 additions & 6 deletions test/encodings/calc_001_ebcdic.c
Expand Up @@ -44,22 +44,18 @@ int scan(char *s, int l)
yy2:
YYDEBUG(2, *YYCURSOR);
++YYCURSOR;
YYDEBUG(3, *YYCURSOR);
{ printf("EOF\n"); return 0; }
yy4:
YYDEBUG(4, *YYCURSOR);
++YYCURSOR;
YYDEBUG(5, *YYCURSOR);
{ printf("ERR\n"); return 1; }
yy6:
YYDEBUG(6, *YYCURSOR);
++YYCURSOR;
YYDEBUG(7, *YYCURSOR);
{ printf("+\n"); continue; }
yy8:
YYDEBUG(8, *YYCURSOR);
++YYCURSOR;
YYDEBUG(9, *YYCURSOR);
{ printf("-\n"); continue; }
yy10:
YYDEBUG(10, *YYCURSOR);
Expand All @@ -85,7 +81,6 @@ int scan(char *s, int l)
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
YYDEBUG(13, *YYCURSOR);
switch (yych) {
case 0xF0 /* 0 */:
case 0xF1 /* 1 */:
Expand All @@ -107,7 +102,6 @@ int scan(char *s, int l)
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
YYDEBUG(16, *YYCURSOR);
switch (yych) {
case 0xF0 /* 0 */:
case 0xF1 /* 1 */:
Expand Down

0 comments on commit a46f01e

Please sign in to comment.