+/* global tree nodes */
+
+/* Global tree objects for global variables and functions exported by
+ mudflap runtime library. mf_init_extern_trees must be called
+ before using these. */
+
+/* uintptr_t (usually "unsigned long") */
+static GTY (()) tree mf_uintptr_type;
+
+/* struct __mf_cache { uintptr_t low; uintptr_t high; }; */
+static GTY (()) tree mf_cache_struct_type;
+
+/* struct __mf_cache * const */
+static GTY (()) tree mf_cache_structptr_type;
+
+/* extern struct __mf_cache __mf_lookup_cache []; */
+static GTY (()) tree mf_cache_array_decl;
+
+/* extern unsigned char __mf_lc_shift; */
+static GTY (()) tree mf_cache_shift_decl;
+
+/* extern uintptr_t __mf_lc_mask; */
+static GTY (()) tree mf_cache_mask_decl;
+
+/* Their function-scope local shadows, used in single-threaded mode only. */
+
+/* auto const unsigned char __mf_lc_shift_l; */
+static GTY (()) tree mf_cache_shift_decl_l;
+
+/* auto const uintptr_t __mf_lc_mask_l; */
+static GTY (()) tree mf_cache_mask_decl_l;
+
+/* extern void __mf_check (void *ptr, size_t sz, int type, const char *); */
+static GTY (()) tree mf_check_fndecl;
+
+/* extern void __mf_register (void *ptr, size_t sz, int type, const char *); */
+static GTY (()) tree mf_register_fndecl;
+
+/* extern void __mf_unregister (void *ptr, size_t sz, int type); */
+static GTY (()) tree mf_unregister_fndecl;
+
+/* extern void __mf_init (); */
+static GTY (()) tree mf_init_fndecl;
+
+/* extern int __mf_set_options (const char*); */
+static GTY (()) tree mf_set_options_fndecl;
+
+
+/* Helper for mudflap_init: construct a decl with the given category,
+ name, and type, mark it an external reference, and pushdecl it. */
+static inline tree
+mf_make_builtin (enum tree_code category, const char *name, tree type)
+{
+ tree decl = mf_mark (build_decl (UNKNOWN_LOCATION,
+ category, get_identifier (name), type));
+ TREE_PUBLIC (decl) = 1;
+ DECL_EXTERNAL (decl) = 1;
+ lang_hooks.decls.pushdecl (decl);
+ /* The decl was declared by the compiler. */
+ DECL_ARTIFICIAL (decl) = 1;
+ /* And we don't want debug info for it. */
+ DECL_IGNORED_P (decl) = 1;
+ return decl;
+}
+
+/* Helper for mudflap_init: construct a tree corresponding to the type
+ struct __mf_cache { uintptr_t low; uintptr_t high; };
+ where uintptr_t is the FIELD_TYPE argument. */
+static inline tree
+mf_make_mf_cache_struct_type (tree field_type)
+{
+ /* There is, abominably, no language-independent way to construct a
+ RECORD_TYPE. So we have to call the basic type construction
+ primitives by hand. */
+ tree fieldlo = build_decl (UNKNOWN_LOCATION,
+ FIELD_DECL, get_identifier ("low"), field_type);
+ tree fieldhi = build_decl (UNKNOWN_LOCATION,
+ FIELD_DECL, get_identifier ("high"), field_type);
+
+ tree struct_type = make_node (RECORD_TYPE);
+ DECL_CONTEXT (fieldlo) = struct_type;
+ DECL_CONTEXT (fieldhi) = struct_type;
+ DECL_CHAIN (fieldlo) = fieldhi;
+ TYPE_FIELDS (struct_type) = fieldlo;
+ TYPE_NAME (struct_type) = get_identifier ("__mf_cache");
+ layout_type (struct_type);
+
+ return struct_type;
+}
+
+/* Initialize the global tree nodes that correspond to mf-runtime.h
+ declarations. */
+void
+mudflap_init (void)
+{
+ static bool done = false;
+ tree mf_const_string_type;
+ tree mf_cache_array_type;
+ tree mf_check_register_fntype;
+ tree mf_unregister_fntype;
+ tree mf_init_fntype;
+ tree mf_set_options_fntype;
+
+ if (done)
+ return;
+ done = true;
+
+ mf_uintptr_type = lang_hooks.types.type_for_mode (ptr_mode,
+ /*unsignedp=*/true);
+ mf_const_string_type
+ = build_pointer_type (build_qualified_type
+ (char_type_node, TYPE_QUAL_CONST));
+
+ mf_cache_struct_type = mf_make_mf_cache_struct_type (mf_uintptr_type);
+ mf_cache_structptr_type = build_pointer_type (mf_cache_struct_type);
+ mf_cache_array_type = build_array_type (mf_cache_struct_type, 0);
+ mf_check_register_fntype =
+ build_function_type_list (void_type_node, ptr_type_node, size_type_node,
+ integer_type_node, mf_const_string_type, NULL_TREE);
+ mf_unregister_fntype =
+ build_function_type_list (void_type_node, ptr_type_node, size_type_node,
+ integer_type_node, NULL_TREE);
+ mf_init_fntype =
+ build_function_type_list (void_type_node, NULL_TREE);
+ mf_set_options_fntype =
+ build_function_type_list (integer_type_node, mf_const_string_type, NULL_TREE);
+
+ mf_cache_array_decl = mf_make_builtin (VAR_DECL, "__mf_lookup_cache",
+ mf_cache_array_type);
+ mf_cache_shift_decl = mf_make_builtin (VAR_DECL, "__mf_lc_shift",
+ unsigned_char_type_node);
+ mf_cache_mask_decl = mf_make_builtin (VAR_DECL, "__mf_lc_mask",
+ mf_uintptr_type);
+ /* Don't process these in mudflap_enqueue_decl, should they come by
+ there for some reason. */
+ mf_mark (mf_cache_array_decl);
+ mf_mark (mf_cache_shift_decl);
+ mf_mark (mf_cache_mask_decl);
+ mf_check_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_check",
+ mf_check_register_fntype);
+ mf_register_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_register",
+ mf_check_register_fntype);
+ mf_unregister_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_unregister",
+ mf_unregister_fntype);
+ mf_init_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_init",
+ mf_init_fntype);
+ mf_set_options_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_set_options",
+ mf_set_options_fntype);
+}
+
+
+/* ------------------------------------------------------------------------ */
+/* This is the second part of the mudflap instrumentation. It works on
+ low-level GIMPLE using the CFG, because we want to run this pass after
+ tree optimizations have been performed, but we have to preserve the CFG
+ for expansion from trees to RTL.
+ Below is the list of transformations performed on statements in the
+ current function.
+
+ 1) Memory reference transforms: Perform the mudflap indirection-related
+ tree transforms on memory references.
+
+ 2) Mark BUILTIN_ALLOCA calls not inlineable.
+
+ */
+
+static unsigned int
+execute_mudflap_function_ops (void)
+{
+ struct gimplify_ctx gctx;
+
+ /* Don't instrument functions such as the synthetic constructor
+ built during mudflap_finish_file. */
+ if (mf_marked_p (current_function_decl)
+ || mf_artificial (current_function_decl))
+ return 0;
+
+ push_gimplify_context (&gctx);
+
+ add_referenced_var (mf_cache_array_decl);
+ add_referenced_var (mf_cache_shift_decl);
+ add_referenced_var (mf_cache_mask_decl);
+
+ /* In multithreaded mode, don't cache the lookup cache parameters. */
+ if (! flag_mudflap_threads)
+ mf_decl_cache_locals ();
+
+ mf_xform_statements ();
+
+ if (! flag_mudflap_threads)
+ mf_decl_clear_locals ();
+
+ pop_gimplify_context (NULL);
+ return 0;
+}
+
+/* Insert a gimple_seq SEQ on all the outgoing edges out of BB. Note that
+ if BB has more than one edge, STMT will be replicated for each edge.
+ Also, abnormal edges will be ignored. */
+
+static void
+insert_edge_copies_seq (gimple_seq seq, basic_block bb)
+{
+ edge e;
+ edge_iterator ei;
+ unsigned n_copies = -1;
+
+ FOR_EACH_EDGE (e, ei, bb->succs)
+ if (!(e->flags & EDGE_ABNORMAL))
+ n_copies++;
+
+ FOR_EACH_EDGE (e, ei, bb->succs)
+ if (!(e->flags & EDGE_ABNORMAL))
+ gsi_insert_seq_on_edge (e, n_copies-- > 0 ? gimple_seq_copy (seq) : seq);
+}
+
+/* Create and initialize local shadow variables for the lookup cache
+ globals. Put their decls in the *_l globals for use by
+ mf_build_check_statement_for. */
+
+static void
+mf_decl_cache_locals (void)
+{
+ gimple g;
+ gimple_seq seq = gimple_seq_alloc ();
+
+ /* Build the cache vars. */
+ mf_cache_shift_decl_l
+ = mf_mark (make_rename_temp (TREE_TYPE (mf_cache_shift_decl),
+ "__mf_lookup_shift_l"));
+
+ mf_cache_mask_decl_l
+ = mf_mark (make_rename_temp (TREE_TYPE (mf_cache_mask_decl),
+ "__mf_lookup_mask_l"));
+
+ /* Build initialization nodes for the cache vars. We just load the
+ globals into the cache variables. */
+ g = gimple_build_assign (mf_cache_shift_decl_l, mf_cache_shift_decl);
+ gimple_set_location (g, DECL_SOURCE_LOCATION (current_function_decl));
+ gimple_seq_add_stmt (&seq, g);
+
+ g = gimple_build_assign (mf_cache_mask_decl_l, mf_cache_mask_decl);
+ gimple_set_location (g, DECL_SOURCE_LOCATION (current_function_decl));
+ gimple_seq_add_stmt (&seq, g);
+
+ insert_edge_copies_seq (seq, ENTRY_BLOCK_PTR);
+
+ gsi_commit_edge_inserts ();
+}
+
+
+static void
+mf_decl_clear_locals (void)
+{
+ /* Unset local shadows. */
+ mf_cache_shift_decl_l = NULL_TREE;
+ mf_cache_mask_decl_l = NULL_TREE;
+}
+