Skip to content

Commit 2171587

Browse files
committed
In reuse mode, use block-local options instead of last block options.
Last block options are used as "global" or "final" options in certain cases for code generation: namely, when generating state dispatch and condition enum, which are accumulated over all blocks. In reuse mode, however, state dispatch is generated in every block, so it should use block-local options. This partially fixes #282 "Options -f, -c and -r do not combine well".
1 parent 292a8b0 commit 2171587

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/codegen/code.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,12 +700,13 @@ struct OutputBlock {
700700
std::vector<OutputFragment> fragments;
701701
bool used_yyaccept;
702702
bool have_user_code;
703+
bool is_reuse_block;
703704
std::vector<std::string> types;
704705
std::set<std::string> stags;
705706
std::set<std::string> mtags;
706707
const opt_t *opts;
707708

708-
explicit OutputBlock(const loc_t &loc);
709+
OutputBlock(const loc_t &loc, bool reuse);
709710
~OutputBlock();
710711
FORBID_COPY(OutputBlock);
711712
};
@@ -743,7 +744,7 @@ class Output {
743744
OutputBlock &block();
744745
size_t blockid() const;
745746
bool open ();
746-
void new_block(Opt &opts, const loc_t &loc);
747+
void new_block(Opt &opts, const loc_t &loc, bool reuse);
747748
void header_mode(bool on);
748749
bool in_header() const;
749750
void wraw (const char *s, const char *e);

src/codegen/gen_program.cc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ static uint32_t write_converting_newlines(const std::string &str, FILE *f)
4242
return lines;
4343
}
4444

45-
OutputBlock::OutputBlock(const loc_t &loc)
45+
OutputBlock::OutputBlock(const loc_t &loc, bool reuse)
4646
: loc(loc)
47-
, fragments ()
48-
, used_yyaccept (false)
49-
, have_user_code (false)
50-
, types ()
51-
, stags ()
52-
, mtags ()
47+
, fragments()
48+
, used_yyaccept(false)
49+
, have_user_code(false)
50+
, is_reuse_block(reuse)
51+
, types()
52+
, stags()
53+
, mtags()
5354
, opts(NULL)
5455
{}
5556

@@ -128,9 +129,9 @@ void Output::wdelay_stmt(uint32_t ind, Code *stmt)
128129
block().fragments.push_back(f);
129130
}
130131

131-
void Output::new_block(Opt &opts, const loc_t &loc)
132+
void Output::new_block(Opt &opts, const loc_t &loc, bool reuse)
132133
{
133-
OutputBlock *b = new OutputBlock(loc);
134+
OutputBlock *b = new OutputBlock(loc, reuse);
134135
b->opts = opts.snapshot();
135136
pblocks->push_back(b);
136137

@@ -174,19 +175,18 @@ bool Output::emit_blocks(const std::string &fname, blocks_t &blocks,
174175

175176
fix_first_block_opts(blocks);
176177

177-
// global options are last block's options
178+
// global options are last block options
178179
const opt_t *globopt = block().opts;
179180

180181
unsigned int line_count = 1;
181182
for (unsigned int j = 0; j < blocks.size(); ++j) {
182-
OutputBlock & b = *blocks[j];
183-
const opt_t *bopt = b.opts;
183+
OutputBlock &b = *blocks[j];
184184

185185
CodegenContext gctx =
186186
{ allocator
187187
, scratchbuf
188-
, globopt
189-
, bopt
188+
, b.is_reuse_block ? b.opts : globopt
189+
, b.opts
190190
, msg
191191
, b.loc
192192
, global_types
@@ -208,7 +208,7 @@ bool Output::emit_blocks(const std::string &fname, blocks_t &blocks,
208208

209209
RenderContext rctx =
210210
{ os
211-
, bopt
211+
, b.opts
212212
, msg
213213
, f.indent
214214
, filename.c_str()

src/compile.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ void compile(Scanner &input, Output &output, Opt &opts)
129129
const loc_t &loc0 = input.tok_loc();
130130

131131
output.header_mode(1);
132-
output.new_block(opts, loc0);
132+
output.new_block(opts, loc0, false /* reuse */);
133133
output.wversion_time();
134134

135135
output.header_mode(0);
136-
output.new_block(opts, loc0);
136+
output.new_block(opts, loc0, false /* reuse */);
137137
output.wversion_time();
138138
output.wdelay_stmt(0, code_line_info_input(alc, loc0));
139139

@@ -161,7 +161,7 @@ void compile(Scanner &input, Output &output, Opt &opts)
161161

162162
// start new output block with accumulated options
163163
const loc_t &loc = input.cur_loc();
164-
output.new_block(opts, loc);
164+
output.new_block(opts, loc, mode == Scanner::Reuse);
165165

166166
if (mode == Scanner::Rules) {
167167
// save AST and options for future use

0 commit comments

Comments
 (0)