Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#529 Print output #530

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ pub fn run() !void {
.required = false,
};

// Command-line option for enabling trace mode.
const print_output = cli.Option{
// The full name of the option.
.long_name = "print-output",
// Description of the option's purpose.
.help = "Print output from Output runner",
// Reference to the trace mode configuration.
.value_ref = r.mkRef(&config.print_output),
// Indicates if the option is required.
.required = false,
};

// Command-line option for specifying the output trace file.
const output_trace = cli.Option{
// The full name of the option.
Expand Down Expand Up @@ -161,6 +173,7 @@ pub fn run() !void {
program_option,
output_trace,
output_memory,
print_output,
},
// Action to be executed for the subcommand.
.target = .{ .action = .{ .exec = execute } },
Expand Down
12 changes: 10 additions & 2 deletions src/vm/cairo_run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !void {

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

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

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

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

if (config.print_output) {
var buf = try std.ArrayList(u8).initCapacity(allocator, 100);
defer buf.deinit();

try buf.appendSlice("Program Output:\n");
try vm.writeOutput(buf.writer());

std.log.debug("{s}", .{buf.items});
}

// TODO readReturnValues necessary for builtins
if (config.output_trace != null or config.output_memory != null) {
try runner.relocate();
Expand Down
2 changes: 2 additions & 0 deletions src/vm/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub const Config = struct {
output_trace: ?[]const u8 = undefined,
/// Write memory to binary file
output_memory: ?[]const u8 = undefined,
/// Print output from Output runner
print_output: bool = false,

allow_missing_builtins: ?bool = null,
};
22 changes: 11 additions & 11 deletions src/vm/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const build_options = @import("../build_options.zig");
const RangeCheckBuiltinRunner = @import("builtins/builtin_runner/range_check.zig").RangeCheckBuiltinRunner;
const SignatureBuiltinRunner = @import("builtins/builtin_runner/signature.zig").SignatureBuiltinRunner;
const BuiltinRunner = @import("builtins/builtin_runner/builtin_runner.zig").BuiltinRunner;
const builtin_runner = @import("builtins/builtin_runner/builtin_runner.zig");
const Felt252 = @import("../math/fields/starknet.zig").Felt252;
const HashBuiltinRunner = @import("./builtins/builtin_runner/hash.zig").HashBuiltinRunner;
const Instruction = instructions.Instruction;
Expand Down Expand Up @@ -1357,22 +1358,21 @@ pub const CairoVM = struct {
///
/// Returns an error if writing the output fails.
pub fn writeOutput(self: *Self, writer: anytype) !void {
var builtin: ?*BuiltinRunner = null;
var builtin: *BuiltinRunner = val: {

// Iterate through the built-in runners to find the output runner.
for (self.builtin_runners.items) |*runner| {
if (runner.* == .Output) {
builtin = runner;
break;
// Iterate through the built-in runners to find the output runner.
for (self.builtin_runners.items) |*runner| {
if (runner.* == .Output) {
break :val runner;
}
}
}

// If no output runner is found, return.
if (builtin == null) return;
// Output runner is not exist, so we just return
return;
};

// Compute effective sizes of memory segments.
const segment_used_sizes = try self.segments.computeEffectiveSize(false);
const segment_index = builtin.?.base();
const segment_index = builtin.base();

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