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