1

I'm trying LLVM and hitting some walls, like this one. When I compile and run the piece of code below, instead of getting the current year, I'm getting the day of the week:

target datalayout = "e"

declare dllimport x86_stdcallcc void @GetLocalTime(%SYSTEMTIME*)

%SYSTEMTIME = type {
    i16, ; wYear
    i16, ; wMonth
    i16, ; wDayOfWeek
    i16, ; wDay
    i16, ; wHour
    i16, ; wMinute
    i16, ; wSecond
    i16  ; wMilliseconds
}

define i32 @main() {
    %now = alloca %SYSTEMTIME
    call void @GetLocalTime(%SYSTEMTIME* %now)
    %ptr = getelementptr %SYSTEMTIME* %now, %i32 0, %i32 0
    %day = load i16* %ptr
    %int = zext i16 %day to i32
    ret i32 %int
}

Please note that I'm not writing C or C++ code, I'm writing the code above as it is. Can someone point me out what I'm doing wrong? All members of SYSTEMTIME seems to be off by 2 positions...

4

2 回答 2

1

你写:

SYSTEMTIME 的所有成员似乎都偏离了 2 个位置......

这可能是由于对齐错误造成的。特别是你在栈上分配结构,而 LLVM 默认数据布局没有指定栈对齐,而 Windows 32 位需要 4 个字节。为了满足这个要求,添加S32到您的数据布局字符串(或者S128,我猜,对于 64 位 Windows)。

为了验证这一点,我检查了 Clang 在我的 Windows 系统上插入的数据布局字符串,你确实可以S32在最后看到:

"e-p:32:32:32-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-f80:32:32-n8:16:32-S32"
于 2014-01-12T13:15:46.813 回答
0

您声明:

declare dllimport x86_stdcallcc void @GetLocalTime(%SYSTEMTIME*)

这是绝对正确的;但是当你调用它时:

call void @GetLocalTime(%SYSTEMTIME* %now)

您忘记了x86_stdcallcc调用约定。从以下文档call

调用的调用约定必须与目标函数的调用约定匹配,否则行为未定义。

所以也许添加 cc 可以解决这个问题。

于 2014-01-12T22:05:08.597 回答