aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/rtl/hipe_rtl_exceptions.erl
blob: 879b84c0b040955926bfd0ec60e2341744ca1372 (plain) (blame)
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
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright (c) 2001 by Erik Johansson.  All Rights Reserved 
%% ====================================================================
%%  Filename : 	hipe_rtl_exceptions.erl
%%  Module   :	hipe_rtl_exceptions
%%  Purpose  :  
%%  Notes    : 
%%  History  :	* 2001-04-10 Erik Johansson (happi@it.uu.se): 
%%               Created.
%%  CVS      :
%%      $Id$
%% ====================================================================
%%  Exports  :
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(hipe_rtl_exceptions).

-export([gen_fail/3, gen_begin_handler/3]).

-include("../main/hipe.hrl").
-include("hipe_literals.hrl").

%% --------------------------------------------------------------------
%% Handle the Icode instruction
%% FAIL
%%
gen_fail(Class, Args, L) ->
  case Args of
    [Reason] ->
      case Class of
	exit -> 
	  gen_exit(Reason, L);
	throw ->
	  gen_throw(Reason, L);
	error ->
	  gen_error(Reason, L)
      end;
    [Arg1,Arg2] ->
      case Class of
 	error ->
	  Reason = Arg1, ArgList = Arg2,
	  gen_error(Reason, ArgList, L);
	rethrow ->
	  Exception = Arg1, Reason = Arg2,
	  gen_rethrow(Exception, Reason, L)
      end
  end.

%% --------------------------------------------------------------------
%% Exception handler glue; interfaces between the runtime system's
%% exception state and the Icode view of exception handling.

gen_begin_handler(I, VarMap, ConstTab) ->
  Ds = hipe_icode:begin_handler_dstlist(I),
  {Vars, VarMap1} = hipe_rtl_varmap:ivs2rvs(Ds, VarMap),
  [FTagVar,FValueVar,FTraceVar] = Vars,
  {[hipe_rtl:mk_comment('begin_handler'),
    hipe_rtl_arch:pcb_load(FValueVar, ?P_FVALUE),
    hipe_rtl_arch:pcb_load(FTraceVar, ?P_FTRACE),
    %% synthesized from P->freason by hipe_handle_exception()
    hipe_rtl_arch:pcb_load(FTagVar, ?P_ARG0)
   ], 
   VarMap1, ConstTab}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%      
%% Exceptions

gen_exit(Reason, L) ->
  gen_fail_call({erlang,exit,1}, [Reason], L).

gen_throw(Reason, L) ->
  gen_fail_call({erlang,throw,1}, [Reason], L).

gen_error(Reason, L) ->
  gen_fail_call({erlang,error,1}, [Reason], L).

gen_error(Reason, ArgList, L) ->
  gen_fail_call({erlang,error,2}, [Reason,ArgList], L).

gen_rethrow(Exception, Reason, L) ->
  gen_fail_call(rethrow, [Exception,Reason], L).

%% Generic fail. We can't use 'enter' with a fail label (there can be no
%% stack descriptor info for an enter), so for a non-nil fail label we
%% generate a call followed by a dummy return.
%%
%% Update: The runtime system now interprets the return address of
%% the BIF call in order to list the invoking MFA in the stack trace.
%% Generating tailcalls here defeats that purpose, so we no longer do that.

%%gen_fail_call(Fun, Args, []) ->
%%  [hipe_rtl:mk_enter(Fun, Args, remote)];
gen_fail_call(Fun, Args, L) ->
  ContLbl = hipe_rtl:mk_new_label(),
  Cont = hipe_rtl:label_name(ContLbl),
  Zero = hipe_rtl:mk_imm(hipe_tagscheme:mk_fixnum(0)),
  [hipe_rtl:mk_call([], Fun, Args, Cont, L, remote),
   ContLbl,
   hipe_rtl:mk_return([Zero])].