SPIN compiler bug???
pedward
Posts: 1,642
Reference http://forums.parallax.com/showthread.php?136865-Writing-a-driver-for-two-steppers-is-harder-than-I-thought!&p=1062271&viewfull=1#post1062271
The short of it is that when SPIN launches a COG with COGNEW using an OBJ as the target, it calls the Start method first, then launches the COG with COGINIT and then calls the Start method again. If the Start method does not return, the program flow is halted in the COG that called COGNEW.
The workaround is to create local functions in the namespace of the calling COG that call the OBJ Start method. In this case the SPIN compiler only calls COGINIT with the Start method, which is the expected behavior.
EDIT:
Well, JonnyMac pointed out that the v1.2 of the Propeller manual states that you shouldn't call outside of the current file! Doh!
The short of it is that when SPIN launches a COG with COGNEW using an OBJ as the target, it calls the Start method first, then launches the COG with COGINIT and then calls the Start method again. If the Start method does not return, the program flow is halted in the COG that called COGNEW.
The workaround is to create local functions in the namespace of the calling COG that call the OBJ Start method. In this case the SPIN compiler only calls COGINIT with the Start method, which is the expected behavior.
EDIT:
Well, JonnyMac pointed out that the v1.2 of the Propeller manual states that you shouldn't call outside of the current file! Doh!
Comments
Sample compiler output when calling Start method in OBJ, doesn't work:
Yes, these are generated by BST, but [allegedly] it generates the same binaries as the PDT.
Most OBJects you call obj.Start, and that function calls cognew, which works predictably because the function passed to cognew is in the local calling space. When you pass a function outside of the local calling space, the compiler generates code that CALLs the passed function, then passes the function to COGNEW. This is clearly aberrant behavior.
There's no need for a "workaround" -- one can simply construct objects and their calls using standardized (and successful) mechanisms demonstrated by Parallax and hundreds of Propeller programmers.
-Phil
To fix the problem, COGNEW must be annotated with a note saying effectively "only pass function names that are local to the calling code".
Furthermore, COGNEW should only be used inside the calling context of the OBJ. This means if you want to launch a routine into a new COG, you must make at least 2 routines in an OBJ, a Start routing and a function that you want to launch into it's own COG. This also allows each OBJ to reserve the exact amount of stack space it needs for each instance.
EG:
For those that missed this same discussion in another thread, that clarification does in fact exist in version 1.2 of the Propeller documentation.