-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoders.py
668 lines (435 loc) · 16.9 KB
/
encoders.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/bin/env python
"""
Encoder functions, producing representations of program objects.
Copyright (C) 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
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 3 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, see <http://www.gnu.org/licenses/>.
"""
from common import first, InstructionSequence
# Value digest computation.
from base64 import b64encode
from hashlib import sha1
def digest(values):
m = sha1()
for value in values:
m.update(str(value))
return b64encode(m.digest()).replace("+", "__").replace("/", "_").rstrip("=")
# Output encoding and decoding for the summary files.
def encode_attrnames(attrnames):
"Encode the 'attrnames' representing usage."
return ", ".join(attrnames) or "{}"
def encode_constrained(constrained):
"Encode the 'constrained' status for program summaries."
return constrained and "constrained" or "deduced"
def encode_usage(usage):
"Encode attribute details from 'usage'."
all_attrnames = []
for t in usage:
attrname, invocation, assignment = t
all_attrnames.append("%s%s" % (attrname, invocation and "!" or assignment and "=" or ""))
return ", ".join(all_attrnames) or "{}"
def decode_usage(s):
"Decode attribute details from 's'."
all_attrnames = set()
for attrname_str in s.split(", "):
all_attrnames.add((attrname_str.rstrip("!="), attrname_str.endswith("!"), attrname_str.endswith("=")))
all_attrnames = list(all_attrnames)
all_attrnames.sort()
return tuple(all_attrnames)
def encode_access_location(t):
"Encode the access location 't'."
return "%s:%s:%s:%d" % (t.path, t.name or "{}", t.attrnames or "{}", t.access_number)
def decode_access_location(s):
"Decode the access location 's'."
path, name, attrnames, access_number = s.split(":")
return path, name, attrnames, access_number
def encode_alias_location(t, invocation=False):
"Encode the alias location 't'."
return "%s:%s:%s%s%s%s" % (t.path, t.name or "{}", t.attrnames or "{}",
t.version is not None and ":=%d" % t.version or "",
t.access_number is not None and ":#%d" % t.access_number or "",
invocation and "!" or "")
def decode_alias_location(s):
"Decode the alias location 's'."
path, name, rest = s.split(":", 2)
attrnames = version = access_number = None
invocation = rest.endswith("!")
t = rest.rstrip("!").split(":#")
if len(t) > 1:
rest = t[0]; access_number = int(t[1])
t = rest.split(":=")
if len(t) > 1:
attrnames = t[0]; version = int(t[1])
else:
attrnames = rest
return path, name, attrnames, version, access_number, invocation
def encode_location(t):
"Encode the general location 't' in a concise form."
if t.name is not None and t.version is not None:
return "%s:%s:%d" % (t.path, t.name, t.version)
elif t.name is not None:
return "%s:%s" % (t.path, t.name)
else:
return "%s::%s" % (t.path, t.attrnames)
def encode_modifiers(modifiers):
"Encode assignment and invocation details from 'modifiers'."
all_modifiers = []
for t in modifiers:
all_modifiers.append(encode_modifier_term(t))
return "".join(all_modifiers)
def encode_modifier_term(t):
"Encode modifier 't' representing an assignment or an invocation."
assignment, invocation = t
if assignment:
return "="
elif invocation is not None:
arguments, keywords = invocation
return "(%d;%s)" % (arguments, ",".join(keywords))
else:
return "_"
def decode_modifiers(s):
"Decode 's' containing modifiers."
i = 0
end = len(s)
modifiers = []
while i < end:
if s[i] == "=":
modifiers.append((True, None))
i += 1
elif s[i] == "(":
j = s.index(";", i)
arguments = int(s[i+1:j])
i = j
j = s.index(")", i)
keywords = s[i+1:j]
keywords = keywords and keywords.split(",") or []
modifiers.append((False, (arguments, keywords)))
i = j + 1
else:
modifiers.append((False, None))
i += 1
return modifiers
# Test generation functions.
def get_kinds(all_types):
"""
Return object kind details for 'all_types', being a collection of
references for program types.
"""
return map(lambda ref: ref.get_kind(), all_types)
def test_label_for_kind(kind):
"Return the label used for 'kind' in test details."
return kind == "<instance>" and "instance" or "type"
def test_label_for_type(ref):
"Return the label used for 'ref' in test details."
return test_label_for_kind(ref.get_kind())
# Instruction representation encoding.
def encode_instruction(instruction):
"""
Encode the 'instruction' - a sequence starting with an operation and
followed by arguments, each of which may be an instruction sequence or a
plain value - to produce a function call string representation.
"""
op = instruction[0]
args = instruction[1:]
if args:
a = []
for arg in args:
if isinstance(arg, tuple):
a.append(encode_instruction(arg))
else:
a.append(arg or "{}")
argstr = "(%s)" % ", ".join(a)
return "%s%s" % (op, argstr)
else:
return op
# Output program encoding.
attribute_loading_ops = (
"__load_via_class", "__load_via_object", "__get_class_and_load",
)
attribute_ops = attribute_loading_ops + (
"__store_via_class", "__store_via_object",
)
checked_loading_ops = (
"__check_and_load_via_class", "__check_and_load_via_object", "__check_and_load_via_any",
)
checked_ops = checked_loading_ops + (
"__check_and_store_via_class", "__check_and_store_via_object", "__check_and_store_via_any",
)
typename_ops = (
"__test_common_instance", "__test_common_object", "__test_common_type",
)
type_ops = (
"__test_specific_instance", "__test_specific_object", "__test_specific_type",
)
static_ops = (
"__load_static_ignore", "__load_static_replace", "__load_static_test", "<test_context_static>",
)
accessor_values = (
"<accessor>",
)
accessor_ops = (
"<accessor>", "<set_accessor>",
)
context_values = (
"<context>",
)
context_ops = (
"<context>", "<set_context>", "<test_context_revert>", "<test_context_static>",
)
context_op_functions = (
"<test_context_revert>", "<test_context_static>",
)
reference_acting_ops = attribute_ops + checked_ops + type_ops + typename_ops
attribute_producing_ops = attribute_loading_ops + checked_loading_ops
attribute_producing_variables = (
"<accessor>", "<context>", "<name>", "<private_context>", "<target_accessor>"
)
def encode_access_instruction(instruction, subs, accessor_index, context_index):
"""
Encode the 'instruction' - a sequence starting with an operation and
followed by arguments, each of which may be an instruction sequence or a
plain value - to produce a function call string representation.
The 'subs' parameter defines a mapping of substitutions for special values
used in instructions.
The 'accessor_index' parameter defines the position in local accessor
storage for the referenced accessor or affected by an accessor operation.
The 'context_index' parameter defines the position in local context storage
for the referenced context or affected by a context operation.
Return both the encoded instruction and a collection of substituted names.
"""
op = instruction[0]
args = instruction[1:]
substituted = set()
# Encode the arguments.
a = []
if args:
converting_op = op
for arg in args:
s, _substituted = encode_access_instruction_arg(arg, subs, converting_op, accessor_index, context_index)
substituted.update(_substituted)
a.append(s)
converting_op = None
# Modify certain arguments.
# Convert type name arguments.
if op in typename_ops:
a[1] = encode_path(encode_type_attribute(args[1]))
# Obtain addresses of type arguments.
elif op in type_ops:
a[1] = "&%s" % a[1]
# Obtain addresses of static objects.
elif op in static_ops:
a[-1] = "&%s" % a[-1]
# Add accessor storage information to certain operations.
if op in accessor_ops:
a.insert(0, accessor_index)
# Add context storage information to certain operations.
if op in context_ops:
a.insert(0, context_index)
# Add the local context array to certain operations.
if op in context_op_functions:
a.append("__tmp_contexts")
# Define any argument string.
if a:
argstr = "(%s)" % ", ".join(map(str, a))
else:
argstr = ""
# Substitute the first element of the instruction, which may not be an
# operation at all.
if subs.has_key(op):
substituted.add(op)
# Break accessor initialisation into initialisation and value-yielding
# parts:
if op == "<set_accessor>" and isinstance(a[0], InstructionSequence):
ops = []
ops += a[0].get_init_instructions()
ops.append("%s(%s)" % (subs[op], a[0].get_value_instruction()))
return ", ".join(map(str, ops)), substituted
op = subs[op]
elif not args:
op = "&%s" % encode_path(op)
return "%s%s" % (op, argstr), substituted
def encode_access_instruction_arg(arg, subs, op, accessor_index, context_index):
"""
Encode 'arg' using 'subs' to define substitutions, 'op' to indicate the
operation to which the argument belongs, and with 'accessor_index' and
'context_index' indicating any affected accessor and context storage.
Return a tuple containing the encoded form of 'arg' along with a collection
of any substituted values.
"""
if isinstance(arg, tuple):
encoded, substituted = encode_access_instruction(arg, subs, accessor_index, context_index)
return attribute_to_reference(op, arg[0], encoded, substituted)
# Special values only need replacing, not encoding.
elif subs.has_key(arg):
# Handle values modified by storage details.
if arg in accessor_values or arg in context_values:
encoded = "%s(%s)" % (subs.get(arg), context_index)
else:
encoded = subs.get(arg)
substituted = set([arg])
return attribute_to_reference(op, arg, encoded, substituted)
# Convert static references to the appropriate type.
elif op and op in reference_acting_ops and \
arg not in attribute_producing_variables:
return "&%s" % encode_path(arg), set()
# Other values may need encoding.
else:
return encode_path(arg), set()
def attribute_to_reference(op, arg, encoded, substituted):
# Convert attribute results to references where required.
if op and op in reference_acting_ops and (
arg in attribute_producing_ops or
arg in attribute_producing_variables):
return "__VALUE(%s)" % encoded, substituted
else:
return encoded, substituted
def encode_function_pointer(path):
"Encode 'path' as a reference to an output program function."
return "__fn_%s" % encode_path(path)
def encode_instantiator_pointer(path):
"Encode 'path' as a reference to an output program instantiator."
return "__new_%s" % encode_path(path)
def encode_instructions(instructions):
"Encode 'instructions' as a sequence."
if len(instructions) == 1:
return instructions[0]
else:
return "(\n%s\n)" % ",\n".join(instructions)
def encode_literal_constant(n):
"Encode a name for the literal constant with the number 'n'."
return "__const%s" % n
def encode_literal_constant_size(value):
"Encode a size for the literal constant with the given 'value'."
if isinstance(value, basestring):
return len(value)
else:
return 0
def encode_literal_constant_member(value):
"Encode the member name for the 'value' in the final program."
return "%svalue" % value.__class__.__name__
def encode_literal_constant_value(value):
"Encode the given 'value' in the final program."
if isinstance(value, (int, float)):
return str(value)
else:
l = []
# Encode characters including non-ASCII ones.
for c in str(value):
if c == '"': l.append('\\"')
elif c == '\n': l.append('\\n')
elif c == '\t': l.append('\\t')
elif c == '\r': l.append('\\r')
elif c == '\\': l.append('\\\\')
elif 0x20 <= ord(c) < 0x80: l.append(c)
else: l.append("\\x%02x" % ord(c))
return '"%s"' % "".join(l)
def encode_literal_data_initialiser(style):
"""
Encode a reference to a function populating the data for a literal having
the given 'style' ("mapping" or "sequence").
"""
return "__newdata_%s" % style
def encode_literal_instantiator(path):
"""
Encode a reference to an instantiator for a literal having the given 'path'.
"""
return "__newliteral_%s" % encode_path(path)
def encode_literal_reference(n):
"Encode a reference to a literal constant with the number 'n'."
return "__constvalue%s" % n
# Track all encoded paths, detecting and avoiding conflicts.
all_encoded_paths = {}
def encode_path(path):
"Encode 'path' as an output program object, translating special symbols."
if path in reserved_words:
return "__%s" % path
else:
part_encoded = path.replace("#", "__").replace("$", "__")
if "." not in path:
return part_encoded
encoded = part_encoded.replace(".", "_")
# Test for a conflict with the encoding of a different path, re-encoding
# if necessary.
previous = all_encoded_paths.get(encoded)
replacement = "_"
while previous:
if path == previous:
return encoded
replacement += "_"
encoded = part_encoded.replace(".", replacement)
previous = all_encoded_paths.get(encoded)
# Store any new or re-encoded path.
all_encoded_paths[encoded] = path
return encoded
def encode_code(name):
"Encode 'name' as an attribute code indicator."
return "__ATTRCODE(%s)" % encode_path(name)
def encode_pcode(name):
"Encode 'name' as an parameter code indicator."
return "__PARAMCODE(%s)" % encode_path(name)
def encode_pos(name):
"Encode 'name' as an attribute position indicator."
return "__ATTRPOS(%s)" % encode_path(name)
def encode_ppos(name):
"Encode 'name' as an parameter position indicator."
return "__PARAMPOS(%s)" % encode_path(name)
def encode_predefined_reference(path):
"Encode a reference to a predefined constant value for 'path'."
return "__predefined_%s" % encode_path(path)
def encode_size(kind, path=None):
"""
Encode a structure size reference for the given 'kind' of structure, with
'path' indicating a specific structure name.
"""
return "__%ssize%s" % (structure_size_prefixes.get(kind, kind), path and "_%s" % encode_path(path) or "")
def encode_symbol(symbol_type, path=None):
"Encode a symbol with the given 'symbol_type' and optional 'path'."
return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "")
def encode_tablename(kind, path):
"""
Encode a table reference for the given 'kind' of table structure, indicating
a 'path' for the specific object concerned.
"""
return "__%sTable_%s" % (table_name_prefixes[kind], encode_path(path))
def encode_type_attribute(path):
"Encode the special type attribute for 'path'."
return "#%s" % path
def decode_type_attribute(s):
"Decode the special type attribute 's'."
return s[1:]
def is_type_attribute(s):
"Return whether 's' is a type attribute name."
return s.startswith("#")
# A mapping from kinds to structure size reference prefixes.
structure_size_prefixes = {
"<class>" : "c",
"<module>" : "m",
"<instance>" : "i"
}
# A mapping from kinds to table name prefixes.
table_name_prefixes = {
"<class>" : "Class",
"<function>" : "Function",
"<module>" : "Module",
"<instance>" : "Instance"
}
# Output language reserved words.
reserved_words = [
"break", "char", "const", "continue",
"default", "double", "else",
"float", "for",
"if", "int", "long",
"NULL",
"return", "struct",
"typedef",
"void", "while",
]
# vim: tabstop=4 expandtab shiftwidth=4