paul@636 | 1 | #!/usr/bin/env python |
paul@636 | 2 | |
paul@636 | 3 | """ |
paul@636 | 4 | Translation result abstractions. |
paul@636 | 5 | |
paul@948 | 6 | Copyright (C) 2016, 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@636 | 7 | |
paul@636 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@636 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@636 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@636 | 11 | version. |
paul@636 | 12 | |
paul@636 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@636 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@636 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@636 | 16 | details. |
paul@636 | 17 | |
paul@636 | 18 | You should have received a copy of the GNU General Public License along with |
paul@636 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@636 | 20 | """ |
paul@636 | 21 | |
paul@636 | 22 | from common import first, InstructionSequence |
paul@636 | 23 | from encoders import encode_instructions, encode_literal_constant, encode_path |
paul@685 | 24 | from results import ConstantValueRef, InstanceRef, LiteralSequenceRef, NameRef, \ |
paul@636 | 25 | ResolvedNameRef, Result |
paul@636 | 26 | |
paul@964 | 27 | special_attributes = ("__args__", "__data__", "__key__", "__size__") |
paul@964 | 28 | |
paul@636 | 29 | # Classes representing intermediate translation results. |
paul@636 | 30 | |
paul@636 | 31 | class ReturnRef: |
paul@636 | 32 | |
paul@636 | 33 | "Indicates usage of a return statement." |
paul@636 | 34 | |
paul@636 | 35 | pass |
paul@636 | 36 | |
paul@636 | 37 | class Expression(Result): |
paul@636 | 38 | |
paul@636 | 39 | "A general expression." |
paul@636 | 40 | |
paul@636 | 41 | def __init__(self, s): |
paul@637 | 42 | if isinstance(s, Result): |
paul@637 | 43 | self.s = str(s) |
paul@637 | 44 | self.expr = s |
paul@637 | 45 | else: |
paul@637 | 46 | self.s = s |
paul@637 | 47 | self.expr = None |
paul@637 | 48 | |
paul@637 | 49 | def discards_temporary(self, test=True): |
paul@637 | 50 | |
paul@637 | 51 | """ |
paul@637 | 52 | Return a list of temporary names that can be discarded if 'test' is |
paul@637 | 53 | specified as a true value (or omitted). |
paul@637 | 54 | """ |
paul@637 | 55 | |
paul@637 | 56 | return self.expr and self.expr.discards_temporary(False) |
paul@637 | 57 | |
paul@636 | 58 | def __str__(self): |
paul@636 | 59 | return self.s |
paul@637 | 60 | |
paul@636 | 61 | def __repr__(self): |
paul@636 | 62 | return "Expression(%r)" % self.s |
paul@636 | 63 | |
paul@636 | 64 | class TrResolvedNameRef(ResolvedNameRef): |
paul@636 | 65 | |
paul@636 | 66 | "A reference to a name in the translation." |
paul@636 | 67 | |
paul@664 | 68 | def __init__(self, name, ref, expr=None, is_global=False, location=None): |
paul@636 | 69 | ResolvedNameRef.__init__(self, name, ref, expr, is_global) |
paul@636 | 70 | self.location = location |
paul@636 | 71 | |
paul@959 | 72 | # For sources, any identified static origin will be constant and thus |
paul@959 | 73 | # usable directly. For targets, no constant should be assigned and thus |
paul@959 | 74 | # the alias (or any plain name) will be used. |
paul@959 | 75 | |
paul@959 | 76 | self.static_ref = self.static() |
paul@959 | 77 | origin = self.static_ref and self.get_origin() |
paul@959 | 78 | self.static_name = origin and encode_path(origin) |
paul@959 | 79 | |
paul@959 | 80 | # Determine whether a qualified name is involved. |
paul@959 | 81 | |
paul@959 | 82 | t = (not self.is_constant_alias() and self.get_name() or self.name).rsplit(".", 1) |
paul@959 | 83 | self.parent = len(t) > 1 and t[0] or None |
paul@959 | 84 | self.attrname = t[-1] and encode_path(t[-1]) |
paul@959 | 85 | |
paul@636 | 86 | def access_location(self): |
paul@636 | 87 | return self.location |
paul@636 | 88 | |
paul@636 | 89 | def __str__(self): |
paul@636 | 90 | |
paul@636 | 91 | "Return an output representation of the referenced name." |
paul@636 | 92 | |
paul@733 | 93 | # Temporary names are output program locals. |
paul@733 | 94 | |
paul@733 | 95 | if self.name.startswith("$t"): |
paul@733 | 96 | if self.expr: |
paul@733 | 97 | return "%s = %s" % (encode_path(self.name), self.expr) |
paul@733 | 98 | else: |
paul@733 | 99 | return encode_path(self.name) |
paul@733 | 100 | |
paul@636 | 101 | # Assignments. |
paul@636 | 102 | |
paul@636 | 103 | if self.expr: |
paul@636 | 104 | |
paul@636 | 105 | # Eliminate assignments between constants. |
paul@636 | 106 | |
paul@959 | 107 | if self.static_ref and self.expr.static(): |
paul@636 | 108 | return "" |
paul@636 | 109 | |
paul@636 | 110 | # Qualified names must be converted into parent-relative assignments. |
paul@636 | 111 | |
paul@959 | 112 | elif self.parent: |
paul@964 | 113 | |
paul@964 | 114 | # NOTE: This is a superficial test for internal attributes that |
paul@964 | 115 | # NOTE: relies on such attributes being used directly and passed |
paul@964 | 116 | # NOTE: to native code. |
paul@964 | 117 | |
paul@964 | 118 | if self.attrname in special_attributes: |
paul@964 | 119 | op = "__store_via_object_internal" |
paul@964 | 120 | else: |
paul@964 | 121 | op = "__store_via_object" |
paul@964 | 122 | |
paul@964 | 123 | return "%s(&%s, %s, %s)" % ( |
paul@964 | 124 | op, encode_path(self.parent), self.attrname, self.expr) |
paul@636 | 125 | |
paul@636 | 126 | # All other assignments involve the names as they were given. |
paul@636 | 127 | |
paul@636 | 128 | else: |
paul@964 | 129 | return "%s = __store_local(%s, %s)" % (self.attrname, self.attrname, self.expr) |
paul@636 | 130 | |
paul@636 | 131 | # Expressions. |
paul@636 | 132 | |
paul@964 | 133 | if self.static_name: |
paul@964 | 134 | s = "__ATTRVALUE(&%s)" % self.static_name |
paul@636 | 135 | |
paul@636 | 136 | # Qualified names must be converted into parent-relative accesses. |
paul@636 | 137 | |
paul@959 | 138 | elif self.parent: |
paul@964 | 139 | |
paul@964 | 140 | # NOTE: This is a superficial test for internal attributes that |
paul@964 | 141 | # NOTE: relies on such attributes being used directly and passed |
paul@964 | 142 | # NOTE: to native code. |
paul@964 | 143 | |
paul@964 | 144 | if self.attrname in special_attributes: |
paul@964 | 145 | op = "__load_via_object_internal" |
paul@964 | 146 | else: |
paul@964 | 147 | op = "__load_via_object" |
paul@964 | 148 | |
paul@964 | 149 | s = "%s(&%s, %s)" % ( |
paul@964 | 150 | op, encode_path(self.parent), self.attrname) |
paul@636 | 151 | |
paul@636 | 152 | # All other accesses involve the names as they were given. |
paul@636 | 153 | |
paul@636 | 154 | else: |
paul@964 | 155 | s = "(%s)" % self.attrname |
paul@961 | 156 | |
paul@964 | 157 | return "__load(%s)" % s |
paul@961 | 158 | |
paul@636 | 159 | class TrConstantValueRef(ConstantValueRef): |
paul@636 | 160 | |
paul@636 | 161 | "A constant value reference in the translation." |
paul@636 | 162 | |
paul@636 | 163 | def __str__(self): |
paul@964 | 164 | return "__load(%s)" % encode_literal_constant(self.number) |
paul@961 | 165 | |
paul@636 | 166 | class TrLiteralSequenceRef(LiteralSequenceRef): |
paul@636 | 167 | |
paul@636 | 168 | "A reference representing a sequence of values." |
paul@636 | 169 | |
paul@636 | 170 | def __str__(self): |
paul@636 | 171 | return str(self.node) |
paul@636 | 172 | |
paul@636 | 173 | class TrInstanceRef(InstanceRef): |
paul@636 | 174 | |
paul@636 | 175 | "A reference representing instantiation of a class." |
paul@636 | 176 | |
paul@636 | 177 | def __init__(self, ref, expr): |
paul@636 | 178 | |
paul@636 | 179 | """ |
paul@636 | 180 | Initialise the reference with 'ref' indicating the nature of the |
paul@636 | 181 | reference and 'expr' being an expression used to create the instance. |
paul@636 | 182 | """ |
paul@636 | 183 | |
paul@636 | 184 | InstanceRef.__init__(self, ref) |
paul@636 | 185 | self.expr = expr |
paul@636 | 186 | |
paul@636 | 187 | def __str__(self): |
paul@636 | 188 | return self.expr |
paul@636 | 189 | |
paul@636 | 190 | def __repr__(self): |
paul@636 | 191 | return "TrResolvedInstanceRef(%r, %r)" % (self.ref, self.expr) |
paul@636 | 192 | |
paul@636 | 193 | class AttrResult(Result, InstructionSequence): |
paul@636 | 194 | |
paul@636 | 195 | "A translation result for an attribute access." |
paul@636 | 196 | |
paul@828 | 197 | def __init__(self, instructions, refs, location, context_identity, |
paul@964 | 198 | context_identity_verified, accessor_test, accessor_stored, |
paul@964 | 199 | attrname, assignment): |
paul@858 | 200 | |
paul@636 | 201 | InstructionSequence.__init__(self, instructions) |
paul@636 | 202 | self.refs = refs |
paul@636 | 203 | self.location = location |
paul@636 | 204 | self.context_identity = context_identity |
paul@776 | 205 | self.context_identity_verified = context_identity_verified |
paul@828 | 206 | self.accessor_test = accessor_test |
paul@858 | 207 | self.accessor_stored = accessor_stored |
paul@964 | 208 | self.attrname = attrname |
paul@964 | 209 | self.assignment = assignment |
paul@636 | 210 | |
paul@636 | 211 | def references(self): |
paul@636 | 212 | return self.refs |
paul@636 | 213 | |
paul@636 | 214 | def access_location(self): |
paul@636 | 215 | return self.location |
paul@636 | 216 | |
paul@636 | 217 | def context(self): |
paul@636 | 218 | return self.context_identity |
paul@636 | 219 | |
paul@776 | 220 | def context_verified(self): |
paul@776 | 221 | return self.context_identity_verified and self.context() or None |
paul@776 | 222 | |
paul@828 | 223 | def tests_accessor(self): |
paul@828 | 224 | return self.accessor_test |
paul@828 | 225 | |
paul@858 | 226 | def stores_accessor(self): |
paul@858 | 227 | return self.accessor_stored |
paul@858 | 228 | |
paul@636 | 229 | def get_origin(self): |
paul@636 | 230 | return self.refs and len(self.refs) == 1 and first(self.refs).get_origin() |
paul@636 | 231 | |
paul@636 | 232 | def has_kind(self, kinds): |
paul@636 | 233 | if not self.refs: |
paul@636 | 234 | return False |
paul@636 | 235 | for ref in self.refs: |
paul@636 | 236 | if ref.has_kind(kinds): |
paul@636 | 237 | return True |
paul@636 | 238 | return False |
paul@636 | 239 | |
paul@636 | 240 | def __nonzero__(self): |
paul@636 | 241 | return bool(self.instructions) |
paul@636 | 242 | |
paul@636 | 243 | def __str__(self): |
paul@964 | 244 | s = encode_instructions(self.instructions) |
paul@964 | 245 | |
paul@964 | 246 | # NOTE: This includes a superficial test for internal attributes that |
paul@964 | 247 | # NOTE: relies on such attributes being used directly and passedto |
paul@964 | 248 | # NOTE: native code. |
paul@964 | 249 | |
paul@964 | 250 | if self.assignment or self.accessor_test or self.attrname in special_attributes: |
paul@964 | 251 | return s |
paul@964 | 252 | else: |
paul@964 | 253 | return "__load(%s)" % s |
paul@636 | 254 | |
paul@636 | 255 | def __repr__(self): |
paul@964 | 256 | return "AttrResult(%r, %r, %r, %r, %r, %r, %r, %r, %r)" % ( |
paul@858 | 257 | self.instructions, self.refs, self.location, |
paul@858 | 258 | self.context_identity, self.context_identity_verified, |
paul@964 | 259 | self.accessor_test, self.accessor_stored, self.attrname, |
paul@964 | 260 | self.assignment) |
paul@961 | 261 | |
paul@685 | 262 | class AliasResult(NameRef, Result): |
paul@685 | 263 | |
paul@685 | 264 | "An alias for other values." |
paul@685 | 265 | |
paul@736 | 266 | def __init__(self, name_ref, refs, location): |
paul@685 | 267 | NameRef.__init__(self, name_ref.name, is_global=name_ref.is_global_name()) |
paul@685 | 268 | self.name_ref = name_ref |
paul@685 | 269 | self.refs = refs |
paul@736 | 270 | self.location = location |
paul@685 | 271 | |
paul@685 | 272 | def references(self): |
paul@685 | 273 | ref = self.name_ref.reference() |
paul@685 | 274 | return self.refs or ref and [ref] or None |
paul@685 | 275 | |
paul@685 | 276 | def reference(self): |
paul@685 | 277 | refs = self.references() |
paul@685 | 278 | return len(refs) == 1 and first(refs) or None |
paul@685 | 279 | |
paul@685 | 280 | def access_location(self): |
paul@736 | 281 | return self.location |
paul@685 | 282 | |
paul@685 | 283 | def get_name(self): |
paul@685 | 284 | ref = self.reference() |
paul@685 | 285 | return ref and ref.get_name() |
paul@685 | 286 | |
paul@685 | 287 | def get_origin(self): |
paul@685 | 288 | ref = self.reference() |
paul@685 | 289 | return ref and ref.get_origin() |
paul@685 | 290 | |
paul@685 | 291 | def static(self): |
paul@685 | 292 | ref = self.reference() |
paul@685 | 293 | return ref and ref.static() |
paul@685 | 294 | |
paul@685 | 295 | def final(self): |
paul@685 | 296 | ref = self.reference() |
paul@685 | 297 | return ref and ref.final() |
paul@685 | 298 | |
paul@685 | 299 | def has_kind(self, kinds): |
paul@685 | 300 | if not self.refs: |
paul@685 | 301 | return self.name_ref.has_kind(kinds) |
paul@685 | 302 | |
paul@685 | 303 | for ref in self.refs: |
paul@685 | 304 | if ref.has_kind(kinds): |
paul@685 | 305 | return True |
paul@685 | 306 | |
paul@685 | 307 | return False |
paul@685 | 308 | |
paul@685 | 309 | def __str__(self): |
paul@685 | 310 | return str(self.name_ref) |
paul@685 | 311 | |
paul@685 | 312 | def __repr__(self): |
paul@685 | 313 | return "AliasResult(%r, %r)" % (self.name_ref, self.refs) |
paul@685 | 314 | |
paul@636 | 315 | class InvocationResult(Result, InstructionSequence): |
paul@636 | 316 | |
paul@636 | 317 | "A translation result for an invocation." |
paul@636 | 318 | |
paul@636 | 319 | def __str__(self): |
paul@636 | 320 | return encode_instructions(self.instructions) |
paul@636 | 321 | |
paul@636 | 322 | def __repr__(self): |
paul@636 | 323 | return "InvocationResult(%r)" % self.instructions |
paul@636 | 324 | |
paul@636 | 325 | class InstantiationResult(InvocationResult, TrInstanceRef): |
paul@636 | 326 | |
paul@636 | 327 | "An instantiation result acting like an invocation result." |
paul@636 | 328 | |
paul@636 | 329 | def __init__(self, ref, instructions): |
paul@636 | 330 | InstanceRef.__init__(self, ref) |
paul@636 | 331 | InvocationResult.__init__(self, instructions) |
paul@636 | 332 | |
paul@636 | 333 | def __repr__(self): |
paul@636 | 334 | return "InstantiationResult(%r, %r)" % (self.ref, self.instructions) |
paul@636 | 335 | |
paul@636 | 336 | class PredefinedConstantRef(Result): |
paul@636 | 337 | |
paul@636 | 338 | "A predefined constant reference." |
paul@636 | 339 | |
paul@636 | 340 | def __init__(self, value, expr=None): |
paul@636 | 341 | self.value = value |
paul@636 | 342 | self.expr = expr |
paul@636 | 343 | |
paul@636 | 344 | def __str__(self): |
paul@636 | 345 | |
paul@636 | 346 | # Eliminate predefined constant assignments. |
paul@636 | 347 | |
paul@636 | 348 | if self.expr: |
paul@636 | 349 | return "" |
paul@636 | 350 | |
paul@636 | 351 | # Generate the specific constants. |
paul@636 | 352 | |
paul@636 | 353 | if self.value in ("False", "True"): |
paul@636 | 354 | return encode_path("__builtins__.boolean.%s" % self.value) |
paul@636 | 355 | elif self.value == "None": |
paul@636 | 356 | return encode_path("__builtins__.none.%s" % self.value) |
paul@636 | 357 | elif self.value == "NotImplemented": |
paul@636 | 358 | return encode_path("__builtins__.notimplemented.%s" % self.value) |
paul@636 | 359 | else: |
paul@636 | 360 | return self.value |
paul@636 | 361 | |
paul@636 | 362 | def __repr__(self): |
paul@636 | 363 | return "PredefinedConstantRef(%r)" % self.value |
paul@636 | 364 | |
paul@636 | 365 | class LogicalResult(Result): |
paul@636 | 366 | |
paul@636 | 367 | "A logical expression result." |
paul@636 | 368 | |
paul@636 | 369 | def _convert(self, expr): |
paul@636 | 370 | |
paul@636 | 371 | "Return 'expr' converted to a testable value." |
paul@636 | 372 | |
paul@636 | 373 | if isinstance(expr, LogicalResult): |
paul@636 | 374 | return expr.apply_test() |
paul@636 | 375 | else: |
paul@636 | 376 | return "__BOOL(%s)" % expr |
paul@636 | 377 | |
paul@636 | 378 | class NegationResult(LogicalResult): |
paul@636 | 379 | |
paul@636 | 380 | "A negation expression result." |
paul@636 | 381 | |
paul@636 | 382 | def __init__(self, expr): |
paul@636 | 383 | self.expr = expr |
paul@636 | 384 | |
paul@636 | 385 | def apply_test(self): |
paul@636 | 386 | |
paul@636 | 387 | "Return the result in a form suitable for direct testing." |
paul@636 | 388 | |
paul@636 | 389 | expr = self._convert(self.expr) |
paul@636 | 390 | return "(!%s)" % expr |
paul@636 | 391 | |
paul@637 | 392 | def discards_temporary(self, test=True): |
paul@637 | 393 | |
paul@637 | 394 | """ |
paul@638 | 395 | Negations should have discarded their operand's temporary names when |
paul@638 | 396 | being instantiated. |
paul@637 | 397 | """ |
paul@637 | 398 | |
paul@638 | 399 | return None |
paul@637 | 400 | |
paul@636 | 401 | def __str__(self): |
paul@636 | 402 | return "(%s ? %s : %s)" % ( |
paul@636 | 403 | self._convert(self.expr), |
paul@636 | 404 | PredefinedConstantRef("False"), |
paul@636 | 405 | PredefinedConstantRef("True")) |
paul@636 | 406 | |
paul@636 | 407 | def __repr__(self): |
paul@636 | 408 | return "NegationResult(%r)" % self.expr |
paul@636 | 409 | |
paul@636 | 410 | class LogicalOperationResult(LogicalResult): |
paul@636 | 411 | |
paul@636 | 412 | "A logical operation result." |
paul@636 | 413 | |
paul@636 | 414 | def __init__(self, exprs, conjunction): |
paul@636 | 415 | self.exprs = exprs |
paul@636 | 416 | self.conjunction = conjunction |
paul@636 | 417 | |
paul@636 | 418 | def apply_test(self): |
paul@636 | 419 | |
paul@636 | 420 | """ |
paul@636 | 421 | Return the result in a form suitable for direct testing. |
paul@636 | 422 | |
paul@636 | 423 | Convert ... to ... |
paul@636 | 424 | |
paul@636 | 425 | <a> and <b> |
paul@636 | 426 | ((__BOOL(<a>)) && (__BOOL(<b>))) |
paul@636 | 427 | |
paul@636 | 428 | <a> or <b> |
paul@636 | 429 | ((__BOOL(<a>)) || (__BOOL(<b>))) |
paul@636 | 430 | """ |
paul@636 | 431 | |
paul@636 | 432 | results = [] |
paul@636 | 433 | for expr in self.exprs: |
paul@636 | 434 | results.append(self._convert(expr)) |
paul@636 | 435 | |
paul@636 | 436 | if self.conjunction: |
paul@636 | 437 | return "(%s)" % " && ".join(results) |
paul@636 | 438 | else: |
paul@636 | 439 | return "(%s)" % " || ".join(results) |
paul@636 | 440 | |
paul@637 | 441 | def discards_temporary(self, test=True): |
paul@637 | 442 | |
paul@637 | 443 | """ |
paul@637 | 444 | Return a list of temporary names that can be discarded if 'test' is |
paul@637 | 445 | specified as a true value (or omitted). |
paul@637 | 446 | """ |
paul@637 | 447 | |
paul@637 | 448 | if not test: |
paul@637 | 449 | return None |
paul@637 | 450 | |
paul@637 | 451 | temps = ["__tmp_result"] |
paul@637 | 452 | |
paul@637 | 453 | for expr in self.exprs: |
paul@637 | 454 | t = expr.discards_temporary(test) |
paul@637 | 455 | if t: |
paul@637 | 456 | temps += t |
paul@637 | 457 | |
paul@637 | 458 | return temps |
paul@637 | 459 | |
paul@636 | 460 | def __str__(self): |
paul@636 | 461 | |
paul@636 | 462 | """ |
paul@636 | 463 | Convert ... to ... |
paul@636 | 464 | |
paul@636 | 465 | <a> and <b> |
paul@636 | 466 | (__tmp_result = <a>, !__BOOL(__tmp_result)) ? __tmp_result : <b> |
paul@636 | 467 | |
paul@636 | 468 | <a> or <b> |
paul@636 | 469 | (__tmp_result = <a>, __BOOL(__tmp_result)) ? __tmp_result : <b> |
paul@636 | 470 | """ |
paul@636 | 471 | |
paul@636 | 472 | results = [] |
paul@636 | 473 | for expr in self.exprs[:-1]: |
paul@636 | 474 | results.append("(__tmp_result = %s, %s__BOOL(__tmp_result)) ? __tmp_result : " % (expr, self.conjunction and "!" or "")) |
paul@636 | 475 | results.append(str(self.exprs[-1])) |
paul@636 | 476 | |
paul@636 | 477 | return "(%s)" % "".join(results) |
paul@636 | 478 | |
paul@636 | 479 | def __repr__(self): |
paul@636 | 480 | return "LogicalOperationResult(%r, %r)" % (self.exprs, self.conjunction) |
paul@636 | 481 | |
paul@636 | 482 | # vim: tabstop=4 expandtab shiftwidth=4 |