Kind Of A Kaleidoscope

Vincent | Gauthier | Anas | Valentin

The project

Write your own Kaleidoscope compiler, using LLVM

Lexer Parser and AST

Monadic Parser Combinator

<digit> ::= '0'-'9'

<number> ::= ('-' / '+')? <digit>+
digit :: Parser Char
digit = sat isDigit

number :: Parser Integer
number = do
          o <- string "+" <|> string "-" <|> return []
          s <- some digit
          return $ value o s
          where
            value "+" s = read (s)::Integer
            value o s = read (o++s)::Integer

Precedence Climbing

Op Pre Assoc
|| 20 Left
&& 30 Left

True && True || False && False

True

True

&&

True

&&

True

|| 20 Left

&& 30 Left

True

&&

True

||

True

&&

True

||

False

True

&&

True

||

False

&&

True && True || False && False

||

&&

&&

True

True

False

False

Code Generation

Compiler pipeline

; ModuleID = 'module 1.0'
source_filename = "<string>"

define i64 @foo(i64 %a, i64 %b) {
entry:
  %0 = alloca i64
  %1 = alloca i64
  %2 = load i64, i64* %0
  %3 = mul i64 2, %2
  %4 = load i64, i64* %0
  %5 = mul i64 2, %4
  %6 = load i64, i64* %1
  %7 = mul i64 %5, %6
  %8 = add i64 %3, %7
  %9 = load i64, i64* %1
  %10 = mul i64 2, %9
  %11 = add i64 %8, %10
  ret i64 %11
}
def foo(a:int, b:int):int 2*a + 2*a*b + 2*b;
define i64 @main() {
entry:
  %0 = call i64 @foo(i64 1, i64 2)
  %1 = call i64 @foo(i64 3, i64 4)
  %2 = sub i64 %0, %1
  ret i64 %2
}
foo(1, 2) - foo(3, 4);

Write code in Kaleidoscope

Convert into LLVM IR

Compile to LLVM bytecode

Execute LLVM bytecode

Convert to ASM

Compile to bytecode

lli

llc

Compile into object file

as

gcc

llvm-as

koak

Feedback

  • discovered compilation process

  • make a real Lexer Parser

  • insufficient time

  • bad team organization

Questions

Copy of Kind Of A Kaleidoscope

By foret_a

Copy of Kind Of A Kaleidoscope

  • 370