You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is one of the dark corners of the escape/emit support in Terra: they are lexically scoped, but strange things can happen because you can capture closures and then run them afterwards.
local c = terralib.includec("stdio.h")
local h = {}
function g()
h.x()
end
terra main()
var x = 1
escape
function h.x() emit quote c.printf("%d\n", x) end end
emit quote
x = x + 20
escape
g()
end
end
end
x = x + 300
escape
emit quote
x = x + 4000
escape
g()
end
end
end
end
-- g()
main:printpretty()
main()
This produces the output:
scape_containment5.t:9: terra main() : {}
cape_containment5.t:10: var x : int32 = 1
cape_containment5.t:12: printf("%d\n", x)
cape_containment5.t:14: x = x + 20
cape_containment5.t:20: x = x + 300
cape_containment5.t:12: printf("%d\n", x)
cape_containment5.t:23: x = x + 4000
scape_containment5.t:9: end
1
321
A couple of notes:
The ordering of the first printf and x = x + 20 prove that emit is lexically scoped. If it were dynamically scoped, the lines would be reversed (and the output would be 21 instead of 1).
The ordering of the second printf is kind of bizarre. It seems that the semantics of escape are to place the code at the bottom of whatever function contains the escape, rather than being inside the escape itself. Therefore, when we capture the h.x closure and run it later, we get the second printf after the statement x = x + 300, which occurs after the escape where h.x was defined in the first place.
The indirection through g is just to prove that we can do this with arbitrary data structures and arbitrary code indirection; i.e., this could be any number of levels deep in the stack.
I don't know if it's worth it, but should we try to tighten up this behavior at all, either disallowing the use of h.x once the associated escape has executed, or fixing it so the output appears in the right place?
The text was updated successfully, but these errors were encountered:
This is one of the dark corners of the
escape
/emit
support in Terra: they are lexically scoped, but strange things can happen because you can capture closures and then run them afterwards.This produces the output:
A couple of notes:
printf
andx = x + 20
prove thatemit
is lexically scoped. If it were dynamically scoped, the lines would be reversed (and the output would be21
instead of1
).printf
is kind of bizarre. It seems that the semantics ofescape
are to place the code at the bottom of whatever function contains theescape
, rather than being inside the escape itself. Therefore, when we capture theh.x
closure and run it later, we get the secondprintf
after the statementx = x + 300
, which occurs after the escape whereh.x
was defined in the first place.g
is just to prove that we can do this with arbitrary data structures and arbitrary code indirection; i.e., this could be any number of levels deep in the stack.I don't know if it's worth it, but should we try to tighten up this behavior at all, either disallowing the use of
h.x
once the associatedescape
has executed, or fixing it so the output appears in the right place?The text was updated successfully, but these errors were encountered: