C 2 C Compiler - Intermediate C99 to C99 optimizing

Bhathiya Perera

YAMA 0006 - C 2 C Compiler - Intermediate C99 to C99 optimizing compiler

  • Author(s): Bhathiya Perera
  • Status : Draft

Problem

Yaksha uses C as a final compilation target. This is to introduce a way to have an intermediate C as a IR that can then be compiled to C

Naming

  • iC99 - Intermediate C 99

Language

  • Looks like C99.
  • Loops -> for, while, do while
  • structs, constants, unions, #define
  • We have built-ins such as yk__sdsnew, etc.
#define yy__raylib_Music Music

i32 yy__main() {
  yy__raylib_Music x;

  return 0;
}

Architecture

  • Tokenize
    • Level 1 -> no keyword recognition
    • Recognizes normal operators, identifiers, #?? and comments
    • Discard comments
  • Parse all with pre-processor statements
  • Remove comments
  • Apply #defines -> until there are no changes present
  • AST based
  • Only works with 1 file
  • Entry points yy__main yy__game_step, yy__init_state

Optimizations

Opt 01 - apply all #defines starting with yy__

  • Implement a simple pre-processor, however only capable of applying #defines that start with yy__
  • Opt 01.01 - Support non function like macros
  • Opt 01.02 - Support for function like macros

Opt 02 - data types

  • Opt 02.01 - Push down support of integer data types from Yaksha compiler to iC99.
  • Instead of having extra complexity at the compiler level for i32 ike data types, we push it to iC99.

Opt 03 - clean up

  • Opt 03.01 - Remove any method that cannot be reached from yy__main()
  • Opt 03.02 - Remove any operation in same block after return

Opt 04 - constant folding

  • Opt 04.01 - Apply -
  • Opt 04.02 - Remove excess ( ) parentheses
  • Opt 04.03 - Do basic math
  • Opt 04.04 - Apply basic string built-ins
  • Opt 04.05 - Apply basic array built-ins
  • Opt 04.06 - Dead code elimination

Opt 05 - apply constant function calls

  • Everything is a constant expression?
  • For each function things with simple math, assignments and built ins are considered pure
  • Any of these pure functions can be invoked during compile time and folded

Opt 06 - small function in-lining

  • Inline small functions < 20 lines?

Opt 07 - multiple rounds of previous optimizations

while changed:

  • Opt 03
  • Opt 04
  • Opt 05
  • Opt 06