/
VM.cpp
415 lines (346 loc) · 10.2 KB
/
VM.cpp
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
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "Compiler/Compiler.h"
#include "Native/Core.h"
#include "Native/IO.h"
#include "Native/Net.h"
#include "Platform/Environment.h"
#include "Platform/Path.h"
#include "Syntax/ErrorReporter.h"
#include "Syntax/Parser.h"
#include "VM/Module.h"
#include "VM/Object.h"
#include "VM/VM.h"
namespace magpie
{
// TODO(bob): Move somewhere else?
struct ImportGraph
{
// Default constructor so we can use it in Array.
ImportGraph()
: module(NULL)
{}
ImportGraph(Module* module)
: module(module),
imports()
{
for (int i = 0; i < module->imports().count(); i++)
{
imports.add(module->imports()[i]);
}
}
Module* module;
Array<Module*> imports;
};
VM::VM()
: modules_(),
replModule_(NULL),
nativeNames_(),
natives_(),
recordTypes_(),
methods_(),
multimethods_(),
scheduler_(*this)
{
Memory::initialize(this, 1024 * 1024 * 2); // TODO(bob): Use non-magic number.
defineCoreNatives(*this);
defineIONatives(*this);
defineNetNatives(*this);
true_ = new AtomObject(ATOM_TRUE);
false_ = new AtomObject(ATOM_FALSE);
nothing_ = new AtomObject(ATOM_NOTHING);
done_ = new AtomObject(ATOM_DONE);
}
bool VM::runProgram(gc<String> path)
{
// Remember where the program is so we can import modules from there.
programDir_ = path::dir(path::real(path));
// Traverse the import graph.
ErrorReporter reporter;
addModule(reporter, NULL, path);
if (reporter.numErrors() > 0) return false;
// Sort the modules by their imports so that dependencies are run before
// modules that depend on them.
// See: http://en.wikipedia.org/wiki/Topological_sorting
// Clone the import graph so we can destructively modify it.
// TODO(bob): This does lots of array copies since ImportGraph stores an
// array directly. Do something smarter here.
Array<ImportGraph> graph;
for (int i = 0; i < modules_.count(); i++)
{
graph.add(ImportGraph(modules_[i]));
}
Array<Module*> modules;
while (graph.count() > 0)
{
bool madeProgress = false;
for (int i = 0; i < graph.count(); i++)
{
// See if all of this module's imports are accounted for.
if (graph[i].imports.count() == 0)
{
// They are, so it's ready to process.
modules.add(graph[i].module);
// And now everything that imports it doesn't have to worry about it
// anymore.
for (int j = 0; j < graph.count(); j++)
{
for (int k = 0; k < graph[j].imports.count(); k++)
{
if (graph[j].imports[k] == graph[i].module)
{
graph[j].imports.removeAt(k);
k--;
}
}
}
graph.removeAt(i);
i--;
madeProgress = true;
}
}
// Bail if there is an import cycle.
// TODO(bob): Better error-handling.
if (!madeProgress) return false;
}
// Compile all modules before running any of them.
for (int i = 0; i < modules.count(); i++)
{
if (!modules[i]->compile(*this)) return false;
}
scheduler_.run(modules);
return true;
}
void VM::importModule(ErrorReporter& reporter, Module* from,
gc<SourcePos> pos, gc<String> name)
{
Module* module = addModule(reporter, name, NULL);
if (module == NULL) return;
from->imports().add(module);
}
bool VM::initRepl()
{
ErrorReporter reporter;
Module* core = addModule(reporter, String::create("core"), NULL);
if (reporter.numErrors() > 0) return false;
if (!core->compile(*this)) return false;
scheduler_.runModule(core);
return true;
}
gc<Object> VM::evaluateReplExpression(gc<Expr> expr)
{
ErrorReporter reporter;
if (replModule_ == NULL)
{
replModule_ = new Module(String::create("<repl>"), String::create(""));
modules_.add(replModule_);
// Implicitly import core.
importModule(reporter, replModule_, NULL, String::create("core"));
}
// Compile it.
Compiler::compileExpression(*this, reporter, expr, replModule_);
if (reporter.numErrors() > 0) return gc<Object>();
return scheduler_.runModule(replModule_);
}
int VM::getModuleIndex(Module& module) const
{
int index = modules_.indexOf(&module);
ASSERT(index != -1, "Cannot get index of unknown module.");
return index;
}
void VM::reachRoots()
{
programDir_.reach();
recordTypes_.reach();
scheduler_.reach();
true_.reach();
false_.reach();
nothing_.reach();
done_.reach();
symbols_.reach();
methods_.reach();
for (int i = 0; i < modules_.count(); i++)
{
modules_[i]->reach();
}
}
gc<Object> VM::getAtom(Atom atom)
{
switch (atom) {
case ATOM_FALSE: return false_;
case ATOM_TRUE: return true_;
case ATOM_NOTHING: return nothing_;
case ATOM_DONE: return done_;
case ATOM_NO_METHOD:
return DynamicObject::create(getClass(CLASS_NO_METHOD_ERROR));
}
ASSERT(false, "Unknown atom ID.");
}
void VM::defineNative(const char* name, Native native)
{
nativeNames_.add(String::create(name));
natives_.add(native);
}
int VM::findNative(gc<String> name)
{
for (int i = 0; i < nativeNames_.count(); i++)
{
if (nativeNames_[i] == name) return i;
}
return -1;
}
int VM::addRecordType(const Array<int>& fields)
{
// TODO(bob): Should use a hash table or something more optimal.
// See if we already have a type for this signature.
for (int i = 0; i < recordTypes_.count(); i++)
{
RecordType& type = *recordTypes_[i];
// See if this record type matches what we're looking for.
if (fields.count() != type.numFields()) continue;
bool found = true;
for (int j = 0; j < fields.count(); j++)
{
// Compare the symbol IDs of the records.
// TODO(bob): Note, assumes symbols are sorted in both records.
if (type.getSymbol(j) != fields[j]) {
found = false;
break;
}
}
if (found) return i;
}
// It's a new type, so add it.
gc<RecordType> type = RecordType::create(fields);
recordTypes_.add(type);
return recordTypes_.count() - 1;
}
gc<RecordType> VM::getRecordType(int id)
{
return recordTypes_[id];
}
symbolId VM::addSymbol(gc<String> name)
{
// See if it's already in the table.
for (int i = 0; i < symbols_.count(); i++)
{
if (*name == *symbols_[i]) return i;
}
// It's a new symbol.
symbols_.add(name);
return symbols_.count() - 1;
}
gc<String> VM::getSymbol(symbolId symbol) const
{
ASSERT_INDEX(symbol, symbols_.count());
return symbols_[symbol];
}
methodId VM::addMethod(gc<Method> method)
{
methods_.add(method);
return methods_.count() - 1;
}
int VM::declareMultimethod(gc<String> signature)
{
// See if it's already declared.
int index = findMultimethod(signature);
if (index != -1) return index;
// It's a new multimethod.
multimethods_.add(new Multimethod(signature));
return multimethods_.count() - 1;
}
int VM::findMultimethod(gc<String> signature)
{
for (int i = 0; i < multimethods_.count(); i++)
{
if (signature == multimethods_[i]->signature()) return i;
}
// Not found.
return -1;
}
void VM::defineMethod(int multimethod, methodId method)
{
multimethods_[multimethod]->addMethod(methods_[method]);
}
gc<Multimethod> VM::getMultimethod(int multimethodId)
{
return multimethods_[multimethodId];
}
void VM::bindClass(const char* module, CoreClass core, const char* name)
{
ASSERT_INDEX(core, CLASS_MAX);
ASSERT(coreClasses_[core].isNull(), "Class is already bound.");
Module* moduleObj = findModule(module);
ASSERT_NOT_NULL(moduleObj);
gc<Object> value = moduleObj->getVariable(name);
ASSERT(!value.isNull(), "Could not find variable.");
coreClasses_[core] = asClass(value);
}
gc<ClassObject> VM::getClass(CoreClass core)
{
ASSERT_INDEX(core, CLASS_MAX);
ASSERT(!coreClasses_[core].isNull(), "Class has not been bound yet.");
return coreClasses_[core];
}
void VM::printUncaughtError(gc<Fiber> fiber, gc<Object> error)
{
// Lazy lookup the error channel.
if (errorChannel_.isNull())
{
Module* core = findModule("core");
ASSERT_NOT_NULL(core);
errorChannel_ = asChannel(core->getVariable("_errorChannel"));
}
// Send the error to the error-printing fiber and suspend the erroring one.
errorChannel_->send(fiber, error);
}
Module* VM::addModule(ErrorReporter& reporter, gc<String> name,
gc<String> path)
{
if (name.isNull())
{
ASSERT(!path.isNull(), "Must be given a path or a name.");
// Infer the name from the script file name.
// TODO(bob): Should take the file name without an extension.
name = path;
}
// Make sure it hasn't already been added.
// TODO(bob): Could make modules_ a hash table for performance.
for (int i = 0; i < modules_.count(); i++)
{
if (modules_[i]->name() == name)
{
return modules_[i];
}
}
// Locate the path to the module if we aren't given it.
if (path.isNull())
{
ASSERT(!name.isNull(), "Must be given a path or a name.");
path = locateModule(programDir_, name);
if (path.isNull())
{
reporter.error(NULL, "Could not find module \"%s\".", name->cString());
return NULL;
}
}
Module* module = new Module(name, path);
modules_.add(module);
if (!module->parse(reporter)) return NULL;
module->addImports(*this, reporter);
return module;
}
void VM::registerClass(Module* module, gc<ClassObject>& classObj,
const char* name)
{
int index = module->findVariable(String::create(name));
classObj = asClass(module->getVariable(index));
}
Module* VM::findModule(const char* name)
{
for (int i = 0; i < modules_.count(); i++)
{
if (*modules_[i]->name() == name) return modules_[i];
}
ASSERT(false, "Could not find core module.");
return NULL;
}
}