Git Product home page Git Product logo

kscope's Introduction

#Kscope#

My personal stab at the kaleidoscope LLVM tutorial found here:

http://llvm.org/docs/tutorial/index.html

Deviations

As part of the learning experience this naturally evolved a bit from the tutorial itself. Here's a rundown of what's different:

Language differences

Commas in prototypes

tutorial:

def foo(x y) x + y;

kscope:

def foo(x, y) x + y;

No user-defined operators

Might bring this stuff back now that the flex/bison combo is working well enough. It's really not a feature that serious languages should have but then again this isn't a serious language.

C++11 features

Part of the reason for all this is to (re)learn c++ and what's new in c++11, so there are a handful of commits like f4b2108 that leverage new features like

Next up will be finding a way to use lambdas in a nice way, since they're rad as hell.

Design

layout

Wherever possible, classes are in their own respective .cc files with matching headers. For example, there's a header-and-implementation pair for each of the abstract syntax tree nodes under src/ast

AST/Codegen decoupling

The IR codegen parts of the system have been entirely decoupled from the AST. Each node class has a codegen method like before, but an IRRenderer class is passed in, and it's this class that provides all of the llvm-specific context and helper methods for generating LLVM IR.

At some point in the future I'll probably write separate renderers for other formats, like having the AST pretty-printed on the screen or something.

Bison/Flex for parsing/lexing

Instead of an ad-hoc parser that uses getchar() all over the place and is tightly coupled to everything else there's now a separate parsing subsystem with a lexer.ll file for token/lexer state and grammar.yy with a distilled, purestrain Backus-Naur Form grammar. The parser and lexer are abstracted into an STree (syntax tree) class that can parse an input stream and set its root node to the result of the parsing.

This simplifies the main.cpp REPL stuff immensely, std::cin is given to the STree in chunks and the root node has it's IR rendered with an IRRenderer. In the future it will probably be updated to abstract the REPL stuff somewhere else and allow for file-reading depending on how kscope is invoked.

Style

CamelCase and pascalCase are used for class names but that's about it. Everything else adheres to an underscore style. I guess this isn't normally how c++ is done but underscore-style is superior so there.

Building

###Requirements:

  • cmake
  • LLVM
  • bison >= 3.0
  • flex

NOTE: Installing llvm via homebrew didn't quite do it for me, doesn't seem to install the proper cmake helper stuff so you gotta install from source. It's a fairly painless procedure though:

http://llvm.org/docs/CMake.html

Source for 3.3 (the version I've been using) is here: http://llvm.org/releases/download.html#3.3 ###to build:

mkdir build && cd build

cmake ..

make

kscope's People

Stargazers

Jsson Xia avatar Harrison Hao avatar Dmitry Kolesnikovich avatar  avatar Miguel Muscat avatar wreulicke avatar Andrzej Kostrzewa avatar JaredYeah avatar  avatar  avatar  avatar Ben avatar liwei avatar Yan Li avatar marky avatar William Glass avatar Benjamin Encz avatar Shnaider Pavel avatar Stephen Belanger avatar Axel Etcheverry avatar Himank Barve avatar Primitivo R. Montero avatar Alex Denisov avatar Ionuț Ambrosie avatar James Campbell avatar  avatar 马迪 avatar Arthur Pham avatar Joe Groff avatar

Watchers

William Glass avatar  avatar

kscope's Issues

Extra syntax error when exiting normally

Example:

ready> extern putchard(x);
ready> ^D
Error: syntax error
ready> ; ModuleID = 'my cool jit'

declare double @putchard(double)

Guess: something to do with how END tokens are treated, or how EOF tokens aren't treated at all! Probably a simply a matter of shoring up the lexer and grammar files.

Extra whitespace after multiline statement

Example at the end of the issue.

Most likely culprit is how the while loop in main.cc is structured.

ready> extern putchard(x);
ready> def printdensity(d)
  if d > 8 then
    putchard(32)  # ' '
  else if d > 4 then
    putchard(46)  # '.'
  else if d > 2 then
    putchard(43)  # '+'
  else
    putchard(42); # '*'






ready>

Segfault when trying to do recursion

Example at the end of the issue.

From the looks of it the result of getPointerToFunction returns a pointer alright, but executing the function causes a segfault.

Was most likely introduced in c4d9c04

[william bin ] master $ lldb src/kscope
Current executable set to 'src/kscope' (x86_64).
(lldb) process launch
Process 24597 launched: '/Users/william/work/kscope/bin/src/kscope' (x86_64)
ready> def fib(x) if (x < 3) then 1 else fib(x-1)+fib(x-2);
ready> fib(10);

Process 24597 stopped
* thread #1: tid = 0x8940bd, 0x0000000100d00086, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=2, address=0x7fff5f3ffff8)
    frame #0: 0x0000000100d00086
-> 0x100d00086:  vmovsd %xmm1, 8(%rsp)
   0x100d0008c:  jmpq   0x100d000c3
   0x100d00091:  movabsq $4308598864, %rax
   0x100d0009b:  vmovsd %xmm0, 16(%rsp)
(lldb)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.