EDDI Compiler 1.1.1 – Dynamic Memory Allocation and Constructors/Destructors

As I'm in holiday, the work is going pretty fast. The version 1.1.1 of the EDDI Compiler (eddic) is available.

This version introduces two major changes. The first is the support of dynamic memory allocation. You can allocate a struct or a standard type in help using the new operator. The memory can be released using the delete operator. Another related improved is the addition of constructors and destructors to the language. The following sample shows what can be done with the new features:

struct A {
    int a;

    this(int a){
        this.a = a;

        print("Constructed");
    }

    ~this(){
        println("Destructed");
    }
}

void main(){
    A* b = new A(55);
    delete b;
}

The constructor is called once the memory is allocated. The delete operator calls the destructor and then free the memory.

When a structure is allocated on the stack, the constructor is called at the declaration point and the destructor is called when the variable gets out of scope.

The memory manager is quite simple for now. Memory is allocated in blocks. Each block has a header indicating the size of the block and its availability. The size of the header is 8 bytes in 32 bits and 16 bytes in 64 bits. The free operation can be done in constant time by just setting the availability flag to false. The disadvantage of this technique is that all the blocks needs to be tested to find a free block. This can be slow in some situations. I will try to make a better version in the future.

For that, the memory model has been improved. All the offsets are now increasing and the stack addresses are set at the end of the block.

Another interesting improvement of the language is the support of switch. For now, only switch on int is supported. Here is an example of a switch in EDDI:

switch(a){
    case 3:
        print("3");
    case 4:
        print("4");
    case 5:
        print("5");
    case 6:
        print("6");
    default:
        print("default");
}

The performances of the optimizer have been improved, by doing live-variable analysis less often. Pointers can now be passed in registers. Some of the variables used as temporary copies are removed

The peephole optimizer has been improved to use conditional move when possible. Moreover, the peephole optimizer is now able to perform some local copy propagation.

Future work

The next version of the EDDI Compiler will be the version 1.1.2. This version will add features to read the command-line. Moreover, it will also add support for char type and string comparisons. With that, I think that the language will start to be usable for toy applications.

There will be some improvements to the code that have been left aside for a too long time.

Download

You can find the EDDI Compiler sources on the Github repository: https://github.com/wichtounet/eddic

The exact version I refer to is the v1.1.1 available in the GitHub tags or directly as the release branch.

Comments

Comments powered by Disqus