|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Algebraic Data Types (ADT) is used to represent two kinds of things: |
| 4 | +
|
| 5 | +1. A discrimintated union of types, called sum |
| 6 | +2. A combination of some types, called product. |
| 7 | +
|
| 8 | +# Sum types |
| 9 | +
|
| 10 | +Sum types represents a concept of generalizing. For example, |
| 11 | +on ARM R0 and R1 are all general purpose registers (GPR). Also on ARM |
| 12 | +we have Condition Code registers (CCR) : |
| 13 | +
|
| 14 | + class Reg(ADT) : pass |
| 15 | + class GPR(Reg) : pass |
| 16 | + class CCR(Reg) : pass |
| 17 | + class R0(GPR) : pass |
| 18 | + class R1(GPR) : pass |
| 19 | +
|
| 20 | +
|
| 21 | +That states that a register can be either R0 or R1, but not both. |
| 22 | +
|
| 23 | +# Product types |
| 24 | +
|
| 25 | +Product types represent a combination of other types. For example, |
| 26 | +mov instruction has two arguments, and the arguments are also ADT's by |
| 27 | +itself: |
| 28 | +
|
| 29 | + def Insn(ADT) : pass |
| 30 | + def Mov(Insn) : pass |
| 31 | +
|
| 32 | + Mov(R0(), R1()) |
| 33 | +
|
| 34 | +
|
| 35 | +# Comparison |
| 36 | +
|
| 37 | +ADT objects are compared structurally: if they have the same class and |
| 38 | +and their values are structurally the same, then they are equal, i.e., |
| 39 | +
|
| 40 | + assert(R0() == R0()) |
| 41 | + assert(R1() != R0()) |
| 42 | +
|
| 43 | +""" |
| 44 | + |
| 45 | +from collections import Iterable |
| 46 | + |
| 47 | +class ADT(object): |
| 48 | + """ Algebraic Data Type. |
| 49 | +
|
| 50 | + This is a base class for all ADTs. ADT represented by a tuple of arguments, |
| 51 | + stored in a val field. Arguments should be instances of ADT class, or numbers, |
| 52 | + or strings. Empty set of arguments is permitted. |
| 53 | + A one-tuple is automatically untupled, i.e., `Int(12)` has value `12`, not `(12,)`. |
| 54 | + For convenience, a name of the constructor is provided in `name` field. |
| 55 | +
|
| 56 | + A structural comparison is provided. |
| 57 | +
|
| 58 | + """ |
| 59 | + def __init__(self, *args): |
| 60 | + self.name = self.__class__.__name__ |
| 61 | + self.val = args if len(args) != 1 else args[0] |
| 62 | + |
| 63 | + def __cmp__(self,other): |
| 64 | + return self.__dict__.__cmp__(other.__dict__) |
| 65 | + |
| 66 | + def __repr__(self): |
| 67 | + def qstr(x): |
| 68 | + if isinstance(x, int) or isinstance(x, ADT): |
| 69 | + return str(x) |
| 70 | + else: |
| 71 | + return '"{0}"'.format(x) |
| 72 | + def args(): |
| 73 | + if isinstance(self.val, tuple): |
| 74 | + return ", ".join(qstr(x) for x in self.val) |
| 75 | + else: |
| 76 | + return qstr(self.val) |
| 77 | + |
| 78 | + return "{0}({1})".format(self.name, args()) |
| 79 | + |
| 80 | + |
| 81 | +class Visitor(object): |
| 82 | + """ ADT Visitor. |
| 83 | + This class helps to perform iterations over arbitrary ADTs. |
| 84 | +
|
| 85 | + This visitor supports, subtyping, i.e. you can match not only on |
| 86 | + leaf constructors, but also on their bases. For example, with |
| 87 | + the `Exp` hierarchy, provided below, you can visit all binary operators, |
| 88 | + by overriding `visit_BinOp` method. See `run` method description for |
| 89 | + more infromation. |
| 90 | + """ |
| 91 | + |
| 92 | + def visit_ADT(self, adt): |
| 93 | + """Default visitor. |
| 94 | +
|
| 95 | + This method will be called for those data types that has |
| 96 | + no specific visitors. It will recursively descent into all |
| 97 | + ADT values. |
| 98 | + """ |
| 99 | + if isinstance(adt.val, tuple): |
| 100 | + for e in adt.val: |
| 101 | + self.run(e) |
| 102 | + |
| 103 | + def run(self, adt): |
| 104 | + """ADT.run(adt-or-iterable) -> None |
| 105 | +
|
| 106 | + if adt is iterable, the run is called recursively for each member |
| 107 | + of adt. |
| 108 | +
|
| 109 | + Otherwise, for an ADT of type C the method `visit_C` is looked up in the |
| 110 | + visitors methods dictionary. If it doesn't exist, then `visit_B` is |
| 111 | + looked up, where `D` is the base class of `C`. The process continues, |
| 112 | + until the method is found. This is guaranteed to terminate, |
| 113 | + since visit_ADT method is defined. |
| 114 | +
|
| 115 | + Note: Non ADTs will be silently ignored. |
| 116 | +
|
| 117 | + Once the method is found it is called. It is the method's responsiblity |
| 118 | + to recurse into sub-elements, e.g., call run method. |
| 119 | +
|
| 120 | + For example, suppose that we want to count negative values in a given |
| 121 | + BIL expression: |
| 122 | +
|
| 123 | + class CountNegatives(Visitor): |
| 124 | + def __init__(self): |
| 125 | + self.neg = False |
| 126 | + self.count = 0 |
| 127 | +
|
| 128 | + def visit_Int(self, int): |
| 129 | + if int.val < 0 and not self.neg \ |
| 130 | + or int.val > 0 and self.neg: |
| 131 | + self.count += 1 |
| 132 | +
|
| 133 | + def visit_NEG(self, op): |
| 134 | + was = self.neg |
| 135 | + self.neg = not was |
| 136 | + self.run(op.val) |
| 137 | + self.neg = was |
| 138 | +
|
| 139 | + We need to keep track on the unary negation operator, and, of |
| 140 | + course, we need to look for immediates, so we override two methods: |
| 141 | + visit_Int for Int constructor and visit_NEG for counting unary minuses. |
| 142 | + (Actually we should count for bitwise NOT operation also, since it will |
| 143 | + change the sign bit also, but lets forget about it for the matter of the |
| 144 | + excercise (and it can be easily fixed just by matching visit_UnOp)). |
| 145 | +
|
| 146 | + When we hit visit_NEG we toggle current sign, storing its previous value |
| 147 | + and recurse into the operand. After we return from the recursion, we restore |
| 148 | + the sign. |
| 149 | + """ |
| 150 | + if isinstance(adt, Iterable): |
| 151 | + for s in adt: |
| 152 | + self.run(s) |
| 153 | + if isinstance(adt, ADT): |
| 154 | + for c in adt.__class__.mro(): |
| 155 | + name = ("visit_%s" % c.__name__) |
| 156 | + fn = getattr(self, name, None) |
| 157 | + if fn is not None: |
| 158 | + return fn(adt) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + class Fruit(ADT) : pass |
| 163 | + class Bannana(Fruit) : pass |
| 164 | + class Apple(Fruit) : pass |
| 165 | + |
| 166 | + assert(Bannana() == Bannana()) |
| 167 | + assert(Bannana() != Apple()) |
| 168 | + assert( Apple() < Bannana()) |
0 commit comments