@@ -29,6 +29,7 @@ use std::sync::Arc;
29
29
use super :: brillig_black_box:: convert_black_box_call;
30
30
use super :: brillig_block_variables:: BlockVariables ;
31
31
use super :: brillig_fn:: FunctionContext ;
32
+ use super :: constant_allocation:: InstructionLocation ;
32
33
33
34
/// Generate the compilation artifacts for compiling a function into brillig bytecode.
34
35
pub ( crate ) struct BrilligBlock < ' block > {
@@ -117,6 +118,13 @@ impl<'block> BrilligBlock<'block> {
117
118
terminator_instruction : & TerminatorInstruction ,
118
119
dfg : & DataFlowGraph ,
119
120
) {
121
+ self . initialize_constants (
122
+ & self
123
+ . function_context
124
+ . constant_allocation
125
+ . allocated_at_location ( self . block_id , InstructionLocation :: Terminator ) ,
126
+ dfg,
127
+ ) ;
120
128
match terminator_instruction {
121
129
TerminatorInstruction :: JmpIf {
122
130
condition,
@@ -244,6 +252,13 @@ impl<'block> BrilligBlock<'block> {
244
252
let instruction = & dfg[ instruction_id] ;
245
253
self . brillig_context . set_call_stack ( dfg. get_call_stack ( instruction_id) ) ;
246
254
255
+ self . initialize_constants (
256
+ & self . function_context . constant_allocation . allocated_at_location (
257
+ self . block_id ,
258
+ InstructionLocation :: Instruction ( instruction_id) ,
259
+ ) ,
260
+ dfg,
261
+ ) ;
247
262
match instruction {
248
263
Instruction :: Binary ( binary) => {
249
264
let result_var = self . variables . define_single_addr_variable (
@@ -768,9 +783,6 @@ impl<'block> BrilligBlock<'block> {
768
783
. brillig_context
769
784
. codegen_pre_call_save_registers_prep_args ( & argument_registers, & variables_to_save) ;
770
785
771
- // We don't save and restore constants, so we dump them before a external call since the callee might use the registers where they are allocated.
772
- self . variables . dump_constants ( ) ;
773
-
774
786
// Call instruction, which will interpret above registers 0..num args
775
787
self . brillig_context . add_external_call_instruction ( func_id) ;
776
788
@@ -1512,6 +1524,12 @@ impl<'block> BrilligBlock<'block> {
1512
1524
}
1513
1525
}
1514
1526
1527
+ fn initialize_constants ( & mut self , constants : & [ ValueId ] , dfg : & DataFlowGraph ) {
1528
+ for & constant_id in constants {
1529
+ self . convert_ssa_value ( constant_id, dfg) ;
1530
+ }
1531
+ }
1532
+
1515
1533
/// Converts an SSA `ValueId` into a `RegisterOrMemory`. Initializes if necessary.
1516
1534
fn convert_ssa_value ( & mut self , value_id : ValueId , dfg : & DataFlowGraph ) -> BrilligVariable {
1517
1535
let value_id = dfg. resolve ( value_id) ;
@@ -1527,23 +1545,31 @@ impl<'block> BrilligBlock<'block> {
1527
1545
Value :: NumericConstant { constant, .. } => {
1528
1546
// Constants might have been converted previously or not, so we get or create and
1529
1547
// (re)initialize the value inside.
1530
- if let Some ( variable ) = self . variables . get_constant ( value_id, dfg ) {
1531
- variable
1548
+ if self . variables . is_allocated ( & value_id) {
1549
+ self . variables . get_allocation ( self . function_context , value_id , dfg )
1532
1550
} else {
1533
- let new_variable =
1534
- self . variables . allocate_constant ( self . brillig_context , value_id, dfg) ;
1551
+ let new_variable = self . variables . define_variable (
1552
+ self . function_context ,
1553
+ self . brillig_context ,
1554
+ value_id,
1555
+ dfg,
1556
+ ) ;
1535
1557
1536
1558
self . brillig_context
1537
1559
. const_instruction ( new_variable. extract_single_addr ( ) , * constant) ;
1538
1560
new_variable
1539
1561
}
1540
1562
}
1541
1563
Value :: Array { array, typ } => {
1542
- if let Some ( variable ) = self . variables . get_constant ( value_id, dfg ) {
1543
- variable
1564
+ if self . variables . is_allocated ( & value_id) {
1565
+ self . variables . get_allocation ( self . function_context , value_id , dfg )
1544
1566
} else {
1545
- let new_variable =
1546
- self . variables . allocate_constant ( self . brillig_context , value_id, dfg) ;
1567
+ let new_variable = self . variables . define_variable (
1568
+ self . function_context ,
1569
+ self . brillig_context ,
1570
+ value_id,
1571
+ dfg,
1572
+ ) ;
1547
1573
1548
1574
// Initialize the variable
1549
1575
let pointer = match new_variable {
@@ -1583,8 +1609,12 @@ impl<'block> BrilligBlock<'block> {
1583
1609
// around values representing function pointers, even though
1584
1610
// there is no interaction with the function possible given that
1585
1611
// value.
1586
- let new_variable =
1587
- self . variables . allocate_constant ( self . brillig_context , value_id, dfg) ;
1612
+ let new_variable = self . variables . define_variable (
1613
+ self . function_context ,
1614
+ self . brillig_context ,
1615
+ value_id,
1616
+ dfg,
1617
+ ) ;
1588
1618
1589
1619
self . brillig_context . const_instruction (
1590
1620
new_variable. extract_single_addr ( ) ,
@@ -1732,18 +1762,10 @@ impl<'block> BrilligBlock<'block> {
1732
1762
self . brillig_context . mov_instruction ( write_pointer_register, pointer) ;
1733
1763
1734
1764
for ( element_idx, element_id) in data. iter ( ) . enumerate ( ) {
1735
- if let Some ( ( constant, typ) ) = dfg. get_numeric_constant_with_type ( * element_id) {
1736
- self . brillig_context . indirect_const_instruction (
1737
- write_pointer_register,
1738
- typ. bit_size ( ) ,
1739
- constant,
1740
- ) ;
1741
- } else {
1742
- let element_variable = self . convert_ssa_value ( * element_id, dfg) ;
1743
- // Store the item in memory
1744
- self . brillig_context
1745
- . codegen_store_variable_in_pointer ( write_pointer_register, element_variable) ;
1746
- }
1765
+ let element_variable = self . convert_ssa_value ( * element_id, dfg) ;
1766
+ // Store the item in memory
1767
+ self . brillig_context
1768
+ . codegen_store_variable_in_pointer ( write_pointer_register, element_variable) ;
1747
1769
1748
1770
if element_idx != data. len ( ) - 1 {
1749
1771
// Increment the write_pointer_register
0 commit comments