summaryrefslogtreecommitdiff
path: root/src/trans/codegen.cpp
blob: 4ec48f8171e087886fbe216e8b70ce85b83b477f (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
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * trans/codegen.cpp
 * - Wrapper for translation
 */
#include "main_bindings.hpp"
#include "trans_list.hpp"
#include <hir/hir.hpp>

#include "codegen.hpp"
#include "monomorphise.hpp"

void Trans_Codegen(const ::std::string& outfile, const ::HIR::Crate& crate, const TransList& list)
{
    auto codegen = Trans_Codegen_GetGeneratorC(outfile);
    
    // 1. Emit structure/type definitions.
    // - Emit in the order they're needed.
    
    // 2. Emit function prototypes
    for(const auto& fcn : list.m_functions)
    {
        DEBUG("FUNCTION " << fcn.first);
    }
    // 3. Emit statics
    for(const auto& ent : list.m_statics)
    {
        DEBUG("STATIC " << ent.first);
        
        if( ent.second->ptr )
        {
            codegen->emit_static_ext(ent.first);
        }
        else
        {
            codegen->emit_static_local(ent.first, *ent.second->ptr, ent.second->pp);
        }
    }
    
    
    // 4. Emit function code
    for(const auto& ent : list.m_functions)
    {
        if( ent.second->ptr && ent.second->ptr->m_code.m_mir ) {
            DEBUG("FUNCTION CODE " << ent.first);
            if( ent.second->pp.has_types() ) {
                auto mir = Trans_Monomorphise(crate, ent.second->pp, ent.second->ptr->m_code.m_mir);
                codegen->emit_function_code(ent.first, *ent.second->ptr, ent.second->pp,  mir);
            }
            else {
                codegen->emit_function_code(ent.first, *ent.second->ptr, ent.second->pp,  ent.second->ptr->m_code.m_mir);
            }
        }
    }
    
    codegen->finalise();
}