|
| 1 | +########################################################################## |
| 2 | +# BTOR2 parser, code optimizer, and circuit miter |
| 3 | +# Copyright (C) 2024-2025 Amelia Dobis |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +########################################################################## |
| 18 | + |
| 19 | +from .program import * |
| 20 | +from .parser import * |
| 21 | +from tqdm import tqdm |
| 22 | + |
| 23 | +# Old API for module parsing |
| 24 | +def parse_file(file: list[str]) -> Program: |
| 25 | + parser = ModParser(file) |
| 26 | + return parser.parse_file() |
| 27 | + |
| 28 | +# Special parser that handles custom instructions |
| 29 | +class ModParser(Parser): |
| 30 | + def __init__(self, p_str): |
| 31 | + super().__init__(p_str) |
| 32 | + self.modules: list[Module] = [] |
| 33 | + self.contracts: list[Contract] = [] |
| 34 | + |
| 35 | + # Checks thaa a given module name has been defined |
| 36 | + def check_name(self, name: str) -> bool: |
| 37 | + return name in [m.name for m in self.modules] |
| 38 | + |
| 39 | + # Retrives a module by name from a list of parsed modules |
| 40 | + def get_module(self, name: str) -> Module: |
| 41 | + return [m for m in self.modules if m.name == name][0] |
| 42 | + |
| 43 | + # Extracts a body from an arbitrary code sequence |
| 44 | + # Returns the line idx at which the scanning ended |
| 45 | + def scan_body(self, i: int) -> tuple[list[str], int]: |
| 46 | + res = [] |
| 47 | + l = self.p_str[i].split(" ") |
| 48 | + ## Check that the declaration line ends with an '{' |
| 49 | + assert str(l[len(l)-1].strip()) == '{', f"invalid body start: {l[len(l)-1]}" |
| 50 | + |
| 51 | + i += 1 |
| 52 | + while self.p_str[i].strip() != "}": |
| 53 | + # Check that there are no nested structures |
| 54 | + lid = self.p_str[i].strip().split(" ")[0] |
| 55 | + assert lid.isnumeric(), f"All body lines must be instructions! Found: {lid}" |
| 56 | + res.append(self.p_str[i].strip()) |
| 57 | + i += 1 |
| 58 | + return (res, i) |
| 59 | + |
| 60 | + # Parses a ref instruction (only custom inst that is allowed in both modules and contracts) |
| 61 | + # @param inst: the pre-split ref instruction to be parsed |
| 62 | + # @param modules: the list of already parsed modules that can be referenced |
| 63 | + def parse_ref(self, inst: list[str]) -> Ref: |
| 64 | + ## Sanity check: Must be a ref instruction |
| 65 | + assert inst[1] == "ref", f"`parse_ref` can only handle ref instructions, not {inst[1]}!" |
| 66 | + ref_mod = inst[2] |
| 67 | + |
| 68 | + ## Sanity check: check that the name exists |
| 69 | + assert self.check_name(ref_mod), f"Named module {ref_mod} is undefined!" |
| 70 | + |
| 71 | + module = self.get_module(ref_mod) |
| 72 | + val = get_inst(module.body, int(inst[3])) |
| 73 | + return Ref(int(inst[0]), ref_mod, val) |
| 74 | + |
| 75 | + # Parse a module's pre-scanned body |
| 76 | + # @param body: the list of instructions contained within the body |
| 77 | + # @param modules: the list of already parsed modules that can be referenced |
| 78 | + def parse_module_body(self, body: list[str]) -> list[Instruction]: |
| 79 | + bodyParser = Parser(body) |
| 80 | + |
| 81 | + for line in body: |
| 82 | + inst = line.split(" ") |
| 83 | + if inst[0] == ";": # handle comments |
| 84 | + continue |
| 85 | + lid = int(inst[0]) |
| 86 | + tag = inst[1] |
| 87 | + match tag: |
| 88 | + # Handle special instructions |
| 89 | + case "inst": |
| 90 | + instd_mod = inst[2] |
| 91 | + ## Sanity check: check that the name exists |
| 92 | + assert self.check_name(instd_mod), f"Named module {instd_mod} is undefined!" |
| 93 | + bodyParser.p.append(Instance(lid, instd_mod)) |
| 94 | + |
| 95 | + case "ref": |
| 96 | + bodyParser.p.append(self.parse_ref(inst)) |
| 97 | + |
| 98 | + case "set": |
| 99 | + instance = bodyParser.find_inst(int(inst[2])) |
| 100 | + ref = bodyParser.find_inst(int(inst[3])) |
| 101 | + assert ref.name == instance.name, "`set` can only set a reference to an instance input!" |
| 102 | + alias = bodyParser.find_inst(int(inst[4])) |
| 103 | + bodyParser.p.append(Set(lid, instance, ref, alias)) |
| 104 | + |
| 105 | + # Handle standard instructions |
| 106 | + case _: |
| 107 | + bodyParser.parse_inst(line) |
| 108 | + |
| 109 | + return bodyParser.p |
| 110 | + |
| 111 | + # Parse a contract's pre-scanned body |
| 112 | + # @param name: the name given to the module |
| 113 | + # @param body: the list of instructions contained within the body |
| 114 | + # @param modules: the list of already parsed modules that can be referenced |
| 115 | + def parse_contract_body(self, body: list[str]) -> list[Instruction]: |
| 116 | + bodyParser = Parser(body) |
| 117 | + for line in body: |
| 118 | + inst = line.split(" ") |
| 119 | + if inst[0] == ";": # handle comments |
| 120 | + continue |
| 121 | + lid = int(inst[0]) |
| 122 | + tag = inst[1] |
| 123 | + match tag: |
| 124 | + # Handle special instructions |
| 125 | + case "prec": |
| 126 | + # Find the op associated to this instruction |
| 127 | + cond = bodyParser.find_inst(int(inst[2])) |
| 128 | + bodyParser.p.append(Prec(lid, cond)) |
| 129 | + |
| 130 | + case "post": |
| 131 | + # Find the op associated to this instruction |
| 132 | + cond = bodyParser.find_inst(int(inst[2])) |
| 133 | + bodyParser.p.append(Post(lid, cond)) |
| 134 | + |
| 135 | + case "ref": |
| 136 | + bodyParser.p.append(self.parse_ref(inst)) |
| 137 | + |
| 138 | + # Handle standard instructions |
| 139 | + case _: |
| 140 | + bodyParser.parse_inst(line) |
| 141 | + |
| 142 | + return bodyParser.p |
| 143 | + |
| 144 | + # Parse an entire file that can contain contracts and modules |
| 145 | + def parse_file(self) -> Program: |
| 146 | + i = 0 |
| 147 | + while i < len(self.p_str): |
| 148 | + symbols = self.p_str[i].strip().split(" ") |
| 149 | + # Check whether it's a module or a contract |
| 150 | + tag = symbols[0] |
| 151 | + match tag: |
| 152 | + case "module": |
| 153 | + name = symbols[1] |
| 154 | + # Scan and parse the body |
| 155 | + (body, i) = self.scan_body(i) |
| 156 | + b = self.parse_module_body(body) |
| 157 | + # Create and store the module |
| 158 | + self.modules.append(Module(name, b)) |
| 159 | + |
| 160 | + case "contract": |
| 161 | + name = symbols[1] |
| 162 | + assert self.check_name(name), f"Contract name {name} is not defined!" |
| 163 | + (body, i) = self.scan_body(i) |
| 164 | + body = self.parse_contract_body(body) |
| 165 | + # Create and store the module |
| 166 | + self.contracts.append(Contract(name, body)) |
| 167 | + |
| 168 | + case "}": |
| 169 | + i+=1 |
| 170 | + continue |
| 171 | + |
| 172 | + case _: |
| 173 | + print(f"Unsupported structure: {tag} is not module | contract") |
| 174 | + exit(1) |
| 175 | + |
| 176 | + return Program(self.modules, self.contracts) |
0 commit comments