issue #169 apply latest updates from MIR project

nometajit
Dibyendu Majumdar 4 years ago
parent 84b4949aa9
commit 224ae4f3df

@ -1820,6 +1820,8 @@ static void new_std_macro (c2m_ctx_t c2m_ctx, const char *id_str) {
}
static void init_macros (c2m_ctx_t c2m_ctx) {
VARR (token_t) * params;
VARR_CREATE (macro_t, macros, 2048);
HTAB_CREATE (macro_t, macro_tab, 2048, macro_hash, macro_eq);
/* Standard macros : */
@ -1827,6 +1829,10 @@ static void init_macros (c2m_ctx_t c2m_ctx) {
new_std_macro (c2m_ctx, "__TIME__");
new_std_macro (c2m_ctx, "__FILE__");
new_std_macro (c2m_ctx, "__LINE__");
VARR_CREATE (token_t, params, 1);
VARR_PUSH (token_t, params, new_id_token (c2m_ctx, no_pos, "$"));
if (!options->pedantic_p)
new_macro (c2m_ctx, new_id_token (c2m_ctx, no_pos, "__has_include"), params, NULL);
}
static macro_t new_macro (c2m_ctx_t c2m_ctx, token_t id, VARR (token_t) * params,
@ -1938,12 +1944,12 @@ static void pre_finish (c2m_ctx_t c2m_ctx) {
free (c2m_ctx->pre_ctx);
}
static void add_include_stream (c2m_ctx_t c2m_ctx, const char *fname) {
static void add_include_stream (c2m_ctx_t c2m_ctx, const char *fname, pos_t err_pos) {
FILE *f;
assert (fname != NULL);
if ((f = fopen (fname, "r")) == NULL) {
if (options->message_file != NULL) fprintf (stderr, "error in opening file %s\n", fname);
if (options->message_file != NULL) error (c2m_ctx, err_pos, "error in opening file %s", fname);
longjmp (c2m_ctx->env, 1); // ???
}
add_stream (c2m_ctx, f, fname, NULL);
@ -2071,8 +2077,10 @@ static void define (c2m_ctx_t c2m_ctx) {
}
} else if (m->replacement == NULL) {
error (c2m_ctx, id->pos, "standard macro %s redefinition", name);
} else if (!params_eq_p (m->params, params) || !replacement_eq_p (m->replacement, repl)) {
error (c2m_ctx, id->pos, "different macro redefinition of %s", name);
} else {
if (!params_eq_p (m->params, params) || !replacement_eq_p (m->replacement, repl))
error (c2m_ctx, id->pos, "different macro redefinition of %s", name);
VARR_DESTROY (token_t, repl);
}
}
@ -2646,9 +2654,24 @@ static void processing (c2m_ctx_t c2m_ctx, int ignore_directive_p);
static struct val eval_expr (c2m_ctx_t c2m_ctx, VARR (token_t) * buffer, token_t if_token);
static const char *get_header_name (c2m_ctx_t c2m_ctx, VARR (token_t) * buffer, pos_t err_pos) {
int i;
transform_to_header (c2m_ctx, buffer);
i = 0;
if (VARR_LENGTH (token_t, buffer) != 0 && VARR_GET (token_t, buffer, 0)->code == ' ') i++;
if (i != VARR_LENGTH (token_t, buffer) - 1
|| (VARR_GET (token_t, buffer, i)->code != T_STR
&& VARR_GET (token_t, buffer, i)->code != T_HEADER)) {
error (c2m_ctx, err_pos, "wrong #include");
return NULL;
}
return get_include_fname (c2m_ctx, VARR_GET (token_t, buffer, i));
}
static void process_directive (c2m_ctx_t c2m_ctx) {
token_t t, t1;
int i, true_p;
int true_p;
VARR (token_t) * temp_buffer;
pos_t pos;
struct macro macro;
@ -2760,24 +2783,16 @@ static void process_directive (c2m_ctx_t c2m_ctx) {
processing (c2m_ctx, TRUE);
no_out_p = FALSE;
move_tokens (temp_buffer, output_buffer);
transform_to_header (c2m_ctx, temp_buffer);
i = 0;
if (VARR_LENGTH (token_t, temp_buffer) != 0
&& VARR_GET (token_t, temp_buffer, 0)->code == ' ')
i++;
if (i != VARR_LENGTH (token_t, temp_buffer) - 1
|| (VARR_GET (token_t, temp_buffer, i)->code != T_STR
&& VARR_GET (token_t, temp_buffer, i)->code != T_HEADER)) {
if ((name = get_header_name (c2m_ctx, temp_buffer, t->pos)) == NULL) {
error (c2m_ctx, t->pos, "wrong #include");
goto ret;
}
name = get_include_fname (c2m_ctx, VARR_GET (token_t, temp_buffer, i));
}
if (VARR_LENGTH (stream_t, streams) >= max_nested_includes + 1) {
error (c2m_ctx, t->pos, "more %d include levels", VARR_LENGTH (stream_t, streams) - 1);
goto ret;
}
add_include_stream (c2m_ctx, name);
add_include_stream (c2m_ctx, name, t->pos);
} else if (strcmp (t->repr, "line") == 0) {
skip_nl (c2m_ctx, NULL, temp_buffer);
unget_next_pptoken (c2m_ctx, new_token (c2m_ctx, t->pos, "", T_EOP, N_IGNORE));
@ -3043,7 +3058,12 @@ static struct val eval_expr (c2m_ctx_t c2m_ctx, VARR (token_t) * expr_buffer, to
VARR_PUSH (token_t, temp_buffer, t);
}
no_out_p = TRUE;
tree = parse_pre_expr (c2m_ctx, temp_buffer);
if (VARR_LENGTH (token_t, temp_buffer) != 0) {
tree = parse_pre_expr (c2m_ctx, temp_buffer);
} else {
error (c2m_ctx, if_token->pos, "empty preprocessor expression");
tree = NULL;
}
no_out_p = FALSE;
VARR_DESTROY (token_t, temp_buffer);
if (tree == NULL) {
@ -3194,6 +3214,30 @@ static struct val eval (c2m_ctx_t c2m_ctx, node_t tree) {
return res;
}
static macro_call_t try_param_macro_call (c2m_ctx_t c2m_ctx, macro_t m, token_t macro_id) {
macro_call_t mc;
token_t t1 = get_next_pptoken (c2m_ctx), t2 = NULL;
if (t1->code == T_EOR) {
pop_macro_call (c2m_ctx);
t1 = get_next_pptoken (c2m_ctx);
}
if (t1->code == ' ' || t1->code == '\n') {
t2 = t1;
t1 = get_next_pptoken (c2m_ctx);
}
if (t1->code != '(') { /* no args: it is not a macro call */
unget_next_pptoken (c2m_ctx, t1);
if (t2 != NULL) unget_next_pptoken (c2m_ctx, t2);
out_token (c2m_ctx, macro_id);
return NULL;
}
mc = new_macro_call (m, macro_id->pos);
find_args (c2m_ctx, mc);
VARR_PUSH (macro_call_t, macro_call_stack, mc);
return mc;
}
static void processing (c2m_ctx_t c2m_ctx, int ignore_directive_p) {
token_t t, t1, t2;
struct macro macro_struct;
@ -3282,6 +3326,31 @@ static void processing (c2m_ctx_t c2m_ctx, int ignore_directive_p) {
t = new_node_token (c2m_ctx, t->pos, time_str_repr, T_STR,
new_str_node (c2m_ctx, N_STR, uniq_cstr (c2m_ctx, time_str), t->pos));
out_token (c2m_ctx, t);
} else if (strcmp (t->repr, "__has_include") == 0) {
int res;
VARR (token_t) * arg;
const char *name;
FILE *f;
if ((mc = try_param_macro_call (c2m_ctx, m, t)) != NULL) {
unget_next_pptoken (c2m_ctx, new_token (c2m_ctx, t->pos, "", T_EOR, N_IGNORE));
if (VARR_LENGTH (token_arr_t, mc->args) != 1) {
res = 0;
} else {
arg = VARR_LAST (token_arr_t, mc->args);
if ((name = get_header_name (c2m_ctx, arg, t->pos)) != NULL) {
res = ((f = fopen (name, "r")) != NULL && !fclose (f)) ? 1 : 0;
} else {
error (c2m_ctx, t->pos, "wrong arg of predefined __has_include");
res = 0;
}
}
m->ignore_p = TRUE;
unget_next_pptoken (c2m_ctx,
new_node_token (c2m_ctx, t->pos,
uniq_cstr (c2m_ctx, res ? "1" : "0").s, T_NUMBER,
new_i_node (c2m_ctx, res, t->pos)));
}
} else {
assert (FALSE);
}
@ -3302,26 +3371,7 @@ static void processing (c2m_ctx_t c2m_ctx, int ignore_directive_p) {
copy_and_push_back (c2m_ctx, do_concat (c2m_ctx, mc->repl_buffer), mc->pos);
m->ignore_p = TRUE;
VARR_PUSH (macro_call_t, macro_call_stack, mc);
} else { /* macro with parameters */
t2 = NULL;
t1 = get_next_pptoken (c2m_ctx);
if (t1->code == T_EOR) {
pop_macro_call (c2m_ctx);
t1 = get_next_pptoken (c2m_ctx);
}
if (t1->code == ' ' || t1->code == '\n') {
t2 = t1;
t1 = get_next_pptoken (c2m_ctx);
}
if (t1->code != '(') { /* no args: it is not a macro call */
unget_next_pptoken (c2m_ctx, t1);
if (t2 != NULL) unget_next_pptoken (c2m_ctx, t2);
out_token (c2m_ctx, t);
continue;
}
mc = new_macro_call (m, t->pos);
find_args (c2m_ctx, mc);
VARR_PUSH (macro_call_t, macro_call_stack, mc);
} else if ((mc = try_param_macro_call (c2m_ctx, m, t)) != NULL) { /* macro with parameters */
process_replacement (c2m_ctx, mc);
}
}
@ -9293,6 +9343,8 @@ static void emit_insn_opt (MIR_context_t ctx, MIR_insn_t insn) {
MIR_insn_op_mode (ctx, tail, 0, &out_p);
if (out_p) {
tail->ops[0] = insn->ops[0];
MIR_append_insn (ctx, curr_func, insn);
MIR_remove_insn (ctx, curr_func, insn);
return;
}
}
@ -12015,6 +12067,7 @@ static void print_node (MIR_context_t ctx, FILE *f, node_t n, int indent, int at
static void init_include_dirs (MIR_context_t ctx) {
c2m_ctx_t c2m_ctx = *c2m_ctx_loc (ctx);
const char *str;
int added_p = FALSE;
VARR_CREATE (char_ptr_t, headers, 0);
VARR_CREATE (char_ptr_t, system_headers, 0);
@ -12038,14 +12091,22 @@ static void init_include_dirs (MIR_context_t ctx) {
}
#if defined(__APPLE__) || defined(__unix__)
VARR_PUSH (char_ptr_t, system_headers, "/usr/local/include");
#endif
#ifdef ADDITIONAL_INCLUDE_PATH
if (ADDITIONAL_INCLUDE_PATH[0] != 0) {
added_p = TRUE;
VARR_PUSH (char_ptr_t, system_headers, ADDITIONAL_INCLUDE_PATH);
}
#endif
#if defined(__APPLE__)
VARR_PUSH (char_ptr_t, system_headers,
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/"
"MacOSX.sdk/usr/include");
if (!added_p)
VARR_PUSH (char_ptr_t, system_headers,
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include");
#endif
#if defined(__linux__) && defined(__x86_64__)
VARR_PUSH (char_ptr_t, system_headers, "/usr/include/x86_64-linux-gnu");
#endif
#if defined(__APPLE__) || defined(__unix__)
VARR_PUSH (char_ptr_t, system_headers, "/usr/include");
#endif
VARR_PUSH (char_ptr_t, system_headers, NULL);

@ -579,6 +579,10 @@ static void delete_bb (MIR_context_t ctx, bb_t bb) {
delete_edge (e);
}
DLIST_REMOVE (bb_t, curr_cfg->bbs, bb);
bitmap_destroy (bb->in);
bitmap_destroy (bb->out);
bitmap_destroy (bb->gen);
bitmap_destroy (bb->kill);
free (bb);
}
@ -696,6 +700,18 @@ static void build_loop_tree (MIR_context_t ctx) {
}
}
static void destroy_loop_tree (MIR_context_t ctx, loop_node_t root) {
struct gen_ctx *gen_ctx = *gen_ctx_loc (ctx);
loop_node_t node, next;
if (root->bb == NULL)
for (node = DLIST_HEAD (loop_node_t, root->children); node != NULL; node = next) {
next = DLIST_NEXT (loop_node_t, node);
destroy_loop_tree (ctx, node);
}
free (root);
}
static void update_min_max_reg (MIR_context_t ctx, MIR_reg_t reg) {
struct gen_ctx *gen_ctx = *gen_ctx_loc (ctx);
@ -976,10 +992,6 @@ static void destroy_func_cfg (MIR_context_t ctx) {
}
for (bb = DLIST_HEAD (bb_t, curr_cfg->bbs); bb != NULL; bb = next_bb) {
next_bb = DLIST_NEXT (bb_t, bb);
bitmap_destroy (bb->in);
bitmap_destroy (bb->out);
bitmap_destroy (bb->gen);
bitmap_destroy (bb->kill);
delete_bb (ctx, bb);
}
for (mv = DLIST_HEAD (mv_t, curr_cfg->used_moves); mv != NULL; mv = next_mv) {
@ -3933,7 +3945,7 @@ static int combine_substitute (MIR_context_t ctx, bb_insn_t bb_insn) {
if (!hreg_refs_addr[hr].def_p) continue;
gen_assert (!hreg_refs_addr[hr].del_p);
def_insn = hreg_refs_addr[hr].insn;
if (obsolete_op_p (ctx, def_insn->ops[1], hreg_refs_addr[hr].insn_num))
if (def_insn->nops > 1 && obsolete_op_p (ctx, def_insn->ops[1], hreg_refs_addr[hr].insn_num))
continue; /* hr0 = ... hr1 ...; ...; hr1 = ...; ...; insn */
insn_hr_change_p = FALSE;
for (i = 0; i < nops; i++) { /* Change all hr occurences: */
@ -4593,6 +4605,7 @@ void *MIR_gen (MIR_context_t ctx, MIR_item_t func_item) {
#endif
_MIR_redirect_thunk (ctx, func_item->addr, func_item->u.func->call_addr);
destroy_func_live_ranges (ctx);
destroy_loop_tree (ctx, curr_cfg->root_loop_node);
destroy_func_cfg (ctx);
#if MIR_GEN_DEBUG
if (debug_file != NULL)

@ -841,7 +841,7 @@ static code_t call_insn_execute (MIR_context_t ctx, code_t pc, MIR_val_t *bp, co
static void OPTIMIZE eval (MIR_context_t ctx, func_desc_t func_desc, MIR_val_t *bp,
MIR_val_t *results) {
struct interp_ctx *interp_ctx = ctx->interp_ctx;
code_t pc, ops, code = func_desc->code;
code_t pc, ops, code;
#if MIR_INTERP_TRACE
MIR_full_insn_code_t trace_insn_code;
@ -927,6 +927,7 @@ static void OPTIMIZE eval (MIR_context_t ctx, func_desc_t func_desc, MIR_val_t *
END_INSN; \
}
code = func_desc->code;
pc = code;
#if DIRECT_THREADED_DISPATCH

@ -141,7 +141,7 @@ struct insn_desc {
unsigned op_modes[4];
};
#define OUTPUT_FLAG (1 << 31)
#define OUTPUT_FLAG (1 << 30)
static const struct insn_desc insn_descs[] = {
{MIR_MOV, "mov", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
@ -631,8 +631,87 @@ MIR_context_t MIR_init (void) {
return ctx;
}
void MIR_remove_insn (MIR_context_t ctx, MIR_item_t func_item, MIR_insn_t insn) {
mir_assert (func_item != NULL);
if (func_item->item_type != MIR_func_item)
(*error_func) (MIR_wrong_param_value_error, "MIR_remove_insn: wrong func item");
DLIST_REMOVE (MIR_insn_t, func_item->u.func->insns, insn);
free (insn);
}
static void remove_func_insns (MIR_context_t ctx, MIR_item_t func_item,
DLIST (MIR_insn_t) * insns) {
MIR_insn_t insn;
mir_assert (func_item->item_type == MIR_func_item);
while ((insn = DLIST_HEAD (MIR_insn_t, *insns)) != NULL) {
MIR_remove_insn (ctx, func_item, insn);
}
}
static void remove_item (MIR_context_t ctx, MIR_item_t item) {
MIR_module_t module = item->module;
switch (item->item_type) {
case MIR_func_item:
remove_func_insns (ctx, item, &item->u.func->insns);
remove_func_insns (ctx, item, &item->u.func->original_insns);
VARR_DESTROY (MIR_var_t, item->u.func->vars);
free (item->u.func);
break;
case MIR_proto_item:
VARR_DESTROY (MIR_var_t, item->u.proto->args);
free (item->u.proto);
break;
case MIR_import_item:
case MIR_export_item:
case MIR_forward_item: break;
case MIR_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.data);
break;
case MIR_ref_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.ref_data);
break;
case MIR_expr_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.expr_data);
break;
case MIR_bss_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.bss);
break;
default: mir_assert (FALSE);
}
if (item->data != NULL) free (item->data);
free (item);
}
static void remove_module (MIR_context_t ctx, MIR_module_t module, int free_module_p) {
MIR_item_t item;
while ((item = DLIST_HEAD (MIR_item_t, module->items)) != NULL) {
DLIST_REMOVE (MIR_item_t, module->items, item);
remove_item (ctx, item);
}
if (module->data != NULL) free (module->data);
if (free_module_p) free (module);
}
static void remove_all_modules (MIR_context_t ctx) {
MIR_module_t module;
while ((module = DLIST_HEAD (MIR_module_t, all_modules)) != NULL) {
DLIST_REMOVE (MIR_module_t, all_modules, module);
remove_module (ctx, module, TRUE);
}
remove_module (ctx, &environment_module, FALSE);
}
void MIR_finish (MIR_context_t ctx) {
interp_finish (ctx);
remove_all_modules (ctx);
HTAB_DESTROY (MIR_item_t, module_item_tab);
VARR_DESTROY (MIR_module_t, modules_to_link);
#if !MIR_NO_SCAN
@ -811,6 +890,7 @@ static MIR_item_t create_item (MIR_context_t ctx, MIR_item_type_t item_type,
item->item_type = item_type;
item->ref_def = NULL;
item->export_p = FALSE;
item->section_head_p = FALSE;
item->addr = NULL;
return item;
}
@ -1372,6 +1452,7 @@ static MIR_item_t load_bss_data_section (MIR_context_t ctx, MIR_item_t item, int
(*error_func) (MIR_alloc_error, "Not enough memory to allocate data/bss %s",
name == NULL ? "" : name);
}
item->section_head_p = TRUE;
}
/* Set up section memory: */
for (last_item = item, curr_item = item, addr = item->addr;
@ -1983,14 +2064,6 @@ void MIR_insert_insn_before (MIR_context_t ctx, MIR_item_t func_item, MIR_insn_t
DLIST_INSERT_BEFORE (MIR_insn_t, func_item->u.func->insns, before, insn);
}
void MIR_remove_insn (MIR_context_t ctx, MIR_item_t func_item, MIR_insn_t insn) {
mir_assert (func_item != NULL);
if (func_item->item_type != MIR_func_item)
(*error_func) (MIR_wrong_param_value_error, "MIR_remove_insn: wrong func item");
DLIST_REMOVE (MIR_insn_t, func_item->u.func->insns, insn);
free (insn);
}
static void store_labels_for_duplication (MIR_context_t ctx, MIR_insn_t insn, MIR_insn_t new_insn) {
if (MIR_branch_code_p (insn->code) || insn->code == MIR_SWITCH) {
VARR_PUSH (MIR_insn_t, temp_insns2, new_insn);

@ -337,6 +337,8 @@ struct MIR_item {
item, imported definition or proto object */
void *addr;
char export_p; /* true for export items (only func items) */
/* defined for data-bss after loading. True if it is a start of allocated section */
char section_head_p;
union {
MIR_func_t func;
MIR_proto_t proto;

Loading…
Cancel
Save