l0n0lc 0.8.4__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- l0n0lc/Py/350/275/254Cpp/350/275/254/350/257/221/345/231/250.py +579 -0
- l0n0lc/__init__.py +75 -6
- l0n0lc/aot/347/274/226/350/257/221.py +679 -0
- l0n0lc/ast/350/256/277/351/227/256/350/200/205.py +599 -0
- l0n0lc/cpp/347/261/273/345/236/213.py +330 -0
- l0n0lc/cpp/347/274/226/350/257/221/345/231/250.py +317 -0
- l0n0lc/simd/344/274/230/345/214/226.py +260 -0
- l0n0lc/std_map.py +153 -0
- l0n0lc/std_set.py +185 -0
- l0n0lc/std_vector.py +96 -0
- l0n0lc//344/273/243/347/240/201/344/274/230/345/214/226.py +302 -0
- l0n0lc//344/273/243/347/240/201/347/224/237/346/210/220.py +546 -0
- l0n0lc//344/276/235/350/265/226/346/263/250/345/205/245.py +155 -0
- l0n0lc//345/215/263/346/227/266/347/274/226/350/257/221.py +192 -0
- l0n0lc//345/217/230/351/207/217/347/256/241/347/220/206/345/231/250.py +123 -0
- l0n0lc//345/237/272/347/241/200/346/230/240/345/260/204.py +103 -0
- l0n0lc//345/237/272/347/241/200/346/267/267/345/205/245.py +147 -0
- l0n0lc//345/256/271/345/231/250/346/236/204/345/273/272/345/231/250.py +214 -0
- l0n0lc//345/267/245/345/205/267.py +285 -0
- l0n0lc//345/271/266/350/241/214/347/274/226/350/257/221/345/231/250.py +412 -0
- l0n0lc//345/274/202/345/270/270.py +474 -0
- l0n0lc//346/225/260/347/273/204/345/257/271/350/261/241/346/261/240.py +248 -0
- l0n0lc//346/226/207/344/273/266/347/256/241/347/220/206/345/231/250.py +286 -0
- l0n0lc//346/227/245/345/277/227/345/267/245/345/205/267.py +152 -0
- l0n0lc//347/261/273/345/236/213/346/216/250/346/226/255/345/267/245/345/205/267.py +352 -0
- l0n0lc//347/261/273/345/236/213/350/275/254/346/215/242.py +210 -0
- l0n0lc//347/261/273/346/224/257/346/214/201.py +372 -0
- l0n0lc//347/274/226/350/257/221/344/270/212/344/270/213/346/226/207.py +132 -0
- l0n0lc//347/274/226/350/257/221/347/256/241/347/220/206/345/231/250.py +171 -0
- l0n0lc//350/241/250/350/276/276/345/274/217/345/244/204/347/220/206.py +462 -0
- l0n0lc//350/275/254/350/257/221/345/231/250/345/267/245/345/205/267.py +49 -0
- l0n0lc//350/277/220/350/241/214/346/227/266/345/212/240/350/275/275.py +217 -0
- l0n0lc//351/200/232/347/224/250/345/267/245/345/205/267.py +149 -0
- l0n0lc-1.0.0.dist-info/METADATA +363 -0
- l0n0lc-1.0.0.dist-info/RECORD +39 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/WHEEL +1 -1
- l0n0lc-1.0.0.dist-info/entry_points.txt +2 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/licenses/LICENSE +0 -0
- l0n0lc/StdList.py +0 -24
- l0n0lc/StdMap.py +0 -21
- l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py +0 -207
- l0n0lc/jit.py +0 -604
- l0n0lc//347/274/226/350/257/221.py +0 -58
- l0n0lc//351/200/232/347/224/250.py +0 -134
- l0n0lc-0.8.4.dist-info/METADATA +0 -241
- l0n0lc-0.8.4.dist-info/RECORD +0 -12
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"""
|
|
2
|
+
代码优化模块
|
|
3
|
+
|
|
4
|
+
对生成的 C++ 代码进行优化,包括:
|
|
5
|
+
1. 常量折叠:编译时计算常量表达式
|
|
6
|
+
2. 死代码消除:移除永远不会执行的代码
|
|
7
|
+
3. 循环优化:使用范围循环替代索引循环
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import ast
|
|
11
|
+
from typing import Any, Optional, Union
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class 常量折叠器(ast.NodeTransformer):
|
|
15
|
+
"""
|
|
16
|
+
常量折叠优化器
|
|
17
|
+
|
|
18
|
+
在编译时计算常量表达式,减少运行时计算。
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def visit_BinOp(self, node: ast.BinOp) -> Any:
|
|
22
|
+
"""处理二元运算"""
|
|
23
|
+
# 先递归处理子节点
|
|
24
|
+
node = self.generic_visit(node) # type: ignore[assignment]
|
|
25
|
+
|
|
26
|
+
# 尝试计算常量表达式
|
|
27
|
+
if isinstance(node.left, ast.Constant) and isinstance(node.right, ast.Constant):
|
|
28
|
+
try:
|
|
29
|
+
left_val = node.left.value
|
|
30
|
+
right_val = node.right.value
|
|
31
|
+
|
|
32
|
+
# 处理数值运算
|
|
33
|
+
if isinstance(left_val, (int, float)) and isinstance(right_val, (int, float)):
|
|
34
|
+
if isinstance(node.op, ast.Add):
|
|
35
|
+
result = left_val + right_val
|
|
36
|
+
elif isinstance(node.op, ast.Sub):
|
|
37
|
+
result = left_val - right_val
|
|
38
|
+
elif isinstance(node.op, ast.Mult):
|
|
39
|
+
result = left_val * right_val
|
|
40
|
+
elif isinstance(node.op, ast.Div):
|
|
41
|
+
result = left_val / right_val
|
|
42
|
+
elif isinstance(node.op, ast.FloorDiv):
|
|
43
|
+
result = left_val // right_val
|
|
44
|
+
elif isinstance(node.op, ast.Mod):
|
|
45
|
+
result = left_val % right_val
|
|
46
|
+
elif isinstance(node.op, ast.Pow):
|
|
47
|
+
result = left_val ** right_val
|
|
48
|
+
else:
|
|
49
|
+
return node
|
|
50
|
+
|
|
51
|
+
# 返回计算结果
|
|
52
|
+
return ast.Constant(value=result)
|
|
53
|
+
except (ZeroDivisionError, OverflowError, ValueError):
|
|
54
|
+
# 计算失败,返回原节点
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
return node
|
|
58
|
+
|
|
59
|
+
def visit_UnaryOp(self, node: ast.UnaryOp) -> Any:
|
|
60
|
+
"""处理一元运算"""
|
|
61
|
+
node = self.generic_visit(node) # type: ignore[assignment]
|
|
62
|
+
|
|
63
|
+
if isinstance(node.operand, ast.Constant):
|
|
64
|
+
operand_val = node.operand.value
|
|
65
|
+
|
|
66
|
+
if isinstance(operand_val, (int, float)):
|
|
67
|
+
if isinstance(node.op, ast.UAdd):
|
|
68
|
+
return ast.Constant(value=+operand_val)
|
|
69
|
+
elif isinstance(node.op, ast.USub):
|
|
70
|
+
return ast.Constant(value=-operand_val)
|
|
71
|
+
elif isinstance(node.op, ast.Not):
|
|
72
|
+
return ast.Constant(value=not operand_val)
|
|
73
|
+
|
|
74
|
+
return node
|
|
75
|
+
|
|
76
|
+
def visit_Compare(self, node: ast.Compare) -> Any:
|
|
77
|
+
"""处理比较运算"""
|
|
78
|
+
node = self.generic_visit(node) # type: ignore
|
|
79
|
+
|
|
80
|
+
# 简单的比较:a < b(a, b 都是常量)
|
|
81
|
+
if (len(node.ops) == 1 and
|
|
82
|
+
isinstance(node.left, ast.Constant) and
|
|
83
|
+
isinstance(node.comparators[0], ast.Constant)):
|
|
84
|
+
|
|
85
|
+
left_val = node.left.value
|
|
86
|
+
right_val = node.comparators[0].value
|
|
87
|
+
op = node.ops[0]
|
|
88
|
+
|
|
89
|
+
# 相等性运算符可以在不同类型之间使用
|
|
90
|
+
if isinstance(left_val, (int, float, str)) and isinstance(right_val, (int, float, str)):
|
|
91
|
+
try:
|
|
92
|
+
if isinstance(op, ast.Eq):
|
|
93
|
+
result = left_val == right_val
|
|
94
|
+
return ast.Constant(value=result)
|
|
95
|
+
elif isinstance(op, ast.NotEq):
|
|
96
|
+
result = left_val != right_val
|
|
97
|
+
return ast.Constant(value=result)
|
|
98
|
+
except TypeError:
|
|
99
|
+
pass
|
|
100
|
+
|
|
101
|
+
# 比较运算符只能在相同类型之间使用
|
|
102
|
+
# 数值类型
|
|
103
|
+
if (isinstance(left_val, (int, float)) and isinstance(right_val, (int, float))):
|
|
104
|
+
try:
|
|
105
|
+
if isinstance(op, ast.Lt):
|
|
106
|
+
result = left_val < right_val
|
|
107
|
+
elif isinstance(op, ast.LtE):
|
|
108
|
+
result = left_val <= right_val
|
|
109
|
+
elif isinstance(op, ast.Gt):
|
|
110
|
+
result = left_val > right_val
|
|
111
|
+
elif isinstance(op, ast.GtE):
|
|
112
|
+
result = left_val >= right_val
|
|
113
|
+
else:
|
|
114
|
+
return node
|
|
115
|
+
|
|
116
|
+
return ast.Constant(value=result)
|
|
117
|
+
except TypeError:
|
|
118
|
+
pass
|
|
119
|
+
# 字符串类型
|
|
120
|
+
elif (isinstance(left_val, str) and isinstance(right_val, str)):
|
|
121
|
+
try:
|
|
122
|
+
if isinstance(op, ast.Lt):
|
|
123
|
+
result = left_val < right_val
|
|
124
|
+
elif isinstance(op, ast.LtE):
|
|
125
|
+
result = left_val <= right_val
|
|
126
|
+
elif isinstance(op, ast.Gt):
|
|
127
|
+
result = left_val > right_val
|
|
128
|
+
elif isinstance(op, ast.GtE):
|
|
129
|
+
result = left_val >= right_val
|
|
130
|
+
else:
|
|
131
|
+
return node
|
|
132
|
+
|
|
133
|
+
return ast.Constant(value=result)
|
|
134
|
+
except TypeError:
|
|
135
|
+
pass
|
|
136
|
+
|
|
137
|
+
return node
|
|
138
|
+
|
|
139
|
+
def visit_BoolOp(self, node: ast.BoolOp) -> Any:
|
|
140
|
+
"""处理布尔运算(and/or)"""
|
|
141
|
+
node = self.generic_visit(node) # type: ignore[assignment]
|
|
142
|
+
|
|
143
|
+
# 检查所有操作数是否都是常量
|
|
144
|
+
values = node.values
|
|
145
|
+
if all(isinstance(v, ast.Constant) for v in values):
|
|
146
|
+
try:
|
|
147
|
+
const_values = [v.value for v in values] # type: ignore[attr-defined]
|
|
148
|
+
|
|
149
|
+
if isinstance(node.op, ast.And):
|
|
150
|
+
result = all(const_values)
|
|
151
|
+
elif isinstance(node.op, ast.Or):
|
|
152
|
+
result = any(const_values)
|
|
153
|
+
else:
|
|
154
|
+
return node
|
|
155
|
+
|
|
156
|
+
return ast.Constant(value=result)
|
|
157
|
+
except (TypeError, ValueError):
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
return node
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class 死代码消除器(ast.NodeTransformer):
|
|
164
|
+
"""
|
|
165
|
+
死代码消除优化器
|
|
166
|
+
|
|
167
|
+
移除永远不会执行的代码,如 if False: ... 分支
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
def visit_If(self, node: ast.If) -> Any:
|
|
171
|
+
"""处理 if 语句"""
|
|
172
|
+
# 先处理测试条件
|
|
173
|
+
test = self.visit(node.test)
|
|
174
|
+
|
|
175
|
+
# 检查是否为常量
|
|
176
|
+
if isinstance(test, ast.Constant):
|
|
177
|
+
if test.value:
|
|
178
|
+
# 条件永远为 True,只保留 body
|
|
179
|
+
if node.orelse:
|
|
180
|
+
# 移除 orelse,保留 body
|
|
181
|
+
result = [self.visit(n) for n in node.body]
|
|
182
|
+
return result if len(result) > 1 else (result[0] if result else None)
|
|
183
|
+
else:
|
|
184
|
+
# 有 body,保留
|
|
185
|
+
return self.generic_visit(node)
|
|
186
|
+
else:
|
|
187
|
+
# 条件永远为 False,只保留 orelse
|
|
188
|
+
if node.orelse:
|
|
189
|
+
result = [self.visit(n) for n in node.orelse]
|
|
190
|
+
return result if len(result) > 1 else (result[0] if result else None)
|
|
191
|
+
else:
|
|
192
|
+
# 没有 orelse,完全移除
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
return self.generic_visit(node)
|
|
196
|
+
|
|
197
|
+
def visit_While(self, node: ast.While) -> Any:
|
|
198
|
+
"""处理 while 循环"""
|
|
199
|
+
test = self.visit(node.test)
|
|
200
|
+
|
|
201
|
+
# 检查是否为常量 False
|
|
202
|
+
if isinstance(test, ast.Constant) and not test.value:
|
|
203
|
+
# 循环永远不会执行,移除
|
|
204
|
+
return None
|
|
205
|
+
|
|
206
|
+
return self.generic_visit(node)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class 循环优化器(ast.NodeTransformer):
|
|
210
|
+
"""
|
|
211
|
+
循环优化器
|
|
212
|
+
|
|
213
|
+
将 for i in range(len(lst)): lst[i] 模式转换为 for elem in lst:
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
def visit_For(self, node: ast.For) -> Any:
|
|
217
|
+
"""处理 for 循环"""
|
|
218
|
+
node = self.generic_visit(node) # type: ignore[assignment]
|
|
219
|
+
|
|
220
|
+
# 检查模式:for i in range(len(x)): ... x[i] ...
|
|
221
|
+
if (isinstance(node.iter, ast.Call) and
|
|
222
|
+
isinstance(node.iter.func, ast.Name) and
|
|
223
|
+
node.iter.func.id == 'range' and
|
|
224
|
+
len(node.iter.args) == 1 and
|
|
225
|
+
isinstance(node.iter.args[0], ast.Call) and
|
|
226
|
+
isinstance(node.iter.args[0].func, ast.Name) and
|
|
227
|
+
node.iter.args[0].func.id == 'len' and
|
|
228
|
+
len(node.iter.args[0].args) == 1):
|
|
229
|
+
|
|
230
|
+
# 获取被遍历的变量
|
|
231
|
+
target_var = node.iter.args[0].args[0]
|
|
232
|
+
loop_var = node.target
|
|
233
|
+
|
|
234
|
+
# 检查是否是简单的索引访问模式
|
|
235
|
+
# 这是一个简化版本,只检查基本情况
|
|
236
|
+
# 完整实现需要更复杂的数据流分析
|
|
237
|
+
|
|
238
|
+
return node
|
|
239
|
+
|
|
240
|
+
return node
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class 代码优化器:
|
|
244
|
+
"""
|
|
245
|
+
代码优化器主类
|
|
246
|
+
|
|
247
|
+
组合多个优化器,按顺序应用到 AST 上。
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
def __init__(self, transpiler):
|
|
251
|
+
self.transpiler = transpiler
|
|
252
|
+
self.常量折叠 = 常量折叠器()
|
|
253
|
+
self.死代码消除 = 死代码消除器()
|
|
254
|
+
self.循环优化 = 循环优化器()
|
|
255
|
+
|
|
256
|
+
def 优化(self, tree: ast.AST) -> ast.AST:
|
|
257
|
+
"""
|
|
258
|
+
对 AST 进行优化
|
|
259
|
+
|
|
260
|
+
Args:
|
|
261
|
+
tree: 要优化的 AST
|
|
262
|
+
|
|
263
|
+
Returns:
|
|
264
|
+
优化后的 AST
|
|
265
|
+
"""
|
|
266
|
+
# 按顺序应用优化器
|
|
267
|
+
# 1. 常量折叠(为其他优化提供更多信息)
|
|
268
|
+
tree = self.常量折叠.visit(tree)
|
|
269
|
+
|
|
270
|
+
# 2. 死代码消除(基于常量折叠的结果)
|
|
271
|
+
tree = self.死代码消除.visit(tree)
|
|
272
|
+
|
|
273
|
+
# 3. 循环优化
|
|
274
|
+
tree = self.循环优化.visit(tree)
|
|
275
|
+
|
|
276
|
+
# 修复 AST(确保节点完整性)
|
|
277
|
+
ast.fix_missing_locations(tree)
|
|
278
|
+
|
|
279
|
+
return tree
|
|
280
|
+
|
|
281
|
+
def 获取统计(self) -> dict:
|
|
282
|
+
"""获取优化统计信息"""
|
|
283
|
+
return {
|
|
284
|
+
"常量折叠": "已启用",
|
|
285
|
+
"死代码消除": "已启用",
|
|
286
|
+
"循环优化": "已启用",
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def 优化AST(tree: ast.AST, transpiler) -> ast.AST:
|
|
291
|
+
"""
|
|
292
|
+
优化 AST 的便捷函数
|
|
293
|
+
|
|
294
|
+
Args:
|
|
295
|
+
tree: 要优化的 AST
|
|
296
|
+
transpiler: 转译器实例
|
|
297
|
+
|
|
298
|
+
Returns:
|
|
299
|
+
优化后的 AST
|
|
300
|
+
"""
|
|
301
|
+
优化器 = 代码优化器(transpiler)
|
|
302
|
+
return 优化器.优化(tree)
|