mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
301 lines
9.1 KiB
C
301 lines
9.1 KiB
C
// #target: macos-x64
|
|
module examples;
|
|
import libc;
|
|
import std::io;
|
|
|
|
fn void example_for()
|
|
{
|
|
// the for-loop is the same as C99.
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
libc::printf("%d\n", i);
|
|
}
|
|
|
|
// also equal
|
|
for (;;)
|
|
{
|
|
// ..
|
|
}
|
|
}
|
|
|
|
enum Height : uint
|
|
{
|
|
LOW,
|
|
MEDIUM,
|
|
HIGH,
|
|
}
|
|
|
|
fn void demo_enum(Height h)
|
|
{
|
|
switch (h)
|
|
{
|
|
case LOW:
|
|
case MEDIUM:
|
|
io::printn("Not high");
|
|
// Implicit break.
|
|
case HIGH:
|
|
io::printn("High");
|
|
}
|
|
|
|
// This also works
|
|
switch (h)
|
|
{
|
|
case LOW:
|
|
case MEDIUM:
|
|
io::printn("Not high");
|
|
// Implicit break.
|
|
case Height.HIGH:
|
|
io::printn("High");
|
|
}
|
|
|
|
// Completely empty cases are not allowed.
|
|
switch (h)
|
|
{
|
|
case LOW:
|
|
break; // Explicit break required, since switches can't be empty.
|
|
case MEDIUM:
|
|
io::printn("Medium");
|
|
case HIGH:
|
|
break;
|
|
}
|
|
|
|
// special checking of switching on enum types
|
|
switch (h)
|
|
{
|
|
case LOW:
|
|
case MEDIUM:
|
|
case HIGH:
|
|
break;
|
|
default: // warning: default label in switch which covers all enumeration value
|
|
break;
|
|
}
|
|
|
|
// Using "nextcase" will fallthrough to the next case statement,
|
|
// and each case statement starts its own scope.
|
|
switch (h)
|
|
{
|
|
case LOW:
|
|
int a = 1;
|
|
io::printn("A");
|
|
nextcase;
|
|
case MEDIUM:
|
|
int a = 2;
|
|
io::printn("B");
|
|
nextcase;
|
|
case HIGH:
|
|
// a is not defined here
|
|
io::printn("C");
|
|
}
|
|
}
|
|
/* #expect: examples.ll
|
|
|
|
define void @examples.example_for() #0 {
|
|
entry:
|
|
%i = alloca i32, align 4
|
|
store i32 0, ptr %i, align 4
|
|
br label %loop.cond
|
|
|
|
loop.cond: ; preds = %loop.body, %entry
|
|
%0 = load i32, ptr %i, align 4
|
|
%lt = icmp slt i32 %0, 10
|
|
br i1 %lt, label %loop.body, label %loop.exit
|
|
|
|
loop.body: ; preds = %loop.cond
|
|
%1 = load i32, ptr %i, align 4
|
|
%2 = call i32 (ptr, ...) @printf(ptr @.str, i32 %1)
|
|
%3 = load i32, ptr %i, align 4
|
|
%add = add i32 %3, 1
|
|
store i32 %add, ptr %i, align 4
|
|
br label %loop.cond
|
|
|
|
loop.exit: ; preds = %loop.cond
|
|
%4 = load ptr, ptr @std.core.builtin.panic, align 8
|
|
call void %4(ptr @.panic_msg, i64 19, ptr @.file, i64 21, ptr @.func, i64 11, i32 14)
|
|
unreachable
|
|
|
|
unreachable_block: ; No predecessors!
|
|
ret void
|
|
}
|
|
|
|
; Function Attrs: nounwind
|
|
define void @examples.demo_enum(i32 %0) #0 {
|
|
entry:
|
|
%switch = alloca i32, align 4
|
|
%x = alloca ptr, align 8
|
|
%retparam = alloca i64, align 8
|
|
%result = alloca %File, align 8
|
|
%x2 = alloca ptr, align 8
|
|
%retparam3 = alloca i64, align 8
|
|
%result4 = alloca %File, align 8
|
|
%switch5 = alloca i32, align 4
|
|
%x8 = alloca ptr, align 8
|
|
%retparam9 = alloca i64, align 8
|
|
%result10 = alloca %File, align 8
|
|
%x12 = alloca ptr, align 8
|
|
%retparam13 = alloca i64, align 8
|
|
%result14 = alloca %File, align 8
|
|
%switch16 = alloca i32, align 4
|
|
%x20 = alloca ptr, align 8
|
|
%retparam21 = alloca i64, align 8
|
|
%result22 = alloca %File, align 8
|
|
%switch25 = alloca i32, align 4
|
|
%switch29 = alloca i32, align 4
|
|
%a = alloca i32, align 4
|
|
%x32 = alloca ptr, align 8
|
|
%retparam33 = alloca i64, align 8
|
|
%result34 = alloca %File, align 8
|
|
%a36 = alloca i32, align 4
|
|
%x37 = alloca ptr, align 8
|
|
%retparam38 = alloca i64, align 8
|
|
%result39 = alloca %File, align 8
|
|
%x41 = alloca ptr, align 8
|
|
%retparam42 = alloca i64, align 8
|
|
%result43 = alloca %File, align 8
|
|
store i32 %0, ptr %switch, align 4
|
|
br label %switch.entry
|
|
|
|
switch.entry: ; preds = %entry
|
|
%1 = load i32, ptr %switch, align 4
|
|
switch i32 %1, label %switch.exit [
|
|
i32 0, label %switch.case
|
|
i32 1, label %switch.case
|
|
i32 2, label %switch.case1
|
|
]
|
|
|
|
switch.case: ; preds = %switch.entry, %switch.entry
|
|
store ptr @.str.1, ptr %x, align 8
|
|
%2 = call ptr @std.io.stdout()
|
|
store ptr %2, ptr %result, align 8
|
|
%3 = load ptr, ptr %result, align 8
|
|
%4 = load ptr, ptr %x, align 8
|
|
%5 = call i64 @std.io.File.printn(ptr %retparam, ptr %3, ptr %4, i64 8)
|
|
br label %switch.exit
|
|
|
|
switch.case1: ; preds = %switch.entry
|
|
store ptr @.str.2, ptr %x2, align 8
|
|
%6 = call ptr @std.io.stdout()
|
|
store ptr %6, ptr %result4, align 8
|
|
%7 = load ptr, ptr %result4, align 8
|
|
%8 = load ptr, ptr %x2, align 8
|
|
%9 = call i64 @std.io.File.printn(ptr %retparam3, ptr %7, ptr %8, i64 4)
|
|
br label %switch.exit
|
|
|
|
switch.exit: ; preds = %switch.case1, %switch.case, %switch.entry
|
|
store i32 %0, ptr %switch5, align 4
|
|
br label %switch.entry6
|
|
|
|
switch.entry6: ; preds = %switch.exit
|
|
%10 = load i32, ptr %switch5, align 4
|
|
switch i32 %10, label %switch.exit15 [
|
|
i32 0, label %switch.case7
|
|
i32 1, label %switch.case7
|
|
i32 2, label %switch.case11
|
|
]
|
|
|
|
switch.case7: ; preds = %switch.entry6, %switch.entry6
|
|
store ptr @.str.3, ptr %x8, align 8
|
|
%11 = call ptr @std.io.stdout()
|
|
store ptr %11, ptr %result10, align 8
|
|
%12 = load ptr, ptr %result10, align 8
|
|
%13 = load ptr, ptr %x8, align 8
|
|
%14 = call i64 @std.io.File.printn(ptr %retparam9, ptr %12, ptr %13, i64 8)
|
|
br label %switch.exit15
|
|
|
|
switch.case11: ; preds = %switch.entry6
|
|
store ptr @.str.4, ptr %x12, align 8
|
|
%15 = call ptr @std.io.stdout()
|
|
store ptr %15, ptr %result14, align 8
|
|
%16 = load ptr, ptr %result14, align 8
|
|
%17 = load ptr, ptr %x12, align 8
|
|
%18 = call i64 @std.io.File.printn(ptr %retparam13, ptr %16, ptr %17, i64 4)
|
|
br label %switch.exit15
|
|
|
|
switch.exit15: ; preds = %switch.case11, %switch.case7, %switch.entry6
|
|
store i32 %0, ptr %switch16, align 4
|
|
br label %switch.entry17
|
|
|
|
switch.entry17: ; preds = %switch.exit15
|
|
%19 = load i32, ptr %switch16, align 4
|
|
switch i32 %19, label %switch.exit24 [
|
|
i32 0, label %switch.case18
|
|
i32 1, label %switch.case19
|
|
i32 2, label %switch.case23
|
|
]
|
|
|
|
switch.case18: ; preds = %switch.entry17
|
|
br label %switch.exit24
|
|
|
|
switch.case19: ; preds = %switch.entry17
|
|
store ptr @.str.5, ptr %x20, align 8
|
|
%20 = call ptr @std.io.stdout()
|
|
store ptr %20, ptr %result22, align 8
|
|
%21 = load ptr, ptr %result22, align 8
|
|
%22 = load ptr, ptr %x20, align 8
|
|
%23 = call i64 @std.io.File.printn(ptr %retparam21, ptr %21, ptr %22, i64 6)
|
|
br label %switch.exit24
|
|
|
|
switch.case23: ; preds = %switch.entry17
|
|
br label %switch.exit24
|
|
|
|
switch.exit24: ; preds = %switch.case23, %switch.case19, %switch.case18, %switch.entry17
|
|
store i32 %0, ptr %switch25, align 4
|
|
br label %switch.entry26
|
|
|
|
switch.entry26: ; preds = %switch.exit24
|
|
%24 = load i32, ptr %switch25, align 4
|
|
switch i32 %24, label %switch.default [
|
|
i32 0, label %switch.case27
|
|
i32 1, label %switch.case27
|
|
i32 2, label %switch.case27
|
|
]
|
|
|
|
switch.case27: ; preds = %switch.entry26, %switch.entry26, %switch.entry26
|
|
br label %switch.exit28
|
|
|
|
switch.default: ; preds = %switch.entry26
|
|
br label %switch.exit28
|
|
|
|
switch.exit28: ; preds = %switch.default, %switch.case27
|
|
store i32 %0, ptr %switch29, align 4
|
|
br label %switch.entry30
|
|
|
|
switch.entry30: ; preds = %switch.exit28
|
|
%25 = load i32, ptr %switch29, align 4
|
|
switch i32 %25, label %switch.exit44 [
|
|
i32 0, label %switch.case31
|
|
i32 1, label %switch.case35
|
|
i32 2, label %switch.case40
|
|
]
|
|
|
|
switch.case31: ; preds = %switch.entry30
|
|
store i32 1, ptr %a, align 4
|
|
store ptr @.str.6, ptr %x32, align 8
|
|
%26 = call ptr @std.io.stdout()
|
|
store ptr %26, ptr %result34, align 8
|
|
%27 = load ptr, ptr %result34, align 8
|
|
%28 = load ptr, ptr %x32, align 8
|
|
%29 = call i64 @std.io.File.printn(ptr %retparam33, ptr %27, ptr %28, i64 1)
|
|
br label %switch.case35
|
|
|
|
switch.case35: ; preds = %switch.entry30, %switch.case31
|
|
store i32 2, ptr %a36, align 4
|
|
store ptr @.str.7, ptr %x37, align 8
|
|
%30 = call ptr @std.io.stdout()
|
|
store ptr %30, ptr %result39, align 8
|
|
%31 = load ptr, ptr %result39, align 8
|
|
%32 = load ptr, ptr %x37, align 8
|
|
%33 = call i64 @std.io.File.printn(ptr %retparam38, ptr %31, ptr %32, i64 1)
|
|
br label %switch.case40
|
|
|
|
switch.case40: ; preds = %switch.entry30, %switch.case35
|
|
store ptr @.str.8, ptr %x41, align 8
|
|
%34 = call ptr @std.io.stdout()
|
|
store ptr %34, ptr %result43, align 8
|
|
%35 = load ptr, ptr %result43, align 8
|
|
%36 = load ptr, ptr %x41, align 8
|
|
%37 = call i64 @std.io.File.printn(ptr %retparam42, ptr %35, ptr %36, i64 1)
|
|
br label %switch.exit44
|
|
|
|
switch.exit44: ; preds = %switch.case40, %switch.entry30
|
|
ret void
|
|
}
|