@@ -486,30 +486,33 @@ pub fn Encoder(comptime chip: Chip, comptime options: Options) type {
486
486
487
487
fn encode_instruction_body (self : * Self , program : * BoundedProgram , diags : * ? Diagnostics ) ! void {
488
488
// first scan through body for labels
489
- var instr_index : u5 = program .origin orelse 0 ;
489
+ var instr_index : ? u5 = program .origin orelse 0 ;
490
490
for (self .tokens [self .index .. ]) | token | {
491
491
switch (token .data ) {
492
492
.label = > | label | try program .labels .append (.{
493
493
.name = label .name ,
494
494
.public = label .public ,
495
- .index = instr_index ,
495
+ .index = instr_index .? ,
496
496
}),
497
- .instruction , .word = > instr_index += 1 ,
497
+ .instruction , .word = > {
498
+ const result , const ov = @addWithOverflow (instr_index .? , 1 );
499
+ instr_index = if (ov != 0 ) null else result ;
500
+ },
498
501
.wrap_target = > {
499
502
if (program .wrap_target != null ) {
500
503
diags .* = Diagnostics .init (token .index , "wrap_target already set for this program" , .{});
501
504
return error .WrapTargetAlreadySet ;
502
505
}
503
506
504
- program .wrap_target = instr_index ;
507
+ program .wrap_target = instr_index .? ;
505
508
},
506
509
.wrap = > {
507
510
if (program .wrap != null ) {
508
511
diags .* = Diagnostics .init (token .index , "wrap already set for this program" , .{});
509
512
return error .WrapAlreadySet ;
510
513
}
511
514
512
- program .wrap = instr_index - 1 ;
515
+ program .wrap = if ( instr_index ) | idx | idx - 1 else 31 ;
513
516
},
514
517
.program = > break ,
515
518
else = > unreachable , // invalid
0 commit comments