util: Convert the m5 utility to C++.

This will make it possible to use the googletest unit testing framework,
and will let us use c++ mechanisms to simplify and streamline the code.

Change-Id: I8ab358de47ce6b5c2d601cc0b9f2a694b2037a9b
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/27548
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Gabe Black <gabeblack@google.com>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Maintainer: Gabe Black <gabeblack@google.com>
This commit is contained in:
Gabe Black
2020-04-04 00:55:41 -07:00
parent f5b5090be1
commit 4b0162342b
24 changed files with 129 additions and 105 deletions

View File

@@ -38,6 +38,7 @@ def abspath(d):
return os.path.abspath(str(d))
# Universal settings.
main.Append(CXXFLAGS=[ '-O2' ])
main.Append(CCFLAGS=[ '-O2' ])
main.Append(CPPPATH=[ common_include ])
@@ -45,6 +46,7 @@ main.Append(CPPPATH=[ common_include ])
main['ENV']['PATH'] = os.environ['PATH']
main['CC'] = '${CROSS_COMPILE}gcc'
main['CXX'] = '${CROSS_COMPILE}g++'
main['AS'] = '${CROSS_COMPILE}as'
main['LD'] = '${CROSS_COMPILE}ld'
main['AR'] = '${CROSS_COMPILE}ar'

View File

@@ -28,15 +28,15 @@ import os
Import('*')
# Raw source files.
args = 'args.c'
call_type = 'call_type.c'
commands = 'commands.c'
m5 = 'm5.c'
args = 'args.cc'
call_type = 'call_type.cc'
commands = 'commands.cc'
m5 = 'm5.cc'
m5_mmap = 'm5_mmap.c'
usage = 'usage.c'
usage = 'usage.cc'
jni = 'jni_gem5Op.c'
lua = 'lua_gem5Op.c'
lua = 'lua_gem5Op.cc'
all_call_types = list(env['CALL_TYPE'].values())
call_types = list([ ct for ct in all_call_types if ct.enabled ])
@@ -52,11 +52,11 @@ static_env = env.Clone()
static_env.Append(LINKFLAGS=[ '-no-pie', '-static' ])
for ct in all_call_types:
static_env.Append(CFLAGS='-DENABLE_CT_%s=%d' %
static_env.Append(CXXFLAGS='-DENABLE_CT_%s=%d' %
(ct.name, 1 if ct.enabled else 0))
static_env.Append(CFLAGS='-DDEFAULT_CT_%s=%d' %
static_env.Append(CXXFLAGS='-DDEFAULT_CT_%s=%d' %
(ct.name, 1 if ct.default else 0))
static_env.Append(CFLAGS='-DDEFAULT_CALL_TYPE=%s' % default_call_type.name)
static_env.Append(CXXFLAGS='-DDEFAULT_CALL_TYPE=%s' % default_call_type.name)
#
# The m5 library for use in other C/C++ programs.
@@ -67,7 +67,7 @@ libm5 = static_env.StaticLibrary('out/m5', [ m5_mmap ] + m5ops)
#
# The m5 stand alone command line utility.
#
ct_support = list([ File('%s_call_type.c' % ct.name) for ct in call_types ])
ct_support = list([ File('%s_call_type.cc' % ct.name) for ct in call_types ])
m5_bin = static_env.Program('out/m5',
ct_support + [ args, call_type, commands, m5, m5_mmap, libm5, usage ])

View File

@@ -25,18 +25,21 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <cstring>
#include "addr_call_type.h"
#include "args.h"
#include "addr_call_type.hh"
#include "args.hh"
#include "m5_mmap.h"
extern "C"
{
#define M5OP(name, func) __typeof__(name) M5OP_MERGE_TOKENS(name, _addr);
M5OP_FOREACH
#undef M5OP
}
static DispatchTable addr_dispatch = {
#define M5OP(name, func) .name = &M5OP_MERGE_TOKENS(name, _addr),
#define M5OP(name, func) .name = &::M5OP_MERGE_TOKENS(name, _addr),
M5OP_FOREACH
#undef M5OP
};

View File

@@ -25,14 +25,14 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ADDR_CALL_TYPE_H__
#define __ADDR_CALL_TYPE_H__
#ifndef __ADDR_CALL_TYPE_HH__
#define __ADDR_CALL_TYPE_HH__
#include "args.h"
#include "dispatch_table.h"
#include "args.hh"
#include "dispatch_table.hh"
// Returns 0 if not detected, 1 if detected successfully, and -1 on error.
int addr_call_type_detect(Args *args);
DispatchTable *addr_call_type_init();
#endif // __ADDR_CALL_TYPE_H__
#endif // __ADDR_CALL_TYPE_HH__

View File

@@ -38,11 +38,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <cinttypes>
#include <cstdlib>
#include <cstring>
#include "args.h"
#include "args.hh"
int
parse_int_args(Args *args, uint64_t ints[], int len)

View File

@@ -38,17 +38,17 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ARGS_H__
#define __ARGS_H__
#ifndef __ARGS_HH__
#define __ARGS_HH__
#include <stddef.h>
#include <stdint.h>
#include <cstddef>
#include <cstdint>
typedef struct Args
struct Args
{
int argc;
const char **argv;
} Args;
};
static inline const char *
pop_arg(Args *args)
@@ -62,4 +62,4 @@ pop_arg(Args *args)
int parse_int_args(Args *args, uint64_t ints[], int len);
int pack_arg_into_regs(Args *args, uint64_t regs[], int num_regs);
#endif // __ARGS_H__
#endif // __ARGS_HH__

View File

@@ -27,6 +27,6 @@ Import('*')
env['VARIANT'] = 'arm'
get_variant_opt('CROSS_COMPILE', 'arm-linux-gnueabihf-')
env.Append(CFLAGS='-march=armv7-a')
env.Append(CXXFLAGS='-march=armv7-a')
env['CALL_TYPE']['inst'].impl('m5op.S', default=True)

View File

@@ -25,18 +25,18 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "args.h"
#include "call_type.h"
#include "usage.h"
#include "args.hh"
#include "call_type.hh"
#include "usage.hh"
#if ENABLE_CT_addr
#include "addr_call_type.h"
#include "addr_call_type.hh"
#endif
#if ENABLE_CT_inst
#include "inst_call_type.h"
#include "inst_call_type.hh"
#endif
#if ENABLE_CT_semi
#include "semi_call_type.h"
#include "semi_call_type.hh"
#endif
#define default_call_type_init() \

View File

@@ -25,12 +25,12 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __CALL_TYPE_H__
#define __CALL_TYPE_H__
#ifndef __CALL_TYPE_HH__
#define __CALL_TYPE_HH__
#include "args.h"
#include "dispatch_table.h"
#include "args.hh"
#include "dispatch_table.hh"
DispatchTable *init_call_type(Args *args);
#endif // __CALL_TYPE_H__
#endif // __CALL_TYPE_HH__

View File

@@ -28,15 +28,16 @@
#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "args.h"
#include "commands.h"
#include "usage.h"
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "args.hh"
#include "commands.hh"
#include "usage.hh"
static int
read_file(DispatchTable *dt, int dest_fid)
@@ -213,10 +214,10 @@ do_initparam(DispatchTable *dt, Args *args)
if (!pack_arg_into_regs(args, key_str, 2))
usage();
uint64_t val = (*dt->m5_init_param)(key_str[0], key_str[1]);
printf("%"PRIu64, val);
std::cout << val;
}
struct CommandInfo command_table[] = {
CommandInfo command_table[] = {
{ "addsymbol", do_addsymbol, "<address> <symbol> // Adds a "
"symbol with address \"address\" "
"to gem5's symbol table" },

View File

@@ -26,22 +26,22 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __COMMANDS_H__
#define __COMMANDS_H__
#ifndef __COMMANDS_HH__
#define __COMMANDS_HH__
#include "args.h"
#include "dispatch_table.h"
#include "args.hh"
#include "dispatch_table.hh"
typedef struct CommandInfo
struct CommandInfo
{
// The name of the command.
char *name;
const char *name;
// A function which processes command line arguments and passes them to
// the underlying function through the dispatch table.
void (*func)(DispatchTable *dt, Args *args);
// Help text for this command.
char *usage;
} CommandInfo;
const char *usage;
};
// The commands themselves.
extern CommandInfo command_table[];
@@ -49,4 +49,4 @@ extern CommandInfo command_table[];
// The number of commands.
extern int num_commands;
#endif // __COMMANDS_H__
#endif // __COMMANDS_HH__

View File

@@ -25,8 +25,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __DISPATCH_TABLE_H__
#define __DISPATCH_TABLE_H__
#ifndef __DISPATCH_TABLE_HH__
#define __DISPATCH_TABLE_HH__
#include <gem5/asm/generic/m5ops.h>
#include <gem5/m5ops.h>
@@ -37,11 +37,11 @@
* functions. They can then be passed to a consumer which knows which function
* it wants, but not exactly how/where it's implemented.
*/
typedef struct DispatchTable
struct DispatchTable
{
#define M5OP(name, func) __typeof__(&name) name;
#define M5OP(name, func) __typeof__(&::name) name;
M5OP_FOREACH
#undef M5OP
} DispatchTable;
};
#endif // __DISPATCH_TABLE_H__
#endif // __DISPATCH_TABLE_HH__

View File

@@ -25,12 +25,12 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <cstring>
#include "inst_call_type.h"
#include "inst_call_type.hh"
static DispatchTable inst_dispatch = {
#define M5OP(name, func) .name = &name,
#define M5OP(name, func) .name = &::name,
M5OP_FOREACH
#undef M5OP
};

View File

@@ -25,13 +25,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __INST_CALL_TYPE_H__
#define __INST_CALL_TYPE_H__
#ifndef __INST_CALL_TYPE_HH__
#define __INST_CALL_TYPE_HH__
#include "args.h"
#include "dispatch_table.h"
#include "args.hh"
#include "dispatch_table.hh"
int inst_call_type_detect(Args *args);
DispatchTable *inst_call_type_init();
#endif // __INST_CALL_TYPE_H__
#endif // __INST_CALL_TYPE_HH__

View File

@@ -26,11 +26,12 @@
*/
#include <assert.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdlib.h>
#include <cassert>
#include <cstdlib>
#include <gem5/m5ops.h>
@@ -158,7 +159,7 @@ do_read_file(lua_State *L)
{
uint64_t len = lua_tointeger(L, 1);
uint64_t offset = lua_tointeger(L, 2);
char *buf = malloc(len);
char *buf = (char *)malloc(len);
uint64_t readlen = m5_read_file(buf, len, offset);
lua_pushlstring(L, buf, readlen);
return 1;
@@ -239,6 +240,13 @@ do_work_end(lua_State *L)
return 0;
}
extern "C"
{
int luaopen_gem5OpLua(lua_State *);
}
int
luaopen_gem5OpLua(lua_State *L)
{

View File

@@ -38,13 +38,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#include <cstring>
#include "args.h"
#include "call_type.h"
#include "commands.h"
#include "usage.h"
#include "args.hh"
#include "call_type.hh"
#include "commands.hh"
#include "usage.hh"
int
main(int argc, const char *argv[])

View File

@@ -41,13 +41,19 @@
#ifndef __UTIL_M5_MMAP_H__
#define __UTIL_M5_MMAP_H__
#include <fcntl.h>
#include <stdint.h>
#include <sys/mman.h>
#ifdef __cplusplus
extern "C" {
#endif
extern void *m5_mem;
extern uint64_t m5op_addr;
void map_m5_mem();
#ifdef __cplusplus
}
#endif
#endif // __UTIL_M5_MMAP_H__

View File

@@ -25,16 +25,19 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <cstring>
#include "semi_call_type.h"
#include "semi_call_type.hh"
extern "C"
{
#define M5OP(name, func) __typeof__(name) M5OP_MERGE_TOKENS(name, _semi);
M5OP_FOREACH
#undef M5OP
}
static DispatchTable semi_dispatch = {
#define M5OP(name, func) .name = &M5OP_MERGE_TOKENS(name, _semi),
#define M5OP(name, func) .name = &::M5OP_MERGE_TOKENS(name, _semi),
M5OP_FOREACH
#undef M5OP
};

View File

@@ -25,13 +25,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __SEMI_CALL_TYPE_H__
#define __SEMI_CALL_TYPE_H__
#ifndef __SEMI_CALL_TYPE_HH__
#define __SEMI_CALL_TYPE_HH__
#include "args.h"
#include "dispatch_table.h"
#include "args.hh"
#include "dispatch_table.hh"
int semi_call_type_detect(Args *args);
DispatchTable *semi_call_type_init();
#endif // __SEMI_CALL_TYPE_H__
#endif // __SEMI_CALL_TYPE_HH__

View File

@@ -27,6 +27,6 @@ Import('*')
env['VARIANT'] = 'sparc'
get_variant_opt('CROSS_COMPILE', 'sparc64-linux-gnu-')
env.Append(CFLAGS='-m64')
env.Append(CXXFLAGS='-m64')
env['CALL_TYPE']['inst'].impl('m5op.S', default=True)

View File

@@ -27,6 +27,6 @@ Import('*')
env['VARIANT'] = 'thumb'
get_variant_opt('CROSS_COMPILE', 'arm-linux-gnueabihf-')
env.Append(CFLAGS=[ '-mthumb', '-march=armv7' ])
env.Append(CXXFLAGS=[ '-mthumb', '-march=armv7' ])
env['CALL_TYPE']['inst'].impl('m5op.S', default=True)

View File

@@ -38,12 +38,12 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include "commands.h"
#include "usage.h"
#include "commands.hh"
#include "usage.hh"
const char *progname = "{progname}";
@@ -63,7 +63,7 @@ usage()
DEFAULT_CT_addr ? " (default)" : "");
fprintf(stderr, " Use the address based invocation method.\n");
# if defined(M5OP_ADDR)
fprintf(stderr, " The default address is %#"PRIx64".\n",
fprintf(stderr, " The default address is %#" PRIx64 ".\n",
(uint64_t)M5OP_ADDR);
# endif
# endif

View File

@@ -38,11 +38,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __USAGE_H__
#define __USAGE_H__
#ifndef __USAGE_HH__
#define __USAGE_HH__
extern const char *progname;
void usage();
#endif // __USAGE_H__
#endif // __USAGE_HH__

View File

@@ -27,7 +27,8 @@ Import('*')
env['VARIANT'] = 'x86'
get_variant_opt('CROSS_COMPILE', '')
env.Append(CFLAGS='-DM5OP_ADDR=0xFFFF0000')
env.Append(CXXFLAGS='-DM5OP_ADDR=0xFFFF0000')
env.Append(CCFLAGS='-DM5OP_ADDR=0xFFFF0000')
env['CALL_TYPE']['inst'].impl('m5op.S')
env['CALL_TYPE']['addr'].impl('m5op_addr.S', default=True)