spimdisasm 2.0.0-alpha.1

MIPS disassembler
Documentation
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/* SPDX-FileCopyrightText: © 2024-2025 Decompollaborate */
/* SPDX-License-Identifier: MIT */

use alloc::{collections::btree_map::BTreeMap, sync::Arc, vec::Vec};
use core::hash;

use rabbitizer::{Instruction, InstructionFlags};

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

use crate::addresses::{AddressRange, Rom, RomVramRange, Size, Vram, VramOffset};
use crate::analysis::{InstrOpTailCall, InstructionOperation, ReferenceWrapper, RegisterTracker};
use crate::collections::{addended_ordered_map::FindSettings, unordered_set::UnorderedSet};
use crate::config::{Compiler, Endian, GlobalConfig};
use crate::context::Context;
use crate::metadata::{ParentSectionMetadata, SegmentMetadata, SymbolType};
use crate::parent_segment_info::ParentSegmentInfo;
use crate::relocation::RelocationInfo;
use crate::section_type::SectionType;
use crate::sections::processed::ExecutableSectionProcessed;
use crate::sections::{
    BadBytesSizeError, BadUserSymbolSizeError, EmptySectionError, RomSectionPreprocessed,
    SectionPreprocessed, UnalignedRomError, UnalignedVramError,
};
use crate::str_decoding::Encoding;
use crate::symbols::before_proc::data_sym::DataSymProperties;
use crate::symbols::before_proc::{DataSym, EitherFuncDataSym};
use crate::symbols::SymbolPreprocessed;
use crate::symbols::{
    before_proc::{function_sym::FunctionSymProperties, FunctionSym},
    Symbol,
};

use crate::sections::{
    section_post_process_error::SectionPostProcessError,
    trait_section::RomSection,
    {Section, SectionCreationError},
};

const SECTION_TYPE: SectionType = SectionType::Text;

const BYTES_PER_INSTR: u32 = 4;

#[derive(Debug, Clone)]
#[must_use]
pub struct ExecutableSection {
    name: Arc<str>,

    ranges: RomVramRange,

    parent_segment_info: ParentSegmentInfo,

    // in_section_offset: u32,
    // section_type: SectionType,

    //
    symbols: Vec<EitherFuncDataSym>,

    symbol_vrams: UnorderedSet<Vram>,
}

impl ExecutableSection {
    pub(crate) fn new(
        context: &mut Context,
        settings: &ExecutableSectionSettings,
        name: Arc<str>,
        raw_bytes: Vec<u8>,
        rom: Rom,
        vram: Vram,
        parent_segment_info: ParentSegmentInfo,
    ) -> Result<Self, SectionCreationError> {
        if raw_bytes.is_empty() {
            return Err(EmptySectionError::new(name, vram).into());
        }
        if raw_bytes.len() % BYTES_PER_INSTR as usize != 0 {
            return Err(
                BadBytesSizeError::new(name, raw_bytes.len(), BYTES_PER_INSTR as usize).into(),
            );
        }
        if vram.inner() % BYTES_PER_INSTR != 0 {
            return Err(UnalignedVramError::new(name, vram, BYTES_PER_INSTR as usize).into());
        }
        if rom.inner() % BYTES_PER_INSTR != 0 {
            return Err(UnalignedRomError::new(name, rom, BYTES_PER_INSTR as usize).into());
        }

        let size = Size::new(raw_bytes.len() as u32);
        let rom_range = AddressRange::new(rom, rom + size);
        let vram_range = AddressRange::new(vram, vram + size);
        let ranges = RomVramRange::new(rom_range, vram_range);

        let instrs =
            instrs_from_bytes(settings, context.global_config().endian(), &raw_bytes, vram);
        debug_assert!(!instrs.is_empty(), "{name}, {vram:?}, {rom:?}");

        let owned_segment = context.find_owned_segment(&parent_segment_info)?;
        let boundaries = find_functions(
            context.global_config(),
            settings,
            owned_segment,
            ranges,
            &instrs,
        )?;

        debug_assert!(!boundaries.is_empty(), "{name}, {vram:?}, {rom:?}");

        let mut symbols = Vec::new();
        let mut symbol_vrams = UnorderedSet::new();

        let mut bytes_iter = raw_bytes.into_iter();
        let mut instrs_iter = instrs.into_iter();

        for boundary in boundaries {
            let local_offset = boundary.start as u32 * BYTES_PER_INSTR;
            let s = Size::new(local_offset);
            let current_vram = vram + s;
            let current_rom = rom + s;

            symbol_vrams.insert(vram);

            let parent_metadata =
                ParentSectionMetadata::new(name.clone(), vram, parent_segment_info.clone());

            let word_count = boundary.end - boundary.start;

            let sym = if let Some(data_type) = boundary.data_type {
                let sym_bytes: Arc<[u8]> = bytes_iter
                    .by_ref()
                    .take(word_count * BYTES_PER_INSTR as usize)
                    .collect();
                debug_assert_eq!(sym_bytes.len(), word_count * BYTES_PER_INSTR as usize);
                // Advance the other buffer too
                let _count = instrs_iter.by_ref().take(word_count).count();
                debug_assert_eq!(_count, word_count);

                let properties = DataSymProperties::new(
                    parent_metadata,
                    settings.compiler,
                    boundary.auto_pad_by,
                    Some(data_type),
                    Encoding::default(),
                );
                let data = DataSym::new(
                    context,
                    sym_bytes,
                    current_rom,
                    current_vram,
                    parent_segment_info.clone(),
                    SECTION_TYPE,
                    properties,
                )?;

                EitherFuncDataSym::Data(data)
            } else {
                let sym_instrs: Arc<[Instruction]> =
                    instrs_iter.by_ref().take(word_count).collect();
                debug_assert_eq!(sym_instrs.len(), word_count);
                // Advance the other buffer too
                let _count = bytes_iter
                    .by_ref()
                    .take(word_count * BYTES_PER_INSTR as usize)
                    .count();
                debug_assert_eq!(
                    _count,
                    word_count * BYTES_PER_INSTR as usize,
                    "{} {}",
                    boundary.start,
                    boundary.end
                );

                let properties = FunctionSymProperties {
                    parent_metadata,
                    compiler: settings.compiler,
                    auto_pad_by: boundary.auto_pad_by,
                };
                let func = FunctionSym::new(
                    context,
                    sym_instrs,
                    current_rom,
                    current_vram,
                    parent_segment_info.clone(),
                    properties,
                )?;

                EitherFuncDataSym::Func(func)
            };

            symbols.push(sym);
        }

        Ok(Self {
            name,
            ranges,
            parent_segment_info,
            symbols,
            symbol_vrams,
        })
    }
}

impl ExecutableSection {
    pub fn symbols(&self) -> &[EitherFuncDataSym] {
        &self.symbols
    }
}

impl ExecutableSection {
    pub fn post_process(
        self,
        context: &mut Context,
        user_relocs: &BTreeMap<Rom, RelocationInfo>,
    ) -> Result<ExecutableSectionProcessed, SectionPostProcessError> {
        let ExecutableSection {
            name,
            ranges,
            parent_segment_info,
            symbols,
            symbol_vrams,
        } = self;

        ExecutableSectionProcessed::new(
            context,
            name,
            ranges,
            parent_segment_info,
            symbols,
            symbol_vrams,
            user_relocs,
        )
    }
}

impl Section for ExecutableSection {
    fn name(&self) -> Arc<str> {
        self.name.clone()
    }

    fn vram_range(&self) -> &AddressRange<Vram> {
        self.ranges.vram()
    }

    fn parent_segment_info(&self) -> &ParentSegmentInfo {
        &self.parent_segment_info
    }

    fn section_type(&self) -> SectionType {
        SECTION_TYPE
    }

    fn symbol_list(&self) -> &[impl Symbol] {
        &self.symbols
    }

    fn symbols_vrams(&self) -> &UnorderedSet<Vram> {
        &self.symbol_vrams
    }
}
impl RomSection for ExecutableSection {
    fn rom_vram_range(&self) -> &RomVramRange {
        &self.ranges
    }
}
impl SectionPreprocessed for ExecutableSection {
    fn symbol_list(&self) -> &[impl SymbolPreprocessed] {
        &self.symbols
    }
}
impl RomSectionPreprocessed for ExecutableSection {}

impl hash::Hash for ExecutableSection {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.parent_segment_info.hash(state);
        self.ranges.hash(state);
    }
}
impl PartialEq for ExecutableSection {
    fn eq(&self, other: &Self) -> bool {
        self.parent_segment_info == other.parent_segment_info && self.ranges == other.ranges
    }
}
impl PartialOrd for ExecutableSection {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        // Compare segment info first, so symbols get sorted by segment
        match self
            .parent_segment_info
            .partial_cmp(&other.parent_segment_info)
        {
            Some(core::cmp::Ordering::Equal) => {}
            ord => return ord,
        }
        self.ranges.partial_cmp(&other.ranges)
    }
}

fn instrs_from_bytes(
    settings: &ExecutableSectionSettings,
    endian: Endian,
    raw_bytes: &[u8],
    mut vram: Vram,
) -> Vec<Instruction> {
    let mut instrs = Vec::new();

    for b in raw_bytes.chunks_exact(BYTES_PER_INSTR as usize) {
        let word = endian.word_from_bytes(b);

        instrs.push(Instruction::new(word, vram, settings.instruction_flags));
        vram += VramOffset::new(BYTES_PER_INSTR as i32);
    }

    instrs
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct FarthestBranch {
    farthest: VramOffset,
    /// Mainly just for debugging
    last_setter_rom: Rom,
}

impl FarthestBranch {
    fn new(current_rom: Rom) -> Self {
        Self {
            farthest: VramOffset::new(0),
            last_setter_rom: current_rom,
        }
    }

    fn farthest(&self) -> VramOffset {
        self.farthest
    }

    fn update(&mut self, current_rom: Rom, new_farthest: VramOffset) {
        if new_farthest > self.farthest {
            self.farthest = new_farthest;
            self.last_setter_rom = current_rom;
        }
    }

    fn decrease(&mut self) {
        self.farthest = VramOffset::new(self.farthest.inner() - BYTES_PER_INSTR as i32);
    }
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[must_use]
struct GatheredFunctionBoundary {
    start: usize,
    end: usize,
    auto_pad_by: Option<Vram>,
    data_type: Option<SymbolType>,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[must_use]
struct TempFunctionBoundary {
    start: usize,
    auto_pad_by: Option<Vram>,
    data_type: Option<SymbolType>,
}

impl TempFunctionBoundary {
    const fn new_func(start: usize, auto_pad_by: Option<Vram>) -> Self {
        Self {
            start,
            auto_pad_by,
            data_type: None,
        }
    }

    const fn new_data(start: usize, auto_pad_by: Option<Vram>, data_type: SymbolType) -> Self {
        Self {
            start,
            auto_pad_by,
            data_type: Some(data_type),
        }
    }

    const fn finish(self, end: usize) -> GatheredFunctionBoundary {
        let Self {
            start,
            auto_pad_by,
            data_type,
        } = self;

        GatheredFunctionBoundary {
            start,
            end,
            auto_pad_by,
            data_type,
        }
    }
}

fn find_functions(
    global_config: &GlobalConfig,
    settings: &ExecutableSectionSettings,
    owned_segment: &SegmentMetadata,
    section_ranges: RomVramRange,
    instrs: &[Instruction],
) -> Result<Vec<GatheredFunctionBoundary>, SectionCreationError> {
    let boundaries = find_functions_impl(
        global_config,
        settings,
        owned_segment,
        section_ranges,
        instrs,
    )?;

    let rom = section_ranges.rom().start();
    let vram = section_ranges.vram().start();

    let mut ends: Vec<usize> = boundaries.iter().skip(1).map(|x| x.start).collect();
    ends.push(instrs.len());
    debug_assert!(boundaries.len() == ends.len());

    Ok(boundaries
        .into_iter()
        .zip(ends)
        .map(|(boundary, end)| {
            debug_assert!(
                boundary.start < end,
                "{:?} {} {} {}",
                rom,
                vram,
                boundary.start,
                end
            );

            boundary.finish(end)
        })
        .collect())
}

fn find_functions_impl(
    global_config: &GlobalConfig,
    settings: &ExecutableSectionSettings,
    owned_segment: &SegmentMetadata,
    section_ranges: RomVramRange,
    instrs: &[Instruction],
) -> Result<Vec<TempFunctionBoundary>, SectionCreationError> {
    let mut starts_data = vec![TempFunctionBoundary::new_func(0, None)];

    let mut function_ended = None;
    let mut farthest_branch = FarthestBranch::new(section_ranges.rom().start());

    let mut index: usize = 0;

    let (mut current_function_start, mut current_function_ref) = match find_current_start(
        owned_segment,
        section_ranges.vram().start(),
        instrs,
        &mut index,
        None,
        &mut starts_data,
        true,
    )? {
        NextStartResult::FunctionStart(current_function_start, current_function_ref) => {
            (current_function_start, current_function_ref)
        }
        NextStartResult::SectionEnded => return Ok(starts_data),
    };

    let mut regs_tracker = RegisterTracker::new(
        settings.instruction_flags().abi(),
        Some(section_ranges.vram().start() + Size::new(index as u32 * BYTES_PER_INSTR)),
        global_config.gp_config().copied(),
        global_config.endian(),
    );
    let mut prev_instr = None;

    let global_offset_table = owned_segment.global_offset_table();

    while index < instrs.len() {
        if let Some(function_ended) = function_ended {
            match function_ended {
                FunctionEndedState::WithDelaySlot => {
                    index += 1;
                }
                FunctionEndedState::ByException => {}
            }

            (current_function_start, current_function_ref) = match find_current_start(
                owned_segment,
                section_ranges.vram().start(),
                instrs,
                &mut index,
                current_function_ref.as_ref(),
                &mut starts_data,
                false,
            )? {
                NextStartResult::FunctionStart(current_function_start, current_function_ref) => {
                    (current_function_start, current_function_ref)
                }
                NextStartResult::SectionEnded => return Ok(starts_data),
            };

            prev_instr = None;
            regs_tracker.soft_reset(
                instrs[index].abi(),
                Some(section_ranges.vram().start() + Size::new(index as u32 * BYTES_PER_INSTR)),
            );
            farthest_branch = FarthestBranch::new(
                section_ranges.rom().start() + Size::new(index as u32 * BYTES_PER_INSTR),
            );
        }

        let instr = &instrs[index];

        let current_rom = section_ranges.rom().start() + Size::new(index as u32 * BYTES_PER_INSTR);
        let instr_processed_result =
            regs_tracker.process_instruction(instr, current_rom, global_offset_table);

        if instr.opcode().is_branch()
            || instr.is_unconditional_branch()
            || instr.is_jumptable_jump()
        {
            find_functions_branch_checker(
                owned_segment,
                &instr_processed_result,
                section_ranges,
                index * BYTES_PER_INSTR as usize,
                instr,
                &mut farthest_branch,
            );
        }

        function_ended = find_functions_check_function_ended(
            owned_segment,
            &instr_processed_result,
            settings,
            index,
            instrs,
            section_ranges.rom().start() + Size::new(index as u32 * BYTES_PER_INSTR),
            section_ranges.vram().start() + Size::new(index as u32 * BYTES_PER_INSTR),
            current_function_ref,
            &farthest_branch,
            current_function_start,
        );

        regs_tracker.clear_afterwards(prev_instr, None);

        if instr.is_valid() {
            prev_instr = Some(instr);
        } else {
            prev_instr = None;
        }

        index += 1;
        farthest_branch.decrease();
    }

    Ok(starts_data)
}

fn find_functions_branch_checker(
    owned_segment: &SegmentMetadata,
    instr_processed_result: &InstructionOperation,
    section_ranges: RomVramRange,
    local_offset: usize,
    instr: &Instruction,
    farthest_branch: &mut FarthestBranch,
) {
    if instr.opcode().is_jump_with_address() {
        // If this instruction is a jump and it is jumping to a function then
        // don't treat it as a branch, it is probably actually being used as
        // a jump

        // TODO
        if let Some(target_vram) = instr.get_instr_index_as_vram() {
            if let Some(aux_ref) =
                owned_segment.find_reference(target_vram, FindSettings::new(false))
            {
                if aux_ref.is_trustable_function() {
                    return;
                }
            }
        }
    }

    match instr_processed_result {
        InstructionOperation::Branch { target_vram } => {
            let branch_offset = *target_vram - instr.vram();
            let current_rom = section_ranges.rom().start() + Size::new(local_offset as u32);

            farthest_branch.update(current_rom, branch_offset);
        }
        InstructionOperation::JumptableJump { jumptable_vram, .. } => {
            // Check jumptables
            if let Some(jumptable_ref) =
                owned_segment.find_reference(*jumptable_vram, FindSettings::new(false))
            {
                for jtbl_label_vram in jumptable_ref.table_labels() {
                    let branch_offset = *jtbl_label_vram - instr.vram();
                    let current_rom = section_ranges.rom().start() + Size::new(local_offset as u32);

                    farthest_branch.update(current_rom, branch_offset);
                }
            }
        }

        InstructionOperation::Link { .. }
        | InstructionOperation::TailCall { .. }
        | InstructionOperation::ReturnJump
        | InstructionOperation::Hi { .. }
        | InstructionOperation::PairedAddress { .. }
        | InstructionOperation::GpSet { .. }
        | InstructionOperation::DereferencedRawAddress { .. }
        | InstructionOperation::DanglingLo { .. }
        | InstructionOperation::Constant { .. }
        | InstructionOperation::UnpairedConstant { .. }
        | InstructionOperation::RegisterOperation { .. }
        | InstructionOperation::UnhandledOpcode { .. }
        | InstructionOperation::InvalidInstr { .. } => {}
    }
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
enum FunctionEndedState {
    WithDelaySlot,
    ByException,
}

#[expect(clippy::too_many_arguments)]
fn find_functions_check_function_ended(
    owned_segment: &SegmentMetadata,
    instr_processed_result: &InstructionOperation,
    settings: &ExecutableSectionSettings,
    index: usize,
    instrs: &[Instruction],
    current_rom: Rom,
    current_vram: Vram,
    current_function_ref: Option<ReferenceWrapper>,
    farthest_branch: &FarthestBranch,
    current_function_start: usize,
) -> Option<FunctionEndedState> {
    let instr = &instrs[index];
    let opcode = instr.opcode();

    if let Some(reference) = current_function_ref {
        if let Some(user_declared_size) = reference.user_declared_size() {
            // If the function has a size set by the user then only use that and ignore the other ways of determining function-ends
            return if (index + 2) * BYTES_PER_INSTR as usize
                == current_function_start + user_declared_size.inner() as usize
            {
                Some(FunctionEndedState::WithDelaySlot)
            } else {
                None
            };
        }
    }

    if let Some(reference) = owned_segment.find_reference(
        current_vram + VramOffset::new(2 * BYTES_PER_INSTR as i32),
        FindSettings::new(false),
    ) {
        // If there's another function after this then the current function has ended
        if reference.is_trustable_function() {
            if let Some(sym_rom) = reference.rom() {
                if current_rom + Size::new(2 * BYTES_PER_INSTR) == sym_rom {
                    return Some(FunctionEndedState::WithDelaySlot);
                }
            } else {
                return Some(FunctionEndedState::WithDelaySlot);
            }
        }
    }

    if farthest_branch.farthest().is_positive() {
        // We still have a branch that branched even farther than where we currently are, so we
        // must still be inside the same function.
        return None;
    }

    if opcode.causes_unconditional_exception() && !opcode.causes_returnable_exception() {
        return Some(FunctionEndedState::ByException);
    }

    if settings.negative_branch_as_end() && instr.is_unconditional_branch() {
        if let Some(target_vram) = instr.get_branch_vram_generic() {
            if target_vram < current_vram {
                return Some(FunctionEndedState::WithDelaySlot);
            }
        }
    }

    // if !opcode.is_jump() {
    //     return None;
    // }

    match instr_processed_result {
        InstructionOperation::Link { .. } => {
            debug_assert!(opcode.does_link(), "{current_rom:?} {opcode:?}");
            None
        }
        InstructionOperation::TailCall { info } => match info {
            InstrOpTailCall::MaybeDirectTailCall { .. } => {
                debug_assert!(opcode.is_jump_with_address(), "{current_rom:?} {opcode:?}");
                debug_assert!(
                    !settings.instruction_flags.j_as_branch(),
                    "{current_rom:?} {opcode:?}"
                );

                Some(FunctionEndedState::WithDelaySlot)
            }
            InstrOpTailCall::RawRegisterTailCall { .. }
            | InstrOpTailCall::DereferencedRegisterTailCall { .. } => {
                Some(FunctionEndedState::WithDelaySlot)
            }
            InstrOpTailCall::UnknownRegisterJump { .. } => Some(FunctionEndedState::WithDelaySlot),
        },

        InstructionOperation::JumptableJump { .. } => {
            debug_assert!(instr.is_jumptable_jump(), "{current_rom:?} {opcode:?}");
            None
        }

        InstructionOperation::ReturnJump => {
            debug_assert!(instr.is_return(), "{current_rom:?} {opcode:?}");

            // Found a jr $ra and there are no branches outside of this function
            if settings.detect_redundant_end() {
                // The IDO compiler may generate a a redundant and unused `jr $ra; nop` at the end of the functions the
                // flags `-g`, `-g1` or `-g2` are used.
                // In normal conditions this would be detected as its own separate empty function, which may cause
                // issues on a decompilation project.
                // In other words, we try to detect the following pattern:
                // ```
                // jr         $ra
                //  nop
                // jr         $ra
                //  nop
                // ```
                // where the last two instructions do not belong to being an already existing function (either
                // referenced by code or user-declared).
                let mut redundant_pattern_detected = false;
                if index + 3 < instrs.len() {
                    let instr1 = instrs[index + 1];
                    let instr2 = instrs[index + 2];
                    let instr3 = instrs[index + 3];
                    // We already checked if there is a function in the previous block, so we don't need to check it again.
                    if instr1.is_nop() && instr2.is_return() && instr3.is_nop() {
                        redundant_pattern_detected = true;
                    }
                }
                if !redundant_pattern_detected {
                    Some(FunctionEndedState::WithDelaySlot)
                } else {
                    None
                }
            } else {
                Some(FunctionEndedState::WithDelaySlot)
            }
        }

        InstructionOperation::Branch { .. }
        | InstructionOperation::Hi { .. }
        | InstructionOperation::PairedAddress { .. }
        | InstructionOperation::GpSet { .. }
        | InstructionOperation::DereferencedRawAddress { .. }
        | InstructionOperation::DanglingLo { .. }
        | InstructionOperation::Constant { .. }
        | InstructionOperation::UnpairedConstant { .. }
        | InstructionOperation::RegisterOperation { .. }
        | InstructionOperation::UnhandledOpcode { .. }
        | InstructionOperation::InvalidInstr {} => None,
    }
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd)]
#[must_use]
enum NextStartResult<'segment> {
    FunctionStart(usize, Option<ReferenceWrapper<'segment, 'segment>>),
    SectionEnded,
}

fn find_current_start<'segment>(
    owned_segment: &'segment SegmentMetadata,
    section_vram: Vram,
    instrs: &[Instruction],
    index: &mut usize,
    prev_function_ref: Option<&ReferenceWrapper<'segment, 'segment>>,
    starts_data: &mut Vec<TempFunctionBoundary>,
    is_first: bool,
) -> Result<NextStartResult<'segment>, SectionCreationError> {
    let current_function_ref = if is_first && !instrs[0].is_nop() {
        owned_segment.find_reference(
            section_vram + Size::new(*index as u32 * BYTES_PER_INSTR),
            FindSettings::new(false),
        )
    } else {
        find_current_start_advance_nops(owned_segment, section_vram, instrs, index)
    };

    // We ran out of functions
    if *index >= instrs.len() {
        Ok(NextStartResult::SectionEnded)
    } else {
        // Add this function to our vec of functions

        let auto_pad_by = prev_function_ref
            .filter(|x| x.user_declared_size().is_some())
            .map(|x| x.vram());

        // Disassemble as data instead of as a function if the user set a non-function type and a size.
        let boundary = if let Some(data_type) = current_function_ref.and_then(|x| {
            x.user_declared_type()
                .filter(|t| *t != SymbolType::Function && x.user_declared_size().is_some())
        }) {
            TempFunctionBoundary::new_data(*index, auto_pad_by, data_type)
        } else {
            TempFunctionBoundary::new_func(*index, auto_pad_by)
        };

        if is_first {
            if *index == 0 {
                starts_data[0] = boundary;
            } else {
                starts_data.push(boundary);
            }
        } else if !owned_segment
            .is_vram_ignored(section_vram + Size::new(*index as u32 * BYTES_PER_INSTR))
        {
            starts_data.push(boundary);
        }

        // Decide if we need to keep checking
        if let Some((func_sym, user_size)) =
            current_function_ref.and_then(|x| x.user_declared_size().map(|y| (x, y)))
        {
            // If the user told us about the size of this function then we should blindly trust it (as long as it is valid)

            // This whole section assumes every symbol is a multiple of a word. We shouldn't let the user break that assumption.
            if user_size.inner() % BYTES_PER_INSTR != 0 {
                return Err(BadUserSymbolSizeError::new(
                    "".into(),
                    func_sym.vram(),
                    user_size,
                    SECTION_TYPE,
                    BYTES_PER_INSTR,
                )
                .into());
            }

            *index += (user_size.inner() / BYTES_PER_INSTR) as usize;
            find_current_start(
                owned_segment,
                section_vram,
                instrs,
                index,
                current_function_ref.as_ref(),
                starts_data,
                false,
            )
        } else if owned_segment
            .find_reference(
                section_vram + Size::new((*index as u32 + 1) * BYTES_PER_INSTR),
                FindSettings::new(false),
            )
            .is_some()
        {
            // Check for 1 instruction functions
            *index += 1;
            find_current_start(
                owned_segment,
                section_vram,
                instrs,
                index,
                current_function_ref.as_ref(),
                starts_data,
                false,
            )
        } else {
            Ok(NextStartResult::FunctionStart(
                *index * BYTES_PER_INSTR as usize,
                current_function_ref,
            ))
        }
    }
}

fn find_current_start_advance_nops<'segment>(
    owned_segment: &'segment SegmentMetadata,
    section_vram: Vram,
    instrs: &[Instruction],
    index: &mut usize,
) -> Option<ReferenceWrapper<'segment, 'segment>> {
    let mut current_function_ref = None;

    // Loop over until we find a instruction that isn't a nop or a referenced function
    while *index < instrs.len() {
        current_function_ref = owned_segment.find_reference(
            section_vram + Size::new(*index as u32 * BYTES_PER_INSTR),
            FindSettings::new(false),
        );

        if current_function_ref.is_some() {
            break;
        }

        let instr = &instrs[*index];
        if !instr.is_nop() {
            break;
        }

        *index += 1;
    }

    current_function_ref
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "pyo3", pyclass(module = "spimdisasm"))]
pub struct ExecutableSectionSettings {
    compiler: Option<Compiler>,
    instruction_flags: InstructionFlags,
    is_handwritten: bool,

    /// Tries to detect one or more redundants and unreferenced function ends and merge them to the previous function.
    /// This option is ignored if the compiler is not set to IDO.
    detect_redundant_end: bool,

    /// Allow considering a negative unconditional branch as a possible function end.
    /// It is disabled by default since many compilers may emit the function's epilogue even if it
    /// is unreachable.
    negative_branch_as_end: bool,
}

impl ExecutableSectionSettings {
    pub fn new(compiler: Option<Compiler>, instruction_flags: InstructionFlags) -> Self {
        Self {
            compiler,
            instruction_flags,
            is_handwritten: false,
            detect_redundant_end: false,
            negative_branch_as_end: false,
        }
    }

    pub fn compiler(&self) -> Option<Compiler> {
        self.compiler
    }
    pub fn instruction_flags(&self) -> InstructionFlags {
        self.instruction_flags
    }

    pub fn is_handwritten(&self) -> bool {
        self.is_handwritten
    }
    pub fn set_is_handwritten(&mut self, is_handwritten: bool) {
        self.is_handwritten = is_handwritten;
    }
    pub fn with_is_handwritten(self, is_handwritten: bool) -> Self {
        Self {
            is_handwritten,
            ..self
        }
    }

    pub fn detect_redundant_end(&self) -> bool {
        // TODO: move hardcoded IDO check to a Compiler function.
        self.compiler
            .is_some_and(|x| x == Compiler::IDO && self.detect_redundant_end)
    }
    /// Tries to detect one or more redundants and unreferenced function ends and merge them to the previous function.
    /// This option is ignored if the compiler is not set to IDO.
    pub fn set_detect_redundant_end(&mut self, detect_redundant_end: bool) {
        self.detect_redundant_end = detect_redundant_end;
    }
    pub fn with_detect_redundant_end(self, detect_redundant_end: bool) -> Self {
        Self {
            detect_redundant_end,
            ..self
        }
    }

    pub fn negative_branch_as_end(&self) -> bool {
        self.negative_branch_as_end
    }
    pub fn set_negative_branch_as_end(&mut self, negative_branch_as_end: bool) {
        self.negative_branch_as_end = negative_branch_as_end;
    }
    /// Allow considering a negative unconditional branch as a possible function end.
    pub fn with_negative_branch_as_end(self, negative_branch_as_end: bool) -> Self {
        Self {
            negative_branch_as_end,
            ..self
        }
    }
}

#[cfg(feature = "pyo3")]
pub(crate) mod python_bindings {
    use super::*;

    #[pymethods]
    impl ExecutableSectionSettings {
        #[new]
        #[pyo3(signature = (compiler, instruction_flags))]
        pub fn py_new(compiler: Option<Compiler>, instruction_flags: InstructionFlags) -> Self {
            Self::new(compiler, instruction_flags)
        }

        #[pyo3(name = "set_is_handwritten")]
        pub fn py_set_is_handwritten(&mut self, is_handwritten: bool) {
            self.set_is_handwritten(is_handwritten)
        }
        #[pyo3(name = "set_detect_redundant_end")]
        pub fn py_set_detect_redundant_end(&mut self, detect_redundant_end: bool) {
            self.set_detect_redundant_end(detect_redundant_end)
        }
    }
}