spin2cpp icon indicating copy to clipboard operation
spin2cpp copied to clipboard

Memory consistency?

Open Wuerfel21 opened this issue 3 years ago • 1 comments

While looking through the optimizer, there's kinda been a question in the back of my mind: Is it ok if memory consistency isn't perfectly maintained? (ie. results don't match an unoptimized program due to memory-related optimization) It partially isn't as-is, but currently the optimizations to that extent are somewhat restricted (i.e. in most cases can't cross branches), though I can still get it to screw up:

CON
_CLKFREQ = 200_000_000
DEBUG_COGS = %01
VAR
long a,b,stack[100]
PUB main() | lock,x,y,z,p,q
p := @a
q := @b
lock := locknew()
x:=locktry(lock)
cogspin(NEWCOG,cog(lock),@stack)
lockrel(lock)
x := long[p]
debug("Just wasting some tiiiiiiiiiiiiimmmmmmmmmmmmeeeeeeeee...")
y := long[q]
z := long[p]
debug(sdec(x,y,z))

PRI cog(lock)
repeat until locktry(lock)
a := 1337
b := 11880

In C this sort of thing is explicitly ok (relatedly: do we even handle volatile right now?), idk about BASIC, but Spin may actually be written with the expectation of consistent memory in some cases.

Wuerfel21 avatar Mar 25 '22 21:03 Wuerfel21

Spin definitely expects memory consistency, and we probably should try to preserve it as much as possible. Which conflicts with the desire to optimize code that's using globals / member variables and stack offsets :(.

totalspectrum avatar Apr 05 '22 14:04 totalspectrum