I have a function idea mark :o)

BlitzMax Forums/BlitzMax Programming/I have a function idea mark :o)

Shagwana(Posted 2004) [#1]
How about the ability to state that a prameter in a function must be a constant. For example;

Function Test(Const num:Int)
  Print num
  End Function

Doing so could help blitzers produce even faster executing functions. Via having the compiler hard code those values into the program. From a compile sense, everytime a function is found with a different const value, then a new set of the code is created reflecting the change.

Anyway, just a brain wave I had, dont know if its even possible or desirable!. Made sense at the time, but could be the drink talking to me. Merry xmas btw :)


Kanati(Posted 2004) [#2]
You are cut off... No more eggnog for you! :)


Shagwana(Posted 2004) [#3]
So is there something flawed in my logic there?


Bot Builder(Posted 2004) [#4]
Makes since to me. howbout if the the parameter is constant it'll hardcode it, otherwise, just act like a normal function.

You might want to simply support inline functions and then these semi-inline functions.


Dreamora(Posted 2004) [#5]
an idea like that can only come from C++ ... all others always bang their had about this const stuff as an "intelligent" compilers / precompilers would do stuff like this on their own ;)


Shagwana(Posted 2004) [#6]
Well just did a little test with the compiler...

Make "N" a varible or a constant

N:Int=10  'Make this a const or not!

Function Test(A:Int) 
  Return (A+A)
  End function
C=Test(N)
Print C
End

The function in both versions is generated as ...
... asm ...
_bb_Test:
push	ebp
mov	ebp,esp
mov	eax,dword [ebp+8]
add	eax,eax
jmp	_6
...


Leads me to conclude that Consts as prameters at this moment in time are not hard coded!

Instead that could be generated into something like ..
... asm ...
_bb_Test__A_INT_20:         <- ? Special const version ? :)
push	ebp
mov	eax,dword [ebp+8]
mov	eax,20             <- ? something like this ? <-
jmp	_6
...


EDIT: Better still, in this extreme example - the compiler could remove the whole of the call to that functions and just stick 20 where the result should go!



Another idea, this time for the webmaster ...
Hows about a [code =asm] forum tag that gets boxed in a different colour!


Floyd(Posted 2004) [#7]
If a value is constant then you don't want to pass it at all. It should simply be a constant whose scope is the function.
Const n = 1

Print n
f

Function f()
	Const n = 2
	Print n
End Function



Dreamora(Posted 2004) [#8]
What he means is that only constant values are allowed.
In such a case the compiler can replace the whole function call etc with the result of the calculation which can save quite some time in runtime afterwards.
But I think only C++ is in the situation where this is needed ... a real precompiler / compiler would check for const input and just replace the function call with the result of the calculation with the constant (as it is a constant as well).


Shagwana(Posted 2004) [#9]
Floyd, thats more or less what it will do, however say you want to change that const to the value 4, 6 and even 1024 later in the programs execution. You would have to code for yourself a new function d(),e() and g() that would do that for yourself. What I would propose would be have the compiler do that sort of work for us!.

In cold light of the morning after the drink, it looks a sound idea, but dont know how usefull it would turn out to be.


Kanati(Posted 2004) [#10]
but... if you change it from 4... to 6... to 1024... it's not much of a constant now is it? Sounds pretty variable to me. :)


AntonyWells(Posted 2004) [#11]
It's constantly changing. (I'll get me (thud) )


Shagwana(Posted 2004) [#12]
Well, nothing ventured, nothing gained.

Gota say i love being able to look at the asm output from the compiler, its dead neat :-)


Curtastic(Posted 2004) [#13]
A reason to have this is:
When coding inside of a function, you are usually not going to change the values of the function's parameters. So it might as well make an error if you try to.

N:Int=10  'this is not a const

Function Test(CONST A:Int)
  'A=A+1 'ERROR trying to change a const paramater!!!
  Return (A+A)
End function

print Test(N)
print Test(6) 'this is constant and also works
print Test(var_that_is_defferent_and_NOT_a_const)

End


It has nothing to do with what you pass, it just makes your coding more stable and nice. (the code inside the function)


Shagwana(Posted 2004) [#14]
I think you got the wrong end of my stick coorrae! :o)

What you got there is read only varibles (by the look of it)


Curtastic(Posted 2004) [#15]
well in c++, if you have Test(const int num) then num is read only, so that would be what I meant


jhague(Posted 2004) [#16]
Note that having multiple versions of the same function, each with certain hardcoded values, could potentially be *slower* than having one generic function, because of cache effects. Things aren't like they were back in the C64 days (or even the early Pentium days), where fewer instructions almost always resulted in faster execution time.

BTW, if you look at the generated x86 code, you'll see it, uh, has quite a lot of potential for improvement :) I'm sure work on the main code generators are going to buy a lot more than your const idea, at least right now.