-
Notifications
You must be signed in to change notification settings - Fork 5
/
bf2wasm.js
227 lines (200 loc) · 5.63 KB
/
bf2wasm.js
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
const bfToWasm = (function() {
'use strict';
const CELL_SIZE = 4;
const PAGE_SIZE = 65536;
const section = {
type: 0x01,
import: 0x02,
func: 0x03,
table: 0x04,
memory: 0x05,
global: 0x06,
export: 0x07,
start: 0x08,
elem: 0x09,
code: 0x0a,
};
const type = {
block: 0x40,
func: 0x60,
anyfunc: 0x70,
i32: 0x7f,
i64: 0x7e,
f32: 0x7d,
f64: 0x7c,
};
const wasmInstr = {
block: 0x02,
loop: 0x03,
end: 0x0b,
br: 0x0c,
br_if: 0x0d,
getLocal: 0x20,
setLocal: 0x21,
i32load: 0x28,
i32store: 0x36,
i32const: 0x41,
i32eqz: 0x45,
i32add: 0x6a,
i32sub: 0x6b,
};
// local 0 : brainfuck array index
// local 1 : output array index
const instrToWasm = {
'>': () => [
wasmInstr.getLocal, 0x00,
wasmInstr.i32const, CELL_SIZE,
wasmInstr.i32add,
wasmInstr.setLocal, 0x00,
],
'<': () => [
wasmInstr.getLocal, 0x00,
wasmInstr.i32const, CELL_SIZE,
wasmInstr.i32sub,
wasmInstr.setLocal, 0x00,
],
'+': () => [
wasmInstr.getLocal, 0x00,
wasmInstr.getLocal, 0x00,
wasmInstr.i32load, 0x02, 0x00,
wasmInstr.i32const, 0x01,
wasmInstr.i32add,
wasmInstr.i32store, 0x02, 0x00,
],
'-': () => [
wasmInstr.getLocal, 0x00,
wasmInstr.getLocal, 0x00,
wasmInstr.i32load, 0x02, 0x00,
wasmInstr.i32const, 0x01,
wasmInstr.i32sub,
wasmInstr.i32store, 0x02, 0x00,
],
',': () => [
],
'.': () => [
wasmInstr.getLocal, 0x01,
wasmInstr.getLocal, 0x00,
wasmInstr.i32load, 0x02, 0x00,
wasmInstr.i32store, 0x02, 0x00,
wasmInstr.getLocal, 0x01,
wasmInstr.i32const, CELL_SIZE,
wasmInstr.i32add,
wasmInstr.setLocal, 0x01,
],
'[': () => [
wasmInstr.block,
type.block,
wasmInstr.loop,
type.block,
],
']': () => [
wasmInstr.getLocal, 0x00,
wasmInstr.i32load, 0x02, 0x00,
wasmInstr.i32eqz,
wasmInstr.br_if, 0x01,
wasmInstr.br, 0x00,
wasmInstr.end,
wasmInstr.end,
],
};
function BfInstr(instrSymbol, toWasm, extraParams) {
this.instrSymbol = instrSymbol;
this.toWasm = toWasm;
this.extraParams = extraParams || [];
}
function stripComments(bfStr) {
const notInstrExp = /[^<>+\-,.[\]]/g;
return bfStr.replace(notInstrExp, '');
}
function parseBf(bfStr) {
const instrs = [];
for (let i = 0; i < bfStr.length; i += 1) {
const toWasm = instrToWasm[bfStr[i]];
instrs.push(new BfInstr(bfStr[i], toWasm));
}
return instrs;
}
function optimizeInstrs(instrs) {
return instrs;
}
function intToVarint(n) {
const buffer = [];
let more = true;
while (more) {
let byte = n & 0x7F;
n >>>= 7;
if ((n === 0 && (byte & 0x40) === 0) || (n === -1 && (byte & 0x40) !== 0)) {
more = false;
} else {
byte |= 0x80;
}
buffer.push(byte);
}
return buffer;
}
function intToVaruint(n) {
const buffer = [];
do {
let byte = n & 0x7F;
n >>>= 7;
if (n !== 0) {
byte |= 0x80;
}
buffer.push(byte);
} while (n !== 0);
return buffer;
}
function createSection(sectionType, ...data) {
const flatData = [].concat(...data);
return [section[sectionType], ...intToVaruint(flatData.length), ...flatData];
}
function getStrBytes(str) {
const bytes = [];
for (let i = 0; i < str.length; i += 1) {
bytes.push(str.charCodeAt(i));
}
return bytes;
}
function compileToWasm(instrs) {
const magicNumber = [0x00, 0x61, 0x73, 0x6d];
const version = [0x01, 0x00, 0x00, 0x00];
const functionsCount = 0x01;
const startFuncIndex = 0x00;
const startFunctionSignature = [type.func, 0x00, 0x00];
const typeSection = createSection('type', functionsCount, startFunctionSignature);
const importsCount = 0x01;
const importModuleName = getStrBytes('imports');
const importExportName = getStrBytes('memory');
const importMemoryKind = 0x02;
const memoryDesc = [0x00, 0x02];
const importSection = createSection('import', importsCount, importModuleName.length, importModuleName, importExportName.length, importExportName, importMemoryKind, memoryDesc);
const funcSection = createSection('func', functionsCount, startFuncIndex);
const startSection = createSection('start', startFuncIndex);
const localEntriesCount = 0x01;
const i32VarCount = 0x02;
const initOutputIndex = [wasmInstr.i32const, ...intToVarint(PAGE_SIZE), wasmInstr.setLocal, 0x00];
const functionBody = instrs.reduce((res, instr) => res.concat(instr.toWasm(...instr.extraParams)), initOutputIndex);
functionBody.push(wasmInstr.end);
const functionLength = intToVaruint(functionBody.length + 3);
const codeSection = createSection('code', functionsCount, ...functionLength, localEntriesCount, i32VarCount, type.i32, functionBody);
const buffer = [...magicNumber, ...version, ...typeSection, ...importSection, ...funcSection, ...startSection, ...codeSection];
return Uint8Array.from(buffer);
}
return function (bfStr, optimize = false) {
if (bfStr == null) {
throw new Error('Argument 0 cannot be null');
}
if (typeof (bfStr) !== 'string') {
throw new Error('Expected string for argument 0');
}
if (typeof (optimize) !== 'boolean') {
throw new Error('Expected boolean for argument 1');
}
const bfNoComment = stripComments(bfStr);
let instrs = parseBf(bfNoComment);
if (optimize) {
instrs = optimizeInstrs(instrs);
}
return compileToWasm(instrs);
};
})();