-
Notifications
You must be signed in to change notification settings - Fork 0
/
rvm.zig
159 lines (115 loc) · 4.65 KB
/
rvm.zig
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const std = @import("std");
const zig_builtin = @import("builtin");
const Config = @import("Config");
const MiscUtils = @import("ZigUtils").Misc;
const CLIMetaData = @import("CLIMetaData");
const TextUtils = @import("ZigUtils").Text;
const ANSI = @import("ZigUtils").ANSI;
const Core = @import("Core");
const Builder = @import("Rbc:Builder");
const log = std.log.scoped(.rvm);
pub const std_options = std.Options {
.log_level = Config.LOG_LEVEL,
.logFn = MiscUtils.FilteredLogger(Config.LOG_SCOPES),
};
const Error = MiscUtils.IOError || std.mem.Allocator.Error || CLIMetaData.CLIError || Core.Fiber.Trap || Builder.Error || error {
TestExpectedEqual,
};
// pub const main = main: {
// if (zig_builtin.mode == .Debug) {
// break :main entry;
// } else {
// break :main struct {
// fn fun() u8 {
// entry() catch {
// return 1;
// };
// return 0;
// }
// }.fun;
// }
// };
// fn entry() Error!void {
// if (zig_builtin.os.tag == .windows) {
// const succ = std.os.windows.kernel32.SetConsoleOutputCP(65001);
// if (succ == 0) {
// const lastErr = std.os.windows.kernel32.GetLastError();
// const safeToPrint = @intFromEnum(lastErr) >= @intFromEnum(std.os.windows.Win32Error.SUCCESS) and @intFromEnum(lastErr) <= @intFromEnum(std.os.windows.Win32Error.IO_REISSUE_AS_CACHED);
// if (safeToPrint) {
// log.warn("failed to set console output code page to UTF-8, error was {s}", .{@tagName(lastErr)});
// } else {
// log.warn("failed to set console output code page to UTF-8, error was {}", .{@intFromEnum(lastErr)});
// }
// }
// }
// const stderr = std.io.getStdErr().writer();
// var GPA = std.heap.GeneralPurposeAllocator(.{}){};
// defer _ = GPA.deinit();
// const gpa = GPA.allocator();
// const args = std.process.argsAlloc(gpa) catch |err| {
// log.err("failed to get command line arguments: {}", .{err});
// return error.Unexpected;
// };
// defer gpa.free(args);
// const endOfOwnArgs =
// for (args, 0..) |arg, i| {
// if (std.mem.eql(u8, arg, "--")) {
// break i;
// }
// } else args.len;
// const scriptArgs = args[@min(endOfOwnArgs + 1, args.len)..];
// const argsResult = try CLIMetaData.processArgs(gpa, args[1..endOfOwnArgs]);
// defer argsResult.deinit();
// switch (argsResult) {
// .exit => return,
// .execute => |x| {
// try earlyTesting(gpa, stderr, x.rootFiles, scriptArgs);
// },
// }
// }
pub fn main() Error!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const context = try Core.Context.init(arena.allocator());
// defer context.deinit();
var builder = try Builder.init(arena.allocator());
// const out_global = try builder.globalNative(@as(i64, 0));
const one = try builder.globalNative(@as(i64, 1));
const two = try builder.globalNative(@as(i64, 2));
const func = try builder.main();
const arg = try func.arg();
const cond = try func.local();
const two_loaded = try func.local();
const one_loaded = try func.local();
try func.entry.read_global_64(two, two_loaded);
try func.entry.read_global_64(one, one_loaded);
try func.entry.s_lt_64(arg, two_loaded, cond);
const thenBlock, const elseBlock = try func.entry.if_nz(cond);
try func.entry.trap();
try thenBlock.ret_v(arg);
const a = try func.local();
try elseBlock.i_sub_64(arg, one_loaded, a);
try elseBlock.call_im_v(func, a, .{a});
const b = try func.local();
try elseBlock.i_sub_64(arg, two_loaded, b);
try elseBlock.call_im_v(func, b, .{b});
try elseBlock.i_add_64(a, b, a);
try elseBlock.ret_v(a);
const program = try builder.assemble(arena.allocator());
// defer program.deinit(arena.allocator());
const fiber = try Core.Fiber.init(context, &program, &[0] Core.Fiber.ForeignFunction {});
defer fiber.deinit();
const n: i64 = 32;
const start = std.time.nanoTimestamp();
const result = try fiber.invoke(i64, program.main, .{ n });
const end = std.time.nanoTimestamp();
const time = @as(f64, @floatFromInt(end - start)) / std.time.ns_per_s;
try std.io.getStdOut().writer().print("result: {} (in {d:.3}s)\n", .{result, time});
try std.testing.expectEqual(2178309, result);
}
// fn fib(n: i64) i64 {
// return if (n < 2) n else fib(n - 1) + fib(n - 2);
// }
test {
std.testing.refAllDeclsRecursive(@This());
}