mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
110 lines
3.3 KiB
C
110 lines
3.3 KiB
C
// #target: x64-darwin
|
|
module test;
|
|
import libc;
|
|
import std::io;
|
|
|
|
macro timeit(#call)
|
|
{
|
|
long t = (long)libc::clock();
|
|
var $Type = $typeof(#call);
|
|
var $is_void = $Type.typeid == void.typeid;
|
|
$if ($is_void):
|
|
#call;
|
|
$else:
|
|
$Type result = #call;
|
|
$endif;
|
|
long diff = (long)libc::clock() - t;
|
|
libc::printf("'%s' took %lld us\n", $stringify(#call), diff);
|
|
$if (!$is_void):
|
|
return result;
|
|
$endif;
|
|
}
|
|
|
|
fn void test()
|
|
{
|
|
for (int i = 0; i < 1000; i++) libc::printf("%d\n", i);
|
|
}
|
|
|
|
fn void main()
|
|
{
|
|
@timeit(test());
|
|
int a = 100;
|
|
int x = @timeit(1 + 3 + a);
|
|
libc::printf("Result was %d\n", x);
|
|
}
|
|
|
|
/* #expect: test.ll
|
|
|
|
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
|
|
@.str.1 = private unnamed_addr constant [19 x i8] c"'%s' took %lld us\0A\00", align 1
|
|
@.str.2 = private unnamed_addr constant [7 x i8] c"test()\00", align 1
|
|
@.str.3 = private unnamed_addr constant [19 x i8] c"'%s' took %lld us\0A\00", align 1
|
|
@.str.4 = private unnamed_addr constant [10 x i8] c"1 + 3 + a\00", align 1
|
|
@.str.5 = private unnamed_addr constant [15 x i8] c"Result was %d\0A\00", align 1
|
|
|
|
define void @test.test() #0 {
|
|
entry:
|
|
%i = alloca i32, align 4
|
|
store i32 0, i32* %i, align 4
|
|
br label %loop.cond
|
|
|
|
loop.cond: ; preds = %loop.body, %entry
|
|
%0 = load i32, i32* %i, align 4
|
|
%lt = icmp slt i32 %0, 1000
|
|
br i1 %lt, label %loop.body, label %loop.exit
|
|
|
|
loop.body: ; preds = %loop.cond
|
|
%1 = load i32, i32* %i, align 4
|
|
%2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %1)
|
|
%3 = load i32, i32* %i, align 4
|
|
%add = add i32 %3, 1
|
|
store i32 %add, i32* %i, align 4
|
|
br label %loop.cond
|
|
|
|
loop.exit: ; preds = %loop.cond
|
|
ret void
|
|
}
|
|
|
|
define void @test.main() #0 {
|
|
entry:
|
|
%t = alloca i64, align 8
|
|
%diff = alloca i64, align 8
|
|
%a = alloca i32, align 4
|
|
%x = alloca i32, align 4
|
|
%t1 = alloca i64, align 8
|
|
%result = alloca i32, align 4
|
|
%diff2 = alloca i64, align 8
|
|
%0 = call i64 @clock()
|
|
store i64 %0, i64* %t, align 8
|
|
call void @test.test()
|
|
%1 = call i64 @clock()
|
|
%2 = load i64, i64* %t, align 8
|
|
%sub = sub i64 %1, %2
|
|
store i64 %sub, i64* %diff, align 8
|
|
%3 = load i64, i64* %diff, align 8
|
|
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i32 0, i32 0), [6 x i8]* bitcast ([7 x i8]* @.str.2 to [6 x i8]*), i64 %3)
|
|
store i32 100, i32* %a, align 4
|
|
%5 = call i64 @clock()
|
|
store i64 %5, i64* %t1, align 8
|
|
%6 = load i32, i32* %a, align 4
|
|
%add = add i32 4, %6
|
|
store i32 %add, i32* %result, align 4
|
|
%7 = call i64 @clock()
|
|
%8 = load i64, i64* %t1, align 8
|
|
%sub3 = sub i64 %7, %8
|
|
store i64 %sub3, i64* %diff2, align 8
|
|
%9 = load i64, i64* %diff2, align 8
|
|
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i32 0, i32 0), [9 x i8]* bitcast ([10 x i8]* @.str.4 to [9 x i8]*), i64 %9)
|
|
%11 = load i32, i32* %result, align 4
|
|
store i32 %11, i32* %x, align 4
|
|
%12 = load i32, i32* %x, align 4
|
|
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i32 0, i32 0), i32 %12)
|
|
ret void
|
|
}
|
|
|
|
define i32 @main(i32 %0, i8** %1) #0 {
|
|
entry:
|
|
call void @test.main()
|
|
ret i32 0
|
|
}
|