Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
fbp-generator: Use modules helper for generated code
Browse files Browse the repository at this point in the history
This way, the generated program doesn't need to worry about linking to
node-type modules manually to ensure their symbols are available.

Signed-off-by: Iván Briano <ivan.briano@intel.com>
  • Loading branch information
ibriano committed Sep 28, 2015
1 parent 45548bc commit 95f4755
Showing 1 changed file with 81 additions and 23 deletions.
104 changes: 81 additions & 23 deletions src/bin/sol-fbp-generator/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ struct declared_fbp_type {
int id;
};

struct node_data {
struct type_description *desc;
int type_index;
bool is_declared;
};

static struct port_description error_port = {
.name = (char *)SOL_FLOW_NODE_PORT_ERROR_NAME,
.data_type = (char *)"error",
Expand All @@ -127,9 +133,11 @@ static struct type_description *
get_node_type_description(const struct fbp_data *data, uint16_t i)
{
struct sol_fbp_node *n = sol_vector_get(&data->graph.nodes, i);
struct node_data *nd;

assert(n);
return n->user_data;
nd = n->user_data;
return nd->desc;
}

static void
Expand Down Expand Up @@ -354,6 +362,23 @@ sol_fbp_generator_resolve_type(struct type_store *common_store, struct type_stor
return type_store_find(parent_store, type_name);
}

static struct node_data *
get_node_data(struct type_store *common_store, struct type_store *parent_store, struct sol_fbp_node *n, const char *fbp_file)
{
struct node_data *nd;

nd = calloc(1, sizeof(*nd));
SOL_NULL_CHECK(nd, NULL);

nd->desc = sol_fbp_generator_resolve_type(common_store, parent_store, n, fbp_file);
SOL_NULL_CHECK_GOTO(nd->desc, resolve_error);

return nd;
resolve_error:
free(nd);
return NULL;
}

static int
compare_conn_specs(const void *a, const void *b)
{
Expand Down Expand Up @@ -440,7 +465,7 @@ generate_options(const struct fbp_data *data)
uint16_t i, j;

SOL_VECTOR_FOREACH_IDX (&data->graph.nodes, n, i) {
struct type_description *desc = n->user_data;
struct type_description *desc = ((struct node_data *)n->user_data)->desc;

if (n->meta.len <= 0)
continue;
Expand Down Expand Up @@ -651,8 +676,12 @@ generate_node_type_assignments(const struct fbp_data *data)
out("\n");

SOL_VECTOR_FOREACH_IDX (&data->graph.nodes, n, i) {
struct type_description *desc = n->user_data;
out(" nodes[%d].type = %s;\n", i, desc->symbol);
struct node_data *nd = n->user_data;

if (nd->is_declared)
out(" nodes[%d].type = %s;\n", i, nd->desc->symbol);
else
out(" nodes[%d].type = external_types[%d];\n", i, nd->type_index);
}

SOL_VECTOR_FOREACH_IDX (&data->declared_fbp_types, dec_type, i) {
Expand Down Expand Up @@ -701,6 +730,11 @@ struct generate_context {
struct sol_vector types_to_initialize;
};

struct type_to_init {
struct sol_str_slice symbol;
struct sol_str_slice module;
};

static bool
is_declared_type(struct fbp_data *data, const struct sol_str_slice name)
{
Expand All @@ -715,14 +749,16 @@ is_declared_type(struct fbp_data *data, const struct sol_str_slice name)
}

static bool
contains_slice(const struct sol_vector *v, const struct sol_str_slice name)
contains_slice(const struct sol_vector *v, const struct sol_str_slice name, uint16_t *idx)
{
struct sol_str_slice *slice;
uint16_t i;

SOL_VECTOR_FOREACH_IDX (v, slice, i) {
if (sol_str_slice_eq(*slice, name))
if (sol_str_slice_eq(*slice, name)) {
*idx = i;
return true;
}
}
return false;
}
Expand All @@ -734,36 +770,45 @@ collect_context_info(struct generate_context *ctx, struct fbp_data *data)
uint16_t i;

SOL_VECTOR_FOREACH_IDX (&data->graph.nodes, node, i) {
struct node_data *nd;
struct type_description *desc;
const char *sep;
struct sol_str_slice name, module, symbol;
struct type_to_init *t = NULL;
uint16_t idx;

/* Need to go via descriptions to get the real resolved name,
* after conffile pass. */
desc = node->user_data;
nd = node->user_data;
desc = nd->desc;
name = sol_str_slice_from_str(desc->name);

/* Ignore since these are completely defined in the generated code. */
if (is_declared_type(data, name)) {
nd->is_declared = true;
continue;
}

symbol = sol_str_slice_from_str(desc->symbol);
if (!contains_slice(&ctx->types_to_initialize, symbol)) {
struct sol_str_slice *t;
if (!contains_slice(&ctx->types_to_initialize, symbol, &idx)) {
t = sol_vector_append(&ctx->types_to_initialize);
if (!t)
return false;
*t = symbol;
t->symbol = symbol;
idx = ctx->types_to_initialize.len - 1;
}
nd->type_index = idx;

module = name;
sep = strstr(name.data, "/");
if (sep) {
module.len = sep - module.data;
}

if (!contains_slice(&ctx->modules, module)) {
if (t)
t->module = module;

if (!contains_slice(&ctx->modules, module, &idx)) {
struct sol_str_slice *m;
m = sol_vector_append(&ctx->modules);
if (!m)
Expand Down Expand Up @@ -813,12 +858,14 @@ generate(struct sol_vector *fbp_data_vector)
{
struct generate_context _ctx = {
.modules = SOL_VECTOR_INIT(struct sol_str_slice),
.types_to_initialize = SOL_VECTOR_INIT(struct sol_str_slice),
.types_to_initialize = SOL_VECTOR_INIT(struct type_to_init),
}, *ctx = &_ctx;

struct fbp_data *data;
struct sol_str_slice *module, *symbol;
struct sol_str_slice *module;
struct sol_ptr_vector *memory_maps;
struct type_to_init *type;
int types_count;
uint16_t i;
int r, memmap_elems = 0;

Expand Down Expand Up @@ -854,6 +901,9 @@ generate(struct sol_vector *fbp_data_vector)
}
#endif

types_count = ctx->types_to_initialize.len;
out("\nstatic const struct sol_flow_node_type *external_types[%d];\n", types_count);

/* Reverse since the dependencies appear later in the vector. */
SOL_VECTOR_FOREACH_REVERSE_IDX (fbp_data_vector, data, i) {
if (!generate_create_type_function(data)) {
Expand All @@ -864,22 +914,28 @@ generate(struct sol_vector *fbp_data_vector)
}

out(
"static void\n"
"static bool\n"
"initialize_types(void)\n"
"{\n");
SOL_VECTOR_FOREACH_IDX (&ctx->types_to_initialize, symbol, i) {
"{\n"
" const struct sol_flow_node_type *t;\n"
" int i = 0;\n\n");
SOL_VECTOR_FOREACH_IDX (&ctx->types_to_initialize, type, i) {
out(
" if (%.*s->init_type)\n"
" %.*s->init_type();\n",
SOL_STR_SLICE_PRINT(*symbol),
SOL_STR_SLICE_PRINT(*symbol));
" if (sol_flow_get_node_type(\"%.*s\", %.*s, &t) < 0)\n"
" return false;\n"
" if (t->init_type)\n"
" t->init_type();\n"
" external_types[i++] = t;\n",
SOL_STR_SLICE_PRINT(type->module),
SOL_STR_SLICE_PRINT(type->symbol));
}
if (memmap_elems) {
out("\n");
for (i = 0; i < memmap_elems; i++)
out(" sol_memmap_add_map(&_memmap%d);\n", i);
}
out(
" return true;\n"
"}\n\n");

if (!args.export_symbol) {
Expand All @@ -890,7 +946,8 @@ generate(struct sol_vector *fbp_data_vector)
"static void\n"
"startup(void)\n"
"{\n"
" initialize_types();\n"
" if (!initialize_types())\n"
" return;\n"
" root_type = create_0_root_type();\n"
" if (!root_type)\n"
" return;\n\n"
Expand All @@ -909,7 +966,8 @@ generate(struct sol_vector *fbp_data_vector)
"%s(void) {\n"
" static const struct sol_flow_node_type *type = NULL;\n"
" if (!type) {\n"
" initialize_types();\n"
" if (!initialize_types())\n"
" return NULL;\n"
" type = create_0_root_type();\n"
" }\n"
"\n"
Expand Down Expand Up @@ -1279,7 +1337,7 @@ resolve_node(struct fbp_data *data, struct type_store *common_store)
uint16_t i;

SOL_VECTOR_FOREACH_IDX (&data->graph.nodes, n, i) {
n->user_data = sol_fbp_generator_resolve_type(common_store, data->store, n, data->filename);
n->user_data = get_node_data(common_store, data->store, n, data->filename);
if (!n->user_data)
return false;
SOL_DBG("Node %.*s resolved", SOL_STR_SLICE_PRINT(n->name));
Expand Down

0 comments on commit 95f4755

Please sign in to comment.