OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 library fletch.bytecodes; | |
6 | |
7 import 'dart:typed_data' show | |
8 ByteData, | |
9 Endianness, | |
10 Uint8List; | |
11 | |
12 part 'generated_bytecodes.dart'; | |
13 | |
14 const int VAR_DIFF = 0x3FFFFFFF; | |
15 | |
16 abstract class Bytecode { | |
17 static bool identicalBytecodes(List<Bytecode> expected, | |
18 List<Bytecode> actual) { | |
19 if (expected.length != actual.length) return false; | |
20 for (int i = 0; i < expected.length; i++) { | |
21 if (expected[i] != actual[i]) return false; | |
22 } | |
23 return true; | |
24 } | |
25 | |
26 const Bytecode(); | |
27 | |
28 Opcode get opcode; | |
29 | |
30 String get name; | |
31 | |
32 String get format; | |
33 | |
34 int get size; | |
35 | |
36 /// The effect on stack size. | |
37 int get stackPointerDifference; | |
38 | |
39 String get formatString; | |
40 | |
41 void addTo(Sink<List<int>> sink); | |
42 | |
43 bool operator==(Bytecode other) => other.opcode == opcode; | |
44 | |
45 int get hashCode => opcode.index; | |
46 | |
47 static void prettyPrint(StringBuffer sb, List<Bytecode> bytecodes) { | |
48 int offset = 0; | |
49 for (Bytecode bytecode in bytecodes) { | |
50 offset += bytecode.size; | |
51 } | |
52 int padding = "$offset".length; | |
53 offset = 0; | |
54 for (Bytecode bytecode in bytecodes) { | |
55 String paddedOffset = ("0" * padding) + "$offset"; | |
56 paddedOffset = paddedOffset.substring(paddedOffset.length - padding); | |
57 sb.writeln(" $paddedOffset: $bytecode"); | |
58 offset += bytecode.size; | |
59 } | |
60 } | |
61 } | |
62 | |
63 class BytecodeBuffer { | |
64 int position = 0; | |
65 | |
66 Uint8List list = new Uint8List(8); | |
67 | |
68 ByteData get view => new ByteData.view(list.buffer); | |
69 | |
70 void growBytes(int size) { | |
71 while (position + size >= list.length) { | |
72 list = new Uint8List(list.length * 2) | |
73 ..setRange(0, list.length, list); | |
74 } | |
75 } | |
76 | |
77 void addUint8(int value) { | |
78 growBytes(1); | |
79 view.setUint8(position++, value); | |
80 } | |
81 | |
82 void addUint32(int value) { | |
83 growBytes(4); | |
84 view.setUint32(position, value, Endianness.LITTLE_ENDIAN); | |
85 position += 4; | |
86 } | |
87 | |
88 void addUint64(int value) { | |
89 growBytes(8); | |
90 view.setUint64(position, value, Endianness.LITTLE_ENDIAN); | |
91 position += 8; | |
92 } | |
93 | |
94 void sendOn(Sink<List<int>> sink) { | |
95 sink.add(new Uint8List.view(list.buffer, list.offsetInBytes, position)); | |
96 } | |
97 } | |
OLD | NEW |