summaryrefslogtreecommitdiff
path: root/indra/lscript/lscript_execute.h
blob: 84cd6e3b0a7ba869c1565f01c7d413066c0c6f31 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/** 
 * @file lscript_execute.h
 * @brief Classes to execute bytecode
 *
 * Copyright (c) 2002-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#ifndef LL_LSCRIPT_EXECUTE_H
#define LL_LSCRIPT_EXECUTE_H

#include <stdio.h>
#include "lscript_byteconvert.h"
#include "linked_lists.h"
#include "lscript_library.h"

// Return values for run() methods
const U32 NO_DELETE_FLAG	= 0x0000;
const U32 DELETE_FLAG		= 0x0001;
const U32 CREDIT_MONEY_FLAG	= 0x0002;

// list of op code execute functions
BOOL run_noop(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pop(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pops(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popl(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popv(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_poparg(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popip(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popbp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popsp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_popslr(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_dup(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_dups(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_dupl(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_dupv(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_dupq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_store(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_stores(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storel(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storev(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storeq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storeg(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storegs(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storegl(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storegv(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_storegq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadsp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadlp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadvp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadqp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadgp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadgsp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadglp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadgvp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_loadgqp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_push(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushs(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushl(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushv(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushg(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushgs(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushgl(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushgv(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushgq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_puship(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushbp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushsp(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushargb(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushargi(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushargf(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushargs(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushargv(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushargq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushe(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pushev(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pusheq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_pusharge(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_add(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_sub(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_mul(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_div(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_mod(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_eq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_neq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_leq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_geq(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_less(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_greater(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_bitand(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_bitor(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_bitxor(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_booland(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_boolor(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_shl(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_shr(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_neg(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_bitnot(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_boolnot(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_jump(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_jumpif(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_jumpnif(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_state(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_call(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_return(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_cast(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_stacktos(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_stacktol(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_print(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

BOOL run_calllib(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);
BOOL run_calllib_two_byte(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

void unknown_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void integer_integer_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void integer_float_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void integer_vector_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void float_integer_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void float_float_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void float_vector_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void string_string_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void string_key_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void key_string_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void key_key_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void vector_integer_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void vector_float_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void vector_vector_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void vector_quaternion_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void quaternion_quaternion_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);


void integer_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void float_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void string_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void key_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void vector_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void quaternion_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_integer_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_float_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_string_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_key_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_vector_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_quaternion_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void list_list_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);

void integer_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void float_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void vector_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);
void quaternion_operation(U8 *buffer, LSCRIPTOpCodesEnum opcode);

class LLScriptDataCollection
{
public:
	LLScriptDataCollection(LSCRIPTStateEventType type, LLScriptLibData *data)
		: mType(type), mData(data)
	{
	}
	LLScriptDataCollection(U8 *src, S32 &offset)
	{
		S32 i, number;
		mType = (LSCRIPTStateEventType)bytestream2integer(src, offset);
		number = bytestream2integer(src,  offset);

		mData = new LLScriptLibData[number];

		for (i = 0; i < number; i++)
		{
			mData[i].set(src, offset);
		}

	}

	~LLScriptDataCollection()
	{
		delete [] mData;
		mData = NULL;
	}

	S32  getSavedSize()
	{
		S32 size = 0;
		// mTyoe
		size += 4;
		// number of entries
		size += 4;

		S32 i = 0;
		do
		{
			size += mData[i].getSavedSize();;
		}
		while (mData[i++].mType != LST_NULL);
		return size;
	}

	S32	 write2bytestream(U8 *dest)
	{
		S32 offset = 0;
		// mTyoe
		integer2bytestream(dest, offset, mType);
		// count number of entries
		S32 number = 0;
		while (mData[number++].mType != LST_NULL)
			;
		integer2bytestream(dest, offset, number);

		// now the entries themselves
		number = 0;
		do
		{
			offset += mData[number].write2bytestream(dest + offset);
		}
		while (mData[number++].mType != LST_NULL);
		return offset;
	}


	LSCRIPTStateEventType	mType;
	LLScriptLibData			*mData;
};
const S32 MAX_EVENTS_IN_QUEUE = 64;

class LLScriptEventData
{
public:
	LLScriptEventData()		{}
	LLScriptEventData(U8 *src, S32 &offset)
	{
		S32 i, number = bytestream2integer(src, offset);
		for (i = 0; i < number; i++)
		{
			mEventDataList.addData(new LLScriptDataCollection(src, offset));
		}
	}

	void set(U8 *src, S32 &offset)
	{
		S32 i, number = bytestream2integer(src, offset);
		for (i = 0; i < number; i++)
		{
			mEventDataList.addData(new LLScriptDataCollection(src, offset));
		}
	}

	~LLScriptEventData()	
	{
		mEventDataList.deleteAllData();
	}

	void addEventData(LLScriptDataCollection *data)
	{
		if (mEventDataList.getLength() < MAX_EVENTS_IN_QUEUE)
			mEventDataList.addDataAtEnd(data);
		else
			delete data;
	}
	LLScriptDataCollection *getNextEvent(LSCRIPTStateEventType type)
	{
		LLScriptDataCollection *temp;
		for (temp = mEventDataList.getFirstData();
			 temp;
			 temp = mEventDataList.getNextData())
		{
			if (temp->mType == type)
			{
				mEventDataList.removeCurrentData();
				return temp;
			}
		}
		return NULL;
	}
	LLScriptDataCollection *getNextEvent()
	{
		LLScriptDataCollection *temp;
		temp = mEventDataList.getFirstData();
		if (temp)
		{
			mEventDataList.removeCurrentData();
			return temp;
		}
		return NULL;
	}
	void removeEventType(LSCRIPTStateEventType type)
	{
		LLScriptDataCollection *temp;
		for (temp = mEventDataList.getFirstData();
			 temp;
			 temp = mEventDataList.getNextData())
		{
			if (temp->mType == type)
			{
				mEventDataList.deleteCurrentData();
			}
		}
	}

	S32  getSavedSize()
	{
		S32 size = 0;
		// number in linked list
		size += 4;
		LLScriptDataCollection *temp;
		for (temp = mEventDataList.getFirstData();
			 temp;
			 temp = mEventDataList.getNextData())
		{
			size += temp->getSavedSize();
		}
		return size;
	}

	S32	 write2bytestream(U8 *dest)
	{
		S32 offset = 0;
		// number in linked list
		S32 number = mEventDataList.getLength();
		integer2bytestream(dest, offset, number);
		LLScriptDataCollection *temp;
		for (temp = mEventDataList.getFirstData();
			 temp;
			 temp = mEventDataList.getNextData())
		{
			offset += temp->write2bytestream(dest + offset);
		}
		return offset;
	}

	LLLinkedList<LLScriptDataCollection>	mEventDataList;
};

class LLScriptExecute
{
public:
	LLScriptExecute(FILE *fp);
	LLScriptExecute(U8 *buffer);
	~LLScriptExecute();

	void init();
	U32 run(BOOL b_print, const LLUUID &id, char **errorstr, BOOL &state_transition);

	BOOL (*mExecuteFuncs[0x100])(U8 *buffer, S32 &offset, BOOL b_print, const LLUUID &id);

	U32						mInstructionCount;
	U8						*mBuffer;
	LLScriptEventData		mEventData;

	static S64 sGlobalInstructionCount;
};

#endif