[committed] MSP430: Use enums to handle -mcpu= values

Message ID 20200908091620.oawsifshoqt73dgl@jozef-acer-manjaro
State New
Headers show
Series
  • [committed] MSP430: Use enums to handle -mcpu= values
Related show

Commit Message

Jozef Lawrynowicz Sept. 8, 2020, 9:16 a.m.
The -mcpu= option accepts only a handful of string values.
Using enums instead of strings to handle the accepted values removes the
need to have specific processing of the strings in the backend, and
simplifies any comparisons that need to be performed on the value.

It also allows the default value to have semantic equivalence to a user
set value, whilst retaining the ability to differentiate between them.
Practically, this allows a user set -mcpu= value to override the the ISA set by
-mmcu, whilst the default -mcpu= value can still have an explicit meaning.

Successfully regtested on trunk.

Committed as obvious.
From cd2d3822ca0f2f743601cc9d048d51f6d326f6a2 Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <jozef.l@mittosystems.com>

Date: Tue, 8 Sep 2020 10:10:17 +0100
Subject: [PATCH] MSP430: Use enums to handle -mcpu= values

The -mcpu= option accepts only a handful of string values.
Using enums instead of strings to handle the accepted values removes the
need to have specific processing of the strings in the backend, and
simplifies any comparisons which need to be performed on the value.

It also allows the default value to have semantic equivalence to a user
set value, whilst retaining the ability to differentiate between them.
Practically, this allows a user set -mcpu= value to override the the ISA set by
-mmcu, whilst the default -mcpu= value can still have an explicit meaning.

gcc/ChangeLog:

	* common/config/msp430/msp430-common.c (msp430_handle_option): Remove
	OPT_mcpu_ handling.
	Set target_cpu value to new enum values when parsing certain -mmcu=
	values.
	* config/msp430/msp430-opts.h (enum msp430_cpu_types): New.
	* config/msp430/msp430.c (msp430_option_override): Handle new
	target_cpu enum values.
	Set target_cpu using extracted value for given MCU when -mcpu=
	option is not passed by the user.
	* config/msp430/msp430.opt: Handle -mcpu= values using enums.

gcc/testsuite/ChangeLog:

	* gcc.target/msp430/mcpu-is-430.c: New test.
	* gcc.target/msp430/mcpu-is-430x.c: New test.
	* gcc.target/msp430/mcpu-is-430xv2.c: New test.
---
 gcc/common/config/msp430/msp430-common.c      | 26 +++----------------
 gcc/config/msp430/msp430-opts.h               | 12 +++++++++
 gcc/config/msp430/msp430.c                    | 21 ++++++---------
 gcc/config/msp430/msp430.opt                  | 23 +++++++++++++++-
 gcc/testsuite/gcc.target/msp430/mcpu-is-430.c | 10 +++++++
 .../gcc.target/msp430/mcpu-is-430x.c          | 12 +++++++++
 .../gcc.target/msp430/mcpu-is-430xv2.c        | 13 ++++++++++
 7 files changed, 80 insertions(+), 37 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/msp430/mcpu-is-430.c
 create mode 100644 gcc/testsuite/gcc.target/msp430/mcpu-is-430x.c
 create mode 100644 gcc/testsuite/gcc.target/msp430/mcpu-is-430xv2.c

-- 
2.28.0

Patch

diff --git a/gcc/common/config/msp430/msp430-common.c b/gcc/common/config/msp430/msp430-common.c
index 0e261c40015..65be3194683 100644
--- a/gcc/common/config/msp430/msp430-common.c
+++ b/gcc/common/config/msp430/msp430-common.c
@@ -27,7 +27,7 @@ 
 #include "opts.h"
 #include "flags.h"
 
-/* Check for generic -mcpu= and -mmcu= names here.  If found then we
+/* Check for generic -mmcu= names here.  If found then we
    convert to a baseline cpu name.  Otherwise we allow the option to
    be passed on to the backend where it can be checked more fully.  */
 
@@ -39,26 +39,6 @@  msp430_handle_option (struct gcc_options *opts ATTRIBUTE_UNUSED,
 {
   switch (decoded->opt_index)
     {
-    case OPT_mcpu_:
-      if (strcasecmp (decoded->arg, "msp430x") == 0
-	  || strcasecmp (decoded->arg, "msp430xv2") == 0
-	  || strcasecmp (decoded->arg, "430x") == 0
-	  || strcasecmp (decoded->arg, "430xv2") == 0)
-	{
-	  target_cpu = "msp430x";
-	}
-      else if (strcasecmp (decoded->arg, "msp430") == 0
-	       || strcasecmp (decoded->arg, "430") == 0)
-	{
-	  target_cpu = "msp430";
-	}
-      else
-	{
-	  error ("unrecognized argument of %<-mcpu%>: %s", decoded->arg);
-	  return false;
-	}
-      break;
-
     case OPT_mmcu_:
       /* For backwards compatibility we recognise two generic MCU
 	 430X names.  However we want to be able to generate special C
@@ -66,13 +46,13 @@  msp430_handle_option (struct gcc_options *opts ATTRIBUTE_UNUSED,
 	 to NULL.  */
       if (strcasecmp (decoded->arg, "msp430") == 0)
 	{
-	  target_cpu = "msp430";
+	  target_cpu = MSP430_CPU_MSP430;
 	  target_mcu = NULL;
 	}
       else if (strcasecmp (decoded->arg, "msp430x") == 0
 	       || strcasecmp (decoded->arg, "msp430xv2") == 0)
 	{
-	  target_cpu = "msp430x";
+	  target_cpu = MSP430_CPU_MSP430X;
 	  target_mcu = NULL;
 	}
       break;
diff --git a/gcc/config/msp430/msp430-opts.h b/gcc/config/msp430/msp430-opts.h
index 4d208306367..fa64677cb0b 100644
--- a/gcc/config/msp430/msp430-opts.h
+++ b/gcc/config/msp430/msp430-opts.h
@@ -29,6 +29,18 @@  enum msp430_hwmult_types
   MSP430_HWMULT_F5SERIES
 };
 
+enum msp430_cpu_types
+{
+  MSP430_CPU_MSP430,
+  MSP430_CPU_430,
+  MSP430_CPU_MSP430X_DEFAULT, /* The default setting, which will be overriden
+				 by any other -mcpu= value.  */
+  MSP430_CPU_MSP430X,
+  MSP430_CPU_430X,
+  MSP430_CPU_MSP430XV2,
+  MSP430_CPU_430XV2
+};
+
 enum msp430_regions
 {
   MSP430_REGION_ANY,
diff --git a/gcc/config/msp430/msp430.c b/gcc/config/msp430/msp430.c
index 129b916715e..d0557fe9058 100644
--- a/gcc/config/msp430/msp430.c
+++ b/gcc/config/msp430/msp430.c
@@ -160,15 +160,7 @@  msp430_option_override (void)
 
   init_machine_status = msp430_init_machine_status;
 
-  if (target_cpu)
-    {
-      /* gcc/common/config/msp430-common.c will have
-	 already canonicalised the string in target_cpu.  */
-      if (strcasecmp (target_cpu, "msp430x") == 0)
-	msp430x = true;
-      else /* target_cpu == "msp430" - already handled by the front end.  */
-	msp430x = false;
-    }
+  msp430x = target_cpu >= MSP430_CPU_MSP430X_DEFAULT;
 
   if (target_mcu)
     {
@@ -180,7 +172,7 @@  msp430_option_override (void)
 
 	  if (msp430_warn_mcu)
 	    {
-	      if (target_cpu && msp430x != xisa)
+	      if (target_cpu != MSP430_CPU_MSP430X_DEFAULT && msp430x != xisa)
 		warning (0, "MCU %qs supports %s ISA but %<-mcpu%> option "
 			 "is set to %s",
 			 target_mcu, xisa ? "430X" : "430",
@@ -212,7 +204,10 @@  msp430_option_override (void)
 			 "but %<-mhwmult%> is set to f5series",
 			 target_mcu, hwmult_name (extracted_mcu_data.hwmpy));
 	    }
-	  msp430x = xisa;
+	  /* Only override the default setting with the extracted ISA value if
+	     the user has not passed -mcpu=.  */
+	  if (target_cpu == MSP430_CPU_MSP430X_DEFAULT)
+	    msp430x = xisa;
 	}
       else
 	{
@@ -220,7 +215,7 @@  msp430_option_override (void)
 	    {
 	      if (msp430_warn_mcu)
 		{
-		  if (target_cpu == NULL)
+		  if (target_cpu == MSP430_CPU_MSP430X_DEFAULT)
 		    warning (0,
 			     "Unrecognized MCU name %qs, assuming that it is "
 			     "just a MSP430X with no hardware multiply.\n"
@@ -237,7 +232,7 @@  msp430_option_override (void)
 
 	      msp430_hwmult_type = MSP430_HWMULT_NONE;
 	    }
-	  else if (target_cpu == NULL)
+	  else if (target_cpu == MSP430_CPU_MSP430X_DEFAULT)
 	    {
 	      if (msp430_warn_mcu)
 		warning (0,
diff --git a/gcc/config/msp430/msp430.opt b/gcc/config/msp430/msp430.opt
index 8134ca7ac95..692e7dccc9e 100644
--- a/gcc/config/msp430/msp430.opt
+++ b/gcc/config/msp430/msp430.opt
@@ -23,9 +23,30 @@  Target Report Var(msp430_warn_devices_csv) Init(1)
 Warn if devices.csv is not found or there are problem parsing it (default: on).
 
 mcpu=
-Target Report Joined RejectNegative Var(target_cpu)
+Target Report Joined RejectNegative Var(target_cpu) ToLower Enum(msp430_cpu_types) Init(MSP430_CPU_MSP430X_DEFAULT)
 Specify the ISA to build for: msp430, msp430x, msp430xv2.
 
+Enum
+Name(msp430_cpu_types) Type(enum msp430_cpu_types)
+
+EnumValue
+Enum(msp430_cpu_types) String(msp430) Value(MSP430_CPU_MSP430) Canonical
+
+EnumValue
+Enum(msp430_cpu_types) String(430) Value(MSP430_CPU_MSP430)
+
+EnumValue
+Enum(msp430_cpu_types) String(msp430x) Value(MSP430_CPU_MSP430X) Canonical
+
+EnumValue
+Enum(msp430_cpu_types) String(430x) Value(MSP430_CPU_MSP430X)
+
+EnumValue
+Enum(msp430_cpu_types) String(msp430xv2) Value(MSP430_CPU_MSP430XV2) Canonical
+
+EnumValue
+Enum(msp430_cpu_types) String(430xv2) Value(MSP430_CPU_MSP430XV2)
+
 mlarge
 Target Report Mask(LARGE) RejectNegative
 Select large model - 20-bit addresses/pointers.
diff --git a/gcc/testsuite/gcc.target/msp430/mcpu-is-430.c b/gcc/testsuite/gcc.target/msp430/mcpu-is-430.c
new file mode 100644
index 00000000000..3139d2d52b0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/msp430/mcpu-is-430.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "-mcpu=*" "-mmcu=*" "-mlarge" } { "" } } */
+/* { dg-options "-mcpu=430" } */
+
+/* Verify that the alternate way of selecting the 430 ISA (i.e. with the
+   value "430" instead of "msp430") successfully selects the correct ISA.  */
+
+#ifdef __MSP430X__
+#error
+#endif
diff --git a/gcc/testsuite/gcc.target/msp430/mcpu-is-430x.c b/gcc/testsuite/gcc.target/msp430/mcpu-is-430x.c
new file mode 100644
index 00000000000..33100dc6bbd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/msp430/mcpu-is-430x.c
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "-mcpu=*" "-mmcu=*" } { "" } } */
+/* { dg-options "-mcpu=430x" } */
+
+/* Verify that the alternate way of selecting the 430X ISA (i.e. with the
+   value "430x" instead of "msp430x") successfully selects the correct
+   ISA.  */
+
+#ifndef __MSP430X__
+#error
+#endif
+
diff --git a/gcc/testsuite/gcc.target/msp430/mcpu-is-430xv2.c b/gcc/testsuite/gcc.target/msp430/mcpu-is-430xv2.c
new file mode 100644
index 00000000000..7bc3da1d2a4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/msp430/mcpu-is-430xv2.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "-mcpu=*" "-mmcu=*" } { "" } } */
+/* { dg-options "-mcpu=430XV2" } */
+
+/* Verify that the alternate way of selecting the 430XV2 ISA (i.e. with the
+   value "430XV2" instead of "msp430xv2") successfully selects the correct
+   ISA.  430xv2 doesn't actually have any observable effect on codegen, so we
+   have to just test for 430X.  */
+
+#ifndef __MSP430X__
+#error
+#endif
+