1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! This module contains the logic for struct layout in memory and instantiation.
use crate::{
    asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSequencer},
    asm_lang::{
        ConstantRegister, Op, VirtualImmediate12, VirtualImmediate24, VirtualOp, VirtualRegister,
    },
    error::*,
    semantic_analysis::ast_node::{TypedExpression, TypedStructExpressionField},
    type_engine::{look_up_type_id, resolve_type, TypeId},
    CompileResult, Ident,
};
use sway_types::span::Span;

/// Contains an ordered array of fields and their sizes in words. Used in the code generation
/// of struct/tuple field reassignments, accesses, and struct/tuple initializations.
#[derive(Debug)]
pub(crate) struct ContiguousMemoryLayoutDescriptor<N> {
    fields: Vec<FieldMemoryLayoutDescriptor<N>>,
}

/// Describes the size, name, and type of an individual struct/tuple field in a memory layout.
#[derive(Debug)]
pub(crate) struct FieldMemoryLayoutDescriptor<N> {
    name_of_field: N,
    size: u64,
}

impl ContiguousMemoryLayoutDescriptor<Ident> {
    /// Calculates the offset in words from the start of a struct to a specific field.
    pub(crate) fn offset_to_field_name(&self, name: &str, span: Span) -> CompileResult<u64> {
        let field_ix = if let Some(ix) =
            self.fields
                .iter()
                .position(|FieldMemoryLayoutDescriptor { name_of_field, .. }| {
                    name_of_field.as_str() == name
                }) {
            ix
        } else {
            return err(vec![],
                vec![
                CompileError::Internal(
                    "Attempted to calculate struct memory offset on field that did not exist in struct.",
                    span
                    )
                ]);
        };

        ok(
            self.fields
                .iter()
                .take(field_ix)
                .fold(0, |acc, FieldMemoryLayoutDescriptor { size, .. }| {
                    acc + *size
                }),
            vec![],
            vec![],
        )
    }
}

impl<N> ContiguousMemoryLayoutDescriptor<N> {
    pub(crate) fn total_size(&self) -> u64 {
        self.fields
            .iter()
            .map(|FieldMemoryLayoutDescriptor { size, .. }| size)
            .sum()
    }
}

#[test]
fn test_struct_memory_layout() {
    let first_field_name = Ident::new_no_span("foo");
    let second_field_name = Ident::new_no_span("bar");

    let numbers = ContiguousMemoryLayoutDescriptor {
        fields: vec![
            FieldMemoryLayoutDescriptor {
                name_of_field: first_field_name.clone(),
                size: 1,
            },
            FieldMemoryLayoutDescriptor {
                name_of_field: second_field_name.clone(),
                size: 1,
            },
        ],
    };

    let mut warnings: Vec<CompileWarning> = Vec::new();
    let mut errors: Vec<CompileError> = Vec::new();
    assert_eq!(numbers.total_size(), 2u64);
    assert_eq!(
        numbers
            .offset_to_field_name(first_field_name.as_str(), first_field_name.span().clone())
            .unwrap(&mut warnings, &mut errors),
        0u64
    );
    assert_eq!(
        numbers
            .offset_to_field_name(second_field_name.as_str(), first_field_name.span().clone())
            .unwrap(&mut warnings, &mut errors),
        1u64
    );
}

pub(crate) fn get_contiguous_memory_layout<N: Clone>(
    fields_with_names: &[(TypeId, Span, N)],
) -> CompileResult<ContiguousMemoryLayoutDescriptor<N>> {
    let mut fields_with_sizes = Vec::with_capacity(fields_with_names.len());
    let warnings = vec![];
    let mut errors = vec![];
    for (field, span, name) in fields_with_names {
        let ty = look_up_type_id(*field);
        let stack_size = match ty.size_in_words(span) {
            Ok(o) => o,
            Err(e) => {
                errors.push(e);
                return err(warnings, errors);
            }
        };

        fields_with_sizes.push(FieldMemoryLayoutDescriptor {
            name_of_field: name.clone(),
            size: stack_size,
        });
    }
    ok(
        ContiguousMemoryLayoutDescriptor {
            fields: fields_with_sizes,
        },
        warnings,
        errors,
    )
}

pub(crate) fn convert_fields_to_asm<N: Clone + std::fmt::Display>(
    fields: &[(TypedExpression, Span, N)],
    struct_beginning_pointer: &VirtualRegister,
    namespace: &mut AsmNamespace,
    register_sequencer: &mut RegisterSequencer,
    mut asm_buf: Vec<Op>,
) -> CompileResult<Vec<Op>> {
    let mut warnings = vec![];
    let mut errors = vec![];
    // step 0: calculate the total size needed for the whole struct
    // step 1: store the value currently in $sp, it will become the pointer to the first field
    // step 2: use CFE to extend the call frame by the size calculated in step 0
    // step 3: for every field in the struct:
    //             evaluate its initializer
    //             SW (store word) at the current pointer
    //             increment pointer by the size of this field
    //
    // for now i dont think this step is needed, we can resolve the types at call time
    // but im leaving this here for historical purposes in case i need to come back and implement
    // step 4
    //
    // step 4: put the pointer to the beginning of the struct in the namespace

    let fields_for_layout = fields
        .iter()
        .map(|(value, span, name)| (value.return_type, span.clone(), name.clone()))
        .collect::<Vec<_>>();

    // step 0
    let descriptor = check!(
        get_contiguous_memory_layout(&fields_for_layout),
        return err(warnings, errors),
        warnings,
        errors
    );

    let total_size = descriptor.total_size();

    if total_size == 0 {
        asm_buf.push(Op::new_comment("fields have total size of zero."));
        return ok(asm_buf, warnings, errors);
    }

    if total_size == 0 {
        asm_buf.push(Op::new_comment("fields have total size of zero."));
        return ok(asm_buf, warnings, errors);
    }

    // step 1
    asm_buf.push(Op::unowned_register_move(
        struct_beginning_pointer.clone(),
        VirtualRegister::Constant(ConstantRegister::StackPointer),
    ));

    // step 2
    // decide how many call frame extensions are needed based on the size of the struct
    // and how many bits can be put in a single cfei op
    // limit struct size to 12 bits for now, for simplicity
    let twelve_bits = super::compiler_constants::TWELVE_BITS;
    let number_of_allocations_necessary = (total_size + (twelve_bits - 1)) / twelve_bits;

    // construct the allocation ops
    for allocation_index in 0..number_of_allocations_necessary {
        let left_to_allocate = total_size - (allocation_index * twelve_bits);
        let this_allocation = if left_to_allocate > twelve_bits {
            twelve_bits
        } else {
            left_to_allocate
        };
        // we call `new_unchecked` here because we have validated the size is okay above
        asm_buf.push(Op::unowned_stack_allocate_memory(
            VirtualImmediate24::new_unchecked(
                this_allocation * 8, // this_allocation is words but this op takes bytes
                "struct size was checked manually to be within 12 bits",
            ),
        ));
    }

    // step 3
    // `offset` is in words
    let mut offset = 0;
    for (value, span, name) in fields {
        // evaluate the expression
        let return_register = register_sequencer.next();
        let value_stack_size: u64 = match resolve_type(value.return_type, span) {
            Ok(o) => match o.size_in_words(span) {
                Ok(o) => o,
                Err(e) => {
                    errors.push(e);
                    return err(warnings, errors);
                }
            },
            Err(e) => {
                errors.push(e.into());
                return err(warnings, errors);
            }
        };
        let mut field_instantiation = check!(
            convert_expression_to_asm(value, namespace, &return_register, register_sequencer),
            vec![],
            warnings,
            errors
        );
        asm_buf.append(&mut field_instantiation);
        // if the value is less than one word in size, we write it via the SW opcode.
        // Otherwise, use MCPI to copy the contiguous memory
        if value_stack_size > 1 {
            // copy the struct beginning pointer and add the offset to it
            let address_to_write_to = register_sequencer.next();
            // load the address via ADDI
            asm_buf.push(Op {
                opcode: either::Either::Left(VirtualOp::ADDI(
                    address_to_write_to.clone(),
                    struct_beginning_pointer.clone(),
                    VirtualImmediate12::new_unchecked(offset * 8, "struct size is too large"),
                )),
                owning_span: Some(value.span.clone()),
                comment: format!(
                    "prep struct field reg (size {} for field {})",
                    value_stack_size, name,
                ),
            });

            // copy the data
            asm_buf.push(Op {
                opcode: either::Either::Left(VirtualOp::MCPI(
                    address_to_write_to,
                    return_register,
                    VirtualImmediate12::new_unchecked(
                        value_stack_size * 8,
                        "struct cannot be this big",
                    ),
                )),
                owning_span: Some(value.span.clone()),
                comment: format!("cp type size {} for field {}", value_stack_size, name),
            });
        } else {
            asm_buf.push(Op::write_register_to_memory(
                struct_beginning_pointer.clone(),
                return_register,
                VirtualImmediate12::new_unchecked(offset, "the whole struct is less than 12 bits so every individual field should be as well."),
                span.clone(),
            ));
        }
        // TODO: if the struct needs multiple allocations, this offset could exceed the size of the
        // immediate value allowed in SW. In that case, we need to shift `struct_beginning_pointer`
        // to the max offset and start the offset back from 0. This is only for structs in excess
        // of 130MB
        // from john about the above: As a TODO, maybe let's just restrict the maximum size of
        // something (I don't know exactly what) at the consensus level so this case is guaranteed
        // to never be hit.
        offset += value_stack_size;
    }

    ok(asm_buf, warnings, errors)
}

pub(crate) fn convert_struct_expression_to_asm(
    struct_name: &Ident,
    fields: &[TypedStructExpressionField],
    struct_beginning_pointer: &VirtualRegister,
    namespace: &mut AsmNamespace,
    register_sequencer: &mut RegisterSequencer,
) -> CompileResult<Vec<Op>> {
    let fields = fields
        .iter()
        .map(|TypedStructExpressionField { name, value }| {
            (
                value.clone(),
                name.span().clone(),
                name.as_str().to_string(),
            )
        })
        .collect::<Vec<_>>();

    let asm_buf = vec![Op::new_comment(format!(
        "{} struct initialization",
        struct_name.as_str()
    ))];

    convert_fields_to_asm(
        &fields,
        struct_beginning_pointer,
        namespace,
        register_sequencer,
        asm_buf,
    )
}

pub(crate) fn convert_tuple_expression_to_asm(
    fields: &[TypedExpression],
    tuple_beginning_pointer: &VirtualRegister,
    namespace: &mut AsmNamespace,
    register_sequencer: &mut RegisterSequencer,
) -> CompileResult<Vec<Op>> {
    let fields = fields
        .iter()
        .enumerate()
        .map(|(i, field)| (field.clone(), field.span.clone(), i))
        .collect::<Vec<_>>();
    let asm_buf = vec![Op::new_comment(format!(
        "{}-tuple initialization",
        fields.len(),
    ))];

    convert_fields_to_asm(
        &fields,
        tuple_beginning_pointer,
        namespace,
        register_sequencer,
        asm_buf,
    )
}