forked from foonathan/lauf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
145 lines (126 loc) · 4.27 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) 2022-2023 Jonathan Müller and lauf contributors
// SPDX-License-Identifier: BSL-1.0
#include <cstdio>
#include <lauf/asm/builder.h>
#include <lauf/asm/module.h>
#include <lauf/asm/program.h>
#include <lauf/backend/dump.h>
#include <lauf/frontend/text.h>
#include <lauf/lib.h>
#include <lauf/reader.h>
#include <lauf/runtime/builtin.h>
#include <lauf/runtime/process.h>
#include <lauf/runtime/value.h>
#include <lauf/vm.h>
#include <lauf/writer.h>
lauf_asm_module* example_module()
{
auto reader = lauf_create_cstring_reader(R"(
module @test;
function @fib(1 => 1) {
block %entry(1 => 1) {
pick 0; sint 2; $lauf.int.scmp; cc lt;
branch %base(1 => 1) %recurse(1 => 1);
}
block %base(1 => 1) {
return;
}
block %recurse(1 => 1) {
pick 0; sint 1; $lauf.int.ssub_wrap; call @fib;
roll 1; sint 2; $lauf.int.ssub_wrap; call @fib;
uint 0; uint 1; $lauf.int.usub_panic; pop 0;
$lauf.int.sadd_wrap;
return;
}
}
function @fib_local(1 => 1) {
local %n : $lauf.Value;
block %entry(1 => 0) {
pick 0; local_addr %n; store_field $lauf.Value 0;
sint 2; $lauf.int.scmp; cc lt;
branch %base(0 => 1) %recurse(0 => 1);
}
block %base(0 => 1) {
local_addr %n; load_field $lauf.Value 0;
return;
}
block %recurse(0 => 1) {
local_addr %n; load_field $lauf.Value 0; sint 1; $lauf.int.ssub_wrap; call @fib_local;
local_addr %n; load_field $lauf.Value 0; sint 2; $lauf.int.ssub_wrap; call @fib_local;
$lauf.int.sadd_wrap;
return;
}
}
function @fib_generator(0 => 0) {
block %entry(0 => 0) {
uint 0;
pick 0; fiber_suspend (1 => 0);
uint 1;
pick 0; fiber_suspend (1 => 0);
jump %loop(2 => 2);
}
block %loop(2 => 2) {
# a b => b (a+b)
pick 0; roll 2; $lauf.int.uadd_panic;
pick 0; fiber_suspend (1 => 0);
jump %loop(2 => 2);
}
}
function @print_n_fibs(1 => 1) {
block %entry (1 => 2) {
function_addr @fib_generator; $lauf.fiber.create;
roll 1; pick 0; branch %loop(2 => 2) %exit(2 => 1);
}
block %loop(2 => 2) {
# handle n => handle n-1
pick 1; fiber_resume (0 => 1); $lauf.debug.print; pop 0;
uint 1; $lauf.int.usub_wrap;
pick 0; branch %loop(2 => 2) %exit(2 => 1);
}
block %exit(2 => 1) {
# handle 0 => 0
roll 1; $lauf.fiber.destroy;
return;
}
}
)");
lauf_reader_set_path(reader, "prototype.lauf");
auto result = lauf_frontend_text(reader, lauf_frontend_default_text_options);
lauf_destroy_reader(reader);
return result;
}
void dump_module(lauf_asm_module* mod)
{
auto writer = lauf_create_stdout_writer();
lauf_backend_dump(writer, lauf_backend_default_dump_options, mod);
lauf_destroy_writer(writer);
}
void execute(lauf_asm_program program)
{
auto vm = lauf_create_vm(lauf_default_vm_options);
lauf_runtime_value output;
if (lauf_vm_execute_oneshot(vm, program, nullptr, &output))
std::printf("result: %lu\n", output.as_sint);
else
std::puts("panic!");
lauf_destroy_vm(vm);
}
lauf_asm_program create_program(lauf_asm_module* mod)
{
auto chunk = lauf_asm_create_chunk(mod);
auto b = lauf_asm_create_builder(lauf_asm_default_build_options);
lauf_asm_build_chunk(b, mod, chunk, {0, 1});
lauf_asm_inst_uint(b, 3);
lauf_asm_inst_call(b, lauf_asm_find_function_by_name(mod, "fib"));
lauf_asm_inst_return(b);
lauf_asm_build_finish(b);
lauf_asm_destroy_builder(b);
return lauf_asm_create_program_from_chunk(mod, chunk);
}
int main()
{
auto mod = example_module();
dump_module(mod);
execute(create_program(mod));
lauf_asm_destroy_module(mod);
}