]> gitweb.factorcode.org Git - factor.git/blob - vm/errors.cpp
Merge branch 'docs-linearization-typo' of http://github.com/mncharity/factor
[factor.git] / vm / errors.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void fatal_error(const char *msg, cell tagged)
7 {
8         std::cout << "fatal_error: " << msg;
9         std::cout << ": " << std::hex << tagged << std::dec;
10         std::cout << std::endl;
11         exit(1);
12 }
13
14 void critical_error(const char *msg, cell tagged)
15 {
16         std::cout << "You have triggered a bug in Factor. Please report.\n";
17         std::cout << "critical_error: " << msg;
18         std::cout << ": " << std::hex << tagged << std::dec;
19         std::cout << std::endl;
20         current_vm()->factorbug();
21 }
22
23 void out_of_memory()
24 {
25         std::cout << "Out of memory\n\n";
26         current_vm()->dump_generations();
27         exit(1);
28 }
29
30 void factor_vm::throw_error(cell error)
31 {
32         /* If the error handler is set, we rewind any C stack frames and
33         pass the error to user-space. */
34         if(!current_gc && to_boolean(special_objects[ERROR_HANDLER_QUOT]))
35         {
36                 /* If error was thrown during heap scan, we re-enable the GC */
37                 gc_off = false;
38
39                 /* Reset local roots */
40                 data_roots.clear();
41                 bignum_roots.clear();
42                 code_roots.clear();
43
44                 /* If we had an underflow or overflow, data or retain stack
45                 pointers might be out of bounds */
46                 ctx->fix_stacks();
47
48                 ctx->push(error);
49
50                 unwind_native_frames(special_objects[ERROR_HANDLER_QUOT],
51                         ctx->callstack_top);
52         }
53         /* Error was thrown in early startup before error handler is set, just
54         crash. */
55         else
56         {
57                 std::cout << "You have triggered a bug in Factor. Please report.\n";
58                 std::cout << "early_error: ";
59                 print_obj(error);
60                 std::cout << std::endl;
61                 factorbug();
62         }
63 }
64
65 void factor_vm::general_error(vm_error_type error, cell arg1, cell arg2)
66 {
67         throw_error(allot_array_4(special_objects[OBJ_ERROR],
68                 tag_fixnum(error),arg1,arg2));
69 }
70
71 void factor_vm::type_error(cell type, cell tagged)
72 {
73         general_error(ERROR_TYPE,tag_fixnum(type),tagged);
74 }
75
76 void factor_vm::not_implemented_error()
77 {
78         general_error(ERROR_NOT_IMPLEMENTED,false_object,false_object);
79 }
80
81 void factor_vm::memory_protection_error(cell addr)
82 {
83         /* Retain and call stack underflows are not supposed to happen */
84
85         if(ctx->datastack_seg->underflow_p(addr))
86                 general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object);
87         else if(ctx->datastack_seg->overflow_p(addr))
88                 general_error(ERROR_DATASTACK_OVERFLOW,false_object,false_object);
89         else if(ctx->retainstack_seg->underflow_p(addr))
90                 general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object);
91         else if(ctx->retainstack_seg->overflow_p(addr))
92                 general_error(ERROR_RETAINSTACK_OVERFLOW,false_object,false_object);
93         else if(ctx->callstack_seg->underflow_p(addr))
94                 general_error(ERROR_CALLSTACK_OVERFLOW,false_object,false_object);
95         else if(ctx->callstack_seg->overflow_p(addr))
96                 general_error(ERROR_CALLSTACK_UNDERFLOW,false_object,false_object);
97         else
98                 general_error(ERROR_MEMORY,from_unsigned_cell(addr),false_object);
99 }
100
101 void factor_vm::signal_error(cell signal)
102 {
103         general_error(ERROR_SIGNAL,from_unsigned_cell(signal),false_object);
104 }
105
106 void factor_vm::divide_by_zero_error()
107 {
108         general_error(ERROR_DIVIDE_BY_ZERO,false_object,false_object);
109 }
110
111 void factor_vm::fp_trap_error(unsigned int fpu_status)
112 {
113         general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),false_object);
114 }
115
116 /* For testing purposes */
117 void factor_vm::primitive_unimplemented()
118 {
119         not_implemented_error();
120 }
121
122 void factor_vm::memory_signal_handler_impl()
123 {
124         scrub_return_address();
125         memory_protection_error(signal_fault_addr);
126 }
127
128 void memory_signal_handler_impl()
129 {
130         current_vm()->memory_signal_handler_impl();
131 }
132
133 void factor_vm::misc_signal_handler_impl()
134 {
135         scrub_return_address();
136         signal_error(signal_number);
137 }
138
139 void misc_signal_handler_impl()
140 {
141         current_vm()->misc_signal_handler_impl();
142 }
143
144 void factor_vm::fp_signal_handler_impl()
145 {
146         /* Clear pending exceptions to avoid getting stuck in a loop */
147         set_fpu_state(get_fpu_state());
148
149         scrub_return_address();
150         fp_trap_error(signal_fpu_status);
151 }
152
153 void fp_signal_handler_impl()
154 {
155         current_vm()->fp_signal_handler_impl();
156 }
157
158 }