Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llvm CVS: llvm2cpp generates incorrect code when string are used #1897

Closed
llvmbot opened this issue Jun 21, 2007 · 1 comment
Closed

llvm CVS: llvm2cpp generates incorrect code when string are used #1897

llvmbot opened this issue Jun 21, 2007 · 1 comment
Labels
bugzilla Issues migrated from bugzilla llvm-tools All llvm tools that do not have corresponding tag

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 21, 2007

Bugzilla Link 1525
Resolution FIXED
Resolved on Mar 06, 2010 14:00
Version 1.5
OS All
Reporter LLVM Bugzilla Contributor

Extended Description

llvm2cpp generates incorrect code, that does not run correctly when compiled and run, when string are
used:

Starting from the following code:

#include <stdio.h>

int main()
{
printf("foo");
return 0;
}

the generated code is:

// Generated by llvm2cpp - DO NOT MODIFY!

#include <llvm/Module.h>
#include <llvm/DerivedTypes.h>
#include <llvm/Constants.h>
#include <llvm/GlobalVariable.h>
#include <llvm/Function.h>
#include <llvm/CallingConv.h>
#include <llvm/BasicBlock.h>
#include <llvm/Instructions.h>
#include <llvm/InlineAsm.h>
#include <llvm/ParameterAttributes.h>
#include <llvm/Support/MathExtras.h>
#include <llvm/Pass.h>
#include <llvm/PassManager.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Assembly/PrintModulePass.h>
#include
#include

using namespace llvm;

Module* makeLLVMModule();

int main(int argc, char**argv) {
Module* Mod = makeLLVMModule();
verifyModule(*Mod, PrintMessageAction);
std::cerr.flush();
std::cout.flush();
PassManager PM;
PM.add(new PrintModulePass(&llvm::cout));
PM.run(*Mod);
return 0;
}

Module* makeLLVMModule() {
// Module Construction
Module* mod = new Module("function.bc");
mod->setDataLayout("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");
mod->setTargetTriple("i686-apple-darwin8.9.1");

// Type Definitions
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(8), 4);

PointerType* PointerTy_1 = PointerType::get(ArrayTy_0);

std::vector<const Type*>FuncTy_2_args;
ParamAttrsList FuncTy_2_PAL = 0;
FunctionType
FuncTy_2 = FunctionType::get(
/Result=/IntegerType::get(32),
/Params=/FuncTy_2_args,
/isVarArg=/false,
/ParamAttrs=/FuncTy_2_PAL);

std::vector<const Type*>FuncTy_4_args;
PointerType* PointerTy_5 = PointerType::get(IntegerType::get(8));

FuncTy_4_args.push_back(PointerTy_5);
ParamAttrsList FuncTy_4_PAL = 0;
FunctionType
FuncTy_4 = FunctionType::get(
/Result=/IntegerType::get(32),
/Params=/FuncTy_4_args,
/isVarArg=/true,
/ParamAttrs=/FuncTy_4_PAL);

PointerType* PointerTy_3 = PointerType::get(FuncTy_4);

// Function Declarations

Function* func_main = new Function(
/Type=/FuncTy_2,
/Linkage=/GlobalValue::ExternalLinkage,
/Name=/"main", mod);
func_main->setCallingConv(CallingConv::C);

Function* func_printf = new Function(
/Type=/FuncTy_4,
/Linkage=/GlobalValue::ExternalLinkage,
/Name=/"printf", mod); // (external, no body)
func_printf->setCallingConv(CallingConv::C);

// Global Variable Declarations

GlobalVariable* gvar_array__str = new GlobalVariable(
/Type=/ArrayTy_0,
/isConstant=/true,
/Linkage=/GlobalValue::InternalLinkage,
/Initializer=/0, // has initializer, specified below
/Name=/".str",
mod);

// Constant Definitions
Constant* const_array_6 = ConstantArray::get("foo\x00", false);
std::vector<Constant*> const_ptr_7_indices;
Constant* const_int32_8 = Constant::getNullValue(IntegerType::get(32));
const_ptr_7_indices.push_back(const_int32_8);
const_ptr_7_indices.push_back(const_int32_8);
Constant* const_ptr_7 = ConstantExpr::getGetElementPtr(gvar_array__str, &const_ptr_7_indices[0], 2 );

// Global Variable Definitions
gvar_array__str->setInitializer(const_array_6);

// Function Definitions

// Function: main (func_main)
{

BasicBlock* label_entry = new BasicBlock("entry",func_main,0);

// Block entry (label_entry)
CallInst* int32_tmp2 = new CallInst(func_printf, const_ptr_7, "tmp2", label_entry);
int32_tmp2->setCallingConv(CallingConv::C);
int32_tmp2->setTailCall(true);
new ReturnInst(const_int32_8, label_entry);

}

return mod;
}

then when compiled and run give the error:

Global variable initializer type does not match global variable type!
[4 x i8]* @.str
Broken module found, verification continues.
; ModuleID = 'function.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"
target triple = "i686-apple-darwin8.9.1"
@.str = internal constant [4 x i8] c"foo" ; <[4 x i8]*> [#uses=1]

define i32 @​main() {
entry:
%tmp2 = tail call i32 (i8*, ...)* @​printf( i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0) ) ;
[#uses=0]
ret i32 0
}

declare i32 @​printf(i8*, ...)

If I manually edit the gnerated code to be : Constant* const_array_6 = ConstantArray::get("foo ",
false); then it works.

So the issue seems a string termination problem.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jun 25, 2007

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm-tools All llvm tools that do not have corresponding tag
Projects
None yet
Development

No branches or pull requests

1 participant