spicy icon indicating copy to clipboard operation
spicy copied to clipboard

Instructions in global scope execute in suprising order

Open bbannier opened this issue 4 years ago • 0 comments

As of c895cf08bb27dd3ce963b59311d1e5bb4482ede3 the following Spicy code

module test;

global x: int32;
x = 32;
global y = x;

print(x, y);

produces the output

(32, 0)

instead of the output (32, 32) one would naively expect.

This is due to us splitting initializations of globals and running code at global scope into two different phases. This causes the initialization of y to run before a value has been assigned to x.

extern void __hlt::test::__init_globals(::hilti::rt::Context* ctx) {
    ::hilti::rt::detail::initModuleGlobals<__globals_t>(__globals_index);
    __globals()->y = test::__globals()->x;
}

extern void __hlt::test::__init_module() {
      __location__("/tmp/foo.spicy:3:17-4:7");
    test::__globals()->x = ::hilti::rt::integer::safe<std::int32_t>{32};
      __location__("/tmp/foo.spicy:7:1");
    ::hilti::rt::print(std::make_tuple(test::__globals()->x, test::__globals()->y), ::hilti::rt::Bool(true));
}

bbannier avatar Nov 01 '21 10:11 bbannier