Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 175 lines (138 sloc) 4.406 kB
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
1 #include "Compiler.h"
2 #include "ErrorReporter.h"
275e7f3 @munificent Get rid of old Chunk stuff and start filling in Method.
authored
3 #include "Method.h"
6e6b803 @munificent Start hacking in support for method definitions.
authored
4 #include "MagpieString.h"
83e8aea @munificent Poke at GC a bit.
authored
5 #include "Object.h"
275e7f3 @munificent Get rid of old Chunk stuff and start filling in Method.
authored
6
7 namespace magpie
8 {
c6d0298 @munificent "Method" -> "Chunk".
authored
9 void Chunk::setCode(const Array<instruction>& code, int numSlots)
76e6ff7 @munificent Add basic support for def expressions. No closures yet.
authored
10 {
11 // TODO(bob): Copying here is lame!
12 code_ = code;
f0ea425 @munificent "slot" -> "register".
authored
13 numSlots_ = numSlots;
76e6ff7 @munificent Add basic support for def expressions. No closures yet.
authored
14 }
15
c6d0298 @munificent "Method" -> "Chunk".
authored
16 int Chunk::addConstant(gc<Object> constant)
76e6ff7 @munificent Add basic support for def expressions. No closures yet.
authored
17 {
18 // TODO(bob): Should check for duplicates. Only need one copy of any
19 // given constant.
20 constants_.add(constant);
21 return constants_.count() - 1;
22 }
23
c6d0298 @munificent "Method" -> "Chunk".
authored
24 gc<Object> Chunk::getConstant(int index) const
900b149 @munificent Minimal vertical slice is working now.
authored
25 {
26 ASSERT_INDEX(index, constants_.count());
27 return constants_[index];
28 }
76e6ff7 @munificent Add basic support for def expressions. No closures yet.
authored
29
c6d0298 @munificent "Method" -> "Chunk".
authored
30 void Chunk::debugTrace() const
bbbac53 @munificent Method calls.
authored
31 {
32 using namespace std;
33
34 // TODO(bob): Constants.
35
36 for (int i = 0; i < code_.count(); i++)
37 {
38 debugTrace(code_[i]);
39 }
40 }
41
c6d0298 @munificent "Method" -> "Chunk".
authored
42 void Chunk::debugTrace(instruction ins) const
bbbac53 @munificent Method calls.
authored
43 {
44 using namespace std;
45
46 switch (GET_OP(ins))
47 {
48 case OP_MOVE:
49 cout << "MOVE " << GET_A(ins) << " -> " << GET_B(ins);
50 break;
51
52 case OP_CONSTANT:
53 cout << "CONSTANT " << GET_A(ins) << " -> " << GET_B(ins);
54 break;
55
631122c @munificent Get "if" fully working.
authored
56 case OP_BUILT_IN:
57 cout << "BUILT_IN " << GET_A(ins) << " -> " << GET_B(ins);
bbbac53 @munificent Method calls.
authored
58 break;
59
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
60 case OP_METHOD:
61 cout << "METHOD " << GET_A(ins) << " <- " << GET_B(ins);
62 break;
63
2dd8dce @munificent Starting to sketch in very rough support for records.
authored
64 case OP_RECORD:
65 cout << "RECORD " << GET_A(ins) << "[" << GET_B(ins) << "] -> " << GET_C(ins);
ac0acb0 @munificent Allocate the result slot before nested parameter slots.
authored
66 break;
67
377f8f2 @munificent List objects and list literals.
authored
68 case OP_LIST:
69 cout << "LIST [" << GET_A(ins) << "..." << GET_B(ins) << "] -> " << GET_C(ins);
70 break;
71
650a012 @munificent Record destructuring in variable declarations.
authored
72 case OP_GET_FIELD:
73 cout << "GET_FIELD " << GET_A(ins) << "[" << GET_B(ins) << "] -> " << GET_C(ins);
74 break;
75
c7b2ca7 @munificent Handle destructuring failure in match expressions.
authored
76 case OP_TEST_FIELD:
77 cout << "TEST_FIELD " << GET_A(ins) << "[" << GET_B(ins) << "] -> " << GET_C(ins);
78 break;
79
b89dc08 @munificent Get basic top-level variables working.
authored
80 case OP_GET_VAR:
81 cout << "OP_GET_VAR module " << GET_A(ins) << ", var " << GET_B(ins) << " -> " << GET_C(ins);
fef1add @munificent Rough support for accessing names defined in imported modules.
authored
82 break;
b89dc08 @munificent Get basic top-level variables working.
authored
83
84 case OP_SET_VAR:
85 cout << "OP_SET_VAR module " << GET_A(ins) << ", var " << GET_B(ins) << " <- " << GET_C(ins);
86 break;
87
af31f9b @munificent Slap together the other comparison operators.
authored
88 case OP_EQUAL:
89 cout << "EQUAL " << GET_A(ins) << " == " << GET_B(ins) << " -> " << GET_C(ins);
90 break;
91
f7066dd @munificent "not" expressions.
authored
92 case OP_NOT:
93 cout << "NOT " << GET_A(ins);
94 break;
95
720464c @munificent Add "is" expressions.
authored
96 case OP_IS:
97 cout << "IS " << GET_A(ins) << " is " << GET_B(ins);
98 break;
99
bbbac53 @munificent Method calls.
authored
100 case OP_JUMP:
9edd499 @munificent Get while loops working.
authored
101 cout << "JUMP " << GET_A(ins) << " " << GET_B(ins);
bbbac53 @munificent Method calls.
authored
102 break;
103
104 case OP_JUMP_IF_FALSE:
105 cout << "JUMP_IF_FALSE " << GET_A(ins) << "? " << GET_B(ins);
106 break;
107
fada5ae @munificent Get "or" expressions working.
authored
108 case OP_JUMP_IF_TRUE:
109 cout << "JUMP_IF_TRUE " << GET_A(ins) << "? " << GET_B(ins);
110 break;
111
bbbac53 @munificent Method calls.
authored
112 case OP_CALL:
5e093ba @munificent Support multi-argument calls!
authored
113 cout << "CALL " << GET_A(ins) << "(" << GET_B(ins) << ") -> " << GET_C(ins);
bbbac53 @munificent Method calls.
authored
114 break;
115
80ccee2 @munificent "Primitive" -> "native".
authored
116 case OP_NATIVE:
117 cout << "NATIVE " << GET_A(ins) << "(" << GET_B(ins) << ") -> " << GET_C(ins);
e0a10f6 @munificent Make primitive calls expressions.
authored
118 break;
119
0037b64 @munificent Return expression.
authored
120 case OP_RETURN:
121 cout << "RETURN " << GET_A(ins);
bbbac53 @munificent Method calls.
authored
122 break;
efbe5fe @munificent Start hacking in throw/catch.
authored
123
124 case OP_THROW:
125 cout << "THROW " << GET_A(ins);
126 break;
a752135 @munificent Start to rough in basic error catching.
authored
127
128 case OP_ENTER_TRY:
e9ea058 @munificent Implement "match" expression.
authored
129 cout << "ENTER_TRY " << GET_A(ins);
a752135 @munificent Start to rough in basic error catching.
authored
130 break;
131
132 case OP_EXIT_TRY:
133 cout << "EXIT_TRY ";
134 break;
e9ea058 @munificent Implement "match" expression.
authored
135
136 case OP_TEST_MATCH:
137 cout << "TEST_MATCH " << GET_A(ins);
138 break;
bbbac53 @munificent Method calls.
authored
139 }
140
141 cout << endl;
142 }
143
c6d0298 @munificent "Method" -> "Chunk".
authored
144 void Chunk::reach()
ee55af6 @munificent Hack in basic support for primitive methods.
authored
145 {
a8dac75 @munificent Make reach() and instance method on gc and Array.
authored
146 constants_.reach();
ee55af6 @munificent Hack in basic support for primitive methods.
authored
147 }
148
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
149 Multimethod::Multimethod(gc<String> signature)
150 : signature_(signature),
151 chunk_(),
152 methods_()
153 {}
154
155 void Multimethod::addMethod(gc<Method> method)
bbbac53 @munificent Method calls.
authored
156 {
cb16051 @munificent Split out Compiler and MethodCompiler.
authored
157 methods_.add(method);
e0a10f6 @munificent Make primitive calls expressions.
authored
158
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
159 // Clear out the code since it needs to be recompiled.
160 chunk_ = NULL;
161 }
162
163 gc<Chunk> Multimethod::getChunk(VM& vm)
bbbac53 @munificent Method calls.
authored
164 {
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
165 // Re-compile if methods have been defined since the last time this was
166 // called.
167 if (chunk_.isNull())
bbbac53 @munificent Method calls.
authored
168 {
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
169 ErrorReporter reporter;
170 chunk_ = Compiler::compileMultimethod(vm, reporter, *this);
bbbac53 @munificent Method calls.
authored
171 }
172
3dfc1fe @munificent Moving to an imperative model for top-level stuff.
authored
173 return chunk_;
6e6b803 @munificent Start hacking in support for method definitions.
authored
174 }
275e7f3 @munificent Get rid of old Chunk stuff and start filling in Method.
authored
175 }
Something went wrong with that request. Please try again.