Recent

Author Topic: how many limitations about nested subroutine in free pascal are there?  (Read 12266 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #15 on: November 14, 2015, 02:48:44 pm »
but i have read some webpages about programming languages, it says nested subroutine's implementation dependent on static links.
http://www.zhihu.com/question/31208722/answer/51050003?group_id=648099945756938240#comment-103311438
i dont't know there is people who are in the forum can read the link.

The idea is to keep a linked list going. So a 5th level function has a framepointer to its 4th lvl parent, which has a link to its 3rd lvl parent etc.  This builds a linked list so that all functions can access the parents parameter list.

If a 4th lvl procedure calls itself or another 4th lvl procedure it still needs to pass on the 3rd lvl frame.

When a function pointer is taken, you pass around either the procedure address and the frame it expects (3rd for 4th level procedure) or create a trampoline that holds the framepointer and procedure address and pass the address of the trampoline on.

The principle is fairly unbounded, but when deeply nested it needs to follow several indirections, so performance is not always ideal.

But since the whole codeblock is within one compilation unit, as long as no function pointers are used, an advanced compiler can optimize use various schemes depending on the pattern found. (e.g. always passing the top level parent framepointer if that is the only frame that is used in subroutines (iow if the 3th level doesn't use 2nd level parent's variables)

« Last Edit: November 14, 2015, 03:02:03 pm by marcov »

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #16 on: November 14, 2015, 02:50:30 pm »
thanks.

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #17 on: November 14, 2015, 03:17:49 pm »
Of course a subroutine can't call a nested>nested subroutine. It needs to be in the same scope the be able to call that routine.
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2.  
  3.   procedure a;
  4.  
  5.     procedure a_b;
  6.     begin
  7.       Foo; // can call Foo again (but watch out for recursion)
  8.     end;
  9.  
  10.   begin
  11.     a_b; //can call a_b
  12.   end;
  13.  
  14. begin
  15.   a_b; // ERROR, can't call a_b
  16.   a; // can call a
  17. end;
i still have a question. could this limitation could resolved? if not, why?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #18 on: November 14, 2015, 03:48:48 pm »
i still have a question. could this limitation could resolved? if not, why?
Yes, this limitation could be resolved. You can use the nestedprocvars-modeswitch. It will enable you to assign a nested subroutine to a procedure-variable which you can call later. (use with care because of all the reason mentioned earlier) Also see http://www.freepascal.org/docs-html/ref/refse17.html

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$modeswitch nestedprocvars}
  4. type proc = procedure is nested;
  5.  
  6. var my_direct_a_b_call: proc = nil;
  7.  
  8.   procedure Foo;
  9.  
  10.     procedure a;
  11.     begin
  12.       writeln('hello');
  13.     end;
  14.  
  15.   begin
  16.     my_direct_a_b_call := @a;
  17.   end;
  18.  
  19. begin
  20.   Foo; // this assigns my_direct_a_b_call
  21.   my_direct_a_b_call;
  22.   readln;
  23. end.
« Last Edit: November 14, 2015, 03:51:00 pm by rvk »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #19 on: November 14, 2015, 03:55:53 pm »
i still have a question. could this limitation could resolved? if not, why?

No, since you don't have a 2nd lvl frame of procedure a to call 3rd level procedure a_b from first level Foo.

Since nested procedure variables are only legal when the frames of the relevant procedures exist, this offers no escape, since at the place where you can't call a_b, the procvar wouldn't be legal too.

lazjump

  • Jr. Member
  • **
  • Posts: 61
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #20 on: November 14, 2015, 04:00:17 pm »
i still have a question. could this limitation could resolved? if not, why?

I wonder if there is any practical value of "resolving the limitation". Or perhaps the reason is academical?
I thought Delphi was expensive until I learned the price of ExtJS

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #21 on: March 27, 2017, 07:23:40 pm »
is free pascal implemented nested subroutine by using static links?

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #22 on: March 29, 2017, 04:29:45 pm »
I think people are talking about two different things in this thread so I'll throw in my 2 cents as usual:
'Procedure or Function definitions can be nested to a level of 32'

That means you can write a procedure or function nested up to 32 levels. Nothing to do with calling. It talks about definition. It's defined at compile time, so to overcome that limit you must recompile FPC.

About calling routines, as long as they are on scope (which usually is not the case for deeply nested routines) there's no problem. About recursion, well I've not hit any limit even when I'm looking at the screen wondering why my program doesn't do anything when in fact it's in an infinite calling recursion loop due to a coding mistake, so for the levels there, as long as you can keep creating stack frames there's no problem.

(Slightly off-topic: I had some writings about how to convert several kinds of recursive algorithms into iterative algorithms, does anyone know a good how-to for Objet Pascal?)
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

Thaddy

  • Hero Member
  • *****
  • Posts: 14161
  • Probably until I exterminate Putin.
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #23 on: March 29, 2017, 06:29:20 pm »
Actually that depth is hard-coded and has comments in the sources. Feel free to compile the compiler with a larger  depth settings and make sure it works for your platform, but don't submit a patch..Ever.
It is more or less a catch all, empirically determined value.
Specialize a type, not a var.

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #24 on: March 30, 2017, 11:06:17 am »
I think people are talking about two different things in this thread so
Thank you, jmm72, for differentiating between these things.

[…] but don't submit a patch..Ever.
It is more or less a catch all, empirically determined value.
I empirically determined 42 as the new limit to be.  :D
Yours Sincerely
Kai Burghardt

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #25 on: March 30, 2017, 11:26:37 am »
Actually that depth is hard-coded and has comments in the sources. Feel free to compile the compiler with a larger  depth settings and make sure it works for your platform, but don't submit a patch..Ever.
It is more or less a catch all, empirically determined value.

32 is the limit of the enter instructions iirc. But that is no longer used.

 

TinyPortal © 2005-2018