Getting started (Windows)

Hi,

I'm just trying to get started with a simple example with LLVM under
windows. I downloaded the mingw binaries from the website and would
like to compile a program. (PS: The mingw binaries did not come with
any documentation on what they are and how to use them)

I used the online code generator (http://llvm.org/demo/index.cgi) to
compile hello world:
#include <stdio.h>
int main() {
  printf("hello world\n");
  return 0;
}

Copied it's output
; ModuleID = '/tmp/webcompile/_3997_0.bc'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
target triple = "i386-pc-linux-gnu"
@.str = internal constant [12 x i8] c"hello world\00" ; <[12 x i8]*> [#uses=1]

define i32 @main() nounwind {
entry:
  %0 = tail call i32 @puts(i8* getelementptr ([12 x i8]* @.str, i32 0,
i32 0)) nounwind ; <i32> [#uses=0]
  ret i32 0
}

declare i32 @puts(i8*)

into a bc file, and tried to run it with lli, but I get this error:

"lli: error loading program 'hello.bc': Bitcode stream should be a multiple of 4
bytes in length"

(I'm guessing I also need to change the 'target' type to something
non-linux...?)

How can I get LLVM to compile, assemble, link and generate a simple exe?

Thanks.

Hi,

Copied it's output
; ModuleID = '/tmp/webcompile/_3997_0.bc'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
target triple = "i386-pc-linux-gnu"
@.str = internal constant [12 x i8] c"hello world\00" ; <[12 x i8]*> [#uses=1]

define i32 @main() nounwind {
entry:
  %0 = tail call i32 @puts(i8* getelementptr ([12 x i8]* @.str, i32 0,
i32 0)) nounwind ; <i32> [#uses=0]
  ret i32 0
}

declare i32 @puts(i8*)

into a bc file, and tried to run it with lli, but I get this error:

"lli: error loading program 'hello.bc': Bitcode stream should be a multiple of 4
bytes in length"

lli and most other tools expect bitcode (.bc) and not LLVM human
readable assembler (.ll). The demo page produces .ll because it's
for humans to read! Copy the demo output into hello.ll, and then
do: llvm-as hello.ll
This will produce hello.bc, which you can run using lli.

Ciao,

Duncan.

PS:

How can I get LLVM to compile, assemble, link and generate a simple exe?

use llvm-gcc, which works the same as gcc.

Hi Duncan,

Thanks for the reply, that did indeed work.

I realise that I could use llvm-gcc but I want to learn how to use the
parts of llvm that would allow me to make my own compiler
(eventually), also at this point I'm trying to avoid having to build
all of llvm myself and just use what is available pre-built from the
site.

I was wondering how to actually create the exe?

For example with gcc:
gcc hello.c -S -o hello_gcc.s
produces a native assembly file
and then
gcc hello_gcc.s -o hello_gcc_asm.exe
will produce the final "hello world" I can run.

With llvm, I've done:
hello.c to hello.ll via the online LLVM compiler tool
(http://llvm.org/demo/index.cgi)
then
llvm-as hello.ll
to create the hello.bc, then
llc hello.bc
to create the hello.s

but if I try and create an exe from this assembly file with gcc I get
a number of errors,
Likewise, "cl.exe" (microsoft compiler) says "cl : Command line
warning D9024 : unrecognized source file type 'hello.s', object file
assumed"

PS: Including a link to this: "http://llvm.org/cmds/&quot; in the mingw
distribution would probably help other people get up and running
quicker.

Try changing the third line of the output from the demo to look like
the following:
target triple = "i586-mingw32msvc"

The online demo output explicitly requests an x86 Linux target, so llc
honors that rather than generating code for your native platform. If
you change the target triple to a Windows target triple, gcc should do
the right thing with the output.

-Eli

There's a pre-built llvm-gcc for Windows at
LLVM Download Page (LLVM-GCC 4.2 Front End
Binaries for Mingw32/x86"). It's a separate download from the rest of
the LLVM binaries.

-Eli

Hi Eli,

Try changing the third line of the output from the demo to look like
the following:
target triple = "i586-mingw32msvc"

Fantastic, thanks!

There's a pre-built llvm-gcc for Windows at

Hmm, don't know how I missed that...

I guess that clears up all my beginners issues.

Thanks,
-Adrian

Hi Eli,

Try changing the third line of the output from the demo to look like
the following:
target triple = "i586-mingw32msvc"

Fantastic, thanks!

There's a pre-built llvm-gcc for Windows at

Hmm, don't know how I missed that...

I guess that clears up all my beginners issues.

Hi,

The only issue is that LLVM-GCC uses GAS to assemble to COFF files, so its a bit slower than direct object emission and you will need to use binutils to link to PE or DLL.

We are working on DOE backends for COFF/PE/DLL, ELF and MachO at present but that work will not be completed for another few months.

Aaron