/
expr.go
639 lines (592 loc) · 17.8 KB
/
expr.go
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Copyright 2020 The Ebiten Authors
//
// 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.
package shader
import (
"fmt"
"go/ast"
gconstant "go/constant"
"go/token"
"regexp"
"strconv"
"github.com/hajimehoshi/ebiten/internal/shaderir"
)
func canTruncateToInteger(v gconstant.Value) bool {
return gconstant.ToInt(v).Kind() != gconstant.Unknown
}
var textureVariableRe = regexp.MustCompile(`\A__t(\d+)\z`)
func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariableUsed bool) ([]shaderir.Expr, []shaderir.Type, []shaderir.Stmt, bool) {
switch e := expr.(type) {
case *ast.BasicLit:
switch e.Kind {
case token.INT:
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeFromLiteral(e.Value, e.Kind, 0),
},
}, []shaderir.Type{{Main: shaderir.Int}}, nil, true
case token.FLOAT:
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeFromLiteral(e.Value, e.Kind, 0),
},
}, []shaderir.Type{{Main: shaderir.Float}}, nil, true
default:
cs.addError(e.Pos(), fmt.Sprintf("literal not implemented: %#v", e))
}
case *ast.BinaryExpr:
var stmts []shaderir.Stmt
// Prase LHS first for the order of the statements.
lhs, ts, ss, ok := cs.parseExpr(block, e.X, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
if len(lhs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a binary operator: %s", e.X))
return nil, nil, nil, false
}
stmts = append(stmts, ss...)
lhst := ts[0]
rhs, ts, ss, ok := cs.parseExpr(block, e.Y, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
if len(rhs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a binary operator: %s", e.Y))
return nil, nil, nil, false
}
stmts = append(stmts, ss...)
rhst := ts[0]
if lhs[0].Type == shaderir.NumberExpr && rhs[0].Type == shaderir.NumberExpr {
op := e.Op
// https://golang.org/pkg/go/constant/#BinaryOp
// "To force integer division of Int operands, use op == token.QUO_ASSIGN instead of
// token.QUO; the result is guaranteed to be Int in this case."
if op == token.QUO && lhs[0].Const.Kind() == gconstant.Int && rhs[0].Const.Kind() == gconstant.Int {
op = token.QUO_ASSIGN
}
var v gconstant.Value
var t shaderir.Type
switch op {
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
v = gconstant.MakeBool(gconstant.Compare(lhs[0].Const, op, rhs[0].Const))
t = shaderir.Type{Main: shaderir.Bool}
default:
v = gconstant.BinaryOp(lhs[0].Const, op, rhs[0].Const)
if v.Kind() == gconstant.Float {
t = shaderir.Type{Main: shaderir.Float}
} else {
t = shaderir.Type{Main: shaderir.Int}
}
}
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: v,
},
}, []shaderir.Type{t}, stmts, true
}
op, ok := shaderir.OpFromToken(e.Op)
if !ok {
cs.addError(e.Pos(), fmt.Sprintf("unexpected operator: %s", e.Op))
return nil, nil, nil, false
}
var t shaderir.Type
switch {
case op == shaderir.LessThanOp || op == shaderir.LessThanEqualOp || op == shaderir.GreaterThanOp || op == shaderir.GreaterThanEqualOp || op == shaderir.EqualOp || op == shaderir.NotEqualOp || op == shaderir.AndAnd || op == shaderir.OrOr:
t = shaderir.Type{Main: shaderir.Bool}
case lhs[0].Type == shaderir.NumberExpr && rhs[0].Type != shaderir.NumberExpr:
if rhst.Main == shaderir.Int {
if !canTruncateToInteger(lhs[0].Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", lhs[0].Const.String()))
return nil, nil, nil, false
}
lhs[0].ConstType = shaderir.ConstTypeInt
}
t = rhst
case lhs[0].Type != shaderir.NumberExpr && rhs[0].Type == shaderir.NumberExpr:
if lhst.Main == shaderir.Int {
if !canTruncateToInteger(rhs[0].Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", rhs[0].Const.String()))
return nil, nil, nil, false
}
rhs[0].ConstType = shaderir.ConstTypeInt
}
t = lhst
case lhst.Equal(&rhst):
t = lhst
case lhst.Main == shaderir.Float || lhst.Main == shaderir.Int:
switch rhst.Main {
case shaderir.Int:
t = lhst
case shaderir.Float, shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
t = rhst
default:
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
case rhst.Main == shaderir.Float || rhst.Main == shaderir.Int:
switch lhst.Main {
case shaderir.Int:
t = rhst
case shaderir.Float, shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
t = lhst
default:
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
case lhst.Main == shaderir.Vec2 && rhst.Main == shaderir.Mat2 ||
lhst.Main == shaderir.Mat2 && rhst.Main == shaderir.Vec2:
t = shaderir.Type{Main: shaderir.Vec2}
case lhst.Main == shaderir.Vec3 && rhst.Main == shaderir.Mat3 ||
lhst.Main == shaderir.Mat3 && rhst.Main == shaderir.Vec3:
t = shaderir.Type{Main: shaderir.Vec3}
case lhst.Main == shaderir.Vec4 && rhst.Main == shaderir.Mat4 ||
lhst.Main == shaderir.Mat4 && rhst.Main == shaderir.Vec4:
t = shaderir.Type{Main: shaderir.Vec4}
default:
cs.addError(e.Pos(), fmt.Sprintf("invalid expression: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
return []shaderir.Expr{
{
Type: shaderir.Binary,
Op: op,
Exprs: []shaderir.Expr{lhs[0], rhs[0]},
},
}, []shaderir.Type{t}, stmts, true
case *ast.CallExpr:
var (
callee shaderir.Expr
args []shaderir.Expr
argts []shaderir.Type
stmts []shaderir.Stmt
)
// Parse the argument first for the order of the statements.
for _, a := range e.Args {
es, ts, ss, ok := cs.parseExpr(block, a, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
if len(es) > 1 && len(e.Args) > 1 {
cs.addError(e.Pos(), fmt.Sprintf("single-value context and multiple-value context cannot be mixed: %s", e.Fun))
return nil, nil, nil, false
}
args = append(args, es...)
argts = append(argts, ts...)
stmts = append(stmts, ss...)
}
// TODO: When len(ss) is not 0?
es, _, ss, ok := cs.parseExpr(block, e.Fun, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
if len(es) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a callee: %s", e.Fun))
return nil, nil, nil, false
}
callee = es[0]
stmts = append(stmts, ss...)
// For built-in functions, we can call this in this position. Return an expression for the function
// call.
if callee.Type == shaderir.BuiltinFuncExpr {
if callee.BuiltinFunc == shaderir.Len {
if len(args) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("number of len's arguments must be 1 but %d", len(args)))
return nil, nil, nil, false
}
if argts[0].Main != shaderir.Array {
cs.addError(e.Pos(), fmt.Sprintf("len takes an array but %s", argts[0].String()))
return nil, nil, nil, false
}
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeInt64(int64(argts[0].Length)),
ConstType: shaderir.ConstTypeInt,
},
}, []shaderir.Type{{Main: shaderir.Int}}, stmts, true
}
var t shaderir.Type
switch callee.BuiltinFunc {
case shaderir.BoolF:
t = shaderir.Type{Main: shaderir.Bool}
case shaderir.IntF:
t = shaderir.Type{Main: shaderir.Int}
case shaderir.FloatF:
t = shaderir.Type{Main: shaderir.Float}
case shaderir.Vec2F:
t = shaderir.Type{Main: shaderir.Vec2}
case shaderir.Vec3F:
t = shaderir.Type{Main: shaderir.Vec3}
case shaderir.Vec4F:
t = shaderir.Type{Main: shaderir.Vec4}
case shaderir.Mat2F:
t = shaderir.Type{Main: shaderir.Mat2}
case shaderir.Mat3F:
t = shaderir.Type{Main: shaderir.Mat3}
case shaderir.Mat4F:
t = shaderir.Type{Main: shaderir.Mat4}
case shaderir.Step:
t = argts[1]
case shaderir.Smoothstep:
t = argts[2]
case shaderir.Length, shaderir.Distance, shaderir.Dot:
t = shaderir.Type{Main: shaderir.Float}
case shaderir.Cross:
t = shaderir.Type{Main: shaderir.Vec3}
case shaderir.Texture2DF:
t = shaderir.Type{Main: shaderir.Vec4}
default:
t = argts[0]
}
return []shaderir.Expr{
{
Type: shaderir.Call,
Exprs: append([]shaderir.Expr{callee}, args...),
},
}, []shaderir.Type{t}, stmts, true
}
if callee.Type != shaderir.FunctionExpr {
cs.addError(e.Pos(), fmt.Sprintf("function callee must be a funciton name but %s", e.Fun))
return nil, nil, nil, false
}
f := cs.funcs[callee.Index]
for i, p := range f.ir.InParams {
if args[i].Type == shaderir.NumberExpr && p.Main == shaderir.Int {
if !cs.forceToInt(e, &args[i]) {
return nil, nil, nil, false
}
}
}
var outParams []int
for _, p := range f.ir.OutParams {
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: p,
})
args = append(args, shaderir.Expr{
Type: shaderir.LocalVariable,
Index: idx,
})
outParams = append(outParams, idx)
}
if t := f.ir.Return; t.Main != shaderir.None {
if len(outParams) != 0 {
cs.addError(e.Pos(), fmt.Sprintf("a function returning value cannot have out-params so far: %s", e.Fun))
return nil, nil, nil, false
}
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: t,
})
// Calling the function should be done eariler to treat out-params correctly.
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
{
Type: shaderir.Call,
Exprs: append([]shaderir.Expr{callee}, args...),
},
},
})
// The actual expression here is just a local variable that includes the result of the
// function call.
return []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
}, []shaderir.Type{t}, stmts, true
}
// Even if the function doesn't return anything, calling the function should be done eariler to keep
// the evaluation order.
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.ExprStmt,
Exprs: []shaderir.Expr{
{
Type: shaderir.Call,
Exprs: append([]shaderir.Expr{callee}, args...),
},
},
})
if len(outParams) == 0 {
// TODO: Is this an error?
}
var exprs []shaderir.Expr
for _, p := range outParams {
exprs = append(exprs, shaderir.Expr{
Type: shaderir.LocalVariable,
Index: p,
})
}
return exprs, f.ir.OutParams, stmts, true
case *ast.Ident:
if e.Name == "_" {
// In the context where a local variable is marked as used, any expressions must have its
// meaning. Then, a blank identifier is not available there.
if markLocalVariableUsed {
cs.addError(e.Pos(), fmt.Sprintf("cannot use _ as value"))
return nil, nil, nil, false
}
return []shaderir.Expr{
{
Type: shaderir.Blank,
},
}, []shaderir.Type{{}}, nil, true
}
if i, t, ok := block.findLocalVariable(e.Name, markLocalVariableUsed); ok {
return []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: i,
},
}, []shaderir.Type{t}, nil, true
}
if i, ok := cs.findFunction(e.Name); ok {
return []shaderir.Expr{
{
Type: shaderir.FunctionExpr,
Index: i,
},
}, nil, nil, true
}
if i, ok := cs.findUniformVariable(e.Name); ok {
return []shaderir.Expr{
{
Type: shaderir.UniformVariable,
Index: i,
},
}, []shaderir.Type{cs.ir.Uniforms[i]}, nil, true
}
if f, ok := shaderir.ParseBuiltinFunc(e.Name); ok {
return []shaderir.Expr{
{
Type: shaderir.BuiltinFuncExpr,
BuiltinFunc: f,
},
}, nil, nil, true
}
if m := textureVariableRe.FindStringSubmatch(e.Name); m != nil {
i, _ := strconv.Atoi(m[1])
return []shaderir.Expr{
{
Type: shaderir.TextureVariable,
Index: i,
},
}, nil, nil, true
}
if e.Name == "true" || e.Name == "false" {
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeBool(e.Name == "true"),
},
}, []shaderir.Type{{Main: shaderir.Bool}}, nil, true
}
cs.addError(e.Pos(), fmt.Sprintf("unexpected identifier: %s", e.Name))
case *ast.ParenExpr:
return cs.parseExpr(block, e.X, markLocalVariableUsed)
case *ast.SelectorExpr:
exprs, _, stmts, ok := cs.parseExpr(block, e.X, true)
if !ok {
return nil, nil, nil, false
}
if len(exprs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a selector: %s", e.X))
return nil, nil, nil, false
}
var t shaderir.Type
switch len(e.Sel.Name) {
case 1:
t.Main = shaderir.Float
case 2:
t.Main = shaderir.Vec2
case 3:
t.Main = shaderir.Vec3
case 4:
t.Main = shaderir.Vec4
default:
cs.addError(e.Pos(), fmt.Sprintf("unexpected swizzling: %s", e.Sel.Name))
return nil, nil, nil, false
}
return []shaderir.Expr{
{
Type: shaderir.FieldSelector,
Exprs: []shaderir.Expr{
exprs[0],
{
Type: shaderir.SwizzlingExpr,
Swizzling: e.Sel.Name,
},
},
},
}, []shaderir.Type{t}, stmts, true
case *ast.UnaryExpr:
exprs, t, stmts, ok := cs.parseExpr(block, e.X, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
if len(exprs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a unary operator: %s", e.X))
return nil, nil, nil, false
}
if exprs[0].Type == shaderir.NumberExpr {
v := gconstant.UnaryOp(e.Op, exprs[0].Const, 0)
t := shaderir.Type{Main: shaderir.Int}
if v.Kind() == gconstant.Float {
t = shaderir.Type{Main: shaderir.Float}
}
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: v,
},
}, []shaderir.Type{t}, stmts, true
}
var op shaderir.Op
switch e.Op {
case token.ADD:
op = shaderir.Add
case token.SUB:
op = shaderir.Sub
case token.NOT:
op = shaderir.NotOp
default:
cs.addError(e.Pos(), fmt.Sprintf("unexpected operator: %s", e.Op))
return nil, nil, nil, false
}
return []shaderir.Expr{
{
Type: shaderir.Unary,
Op: op,
Exprs: exprs,
},
}, t, stmts, true
case *ast.CompositeLit:
t, ok := cs.parseType(block, e.Type)
if !ok {
return nil, nil, nil, false
}
if t.Main == shaderir.Array && t.Length == -1 {
t.Length = len(e.Elts)
}
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: t,
})
var stmts []shaderir.Stmt
for i, e := range e.Elts {
exprs, _, ss, ok := cs.parseExpr(block, e, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
if len(exprs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a composite literal"))
return nil, nil, nil, false
}
stmts = append(stmts, ss...)
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
{
Type: shaderir.Index,
Exprs: []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeInt64(int64(i)),
ConstType: shaderir.ConstTypeInt,
},
},
},
exprs[0],
},
})
}
return []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
}, []shaderir.Type{t}, stmts, true
case *ast.IndexExpr:
var stmts []shaderir.Stmt
// Parse the index first
exprs, _, ss, ok := cs.parseExpr(block, e.Index, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
stmts = append(stmts, ss...)
if len(exprs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at an index expression"))
return nil, nil, nil, false
}
idx := exprs[0]
if idx.Type == shaderir.NumberExpr {
if !canTruncateToInteger(idx.Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", idx.Const.String()))
return nil, nil, nil, false
}
idx.ConstType = shaderir.ConstTypeInt
}
exprs, ts, ss, ok := cs.parseExpr(block, e.X, markLocalVariableUsed)
if !ok {
return nil, nil, nil, false
}
stmts = append(stmts, ss...)
if len(exprs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at an index expression"))
return nil, nil, nil, false
}
x := exprs[0]
t := ts[0]
var typ shaderir.Type
switch t.Main {
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4:
typ = shaderir.Type{Main: shaderir.Float}
case shaderir.Mat2:
typ = shaderir.Type{Main: shaderir.Vec2}
case shaderir.Mat3:
typ = shaderir.Type{Main: shaderir.Vec3}
case shaderir.Mat4:
typ = shaderir.Type{Main: shaderir.Vec4}
case shaderir.Array:
typ = t.Sub[0]
default:
cs.addError(e.Pos(), fmt.Sprintf("index operator cannot be applied to the type %s", t.String()))
return nil, nil, nil, false
}
return []shaderir.Expr{
{
Type: shaderir.Index,
Exprs: []shaderir.Expr{
x,
idx,
},
},
}, []shaderir.Type{typ}, stmts, true
default:
cs.addError(e.Pos(), fmt.Sprintf("expression not implemented: %#v", e))
}
return nil, nil, nil, false
}