Skip to content

Commit a3eb1bc

Browse files
committed
Change to codegen's visit_array.
An extra space was being produced after `cast`, and we can also save some allocations.
1 parent 24d4757 commit a3eb1bc

File tree

7 files changed

+74
-78
lines changed

7 files changed

+74
-78
lines changed

compiler/passes/src/code_generation/expression.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ use leo_ast::{
4343
Variant,
4444
};
4545
use leo_span::sym;
46-
use std::borrow::Borrow;
4746

48-
use std::fmt::Write as _;
47+
use std::{borrow::Borrow, fmt::Write as _};
4948

5049
/// Implement the necessary methods to visit nodes in the AST.
5150
impl CodeGeneratingVisitor<'_> {
@@ -169,25 +168,22 @@ impl CodeGeneratingVisitor<'_> {
169168
}
170169

171170
fn visit_array(&mut self, input: &ArrayExpression) -> (String, String) {
172-
let (expression_operands, mut instructions) =
173-
input.elements.iter().map(|expr| self.visit_expression(expr)).fold(
174-
(String::new(), String::new()),
175-
|(mut operands, mut instructions), (operand, operand_instructions)| {
176-
operands.push_str(&format!(" {operand}"));
177-
instructions.push_str(&operand_instructions);
178-
(operands, instructions)
179-
},
180-
);
171+
let mut expression_operands = String::new();
172+
let mut instructions = String::new();
173+
for (operand, operand_instructions) in input.elements.iter().map(|expr| self.visit_expression(expr)) {
174+
let space = if expression_operands.is_empty() { "" } else { " " };
175+
write!(&mut expression_operands, "{space}{operand}").unwrap();
176+
instructions.push_str(&operand_instructions);
177+
}
181178

182179
// Construct the destination register.
183180
let destination_register = format!("r{}", self.next_register);
184181
// Increment the register counter.
185182
self.next_register += 1;
186183

187184
// Get the array type.
188-
let array_type = match self.state.type_table.get(&input.id) {
189-
Some(Type::Array(array_type)) => Type::Array(array_type),
190-
_ => panic!("All types should be known at this phase of compilation"),
185+
let Some(array_type @ Type::Array(..)) = self.state.type_table.get(&input.id) else {
186+
panic!("All types should be known at this phase of compilation");
191187
};
192188
let array_type: String = Self::visit_type(&array_type);
193189

tests/expectations/compiler/array/array_in_composite_data_types.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ struct bar:
1010

1111
function foo:
1212
input r0 as [[boolean; 8u32]; 8u32].private;
13-
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r1 as [u8; 8u32];
13+
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r1 as [u8; 8u32];
1414
cast r1 into r2 as bar;
15-
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r3 as [u8; 8u32];
15+
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r3 as [u8; 8u32];
1616
cast self.caller r3 into r4 as floo.record;
1717
output r0[0u32][0u32] as boolean.private;
1818
output r2 as bar.private;
@@ -30,9 +30,9 @@ struct bar:
3030

3131
function foo:
3232
input r0 as [[boolean; 8u32]; 8u32].private;
33-
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r1 as [u8; 8u32];
33+
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r1 as [u8; 8u32];
3434
cast r1 into r2 as bar;
35-
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r3 as [u8; 8u32];
35+
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r3 as [u8; 8u32];
3636
cast self.caller r3 into r4 as floo.record;
3737
output r0[0u32][0u32] as boolean.private;
3838
output r2 as bar.private;

tests/expectations/compiler/array/array_initialization.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ program test.aleo;
33

44
function bar:
55
input r0 as boolean.private;
6-
cast r0 r0 r0 r0 r0 r0 r0 r0 into r1 as [boolean; 8u32];
6+
cast r0 r0 r0 r0 r0 r0 r0 r0 into r1 as [boolean; 8u32];
77
output r1 as [boolean; 8u32].private;
88

99
DCE_DISABLED:
1010
program test.aleo;
1111

1212
function bar:
1313
input r0 as boolean.private;
14-
cast r0 r0 r0 r0 r0 r0 r0 r0 into r1 as [boolean; 8u32];
14+
cast r0 r0 r0 r0 r0 r0 r0 r0 into r1 as [boolean; 8u32];
1515
output r1 as [boolean; 8u32].private;

tests/expectations/compiler/const_prop/prop_literals.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function f2:
88
output 0field as field.private;
99

1010
function f3:
11-
cast aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta into r0 as [address; 2u32];
11+
cast aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta into r0 as [address; 2u32];
1212
output r0 as [address; 2u32].private;
1313

1414
DCE_DISABLED:
@@ -21,5 +21,5 @@ function f2:
2121
output 0field as field.private;
2222

2323
function f3:
24-
cast aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta into r0 as [address; 2u32];
24+
cast aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta into r0 as [address; 2u32];
2525
output r0 as [address; 2u32].private;

tests/expectations/compiler/function/flatten_arrays.out

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ struct Data:
77
closure foo:
88
input r0 as u8;
99
input r1 as u8;
10-
cast r0 r1 into r2 as [u8; 2u32];
10+
cast r0 r1 into r2 as [u8; 2u32];
1111
cast r2 into r3 as Data;
12-
cast r1 r0 into r4 as [u8; 2u32];
12+
cast r1 r0 into r4 as [u8; 2u32];
1313
cast r4 into r5 as Data;
1414
is.eq r0 r1 into r6;
15-
cast r3 r5 into r7 as [Data; 2u32];
15+
cast r3 r5 into r7 as [Data; 2u32];
1616
mul 2u8 r3.data[0u32] into r8;
1717
mul 4u8 r5.data[1u32] into r9;
18-
cast r8 r9 into r10 as [u8; 2u32];
18+
cast r8 r9 into r10 as [u8; 2u32];
1919
cast r10 into r11 as Data;
20-
cast r5 r11 into r12 as [Data; 2u32];
20+
cast r5 r11 into r12 as [Data; 2u32];
2121
ternary r6 r7[0u32].data[0u32] r12[0u32].data[0u32] into r13;
2222
ternary r6 r7[0u32].data[1u32] r12[0u32].data[1u32] into r14;
23-
cast r13 r14 into r15 as [u8; 2u32];
23+
cast r13 r14 into r15 as [u8; 2u32];
2424
cast r15 into r16 as Data;
2525
ternary r6 r7[1u32].data[0u32] r12[1u32].data[0u32] into r17;
2626
ternary r6 r7[1u32].data[1u32] r12[1u32].data[1u32] into r18;
27-
cast r17 r18 into r19 as [u8; 2u32];
27+
cast r17 r18 into r19 as [u8; 2u32];
2828
cast r19 into r20 as Data;
29-
cast r16 r20 into r21 as [Data; 2u32];
29+
cast r16 r20 into r21 as [Data; 2u32];
3030
output r21 as [Data; 2u32];
3131

3232
function bar:
@@ -40,22 +40,22 @@ function bar:
4040
call foo r4[0u32].data[1u32] r4[1u32].data[0u32] into r7;
4141
ternary r1 r6[0u32].data[0u32] r7[0u32].data[0u32] into r8;
4242
ternary r1 r6[0u32].data[1u32] r7[0u32].data[1u32] into r9;
43-
cast r8 r9 into r10 as [u8; 2u32];
43+
cast r8 r9 into r10 as [u8; 2u32];
4444
cast r10 into r11 as Data;
4545
ternary r1 r6[1u32].data[0u32] r7[1u32].data[0u32] into r12;
4646
ternary r1 r6[1u32].data[1u32] r7[1u32].data[1u32] into r13;
47-
cast r12 r13 into r14 as [u8; 2u32];
47+
cast r12 r13 into r14 as [u8; 2u32];
4848
cast r14 into r15 as Data;
49-
cast r11 r15 into r16 as [Data; 2u32];
49+
cast r11 r15 into r16 as [Data; 2u32];
5050
ternary r0 r5[0u32].data[0u32] r16[0u32].data[0u32] into r17;
5151
ternary r0 r5[0u32].data[1u32] r16[0u32].data[1u32] into r18;
52-
cast r17 r18 into r19 as [u8; 2u32];
52+
cast r17 r18 into r19 as [u8; 2u32];
5353
cast r19 into r20 as Data;
5454
ternary r0 r5[1u32].data[0u32] r16[1u32].data[0u32] into r21;
5555
ternary r0 r5[1u32].data[1u32] r16[1u32].data[1u32] into r22;
56-
cast r21 r22 into r23 as [u8; 2u32];
56+
cast r21 r22 into r23 as [u8; 2u32];
5757
cast r23 into r24 as Data;
58-
cast r20 r24 into r25 as [Data; 2u32];
58+
cast r20 r24 into r25 as [Data; 2u32];
5959
output r25 as [Data; 2u32].private;
6060

6161
DCE_DISABLED:
@@ -67,26 +67,26 @@ struct Data:
6767
closure foo:
6868
input r0 as u8;
6969
input r1 as u8;
70-
cast r0 r1 into r2 as [u8; 2u32];
70+
cast r0 r1 into r2 as [u8; 2u32];
7171
cast r2 into r3 as Data;
72-
cast r1 r0 into r4 as [u8; 2u32];
72+
cast r1 r0 into r4 as [u8; 2u32];
7373
cast r4 into r5 as Data;
7474
is.eq r0 r1 into r6;
75-
cast r3 r5 into r7 as [Data; 2u32];
75+
cast r3 r5 into r7 as [Data; 2u32];
7676
mul 2u8 r3.data[0u32] into r8;
7777
mul 4u8 r5.data[1u32] into r9;
78-
cast r8 r9 into r10 as [u8; 2u32];
78+
cast r8 r9 into r10 as [u8; 2u32];
7979
cast r10 into r11 as Data;
80-
cast r5 r11 into r12 as [Data; 2u32];
80+
cast r5 r11 into r12 as [Data; 2u32];
8181
ternary r6 r7[0u32].data[0u32] r12[0u32].data[0u32] into r13;
8282
ternary r6 r7[0u32].data[1u32] r12[0u32].data[1u32] into r14;
83-
cast r13 r14 into r15 as [u8; 2u32];
83+
cast r13 r14 into r15 as [u8; 2u32];
8484
cast r15 into r16 as Data;
8585
ternary r6 r7[1u32].data[0u32] r12[1u32].data[0u32] into r17;
8686
ternary r6 r7[1u32].data[1u32] r12[1u32].data[1u32] into r18;
87-
cast r17 r18 into r19 as [u8; 2u32];
87+
cast r17 r18 into r19 as [u8; 2u32];
8888
cast r19 into r20 as Data;
89-
cast r16 r20 into r21 as [Data; 2u32];
89+
cast r16 r20 into r21 as [Data; 2u32];
9090
output r21 as [Data; 2u32];
9191

9292
function bar:
@@ -102,20 +102,20 @@ function bar:
102102
call foo r4[0u32].data[1u32] r4[1u32].data[0u32] into r9;
103103
ternary r1 r7[0u32].data[0u32] r9[0u32].data[0u32] into r10;
104104
ternary r1 r7[0u32].data[1u32] r9[0u32].data[1u32] into r11;
105-
cast r10 r11 into r12 as [u8; 2u32];
105+
cast r10 r11 into r12 as [u8; 2u32];
106106
cast r12 into r13 as Data;
107107
ternary r1 r7[1u32].data[0u32] r9[1u32].data[0u32] into r14;
108108
ternary r1 r7[1u32].data[1u32] r9[1u32].data[1u32] into r15;
109-
cast r14 r15 into r16 as [u8; 2u32];
109+
cast r14 r15 into r16 as [u8; 2u32];
110110
cast r16 into r17 as Data;
111-
cast r13 r17 into r18 as [Data; 2u32];
111+
cast r13 r17 into r18 as [Data; 2u32];
112112
ternary r0 r5[0u32].data[0u32] r18[0u32].data[0u32] into r19;
113113
ternary r0 r5[0u32].data[1u32] r18[0u32].data[1u32] into r20;
114-
cast r19 r20 into r21 as [u8; 2u32];
114+
cast r19 r20 into r21 as [u8; 2u32];
115115
cast r21 into r22 as Data;
116116
ternary r0 r5[1u32].data[0u32] r18[1u32].data[0u32] into r23;
117117
ternary r0 r5[1u32].data[1u32] r18[1u32].data[1u32] into r24;
118-
cast r23 r24 into r25 as [u8; 2u32];
118+
cast r23 r24 into r25 as [u8; 2u32];
119119
cast r25 into r26 as Data;
120-
cast r22 r26 into r27 as [Data; 2u32];
120+
cast r22 r26 into r27 as [Data; 2u32];
121121
output r27 as [Data; 2u32].private;

tests/expectations/compiler/structs/external_struct.out

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ struct Foo:
2424
function create:
2525
cast 1u32 2u32 into r0 as Two;
2626
cast 3u32 4u32 into r1 as Two;
27-
cast r0 r1 into r2 as [Two; 2u32];
27+
cast r0 r1 into r2 as [Two; 2u32];
2828
cast r2 into r3 as One;
2929
cast r3 into r4 as Baz;
3030
cast 5u32 6u32 into r5 as Two;
3131
cast 7u32 8u32 into r6 as Two;
32-
cast r5 r6 into r7 as [Two; 2u32];
32+
cast r5 r6 into r7 as [Two; 2u32];
3333
cast r7 into r8 as One;
3434
cast r8 into r9 as Baz;
35-
cast r4 r9 into r10 as [Baz; 2u32];
35+
cast r4 r9 into r10 as [Baz; 2u32];
3636
cast r10 into r11 as Bar;
37-
cast r11 into r12 as [Bar; 1u32];
37+
cast r11 into r12 as [Bar; 1u32];
3838
cast r12 into r13 as Foo;
3939
cast self.caller 10u32 into r14 as Boo.record;
4040
output r13 as Foo.private;
@@ -147,17 +147,17 @@ struct Foo:
147147
function create:
148148
cast 1u32 2u32 into r0 as Two;
149149
cast 3u32 4u32 into r1 as Two;
150-
cast r0 r1 into r2 as [Two; 2u32];
150+
cast r0 r1 into r2 as [Two; 2u32];
151151
cast r2 into r3 as One;
152152
cast r3 into r4 as Baz;
153153
cast 5u32 6u32 into r5 as Two;
154154
cast 7u32 8u32 into r6 as Two;
155-
cast r5 r6 into r7 as [Two; 2u32];
155+
cast r5 r6 into r7 as [Two; 2u32];
156156
cast r7 into r8 as One;
157157
cast r8 into r9 as Baz;
158-
cast r4 r9 into r10 as [Baz; 2u32];
158+
cast r4 r9 into r10 as [Baz; 2u32];
159159
cast r10 into r11 as Bar;
160-
cast r11 into r12 as [Bar; 1u32];
160+
cast r11 into r12 as [Bar; 1u32];
161161
cast r12 into r13 as Foo;
162162
cast self.caller 10u32 into r14 as Boo.record;
163163
output r13 as Foo.private;
@@ -194,17 +194,17 @@ struct Foo:
194194
function create_wrapper:
195195
cast 1u32 2u32 into r0 as Two;
196196
cast 3u32 4u32 into r1 as Two;
197-
cast r0 r1 into r2 as [Two; 2u32];
197+
cast r0 r1 into r2 as [Two; 2u32];
198198
cast r2 into r3 as One;
199199
cast r3 into r4 as Baz;
200200
cast 5u32 6u32 into r5 as Two;
201201
cast 7u32 8u32 into r6 as Two;
202-
cast r5 r6 into r7 as [Two; 2u32];
202+
cast r5 r6 into r7 as [Two; 2u32];
203203
cast r7 into r8 as One;
204204
cast r8 into r9 as Baz;
205-
cast r4 r9 into r10 as [Baz; 2u32];
205+
cast r4 r9 into r10 as [Baz; 2u32];
206206
cast r10 into r11 as Bar;
207-
cast r11 into r12 as [Bar; 1u32];
207+
cast r11 into r12 as [Bar; 1u32];
208208
cast r12 into r13 as Foo;
209209
call child.aleo/create into r14 r15;
210210
call child.aleo/create into r16 r17;
@@ -214,17 +214,17 @@ function create_wrapper:
214214
function create_another_wrapper:
215215
cast 1u32 2u32 into r0 as Two;
216216
cast 3u32 4u32 into r1 as Two;
217-
cast r0 r1 into r2 as [Two; 2u32];
217+
cast r0 r1 into r2 as [Two; 2u32];
218218
cast r2 into r3 as One;
219219
cast r3 into r4 as Baz;
220220
cast 5u32 6u32 into r5 as Two;
221221
cast 7u32 8u32 into r6 as Two;
222-
cast r5 r6 into r7 as [Two; 2u32];
222+
cast r5 r6 into r7 as [Two; 2u32];
223223
cast r7 into r8 as One;
224224
cast r8 into r9 as Baz;
225-
cast r4 r9 into r10 as [Baz; 2u32];
225+
cast r4 r9 into r10 as [Baz; 2u32];
226226
cast r10 into r11 as Bar;
227-
cast r11 into r12 as [Bar; 1u32];
227+
cast r11 into r12 as [Bar; 1u32];
228228
cast r12 into r13 as Foo;
229229
call child.aleo/create into r14 r15;
230230
cast 1u32 2u32 into r16 as Woo;

tests/expectations/compiler/structs/redefine_external_struct.out

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ struct Foo:
2020
function create:
2121
cast 1u32 2u32 into r0 as Two;
2222
cast 3u32 4u32 into r1 as Two;
23-
cast r0 r1 into r2 as [Two; 2u32];
23+
cast r0 r1 into r2 as [Two; 2u32];
2424
cast r2 into r3 as One;
2525
cast r3 into r4 as Baz;
2626
cast 5u32 6u32 into r5 as Two;
2727
cast 7u32 8u32 into r6 as Two;
28-
cast r5 r6 into r7 as [Two; 2u32];
28+
cast r5 r6 into r7 as [Two; 2u32];
2929
cast r7 into r8 as One;
3030
cast r8 into r9 as Baz;
31-
cast r4 r9 into r10 as [Baz; 2u32];
31+
cast r4 r9 into r10 as [Baz; 2u32];
3232
cast r10 into r11 as Bar;
33-
cast r11 into r12 as [Bar; 1u32];
33+
cast r11 into r12 as [Bar; 1u32];
3434
cast r12 into r13 as Foo;
3535
output r13 as Foo.private;
3636
// --- Next Program --- //
@@ -79,17 +79,17 @@ struct Foo:
7979
function create:
8080
cast 1u32 2u32 into r0 as Two;
8181
cast 3u32 4u32 into r1 as Two;
82-
cast r0 r1 into r2 as [Two; 2u32];
82+
cast r0 r1 into r2 as [Two; 2u32];
8383
cast r2 into r3 as One;
8484
cast r3 into r4 as Baz;
8585
cast 5u32 6u32 into r5 as Two;
8686
cast 7u32 8u32 into r6 as Two;
87-
cast r5 r6 into r7 as [Two; 2u32];
87+
cast r5 r6 into r7 as [Two; 2u32];
8888
cast r7 into r8 as One;
8989
cast r8 into r9 as Baz;
90-
cast r4 r9 into r10 as [Baz; 2u32];
90+
cast r4 r9 into r10 as [Baz; 2u32];
9191
cast r10 into r11 as Bar;
92-
cast r11 into r12 as [Bar; 1u32];
92+
cast r11 into r12 as [Bar; 1u32];
9393
cast r12 into r13 as Foo;
9494
output r13 as Foo.private;
9595
// --- Next Program --- //
@@ -115,17 +115,17 @@ struct Foo:
115115
function create_wrapper:
116116
cast 1u32 2u32 into r0 as Two;
117117
cast 3u32 4u32 into r1 as Two;
118-
cast r0 r1 into r2 as [Two; 2u32];
118+
cast r0 r1 into r2 as [Two; 2u32];
119119
cast r2 into r3 as One;
120120
cast r3 into r4 as Baz;
121121
cast 5u32 6u32 into r5 as Two;
122122
cast 7u32 8u32 into r6 as Two;
123-
cast r5 r6 into r7 as [Two; 2u32];
123+
cast r5 r6 into r7 as [Two; 2u32];
124124
cast r7 into r8 as One;
125125
cast r8 into r9 as Baz;
126-
cast r4 r9 into r10 as [Baz; 2u32];
126+
cast r4 r9 into r10 as [Baz; 2u32];
127127
cast r10 into r11 as Bar;
128-
cast r11 into r12 as [Bar; 1u32];
128+
cast r11 into r12 as [Bar; 1u32];
129129
cast r12 into r13 as Foo;
130130
call child.aleo/create into r14;
131131
output r14 as Foo.private;

0 commit comments

Comments
 (0)