-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.ts
More file actions
191 lines (164 loc) · 7.1 KB
/
Copy pathoptimizer.ts
File metadata and controls
191 lines (164 loc) · 7.1 KB
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
import { Opcode } from '../types.js';
import { CompilerLogLevel } from './types.js';
/**
* TODO: рефактор оптимизатора, сейчас тут откровенно говно-код, плюсом я его не комментировал что усложняет поддержку
*/
export function optimizePeephole(
bytecode: number[],
opcodeMap: Uint8Array,
usedOpcodes: Set<number>,
log: (level: CompilerLogLevel, msg: string) => void
): number[] {
log(CompilerLogLevel.INFO, `Starting Peephole optimization (Initial size: ${bytecode.length} bytes)`);
const argLengths: Record<number, number> = {
[Opcode.STORE_VAR]: 3,
[Opcode.LOAD_VAR]: 3,
[Opcode.CONSOLE_LOG]: 1,
[Opcode.CONSOLE_WARN]: 1,
[Opcode.CONSOLE_ERROR]: 1,
[Opcode.CALL]: 1,
[Opcode.NEW]: 1,
[Opcode.INVOKE]: 3,
[Opcode.INVOKE_SPREAD]: 2,
[Opcode.INVOKE_SUPER]: 3,
[Opcode.SUPER_CONSTRUCTOR]: 1,
[Opcode.PUSH_CONST]: 2,
[Opcode.STORE_LOCAL]: 2,
[Opcode.LOAD_LOCAL]: 2,
[Opcode.JUMP]: 2,
[Opcode.JUMP_IF_FALSE]: 2,
[Opcode.JUMP_IF_TRUE]: 2,
[Opcode.JUMP_IF_NOT_NULLISH]: 2,
[Opcode.JUMP_IF_NULLISH_SHORT_CIRCUIT]: 2,
[Opcode.SETUP_CATCH]: 2,
[Opcode.MAKE_CLOSURE]: 2,
[Opcode.MAKE_ARRAY]: 2,
[Opcode.MAKE_OBJECT]: 2,
[Opcode.REST_OBJECT]: 2,
[Opcode.LOAD_GLOBAL]: 2,
[Opcode.TYPEOF_GLOBAL]: 2,
[Opcode.STORE_GLOBAL]: 2,
[Opcode.DELETE_GLOBAL]: 2,
[Opcode.PUSH_INT8]: 1,
[Opcode.GET_NAMED_PROPERTY]: 4,
[Opcode.SET_NAMED_PROPERTY]: 4,
};
interface Inst {
opcode: number;
args: number[];
originalOffset: number;
targetOffset?: number;
isDead?: boolean;
newOffset?: number;
}
const insts: Inst[] = [];
let ip = 0;
while (ip < bytecode.length) {
const originalOffset = ip;
const opcode = bytecode[ip++];
const argLen = argLengths[opcode] || 0;
const args = [];
for (let i = 0; i < argLen; i++) args.push(bytecode[ip++]);
let targetOffset = undefined;
if ([Opcode.JUMP, Opcode.JUMP_IF_FALSE, Opcode.JUMP_IF_TRUE, Opcode.JUMP_IF_NOT_NULLISH, Opcode.JUMP_IF_NULLISH_SHORT_CIRCUIT, Opcode.SETUP_CATCH].includes(opcode)) {
const rel = (args[0] << 8) | args[1];
const signedRel = rel >= 0x8000 ? rel - 0x10000 : rel;
targetOffset = ip + signedRel;
}
insts.push({ opcode, args, originalOffset, targetOffset });
}
const jumpTargets = new Set<number>();
for (const inst of insts) {
if (inst.targetOffset !== undefined) jumpTargets.add(inst.targetOffset);
}
let changed = true;
while (changed) {
changed = false;
for (let i = 0; i < insts.length - 1; i++) {
if (insts[i].isDead) continue;
let next1 = -1;
for (let j = i + 1; j < insts.length; j++) if (!insts[j].isDead) { next1 = j; break; }
if (next1 === -1) continue;
let next2 = -1;
for (let j = next1 + 1; j < insts.length; j++) if (!insts[j].isDead) { next2 = j; break; }
const i1 = insts[i];
const i2 = insts[next1];
const i3 = next2 !== -1 ? insts[next2] : null;
if (i3 && (i1.opcode === Opcode.STORE_LOCAL || i1.opcode === Opcode.STORE_VAR) &&
i2.opcode === Opcode.POP &&
i3.opcode === (i1.opcode === Opcode.STORE_LOCAL ? Opcode.LOAD_LOCAL : Opcode.LOAD_VAR)) {
if (!jumpTargets.has(i2.originalOffset) && !jumpTargets.has(i3.originalOffset)) {
if (i1.args.join() === i3.args.join()) {
i2.isDead = true;
i3.isDead = true;
changed = true;
continue;
}
}
}
const safeToPop = [Opcode.PUSH_CONST, Opcode.PUSH_INT8, Opcode.PUSH_TRUE, Opcode.PUSH_FALSE, Opcode.PUSH_NULL, Opcode.PUSH_UNDEFINED, Opcode.LOAD_LOCAL, Opcode.LOAD_VAR];
if (safeToPop.includes(i1.opcode) && i2.opcode === Opcode.POP) {
if (!jumpTargets.has(i2.originalOffset)) {
i1.isDead = true;
i2.isDead = true;
changed = true;
continue;
}
}
}
}
let currentNewOffset = 0;
const offsetMap = new Map<number, number>();
/**
* 17.03
* фейковые блоки против статистического анализа, по идее 10% должно хватить.
*/
const dummyBlocks = new Map<number, { type: number, args: number[] }>();
const fakeOpcodes = [
Opcode.JUMP, Opcode.JUMP_IF_FALSE, Opcode.JUMP_IF_TRUE,
Opcode.SETUP_CATCH, Opcode.PUSH_CONST, Opcode.LOAD_GLOBAL
];
for (let i = 0; i < insts.length; i++) {
const inst = insts[i];
if (!inst.isDead && !jumpTargets.has(inst.originalOffset) && Math.random() < 0.10) { // 0.10 = 10%
const fakeOpcode = fakeOpcodes[Math.floor(Math.random() * fakeOpcodes.length)];
const fakeArgs = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
dummyBlocks.set(i, { type: fakeOpcode, args: fakeArgs });
currentNewOffset += 6;
}
const length = 1 + inst.args.length;
for (let b = 0; b < length; b++) offsetMap.set(inst.originalOffset + b, currentNewOffset);
if (!inst.isDead) {
inst.newOffset = currentNewOffset;
currentNewOffset += length;
}
}
offsetMap.set(bytecode.length, currentNewOffset);
const optimizedBytecode: number[] = [];
for (let i = 0; i < insts.length; i++) {
const inst = insts[i];
if (inst.isDead) continue;
if (dummyBlocks.has(i)) {
const dummy = dummyBlocks.get(i)!;
usedOpcodes.add(Opcode.JUMP);
optimizedBytecode.push(opcodeMap[Opcode.JUMP]);
optimizedBytecode.push(0, 3);
usedOpcodes.add(dummy.type);
optimizedBytecode.push(opcodeMap[dummy.type]);
optimizedBytecode.push(...dummy.args);
}
usedOpcodes.add(inst.opcode);
optimizedBytecode.push(opcodeMap[inst.opcode]);
if (inst.targetOffset !== undefined) {
const newTarget = offsetMap.get(inst.targetOffset);
if (newTarget === undefined) throw new Error("Peephole Optimizer: Invalid JUMP target");
let newRel = newTarget - (inst.newOffset! + 1 + inst.args.length);
if (newRel < 0) newRel += 0x10000; // обратный переход
optimizedBytecode.push((newRel >> 8) & 0xFF, newRel & 0xFF);
} else {
optimizedBytecode.push(...inst.args);
}
}
log(CompilerLogLevel.INFO, `Peephole optimization finished (Final size: ${optimizedBytecode.length} bytes)`);
return optimizedBytecode;
}