Question

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...

Was it helpful?

Solution 2

You declare:

declare dllimport x86_stdcallcc void @GetLocalTime(%SYSTEMTIME*)

Which is absolutely correct; but when you call it:

call void @GetLocalTime(%SYSTEMTIME* %now)

You forgot the x86_stdcallcc calling convention. From the documentation of call:

The calling convention of the call must match the calling convention of the target function, or else the behavior is undefined.

So perhaps adding the cc would fix this issue.

OTHER TIPS

You write:

All members of SYSTEMTIME seems to be off by 2 positions...

This might be caused by bad alignment. In particular, you allocate the structure on the stack, and LLVM default data layout does not specify the stack alignment, while Windows 32-bit requires 4 bytes. To satisfy this requirement, add S32 to your data layout string (or S128, I guess, for 64-bit Windows).

To verify this I checked what data layout string Clang inserts on my Windows system, and indeed you can see S32 there, right at the end:

"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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top