111821SChristian.Menard@tu-dresden.de/*
211821SChristian.Menard@tu-dresden.de * Copyright (c) 2016, Dresden University of Technology (TU Dresden)
311821SChristian.Menard@tu-dresden.de * All rights reserved.
411821SChristian.Menard@tu-dresden.de *
511821SChristian.Menard@tu-dresden.de * Redistribution and use in source and binary forms, with or without
611821SChristian.Menard@tu-dresden.de * modification, are permitted provided that the following conditions are
711821SChristian.Menard@tu-dresden.de * met:
811821SChristian.Menard@tu-dresden.de *
911821SChristian.Menard@tu-dresden.de * 1. Redistributions of source code must retain the above copyright notice,
1011821SChristian.Menard@tu-dresden.de *    this list of conditions and the following disclaimer.
1111821SChristian.Menard@tu-dresden.de *
1211821SChristian.Menard@tu-dresden.de * 2. Redistributions in binary form must reproduce the above copyright
1311821SChristian.Menard@tu-dresden.de *    notice, this list of conditions and the following disclaimer in the
1411821SChristian.Menard@tu-dresden.de *    documentation and/or other materials provided with the distribution.
1511821SChristian.Menard@tu-dresden.de *
1611821SChristian.Menard@tu-dresden.de * 3. Neither the name of the copyright holder nor the names of its
1711821SChristian.Menard@tu-dresden.de *    contributors may be used to endorse or promote products derived from
1811821SChristian.Menard@tu-dresden.de *    this software without specific prior written permission.
1911821SChristian.Menard@tu-dresden.de *
2011821SChristian.Menard@tu-dresden.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2111821SChristian.Menard@tu-dresden.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2211821SChristian.Menard@tu-dresden.de * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2311821SChristian.Menard@tu-dresden.de * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
2411821SChristian.Menard@tu-dresden.de * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2511821SChristian.Menard@tu-dresden.de * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2611821SChristian.Menard@tu-dresden.de * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2711821SChristian.Menard@tu-dresden.de * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2811821SChristian.Menard@tu-dresden.de * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2911821SChristian.Menard@tu-dresden.de * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3011821SChristian.Menard@tu-dresden.de * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3111821SChristian.Menard@tu-dresden.de *
3211821SChristian.Menard@tu-dresden.de * Authors: Christian Menard
3311821SChristian.Menard@tu-dresden.de */
3411821SChristian.Menard@tu-dresden.de
3511821SChristian.Menard@tu-dresden.de#include <iostream>
3611821SChristian.Menard@tu-dresden.de#include <sstream>
3711821SChristian.Menard@tu-dresden.de
3811821SChristian.Menard@tu-dresden.de#include "cli_parser.hh"
3911821SChristian.Menard@tu-dresden.de#include "sim/cxx_manager.hh"
4011821SChristian.Menard@tu-dresden.de
4111821SChristian.Menard@tu-dresden.devoid
4211821SChristian.Menard@tu-dresden.deCliParser::usage(const std::string& prog_name)
4311821SChristian.Menard@tu-dresden.de{
4411821SChristian.Menard@tu-dresden.de    std::cerr
4511821SChristian.Menard@tu-dresden.de      << "Usage: " << prog_name
4611821SChristian.Menard@tu-dresden.de      << (" <config_file.ini> [ <option> ]\n\n"
4711821SChristian.Menard@tu-dresden.de          "OPTIONS:\n"
4811821SChristian.Menard@tu-dresden.de
4911821SChristian.Menard@tu-dresden.de          "    -o <offset>                  -- set memory offset\n"
5011821SChristian.Menard@tu-dresden.de          "    -d <flag>                    -- set a gem5 debug flag\n"
5111821SChristian.Menard@tu-dresden.de          "                                    (-<flag> clears a flag)\n"
5211821SChristian.Menard@tu-dresden.de          "    -v                           -- verbose output\n"
5311821SChristian.Menard@tu-dresden.de          "    -e <ticks>                   -- end of simulation after a \n"
5411821SChristian.Menard@tu-dresden.de          "                                    given number of ticks\n"
5511821SChristian.Menard@tu-dresden.de          "\n");
5611821SChristian.Menard@tu-dresden.de    std::exit(EXIT_FAILURE);
5711821SChristian.Menard@tu-dresden.de}
5811821SChristian.Menard@tu-dresden.de
5911821SChristian.Menard@tu-dresden.devoid
6011821SChristian.Menard@tu-dresden.deCliParser::parse(int argc, char** argv)
6111821SChristian.Menard@tu-dresden.de{
6211821SChristian.Menard@tu-dresden.de    std::string prog_name(argv[0]);
6311821SChristian.Menard@tu-dresden.de
6411821SChristian.Menard@tu-dresden.de    unsigned int arg_ptr = 1;
6511821SChristian.Menard@tu-dresden.de
6611821SChristian.Menard@tu-dresden.de    if (argc == 1) {
6711821SChristian.Menard@tu-dresden.de        usage(prog_name);
6811821SChristian.Menard@tu-dresden.de    }
6911821SChristian.Menard@tu-dresden.de
7011821SChristian.Menard@tu-dresden.de    configFile = std::string(argv[arg_ptr]);
7111821SChristian.Menard@tu-dresden.de    arg_ptr++;
7211821SChristian.Menard@tu-dresden.de
7311821SChristian.Menard@tu-dresden.de    // default values
7411821SChristian.Menard@tu-dresden.de    verboseFlag = false;
7511821SChristian.Menard@tu-dresden.de    simulationEnd = 0;
7611821SChristian.Menard@tu-dresden.de    memoryOffset = 0;
7711821SChristian.Menard@tu-dresden.de
7811821SChristian.Menard@tu-dresden.de    try {
7911821SChristian.Menard@tu-dresden.de        while (arg_ptr < argc) {
8011821SChristian.Menard@tu-dresden.de            std::string option(argv[arg_ptr]);
8111821SChristian.Menard@tu-dresden.de            arg_ptr++;
8211821SChristian.Menard@tu-dresden.de            unsigned num_args = argc - arg_ptr;
8311821SChristian.Menard@tu-dresden.de
8411821SChristian.Menard@tu-dresden.de            if (option == "-d") {
8511821SChristian.Menard@tu-dresden.de                if (num_args < 1) {
8611821SChristian.Menard@tu-dresden.de                    usage(prog_name);
8711821SChristian.Menard@tu-dresden.de                }
8811821SChristian.Menard@tu-dresden.de                std::string flag(argv[arg_ptr]);
8911821SChristian.Menard@tu-dresden.de                arg_ptr++;
9011821SChristian.Menard@tu-dresden.de                debugFlags.push_back(flag);
9111821SChristian.Menard@tu-dresden.de            } else if (option == "-e") {
9211821SChristian.Menard@tu-dresden.de                if (num_args < 1) {
9311821SChristian.Menard@tu-dresden.de                    usage(prog_name);
9411821SChristian.Menard@tu-dresden.de                }
9511821SChristian.Menard@tu-dresden.de                std::istringstream(argv[arg_ptr]) >> simulationEnd;
9611821SChristian.Menard@tu-dresden.de                arg_ptr++;
9711821SChristian.Menard@tu-dresden.de            } else if (option == "-v") {
9811821SChristian.Menard@tu-dresden.de                verboseFlag = true;
9911821SChristian.Menard@tu-dresden.de            } else if (option == "-o") {
10011821SChristian.Menard@tu-dresden.de                if (num_args < 1) {
10111821SChristian.Menard@tu-dresden.de                    usage(prog_name);
10211821SChristian.Menard@tu-dresden.de                }
10311821SChristian.Menard@tu-dresden.de                std::istringstream(argv[arg_ptr]) >> memoryOffset;
10411821SChristian.Menard@tu-dresden.de                arg_ptr++;
10511821SChristian.Menard@tu-dresden.de                /* code */
10611821SChristian.Menard@tu-dresden.de            } else {
10711821SChristian.Menard@tu-dresden.de                usage(prog_name);
10811821SChristian.Menard@tu-dresden.de            }
10911821SChristian.Menard@tu-dresden.de        }
11011821SChristian.Menard@tu-dresden.de    } catch (CxxConfigManager::Exception &e) {
11111821SChristian.Menard@tu-dresden.de        std::cerr << e.name << ": " << e.message << "\n";
11211821SChristian.Menard@tu-dresden.de        std::exit(EXIT_FAILURE);
11311821SChristian.Menard@tu-dresden.de    }
11411821SChristian.Menard@tu-dresden.de
11511821SChristian.Menard@tu-dresden.de    parsed = true;
11611821SChristian.Menard@tu-dresden.de}
11711821SChristian.Menard@tu-dresden.de
11811821SChristian.Menard@tu-dresden.destd::string
11911821SChristian.Menard@tu-dresden.deCliParser::getDebugFlags()
12011821SChristian.Menard@tu-dresden.de{
12111821SChristian.Menard@tu-dresden.de    std::stringstream ss;
12211821SChristian.Menard@tu-dresden.de    for (auto& flag : debugFlags) {
12311821SChristian.Menard@tu-dresden.de        ss << flag << ' ';
12411821SChristian.Menard@tu-dresden.de    }
12511821SChristian.Menard@tu-dresden.de    return ss.str();
12611821SChristian.Menard@tu-dresden.de}
127