Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / bytecode.hpp
blob: c08da46e833b4bc56a4886095e0b1ce0e3fa9586 (plain) (blame)
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
#ifndef BYTECODE_HPP
#define BYTECODE_HPP

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

namespace SpasmImpl
{
namespace ASM
{
class Bytecode_Stream
{
   public:
    typedef unsigned char byte;
    enum Opcode_t : byte
    {
        halt = 0,
        push,
        pop,
        dup,
        print,
        read,
        plus,
        minus,
        multiply,
        divide,
        modulus,
        gotrue,
        gofalse,
        go,
        call,
        ret,
        load,
        store,
        less,
        lesseq
    };


    virtual ~Bytecode_Stream();
    virtual void push_opcode(Opcode_t) = 0;
    virtual void push_integer(int) = 0;
    virtual void push_location(size_t) = 0;
    virtual void set_location(size_t, size_t) = 0;
    virtual size_t size() const = 0;
};  // class Bytecode_Stream

class Bytecode_File : public Bytecode_Stream
{
   public:
    Bytecode_File(const std::string&);

    virtual void push_opcode(Opcode_t);
    virtual void push_integer(int);

   private:
    Bytecode_File(const Bytecode_File&);
    Bytecode_File& operator=(const Bytecode_File&);
    void push_byte(Bytecode_Stream::byte);

    std::ofstream _bytecode;

};  // class Bytecode_File

class Bytecode_Memory : public Bytecode_Stream
{
   public:
    virtual void push_opcode(Opcode_t);
    virtual void push_integer(int);
    virtual void push_location(size_t);
    virtual void set_location(size_t, size_t);
    virtual size_t size() const;

    typedef std::vector<Bytecode_Stream::byte> Bytecode;
    const Bytecode& bytecode() const;

   private:
    void push_byte(Bytecode_Stream::byte);

    std::vector<Bytecode_Stream::byte> _bytecode;
};  // class Bytecode_Memory
}  // namespace ASM
}  // namespace SpasmImpl

#endif  // BYTECODE_HPP