forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionNode_webgl.js
More file actions
557 lines (491 loc) · 15.3 KB
/
functionNode_webgl.js
File metadata and controls
557 lines (491 loc) · 15.3 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
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
// Closure capture for the ast function, prevent collision with existing AST functions
var functionNode_webgl = (function() {
///
/// Function: functionNode_webgl
///
/// [INTERNAL] Takes in a function node, and does all the AST voodoo required to generate its respective webGL code.
///
/// Parameter:
/// inNode - {functionNode} The function node object
///
/// Returns:
/// the converted webGL function string
///
function functionNode_webgl( inNode ) {
inNode.webglFunctionString_array = ast_generic( inNode.getJS_AST(), [], inNode );
inNode.webglFunctionString = inNode.webglFunctionString_array.join("").trim();
return inNode.webglFunctionString;
}
/// the AST error, with its location. To throw
///
/// @TODO: add location support fpr the AST error
///
/// @param error the error message output
/// @param ast the AST object where the error is
/// @param funcParam FunctionNode, that tracks compilation state
function ast_errorOutput(error, ast, funcParam) {
console.error(error, ast, funcParam);
return error;
}
/// Prases the abstract syntax tree, genericially to its respective function
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the prased openclgl string array
function ast_generic(ast, retArr, funcParam) {
if(ast === null) {
throw ast_errorOutput("NULL ast", ast, funcParam);
} else {
if (Array.isArray(ast)) {
for (var i=0; i<ast.length; i++) {
ast_generic(ast[i], retArr, funcParam);
}
return retArr;
}
switch(ast.type) {
case "FunctionExpression":
return ast_FunctionExpression(ast, retArr, funcParam);
case "ReturnStatement":
return ast_ReturnStatement(ast, retArr, funcParam);
case "Literal":
return ast_Literal(ast, retArr, funcParam);
case "BinaryExpression":
return ast_BinaryExpression(ast, retArr, funcParam);
case "Identifier":
return ast_IdentifierExpression(ast, retArr, funcParam);
case "AssignmentExpression":
return ast_AssignmentExpression(ast, retArr, funcParam);
case "ExpressionStatement":
return ast_ExpressionStatement(ast, retArr, funcParam);
case "EmptyStatement":
return ast_EmptyStatement(ast, retArr, funcParam);
case "BlockStatement":
return ast_BlockStatement(ast, retArr, funcParam);
case "IfStatement":
return ast_IfStatement(ast, retArr, funcParam);
case "BreakStatement":
return ast_BreakStatement(ast, retArr, funcParam);
case "ContinueStatement":
return ast_ContinueStatement(ast, retArr, funcParam);
case "ForStatement":
return ast_ForStatement(ast, retArr, funcParam);
case "VariableDeclaration":
return ast_VariableDeclaration(ast, retArr, funcParam);
case "VariableDeclarator":
return ast_VariableDeclarator(ast, retArr, funcParam);
case "ThisExpression":
return ast_ThisExpression(ast, retArr, funcParam);
case "SequenceExpression":
return ast_SequenceExpression(ast, retArr, funcParam);
case "UnaryExpression":
return ast_UnaryExpression(ast, retArr, funcParam);
case "UpdateExpression":
return ast_UpdateExpression(ast, retArr, funcParam);
case "LogicalExpression":
return ast_LogicalExpression(ast, retArr, funcParam);
case "MemberExpression":
return ast_MemberExpression(ast, retArr, funcParam);
case "CallExpression":
return ast_CallExpression(ast, retArr, funcParam);
}
throw ast_errorOutput("Unknown ast type : "+ast.type, ast, funcParam);
}
}
/// Prases the abstract syntax tree, to its named function
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_FunctionExpression(ast, retArr, funcParam) {
// Setup function return type and name
if(funcParam.isRootKernel) {
retArr.push("vec4");
} else {
retArr.push(funcParam.returnType);
}
retArr.push(" ");
retArr.push(funcParam.functionName);
retArr.push("(");
// Arguments handling
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
if( i > 0 ) {
retArr.push(", ");
}
retArr.push( funcParam.paramType[i] );
retArr.push(" ");
retArr.push( funcParam.paramNames[i] );
}
// Function opening
retArr.push(") {\n");
// Body statement iteration
for(var i=0; i<ast.body.length; ++i) {
ast_generic(ast.body[i], retArr, funcParam);
retArr.push("\n");
}
if(funcParam.isRootKernel) {
retArr.push("\nreturn vec4(0.0);");
}
// Function closing
retArr.push("}\n");
return retArr;
}
/// Prases the abstract syntax tree, to return function
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_ReturnStatement(ast, retArr, funcParam) {
if( funcParam.isRootKernel ) {
retArr.push("return encode32(");
ast_generic(ast.argument, retArr, funcParam);
retArr.push("); ");
} else {
retArr.push("return ");
ast_generic(ast.argument, retArr, funcParam);
retArr.push(";");
}
//throw ast_errorOutput(
// "Non main function return, is not supported : "+funcParam.currentFunctionNamespace,
// ast, funcParam
//);
return retArr;
}
/// Prases the abstract syntax tree, literal value
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_Literal(ast, retArr, funcParam) {
// Reject non numeric literals
if( isNaN(ast.value) ) {
throw ast_errorOutput(
"Non-numeric literal not supported : "+ast.value,
ast, funcParam
);
}
// Push the literal value as a float/int
retArr.push( ast.value );
// If it was an int, node made a float
if( Number.isInteger(ast.value) ) {
retArr.push(".0");
}
return retArr;
}
/// Prases the abstract syntax tree, binary expression
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_BinaryExpression(ast, retArr, funcParam) {
if (ast.operator == "%") {
retArr.push("mod(");
ast_generic(ast.left, retArr, funcParam);
retArr.push(",");
ast_generic(ast.right, retArr, funcParam);
retArr.push(")");
} else {
ast_generic(ast.left, retArr, funcParam);
retArr.push(ast.operator);
ast_generic(ast.right, retArr, funcParam);
}
return retArr;
}
/// Prases the abstract syntax tree, identifier expression
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_IdentifierExpression(idtNode, retArr, funcParam) {
if (idtNode.type != "Identifier") {
throw ast_errorOutput(
"IdentifierExpression - not an Identifier",
ast, funcParam
);
}
if (idtNode.name == "gpu_threadX") {
retArr.push('threadId.x');
} else if (idtNode.name == "gpu_threadY") {
retArr.push('threadId.y');
} else if (idtNode.name == "gpu_threadZ") {
retArr.push('threadId.z');
} else if (idtNode.name == "gpu_dimensionsX") {
retArr.push('uOutputDim.x');
} else if (idtNode.name == "gpu_dimensionsY") {
retArr.push('uOutputDim.y');
} else if (idtNode.name == "gpu_dimensionsZ") {
retArr.push('uOutputDim.z');
} else if(funcParam.isRootKernel) {
retArr.push("user_"+idtNode.name);
} else {
retArr.push(idtNode.name);
}
return retArr;
}
/// Prases the abstract syntax tree, genericially to its respective function
///
/// @param ast the AST object to parse
///
/// @returns the prased openclgl string
function ast_ForStatement(forNode, retArr, funcParam) {
if (forNode.type != "ForStatement") {
throw ast_errorOutput(
"Invalid for statment",
ast, funcParam
);
}
retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.test, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(")");
ast_generic(forNode.body, retArr, funcParam);
return retArr;
}
function ast_AssignmentExpression(assNode, retArr, funcParam) {
if(assNode.operator == "%=") {
ast_generic(assNode.left, retArr, funcParam);
retArr.push("=");
retArr.push("mod(");
ast_generic(assNode.left, retArr, funcParam);
retArr.push(",");
ast_generic(assNode.right, retArr, funcParam);
retArr.push(")");
} else {
ast_generic(assNode.left, retArr, funcParam);
retArr.push(assNode.operator);
ast_generic(assNode.right, retArr, funcParam);
return retArr;
}
}
function ast_EmptyStatement(eNode, retArr, funcParam) {
retArr.push(";\n");
return retArr;
}
function ast_BlockStatement(bNode, retArr, funcParam) {
retArr.push("{\n");
for (var i = 0; i < bNode.body.length; i++) {
ast_generic(bNode.body[i], retArr, funcParam);
}
retArr.push("}\n");
return retArr;
}
function ast_ExpressionStatement(esNode, retArr, funcParam) {
ast_generic(esNode.expression, retArr, funcParam);
retArr.push(";\n");
return retArr;
}
function ast_VariableDeclaration(vardecNode, retArr, funcParam) {
retArr.push("float ");
for (var i = 0; i < vardecNode.declarations.length; i++) {
if (i > 0) {
retArr.push(",");
}
ast_generic(vardecNode.declarations[i], retArr, funcParam);
}
retArr.push(";");
return retArr;
}
function ast_VariableDeclarator(ivardecNode, retArr, funcParam) {
ast_generic(ivardecNode.id, retArr, funcParam);
if (ivardecNode.init !== null) {
retArr.push("=");
ast_generic(ivardecNode.init, retArr, funcParam);
}
return retArr;
}
function ast_IfStatement(ifNode, retArr, funcParam) {
retArr.push("if(");
ast_generic(ifNode.test, retArr, funcParam);
retArr.push(")");
ast_generic(ifNode.consequent, retArr, funcParam);
if (ifNode.alternate) {
retArr.push("else ");
ast_generic(ifNode.alternate, retArr, funcParam);
}
return retArr;
}
function ast_Break(brNode, retArr, funcParam) {
retArr.push("break;\n");
return retArr;
}
function ast_Continue(crNode, retArr, funcParam) {
retArr.push("continue;\n");
return retArr;
}
function ast_LogicalExpression(logNode, retArr, funcParam) {
ast_generic(logNode.left, retArr, funcParam);
ast_generic(logNode.operator, retArr, funcParam);
ast_generic(logNode.right, retArr, funcParam);
return retArr;
}
function ast_UpdateExpression(uNode, retArr, funcParam) {
if(uNode.prefix) {
retArr.push(uNode.operator);
ast_generic(uNode.argument, retArr, funcParam);
} else {
ast_generic(uNode.argument, retArr, funcParam);
retArr.push(uNode.operator);
}
return retArr;
}
function ast_UnaryExpression(uNode, retArr, funcParam) {
if(uNode.prefix) {
retArr.push(uNode.operator);
ast_generic(uNode.argument, retArr, funcParam);
} else {
ast_generic(uNode.argument, retArr, funcParam);
retArr.push(uNode.operator);
}
return retArr;
}
function ast_ThisExpression(tNode, retArr, funcParam) {
retArr.push("this");
return retArr;
}
function ast_MemberExpression(mNode, retArr, funcParam) {
if(mNode.computed) {
if (mNode.object.type == "Identifier") {
retArr.push("get(");
ast_generic(mNode.object, retArr, funcParam);
retArr.push(", vec2(");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Size[0],");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Size[1]), vec3(");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Dim[0],");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Dim[1],");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Dim[2]");
retArr.push("), ");
} else {
ast_generic(mNode.object, retArr, funcParam);
var last = retArr.pop();
retArr.push(",");
}
ast_generic(mNode.property, retArr, funcParam);
retArr.push(")");
} else {
// Unroll the member expression
var unrolled = ast_MemberExpression_unroll(mNode);
var unrolled_lc = unrolled.toLowerCase()
if (unrolled_lc == "this.thread.x") {
retArr.push('threadId.x');
} else if (unrolled_lc == "this.thread.y") {
retArr.push('threadId.y');
} else if (unrolled_lc == "this.thread.z") {
retArr.push('threadId.z');
} else if (unrolled_lc == "this.dimensions.x") {
retArr.push('uOutputDim.x');
} else if (unrolled_lc == "this.dimensions.y") {
retArr.push('uOutputDim.y');
} else if (unrolled_lc == "this.dimensions.z") {
retArr.push('uOutputDim.z');
} else {
retArr.push(unrolled);
}
}
return retArr;
}
function ast_SequenceExpression(sNode, retArr, funcParam) {
for (var i = 0; i < sNode.expressions.length; i++) {
if (i > 0) {
retArr.push(",");
}
ast_generic(sNode.expressions, retArr, funcParam);
}
return retArr;
}
/// Utility function for ast_CallExpression.
///
/// Prases the abstract syntax tree, binary expression.
///
/// @param ast the AST object to parse
///
/// @returns {String} the function namespace call, unrolled
function ast_MemberExpression_unroll(ast, funcParam) {
if( ast.type == "Identifier" ) {
return ast.name;
} else if( ast.type == "ThisExpression" ) {
return "this";
}
if( ast.type == "MemberExpression" ) {
if( ast.object && ast.property ) {
return (
ast_MemberExpression_unroll( ast.object, funcParam ) +
"." +
ast_MemberExpression_unroll( ast.property, funcParam )
);
}
}
// Failure, unknown expression
throw ast_errorOutput(
"Unknown CallExpression_unroll",
ast, funcParam
);
}
// The prefixes to use
var jsMathPrefix = "Math.";
var localPrefix = "this.";
/// Prases the abstract syntax tree, binary expression
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_CallExpression(ast, retArr, funcParam) {
if( ast.callee ) {
// Get the full function call, unrolled
var funcName = ast_MemberExpression_unroll(ast.callee);
// Its a math operator, remove the prefix
if( funcName.indexOf(jsMathPrefix) === 0 ) {
funcName = funcName.slice( jsMathPrefix.length );
}
// Its a local function, remove this
if( funcName.indexOf(localPrefix) === 0 ) {
funcName = funcName.slice( localPrefix.length );
}
// Register the function into the called registry
if( funcParam.calledFunctions.indexOf(funcName) < 0 ) {
funcParam.calledFunctions.push(funcName);
}
// Call the function
retArr.push( funcName );
// Open arguments space
retArr.push( "(" );
// Add the vars
for(var i=0; i<ast.arguments.length; ++i) {
if(i > 0) {
retArr.push(", ");
}
ast_generic(ast.arguments[i],retArr,funcParam);
}
// Close arguments space
retArr.push( ")" );
return retArr;
}
// Failure, unknown expression
throw ast_errorOutput(
"Unknown CallExpression",
ast, funcParam
);
return retArr;
}
return functionNode_webgl;
})();