Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 287 lines (224 sloc) 6.396 kB
0489e93 @brixen Always use #include <xxx.h> rather than <cxxx>.
brixen authored
1 #include <ctype.h>
2 #include <string.h>
3 #include <math.h>
cd1bc42 @brixen Fix marshalling float values.
brixen authored
4 #include <iomanip>
3a0e9a4 @rue Renamed vm/object.hpp to vm/object_utils.hpp. File/#include shuffling.
rue authored
5
8e92bdf Fixed dependencies for marshal
Ryan Davis authored
6 #include "objectmemory.hpp"
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
7 #include "marshal.hpp"
8
b5771b4 @brixen Import libgdtoa and use ruby_strtod instead of libc strtod.
brixen authored
9 #include "object_utils.hpp"
3a0e9a4 @rue Renamed vm/object.hpp to vm/object_utils.hpp. File/#include shuffling.
rue authored
10
621d091 Fix includes to properly specify project external headers
Evan Phoenix authored
11 #include <tommath.h>
12 #include <gdtoa.h>
0322cf8 Reworked the RBC format.
Tilman Sauerbeck authored
13
6ac48bd Fixed a LOT of dependencies by stripping objects.hpp and prelude.hpp …
Ryan Davis authored
14 #include "builtin/array.hpp"
35dbaf5 @dbussink Rename CompiledMethod to CompiledCode
dbussink authored
15 #include "builtin/compiledcode.hpp"
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
16 #include "builtin/encoding.hpp"
6ac48bd Fixed a LOT of dependencies by stripping objects.hpp and prelude.hpp …
Ryan Davis authored
17 #include "builtin/fixnum.hpp"
a390c02 More dependency fixes
Ryan Davis authored
18 #include "builtin/float.hpp"
6ac48bd Fixed a LOT of dependencies by stripping objects.hpp and prelude.hpp …
Ryan Davis authored
19 #include "builtin/iseq.hpp"
20 #include "builtin/string.hpp"
21 #include "builtin/symbol.hpp"
22 #include "builtin/tuple.hpp"
23
baf87dc GC performance tweeks
Evan Phoenix authored
24 #include "detection.hpp"
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
25
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
26 #define STACK_BUF_SZ 1024
27
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
28 namespace rubinius {
29
3ceb20f @evanphx Fix 'for' in 1.9 mode
evanphx authored
30 Object* UnMarshaller::get_constant() {
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
31 char stack_data[STACK_BUF_SZ];
32 char *malloc_data = NULL;
33 char *data = stack_data;
34
3ceb20f @evanphx Fix 'for' in 1.9 mode
evanphx authored
35 size_t count;
36
37 stream >> count;
38 stream.get();
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
39
40 if(count >= STACK_BUF_SZ) {
41 malloc_data = (char*)malloc(count + 1);
42 data = malloc_data;
43 }
44
3ceb20f @evanphx Fix 'for' in 1.9 mode
evanphx authored
45 stream.read(data, count + 1);
46 data[count] = 0; // clamp
47
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
48 Object* cls = state->vm()->path2class(data);
49
50 if(malloc_data) {
51 free(malloc_data);
52 }
53 return cls;
3ceb20f @evanphx Fix 'for' in 1.9 mode
evanphx authored
54 }
55
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
56 Object* UnMarshaller::get_encoding() {
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
57 char stack_data[STACK_BUF_SZ];
58 char *malloc_data = NULL;
59 char *data = stack_data;
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
60 size_t count;
61
62 stream >> count;
63 stream.get();
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
64
65 if(count >= STACK_BUF_SZ) {
66 malloc_data = (char*)malloc(count + 1);
67 data = malloc_data;
68 }
69
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
70 stream.read(data, count + 1);
71 data[count] = 0; // clamp
72
73 if(count > 0) {
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
74 Encoding* enc = Encoding::find(state, data);
75 if(malloc_data) {
76 free(malloc_data);
77 }
78 return enc;
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
79 } else {
832cbb3 @brixen Removed use of Qtrue|false|nil|undef from native code.
brixen authored
80 return cNil;
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
81 }
82 }
83
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
84 Object* UnMarshaller::get_int() {
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
85 std::string data;
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
86 stream >> data;
0322cf8 Reworked the RBC format.
Tilman Sauerbeck authored
87
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
88 return Bignum::from_string(state, data.c_str(), 16);
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
89 }
90
91 String* UnMarshaller::get_string() {
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
92 size_t count;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
93
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
94 Encoding* enc = try_as<Encoding>(unmarshal());
95
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
96 stream >> count;
7e36fb1 @wilson Fix String::append, UnMarshaller::get_string, and String::create.
wilson authored
97 // String::create adds room for a trailing null on its own
a352624 Start of new LLVM JIT
Evan Phoenix authored
98 // using pinned here allows later stages to optimize these literal
99 // strings better.
7e36fb1 @wilson Fix String::append, UnMarshaller::get_string, and String::create.
wilson authored
100 String* str = String::create(state, NULL, count);
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
101
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
102 stream.get(); // read off newline
c4b8ce3 Abstract all uses of ByteArray::bytes
Evan Phoenix authored
103 stream.read(reinterpret_cast<char*>(str->byte_address()), count);
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
104 stream.get(); // read off newline
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
105
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
106 if(enc) str->encoding(state, enc);
107
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
108 return str;
109 }
110
1fdd151 @rue Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
111 Symbol* UnMarshaller::get_symbol() {
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
112 char stack_data[STACK_BUF_SZ];
113 char *malloc_data = NULL;
114 char *data = stack_data;
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
115 size_t count;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
116
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
117 stream >> count;
118 stream.get();
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
119
120 if(count >= STACK_BUF_SZ) {
121 malloc_data = (char*)malloc(count + 1);
122 data = malloc_data;
123 }
124
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
125 stream.read(data, count + 1);
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
126 data[count] = 0; // clamp
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
127
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
128 Symbol* sym = state->symbol(data);
129
130 if(malloc_data) {
131 free(malloc_data);
132 }
133
134 return sym;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
135 }
136
137 Tuple* UnMarshaller::get_tuple() {
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
138 size_t count;
139 stream >> count;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
140
141 Tuple* tup = Tuple::create(state, count);
142
143 for(size_t i = 0; i < count; i++) {
144 tup->put(state, i, unmarshal());
145 }
146
147 return tup;
148 }
149
cd1bc42 @brixen Fix marshalling float values.
brixen authored
150 #define FLOAT_EXP_OFFSET 58
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
151
152 Float* UnMarshaller::get_float() {
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
153 std::string line;
154 const char* data;
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
155
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
156 // discard the delimiter
157 stream.get();
158
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
159 std::getline(stream, line);
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
160 if(stream.fail()) {
a1704be @brixen Rework exceptions in the VM.
brixen authored
161 Exception::type_error(state, "Unable to unmarshal Float: failed to read value");
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
162 }
163
0194292 @dbussink Handle cases where people throw in insanely big elements
dbussink authored
164 data = line.c_str();
cd1bc42 @brixen Fix marshalling float values.
brixen authored
165 if(data[0] == ' ') {
166 double x;
167 long e;
168
2c016fa @brixen Some capi fixes to compile do_postgres gem.
brixen authored
169 x = ::ruby_strtod(data, NULL);
cd1bc42 @brixen Fix marshalling float values.
brixen authored
170 e = strtol(data+FLOAT_EXP_OFFSET, NULL, 10);
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
171
cd1bc42 @brixen Fix marshalling float values.
brixen authored
172 // This is necessary because exp2(1024) yields inf
173 if(e == 1024) {
174 double root_exp = ::exp2(512);
175 return Float::create(state, x * root_exp * root_exp);
176 } else {
177 return Float::create(state, x * ::exp2(e));
178 }
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
179 } else {
180 // avoid compiler warning
181 double zero = 0.0;
abc2b99 fix FAST compile error
Evan Phoenix authored
182 double val = 0.0;
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
183
184 if(!strncasecmp(data, "Infinity", 8U)) {
185 val = 1.0;
cd1bc42 @brixen Fix marshalling float values.
brixen authored
186 } else if(!strncasecmp(data, "-Infinity", 9U)) {
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
187 val = -1.0;
cd1bc42 @brixen Fix marshalling float values.
brixen authored
188 } else if(!strncasecmp(data, "NaN", 3U)) {
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
189 val = zero;
190 } else {
a1704be @brixen Rework exceptions in the VM.
brixen authored
191 Exception::type_error(state, "Unable to unmarshal Float: invalid format");
ae041e0 @brixen Handle unmarshalling literals for Infinity, etc.
brixen authored
192 }
193
194 return Float::create(state, val / zero);
195 }
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
196 }
197
7fdf623 @ileitch Define the length of an opcode.
ileitch authored
198 #define OPCODE_LENGTH 32
199
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
200 InstructionSequence* UnMarshaller::get_iseq() {
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
201 size_t count;
202 long op;
7fdf623 @ileitch Define the length of an opcode.
ileitch authored
203 char data[OPCODE_LENGTH];
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
204 stream >> count;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
205
7ca3bca @dbussink Improve deserialization of instruction sequences
dbussink authored
206 // Read off newline
207 stream.get();
208
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
209 InstructionSequence* iseq = InstructionSequence::create(state, count);
387bc2d @brixen Converted to attr_accessor for C++ member variable access.
brixen authored
210 Tuple* ops = iseq->opcodes();
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
211
212 for(size_t i = 0; i < count; i++) {
7fdf623 @ileitch Define the length of an opcode.
ileitch authored
213 stream.getline(data, OPCODE_LENGTH);
7ca3bca @dbussink Improve deserialization of instruction sequences
dbussink authored
214 op = strtol(data, NULL, 10);
e17197f @dbussink Refactor Object::i2n() and relatives into Integer::from() style
dbussink authored
215 ops->put(state, i, Fixnum::from(op));
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
216 }
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
217
218 iseq->post_marshal(state);
219
220 return iseq;
221 }
222
149a364 @brixen Cleaned up debris from CompiledMethod rename.
brixen authored
223 CompiledCode* UnMarshaller::get_compiled_code() {
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
224 size_t ver;
225 stream >> ver;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
226
149a364 @brixen Cleaned up debris from CompiledMethod rename.
brixen authored
227 CompiledCode* code = CompiledCode::create(state);
228
229 code->metadata(state, unmarshal());
230 code->primitive(state, (Symbol*)unmarshal());
231 code->name(state, (Symbol*)unmarshal());
232 code->iseq(state, (InstructionSequence*)unmarshal());
233 code->stack_size(state, (Fixnum*)unmarshal());
234 code->local_count(state, (Fixnum*)unmarshal());
235 code->required_args(state, (Fixnum*)unmarshal());
236 code->post_args(state, (Fixnum*)unmarshal());
237 code->total_args(state, (Fixnum*)unmarshal());
238 code->splat(state, unmarshal());
239 code->literals(state, (Tuple*)unmarshal());
240 code->lines(state, (Tuple*)unmarshal());
241 code->file(state, (Symbol*)unmarshal());
242 code->local_names(state, (Tuple*)unmarshal());
243
244 code->post_marshal(state);
245
246 return code;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
247 }
248
1fdd151 @rue Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
249 Object* UnMarshaller::unmarshal() {
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
250 char code;
251
252 stream >> code;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
253
254 switch(code) {
255 case 'n':
832cbb3 @brixen Removed use of Qtrue|false|nil|undef from native code.
brixen authored
256 return cNil;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
257 case 't':
832cbb3 @brixen Removed use of Qtrue|false|nil|undef from native code.
brixen authored
258 return cTrue;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
259 case 'f':
832cbb3 @brixen Removed use of Qtrue|false|nil|undef from native code.
brixen authored
260 return cFalse;
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
261 case 'I':
e59317e @brixen Revert varint changes to .rbc format. See below.
brixen authored
262 return get_int();
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
263 case 's':
264 return get_string();
265 case 'x':
266 return get_symbol();
267 case 'p':
268 return get_tuple();
269 case 'd':
270 return get_float();
271 case 'i':
272 return get_iseq();
273 case 'M':
149a364 @brixen Cleaned up debris from CompiledMethod rename.
brixen authored
274 return get_compiled_code();
3ceb20f @evanphx Fix 'for' in 1.9 mode
evanphx authored
275 case 'c':
276 return get_constant();
98a47de @brixen Marshal Encoding as an attribute of String for compile cache.
brixen authored
277 case 'E':
278 return get_encoding();
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
279 default:
f7c468d @dbussink Improve error message if an unknown code is found in a rbc file
dbussink authored
280 std::string str = "unknown marshal code: ";
281 str.append( 1, code );
9387877 @brixen Use Ruby exceptions in C++ marshal code.
brixen authored
282 Exception::type_error(state, str.c_str());
832cbb3 @brixen Removed use of Qtrue|false|nil|undef from native code.
brixen authored
283 return cNil; // make compiler happy
92c899e New marshal, using new, simpler format.
Evan Phoenix authored
284 }
285 }
286 }
Something went wrong with that request. Please try again.