bc: "dc only" config does not need G.prog.fn_map

function                                             old     new   delta
bc_program_add_fn                                      -      43     +43
bc_vm_init                                           655     675     +20
bc_program_addFunc                                   138     118     -20
zdc_parse_expr                                       658     635     -23
zdc_program_asciify                                  447     407     -40
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/3 up/down: 63/-83)            Total: -20 bytes
   text	   data	    bss	    dec	    hex	filename
 984739	    489	   7312	 992540	  f251c	busybox_old
 984712	    489	   7312	 992513	  f2501	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-12-20 20:34:09 +01:00
parent 5acd14ba5b
commit 44a99ca617

View File

@ -686,7 +686,7 @@ typedef struct BcProgram {
BcVec exestack; BcVec exestack;
BcVec fns; BcVec fns;
BcVec fn_map; //TODO: dc does not need this, its 'functions' are anonynomous (have no names) IF_BC(BcVec fn_map;)
BcVec vars; BcVec vars;
BcVec var_map; BcVec var_map;
@ -3606,12 +3606,23 @@ static void bc_parse_create(BcParse *p, size_t fidx)
p->func = bc_program_func(fidx); p->func = bc_program_func(fidx);
} }
static size_t bc_program_add_fn(void)
{
size_t idx;
BcFunc f;
bc_func_init(&f);
idx = G.prog.fns.len;
bc_vec_push(&G.prog.fns, &f);
return idx;
}
#if ENABLE_BC
// Note: takes ownership of 'name' (must be malloced) // Note: takes ownership of 'name' (must be malloced)
static size_t bc_program_addFunc(char *name) static size_t bc_program_addFunc(char *name)
{ {
size_t idx; size_t idx;
BcId entry, *entry_ptr; BcId entry, *entry_ptr;
BcFunc f;
int inserted; int inserted;
entry.name = name; entry.name = name;
@ -3630,15 +3641,12 @@ static size_t bc_program_addFunc(char *name)
bc_func_free(func); bc_func_free(func);
bc_func_init(func); bc_func_init(func);
} else { } else {
bc_func_init(&f); bc_program_add_fn();
bc_vec_push(&G.prog.fns, &f);
} }
return idx; return idx;
} }
#if ENABLE_BC
#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
#define BC_PARSE_LEAF(p, rparen) \ #define BC_PARSE_LEAF(p, rparen) \
(((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
@ -4776,19 +4784,22 @@ static BC_STATUS zdc_parse_register(BcParse *p)
static BC_STATUS zdc_parse_string(BcParse *p) static BC_STATUS zdc_parse_string(BcParse *p)
{ {
char *str, *name; char *str;
size_t len = G.prog.strs.len; size_t len = G.prog.strs.len;
//why pad to 32 zeros?? dbg_lex_enter("%s:%d entered", __func__, __LINE__);
name = xasprintf("%032lu", (unsigned long)len);
str = xstrdup(p->l.t.v.v); str = xstrdup(p->l.t.v.v);
bc_parse_push(p, BC_INST_STR); bc_parse_push(p, BC_INST_STR);
bc_parse_pushIndex(p, len); bc_parse_pushIndex(p, len);
bc_vec_push(&G.prog.strs, &str); bc_vec_push(&G.prog.strs, &str);
bc_program_addFunc(name);
// Explanation needed here
bc_program_add_fn();
p->func = bc_program_func(p->fidx); p->func = bc_program_func(p->fidx);
dbg_lex_done("%s:%d done", __func__, __LINE__);
RETURN_STATUS(zbc_lex_next(&p->l)); RETURN_STATUS(zbc_lex_next(&p->l));
} }
#define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__) COMMA_SUCCESS) #define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__) COMMA_SUCCESS)
@ -4843,6 +4854,7 @@ static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
uint8_t inst; uint8_t inst;
bool assign, get_token = false; bool assign, get_token = false;
dbg_lex_enter("%s:%d entered", __func__, __LINE__);
switch (t) { switch (t) {
case BC_LEX_OP_REL_EQ: case BC_LEX_OP_REL_EQ:
case BC_LEX_OP_REL_LE: case BC_LEX_OP_REL_LE:
@ -4857,10 +4869,12 @@ static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
break; break;
case BC_LEX_STR: case BC_LEX_STR:
dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
s = zdc_parse_string(p); s = zdc_parse_string(p);
break; break;
case BC_LEX_NEG: case BC_LEX_NEG:
case BC_LEX_NUMBER: case BC_LEX_NUMBER:
dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__);
if (t == BC_LEX_NEG) { if (t == BC_LEX_NEG) {
s = zbc_lex_next(&p->l); s = zbc_lex_next(&p->l);
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
@ -4903,6 +4917,7 @@ static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
if (!s && get_token) s = zbc_lex_next(&p->l); if (!s && get_token) s = zbc_lex_next(&p->l);
dbg_lex_done("%s:%d done", __func__, __LINE__);
RETURN_STATUS(s); RETURN_STATUS(s);
} }
#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
@ -4911,6 +4926,7 @@ static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
{ {
BcLexType t; BcLexType t;
dbg_lex_enter("%s:%d entered G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
for (;;) { for (;;) {
BcInst inst; BcInst inst;
BcStatus s; BcStatus s;
@ -4920,9 +4936,11 @@ static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
inst = dc_parse_insts[t]; inst = dc_parse_insts[t];
if (inst != BC_INST_INVALID) { if (inst != BC_INST_INVALID) {
dbg_lex("%s:%d", __func__, __LINE__);
bc_parse_push(p, inst); bc_parse_push(p, inst);
s = zbc_lex_next(&p->l); s = zbc_lex_next(&p->l);
} else { } else {
dbg_lex("%s:%d", __func__, __LINE__);
s = zdc_parse_token(p, t, flags); s = zdc_parse_token(p, t, flags);
} }
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
@ -4931,6 +4949,7 @@ static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
if (flags & BC_PARSE_NOCALL) if (flags & BC_PARSE_NOCALL)
bc_parse_push(p, BC_INST_POP_EXEC); bc_parse_push(p, BC_INST_POP_EXEC);
dbg_lex_done("%s:%d done", __func__, __LINE__);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
} }
#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
@ -6122,8 +6141,10 @@ static BC_STATUS zdc_program_asciify(void)
BcStatus s; BcStatus s;
BcResult *r, res; BcResult *r, res;
BcNum *num, n; BcNum *num, n;
char *str, *str2, c; char **strs;
size_t len = G.prog.strs.len, idx; char *str;
char c;
size_t idx;
unsigned long val; unsigned long val;
if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
@ -6155,31 +6176,25 @@ static BC_STATUS zdc_program_asciify(void)
bc_num_free(&n); bc_num_free(&n);
} else { } else {
char *sp;
idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
str2 = *bc_program_str(idx); sp = *bc_program_str(idx);
c = str2[0]; c = sp[0];
} }
strs = (void*)G.prog.strs.v;
for (idx = 0; idx < G.prog.strs.len; idx++) {
if (strs[idx][0] == c && strs[idx][1] == '\0') {
goto dup;
}
}
str = xzalloc(2); str = xzalloc(2);
str[0] = c; str[0] = c;
//str[1] = '\0'; - already is //str[1] = '\0'; - already is
str2 = xstrdup(str);
idx = bc_program_addFunc(str2);
if (idx != len + BC_PROG_REQ_FUNCS) {
for (idx = 0; idx < G.prog.strs.len; ++idx) {
if (strcmp(*bc_program_str(idx), str) == 0) {
len = idx;
break;
}
}
free(str);
} else
bc_vec_push(&G.prog.strs, &str); bc_vec_push(&G.prog.strs, &str);
dup:
res.t = BC_RESULT_STR; res.t = BC_RESULT_STR;
res.d.id.idx = len; res.d.id.idx = idx;
bc_vec_pop(&G.prog.results); bc_vec_pop(&G.prog.results);
bc_vec_push(&G.prog.results, &res); bc_vec_push(&G.prog.results, &res);
@ -6973,7 +6988,7 @@ static BC_STATUS zbc_vm_exec(void)
static void bc_program_free(void) static void bc_program_free(void)
{ {
bc_vec_free(&G.prog.fns); bc_vec_free(&G.prog.fns);
bc_vec_free(&G.prog.fn_map); IF_BC(bc_vec_free(&G.prog.fn_map);)
bc_vec_free(&G.prog.vars); bc_vec_free(&G.prog.vars);
bc_vec_free(&G.prog.var_map); bc_vec_free(&G.prog.var_map);
bc_vec_free(&G.prog.arrs); bc_vec_free(&G.prog.arrs);
@ -6982,8 +6997,8 @@ static void bc_program_free(void)
bc_vec_free(&G.prog.consts); bc_vec_free(&G.prog.consts);
bc_vec_free(&G.prog.results); bc_vec_free(&G.prog.results);
bc_vec_free(&G.prog.exestack); bc_vec_free(&G.prog.exestack);
bc_num_free(&G.prog.last); IF_BC(bc_num_free(&G.prog.last);)
bc_num_free(&G.prog.zero); IF_BC(bc_num_free(&G.prog.zero);)
IF_BC(bc_num_free(&G.prog.one);) IF_BC(bc_num_free(&G.prog.one);)
bc_vec_free(&G.input_buffer); bc_vec_free(&G.input_buffer);
} }
@ -7018,11 +7033,16 @@ static void bc_program_init(void)
IF_BC(bc_num_one(&G.prog.one);) IF_BC(bc_num_one(&G.prog.one);)
bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
//TODO: with "", dc_strings.dc enters infinite loop, ??! //TODO: with "", dc_strings.dc enters infinite loop, ??!
bc_program_addFunc(xstrdup("(m)")); // func #0: main if (IS_BC) {
bc_program_addFunc(xstrdup("(r)")); // func #1: for read() IF_BC(bc_program_addFunc(xstrdup("(m)"))); // func #0: main
IF_BC(bc_program_addFunc(xstrdup("(r)"))); // func #1: for read()
} else {
bc_program_add_fn();
bc_program_add_fn();
}
bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);