summaryrefslogtreecommitdiff
path: root/.github/calcrom/BuildAnalyzer.h
blob: 0b13c2cf90eedce58d40d768a08d633df4cc586c (plain)
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
#ifndef CALCROM_BUILDANALYZER_H
#define CALCROM_BUILDANALYZER_H

#include <filesystem>
#include <utility>
#include <vector>
#include <unordered_set>
#include "ElfFile.h"

using namespace std;
using namespace std::filesystem;

extern string default_version;

enum SectionType {
    SECTION_DATA,
    SECTION_TEXT,
    SECTION_MAX,
    SECTION_OTHER = -1
};

enum SourceType {
    SOURCE_C,
    SOURCE_ASM,
    SOURCE_MAX
};

static enum SourceType GetSourceType(const path &fname) {
    string ext = fname.extension();
    return ext == ".s" ? SOURCE_ASM : SOURCE_C;
}

static enum SectionType GetSectionType(const string &shname) {
    if (shname == ".text" || shname == ".init" || shname == ".itcm") {
        return SECTION_TEXT;
    } else if (shname == ".data" || shname == ".rodata" || shname == ".sdata" || shname == ".dtcm") {
        return SECTION_DATA;
    } else {
        return SECTION_OTHER;
    }
}

class BuildAnalyzer {
    bool analyzed = false;
    path basedir;
    path subdir;
    string version = "";
    path srcbase;
    string builddir;
    Elf32File program;

    // Accumulate sizes
    //        src   asm
    // data  _____|_____
    // text       |
    unsigned sizes[SECTION_MAX][SOURCE_MAX] = {{0, 0}, {0, 0}};
    unsigned n_hardcoded = 0;
    unsigned n_relocations = 0;

    void reset();
    void AnalyzeObject(path fname_s);
public:
    BuildAnalyzer(path &_basedir, path &_subdir, string &_version = default_version);
    BuildAnalyzer &operator()();
    friend ostream &operator<<(ostream &strm, BuildAnalyzer &_this);
};

#endif //CALCROM_BUILDANALYZER_H