Skip to content

Commit

Permalink
making exported vars available from C code
Browse files Browse the repository at this point in the history
Addes an array of exports to runtime data. Also declares each export as an
integer. The value of the export can be passed to colm_get_gloal(). They are
named colm_export_<export-name>. refs #116.
  • Loading branch information
adrian-thurston committed Apr 10, 2020
1 parent 848214f commit 9760b2b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/pdabuild.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,36 @@ void Compiler::makeRuntimeData()

runtimeData->global_size = globalObjectDef->size();

/*
* Exports.
*/
count = 0;
for ( FieldList::Iter of = globalObjectDef->fieldList; of.lte(); of++ ) {
ObjectField *field = of->value;
if ( field->isExport ) {
count += 1;
}
}

runtimeData->num_exports = count;
if ( count == 0 ) {
runtimeData->export_info = 0;
}
else {
runtimeData->export_info = new export_info[count];

long i = 0;
for ( FieldList::Iter of = globalObjectDef->fieldList; of.lte(); of++ ) {
ObjectField *field = of->value;
if ( field->isExport ) {
runtimeData->export_info[i].name = strdup( field->name );
runtimeData->export_info[i].global_id = field->offset;
i += 1;
}
}
}


/*
* Boundary between terms and non-terms.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/pdacodegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ void PdaCodeGen::writeRuntimeData( colm_sections *runtimeData, struct pda_tables
"void " << objectName << "_read_reduce( program_t *prg, int reducer, input_t *stream );\n"
"\n";

out <<
"static struct export_info " << exportInfo() << "[] = {\n";

for ( long i = 0; i < runtimeData->num_exports; i++ ) {
out << " { \"" << runtimeData->export_info[i].name << "\", " <<
runtimeData->export_info[i].global_id << " },\n";
}

out <<
"};\n";

for ( long i = 0; i < runtimeData->num_exports; i++ ) {
out << "const int colm_export_" << runtimeData->export_info[i].name << " = " <<
runtimeData->export_info[i].global_id << ";\n";
}
out <<
"\n";

out <<
"struct colm_sections " << objectName << " = \n"
"{\n"
Expand Down Expand Up @@ -486,6 +504,10 @@ void PdaCodeGen::writeRuntimeData( colm_sections *runtimeData, struct pda_tables
"\n"
" " << genericInfo() << ",\n"
" " << runtimeData->num_generics << ",\n"
"\n"
" " << exportInfo() << ",\n"
" " << runtimeData->num_exports << ",\n"
"\n"
" " << runtimeData->argv_generic_id << ",\n"
" " << runtimeData->stds_generic_id << ",\n"
"\n"
Expand Down
1 change: 1 addition & 0 deletions src/pdacodegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct PdaCodeGen
String litlen() { return PARSER() + "litlen"; }
String literals() { return PARSER() + "literals"; }
String fsmTables() { return PARSER() + "fsmTables"; }
String exportInfo() { return PARSER() + "exportInfo"; }

/*
* Graphviz Generation
Expand Down
9 changes: 9 additions & 0 deletions src/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ struct stack_block
struct stack_block *next;
};

struct export_info
{
const char *name;
int global_id;
};

struct colm_sections
{
struct lang_el_info *lel_info;
Expand Down Expand Up @@ -70,6 +76,9 @@ struct colm_sections
struct generic_info *generic_info;
long num_generics;

struct export_info *export_info;
long num_exports;

long argv_generic_id;
long stds_generic_id;

Expand Down

0 comments on commit 9760b2b

Please sign in to comment.