CoolBasic Intermediate Language

Perhaps the title of the previous blog entry could’ve been better thought out. For some reason, I keep reading it wrong “IL logic is gone”, and I’m not sure if I’m the only one 🙂 . In addition, “program logic” is a term that’s basically the less technical alternative to “algorithm”. In the previous post, I was referring to how the execution bounces between “labels” generated by program flow control structures such as conditions and loops. Anyways, for today’s post I also have news about Intermediate Language, only this time I actually mean how separate lines are executed expression-wise (which also can be referred to as “logic”).

I have now almost finished those necessary functions that are responsible for generating the high-level instructions from each line of the original source code. This is essentially the base for “Byte code” that is used by runtime interpreters. The order of tokens, in CoolBasic Intermediate Language, is fixed so that they can be processed and calculated very efficiently at runtime. You can learn more about postfix notation in this wiki article.

The following CoolBasic code:

Module M
	Function R()
		Dim i As Integer
		i *= i + (6 / i) - i * 2
	EndFunction
EndModule

… will compile into:

.method public shared m.r ()
{
.size 4
IL_000002: ldloc     m.r.i
IL_000003: ldloc     m.r.i
IL_000004: ldc.i4    6
IL_000005: ldloc     m.r.i
IL_000006: div       
IL_000007: add       
IL_000008: ldloc     m.r.i
IL_000009: ldc.i4    1
IL_000010: shl       
IL_000011: sub       
IL_000012: mul       
IL_000013: stloc     m.r.i
IL_000014: ldc.i4    0
           IL_prog_6_end:
IL_000016: ret       
}

Operator data type pairs don’t show there, but those are to be determined later. Please also notice that this is not a complete program. I merely wanted to give you a demonstration that CoolBasic Intermediate Language generation is quite mature already. You can also see some code optimization in this example. However, there’s much more into it than simply performing binary shifts instead of multiplication or division in certain situations. The CoolBasic compiler is able to generate the equivalent IL from almost any language feature now.

IL generation from expressions also means that most if not all of the error messages (and so error checks) are done, and that every statement has reached their final design. During this time I have fixed quite a lot of bugs, none of which has proved to be overwhelming enough to compel me to come up with a work-around. There’s also been some bigger challenges such as property behavior with the direct assign operators (+=, -= etc), field access with long dot notation paths, constructor calls, and IL generation for special operators (to be revealed later). Minor and small problems usually take only minutes to solve, but bigger ones can take up several hours until I come up with a solution of some sort. Looking back, those “difficult” problems have solved quite nicely, and now that I think of all the benefits I’ve gained thanks to them, there’s no gum code into it after all.

In addition to technical coding, I’ve also established the new www-portal framework I’m experimenting with. CoolBasic web page will feature lots of dynamic content in the future, maybe even some kind of a publishing interface for admins. I’m also thinking of ways to bind this dynamic content to the development environment. As a small side-note I’ve also spent some time on new icon candidates for CoolBasic files, and at least the Vista versions look quite nice already.

The next blog entry will in all probability be just an announcement of two things: for one, of me having finished IL generation entirely. About the second I don’t want to talk about yet.

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress