-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants.py
404 lines (321 loc) · 10.1 KB
/
constants.py
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Constants shared between LSL and Python
Converted to LSL by generated_code.py.
* Integer constants will be included as-is
* Enums will be converted to LSL with prefixed values
* NamedTuples will be treated as indices into strided lists
"""
import enum
import os
import typing
from lummao import Key
OPCODE_WIDTH = 5
# How many registers get pushed when we CALL
CALL_REGISTERS_LEN = 3
def _limit_bits(num_bits):
max_val = (2 ** num_bits) - 1
def _cls_wrapper(cls):
if max(cls) > max_val:
raise ValueError(f"{cls}'s {int(max(cls))} is bigger than {max_val}")
return cls
return _cls_wrapper
class LibraryFunc(typing.NamedTuple):
num: int
arg_types: typing.List[str]
ret_type: str
name: str
def _parse_builtins() -> typing.Tuple[dict, dict]:
library_funcs = {}
events = {}
with open(os.environ["LSL_PYOPTIMIZER_PATH"] + "/builtins.txt", "r") as f:
cur_func_num = 0
cur_event_num = 0
for line in f:
line = line.strip()
if not line or line.startswith("//"):
continue
decl_type, _, decl = line.partition(" ")
# Only care about functions
if decl_type == "const":
continue
func_name, _, argspec = decl.partition("(")
argspec = argspec.rstrip(")")
args = []
for arg in argspec.split(","):
arg = arg.strip()
if not arg:
continue
arg_type, _, arg_name = arg.partition(" ")
args.append(arg_type.strip())
func_name = func_name.strip()
if decl_type == "event":
events[func_name] = LibraryFunc(
ret_type="void",
arg_types=args,
num=cur_event_num,
name=func_name,
)
cur_event_num += 1
else:
library_funcs[func_name] = LibraryFunc(
ret_type=decl_type,
arg_types=args,
num=cur_func_num,
name=func_name,
)
cur_func_num += 1
return library_funcs, events
LIBRARY_FUNCS, EVENTS = _parse_builtins()
# relatively small number of opcodes
# with a lot of options for those opcodes to reduce
# branching overhead. LSL doesn't have a switch() statement,
# jump tables, or computed goto!
#
# For ex, LSO2 has a "PUSHGS", which means push a string
# from a global, instead we have a "PUSH" with "whence" and
# "type" operands of "global" and "string".
@_limit_bits(OPCODE_WIDTH)
class OpCode(enum.IntEnum):
NO_OP = 0
BIN_OP = enum.auto()
UN_OP = enum.auto()
CAST = enum.auto()
BOOL = enum.auto()
ALLOC_SLOTS = enum.auto()
PUSH = enum.auto()
POP_N = enum.auto()
STORE = enum.auto()
STORE_DEFAULT = enum.auto()
DUP = enum.auto()
SWAP = enum.auto()
JUMP = enum.auto()
CALL = enum.auto()
RET = enum.auto()
CALL_LIB = enum.auto()
BUILD_LIST = enum.auto()
BUILD_COORD = enum.auto()
# These should be close to last.
# They're instructions for working with the member
# accessors of vectors and rotations
TAKE_MEMBER = enum.auto()
REPLACE_MEMBER = enum.auto()
YIELD = enum.auto()
CHANGE_STATE = enum.auto()
DUMP = enum.auto()
@_limit_bits(2)
class Register(enum.IntEnum):
BP = 0
IP = enum.auto()
@_limit_bits(2)
class JumpType(enum.IntEnum):
ALWAYS = 0
IF = 1
NIF = 2
@_limit_bits(2)
class CallType(enum.IntEnum):
RELATIVE = 0
ABSOLUTE = enum.auto()
@_limit_bits(2)
class Whence(enum.IntEnum):
CONST = 0
LOCAL = enum.auto()
GLOBAL = enum.auto()
ARG = enum.auto()
@_limit_bits(4)
class LSLType(enum.IntEnum):
# Note that this does NOT match LSL's TYPE_ builtin constants!
# We optimize for smaller string representation of integer opcodes.
# Can't ever go above 9 due to how we do type strings
# (or 15 if we switched to hex)
INTEGER = 0
FLOAT = 1
STRING = 2
KEY = 3
VECTOR = 4
ROTATION = 5
LIST = 6
VOID = 7
@_limit_bits(2)
class CoordAccessor(enum.IntEnum):
X = 0
Y = enum.auto()
Z = enum.auto()
S = enum.auto()
@_limit_bits(5)
class Operation(enum.IntEnum):
"""
Kinds of operations in LSL
Some of these that LSL supports aren't in here because they'll
be de-sugared by the compiler.
"""
PLUS = 0
MINUS = enum.auto()
MUL = enum.auto()
DIV = enum.auto()
MOD = enum.auto()
BOOLEAN_NOT = enum.auto()
BOOLEAN_AND = enum.auto()
BOOLEAN_OR = enum.auto()
LESS = enum.auto()
GREATER = enum.auto()
LEQ = enum.auto()
GEQ = enum.auto()
EQ = enum.auto()
NEQ = enum.auto()
BIT_NOT = enum.auto()
BIT_XOR = enum.auto()
BIT_AND = enum.auto()
BIT_OR = enum.auto()
SHIFT_LEFT = enum.auto()
SHIFT_RIGHT = enum.auto()
# Not bit-limited, this will occupy the whole `num` field of a `link_message`
class IPCType(enum.IntEnum):
# MIN and MAX MUST be set so the manager can figure out
# which link_message ranges belong to the interpreter,
# to strip those out when forwarding events along.
MIN = 0XF1FE
# Interpreter -> Library
CALL_LIB = enum.auto()
# Interpreter <- Library
CALL_LIB_REPLY = enum.auto()
# Manager -> Interpreter
INVOKE_HANDLER = enum.auto()
# Interpreter -> Manager
HANDLER_FINISHED = enum.auto()
# Interpreter -> Manager
REQUEST_CODE = enum.auto()
# Interpreter <- Manager
REQUEST_CODE_REPLY = enum.auto()
# Manager -> *
MANAGER_RESTARTED = enum.auto()
# Manager -> Interpreter
SCRIPT_LOADED = enum.auto()
# Interpreter -> Manager
CHANGE_STATE = enum.auto()
MAX = enum.auto()
class LibFuncRet(enum.IntEnum):
NONE = 0
SIMPLE = enum.auto()
LIST = enum.auto()
class LoadState(enum.IntEnum):
NONE = 0
HANDLERS = enum.auto()
LINES = enum.auto()
CODE = enum.auto()
def _lsl_namedtuple(cls):
"""
Mark this NamedTuple as a definition for a typed list stride
Makes it less annoying to ensure we're working with the same stride definitions in
both Python and LSL. These get splatted out into index offsets + stride definitions
for LSL in generate_code.py.
"""
cls._is_lsl_namedtuple = True
cls.indices = enum.IntEnum(cls.__name__, [(v.upper(), k) for k, v in enumerate(cls._fields)])
return cls
@_lsl_namedtuple
class HandlerIndex(typing.NamedTuple):
"""An index allowing lookup of state and event -> instruction pointer"""
# ORed together, state is top 16 bits, event type is bottom 16 bits.
state_and_event: int
# IP where the event handler starts, manager asks interpreter to call
# into this.
ip: int
@_lsl_namedtuple
class CodeIndex(typing.NamedTuple):
"""An index used to find which notecard line a code chunk lives on"""
# IP for the first byte in the code line
ip: int
# ORed together to notecard num and line. Notecard in the high bits.
nc_and_line: int
@_lsl_namedtuple
class CachedCode(typing.NamedTuple):
"""An entry in the Manager's LRU code cache"""
last_used: int
# ORed together to notecard num and line. Notecard in the high bits.
# Used as a cache key
nc_and_line: int
code: typing.Union[str, int] # only the placeholder is an int
# How many code lines to keep in the cache
NUM_CACHED_CODES = 15
CACHED_CODES_LEN = NUM_CACHED_CODES * len(CachedCode.indices)
@_lsl_namedtuple
class QueuedEvent(typing.NamedTuple):
"""An event the Manager is holding until the Interpreter is ready"""
state_and_event: int
ip: int
args: str
detected_stack: str
@_lsl_namedtuple
class DetectEntry(typing.NamedTuple):
key: Key
owner: Key
MAX_QUEUED_EVENTS = 5
MAX_EVENT_QUEUE_SIZE = MAX_QUEUED_EVENTS * len(QueuedEvent.indices)
# Some functions need special handling by some other script, and should _not_ be handled
# by the library scripts. Especially important for functions that only generate events
# in the script that called the function, like llListen().
# These are ones that can have autogenerated wrappers that just need to live in the
# manager script.
MANAGER_AUTO_FUNCTIONS = {
# Things might get wacky if some async thing is the thing sleeping
"llSleep",
# We need to handle listen() events
"llListen",
"llListenRemove",
"llListenControl",
# We need to handle timer() events
"llSetTimerEvent",
# Targets are script-specific, we need to make sure they're always dealt with from
# the same script.
"llMoveToTarget",
"llStopMoveToTarget",
# These are probably the same as above
"llLookAt",
"llRotLookAt",
"llStopLookAt",
# These all need to live in the same script
"llGetTime",
"llResetTime",
"llGetAndResetTime",
# If we don't do this from the same script as the one with the link_message() event
# (the manager) then we'll pick up our own link messages!
"llMessageLinked",
# These are functions that deal with perms. They need to live in the same script
# that has the perms, they can't be in _any_ other script!
"llGetPermissions",
"llGetPermissionsKey",
"llRequestPermissions",
"llTransferLindenDollars",
"llGiveMoney",
"llTakeControls",
"llReleaseControls",
"llStartAnimation",
"llStopAnimation",
"llAttachToAvatar",
"llAttachToAvatarTemp",
"llDetachFromAvatar",
"llBreakAllLinks",
"llBreakLink",
"llCreateLink",
"llGetCameraPos",
"llGetCameraRot",
"llSetCameraParams",
"llClearCameraParams",
"llTeleportAgent",
"llManageEstateAccess",
"llSetAnimationOverride",
"llGetAnimationOverride",
"llResetAnimationOverride",
"llReturnObjectsByOwner",
"llReturnObjectsByID",
}
# All functions that have implementations that live in manager.lsl
SPECIAL_FUNCTIONS = sorted({
*MANAGER_AUTO_FUNCTIONS,
# These have special implementations that can't be autogenerated. They're written
# directly in manager.lsl.
"llDie",
"llResetScript",
"llDetectedKey",
"llDetectedOwner",
})