Skip to content

Commit

Permalink
use Self type for struct, enum and union
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Oct 27, 2023
1 parent 53f6a5b commit 89ac3e7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 64 deletions.
35 changes: 19 additions & 16 deletions src/vm/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Instruction = @import("instructions.zig").Instruction;

/// Represents the Cairo VM.
pub const CairoVM = struct {
const Self = @This();

// ************************************************************
// * FIELDS *
Expand Down Expand Up @@ -51,15 +52,15 @@ pub const CairoVM = struct {
pub fn init(
allocator: Allocator,
config: Config,
) !CairoVM {
) !Self {
// Initialize the memory segment manager.
const memory_segment_manager = try segments.MemorySegmentManager.init(allocator);
// Initialize the run context.
const run_context = try RunContext.init(allocator);
// Initialize the trace context.
const trace_context = try TraceContext.init(allocator, config.enable_trace);

return CairoVM{
return Self{
.allocator = allocator,
.run_context = run_context,
.segments = memory_segment_manager,
Expand All @@ -69,7 +70,7 @@ pub const CairoVM = struct {
}

/// Safe deallocation of the VM resources.
pub fn deinit(self: *CairoVM) void {
pub fn deinit(self: *Self) void {
// Deallocate the memory segment manager.
self.segments.deinit();
// Deallocate the run context.
Expand All @@ -84,7 +85,7 @@ pub const CairoVM = struct {

/// Do a single step of the VM.
/// Process an instruction cycle using the typical fetch-decode-execute cycle.
pub fn step(self: *CairoVM) !void {
pub fn step(self: *Self) !void {
// TODO: Run hints.

std.log.debug(
Expand Down Expand Up @@ -124,7 +125,7 @@ pub const CairoVM = struct {
// # Arguments
/// - `instruction`: The instruction to run.
pub fn runInstruction(
self: *CairoVM,
self: *Self,
instruction: *const instructions.Instruction,
) !void {
if (!build_options.trace_disable) {
Expand All @@ -145,7 +146,7 @@ pub const CairoVM = struct {
/// # Returns
/// - `Operands`: The operands for the instruction.
pub fn computeOperands(
self: *CairoVM,
self: *Self,
instruction: *const instructions.Instruction,
) !OperandsResult {
// Compute the destination address and get value from the memory.
Expand Down Expand Up @@ -191,7 +192,7 @@ pub const CairoVM = struct {
/// - `dst`: The destination.
/// - `op1`: The op1.
pub fn computeOp0Deductions(
self: *CairoVM,
self: *Self,
op_0_addr: MaybeRelocatable,
instruction: *const instructions.Instruction,
dst: ?MaybeRelocatable,
Expand All @@ -212,7 +213,7 @@ pub const CairoVM = struct {
/// - `MaybeRelocatable`: The deduced value.
/// TODO: Implement this.
pub fn deduceMemoryCell(
self: *CairoVM,
self: *Self,
address: Relocatable,
) !?MaybeRelocatable {
_ = address;
Expand All @@ -225,7 +226,7 @@ pub const CairoVM = struct {
/// - `instruction`: The instruction that was executed.
/// - `operands`: The operands of the instruction.
pub fn updatePc(
self: *CairoVM,
self: *Self,
instruction: *const instructions.Instruction,
operands: OperandsResult,
) !void {
Expand Down Expand Up @@ -287,7 +288,7 @@ pub const CairoVM = struct {
/// - `instruction`: The instruction that was executed.
/// - `operands`: The operands of the instruction.
pub fn updateAp(
self: *CairoVM,
self: *Self,
instruction: *const instructions.Instruction,
operands: OperandsResult,
) !void {
Expand Down Expand Up @@ -324,7 +325,7 @@ pub const CairoVM = struct {
/// - `instruction`: The instruction that was executed.
/// - `operands`: The operands of the instruction.
pub fn updateFp(
self: *CairoVM,
self: *Self,
instruction: *const instructions.Instruction,
operands: OperandsResult,
) !void {
Expand Down Expand Up @@ -365,28 +366,28 @@ pub const CairoVM = struct {
/// Returns whether the run is finished or not.
/// # Returns
/// - `bool`: Whether the run is finished or not.
pub fn isRunFinished(self: *const CairoVM) bool {
pub fn isRunFinished(self: *const Self) bool {
return self.is_run_finished;
}

/// Returns the current ap.
/// # Returns
/// - `MaybeRelocatable`: The current ap.
pub fn getAp(self: *const CairoVM) Relocatable {
pub fn getAp(self: *const Self) Relocatable {
return self.run_context.ap.*;
}

/// Returns the current fp.
/// # Returns
/// - `MaybeRelocatable`: The current fp.
pub fn getFp(self: *const CairoVM) Relocatable {
pub fn getFp(self: *const Self) Relocatable {
return self.run_context.fp.*;
}

/// Returns the current pc.
/// # Returns
/// - `MaybeRelocatable`: The current pc.
pub fn getPc(self: *const CairoVM) Relocatable {
pub fn getPc(self: *const Self) Relocatable {
return self.run_context.pc.*;
}
};
Expand Down Expand Up @@ -488,6 +489,8 @@ pub fn mulOperands(

/// Represents the operands for an instruction.
const OperandsResult = struct {
const Self = @This();

dst: MaybeRelocatable,
res: ?MaybeRelocatable,
op_0: MaybeRelocatable,
Expand All @@ -497,7 +500,7 @@ const OperandsResult = struct {
op_1_addr: Relocatable,

/// Returns a default instance of the OperandsResult struct.
pub fn default() OperandsResult {
pub fn default() Self {
return .{
.dst = relocatable.fromU64(0),
.res = relocatable.fromU64(0),
Expand Down
6 changes: 4 additions & 2 deletions src/vm/instructions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ pub const Opcode = enum {

/// Represents a decoded instruction.
pub const Instruction = struct {
const Self = @This();

off_0: i16,
off_1: i16,
off_2: i16,
Expand All @@ -132,7 +134,7 @@ pub const Instruction = struct {
/// Returns the size of an instruction.
/// # Returns
/// Size of the instruction.
pub fn size(self: Instruction) usize {
pub fn size(self: Self) usize {
if (self.op_1_addr == Op1Src.Imm) {
return 2;
} else {
Expand All @@ -141,7 +143,7 @@ pub const Instruction = struct {
}

/// Returns a default instruction.
pub fn default() Instruction {
pub fn default() Self {
// 0| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
// 15|14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
// | CALL| ADD| JUMP| ADD| IMM| FP| FP
Expand Down
14 changes: 8 additions & 6 deletions src/vm/memory/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const starknet_felt = @import("../../math/fields/starknet.zig");

// Representation of the VM memory.
pub const Memory = struct {
const Self = @This();

// ************************************************************
// * FIELDS *
// ************************************************************
Expand Down Expand Up @@ -45,10 +47,10 @@ pub const Memory = struct {
// - `allocator` - The allocator to use.
// # Returns
// The new memory.
pub fn init(allocator: Allocator) !*Memory {
var memory = try allocator.create(Memory);
pub fn init(allocator: Allocator) !*Self {
var memory = try allocator.create(Self);

memory.* = Memory{
memory.* = Self{
.allocator = allocator,
.data = std.AutoHashMap(
Relocatable,
Expand All @@ -64,7 +66,7 @@ pub const Memory = struct {
}

// Safe deallocation of the memory.
pub fn deinit(self: *Memory) void {
pub fn deinit(self: *Self) void {
// Clear the hash maps
self.data.deinit();
self.validated_addresses.deinit();
Expand All @@ -81,7 +83,7 @@ pub const Memory = struct {
// - `address` - The address to insert the value at.
// - `value` - The value to insert.
pub fn set(
self: *Memory,
self: *Self,
address: Relocatable,
value: MaybeRelocatable,
) error{
Expand Down Expand Up @@ -111,7 +113,7 @@ pub const Memory = struct {
// # Returns
// The value at the given address.
pub fn get(
self: *Memory,
self: *Self,
address: Relocatable,
) error{MemoryOutOfBounds}!MaybeRelocatable {
var maybe_value = self.data.get(address);
Expand Down
42 changes: 23 additions & 19 deletions src/vm/memory/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const CairoVMError = @import("../error.zig").CairoVMError;
// these values are replaced by real memory addresses,
// represented by a field element.
pub const Relocatable = struct {
const Self = @This();

// The index of the memory segment.
segment_index: u64 = 0,
// The offset in the memory segment.
Expand All @@ -21,7 +23,7 @@ pub const Relocatable = struct {
pub fn new(
segment_index: u64,
offset: u64,
) Relocatable {
) Self {
return .{
.segment_index = segment_index,
.offset = offset,
Expand All @@ -34,8 +36,8 @@ pub const Relocatable = struct {
// # Returns
// `true` if they are equal, `false` otherwise.
pub fn eq(
self: Relocatable,
other: Relocatable,
self: Self,
other: Self,
) bool {
return self.segment_index == other.segment_index and self.offset == other.offset;
}
Expand All @@ -46,9 +48,9 @@ pub const Relocatable = struct {
// # Returns
// A new Relocatable.
pub fn subUint(
self: Relocatable,
self: Self,
other: u64,
) !Relocatable {
) !Self {
if (self.offset < other) {
return error.RelocatableSubUsizeNegOffset;
}
Expand All @@ -64,9 +66,9 @@ pub const Relocatable = struct {
// # Returns
// A new Relocatable.
pub fn addUint(
self: Relocatable,
self: Self,
other: u64,
) !Relocatable {
) !Self {
return .{
.segment_index = self.segment_index,
.offset = self.offset + other,
Expand All @@ -78,7 +80,7 @@ pub const Relocatable = struct {
/// - self: Pointer to the Relocatable object to modify.
/// - other: The u64 to add to `self.offset`.
pub fn addUintInPlace(
self: *Relocatable,
self: *Self,
other: u64,
) void {
// Modify the offset of the existing Relocatable object
Expand All @@ -91,9 +93,9 @@ pub const Relocatable = struct {
// # Returns
// A new Relocatable.
pub fn addInt(
self: Relocatable,
self: Self,
other: i64,
) !Relocatable {
) !Self {
if (other < 0) {
return self.subUint(@as(u64, @intCast(
-other,
Expand All @@ -109,7 +111,7 @@ pub const Relocatable = struct {
/// - self: Pointer to the Relocatable object to modify.
/// - other: The felt to add to `self.offset`.
pub fn addFeltInPlace(
self: *Relocatable,
self: *Self,
other: Felt252,
) !void {
const new_offset_felt = Felt252.fromInteger(@as(
Expand All @@ -124,7 +126,7 @@ pub const Relocatable = struct {
/// # Arguments
/// - other - The other MaybeRelocatable to add.
pub fn addMaybeRelocatableInplace(
self: *Relocatable,
self: *Self,
other: MaybeRelocatable,
) !void {
const other_as_felt = try other.tryIntoFelt();
Expand All @@ -135,6 +137,8 @@ pub const Relocatable = struct {
// MaybeRelocatable is the type of the memory cells in the Cairo
// VM. It can either be a Relocatable or a field element.
pub const MaybeRelocatable = union(enum) {
const Self = @This();

relocatable: Relocatable,
felt: Felt252,

Expand All @@ -150,8 +154,8 @@ pub const MaybeRelocatable = union(enum) {
/// * `true` if the two instances are equal.
/// * `false` otherwise.
pub fn eq(
self: MaybeRelocatable,
other: MaybeRelocatable,
self: Self,
other: Self,
) bool {
// Switch on the type of `self`
return switch (self) {
Expand All @@ -175,7 +179,7 @@ pub const MaybeRelocatable = union(enum) {
/// Return the value of the MaybeRelocatable as a felt or error.
/// # Returns
/// The value of the MaybeRelocatable as a Relocatable felt or error.
pub fn tryIntoFelt(self: MaybeRelocatable) error{TypeMismatchNotFelt}!Felt252 {
pub fn tryIntoFelt(self: Self) error{TypeMismatchNotFelt}!Felt252 {
return switch (self) {
.relocatable => CairoVMError.TypeMismatchNotFelt,
.felt => |felt| felt,
Expand All @@ -185,7 +189,7 @@ pub const MaybeRelocatable = union(enum) {
/// Return the value of the MaybeRelocatable as a felt or error.
/// # Returns
/// The value of the MaybeRelocatable as a Relocatable felt or error.
pub fn tryIntoU64(self: MaybeRelocatable) error{
pub fn tryIntoU64(self: Self) error{
TypeMismatchNotFelt,
ValueTooLarge,
}!u64 {
Expand All @@ -198,7 +202,7 @@ pub const MaybeRelocatable = union(enum) {
/// Return the value of the MaybeRelocatable as a Relocatable.
/// # Returns
/// The value of the MaybeRelocatable as a Relocatable.
pub fn tryIntoRelocatable(self: MaybeRelocatable) !Relocatable {
pub fn tryIntoRelocatable(self: Self) !Relocatable {
return switch (self) {
.relocatable => |relocatable| relocatable,
.felt => error.TypeMismatchNotRelocatable,
Expand All @@ -208,7 +212,7 @@ pub const MaybeRelocatable = union(enum) {
/// Whether the MaybeRelocatable is zero or not.
/// # Returns
/// true if the MaybeRelocatable is zero, false otherwise.
pub fn isZero(self: MaybeRelocatable) bool {
pub fn isZero(self: Self) bool {
return switch (self) {
.relocatable => false,
.felt => |felt| felt.isZero(),
Expand All @@ -218,7 +222,7 @@ pub const MaybeRelocatable = union(enum) {
/// Whether the MaybeRelocatable is a relocatable or not.
/// # Returns
/// true if the MaybeRelocatable is a relocatable, false otherwise.
pub fn isRelocatable(self: MaybeRelocatable) bool {
pub fn isRelocatable(self: Self) bool {
return switch (self) {
.relocatable => true,
.felt => false,
Expand Down
Loading

0 comments on commit 89ac3e7

Please sign in to comment.