Skip to content

Commit

Permalink
an example to examine module
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Apr 22, 2023
1 parent 85a0b2f commit 358c377
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/app/app.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

#include <toywasm/fileio.h>
Expand Down Expand Up @@ -43,5 +44,51 @@ main(int argc, char **argv)
xlog_printf("module %s contains %" PRIu32 " functions\n", filename,
m->nfuncs);
module_print_stats(m);

/* perform some investigations on the loaded module */
uint32_t i;
uint32_t funcidx_with_most_locals = UINT32_MAX;
uint32_t nlocals = 0;
uint32_t funcidx_with_most_params = UINT32_MAX;
uint32_t nparams = 0;
uint32_t funcidx_with_most_results = UINT32_MAX;
uint32_t nresults = 0;
for (i = 0; i < m->nfuncs - m->nimportedfuncs; i++) {
const struct func *func = &m->funcs[i];
const struct localtype *lt = &func->localtype;
if (funcidx_with_most_locals == UINT32_MAX ||
lt->nlocals > nlocals) {
funcidx_with_most_locals = i;
nlocals = lt->nlocals;
}
const struct functype *ft = &m->types[m->functypeidxes[i]];
if (funcidx_with_most_params == UINT32_MAX ||
ft->parameter.ntypes > nparams) {
funcidx_with_most_params = i;
nparams = ft->parameter.ntypes;
}
if (funcidx_with_most_results == UINT32_MAX ||
ft->result.ntypes > nresults) {
funcidx_with_most_results = i;
nresults = ft->result.ntypes;
}
}
if (funcidx_with_most_locals != UINT32_MAX) {
printf("func %" PRIu32 " has the most locals (%" PRIu32
") in this module.\n",
m->nimportedfuncs + funcidx_with_most_locals, nlocals);
}
if (funcidx_with_most_params != UINT32_MAX) {
printf("func %" PRIu32 " has the most parameters (%" PRIu32
") in this module.\n",
m->nimportedfuncs + funcidx_with_most_params, nparams);
}
if (funcidx_with_most_results != UINT32_MAX) {
printf("func %" PRIu32 " has the most results (%" PRIu32
") in this module.\n",
m->nimportedfuncs + funcidx_with_most_results,
nresults);
}

exit(0);
}

0 comments on commit 358c377

Please sign in to comment.