Giter Site home page Giter Site logo

sdiehl / kaleidoscope Goto Github PK

View Code? Open in Web Editor NEW
1.0K 39.0 130.0 531 KB

Haskell LLVM JIT Compiler Tutorial

Home Page: http://www.stephendiehl.com/llvm

License: Other

Makefile 1.37% Haskell 94.11% CSS 2.90% Shell 0.12% C 1.15% LLVM 0.35%
llvm-bindings llvm-tutorial haskell kaleidoscope compiler tutorial

kaleidoscope's Introduction

A short guide to building a tiny programming language in Haskell with LLVM.

Stephen Diehl

Haskell LLVM Tutorial

Build Status MIT License

Read Online:

Setup

You will need GHC 7.8 or newer as well as LLVM 4.0. For information on installing LLVM 4.0 (not 3.9 or earlier) on your platform of choice, take a look at the instructions posted by the llvm-hs maintainers.

With Haskell and LLVM in place, you can use either Stack or Cabal to install the necessary Haskell bindings and compile the source code from each chapter.

Building with Stack (Recommended)

$ stack build

You can then run the source code from each chapter (starting with chapter 2) as follows:

$ stack exec chapter2

Building with Cabal

Ensure that llvm-config is on your $PATH, then run:

$ cabal sandbox init
$ cabal configure
$ cabal install --only-dependencies

Then to run the source code from each chapter (e.g. chapter 2):

$ cabal run chapter2

Building with make

The source code for the example compiler of each chapter is included in the /src folder. With the dependencies installed globally, these can be built using the Makefile at the root level:

$ make chapter2
$ make chapter6

A smaller version of the code without the parser frontend can be found in the llvm-tutorial-standalone repository. The LLVM code generation technique is identical.

Editing

This is an open source project, patches and corrections always welcome.

To generate the HTML page:

$ make tutorial.html

A standalone PDF can also be generated with:

$ make tutorial.pdf

License

Text is adapted from the LLVM tutorial and is subsequently licensed under the LLVM license.

The Haskell source files are released under the MIT license. Copyright (c) 2013-2016, Stephen Diehl

kaleidoscope's People

Contributors

amir avatar arminius avatar djv avatar doersino avatar erdeszt avatar jasondavies avatar kmarekspartz avatar oregu avatar sdiehl avatar thekafkaf avatar timjb avatar torosfanny avatar wdv4758h avatar yigitozkavci avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kaleidoscope's Issues

Not in scope: `moduleString'

ghc -no-user-package-db -package-db .cabal-sandbox/*-packages.conf.d --make src/chapter3/*.hs -o chapter3
[5 of 6] Compiling Emit             ( src/chapter3/Emit.hs, src/chapter3/Emit.o )

src/chapter3/Emit.hs:99:14: Not in scope: `moduleString'
Makefile:25: recipe for target 'chapter3' failed
make: *** [chapter3] Error 1

Hi, I'm new to haskell so I don't actually know what I'm doing, but I'm not able to build chapters 3 through 7. Do you have any advice for resolving this error? Thanks.

Wrong type when using alloca?

As noted in llvm document, the return value of alloca should be a pointer. However, in the process of generating the IR of a Function, it is used with type double:

forM args $ \a -> do
    var <- alloca double
    store var (local (AST.Name a))
    assign a var

That's because the alloca is defined with the instr function:

alloca :: Type -> Codegen Operand
alloca ty = instr $ Alloca ty Nothing 0 []

where instr is defined to be a double reference:

local ::  Name -> Operand
local = LocalReference double

instr :: Instruction -> Codegen (Operand)
instr ins = do
  n <- fresh
  let ref = (UnName n)
  blk <- current
  let i = stack blk
  modifyBlock (blk { stack = (ref := ins) : i } )
  return $ local ref

I found this error when I tried to print the generated IR with llvm-hs-pretty. Even a simple function definition def f(a) a; will trigger this problem with the following generated IR:

GlobalDefinition $ functionDefaults {
    name          = makeName "foo"
    , parameters  = ([Parameter double (makeName "a") []], False)
    , returnType  = double
    , basicBlocks = [
        BasicBlock (makeName "entry") [
            UnName 1 := Alloca double Nothing 0 []
            , UnName 2 := Store False (local $ UnName 1) (local $ makeName "a") Nothing 0 []
            , UnName 3 := Load False (local $ UnName 1) Nothing 0 []
        ] (ret . local $ UnName 3)
    ]
}

I am not sure whether this is some incompatibility with newer version LLVM or this is a fault made by myself, but as suggested by @sdiehl in the repo of llvm-hs-pretty, this should be wrong.

Only the first top level expression gets evaluated

For example

$ stack exec chapter4                                                                                                                                  [0 git/remotes/origin/HEAD]
ready> extern sin(x);
; ModuleID = 'my cool jit'
source_filename = "<string>"

declare double @sin(double)

ready> sin(1);
; ModuleID = 'my cool jit'
source_filename = "<string>"

declare double @sin(double)

define double @main() {
entry:
  %0 = call double @sin(double 1.000000e+00)
  ret double %0
}

Evaluated to: 0.8414709848078965
ready> sin(2);
; ModuleID = 'my cool jit'
source_filename = "<string>"

declare double @sin(double)

define double @main() {
entry:
  %0 = call double @sin(double 1.000000e+00)
  ret double %0
}

define double @main.1() {
entry:
  %0 = call double @sin(double 2.000000e+00)
  ret double %0
}

Evaluated to: 0.8414709848078965
ready> 
Goodbye.

sin(2) is 0.91, and if we try that prior to sin(1), that's the answer we get. But here, the result is 0.84 from both.

The problem seems to be that the JIT always executes the function "main" if it exists:

          EE.withModuleInEngine executionEngine m $ \ee -> do
            mainfn <- EE.getFunction ee (AST.Name "main")
            case mainfn of
              Just fn -> do
                res <- run fn
                putStrLn $ "Evaluated to: " ++ show res
              Nothing -> return ()

but in codegenTop we use define double "main", which only generates a function named "main" if it doesn't already exist. If it does exist, we get "main.1", "main.2" and so on.

Solution might be to have a version of define that handles these name conflicts like the current one, and a version that allows overwriting them. (Somehow? We can filter moduleDefinitions, but presumably mkName "main" will still give us "main.1".) I'm not sure if that's the best way to go about it.

can't compile chapters 3, 4, ...

➜  kaleidoscope git:(master) ✗ make chapter3
ghc -no-user-package-db -package-db .cabal-sandbox/*-packages.conf.d --make src/chapter3/*.hs -o chapter3
[5 of 6] Compiling Emit             ( src/chapter3/Emit.hs, src/chapter3/Emit.o )

src/chapter3/Emit.hs:99:14: Not in scope: `moduleString'
make: *** [chapter3] Error 1
➜  kaleidoscope git:(master) ✗ make chapter4
gcc -fPIC -shared src/chapter4/cbits.c -o src/chapter4/cbits.so
ghc -no-user-package-db -package-db .cabal-sandbox/*-packages.conf.d src/chapter4/cbits.so --make src/chapter4/*.hs -o chapter4
[4 of 7] Compiling JIT              ( src/chapter4/JIT.hs, src/chapter4/JIT.o )

src/chapter4/JIT.hs:46:16: Not in scope: `moduleString'
make: *** [chapter4] Error 1
➜  kaleidoscope git:(master) ✗ make chapter5
gcc -fPIC -shared src/chapter5/cbits.c -o src/chapter5/cbits.so
ghc -no-user-package-db -package-db .cabal-sandbox/*-packages.conf.d src/chapter5/cbits.so --make src/chapter5/*.hs -o chapter5
[1 of 7] Compiling Lexer            ( src/chapter5/Lexer.hs, src/chapter5/Lexer.o )
[2 of 7] Compiling Syntax           ( src/chapter5/Syntax.hs, src/chapter5/Syntax.o )
[3 of 7] Compiling Parser           ( src/chapter5/Parser.hs, src/chapter5/Parser.o )
[4 of 7] Compiling JIT              ( src/chapter5/JIT.hs, src/chapter5/JIT.o )

src/chapter5/JIT.hs:46:16: Not in scope: `moduleString'
make: *** [chapter5] Error 1
➜  kaleidoscope git:(master) ✗ make chapter6
gcc -fPIC -shared src/chapter6/cbits.c -o src/chapter6/cbits.so
ghc -no-user-package-db -package-db .cabal-sandbox/*-packages.conf.d src/chapter6/cbits.so --make src/chapter6/*.hs -o chapter6
[1 of 7] Compiling Lexer            ( src/chapter6/Lexer.hs, src/chapter6/Lexer.o )
[2 of 7] Compiling Syntax           ( src/chapter6/Syntax.hs, src/chapter6/Syntax.o )
[3 of 7] Compiling Parser           ( src/chapter6/Parser.hs, src/chapter6/Parser.o )
[4 of 7] Compiling JIT              ( src/chapter6/JIT.hs, src/chapter6/JIT.o )

src/chapter6/JIT.hs:46:16: Not in scope: `moduleString'
make: *** [chapter6] Error 1
➜  kaleidoscope git:(master) ✗ 

`stack ghc preprocessor.hs -- -o preprocessor` fails

I am trying to build kaleidoscope on Ubuntu 15.10.

I installed llvm-3.5 and libedit-dev, andstack build runs successfully. Then when I run stack ghc preprocessor.hs -- -o preprocessor, I get this error:

[1 of 1] Compiling Main             ( preprocessor.hs, preprocessor.o )

preprocessor.hs:46:35:
    Couldn't match type ‘Either Text.Pandoc.Error.PandocError Pandoc’
                   with ‘Pandoc’
    Expected type: Either Text.Pandoc.Error.PandocError Pandoc
                   -> String
      Actual type: Pandoc -> String
    In the second argument of ‘(.)’, namely ‘writeMarkdown def’
    In the second argument of ‘(>>=)’, namely
      ‘putStrLn . writeMarkdown def’

Any ideas what could be going on here?

Setup instuctions differ between README.md and tutorial.md

In README.md, it's recommended to install LLVM 3.5 (which corresponds to the dependencies in kaleidoscope.cabal), but tutorial.md reads as follows:

You will of course also need LLVM 3.3 or 3.4 (not 3.2 or earlier) installed on your system.

The commands below that line in tutorial.md install the most recent available version (not 3.5), which could possibly break the tutorial at some point in the future.

I'd send a pull request, but I'm not sure what's intended or how to install LLVM 3.5 on all the distributions listed.

Release on Hackage

How about releasing the tutorial package on Hackage? It would increase the visibility.

Precedence on `|` operator is problematic

In the kicking-the-tires part, we used:

def binary == 9 (LHS, RHS) =
  !(LHS < RHS | LHS > RHS);

However, precedence of | should be higher than both < and > in order to use it properly without brackets. These two expressions are being evaluated to two different results now:

putchar(30 < 31 | 30 > 31);     # 0.0, wrong
putchar((30 < 31) | (30 > 31)); # 1.0, right

It's fixed by adding binary ">" Ex.AssocLeft just in near binary "<" Ex.AssocLeft in the parser, but don't know if it's the right solution.

EncodeException Instruction of type void must not have a name

Hi, I was doing chapter 3 of the tutorial with LLVM 9 but the part of function parameters seems to have issues:

ready> def f(x) 1;;
kaleidoscope-exe: EncodeException "Instruction of type void must not have a name: UnName 1 := Store {volatile = False, address = LocalReference (FloatingPointType {floatingPointType = DoubleFP}) (UnName 0), value = LocalReference (FloatingPointType {floatingPointType = DoubleFP}) (Name \"x\"), maybeAtomicity = Nothing, alignment = 0, metadata = []}"

I am able to locate the problem -- codegenTop:

codegenTop (AST.Function name args body) = do
  define double name fnargs bls
  where
    fnargs = toSig args

    bls = createBlocks
      $ execCodegen
      $ do
        entry <- addBlock entryBlockName
        setBlock entry
        forM_ args
          $ \a -> do
            var <- alloca double
            store var (local (Name (BSS.toShort (BS.pack a)))) -- <<< PROBLEM HERE
            assign a var
        cgen body >>= ret


instr :: Instruction -> Codegen Operand
instr ins = do
  n <- fresh
  let ref = UnName n
  blk <- current
  modifyBlock (blk { stack = ref := ins <| stack blk })
  return $ local ref

store :: Operand -> Operand -> Codegen Operand
store ptr val = instr $ Store False ptr val Nothing 0 []

Why is this happening?

stack.yaml:

resolver: lts-18.28
packages:
- .

package.yaml:

dependencies:
- base >= 4.7 && < 5
- llvm-hs == 9.0.1
- llvm-hs-pure == 9.0.0
- parsec
- haskeline
- containers
- unordered-containers
- mtl
- bytestring
- lens

ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src

executables:
  kaleidoscope-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - kaleidoscope

tests:
  kaleidoscope-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - kaleidoscope

haskeline 0.7.1.1 or above seems to break the build.

I was unable to make any of the builds until I switched to haskeline 0.7.0.0

It seems that >0.7.1.1 they may have deprecated the MonadIO instance. I got a "no instanceof MonadIO (InputT IO () )" error from ghc.

When I changed the haskeline version to 0.7.0.0 everything worked.

missing module Parser in package at Hackage

The module Parser and others are present in the Git repository but not in the package at Hackage:

Preprocessing executable 'chapter2' for kaleidoscope-0.1.0.0...

src/chapter2/Main.hs:3:8:
    Could not find module ‘Parser’
    It is a member of the hidden package ‘uuagc-0.9.52.1’.
    Perhaps you need to add ‘uuagc’ to the build-depends in your .cabal file.
    It is a member of the hidden package ‘ghc-7.8.4’.
    Perhaps you need to add ‘ghc’ to the build-depends in your .cabal file.
    it is a hidden module in the package ‘uuagc-0.9.52.1’
    Use -v to see a list of the files searched for.

Issues building against llvm 8

Attempted to build against LLVM 8 but failed with numerous compilation errors. Anyone looking at this or can I have a go at getting working?

Run the first chapter on a mac

Hello,

I don't know if it's the good place to ask, so if I'm wrong don't be mad just tell me :)
I'm running OSX.
I installed llvm using brew and I compiled the first functions using llc.
Now I have a test.s and I don't know how to run it.

I tried the command gcc test.s -o hello.native and I have now the error:
test.s:2:2: error: unknown directive
.macosx_version_min 14, 1

Does anyone knows why?
Thanks

haskeline version requirement issue

Great project to learn languages!

I solved two issues of library versions when setting up the environment, one of which is worth mentioning here. Hope this helps get it better.

The requirement of haskeline in kaleidoscope.cabal is >= 0.7.1.2. I am using that version and cannot compile chapter 2 code.

$ ghc Main.hs 
[4 of 4] Compiling Main             ( Main.hs, Main.o )

Main.hs:22:24:
    No instance for (MonadIO (InputT IO))
      arising from a use of ‘liftIO’
    In the expression: liftIO
    In the first argument of ‘(>>)’, namely ‘(liftIO $ process input)’
    In the expression: (liftIO $ process input) >> loop

I then installed the latest haskeline 0.7.3.1 from hackage. Problem solved.

The other issue I encountered is the mismatch of versions between my llvm (Fedora default) and llvm-general (from hackage), which is not a kaleidescope problem.

Attempt to port to IRBuilder

I have a version of the code with IRBuilder in https://github.com/ibrahimsag/urban-goggles.

I think it is in relatively good shape with some hacks. I would be more then happy to touch the prose to update the tutorial but thought i should give a heads up since there might be another work going on, or the implementation may require adjustment.

Thanks for considering it.

Can't install dependencies with ghc-7.6.3

When I try to set up the cabal sandbox, cabal tells me that installing the missing packages would break everything:

(master) % cabal sandbox init
Writing a default package environment file to
/home/arminius/Dev/kaleidoscope/cabal.sandbox.config
Creating a new sandbox at /home/arminius/Dev/kaleidoscope/.cabal-sandbox
(master) % cabal configure
Resolving dependencies...
Configuring kaleidoscope-0.1.0.0...
cabal: At least the following dependencies are missing:
haskeline ==0.7.0.0,
llvm-general ==3.3.8.2,
llvm-general-pure ==3.3.8.2,
mtl -any,
pandoc >=1.12,
transformers -any
(master) [1] % cabal install --only-dependencies
Resolving dependencies...
In order, the following would be installed:
asn1-types-0.2.3 (new package)
base-unicode-symbols-0.2.2.4 (new package)
base64-bytestring-1.0.0.1 (new package)
byteable-0.1.1 (new package)
cereal-0.4.0.1 (new package)
crypto-pubkey-types-0.4.2.2 (new package)
cryptohash-0.11.2 (new package)
data-default-class-0.0.1 (new package)
data-default-instances-base-0.0.1 (new package)
data-default-instances-containers-0.0.1 (new package)
data-default-instances-old-locale-0.0.1 (new package)
digest-0.0.1.2 (new package)
directory-1.0.0.3 (latest: 1.2.0.1) (new version)
dlist-0.6.0.1 (new package)
data-default-instances-dlist-0.0.1 (new package)
data-default-0.5.3 (new package)
extensible-exceptions-0.1.1.4 (new package)
nats-0.1.2 (new package)
primitive-0.5.2.1 (new package)
process-1.1.0.2 (latest: 1.2.0.0) (reinstall) changes: directory-1.2.0.1 ->
1.0.0.3
random-1.0.1.1 (new package)
securemem-0.1.3 (new package)
crypto-cipher-types-0.0.9 (new package)
cipher-aes-0.2.6 (new package)
cipher-rc4-0.1.4 (new package)
setenv-0.1.1.1 (new package)
syb-0.4.1 (new package)
terminfo-0.3.2.6 (latest: 0.4.0.0) (new package)
text-1.1.0.1 (new package)
blaze-builder-0.3.3.2 (new package)
blaze-markup-0.6.0.0 (new package)
blaze-html-0.7.0.1 (new package)
cookie-0.4.0.1 (new package)
hashable-1.2.1.0 (new package)
case-insensitive-1.1.0.3 (new package)
http-types-0.8.3 (new package)
scientific-0.2.0.2 (new package)
attoparsec-0.11.2.1 (new package)
tagsoup-0.13.1 (new package)
text-stream-decode-0.1.0.4 +text11 (new package)
transformers-0.3.0.0 (new package)
failure-0.2.0.1 (new package)
haskeline-0.7.0.0 (latest: 0.7.1.2) (new package)
mmorph-1.0.2 (new package)
mtl-2.1.2 (new package)
asn1-encoding-0.8.1.3 (new package)
asn1-parse-0.8.1 (new package)
exceptions-0.3.3 (new package)
hslua-0.3.10 (new package)
parsec-3.1.5 (new package)
llvm-general-pure-3.3.8.2 (latest: 3.4.1.0) (new package)
network-2.4.2.2 (new package)
HTTP-4000.2.11 (new package)
pem-0.2.1 (new package)
regex-base-0.93.2 (new package)
regex-pcre-builtin-0.94.4.8.8.34 (new package)
highlighting-kate-0.5.6.1 (new package)
socks-0.5.4 (new package)
temporary-1.2.0.1 (new package)
transformers-base-0.4.1 (new package)
monad-control-0.3.2.3 (new package)
lifted-base-0.2.2.1 (new package)
resourcet-0.4.10 (new package)
unordered-containers-0.2.3.3 (new package)
semigroups-0.12.2 (new package)
utf8-string-0.3.7 -bytestring-in-base (new package)
llvm-general-3.3.8.2 (latest: 3.4.1.0) (new package)
publicsuffixlist-0.1 (new package)
vector-0.10.9.1 (new package)
aeson-0.7.0.2 (new package)
crypto-random-0.0.7 (new package)
cprng-aes-0.5.2 (new package)
crypto-numbers-0.2.3 (new package)
crypto-pubkey-0.2.4 (new package)
pandoc-types-1.12.3.2 (new package)
void-0.6.1 (new package)
conduit-1.0.15.1 (new package)
x509-1.4.11 (new package)
x509-store-1.4.4 (new package)
x509-system-1.4.2 (new package)
x509-validation-1.5.0 (new package)
tls-1.2.2 (new package)
connection-0.2.0 (new package)
xml-1.3.13 (new package)
texmath-0.6.6 (new package)
yaml-0.8.7.2 (new package)
zlib-0.5.4.1 (new package)
zip-archive-0.1.3.3 (latest: 0.2.2) (new package)
zlib-bindings-0.1.1.3 (new package)
http-client-0.2.2.2 (new package)
http-client-conduit-0.2.0.1 (new package)
http-client-tls-0.2.1.1 (new package)
http-conduit-2.0.0.8 (new package)
pandoc-1.12.3.3 (new package)
cabal: The following packages are likely to be broken by the reinstalls:
haskell98-2.0.0.2
ghc-7.6.3
haddock-2.13.2.1
Cabal-1.16.0
bin-package-db-0.0.0.0
Use --force-reinstalls if you want to install anyway.

When I do use --force-reinstalls, the build fails:

cabal: Error: some packages failed to install:
conduit-1.0.15.1 depends on directory-1.0.0.3 which failed to install.
connection-0.2.0 depends on directory-1.0.0.3 which failed to install.
directory-1.0.0.3 failed during the building phase. The exception was:
ExitFailure 1
haskeline-0.7.0.0 depends on directory-1.0.0.3 which failed to install.
http-client-conduit-0.2.0.1 depends on directory-1.0.0.3 which failed to
install.
http-client-tls-0.2.1.1 depends on directory-1.0.0.3 which failed to install.
http-conduit-2.0.0.8 depends on directory-1.0.0.3 which failed to install.
llvm-general-3.3.8.2 failed during the configure step. The exception was:
ExitFailure 1
pandoc-1.12.3.3 depends on directory-1.0.0.3 which failed to install.
process-1.1.0.2 depends on directory-1.0.0.3 which failed to install.
temporary-1.2.0.1 depends on directory-1.0.0.3 which failed to install.
tls-1.2.2 depends on directory-1.0.0.3 which failed to install.
x509-1.4.11 depends on directory-1.0.0.3 which failed to install.
x509-store-1.4.4 depends on directory-1.0.0.3 which failed to install.
x509-system-1.4.2 depends on directory-1.0.0.3 which failed to install.
x509-validation-1.5.0 depends on directory-1.0.0.3 which failed to install.
yaml-0.8.7.2 depends on directory-1.0.0.3 which failed to install.
zip-archive-0.1.3.3 depends on directory-1.0.0.3 which failed to install.
(master) [1] %

Further up in the log, these are the errors for directory-1.0.0.3:

Building directory-1.0.0.3...
Preprocessing library directory-1.0.0.3...
[1 of 1] Compiling System.Directory ( System/Directory.hs, dist/dist-sandbox-749f3e8e/build/System/Directory.o )

System/Directory.hs:595:10:
    Not in scope: `Prelude.catch'
    Perhaps you meant one of these:
      `Prelude.acosh' (imported from Prelude),
      `Prelude.acosh' (imported from Prelude),
      `Prelude.atan' (imported from Prelude)

System/Directory.hs:741:40: Not in scope: `c_opendir'

System/Directory.hs:742:42:
    Not in scope: `c_closedir'
    Perhaps you meant `c_close' (imported from System.Posix.Internals)

System/Directory.hs:747:22:
    Not in scope: type constructor or class `CDirent'

System/Directory.hs:747:38:
    Not in scope: type constructor or class `CDir'

System/Directory.hs:750:12: Not in scope: `readdir'

System/Directory.hs:757:33:
    Not in scope: `d_name'
    Perhaps you meant `System.Posix.rename' (imported from System.Posix)

System/Directory.hs:758:21: Not in scope: `freeDirEnt'

System/Directory.hs:764:28: Not in scope: `end_of_dir'

System/Directory.hs:1079:5:
    Not in scope: `Prelude.catch'
    Perhaps you meant one of these:
      `Prelude.acosh' (imported from Prelude),
      `Prelude.acosh' (imported from Prelude),
      `Prelude.atan' (imported from Prelude)
Failed to install directory-1.0.0.3

So among other things, the library directory depends on obsolete functions in the prelude which are no longer included in GHC.
Which versions of GHC does kaleidoscope support?

For further information: I am running ghc-7.6.3 and cabal-1.18.0.3 on Arch Linux.

LLVM 5.0 Error Calling Function

I am following along with the tutorial using llvm-hs-5.1.0 and llvm-hs-pure-5.1.0 and with the exception of a few minor issues between the versions, most things are working correctly.

However, I am stuck on an error I am getting when trying to call a previously defined function.

Using the code up to chapter 3 I can do

ready> def foo(a) a;
; ModuleID = 'my cool jit'
source_filename = \"<string>\"

define double @foo(double %a) {
  entry:
  %0 = alloca double
  store double %a, double* %0
  %1 = load double, double* %0
  ret double %1
}

ready> def bar() foo(3);
EncodeException "The serialized GlobalReference has type 

PointerType {
  pointerReferent = FunctionType {
    resultType = FloatingPointType { floatingPointType = DoubleFP }, 
    argumentTypes = [ FloatingPointType { floatingPointType = DoubleFP } ], 
    isVarArg = False
  }, 
  pointerAddrSpace = AddrSpace 0
} 

but should have type 

FloatingPointType { floatingPointType = DoubleFP }"

I believe the problem is from trying to create the constant global reference operand in the externf function.

I can fix the error when supplying a type

-- Codegen.hs
externf :: Type -> Name -> Operand
externf ty nm = ConstantOperand (C.GlobalReference ty nm)

-- Emit.hs
cgen (S.Call fn args) = do
  largs <- mapM cgen args
  let fnT = AST.PointerType { AST.pointerReferent = (AST.FunctionType { AST.resultType = double, AST.argumentTypes = [], AST.isVarArg = False}) , AST.pointerAddrSpace = AddrSpace 0 }
  let oper = externf fnT (AST.Name fn)
  call oper largs

However, this is very specific to this exact function. Preferably, the type would be determined automatically.

So in summary, how can I find the type of the function I am calling so I can pass it into externf?

Note: This error has been added in llvm-hs 5.0 https://github.com/llvm-hs/llvm-hs/blob/4b8269a3be55e84a75de41cdc481c589d161b7f3/llvm-hs/test/LLVM/Test/Regression.hs#L98

Can't build, tried two approaches

Hello,

New to NixOS, but it seemed the easiest way to get the Haskell kaleidoscope tutorial up and running. Seems close working but not quite. Any suggestions or fixes would be most welcome.

Fresh latest stable NixOS install in VirtualBox.

nix-channel --list

nixos https://nixos.org/channels/nixos-14.04

First approach:
nix-env -i git
git clone https://github.com/sdiehl/kaleidoscope.git
cd kaleidoscope
nix-shell --pure

Result:

these derivations will be built:
/nix/store/akkrh6m0wffzw6zskjbw92l4k14324ac-kaleidoscope-0.1.0.0.tar.gz.drv
building path(s) `/nix/store/7j4pal4fy1ixx4l127bkdz4gmhsxx1i4-kaleidoscope-0.1.0.0.tar.gz'

�[ptrying http://hackage.haskell.org/packages/archive/kaleidoscope/0.1.0.0/kaleidoscope-0.1.0.0.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (22) The requested URL returned error: 404 Not Found
�[q
�[ptrying http://hdiff.luite.com/packages/archive/kaleidoscope/kaleidoscope-0.1.0.0.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (22) The requested URL returned error: 404 Not Found
�[qerror: cannot download kaleidoscope-0.1.0.0.tar.gz from any mirror
builder for /nix/store/akkrh6m0wffzw6zskjbw92l4k14324ac-kaleidoscope-0.1.0.0.tar.gz.drv' failed with exit code 1 error: build of/nix/store/akkrh6m0wffzw6zskjbw92l4k14324ac-kaleidoscope-0.1.0.0.tar.gz.drv' failed
/run/current-system/sw/bin/nix-shell: failed to build all dependencies

(Why is it trying to pull down the .tar.gz? And those URLs really are broken.)


Second approach:
manually install Haskell Platform, ghc, etc.
make chapter3

Result:

Linking chapter3 ...
/nix/store/ahlnhixrk2hjwd634aamkhj474ay6ian-binutils-2.23.1/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
make: *** [chapter3] Error 1

Attempted fix:

manually install zlib, zlib-static, and haskell-zlib-ghc. No luck. Same problem for later chapters. Environment is

$ nix-env -q '*'
gcc-wrapper-4.8.2
ghc-7.6.3-wrapper
git-minimal-1.9.4
gnumake-3.82
haskell-haskeline-ghc7.6.3-0.7.1.2
haskell-haskell-platform-ghc7.6.3-2013.2.0.0
haskell-llvm-general-ghc7.6.3-3.4.2.2
haskell-llvm-general-pure-ghc7.6.3-3.4.2.2
haskell-mtl-ghc7.6.3-2.1.2
haskell-transformers-ghc7.6.3-0.3.0.0
haskell-zlib-ghc7.6.3-0.5.4.1
llvm-3.4.2
zlib-1.2.8
zlib-static-1.2.8

Optimization removes extern definitions

I implemented everything up to Chapter 4 and have a small issue. For some reason, if I use extern in the REPL, the optimization pass removes the function definition straight away.

ready> extern sin(x);
; ModuleID = 'my cool jit'

if I uncomment the line with runPassManager pm m in JIT.hs, it works as it should:

ready> extern sin(x);
; ModuleID = 'my cool jit'

declare double @sin(double)

I've been looking if I can find a way to omit the optimization pass that removes the declaration but no luck so far.

I'm running llvm-3.5, maybe something changed w.r.t. default optimization passes? I tried setting the optimization to levels 2/1 instead of 3 but the problem persists.

macOS Sierra/llvm 3.9 Support

When running stack build, I get an error saying

    setup: The program 'llvm-config' version ==3.5.* is required but the version
    found at /usr/local/Cellar/llvm/3.9.1/bin/llvm-config is version 3.9.1

I am using macOS Sierra, which doesn't support llvm 3.5. Is there anyway to get this to work with a newer version of llvm like 3.9?

Running cbits.c functions

I am running the examples on a Mint Linux box.

When I try to run extern putchard(x); putchard(120); I get the following error message:

LLVM ERROR: Program used external function 'putchard' which could not be resolved!

In order to build the chapter 4 example i did make chapter4.

Is there something I am missing?

Compiling the preprocessor gives error about missing pandoc

When running stack ghc preprocessor.hs -- -o preprocessor as described in the readme, I get the following error:

preprocessor.hs:6:8:
    Could not find module ‘Text.Pandoc’
    Perhaps you meant
      Text.Parsec (from parsec-3.1.9@parse_8fSmqig53hOL2SEu1k9Tv0)
    Use -v to see a list of the files searched for.

I have pandoc installed, as well as libghc-pandoc-dev (1.16.0.2~dfsg-1 on Ubuntu 16.04. Am I missing something else?

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.