BlitzMax Preprocessor

BlitzMax Forums/BlitzMax Programming/BlitzMax Preprocessor

Czar Flavius(Posted 2011) [#1]
Has anybody made or used one of these?

I'm interested in making a preprocessor to add some optimizations to my code. The big problem with a preprocessor is debugging, as the visible code might be different, but I think this can be solved by only apply it to release code which can't be debugged.

You could use a preprocessor to inline/macro functions like this

Function Add(one:Int, two:Int) Inline
	Return one + two
End Function

Local sum:Int = Add(5, 7)


A preprocessor could convert this code to the following

Local sum:Int
If True ' to maintain scope
	Local Add_one:Int = 5
	Local Add_two:Int = 7
	Local Ret = Add_one + Add_two
	sum = Ret
End If


Which removes the overhead of calling a function. I don't know of the regular Blitz compiler can optimize away the locals or not, but a smarter preprocessor could convert just to the following in this case

Local sum:Int = 5 + 7


I'm aware of "don't optimize unless you need to". I need to optimize some parts of my game. The biggest benefit would be to inline methods, which are slower than functions due to virtual-lookup even when it is not required (I think). If this data type is used frequently, there can be signifiant improvements to speed by inlining these methods.

Type TData
	Field data:Int
	
	Method GetData:Int() Final
		return data
	End Method
	
	Method SetData(value:Int) Final
		data = value
	End Method
End Type

Local data:TData = New TData
data.SetData(5)
Print data.GetData()


Can become

Local data:TData = New TData
data.data = 5
Print data.data



ImaginaryHuman(Posted 2011) [#2]
It's probably a good idea.. for example you can look at when global variables are being unnecessarily used or created inside loops and instead move them out of the loop automatically as locals.


Czar Flavius(Posted 2011) [#3]
That's a good idea. If you are using a field in a method a lot, it can also speed things up if you store the field value into a local variable, and copy it back into the field at the end of the function if required. Does anyone know if Blitz makes any optimizations like this already? I don't want to spend time unnecessarily.


AdamRedwoods(Posted 2011) [#4]
I would love BlitzMax to add an inline.
Surprised Mr. S never implemented one.


GW(Posted 2011) [#5]
Conditional compilation would go a long way towards moving bmax into adulthood.


Czar Flavius(Posted 2011) [#6]
We could add all kinds of extentions to the language using a preprocessor, for example:

Type vec
	Field x, y
	
	Method something:Int()
		Return x * y
	End Method
End Type

Local v:vec = New vec
Using v
	x = 5
	y = 6
	Print something()
End Using

'convert to this

Local v:vec = New vec
If True
	v.x = 5
	v.y = 6
	Print v.something()
End If


The If True trickery is used both to gain a scope and also to keep everything on the same line, so this change could be made in a debug build. It would report each function call and assignment on the same line then.


Basicprogger(Posted 2011) [#7]
Such a project already exists:
http://code.google.com/p/bb-bmax-precompiler/

Currently it is only being used as a profiler to analyze the execution time of all BMax functions:
http://blitzprog.org/quickupload/Lyphia.profiler.html

You might want to add the inlining feature to this project - unless you want to start from scratch.
I haven't worked on it for a long time (Oct 2009) as I didn't really plan to use it for anything else but profiling.

Last edited 2011

Last edited 2011


GW(Posted 2011) [#8]
Such a project already exists...

That pre-processor/profiler is pretty impressive. But it's also broken and generates invalid code on anything but trivial bmax programs.

Last edited 2011


Czar Flavius(Posted 2011) [#9]
Basicprogger, that looks interesting thanks for sharing! How do you execute the preprocessor? Do you use Blide's system?

I might want to reuse your infrastructure (parsing etc) though I think I wouldn't include the profilling aspect as the goals of our two projects are different.


Basicprogger(Posted 2011) [#10]
I didn't use "BlIDE's system" (don't know what that is), it's a parser written in Python.
I won't answer your question though as everything is described on the project page ("Installation" and "Configuration"). If you had a look at the configuration page you'd notice the profiler can be disabled.

Sadly, according to GW it still contains some bugs and I don't have any time to fix them. Maybe someone else might try to fix it as it's open source?

I successfully ran the profiler on a project with 16k lines of code 1-2 years ago but I guess there are a few little bugs now which could be fixed by changing 2-3 lines in the code.


ImaginaryHuman(Posted 2011) [#11]
One thing you could really make amazing for Blitz is to add MACROS so that you can define blocks of code and then reference them by a simple name, supporting nesting, and then they get auto-inlined.