This repository was archived by the owner on Mar 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir2asm.py
More file actions
169 lines (148 loc) · 6.89 KB
/
Copy pathir2asm.py
File metadata and controls
169 lines (148 loc) · 6.89 KB
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
"""
Compiles Lummao LSL intermediate representation to SickJoke bytecode.
"""
from typing import *
from lummao import Key, Vector, Quaternion, convert_script_to_ir
from assembler import Label, types_to_str, HandlerLabel
from constants import *
class IRConverter:
def __init__(self, ir: Dict):
self.ir = ir
self.asm_list = []
self.states = []
def convert(self) -> List[Any]:
if self.asm_list:
raise RuntimeError("IRConverter has already been used!")
for state in self.ir["states"]:
self.states.append(state["name"])
self.asm_list.append(Label("_start"))
global_types = [LSLType[x.upper()] for x in self.ir["globals"]]
if global_types:
self.asm_list.append([OpCode.ALLOC_SLOTS, Whence.GLOBAL, types_to_str(global_types)])
self.build_function({"code": self.ir["init_code"]}, "_start")
for func in self.ir["functions"]:
unique_prefix = f"f{func['name']}"
self.asm_list.append(Label(unique_prefix))
self.build_function(func, unique_prefix + "/")
for state_num, state in enumerate(self.ir["states"]):
for handler in state["handlers"]:
unique_prefix = f"e{state['name']}/{handler['name']}"
self.asm_list.append(HandlerLabel(state_num, handler["name"]))
self.build_function(handler, unique_prefix + "/")
return self.asm_list
def build_function(self, func_data: Dict, unique_prefix: str):
if func_data.get("locals"):
local_types = [LSLType[x.upper()] for x in func_data["locals"]]
self.asm_list.append([OpCode.ALLOC_SLOTS, Whence.LOCAL, types_to_str(local_types)])
for instr in func_data["code"]:
instr_type = instr["instr_type"]
if instr_type == "label":
self.asm_list.append(Label(unique_prefix + instr["label"]))
continue
assert(instr_type == "op")
whence, index = self._fix_whence(func_data, instr)
op = instr["op"]
if op in ("STORE", "STORE_DEFAULT", "PUSH"):
self.asm_list.append([
OpCode[op], LSLType[instr["type"].upper()], whence, index,
])
elif op == "PUSH_CONSTANT":
value = instr["value"]
if instr["type"] == "vector":
value = str(Vector([float(x) for x in value]))
elif instr["type"] == "rotation":
value = str(Quaternion([float(x) for x in value]))
elif instr["type"] == "key":
value = Key(value)
elif instr["type"] == "float":
value = float(value)
self.asm_list.append([
OpCode.PUSH, LSLType[instr["type"].upper()], Whence.CONST, 0, value
])
elif op == "PUSH_EMPTY":
# Equivalent to PUSHE in LSO. Empties are only really used as placeholders,
# and we don't really care about the type of those. Just use an empty integer
# since it's the smallest.
self.asm_list.append([
OpCode.PUSH, LSLType.INTEGER, Whence.CONST, 1
])
elif op == "JUMP":
self.asm_list.append([
OpCode[op], JumpType[instr["jump_type"].upper()], Label(unique_prefix + instr["label"]),
])
elif op == "POP_N":
self.asm_list.append([
OpCode[op], instr["num"],
])
elif op == "CHANGE_STATE":
self.asm_list.append([
OpCode[op], self.states.index(instr["state"]),
])
elif op == "RET":
self.asm_list.append([
OpCode[op], len(func_data.get("args", [])),
])
elif op in ("TAKE_MEMBER", "REPLACE_MEMBER"):
self.asm_list.append([
OpCode[op], LSLType[instr["type"].upper()], instr["offset"],
])
elif op in ("DUP", "SWAP", "DUMP"):
self.asm_list.append([
OpCode[op],
])
elif op == "CAST":
self.asm_list.append([
OpCode[op], LSLType[instr["from_type"].upper()], LSLType[instr["to_type"].upper()],
])
elif op in ("BOOL", "BUILD_COORD"):
self.asm_list.append([
OpCode[op], LSLType[instr["type"].upper()],
])
elif op == "BUILD_LIST":
self.asm_list.append([
OpCode[op], instr["num_elems"],
])
elif op == "CALL":
self.asm_list.append([
OpCode[op], 0, Label(f"f{instr['name']}"),
])
elif op == "CALL_LIB":
num_args = len(LIBRARY_FUNCS[instr["name"]].arg_types)
self.asm_list.append([
OpCode[op], num_args, instr["name"], 0
])
elif op == "UN_OP":
self.asm_list.append([
OpCode[op], Operation[instr["operation"]], LSLType[instr["type"].upper()],
])
elif op == "BIN_OP":
left_type = LSLType[instr["left_type"].upper()]
right_type = LSLType[instr["right_type"].upper()]
# To simplify things, the interpreter only implements a single variant of equivalent
# expressions where the types can be swapped. Swap the operands on the stack before
# we execute. This ensures we still evaluate the expressions right-to-left like LSL
# demands.
need_swap = False
if left_type == LSLType.FLOAT and right_type == LSLType.VECTOR:
need_swap = True
elif left_type == LSLType.ROTATION and right_type == LSLType.VECTOR:
need_swap = True
if need_swap:
self.asm_list.append([OpCode.SWAP])
left_type, right_type = right_type, left_type
self.asm_list.append([OpCode[op], Operation[instr["operation"]], left_type, right_type])
else:
raise ValueError(f"Unknown instr {instr!r}")
def _fix_whence(self, func_data, instr) -> Tuple[Optional[Whence], Optional[int]]:
if "whence" not in instr:
return None, None
whence = instr["whence"]
index = int(instr["index"])
# Store in retval, actually uses ARG offset in our bytecode
if whence == "RETURN":
whence = "ARG"
# retval goes just past the first arg
index = len(func_data.get("args", []))
elif whence == "ARG":
index = (len(func_data.get("args", [])) - 1) - index
return Whence[whence], index