这是 LLVM 中的错误吗?
Is this a bug in LLVM?
我正在编译这段代码(使用 clang 3.4.2):
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
typedef struct __entry {
char *name;
int value;
} entry;
int main(int argv, char **argc) {
printf("Size of entry: %lu\n", sizeof(entry));
entry *entry = malloc(sizeof(entry));
printf("entry is at %lu\n", (uint64_t) entry);
}
我收到了这个位码:
define i32 @main(i32 %argv, i8** %argc) #0 {
entry:
%argv.addr = alloca i32, align 4
%argc.addr = alloca i8**, align 8
%entry1 = alloca %struct.__entry*, align 8
store i32 %argv, i32* %argv.addr, align 4
store i8** %argc, i8*** %argc.addr, align 8
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20 x i8]* @.str, i32 0, i32 0), i64 16)
%call2 = call noalias i8* @malloc(i64 8) #3
%0 = bitcast i8* %call2 to %struct.__entry*
store %struct.__entry* %0, %struct.__entry** %entry1, align 8
%1 = load %struct.__entry** %entry1, align 8
%2 = ptrtoint %struct.__entry* %1 to i64
%call3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @.str1, i32 0, i32 0), i64 %2)
ret i32 0
}
对 printf 的调用接收 16 作为参数(我期望在 64 位系统上具有两个指针的结构)。但是,对 malloc 的调用收到 8。在 C 中,它们都得到相同的参数。怎么回事?
sizeof
的两次调用没有得到相同的参数!乍一看好像是这样。
第一个sizeof(entry)
指的是类型名称。
第二个sizeof(entry)
指的是局部指针变量
定义变量后,不能再引用代码块中的类型
这意味着标题问题的答案是 "No — LLVM is following the requirements of the standard"。
一个小怪癖:第一个 sizeof(entry)
必须有括号,因为参数是类型名称。第二个 sizeof(entry)
可以写成 sizeof entry
因为这个参数是变量的名称而不是类型。这使您可以确认第二次出现是指变量而不是类型。
我正在编译这段代码(使用 clang 3.4.2):
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
typedef struct __entry {
char *name;
int value;
} entry;
int main(int argv, char **argc) {
printf("Size of entry: %lu\n", sizeof(entry));
entry *entry = malloc(sizeof(entry));
printf("entry is at %lu\n", (uint64_t) entry);
}
我收到了这个位码:
define i32 @main(i32 %argv, i8** %argc) #0 {
entry:
%argv.addr = alloca i32, align 4
%argc.addr = alloca i8**, align 8
%entry1 = alloca %struct.__entry*, align 8
store i32 %argv, i32* %argv.addr, align 4
store i8** %argc, i8*** %argc.addr, align 8
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20 x i8]* @.str, i32 0, i32 0), i64 16)
%call2 = call noalias i8* @malloc(i64 8) #3
%0 = bitcast i8* %call2 to %struct.__entry*
store %struct.__entry* %0, %struct.__entry** %entry1, align 8
%1 = load %struct.__entry** %entry1, align 8
%2 = ptrtoint %struct.__entry* %1 to i64
%call3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @.str1, i32 0, i32 0), i64 %2)
ret i32 0
}
对 printf 的调用接收 16 作为参数(我期望在 64 位系统上具有两个指针的结构)。但是,对 malloc 的调用收到 8。在 C 中,它们都得到相同的参数。怎么回事?
sizeof
的两次调用没有得到相同的参数!乍一看好像是这样。
第一个sizeof(entry)
指的是类型名称。
第二个sizeof(entry)
指的是局部指针变量
定义变量后,不能再引用代码块中的类型
这意味着标题问题的答案是 "No — LLVM is following the requirements of the standard"。
一个小怪癖:第一个 sizeof(entry)
必须有括号,因为参数是类型名称。第二个 sizeof(entry)
可以写成 sizeof entry
因为这个参数是变量的名称而不是类型。这使您可以确认第二次出现是指变量而不是类型。