initializer does not match global variable type.

From: llvmdev-bounces@cs.uiuc.edu
[mailto:llvmdev-bounces@cs.uiuc.edu] On Behalf Of Chris Lattner
Sent: Monday, October 16, 2006 2:09 PM
To: LLVM Developers Mailing List
Subject: Re: [LLVMdev] initializer does not match global variable type.

I have an objective-c file, bar.m, that I try to process in the
following way generating the error shown below. Any help would
be appreciated. I suspect the error is in the first few lines
of output.

That is really really strange. Can you please file a bugzilla
report and
indicate what target you have llvm-gcc configured for?

I don't understand what llvm-gcc has to do with this. Maybe I wasn't
clear. The "g++" is ordinary vanilla g++, not llvm-g++. Are there
any versioning issues with regular g++ compiling llvm2cpp generated
C++ files? I believe cfrontend-g++ comes from the cfrontend tarball.
llvm2cpp came from the llvm tarball. My g++ was 4.1.1. The LLVM
libs are from the llvm tarball I believe and the objc library is
probably the gcc 4.1.1 one. What other information would help?

Hi Todd,

>From: llvmdev-bounces@cs.uiuc.edu
>[mailto:llvmdev-bounces@cs.uiuc.edu] On Behalf Of Chris Lattner
>Sent: Monday, October 16, 2006 2:09 PM
>To: LLVM Developers Mailing List
>Subject: Re: [LLVMdev] initializer does not match global variable type.
>
>> I have an objective-c file, bar.m, that I try to process in the
>> following way generating the error shown below. Any help would
>> be appreciated. I suspect the error is in the first few lines
>> of output.
>
>That is really really strange. Can you please file a bugzilla
>report and
>indicate what target you have llvm-gcc configured for?

I don't understand what llvm-gcc has to do with this.

I think in your instructions (below) you referred to using "cfrontend-g
++" which both Chris and I took to mean llvm-g++.

Maybe I wasn't
clear. The "g++" is ordinary vanilla g++, not llvm-g++. Are there
any versioning issues with regular g++ compiling llvm2cpp generated
C++ files?

This could be the weak link. llvm2cpp is a relatively new and fairly
untested addition to the llvm toolsuite. Its purposes are more for
instruction and example generation than for use in compiling. It is
entirely possible that llvm2cpp is not generating *exactly* the
C++ code necessary to recreate your module. This is especially true
since your source language is Objective-C and that has never been tried
with llvm2cpp.

I believe cfrontend-g++ comes from the cfrontend tarball.

Okay, that's the llvm-gcc that Chris was referring to.

llvm2cpp came from the llvm tarball. My g++ was 4.1.1. The LLVM
libs are from the llvm tarball I believe and the objc library is
probably the gcc 4.1.1 one. What other information would help?

Well, what are you trying to accomplish?

>>> cfrontend-g++ -o bar.bc bar.m
>>> llvm2cpp -o bar.cpp bar.bc
>>> g++ -c bar.o bar.cpp
>>> ld -o bar bar.o -l objc -l LLVMCore -l LLVMSupport -l LLVMSystem
>>> ./bar

Are you just trying to get your bar.m program to run? If so, there's a
route that doesn't involve llvm2cpp:

cfrontend-g++ -o bar.bc bar.m # Source -> Bytecode
llc -o bar.s bar.bc # Bytecode -> Native Assembly Source
g++ -S bar.s -o bar.o # Native Assembly -> Object File
g++ -o bar bar.o -lobjc
./bar

Reid.

Maybe I wasn't
clear. The "g++" is ordinary vanilla g++, not llvm-g++. Are there
any versioning issues with regular g++ compiling llvm2cpp generated
C++ files?

Oh, sorry I was confused about what was failing.

This could be the weak link. llvm2cpp is a relatively new and fairly
untested addition to the llvm toolsuite. Its purposes are more for
instruction and example generation than for use in compiling. It is
entirely possible that llvm2cpp is not generating *exactly* the
C++ code necessary to recreate your module. This is especially true
since your source language is Objective-C and that has never been tried
with llvm2cpp.

Right. This looks like it's just a simple bug in llvm2cpp. CppWriter.cpp:698 contains:

     if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) {
       Out << "Constant* " << constName << " = ConstantArray::get(\"";
       printEscapedString(CA->getAsString());
       // Determine if we want null termination or not.
       if (CA->getType()->getNumElements() <= CA->getAsString().length())
         Out << "\", false";// No null terminator
       else
         Out << "\", true"; // Indicate that the null terminator should be added.
       Out << ");";
     } else {

This doesn't handle null terminated strings right (the "<=" check is wrong) and could never handle strings (or other arrays) with nulls in the middle of them. Try removing that chunk of code and seeing if the 'else' block works.

As Reid says, llvm2cpp is at best beta-quality, I don't think anyone has used it for something serious. That said, if you're working on your own llvm2foo, it can be a useful starting point. Another similar but different thing which may be more useful and *is* well tested is the LLVM C backend (lib/Target/CBackend).

-Chris