Skip to content

Compiler optimizations #1

@itsaky

Description

@itsaky

This is a list of possible optimizations that could be implemented in the YuvaKriti compiler. These are just some random thoughts that crossed my mind at some point. They may or may not be implemented :

Redundant instructions

Consider the program :

var a = 0;
print a;

The bytecode instructions for the above program are :

========= YKB =========
major version: 0
minor version: 1
Constant pool: 
    #1: NumberInfo           0
    #2: Utf8Info             i
    #3: Utf8Info             Code
Attributes: 
    Code: max_stack=1
        ldc #1 // 0
        store_0 
        load_0 
        print

It is seen that the store_0 and load_0 instructions are redundant. Maybe we could add an optimizer
which optimizes bytecode instructions like above. Or rather, the compiler could inline the variables in if they are never modified.

Redundant jumps

if false {
    print "Hello";
} else {
    print "World!";
}

In the above program, the if condition is always false and hence, the print "Hello"; instruction is
never executed. Maybe the compiler could simply ignore the if-then statements and write the instructions in the else
block only.

This could be true for the following statements as well :

if true {
    print "Hello";
} else {
    print "World!";
}

// In this case, the compiler should check if the variable 'a' is re-assigned
// before the if-then block is executed. If there are re-assigments for the same variable,
// the optimization should not be performed.
var a = false;
if a {
    print "Hello";
} else {
    print "World!";
}

Redundant unary operators

var a = false;
var b = 10;
print !!a;
print -(-b);

This can be optimized to :

var a = false;
var b = 10;
print a;
print b;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions