mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
110 lines
2.6 KiB
C
110 lines
2.6 KiB
C
// #target: macos-x64
|
|
|
|
module foo;
|
|
|
|
extern fn void printf(char*, ...);
|
|
|
|
bitstruct Foo : uint
|
|
{
|
|
int x : 1..3;
|
|
uint y : 11..13;
|
|
int z : 15..15;
|
|
}
|
|
|
|
bitstruct Foo2 : char[4]
|
|
{
|
|
int x : 1..3;
|
|
uint y : 11..13;
|
|
int z : 15..15;
|
|
}
|
|
|
|
fn void test()
|
|
{
|
|
Foo b = {};
|
|
int x = (int)b;
|
|
char[4] y = (char[4])b;
|
|
Foo *c = &b;
|
|
c.x;
|
|
int* x2 = (int*)c;
|
|
char[4]* y2 = (char[4]*)c;
|
|
}
|
|
|
|
fn void test2()
|
|
{
|
|
Foo2 b = { 3, 2, -1 };
|
|
int x = (int)b;
|
|
char[4] y = (char[4])b;
|
|
Foo2 *c = &b;
|
|
printf("%d\n", c.x);
|
|
int* x2 = (int*)c;
|
|
char[4]* y2 = (char[4]*)c;
|
|
}
|
|
|
|
fn void main()
|
|
{
|
|
test();
|
|
test2();
|
|
}
|
|
|
|
/* #expect: foo.ll
|
|
|
|
define void @foo.test() #0 {
|
|
entry:
|
|
%b = alloca i32, align 4
|
|
%x = alloca i32, align 4
|
|
%y = alloca [4 x i8], align 1
|
|
%c = alloca i32*, align 8
|
|
%x2 = alloca i32*, align 8
|
|
%y2 = alloca [4 x i8]*, align 8
|
|
store i32 0, i32* %b, align 4
|
|
%0 = load i32, i32* %b, align 4
|
|
store i32 %0, i32* %x, align 4
|
|
%1 = bitcast i32* %b to [4 x i8]*
|
|
%2 = load [4 x i8], [4 x i8]* %1, align 4
|
|
store [4 x i8] %2, [4 x i8]* %y, align 1
|
|
store i32* %b, i32** %c, align 8
|
|
%3 = load i32*, i32** %c, align 8
|
|
%4 = load i32, i32* %3, align 8
|
|
%5 = shl i32 %4, 28
|
|
%6 = ashr i32 %5, 29
|
|
%7 = load i32*, i32** %c, align 8
|
|
store i32* %7, i32** %x2, align 8
|
|
%8 = load i32*, i32** %c, align 8
|
|
%ptrptr = bitcast i32* %8 to [4 x i8]*
|
|
store [4 x i8]* %ptrptr, [4 x i8]** %y2, align 8
|
|
ret void
|
|
}
|
|
|
|
; Function Attrs: nounwind
|
|
define void @foo.test2() #0 {
|
|
entry:
|
|
%b = alloca [4 x i8], align 1
|
|
%x = alloca i32, align 4
|
|
%y = alloca [4 x i8], align 1
|
|
%c = alloca [4 x i8]*, align 8
|
|
%x2 = alloca i32*, align 8
|
|
%y2 = alloca [4 x i8]*, align 8
|
|
store [4 x i8] c"\06\90\00\00", [4 x i8]* %b, align 1
|
|
%0 = bitcast [4 x i8]* %b to i32*
|
|
%1 = load i32, i32* %0, align 1
|
|
store i32 %1, i32* %x, align 4
|
|
%2 = bitcast [4 x i8]* %y to i8*
|
|
%3 = bitcast [4 x i8]* %b to i8*
|
|
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %2, i8* align 1 %3, i32 4, i1 false)
|
|
store [4 x i8]* %b, [4 x i8]** %c, align 8
|
|
%4 = load [4 x i8]*, [4 x i8]** %c, align 8
|
|
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
|
|
%6 = load i8, i8* %5, align 1
|
|
%7 = zext i8 %6 to i32
|
|
%8 = lshr i32 %7, 1
|
|
%9 = shl i32 %8, 29
|
|
%10 = ashr i32 %9, 29
|
|
call void (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %10)
|
|
%11 = load [4 x i8]*, [4 x i8]** %c, align 8
|
|
%ptrptr = bitcast [4 x i8]* %11 to i32*
|
|
store i32* %ptrptr, i32** %x2, align 8
|
|
%12 = load [4 x i8]*, [4 x i8]** %c, align 8
|
|
store [4 x i8]* %12, [4 x i8]** %y2, align 8
|
|
ret void
|
|
}
|