Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 204 lines (159 sloc) 4.766 kB
d769c94 Initial work on new C++ VM
Evan Phoenix authored
1 #include "vm.hpp"
2 #include "objectmemory.hpp"
304caf3 Bunch more work.
Evan Phoenix authored
3 #include "objects.hpp"
9eb4748 Flesh out Task considerable more
Evan Phoenix authored
4 #include "event.hpp"
5 #include "global_cache.hpp"
152cc4c Initial LLVM integration work.
Evan Phoenix authored
6 #include "llvm.hpp"
6ac48bd Fixed a LOT of dependencies by stripping objects.hpp and prelude.hpp …
Ryan Davis authored
7 #include "builtin/class.hpp"
8 #include "builtin/contexts.hpp"
9 #include "builtin/fixnum.hpp"
10 #include "builtin/list.hpp"
11 #include "builtin/symbol.hpp"
12 #include "builtin/thread.hpp"
13 #include "builtin/tuple.hpp"
14 #include "builtin/string.hpp"
d769c94 Initial work on new C++ VM
Evan Phoenix authored
15
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
16 #include "config.hpp"
17
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
18 #include <iostream>
19
d769c94 Initial work on new C++ VM
Evan Phoenix authored
20 namespace rubinius {
879a046 A whole slew. Enhanced type-info, debugged GC.
Evan Phoenix authored
21 VM::VM(size_t bytes) : probe(NULL), wait_events(false) {
310a461 Cleanup stack usage and fix interpreter
Evan Phoenix authored
22 config.compile_up_front = false;
5be79de Implement simple context cache
Evan Phoenix authored
23 context_cache = NULL;
24
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
25 user_config = new ConfigParser();
26
d769c94 Initial work on new C++ VM
Evan Phoenix authored
27 om = new ObjectMemory(bytes);
5be79de Implement simple context cache
Evan Phoenix authored
28
29 MethodContext::initialize_cache(this);
30
d769c94 Initial work on new C++ VM
Evan Phoenix authored
31 bootstrap_ontology();
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
32
33 events = new event::Loop(EVFLAG_FORKCHECK);
9eb4748 Flesh out Task considerable more
Evan Phoenix authored
34 global_cache = new GlobalCache;
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
35
569fccd @drbrain Move vm/Rakefile to rakelib/vm.rake, integrate it into the base rake …
drbrain authored
36 VMLLVMMethod::init("vm/instructions.bc");
49d27d7 Move LLVM's lazy code generation into the execute pipeline
Evan Phoenix authored
37 boot_threads();
d769c94 Initial work on new C++ VM
Evan Phoenix authored
38 }
39
40 VM::~VM() {
41 delete om;
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
42 delete events;
9eb4748 Flesh out Task considerable more
Evan Phoenix authored
43 delete global_cache;
ffcb663 Fix memory leak found while running tests
Evan Phoenix authored
44 llvm_cleanup();
d769c94 Initial work on new C++ VM
Evan Phoenix authored
45 }
304caf3 Bunch more work.
Evan Phoenix authored
46
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
47 void VM::boot_threads() {
48 Thread* thr = Thread::create(this);
49 thr->boot_task(this);
50
51 activate_thread(thr);
52 }
53
15fc505 More builtin objects, and a README file
Evan Phoenix authored
54 OBJECT VM::new_object(Class *cls) {
4a7b116 @dbussink Refactor Bignum::create to Bignum::from and clean up to_ functions
dbussink authored
55 return om->new_object(cls, cls->instance_fields->to_native());
15fc505 More builtin objects, and a README file
Evan Phoenix authored
56 }
57
5f2adf4 @brixen Replaced SymbolTable with native C++ class using STL.
brixen authored
58 SYMBOL VM::symbol(const char* str) {
59 return symbols.lookup(this, str);
304caf3 Bunch more work.
Evan Phoenix authored
60 }
61
195ef1b @brixen Isolated access to SymbolTable behind VM methods.
brixen authored
62 SYMBOL VM::symbol(String* str) {
5f2adf4 @brixen Replaced SymbolTable with native C++ class using STL.
brixen authored
63 return symbols.lookup(this, str);
195ef1b @brixen Isolated access to SymbolTable behind VM methods.
brixen authored
64 }
65
58105ff @brixen Added Symbol::all_symbols primitive and fixed up Ruby Symbol code.
brixen authored
66 SYMBOL VM::symbol(std::string str) {
67 return symbols.lookup(this, str);
195ef1b @brixen Isolated access to SymbolTable behind VM methods.
brixen authored
68 }
69
304caf3 Bunch more work.
Evan Phoenix authored
70 OBJECT VM::new_struct(Class* cls, size_t bytes) {
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
71 return om->new_object_bytes(cls, bytes);
304caf3 Bunch more work.
Evan Phoenix authored
72 }
73
6337ae0 @dgtized conversion from static allocated strings to char * is no longer autom…
dgtized authored
74 void type_assert(OBJECT obj, object_type type, const char* reason) {
fceff3d NativeFunction, Selector, SendSite
Evan Phoenix authored
75 if(obj->reference_p() && obj->obj_type != type) {
8a25128 Clean up exception usage.
Evan Phoenix authored
76 TypeError::raise(type, obj, reason);
fceff3d NativeFunction, Selector, SendSite
Evan Phoenix authored
77 } else if(type == FixnumType && !obj->fixnum_p()) {
8a25128 Clean up exception usage.
Evan Phoenix authored
78 TypeError::raise(type, obj, reason);
fceff3d NativeFunction, Selector, SendSite
Evan Phoenix authored
79 }
304caf3 Bunch more work.
Evan Phoenix authored
80 }
4c08cc4 Add object cleanup post collect and on shutdown
Evan Phoenix authored
81
bbe450f Code purge and reorganization.
Evan Phoenix authored
82 void VM::add_type_info(TypeInfo* ti) {
83 om->add_type_info(ti);
4c08cc4 Add object cleanup post collect and on shutdown
Evan Phoenix authored
84 ti->state = this;
85 }
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
86
388b751 Wire up primitives to be resolved and executed.
Evan Phoenix authored
87 TypeInfo* VM::find_type(int type) {
88 return om->type_info[type];
89 }
90
f1fa512 @drbrain Oh yeah, C++ isn't ruby.
drbrain authored
91 Thread *VM::current_thread() {
6f546f0 @drbrain Implement VM::current_thread().
drbrain authored
92 return globals.current_thread.get();
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
93 }
f10f155 Pass in valgrind to find some GC bugs
Evan Phoenix authored
94
95 void VM::collect() {
96 om->collect_young(globals.roots);
97 om->collect_mature(globals.roots);
98 }
52c078b Fleshing out Instruction tests, more type safety
Evan Phoenix authored
99
100 void VM::run_best_thread() {
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
101 Thread* next = NULL;
102
103 events->poll();
104
0bdfa48 @drbrain Test VM::run_best_thread(), reverse thread priority search order.
drbrain authored
105 for(size_t i = globals.scheduled_threads->field_count - 1; i > 0; i--) {
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
106 List* lst = as<List>(globals.scheduled_threads->at(i));
107 if(lst->empty_p()) continue;
108 next = as<Thread>(lst->shift(this));
0bdfa48 @drbrain Test VM::run_best_thread(), reverse thread priority search order.
drbrain authored
109 break;
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
110 }
111
112 if(!next) {
113 if(events->num_of_events() == 0) {
8a25128 Clean up exception usage.
Evan Phoenix authored
114 throw DeadLock("no runnable threads, present or future.");
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
115 }
116
117 wait_events = true;
118 return;
119 }
120
121 activate_thread(next);
52c078b Fleshing out Instruction tests, more type safety
Evan Phoenix authored
122 }
123
124 void VM::return_value(OBJECT val) {
125 globals.current_task->push(val);
126 }
127
128 void VM::queue_thread(Thread* thread) {
4a7b116 @dbussink Refactor Bignum::create to Bignum::from and clean up to_ functions
dbussink authored
129 List* lst = as<List>(globals.scheduled_threads->at(thread->priority->to_native()));
52c078b Fleshing out Instruction tests, more type safety
Evan Phoenix authored
130 lst->append(this, thread);
131 }
132
133 void VM::activate_thread(Thread* thread) {
134 globals.current_thread.set(thread);
135 globals.current_task.set(thread->task);
136 }
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
137
138 OBJECT VM::current_block() {
139 return globals.current_task->active->block;
140 }
141
da078e6 @mernen Changed VM::raise_from_errno signature to use const char*.
mernen authored
142 void VM::raise_from_errno(const char* msg) {
4f7df71 @brixen Added stub for VM::raise_exception.
brixen authored
143 // TODO: implement me
144 }
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
145
4f7df71 @brixen Added stub for VM::raise_exception.
brixen authored
146 void VM::raise_exception(Exception* exc) {
147 // TODO: implement me
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
148 }
149
150 void VM::inspect(OBJECT obj) {
151 if(obj->symbol_p()) {
152 String* str = as<Symbol>(obj)->to_str(this);
153 std::cout << "<Symbol :" << (char*)*str << ">" << std::endl;
154 } else if(obj->fixnum_p()) {
4a7b116 @dbussink Refactor Bignum::create to Bignum::from and clean up to_ functions
dbussink authored
155 std::cout << "<Fixnum " << as<Fixnum>(obj)->to_native() << ">" << std::endl;
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
156 } else {
157 std::cout << "<Object: " << (void*)obj << ">" << std::endl;
158 }
159 }
160
161 void VM::set_const(const char* name, OBJECT val) {
162 globals.object->set_const(this, (char*)name, val);
163 }
879a046 A whole slew. Enhanced type-info, debugged GC.
Evan Phoenix authored
164
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
165 void VM::set_const(Module* mod, const char* name, OBJECT val) {
166 mod->set_const(this, (char*)name, val);
167 }
310a461 Cleanup stack usage and fix interpreter
Evan Phoenix authored
168
169 void VM::print_backtrace() {
170 MethodContext* ctx = globals.current_task.get()->active;
171
172 while(!ctx->nil_p()) {
173 std::cout << (void*)ctx << ": ";
5c9c7f5 @drbrain Add MethodContext::line and update VM ruby backtrace to use it
drbrain authored
174 // HACK reports Object#[] instead of Hash::[], etc
310a461 Cleanup stack usage and fix interpreter
Evan Phoenix authored
175 std::cout << *ctx->module->name->to_str(this) << "#";
176
177 SYMBOL name = try_as<Symbol>(ctx->name);
178 if(name) {
179 std::cout << *name->to_str(this);
180 } else {
181 std::cout << *ctx->cm->name->to_str(this);
182 }
183
5c9c7f5 @drbrain Add MethodContext::line and update VM ruby backtrace to use it
drbrain authored
184 std::cout << ":" << ctx->line() << " in " << *ctx->cm->file->to_str(this);
c090ee3 When calling method_missing, prepend the method name
Evan Phoenix authored
185
310a461 Cleanup stack usage and fix interpreter
Evan Phoenix authored
186 std::cout << "\n";
187 ctx = ctx->sender;
188 }
189 }
190
191 Task* VM::new_task() {
192 Task* task = Task::create(this);
193 globals.current_task.set(task);
194 return task;
195 }
196
197 /* For debugging. */
198 extern "C" {
199 void __printbt__(STATE) {
200 state->print_backtrace();
201 }
202 }
d769c94 Initial work on new C++ VM
Evan Phoenix authored
203 };
Something went wrong with that request. Please try again.