File: extensions.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (207 lines) | stat: -rw-r--r-- 6,501 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "extensions.hpp"

#include <cassert>
#include <stdexcept>

#include "generator.hpp"
#include "literals.hpp"

namespace Compiler
{
    Extensions::Extensions()
        : mNextKeywordIndex(-1)
    {
    }

    int Extensions::searchKeyword(const std::string& keyword) const
    {
        auto iter = mKeywords.find(keyword);
        if (iter == mKeywords.end())
            return 0;

        return iter->second;
    }

    bool Extensions::isFunction(
        int keyword, ScriptReturn& returnType, ScriptArgs& argumentType, bool& explicitReference) const
    {
        auto iter = mFunctions.find(keyword);
        if (iter == mFunctions.end())
            return false;

        if (explicitReference && iter->second.mCodeExplicit == -1)
            explicitReference = false;

        returnType = iter->second.mReturn;
        argumentType = iter->second.mArguments;
        return true;
    }

    bool Extensions::isInstruction(int keyword, ScriptArgs& argumentType, bool& explicitReference) const
    {
        auto iter = mInstructions.find(keyword);
        if (iter == mInstructions.end())
            return false;

        if (explicitReference && iter->second.mCodeExplicit == -1)
            explicitReference = false;

        argumentType = iter->second.mArguments;
        return true;
    }

    void Extensions::registerFunction(
        std::string_view keyword, ScriptReturn returnType, std::string_view argumentType, int code, int codeExplicit)
    {
        Function function;

        if (argumentType.find('/') == std::string_view::npos)
        {
            function.mSegment = 5;
            assert(code >= 33554432 && code <= 67108863);
            assert(codeExplicit == -1 || (codeExplicit >= 33554432 && codeExplicit <= 67108863));
        }
        else
        {
            function.mSegment = 3;
            assert(code >= 0x20000 && code <= 0x2ffff);
            assert(codeExplicit == -1 || (codeExplicit >= 0x20000 && codeExplicit <= 0x2ffff));
        }

        int keywordIndex = mNextKeywordIndex--;

        mKeywords.emplace(keyword, keywordIndex);

        function.mReturn = returnType;
        function.mArguments = argumentType;
        function.mCode = code;
        function.mCodeExplicit = codeExplicit;

        mFunctions.emplace(keywordIndex, std::move(function));
    }

    void Extensions::registerInstruction(
        std::string_view keyword, std::string_view argumentType, int code, int codeExplicit)
    {
        Instruction instruction;

        if (argumentType.find('/') == std::string_view::npos)
        {
            instruction.mSegment = 5;
            assert(code >= 33554432 && code <= 67108863);
            assert(codeExplicit == -1 || (codeExplicit >= 33554432 && codeExplicit <= 67108863));
        }
        else
        {
            instruction.mSegment = 3;
            assert(code >= 0x20000 && code <= 0x2ffff);
            assert(codeExplicit == -1 || (codeExplicit >= 0x20000 && codeExplicit <= 0x2ffff));
        }

        int keywordIndex = mNextKeywordIndex--;

        mKeywords.emplace(keyword, keywordIndex);

        instruction.mArguments = argumentType;
        instruction.mCode = code;
        instruction.mCodeExplicit = codeExplicit;

        mInstructions.emplace(keywordIndex, std::move(instruction));
    }

    void Extensions::generateFunctionCode(int keyword, std::vector<Interpreter::Type_Code>& code, Literals& literals,
        const std::string& id, int optionalArguments) const
    {
        assert(optionalArguments >= 0);

        auto iter = mFunctions.find(keyword);
        if (iter == mFunctions.end())
            throw std::logic_error("unknown custom function keyword");

        if (optionalArguments && iter->second.mSegment != 3)
            throw std::logic_error("functions with optional arguments must be placed into segment 3");

        if (!id.empty())
        {
            if (iter->second.mCodeExplicit == -1)
                throw std::logic_error("explicit references not supported");

            int index = literals.addString(id);
            Generator::pushInt(code, literals, index);
        }

        switch (iter->second.mSegment)
        {
            case 3:

                if (optionalArguments >= 256)
                    throw std::logic_error("number of optional arguments is too large for segment 3");

                code.push_back(Generator::segment3(
                    id.empty() ? iter->second.mCode : iter->second.mCodeExplicit, optionalArguments));

                break;

            case 5:

                code.push_back(Generator::segment5(id.empty() ? iter->second.mCode : iter->second.mCodeExplicit));

                break;

            default:

                throw std::logic_error("unsupported code segment");
        }
    }

    void Extensions::generateInstructionCode(int keyword, std::vector<Interpreter::Type_Code>& code, Literals& literals,
        const std::string& id, int optionalArguments) const
    {
        assert(optionalArguments >= 0);

        auto iter = mInstructions.find(keyword);
        if (iter == mInstructions.end())
            throw std::logic_error("unknown custom instruction keyword");

        if (optionalArguments && iter->second.mSegment != 3)
            throw std::logic_error("instructions with optional arguments must be placed into segment 3");

        if (!id.empty())
        {
            if (iter->second.mCodeExplicit == -1)
                throw std::logic_error("explicit references not supported");

            int index = literals.addString(id);
            Generator::pushInt(code, literals, index);
        }

        switch (iter->second.mSegment)
        {
            case 3:

                if (optionalArguments >= 256)
                    throw std::logic_error("number of optional arguments is too large for segment 3");

                code.push_back(Generator::segment3(
                    id.empty() ? iter->second.mCode : iter->second.mCodeExplicit, optionalArguments));

                break;

            case 5:

                code.push_back(Generator::segment5(id.empty() ? iter->second.mCode : iter->second.mCodeExplicit));

                break;

            default:

                throw std::logic_error("unsupported code segment");
        }
    }

    void Extensions::listKeywords(std::vector<std::string>& keywords) const
    {
        for (const auto& mKeyword : mKeywords)
            keywords.push_back(mKeyword.first);
    }
}