paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Encoder functions, producing representations of program objects. |
paul@0 | 5 | |
paul@833 | 6 | Copyright (C) 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 20 | """ |
paul@0 | 21 | |
paul@498 | 22 | from common import first, InstructionSequence |
paul@56 | 23 | |
paul@609 | 24 | |
paul@609 | 25 | |
paul@609 | 26 | # Value digest computation. |
paul@609 | 27 | |
paul@609 | 28 | from base64 import b64encode |
paul@609 | 29 | from hashlib import sha1 |
paul@609 | 30 | |
paul@609 | 31 | def digest(values): |
paul@609 | 32 | m = sha1() |
paul@609 | 33 | for value in values: |
paul@609 | 34 | m.update(str(value)) |
paul@609 | 35 | return b64encode(m.digest()).replace("+", "__").replace("/", "_").rstrip("=") |
paul@609 | 36 | |
paul@609 | 37 | |
paul@609 | 38 | |
paul@0 | 39 | # Output encoding and decoding for the summary files. |
paul@0 | 40 | |
paul@0 | 41 | def encode_attrnames(attrnames): |
paul@0 | 42 | |
paul@0 | 43 | "Encode the 'attrnames' representing usage." |
paul@0 | 44 | |
paul@0 | 45 | return ", ".join(attrnames) or "{}" |
paul@0 | 46 | |
paul@0 | 47 | def encode_constrained(constrained): |
paul@0 | 48 | |
paul@0 | 49 | "Encode the 'constrained' status for program summaries." |
paul@0 | 50 | |
paul@0 | 51 | return constrained and "constrained" or "deduced" |
paul@0 | 52 | |
paul@0 | 53 | def encode_usage(usage): |
paul@0 | 54 | |
paul@0 | 55 | "Encode attribute details from 'usage'." |
paul@0 | 56 | |
paul@0 | 57 | all_attrnames = [] |
paul@0 | 58 | for t in usage: |
paul@107 | 59 | attrname, invocation, assignment = t |
paul@107 | 60 | all_attrnames.append("%s%s" % (attrname, invocation and "!" or assignment and "=" or "")) |
paul@0 | 61 | return ", ".join(all_attrnames) or "{}" |
paul@0 | 62 | |
paul@88 | 63 | def decode_usage(s): |
paul@88 | 64 | |
paul@88 | 65 | "Decode attribute details from 's'." |
paul@88 | 66 | |
paul@88 | 67 | all_attrnames = set() |
paul@88 | 68 | for attrname_str in s.split(", "): |
paul@107 | 69 | all_attrnames.add((attrname_str.rstrip("!="), attrname_str.endswith("!"), attrname_str.endswith("="))) |
paul@88 | 70 | |
paul@88 | 71 | all_attrnames = list(all_attrnames) |
paul@88 | 72 | all_attrnames.sort() |
paul@88 | 73 | return tuple(all_attrnames) |
paul@88 | 74 | |
paul@0 | 75 | def encode_access_location(t): |
paul@0 | 76 | |
paul@0 | 77 | "Encode the access location 't'." |
paul@0 | 78 | |
paul@791 | 79 | return "%s:%s:%s:%d" % (t.path, t.name or "{}", t.attrnames or "{}", t.access_number) |
paul@0 | 80 | |
paul@841 | 81 | def decode_access_location(s): |
paul@841 | 82 | |
paul@841 | 83 | "Decode the access location 's'." |
paul@841 | 84 | |
paul@841 | 85 | path, name, attrnames, access_number = s.split(":") |
paul@841 | 86 | return path, name, attrnames, access_number |
paul@841 | 87 | |
paul@789 | 88 | def encode_alias_location(t, invocation=False): |
paul@789 | 89 | |
paul@789 | 90 | "Encode the alias location 't'." |
paul@789 | 91 | |
paul@791 | 92 | return "%s:%s:%s%s%s%s" % (t.path, t.name or "{}", t.attrnames or "{}", |
paul@791 | 93 | t.version is not None and ":=%d" % t.version or "", |
paul@791 | 94 | t.access_number is not None and ":#%d" % t.access_number or "", |
paul@791 | 95 | invocation and "!" or "") |
paul@789 | 96 | |
paul@833 | 97 | def decode_alias_location(s): |
paul@833 | 98 | |
paul@833 | 99 | "Decode the alias location 's'." |
paul@833 | 100 | |
paul@833 | 101 | path, name, rest = s.split(":", 2) |
paul@833 | 102 | attrnames = version = access_number = None |
paul@833 | 103 | invocation = rest.endswith("!") |
paul@833 | 104 | |
paul@833 | 105 | t = rest.rstrip("!").split(":#") |
paul@833 | 106 | if len(t) > 1: |
paul@833 | 107 | rest = t[0]; access_number = int(t[1]) |
paul@833 | 108 | |
paul@833 | 109 | t = rest.split(":=") |
paul@833 | 110 | if len(t) > 1: |
paul@833 | 111 | attrnames = t[0]; version = int(t[1]) |
paul@833 | 112 | else: |
paul@833 | 113 | attrnames = rest |
paul@833 | 114 | |
paul@833 | 115 | return path, name, attrnames, version, access_number, invocation |
paul@833 | 116 | |
paul@0 | 117 | def encode_location(t): |
paul@0 | 118 | |
paul@0 | 119 | "Encode the general location 't' in a concise form." |
paul@0 | 120 | |
paul@791 | 121 | if t.name is not None and t.version is not None: |
paul@791 | 122 | return "%s:%s:%d" % (t.path, t.name, t.version) |
paul@791 | 123 | elif t.name is not None: |
paul@791 | 124 | return "%s:%s" % (t.path, t.name) |
paul@0 | 125 | else: |
paul@791 | 126 | return "%s::%s" % (t.path, t.attrnames) |
paul@0 | 127 | |
paul@0 | 128 | def encode_modifiers(modifiers): |
paul@0 | 129 | |
paul@553 | 130 | "Encode assignment and invocation details from 'modifiers'." |
paul@0 | 131 | |
paul@0 | 132 | all_modifiers = [] |
paul@0 | 133 | for t in modifiers: |
paul@0 | 134 | all_modifiers.append(encode_modifier_term(t)) |
paul@0 | 135 | return "".join(all_modifiers) |
paul@0 | 136 | |
paul@0 | 137 | def encode_modifier_term(t): |
paul@0 | 138 | |
paul@553 | 139 | "Encode modifier 't' representing an assignment or an invocation." |
paul@0 | 140 | |
paul@117 | 141 | assignment, invocation = t |
paul@553 | 142 | if assignment: |
paul@553 | 143 | return "=" |
paul@553 | 144 | elif invocation is not None: |
paul@557 | 145 | arguments, keywords = invocation |
paul@557 | 146 | return "(%d;%s)" % (arguments, ",".join(keywords)) |
paul@553 | 147 | else: |
paul@553 | 148 | return "_" |
paul@0 | 149 | |
paul@553 | 150 | def decode_modifiers(s): |
paul@553 | 151 | |
paul@553 | 152 | "Decode 's' containing modifiers." |
paul@553 | 153 | |
paul@553 | 154 | i = 0 |
paul@553 | 155 | end = len(s) |
paul@0 | 156 | |
paul@553 | 157 | modifiers = [] |
paul@0 | 158 | |
paul@553 | 159 | while i < end: |
paul@553 | 160 | if s[i] == "=": |
paul@553 | 161 | modifiers.append((True, None)) |
paul@553 | 162 | i += 1 |
paul@553 | 163 | elif s[i] == "(": |
paul@557 | 164 | j = s.index(";", i) |
paul@557 | 165 | arguments = int(s[i+1:j]) |
paul@557 | 166 | i = j |
paul@553 | 167 | j = s.index(")", i) |
paul@557 | 168 | keywords = s[i+1:j] |
paul@557 | 169 | keywords = keywords and keywords.split(",") or [] |
paul@557 | 170 | modifiers.append((False, (arguments, keywords))) |
paul@553 | 171 | i = j + 1 |
paul@553 | 172 | else: |
paul@553 | 173 | modifiers.append((False, None)) |
paul@553 | 174 | i += 1 |
paul@553 | 175 | |
paul@553 | 176 | return modifiers |
paul@0 | 177 | |
paul@56 | 178 | |
paul@56 | 179 | |
paul@56 | 180 | # Test generation functions. |
paul@56 | 181 | |
paul@56 | 182 | def get_kinds(all_types): |
paul@56 | 183 | |
paul@56 | 184 | """ |
paul@56 | 185 | Return object kind details for 'all_types', being a collection of |
paul@56 | 186 | references for program types. |
paul@56 | 187 | """ |
paul@56 | 188 | |
paul@56 | 189 | return map(lambda ref: ref.get_kind(), all_types) |
paul@56 | 190 | |
paul@237 | 191 | def test_label_for_kind(kind): |
paul@56 | 192 | |
paul@237 | 193 | "Return the label used for 'kind' in test details." |
paul@56 | 194 | |
paul@237 | 195 | return kind == "<instance>" and "instance" or "type" |
paul@56 | 196 | |
paul@237 | 197 | def test_label_for_type(ref): |
paul@56 | 198 | |
paul@237 | 199 | "Return the label used for 'ref' in test details." |
paul@56 | 200 | |
paul@237 | 201 | return test_label_for_kind(ref.get_kind()) |
paul@56 | 202 | |
paul@56 | 203 | |
paul@56 | 204 | |
paul@94 | 205 | # Instruction representation encoding. |
paul@94 | 206 | |
paul@94 | 207 | def encode_instruction(instruction): |
paul@94 | 208 | |
paul@94 | 209 | """ |
paul@94 | 210 | Encode the 'instruction' - a sequence starting with an operation and |
paul@94 | 211 | followed by arguments, each of which may be an instruction sequence or a |
paul@94 | 212 | plain value - to produce a function call string representation. |
paul@94 | 213 | """ |
paul@94 | 214 | |
paul@94 | 215 | op = instruction[0] |
paul@94 | 216 | args = instruction[1:] |
paul@94 | 217 | |
paul@94 | 218 | if args: |
paul@94 | 219 | a = [] |
paul@113 | 220 | for arg in args: |
paul@113 | 221 | if isinstance(arg, tuple): |
paul@113 | 222 | a.append(encode_instruction(arg)) |
paul@94 | 223 | else: |
paul@113 | 224 | a.append(arg or "{}") |
paul@94 | 225 | argstr = "(%s)" % ", ".join(a) |
paul@94 | 226 | return "%s%s" % (op, argstr) |
paul@94 | 227 | else: |
paul@94 | 228 | return op |
paul@94 | 229 | |
paul@94 | 230 | |
paul@94 | 231 | |
paul@0 | 232 | # Output program encoding. |
paul@0 | 233 | |
paul@153 | 234 | attribute_loading_ops = ( |
paul@153 | 235 | "__load_via_class", "__load_via_object", "__get_class_and_load", |
paul@153 | 236 | ) |
paul@153 | 237 | |
paul@153 | 238 | attribute_ops = attribute_loading_ops + ( |
paul@815 | 239 | "__store_via_class", "__store_via_object", |
paul@113 | 240 | ) |
paul@113 | 241 | |
paul@153 | 242 | checked_loading_ops = ( |
paul@113 | 243 | "__check_and_load_via_class", "__check_and_load_via_object", "__check_and_load_via_any", |
paul@153 | 244 | ) |
paul@153 | 245 | |
paul@153 | 246 | checked_ops = checked_loading_ops + ( |
paul@113 | 247 | "__check_and_store_via_class", "__check_and_store_via_object", "__check_and_store_via_any", |
paul@113 | 248 | ) |
paul@113 | 249 | |
paul@113 | 250 | typename_ops = ( |
paul@144 | 251 | "__test_common_instance", "__test_common_object", "__test_common_type", |
paul@113 | 252 | ) |
paul@113 | 253 | |
paul@385 | 254 | type_ops = ( |
paul@385 | 255 | "__test_specific_instance", "__test_specific_object", "__test_specific_type", |
paul@385 | 256 | ) |
paul@385 | 257 | |
paul@141 | 258 | static_ops = ( |
paul@595 | 259 | "__load_static_ignore", "__load_static_replace", "__load_static_test", "<test_context_static>", |
paul@141 | 260 | ) |
paul@141 | 261 | |
paul@591 | 262 | context_values = ( |
paul@591 | 263 | "<context>", |
paul@591 | 264 | ) |
paul@591 | 265 | |
paul@591 | 266 | context_ops = ( |
paul@601 | 267 | "<context>", "<set_context>", "<test_context_revert>", "<test_context_static>", |
paul@591 | 268 | ) |
paul@591 | 269 | |
paul@602 | 270 | context_op_functions = ( |
paul@602 | 271 | "<test_context_revert>", "<test_context_static>", |
paul@602 | 272 | ) |
paul@602 | 273 | |
paul@757 | 274 | reference_acting_ops = attribute_ops + checked_ops + type_ops + typename_ops |
paul@153 | 275 | attribute_producing_ops = attribute_loading_ops + checked_loading_ops |
paul@153 | 276 | |
paul@757 | 277 | attribute_producing_variables = ( |
paul@757 | 278 | "<accessor>", "<context>", "<name>", "<private_context>", "<target_accessor>" |
paul@757 | 279 | ) |
paul@757 | 280 | |
paul@591 | 281 | def encode_access_instruction(instruction, subs, context_index): |
paul@113 | 282 | |
paul@113 | 283 | """ |
paul@113 | 284 | Encode the 'instruction' - a sequence starting with an operation and |
paul@113 | 285 | followed by arguments, each of which may be an instruction sequence or a |
paul@113 | 286 | plain value - to produce a function call string representation. |
paul@113 | 287 | |
paul@113 | 288 | The 'subs' parameter defines a mapping of substitutions for special values |
paul@113 | 289 | used in instructions. |
paul@482 | 290 | |
paul@591 | 291 | The 'context_index' parameter defines the position in local context storage |
paul@591 | 292 | for the referenced context or affected by a context operation. |
paul@591 | 293 | |
paul@482 | 294 | Return both the encoded instruction and a collection of substituted names. |
paul@113 | 295 | """ |
paul@113 | 296 | |
paul@113 | 297 | op = instruction[0] |
paul@113 | 298 | args = instruction[1:] |
paul@482 | 299 | substituted = set() |
paul@113 | 300 | |
paul@591 | 301 | # Encode the arguments. |
paul@113 | 302 | |
paul@591 | 303 | a = [] |
paul@591 | 304 | if args: |
paul@153 | 305 | converting_op = op |
paul@113 | 306 | for arg in args: |
paul@591 | 307 | s, _substituted = encode_access_instruction_arg(arg, subs, converting_op, context_index) |
paul@482 | 308 | substituted.update(_substituted) |
paul@482 | 309 | a.append(s) |
paul@153 | 310 | converting_op = None |
paul@113 | 311 | |
paul@591 | 312 | # Modify certain arguments. |
paul@113 | 313 | |
paul@624 | 314 | # Convert type name arguments. |
paul@113 | 315 | |
paul@624 | 316 | if op in typename_ops: |
paul@624 | 317 | a[1] = encode_path(encode_type_attribute(args[1])) |
paul@113 | 318 | |
paul@591 | 319 | # Obtain addresses of type arguments. |
paul@591 | 320 | |
paul@591 | 321 | elif op in type_ops: |
paul@591 | 322 | a[1] = "&%s" % a[1] |
paul@113 | 323 | |
paul@591 | 324 | # Obtain addresses of static objects. |
paul@591 | 325 | |
paul@591 | 326 | elif op in static_ops: |
paul@591 | 327 | a[-1] = "&%s" % a[-1] |
paul@385 | 328 | |
paul@591 | 329 | # Add context storage information to certain operations. |
paul@385 | 330 | |
paul@595 | 331 | if op in context_ops: |
paul@591 | 332 | a.insert(0, context_index) |
paul@141 | 333 | |
paul@602 | 334 | # Add the local context array to certain operations. |
paul@602 | 335 | |
paul@602 | 336 | if op in context_op_functions: |
paul@602 | 337 | a.append("__tmp_contexts") |
paul@602 | 338 | |
paul@591 | 339 | # Define any argument string. |
paul@141 | 340 | |
paul@591 | 341 | if a: |
paul@491 | 342 | argstr = "(%s)" % ", ".join(map(str, a)) |
paul@591 | 343 | else: |
paul@591 | 344 | argstr = "" |
paul@113 | 345 | |
paul@113 | 346 | # Substitute the first element of the instruction, which may not be an |
paul@113 | 347 | # operation at all. |
paul@113 | 348 | |
paul@144 | 349 | if subs.has_key(op): |
paul@482 | 350 | substituted.add(op) |
paul@498 | 351 | |
paul@498 | 352 | # Break accessor initialisation into initialisation and value-yielding |
paul@498 | 353 | # parts: |
paul@498 | 354 | |
paul@498 | 355 | if op == "<set_accessor>" and isinstance(a[0], InstructionSequence): |
paul@498 | 356 | ops = [] |
paul@498 | 357 | ops += a[0].get_init_instructions() |
paul@498 | 358 | ops.append("%s(%s)" % (subs[op], a[0].get_value_instruction())) |
paul@498 | 359 | return ", ".join(map(str, ops)), substituted |
paul@498 | 360 | |
paul@144 | 361 | op = subs[op] |
paul@498 | 362 | |
paul@144 | 363 | elif not args: |
paul@144 | 364 | op = "&%s" % encode_path(op) |
paul@144 | 365 | |
paul@482 | 366 | return "%s%s" % (op, argstr), substituted |
paul@113 | 367 | |
paul@591 | 368 | def encode_access_instruction_arg(arg, subs, op, context_index): |
paul@113 | 369 | |
paul@482 | 370 | """ |
paul@591 | 371 | Encode 'arg' using 'subs' to define substitutions, 'op' to indicate the |
paul@591 | 372 | operation to which the argument belongs, and 'context_index' to indicate any |
paul@591 | 373 | affected context storage. |
paul@591 | 374 | |
paul@591 | 375 | Return a tuple containing the encoded form of 'arg' along with a collection |
paul@591 | 376 | of any substituted values. |
paul@482 | 377 | """ |
paul@113 | 378 | |
paul@113 | 379 | if isinstance(arg, tuple): |
paul@591 | 380 | encoded, substituted = encode_access_instruction(arg, subs, context_index) |
paul@757 | 381 | return attribute_to_reference(op, arg[0], encoded, substituted) |
paul@113 | 382 | |
paul@113 | 383 | # Special values only need replacing, not encoding. |
paul@113 | 384 | |
paul@113 | 385 | elif subs.has_key(arg): |
paul@591 | 386 | |
paul@591 | 387 | # Handle values modified by storage details. |
paul@591 | 388 | |
paul@591 | 389 | if arg in context_values: |
paul@757 | 390 | encoded = "%s(%s)" % (subs.get(arg), context_index) |
paul@591 | 391 | else: |
paul@757 | 392 | encoded = subs.get(arg) |
paul@757 | 393 | |
paul@757 | 394 | substituted = set([arg]) |
paul@757 | 395 | return attribute_to_reference(op, arg, encoded, substituted) |
paul@113 | 396 | |
paul@258 | 397 | # Convert static references to the appropriate type. |
paul@258 | 398 | |
paul@757 | 399 | elif op and op in reference_acting_ops and \ |
paul@757 | 400 | arg not in attribute_producing_variables: |
paul@757 | 401 | |
paul@482 | 402 | return "&%s" % encode_path(arg), set() |
paul@258 | 403 | |
paul@113 | 404 | # Other values may need encoding. |
paul@113 | 405 | |
paul@113 | 406 | else: |
paul@482 | 407 | return encode_path(arg), set() |
paul@113 | 408 | |
paul@757 | 409 | def attribute_to_reference(op, arg, encoded, substituted): |
paul@757 | 410 | |
paul@757 | 411 | # Convert attribute results to references where required. |
paul@757 | 412 | |
paul@757 | 413 | if op and op in reference_acting_ops and ( |
paul@757 | 414 | arg in attribute_producing_ops or |
paul@757 | 415 | arg in attribute_producing_variables): |
paul@757 | 416 | |
paul@757 | 417 | return "__VALUE(%s)" % encoded, substituted |
paul@757 | 418 | else: |
paul@757 | 419 | return encoded, substituted |
paul@757 | 420 | |
paul@0 | 421 | def encode_function_pointer(path): |
paul@0 | 422 | |
paul@0 | 423 | "Encode 'path' as a reference to an output program function." |
paul@0 | 424 | |
paul@0 | 425 | return "__fn_%s" % encode_path(path) |
paul@0 | 426 | |
paul@0 | 427 | def encode_instantiator_pointer(path): |
paul@0 | 428 | |
paul@0 | 429 | "Encode 'path' as a reference to an output program instantiator." |
paul@0 | 430 | |
paul@0 | 431 | return "__new_%s" % encode_path(path) |
paul@0 | 432 | |
paul@491 | 433 | def encode_instructions(instructions): |
paul@491 | 434 | |
paul@491 | 435 | "Encode 'instructions' as a sequence." |
paul@491 | 436 | |
paul@491 | 437 | if len(instructions) == 1: |
paul@491 | 438 | return instructions[0] |
paul@491 | 439 | else: |
paul@491 | 440 | return "(\n%s\n)" % ",\n".join(instructions) |
paul@491 | 441 | |
paul@136 | 442 | def encode_literal_constant(n): |
paul@136 | 443 | |
paul@136 | 444 | "Encode a name for the literal constant with the number 'n'." |
paul@136 | 445 | |
paul@609 | 446 | return "__const%s" % n |
paul@136 | 447 | |
paul@378 | 448 | def encode_literal_constant_size(value): |
paul@378 | 449 | |
paul@378 | 450 | "Encode a size for the literal constant with the given 'value'." |
paul@378 | 451 | |
paul@378 | 452 | if isinstance(value, basestring): |
paul@378 | 453 | return len(value) |
paul@378 | 454 | else: |
paul@378 | 455 | return 0 |
paul@378 | 456 | |
paul@136 | 457 | def encode_literal_constant_member(value): |
paul@136 | 458 | |
paul@136 | 459 | "Encode the member name for the 'value' in the final program." |
paul@136 | 460 | |
paul@136 | 461 | return "%svalue" % value.__class__.__name__ |
paul@136 | 462 | |
paul@136 | 463 | def encode_literal_constant_value(value): |
paul@136 | 464 | |
paul@136 | 465 | "Encode the given 'value' in the final program." |
paul@136 | 466 | |
paul@136 | 467 | if isinstance(value, (int, float)): |
paul@136 | 468 | return str(value) |
paul@136 | 469 | else: |
paul@451 | 470 | l = [] |
paul@451 | 471 | |
paul@451 | 472 | # Encode characters including non-ASCII ones. |
paul@451 | 473 | |
paul@451 | 474 | for c in str(value): |
paul@451 | 475 | if c == '"': l.append('\\"') |
paul@451 | 476 | elif c == '\n': l.append('\\n') |
paul@451 | 477 | elif c == '\t': l.append('\\t') |
paul@451 | 478 | elif c == '\r': l.append('\\r') |
paul@512 | 479 | elif c == '\\': l.append('\\\\') |
paul@451 | 480 | elif 0x20 <= ord(c) < 0x80: l.append(c) |
paul@451 | 481 | else: l.append("\\x%02x" % ord(c)) |
paul@451 | 482 | |
paul@451 | 483 | return '"%s"' % "".join(l) |
paul@136 | 484 | |
paul@283 | 485 | def encode_literal_data_initialiser(style): |
paul@283 | 486 | |
paul@283 | 487 | """ |
paul@283 | 488 | Encode a reference to a function populating the data for a literal having |
paul@283 | 489 | the given 'style' ("mapping" or "sequence"). |
paul@283 | 490 | """ |
paul@283 | 491 | |
paul@283 | 492 | return "__newdata_%s" % style |
paul@283 | 493 | |
paul@159 | 494 | def encode_literal_instantiator(path): |
paul@159 | 495 | |
paul@159 | 496 | """ |
paul@159 | 497 | Encode a reference to an instantiator for a literal having the given 'path'. |
paul@159 | 498 | """ |
paul@159 | 499 | |
paul@159 | 500 | return "__newliteral_%s" % encode_path(path) |
paul@159 | 501 | |
paul@136 | 502 | def encode_literal_reference(n): |
paul@136 | 503 | |
paul@136 | 504 | "Encode a reference to a literal constant with the number 'n'." |
paul@136 | 505 | |
paul@609 | 506 | return "__constvalue%s" % n |
paul@136 | 507 | |
paul@850 | 508 | def encode_trailing_area(path): |
paul@850 | 509 | |
paul@850 | 510 | """ |
paul@850 | 511 | Encode any reference to trailing data members for instances of the type |
paul@850 | 512 | given by 'path'. |
paul@850 | 513 | """ |
paul@850 | 514 | |
paul@850 | 515 | return "__TRAILING_%s" % encode_path(path) |
paul@850 | 516 | |
paul@512 | 517 | |
paul@512 | 518 | |
paul@340 | 519 | # Track all encoded paths, detecting and avoiding conflicts. |
paul@340 | 520 | |
paul@340 | 521 | all_encoded_paths = {} |
paul@340 | 522 | |
paul@0 | 523 | def encode_path(path): |
paul@0 | 524 | |
paul@0 | 525 | "Encode 'path' as an output program object, translating special symbols." |
paul@0 | 526 | |
paul@0 | 527 | if path in reserved_words: |
paul@0 | 528 | return "__%s" % path |
paul@0 | 529 | else: |
paul@340 | 530 | part_encoded = path.replace("#", "__").replace("$", "__") |
paul@349 | 531 | |
paul@349 | 532 | if "." not in path: |
paul@349 | 533 | return part_encoded |
paul@349 | 534 | |
paul@340 | 535 | encoded = part_encoded.replace(".", "_") |
paul@340 | 536 | |
paul@340 | 537 | # Test for a conflict with the encoding of a different path, re-encoding |
paul@340 | 538 | # if necessary. |
paul@340 | 539 | |
paul@340 | 540 | previous = all_encoded_paths.get(encoded) |
paul@340 | 541 | replacement = "_" |
paul@340 | 542 | |
paul@340 | 543 | while previous: |
paul@340 | 544 | if path == previous: |
paul@340 | 545 | return encoded |
paul@340 | 546 | replacement += "_" |
paul@340 | 547 | encoded = part_encoded.replace(".", replacement) |
paul@340 | 548 | previous = all_encoded_paths.get(encoded) |
paul@340 | 549 | |
paul@340 | 550 | # Store any new or re-encoded path. |
paul@340 | 551 | |
paul@340 | 552 | all_encoded_paths[encoded] = path |
paul@340 | 553 | return encoded |
paul@0 | 554 | |
paul@623 | 555 | def encode_code(name): |
paul@623 | 556 | |
paul@623 | 557 | "Encode 'name' as an attribute code indicator." |
paul@623 | 558 | |
paul@623 | 559 | return "__ATTRCODE(%s)" % encode_path(name) |
paul@623 | 560 | |
paul@623 | 561 | def encode_pcode(name): |
paul@623 | 562 | |
paul@623 | 563 | "Encode 'name' as an parameter code indicator." |
paul@623 | 564 | |
paul@623 | 565 | return "__PARAMCODE(%s)" % encode_path(name) |
paul@623 | 566 | |
paul@623 | 567 | def encode_pos(name): |
paul@623 | 568 | |
paul@623 | 569 | "Encode 'name' as an attribute position indicator." |
paul@623 | 570 | |
paul@623 | 571 | return "__ATTRPOS(%s)" % encode_path(name) |
paul@623 | 572 | |
paul@623 | 573 | def encode_ppos(name): |
paul@623 | 574 | |
paul@623 | 575 | "Encode 'name' as an parameter position indicator." |
paul@623 | 576 | |
paul@623 | 577 | return "__PARAMPOS(%s)" % encode_path(name) |
paul@623 | 578 | |
paul@136 | 579 | def encode_predefined_reference(path): |
paul@136 | 580 | |
paul@136 | 581 | "Encode a reference to a predefined constant value for 'path'." |
paul@136 | 582 | |
paul@136 | 583 | return "__predefined_%s" % encode_path(path) |
paul@136 | 584 | |
paul@150 | 585 | def encode_size(kind, path=None): |
paul@150 | 586 | |
paul@150 | 587 | """ |
paul@150 | 588 | Encode a structure size reference for the given 'kind' of structure, with |
paul@150 | 589 | 'path' indicating a specific structure name. |
paul@150 | 590 | """ |
paul@150 | 591 | |
paul@150 | 592 | return "__%ssize%s" % (structure_size_prefixes.get(kind, kind), path and "_%s" % encode_path(path) or "") |
paul@150 | 593 | |
paul@0 | 594 | def encode_symbol(symbol_type, path=None): |
paul@0 | 595 | |
paul@0 | 596 | "Encode a symbol with the given 'symbol_type' and optional 'path'." |
paul@0 | 597 | |
paul@0 | 598 | return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "") |
paul@0 | 599 | |
paul@150 | 600 | def encode_tablename(kind, path): |
paul@150 | 601 | |
paul@150 | 602 | """ |
paul@150 | 603 | Encode a table reference for the given 'kind' of table structure, indicating |
paul@150 | 604 | a 'path' for the specific object concerned. |
paul@150 | 605 | """ |
paul@150 | 606 | |
paul@150 | 607 | return "__%sTable_%s" % (table_name_prefixes[kind], encode_path(path)) |
paul@150 | 608 | |
paul@131 | 609 | def encode_type_attribute(path): |
paul@131 | 610 | |
paul@131 | 611 | "Encode the special type attribute for 'path'." |
paul@131 | 612 | |
paul@131 | 613 | return "#%s" % path |
paul@131 | 614 | |
paul@318 | 615 | def decode_type_attribute(s): |
paul@318 | 616 | |
paul@318 | 617 | "Decode the special type attribute 's'." |
paul@318 | 618 | |
paul@318 | 619 | return s[1:] |
paul@318 | 620 | |
paul@318 | 621 | def is_type_attribute(s): |
paul@318 | 622 | |
paul@318 | 623 | "Return whether 's' is a type attribute name." |
paul@318 | 624 | |
paul@318 | 625 | return s.startswith("#") |
paul@318 | 626 | |
paul@56 | 627 | |
paul@56 | 628 | |
paul@150 | 629 | # A mapping from kinds to structure size reference prefixes. |
paul@150 | 630 | |
paul@150 | 631 | structure_size_prefixes = { |
paul@150 | 632 | "<class>" : "c", |
paul@150 | 633 | "<module>" : "m", |
paul@150 | 634 | "<instance>" : "i" |
paul@150 | 635 | } |
paul@150 | 636 | |
paul@150 | 637 | # A mapping from kinds to table name prefixes. |
paul@150 | 638 | |
paul@150 | 639 | table_name_prefixes = { |
paul@150 | 640 | "<class>" : "Class", |
paul@150 | 641 | "<function>" : "Function", |
paul@150 | 642 | "<module>" : "Module", |
paul@150 | 643 | "<instance>" : "Instance" |
paul@150 | 644 | } |
paul@150 | 645 | |
paul@150 | 646 | |
paul@150 | 647 | |
paul@0 | 648 | # Output language reserved words. |
paul@0 | 649 | |
paul@0 | 650 | reserved_words = [ |
paul@0 | 651 | "break", "char", "const", "continue", |
paul@0 | 652 | "default", "double", "else", |
paul@0 | 653 | "float", "for", |
paul@0 | 654 | "if", "int", "long", |
paul@0 | 655 | "NULL", |
paul@0 | 656 | "return", "struct", |
paul@0 | 657 | "typedef", |
paul@0 | 658 | "void", "while", |
paul@0 | 659 | ] |
paul@0 | 660 | |
paul@0 | 661 | # vim: tabstop=4 expandtab shiftwidth=4 |