trouble with getelementptr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble with getelementptr

Zalunin Pavel
Hello,
  I have next code:
 ;begin
; ModuleID = 'sample.lz'
@.str1 = internal constant [20 x i8] c"\22hello, cruel world\22"                ; <[20 x i8]*> [#uses=1]
@.str4 = internal constant [9 x i8] c"\22hello, \22"            ; <[9 x i8]*> [#uses=1]
@.str7 = internal constant [7 x i8] c"\22heya!\22"              ; <[7 x i8]*> [#uses=1]

declare i32 @puts(i8*)

declare i8 @strcat(i8*, i8*)

define i32 @main() {
mainBlock:
        %tmp2 = getelementptr [20 x i8]* @.str1, i64 0, i64 0           ; <i8*> [#uses=1]
        %tmp3 = call i32 @puts( i8* %tmp2 )             ; <i32> [#uses=0]
        %tmp5 = getelementptr [9 x i8]* @.str4, i64 0, i64 0            ; <i8*> [#uses=1]
        %tmp6 = call i32 @puts( i8* %tmp5 )             ; <i32> [#uses=0]
        %tmp8 = getelementptr [7 x i8]* @.str7, i64 0, i64 0            ; <i8*> [#uses=1]
        %tmp9 = call i32 @puts( i8* %tmp8 )             ; <i32> [#uses=0]
        ret i32 0
}

 ;end

llvm-as test.ll -o test.ll.bc
llc test.ll.bc -o test.ll.s
gcc test.ll.s -o test.ll.native

After running a see next:

whiter4bbit@LiZa /home/whiter4bbit/programming
/study/yapis/liza $ ./test.ll.native
"hello, cruel world""hello, ""heya!"
"hello, ""heya!"
"heya!"

It isn't a target result:) target is:

whiter4bbit@LiZa /home/whiter4bbit/programming/study/yapis/liza $ ./test.ll.native
"hello, cruel world"
"hello, "
"heya!"

what is the trouble here?
I think about freeing memory after using getelementptr, is it right?(and if it right can anybody answer 'how?')
Help me please...

Best regards, Zalunin Pavel

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: trouble with getelementptr

Dale Johannesen

On Jan 5, 2008, at 4:09 PM, Zalunin Pavel wrote:

> Hello,
>   I have next code:
>  ;begin
> ; ModuleID = 'sample.lz'
> @.str1 = internal constant [20 x i8] c"\22hello, cruel world
> \22"                ; <[20 x i8]*> [#uses=1]
> @.str4 = internal constant [9 x i8] c"\22hello, \22"            ; <
> [9 x i8]*> [#uses=1]
> @.str7 = internal constant [7 x i8] c"\22heya!\22"              ; <
> [7 x i8]*> [#uses=1]
>
> declare i32 @puts(i8*)

puts expects strings to be null-terminated.  Compare output of llvm-
gcc -emit-llvm -S -O0.

> declare i8 @strcat(i8*, i8*)
>
> define i32 @main() {
> mainBlock:
>         %tmp2 = getelementptr [20 x i8]* @.str1, i64 0, i64  
> 0           ; <i8*> [#uses=1]
>         %tmp3 = call i32 @puts( i8* %tmp2 )             ; <i32>  
> [#uses=0]
>         %tmp5 = getelementptr [9 x i8]* @.str4, i64 0, i64  
> 0            ; <i8*> [#uses=1]
>         %tmp6 = call i32 @puts( i8* %tmp5 )             ; <i32>  
> [#uses=0]
>         %tmp8 = getelementptr [7 x i8]* @.str7, i64 0, i64  
> 0            ; <i8*> [#uses=1]
>         %tmp9 = call i32 @puts( i8* %tmp8 )             ; <i32>  
> [#uses=0]
>         ret i32 0
> }
>
>  ;end
>
> llvm-as test.ll -o test.ll.bc
> llc test.ll.bc -o test.ll.s
> gcc test.ll.s -o test.ll.native
>
> After running a see next:
>
> whiter4bbit@LiZa /home/whiter4bbit/programming
> /study/yapis/liza $ ./test.ll.native
> "hello, cruel world""hello, ""heya!"
> "hello, ""heya!"
> "heya!"
>
> It isn't a target result:) target is:
>
> whiter4bbit@LiZa /home/whiter4bbit/programming/study/yapis/liza $ ./
> test.ll.native
> "hello, cruel world"
> "hello, "
> "heya!"
>
> what is the trouble here?
> I think about freeing memory after using getelementptr, is it right?
> (and if it right can anybody answer 'how?')
> Help me please...
>
> Best regards, Zalunin Pavel
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: trouble with getelementptr

Nick Lewycky
In reply to this post by Zalunin Pavel
Hi Zalunin,

Try including a NULL. :) The c"foo" notation does not implicitly add one
for you. Besides that, your use of getelementptr looks correct.

   c"\22hello, cruel world\22\00"

As for freeing memory, don't free global strings. They're deliberately
put in a read-only memory area initialized when the binary starts up.
You can free memory that you malloc, using the instructions of the same
name.

Nick

Zalunin Pavel wrote:

> Hello,
>   I have next code:
>  ;begin
> ; ModuleID = 'sample.lz'
> @.str1 = internal constant [20 x i8] c"\22hello, cruel
> world\22"                ; <[20 x i8]*> [#uses=1]
> @.str4 = internal constant [9 x i8] c"\22hello, \22"            ; <[9 x
> i8]*> [#uses=1]
> @.str7 = internal constant [7 x i8] c"\22heya!\22"              ; <[7 x
> i8]*> [#uses=1]


> declare i32 @puts(i8*)
>
> declare i8 @strcat(i8*, i8*)
>
> define i32 @main() {
> mainBlock:
>         %tmp2 = getelementptr [20 x i8]* @.str1, i64 0, i64 0          
> ; <i8*> [#uses=1]
>         %tmp3 = call i32 @puts( i8* %tmp2 )             ; <i32> [#uses=0]
>         %tmp5 = getelementptr [9 x i8]* @.str4, i64 0, i64 0            
> ; <i8*> [#uses=1]
>         %tmp6 = call i32 @puts( i8* %tmp5 )             ; <i32> [#uses=0]
>         %tmp8 = getelementptr [7 x i8]* @.str7, i64 0, i64 0            
> ; <i8*> [#uses=1]
>         %tmp9 = call i32 @puts( i8* %tmp8 )             ; <i32> [#uses=0]
>         ret i32 0
> }
>
>  ;end
>
> llvm-as test.ll -o test.ll.bc
> llc test.ll.bc -o test.ll.s
> gcc test.ll.s -o test.ll.native
>
> After running a see next:
>
> whiter4bbit@LiZa /home/whiter4bbit/programming
> /study/yapis/liza $ ./test.ll.native
> "hello, cruel world""hello, ""heya!"
> "hello, ""heya!"
> "heya!"
>
> It isn't a target result:) target is:
>
> whiter4bbit@LiZa /home/whiter4bbit/programming/study/yapis/liza $
> ./test.ll.native
> "hello, cruel world"
> "hello, "
> "heya!"
>
> what is the trouble here?
> I think about freeing memory after using getelementptr, is it right?(and
> if it right can anybody answer 'how?')
> Help me please...
>
> Best regards, Zalunin Pavel

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev