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
//! Intermediate representation of the globals.

use crate::error::Error;
use crate::ir::id::{GlobalID, ImportsID};
use crate::ir::module::module_imports::ModuleImports;
use crate::ir::module::{GetID, Iter, LocalOrImport, ReIndexable};
use crate::InitExpr;
use std::vec::IntoIter;
use wasmparser::{GlobalType, TypeRef};

type Result<T> = std::result::Result<T, Error>;

/// Represents whether a Global is Local or Imported
#[derive(Clone, Debug)]
pub enum GlobalKind {
    Local(LocalGlobal),
    Import(ImportedGlobal),
}

/// Represents a global that is locally defined in the module.
#[derive(Clone, Debug)]
pub struct LocalGlobal {
    pub global_id: GlobalID,
    pub ty: GlobalType,
    pub init_expr: InitExpr,
}

/// Represents a global that is imported into the module.
#[derive(Clone, Debug)]
pub struct ImportedGlobal {
    pub import_id: ImportsID, // Maps to location in a modules imports
    pub(crate) import_global_id: GlobalID, // Maps to location in a modules imported globals
    pub ty: GlobalType,
}

impl ImportedGlobal {
    pub(crate) fn new(import_id: ImportsID, import_global_id: GlobalID, ty: GlobalType) -> Self {
        Self {
            import_id,
            import_global_id,
            ty,
        }
    }
}

/// Globals in a wasm module.
#[derive(Debug, Clone)]
pub struct Global {
    /// The kind of global (imported or locally-defined).
    pub(crate) kind: GlobalKind,
    /// Whether this global was deleted.
    pub(crate) deleted: bool,
}

impl GetID for Global {
    /// Get the ID of the global
    fn get_id(&self) -> u32 {
        match &self.kind {
            GlobalKind::Local(LocalGlobal { global_id, .. })
            | GlobalKind::Import(ImportedGlobal {
                import_global_id: global_id,
                ..
            }) => **global_id,
        }
    }
}

impl LocalOrImport for Global {
    /// Returns whether this global is locally defined (not imported).
    fn is_local(&self) -> bool {
        matches!(&self.kind, GlobalKind::Local(_))
    }

    /// Returns whether this global is imported.
    fn is_import(&self) -> bool {
        matches!(&self.kind, GlobalKind::Import(_))
    }

    /// Check if this global has been deleted
    fn is_deleted(&self) -> bool {
        self.deleted
    }
}

impl Global {
    pub fn new(kind: GlobalKind) -> Self {
        Self {
            kind,
            deleted: false,
        }
    }

    /// Convert from wasmparser Global representation to Orca's representation.
    /// Assumes this is a locally-defined global (not imported).
    pub(crate) fn from_wasmparser(global: wasmparser::Global) -> Result<Global> {
        let ty = global.ty;
        let init_expr = InitExpr::eval(&global.init_expr);
        Ok(Global {
            kind: GlobalKind::Local(LocalGlobal {
                global_id: GlobalID(0),
                ty,
                init_expr,
            }),
            deleted: false,
        })
    }

    pub(crate) fn set_id(&mut self, id: GlobalID) {
        match &mut self.kind {
            GlobalKind::Local(LocalGlobal { global_id, .. })
            | GlobalKind::Import(ImportedGlobal {
                import_global_id: global_id,
                ..
            }) => {
                *global_id = id;
            }
        }
    }

    fn delete(&mut self) {
        self.deleted = true;
    }
}

/// The globals section of a module
#[derive(Clone, Debug, Default)]
pub struct ModuleGlobals {
    globals: Vec<Global>,
    pub(crate) recalculate_ids: bool,
}

impl Iter<Global> for ModuleGlobals {
    /// Get an iterator for the functions.
    fn iter(&self) -> std::slice::Iter<'_, Global> {
        self.globals.iter()
    }

    fn get_into_iter(&self) -> IntoIter<Global> {
        self.globals.clone().into_iter()
    }
}

impl ReIndexable<Global> for ModuleGlobals {
    /// Get the number of functions
    fn len(&self) -> usize {
        self.globals.len()
    }
    fn remove(&mut self, global_id: u32) -> Global {
        self.globals.remove(global_id as usize)
    }

    fn insert(&mut self, global_id: u32, global: Global) {
        self.globals.insert(global_id as usize, global);
    }
    /// Add a new function
    fn push(&mut self, global: Global) {
        self.globals.push(global);
    }
}

impl ModuleGlobals {
    /// Create a new globals section
    pub fn new(imports: &ModuleImports, local_globals: Vec<Global>) -> Self {
        let mut result = ModuleGlobals::default();

        // Add the imported globals
        let mut curr_global_id: u32 = 0;
        for (id, import) in imports.iter().enumerate() {
            if let TypeRef::Global(ty) = import.ty {
                curr_global_id += 1;
                // This is an imported global
                result.add(Global {
                    kind: GlobalKind::Import(ImportedGlobal {
                        import_id: ImportsID(id as u32),
                        import_global_id: GlobalID(curr_global_id),
                        ty,
                    }),
                    deleted: false,
                });
            };
        }

        // Add the locally defined globals and fix the IDs
        for global in local_globals.iter() {
            // fix the ID
            let mut owned = global.to_owned();
            owned.set_id(GlobalID(curr_global_id));
            curr_global_id += 1;

            result.add(owned);
        }
        result
    }

    /// Get kind of global
    pub fn get_kind(&self, global_id: GlobalID) -> &GlobalKind {
        &self.globals[*global_id as usize].kind
    }

    /// Create an iterable over the global section
    pub fn iter(&self) -> std::slice::Iter<'_, Global> {
        self.globals.iter()
    }

    /// Get the number of globals
    pub fn len(&self) -> usize {
        self.globals.len()
    }

    /// Check if there are any globals
    pub fn is_empty(&self) -> bool {
        self.globals.is_empty()
    }

    /// Remove the last global from the list. Can only remove the final Global due to indexing
    pub(crate) fn delete(&mut self, id: GlobalID) {
        self.recalculate_ids = true;
        if *id < self.globals.len() as u32 {
            self.globals[*id as usize].delete();
        }
    }

    /// Add a new Global to the module. Returns the index of the new Global.
    pub(crate) fn add(&mut self, mut global: Global) -> GlobalID {
        let id = GlobalID(self.globals.len() as u32);
        global.set_id(id);
        self.globals.push(global);
        id
    }
}