Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

VM: How to determine generator's current stack top?

Last post 09-28-2008, 10:59 PM by Mr. Accident. 3 replies.
Sort Posts: Previous Next
  •  09-27-2008, 1:28 AM 2792

    VM: How to determine generator's current stack top?

    This is a question mainly for Alberto, or anybody else who happens to know the insides of the VM fairly well.

    Say I have a suspended SQGenerator. I can access its stack (SQGenerator::_stack) and its call frame (SQGenerator::_ci), and I assume the information must be in there somewhere, but I'm not quite sure how to determine the current top of the generator's stack.

    For example, if the generator yielded immediately after calling a function, its stack vector still contains the return value and the parameters to the function call, even though these are no longer really part of the "stack." I'd like to be able to identify exactly where the relevant part of the generator's stack ends and where the extraneous "leftover" values begin.

    Take the following generator:

    function my_gen() {
        local count = 0
        while(true) {
            print("count: " + count++)
            yield
        }
    }


    After yielding, the generator's stack looks like this:

      0: <this>
      1: <count> = 1
      2: null (return value from print)    <-- everything from this point on is extraneous
      3: <this>  (first parameter to print)
      4: "count: 0"
      5: "count: 0"


    I'd like to be able to identify the "actual" top of the generator's stack at that point (which should be 2), but none of the available information in the generator and its callframe seems to be what I'm looking for. As near as I can tell, the generator stack vector is just copied in total onto the VM's stack when it resumes, and copied back when it yields, with the actual usage of stack positions determined at compile time.

    Is there any way to identify this position without changing the Squirrel VM to keep track of it explicitly?
  •  09-27-2008, 8:28 PM 2795 in reply to 2792

    Re: VM: How to determine generator's current stack top?

    Well, I managed to get the information without too much trouble, but I had to modify the compiler to do it. Basically now when the compiler processes a "yield" statement it puts the current top target in one of the yield op's unused arguments. I can then find the top of a generator's stack by looking at the opcode before the IP (assuming the generator has resumed at least once).

    This is obviously a hack, so I'm curious if there might be a better way to do it, ideally without having to modify the VM or the compiler. I suppose examining the whole bytecode for the function would be one option, but that's a lot more work. :-)
  •  09-28-2008, 5:51 AM 2797 in reply to 2795

    Re: VM: How to determine generator's current stack top?

    I'm not really sure what you are tryeing to archieve. Why do you need to know where the top is? Also consider that in squirrel 'top' is the end of the function frame, so is always _stackbase + framesize. This because squirrel is not a stack machine, and the top is only used when invoking a metamethod.

    Alberto

  •  09-28-2008, 10:59 PM 2801 in reply to 2797

    Re: VM: How to determine generator's current stack top?

    It came up when I was trying to serialize generators. I only want to serialize the parts of the stack that are still accessible, i.e., still "on" the semantic stack. Basically this amounts to serializing only the parameters and the currently "in scope" local variables.

    And of course I feel silly now, as I have since realized that I can just use SQFunctionProto::_localvarinfos to get that information.  :-P
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems