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