forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.hpp
401 lines (298 loc) · 8.76 KB
/
vm.hpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#ifndef RBX_VM_H
#define RBX_VM_H
#include "globals.hpp"
#include "gc/object_mark.hpp"
#include "gc/managed.hpp"
#include "thread_state.hpp"
#include "util/refcount.hpp"
#include "global_lock.hpp"
#include "maps.hpp"
#include "call_frame_list.hpp"
#include "async_message.hpp"
#include "gc/variable_buffer.hpp"
#include "gc/root_buffer.hpp"
#include "gc/slab.hpp"
#include "shared_state.hpp"
#include <vector>
#include <pthread.h>
#include <setjmp.h>
namespace llvm {
class Module;
}
namespace rubinius {
class Exception;
class LLVMState;
namespace event {
class Loop;
}
namespace profiler {
class Profiler;
}
namespace gc {
class WriteBarrier;
}
class GlobalCache;
class TaskProbe;
class Primitives;
class ObjectMemory;
class TypeInfo;
class MethodContext;
class String;
class Symbol;
class ConfigParser;
class TypeError;
class Assertion;
struct CallFrame;
class Object;
class Configuration;
struct Interrupts;
class VMManager;
class Waiter;
class LookupTable;
class SymbolTable;
class SharedState;
class Fiber;
enum MethodMissingReason {
eNone, ePrivate, eProtected, eSuper, eVCall, eNormal
};
class VM : public ManagedThread {
private:
CallFrame* saved_call_frame_;
uintptr_t stack_start_;
uintptr_t stack_limit_;
int stack_size_;
profiler::Profiler* profiler_;
bool run_signals_;
MethodMissingReason method_missing_reason_;
void* young_start_;
void* young_end_;
bool thread_step_;
public:
/* Data members */
SharedState& shared;
thread::Mutex local_lock_;
Waiter* waiter_;
bool interrupt_with_signal_;
pthread_t os_thread_;
ObjectMemory* om;
TypedRoot<TaskProbe*> probe;
Interrupts& interrupts;
bool check_local_interrupts;
ThreadState thread_state_;
// The Thread object for this VM state
TypedRoot<Thread*> thread;
// The current fiber running on this thread
TypedRoot<Fiber*> current_fiber;
// Root fiber, if any (lazily initialized)
TypedRoot<Fiber*> root_fiber;
static unsigned long cStackDepthMax;
public: /* Inline methods */
bool run_signals_p() {
return run_signals_;
}
void set_run_signals(bool val) {
run_signals_ = val;
}
ThreadState* thread_state() {
return &thread_state_;
}
GlobalLock& global_lock() {
return shared.global_lock();
}
thread::Mutex& local_lock() {
return local_lock_;
}
CallFrame** call_frame_location() {
return &saved_call_frame_;
}
void set_call_frame(CallFrame* frame) {
saved_call_frame_ = frame;
}
CallFrame* saved_call_frame() {
return saved_call_frame_;
}
// NOTE this will need to be VM local, ie Thread local, once the GIL
// is removed.
VariableRootBuffers* variable_buffers() {
return shared.variable_buffers();
}
RootBuffers* root_buffers() {
return shared.root_buffers();
}
GlobalCache* global_cache() {
return shared.global_cache;
}
Globals& globals() {
return shared.globals;
}
void* stack_start() {
return reinterpret_cast<void*>(stack_start_);
}
int stack_size() {
return stack_size_;
}
void reset_stack_limit() {
// @TODO assumes stack growth direction
stack_limit_ = (stack_start_ - stack_size_) + (4096 * 3);
}
void set_stack_bounds(uintptr_t start, int length) {
stack_start_ = start;
stack_size_ = length;
reset_stack_limit();
}
void set_stack_start(void* s) {
set_stack_bounds(reinterpret_cast<uintptr_t>(s), stack_size_);
}
void set_stack_size(int s) {
set_stack_bounds(stack_start_, s);
}
void get_attention() {
stack_limit_ = stack_start_;
}
bool detect_stack_condition(void* end) {
// @TODO assumes stack growth direction
return reinterpret_cast<uintptr_t>(end) < stack_limit_;
}
bool check_stack(CallFrame* call_frame, void* end) {
// @TODO assumes stack growth direction
if(unlikely(reinterpret_cast<uintptr_t>(end) < stack_limit_)) {
raise_stack_error(call_frame);
return false;
}
return true;
}
bool check_interrupts(CallFrame* call_frame, void* end);
MethodMissingReason method_missing_reason() {
return method_missing_reason_;
}
void set_method_missing_reason(MethodMissingReason reason) {
method_missing_reason_ = reason;
}
bool young_object_p(Object* obj) {
return obj >= young_start_ && obj <= young_end_;
}
bool thread_step() {
return thread_step_;
}
void clear_thread_step() {
thread_step_ = false;
}
void set_thread_step() {
thread_step_ = true;
}
public:
static void init_stack_size();
// Better than current_state, uses a pthread local.
static VM* current();
static void set_current(VM* vm);
// Returns the current VM state object.
static VM* current_state();
// Registers a VM* object as the current state.
static void register_state(VM*);
static void discard(VM*);
public:
/* Prototypes */
VM(SharedState& shared);
void check_exception(CallFrame* call_frame);
void initialize();
// Initialize the basic objects and the execution machinery
void boot();
void bootstrap_class();
void bootstrap_ontology();
void bootstrap_symbol();
void initialize_config();
void setup_errno(int num, const char* name, Class* sce, Module* ern);
void bootstrap_exceptions();
void initialize_fundamental_constants();
void initialize_builtin_classes();
void initialize_platform_data();
void boot_threads();
void set_current_fiber(Fiber* fib);
void raise_stack_error(CallFrame* call_frame);
Object* new_object_typed(Class* cls, size_t bytes, object_type type);
Object* new_object_typed_mature(Class* cls, size_t bytes, object_type type);
Object* new_object_from_type(Class* cls, TypeInfo* ti);
template <class T>
T* new_object(Class *cls) {
return reinterpret_cast<T*>(new_object_typed(cls, sizeof(T), T::type));
}
template <class T>
T* new_struct(Class* cls, size_t bytes = 0) {
T* obj = reinterpret_cast<T*>(new_object_typed(cls, sizeof(T) + bytes, T::type));
return obj;
}
template <class T>
T* new_object_mature(Class *cls) {
return reinterpret_cast<T*>(new_object_typed_mature(cls, sizeof(T), T::type));
}
// Create an uninitialized Class object
Class* new_basic_class(Class* sup);
// Create a Class of name +name+ as an Object subclass
Class* new_class(const char* name);
// Create a Class of name +name+ as a subclass of +super_class+
Class* new_class(const char* name, Class* super_class);
// Create a Class of name +name+ as a subclass of +sup+
// under Module +under+
Class* new_class(const char* name, Class* sup, Module* under);
// Create a Class of name +name+ under +under+
Class* new_class_under(const char* name, Module* under);
Module* new_module(const char* name, Module* under = NULL);
Symbol* symbol(const char* str);
Symbol* symbol(String* str);
TypeInfo* find_type(int type);
void init_ffi();
void init_native_libraries();
Thread* current_thread();
void collect(CallFrame* call_frame);
// Check the flags in ObjectMemory and collect if we need to.
void collect_maybe(CallFrame* call_frame);
void raise_from_errno(const char* reason);
void raise_exception(Exception* exc);
Exception* new_exception(Class* cls, const char* msg);
Object* current_block();
void set_const(const char* name, Object* val);
void set_const(Module* mod, const char* name, Object* val);
#ifdef ENABLE_LLVM
llvm::Module* llvm_module();
void llvm_cleanup();
#endif
void print_backtrace();
// Run the garbage collectors as soon as you can
void run_gc_soon();
void install_waiter(Waiter& waiter);
void clear_waiter();
bool wakeup();
void interrupt_with_signal();
bool should_interrupt_with_signal() {
return interrupt_with_signal_;
}
bool waiting_p() {
return interrupt_with_signal_ || waiter_ != NULL;
}
void register_raise(Exception* exc);
bool process_async(CallFrame* call_frame);
bool check_async(CallFrame* call_frame) {
if(check_local_interrupts) {
return process_async(call_frame);
}
return true;
}
profiler::Profiler* profiler();
void remove_profiler();
// For thread-local roots
static std::list<Roots*>* roots;
};
class StopTheWorld {
VM* vm_;
public:
StopTheWorld(STATE) :
vm_(state)
{
vm_->shared.stop_the_world();
}
~StopTheWorld() {
vm_->shared.restart_world();
}
};
};
#endif