|
| 1 | +# Copyright 2024-2025 Open Quantum Design |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from oqd_compiler_infrastructure import RewriteRule |
| 19 | + |
| 20 | +from oqd_core.analysis.utils import CFGNode |
| 21 | +from oqd_core.interface.analog import ( |
| 22 | + AnalogCircuit, |
| 23 | + Break, |
| 24 | + Continue, |
| 25 | + IfElse, |
| 26 | + While, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class AnalogCFGBuilder(RewriteRule): |
| 31 | + def __init__(self): |
| 32 | + super().__init__() |
| 33 | + self.registry = 0 |
| 34 | + self.cache = {} |
| 35 | + self.loop_stack = [] |
| 36 | + self.preds = [] |
| 37 | + self.founder = None |
| 38 | + self.last_node = None |
| 39 | + self.edge_labels = None |
| 40 | + self.fallthrough_labels = {} |
| 41 | + |
| 42 | + |
| 43 | + def new_node(self, preds, stmt, kind = "stmt"): |
| 44 | + node = CFGNode(register_id=self.registry, stmt=stmt,preds=preds, kind=kind) |
| 45 | + self.cache[node.register_id] = node |
| 46 | + self.registry += 1 |
| 47 | + |
| 48 | + explicit_labels = self.edge_labels or {} |
| 49 | + self.edge_labels = None |
| 50 | + |
| 51 | + for pred in node.preds: |
| 52 | + label = explicit_labels.get(pred.register_id) |
| 53 | + if label is None: |
| 54 | + label = self.fallthrough_labels.pop(pred.register_id, None) |
| 55 | + pred.add_succ(node, label=label) |
| 56 | + |
| 57 | + return node |
| 58 | + |
| 59 | + def walk_stmt(self, stmt, preds, edge_labels=None): |
| 60 | + old = self.preds |
| 61 | + old_labels = self.edge_labels |
| 62 | + self.preds = preds |
| 63 | + self.edge_labels = edge_labels |
| 64 | + result = self(stmt) |
| 65 | + self.preds = old |
| 66 | + self.edge_labels = old_labels |
| 67 | + return result |
| 68 | + |
| 69 | + def walk_block(self, statements, preds, entry_label=None): |
| 70 | + pred = preds |
| 71 | + first = True |
| 72 | + for stmt in statements: |
| 73 | + edge_labels = None |
| 74 | + if first and entry_label is not None: |
| 75 | + edge_labels = {p.register_id: entry_label for p in pred} |
| 76 | + pred = self.walk_stmt(stmt, pred, edge_labels=edge_labels) |
| 77 | + first = False |
| 78 | + return pred |
| 79 | + |
| 80 | + def run(self, circuit: AnalogCircuit): |
| 81 | + self.registry = 0 |
| 82 | + self.cache = {} |
| 83 | + self.loop_stack = [] |
| 84 | + self.edge_labels = None |
| 85 | + self.fallthrough_labels = {} |
| 86 | + self.founder = self.new_node([], "start", kind="start") |
| 87 | + exits = self.walk_stmt(circuit, [self.founder]) |
| 88 | + self.last_node = self.new_node(exits, "stop", kind="stop") |
| 89 | + return self.cache |
| 90 | + |
| 91 | + def map_AnalogCircuit(self, model: AnalogCircuit): |
| 92 | + return self.walk_block(model.statements, self.preds) |
| 93 | + |
| 94 | + def map_IfElse(self, model: IfElse): |
| 95 | + node = self.new_node(self.preds, model.condition, kind="branch") |
| 96 | + then_branch = self.walk_block(model.then_branch, [node], entry_label="true") |
| 97 | + if model.else_branch: |
| 98 | + else_branch = self.walk_block(model.else_branch, [node], entry_label="false") |
| 99 | + return list(then_branch) + (list(else_branch)) |
| 100 | + |
| 101 | + self.fallthrough_labels[node.register_id] = "false" |
| 102 | + |
| 103 | + return list(then_branch) + [node] |
| 104 | + |
| 105 | + def map_While(self, model: While): |
| 106 | + node = self.new_node(self.preds, model.condition, kind="branch") |
| 107 | + self.loop_stack.append(node) |
| 108 | + body = self.walk_block(model.body, [node], entry_label="true") |
| 109 | + self.loop_stack.pop() |
| 110 | + |
| 111 | + node.add_preds(body) |
| 112 | + self.fallthrough_labels[node.register_id] = "false" |
| 113 | + |
| 114 | + return node.exit_nodes + [node] |
| 115 | + |
| 116 | + def map_Break(self, model: Break): |
| 117 | + if not self.loop_stack: |
| 118 | + raise TypeError("break statement used outside loop") |
| 119 | + break_node = self.new_node(self.preds, model) |
| 120 | + self.loop_stack[-1].exit_nodes.append(break_node) |
| 121 | + self.fallthrough_labels[break_node.register_id] = "break" |
| 122 | + return [] |
| 123 | + |
| 124 | + def map_Continue(self, model: Continue): |
| 125 | + if not self.loop_stack: |
| 126 | + raise TypeError("continue statement used outside loop") |
| 127 | + continue_node = self.new_node(self.preds, model) |
| 128 | + self.loop_stack[-1].add_pred(continue_node, label="continue") |
| 129 | + return [] |
| 130 | + |
| 131 | + def generic_map(self, model): |
| 132 | + return [self.new_node(self.preds, model)] |
| 133 | + |
| 134 | + |
0 commit comments