Skip to content
Snippets Groups Projects
Commit 7534d6fa authored by ben.franksen's avatar ben.franksen
Browse files

finally found the right solution to variable initialization, added array initializers

parent d7ca66af
No related branches found
No related tags found
No related merge requests found
...@@ -158,7 +158,13 @@ static void gen_user_var(Program *p) ...@@ -158,7 +158,13 @@ static void gen_user_var(Program *p)
gen_line_marker(vp->decl); gen_line_marker(vp->decl);
if (!opt_reent) printf("static"); if (!opt_reent) printf("static");
indent(1); indent(1);
gen_var_decl(vp); printf(";\n"); gen_var_decl(vp);
if (!opt_reent)
{
printf(" = ");
gen_var_init(vp, 0);
}
printf(";\n");
} }
} }
foreach (ssp, p->prog->prog_statesets) foreach (ssp, p->prog->prog_statesets)
...@@ -200,10 +206,17 @@ static void gen_user_var(Program *p) ...@@ -200,10 +206,17 @@ static void gen_user_var(Program *p)
printf("} %s_%s;\n", VAR_PREFIX, sp->value); printf("} %s_%s;\n", VAR_PREFIX, sp->value);
} }
} }
indent(level); printf("} %s_%s;\n", VAR_PREFIX, ssp->value); indent(level); printf("} %s_%s", VAR_PREFIX, ssp->value);
if (!opt_reent)
{
printf(" = ");
gen_ss_user_var_init(ssp, level);
}
printf(";\n");
} }
} }
if (opt_reent) printf("};\n"); if (opt_reent) printf("};\n");
printf("\n");
} }
/* Generate C code in definition section */ /* Generate C code in definition section */
......
...@@ -136,7 +136,7 @@ static void gen_prog_func( ...@@ -136,7 +136,7 @@ static void gen_prog_func(
const char *name, const char *name,
Expr *xp, Expr *xp,
void (*gen_body)(Expr *xp)); void (*gen_body)(Expr *xp));
static void gen_prog_init_func(Expr *prog); static void gen_prog_init_func(Expr *prog, int opt_reent);
/* /*
* Expression context. Certain nodes of the syntax tree are * Expression context. Certain nodes of the syntax tree are
...@@ -216,7 +216,7 @@ void gen_ss_code(Program *program) ...@@ -216,7 +216,7 @@ void gen_ss_code(Program *program)
Expr *sp; Expr *sp;
/* Generate program init func */ /* Generate program init func */
gen_prog_init_func(prog); gen_prog_init_func(prog, program->options.reent);
/* Generate program entry func */ /* Generate program entry func */
if (prog->prog_entry) if (prog->prog_entry)
...@@ -291,6 +291,40 @@ static void gen_local_var_decls(Expr *scope, int level) ...@@ -291,6 +291,40 @@ static void gen_local_var_decls(Expr *scope, int level)
} }
} }
static void gen_type_default(Type *type)
{
uint n;
assert(type);
switch(type->tag)
{
case V_STRING:
printf("\"\"");
break;
case V_ARRAY:
printf("{");
for (n=0; n<type->val.array.num_elems; n++)
{
gen_type_default(type->val.array.elem_type);
if (n+1<type->val.array.num_elems) printf(",");
}
printf("}");
break;
default:
printf("0");
}
}
void gen_var_init(Var *vp, int level)
{
assert(vp);
if (vp->init)
gen_expr(C_INIT, vp->init, level);
else
gen_type_default(vp->type);
}
static void gen_state_func( static void gen_state_func(
const char *ss_name, const char *ss_name,
const char *state_name, const char *state_name,
...@@ -615,6 +649,16 @@ static void gen_expr( ...@@ -615,6 +649,16 @@ static void gen_expr(
} }
printf(")"); printf(")");
break; break;
case E_INIT:
printf("{");
foreach (cep, ep->init_elems)
{
gen_expr(context, cep, 0);
if (cep->next)
printf(", ");
}
printf("}");
break;
case E_TERNOP: case E_TERNOP:
gen_expr(context, ep->ternop_cond, 0); gen_expr(context, ep->ternop_cond, 0);
printf(" ? "); printf(" ? ");
...@@ -945,51 +989,92 @@ static void gen_pv_func( ...@@ -945,51 +989,92 @@ static void gen_pv_func(
#endif #endif
} }
/* Generate initialisation code for one element of the UserVar struct. */ /* Generate initializer for the UserVar structs. */
static int iter_user_var_init(Expr *dp, Expr *scope, void *parg) void gen_ss_user_var_init(Expr *ssp, int level)
{ {
assert(dp); Var *vp;
assert(dp->type == D_DECL); Expr *sp;
Var *vp = dp->extra.e_decl;
assert(vp); assert(ssp->type == D_SS);
if (vp->init && vp->decl) printf("{\n");
foreach(vp, ssp->extra.e_ss->var_list->first)
{ {
if (vp->type->tag < V_CHAR) indent(level+1); gen_var_init(vp, level+1); printf(",\n");
{ }
error_at_expr(vp->decl, foreach (sp, ssp->ss_states)
"initialisation not allowed for variables of this type"); {
} int s_empty;
else
assert(sp->type == D_STATE);
s_empty = !sp->extra.e_state->var_list->first;
if (!s_empty)
{ {
gen_line_marker(dp); indent(level+1); printf("{\n");
indent(1); foreach (vp, sp->extra.e_state->var_list->first)
gen_var_access(vp); {
printf(" = "); indent(level+2); gen_var_init(vp, level+2);
gen_expr(C_INIT, vp->init, 0); printf("%s\n", vp->next ? "," : "");
printf(";\n"); }
indent(level+1);
printf("}%s\n", sp->next ? "," : "");
} }
} }
return FALSE; /* do not descend into children */ indent(level); printf("}");
} }
/* Generate initialisation for variables with global lifetime. */ /* Generate initializer for the UserVar structs. */
static void gen_user_var_init(Expr *ep, int stop_mask) static void gen_user_var_init(Expr *prog, int level)
{ {
const int type_mask = (1<<D_DECL); Var *vp;
traverse_expr_tree(ep, type_mask, stop_mask, 0, iter_user_var_init, 0); Expr *ssp;
assert(prog->type == D_PROG);
printf("{\n");
/* global variables */
foreach(vp, prog->extra.e_prog->first)
{
if (vp->type->tag >= V_CHAR)
{
indent(level+1); gen_var_init(vp, level+1); printf(",\n");
}
}
foreach (ssp, prog->prog_statesets)
{
Expr *sp;
int ss_empty = !ssp->extra.e_ss->var_list->first;
if (ss_empty)
{
foreach (sp, ssp->ss_states)
{
if (sp->extra.e_state->var_list->first)
{
ss_empty = 0;
break;
}
}
}
if (!ss_empty)
{
indent(level+1);
gen_ss_user_var_init(ssp, level+1);
printf("%s\n", ssp->next ? "," : "");
}
}
indent(level); printf("}");
} }
static void gen_prog_init_func(Expr *prog) static void gen_prog_init_func(Expr *prog, int opt_reent)
{ {
const int global_stop_mask = ~((1<<D_DECL)|(1<<D_PROG));
assert(prog->type == D_PROG); assert(prog->type == D_PROG);
printf("\n/* Program init func */\n"); printf("\n/* Program init func */\n");
printf("static void global_prog_init(struct %s *pVar)\n{\n", VAR_PREFIX); printf("static void global_prog_init(struct %s *pVar)\n{\n", VAR_PREFIX);
/* initialize global variables */ if (opt_reent)
gen_user_var_init(prog, global_stop_mask); {
indent(1); printf("*pVar = (struct %s)", VAR_PREFIX);
gen_user_var_init(prog, 1);
printf(";\n");
}
printf("}\n"); printf("}\n");
} }
......
...@@ -16,5 +16,7 @@ ...@@ -16,5 +16,7 @@
void init_gen_ss_code(Program *program); void init_gen_ss_code(Program *program);
void gen_ss_code(Program *program); void gen_ss_code(Program *program);
void gen_ss_user_var_init(Expr *ssp, int level);
void gen_var_init(Var *vp, int level);
#endif /*INCLgensscodeh*/ #endif /*INCLgensscodeh*/
...@@ -164,7 +164,7 @@ init_declarators(p) ::= init_declarators(xs) COMMA init_declarator(x). ...@@ -164,7 +164,7 @@ init_declarators(p) ::= init_declarators(xs) COMMA init_declarator(x).
{ p = link_expr(xs, x); } { p = link_expr(xs, x); }
init_declarator(p) ::= declarator(x). { p = decl_add_init(x, 0); } init_declarator(p) ::= declarator(x). { p = decl_add_init(x, 0); }
init_declarator(p) ::= declarator(x) EQUAL expr(i). init_declarator(p) ::= declarator(x) EQUAL init_expr(i).
{ p = decl_add_init(x, i); } { p = decl_add_init(x, i); }
declarator(p) ::= ASTERISK declarator(x). { p = decl_prefix_pointer(x); } declarator(p) ::= ASTERISK declarator(x). { p = decl_prefix_pointer(x); }
...@@ -176,6 +176,14 @@ direct_declarator(p) ::= LPAREN declarator(x) RPAREN. ...@@ -176,6 +176,14 @@ direct_declarator(p) ::= LPAREN declarator(x) RPAREN.
direct_declarator(p) ::= direct_declarator(x) subscript(s). direct_declarator(p) ::= direct_declarator(x) subscript(s).
{ p = decl_postfix_array(x, s.str); } { p = decl_postfix_array(x, s.str); }
// Initializer
init_expr(p) ::= LBRACE(t) init_exprs(x) RBRACE.{ p = expr(E_INIT, t, x); }
init_expr(p) ::= expr(x). { p = x; }
init_exprs(p) ::= init_exprs(xs) COMMA init_expr(x). { p = link_expr(xs, x); }
init_exprs(p) ::= init_expr(x). { p = x; }
init_exprs(p) ::= . { p = 0; }
%type type {int} %type type {int}
type(p) ::= CHAR. { p = V_CHAR; } type(p) ::= CHAR. { p = V_CHAR; }
type(p) ::= SHORT. { p = V_SHORT; } type(p) ::= SHORT. { p = V_SHORT; }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment