Files
c3c/test/test_suite/from_docs/examples_forswitch.c3t
Christoffer Lerno 9a6d83f526 Updated stream API.
2023-09-03 01:14:15 +02:00

224 lines
7.4 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
}
; Function Attrs: nounwind
define void @examples.demo_enum(i32 %0) #0 {
entry:
%switch = alloca i32, align 4
%retparam = alloca i64, align 8
%retparam2 = alloca i64, align 8
%switch3 = alloca i32, align 4
%retparam6 = alloca i64, align 8
%retparam8 = alloca i64, align 8
%switch10 = alloca i32, align 4
%retparam14 = alloca i64, align 8
%switch17 = alloca i32, align 4
%switch21 = alloca i32, align 4
%a = alloca i32, align 4
%retparam24 = alloca i64, align 8
%a26 = alloca i32, align 4
%retparam27 = alloca i64, align 8
%retparam29 = alloca i64, 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
%2 = call ptr @std.io.stdout()
%3 = call i64 @std.io.File.printn(ptr %retparam, ptr %2, ptr @.str.1, i64 8)
br label %switch.exit
switch.case1: ; preds = %switch.entry
%4 = call ptr @std.io.stdout()
%5 = call i64 @std.io.File.printn(ptr %retparam2, ptr %4, ptr @.str.2, i64 4)
br label %switch.exit
switch.exit: ; preds = %switch.case1, %switch.case, %switch.entry
store i32 %0, ptr %switch3, align 4
br label %switch.entry4
switch.entry4: ; preds = %switch.exit
%6 = load i32, ptr %switch3, align 4
switch i32 %6, label %switch.exit9 [
i32 0, label %switch.case5
i32 1, label %switch.case5
i32 2, label %switch.case7
]
switch.case5: ; preds = %switch.entry4, %switch.entry4
%7 = call ptr @std.io.stdout()
%8 = call i64 @std.io.File.printn(ptr %retparam6, ptr %7, ptr @.str.3, i64 8)
br label %switch.exit9
switch.case7: ; preds = %switch.entry4
%9 = call ptr @std.io.stdout()
%10 = call i64 @std.io.File.printn(ptr %retparam8, ptr %9, ptr @.str.4, i64 4)
br label %switch.exit9
switch.exit9: ; preds = %switch.case7, %switch.case5, %switch.entry4
store i32 %0, ptr %switch10, align 4
br label %switch.entry11
switch.entry11: ; preds = %switch.exit9
%11 = load i32, ptr %switch10, align 4
switch i32 %11, label %switch.exit16 [
i32 0, label %switch.case12
i32 1, label %switch.case13
i32 2, label %switch.case15
]
switch.case12: ; preds = %switch.entry11
br label %switch.exit16
switch.case13: ; preds = %switch.entry11
%12 = call ptr @std.io.stdout()
%13 = call i64 @std.io.File.printn(ptr %retparam14, ptr %12, ptr @.str.5, i64 6)
br label %switch.exit16
switch.case15: ; preds = %switch.entry11
br label %switch.exit16
switch.exit16: ; preds = %switch.case15, %switch.case13, %switch.case12, %switch.entry11
store i32 %0, ptr %switch17, align 4
br label %switch.entry18
switch.entry18: ; preds = %switch.exit16
%14 = load i32, ptr %switch17, align 4
switch i32 %14, label %switch.default [
i32 0, label %switch.case19
i32 1, label %switch.case19
i32 2, label %switch.case19
]
switch.case19: ; preds = %switch.entry18, %switch.entry18, %switch.entry18
br label %switch.exit20
switch.default: ; preds = %switch.entry18
br label %switch.exit20
switch.exit20: ; preds = %switch.default, %switch.case19
store i32 %0, ptr %switch21, align 4
br label %switch.entry22
switch.entry22: ; preds = %switch.exit20
%15 = load i32, ptr %switch21, align 4
switch i32 %15, label %switch.exit30 [
i32 0, label %switch.case23
i32 1, label %switch.case25
i32 2, label %switch.case28
]
switch.case23: ; preds = %switch.entry22
store i32 1, ptr %a, align 4
%16 = call ptr @std.io.stdout()
%17 = call i64 @std.io.File.printn(ptr %retparam24, ptr %16, ptr @.str.6, i64 1)
br label %switch.case25
switch.case25: ; preds = %switch.entry22, %switch.case23
store i32 2, ptr %a26, align 4
%18 = call ptr @std.io.stdout()
%19 = call i64 @std.io.File.printn(ptr %retparam27, ptr %18, ptr @.str.7, i64 1)
br label %switch.case28
switch.case28: ; preds = %switch.entry22, %switch.case25
%20 = call ptr @std.io.stdout()
%21 = call i64 @std.io.File.printn(ptr %retparam29, ptr %20, ptr @.str.8, i64 1)
br label %switch.exit30
switch.exit30: ; preds = %switch.case28, %switch.entry22
ret void
}