Skip to content

Commit db74dd1

Browse files
committed
Remove optimization
1 parent a54afdc commit db74dd1

File tree

3 files changed

+130
-152
lines changed

3 files changed

+130
-152
lines changed

tests/transpiler/options_test.go

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,24 @@ func foo(x int) int {
3232
options: transpiler.Options{
3333
Numbers: true,
3434
},
35-
output: `jump 5 always # 0
36-
set _foo_x @funcArg_foo_0 # 1
37-
op add _foo_0 _foo_x 20 # 2
38-
set @return_0 _foo_0 # 3
39-
set @counter @funcTramp_foo # 4
40-
set _main_i 0 # 5
41-
jump 8 lessThan _main_i 10 # 6
42-
jump 16 always # 7
43-
set @funcArg_foo_0 _main_i # 8
44-
set @funcTramp_foo 11 # 9
45-
jump 1 always # 10
46-
set _main_0 @return_0 # 11
47-
print _main_0 # 12
48-
print "\n" # 13
49-
op add _main_i _main_i 1 # 14
50-
jump 8 lessThan _main_i 10 # 15
51-
end # 16 `,
35+
output: `jump 5 always # 0
36+
set _foo_x @funcArg_foo_0 # 1
37+
op add _foo_0 _foo_x 20 # 2
38+
set @return_0 _foo_0 # 3
39+
set @counter @funcTramp_foo # 4
40+
set _main_i 0 # 5
41+
op lessThan _main_0 _main_i 10 # 6
42+
jump 9 equal _main_0 true # 7
43+
jump 17 always # 8
44+
set @funcArg_foo_0 _main_i # 9
45+
set @funcTramp_foo 12 # 10
46+
jump 1 always # 11
47+
set _main_1 @return_0 # 12
48+
print _main_1 # 13
49+
print "\n" # 14
50+
op add _main_i _main_i 1 # 15
51+
jump 6 always # 16
52+
end # 17 `,
5253
},
5354
{
5455
name: "Comments",
@@ -57,29 +58,30 @@ end # 16 `,
5758
Comments: true,
5859
CommentOffset: 45,
5960
},
60-
output: `jump 5 always # Jump to start of main
61-
#
62-
# Function: foo #
63-
#
64-
set _foo_x @funcArg_foo_0 # Read parameter into variable
65-
op add _foo_0 _foo_x 20 # Execute operation
66-
set @return_0 _foo_0 # Set return data
67-
set @counter @funcTramp_foo # Trampoline back
68-
#
69-
# Function: main #
70-
#
71-
set _main_i 0 # Assign value to variable
72-
jump 8 lessThan _main_i 10 # Jump into the loop
73-
jump 16 always # Jump to end of loop
74-
set @funcArg_foo_0 _main_i # Set foo argument: 0
75-
set @funcTramp_foo 11 # Set Trampoline Address
76-
jump 1 always # Jump to function: foo
77-
set _main_0 @return_0 # Set variable to returned value
78-
print _main_0 # Call to native function
79-
print "\n" # Call to native function
80-
op add _main_i _main_i 1 # Execute increment/decrement
81-
jump 8 lessThan _main_i 10 # Jump to start of loop
82-
end # Trampoline back `,
61+
output: `jump 5 always # Jump to start of main
62+
#
63+
# Function: foo #
64+
#
65+
set _foo_x @funcArg_foo_0 # Read parameter into variable
66+
op add _foo_0 _foo_x 20 # Execute operation
67+
set @return_0 _foo_0 # Set return data
68+
set @counter @funcTramp_foo # Trampoline back
69+
#
70+
# Function: main #
71+
#
72+
set _main_i 0 # Assign value to variable
73+
op lessThan _main_0 _main_i 10 # Execute operation
74+
jump 9 equal _main_0 true # Jump into the loop
75+
jump 17 always # Jump to end of loop
76+
set @funcArg_foo_0 _main_i # Set foo argument: 0
77+
set @funcTramp_foo 12 # Set Trampoline Address
78+
jump 1 always # Jump to function: foo
79+
set _main_1 @return_0 # Set variable to returned value
80+
print _main_1 # Call to native function
81+
print "\n" # Call to native function
82+
op add _main_i _main_i 1 # Execute increment/decrement
83+
jump 6 always # Jump to start of loop
84+
end # Trampoline back `,
8385
},
8486
{
8587
name: "Comments",
@@ -88,23 +90,24 @@ end # Trampoline back `,
8890
Source: true,
8991
CommentOffset: 45,
9092
},
91-
output: `jump 5 always
92-
set _foo_x @funcArg_foo_0
93-
op add _foo_0 _foo_x 20 # x + 20
94-
set @return_0 _foo_0 # return x + 20
95-
set @counter @funcTramp_foo
96-
set _main_i 0 # i := 0
97-
jump 8 lessThan _main_i 10
98-
jump 16 always
99-
set @funcArg_foo_0 _main_i
100-
set @funcTramp_foo 11
101-
jump 1 always # foo(i)
102-
set _main_0 @return_0
103-
print _main_0 # println(foo(i))
104-
print "\n" # println(foo(i))
105-
op add _main_i _main_i 1 # i++
106-
jump 8 lessThan _main_i 10
107-
end `,
93+
output: `jump 5 always
94+
set _foo_x @funcArg_foo_0
95+
op add _foo_0 _foo_x 20 # x + 20
96+
set @return_0 _foo_0 # return x + 20
97+
set @counter @funcTramp_foo
98+
set _main_i 0 # i := 0
99+
op lessThan _main_0 _main_i 10 # i < 10
100+
jump 9 equal _main_0 true
101+
jump 17 always
102+
set @funcArg_foo_0 _main_i
103+
set @funcTramp_foo 12
104+
jump 1 always # foo(i)
105+
set _main_1 @return_0
106+
print _main_1 # println(foo(i))
107+
print "\n" # println(foo(i))
108+
op add _main_i _main_i 1 # i++
109+
jump 6 always
110+
end `,
108111
},
109112
{
110113
name: "All",
@@ -115,29 +118,30 @@ end `,
115118
Source: true,
116119
CommentOffset: 45,
117120
},
118-
output: `jump 5 always # 0 # Jump to start of main
119-
#
120-
# Function: foo #
121-
#
122-
set _foo_x @funcArg_foo_0 # 1 # Read parameter into variable
123-
op add _foo_0 _foo_x 20 # 2 # Execute operation # x + 20
124-
set @return_0 _foo_0 # 3 # Set return data # return x + 20
125-
set @counter @funcTramp_foo # 4 # Trampoline back
126-
#
127-
# Function: main #
128-
#
129-
set _main_i 0 # 5 # Assign value to variable # i := 0
130-
jump 8 lessThan _main_i 10 # 6 # Jump into the loop
131-
jump 16 always # 7 # Jump to end of loop
132-
set @funcArg_foo_0 _main_i # 8 # Set foo argument: 0
133-
set @funcTramp_foo 11 # 9 # Set Trampoline Address
134-
jump 1 always # 10 # Jump to function: foo # foo(i)
135-
set _main_0 @return_0 # 11 # Set variable to returned value
136-
print _main_0 # 12 # Call to native function # println(foo(i))
137-
print "\n" # 13 # Call to native function # println(foo(i))
138-
op add _main_i _main_i 1 # 14 # Execute increment/decrement # i++
139-
jump 8 lessThan _main_i 10 # 15 # Jump to start of loop
140-
end # 16 # Trampoline back `,
121+
output: `jump 5 always # 0 # Jump to start of main
122+
#
123+
# Function: foo #
124+
#
125+
set _foo_x @funcArg_foo_0 # 1 # Read parameter into variable
126+
op add _foo_0 _foo_x 20 # 2 # Execute operation # x + 20
127+
set @return_0 _foo_0 # 3 # Set return data # return x + 20
128+
set @counter @funcTramp_foo # 4 # Trampoline back
129+
#
130+
# Function: main #
131+
#
132+
set _main_i 0 # 5 # Assign value to variable # i := 0
133+
op lessThan _main_0 _main_i 10 # 6 # Execute operation # i < 10
134+
jump 9 equal _main_0 true # 7 # Jump into the loop
135+
jump 17 always # 8 # Jump to end of loop
136+
set @funcArg_foo_0 _main_i # 9 # Set foo argument: 0
137+
set @funcTramp_foo 12 # 10 # Set Trampoline Address
138+
jump 1 always # 11 # Jump to function: foo # foo(i)
139+
set _main_1 @return_0 # 12 # Set variable to returned value
140+
print _main_1 # 13 # Call to native function # println(foo(i))
141+
print "\n" # 14 # Call to native function # println(foo(i))
142+
op add _main_i _main_i 1 # 15 # Execute increment/decrement # i++
143+
jump 6 always # 16 # Jump to start of loop
144+
end # 17 # Trampoline back `,
141145
},
142146
}
143147
for _, test := range tests {

tests/transpiler/statement_test.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ print 6`,
3232
name: "ForLoop",
3333
input: TestMain(`for i := 0; i < 10; i++ { print(i) }`, false, false),
3434
output: `set _main_i 0
35-
jump 3 lessThan _main_i 10
36-
jump 6 always
35+
op lessThan _main_0 _main_i 10
36+
jump 4 equal _main_0 true
37+
jump 7 always
3738
print _main_i
3839
op add _main_i _main_i 1
39-
jump 3 lessThan _main_i 10`,
40+
jump 1 always`,
4041
},
4142
{
4243
name: "ForLoop2",
@@ -62,8 +63,10 @@ for s.IsEnabled() && a < b {
6263
set _main_a 1
6364
set _main_b 2
6465
sensor _main_0 _main_s @enabled
65-
jump 6 equal _main_0 true
66-
jump 8 always
66+
op lessThan _main_1 _main_a _main_b
67+
op land _main_2 _main_0 _main_1
68+
jump 8 equal _main_2 true
69+
jump 10 always
6770
print 1
6871
jump 3 always`,
6972
},
@@ -94,31 +97,33 @@ print _main_x`,
9497
name: "Break",
9598
input: TestMain(`for i := 0; i < 10; i++ { if i == 5 { break; }; println(i); }`, false, false),
9699
output: `set _main_i 0
97-
jump 3 lessThan _main_i 10
98-
jump 11 always
99-
op equal _main_0 _main_i 5
100-
jump 6 equal _main_0 1
101-
jump 7 always
102-
jump 11 always
100+
op lessThan _main_0 _main_i 10
101+
jump 4 equal _main_0 true
102+
jump 12 always
103+
op equal _main_1 _main_i 5
104+
jump 7 equal _main_1 1
105+
jump 8 always
106+
jump 12 always
103107
print _main_i
104108
print "\n"
105109
op add _main_i _main_i 1
106-
jump 3 lessThan _main_i 10`,
110+
jump 1 always`,
107111
},
108112
{
109113
name: "Continue",
110114
input: TestMain(`for i := 0; i < 10; i++ { if i == 5 { continue; }; println(i); }`, false, false),
111115
output: `set _main_i 0
112-
jump 3 lessThan _main_i 10
113-
jump 11 always
114-
op equal _main_0 _main_i 5
115-
jump 6 equal _main_0 1
116-
jump 7 always
117-
jump 9 always
116+
op lessThan _main_0 _main_i 10
117+
jump 4 equal _main_0 true
118+
jump 12 always
119+
op equal _main_1 _main_i 5
120+
jump 7 equal _main_1 1
121+
jump 8 always
122+
jump 10 always
118123
print _main_i
119124
print "\n"
120125
op add _main_i _main_i 1
121-
jump 3 lessThan _main_i 10`,
126+
jump 1 always`,
122127
},
123128
{
124129
name: "Switch",
@@ -233,11 +238,12 @@ print 2
233238
jump end
234239
loop:
235240
set _main_i 0
236-
jump 7 lessThan _main_i 10
237-
jump 10 always
241+
op lessThan _main_0 _main_i 10
242+
jump 8 equal _main_0 true
243+
jump 11 always
238244
print 3
239245
op add _main_i _main_i 1
240-
jump 7 lessThan _main_i 10
246+
jump 5 always
241247
print 4
242248
jump test
243249
end:

transpiler/statement.go

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -335,62 +335,30 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
335335
}
336336

337337
if binaryExpr, ok := statement.Cond.(*ast.BinaryExpr); ok {
338-
if translatedOp, ok := jumpOperators[binaryExpr.Op]; ok {
339-
leftSide, leftExprInstructions, err := exprToResolvable(ctx, binaryExpr.X)
340-
if err != nil {
341-
return nil, err
342-
}
343-
results = append(results, leftExprInstructions...)
344-
345-
if len(leftSide) != 1 {
346-
return nil, Err(ctx, "unknown error")
347-
}
348-
349-
rightSide, rightExprInstructions, err := exprToResolvable(ctx, binaryExpr.Y)
350-
if err != nil {
351-
return nil, err
352-
}
353-
results = append(results, rightExprInstructions...)
354-
355-
if len(rightSide) != 1 {
356-
return nil, Err(ctx, "unknown error")
357-
}
358-
359-
loopStartJump.Condition = []Resolvable{
360-
&Value{Value: translatedOp},
361-
leftSide[0],
362-
rightSide[0],
363-
}
364-
365-
intoLoopJump.Condition = []Resolvable{
366-
&Value{Value: translatedOp},
367-
leftSide[0],
368-
rightSide[0],
369-
}
370-
} else {
371-
expr, exprInstructions, err := exprToResolvable(ctx, binaryExpr.X)
372-
if err != nil {
373-
return nil, err
374-
}
338+
// TODO Optimize jump instruction if possible
375339

376-
results = append(results, exprInstructions...)
340+
expr, exprInstructions, err := exprToResolvable(ctx, binaryExpr)
341+
if err != nil {
342+
return nil, err
343+
}
377344

378-
if len(expr) != 1 {
379-
return nil, Err(ctx, "unknown error")
380-
}
345+
results = append(results, exprInstructions...)
381346

382-
loopStartJump.Condition = []Resolvable{
383-
&Value{Value: "always"},
384-
}
347+
if len(expr) != 1 {
348+
return nil, Err(ctx, "unknown error")
349+
}
385350

386-
intoLoopJump.Condition = []Resolvable{
387-
&Value{Value: jumpOperators[token.EQL]},
388-
expr[0],
389-
&Value{Value: "true"},
390-
}
351+
loopStartJump.Condition = []Resolvable{
352+
&Value{Value: "always"},
353+
}
391354

392-
loopStartOverride = &exprInstructions[0]
355+
intoLoopJump.Condition = []Resolvable{
356+
&Value{Value: jumpOperators[token.EQL]},
357+
expr[0],
358+
&Value{Value: "true"},
393359
}
360+
361+
loopStartOverride = &exprInstructions[0]
394362
} else if unaryExpr, ok := statement.Cond.(*ast.UnaryExpr); ok {
395363
if unaryExpr.Op != token.NOT {
396364
return nil, Err(ctx, fmt.Sprintf("loop unary expresion cannot use this operation: %T", binaryExpr.Op))

0 commit comments

Comments
 (0)