EndProcedure value

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: EndProcedure value

Post 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.
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: EndProcedure value

Post 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 :?
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: EndProcedure value

Post 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?
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: EndProcedure value

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: EndProcedure value

Post 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 ?
Windows (x64)
Raspberry Pi OS (Arm64)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: EndProcedure value

Post 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.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: EndProcedure value

Post 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




Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: EndProcedure value

Post 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
The Stone Age did not end due to a shortage of stones !
Bo Marchais
User
User
Posts: 61
Joined: Sun Apr 03, 2016 12:03 am

Re: EndProcedure value

Post by Bo Marchais »

Very handy!
User avatar
TI-994A
Addict
Addict
Posts: 2704
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: EndProcedure value

Post 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
:lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: EndProcedure value

Post 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

Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: EndProcedure value

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: EndProcedure value

Post 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.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: EndProcedure value

Post by DontTalkToMe »

Lunasole wrote: It is not "outside". There already is For/Next code block and Next can take argument (for example).
LOL :D
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).
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: EndProcedure value

Post 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
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply