Recent

Author Topic: Break and inline methods  (Read 4400 times)

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Break and inline methods
« on: August 24, 2017, 11:50:00 pm »
I know that break will exit all loops, nested or not. But what about that:
Code: Pascal  [Select][+][-]
  1. program LoopTest;
  2.  
  3. var breaked : False;
  4.  
  5. procedure MyLoop; inline;
  6. begin
  7.   repeat
  8.     if True then break;
  9.   until breaked; // for sure
  10. end;
  11.  
  12. begin
  13.   repeat
  14.     MyLoop;
  15.   until breaked; // or forever?
  16. end.
  17.  
  18.  

Be mindful and excellent with each other.
https://github.com/cpicanco/

dot.not

  • New Member
  • *
  • Posts: 22
  • The answer is 42
Re: Break and inline methods
« Reply #1 on: August 25, 2017, 12:05:36 am »
AFAIK break only breaks out of the inner most loop

the way your code is written you essentially have 2 loops, one nested inside the other like so
Code: Pascal  [Select][+][-]
  1.  
  2. program LoopTest;
  3.  
  4. var breaked : False;
  5.  
  6. begin
  7.   repeat
  8.     repeat
  9.       if True then break;
  10.     until breaked; // for sure
  11.   until breaked; // or forever?
  12. end.
  13.  

I dont see this will ever be broken
There's 10 kinds of people

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Break and inline methods
« Reply #2 on: August 25, 2017, 12:11:31 am »
So I guess I don't know.  :D

Thank you dot.not!
Be mindful and excellent with each other.
https://github.com/cpicanco/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Break and inline methods
« Reply #3 on: August 25, 2017, 12:12:30 am »
No, the outer loop is never exited since the value of breaked is never assigned True.

BTW, breaked has to be declared as a Boolean. (Also English is irregular, so the past tense of break is broken).

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Break and inline methods
« Reply #4 on: August 26, 2017, 05:32:35 pm »
Code: Pascal  [Select][+][-]
  1. program LoopTest;
  2.  
  3. var broken : Boolean = False;
  4.  
  5. procedure MyLoop; inline;
  6. begin
  7.   repeat
  8.     if True then break;
  9.   until broken; // for sure
  10. end;
  11.  
  12. begin
  13.   repeat
  14.     MyLoop;
  15.   until broken; // or forever?
  16. end.
  17.  
  18.  

Thanks howardpc! And what about  Exit and inline methods? It makes any sense exiting an inline method?
« Last Edit: August 26, 2017, 05:35:04 pm by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

Thaddy

  • Hero Member
  • *****
  • Posts: 18982
  • Glad to be alive.
Re: Break and inline methods
« Reply #5 on: August 26, 2017, 05:39:15 pm »
Thanks howardpc! And what about  Exit and inline methods? It makes any sense exiting an inline method?
An inlined method will still perform a jump instruction when exit, halt, break etc are specified. Examine assembler code. Silly question.<grumpy  >:D>
howardpc's answer is right, btw, for break only. Exit will exit a procedure or function or even a program if it is in the main program source. Exit can take a parameter, btw, just like halt, and equals result in functions.
« Last Edit: August 26, 2017, 05:43:24 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Break and inline methods
« Reply #6 on: August 26, 2017, 05:49:00 pm »
Quote
An inlined method will still perform a jump instruction when exit, halt, break etc are specified.

So the calling procedure (the one calling an inlined procedure) will be exited. That makes sense.
Be mindful and excellent with each other.
https://github.com/cpicanco/

Thaddy

  • Hero Member
  • *****
  • Posts: 18982
  • Glad to be alive.
Re: Break and inline methods
« Reply #7 on: August 26, 2017, 06:14:37 pm »
Quote
An inlined method will still perform a jump instruction when exit, halt, break etc are specified.

So the calling procedure (the one calling an inlined procedure) will be exited. That makes sense.

Note that can be dangerous and is mostly not necessary when your code is right. (the bad examples to the contrary are available too. I wrote mostly...)
You have to be very,very careful if you code like that on purpose: finalize classes etc, etc).

But I think you still do not understand that even an inlined procedure itself can use exit or break or halt. And generates a jump instruction.
« Last Edit: August 26, 2017, 06:16:33 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12303
  • Debugger - SynEdit - and more
    • wiki
Re: Break and inline methods
« Reply #8 on: August 26, 2017, 06:17:52 pm »
So the calling procedure (the one calling an inlined procedure) will be exited. That makes sense.

Nope.

Inline is an optimization. It does not change any behaviour (unless there is a bug, or you do some asm, or you hack pointers..., or try to directly access stackframes)

Adding/removing "inline" to a function, does NOT change the behaviour of your app.
It still will behave as if a function was called.

You can even still do
Code: Pascal  [Select][+][-]
  1. somevar = @function_with_inline;

In fact adding "inline" does not guarantee the function will actually be inlined. You simply tell the compiler that you would prefer it, if it is possible.

And even if a function is inlined, this may generate different code (more/slower, but sometimes also faster) than if you copied and pasted the source into the position.

"inline" is fundamentally different from a C preprocessor macro.
« Last Edit: August 26, 2017, 06:19:25 pm by Martin_fr »

Thaddy

  • Hero Member
  • *****
  • Posts: 18982
  • Glad to be alive.
Re: Break and inline methods
« Reply #9 on: August 26, 2017, 06:26:27 pm »
Martin, I think he is mixing up inline with loop unroll. But you wrote the correct additional information,of course.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Break and inline methods
« Reply #10 on: August 28, 2017, 11:15:53 pm »
Quote
Adding/removing "inline" to a function, does NOT change the behaviour of your app.

Thank you Martin, but I must say that I have a concrete example of an app that will change its behaviour if a method is inlined. Please correct me if I am wrong (note that you must accept that exception handling by the programmer is part of the app):

Code: Pascal  [Select][+][-]
  1. procedure RaiseExceptionOutsideLine; inline;
  2. procedure RaiseExceptionActualLine;

If both procedures have the same implementation:

Code: Pascal  [Select][+][-]
  1.   raise exception.create('error') at  
  2.     get_caller_addr(get_frame),  
  3.     get_caller_frame(get_frame);
  4.  

... the inlined one will show the line of the caller procedure from outside and the other one will show the line of the actual procedure.
Be mindful and excellent with each other.
https://github.com/cpicanco/

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12303
  • Debugger - SynEdit - and more
    • wiki
Re: Break and inline methods
« Reply #11 on: August 29, 2017, 12:14:44 am »
As I said, they are not the same if you "directly access stackframes".

get_caller_frame => stack frame.

And probably that also applies if you access stackframes (but not directly)

There is also https://freepascal.org/docs-html/prog/progsu78.html
If you turn stackframes off (for those functions for which you can), then they will have the same change.
« Last Edit: August 29, 2017, 12:16:53 am by Martin_fr »

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Break and inline methods
« Reply #12 on: August 29, 2017, 12:52:49 am »
Thank you martin!
Be mindful and excellent with each other.
https://github.com/cpicanco/

 

TinyPortal © 2005-2018