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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    cfgir,
    command_line::{DEFAULT_OUTPUT_DIR, MOVE_COMPILED_INTERFACES_DIR},
    compiled_unit,
    compiled_unit::AnnotatedCompiledUnit,
    diagnostics::{codes::Severity, *},
    expansion, hlir, interface_generator, naming, parser,
    parser::{comments::*, *},
    shared::{AddressBytes, CompilationEnv, Flags},
    to_bytecode, typing, unit_test,
};
use move_command_line_common::files::{
    extension_equals, find_filenames, MOVE_COMPILED_EXTENSION, MOVE_EXTENSION, SOURCE_MAP_EXTENSION,
};
use move_core_types::language_storage::ModuleId as CompiledModuleId;
use move_symbol_pool::Symbol;
use std::{
    collections::BTreeMap,
    fs,
    fs::File,
    io::{Read, Write},
    path::{Path, PathBuf},
};
use tempfile::NamedTempFile;

//**************************************************************************************************
// Definitions
//**************************************************************************************************

pub struct Compiler<'a, 'b> {
    targets: &'a [String],
    deps: &'a [String],
    interface_files_dir_opt: Option<String>,
    pre_compiled_lib: Option<&'b FullyCompiledProgram>,
    compiled_module_named_address_mapping: BTreeMap<CompiledModuleId, String>,
    named_address_mapping: BTreeMap<Symbol, AddressBytes>,
    flags: Flags,
}

pub struct SteppedCompiler<'a, const P: Pass> {
    compilation_env: CompilationEnv,
    pre_compiled_lib: Option<&'a FullyCompiledProgram>,
    program: Option<PassResult>,
}

pub type Pass = u8;
pub const EMPTY_COMPILER: Pass = 0;
pub const PASS_PARSER: Pass = 1;
pub const PASS_EXPANSION: Pass = 2;
pub const PASS_NAMING: Pass = 3;
pub const PASS_TYPING: Pass = 4;
pub const PASS_HLIR: Pass = 5;
pub const PASS_CFGIR: Pass = 6;
pub const PASS_COMPILATION: Pass = 7;

#[derive(Debug)]
enum PassResult {
    Parser(parser::ast::Program),
    Expansion(expansion::ast::Program),
    Naming(naming::ast::Program),
    Typing(typing::ast::Program),
    HLIR(hlir::ast::Program),
    CFGIR(cfgir::ast::Program),
    Compilation(Vec<AnnotatedCompiledUnit>, /* warnings */ Diagnostics),
}

#[derive(Clone)]
pub struct FullyCompiledProgram {
    // TODO don't store this...
    pub files: FilesSourceText,
    pub parser: parser::ast::Program,
    pub expansion: expansion::ast::Program,
    pub naming: naming::ast::Program,
    pub typing: typing::ast::Program,
    pub hlir: hlir::ast::Program,
    pub cfgir: cfgir::ast::Program,
    pub compiled: Vec<AnnotatedCompiledUnit>,
}

//**************************************************************************************************
// Entry points and impls
//**************************************************************************************************

impl<'a, 'b> Compiler<'a, 'b> {
    pub fn new(targets: &'a [String], deps: &'a [String]) -> Self {
        Self {
            targets,
            deps,
            interface_files_dir_opt: None,
            pre_compiled_lib: None,
            compiled_module_named_address_mapping: BTreeMap::new(),
            flags: Flags::empty(),
            named_address_mapping: BTreeMap::new(),
        }
    }

    pub fn set_flags(mut self, flags: Flags) -> Self {
        assert!(self.flags.is_empty());
        self.flags = flags;
        self
    }

    pub fn set_interface_files_dir(mut self, dir: String) -> Self {
        assert!(self.interface_files_dir_opt.is_none());
        self.interface_files_dir_opt = Some(dir);
        self
    }

    pub fn set_interface_files_dir_opt(mut self, dir_opt: Option<String>) -> Self {
        assert!(self.interface_files_dir_opt.is_none());
        self.interface_files_dir_opt = dir_opt;
        self
    }

    pub fn set_pre_compiled_lib(mut self, pre_compiled_lib: &'b FullyCompiledProgram) -> Self {
        assert!(self.pre_compiled_lib.is_none());
        self.pre_compiled_lib = Some(pre_compiled_lib);
        self
    }

    pub fn set_pre_compiled_lib_opt(
        mut self,
        pre_compiled_lib: Option<&'b FullyCompiledProgram>,
    ) -> Self {
        assert!(self.pre_compiled_lib.is_none());
        self.pre_compiled_lib = pre_compiled_lib;
        self
    }

    pub fn set_compiled_module_named_address_mapping(
        mut self,
        compiled_module_named_address_mapping: BTreeMap<CompiledModuleId, String>,
    ) -> Self {
        assert!(self.compiled_module_named_address_mapping.is_empty());
        self.compiled_module_named_address_mapping = compiled_module_named_address_mapping;
        self
    }

    pub fn set_named_address_values(
        mut self,
        named_address_mapping: BTreeMap<impl Into<Symbol>, AddressBytes>,
    ) -> Self {
        assert!(self.named_address_mapping.is_empty());
        self.named_address_mapping = named_address_mapping
            .into_iter()
            .map(|(k, v)| (k.into(), v))
            .collect();
        self
    }

    pub fn run<const TARGET: Pass>(
        self,
    ) -> anyhow::Result<(
        FilesSourceText,
        Result<(CommentMap, SteppedCompiler<'b, TARGET>), Diagnostics>,
    )> {
        let Self {
            targets,
            deps,
            interface_files_dir_opt,
            pre_compiled_lib,
            compiled_module_named_address_mapping,
            named_address_mapping,
            flags,
        } = self;
        let mut deps = deps.to_vec();
        generate_interface_files_for_deps(
            &mut deps,
            interface_files_dir_opt,
            &compiled_module_named_address_mapping,
        )?;
        let compilation_env = CompilationEnv::new(flags, named_address_mapping);
        let (source_text, pprog_and_comments_res) =
            parse_program(&compilation_env, targets, &deps)?;
        let res: Result<_, Diagnostics> = pprog_and_comments_res.and_then(|(pprog, comments)| {
            SteppedCompiler::new_at_parser(compilation_env, pre_compiled_lib, pprog)
                .run::<TARGET>()
                .map(|compiler| (comments, compiler))
        });
        Ok((source_text, res))
    }

    pub fn check(self) -> anyhow::Result<(FilesSourceText, Result<(), Diagnostics>)> {
        let (files, res) = self.run::<PASS_COMPILATION>()?;
        Ok((files, res.map(|_| ())))
    }

    pub fn check_and_report(self) -> anyhow::Result<FilesSourceText> {
        let (files, res) = self.check()?;
        unwrap_or_report_diagnostics(&files, res);
        Ok(files)
    }

    pub fn build(
        self,
    ) -> anyhow::Result<(
        FilesSourceText,
        Result<(Vec<AnnotatedCompiledUnit>, Diagnostics), Diagnostics>,
    )> {
        let (files, res) = self.run::<PASS_COMPILATION>()?;
        Ok((
            files,
            res.map(|(_comments, stepped)| stepped.into_compiled_units()),
        ))
    }

    pub fn build_and_report(self) -> anyhow::Result<(FilesSourceText, Vec<AnnotatedCompiledUnit>)> {
        let (files, units_res) = self.build()?;
        let (units, warnings) = unwrap_or_report_diagnostics(&files, units_res);
        report_warnings(&files, warnings);
        Ok((files, units))
    }
}

impl<'a, const P: Pass> SteppedCompiler<'a, P> {
    fn run_impl<const TARGET: Pass>(self) -> Result<SteppedCompiler<'a, TARGET>, Diagnostics> {
        assert!(P > EMPTY_COMPILER);
        assert!(self.program.is_some());
        assert!(self.program.as_ref().unwrap().equivalent_pass() == P);
        assert!(
            P <= PASS_COMPILATION,
            "Invalid pass for run_to. Initial pass is too large."
        );
        assert!(
            P <= TARGET,
            "Invalid pass for run_to. Target pass precedes the current pass"
        );
        let Self {
            mut compilation_env,
            pre_compiled_lib,
            program,
        } = self;
        let new_prog = run(
            &mut compilation_env,
            pre_compiled_lib,
            program.unwrap(),
            TARGET,
            |_, _| (),
        )?;
        assert!(new_prog.equivalent_pass() == TARGET);
        Ok(SteppedCompiler {
            compilation_env,
            pre_compiled_lib,
            program: Some(new_prog),
        })
    }

    pub fn compilation_env(&mut self) -> &mut CompilationEnv {
        &mut self.compilation_env
    }
}

macro_rules! ast_stepped_compilers {
    ($(($pass:ident, $mod:ident, $result:ident, $at_ast:ident, $new:ident)),*) => {
        impl<'a> SteppedCompiler<'a, EMPTY_COMPILER> {
            $(
                pub fn $at_ast(self, ast: $mod::ast::Program) -> SteppedCompiler<'a, {$pass}> {
                    let Self {
                        compilation_env,
                        pre_compiled_lib,
                        program,
                    } = self;
                    assert!(program.is_none());
                    SteppedCompiler::$new(
                        compilation_env,
                        pre_compiled_lib,
                        ast
                    )
                }
            )*
        }

        $(
            impl<'a> SteppedCompiler<'a, {$pass}> {
                fn $new(
                    compilation_env: CompilationEnv,
                    pre_compiled_lib: Option<&'a FullyCompiledProgram>,
                    ast: $mod::ast::Program,
                ) -> Self {
                    Self {
                        compilation_env,
                        pre_compiled_lib,
                        program: Some(PassResult::$result(ast)),
                    }
                }

                pub fn run<const TARGET: Pass>(
                    self
                ) -> Result<SteppedCompiler<'a, TARGET>, Diagnostics> {
                    self.run_impl()
                }

                pub fn into_ast(self) -> (SteppedCompiler<'a, EMPTY_COMPILER>, $mod::ast::Program) {
                    let Self {
                        compilation_env,
                        pre_compiled_lib,
                        program,
                    } = self;
                    let ast = match program {
                        Some(PassResult::$result(ast)) => ast,
                        _ => panic!(),
                    };
                    let next = SteppedCompiler {
                        compilation_env,
                        pre_compiled_lib,
                        program: None,
                    };
                    (next, ast)
                }

                pub fn check(self) -> Result<(), Diagnostics> {
                    self.run::<PASS_COMPILATION>()?;
                    Ok(())
                }

                pub fn build(self) -> Result<(Vec<AnnotatedCompiledUnit>, Diagnostics), Diagnostics> {
                    let units = self.run::<PASS_COMPILATION>()?.into_compiled_units();
                    Ok(units)
                }

                pub fn check_and_report(self, files: &FilesSourceText)  {
                    let errors_result = self.check();
                    unwrap_or_report_diagnostics(&files, errors_result);
                }

                pub fn build_and_report(
                    self,
                    files: &FilesSourceText,
                ) -> Vec<AnnotatedCompiledUnit> {
                    let units_result = self.build();
                    let (units, warnings) = unwrap_or_report_diagnostics(&files, units_result);
                    report_warnings(&files, warnings);
                    units
                }
            }
        )*
    };
}

ast_stepped_compilers!(
    (PASS_PARSER, parser, Parser, at_parser, new_at_parser),
    (
        PASS_EXPANSION,
        expansion,
        Expansion,
        at_expansion,
        new_at_expansion
    ),
    (PASS_NAMING, naming, Naming, at_naming, new_at_naming),
    (PASS_TYPING, typing, Typing, at_typing, new_at_typing),
    (PASS_HLIR, hlir, HLIR, at_hlir, new_at_hlir),
    (PASS_CFGIR, cfgir, CFGIR, at_cfgir, new_at_cfgir)
);

impl<'a> SteppedCompiler<'a, PASS_COMPILATION> {
    pub fn into_compiled_units(self) -> (Vec<AnnotatedCompiledUnit>, Diagnostics) {
        let Self {
            compilation_env: _,
            pre_compiled_lib: _,
            program,
        } = self;
        match program {
            Some(PassResult::Compilation(units, warnings)) => (units, warnings),
            _ => panic!(),
        }
    }
}

/// Given a set of dependencies, precompile them and save the ASTs so that they can be used again
/// to compile against without having to recompile these dependencies
pub fn construct_pre_compiled_lib(
    deps: &[String],
    interface_files_dir_opt: Option<String>,
    flags: Flags,
    named_address_values: BTreeMap<String, AddressBytes>,
) -> anyhow::Result<Result<FullyCompiledProgram, (FilesSourceText, Diagnostics)>> {
    let (files, pprog_and_comments_res) = Compiler::new(&[], deps)
        .set_interface_files_dir_opt(interface_files_dir_opt)
        .set_flags(flags)
        .set_named_address_values(named_address_values)
        .run::<PASS_PARSER>()?;

    let (_comments, stepped) = match pprog_and_comments_res {
        Err(errors) => return Ok(Err((files, errors))),
        Ok(res) => res,
    };

    let (empty_compiler, ast) = stepped.into_ast();
    let mut compilation_env = empty_compiler.compilation_env;
    let start = PassResult::Parser(ast);
    let mut parser = None;
    let mut expansion = None;
    let mut naming = None;
    let mut typing = None;
    let mut hlir = None;
    let mut cfgir = None;
    let mut compiled = None;

    let save_result = |cur: &PassResult, env: &CompilationEnv| match cur {
        PassResult::Parser(prog) => {
            assert!(parser.is_none());
            parser = Some(prog.clone())
        }
        PassResult::Expansion(eprog) => {
            if env.has_diags() {
                return;
            }
            assert!(expansion.is_none());
            expansion = Some(eprog.clone())
        }
        PassResult::Naming(nprog) => {
            if env.has_diags() {
                return;
            }
            assert!(naming.is_none());
            naming = Some(nprog.clone())
        }
        PassResult::Typing(tprog) => {
            assert!(typing.is_none());
            typing = Some(tprog.clone())
        }
        PassResult::HLIR(hprog) => {
            if env.has_diags() {
                return;
            }
            assert!(hlir.is_none());
            hlir = Some(hprog.clone());
        }
        PassResult::CFGIR(cprog) => {
            assert!(cfgir.is_none());
            cfgir = Some(cprog.clone());
        }
        PassResult::Compilation(units, _final_diags) => {
            assert!(compiled.is_none());
            compiled = Some(units.clone())
        }
    };
    match run(
        &mut compilation_env,
        None,
        start,
        PASS_COMPILATION,
        save_result,
    ) {
        Err(errors) => Ok(Err((files, errors))),
        Ok(_) => Ok(Ok(FullyCompiledProgram {
            files,
            parser: parser.unwrap(),
            expansion: expansion.unwrap(),
            naming: naming.unwrap(),
            typing: typing.unwrap(),
            hlir: hlir.unwrap(),
            cfgir: cfgir.unwrap(),
            compiled: compiled.unwrap(),
        })),
    }
}

//**************************************************************************************************
// Utils
//**************************************************************************************************

macro_rules! dir_path {
    ($($dir:expr),+) => {{
        let mut p = PathBuf::new();
        $(p.push($dir);)+
        p
    }};
}

macro_rules! file_path {
    ($dir:expr, $name:expr, $ext:expr) => {{
        let mut p = PathBuf::from($dir);
        p.push($name);
        p.set_extension($ext);
        p
    }};
}

/// Runs the bytecode verifier on the compiled units
/// Fails if the bytecode verifier errors
pub fn sanity_check_compiled_units(
    files: FilesSourceText,
    compiled_units: Vec<AnnotatedCompiledUnit>,
) {
    let (_, ice_errors) = compiled_unit::verify_units(compiled_units);
    if !ice_errors.is_empty() {
        report_diagnostics(&files, ice_errors)
    }
}

/// Given a file map and a set of compiled programs, saves the compiled programs to disk
pub fn output_compiled_units(
    emit_source_maps: bool,
    files: FilesSourceText,
    compiled_units: Vec<AnnotatedCompiledUnit>,
    out_dir: &str,
) -> anyhow::Result<()> {
    const SCRIPT_SUB_DIR: &str = "scripts";
    const MODULE_SUB_DIR: &str = "modules";
    fn num_digits(n: usize) -> usize {
        format!("{}", n).len()
    }
    fn format_idx(idx: usize, width: usize) -> String {
        format!("{:0width$}", idx, width = width)
    }

    macro_rules! emit_unit {
        ($path:ident, $unit:ident) => {{
            if emit_source_maps {
                $path.set_extension(SOURCE_MAP_EXTENSION);
                fs::write($path.as_path(), &$unit.serialize_source_map())?;
            }

            $path.set_extension(MOVE_COMPILED_EXTENSION);
            fs::write($path.as_path(), &$unit.serialize())?
        }};
    }

    let (compiled_units, ice_errors) = compiled_unit::verify_units(compiled_units);
    let (modules, scripts): (Vec<_>, Vec<_>) = compiled_units
        .into_iter()
        .partition(|u| matches!(u, AnnotatedCompiledUnit::Module(_)));

    // modules
    if !modules.is_empty() {
        std::fs::create_dir_all(dir_path!(out_dir, MODULE_SUB_DIR))?;
    }
    let digit_width = num_digits(modules.len());
    for (idx, unit) in modules.into_iter().enumerate() {
        let unit = unit.into_compiled_unit();
        let mut path = dir_path!(
            out_dir,
            MODULE_SUB_DIR,
            format!("{}_{}", format_idx(idx, digit_width), unit.name())
        );
        emit_unit!(path, unit);
    }

    // scripts
    if !scripts.is_empty() {
        std::fs::create_dir_all(dir_path!(out_dir, SCRIPT_SUB_DIR))?;
    }
    for unit in scripts {
        let unit = unit.into_compiled_unit();
        let mut path = dir_path!(out_dir, SCRIPT_SUB_DIR, unit.name().as_str());
        emit_unit!(path, unit);
    }

    if !ice_errors.is_empty() {
        report_diagnostics(&files, ice_errors)
    }
    Ok(())
}

fn generate_interface_files_for_deps(
    deps: &mut Vec<String>,
    interface_files_dir_opt: Option<String>,
    named_address_mapping: &BTreeMap<CompiledModuleId, String>,
) -> anyhow::Result<()> {
    if let Some(dir) =
        generate_interface_files(deps, interface_files_dir_opt, named_address_mapping, true)?
    {
        deps.push(dir)
    }
    Ok(())
}

pub fn generate_interface_files(
    mv_file_locations: &[String],
    interface_files_dir_opt: Option<String>,
    named_address_mapping: &BTreeMap<CompiledModuleId, String>,
    separate_by_hash: bool,
) -> anyhow::Result<Option<String>> {
    let mv_files = {
        let mut v = vec![];
        let (mv_magic_files, other_file_locations): (Vec<_>, Vec<_>) = mv_file_locations
            .iter()
            .cloned()
            .partition(|s| Path::new(s).is_file() && has_compiled_module_magic_number(s));
        v.extend(mv_magic_files);
        let mv_ext_files = find_filenames(&other_file_locations, |path| {
            extension_equals(path, MOVE_COMPILED_EXTENSION)
        })?;
        v.extend(mv_ext_files);
        v
    };
    if mv_files.is_empty() {
        return Ok(None);
    }

    let interface_files_dir =
        interface_files_dir_opt.unwrap_or_else(|| DEFAULT_OUTPUT_DIR.to_string());
    let interface_sub_dir = dir_path!(interface_files_dir, MOVE_COMPILED_INTERFACES_DIR);
    let all_addr_dir = if separate_by_hash {
        use std::{
            collections::hash_map::DefaultHasher,
            hash::{Hash, Hasher},
        };
        const HASH_DELIM: &str = "%|%";

        let mut hasher = DefaultHasher::new();
        mv_files.len().hash(&mut hasher);
        HASH_DELIM.hash(&mut hasher);
        for mv_file in &mv_files {
            std::fs::read(mv_file)?.hash(&mut hasher);
            HASH_DELIM.hash(&mut hasher);
        }

        let mut dir = interface_sub_dir;
        dir.push(format!("{:020}", hasher.finish()));
        dir
    } else {
        interface_sub_dir
    };

    for mv_file in mv_files {
        let (id, interface_contents) =
            interface_generator::write_file_to_string(named_address_mapping, &mv_file)?;
        let addr_dir = dir_path!(all_addr_dir.clone(), format!("{}", id.address()));
        let file_path = file_path!(addr_dir.clone(), format!("{}", id.name()), MOVE_EXTENSION);
        // it's possible some files exist but not others due to multithreaded environments
        if separate_by_hash && Path::new(&file_path).is_file() {
            continue;
        }

        std::fs::create_dir_all(&addr_dir)?;

        let mut tmp = NamedTempFile::new_in(addr_dir)?;
        tmp.write_all(interface_contents.as_bytes())?;

        // it's possible some files exist but not others due to multithreaded environments
        // Check for the file existing and then safely move the tmp file there if
        // it does not
        if separate_by_hash && Path::new(&file_path).is_file() {
            continue;
        }
        std::fs::rename(tmp.path(), file_path)?;
    }

    Ok(Some(all_addr_dir.into_os_string().into_string().unwrap()))
}

fn has_compiled_module_magic_number(path: &str) -> bool {
    use move_binary_format::file_format_common::BinaryConstants;
    let mut file = match File::open(path) {
        Err(_) => return false,
        Ok(f) => f,
    };
    let mut magic = [0u8; BinaryConstants::DIEM_MAGIC_SIZE];
    let num_bytes_read = match file.read(&mut magic) {
        Err(_) => return false,
        Ok(n) => n,
    };
    num_bytes_read == BinaryConstants::DIEM_MAGIC_SIZE && magic == BinaryConstants::DIEM_MAGIC
}

//**************************************************************************************************
// Translations
//**************************************************************************************************

impl PassResult {
    pub fn equivalent_pass(&self) -> Pass {
        match self {
            PassResult::Parser(_) => PASS_PARSER,
            PassResult::Expansion(_) => PASS_EXPANSION,
            PassResult::Naming(_) => PASS_NAMING,
            PassResult::Typing(_) => PASS_TYPING,
            PassResult::HLIR(_) => PASS_HLIR,
            PassResult::CFGIR(_) => PASS_CFGIR,
            PassResult::Compilation(_, _) => PASS_COMPILATION,
        }
    }
}

fn run(
    compilation_env: &mut CompilationEnv,
    pre_compiled_lib: Option<&FullyCompiledProgram>,
    cur: PassResult,
    until: Pass,
    mut result_check: impl FnMut(&PassResult, &CompilationEnv),
) -> Result<PassResult, Diagnostics> {
    assert!(
        until <= PASS_COMPILATION,
        "Invalid pass for run_to. Target is greater than maximum pass"
    );
    result_check(&cur, compilation_env);
    if cur.equivalent_pass() >= until {
        return Ok(cur);
    }

    match cur {
        PassResult::Parser(prog) => {
            let prog = parser::sources_shadow_deps::program(compilation_env, prog);
            let prog = parser::merge_spec_modules::program(compilation_env, prog);
            let prog = unit_test::filter_test_members::program(compilation_env, prog);
            let eprog = expansion::translate::program(compilation_env, pre_compiled_lib, prog);
            compilation_env.check_diags_at_or_above_severity(Severity::Bug)?;
            run(
                compilation_env,
                pre_compiled_lib,
                PassResult::Expansion(eprog),
                until,
                result_check,
            )
        }
        PassResult::Expansion(eprog) => {
            let nprog = naming::translate::program(compilation_env, pre_compiled_lib, eprog);
            compilation_env.check_diags_at_or_above_severity(Severity::Bug)?;
            run(
                compilation_env,
                pre_compiled_lib,
                PassResult::Naming(nprog),
                until,
                result_check,
            )
        }
        PassResult::Naming(nprog) => {
            let tprog = typing::translate::program(compilation_env, pre_compiled_lib, nprog);
            compilation_env.check_diags_at_or_above_severity(Severity::BlockingError)?;
            run(
                compilation_env,
                pre_compiled_lib,
                PassResult::Typing(tprog),
                until,
                result_check,
            )
        }
        PassResult::Typing(tprog) => {
            let hprog = hlir::translate::program(compilation_env, pre_compiled_lib, tprog);
            compilation_env.check_diags_at_or_above_severity(Severity::Bug)?;
            run(
                compilation_env,
                pre_compiled_lib,
                PassResult::HLIR(hprog),
                until,
                result_check,
            )
        }
        PassResult::HLIR(hprog) => {
            let cprog = cfgir::translate::program(compilation_env, pre_compiled_lib, hprog);
            compilation_env.check_diags_at_or_above_severity(Severity::NonblockingError)?;
            run(
                compilation_env,
                pre_compiled_lib,
                PassResult::CFGIR(cprog),
                until,
                result_check,
            )
        }
        PassResult::CFGIR(cprog) => {
            let compiled_units =
                to_bytecode::translate::program(compilation_env, pre_compiled_lib, cprog);
            compilation_env.check_diags_at_or_above_severity(Severity::NonblockingError)?;
            let warnings = compilation_env.take_final_warning_diags();
            assert!(until == PASS_COMPILATION);
            run(
                compilation_env,
                pre_compiled_lib,
                PassResult::Compilation(compiled_units, warnings),
                PASS_COMPILATION,
                result_check,
            )
        }
        PassResult::Compilation(_, _) => unreachable!("ICE Pass::Compilation is >= all passes"),
    }
}