1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 #ifndef CMAJOR_CMSXBE_VALUE_STACK_INCLUDED
 7 #define CMAJOR_CMSXBE_VALUE_STACK_INCLUDED
 8 #include <cmajor/ir/ValueStack.hpp>
 9 #include <soulng/util/Error.hpp>
10 #include <cmajor/cmsxi/Value.hpp>
11 #include <vector>
12 
13 namespace cmsxbe {
14 
15 class ValueStack public cmajor::ir::ValueStack
16 {
17 public:
18     void Push(void* val) override
19     {
20         cmsxi::Value* value = static_cast<cmsxi::Value*>(val);
21         s.push_back(value);
22     }
23     void* Pop() override
24     {
25         Assert(!s.empty()"value stack is empty");
26         cmsxi::Value* top = s.back();
27         s.pop_back();
28         return top;
29     }
30     void Dup() override
31     {
32         s.push_back(s.back());
33     }
34     void Swap() override
35     {
36         std::swap(s.back()s[s.size() - 2]);
37     }
38     void Rotate() override
39     {
40         std::swap(s[s.size() - 3]s[s.size() - 2]);
41         std::swap(s.back()s[s.size() - 2]);
42     }
43 private:
44     std::vector<cmsxi::Value*> s;
45 };
46 
47 } // namespace cmsxbe
48 
49 #endif // CMAJOR_CMSXBE_VALUE_STACK_INCLUDED