Euphoria
Ticket #204:
machine crash in switch
-
Reported by
mattlewis
Sep 24, 2010
include std/regex.e as re
include std/console.e
function my_convert(sequence params)
switch params[1] do
case "1" then
return "one "
case "2" then
return "two "
case else
return "unknown "
end switch
end function
regex r = re:new(`\d`)
sequence result = re:find_replace_callback(r, "125", routine_id("my_convert"))
-- result = "one two unknown "
display( result )
Error:
bug.ex:5 in function my_convert()
A machine-level exception occurred during execution of this statement
... called from /usr/local/share/euphoria/include/std/regex.e:1218 in function find_replace_callback()
... called from tom.ex:15
--> See ex.err
Details
1. Comment by mattlewis
Sep 24, 2010
It looks like a problem calculating the jump table. Here is the disassembly of the function:
SubProgram [tom.ex-my_convert:15695] [params:15697]
[tom.ex:4] (30284)
1: 097 15697 # SEQUENCE_CHECK: [params:15697]
[tom.ex:5] (30285)
3: 025 15697 15557 15698 # RHS_SUBS: [params:15697] sub [LIT 1:15557] => [_temp_:15698]
7: 187 15698 15699 15700 26 210 # SWITCH: value [_temp_:15698] cases [LIT {"1","2"}:15699] jump
# [LIT {5,14}:15700] else goto 26
[tom.ex:7] (30287)
12: 210 15698 # DEREF_TEMP: [_temp_:15698]
14: 028 15695 15696 15703 # RETURNF: [LIT "one ":15703] block[15696]
18: 023 30 # ELSE goto 0030
[tom.ex:9] (30289)
20: 028 15695 15696 15706 # RETURNF: [LIT "two ":15706] block[15696]
24: 023 30 # ELSE goto 0030
[tom.ex:11] (30291)
26: 028 15695 15696 15708 # RETURNF: [LIT "unknown ":15708] block[15696]
30: 043 # BADRETURNF:
End SubProgram [my_convert:15695]
Examining the jump table (using eu.ex) I see that the jump for the first case is 5, which is correct. However, the second jump is 14, which would go to instruction 21. However, the correct place is 20.
Additionally, it looks like we're leaking that temp, except when the first case is taken.
2. Comment by mattlewis
Sep 26, 2010
When inlining a switch, it might be necessary to adjust the jump table, which would corrupt the non-inlined version of the switch. We also now make sure that if a temp value is what's switched upon, the temp is freed.