+/**
+ Allocate a new ctx for the given graph and the given enclosing ctx.
+*/
+static ctx_info_t *new_ctx (ir_graph *graph, ir_node *call, ctx_info_t *enc)
+{
+ ctx_info_t *res = xmalloc (sizeof (ctx_info_t));
+
+ res->graph = graph;
+ res->call = call;
+ res->enc = enc;
+ res->id = ctx_id ++;
+
+ return (res);
+}
+
+
+/**
+ Fill in the ctxs parts of the graph_infos
+*/
+static void ecg_fill_ctxs_count (ir_graph *graph)
+{
+ graph_info_t *ginfo = ecg_get_info (graph);
+
+ /* count how many ctxs we have per graph */
+ if (0 == ginfo->ecg_seen) {
+ call_info_t *cinfo = ginfo->calls;
+
+ ginfo->ecg_seen = 1;
+
+ while (NULL != cinfo) {
+ callEd_info_t *ced = cinfo->callEds;
+
+ while (NULL != ced) {
+ ir_graph *callEd_graph = ced->callEd;
+
+ /* first step: we have a new ctx */
+ graph_info_t *callEd_info = ecg_get_info (callEd_graph);
+ callEd_info->n_ctxs ++;
+
+ /* CallR graph -> CallEd_graph */
+ ecg_fill_ctxs_count (callEd_graph);
+
+ ced = ced->prev;
+ } /* end forall callEds (call) */
+
+ cinfo = cinfo->prev;
+ } /* end forall (calls(graph)) */
+
+ ginfo->ecg_seen = 0;
+ }
+}
+
+static void ecg_fill_ctxs_alloc (void)
+{
+ /* allocate the memory needed for the ctxts: */
+ graph_info_t *ginfo = graph_infos_list;
+
+ while (NULL != ginfo) {
+ ginfo->ctxs = xcalloc (ginfo->n_ctxs, sizeof (ctx_info_t *));
+
+ /*
+ fprintf (stdout, "graph of \"%s\": n_ctxs = %i\n",
+ get_entity_name (get_irg_entity (ginfo->graph)), ginfo->n_ctxs);
+ */
+ ginfo->n_ctxs = 0;
+
+ assert (ginfo != ginfo->prev);
+ ginfo = ginfo->prev;
+ }
+}
+
+/**
+ Fill in the ctxs parts of the graph_infos
+*/
+static void ecg_fill_ctxs_write (ir_graph *graph, ctx_info_t *enc_ctx)
+{
+ graph_info_t *ginfo = ecg_get_info (graph);
+
+ /* enter a new ctx for all callEds along the call edges of this graph */
+ if (0 == ginfo->ecg_seen) {
+ call_info_t *cinfo = ginfo->calls;
+ ginfo->ecg_seen = 1;
+
+ while (NULL != cinfo) {
+ callEd_info_t *ced = cinfo->callEds;
+
+ while (NULL != ced) {
+ ctx_info_t *ctx = new_ctx (graph, cinfo->call, enc_ctx);
+
+ ir_graph *callEd_graph = ced->callEd;
+
+ /* write the ctx of this call into the callEd graph */
+ graph_info_t *callEd_info = ecg_get_info (callEd_graph);
+
+ callEd_info->ctxs [callEd_info->n_ctxs] = ctx;
+ callEd_info->n_ctxs ++;
+
+ /* CallR graph -> callEd_graph */
+ ecg_fill_ctxs_write (callEd_graph, ctx);
+
+ ced = ced->prev;
+ } /* end forall callEds (call) */
+
+ cinfo = cinfo->prev;
+ } /* end forall (calls(graph)) */
+
+ ginfo->ecg_seen = 0;
+ }
+}
+
+/**
+ Fill in the ctxs parts of the graph_infos
+*/
+static void ecg_fill_ctxs (void)
+{
+ ctx_info_t *main_ctx;
+ ir_graph *main_irg;
+ graph_info_t *ginfo;
+
+ ecg_fill_ctxs_count (get_irp_main_irg ());
+ ecg_fill_ctxs_alloc ();
+
+ main_ctx = new_ctx (get_irp_main_irg (), NULL, NULL);
+ main_irg = get_irp_main_irg ();
+
+ set_main_ctx (main_ctx);
+
+ /* Grrr, have to add this ctx manually to main.ginfo ... */
+ ginfo = ecg_get_info (main_irg);
+ ginfo->n_ctxs = 1;
+ ginfo->ctxs = xcalloc (1, sizeof (ctx_info_t *));
+ ginfo->ctxs [0] = main_ctx;
+
+ ecg_fill_ctxs_write (main_irg, main_ctx);
+}
+
+/* ====================
+ CTX stuff
+ ==================== */
+/*
+ Nicely print a ctx_info_t to the given output stream
+*/
+void ecg_print_ctx (ctx_info_t *ctx, FILE *stream)
+{
+ entity *ent = get_irg_entity(ctx->graph);
+ ir_node *call = ctx->call;
+ const char *ent_name = get_entity_name (ent);
+ const char *own_name = get_type_name (get_entity_owner (ent));
+
+ fprintf (stream, "CTX[%i](%s.%s->%s[%li])",
+ ctx->id, own_name, ent_name,
+ get_op_name (get_irn_op (call)),
+ get_irn_node_nr (call));
+
+ if (NULL != ctx->enc) {
+ fprintf (stream, "->%i", ctx->enc->id);
+ }
+
+ fprintf (stream, "\n");
+}
+
+/*
+ Get a ctx of the given graph info
+*/
+ctx_info_t *get_ctx (graph_info_t *ginfo, int ctx_idx)
+{
+ assert (ginfo->n_ctxs > ctx_idx);
+
+ return (ginfo->ctxs [ctx_idx]);
+}
+
+/*
+ Get the pseudo-ctx of 'main'
+*/
+ctx_info_t *get_main_ctx ()
+{
+ return (main_ctx);
+}
+
+/*
+ Set the pseudo-ctx of 'main'
+*/
+void set_main_ctx (ctx_info_t *ctx)
+{
+ main_ctx = ctx;
+}
+
+
+/* ====================
+ ECG stuff
+ ==================== */
+