-
Notifications
You must be signed in to change notification settings - Fork 37
/
ObjectInfo.hx
584 lines (537 loc) · 17.9 KB
/
ObjectInfo.hx
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import Flow;
import haxe.io.Input;
import haxe.io.Output;
import FlowArray;
#if sys
import sys.io.File;
import sys.FileSystem;
#end
typedef Import = {name: String, filename: String, fullFilename: String, objectPath: String, objectHash: String};
typedef Include = {name: String, hash: String};
typedef Decl = {name: String, hidden: Bool, no:Int, type : TypeScheme };
typedef InfoWriter = {out: Output, types: TypeTable, strings : Map<String, Int>, noStrings : Int};
typedef InfoReader = {inp: Input, types: Array<FlowType>, strings : Map<Int, String>, noStrings : Int};
typedef NameRef = {pc: Int, name: String};
typedef TypeDef = {t: FlowType, i: Int};
class TypeTable {
private static var HASH_SIZE = Type.getEnumConstructs(FlowType).length;
var types : Array<Array<TypeDef>>;
var index : Int;
public function new() {
types = new Array();
for (i in 0 ... HASH_SIZE) {
types.push(new Array());
}
index = 0;
}
public function put(t: FlowType) {
types [hash(t)].push({i:index,t:t});
Assert.check(get(t) == index);
++ index;
}
public function get(t: FlowType): Int {
var ts = types [hash(t)];
for (tp in ts)
if (t == tp.t) {
return tp.i;
}
return -1;
}
private static function hash(t: FlowType) {
return Type.enumIndex(t);
}
}
class ObjectInfo {
private static var cnt = 0;
public static var MAGIC = 0x10402030;
public static var VERSION = 76;
private static var infoCache = new Map();
public var moduleName : String;
public var filename : String;
public var sourceHash : String;
public var imports : Array<Import>;
public var exports : Array<String>;
public var includedStrings : Array<Include>;
public var expTypes : Array<TypeDeclaration>;
public var order : Array<Decl>;
public var bytecode (default, null) : haxe.io.Bytes;
public var bytecodeHash : String;
public var structDefs : Array<Int>;
public var structRefs : Array<NameRef>;
public var globalRefs : Array<NameRef>;
public var debugInfo : DebugInfo;
public function setBytecode(b: haxe.io.Bytes) {
this.bytecode = b;
this.bytecodeHash = if (b == null) "" else Md5.encode(b.toString());
return b;
}
public function new(moduleName: String, filename: String, hash : String) {
this.moduleName = moduleName;
this.filename = filename;
this.sourceHash = hash;
this.imports = new Array();
this.exports = new Array();
this.order = new Array();
this.structDefs = new Array();
this.structRefs = new Array();
this.globalRefs = new Array();
this.bytecode = null;
this.bytecodeHash = "";
this.debugInfo = null;
}
public function dump(): String {
var s =
"ModuleName: " + moduleName + "\n" +
" FileName: " + filename + "\n" +
"SourceHash: " + sourceHash + "\n";
s += "\n";
for (i in imports) {
s +=
" Imported " + i.name + /*" fileName: " + i.filename + " fullFilename " + i.fullFilename +
" objectPath: " + i.objectPath +*/ " objectHash: " + i.objectHash + "\n";
}
s += "\nExported:\n";
for (e in exports) {
s += " " + e + "\n";
}
s += "\nIncluded Strings: \n";
for (is in includedStrings) {
s += " " + is.name + " Map: " + is.hash + "\n";
}
s += "\nExported types:\n";
for (td in expTypes) {
s += " " + td.name + " = " + Prettyprint.prettyprintTypeScheme(td.type) + "\n";
}
s += "\nOrder decls:\n";
for (d in order) {
s += " " + d.name + " #" + d.no + " hidded: " + d.hidden + "\n";
}
s += "\nName defs:\n";
for (n in structDefs) {
s += n;
}
s += "\nName refs:\n";
for (n in structRefs) {
s += " " + n.name + " at " + n.pc + "\n";
}
s += "\nName refs:\n";
for (n in globalRefs) {
s += " " + n.name + " at " + n.pc + "\n";
}
s += "\nBytecode length: " + bytecode.length + " hash: " + bytecodeHash + "\n";
return s;
}
//typedef Import = {name: String, filename: String, fullFilename: String, objectPath: String, time: Date};
//typedef Decl = {name: String, hidden: Bool, no:Int};
public function serialize(module: Module, out: Output) {
Profiler.get().profileStart("Serializing");
var writer = {out: out, types: new TypeTable(), strings : new Map(), noStrings: 0 };
writer.out.writeInt32(MAGIC);
writer.out.writeInt32(VERSION);
writeString(writer, moduleName);
writeString(writer, filename);
writeString(writer, sourceHash);
Assert.check(imports != null, "imports != null");
writeArray (writer, function(writer: InfoWriter, i: Import) {
writeString(writer, i.name);
writeString(writer, i.filename);
writeString(writer, i.fullFilename);
writeString(writer, i.objectPath);
writeString(writer, i.objectHash);
}, imports);
writeArray(writer, writeString, exports);
writeArray (writer, function(writer: InfoWriter, i: Include) {
writeString(writer, i.name);
writeString(writer, i.hash);
}, includedStrings);
writeArray(writer, writeTypeDecl, expTypes);
writeOrder(writer);
writeArray(writer, writeUInt, structDefs);
writeArray(writer, writeNameRef, structRefs);
writeArray(writer, writeNameRef, globalRefs);
writeBytes(writer, bytecode);
writeString(writer, bytecodeHash);
debugInfo.write(writer);
Profiler.get().profileEnd("Serializing");
infoCache.set(module.relativeFilename, this);
}
#if sys
public static function getUnserializedInfo(module: Module, noHashCheck : Bool = false) {
var fname = module.getObjectFileName();
var info = infoCache.get(module.relativeFilename);
if (info == null && FileSystem.exists(fname)) {
Profiler.get().profileStart("Reading incremental object files");
var fileContents = File.getBytes(fname);
info = ObjectInfo.unserialize(module, new haxe.io.BytesInput(fileContents), noHashCheck);
Profiler.get().profileStart("Reading incremental object files");
infoCache.set(module.relativeFilename, info);
}
return info;
}
#end
public static function unserialize(module: Module, inp: Input, noHashCheck : Bool = false) : ObjectInfo {
//Assert.trace("loaded from store: " + module.name + " endian=" + inp.bigEndian);
cnt = 0;
var magic = inp.readInt32();
if (magic != MAGIC) {
//Assert.trace(module.name + ": invalid MAGIC");
return null;
}
var version = inp.readInt32();
if (version != VERSION) {
//Assert.trace(module.name + ": invalid version: " + version);
return null;
}
Profiler.get().profileStart("Deserializing");
var reader = {inp:inp, types:new Array(), strings : new Map(), noStrings : 0 };
var moduleName = readString(reader);
var filename = readString(reader);
var oldHash = readString(reader);
if (!noHashCheck) {
var newHash = module.getSourceHash();
if (oldHash != newHash) {
Assert.trace(module.name + ": file hashes mismatch: " + oldHash + " /= " + newHash);
return null;
}
}
var i = new ObjectInfo(moduleName, filename, oldHash);
i.imports = readArray(reader, function(reader: InfoReader) {
var name = readString(reader);
var filename = readString(reader);
var fullFilename = readString(reader);
var objectPath = readString(reader);
var objectHash = readString(reader);
return {name:name, filename:filename, fullFilename:fullFilename, objectPath:objectPath, objectHash:objectHash};
});
i.exports = readArray(reader, readString);
i.includedStrings = readArray(reader, function(reader: InfoReader) {
var name = readString(reader);
var hash = readString(reader);
return {name:name, hash:hash};
});
i.expTypes = readArray(reader, readTypeDecl);
i.readOrder(reader);
i.structDefs = readArray(reader, readUInt);
i.structRefs = readArray(reader, readNameRef);
i.globalRefs = readArray(reader, readNameRef);
i.bytecode = readBytes(reader);
i.bytecodeHash = readString(reader);
i.debugInfo = DebugInfo.read(reader);
Profiler.get().profileEnd("Deserializing");
return i;
}
private static function writeBytes(writer: InfoWriter, b: haxe.io.Bytes) {
writeUInt(writer, b.length);
writer.out.write(b);
}
private static function readBytes(reader: InfoReader): haxe.io.Bytes {
var len = readUInt(reader);
return reader.inp.read(len);
}
private static function writeNameRef(writer: InfoWriter, ref: NameRef) {
writeUInt(writer, ref.pc);
writeString(writer, ref.name);
}
private static function readNameRef(reader: InfoReader): NameRef {
var pc = readUInt(reader);
return {pc:pc, name:readString(reader)};
}
private static function writeTypeDecl(writer: InfoWriter, td: TypeDeclaration) {
writeString(writer, td.name);
writeTypeScheme(writer, td.type);
writePos(writer, td.position);
}
private static inline function readTypeDecl(reader: InfoReader) {
var name = readString(reader);
var type = readTypeScheme(reader);
var position = readPos(reader);
return {name:name, type:type, position:position};
}
public static inline function readString(reader: InfoReader) {
var len = readSInt(reader);
if (len < 0) {
return reader.strings.get(len);
} else {
var s = reader.inp.readString(len);
reader.noStrings = reader.noStrings - 1;
reader.strings.set(reader.noStrings, s);
return s;
}
}
public static inline function writeString(writer: InfoWriter, string: String) {
var l = writer.strings.get(string);
if (l == null) {
writeSInt(writer, string.length);
writer.out.writeString(string);
writer.noStrings = writer.noStrings - 1;
writer.strings.set(string, writer.noStrings);
} else {
writeSInt(writer, l);
}
}
public static inline function readBool(reader: InfoReader) { return reader.inp.readInt8() != 0; }
public static inline function writeBool(writer: InfoWriter, b: Bool) {
writer.out.writeInt8(if (b) 1 else 0);
}
public static function writeUInt(writer: InfoWriter, v: Int) {
if (v < 0)
Assert.check(false, "writeUInt: v<0: " + v);
if (v < 192)
writer.out.writeByte(v);
else {
writer.out.writeByte((v & 0x3F) + 192);
v >>>= 6;
while (v >= 0x80) {
writer.out.writeByte(v & 0x7F);
v >>>= 7;
}
writer.out.writeByte(v | 0x80);
}
}
public static function readUInt(reader: InfoReader) {
var b = reader.inp.readByte();
if (b < 192) {
return b;
} else {
var v = b - 192;
var shift = 6;
do {
b = reader.inp.readByte();
v |= ((b & 0x7F) << shift);
shift += 7;
} while((b & 0x80) == 0);
return v;
}
}
public static function readSInt(reader: InfoReader) {
var i = readUInt(reader);
if ((i & 1) == 0)
return i >> 1;
else
return -(i >> 1);
}
public static function writeSInt(writer: InfoWriter, v: Int) {
//Assert.check(v <= 0x20000000, "v=" + v);
//Assert.check(v > -0x20000000, "v=" + v);
if (v >= 0)
writeUInt(writer, v << 1);
else
writeUInt(writer, ((-v) << 1) | 1);
}
public static function readPos(reader: InfoReader): Position {
var f = readString(reader);
var l : Null<Int> = readUInt (reader);
var s = readSInt (reader);
var e = readSInt (reader);
var t = readType (reader);
var t2 = readType (reader);
return {f:f, l:l, s:s, e:e, type:t, type2:t2};
}
public static function writePos(writer: InfoWriter, pos: Position, ?types : Bool = true) {
writeString(writer, pos.f);
writeUInt (writer, pos.l);
writeSInt (writer, pos.s);
writeSInt (writer, pos.e);
if (types) {
writeType (writer, pos.type);
writeType (writer, pos.type2);
} else {
writeType (writer, null);
writeType (writer, null);
}
}
public static function readArray<T>(reader: InfoReader, readT: InfoReader -> T): Array<T> {
var len = readUInt(reader);
var res = new Array();
for (i in 0 ... len)
res.push(readT(reader));
return res;
}
public static function writeArray<T>(writer: InfoWriter, tWrite: InfoWriter -> T -> Void, v: Array<T>) {
writeUInt(writer, v.length);
for (f in v) {
tWrite(writer, f);
}
}
private function writeOrder(writer: InfoWriter) {
writeUInt(writer, order.length);
for (d in order) {
writeString(writer, d.name );
writeBool (writer, d.hidden );
writeUInt (writer, d.no );
writeTypeScheme(writer, d.type); }
}
private function readOrder(reader: InfoReader) {
var len = readUInt(reader);
for (i in 0 ... len) {
var name = readString(reader);
var hidden = readBool (reader);
var no = readUInt (reader);
var type = readTypeScheme(reader);
order.push({name:name, hidden:hidden, no:no, type:type});
}
}
public static function writeType(writer: InfoWriter, t: FlowType) {
if (t == null) {
writeSInt(writer,-1);
return;
}
var ti = writer.types.get(t);
if (ti != -1) {
writeSInt(writer,ti);
return;
}
writeSInt(writer,-2);
switch (t) {
case TVoid : writer.out.writeByte(TypeTag.TVoid );
case TBool : writer.out.writeByte(TypeTag.TBool );
case TInt : writer.out.writeByte(TypeTag.TInt );
case TDouble: writer.out.writeByte(TypeTag.TDouble);
case TString: writer.out.writeByte(TypeTag.TString);
case TFlow : writer.out.writeByte(TypeTag.TFlow );
case TNative: writer.out.writeByte(TypeTag.TNative);
case TReference(t): writer.out.writeByte(TypeTag.TReference); writeType(writer,t);
case TPointer (t): writer.out.writeByte(TypeTag.TPointer ); writeType(writer,t);
case TArray (t): writer.out.writeByte(TypeTag.TArray ); writeType(writer,t);
case TTyvar (r): writer.out.writeByte(TypeTag.TTyvar ); writeType(writer,r.type);
case TFunction(args, returns):
writer.out.writeByte(TypeTag.TFunction);
writeArray(writer, writeType, FlowArrayUtil.toArray(args));
writeType(writer,returns);
case TStruct(structname, args, max):
writer.out.writeByte(TypeTag.TStruct);
writeString(writer, structname);
writeArray(writer, function(writer: InfoWriter, m: MonoTypeDeclaration) {
writeString(writer, m.name);
writeType(writer, m.type);
writePos(writer, m.position);
writeBool(writer, m.is_mutable);
}, FlowArrayUtil.toArray(args));
writeBool(writer, max);
case TUnion(min, max):
//Assert.check(min == max, "min == max");
writer.out.writeByte(TypeTag.TUnion);
var write = function (h: Map<String,FlowType>) {
if (h == null) {
writeBool(writer, false);
return;
}
writeBool(writer, true);
for (id in h.keys()) {
writeBool(writer, true);
writeString(writer, id);
writeType(writer, h.get(id));
}
writeBool(writer, false);
};
write(min);
// this is no opt - physical equality of min & max is essential and should be preserved
if (min != max) {
writeBool(writer, false);
write(max);
} else {
writeBool(writer, true);
}
case TBoundTyvar(id): writer.out.writeByte(TypeTag.TBoundTyvar); writeUInt(writer,id);
case TName(name, args):
writer.out.writeByte(TypeTag.TName);
writeString(writer, name);
writeArray(writer, writeType, FlowArrayUtil.toArray(args));
}
writer.types.put(t);
}
public static function readType(reader: InfoReader): FlowType {
var kind = readSInt(reader);
if (kind == -1) {
return null;
} else if (kind >= 0) {
//Assert.check(reader.types.length - kind > 0, "" + kind + " < " + reader.types.length);
return reader.types[kind];
} else {
Assert.check(kind == -2, "kind == -2 at offset ");
}
var tag = reader.inp.readByte();
var t = switch (tag) {
case TypeTag.TVoid : TVoid;
case TypeTag.TBool : TBool;
case TypeTag.TInt : TInt ;
case TypeTag.TDouble: TDouble;
case TypeTag.TString: TString;
case TypeTag.TFlow : TFlow;
case TypeTag.TNative: TNative;
case TypeTag.TReference: TReference(readType(reader));
case TypeTag.TPointer : TPointer (readType(reader));
case TypeTag.TArray : TArray (readType(reader));
case TypeTag.TTyvar : TTyvar(FlowUtil.mkTyvar(readType(reader)));
case TypeTag.TFunction : {
var args = FlowArrayUtil.fromArray(readArray(reader, readType));
TFunction(args, readType(reader));
}
case TypeTag.TStruct: {
var name = readString(reader);
var args = FlowArrayUtil.fromArray(readArray(reader, function(reader: InfoReader) {
var name = readString(reader);
var type = readType(reader);
var pos = readPos(reader);
var mut = readBool(reader);
return {name:name, type:type, position:pos, is_mutable:mut};
}));
TStruct(name, args, readBool(reader));
}
case TypeTag.TUnion: {
var read = function() {
if (!readBool(reader)) {
return null;
}
var b = new Map();
while (readBool(reader)) {
var id = readString(reader);
b.set(id, readType(reader));
};
return b;
};
var min = read();
var max = if (readBool(reader)) min else read();
TUnion(min,max);
}
case TypeTag.TBoundTyvar: TBoundTyvar(readUInt(reader));
case TypeTag.TName: {
var name = readString(reader);
TName(name, FlowArrayUtil.fromArray(readArray(reader, readType)));
}
default: Assert.fail("Invalid tag=" + tag); null;
};
reader.types.push(t);
Assert.check(t != null);
return t;
}
public static function writeTypeScheme(writer: InfoWriter, t: TypeScheme) {
writeUInt(writer, t.tyvars.length);
for (f in t.tyvars) {
writeUInt(writer, f);
}
writeType(writer, t.type);
}
public static function readTypeScheme(reader: InfoReader): TypeScheme {
var tyvars = FlowArrayUtil.fromArray(readArray(reader, readUInt));
return {tyvars:tyvars, type:readType(reader)};
}
}
class TypeTag {
public static inline var TVoid = 1;
public static inline var TBool = 2;
public static inline var TInt = 3;
public static inline var TDouble = 4;
public static inline var TString = 5;
public static inline var TReference = 6;
public static inline var TPointer = 7;
public static inline var TArray = 8;
public static inline var TFunction = 9;
public static inline var TStruct = 10;
public static inline var TUnion = 11;
public static inline var TTyvar = 12;
public static inline var TBoundTyvar = 13;
public static inline var TFlow = 14;
public static inline var TNative = 15;
public static inline var TName = 16;
}