/usr/src/gcc-7/debian/patches/sh-builtin-trap.diff is in gcc-7-source 7.3.0-16ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | # DP: Proposed patch for PR target/70216, __builtin_trap() on SH
Index: b/src/gcc/config/sh/sh-c.c
===================================================================
--- a/src/gcc/config/sh/sh-c.c
+++ b/src/gcc/config/sh/sh-c.c
@@ -139,4 +139,7 @@ sh_cpu_cpp_builtins (cpp_reader* pfile)
cpp_define_formatted (pfile, "__SH_ATOMIC_MODEL_%s__",
selected_atomic_model ().cdef_name);
+
+ if (TARGET_SH4_TRAPA_SLEEP_BUG)
+ builtin_define ("__SH4_TRAPA_SLEEP_BUG__");
}
Index: b/src/gcc/config/sh/sh-protos.h
===================================================================
--- a/src/gcc/config/sh/sh-protos.h
+++ b/src/gcc/config/sh/sh-protos.h
@@ -35,6 +35,46 @@ enum sh_function_kind {
SFUNC_STATIC
};
+/* __builtin_trapa handling options. */
+struct sh_builtin_trap_handler
+{
+ enum enum_type
+ {
+ none = 0,
+ libcall,
+ trapa,
+
+ num_handlers
+ };
+
+ enum_type type;
+ int trapa_imm_val;
+};
+
+extern const sh_builtin_trap_handler& selected_builtin_trap_handler (void);
+
+#define TARGET_BUILTIN_TRAP_NONE \
+ (selected_builtin_trap_handler ().type == sh_builtin_trap_handler::none)
+
+#define TARGET_BUILTIN_TRAP_TRAPA \
+ (selected_builtin_trap_handler ().type == sh_builtin_trap_handler::trapa)
+
+#define TARGET_BUILTIN_TRAP_TRAPA_VAL_RTX \
+ GEN_INT (selected_builtin_trap_handler ().trapa_imm_val)
+
+#define TARGET_BUILTIN_TRAP_LIBCALL \
+ (selected_builtin_trap_handler ().type == sh_builtin_trap_handler::libcall)
+
+#ifdef SH4_TRAPA_SLEEP_BUG_DEFAULT
+#define TARGET_SH4_TRAPA_SLEEP_BUG \
+ (sh4_trapa_sleep_bug_option == -1 ? (SH4_TRAPA_SLEEP_BUG_DEFAULT != 0) \
+ : (sh4_trapa_sleep_bug_option != 0))
+#else
+#define TARGET_SH4_TRAPA_SLEEP_BUG \
+ ((sh4_trapa_sleep_bug_option == -1 && TARGET_SH4 && !TARGET_SH4A) \
+ || sh4_trapa_sleep_bug_option == 1)
+#endif
+
#ifdef RTX_CODE
extern rtx sh_fsca_sf2int (void);
extern rtx sh_fsca_int2sf (void);
Index: b/src/gcc/config/sh/sh.c
===================================================================
--- a/src/gcc/config/sh/sh.c
+++ b/src/gcc/config/sh/sh.c
@@ -752,6 +752,102 @@ got_mode_name:;
#undef err_ret
}
+/* Information on the currently selected __builtin_trap handler. */
+static sh_builtin_trap_handler selected_builtin_trap_handler_;
+
+const sh_builtin_trap_handler&
+selected_builtin_trap_handler (void)
+{
+ return selected_builtin_trap_handler_;
+}
+
+static sh_builtin_trap_handler
+parse_validate_builtin_trap_option (const char* str)
+{
+ const char* names[sh_builtin_trap_handler::num_handlers];
+ names[sh_builtin_trap_handler::none] = "none";
+ names[sh_builtin_trap_handler::libcall] = "libcall";
+ names[sh_builtin_trap_handler::trapa] = "trapa";
+
+ sh_builtin_trap_handler ret;
+
+ #if defined (SH_BUILTIN_TRAP_DEFAULT_TRAPA)
+ #if SH_BUILTIN_TRAP_DEFAULT_TRAPA < 0 || SH_BUILTIN_TRAP_DEFAULT_TRAPA > 255
+ #error default builtin trap trapa handler value out of range
+ #endif
+ ret.type = sh_builtin_trap_handler::trapa;
+ ret.trapa_imm_val = SH_BUILTIN_TRAP_DEFAULT_TRAPA;
+ #elif defined (SH_BUILTIN_TRAP_DEFAULT_LIBCALL)
+ ret.type = sh_builtin_trap_handler::libcall;
+ ret.trapa_imm_val = 0;
+ #else
+ ret.type = sh_builtin_trap_handler::none;
+ ret.trapa_imm_val = 0;
+ #endif
+
+ /* Handle empty string as 'none'. */
+ if (str == NULL || *str == '\0')
+ return ret;
+
+#define err_ret(...) do { error (__VA_ARGS__); return ret; } while (0)
+
+ std::vector<std::string> tokens;
+ for (std::stringstream ss (str); ss.good (); )
+ {
+ tokens.push_back (std::string ());
+ std::getline (ss, tokens.back (), ',');
+ }
+
+ if (tokens.empty ())
+ err_ret ("invalid builtin trap handler option");
+
+ /* The first token must be the handler name. */
+ {
+ for (size_t i = 0; i < sh_builtin_trap_handler::num_handlers; ++i)
+ if (tokens.front () == names[i])
+ {
+ ret.type = (sh_builtin_trap_handler::enum_type)i;
+ goto got_mode_name;
+ }
+
+ err_ret ("invalid builtin trap handler name \"%s\"",
+ tokens.front ().c_str ());
+got_mode_name:;
+ }
+
+ /* Go through the remaining tokens. */
+ bool have_imm = false;
+
+ for (size_t i = 1; i < tokens.size (); ++i)
+ {
+ if (tokens[i].find ("imm=") == 0)
+ {
+ have_imm = true;
+ std::string imm_str = tokens[i].substr (strlen ("imm="));
+ ret.trapa_imm_val = integral_argument (imm_str.c_str ());
+ if (imm_str.empty () || ret.trapa_imm_val == -1)
+ err_ret ("could not parse imm value \"%s\" in builtin trap handler "
+ "option", imm_str.c_str ());
+ }
+ else
+ err_ret ("unknown parameter \"%s\" in builtin trap handler option",
+ tokens[i].c_str ());
+ }
+
+ /* Check that the selection makes sense. */
+ if (ret.type == sh_builtin_trap_handler::trapa && have_imm == false)
+ err_ret ("immediate value not specified for trapa builtin trap handler");
+
+ if (ret.type == sh_builtin_trap_handler::trapa
+ && (ret.trapa_imm_val < 0 || ret.trapa_imm_val > 255))
+ err_ret ("immediate value for trapa builtin trap handler must be in the "
+ "range 0-255");
+
+ return ret;
+
+#undef err_ret
+}
+
/* Register SH specific RTL passes. */
extern opt_pass* make_pass_sh_treg_combine (gcc::context* ctx, bool split_insns,
const char* name);
@@ -967,6 +1063,10 @@ sh_option_override (void)
selected_atomic_model_
= parse_validate_atomic_model_option (sh_atomic_model_str);
+ /* Parse __builtin_trap handler option. */
+ selected_builtin_trap_handler_
+ = parse_validate_builtin_trap_option (sh_builtin_trap_handler_str);
+
register_sh_passes ();
}
Index: b/src/gcc/config/sh/sh.md
===================================================================
--- a/src/gcc/config/sh/sh.md
+++ b/src/gcc/config/sh/sh.md
@@ -8902,6 +8902,55 @@
;; Misc
;; -------------------------------------------------------------------------
+;; __builtin_trap
+(define_expand "trap"
+ [(trap_if (const_int 1) (const_int 0))]
+ "TARGET_SH1 && !TARGET_BUILTIN_TRAP_NONE"
+{
+ if (TARGET_BUILTIN_TRAP_TRAPA)
+ emit_insn (gen_trapa (TARGET_BUILTIN_TRAP_TRAPA_VAL_RTX));
+ else if (TARGET_BUILTIN_TRAP_LIBCALL)
+ {
+ rtx funcaddr = gen_reg_rtx (Pmode);
+ rtx lab = function_symbol (funcaddr, "__builtin_trap", SFUNC_STATIC).lab;
+ emit_insn (gen_trap_call (funcaddr, lab));
+ }
+ else
+ gcc_unreachable ();
+
+ DONE;
+})
+
+(define_insn "trapa"
+ [(trap_if (const_int 1) (match_operand:SI 0 "const_logical_operand"))]
+ "TARGET_SH1"
+{
+ if (TARGET_SH4_TRAPA_SLEEP_BUG)
+ return "trapa %0" "\n"
+ " or r0,r0" "\n"
+ " or r0,r0" "\n"
+ " or r0,r0" "\n"
+ " or r0,r0" "\n"
+ " or r0,r0";
+ else
+ return "trapa %0";
+}
+ [(set (attr "length") (if_then_else (match_test "TARGET_SH4_TRAPA_SLEEP_BUG")
+ (const_int 12) (const_int 2)))
+ (set_attr "in_delay_slot" "no")])
+
+(define_insn "trap_call"
+ [(trap_if (const_int 1) (const_int 0))
+ (use (match_operand:SI 0 "arith_reg_operand" "r,r"))
+ (use (match_operand:SI 1 "" "Z,Ccl"))
+ (clobber (reg:SI PR_REG))]
+ "TARGET_SH1"
+ "@
+ jsr @%0%#
+ bsrf %0\n%01:%#"
+ [(set_attr "type" "sfunc")
+ (set_attr "needs_delay_slot" "yes")])
+
;; String/block move insn.
(define_expand "movmemsi"
Index: b/src/gcc/config.gcc
===================================================================
--- a/src/gcc/config.gcc
+++ b/src/gcc/config.gcc
@@ -2692,6 +2692,39 @@ sh-*-elf* | sh[12346l]*-*-elf* | \
case ${with_endian} in
little*) tm_file="sh/little.h ${tm_file}" ;;
esac
+
+ case ${with_builtin_trap} in
+ "")
+ case ${target} in
+ sh*-*-linux*)
+ tm_defines="$tm_defines SH_BUILTIN_TRAP_DEFAULT_TRAPA=0x54"
+ esac
+ ;;
+ libcall)
+ tm_defines="$tm_defines SH_BUILTIN_TRAP_DEFAULT_LIBCALL=1"
+ ;;
+ trapa-[0123456789]*)
+ trapa_value=`echo $with_builtin_trap | tr -d -c 0-9`
+
+ if [ $trapa_value -gt 255 ]; then
+ echo "Invalid trapa number in --with-builtin-trap=$with_builtin_trap"
+ exit 1
+ fi
+
+ tm_defines="$tm_defines SH_BUILTIN_TRAP_DEFAULT_TRAPA=$trapa_value"
+ ;;
+ *)
+ echo "Invalid builtin trap handler in --with-builtin-trap=$with_builtin_trap"
+ exit 1
+ esac
+
+ if test x$enable_sh4_trapa_sleep_bug = xyes; then
+ tm_defines="$tm_defines SH4_TRAPA_SLEEP_BUG_DEFAULT=1"
+ fi
+ if test x$enable_sh4_trapa_sleep_bug = xno; then
+ tm_defines="$tm_defines SH4_TRAPA_SLEEP_BUG_DEFAULT=0"
+ fi
+
tm_file="${tm_file} dbxelf.h elfos.h sh/elf.h"
case ${target} in
sh*-*-linux*) tmake_file="${tmake_file} sh/t-linux"
Index: b/src/gcc/config/sh/sh.opt
===================================================================
--- a/src/gcc/config/sh/sh.opt
+++ b/src/gcc/config/sh/sh.opt
@@ -301,3 +301,11 @@ Enable the use of the fsrra instruction.
mlra
Target Report Var(sh_lra_flag) Init(0) Save
Use LRA instead of reload (transitional).
+
+msh4-trapa-sleep-bug
+Target Report Var(sh4_trapa_sleep_bug_option) Init(-1)
+Handle the trapa/sleep/undefined instruction 0xFFFD bug.
+
+mbuiltin-trap=
+Target Report RejectNegative Joined Var(sh_builtin_trap_handler_str)
+Specify what code to emit for __builtin_trap.
|