@@ -134,12 +134,9 @@ impl CodeGeneratingVisitor<'_> {
134
134
BinaryOperation :: Xor => String :: from ( "xor" ) ,
135
135
} ;
136
136
137
- let destination_register = format ! ( "r{}" , self . next_register) ;
137
+ let destination_register = self . next_register ( ) ;
138
138
let binary_instruction = format ! ( " {opcode} {left_operand} {right_operand} into {destination_register};\n " , ) ;
139
139
140
- // Increment the register counter.
141
- self . next_register += 1 ;
142
-
143
140
// Concatenate the instructions.
144
141
let mut instructions = left_instructions;
145
142
instructions. push_str ( & right_instructions) ;
@@ -152,9 +149,7 @@ impl CodeGeneratingVisitor<'_> {
152
149
let ( expression_operand, mut instructions) = self . visit_expression ( & input. expression ) ;
153
150
154
151
// Construct the destination register.
155
- let destination_register = format ! ( "r{}" , self . next_register) ;
156
- // Increment the register counter.
157
- self . next_register += 1 ;
152
+ let destination_register = self . next_register ( ) ;
158
153
159
154
let cast_instruction = format ! (
160
155
" cast {expression_operand} into {destination_register} as {};\n " ,
@@ -177,9 +172,7 @@ impl CodeGeneratingVisitor<'_> {
177
172
}
178
173
179
174
// Construct the destination register.
180
- let destination_register = format ! ( "r{}" , self . next_register) ;
181
- // Increment the register counter.
182
- self . next_register += 1 ;
175
+ let destination_register = self . next_register ( ) ;
183
176
184
177
// Get the array type.
185
178
let Some ( array_type @ Type :: Array ( ..) ) = self . state . type_table . get ( & input. id ) else {
@@ -213,12 +206,9 @@ impl CodeGeneratingVisitor<'_> {
213
206
UnaryOperation :: ToYCoordinate => ( "cast" , " as group.y" ) ,
214
207
} ;
215
208
216
- let destination_register = format ! ( "r{}" , self . next_register) ;
209
+ let destination_register = self . next_register ( ) ;
217
210
let unary_instruction = format ! ( " {opcode} {expression_operand} into {destination_register}{suffix};\n " ) ;
218
211
219
- // Increment the register counter.
220
- self . next_register += 1 ;
221
-
222
212
// Concatenate the instructions.
223
213
let mut instructions = expression_instructions;
224
214
instructions. push_str ( & unary_instruction) ;
@@ -231,14 +221,11 @@ impl CodeGeneratingVisitor<'_> {
231
221
let ( if_true_operand, if_true_instructions) = self . visit_expression ( & input. if_true ) ;
232
222
let ( if_false_operand, if_false_instructions) = self . visit_expression ( & input. if_false ) ;
233
223
234
- let destination_register = format ! ( "r{}" , self . next_register) ;
224
+ let destination_register = self . next_register ( ) ;
235
225
let ternary_instruction = format ! (
236
226
" ternary {condition_operand} {if_true_operand} {if_false_operand} into {destination_register};\n " ,
237
227
) ;
238
228
239
- // Increment the register counter.
240
- self . next_register += 1 ;
241
-
242
229
// Concatenate the instructions.
243
230
let mut instructions = condition_instructions;
244
231
instructions. push_str ( & if_true_instructions) ;
@@ -287,15 +274,12 @@ impl CodeGeneratingVisitor<'_> {
287
274
}
288
275
289
276
// Push destination register to struct init instruction.
290
- let destination_register = format ! ( "r{}" , self . next_register) ;
277
+ let destination_register = self . next_register ( ) ;
291
278
writeln ! ( struct_init_instruction, "into {destination_register} as {name};" , )
292
279
. expect ( "failed to write to string" ) ;
293
280
294
281
instructions. push_str ( & struct_init_instruction) ;
295
282
296
- // Increment the register counter.
297
- self . next_register += 1 ;
298
-
299
283
( destination_register, instructions)
300
284
}
301
285
@@ -339,13 +323,6 @@ impl CodeGeneratingVisitor<'_> {
339
323
} )
340
324
. collect :: < Vec < _ > > ( ) ;
341
325
342
- // Helper function to get a destination register for a function call.
343
- let mut get_destination_register = || {
344
- let destination_register = format ! ( "r{}" , self . next_register) ;
345
- self . next_register += 1 ;
346
- destination_register
347
- } ;
348
-
349
326
// Helper function to construct the instruction associated with a simple function call.
350
327
// This assumes that the function call has one output.
351
328
let mut construct_simple_function_call = |function : & Identifier , variant : & str , arguments : Vec < String > | {
@@ -359,7 +336,7 @@ impl CodeGeneratingVisitor<'_> {
359
336
for argument in arguments {
360
337
write ! ( instruction, " {argument}" ) . expect ( "failed to write to string" ) ;
361
338
}
362
- let destination_register = get_destination_register ( ) ;
339
+ let destination_register = self . next_register ( ) ;
363
340
writeln ! ( instruction, " into {destination_register} as {return_type};" ) . expect ( "failed to write to string" ) ;
364
341
( destination_register, instruction)
365
342
} ;
@@ -384,15 +361,15 @@ impl CodeGeneratingVisitor<'_> {
384
361
sym:: Mapping => match input. name . name {
385
362
sym:: get => {
386
363
let mut instruction = " get" . to_string ( ) ;
387
- let destination_register = get_destination_register ( ) ;
364
+ let destination_register = self . next_register ( ) ;
388
365
// Write the mapping name and the key.
389
366
writeln ! ( instruction, " {}[{}] into {destination_register};" , arguments[ 0 ] , arguments[ 1 ] )
390
367
. expect ( "failed to write to string" ) ;
391
368
( destination_register, instruction)
392
369
}
393
370
sym:: get_or_use => {
394
371
let mut instruction = " get.or_use" . to_string ( ) ;
395
- let destination_register = get_destination_register ( ) ;
372
+ let destination_register = self . next_register ( ) ;
396
373
// Write the mapping name, the key, and the default value.
397
374
writeln ! (
398
375
instruction,
@@ -417,7 +394,7 @@ impl CodeGeneratingVisitor<'_> {
417
394
}
418
395
sym:: contains => {
419
396
let mut instruction = " contains" . to_string ( ) ;
420
- let destination_register = get_destination_register ( ) ;
397
+ let destination_register = self . next_register ( ) ;
421
398
// Write the mapping name and the key.
422
399
writeln ! ( instruction, " {}[{}] into {destination_register};" , arguments[ 0 ] , arguments[ 1 ] )
423
400
. expect ( "failed to write to string" ) ;
@@ -429,15 +406,15 @@ impl CodeGeneratingVisitor<'_> {
429
406
match input. name {
430
407
Identifier { name : sym:: to_x_coordinate, .. } => {
431
408
let mut instruction = " cast" . to_string ( ) ;
432
- let destination_register = get_destination_register ( ) ;
409
+ let destination_register = self . next_register ( ) ;
433
410
// Write the argument and the destination register.
434
411
writeln ! ( instruction, " {} into {destination_register} as group.x;" , arguments[ 0 ] , )
435
412
. expect ( "failed to write to string" ) ;
436
413
( destination_register, instruction)
437
414
}
438
415
Identifier { name : sym:: to_y_coordinate, .. } => {
439
416
let mut instruction = " cast" . to_string ( ) ;
440
- let destination_register = get_destination_register ( ) ;
417
+ let destination_register = self . next_register ( ) ;
441
418
// Write the argument and the destination register.
442
419
writeln ! ( instruction, " {} into {destination_register} as group.y;" , arguments[ 0 ] , )
443
420
. expect ( "failed to write to string" ) ;
@@ -448,7 +425,7 @@ impl CodeGeneratingVisitor<'_> {
448
425
}
449
426
sym:: ChaCha => {
450
427
// Get the destination register.
451
- let destination_register = get_destination_register ( ) ;
428
+ let destination_register = self . next_register ( ) ;
452
429
// Construct the instruction template.
453
430
let mut instruction = format ! ( " rand.chacha into {destination_register} as " ) ;
454
431
// Write the return type.
@@ -475,7 +452,7 @@ impl CodeGeneratingVisitor<'_> {
475
452
}
476
453
sym:: signature => {
477
454
let mut instruction = " sign.verify" . to_string ( ) ;
478
- let destination_register = get_destination_register ( ) ;
455
+ let destination_register = self . next_register ( ) ;
479
456
// Write the arguments and the destination register.
480
457
writeln ! (
481
458
instruction,
@@ -552,24 +529,18 @@ impl CodeGeneratingVisitor<'_> {
552
529
0 | 1 => panic ! ( "Parsing guarantees that a tuple type has at least two elements" ) ,
553
530
len => {
554
531
for _ in 0 ..len {
555
- let destination_register = format ! ( "r{}" , self . next_register) ;
556
- destinations. push ( destination_register) ;
557
- self . next_register += 1 ;
532
+ destinations. push ( self . next_register ( ) ) ;
558
533
}
559
534
}
560
535
} ,
561
536
_ => {
562
- let destination_register = format ! ( "r{}" , self . next_register) ;
563
- destinations. push ( destination_register) ;
564
- self . next_register += 1 ;
537
+ destinations. push ( self . next_register ( ) ) ;
565
538
}
566
539
}
567
540
568
541
// Add a register for async functions to represent the future created.
569
542
if func_symbol. function . variant == Variant :: AsyncFunction {
570
- let destination_register = format ! ( "r{}" , self . next_register) ;
571
- destinations. push ( destination_register) ;
572
- self . next_register += 1 ;
543
+ destinations. push ( self . next_register ( ) ) ;
573
544
}
574
545
575
546
// Construct the output operands. These are the destination registers **without** the future.
@@ -615,8 +586,7 @@ impl CodeGeneratingVisitor<'_> {
615
586
}
616
587
617
588
pub fn clone_register ( & mut self , register : & str , typ : & Type ) -> ( String , String ) {
618
- let new_reg = format ! ( "r{}" , self . next_register) ;
619
- self . next_register += 1 ;
589
+ let new_reg = self . next_register ( ) ;
620
590
match typ {
621
591
Type :: Address
622
592
| Type :: Boolean
0 commit comments