Тема: f(const x)
Показать сообщение отдельно
Старый 27.06.2012, 12:46   #9
jimon
 
Сообщений: n/a
Ответ: f(const x)

давайте попробуем заставить llvm ( http://llvm.org/demo/index.cgi ) выдать разный код с const и без, мой пример пока даёт одинаковый код :

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

uintptr_t test1(const int * a)
{
return (uintptr_t)a + 0xff;
}

uintptr_t test2(const int * const a)
{
return (uintptr_t)a + 0xff;
}

int main()
{
int a = 0, b = 0;
printf("%li\n", test1(&a));
printf("%li\n", test2(&b));
}
и выхлоп :

; ModuleID = '/tmp/webcompile/_10893_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@.str = private unnamed_addr constant [5 x i8] c"%li\0A\00", align 1

define i64 @test1(i32* %a) nounwind uwtable readnone {
  %1 = ptrtoint i32* %a to i64
  %2 = add i64 %1, 255
  ret i64 %2
}

define i64 @test2(i32* %a) nounwind uwtable readnone {
  %1 = ptrtoint i32* %a to i64
  %2 = add i64 %1, 255
  ret i64 %2
}

define i32 @main() nounwind uwtable {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4, !tbaa !0
  store i32 0, i32* %b, align 4, !tbaa !0
  %1 = ptrtoint i32* %a to i64
  %2 = add i64 %1, 255
  %3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([5 x i8]* @.str, i64 0, i64 0), i64 %2) nounwind
  %4 = ptrtoint i32* %b to i64
  %5 = add i64 %4, 255
  %6 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([5 x i8]* @.str, i64 0, i64 0), i64 %5) nounwind
  ret i32 0
}

declare i32 @printf(i8* nocapture, ...) nounwind
кодогенератор в x64 вообще интересно свернул llvm ассемблер :
.file	"/tmp/webcompile/_11162_0.bc"
	.text
	.globl	test1
	.align	16, 0x90
	.type	test1,@function
test1:                                  # @test1
.Ltmp0:
	.cfi_startproc
# BB#0:
	leaq	255(%rdi), %rax
	ret
.Ltmp1:
	.size	test1, .Ltmp1-test1
.Ltmp2:
	.cfi_endproc
.Leh_func_end0:

	.globl	test2
	.align	16, 0x90
	.type	test2,@function
test2:                                  # @test2
.Ltmp3:
	.cfi_startproc
# BB#0:
	leaq	255(%rdi), %rax
	ret
.Ltmp4:
	.size	test2, .Ltmp4-test2
.Ltmp5:
	.cfi_endproc
.Leh_func_end1:

	.globl	main
	.align	16, 0x90
	.type	main,@function
main:                                   # @main
.Ltmp7:
	.cfi_startproc
# BB#0:
	pushq	%rax
.Ltmp8:
	.cfi_def_cfa_offset 16
	movl	$0, 4(%rsp)
	movl	$0, (%rsp)
	leaq	259(%rsp), %rsi
	movl	$.L.str, %edi
	xorb	%al, %al
	callq	printf
	leaq	255(%rsp), %rsi
	movl	$.L.str, %edi
	xorb	%al, %al
	callq	printf
	xorl	%eax, %eax
	popq	%rdx
	ret
.Ltmp9:
	.size	main, .Ltmp9-main
.Ltmp10:
	.cfi_endproc
.Leh_func_end2:

	.type	.L.str,@object          # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	 "%li\n"
	.size	.L.str, 5


	.section	".note.GNU-stack","",@progbits
функции в две команды
 
Ответить с цитированием