File 3801-Don-t-emit-badrecord-instructions-for-OTP-24.patch of Package erlang

From 3de9644217c73c9bae45af4b16e692365e064c98 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Wed, 31 May 2023 12:29:58 +0200
Subject: [PATCH 1/2] Don't emit badrecord instructions for OTP 24

---
 lib/compiler/src/beam_clean.erl | 25 ++++++++++++++++++++++++-
 lib/compiler/src/compile.erl    |  2 +-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/lib/compiler/src/beam_clean.erl b/lib/compiler/src/beam_clean.erl
index ef44bbfdf5..46ef7cf5ea 100644
--- a/lib/compiler/src/beam_clean.erl
+++ b/lib/compiler/src/beam_clean.erl
@@ -35,7 +35,8 @@ module({Mod,Exp,Attr,Fs0,_}, Opts) ->
     {Fs2,Lc} = clean_labels(Fs1),
     Fs3 = fix_swap(Fs2, Opts),
     Fs4 = fix_bs_create_bin(Fs3, Opts),
-    Fs = maybe_remove_lines(Fs4, Opts),
+    Fs5 = fix_badrecord(Fs4, Opts),
+    Fs = maybe_remove_lines(Fs5, Opts),
     {ok,{Mod,Exp,Attr,Fs,Lc}}.
 
 %% Determine the rootset, i.e. exported functions and
@@ -314,6 +315,28 @@ bs_puts([{atom,Type},_Seg,Unit,Flags0,Src,Size|Is], Fail) ->
     [I|bs_puts(Is, Fail)];
 bs_puts([], _Fail) -> [].
 
+%%%
+%%% If compatibility with a previous release (OTP 24 or earlier) has
+%%% been requested, eliminate badrecord instructions by translating
+%%% them to calls to error({badrecord,Value}).
+%%%
+
+fix_badrecord(Fs, Opts) ->
+    case proplists:get_bool(no_badrecord, Opts) of
+        false -> Fs;
+        true -> fold_functions(fun fix_badrecord/1, Fs)
+    end.
+
+fix_badrecord([{badrecord,Value}|Is]) ->
+    [{move,Value,{x,0}},
+     {test_heap,3,1},
+     {put_tuple2,{x,0},{list,[{atom,badrecord},{x,0}]}},
+     {call_ext_only,1,{extfunc,erlang,error,1}}|fix_badrecord(Is)];
+fix_badrecord([I|Is]) ->
+    [I|fix_badrecord(Is)];
+fix_badrecord([]) -> [].
+
+
 %%%
 %%% Helpers.
 %%%
diff --git a/lib/compiler/src/compile.erl b/lib/compiler/src/compile.erl
index 7fcbec9a3b..798b6018a9 100644
--- a/lib/compiler/src/compile.erl
+++ b/lib/compiler/src/compile.erl
@@ -274,7 +274,7 @@ expand_opt(r23, Os) ->
                               no_recv_opt, no_init_yregs |
                               expand_opt(r24, Os)]);
 expand_opt(r24, Os) ->
-    expand_opt(no_type_opt, [no_bs_create_bin, no_ssa_opt_ranges |
+    expand_opt(no_type_opt, [no_badrecord, no_bs_create_bin, no_ssa_opt_ranges |
                              expand_opt(r25, Os)]);
 expand_opt(r25, Os) ->
     [no_ssa_opt_update_tuple, no_bs_match, no_min_max_bifs | Os];
-- 
2.35.3

openSUSE Build Service is sponsored by