+/* 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 (category, get_identifier (name), type));
+ TREE_PUBLIC (decl) = 1;
+ DECL_EXTERNAL (decl) = 1;
+ lang_hooks.decls.pushdecl (decl);
+ 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 (FIELD_DECL, get_identifier ("low"), field_type);
+ tree fieldhi = build_decl (FIELD_DECL, get_identifier ("high"), field_type);
+
+ tree struct_type = make_node (RECORD_TYPE);
+ DECL_CONTEXT (fieldlo) = struct_type;
+ DECL_CONTEXT (fieldhi) = struct_type;
+ TREE_CHAIN (fieldlo) = fieldhi;
+ TYPE_FIELDS (struct_type) = fieldlo;
+ TYPE_NAME (struct_type) = get_identifier ("__mf_cache");
+ layout_type (struct_type);
+
+ return struct_type;
+}
+
+#define build_function_type_0(rtype) \
+ build_function_type (rtype, void_list_node)
+#define build_function_type_1(rtype, arg1) \
+ build_function_type (rtype, tree_cons (0, arg1, void_list_node))
+#define build_function_type_3(rtype, arg1, arg2, arg3) \
+ build_function_type (rtype, tree_cons (0, arg1, tree_cons (0, arg2, \
+ tree_cons (0, arg3, void_list_node))))
+#define build_function_type_4(rtype, arg1, arg2, arg3, arg4) \
+ build_function_type (rtype, tree_cons (0, arg1, tree_cons (0, arg2, \
+ tree_cons (0, arg3, tree_cons (0, arg4, \
+ void_list_node)))))
+
+/* 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_4 (void_type_node, ptr_type_node, size_type_node,
+ integer_type_node, mf_const_string_type);
+ mf_unregister_fntype =
+ build_function_type_3 (void_type_node, ptr_type_node, size_type_node,
+ integer_type_node);
+ mf_init_fntype =
+ build_function_type_0 (void_type_node);
+ mf_set_options_fntype =
+ build_function_type_1 (integer_type_node, mf_const_string_type);
+
+ 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);
+}
+#undef build_function_type_4
+#undef build_function_type_3
+#undef build_function_type_1
+#undef build_function_type_0
+
+
+/* ------------------------------------------------------------------------ */
+/* Memory reference transforms. Perform the mudflap indirection-related
+ tree transforms on the current function.
+
+ 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. */
+
+static unsigned int
+execute_mudflap_function_ops (void)
+{
+ /* Don't instrument functions such as the synthetic constructor
+ built during mudflap_finish_file. */
+ if (mf_marked_p (current_function_decl) ||
+ DECL_ARTIFICIAL (current_function_decl))
+ return 0;
+
+ push_gimplify_context ();
+
+ /* In multithreaded mode, don't cache the lookup cache parameters. */
+ if (! flag_mudflap_threads)
+ mf_decl_cache_locals ();
+
+ mf_xform_derefs ();
+
+ if (! flag_mudflap_threads)
+ mf_decl_clear_locals ();
+
+ pop_gimplify_context (NULL);
+ return 0;
+}
+
+/* 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)
+{
+ tree t, shift_init_stmts, mask_init_stmts;
+ tree_stmt_iterator tsi;
+
+ /* Build the cache vars. */
+ mf_cache_shift_decl_l
+ = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_shift_decl),
+ "__mf_lookup_shift_l"));
+
+ mf_cache_mask_decl_l
+ = mf_mark (create_tmp_var (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. */
+ t = build2 (MODIFY_EXPR, TREE_TYPE (mf_cache_shift_decl_l),
+ mf_cache_shift_decl_l, mf_cache_shift_decl);
+ SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (current_function_decl));
+ gimplify_to_stmt_list (&t);
+ shift_init_stmts = t;
+
+ t = build2 (MODIFY_EXPR, TREE_TYPE (mf_cache_mask_decl_l),
+ mf_cache_mask_decl_l, mf_cache_mask_decl);
+ SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (current_function_decl));
+ gimplify_to_stmt_list (&t);
+ mask_init_stmts = t;
+
+ /* Anticipating multiple entry points, we insert the cache vars
+ initializers in each successor of the ENTRY_BLOCK_PTR. */
+ for (tsi = tsi_start (shift_init_stmts);
+ ! tsi_end_p (tsi);
+ tsi_next (&tsi))
+ insert_edge_copies (tsi_stmt (tsi), ENTRY_BLOCK_PTR);
+
+ for (tsi = tsi_start (mask_init_stmts);
+ ! tsi_end_p (tsi);
+ tsi_next (&tsi))
+ insert_edge_copies (tsi_stmt (tsi), ENTRY_BLOCK_PTR);
+ bsi_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;
+}
+