summaryrefslogtreecommitdiff
path: root/tools/standalone_miri/main.cpp
blob: e3a8d22eda942bbbc9c4bb806b8d7063e62af148 (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
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
/*
 * mrustc Standalone MIRI
 * - by John Hodge (Mutabah)
 *
 * main.cpp
 * - Program entrypoint
 */
#include <iostream>
#include "module_tree.hpp"
#include "value.hpp"
#include <algorithm>
#include <iomanip>
#include "debug.hpp"
#include "miri.hpp"


struct ProgramOptions
{
    ::std::string   infile;
    //TODO: Architecture file
    //::std::string   archname;
    //TODO: Loadable FFI descriptions
    //::std::vector<const char*>  ffi_api_files;

    // Output logfile
    ::std::string   logfile;
    // Arguments for the program
    ::std::vector<const char*>  args;

    int parse(int argc, const char* argv[]);
    void show_help(const char* prog) const;
};

int main(int argc, const char* argv[])
{
    ProgramOptions  opts;

    if( opts.parse(argc, argv) )
    {
        return 1;
    }

    // Configure logging
    if( opts.logfile != "" )
    {
        DebugSink::set_output_file(opts.logfile);
    }

    // Load HIR tree
    auto tree = ModuleTree {};
    tree.load_file(opts.infile);

    // Create argc/argv based on input arguments
    auto argv_alloc = Allocation::new_alloc((1 + opts.args.size()) * POINTER_SIZE);
    argv_alloc->write_usize(0 * POINTER_SIZE, 0);
    argv_alloc->relocations.push_back({ 0 * POINTER_SIZE, RelocationPtr::new_ffi(FFIPointer { "", (void*)(opts.infile.c_str()), opts.infile.size() + 1 }) });
    for(size_t i = 0; i < opts.args.size(); i ++)
    {
        argv_alloc->write_usize((1 + i) * POINTER_SIZE, 0);
        argv_alloc->relocations.push_back({ (1 + i) * POINTER_SIZE, RelocationPtr::new_ffi({ "", (void*)(opts.args[0]), ::std::strlen(opts.args[0]) + 1 }) });
    }
    
    // Construct argc/argv values
    auto val_argc = Value::new_isize(1 + opts.args.size());
    ::HIR::TypeRef  argv_ty { RawType::I8 };
    argv_ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Pointer, 0 });
    argv_ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Pointer, 0 });
    auto val_argv = Value::new_pointer(argv_ty, 0, RelocationPtr::new_alloc(argv_alloc));

    // Catch various exceptions from the interpreter
    try
    {
        InterpreterThread   root_thread(tree);
        
        ::std::vector<Value>    args;
        args.push_back(::std::move(val_argc));
        args.push_back(::std::move(val_argv));
        Value   rv;
        root_thread.start(tree.find_lang_item("start"), ::std::move(args));
        while( !root_thread.step_one(rv) )
        {
        }

        LOG_NOTICE("Return code: " << rv);
    }
    catch(const DebugExceptionTodo& /*e*/)
    {
        ::std::cerr << "TODO Hit" << ::std::endl;
        if(opts.logfile != "")
        {
            ::std::cerr << "- See '" << opts.logfile << "' for details" << ::std::endl;
        }
        return 1;
    }
    catch(const DebugExceptionError& /*e*/)
    {
        ::std::cerr << "Error encountered" << ::std::endl;
        if(opts.logfile != "")
        {
            ::std::cerr << "- See '" << opts.logfile << "' for details" << ::std::endl;
        }
        return 1;
    }

    return 0;
}

int ProgramOptions::parse(int argc, const char* argv[])
{
    bool all_free = false;
    // TODO: use getopt? POSIX only
    for(int argidx = 1; argidx < argc; argidx ++)
    {
        const char* arg = argv[argidx]; 
        if( arg[0] != '-' || all_free )
        {
            // Free arguments
            // - First is the input file
            if( this->infile == "" )
            {
                this->infile = arg;
            }
            else
            {
                // Any subsequent arguments are passed to the taget
                this->args.push_back(arg);
            }
        }
        else if( arg[1] != '-' )
        {
            // Short arguments
            if( arg[2] != '\0' ) {
                // Error?
                ::std::cerr << "Unexpected option " << arg << ::std::endl;
                return 1;
            }
            switch(arg[1])
            {
            case 'h':
                this->show_help(argv[0]);
                exit(0);
            default:
                ::std::cerr << "Unexpected option -" << arg[1] << ::std::endl;
                return 1;
            }
        }
        else if( arg[2] != '\0' )
        {
            // Long
            if( ::std::strcmp(arg, "--help") == 0 ) {
                this->show_help(argv[0]);
                exit(0);
            }
            else if( ::std::strcmp(arg, "--logfile") == 0 ) {
                if( argidx + 1 == argc ) {
                    ::std::cerr << "Option " << arg << " requires an argument" << ::std::endl;
                    return 1;
                }
                const char* opt = argv[++argidx];
                this->logfile = opt;
            }
            //else if( ::std::strcmp(arg, "--api") == 0 ) {
            //}
            else {
                ::std::cerr << "Unexpected option " << arg << ::std::endl;
                return 1;
            }
        }
        else
        {
            all_free = true;
        }
    }
    return 0;
}

void ProgramOptions::show_help(const char* prog) const
{
    ::std::cout << "USAGE: " << prog << " <infile> <... args>" << ::std::endl;
}