@@ -149,16 +149,28 @@ fn compile_function(
149149 compiler. const_malloc_vars . clear ( ) ;
150150 ( compiler. dead_fp_relative_vars , compiler. dead_store_vars ) = compute_dead_vars ( & function. instructions ) ;
151151
152- compile_lines (
153- & Label :: function ( function. name . clone ( ) ) ,
154- & function. instructions ,
155- compiler,
156- None ,
157- )
152+ let mut instructions = Vec :: new ( ) ;
153+
154+ // Emit ParallelBatchStart for parallel loop functions.
155+ // The first instruction is always `diff = iterator - end`. The end value is either
156+ // a function argument (runtime) or a compile-time constant.
157+ if function. name . starts_with ( "@parallel_loop_" ) {
158+ let SimpleLine :: Assignment { arg1, .. } = & function. instructions [ 0 ] else {
159+ panic ! ( "parallel loop: expected first instruction to be `diff = i - end`" ) ;
160+ } ;
161+ let end_value = IntermediateValue :: from_simple_expr ( arg1, compiler) ;
162+ instructions. push ( IntermediateInstruction :: ParallelBatchStart {
163+ n_args : function. arguments . len ( ) ,
164+ end_value,
165+ } ) ;
166+ }
167+
168+ instructions. extend ( compile_lines ( & function. instructions , compiler, None ) ?) ;
169+
170+ Ok ( instructions)
158171}
159172
160173fn compile_lines (
161- function_name : & Label ,
162174 lines : & [ SimpleLine ] ,
163175 compiler : & mut Compiler ,
164176 final_jump : Option < Label > ,
@@ -263,14 +275,13 @@ fn compile_lines(
263275 for arm in arms. iter ( ) {
264276 compiler. stack_pos = saved_stack_pos;
265277 compiler. stack_frame_layout . scopes . push ( ScopeLayout :: default ( ) ) ;
266- let arm_instructions = compile_lines ( function_name , arm, compiler, Some ( end_label. clone ( ) ) ) ?;
278+ let arm_instructions = compile_lines ( arm, compiler, Some ( end_label. clone ( ) ) ) ?;
267279 compiled_arms. push ( arm_instructions) ;
268280 compiler. stack_frame_layout . scopes . pop ( ) ;
269281 new_stack_pos = new_stack_pos. max ( compiler. stack_pos ) ;
270282 }
271283 compiler. stack_pos = new_stack_pos;
272284 compiler. match_blocks . push ( MatchBlock {
273- function_name : function_name. clone ( ) ,
274285 match_cases : compiled_arms,
275286 } ) ;
276287 // Get the actual index AFTER pushing (nested matches may have pushed their blocks first)
@@ -317,7 +328,7 @@ fn compile_lines(
317328 updated_fp : None ,
318329 } ) ;
319330
320- let remaining = compile_lines ( function_name , & lines[ i + 1 ..] , compiler, final_jump) ?;
331+ let remaining = compile_lines ( & lines[ i + 1 ..] , compiler, final_jump) ?;
321332 compiler. bytecode . insert ( end_label, remaining) ;
322333
323334 compiler. stack_frame_layout . scopes . pop ( ) ;
@@ -409,22 +420,22 @@ fn compile_lines(
409420 let saved_stack_pos = compiler. stack_pos ;
410421
411422 compiler. stack_frame_layout . scopes . push ( ScopeLayout :: default ( ) ) ;
412- let then_instructions = compile_lines ( function_name , then_branch, compiler, Some ( end_label. clone ( ) ) ) ?;
423+ let then_instructions = compile_lines ( then_branch, compiler, Some ( end_label. clone ( ) ) ) ?;
413424
414425 let then_stack_pos = compiler. stack_pos ;
415426 compiler. stack_pos = saved_stack_pos;
416427 compiler. stack_frame_layout . scopes . pop ( ) ;
417428 compiler. stack_frame_layout . scopes . push ( ScopeLayout :: default ( ) ) ;
418429
419- let else_instructions = compile_lines ( function_name , else_branch, compiler, Some ( end_label. clone ( ) ) ) ?;
430+ let else_instructions = compile_lines ( else_branch, compiler, Some ( end_label. clone ( ) ) ) ?;
420431
421432 compiler. bytecode . insert ( if_label, then_instructions) ;
422433 compiler. bytecode . insert ( else_label, else_instructions) ;
423434
424435 compiler. stack_frame_layout . scopes . pop ( ) ;
425436 compiler. stack_pos = compiler. stack_pos . max ( then_stack_pos) ;
426437
427- let remaining = compile_lines ( function_name , & lines[ i + 1 ..] , compiler, final_jump) ?;
438+ let remaining = compile_lines ( & lines[ i + 1 ..] , compiler, final_jump) ?;
428439 compiler. bytecode . insert ( end_label, remaining) ;
429440 // It is not necessary to update compiler.stack_size here because the preceding call to
430441 // compile_lines should have done so.
@@ -485,7 +496,7 @@ fn compile_lines(
485496 } ) ;
486497 }
487498
488- instructions. extend ( compile_lines ( function_name , & lines[ i + 1 ..] , compiler, final_jump) ?) ;
499+ instructions. extend ( compile_lines ( & lines[ i + 1 ..] , compiler, final_jump) ?) ;
489500
490501 instructions
491502 } ;
0 commit comments