arguments.hh revision 11800:54436a1784dc
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#ifndef __SIM_ARGUMENTS_HH__
32#define __SIM_ARGUMENTS_HH__
33
34#include <cassert>
35#include <memory>
36
37#include "mem/fs_translating_port_proxy.hh"
38
39class ThreadContext;
40
41class Arguments
42{
43  protected:
44    ThreadContext *tc;
45    int number;
46    uint64_t getArg(uint16_t size = (uint16_t)(-1), bool fp = false);
47
48  protected:
49    class Data
50    {
51      public:
52        Data(){}
53        ~Data();
54
55      private:
56        std::list<char *> data;
57
58      public:
59        char *alloc(size_t size);
60    };
61
62    std::shared_ptr<Data> data;
63
64  public:
65    Arguments(ThreadContext *ctx, int n = 0)
66        : tc(ctx), number(n), data(new Data())
67    { assert(number >= 0); }
68    Arguments(const Arguments &args)
69        : tc(args.tc), number(args.number), data(args.data) {}
70    ~Arguments() {}
71
72    ThreadContext *getThreadContext() const { return tc; }
73
74    const Arguments &operator=(const Arguments &args) {
75        if (this != &args) {
76            tc = args.tc;
77            number = args.number;
78            data = args.data;
79        }
80        return *this;
81    }
82
83    // for checking if an argument is NULL
84    bool operator!() {
85        return getArg() == 0;
86    }
87
88    Arguments &operator++() {
89        ++number;
90        assert(number >= 0);
91        return *this;
92    }
93
94    Arguments operator++(int) {
95        Arguments args = *this;
96        ++number;
97        assert(number >= 0);
98        return args;
99    }
100
101    Arguments &operator--() {
102        --number;
103        assert(number >= 0);
104        return *this;
105    }
106
107    Arguments operator--(int) {
108        Arguments args = *this;
109        --number;
110        assert(number >= 0);
111        return args;
112    }
113
114    const Arguments &operator+=(int index) {
115        number += index;
116        assert(number >= 0);
117        return *this;
118    }
119
120    const Arguments &operator-=(int index) {
121        number -= index;
122        assert(number >= 0);
123        return *this;
124    }
125
126    Arguments operator[](int index) {
127        return Arguments(tc, index);
128    }
129
130    template <class T>
131    operator T() {
132        assert(sizeof(T) <= sizeof(uint64_t));
133        T d = static_cast<T>(getArg(sizeof(T)));
134        return d;
135    }
136
137    template <class T>
138    operator T *() {
139        T *buf = (T *)data->alloc(sizeof(T));
140        CopyOut(tc, buf, getArg(sizeof(T)), sizeof(T));
141        return buf;
142    }
143
144    operator char *() {
145        char *buf = data->alloc(2048);
146        CopyStringOut(tc, buf, getArg(), 2048);
147        return buf;
148    }
149};
150
151#endif // __SIM_ARGUMENTS_HH__
152