Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Support Darwin x64 #1049

Closed
wants to merge 1 commit into from
Closed
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
Binary file added cairo1/darwin_x64/bin/cairo-compile
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/cairo-format
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/cairo-language-server
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/cairo-run
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/cairo-test
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/sierra-compile
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/starknet-compile
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/starknet-sierra-compile
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/warp
Binary file not shown.
Binary file added cairo1/darwin_x64/bin/warp-language-server
Binary file not shown.
7 changes: 7 additions & 0 deletions cairo1/darwin_x64/corelib/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "core"
version = "1.0.0-alpha.7"

# NOTE: This is non-public, unstable Scarb's field, which instructs resolver that this package does not
# depend on `core`, which is only true for this particular package. Nobody else should use it.
no-core = true
2 changes: 2 additions & 0 deletions cairo1/darwin_x64/corelib/cairo_project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[crate_roots]
core = "src"
166 changes: 166 additions & 0 deletions cairo1/darwin_x64/corelib/src/array.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use traits::IndexView;

use box::BoxTrait;
use gas::withdraw_gas;
use option::OptionTrait;

extern type Array<T>;
extern fn array_new<T>() -> Array<T> nopanic;
extern fn array_append<T>(ref arr: Array<T>, value: T) nopanic;
extern fn array_pop_front<T>(ref arr: Array<T>) -> Option<Box<T>> nopanic;
extern fn array_snapshot_pop_front<T>(ref arr: @Array<T>) -> Option<Box<@T>> nopanic;
extern fn array_snapshot_pop_back<T>(ref arr: @Array<T>) -> Option<Box<@T>> nopanic;
#[panic_with('Index out of bounds', array_at)]
extern fn array_get<T>(
arr: @Array<T>, index: usize
) -> Option<Box<@T>> implicits(RangeCheck) nopanic;
extern fn array_slice<T>(
arr: @Array<T>, start: usize, length: usize
) -> Option<@Array<T>> implicits(RangeCheck) nopanic;
extern fn array_len<T>(arr: @Array<T>) -> usize nopanic;

trait ArrayTrait<T> {
fn new() -> Array<T>;
fn append(ref self: Array<T>, value: T);
fn pop_front(ref self: Array<T>) -> Option<T> nopanic;
fn get(self: @Array<T>, index: usize) -> Option<Box<@T>>;
fn at(self: @Array<T>, index: usize) -> @T;
fn len(self: @Array<T>) -> usize;
fn is_empty(self: @Array<T>) -> bool;
fn span(self: @Array<T>) -> Span<T>;
}
impl ArrayImpl<T> of ArrayTrait<T> {
#[inline(always)]
fn new() -> Array<T> {
array_new()
}
#[inline(always)]
fn append(ref self: Array<T>, value: T) {
array_append(ref self, value)
}
#[inline(always)]
fn pop_front(ref self: Array<T>) -> Option<T> nopanic {
match array_pop_front(ref self) {
Option::Some(x) => Option::Some(x.unbox()),
Option::None(_) => Option::None(()),
}
}
#[inline(always)]
fn get(self: @Array<T>, index: usize) -> Option<Box<@T>> {
array_get(self, index)
}
fn at(self: @Array<T>, index: usize) -> @T {
array_at(self, index).unbox()
}
#[inline(always)]
fn len(self: @Array<T>) -> usize {
array_len(self)
}
#[inline(always)]
fn is_empty(self: @Array<T>) -> bool {
self.len() == 0_usize
}

#[inline(always)]
fn span(self: @Array<T>) -> Span<T> {
Span { snapshot: self }
}
}

impl ArrayIndex<T> of IndexView<Array<T>, usize, @T> {
fn index(self: @Array<T>, index: usize) -> @T {
array_at(self, index).unbox()
}
}

// Impls for common generic types
impl ArrayDrop<T, impl TDrop: Drop<T>> of Drop<Array<T>>;

// Span.
struct Span<T> {
snapshot: @Array<T>
}

impl SpanCopy<T> of Copy<Span<T>>;
impl SpanDrop<T> of Drop<Span<T>>;

trait SpanTrait<T> {
fn pop_front(ref self: Span<T>) -> Option<@T>;
fn pop_back(ref self: Span<T>) -> Option<@T>;
fn get(self: Span<T>, index: usize) -> Option<Box<@T>>;
fn at(self: Span<T>, index: usize) -> @T;
fn slice(self: Span<T>, start: usize, length: usize) -> Span<T>;
fn len(self: Span<T>) -> usize;
fn is_empty(self: Span<T>) -> bool;
}
impl SpanImpl<T> of SpanTrait<T> {
#[inline(always)]
fn pop_front(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
let item = array_snapshot_pop_front(ref snapshot);
self = Span { snapshot };
match item {
Option::Some(x) => Option::Some(x.unbox()),
Option::None(_) => Option::None(()),
}
}

#[inline(always)]
fn pop_back(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
let item = array_snapshot_pop_back(ref snapshot);
self = Span { snapshot };
match item {
Option::Some(x) => Option::Some(x.unbox()),
Option::None(_) => Option::None(()),
}
}

#[inline(always)]
fn get(self: Span<T>, index: usize) -> Option<Box<@T>> {
array_get(self.snapshot, index)
}
#[inline(always)]
fn at(self: Span<T>, index: usize) -> @T {
array_at(self.snapshot, index).unbox()
}
#[inline(always)]
fn slice(self: Span<T>, start: usize, length: usize) -> Span<T> {
Span { snapshot: array_slice(self.snapshot, start, length).expect('Index out of bounds') }
}
#[inline(always)]
fn len(self: Span<T>) -> usize {
array_len(self.snapshot)
}
#[inline(always)]
fn is_empty(self: Span<T>) -> bool {
self.len() == 0_usize
}
}

impl SpanIndex<T> of IndexView<Span<T>, usize, @T> {
#[inline(always)]
fn index(self: @Span<T>, index: usize) -> @T {
array_at(*self.snapshot, index).unbox()
}
}

// TODO(spapini): Remove TDrop. It is necessary to get rid of response in case of panic.
impl ArrayTCloneImpl<T, impl TClone: Clone<T>, impl TDrop: Drop<T>> of Clone<Array<T>> {
fn clone(self: @Array<T>) -> Array<T> {
let mut response = array_new();
let mut span = self.span();
loop {
withdraw_gas().expect('Out of gas');
match span.pop_front() {
Option::Some(v) => {
response.append(TClone::clone(v));
},
Option::None(_) => {
break ();
},
};
};
response
}
}
25 changes: 25 additions & 0 deletions cairo1/darwin_x64/corelib/src/box.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extern type Box<T>;
impl BoxTCopy<T, impl TCopy: Copy<T>> of Copy<Box<T>>;
impl BoxTDrop<T, impl TDrop: Drop<T>> of Drop<Box<T>>;

// These functions are only exposed in the corelib through the trait below since calling them
// directly with tuples panics due to auto unpacking of the tuple.
// TODO(Gil): Expose in the core lib when the described behaviour is fixed.
extern fn into_box<T>(value: T) -> Box<T> nopanic;
extern fn unbox<T>(box: Box<T>) -> T nopanic;

trait BoxTrait<T> {
fn new(value: T) -> Box<T> nopanic;
fn unbox(self: Box<T>) -> T nopanic;
}

impl BoxImpl<T> of BoxTrait<T> {
#[inline(always)]
fn new(value: T) -> Box<T> nopanic {
into_box(value)
}
#[inline(always)]
fn unbox(self: Box<T>) -> T nopanic {
unbox(self)
}
}
9 changes: 9 additions & 0 deletions cairo1/darwin_x64/corelib/src/clone.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Clone<T> {
fn clone(self: @T) -> T;
}

impl TCopyClone<T, impl TCopy: Copy<T>> of Clone<T> {
fn clone(self: @T) -> T {
*self
}
}
97 changes: 97 additions & 0 deletions cairo1/darwin_x64/corelib/src/debug.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use array::ArrayTrait;
use traits::Into;
use starknet::ContractAddressIntoFelt252;
use option::Option;

// Usage:
//
// use debug::PrintTrait;
//
// 1.print();
//
// (1 == 2).print();
//
// get_caller_address().print();
//
// let mut arr = ArrayTrait::new();
// arr.append('1234567890123456789012345678901');
// arr.append('Sca');
// arr.append('SomeVeryLongMessage');
// arr.print();

extern fn print(message: Array<felt252>) nopanic;

fn print_felt252(message: felt252) {
let mut arr = ArrayTrait::new();
arr.append(message);
print(arr);
}

trait PrintTrait<T> {
fn print(self: T);
}

impl Felt252PrintImpl of PrintTrait<felt252> {
fn print(self: felt252) {
print_felt252(self);
}
}

impl BoolPrintImpl of PrintTrait<bool> {
fn print(self: bool) {
if self {
'true'.print();
} else {
'false'.print();
}
}
}

impl ContractAddressPrintImpl of PrintTrait<starknet::ContractAddress> {
fn print(self: starknet::ContractAddress) {
self.into().print();
}
}

impl U8PrintImpl of PrintTrait<u8> {
fn print(self: u8) {
self.into().print();
}
}

impl U16PrintImpl of PrintTrait<u16> {
fn print(self: u16) {
self.into().print();
}
}

impl U32PrintImpl of PrintTrait<u32> {
fn print(self: u32) {
self.into().print();
}
}

impl U64PrintImpl of PrintTrait<u64> {
fn print(self: u64) {
self.into().print();
}
}

impl U128PrintImpl of PrintTrait<u128> {
fn print(self: u128) {
self.into().print();
}
}

impl U256PrintImpl of PrintTrait<u256> {
fn print(self: u256) {
self.low.into().print();
self.high.into().print();
}
}

impl ArrayGenericPrintImpl of PrintTrait<Array<felt252>> {
fn print(mut self: Array<felt252>) {
print(self);
}
}
55 changes: 55 additions & 0 deletions cairo1/darwin_x64/corelib/src/dict.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use traits::Index;

extern type Felt252Dict<T>;
extern type SquashedFelt252Dict<T>;
impl SquashedFelt252DictDrop<T, impl TDrop: Drop<T>> of Drop<SquashedFelt252Dict<T>>;

extern fn felt252_dict_new<T>() -> Felt252Dict<T> implicits(SegmentArena) nopanic;
extern fn felt252_dict_write<T>(ref dict: Felt252Dict<T>, key: felt252, value: T) nopanic;
extern fn felt252_dict_read<T>(ref dict: Felt252Dict<T>, key: felt252) -> T nopanic;

/// Squashes the dictionary and returns SquashedFelt252Dict.
///
/// NOTE: Never use this libfunc directly. Use Felt252DictTrait::squash() instead. Using this
/// libfunc directly will result in multiple unnecessary copies of the libfunc in the compiled CASM
/// code.
extern fn felt252_dict_squash<T>(
dict: Felt252Dict<T>
) -> SquashedFelt252Dict<T> implicits(RangeCheck, GasBuiltin, SegmentArena) nopanic;

trait Felt252DictTrait<T> {
fn new() -> Felt252Dict<T>;
fn insert(ref self: Felt252Dict<T>, key: felt252, value: T);
fn get(ref self: Felt252Dict<T>, key: felt252) -> T;
fn squash(self: Felt252Dict<T>) -> SquashedFelt252Dict<T> nopanic;
}
impl Felt252DictImpl<T> of Felt252DictTrait<T> {
fn new() -> Felt252Dict<T> {
felt252_dict_new()
}
fn insert(ref self: Felt252Dict<T>, key: felt252, value: T) {
felt252_dict_write(ref self, key, value)
}
fn get(ref self: Felt252Dict<T>, key: felt252) -> T {
felt252_dict_read(ref self, key)
}
#[inline(never)]
fn squash(self: Felt252Dict<T>) -> SquashedFelt252Dict<T> nopanic {
felt252_dict_squash(self)
}
}

impl Felt252DictDestruct<T, impl TDrop: Drop<T>> of Destruct<Felt252Dict<T>> {
#[inline(always)]
fn destruct(self: Felt252Dict<T>) nopanic {
self.squash();
}
}

impl Felt252DictIndex<T> of Index<Felt252Dict<T>, felt252, T> {
#[inline(always)]
fn index(ref self: Felt252Dict<T>, index: felt252) -> T {
felt252_dict_read(ref self, index)
}
}

Loading