Page 2 of 2
Re: EndProcedure value
Posted: Sat Apr 16, 2016 5:39 am
by Danilo
Roger Hågensen wrote:Let me re-iterate. The "hidden" return will only be replaced if "EndProcedure value" is used.
All use of ProcedureReturn inside the procedure will continue to function just like they do today.
Let me simplify this further. "EndProcedure value" let you replace the xor eax,eax with a mov eax, value
That's it. That is the only thing I'm suggesting, I'm not suggesting to change ProcedureReturn at all.
With some dead-code-elimination at PB and assembly level, this could be avoided at all.
It then would be optimized automatically, without the need for syntax changes or additional syntax.
For example, all code after a major ProcedureReturn statement is dead code and can be removed
by the compiler - if control flow does not jump to a label after the ProcedureReturn statement.
Likewise, at assembly level, dead code can be eliminated if never executed and not reachable by control flow -
but all that would probably be easier with a second pass over the generated ASM.
A more simple approach:
If the last statement in a procedure is ProcedureReturn (and that's often the case),
it is not required to generate code for returning the default 0-value.
'EndProcedure Value' wouldn't be required. It is just a small optimization to the current system.
According to my understanding, you just want some more optimizations for generated procedure code.
Re: EndProcedure value
Posted: Thu Apr 21, 2016 8:29 pm
by uwekel
+1
Nice idea! Would save me more than hundred lines of code! I am sure the discussion in this thread took more time than implementing this feature by the PB team

Re: EndProcedure value
Posted: Fri Apr 22, 2016 11:21 am
by Dude
Danilo wrote:all code after a major ProcedureReturn statement is dead code and can be removed
Say what? What about times when you have multiple ProcedureReturns depending on a decision?
Re: EndProcedure value
Posted: Fri Apr 22, 2016 11:38 am
by Little John
Danilo wrote:With some dead-code-elimination at PB and assembly level, this could be avoided at all.
It then would be optimized automatically, without the need for syntax changes or additional syntax.
Exactly.
Re: EndProcedure value
Posted: Fri Apr 22, 2016 12:22 pm
by wilbert
Little John wrote:Danilo wrote:With some dead-code-elimination at PB and assembly level, this could be avoided at all.
It then would be optimized automatically, without the need for syntax changes or additional syntax.
Exactly.
Seems hard to implement.
I have sometimes procedures like this.
Code: Select all
Procedure Test()
!mov eax, [l_test]
ProcedureReturn
!l_test: dd 5
EndProcedure
Debug Test()
How would PB detect it has to stay in ?
Re: EndProcedure value
Posted: Fri Apr 22, 2016 1:17 pm
by Little John
Hi wilbert,
I was referring to assembler code that has been produced by PB itself from plain PB code entered by the programmer.
As far as I understand, lines which start with ! are not touched by PB at all, but are just passed to the assembler. I think that should not be changed.
Re: EndProcedure value
Posted: Fri Apr 22, 2016 6:30 pm
by dobro
Bo Marchais wrote:
a,b,c = someprocedure(foo)
Not possible in purebasic, right?
maybe :
Code: Select all
Declare foo(in)
foo(2)
debug a
debug b
debug c
Procedure foo(in)
shared a,b,c
a=in*2
b=in*4
c=in*8
EndProcedure
Re: EndProcedure value
Posted: Fri Apr 22, 2016 9:38 pm
by StarBootics
Bo Marchais wrote:a,b,c = someprocedure(foo)
Not possible in purebasic, right?
You are right it's possible in MatLAB or FreeMat. By the way my favorite way to handle multiple return :
Code: Select all
Declare foo(in, *OutA, *OutB, *OutC)
foo(2, @a.l, @b.l, @c.l)
Debug a
Debug b
Debug c
Procedure foo(in, *OutA, *OutB, *OutC)
PokeL(*OutA, in*2)
PokeL(*OutB, in*4)
PokeL(*OutC, in*8)
EndProcedure
Best regards
StarBootics
Re: EndProcedure value
Posted: Sat Apr 23, 2016 1:17 am
by Bo Marchais
Very handy!
Re: EndProcedure value
Posted: Sat Apr 23, 2016 6:52 am
by TI-994A
Bo Marchais wrote:a,b,c = someprocedure(foo)
Not possible in purebasic, right?
Using a non-global array, and without passing the array to the procedure:
Code: Select all
Procedure someprocedure(foo, *bar)
For i = 1 To 3
PokeI(*bar + offset, i * foo)
offset + SizeOf(Integer)
Next i
EndProcedure
Dim a(2)
someprocedure(123, a())
Debug a(0)
Debug a(1)
Debug a(2)
Or, if your feeling especially adventurous:
Code: Select all
;#### WORKS BUT NOT RECOMMENDED! ####
;assumes contiguous memory allocation
Procedure someprocedure(foo, *bar)
For i = 1 To 3
PokeI(*bar + offset, i * foo)
offset + SizeOf(Integer)
Next i
EndProcedure
Define.i a, b, c
someprocedure(123, @a)
Debug a
Debug b
Debug c

Re: EndProcedure value
Posted: Sat Apr 23, 2016 8:38 am
by dobro
with String
Code: Select all
Declare.s foo()
o.s=foo()
For i=1 to 3
debug StringField(o.s,i,"," )
Next i
Procedure.s foo()
;Huey, Dewey and Louie
a.s="Huey"
b.s="Dewey"
c.s="Louie"
out$=a.s+","+b.s+","+c.s ; group
ProcedureReturn out$
EndProcedure
; Epb
or
Code: Select all
Declare.s foo()
foo()
debug a.s
debug b.s
debug c.s
Procedure.s foo()
shared a.s,b.s,c.s
a.s="Huey"
b.s="Dewey"
c.s="Louie"
EndProcedure
; Epb
Re: EndProcedure value
Posted: Sat Apr 23, 2016 11:14 am
by wilbert
dobro wrote:with String
Another way
Code: Select all
Procedure MulDiv(a, b, *mul.Integer, *div.Integer)
*mul\i = a * b
If b
*div\i = a / b
EndIf
EndProcedure
Procedure MulDivS(a, b, *mul.String, *div.String)
*mul\s = Str(a * b)
If b
*div\s = Str(a / b)
EndIf
EndProcedure
MulDiv(20, 5, @a, @b)
MulDivS(16, 2, @c.String, @d.String)
Debug a
Debug b
Debug c\s
Debug d\s
Re: EndProcedure value
Posted: Mon Apr 25, 2016 9:14 pm
by Lunasole
DontTalkToMe wrote:
Then it is outside. Procedure / EndProcedure it's a code block, so after EndProcedure it would be outside.
It is not "outside". There already is For/Next code block and Next can take argument (for example).
I like any syntax improvements making code shorter, more usable, less ugly and raw/"looking like a .bat file" without loss of readability and ability to parse it easily with some script (for case you need to translate it to other language). This idea match all criterias, so might be nice.
Re: EndProcedure value
Posted: Mon Apr 25, 2016 9:31 pm
by DontTalkToMe
Lunasole wrote:
It is not "outside". There already is For/Next code block and Next can take argument (for example).
LOL
DontTalkToMe wrote:
OK, I get your point above, Still I wouldn't put a clear-cut code unit like procedure-endprocedure on the same plane of repeat-until or for-next.
You can even write for i - next i (so i is after the next).
Re: EndProcedure value
Posted: Mon Apr 25, 2016 10:27 pm
by Lunasole
@DontTalkToMe, hah, It happens. It is hard for me to read whole threads in english (takes much more time than native langs) this time posted without reading and missed, sorry