src to src conversion

I am trying to use LLVM as a source to source C compiler. I use llvm-gcc to convert file.c->file.bc. Then I use opt to run my own compiler passes to convert file.bc->file.opt.bc. Then I use llc to convert file.opt.bc->file.opt.c. Now, I want to use normal gcc to compile file.opt.c into an executable. However, I'm getting the following errors:

test.opt.c:89: warning: conflicting types for built-in function 'malloc'
test.opt.o(.text+0xe7): In function `main':
: undefined reference to `__main'
collect2: ld returned 1 exit status
make: *** [test.opt.exe] Error 1

Ryan M. Lefever wrote:

I am trying to use LLVM as a source to source C compiler. I use llvm-gcc to convert file.c->file.bc. Then I use opt to run my own compiler passes to convert file.bc->file.opt.bc. Then I use llc to convert file.opt.bc->file.opt.c. Now, I want to use normal gcc to compile file.opt.c into an executable. However, I'm getting the following errors:

Please try the following:

llvm-gcc -o final file.opt.bc -lcrtend
llc -march=c final.bc
gcc -o finalexec final.cbe.c

The first line uses llvm-gcc to link libcrtend.a to your optimized bytecode file. Using llvm-gcc ensures that it picks up the version of libcrtend.a located within your llvm-gcc distribution (which should work).

The second and third lines generate the C code and compile it into a final executable.

If that doesn't work, please let me know.

test.opt.c:89: warning: conflicting types for built-in function 'malloc'
test.opt.o(.text+0xe7): In function `main':
: undefined reference to `__main'
collect2: ld returned 1 exit status
make: *** [test.opt.exe] Error 1

------

I think the problem with main and __main is that I need to link in some LLVM runtime libraries. So, I tried to link in crtend (which defines __main). However, I get the following error:

/home/testbed1/lefever/work/llvm/install/lib/libcrtend.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status
make: *** [test.opt.exe] Error 1

I tried creating a copy of the libcrtend.a and running ranlib on it and I also separately tried running llvm-ranlib on it. Neither of those actions worked though. Am I linking the correct library, i.e., crtend? Why am I getting the error about the archive having no index?

-----

As far as the warning about conflicting types for malloc, it appears that llvm is using the declaration where the output of malloc is a "signed char *" rather than "void *". Why is that?

My memory is foggy, but it's probably because either the LLVM libraries (from llvm/runtime) or the LLVM code you're compiling has a prototype for malloc that returns "signed char *," but the header file being pulled in by GCC when compiling the .cbe.c file has a prototype for "void *." It's also possible that llc is adding the malloc prototype as well. If you're really curious, some simple experimentation should indicate which of the above it is.

Thanks in advance for any help.

Regards,
Ryan

-- John T.

Ryan M. Lefever wrote:

I am trying to use LLVM as a source to source C compiler. I use llvm-gcc to convert file.c->file.bc. Then I use opt to run my own compiler passes to convert file.bc->file.opt.bc. Then I use llc to convert file.opt.bc->file.opt.c. Now, I want to use normal gcc to compile file.opt.c into an executable. However, I'm getting the following errors:

Please try the following:

llvm-gcc -o final file.opt.bc -lcrtend
llc -march=c final.bc
gcc -o finalexec final.cbe.c

The first line uses llvm-gcc to link libcrtend.a to your optimized bytecode file. Using llvm-gcc ensures that it picks up the version of libcrtend.a located within your llvm-gcc distribution (which should work).

The second and third lines generate the C code and compile it into a final executable.

If that doesn't work, please let me know.

test.opt.c:89: warning: conflicting types for built-in function 'malloc'
test.opt.o(.text+0xe7): In function `main':
: undefined reference to `__main'
collect2: ld returned 1 exit status
make: *** [test.opt.exe] Error 1
------
I think the problem with main and __main is that I need to link in some LLVM runtime libraries. So, I tried to link in crtend (which defines __main). However, I get the following error:
/home/testbed1/lefever/work/llvm/install/lib/libcrtend.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status
make: *** [test.opt.exe] Error 1
I tried creating a copy of the libcrtend.a and running ranlib on it and I also separately tried running llvm-ranlib on it. Neither of those actions worked though. Am I linking the correct library, i.e., crtend? Why am I getting the error about the archive having no index?
-----
As far as the warning about conflicting types for malloc, it appears that llvm is using the declaration where the output of malloc is a "signed char *" rather than "void *". Why is that?

My memory is foggy, but it's probably because either the LLVM libraries (from llvm/runtime) or the LLVM code you're compiling has a prototype for malloc that returns "signed char *," but the header file being pulled in by GCC when compiling the .cbe.c file has a prototype for "void *." It's also possible that llc is adding the malloc prototype as well. If you're really curious, some simple experimentation should indicate which of the above it is.

My guess is that it is because void* is always converted to sbyte* in llvm. Since both char* and void* get mapped to sbyte*
the mapping back is difficult, and perhaps impossible. (Maybe I'm wrong?)

Yup, that's right.

-Chris

This seems to work. Thank you for the help!

John Criswell wrote:

Hello,
I have just downloaded the source from CVS.
Does exist a LLVM Java front-end?
If yes, where can I find documentations about it?
If no, is anyway somebody working on it? Where can I find the source code
related?

Thanks,
Alessandro

Hi Alessandro,

There is a partial implementation of a Java front end in the llvm-java
CVS repository. Here's some info on front-end language support:

http://llvm.org/docs/FAQ.html#felangs

Reid.

Hello Alessandro,

You can get the source code for the existing Java front-end by doing this:

cvs -d :pserver:anon@llvm-cvs.cs.uiuc.edu:/var/cvs/llvm login
Hit the return key when prompted for the password.
cvs -z3 -d :pserver:anon@llvm-cvs.cs.uiuc.edu:/var/cvs/llvm co llvm-java

You will need the GNU classpath libraries to compile llvm-java, and the GNU classpath libraries need GTK+

llvm-java needs a "flat" classpath structure so you will need to unzip the file GNU_classpath_dir/share/classpath/glibj.zip

Then in llvm-java directory just do
./configure --prefix=install_dir --with-classpathdir=My_GNU_classpath_dir

I was able to compile and install the llvm-java front-end without any erros, but i get a ugly error as soon I execute the front-end...:

class2llvm: Constants.cpp:228: llvm::ConstantSInt::ConstantSInt(const llvm::Type*, int64_t): Assertion `Ty->isInteger() && Ty->isSigned() && "Illegal type for signed integer constant!"' failed.
Aborted

I didn't have the time yet to look at the problem, but I will....

Sebastien

ac@tml.hut.fi wrote: