This patch by Chris Manghane stores the flags for division checks in the
Gogo object, rather than referring directly to the GCC global variables.
Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline.

Ian


2014-05-06  Chris Manghane  <cm...@google.com>

        * go-c.h (go_create_gogo): Update declaration to add
        check_divide_zero and check_divide_overflow parameters.
        * go-lang.c (go_langhook_init): Pass new arguments to
        go_create_gogo.


Index: gcc/go/go-lang.c
===================================================================
--- gcc/go/go-lang.c	(revision 209945)
+++ gcc/go/go-lang.c	(working copy)
@@ -104,7 +104,8 @@ go_langhook_init (void)
      build_common_builtin_nodes (because it calls, indirectly,
      go_type_for_size).  */
   go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix,
-		  go_relative_import_path);
+		  go_relative_import_path, go_check_divide_zero,
+		  go_check_divide_overflow);
 
   build_common_builtin_nodes ();
 
Index: gcc/go/gofrontend/go.cc
===================================================================
--- gcc/go/gofrontend/go.cc	(revision 209941)
+++ gcc/go/gofrontend/go.cc	(working copy)
@@ -21,7 +21,8 @@ static Gogo* gogo;
 GO_EXTERN_C
 void
 go_create_gogo(int int_type_size, int pointer_size, const char *pkgpath,
-	       const char *prefix, const char *relative_import_path)
+	       const char *prefix, const char *relative_import_path,
+	       bool check_divide_by_zero, bool check_divide_overflow)
 {
   go_assert(::gogo == NULL);
   Linemap* linemap = go_get_linemap();
@@ -34,6 +35,10 @@ go_create_gogo(int int_type_size, int po
 
   if (relative_import_path != NULL)
     ::gogo->set_relative_import_path(relative_import_path);
+  if (check_divide_by_zero)
+    ::gogo->set_check_divide_by_zero(check_divide_by_zero);
+  if (check_divide_overflow)
+    ::gogo->set_check_divide_overflow(check_divide_overflow);
 }
 
 // Parse the input files.
Index: gcc/go/gofrontend/expressions.cc
===================================================================
--- gcc/go/gofrontend/expressions.cc	(revision 210088)
+++ gcc/go/gofrontend/expressions.cc	(working copy)
@@ -5432,7 +5432,7 @@ Binary_expression::lower_compare_to_memc
 }
 
 Expression*
-Binary_expression::do_flatten(Gogo*, Named_object*,
+Binary_expression::do_flatten(Gogo* gogo, Named_object*,
                               Statement_inserter* inserter)
 {
   Location loc = this->location();
@@ -5462,11 +5462,9 @@ Binary_expression::do_flatten(Gogo*, Nam
                       left_type->integer_type() != NULL)
                      || this->op_ == OPERATOR_MOD);
 
-  // FIXME: go_check_divide_zero and go_check_divide_overflow are globals
-  // defined in gcc/go/lang.opt.  These should be defined in go_create_gogo
-  // and accessed from the Gogo* passed to do_flatten.
   if (is_shift_op
-      || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow)))
+      || (is_idiv_op
+	  && (gogo->check_divide_by_zero() || gogo->check_divide_overflow())))
     {
       if (!this->left_->is_variable())
         {
@@ -6046,7 +6044,7 @@ Binary_expression::do_get_tree(Translate
   // Add checks for division by zero and division overflow as needed.
   if (is_idiv_op)
     {
-      if (go_check_divide_zero)
+      if (gogo->check_divide_by_zero())
 	{
 	  // right == 0
           Bexpression* zero_expr =
@@ -6065,7 +6063,7 @@ Binary_expression::do_get_tree(Translate
                                                         crash_expr, ret, loc);
 	}
 
-      if (go_check_divide_overflow)
+      if (gogo->check_divide_overflow())
 	{
 	  // right == -1
 	  // FIXME: It would be nice to say that this test is expected
Index: gcc/go/gofrontend/gogo.h
===================================================================
--- gcc/go/gofrontend/gogo.h	(revision 209941)
+++ gcc/go/gofrontend/gogo.h	(working copy)
@@ -215,7 +215,27 @@ class Gogo
   // Set the relative import path from a command line option.
   void
   set_relative_import_path(const std::string& s)
-  {this->relative_import_path_ = s; }
+  { this->relative_import_path_ = s; }
+
+  // Return whether to check for division by zero in binary operations.
+  bool
+  check_divide_by_zero() const
+  { return this->check_divide_by_zero_; }
+
+  // Set the option to check division by zero from a command line option.
+  void
+  set_check_divide_by_zero(bool b)
+  { this->check_divide_by_zero_ = b; }
+
+  // Return whether to check for division overflow in binary operations.
+  bool
+  check_divide_overflow() const
+  { return this->check_divide_overflow_; }
+
+  // Set the option to check division overflow from a command line option.
+  void
+  set_check_divide_overflow(bool b)
+  { this->check_divide_overflow_ = b; }
 
   // Return the priority to use for the package we are compiling.
   // This is two more than the largest priority of any package we
@@ -716,6 +736,12 @@ class Gogo
   // The relative import path, from the -fgo-relative-import-path
   // option.
   std::string relative_import_path_;
+  // Whether or not to check for division by zero, from the
+  // -fgo-check-divide-zero option.
+  bool check_divide_by_zero_;
+  // Whether or not to check for division overflow, from the
+  // -fgo-check-divide-overflow option.
+  bool check_divide_overflow_;
   // A list of types to verify.
   std::vector<Type*> verify_types_;
   // A list of interface types defined while parsing.
Index: gcc/go/go-c.h
===================================================================
--- gcc/go/go-c.h	(revision 209968)
+++ gcc/go/go-c.h	(working copy)
@@ -34,7 +34,8 @@ extern void go_add_search_path (const ch
 
 extern void go_create_gogo (int int_type_size, int pointer_size,
 			    const char* pkgpath, const char *prefix,
-			    const char *relative_import_path);
+			    const char *relative_import_path,
+			    bool check_divide_zero, bool check_divide_overflow);
 
 extern void go_parse_input_files (const char**, unsigned int,
 				  bool only_check_syntax,

Reply via email to