# with a working directory wherever the conda env lives (i.e. `chipyard` root)
c++ ./scratch/context.cc -g -o ./scratch/context -I "$RISCV/include" \
-L.conda-env/riscv-tools/lib -Wl,-rpath,.conda-env/riscv-tools/lib \
./toolchains/riscv-tools/riscv-isa-sim/fesvr/context.cc \
&& ./scratch/context
ought to produce output like:
hello from initial
hello from always
back to always
back to always
all done!
Which is perhaps not quite what we'd expect from a cursory read of context.cc.
Well, we never see back to initial and only half as many of the back to always as we might've expected because top and target are the same:
$ gdb ./scratch/context -ex 'b Bottom' -ex 'run'
...
hello from initial
Breakpoint 1, Bottom () at ./scratch/context.cc:45
45 while (true) target->switch_to();
(gdb) p target
$1 = (context_t *) 0x5555555580a0 <top>
(gdb) c
Continuing.
hello from always
Breakpoint 1, Bottom () at ./scratch/context.cc:45
45 while (true) target->switch_to();
(gdb) p target
$1 = (context_t *) 0x5555555580a0 <top>
So, bottom.switch_to() and top.switch_to() are aliases: that is, the line in Initial that says bottom.switch_to() will send control flow back to Top, where it'll continue to execute on the line after initial.switch_to().
Similarly, we get half as many prints because Always loop is effectively:
while (true) {
top.switch_to();
printf(" back to always\n");
top.switch_to();
}meaning, we have to resume the Always task twice to see the first print, and then the next always.switch_to() will trigger the loop back-edge and stop.
The result I was expecting:
hello from initial
back to initial
hello from always
back to always
back to always
back to always
back to always
all done!
can be obtained by overwriting target at points b. and c. instead of (or, in addition to) a..