llvm2c icon indicating copy to clipboard operation
llvm2c copied to clipboard

Simplifying generated code

Open mchalupa opened this issue 6 years ago • 0 comments

(Just tracking the ideas for the future...)

// struct declarations

// anonymous struct declarations

// type definitions

// struct definitions

// function declarations
int main();
void test_assert(unsigned int var0);

// global variable definitions

int main(){
    unsigned int var0;
    unsigned int var1;
    unsigned int var2;
    unsigned int var3;
    block0: ;
    var0 = 0;
    var1 = 0;
    var2 = 1;
    var3 = 3;
    var1 = (((int)var2) + ((int)var3));
    var2 = 3;
    var3 = 5;
    test_assert((unsigned int)(var1 == 4));
    return 0;
}

This code has several issues that we could remove:

  • [x] the comments at the beginning of the program are not needed if nothing is generated for them

  • [ ] #26 (var2 and var3)

  • [x] #25 (the main function)

  • [x] we could get rid of the ; after block0: in cases where there's no declaration immediately after the label

  • [ ] we can remove labels that are not referred to by any goto (block0 in this case)

  • [ ] we can remove unconditional jumps on labels that are going to be executed even without the jump (when the C code immediately follows the jump) (not in this code)

mchalupa avatar Sep 03 '19 10:09 mchalupa