swamp_code_gen/
layout.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use crate::alloc::StackFrameAllocator;
7use crate::FrameAndVariableInfo;
8use seq_map::SeqMap;
9use source_map_node::Node;
10use std::fmt::Write;
11use swamp_semantic::{VariableScopes, VariableType};
12use swamp_types::TypeRef;
13use swamp_vm_layout::LayoutCache;
14use swamp_vm_types::types::{
15    FrameAddressInfo, FrameMemoryInfo, TypedRegister, VariableInfo, VariableInfoKind,
16    VariableRegister, VmType,
17};
18use swamp_vm_types::{FrameMemoryAddress, FrameMemoryRegion, MemorySize};
19
20/// # Errors
21///
22#[allow(clippy::too_many_lines)]
23#[must_use]
24pub fn layout_variables(
25    layout_cache: &mut LayoutCache,
26    _node: &Node,
27    scopes: &VariableScopes,
28    exp_return_type: &TypeRef,
29) -> FrameAndVariableInfo {
30    let mut local_frame_allocator = StackFrameAllocator::new(FrameMemoryRegion::new(
31        FrameMemoryAddress(0),
32        MemorySize(3 * 1024 * 1024 * 1024),
33    ));
34
35    let mut enter_comment = "variables:\n".to_string();
36    let mut frame_memory_infos = Vec::new();
37
38    let mut all_variable_unique_to_register = SeqMap::new();
39
40    let mut variable_registers_for_debug_info = Vec::new();
41    for var_ref in &scopes.all_variables {
42        let basic_type = layout_cache.layout(&var_ref.resolved_type);
43
44        let vm_type = if basic_type.is_aggregate() {
45            // TODO: Should have a check if the variable needs the storage (if it is in an assignment in a copy)
46            swamp_vm_layout::check_type_size(
47                &basic_type,
48                &format!("variable '{}'", var_ref.assigned_name),
49            );
50
51            let kind = match var_ref.variable_type {
52                VariableType::Local => VariableInfoKind::Variable(VariableInfo {
53                    is_mutable: var_ref.is_mutable(),
54                    name: var_ref.assigned_name.clone(),
55                }),
56                VariableType::Parameter => VariableInfoKind::Parameter(VariableInfo {
57                    is_mutable: var_ref.is_mutable(),
58                    name: var_ref.assigned_name.clone(),
59                }),
60            };
61
62            if matches!(var_ref.variable_type, VariableType::Local) {
63                let var_frame_placed_type = local_frame_allocator.allocate_type(&basic_type);
64                //trace!(?var_ref.assigned_name, ?var_frame_placed_type, "laying out");
65                writeln!(
66                    &mut enter_comment,
67                    "  {}:{} {}",
68                    var_frame_placed_type.addr(),
69                    var_frame_placed_type.size().0,
70                    var_ref.assigned_name
71                )
72                    .unwrap();
73
74                frame_memory_infos.push(FrameAddressInfo {
75                    kind,
76                    frame_placed_type: var_frame_placed_type.clone(),
77                });
78                VmType::new_frame_placed(var_frame_placed_type)
79            } else {
80                // even if it is an aggregate, parameters do not need any frame memory, they are allocated elsewhere
81                VmType::new_contained_in_register(basic_type)
82            }
83        } else {
84            // If it is a scalar, then it is in a register no matter if it is a parameter or local
85            VmType::new_contained_in_register(basic_type)
86        };
87
88        let typed_register = TypedRegister {
89            index: var_ref.virtual_register,
90            ty: vm_type,
91            comment: String::new(),
92        };
93
94        //info!(unique_id=?var_ref.unique_id_within_function, name=?var_ref.assigned_name, "insert variable");
95        all_variable_unique_to_register
96            .insert(var_ref.unique_id_within_function, typed_register.clone())
97            .unwrap();
98
99        let var_register = VariableRegister {
100            unique_id_in_function: var_ref.unique_id_within_function,
101            variable: VariableInfo {
102                is_mutable: var_ref.is_mutable(),
103                name: var_ref.assigned_name.clone(),
104            },
105            register: typed_register.clone(),
106        };
107        variable_registers_for_debug_info.push(var_register);
108    }
109
110    let variable_space = local_frame_allocator.addr().as_size();
111
112    let frame_size = local_frame_allocator.addr().as_size();
113
114    let return_type_ref = TypeRef::from(exp_return_type.clone());
115    let return_type = VmType::new_contained_in_register(layout_cache.layout(&return_type_ref));
116
117    FrameAndVariableInfo {
118        frame_memory: FrameMemoryInfo {
119            infos: frame_memory_infos,
120            total_frame_size: frame_size,
121            variable_frame_size: frame_size,
122            frame_size_for_variables_except_temp: variable_space,
123            variable_registers: variable_registers_for_debug_info,
124        },
125        local_frame_allocator,
126        return_type,
127        parameter_and_variable_offsets: all_variable_unique_to_register,
128    }
129}