@@ -54,6 +54,11 @@ use itertools::{chain, Itertools};
5454use num_bigint:: { BigInt , Sign } ;
5555use num_traits:: { cast:: ToPrimitive , Zero } ;
5656
57+ // Necessary memory gaps for the values created on the entry code of each implicit builtin
58+ const SEGMENT_ARENA_GAPS : usize = 4 ;
59+ const GAS_BUILTIN_GAPS : usize = 1 ;
60+ const SYSTEM_BUILTIN_GAPS : usize = 1 ;
61+
5762/// Representation of a cairo argument
5863/// Can consist of a single Felt or an array of Felts
5964#[ derive( Debug , Clone ) ]
@@ -469,20 +474,13 @@ fn load_arguments(
469474 cairo_run_config : & Cairo1RunConfig ,
470475 main_func : & Function ,
471476) -> Result < ( ) , Error > {
472- let got_gas_builtin = main_func
473- . signature
474- . param_types
475- . iter ( )
476- . any ( |ty| ty. debug_name . as_ref ( ) . is_some_and ( |n| n == "GasBuiltin" ) ) ;
477+ let got_gas_builtin = got_implicit_builtin ( & main_func. signature . param_types , "GasBuiltin" ) ;
477478 if cairo_run_config. args . is_empty ( ) && !got_gas_builtin {
478479 // Nothing to be done
479480 return Ok ( ( ) ) ;
480481 }
481- let got_segment_arena = main_func
482- . signature
483- . param_types
484- . iter ( )
485- . any ( |ty| ty. debug_name . as_ref ( ) . is_some_and ( |n| n == "SegmentArena" ) ) ;
482+ let got_segment_arena = got_implicit_builtin ( & main_func. signature . param_types , "SegmentArena" ) ;
483+ let got_system_builtin = got_implicit_builtin ( & main_func. signature . param_types , "System" ) ;
486484 // This AP correction represents the memory slots taken up by the values created by `create_entry_code`:
487485 // These include:
488486 // * The builtin bases (not including output)
@@ -497,10 +495,13 @@ fn load_arguments(
497495 ap_offset += runner. get_program ( ) . builtins_len ( ) - 1 ;
498496 }
499497 if got_segment_arena {
500- ap_offset += 4 ;
498+ ap_offset += SEGMENT_ARENA_GAPS ;
501499 }
502500 if got_gas_builtin {
503- ap_offset += 1 ;
501+ ap_offset += GAS_BUILTIN_GAPS ;
502+ }
503+ if got_system_builtin {
504+ ap_offset += SYSTEM_BUILTIN_GAPS ;
504505 }
505506 for arg in cairo_run_config. args {
506507 match arg {
@@ -547,16 +548,9 @@ fn create_entry_code(
547548) -> Result < ( CasmContext , Vec < BuiltinName > ) , Error > {
548549 let copy_to_output_builtin = config. copy_to_output ( ) ;
549550 let signature = & func. signature ;
550- let got_segment_arena = signature. param_types . iter ( ) . any ( |ty| {
551- get_info ( sierra_program_registry, ty)
552- . map ( |x| x. long_id . generic_id == SegmentArenaType :: ID )
553- . unwrap_or_default ( )
554- } ) ;
555- let got_gas_builtin = signature. param_types . iter ( ) . any ( |ty| {
556- get_info ( sierra_program_registry, ty)
557- . map ( |x| x. long_id . generic_id == GasBuiltinType :: ID )
558- . unwrap_or_default ( )
559- } ) ;
551+ let got_segment_arena = got_implicit_builtin ( & signature. param_types , "SegmentArena" ) ;
552+ let got_gas_builtin = got_implicit_builtin ( & signature. param_types , "GasBuiltin" ) ;
553+ let got_system_builtin = got_implicit_builtin ( & signature. param_types , "System" ) ;
560554 // The builtins in the formatting expected by the runner.
561555 let ( builtins, builtin_offset) =
562556 get_function_builtins ( & signature. param_types , copy_to_output_builtin) ;
@@ -738,10 +732,12 @@ fn create_entry_code(
738732 // We lost the output_ptr var after re-scoping, so we need to create it again
739733 // The last instruction will write the last output ptr so we can find it in [ap - 1]
740734 let output_ptr = ctx. add_var ( CellExpression :: Deref ( deref ! ( [ ap - 1 ] ) ) ) ;
741- // len(builtins - output) + len(builtins) + if segment_arena: segment_arena_ptr + info_ptr + 0 + (segment_arena_ptr + 3) + (gas_builtin)
735+ // len(builtins - output) + len(builtins) + if segment_arena: segment_arena_ptr +
736+ // info_ptr + 0 + (segment_arena_ptr + 3) + (gas_builtin) + (system_builtin)
742737 let offset = ( 2 * builtins. len ( ) - 1
743738 + 4 * got_segment_arena as usize
744- + got_gas_builtin as usize ) as i16 ;
739+ + got_gas_builtin as usize
740+ + got_system_builtin as usize ) as i16 ;
745741 let array_start_ptr = ctx. add_var ( CellExpression :: Deref ( deref ! ( [ fp + offset] ) ) ) ;
746742 let array_end_ptr = ctx. add_var ( CellExpression :: Deref ( deref ! ( [ fp + offset + 1 ] ) ) ) ;
747743 casm_build_extend ! { ctx,
@@ -1008,6 +1004,11 @@ fn check_only_array_felt_return_type(
10081004 _ => false ,
10091005 }
10101006}
1007+ fn got_implicit_builtin ( param_types : & [ ConcreteTypeId ] , builtin_name : & str ) -> bool {
1008+ param_types
1009+ . iter ( )
1010+ . any ( |ty| ty. debug_name . as_ref ( ) . is_some_and ( |n| n == builtin_name) )
1011+ }
10111012
10121013fn is_panic_result ( return_type_id : Option < & ConcreteTypeId > ) -> bool {
10131014 return_type_id
0 commit comments