Skip to content

Commit 28e4fdc

Browse files
authored
#529 Print output (#530)
closes #529
1 parent 06328b3 commit 28e4fdc

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/cmd/cmd.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ pub fn run() !void {
7979
.required = false,
8080
};
8181

82+
// Command-line option for enabling trace mode.
83+
const print_output = cli.Option{
84+
// The full name of the option.
85+
.long_name = "print-output",
86+
// Description of the option's purpose.
87+
.help = "Print output from Output runner",
88+
// Reference to the trace mode configuration.
89+
.value_ref = r.mkRef(&config.print_output),
90+
// Indicates if the option is required.
91+
.required = false,
92+
};
93+
8294
// Command-line option for specifying the output trace file.
8395
const output_trace = cli.Option{
8496
// The full name of the option.
@@ -161,6 +173,7 @@ pub fn run() !void {
161173
program_option,
162174
output_trace,
163175
output_memory,
176+
print_output,
164177
},
165178
// Action to be executed for the subcommand.
166179
.target = .{ .action = .{ .exec = execute } },

src/vm/cairo_run.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !void {
7070

7171
var entrypoint: []const u8 = "main";
7272

73-
// TODO: add flag for extensive_hints
7473
var runner = try CairoRunner.init(
7574
allocator,
7675
try parsed_program.value.parseProgramJson(allocator, &entrypoint),
@@ -83,7 +82,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !void {
8382

8483
const end = try runner.setupExecutionState(config.allow_missing_builtins orelse config.proof_mode);
8584

86-
// TODO: make flag for extensive_hints
8785
var hint_processor: HintProcessor = .{};
8886
try runner.runUntilPC(end, &hint_processor);
8987
try runner.endRun(
@@ -93,6 +91,16 @@ pub fn runConfig(allocator: Allocator, config: Config) !void {
9391
&hint_processor,
9492
);
9593

94+
if (config.print_output) {
95+
var buf = try std.ArrayList(u8).initCapacity(allocator, 100);
96+
defer buf.deinit();
97+
98+
try buf.appendSlice("Program Output:\n");
99+
try vm.writeOutput(buf.writer());
100+
101+
std.log.debug("{s}", .{buf.items});
102+
}
103+
96104
// TODO readReturnValues necessary for builtins
97105
if (config.output_trace != null or config.output_memory != null) {
98106
try runner.relocate();

src/vm/config.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub const Config = struct {
1616
output_trace: ?[]const u8 = undefined,
1717
/// Write memory to binary file
1818
output_memory: ?[]const u8 = undefined,
19+
/// Print output from Output runner
20+
print_output: bool = false,
1921

2022
allow_missing_builtins: ?bool = null,
2123
};

src/vm/core.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const build_options = @import("../build_options.zig");
2323
const RangeCheckBuiltinRunner = @import("builtins/builtin_runner/range_check.zig").RangeCheckBuiltinRunner;
2424
const SignatureBuiltinRunner = @import("builtins/builtin_runner/signature.zig").SignatureBuiltinRunner;
2525
const BuiltinRunner = @import("builtins/builtin_runner/builtin_runner.zig").BuiltinRunner;
26+
const builtin_runner = @import("builtins/builtin_runner/builtin_runner.zig");
2627
const Felt252 = @import("../math/fields/starknet.zig").Felt252;
2728
const HashBuiltinRunner = @import("./builtins/builtin_runner/hash.zig").HashBuiltinRunner;
2829
const Instruction = instructions.Instruction;
@@ -1357,22 +1358,21 @@ pub const CairoVM = struct {
13571358
///
13581359
/// Returns an error if writing the output fails.
13591360
pub fn writeOutput(self: *Self, writer: anytype) !void {
1360-
var builtin: ?*BuiltinRunner = null;
1361+
var builtin: *BuiltinRunner = val: {
13611362

1362-
// Iterate through the built-in runners to find the output runner.
1363-
for (self.builtin_runners.items) |*runner| {
1364-
if (runner.* == .Output) {
1365-
builtin = runner;
1366-
break;
1363+
// Iterate through the built-in runners to find the output runner.
1364+
for (self.builtin_runners.items) |*runner| {
1365+
if (runner.* == .Output) {
1366+
break :val runner;
1367+
}
13671368
}
1368-
}
1369-
1370-
// If no output runner is found, return.
1371-
if (builtin == null) return;
1369+
// Output runner is not exist, so we just return
1370+
return;
1371+
};
13721372

13731373
// Compute effective sizes of memory segments.
13741374
const segment_used_sizes = try self.segments.computeEffectiveSize(false);
1375-
const segment_index = builtin.?.base();
1375+
const segment_index = builtin.base();
13761376

13771377
// Iterate through the memory segments and write output based on their content.
13781378
for (0..segment_used_sizes.items[@intCast(segment_index)]) |i| {

0 commit comments

Comments
 (0)