stack questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DC

    #1

    stack questions

    Hi all,

    First of all, let me asure you you will not be doing my homework for me if
    you are good enough to reply - I have been a professional programmer for a
    couple of years now, I just never studied Computer Science so I like to try
    and 'catch up' on stuff like this at the weekend.

    Having said that I have a couple of questions around the excution stack, in
    particular 2 things are bothing me:

    1. It is said that on encountering an exception, the execution engine
    "unwinds" the call stack looking for a suitable exception handler. In the
    example below can we assume that the address of the error handling code is
    pushed onto the stack as the return address before executing the try block
    and that, infact, the code within the try block is in itself executed as a
    function call within it's own stack frame? This can't be the answer though
    as the catch block only executes where the stack is unwinding 'in error' not
    simply in unwinding during normal flow. So what is really going on??

    try
    {
    //some work
    }
    catch
    {
    //handling code
    }

    2. Continuations in C# - are they really unwinding and rewinding the stack -
    or is there some sort of complirer magic going on that creates an invisible
    state machine?


    Love to know your thoughts on the above.
    Cheers


  • Bruno Jouhier

    #2
    Re: stack questions

    I don't know exactly how Microsoft implements the throw/catch logic, but a
    classical way to do it is to save the value of the registers just before the
    try block and to implement throw by restoring the registers.

    In C, this can be done with setjmp and longjmp. See
    http://www.di.unipi.it/~nids/docs/lo...ow_catch.shtml for details.

    Bruno

    Of course, you need some logic to chain the areas where you save the
    registers, so that the throw can find the last

    "DC" <***@***.coma écrit dans le message de news:
    e8Ym2Wc9GHA.408 4@TK2MSFTNGP05. phx.gbl...
    Hi all,
    >
    First of all, let me asure you you will not be doing my homework for me if
    you are good enough to reply - I have been a professional programmer for a
    couple of years now, I just never studied Computer Science so I like to
    try and 'catch up' on stuff like this at the weekend.
    >
    Having said that I have a couple of questions around the excution stack,
    in particular 2 things are bothing me:
    >
    1. It is said that on encountering an exception, the execution engine
    "unwinds" the call stack looking for a suitable exception handler. In the
    example below can we assume that the address of the error handling code is
    pushed onto the stack as the return address before executing the try block
    and that, infact, the code within the try block is in itself executed as a
    function call within it's own stack frame? This can't be the answer though
    as the catch block only executes where the stack is unwinding 'in error'
    not simply in unwinding during normal flow. So what is really going on??
    >
    try
    {
    //some work
    }
    catch
    {
    //handling code
    }
    >
    2. Continuations in C# - are they really unwinding and rewinding the
    stack - or is there some sort of complirer magic going on that creates an
    invisible state machine?
    >
    >
    Love to know your thoughts on the above.
    Cheers
    >

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: stack questions

      DC <***@***.comwro te:
      First of all, let me asure you you will not be doing my homework for me if
      you are good enough to reply - I have been a professional programmer for a
      couple of years now, I just never studied Computer Science so I like to try
      and 'catch up' on stuff like this at the weekend.
      >
      Having said that I have a couple of questions around the excution stack, in
      particular 2 things are bothing me:
      >
      1. It is said that on encountering an exception, the execution engine
      "unwinds" the call stack looking for a suitable exception handler. In the
      example below can we assume that the address of the error handling code is
      pushed onto the stack as the return address before executing the try block
      and that, infact, the code within the try block is in itself executed as a
      function call within it's own stack frame? This can't be the answer though
      as the catch block only executes where the stack is unwinding 'in error' not
      simply in unwinding during normal flow. So what is really going on??
      I believe that when the exception is thrown, the JIT knows about
      exception handlers within each stack frame - it doesn't necessarily
      need to unwind the stack, as you've pointed out. The basic principle is
      that execution jumps to the innermost exception handler which is
      capable of handling that exception type.
      2. Continuations in C# - are they really unwinding and rewinding the stack -
      or is there some sort of complirer magic going on that creates an invisible
      state machine?
      I assume you mean the "yield" statement? It's creating a state machine.
      If you decompile the generated code, it can be very interesting to look
      through.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • DC

        #4
        Re: stack questions

        My thanks to both who replied.

        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:MPG.1fa5d2 cae14e7c7a98d56 e@msnews.micros oft.com...
        DC <***@***.comwro te:
        >First of all, let me asure you you will not be doing my homework for me
        >if
        >you are good enough to reply - I have been a professional programmer for
        >a
        >couple of years now, I just never studied Computer Science so I like to
        >try
        >and 'catch up' on stuff like this at the weekend.
        >>
        >Having said that I have a couple of questions around the excution stack,
        >in
        >particular 2 things are bothing me:
        >>
        >1. It is said that on encountering an exception, the execution engine
        >"unwinds" the call stack looking for a suitable exception handler. In the
        >example below can we assume that the address of the error handling code
        >is
        >pushed onto the stack as the return address before executing the try
        >block
        >and that, infact, the code within the try block is in itself executed as
        >a
        >function call within it's own stack frame? This can't be the answer
        >though
        >as the catch block only executes where the stack is unwinding 'in error'
        >not
        >simply in unwinding during normal flow. So what is really going on??
        >
        I believe that when the exception is thrown, the JIT knows about
        exception handlers within each stack frame - it doesn't necessarily
        need to unwind the stack, as you've pointed out. The basic principle is
        that execution jumps to the innermost exception handler which is
        capable of handling that exception type.
        >
        >2. Continuations in C# - are they really unwinding and rewinding the
        >stack -
        >or is there some sort of complirer magic going on that creates an
        >invisible
        >state machine?
        >
        I assume you mean the "yield" statement? It's creating a state machine.
        If you decompile the generated code, it can be very interesting to look
        through.
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • DC

          #5
          Re: stack questions


          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:MPG.1fa5d2 cae14e7c7a98d56 e@msnews.micros oft.com...
          DC <***@***.comwro te:
          >First of all, let me asure you you will not be doing my homework for me
          >if
          >you are good enough to reply - I have been a professional programmer for
          >a
          >couple of years now, I just never studied Computer Science so I like to
          >try
          >and 'catch up' on stuff like this at the weekend.
          >>
          >Having said that I have a couple of questions around the excution stack,
          >in
          >particular 2 things are bothing me:
          >>
          >1. It is said that on encountering an exception, the execution engine
          >"unwinds" the call stack looking for a suitable exception handler. In the
          >example below can we assume that the address of the error handling code
          >is
          >pushed onto the stack as the return address before executing the try
          >block
          >and that, infact, the code within the try block is in itself executed as
          >a
          >function call within it's own stack frame? This can't be the answer
          >though
          >as the catch block only executes where the stack is unwinding 'in error'
          >not
          >simply in unwinding during normal flow. So what is really going on??
          >
          I believe that when the exception is thrown, the JIT knows about
          exception handlers within each stack frame - it doesn't necessarily
          need to unwind the stack, as you've pointed out. The basic principle is
          that execution jumps to the innermost exception handler which is
          capable of handling that exception type.
          >
          >2. Continuations in C# - are they really unwinding and rewinding the
          >stack -
          >or is there some sort of complirer magic going on that creates an
          >invisible
          >state machine?
          >
          I assume you mean the "yield" statement? It's creating a state machine.
          If you decompile the generated code, it can be very interesting to look
          through.
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Willy Denoyette [MVP]

            #6
            Re: stack questions


            "DC" <***@***.comwro te in message
            news:e8Ym2Wc9GH A.4084@TK2MSFTN GP05.phx.gbl...
            | Hi all,
            |
            | First of all, let me asure you you will not be doing my homework for me if
            | you are good enough to reply - I have been a professional programmer for a
            | couple of years now, I just never studied Computer Science so I like to
            try
            | and 'catch up' on stuff like this at the weekend.
            |
            | Having said that I have a couple of questions around the excution stack,
            in
            | particular 2 things are bothing me:
            |
            | 1. It is said that on encountering an exception, the execution engine
            | "unwinds" the call stack looking for a suitable exception handler. In the
            | example below can we assume that the address of the error handling code is
            | pushed onto the stack as the return address before executing the try block
            | and that, infact, the code within the try block is in itself executed as a
            | function call within it's own stack frame? This can't be the answer though
            | as the catch block only executes where the stack is unwinding 'in error'
            not
            | simply in unwinding during normal flow. So what is really going on??
            |
            | try
            | {
            | //some work
            | }
            | catch
            | {
            | //handling code
            | }
            |
            A two phase exception handling model is employed by the CLR, and CLR
            exceptions are implemented on top of the Windows SEH mechanism (as are the
            C++ exceptions).
            When an exception is thrown, the CLR initiates the first phase which consist
            of a stack walk to search for an appropriate exception handler (the one
            who's filter is able to deal with the exception!), in the second phase the
            stack is unwound until the frame containing the handler is reached. Note
            that unwinding the stack, causes the finally blocks on the unwounded frames
            to be executed.
            More gory detail, can be found here:
            http://blogs.msdn.com/cbrumme/archiv.../01/51524.aspx



            Willy.


            Comment

            Working...