aboutsummaryrefslogtreecommitdiff
path: root/engines/director/lingo/lingo-bytecode.cpp
blob: c04515ac07a8a220d9e9c2b0afb5c0b698cee8df (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "director/lingo/lingo.h"

namespace Director {

static struct LingoV4Bytecode {
    const uint8 opcode;
    const inst func;
    const char *args;
} lingoV4[] = {
    { 0x03, Lingo::c_voidpush, "" },
    { 0x04, Lingo::c_mul, "" },
    { 0x05, Lingo::c_add, "" },
    { 0x06, Lingo::c_sub, "" },
    { 0x07, Lingo::c_div, "" },
    { 0x08, Lingo::c_mod, "" },
    { 0x09, Lingo::c_negate, "" },
    { 0x0a, Lingo::c_ampersand, "" },
    { 0x0b, Lingo::c_concat, "" },
    { 0x0c, Lingo::c_lt, "" },
    { 0x0d, Lingo::c_le, "" },
    { 0x0e, Lingo::c_neq, "" },
    { 0x0f, Lingo::c_eq, "" },
    { 0x10, Lingo::c_gt, "" },
    { 0x11, Lingo::c_ge, "" },
    { 0x12, Lingo::c_and, "" },
    { 0x13, Lingo::c_or, "" },
    { 0x14, Lingo::c_not, "" },
    { 0x15, Lingo::c_contains, "" },
    { 0x16, Lingo::c_starts, "" },
    { 0x17, Lingo::c_of, "" },
    { 0x18, Lingo::c_hilite, "" },
    { 0x19, Lingo::c_intersects, "" },
    { 0x1a, Lingo::c_within, "" },
    { 0x1b, Lingo::c_field, "" },
    { 0x1c, Lingo::c_tell, "" },
    { 0x1d, Lingo::c_telldone, "" },
    
    { 0x41, Lingo::c_intpush, "b" },
    { 0, 0, 0 }
};

void Lingo::initBytecode() {
    for (LingoV4Bytecode *op = lingoV4; op->opcode; op++) {
        _lingoV4[op->opcode] = new Opcode( op->func, op->args );
    }
}


void Lingo::addCodeV4(Common::SeekableSubReadStreamEndian &stream, ScriptType type, uint16 id) { 
	debugC(1, kDebugLingoCompile, "Add V4 bytecode for type %s with id %d", scriptType2str(type), id);

	if (_scripts[type].contains(id)) {
		delete _scripts[type][id];
	}

	_currentScript = new ScriptData;
	_currentScriptType = type;
	_scripts[type][id] = _currentScript;
	_currentEntityId = id;

    // read the Lscr header!
    // unk1
    for (uint32 i = 0; i < 0x10; i++) {
        stream.readByte();
    }
    uint16 code_store_offset = stream.readUint16();
    // unk2
    for (uint32 i = 0; i < 0x2e; i++) {
        stream.readByte();
    }
    uint16 functions_offset = stream.readUint16();
    // unk3
    for (uint32 i = 0; i < 0x6; i++) {
        stream.readByte();
    }
    uint16 functions_count = stream.readUint16();
    uint16 unk4 = stream.readUint16();
    uint16 unk5 = stream.readUint16();
    uint16 consts_count = stream.readUint16();
    stream.readUint16();
    uint16 consts_offset = stream.readUint16();
    stream.readUint16();
    uint16 unk6 = stream.readUint16();
    stream.readUint16();
    uint16 consts_base = stream.readUint16();

    // preload all the constants!
    // these are stored as a reference table of 6 byte entries, followed by a storage area.

    // copy the storage area first.
    uint32 consts_store_offset = consts_offset+6*consts_count;
    uint32 consts_store_size = stream.size()-consts_store_offset;
    stream.seek(consts_store_offset);
    byte *const_store = (byte *)malloc(consts_store_size);
    stream.read(const_store, consts_store_size);

    Common::Array<Datum> const_data;

    // read each entry in the reference table.
    stream.seek(consts_offset);
    for (uint16 i=0; i<consts_count; i++) {
        Datum constant;
        uint16 const_type = stream.readUint16();
        uint32 value = stream.readUint32();
        switch (const_type) {
            case 1: { // String type
                constant.type = STRING;
                constant.u.s = new Common::String();
                if (value < consts_store_offset) {
                    warning("Constant string start offset is out of bounds!");
                    break;
                }
                uint32 pointer = value - consts_store_offset;
                while (pointer < consts_store_size) {
                    if (const_store[pointer] == '\r') {
                        constant.u.s += '\n';
                    } else if (const_store[pointer] == '\0') {
                        break;
                    } else {
                        constant.u.s += const_store[pointer];
                    }
                    pointer += 1;
                }
                if (pointer == consts_store_size) {
                    warning("Constant string has no null terminator!");
                    break;
                }
            }
            break;
            case 4: // Integer type
                constant.type = INT;
                constant.u.i = (int)value;
                break;
            case 9: { // Float type
                constant.type = FLOAT;
                if (value < consts_store_offset) {
                    warning("Constant float start offset is out of bounds!");
                    break;
                } else if (value+4 > consts_store_offset + consts_store_size) {
                    warning("Constant float end offset is out of bounds!");
                    break;
                }
                constant.u.f = *(float *)(const_store+value-consts_store_offset);
            }
            break;
            default:
                warning("Unknown constant type %d", type);
                break;
        }

        const_data.push_back(constant);
    }
    free(const_store);

    // parse each function!
    // these are stored as a code storage area, followed by a reference table of 42 byte entries.

    // copy the storage area first.
    uint32 code_store_size = functions_offset - code_store_offset;
    stream.seek(code_store_offset);
    byte *code_store = (byte *)malloc(code_store_size);
    stream.read(code_store, code_store_size);

    // read each entry in the function table.
    stream.seek(functions_offset);
    for (uint16 i=0; i<functions_count; i++) {
        stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        uint16 arg_count = stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        uint16 var_count = stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        uint16 name_index = stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        uint16 length = stream.readUint16();
        stream.readUint16();
        uint16 start_offset = stream.readUint16();
        stream.readUint16();
        stream.readUint16();
        uint16 end_offset = stream.readUint16();

        if (start_offset < code_store_offset) {
            warning("Function %d start offset is out of bounds!", i);
            continue;
        } else if (end_offset >= code_store_offset+code_store_size) {
            warning("Function %d end offset is out of bounds!", i);
            continue;
        }

        uint32 pointer = start_offset-code_store_offset;
        Common::Array<uint32> offset_list;
        while (pointer < end_offset-code_store_offset) {
            uint8 opcode = code_store[pointer];

            pointer += 1;
            if (_lingoV4.contains(opcode)) {
                offset_list.push_back(_currentScript->size());
                _currentScript->push_back(_lingoV4[opcode]->func);

            } else {
                // unimplemented instruction
                inst op_value = 0;
                WRITE_UINT32(&op_value, opcode);

                if (opcode < 0x40) {
                    offset_list.push_back(_currentScript->size());
                    _currentScript->push_back(Lingo::c_nop);
                    _currentScript->push_back(op_value);
                } else if (opcode < 0x80) {
                    offset_list.push_back(_currentScript->size());
                    _currentScript->push_back(Lingo::c_nop1);
                    _currentScript->push_back(op_value);
                    inst arg1 = 0;
                    WRITE_UINT32(&arg1, code_store[pointer]);
                    offset_list.push_back(_currentScript->size());
                    _currentScript->push_back(arg1);
                    pointer += 1;
                } else {
                    offset_list.push_back(_currentScript->size());
                    _currentScript->push_back(Lingo::c_nop2);
                    _currentScript->push_back(op_value);
                    inst arg1 = 0;
                    WRITE_UINT32(&arg1, code_store[pointer]);
                    offset_list.push_back(_currentScript->size());
                    _currentScript->push_back(arg1);
                    inst arg2 = 0;
                    WRITE_UINT32(&arg2, code_store[pointer+1]);
                    offset_list.push_back(_currentScript->size());
                    _currentScript->push_back(arg2);
                    pointer += 2;
                }
            }
        }

    }

}

}