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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    diag,
    diagnostics::Diagnostics,
    expansion::ast::{ModuleIdent, ModuleIdent_, SpecId},
    hlir::ast as H,
    parser::ast::{FunctionName, ModuleName, Var},
    shared::{unique_map::UniqueMap, AddressBytes, Name},
};
use bytecode_source_map::source_map::SourceMap;
use move_binary_format::file_format as F;
use move_core_types::{
    account_address::AccountAddress, identifier::Identifier as MoveCoreIdentifier,
    language_storage::ModuleId,
};
use move_ir_types::location::*;
use move_symbol_pool::Symbol;
use std::collections::BTreeMap;

//**************************************************************************************************
// Compiled Unit
//**************************************************************************************************

#[derive(Debug, Clone)]
pub struct VarInfo {
    pub type_: H::SingleType,
    pub index: F::LocalIndex,
}

#[derive(Debug, Clone)]
pub struct SpecInfo {
    pub offset: F::CodeOffset,
    // Free locals that are used but not declared in the block
    pub used_locals: UniqueMap<Var, VarInfo>,
}

#[derive(Debug, Clone)]
pub struct FunctionInfo {
    pub spec_info: BTreeMap<SpecId, SpecInfo>,
    pub parameters: Vec<(Var, VarInfo)>,
}

#[derive(Debug, Clone)]
pub struct NamedCompiledModule {
    pub address_bytes: AddressBytes,
    pub name: Symbol,
    pub module: F::CompiledModule,
    pub source_map: SourceMap,
}

#[derive(Debug, Clone)]
pub struct NamedCompiledScript {
    pub name: Symbol,
    pub script: F::CompiledScript,
    pub source_map: SourceMap,
}

#[derive(Debug, Clone)]
pub struct AnnotatedCompiledModule {
    pub loc: Loc,
    pub module_name_loc: Loc,
    pub address_name: Option<Name>,
    pub named_module: NamedCompiledModule,
    pub function_infos: UniqueMap<FunctionName, FunctionInfo>,
}

#[derive(Debug, Clone)]
pub struct AnnotatedCompiledScript {
    pub loc: Loc,
    pub named_script: NamedCompiledScript,
    pub function_info: FunctionInfo,
}

pub trait TargetModule {}
pub trait TargetScript {}
impl TargetScript for AnnotatedCompiledScript {}
impl TargetScript for NamedCompiledScript {}
impl TargetModule for AnnotatedCompiledModule {}
impl TargetModule for NamedCompiledModule {}

#[derive(Debug, Clone)]
pub enum CompiledUnitEnum<TModule: TargetModule, TScript: TargetScript> {
    Module(TModule),
    Script(TScript),
}

pub type CompiledUnit = CompiledUnitEnum<NamedCompiledModule, NamedCompiledScript>;
pub type AnnotatedCompiledUnit = CompiledUnitEnum<AnnotatedCompiledModule, AnnotatedCompiledScript>;

impl AnnotatedCompiledModule {
    pub fn module_ident(&self) -> ModuleIdent {
        use crate::expansion::ast::Address;
        let address = match self.address_name {
            None => Address::Anonymous(sp(self.loc, self.named_module.address_bytes)),
            Some(n) => Address::Named(n),
        };
        sp(
            self.loc,
            ModuleIdent_::new(
                address,
                ModuleName(sp(self.module_name_loc, self.named_module.name)),
            ),
        )
    }

    pub fn module_id(&self) -> (Option<Name>, ModuleId) {
        let id = ModuleId::new(
            AccountAddress::new(self.named_module.address_bytes.into_bytes()),
            MoveCoreIdentifier::new(self.named_module.name.to_string()).unwrap(),
        );
        (self.address_name, id)
    }
}

impl AnnotatedCompiledUnit {
    pub fn verify(self) -> (Self, Diagnostics) {
        match self {
            Self::Module(AnnotatedCompiledModule {
                loc,
                module_name_loc,
                address_name,
                named_module: module,
                function_infos,
            }) => {
                let NamedCompiledModule {
                    address_bytes,
                    name,
                    module,
                    source_map,
                } = module;
                let (module, errors) = verify_module(loc, module);
                let verified = AnnotatedCompiledModule {
                    loc,
                    module_name_loc,
                    address_name,
                    named_module: NamedCompiledModule {
                        address_bytes,
                        name,
                        module,
                        source_map,
                    },
                    function_infos,
                };
                (Self::Module(verified), errors)
            }
            Self::Script(AnnotatedCompiledScript {
                loc,
                named_script: script,
                function_info,
            }) => {
                let NamedCompiledScript {
                    name,
                    script,
                    source_map,
                } = script;
                let (script, errors) = verify_script(loc, script);
                let verified = AnnotatedCompiledScript {
                    named_script: NamedCompiledScript {
                        name,
                        script,
                        source_map,
                    },
                    loc,
                    function_info,
                };
                (Self::Script(verified), errors)
            }
        }
    }

    pub fn into_compiled_unit(self) -> CompiledUnit {
        match self {
            Self::Module(AnnotatedCompiledModule {
                named_module: module,
                ..
            }) => CompiledUnitEnum::Module(module),
            Self::Script(AnnotatedCompiledScript {
                named_script: script,
                ..
            }) => CompiledUnitEnum::Script(script),
        }
    }

    pub fn loc(&self) -> &Loc {
        match self {
            Self::Module(AnnotatedCompiledModule { loc, .. })
            | Self::Script(AnnotatedCompiledScript { loc, .. }) => loc,
        }
    }
}

impl CompiledUnit {
    pub fn name(&self) -> Symbol {
        match self {
            Self::Module(NamedCompiledModule { name, .. })
            | Self::Script(NamedCompiledScript { name, .. }) => *name,
        }
    }

    pub fn serialize(&self) -> Vec<u8> {
        let mut serialized = Vec::<u8>::new();
        match self {
            Self::Module(NamedCompiledModule { module, .. }) => {
                module.serialize(&mut serialized).unwrap()
            }
            Self::Script(NamedCompiledScript { script, .. }) => {
                script.serialize(&mut serialized).unwrap()
            }
        };
        serialized
    }

    #[allow(dead_code)]
    pub fn serialize_debug(self) -> Vec<u8> {
        match self {
            Self::Module(NamedCompiledModule { module, .. }) => format!("{:?}", module),
            Self::Script(NamedCompiledScript { script, .. }) => format!("{:?}", script),
        }
        .into()
    }

    pub fn serialize_source_map(&self) -> Vec<u8> {
        match self {
            Self::Module(NamedCompiledModule { source_map, .. }) => {
                bcs::to_bytes(source_map).unwrap()
            }
            Self::Script(NamedCompiledScript { source_map, .. }) => {
                bcs::to_bytes(source_map).unwrap()
            }
        }
    }
}

fn verify_module(loc: Loc, cm: F::CompiledModule) -> (F::CompiledModule, Diagnostics) {
    match move_bytecode_verifier::verifier::verify_module(&cm) {
        Ok(_) => (cm, Diagnostics::new()),
        Err(e) => (
            cm,
            Diagnostics::from(vec![diag!(
                Bug::BytecodeVerification,
                (loc, format!("ICE failed bytecode verifier: {:#?}", e)),
            )]),
        ),
    }
}

fn verify_script(loc: Loc, cs: F::CompiledScript) -> (F::CompiledScript, Diagnostics) {
    match move_bytecode_verifier::verifier::verify_script(&cs) {
        Ok(_) => (cs, Diagnostics::new()),
        Err(e) => (
            cs,
            Diagnostics::from(vec![diag!(
                Bug::BytecodeVerification,
                (loc, format!("ICE failed bytecode verifier: {:#?}", e)),
            )]),
        ),
    }
}

pub fn verify_units(
    units: Vec<AnnotatedCompiledUnit>,
) -> (Vec<AnnotatedCompiledUnit>, Diagnostics) {
    let mut new_units = vec![];
    let mut diags = Diagnostics::new();
    for unit in units {
        let (unit, ds) = unit.verify();
        new_units.push(unit);
        diags.extend(ds);
    }
    (new_units, diags)
}