From 979537042170236085d754921bfbbe6d4c253494 Mon Sep 17 00:00:00 2001
From: Anthony Ramine <n.oxyde@gmail.com>
Date: Fri, 24 May 2013 18:58:01 +0200
Subject: Fix renaming of bs_put_string instructions

The clause was formerly commented-out because at this point in the code,
no bs_put_string instruction has been generated yet when compiling from
Erlang.

If an Erlang module is compiled to BEAM assembly and the result contains
a bs_put_string instruction, the output can't be compiled to binary
anymore and the compiler crashes with the following error:

$ erlc prs.S
Function: compress/1
prs.S:none: internal error in beam_block;
crash reason: {{case_clause,
                   {'EXIT',
                       {function_clause,
                           [{beam_utils,live_opt,
                                [[{bs_put_string,1,{string,[0]}},
                                  {bs_init,
                                      {f,0},
                                      {bs_append,0,8,{field_flags,[]}},
                                      0,
                                      [{integer,8},{x,0}],
                                      {x,1}},
                                  {label,2}],
                                 2,
                                 {1,{1,1,nil,nil}},
                                 [{block,
                                      [{'%live',2},
                                       {set,[{x,0}],[{x,1}],move},
                                       {'%live',1}]},
                                  return]],
                                [{file,"beam_utils.erl"},{line,639}]},
                            {beam_utils,live_opt,1,
                                [{file,"beam_utils.erl"},{line,205}]},
                            {beam_block,function,2,
                                [{file,"beam_block.erl"},{line,38}]},
                            {lists,mapfoldl,3,
                                [{file,"lists.erl"},{line,1329}]},
                            {beam_block,module,2,
                                [{file,"beam_block.erl"},{line,29}]},
                            {compile,'-select_passes/2-anonymous-2-',2,
                                [{file,"compile.erl"},{line,476}]},
                            {compile,'-internal_comp/4-anonymous-1-',2,
                                [{file,"compile.erl"},{line,276}]},
                            {compile,fold_comp,3,
                                [{file,"compile.erl"},{line,294}]}]}}},
               [{compile,'-select_passes/2-anonymous-2-',2,
                    [{file,"compile.erl"},{line,476}]},
                {compile,'-internal_comp/4-anonymous-1-',2,
                    [{file,"compile.erl"},{line,276}]},
                {compile,fold_comp,3,[{file,"compile.erl"},{line,294}]},
                {compile,internal_comp,4,[{file,"compile.erl"},{line,278}]},
                {compile,'-do_compile/2-anonymous-0-',2,
                    [{file,"compile.erl"},{line,152}]}]}
---
 lib/compiler/src/beam_a.erl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'lib/compiler')

diff --git a/lib/compiler/src/beam_a.erl b/lib/compiler/src/beam_a.erl
index 1c51226314..b348e854a0 100644
--- a/lib/compiler/src/beam_a.erl
+++ b/lib/compiler/src/beam_a.erl
@@ -70,8 +70,8 @@ rename_instr({bs_put_utf16=I,F,Fl,Src}) ->
     {bs_put,F,{I,Fl},[Src]};
 rename_instr({bs_put_utf32=I,F,Fl,Src}) ->
     {bs_put,F,{I,Fl},[Src]};
-%% rename_instr({bs_put_string,_,_}=I) ->
-%%     {bs_put,{f,0},I,[]};
+rename_instr({bs_put_string,_,_}=I) ->
+    {bs_put,{f,0},I,[]};
 rename_instr({bs_add=I,F,[Src1,Src2,U],Dst}) when is_integer(U) ->
     {bif,I,F,[Src1,Src2,{integer,U}],Dst};
 rename_instr({bs_utf8_size=I,F,Src,Dst}) ->
-- 
cgit v1.2.3