Ens example
// Code starts to execute at the main function.
s32 main() {
// Named variables.
s32 x = 10
// If statements.
if x == 10 {
print("X is 10! \n")
}
// Range based for loops.
for x {
// For loops have an iterator value called 'it',
// Also note that semicolons are optional.
print("%d \n", it);
}
// Heap memory allocation using LIBC's malloc.
s32 *int_ptr = malloc(size_of(s32))
$int_ptr = 10;
print("%p \n", int_ptr)
print("%d \n", $int_ptr);
free(pointer)
return 0;
}
The output of this simple program is:
X is 10!
0
1
2
3
4
5
6
7
8
9
00000269EE4E7520
10
This is the LLVM ir I generate from the Abstract syntax tree's for the example program.
; ModuleID = 'Ens_llvm_module'
source_filename = "Ens_llvm_module"
@.str = internal constant [11 x i8] c"X is 10! \0A\00"
@.str.1 = internal constant [5 x i8] c"%d \0A\00"
@.str.2 = internal constant [5 x i8] c"%p \0A\00"
@.str.3 = internal constant [5 x i8] c"%d \0A\00"
define i32 @main() {
entry:
%it = alloca i32, align 4
%x = alloca i32, align 4
store i32 10, ptr %x, align 4
%0 = load i32, ptr %x, align 4
%icmpeq = icmp eq i32 %0, 10
br i1 %icmpeq, label %1, label %3
1: ; preds = %entry
%2 = call i32 (ptr, ...) @print(ptr @.str)
br label %3
3: ; preds = %1, %entry
store i32 0, ptr %it, align 4
br label %for_block
for_block: ; preds = %for_block, %3
%4 = load i32, ptr %it, align 4
%5 = call i32 (ptr, ...) @print(ptr @.str.1, i32 %4)
%6 = load i32, ptr %x, align 4
%it1 = load i32, ptr %it, align 4
%it_value = add i32 %it1, 1
store i32 %it_value, ptr %it, align 4
%loop_condition = icmp eq i32 %6, %it_value
br i1 %loop_condition, label %after_for_loop_block, label %for_block
after_for_loop_block: ; preds = %for_block
%int_ptr = alloca ptr, align 8
%7 = call ptr @malloc(i32 4)
store ptr %7, ptr %int_ptr, align 8
%8 = load ptr, ptr %int_ptr, align 8
store i32 10, ptr %8, align 4
%9 = load ptr, ptr %int_ptr, align 8
%10 = call i32 (ptr, ...) @print(ptr @.str.2, ptr %9)
%11 = load ptr, ptr %int_ptr, align 8
%12 = load i32, ptr %11, align 4
%13 = call i32 (ptr, ...) @print(ptr @.str.3, i32 %12)
%14 = load ptr, ptr %int_ptr, align 8
call void @free(ptr %14)
ret i32 0
}
declare i32 @puts(ptr)
declare i32 @print(ptr, ...)
declare ptr @malloc(i32)
declare ptr @memset(ptr, i32, i32)
declare void @free(ptr)
declare i32 @exit(i32)