Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / bytecode.cpp
blob: 39f1de688d30746cb3e81afe569d721b3f9b5de5 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "bytecode.hpp"

namespace SpasmImpl
{
namespace ASM
{
Bytecode_Stream::~Bytecode_Stream() {}

Bytecode_File::Bytecode_File(const std::string& filename)
    : _bytecode(filename.c_str(), std::ios_base::out | std::ios_base::binary)
{
}

void Bytecode_File::push_byte(Bytecode_Stream::byte byte)
{
    _bytecode.put(byte);
}

void Bytecode_File::push_opcode(Bytecode_Stream::Opcode_t opcode)
{
    push_byte(opcode);
}

void Bytecode_File::push_integer(int number)
{
    if (number <= 0xff)
    {
        push_byte((byte)number);
    }
    else
    {
        for (size_t i = 0; i < sizeof(int); ++i)
            push_byte((number >> (i << 3)) & 0xff);
    }
}

void Bytecode_Memory::push_byte(Bytecode_Stream::byte byte)
{
    _bytecode.push_back(byte);
}

void Bytecode_Memory::push_opcode(Bytecode_Stream::Opcode_t opcode)
{
    push_byte(opcode);
}

void Bytecode_Memory::push_integer(int number)
{
    if (number <= 0xff)
    {
        push_byte((byte)number);
    }
    else
    {
        for (size_t i = 0; i < sizeof(int); ++i)
            push_byte((number >> (i << 3)) & 0xff);
    }
}

void Bytecode_Memory::set_location(size_t index, size_t location)
{
    if (location <= 0xff)
    {
        _bytecode[index] = (byte)location;
    }
    else
    {
        for (size_t i = 0; i < sizeof(size_t); ++i)
            _bytecode.at(index + i) = ((location >> (i << 3)) & 0xff);
    }
}

void Bytecode_Memory::push_location(size_t location)
{
    if (location <= 0xff)
    {
        push_byte((byte)location);
    }
    else
    {
        for (size_t i = 0; i < sizeof(size_t); ++i)
            _bytecode.push_back((location >> (i << 3)) & 0xff);
    }
}

const Bytecode_Memory::Bytecode& Bytecode_Memory::bytecode() const
{
    return _bytecode;
}

size_t Bytecode_Memory::size() const
{
    return _bytecode.size();
}

}  // namespace ASM
}  // namespace SpasmImpl