Compiler analysis techniques

The CoolBasic Classic compiler has progressed again, and this time I’d like to share some interesting new features about its internal architecture. Using C# enables me to do certain things much more easily than the old procedural approach and the Object Oriented design really starts to kick in. The new compiler is highly structural, and it now records much more data about declared symbols, scopes, and execution paths. This also serves as a good foundation for some code analysis techniques. For example, the compiler will emit warnings for variables, types, and functions that are declared but never used, as well as if a variable is used before it has been initialized. Warnings like these will encourage the users to write better and cleaner code.

The original CoolBasic has almost no optimizations regarding the generated byte code. This is now different: Constant value analysis can ignore code branches (such as If/Else) if the condition can be evaluated to True or False at compile time. Thus, redundant code will not even make it to the final compiled program. Also, since we have constant value pre-evaluation, some expressions will be simplified before conversion to byte code. This ought to improve the runtime performance. In addition, thanks to internal scope-specific dictionaries, resolving branch targets does not produce linear search to all recorded labels (user-defined or generated). This should improve compile time performance greatly for large programs.

We’ll see how far we end up going with code analysis in the future… I’d love to collect data such as Cyclomatic Complexity or Number of Executable Statements in order to derive a general Maintainability Index out of the given source code.

Local scoping
All scopes now have their own list of local variables. This means that the user can declare variables in a code block such as If, For, or Case. These variables are allocated when the execution begins in that block and they will cease to exist after the execution exits the scope. Therefore, it is possible to declare several variables that have the same name as long as they aren’t conflicting in an enclosing block. Consider the following example:

Dim a As Integer

If a = 1 Then
    Dim b As Integer = 2
    // Variable 'b' is only visible within this If block.
	
    // You cannot declare variable 'a' here because it is already declared 
    // outside.
EndIf

If a = 1 Then
    Dim b As Integer = 3
    // Variable 'b' is visible within this If block and any child blocks.
    // Variable 'b' is different than the one declared in the previous 
    // If block.
	
    For i As Integer = 1 To 10
        // Variable 'i' is only visible within this For block.
		
        // Varible 'b' is also visible here since it was declared 
        // in an enclosing block.
        b = b + 1
    Next
EndIf

It is also possible to declare scope specific constants in the same way. Upon entering a scope the Runtime will ensure that all of its local variables are initialized with zero. Similarly, local arrays and strings can now be freed upon leaving the scope.

Short-circuit And/Or
We’ll also change the way how the Boolean And and Or operators work. They have become ‘short-circuit’, meaning that if the end result of the Boolean operation can be determined by just evaluating the first (left) operand, then the program will not evaluate the right half at all. Again this will improve runtime performance, but users need to pay close attention to their code if the right side has any function calls that need to be executed regardless of the end result of the Boolean operation. This is easily fixed, though, by storing the right side expression into a temporary variable first.

Hello 2011!

Okay… where to start. The year changed. Gosh, that went fast. I can remember me writing tons of stuff and assembling the DevTeam 12 months back like it was yesterday. Obviously we didn’t make it in 2010 like originally planned, but the CoolBasic Classic project continues still. Actually, I just went ahead and changed the year from 2010 to 2011 on coolbasic.com (…and yes, it’s a common joke amongst project managers that deadlines should indicate month, but intentionally leave the year unspecified 🙂 ). I’ve been a little slacky the past few weeks, I admit, but it’s been hectic at work and other members of the Team have had all kinds of stuff going on in their lives (who wouldn’t). It being a year now, the DevTeam members’ contracts are closing to an end soon, naturally. This means it’s time for a round of renewals! That being said, few people are leaving their duties due to a number of things, but the majority has expressed their interest of continuing in the Team. And that’s great! For now, we have decided not to open new positions, and will recruit more only when needed – who knows, perhaps some of the original members will make a come-back. The organization structure is going to change a little bit, and details will be published later.

So what’s been done since last time… well, most of it has been management oriented. I feel it’s most convinient for you if I just list the things:

  • We have explored alternatives to SVN version control, like Mercurial
  • We actually purchased a Virtual Server for web hosting
  • We now have domain coolbasic.fi (and it’s already operational on the Virtual Server)
  • We have configured PHP and MySQL databases
  • We have explored ways to use LDAP authentication services
  • We have explored ways to improve forum experience through measures in order to shut down spam
  • The PureBasic accounts have been terminated since we now use other tools

The web host isn’t fully configured yet, but we’re working on it. When that’s done, we can set up a true testing environment for web developing (full copy of the portal, forums, everything).

I saved some money for not having to purchase a new set of PureBasic one-year licenses, but then again… I did purchase the Virtual Server, and it costs roughly the same. No gain there. In addition, I just recently purchased a license of Resharper which was around 200 €. I use it together with StyleCop at work, so I’m quite used to it. Coding without these essential tools feels somehow awfully wrong nowadays. Resharper is an extension for Visual Studio that provides advanced code analysis and refactoring tools that will help you write better code that is styled and constructed according to “best practises”. It helps to improve code readability and maintainability, and overall I think the gain is notable. I’ll be spending at least a few days refactoring my existing code now…

Fortunate me, I’ve had a chance to experiment some of my compiler specific ideas at work as well (I’m working on a project where I am able to use this expertise of mine), so even though not much code have been added to the CBC compiler project for the past 2 months, I, in fact, have done something useful that will help me achieve my goal in the CoolBasic project (I proved some techniques and theories work in practise).

Until next time…

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