Arduino Forum

Using Arduino => Programming Questions => Topic started by: Begraphics on Mar 01, 2016, 11:45 am

Title: Using goto instead void calls.
Post by: Begraphics on Mar 01, 2016, 11:45 am
I am thinking this method to avoid a lot void calls that results variables must be saved outside void setup, void loop, and other calls functions which leads bigger program size.

F.ex:
loop()
{
  // code here
 
  "call" a function inside loop instead void call(); but with number variable to return to correct
  position with if else if as void function would do.
  code with if else if to using goto to go to correct position with value before function.
<- goto pass_fake_void_function;
|     trigonometry:
|     Doing calculation square root
|     // square root calculation
|     // then
|     if ( value == 1 ) { goto value1; } else if ( value == 2 ) { goto value2; }
|   }
|   value1: ;
-> pass_fake_void_function: ;
     //loop go as normal....
    if several function call
    value = 2;
    goto trigonometry;
    value2: ;

Do you understood?
Title: Re: Using goto instead void calls.
Post by: Whandall on Mar 01, 2016, 11:54 am
Did you understand the C-language switch construct?
Why do you want to develop a goto based replacement?

BTW: Using functions (if not used for clarity only) most often leads to smaller code.
Title: Re: Using goto instead void calls.
Post by: Begraphics on Mar 01, 2016, 12:04 pm
You asking me this question, then prove me an example of switch construct.
You did not mention where to put switchCase.
Title: Re: Using goto instead void calls.
Post by: Whandall on Mar 01, 2016, 12:07 pm
Let's start with a compiling example of your version.
Title: Re: Using goto instead void calls.
Post by: Begraphics on Mar 01, 2016, 12:10 pm
OK, I will try.
Title: Re: Using goto instead void calls.
Post by: aarg on Mar 01, 2016, 12:13 pm
In effect, you have created an indexed return stack. It is hampered by the need to associate multiple return points with arbitrary numbers. It has the overhead of having to test the numbers, to match the correct return point. This cancels out any gain that you might have made from not having to place return addresses on the stack and pull them off (which is automatic on most processors). Note that you are not required to pass and return parameters from a function. You can use global variables. It is just convenient and aligns well with a lot of fundamental design approaches, to pass and return parameters..

Code: [Select]
value = 2;
    goto trigonometry;
    value2: ;

is like the processor level actions that go with a subroutine call:
Code: [Select]

save return address
load the sub address into the program counter

but the processor does that faster than your code.
Close, but no cigar.
Title: Re: Using goto instead void calls.
Post by: Robin2 on Mar 01, 2016, 12:22 pm
to avoid a lot void calls that results variables must be saved outside void setup, void loop, and other calls functions which leads bigger program size.
The size of storage space for variables is unlikely to be reduced by the use of GOTO whereas the tangles in your code will grow exponentially.

Just use functions.

...R
Title: Re: Using goto instead void calls.
Post by: Budvar10 on Mar 01, 2016, 12:35 pm
It is able to define the function as naked ( __attribute__ ((naked)) ) in avr-gcc. Compiler doesn't provide any prolog and epilog for such function - save/restore the registers. In C it is still function, in result it is replaced by jump.
Maybe this helps.
Title: Re: Using goto instead void calls.
Post by: aarg on Mar 01, 2016, 01:09 pm
It is able to define the function as naked ( __attribute__ ((naked)) ) in avr-gcc. Compiler doesn't provide any prolog and epilog for such function - save/restore the registers. In C it is still function, in result it is replaced by jump.
Maybe this helps.

I doubt it. A jump would be the result of a goto.

Also, I wonder if the optimizer doesn't already remove all the overhead automatically when a function is a void f(void);
Title: Re: Using goto instead void calls.
Post by: Begraphics on Mar 01, 2016, 03:09 pm
I doubt it. A jump would be the result of a goto.

Also, I wonder if the optimizer doesn't already remove all the overhead automatically when a function is a void f(void);
Yes, I wondering that too about the optimizer.
That's the reason I started this topic.