Preparationn for macroizing the last eight methods.

This commit collects several minor changes:

* Fix a minor type specification bug in a tablesext initializer.

* macroize the trans_offset, mkctbl, and mkftbl methods.

* Fix a bug in footprint computation.

This commit oroduces no code diffs in the generated test code, but the
footprint reports change due to the bug fix.

#66 in the retargeting patch series
This commit is contained in:
Eric S. Raymond 2020-10-06 02:55:43 -04:00
parent 716cb63ff2
commit fd8748a311
5 changed files with 62 additions and 84 deletions

View File

@ -1522,9 +1522,11 @@ struct yytbl_dmap {
/** A {0,0,0}-terminated list of structs, forming the map */
static struct yytbl_dmap yydmap[] = {
m4_ifdef([[M4_MODE_FULLSPD]], [[ {YYTD_ID_START_STATE_LIST, (void**)&yy_start_state_list, sizeof(struct yy_trans_info*)},]])
m4_ifdef([[M4_MODE_YYLINENO]], [[ {YYTD_ID_RULE_CAN_MATCH_EOL, (void**)&yy_rule_can_match_eol, sizeof(flex_int32_t)},]])
m4_ifdef([[M4_MODE_YYLINENO]], [[ {YYTD_ID_RULE_CAN_MATCH_EOL, (void**)&yy_rule_can_match_eol, sizeof(M4_HOOK_EOLTABLE_TYPE)},]])
m4_ifdef([[M4_MODE_USEECS]], [[ {YYTD_ID_EC, (void**)&yy_ec, sizeof(YY_CHAR)},]])
m4_ifdef([[M4_MODE_USEMECS]], [[ {YYTD_ID_META, (void**)&yy_meta, sizeof(YY_CHAR)},]])
m4_ifdef([[M4_HOOK_MKCTBL_TYPE]], [[ {YYTD_ID_TRANSITION, (void**)&yy_transition, sizeof(M4_HOOK_MKCTBL_TYPE)},]])
m4_ifdef([[M4_HOOK_MKFTBL_TYPE]], [[ {YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(M4_HOOK_MKFTBL_TYPE)},]])
%tables-yydmap generated elements
{0,0,0}
};

View File

@ -83,119 +83,101 @@ static const char *cpp_suffix (void)
static void cpp_ntod(size_t num_full_table_rows)
// Generate nxt table for ntod
{
buf_prints (&yydmap_buf,
"\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(%s)},\n",
ctrl.long_align ? "flex_int32_t" : "flex_int16_t");
struct packtype_t *ptype = optimize_pack(0);
out_str ("m4_define([[M4_HOOK_NXT_TYPE]], [[%s]])", ptype->name);
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(M4_HOOK_NXT_TYPE)},\n");
/* Unless -Ca, declare it "short" because it's a real
* long-shot that that won't be large enough.
*/
if (gentables)
out_str_dec
("static const %s yy_nxt[][%d] =\n {\n",
ctrl.long_align ? "flex_int32_t" : "flex_int16_t",
num_full_table_rows);
out_dec
("static const M4_HOOK_NXT_TYPE yy_nxt[][%d] =\n {\n", num_full_table_rows);
else {
out_dec ("#undef YY_NXT_LOLEN\n#define YY_NXT_LOLEN (%d)\n", num_full_table_rows);
out_str ("static const %s *yy_nxt =0;\n",
ctrl.long_align ? "flex_int32_t" : "flex_int16_t");
out ("static const M4_HOOK_NXT_TYPE *yy_nxt =0;\n");
}
/* It would be no good trying to return an allocation size here,
* as it's not known before table generation is finished.
*/
}
static void cpp_mkctbl (size_t sz)
// Make full-speed compressed transition table
{
buf_prints (&yydmap_buf,
"\t{YYTD_ID_TRANSITION, (void**)&yy_transition, sizeof(%s)},\n",
(sz >= INT16_MAX
|| ctrl.long_align) ? "flex_int32_t" : "flex_int16_t");
}
static void cpp_mkftbl(void)
// Make full table
{
// FIXME: why are there two places this is emitted, here and in cpp_gentabs_accept()?
buf_prints (&yydmap_buf,
"\t{YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(%s)},\n",
ctrl.long_align ? "flex_int32_t" : "flex_int16_t");
}
static size_t cpp_gentabs_acclist(size_t sz)
// Generate accept list initializer
{
struct packtype_t *ptype = optimize_pack(0);
out_str ("m4_define([[M4_HOOK_ACCLIST_TYPE]], [[%s]])", ptype->name);
out_str_dec (ctrl.long_align ? cpp_get_int32_decl () :
cpp_get_int16_decl (), "yy_acclist", sz);
buf_prints (&yydmap_buf,
"\t{YYTD_ID_ACCLIST, (void**)&yy_acclist, sizeof(%s)},\n",
ctrl.long_align ? "flex_int32_t" : "flex_int16_t");
return sz * (ctrl.long_align ? sizeof(int32_t) : sizeof(int16_t));
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_ACCLIST, (void**)&yy_acclist, sizeof(M4_HOOK_ACCLIST_TYPE)},\n");
return sz * ptype->width;
}
static size_t cpp_gentabs_accept(size_t sz)
// Generate accept table initializer
{
/* FIXME: Could pack tighter by passing the size limit to optimize_pack()_ */
struct packtype_t *ptype = optimize_pack(0);
out_str ("m4_define([[M4_HOOK_ACCEPT_TYPE]], [[%s]])", ptype->name);
out_str_dec (ctrl.long_align ? cpp_get_int32_decl () : cpp_get_int16_decl (),
"yy_accept", sz);
buf_prints (&yydmap_buf,
"\t{YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(%s)},\n",
ctrl.long_align ? "flex_int32_t" : "flex_int16_t");
return sz * (ctrl.long_align ? sizeof(int32_t) : sizeof(int16_t));
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(M4_HOOK_ACCEPT_TYPE)},\n");
return sz * ptype->width;
}
static size_t cpp_gentabs_yy_base(size_t sz)
// Generate yy_meta base initializer
{
out_str_dec ((tblend >= INT16_MAX || ctrl.long_align) ?
struct packtype_t *ptype = optimize_pack(sz);
out_str ("m4_define([[M4_HOOK_BASE_TYPE]], [[%s]])", ptype->name);
out_str_dec ((sz >= INT16_MAX || ctrl.long_align) ?
cpp_get_int32_decl () : cpp_get_int16_decl (),
"yy_base", sz);
buf_prints (&yydmap_buf,
"\t{YYTD_ID_BASE, (void**)&yy_base, sizeof(%s)},\n",
(sz >= INT16_MAX
|| ctrl.long_align) ? "flex_int32_t" : "flex_int16_t");
return sz * ((sz >= INT16_MAX || ctrl.long_align) ? sizeof(int32_t) : sizeof(int16_t));
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_BASE, (void**)&yy_base, sizeof(M4_HOOK_BASE_TYPE)},\n");
return sz * ptype->width;
}
static size_t cpp_gentabs_yy_def(size_t sz)
// Generate yy_def initializer
{
struct packtype_t *ptype = optimize_pack(sz);
out_str ("m4_define([[M4_HOOK_DEF_BASE_TYPE]], [[%s]])", ptype->name);
out_str_dec ((sz >= INT16_MAX || ctrl.long_align) ?
cpp_get_int32_decl () : cpp_get_int16_decl (),
"yy_def", sz);
buf_prints (&yydmap_buf,
"\t{YYTD_ID_DEF, (void**)&yy_def, sizeof(%s)},\n",
(sz >= INT16_MAX
|| ctrl.long_align) ? "flex_int32_t" : "flex_int16_t");
return sz * ((sz >= INT16_MAX || ctrl.long_align) ? sizeof(int32_t) : sizeof(int16_t));
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_DEF, (void**)&yy_def, sizeof(M4_HOOK_DEF_BASE_TYPE)},\n");
return sz * ptype->width;
}
static size_t cpp_gentabs_yy_nxt(size_t tblafter)
// Generate yy_nxt initializer
{
/* Begin generating yy_nxt */
struct packtype_t *ptype = optimize_pack(tblafter);
out_str ("m4_define([[M4_HOOK_NXT_BASE_TYPE]], [[%s]])", ptype->name);
out_str_dec ((tblafter >= INT16_MAX || ctrl.long_align) ?
cpp_get_int32_decl () : cpp_get_int16_decl (), "yy_nxt",
tblafter);
buf_prints (&yydmap_buf,
"\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(%s)},\n",
(tblafter >= INT16_MAX
|| ctrl.long_align) ? "flex_int32_t" : "flex_int16_t");
return tblafter * ((tblafter >= INT16_MAX || ctrl.long_align) ? sizeof(int32_t) : sizeof(int16_t));
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(M4_HOOK_NXT_BASE_TYPE)},\n");
return tblafter * ptype->width;
}
static size_t cpp_gentabs_yy_chk(size_t tblafter)
// Generate yy_chk initializer
{
struct packtype_t *ptype = optimize_pack(tblafter);
out_str ("m4_define([[M4_HOOK_IDCHK_BASE_TYPE]], [[%s]])", ptype->name);
out_str_dec ((tblafter >= INT16_MAX || ctrl.long_align) ?
cpp_get_int32_decl () : cpp_get_int16_decl (), "yy_chk",
tblafter);
buf_prints (&yydmap_buf,
"\t{YYTD_ID_CHK, (void**)&yy_chk, sizeof(%s)},\n",
(tblafter >= INT16_MAX
|| ctrl.long_align) ? "flex_int32_t" : "flex_int16_t");
return tblafter * ((tblafter >= INT16_MAX || ctrl.long_align) ? sizeof(int32_t) : sizeof(int16_t));
buf_strappend (&yydmap_buf,
"\t{YYTD_ID_CHK, (void**)&yy_chk, sizeof(M4_HOOK_IDCHK_BASE_TYPE)},\n");
return tblafter * ptype->width;
}
static size_t cpp_nultrans(int fullspd, size_t afterdfa)
@ -211,12 +193,6 @@ static size_t cpp_nultrans(int fullspd, size_t afterdfa)
return afterdfa * (fullspd ? sizeof(struct yy_trans_info *) : sizeof(int32_t));
}
static const char *cpp_trans_offset_type(int total_table_size)
{
return (total_table_size >= INT16_MAX || ctrl.long_align) ?
"flex_int32_t" : "flex_int16_t";
}
const char *cpp_skel[] = {
#include "cpp-skel.h"
0,
@ -227,8 +203,6 @@ struct flex_backend_t cpp_backend = {
.suffix = cpp_suffix,
.skel = cpp_skel,
.ntod = cpp_ntod,
.mkctbl = cpp_mkctbl,
.mkftbl = cpp_mkftbl,
.gentabs_acclist = cpp_gentabs_acclist,
.gentabs_accept = cpp_gentabs_accept,
.gentabs_yy_base = cpp_gentabs_yy_base,
@ -236,6 +210,5 @@ struct flex_backend_t cpp_backend = {
.gentabs_yy_nxt = cpp_gentabs_yy_nxt,
.gentabs_yy_chk = cpp_gentabs_yy_chk,
.nultrans = cpp_nultrans,
.trans_offset_type = cpp_trans_offset_type,
.c_like = true,
};

View File

@ -306,10 +306,7 @@
struct flex_backend_t {
const char *(*suffix)(void); // Generate suffix for lexer source code
const char **skel;
// Flex table generation
void (*ntod)(size_t); // Generate nxt table initializer start (fulltbl mode)
void (*mkctbl)(size_t); // Make full-speed compressed table initializer start
void (*mkftbl)(); // Make full table serialization entry
size_t (*genftbl)(size_t); // Make full table initializer start
size_t (*gentabs_acclist)(size_t); // Generate accept list initializer start
size_t (*gentabs_accept)(size_t); // Generate accept table initializer start
@ -318,7 +315,6 @@ struct flex_backend_t {
size_t (*gentabs_yy_nxt)(size_t); // Generate yy_nxt initializer start
size_t (*gentabs_yy_chk)(size_t); // Generate yy_chk initializer start
size_t (*nultrans)(int, size_t); // Generate nulltrans initializer
const char *(*trans_offset_type)(int); // Compute an efficient type for transition tables
bool c_like; // Will &yy_transition[%d]," produce a pointer table entry?
};
@ -427,6 +423,12 @@ struct env_bundle_t {
bool use_stdout; // the -t flag
};
/* Name and byte-width information on a type for code-generation purposes. */
struct packtype_t {
char *name;
size_t width;
};
extern struct ctrl_bundle_t ctrl;
extern struct env_bundle_t env;
@ -825,8 +827,11 @@ extern void indent_put2s(const char *, const char *);
/* Write out a string + newline at the current indentation level. */
extern void indent_puts(const char *);
extern void make_tables(void); /* generate transition tables */
/* generate transition tables */
extern void make_tables(void);
/* Select a type for optimal packing */
struct packtype_t *optimize_pack(size_t);
/* from file main.c */

View File

@ -45,13 +45,7 @@ struct yy_trans_info {int32_t yy_verify; int32_t yy_nxt;};
void genecs(void);
struct packtype_t {
char *name;
size_t width;
};
static struct packtype_t *optimize_pack(size_t sz)
struct packtype_t *optimize_pack(size_t sz)
{
/* FIXME: There's a 32-bit assumption lurking here */
static struct packtype_t out;
@ -59,8 +53,8 @@ static struct packtype_t *optimize_pack(size_t sz)
out.name = ctrl.long_align ? "flex_int32_t" : "flex_int16_t";
out.width = ctrl.long_align ? 32 : 16;
} else {
out.name = (ctrl.long_align || sz <= INT16_MAX) ? "flex_int32_t" : "flex_ini16_t";
out.width = (ctrl.long_align || sz <= INT16_MAX) ? 32 : 16;
out.name = (ctrl.long_align || sz >= INT16_MAX) ? "flex_int32_t" : "flex_int16_t";
out.width = (ctrl.long_align || sz >= INT16_MAX) ? 32 : 16;
}
return &out;
}
@ -126,7 +120,8 @@ static struct yytbl_data *mkctbl (void)
flex_int32_t *tdata = 0, curr = 0;
int end_of_buffer_action = num_rules + 1;
backend->mkctbl(tblend + numecs + 1);
struct packtype_t *ptype = optimize_pack(tblend + numecs + 1);
out_str ("m4_define([[M4_HOOK_MKCTBL_TYPE]], [[%s]])", ptype->name);
tbl = calloc(1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_TRANSITION);
@ -431,7 +426,6 @@ struct yytbl_data *mkftbl (void)
i, anum);
}
backend->mkftbl();
return tbl;
}
@ -442,6 +436,7 @@ void genftbl (void)
{
int i;
int end_of_buffer_action = num_rules + 1;
/* FIXME: Could make this smaller by passing the table size to pack_optimize(). */
struct packtype_t *ptype = optimize_pack(0);
dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
@ -906,6 +901,8 @@ void make_tables (void)
if (tablesext) {
struct yytbl_data *tbl;
struct packtype_t *ptype = optimize_pack(0);
out_str ("m4_define([[M4_HOOK_MKFTBL_TYPE]], [[%s]])", ptype->name);
tbl = mkftbl ();
yytbl_data_compress (tbl);
if (yytbl_data_fwrite (&tableswr, tbl) < 0)
@ -1014,6 +1011,7 @@ void make_tables (void)
if (ctrl.ddebug) { /* Spit out table mapping rules to line numbers. */
/* Policy choice: we don't include this space
* in the table metering.
* FIXME: Could make this smaller by passing the table size to optimize_pack().
*/
struct packtype_t *ptype = optimize_pack(0);

View File

@ -207,7 +207,7 @@ int flex_main (int argc, char *argv[])
/* Need to define the transet type as a size large
* enough to hold the biggest offset.
*/
out_str3 ("M4_HOOK_CONST_DEFINE(%s, %s)", "YY_OFFSET_TYPE", backend->trans_offset_type(tblend + numecs + 1), "");
out_str3 ("M4_HOOK_CONST_DEFINE(%s, %s)", "YY_OFFSET_TYPE", optimize_pack(tblend + numecs + 1)->name, "");
comment("END of Flex-generated definitions\n");
skelout (true); /* %% [2.0] - tables get dumped here */