-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatorize_asmjs.js
executable file
·306 lines (239 loc) · 9.34 KB
/
flatorize_asmjs.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
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
/*
ECMAScript implementation of "flatorize": Generate fast, flat,
factorized ** ASM.JS code ** for mathematical expressions.
Requires: ./flatorize.js and ./flatorize_type_util.js
Copyright 2014, 2015 Guillaume Lathoud
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A copy of the Apache License Version 2.0 as of February 20th, 2013
can be found in the file ./LICENSE.TXT
*/
// -*- coding: utf-8 -*-
/*global flatorize load console*/
if ('undefined' === typeof flatorize && 'function' === typeof load)
load( 'flatorize.js' ); // e.g. V8
if ('undefined' === typeof flatorize.type_util && 'function' === typeof load)
load( 'flatorize_type_util.js' ); // e.g. V8
(function (global) {
var FTU = flatorize.type_util;
// ---------- Public API
flatorize.getAsmjsGen = flatorize_getAsmjsGen;
flatorize.getAsmjsImplCode = flatorize_getAsmjsImplCode;
// ---------- Public API implementation
function flatorize_getAsmjsGen( /*object*/cfg )
// Returns a generator function that can be called to compile
// asm.js code.
//
// Two usages:
//
// (1) In one shot, if `exprgen_fun` has no dependency, or all of them have been flatorized already
//
// {{{
// var asmjsGen = flatorize.getAsmjs( { name: "functionname", varstr: "a:float,b:[16 int]->c:float", exprgen: exprgen_fun } );
// var buffer = new ArrayBuffer( 1 << 24 );
// var f = asmjsGen( window, {}, buffer ); // Compile the asm.js code
// // ...
// f(); // call the asm.js code
// // ...
// }}}
//
// (2) In two steps (useful if your expression has dependencies, esp. mutual dependencies):
//
// {{{
// var switcherfun = flatorize( "a:float,b:[16 int]->c:float", exprgen_fun );
// // ... the remaining dependencies of `exprgen_fun` can be flatorized here ...
//
// // Now we have all flatorized all dependencies of `exprgen_fun`, so we can generate code
// var asmjsGen = flatorize.getAsmjs( { name: "functionname", switcher: switcherfun } );
//
// // ... usage of `asmjsGen`: as above.
// }}}
//
// glathoud@yahoo.fr
{
var topFunName = cfg.name; // Mandatory
(topFunName || null).substring.call.a; // Cheap assert: Must be a string
var js_switcher = cfg.switcher || flatorize( cfg.varstr, cfg.exprgen ); // Two variants
js_switcher.call.a; // Cheap assert: Must be a function
var js_direct = js_switcher.getDirect ? js_switcher.getDirect() : js_switcher
, fixed = FTU.create_fixed_info( js_direct )
;
fixed.topFunName = topFunName;
// Syntax definitions
fixed.castwrap = castwrap;
fixed.assign_statement_code = assign_statement_code;
fixed.declaration_statement_code = asmjs_typed_variable_declaration_statement_code;
fixed.line_comment_code = line_comment_code;
fixed.read_array_value_expression_code = read_array_value_expression_code;
fixed.return_statement_code = return_statement_code;
fixed.write_array_value_statement_code = write_array_value_statement_code;
fixed.indent = indent;
return generateAsmjsGen( fixed );
}
function flatorize_getAsmjsImplCode( /*object*/cfg )
// Convenience wrapper for:
// {{{
// flatorize.getAsmjsGen( cfg ).implCode;
// }}}
//
// Input: `cfg`: same as `flatorize.getAsmjsGen` (documented
// there).
//
// Output: a string containing the code for the asm.js
// implementation.
//
//
// glathoud@yahoo.fr
{
return flatorize_getAsmjsGen( cfg ).implCode;
}
// ---------- Private details ----------
function generateAsmjsGen( /*object*/fixed )
// Returns a `gen` function that can be called to compile the
// asm.js code, e.g.
//
// {{{
// var o = gen( window, {}, heap ); // compile the asm.js code
//
// // Use it:
// var result = o[ fixed.topFunName ]( some, input, parameters );
// }}}
//
// `o[ fixed.topFunName ]` may also modify the heap in-place,
// in which case you have to write inputs and read outputs
// through the heap.
//
// For more examples of use see ./asmjs.html
//
// glathoud@yahoo.fr
{
(fixed.topFunName || null).substring.call.a;
var fixed2 = Object.create( fixed ) // we will augment it a little bit with derived values, e.g. with `array_name2info`
, before = []
, body = []
, after = []
, wrap = []
;
if (fixed2.single_common_array_btd)
{
// Dealing with arrays
var cat = fixed2.single_common_array_btd.type
, cat_js = cat === 'int' ? 'Int32'
: cat === 'float' ? 'Float32'
: cat === 'double' ? 'Float64'
: (null).unsupported
, cat_bits = /\d+$/.exec( cat_js )|0
, cat_bytes = cat_bits >> 3
;
fixed2.sca_name = FTU.get_new_varname( fixed2, cat_js.toLowerCase() )
fixed2.sca_type = cat;
}
else
{
// Dealing with scalars only
}
FTU.extract_array_info_and_count( fixed2 );
var asmjs_buffer_bytes = Math.ceil( fixed2.count * cat_bytes / (1 << 24) ) << 24;
before = funDeclCodeAsmjs( fixed2 );
body = FTU.fun_body_imperative_code( fixed2 );
after = [ '}' ];
wrap = [ '', return_statement_code( '{ ' + fixed2.topFunName + ' : ' + fixed2.topFunName + ' }' ) ];
// ---------- asm.js wrapper generator ----------
var gen = new Function(
'stdlib', 'foreign', 'heap',
[
'/* code generated by flatorize.asmjsGen.js */'
, '"use asm";'
, ''
, fixed2.sca_name
? 'var ' + assign_statement_code( fixed2.sca_name, 'new stdlib.' + cat_js + 'Array( heap )' )
: ''
, ''
]
.concat( before
.concat( body )
.concat( after )
)
.concat( wrap )
.map( indent )
.join( '\n' )
);
gen.implCodeArr = before.concat( body ).concat( after );
gen.implCode = gen.implCodeArr.join( '\n' );
gen.count = fixed2.count;
gen.cat_bits = cat_bits;
gen.cat_bytes = cat_bytes;
gen.buffer_bytes = asmjs_buffer_bytes;
gen.array_type = cat_js && cat;
gen.array_name2info = cat_js && fixed2.array_name2info;
gen.TypedArray = cat_js && global[ cat_js + 'Array' ]; // Constructor function, e.g. Float64Array
return gen;
}
function funDeclCodeAsmjs( fixed )
// Returns an array of codeline strings.
{
return [
'function ' + fixed.topFunName + '( ' + fixed.simple_in_vararr.join( ', ' ) + ' )'
, '{'
].concat(
fixed.simple_in_vararr.map( function (name) {
var t = fixed.typed_in_var[ name ];
if (t === 'float' || t === 'double')
return name + '= +' + name + ';';
if (t === 'int')
return name + '= ' + name + '|0;';
(null).unsupported;
})
);
}
// Syntax definitions
function assign_statement_code( /*string*/name, /*string*/code )
{
return name + ' = ' + code + ';';
}
function asmjs_typed_variable_declaration_statement_code( /*string*/name, /*string*/type )
{
return 'var ' + assign_statement_code(
name
, type === 'float' || type === 'double' ? '0.0'
: type === 'int' ? '0'
: (null).unsupported
);
}
function line_comment_code( s )
{
return '// ' + s;
}
function read_array_value_expression_code( /*string*/array_name, /*integer*/ind )
{
(array_name || null).substring.call.a;
ind.toPrecision.call.a;
return array_name + '[' + ind + ']';
}
function castwrap( /*string*/type, /*string*/code)
{
return type === 'double' || type === 'float' ? '+(' + code + ')'
: type === 'int' ? '((' + code + ')|0)'
: null.unsupported
;
}
function return_statement_code( /*string*/code )
{
return 'return ' + code + ';';
}
function write_array_value_statement_code( /*string*/array_name, /*integer*/ind, /*string*/code )
{
return array_name + '[' + ind + '] = ' + code + ';';
}
function indent( s )
{
return ' ' + s;
}
})(this);