Skip to content

Commit

Permalink
#529 Print output
Browse files Browse the repository at this point in the history
closes #529
  • Loading branch information
StringNick committed Jun 24, 2024
1 parent 06328b3 commit 546249b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
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

0 comments on commit 546249b

Please sign in to comment.