Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MIR_get_global_item #383

Open
wants to merge 2 commits into
base: v0_master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions mir.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,22 +505,26 @@ static void push_data (MIR_context_t ctx, uint8_t *els, size_t size) {
for (size_t i = 0; i < size; i++) VARR_PUSH (uint8_t, temp_data, els[i]);
}

const char *MIR_item_name (MIR_context_t ctx, MIR_item_t item) {
static const char** item_name_ptr (MIR_item_t item) {
mir_assert (item != NULL);
switch (item->item_type) {
case MIR_func_item: return item->u.func->name;
case MIR_proto_item: return item->u.proto->name;
case MIR_import_item: return item->u.import_id;
case MIR_export_item: return item->u.export_id;
case MIR_forward_item: return item->u.forward_id;
case MIR_bss_item: return item->u.bss->name;
case MIR_data_item: return item->u.data->name;
case MIR_ref_data_item: return item->u.ref_data->name;
case MIR_expr_data_item: return item->u.expr_data->name;
default: mir_assert (FALSE); return NULL;
case MIR_func_item: return &item->u.func->name;
case MIR_proto_item: return &item->u.proto->name;
case MIR_import_item: return &item->u.import_id;
case MIR_export_item: return &item->u.export_id;
case MIR_forward_item: return &item->u.forward_id;
case MIR_bss_item: return &item->u.bss->name;
case MIR_data_item: return &item->u.data->name;
case MIR_ref_data_item: return &item->u.ref_data->name;
case MIR_expr_data_item: return &item->u.expr_data->name;
default: mir_assert (FALSE);
}
}

const char *MIR_item_name (MIR_context_t ctx, MIR_item_t item) {
return *item_name_ptr(item);
}

MIR_func_t MIR_get_item_func (MIR_context_t ctx, MIR_item_t item) {
mir_assert (item != NULL);
if (item->item_type == MIR_func_item) {
Expand Down Expand Up @@ -1579,6 +1583,26 @@ static MIR_item_t load_bss_data_section (MIR_context_t ctx, MIR_item_t item, int
return last_item;
}

extern MIR_item_t MIR_get_module_item (MIR_context_t ctx, MIR_module_t module, const char *name) {
mir_assert (module != NULL);
for (MIR_item_t item = DLIST_HEAD (MIR_item_t, module->items); item != NULL;
item = DLIST_NEXT (MIR_item_t, item)) {
if (strcmp (MIR_item_name (ctx, item), name) == 0) {
return item;
}
}
return NULL;
}

MIR_item_t MIR_get_global_item (MIR_context_t ctx, const char *name) {
for (MIR_module_t module = DLIST_HEAD (MIR_module_t, *MIR_get_module_list (ctx)); module != NULL;
module = DLIST_NEXT (MIR_module_t, module)) {
MIR_item_t ret = MIR_get_module_item(ctx, module, name);
if (ret != NULL) return ret;
}
return NULL;
}

void MIR_load_module (MIR_context_t ctx, MIR_module_t m) {
mir_assert (m != NULL);
for (MIR_item_t item = DLIST_HEAD (MIR_item_t, m->items); item != NULL;
Expand Down Expand Up @@ -2392,19 +2416,7 @@ void _MIR_restore_func_insns (MIR_context_t ctx, MIR_item_t func_item) {
}

static void set_item_name (MIR_item_t item, const char *name) {
mir_assert (item != NULL);
switch (item->item_type) {
case MIR_func_item: item->u.func->name = name; break;
case MIR_proto_item: item->u.proto->name = name; break;
case MIR_import_item: item->u.import_id = name; break;
case MIR_export_item: item->u.export_id = name; break;
case MIR_forward_item: item->u.forward_id = name; break;
case MIR_bss_item: item->u.bss->name = name; break;
case MIR_data_item: item->u.data->name = name; break;
case MIR_ref_data_item: item->u.ref_data->name = name; break;
case MIR_expr_data_item: item->u.expr_data->name = name; break;
default: mir_assert (FALSE);
}
*item_name_ptr(item) = name;
}

static void change_var_names (MIR_context_t new_ctx, VARR (MIR_var_t) * vars) {
Expand Down
1 change: 1 addition & 0 deletions mir.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ extern void MIR_scan_string (MIR_context_t ctx, const char *str);
#endif

extern MIR_item_t MIR_get_global_item (MIR_context_t ctx, const char *name);
extern MIR_item_t MIR_get_module_item (MIR_context_t ctx, MIR_module_t module, const char *name);
extern void MIR_load_module (MIR_context_t ctx, MIR_module_t m);
extern void MIR_load_external (MIR_context_t ctx, const char *name, void *addr);
extern void MIR_link (MIR_context_t ctx, void (*set_interface) (MIR_context_t ctx, MIR_item_t item),
Expand Down