Popping a Constant ;^)

classic Classic list List threaded Threaded
6 messages Options
dlk
Reply | Threaded
Open this post in threaded view
|

Popping a Constant ;^)

dlk
Hi,

When one pops the top of the stack into a constant, what's the intended behavior
if the value on the top of stack is not equal to the constant?  For instance, if
the value on the top of the stack is 42, and the following VM instruction is executed:

   pop constant 1

should the  VM, besides decrementing SP:

   - execute the HCF* instruction?
   - make all subsequent uses of the constant 1 behave as if they were instead uses of 42?
   - do something else?

Very confused,
  - Dan

*Halt and Catch Fire.  Also called the LOTS instruction (Let Out the Smoke).
Reply | Threaded
Open this post in threaded view
|

Re: Popping a Constant ;^)

cadet1620
Administrator
A compiler should never generate a "pop constant" VM instruction.  The behavior of the VM translator upon encountering a "pop constant" is undefined.  Mine raises an exception and halts.

--Mark
dlk
Reply | Threaded
Open this post in threaded view
|

Re: Popping a Constant ;^)

dlk
Seriously, the best thing would probably just be to pop the stack ignoring the value. If this were the defined behavior, the compiler could use it to discard an unused function return value without emitting the code necessary to store the value in a sacrificial temp register. The VM can't easily optimize away the store into the temp register since it doesn't really know if subsequently executed code will use it...
Reply | Threaded
Open this post in threaded view
|

Re: Popping a Constant ;^)

ybakos
@dlk: I'm not sure what you mean. It sounds like you're not understanding the point of push constant. This is a "special" instruction that allows the compiler to generate constants within an instruction and ultimately store them somewhere. There's no need to "pop constant FOO" because in general if you want to move that value around you'll be popping into another location such as local or arg.
Reply | Threaded
Open this post in threaded view
|

Re: Popping a Constant ;^)

cadet1620
Administrator
In reply to this post by dlk
dlk wrote
...make all subsequent uses of the constant 1 behave as if they were instead uses of 42?

Seriously, the best thing would probably just be to pop the stack ignoring the value.
But sometimes you want to change the constant.

My first industry job was fixing bugs in an APT (NC machining) compiler. This program was written in FORTRAN II and was running on a system that was so starved for memory that every trick possible was being used to save memory usage. There were about 40 source files in the program. Because of the machine architecture, a word of memory was required for every unique constant used in each source file. To save memory, global variables IONE, ITWO, etc. were used to store commonly used constants.

One of the bugs was tracked down to code that read
    IONE=ITWO
    INTERP(...)
    IONE=1
INTERP did interpolation of array data, and in this case it was supposed to handle every other item in the array.  INTERP was full of "IONE"s and "1"s, and the bug of course, was that one of them should have been the other.

Those were the days...

--Mark
dlk
Reply | Threaded
Open this post in threaded view
|

Re: Popping a Constant ;^)

dlk
Lovely!