Recent

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

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
how many limitations about nested subroutine in free pascal are there?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.

rabbit_dance

  • Full Member
  • ***
  • Posts: 157

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #3 on: November 13, 2015, 03:29:46 pm »
The text says "1.Procedure or Function definitions can be nested to a level of 32", i.e. a subroutine can call another subroutine which can call another subroutine up to a depth of 32. What else do you need to know ?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #4 on: November 13, 2015, 03:50:21 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;

lagprogramming

  • Sr. Member
  • ****
  • Posts: 405
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #5 on: November 13, 2015, 04:14:42 pm »
Do you refer to something like?
Quote
"nested procedures" not yet supported inside inline procedure/function.
Inlining disabled.

I find the above hint ambiguous. It refers to something like:

Code: Pascal  [Select][+][-]
  1. procedure routine;
  2.  
  3.   procedure subroutine;inline;
  4.   begin
  5.     //Blah Blah
  6.   end;
  7.  
  8. begin
  9.   //Blah Blah
  10.   subroutine;
  11.   //Blah Blah
  12. end;

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #6 on: November 13, 2015, 04:34:03 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;
why?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #7 on: November 13, 2015, 04:41:54 pm »
why?
Because procedure a_b is a subroutine of procedure a. It's only "local" accessible within the procedure a. The same goes for variables declared in procedure a. You can't call them from outside the procedure. (And when your in the main-part of procedure Foo, you're "outside" of procedure a, so can't access local variables and procedure declared within a).

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #8 on: November 13, 2015, 05:38:48 pm »
Do you refer to something like?
Quote
"nested procedures" not yet supported inside inline procedure/function.
Inlining disabled.

I find the above hint ambiguous. It refers to something like:

Code: Pascal  [Select][+][-]
  1. procedure routine;
  2.  
  3.   procedure subroutine;inline;
  4.   begin
  5.     //Blah Blah
  6.   end;
  7.  
  8. begin
  9.   //Blah Blah
  10.   subroutine;
  11.   //Blah Blah
  12. end;

The other way around:
Code: Pascal  [Select][+][-]
  1. procedure routine;inline;
  2.  
  3.   procedure subroutine;
  4.   begin
  5. ...
  6.  

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #9 on: November 14, 2015, 06:56:24 am »
how is nested subroutines being implemented?

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #10 on: November 14, 2015, 06:58:46 am »
why?
Because procedure a_b is a subroutine of procedure a. It's only "local" accessible within the procedure a. The same goes for variables declared in procedure a. You can't call them from outside the procedure. (And when your in the main-part of procedure Foo, you're "outside" of procedure a, so can't access local variables and procedure declared within a).
and what causes the limitation occured?please interpret from the perspective of language implementation.
i have some assembly knowledge of mmix.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #11 on: November 14, 2015, 08:10:30 am »
how is nested subroutines being implemented?
...
i have some assembly knowledge of mmix.
Then you can just generate the assembly and analyze ;)
It's documented a little (well, the rest is the same as normal procedures): http://www.freepascal.org/docs-html/3.0.0/prog/progse23.html
and what causes the limitation occured?please interpret from the perspective of language implementation.
Because stackframe for a_b (which requires a's stackframe) is not yet available when you call it. There's NO guarantee that a_b won't access anything declared locally in a so if a_b does, it may refer to invalid location. Well, of course such a check can be implemented at compile time but I think the language becomes inconsistent as sometimes you can call deeply nested procedure and sometimes can't.

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #12 on: November 14, 2015, 09:43:51 am »
how is nested subroutines being implemented?
...
i have some assembly knowledge of mmix.
Then you can just generate the assembly and analyze ;)
It's documented a little (well, the rest is the same as normal procedures): http://www.freepascal.org/docs-html/3.0.0/prog/progse23.html
and what causes the limitation occured?please interpret from the perspective of language implementation.
Because stackframe for a_b (which requires a's stackframe) is not yet available when you call it. There's NO guarantee that a_b won't access anything declared locally in a so if a_b does, it may refer to invalid location. Well, of course such a check can be implemented at compile time but I think the language becomes inconsistent as sometimes you can call deeply nested procedure and sometimes can't.
all i want is the theoretical knowledge on the implementation of nested subroutine of pascal, and how to compile pascal code into mmix code.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #13 on: November 14, 2015, 02:25:00 pm »
all i want is the theoretical knowledge on the implementation of nested subroutine of pascal, and how to compile pascal code into mmix code.
Do you have knowledge about normal procedure implementation? If yes, nested procedure is the same, with the difference that there's a hidden pointer parameter pointing to parent procedure's stackframe, as explained in the docs. That's it.

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: how many limitations about nested subroutine in free pascal are there?
« Reply #14 on: November 14, 2015, 02:39:13 pm »
all i want is the theoretical knowledge on the implementation of nested subroutine of pascal, and how to compile pascal code into mmix code.
Do you have knowledge about normal procedure implementation? If yes, nested procedure is the same, with the difference that there's a hidden pointer parameter pointing to parent procedure's stackframe, as explained in the docs. That's it.
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.

 

TinyPortal © 2005-2018