Merge branch 'master' into clifford/fix1255
[yosys.git] / passes / opt / opt_expr.cc
index 1512cea1bab23e174babea935012e335ecb57fcb..cb89e90caa6a2e2c746df7f98fa8df0253da7a53 100644 (file)
@@ -51,9 +51,9 @@ void replace_undriven(RTLIL::Design *design, RTLIL::Module *module)
        }
 
        for (auto wire : module->wires()) {
-               if (wire->attributes.count("\\init")) {
+               if (wire->attributes.count(ID(init))) {
                        SigSpec sig = sigmap(wire);
-                       Const initval = wire->attributes.at("\\init");
+                       Const initval = wire->attributes.at(ID(init));
                        for (int i = 0; i < GetSize(initval) && i < GetSize(wire); i++) {
                                if (initval[i] == State::S0 || initval[i] == State::S1)
                                        initbits[sig[i]] = make_pair(wire, initval[i]);
@@ -61,7 +61,7 @@ void replace_undriven(RTLIL::Design *design, RTLIL::Module *module)
                }
                if (wire->port_input)
                        driven_signals.add(sigmap(wire));
-               if (wire->port_output || wire->get_bool_attribute("\\keep"))
+               if (wire->port_output || wire->get_bool_attribute(ID(keep)))
                        used_signals.add(sigmap(wire));
                all_signals.add(sigmap(wire));
        }
@@ -99,18 +99,18 @@ void replace_undriven(RTLIL::Design *design, RTLIL::Module *module)
 
                for (auto wire : revisit_initwires) {
                        SigSpec sig = sm2(wire);
-                       Const initval = wire->attributes.at("\\init");
+                       Const initval = wire->attributes.at(ID(init));
                        for (int i = 0; i < GetSize(initval) && i < GetSize(wire); i++) {
                                if (SigBit(initval[i]) == sig[i])
                                        initval[i] = State::Sx;
                        }
                        if (initval.is_fully_undef()) {
                                log_debug("Removing init attribute from %s/%s.\n", log_id(module), log_id(wire));
-                               wire->attributes.erase("\\init");
+                               wire->attributes.erase(ID(init));
                                did_something = true;
-                       } else if (initval != wire->attributes.at("\\init")) {
+                       } else if (initval != wire->attributes.at(ID(init))) {
                                log_debug("Updating init attribute on %s/%s: %s\n", log_id(module), log_id(wire), log_signal(initval));
-                               wire->attributes["\\init"] = initval;
+                               wire->attributes[ID(init)] = initval;
                                did_something = true;
                        }
                }
@@ -135,14 +135,14 @@ void replace_cell(SigMap &assign_map, RTLIL::Module *module, RTLIL::Cell *cell,
 
 bool group_cell_inputs(RTLIL::Module *module, RTLIL::Cell *cell, bool commutative, SigMap &sigmap)
 {
-       std::string b_name = cell->hasPort("\\B") ? "\\B" : "\\A";
+       IdString b_name = cell->hasPort(ID(B)) ? ID(B) : ID(A);
 
-       bool a_signed = cell->parameters.at("\\A_SIGNED").as_bool();
-       bool b_signed = cell->parameters.at(b_name + "_SIGNED").as_bool();
+       bool a_signed = cell->parameters.at(ID(A_SIGNED)).as_bool();
+       bool b_signed = cell->parameters.at(b_name.str() + "_SIGNED").as_bool();
 
-       RTLIL::SigSpec sig_a = sigmap(cell->getPort("\\A"));
+       RTLIL::SigSpec sig_a = sigmap(cell->getPort(ID(A)));
        RTLIL::SigSpec sig_b = sigmap(cell->getPort(b_name));
-       RTLIL::SigSpec sig_y = sigmap(cell->getPort("\\Y"));
+       RTLIL::SigSpec sig_y = sigmap(cell->getPort(ID(Y)));
 
        sig_a.extend_u0(sig_y.size(), a_signed);
        sig_b.extend_u0(sig_y.size(), b_signed);
@@ -157,10 +157,10 @@ bool group_cell_inputs(RTLIL::Module *module, RTLIL::Cell *cell, bool commutativ
                int group_idx = GRP_DYN;
                RTLIL::SigBit bit_a = bits_a[i], bit_b = bits_b[i];
 
-               if (cell->type == "$or" && (bit_a == RTLIL::State::S1 || bit_b == RTLIL::State::S1))
+               if (cell->type == ID($or) && (bit_a == RTLIL::State::S1 || bit_b == RTLIL::State::S1))
                        bit_a = bit_b = RTLIL::State::S1;
 
-               if (cell->type == "$and" && (bit_a == RTLIL::State::S0 || bit_b == RTLIL::State::S0))
+               if (cell->type == ID($and) && (bit_a == RTLIL::State::S0 || bit_b == RTLIL::State::S0))
                        bit_a = bit_b = RTLIL::State::S0;
 
                if (bit_a.wire == NULL && bit_b.wire == NULL)
@@ -200,7 +200,7 @@ bool group_cell_inputs(RTLIL::Module *module, RTLIL::Cell *cell, bool commutativ
                        new_b.append_bit(it.first.second);
                }
 
-               if (cell->type.in("$and", "$or") && i == GRP_CONST_A) {
+               if (cell->type.in(ID($and), ID($or)) && i == GRP_CONST_A) {
                        log_debug("  Direct Connection: %s (%s with %s)\n", log_signal(new_b), log_id(cell->type), log_signal(new_a));
                        module->connect(new_y, new_b);
                        module->connect(new_conn);
@@ -209,24 +209,24 @@ bool group_cell_inputs(RTLIL::Module *module, RTLIL::Cell *cell, bool commutativ
 
                RTLIL::Cell *c = module->addCell(NEW_ID, cell->type);
 
-               c->setPort("\\A", new_a);
-               c->parameters["\\A_WIDTH"] = new_a.size();
-               c->parameters["\\A_SIGNED"] = false;
+               c->setPort(ID(A), new_a);
+               c->parameters[ID(A_WIDTH)] = new_a.size();
+               c->parameters[ID(A_SIGNED)] = false;
 
-               if (b_name == "\\B") {
-                       c->setPort("\\B", new_b);
-                       c->parameters["\\B_WIDTH"] = new_b.size();
-                       c->parameters["\\B_SIGNED"] = false;
+               if (b_name == ID(B)) {
+                       c->setPort(ID(B), new_b);
+                       c->parameters[ID(B_WIDTH)] = new_b.size();
+                       c->parameters[ID(B_SIGNED)] = false;
                }
 
-               c->setPort("\\Y", new_y);
-               c->parameters["\\Y_WIDTH"] = new_y->width;
+               c->setPort(ID(Y), new_y);
+               c->parameters[ID(Y_WIDTH)] = new_y->width;
                c->check();
 
                module->connect(new_conn);
 
                log_debug("  New cell `%s': A=%s", log_id(c), log_signal(new_a));
-               if (b_name == "\\B")
+               if (b_name == ID(B))
                        log_debug(", B=%s", log_signal(new_b));
                log_debug("\n");
        }
@@ -368,12 +368,12 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
 
        for (auto cell : module->cells())
                if (design->selected(module, cell) && cell->type[0] == '$') {
-                       if (cell->type.in("$_NOT_", "$not", "$logic_not") &&
-                                       cell->getPort("\\A").size() == 1 && cell->getPort("\\Y").size() == 1)
-                               invert_map[assign_map(cell->getPort("\\Y"))] = assign_map(cell->getPort("\\A"));
-                       if (cell->type.in("$mux", "$_MUX_") &&
-                                       cell->getPort("\\A") == SigSpec(State::S1) && cell->getPort("\\B") == SigSpec(State::S0))
-                               invert_map[assign_map(cell->getPort("\\Y"))] = assign_map(cell->getPort("\\S"));
+                       if (cell->type.in(ID($_NOT_), ID($not), ID($logic_not)) &&
+                                       cell->getPort(ID(A)).size() == 1 && cell->getPort(ID(Y)).size() == 1)
+                               invert_map[assign_map(cell->getPort(ID(Y)))] = assign_map(cell->getPort(ID(A)));
+                       if (cell->type.in(ID($mux), ID($_MUX_)) &&
+                                       cell->getPort(ID(A)) == SigSpec(State::S1) && cell->getPort(ID(B)) == SigSpec(State::S0))
+                               invert_map[assign_map(cell->getPort(ID(Y)))] = assign_map(cell->getPort(ID(S)));
                        if (ct_combinational.cell_known(cell->type))
                                for (auto &conn : cell->connections()) {
                                        RTLIL::SigSpec sig = assign_map(conn.second);
@@ -397,66 +397,66 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
        for (auto cell : cells.sorted)
        {
 #define ACTION_DO(_p_, _s_) do { cover("opt.opt_expr.action_" S__LINE__); replace_cell(assign_map, module, cell, input.as_string(), _p_, _s_); goto next_cell; } while (0)
-#define ACTION_DO_Y(_v_) ACTION_DO("\\Y", RTLIL::SigSpec(RTLIL::State::S ## _v_))
+#define ACTION_DO_Y(_v_) ACTION_DO(ID(Y), RTLIL::SigSpec(RTLIL::State::S ## _v_))
 
                if (clkinv)
                {
-                       if (cell->type.in("$dff", "$dffe", "$dffsr", "$adff", "$fsm", "$memrd", "$memwr"))
-                               handle_polarity_inv(cell, "\\CLK", "\\CLK_POLARITY", assign_map, invert_map);
+                       if (cell->type.in(ID($dff), ID($dffe), ID($dffsr), ID($adff), ID($fsm), ID($memrd), ID($memwr)))
+                               handle_polarity_inv(cell, ID(CLK), ID(CLK_POLARITY), assign_map, invert_map);
 
-                       if (cell->type.in("$sr", "$dffsr", "$dlatchsr")) {
-                               handle_polarity_inv(cell, "\\SET", "\\SET_POLARITY", assign_map, invert_map);
-                               handle_polarity_inv(cell, "\\CLR", "\\CLR_POLARITY", assign_map, invert_map);
+                       if (cell->type.in(ID($sr), ID($dffsr), ID($dlatchsr))) {
+                               handle_polarity_inv(cell, ID(SET), ID(SET_POLARITY), assign_map, invert_map);
+                               handle_polarity_inv(cell, ID(CLR), ID(CLR_POLARITY), assign_map, invert_map);
                        }
 
-                       if (cell->type.in("$dffe", "$dlatch", "$dlatchsr"))
-                               handle_polarity_inv(cell, "\\EN", "\\EN_POLARITY", assign_map, invert_map);
+                       if (cell->type.in(ID($dffe), ID($dlatch), ID($dlatchsr)))
+                               handle_polarity_inv(cell, ID(EN), ID(EN_POLARITY), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_SR_N?_", "$_SR_P?_", "\\S", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_SR_?N_", "$_SR_?P_", "\\R", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_SR_N?_", "$_SR_P?_", ID(S), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_SR_?N_", "$_SR_?P_", ID(R), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_DFF_N_", "$_DFF_P_", "\\C", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFF_N_", "$_DFF_P_", ID(C), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_DFFE_N?_", "$_DFFE_P?_", "\\C", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_DFFE_?N_", "$_DFFE_?P_", "\\E", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFFE_N?_", "$_DFFE_P?_", ID(C), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFFE_?N_", "$_DFFE_?P_", ID(E), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_DFF_N??_", "$_DFF_P??_", "\\C", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_DFF_?N?_", "$_DFF_?P?_", "\\R", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFF_N??_", "$_DFF_P??_", ID(C), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFF_?N?_", "$_DFF_?P?_", ID(R), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_DFFSR_N??_", "$_DFFSR_P??_", "\\C", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_DFFSR_?N?_", "$_DFFSR_?P?_", "\\S", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_DFFSR_??N_", "$_DFFSR_??P_", "\\R", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFFSR_N??_", "$_DFFSR_P??_", ID(C), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFFSR_?N?_", "$_DFFSR_?P?_", ID(S), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DFFSR_??N_", "$_DFFSR_??P_", ID(R), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_DLATCH_N_", "$_DLATCH_P_", "\\E", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DLATCH_N_", "$_DLATCH_P_", ID(E), assign_map, invert_map);
 
-                       handle_clkpol_celltype_swap(cell, "$_DLATCHSR_N??_", "$_DLATCHSR_P??_", "\\E", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_DLATCHSR_?N?_", "$_DLATCHSR_?P?_", "\\S", assign_map, invert_map);
-                       handle_clkpol_celltype_swap(cell, "$_DLATCHSR_??N_", "$_DLATCHSR_??P_", "\\R", assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DLATCHSR_N??_", "$_DLATCHSR_P??_", ID(E), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DLATCHSR_?N?_", "$_DLATCHSR_?P?_", ID(S), assign_map, invert_map);
+                       handle_clkpol_celltype_swap(cell, "$_DLATCHSR_??N_", "$_DLATCHSR_??P_", ID(R), assign_map, invert_map);
                }
 
                bool detect_const_and = false;
                bool detect_const_or = false;
 
-               if (cell->type.in("$reduce_and", "$_AND_"))
+               if (cell->type.in(ID($reduce_and), ID($_AND_)))
                        detect_const_and = true;
 
-               if (cell->type.in("$and", "$logic_and") && GetSize(cell->getPort("\\A")) == 1 && GetSize(cell->getPort("\\B")) == 1 && !cell->getParam("\\A_SIGNED").as_bool())
+               if (cell->type.in(ID($and), ID($logic_and)) && GetSize(cell->getPort(ID(A))) == 1 && GetSize(cell->getPort(ID(B))) == 1 && !cell->getParam(ID(A_SIGNED)).as_bool())
                        detect_const_and = true;
 
-               if (cell->type.in("$reduce_or", "$reduce_bool", "$_OR_"))
+               if (cell->type.in(ID($reduce_or), ID($reduce_bool), ID($_OR_)))
                        detect_const_or = true;
 
-               if (cell->type.in("$or", "$logic_or") && GetSize(cell->getPort("\\A")) == 1 && GetSize(cell->getPort("\\B")) == 1 && !cell->getParam("\\A_SIGNED").as_bool())
+               if (cell->type.in(ID($or), ID($logic_or)) && GetSize(cell->getPort(ID(A))) == 1 && GetSize(cell->getPort(ID(B))) == 1 && !cell->getParam(ID(A_SIGNED)).as_bool())
                        detect_const_or = true;
 
                if (detect_const_and || detect_const_or)
                {
-                       pool<SigBit> input_bits = assign_map(cell->getPort("\\A")).to_sigbit_pool();
+                       pool<SigBit> input_bits = assign_map(cell->getPort(ID(A))).to_sigbit_pool();
                        bool found_zero = false, found_one = false, found_undef = false, found_inv = false, many_conconst = false;
                        SigBit non_const_input = State::Sm;
 
-                       if (cell->hasPort("\\B")) {
-                               vector<SigBit> more_bits = assign_map(cell->getPort("\\B")).to_sigbit_vector();
+                       if (cell->hasPort(ID(B))) {
+                               vector<SigBit> more_bits = assign_map(cell->getPort(ID(B))).to_sigbit_vector();
                                input_bits.insert(more_bits.begin(), more_bits.end());
                        }
 
@@ -496,14 +496,14 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        }
                }
 
-               if (cell->type.in("$reduce_and", "$reduce_or", "$reduce_bool", "$reduce_xor", "$reduce_xnor", "$neg") &&
-                               GetSize(cell->getPort("\\A")) == 1 && GetSize(cell->getPort("\\Y")) == 1)
+               if (cell->type.in(ID($reduce_and), ID($reduce_or), ID($reduce_bool), ID($reduce_xor), ID($reduce_xnor), ID($neg)) &&
+                               GetSize(cell->getPort(ID(A))) == 1 && GetSize(cell->getPort(ID(Y))) == 1)
                {
-                       if (cell->type == "$reduce_xnor") {
+                       if (cell->type == ID($reduce_xnor)) {
                                cover("opt.opt_expr.reduce_xnor_not");
                                log_debug("Replacing %s cell `%s' in module `%s' with $not cell.\n",
                                                log_id(cell->type), log_id(cell->name), log_id(module));
-                               cell->type = "$not";
+                               cell->type = ID($not);
                                did_something = true;
                        } else {
                                cover("opt.opt_expr.unary_buffer");
@@ -514,15 +514,15 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
 
                if (do_fine)
                {
-                       if (cell->type.in("$not", "$pos", "$and", "$or", "$xor", "$xnor"))
+                       if (cell->type.in(ID($not), ID($pos), ID($and), ID($or), ID($xor), ID($xnor)))
                                if (group_cell_inputs(module, cell, true, assign_map))
                                        goto next_cell;
 
-                       if (cell->type.in("$logic_not", "$logic_and", "$logic_or", "$reduce_or", "$reduce_and", "$reduce_bool"))
+                       if (cell->type.in(ID($logic_not), ID($logic_and), ID($logic_or), ID($reduce_or), ID($reduce_and), ID($reduce_bool)))
                        {
-                               SigBit neutral_bit = cell->type == "$reduce_and" ? State::S1 : State::S0;
+                               SigBit neutral_bit = cell->type == ID($reduce_and) ? State::S1 : State::S0;
 
-                               RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
+                               RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
                                RTLIL::SigSpec new_sig_a;
 
                                for (auto bit : sig_a)
@@ -535,17 +535,17 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        cover_list("opt.opt_expr.fine.neutral_A", "$logic_not", "$logic_and", "$logic_or", "$reduce_or", "$reduce_and", "$reduce_bool", cell->type.str());
                                        log_debug("Replacing port A of %s cell `%s' in module `%s' with shorter expression: %s -> %s\n",
                                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str(), log_signal(sig_a), log_signal(new_sig_a));
-                                       cell->setPort("\\A", new_sig_a);
-                                       cell->parameters.at("\\A_WIDTH") = GetSize(new_sig_a);
+                                       cell->setPort(ID(A), new_sig_a);
+                                       cell->parameters.at(ID(A_WIDTH)) = GetSize(new_sig_a);
                                        did_something = true;
                                }
                        }
 
-                       if (cell->type.in("$logic_and", "$logic_or"))
+                       if (cell->type.in(ID($logic_and), ID($logic_or)))
                        {
                                SigBit neutral_bit = State::S0;
 
-                               RTLIL::SigSpec sig_b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigSpec sig_b = assign_map(cell->getPort(ID(B)));
                                RTLIL::SigSpec new_sig_b;
 
                                for (auto bit : sig_b)
@@ -558,15 +558,15 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        cover_list("opt.opt_expr.fine.neutral_B", "$logic_and", "$logic_or", cell->type.str());
                                        log_debug("Replacing port B of %s cell `%s' in module `%s' with shorter expression: %s -> %s\n",
                                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str(), log_signal(sig_b), log_signal(new_sig_b));
-                                       cell->setPort("\\B", new_sig_b);
-                                       cell->parameters.at("\\B_WIDTH") = GetSize(new_sig_b);
+                                       cell->setPort(ID(B), new_sig_b);
+                                       cell->parameters.at(ID(B_WIDTH)) = GetSize(new_sig_b);
                                        did_something = true;
                                }
                        }
 
-                       if (cell->type == "$reduce_and")
+                       if (cell->type == ID($reduce_and))
                        {
-                               RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
+                               RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
 
                                RTLIL::State new_a = RTLIL::State::S1;
                                for (auto &bit : sig_a.to_sigbit_vector())
@@ -584,15 +584,15 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        cover("opt.opt_expr.fine.$reduce_and");
                                        log_debug("Replacing port A of %s cell `%s' in module `%s' with constant driver: %s -> %s\n",
                                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str(), log_signal(sig_a), log_signal(new_a));
-                                       cell->setPort("\\A", sig_a = new_a);
-                                       cell->parameters.at("\\A_WIDTH") = 1;
+                                       cell->setPort(ID(A), sig_a = new_a);
+                                       cell->parameters.at(ID(A_WIDTH)) = 1;
                                        did_something = true;
                                }
                        }
 
-                       if (cell->type.in("$logic_not", "$logic_and", "$logic_or", "$reduce_or", "$reduce_bool"))
+                       if (cell->type.in(ID($logic_not), ID($logic_and), ID($logic_or), ID($reduce_or), ID($reduce_bool)))
                        {
-                               RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
+                               RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
 
                                RTLIL::State new_a = RTLIL::State::S0;
                                for (auto &bit : sig_a.to_sigbit_vector())
@@ -610,15 +610,15 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        cover_list("opt.opt_expr.fine.A", "$logic_not", "$logic_and", "$logic_or", "$reduce_or", "$reduce_bool", cell->type.str());
                                        log_debug("Replacing port A of %s cell `%s' in module `%s' with constant driver: %s -> %s\n",
                                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str(), log_signal(sig_a), log_signal(new_a));
-                                       cell->setPort("\\A", sig_a = new_a);
-                                       cell->parameters.at("\\A_WIDTH") = 1;
+                                       cell->setPort(ID(A), sig_a = new_a);
+                                       cell->parameters.at(ID(A_WIDTH)) = 1;
                                        did_something = true;
                                }
                        }
 
-                       if (cell->type.in("$logic_and", "$logic_or"))
+                       if (cell->type.in(ID($logic_and), ID($logic_or)))
                        {
-                               RTLIL::SigSpec sig_b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigSpec sig_b = assign_map(cell->getPort(ID(B)));
 
                                RTLIL::State new_b = RTLIL::State::S0;
                                for (auto &bit : sig_b.to_sigbit_vector())
@@ -636,17 +636,18 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        cover_list("opt.opt_expr.fine.B", "$logic_and", "$logic_or", cell->type.str());
                                        log_debug("Replacing port B of %s cell `%s' in module `%s' with constant driver: %s -> %s\n",
                                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str(), log_signal(sig_b), log_signal(new_b));
-                                       cell->setPort("\\B", sig_b = new_b);
-                                       cell->parameters.at("\\B_WIDTH") = 1;
+                                       cell->setPort(ID(B), sig_b = new_b);
+                                       cell->parameters.at(ID(B_WIDTH)) = 1;
                                        did_something = true;
                                }
                        }
 
-                       if (cell->type.in("$add", "$sub")) {
-                               RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
-                               RTLIL::SigSpec sig_b = assign_map(cell->getPort("\\B"));
-                               RTLIL::SigSpec sig_y = cell->getPort("\\Y");
-                               bool sub = cell->type == "$sub";
+                       if (cell->type.in(ID($add), ID($sub)))
+                       {
+                               RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
+                               RTLIL::SigSpec sig_b = assign_map(cell->getPort(ID(B)));
+                               RTLIL::SigSpec sig_y = cell->getPort(ID(Y));
+                               bool sub = cell->type == ID($sub);
 
                                int i;
                                for (i = 0; i < GetSize(sig_y); i++) {
@@ -659,22 +660,69 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                }
                                if (i > 0) {
                                        cover_list("opt.opt_expr.fine", "$add", "$sub", cell->type.str());
+                                       cell->setPort(ID(A), sig_a.extract_end(i));
+                                       cell->setPort(ID(B), sig_b.extract_end(i));
+                                       cell->setPort(ID(Y), sig_y.extract_end(i));
+                                       cell->fixup_parameters();
+                                       did_something = true;
+                               }
+                       }
+
+                       if (cell->type == "$alu")
+                       {
+                               RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
+                               RTLIL::SigSpec sig_b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigBit sig_ci = assign_map(cell->getPort("\\CI"));
+                               RTLIL::SigBit sig_bi = assign_map(cell->getPort("\\BI"));
+                               RTLIL::SigSpec sig_x = cell->getPort("\\X");
+                               RTLIL::SigSpec sig_y = cell->getPort("\\Y");
+                               RTLIL::SigSpec sig_co = cell->getPort("\\CO");
+
+                               if (sig_ci.wire || sig_bi.wire)
+                                       goto next_cell;
+
+                               bool sub = (sig_ci == State::S1 && sig_bi == State::S1);
+
+                               // If not a subtraction, yet there is a carry or B is inverted
+                               //   then no optimisation is possible as carry will not be constant
+                               if (!sub && (sig_ci != State::S0 || sig_bi != State::S0))
+                                       goto next_cell;
+
+                               int i;
+                               for (i = 0; i < GetSize(sig_y); i++) {
+                                       if (sig_b.at(i, State::Sx) == State::S0 && sig_a.at(i, State::Sx) != State::Sx) {
+                                               module->connect(sig_x[i], sub ? module->Not(NEW_ID, sig_a[i]).as_bit() : sig_a[i]);
+                                               module->connect(sig_y[i], sig_a[i]);
+                                               module->connect(sig_co[i], sub ? State::S1 : State::S0);
+                                       }
+                                       else if (!sub && sig_a.at(i, State::Sx) == State::S0 && sig_b.at(i, State::Sx) != State::Sx) {
+                                               module->connect(sig_x[i], sig_b[i]);
+                                               module->connect(sig_y[i], sig_b[i]);
+                                               module->connect(sig_co[i], State::S0);
+                                       }
+                                       else
+                                               break;
+                               }
+                               if (i > 0) {
+                                       cover("opt.opt_expr.fine.$alu");
                                        cell->setPort("\\A", sig_a.extract_end(i));
                                        cell->setPort("\\B", sig_b.extract_end(i));
+                                       cell->setPort("\\X", sig_x.extract_end(i));
                                        cell->setPort("\\Y", sig_y.extract_end(i));
+                                       cell->setPort("\\CO", sig_co.extract_end(i));
                                        cell->fixup_parameters();
                                        did_something = true;
                                }
                        }
                }
 
-               if (cell->type.in("$reduce_xor", "$reduce_xnor", "$shift", "$shiftx", "$shl", "$shr", "$sshl", "$sshr",
-                                       "$lt", "$le", "$ge", "$gt", "$neg", "$add", "$sub", "$mul", "$div", "$mod", "$pow"))
+               if (cell->type.in(ID($reduce_xor), ID($reduce_xnor), ID($shift), ID($shiftx), ID($shl), ID($shr), ID($sshl), ID($sshr),
+                                       ID($lt), ID($le), ID($ge), ID($gt), ID($neg), ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($pow)))
                {
-                       RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
-                       RTLIL::SigSpec sig_b = cell->hasPort("\\B") ? assign_map(cell->getPort("\\B")) : RTLIL::SigSpec();
+                       RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
+                       RTLIL::SigSpec sig_b = cell->hasPort(ID(B)) ? assign_map(cell->getPort(ID(B))) : RTLIL::SigSpec();
 
-                       if (cell->type.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx"))
+                       if (cell->type.in(ID($shl), ID($shr), ID($sshl), ID($sshr), ID($shift), ID($shiftx)))
                                sig_a = RTLIL::SigSpec();
 
                        for (auto &bit : sig_a.to_sigbit_vector())
@@ -689,7 +737,7 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                found_the_x_bit:
                                cover_list("opt.opt_expr.xbit", "$reduce_xor", "$reduce_xnor", "$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx",
                                                "$lt", "$le", "$ge", "$gt", "$neg", "$add", "$sub", "$mul", "$div", "$mod", "$pow", cell->type.str());
-                               if (cell->type.in("$reduce_xor", "$reduce_xnor", "$lt", "$le", "$ge", "$gt"))
+                               if (cell->type.in(ID($reduce_xor), ID($reduce_xnor), ID($lt), ID($le), ID($ge), ID($gt)))
                                        replace_cell(assign_map, module, cell, "x-bit in input", "\\Y", RTLIL::State::Sx);
                                else
                                        replace_cell(assign_map, module, cell, "x-bit in input", "\\Y", RTLIL::SigSpec(RTLIL::State::Sx, cell->getPort("\\Y").size()));
@@ -697,36 +745,36 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        }
                }
 
-               if (cell->type.in("$_NOT_", "$not", "$logic_not") && cell->getPort("\\Y").size() == 1 &&
-                               invert_map.count(assign_map(cell->getPort("\\A"))) != 0) {
+               if (cell->type.in(ID($_NOT_), ID($not), ID($logic_not)) && cell->getPort(ID(Y)).size() == 1 &&
+                               invert_map.count(assign_map(cell->getPort(ID(A)))) != 0) {
                        cover_list("opt.opt_expr.invert.double", "$_NOT_", "$not", "$logic_not", cell->type.str());
                        replace_cell(assign_map, module, cell, "double_invert", "\\Y", invert_map.at(assign_map(cell->getPort("\\A"))));
                        goto next_cell;
                }
 
-               if (cell->type.in("$_MUX_", "$mux") && invert_map.count(assign_map(cell->getPort("\\S"))) != 0) {
+               if (cell->type.in(ID($_MUX_), ID($mux)) && invert_map.count(assign_map(cell->getPort(ID(S)))) != 0) {
                        cover_list("opt.opt_expr.invert.muxsel", "$_MUX_", "$mux", cell->type.str());
                        log_debug("Optimizing away select inverter for %s cell `%s' in module `%s'.\n", log_id(cell->type), log_id(cell), log_id(module));
-                       RTLIL::SigSpec tmp = cell->getPort("\\A");
-                       cell->setPort("\\A", cell->getPort("\\B"));
-                       cell->setPort("\\B", tmp);
-                       cell->setPort("\\S", invert_map.at(assign_map(cell->getPort("\\S"))));
+                       RTLIL::SigSpec tmp = cell->getPort(ID(A));
+                       cell->setPort(ID(A), cell->getPort(ID(B)));
+                       cell->setPort(ID(B), tmp);
+                       cell->setPort(ID(S), invert_map.at(assign_map(cell->getPort(ID(S)))));
                        did_something = true;
                        goto next_cell;
                }
 
-               if (cell->type == "$_NOT_") {
-                       RTLIL::SigSpec input = cell->getPort("\\A");
+               if (cell->type == ID($_NOT_)) {
+                       RTLIL::SigSpec input = cell->getPort(ID(A));
                        assign_map.apply(input);
                        if (input.match("1")) ACTION_DO_Y(0);
                        if (input.match("0")) ACTION_DO_Y(1);
                        if (input.match("*")) ACTION_DO_Y(x);
                }
 
-               if (cell->type == "$_AND_") {
+               if (cell->type == ID($_AND_)) {
                        RTLIL::SigSpec input;
-                       input.append(cell->getPort("\\B"));
-                       input.append(cell->getPort("\\A"));
+                       input.append(cell->getPort(ID(B)));
+                       input.append(cell->getPort(ID(A)));
                        assign_map.apply(input);
                        if (input.match(" 0")) ACTION_DO_Y(0);
                        if (input.match("0 ")) ACTION_DO_Y(0);
@@ -738,14 +786,14 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                if (input.match(" *")) ACTION_DO_Y(0);
                                if (input.match("* ")) ACTION_DO_Y(0);
                        }
-                       if (input.match(" 1")) ACTION_DO("\\Y", input.extract(1, 1));
-                       if (input.match("1 ")) ACTION_DO("\\Y", input.extract(0, 1));
+                       if (input.match(" 1")) ACTION_DO(ID(Y), input.extract(1, 1));
+                       if (input.match("1 ")) ACTION_DO(ID(Y), input.extract(0, 1));
                }
 
-               if (cell->type == "$_OR_") {
+               if (cell->type == ID($_OR_)) {
                        RTLIL::SigSpec input;
-                       input.append(cell->getPort("\\B"));
-                       input.append(cell->getPort("\\A"));
+                       input.append(cell->getPort(ID(B)));
+                       input.append(cell->getPort(ID(A)));
                        assign_map.apply(input);
                        if (input.match(" 1")) ACTION_DO_Y(1);
                        if (input.match("1 ")) ACTION_DO_Y(1);
@@ -757,14 +805,14 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                if (input.match(" *")) ACTION_DO_Y(1);
                                if (input.match("* ")) ACTION_DO_Y(1);
                        }
-                       if (input.match(" 0")) ACTION_DO("\\Y", input.extract(1, 1));
-                       if (input.match("0 ")) ACTION_DO("\\Y", input.extract(0, 1));
+                       if (input.match(" 0")) ACTION_DO(ID(Y), input.extract(1, 1));
+                       if (input.match("0 ")) ACTION_DO(ID(Y), input.extract(0, 1));
                }
 
-               if (cell->type == "$_XOR_") {
+               if (cell->type == ID($_XOR_)) {
                        RTLIL::SigSpec input;
-                       input.append(cell->getPort("\\B"));
-                       input.append(cell->getPort("\\A"));
+                       input.append(cell->getPort(ID(B)));
+                       input.append(cell->getPort(ID(A)));
                        assign_map.apply(input);
                        if (input.match("00")) ACTION_DO_Y(0);
                        if (input.match("01")) ACTION_DO_Y(1);
@@ -772,27 +820,27 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        if (input.match("11")) ACTION_DO_Y(0);
                        if (input.match(" *")) ACTION_DO_Y(x);
                        if (input.match("* ")) ACTION_DO_Y(x);
-                       if (input.match(" 0")) ACTION_DO("\\Y", input.extract(1, 1));
-                       if (input.match("0 ")) ACTION_DO("\\Y", input.extract(0, 1));
+                       if (input.match(" 0")) ACTION_DO(ID(Y), input.extract(1, 1));
+                       if (input.match("0 ")) ACTION_DO(ID(Y), input.extract(0, 1));
                }
 
-               if (cell->type == "$_MUX_") {
+               if (cell->type == ID($_MUX_)) {
                        RTLIL::SigSpec input;
-                       input.append(cell->getPort("\\S"));
-                       input.append(cell->getPort("\\B"));
-                       input.append(cell->getPort("\\A"));
+                       input.append(cell->getPort(ID(S)));
+                       input.append(cell->getPort(ID(B)));
+                       input.append(cell->getPort(ID(A)));
                        assign_map.apply(input);
                        if (input.extract(2, 1) == input.extract(1, 1))
-                               ACTION_DO("\\Y", input.extract(2, 1));
-                       if (input.match("  0")) ACTION_DO("\\Y", input.extract(2, 1));
-                       if (input.match("  1")) ACTION_DO("\\Y", input.extract(1, 1));
-                       if (input.match("01 ")) ACTION_DO("\\Y", input.extract(0, 1));
+                               ACTION_DO(ID(Y), input.extract(2, 1));
+                       if (input.match("  0")) ACTION_DO(ID(Y), input.extract(2, 1));
+                       if (input.match("  1")) ACTION_DO(ID(Y), input.extract(1, 1));
+                       if (input.match("01 ")) ACTION_DO(ID(Y), input.extract(0, 1));
                        if (input.match("10 ")) {
                                cover("opt.opt_expr.mux_to_inv");
-                               cell->type = "$_NOT_";
-                               cell->setPort("\\A", input.extract(0, 1));
-                               cell->unsetPort("\\B");
-                               cell->unsetPort("\\S");
+                               cell->type = ID($_NOT_);
+                               cell->setPort(ID(A), input.extract(0, 1));
+                               cell->unsetPort(ID(B));
+                               cell->unsetPort(ID(S));
                                goto next_cell;
                        }
                        if (input.match("11 ")) ACTION_DO_Y(1);
@@ -801,38 +849,38 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        if (input.match("01*")) ACTION_DO_Y(x);
                        if (input.match("10*")) ACTION_DO_Y(x);
                        if (mux_undef) {
-                               if (input.match("*  ")) ACTION_DO("\\Y", input.extract(1, 1));
-                               if (input.match(" * ")) ACTION_DO("\\Y", input.extract(2, 1));
-                               if (input.match("  *")) ACTION_DO("\\Y", input.extract(2, 1));
+                               if (input.match("*  ")) ACTION_DO(ID(Y), input.extract(1, 1));
+                               if (input.match(" * ")) ACTION_DO(ID(Y), input.extract(2, 1));
+                               if (input.match("  *")) ACTION_DO(ID(Y), input.extract(2, 1));
                        }
                }
 
-               if (cell->type.in("$_TBUF_", "$tribuf")) {
-                       RTLIL::SigSpec input = cell->getPort(cell->type == "$_TBUF_" ? "\\E" : "\\EN");
-                       RTLIL::SigSpec a = cell->getPort("\\A");
+               if (cell->type.in(ID($_TBUF_), ID($tribuf))) {
+                       RTLIL::SigSpec input = cell->getPort(cell->type == ID($_TBUF_) ? ID(E) : ID(EN));
+                       RTLIL::SigSpec a = cell->getPort(ID(A));
                        assign_map.apply(input);
                        assign_map.apply(a);
                        if (input == State::S1)
-                               ACTION_DO("\\Y", cell->getPort("\\A"));
+                               ACTION_DO(ID(Y), cell->getPort(ID(A)));
                        if (input == State::S0 && !a.is_fully_undef()) {
                                cover("opt.opt_expr.action_" S__LINE__);
                                log_debug("Replacing data input of %s cell `%s' in module `%s' with constant undef.\n",
                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str());
-                               cell->setPort("\\A", SigSpec(State::Sx, GetSize(a)));
+                               cell->setPort(ID(A), SigSpec(State::Sx, GetSize(a)));
                                did_something = true;
                                goto next_cell;
                        }
                }
 
-               if (cell->type.in("$eq", "$ne", "$eqx", "$nex"))
+               if (cell->type.in(ID($eq), ID($ne), ID($eqx), ID($nex)))
                {
-                       RTLIL::SigSpec a = cell->getPort("\\A");
-                       RTLIL::SigSpec b = cell->getPort("\\B");
+                       RTLIL::SigSpec a = cell->getPort(ID(A));
+                       RTLIL::SigSpec b = cell->getPort(ID(B));
 
-                       if (cell->parameters["\\A_WIDTH"].as_int() != cell->parameters["\\B_WIDTH"].as_int()) {
-                               int width = max(cell->parameters["\\A_WIDTH"].as_int(), cell->parameters["\\B_WIDTH"].as_int());
-                               a.extend_u0(width, cell->parameters["\\A_SIGNED"].as_bool() && cell->parameters["\\B_SIGNED"].as_bool());
-                               b.extend_u0(width, cell->parameters["\\A_SIGNED"].as_bool() && cell->parameters["\\B_SIGNED"].as_bool());
+                       if (cell->parameters[ID(A_WIDTH)].as_int() != cell->parameters[ID(B_WIDTH)].as_int()) {
+                               int width = max(cell->parameters[ID(A_WIDTH)].as_int(), cell->parameters[ID(B_WIDTH)].as_int());
+                               a.extend_u0(width, cell->parameters[ID(A_SIGNED)].as_bool() && cell->parameters[ID(B_SIGNED)].as_bool());
+                               b.extend_u0(width, cell->parameters[ID(A_SIGNED)].as_bool() && cell->parameters[ID(B_SIGNED)].as_bool());
                        }
 
                        RTLIL::SigSpec new_a, new_b;
@@ -841,8 +889,8 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        for (int i = 0; i < GetSize(a); i++) {
                                if (a[i].wire == NULL && b[i].wire == NULL && a[i] != b[i] && a[i].data <= RTLIL::State::S1 && b[i].data <= RTLIL::State::S1) {
                                        cover_list("opt.opt_expr.eqneq.isneq", "$eq", "$ne", "$eqx", "$nex", cell->type.str());
-                                       RTLIL::SigSpec new_y = RTLIL::SigSpec(cell->type.in("$eq", "$eqx") ?  RTLIL::State::S0 : RTLIL::State::S1);
-                                       new_y.extend_u0(cell->parameters["\\Y_WIDTH"].as_int(), false);
+                                       RTLIL::SigSpec new_y = RTLIL::SigSpec(cell->type.in(ID($eq), ID($eqx)) ?  RTLIL::State::S0 : RTLIL::State::S1);
+                                       new_y.extend_u0(cell->parameters[ID(Y_WIDTH)].as_int(), false);
                                        replace_cell(assign_map, module, cell, "isneq", "\\Y", new_y);
                                        goto next_cell;
                                }
@@ -854,83 +902,83 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
 
                        if (new_a.size() == 0) {
                                cover_list("opt.opt_expr.eqneq.empty", "$eq", "$ne", "$eqx", "$nex", cell->type.str());
-                               RTLIL::SigSpec new_y = RTLIL::SigSpec(cell->type.in("$eq", "$eqx") ?  RTLIL::State::S1 : RTLIL::State::S0);
-                               new_y.extend_u0(cell->parameters["\\Y_WIDTH"].as_int(), false);
-                               replace_cell(assign_map, module, cell, "empty", "\\Y", new_y);
+                               RTLIL::SigSpec new_y = RTLIL::SigSpec(cell->type.in(ID($eq), ID($eqx)) ?  RTLIL::State::S1 : RTLIL::State::S0);
+                               new_y.extend_u0(cell->parameters[ID(Y_WIDTH)].as_int(), false);
+                               replace_cell(assign_map, module, cell, "empty", ID(Y), new_y);
                                goto next_cell;
                        }
 
                        if (new_a.size() < a.size() || new_b.size() < b.size()) {
                                cover_list("opt.opt_expr.eqneq.resize", "$eq", "$ne", "$eqx", "$nex", cell->type.str());
-                               cell->setPort("\\A", new_a);
-                               cell->setPort("\\B", new_b);
-                               cell->parameters["\\A_WIDTH"] = new_a.size();
-                               cell->parameters["\\B_WIDTH"] = new_b.size();
+                               cell->setPort(ID(A), new_a);
+                               cell->setPort(ID(B), new_b);
+                               cell->parameters[ID(A_WIDTH)] = new_a.size();
+                               cell->parameters[ID(B_WIDTH)] = new_b.size();
                        }
                }
 
-               if (cell->type.in("$eq", "$ne") && cell->parameters["\\Y_WIDTH"].as_int() == 1 &&
-                               cell->parameters["\\A_WIDTH"].as_int() == 1 && cell->parameters["\\B_WIDTH"].as_int() == 1)
+               if (cell->type.in(ID($eq), ID($ne)) && cell->parameters[ID(Y_WIDTH)].as_int() == 1 &&
+                               cell->parameters[ID(A_WIDTH)].as_int() == 1 && cell->parameters[ID(B_WIDTH)].as_int() == 1)
                {
-                       RTLIL::SigSpec a = assign_map(cell->getPort("\\A"));
-                       RTLIL::SigSpec b = assign_map(cell->getPort("\\B"));
+                       RTLIL::SigSpec a = assign_map(cell->getPort(ID(A)));
+                       RTLIL::SigSpec b = assign_map(cell->getPort(ID(B)));
 
                        if (a.is_fully_const() && !b.is_fully_const()) {
                                cover_list("opt.opt_expr.eqneq.swapconst", "$eq", "$ne", cell->type.str());
-                               cell->setPort("\\A", b);
-                               cell->setPort("\\B", a);
+                               cell->setPort(ID(A), b);
+                               cell->setPort(ID(B), a);
                                std::swap(a, b);
                        }
 
                        if (b.is_fully_const()) {
-                               if (b.as_bool() == (cell->type == "$eq")) {
+                               if (b.as_bool() == (cell->type == ID($eq))) {
                                        RTLIL::SigSpec input = b;
-                                       ACTION_DO("\\Y", cell->getPort("\\A"));
+                                       ACTION_DO(ID(Y), cell->getPort(ID(A)));
                                } else {
                                        cover_list("opt.opt_expr.eqneq.isnot", "$eq", "$ne", cell->type.str());
                                        log_debug("Replacing %s cell `%s' in module `%s' with inverter.\n", log_id(cell->type), log_id(cell), log_id(module));
-                                       cell->type = "$not";
-                                       cell->parameters.erase("\\B_WIDTH");
-                                       cell->parameters.erase("\\B_SIGNED");
-                                       cell->unsetPort("\\B");
+                                       cell->type = ID($not);
+                                       cell->parameters.erase(ID(B_WIDTH));
+                                       cell->parameters.erase(ID(B_SIGNED));
+                                       cell->unsetPort(ID(B));
                                        did_something = true;
                                }
                                goto next_cell;
                        }
                }
 
-               if (cell->type.in("$eq", "$ne") &&
-                               (assign_map(cell->getPort("\\A")).is_fully_zero() || assign_map(cell->getPort("\\B")).is_fully_zero()))
+               if (cell->type.in(ID($eq), ID($ne)) &&
+                               (assign_map(cell->getPort(ID(A))).is_fully_zero() || assign_map(cell->getPort(ID(B))).is_fully_zero()))
                {
                        cover_list("opt.opt_expr.eqneq.cmpzero", "$eq", "$ne", cell->type.str());
                        log_debug("Replacing %s cell `%s' in module `%s' with %s.\n", log_id(cell->type), log_id(cell),
                                        log_id(module), "$eq" ? "$logic_not" : "$reduce_bool");
-                       cell->type = cell->type == "$eq" ? "$logic_not" : "$reduce_bool";
-                       if (assign_map(cell->getPort("\\A")).is_fully_zero()) {
-                               cell->setPort("\\A", cell->getPort("\\B"));
-                               cell->setParam("\\A_SIGNED", cell->getParam("\\B_SIGNED"));
-                               cell->setParam("\\A_WIDTH", cell->getParam("\\B_WIDTH"));
+                       cell->type = cell->type == ID($eq) ? ID($logic_not) : ID($reduce_bool);
+                       if (assign_map(cell->getPort(ID(A))).is_fully_zero()) {
+                               cell->setPort(ID(A), cell->getPort(ID(B)));
+                               cell->setParam(ID(A_SIGNED), cell->getParam(ID(B_SIGNED)));
+                               cell->setParam(ID(A_WIDTH), cell->getParam(ID(B_WIDTH)));
                        }
-                       cell->unsetPort("\\B");
-                       cell->unsetParam("\\B_SIGNED");
-                       cell->unsetParam("\\B_WIDTH");
+                       cell->unsetPort(ID(B));
+                       cell->unsetParam(ID(B_SIGNED));
+                       cell->unsetParam(ID(B_WIDTH));
                        did_something = true;
                        goto next_cell;
                }
 
-               if (cell->type.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx") && assign_map(cell->getPort("\\B")).is_fully_const())
+               if (cell->type.in(ID($shl), ID($shr), ID($sshl), ID($sshr), ID($shift), ID($shiftx)) && assign_map(cell->getPort(ID(B))).is_fully_const())
                {
-                       bool sign_ext = cell->type == "$sshr" && cell->getParam("\\A_SIGNED").as_bool();
-                       int shift_bits = assign_map(cell->getPort("\\B")).as_int(cell->type.in("$shift", "$shiftx") && cell->getParam("\\B_SIGNED").as_bool());
+                       bool sign_ext = cell->type == ID($sshr) && cell->getParam(ID(A_SIGNED)).as_bool();
+                       int shift_bits = assign_map(cell->getPort(ID(B))).as_int(cell->type.in(ID($shift), ID($shiftx)) && cell->getParam(ID(B_SIGNED)).as_bool());
 
-                       if (cell->type.in("$shl", "$sshl"))
+                       if (cell->type.in(ID($shl), ID($sshl)))
                                shift_bits *= -1;
 
-                       RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
-                       RTLIL::SigSpec sig_y(cell->type == "$shiftx" ? RTLIL::State::Sx : RTLIL::State::S0, cell->getParam("\\Y_WIDTH").as_int());
+                       RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
+                       RTLIL::SigSpec sig_y(cell->type == ID($shiftx) ? RTLIL::State::Sx : RTLIL::State::S0, cell->getParam(ID(Y_WIDTH)).as_int());
 
                        if (GetSize(sig_a) < GetSize(sig_y))
-                               sig_a.extend_u0(GetSize(sig_y), cell->getParam("\\A_SIGNED").as_bool());
+                               sig_a.extend_u0(GetSize(sig_y), cell->getParam(ID(A_SIGNED)).as_bool());
 
                        for (int i = 0; i < GetSize(sig_y); i++) {
                                int idx = i + shift_bits;
@@ -943,9 +991,9 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        cover_list("opt.opt_expr.constshift", "$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx", cell->type.str());
 
                        log_debug("Replacing %s cell `%s' (B=%s, SHR=%d) in module `%s' with fixed wiring: %s\n",
-                                       log_id(cell->type), log_id(cell), log_signal(assign_map(cell->getPort("\\B"))), shift_bits, log_id(module), log_signal(sig_y));
+                                       log_id(cell->type), log_id(cell), log_signal(assign_map(cell->getPort(ID(B)))), shift_bits, log_id(module), log_signal(sig_y));
 
-                       module->connect(cell->getPort("\\Y"), sig_y);
+                       module->connect(cell->getPort(ID(Y)), sig_y);
                        module->remove(cell);
 
                        did_something = true;
@@ -958,41 +1006,41 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        bool identity_wrt_b = false;
                        bool arith_inverse = false;
 
-                       if (cell->type.in("$add", "$sub", "$or", "$xor"))
+                       if (cell->type.in(ID($add), ID($sub), ID($or), ID($xor)))
                        {
-                               RTLIL::SigSpec a = assign_map(cell->getPort("\\A"));
-                               RTLIL::SigSpec b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigSpec a = assign_map(cell->getPort(ID(A)));
+                               RTLIL::SigSpec b = assign_map(cell->getPort(ID(B)));
 
-                               if (cell->type != "$sub" && a.is_fully_const() && a.as_bool() == false)
+                               if (cell->type != ID($sub) && a.is_fully_const() && a.as_bool() == false)
                                        identity_wrt_b = true;
 
                                if (b.is_fully_const() && b.as_bool() == false)
                                        identity_wrt_a = true;
                        }
 
-                       if (cell->type.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx"))
+                       if (cell->type.in(ID($shl), ID($shr), ID($sshl), ID($sshr), ID($shift), ID($shiftx)))
                        {
-                               RTLIL::SigSpec b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigSpec b = assign_map(cell->getPort(ID(B)));
 
                                if (b.is_fully_const() && b.as_bool() == false)
                                        identity_wrt_a = true;
                        }
 
-                       if (cell->type == "$mul")
+                       if (cell->type == ID($mul))
                        {
-                               RTLIL::SigSpec a = assign_map(cell->getPort("\\A"));
-                               RTLIL::SigSpec b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigSpec a = assign_map(cell->getPort(ID(A)));
+                               RTLIL::SigSpec b = assign_map(cell->getPort(ID(B)));
 
-                               if (a.is_fully_const() && is_one_or_minus_one(a.as_const(), cell->getParam("\\A_SIGNED").as_bool(), arith_inverse))
+                               if (a.is_fully_const() && is_one_or_minus_one(a.as_const(), cell->getParam(ID(A_SIGNED)).as_bool(), arith_inverse))
                                        identity_wrt_b = true;
                                else
-                               if (b.is_fully_const() && is_one_or_minus_one(b.as_const(), cell->getParam("\\B_SIGNED").as_bool(), arith_inverse))
+                               if (b.is_fully_const() && is_one_or_minus_one(b.as_const(), cell->getParam(ID(B_SIGNED)).as_bool(), arith_inverse))
                                        identity_wrt_a = true;
                        }
 
-                       if (cell->type == "$div")
+                       if (cell->type == ID($div))
                        {
-                               RTLIL::SigSpec b = assign_map(cell->getPort("\\B"));
+                               RTLIL::SigSpec b = assign_map(cell->getPort(ID(B)));
 
                                if (b.is_fully_const() && b.size() <= 32 && b.as_int() == 1)
                                        identity_wrt_a = true;
@@ -1009,15 +1057,15 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        cell->type.c_str(), cell->name.c_str(), module->name.c_str(), identity_wrt_a ? 'A' : 'B');
 
                                if (!identity_wrt_a) {
-                                       cell->setPort("\\A", cell->getPort("\\B"));
-                                       cell->parameters.at("\\A_WIDTH") = cell->parameters.at("\\B_WIDTH");
-                                       cell->parameters.at("\\A_SIGNED") = cell->parameters.at("\\B_SIGNED");
+                                       cell->setPort(ID(A), cell->getPort(ID(B)));
+                                       cell->parameters.at(ID(A_WIDTH)) = cell->parameters.at(ID(B_WIDTH));
+                                       cell->parameters.at(ID(A_SIGNED)) = cell->parameters.at(ID(B_SIGNED));
                                }
 
-                               cell->type = arith_inverse ? "$neg" : "$pos";
-                               cell->unsetPort("\\B");
-                               cell->parameters.erase("\\B_WIDTH");
-                               cell->parameters.erase("\\B_SIGNED");
+                               cell->type = arith_inverse ? ID($neg) : ID($pos);
+                               cell->unsetPort(ID(B));
+                               cell->parameters.erase(ID(B_WIDTH));
+                               cell->parameters.erase(ID(B_SIGNED));
                                cell->check();
 
                                did_something = true;
@@ -1025,91 +1073,91 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        }
                }
 
-               if (mux_bool && cell->type.in("$mux", "$_MUX_") &&
-                               cell->getPort("\\A") == State::S0 && cell->getPort("\\B") == State::S1) {
+               if (mux_bool && cell->type.in(ID($mux), ID($_MUX_)) &&
+                               cell->getPort(ID(A)) == State::S0 && cell->getPort(ID(B)) == State::S1) {
                        cover_list("opt.opt_expr.mux_bool", "$mux", "$_MUX_", cell->type.str());
-                       replace_cell(assign_map, module, cell, "mux_bool", "\\Y", cell->getPort("\\S"));
+                       replace_cell(assign_map, module, cell, "mux_bool", ID(Y), cell->getPort(ID(S)));
                        goto next_cell;
                }
 
-               if (mux_bool && cell->type.in("$mux", "$_MUX_") &&
-                               cell->getPort("\\A") == State::S1 && cell->getPort("\\B") == State::S0) {
+               if (mux_bool && cell->type.in(ID($mux), ID($_MUX_)) &&
+                               cell->getPort(ID(A)) == State::S1 && cell->getPort(ID(B)) == State::S0) {
                        cover_list("opt.opt_expr.mux_invert", "$mux", "$_MUX_", cell->type.str());
                        log_debug("Replacing %s cell `%s' in module `%s' with inverter.\n", log_id(cell->type), log_id(cell), log_id(module));
-                       cell->setPort("\\A", cell->getPort("\\S"));
-                       cell->unsetPort("\\B");
-                       cell->unsetPort("\\S");
-                       if (cell->type == "$mux") {
-                               Const width = cell->parameters["\\WIDTH"];
-                               cell->parameters["\\A_WIDTH"] = width;
-                               cell->parameters["\\Y_WIDTH"] = width;
-                               cell->parameters["\\A_SIGNED"] = 0;
-                               cell->parameters.erase("\\WIDTH");
-                               cell->type = "$not";
+                       cell->setPort(ID(A), cell->getPort(ID(S)));
+                       cell->unsetPort(ID(B));
+                       cell->unsetPort(ID(S));
+                       if (cell->type == ID($mux)) {
+                               Const width = cell->parameters[ID(WIDTH)];
+                               cell->parameters[ID(A_WIDTH)] = width;
+                               cell->parameters[ID(Y_WIDTH)] = width;
+                               cell->parameters[ID(A_SIGNED)] = 0;
+                               cell->parameters.erase(ID(WIDTH));
+                               cell->type = ID($not);
                        } else
-                               cell->type = "$_NOT_";
+                               cell->type = ID($_NOT_);
                        did_something = true;
                        goto next_cell;
                }
 
-               if (consume_x && mux_bool && cell->type.in("$mux", "$_MUX_") && cell->getPort("\\A") == State::S0) {
+               if (consume_x && mux_bool && cell->type.in(ID($mux), ID($_MUX_)) && cell->getPort(ID(A)) == State::S0) {
                        cover_list("opt.opt_expr.mux_and", "$mux", "$_MUX_", cell->type.str());
                        log_debug("Replacing %s cell `%s' in module `%s' with and-gate.\n", log_id(cell->type), log_id(cell), log_id(module));
-                       cell->setPort("\\A", cell->getPort("\\S"));
-                       cell->unsetPort("\\S");
-                       if (cell->type == "$mux") {
-                               Const width = cell->parameters["\\WIDTH"];
-                               cell->parameters["\\A_WIDTH"] = width;
-                               cell->parameters["\\B_WIDTH"] = width;
-                               cell->parameters["\\Y_WIDTH"] = width;
-                               cell->parameters["\\A_SIGNED"] = 0;
-                               cell->parameters["\\B_SIGNED"] = 0;
-                               cell->parameters.erase("\\WIDTH");
-                               cell->type = "$and";
+                       cell->setPort(ID(A), cell->getPort(ID(S)));
+                       cell->unsetPort(ID(S));
+                       if (cell->type == ID($mux)) {
+                               Const width = cell->parameters[ID(WIDTH)];
+                               cell->parameters[ID(A_WIDTH)] = width;
+                               cell->parameters[ID(B_WIDTH)] = width;
+                               cell->parameters[ID(Y_WIDTH)] = width;
+                               cell->parameters[ID(A_SIGNED)] = 0;
+                               cell->parameters[ID(B_SIGNED)] = 0;
+                               cell->parameters.erase(ID(WIDTH));
+                               cell->type = ID($and);
                        } else
-                               cell->type = "$_AND_";
+                               cell->type = ID($_AND_);
                        did_something = true;
                        goto next_cell;
                }
 
-               if (consume_x && mux_bool && cell->type.in("$mux", "$_MUX_") && cell->getPort("\\B") == State::S1) {
+               if (consume_x && mux_bool && cell->type.in(ID($mux), ID($_MUX_)) && cell->getPort(ID(B)) == State::S1) {
                        cover_list("opt.opt_expr.mux_or", "$mux", "$_MUX_", cell->type.str());
                        log_debug("Replacing %s cell `%s' in module `%s' with or-gate.\n", log_id(cell->type), log_id(cell), log_id(module));
-                       cell->setPort("\\B", cell->getPort("\\S"));
-                       cell->unsetPort("\\S");
-                       if (cell->type == "$mux") {
-                               Const width = cell->parameters["\\WIDTH"];
-                               cell->parameters["\\A_WIDTH"] = width;
-                               cell->parameters["\\B_WIDTH"] = width;
-                               cell->parameters["\\Y_WIDTH"] = width;
-                               cell->parameters["\\A_SIGNED"] = 0;
-                               cell->parameters["\\B_SIGNED"] = 0;
-                               cell->parameters.erase("\\WIDTH");
-                               cell->type = "$or";
+                       cell->setPort(ID(B), cell->getPort(ID(S)));
+                       cell->unsetPort(ID(S));
+                       if (cell->type == ID($mux)) {
+                               Const width = cell->parameters[ID(WIDTH)];
+                               cell->parameters[ID(A_WIDTH)] = width;
+                               cell->parameters[ID(B_WIDTH)] = width;
+                               cell->parameters[ID(Y_WIDTH)] = width;
+                               cell->parameters[ID(A_SIGNED)] = 0;
+                               cell->parameters[ID(B_SIGNED)] = 0;
+                               cell->parameters.erase(ID(WIDTH));
+                               cell->type = ID($or);
                        } else
-                               cell->type = "$_OR_";
+                               cell->type = ID($_OR_);
                        did_something = true;
                        goto next_cell;
                }
 
-               if (mux_undef && cell->type.in("$mux", "$pmux")) {
+               if (mux_undef && cell->type.in(ID($mux), ID($pmux))) {
                        RTLIL::SigSpec new_a, new_b, new_s;
-                       int width = cell->getPort("\\A").size();
-                       if ((cell->getPort("\\A").is_fully_undef() && cell->getPort("\\B").is_fully_undef()) ||
-                                       cell->getPort("\\S").is_fully_undef()) {
+                       int width = cell->getPort(ID(A)).size();
+                       if ((cell->getPort(ID(A)).is_fully_undef() && cell->getPort(ID(B)).is_fully_undef()) ||
+                                       cell->getPort(ID(S)).is_fully_undef()) {
                                cover_list("opt.opt_expr.mux_undef", "$mux", "$pmux", cell->type.str());
-                               replace_cell(assign_map, module, cell, "mux_undef", "\\Y", cell->getPort("\\A"));
+                               replace_cell(assign_map, module, cell, "mux_undef", ID(Y), cell->getPort(ID(A)));
                                goto next_cell;
                        }
-                       for (int i = 0; i < cell->getPort("\\S").size(); i++) {
-                               RTLIL::SigSpec old_b = cell->getPort("\\B").extract(i*width, width);
-                               RTLIL::SigSpec old_s = cell->getPort("\\S").extract(i, 1);
+                       for (int i = 0; i < cell->getPort(ID(S)).size(); i++) {
+                               RTLIL::SigSpec old_b = cell->getPort(ID(B)).extract(i*width, width);
+                               RTLIL::SigSpec old_s = cell->getPort(ID(S)).extract(i, 1);
                                if (old_b.is_fully_undef() || old_s.is_fully_undef())
                                        continue;
                                new_b.append(old_b);
                                new_s.append(old_s);
                        }
-                       new_a = cell->getPort("\\A");
+                       new_a = cell->getPort(ID(A));
                        if (new_a.is_fully_undef() && new_s.size() > 0) {
                                new_a = new_b.extract((new_s.size()-1)*width, width);
                                new_b = new_b.extract(0, (new_s.size()-1)*width);
@@ -1117,27 +1165,27 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        }
                        if (new_s.size() == 0) {
                                cover_list("opt.opt_expr.mux_empty", "$mux", "$pmux", cell->type.str());
-                               replace_cell(assign_map, module, cell, "mux_empty", "\\Y", new_a);
+                               replace_cell(assign_map, module, cell, "mux_empty", ID(Y), new_a);
                                goto next_cell;
                        }
                        if (new_a == RTLIL::SigSpec(RTLIL::State::S0) && new_b == RTLIL::SigSpec(RTLIL::State::S1)) {
                                cover_list("opt.opt_expr.mux_sel01", "$mux", "$pmux", cell->type.str());
-                               replace_cell(assign_map, module, cell, "mux_sel01", "\\Y", new_s);
+                               replace_cell(assign_map, module, cell, "mux_sel01", ID(Y), new_s);
                                goto next_cell;
                        }
-                       if (cell->getPort("\\S").size() != new_s.size()) {
+                       if (cell->getPort(ID(S)).size() != new_s.size()) {
                                cover_list("opt.opt_expr.mux_reduce", "$mux", "$pmux", cell->type.str());
                                log_debug("Optimized away %d select inputs of %s cell `%s' in module `%s'.\n",
-                                               GetSize(cell->getPort("\\S")) - GetSize(new_s), log_id(cell->type), log_id(cell), log_id(module));
-                               cell->setPort("\\A", new_a);
-                               cell->setPort("\\B", new_b);
-                               cell->setPort("\\S", new_s);
+                                               GetSize(cell->getPort(ID(S))) - GetSize(new_s), log_id(cell->type), log_id(cell), log_id(module));
+                               cell->setPort(ID(A), new_a);
+                               cell->setPort(ID(B), new_b);
+                               cell->setPort(ID(S), new_s);
                                if (new_s.size() > 1) {
-                                       cell->type = "$pmux";
-                                       cell->parameters["\\S_WIDTH"] = new_s.size();
+                                       cell->type = ID($pmux);
+                                       cell->parameters[ID(S_WIDTH)] = new_s.size();
                                } else {
-                                       cell->type = "$mux";
-                                       cell->parameters.erase("\\S_WIDTH");
+                                       cell->type = ID($mux);
+                                       cell->parameters.erase(ID(S_WIDTH));
                                }
                                did_something = true;
                        }
@@ -1145,30 +1193,30 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
 
 #define FOLD_1ARG_CELL(_t) \
                if (cell->type == "$" #_t) { \
-                       RTLIL::SigSpec a = cell->getPort("\\A"); \
+                       RTLIL::SigSpec a = cell->getPort(ID(A)); \
                        assign_map.apply(a); \
                        if (a.is_fully_const()) { \
                                RTLIL::Const dummy_arg(RTLIL::State::S0, 1); \
                                RTLIL::SigSpec y(RTLIL::const_ ## _t(a.as_const(), dummy_arg, \
-                                               cell->parameters["\\A_SIGNED"].as_bool(), false, \
-                                               cell->parameters["\\Y_WIDTH"].as_int())); \
+                                               cell->parameters[ID(A_SIGNED)].as_bool(), false, \
+                                               cell->parameters[ID(Y_WIDTH)].as_int())); \
                                cover("opt.opt_expr.const.$" #_t); \
-                               replace_cell(assign_map, module, cell, stringf("%s", log_signal(a)), "\\Y", y); \
+                               replace_cell(assign_map, module, cell, stringf("%s", log_signal(a)), ID(Y), y); \
                                goto next_cell; \
                        } \
                }
 #define FOLD_2ARG_CELL(_t) \
                if (cell->type == "$" #_t) { \
-                       RTLIL::SigSpec a = cell->getPort("\\A"); \
-                       RTLIL::SigSpec b = cell->getPort("\\B"); \
+                       RTLIL::SigSpec a = cell->getPort(ID(A)); \
+                       RTLIL::SigSpec b = cell->getPort(ID(B)); \
                        assign_map.apply(a), assign_map.apply(b); \
                        if (a.is_fully_const() && b.is_fully_const()) { \
                                RTLIL::SigSpec y(RTLIL::const_ ## _t(a.as_const(), b.as_const(), \
-                                               cell->parameters["\\A_SIGNED"].as_bool(), \
-                                               cell->parameters["\\B_SIGNED"].as_bool(), \
-                                               cell->parameters["\\Y_WIDTH"].as_int())); \
+                                               cell->parameters[ID(A_SIGNED)].as_bool(), \
+                                               cell->parameters[ID(B_SIGNED)].as_bool(), \
+                                               cell->parameters[ID(Y_WIDTH)].as_int())); \
                                cover("opt.opt_expr.const.$" #_t); \
-                               replace_cell(assign_map, module, cell, stringf("%s, %s", log_signal(a), log_signal(b)), "\\Y", y); \
+                               replace_cell(assign_map, module, cell, stringf("%s, %s", log_signal(a), log_signal(b)), ID(Y), y); \
                                goto next_cell; \
                        } \
                }
@@ -1214,25 +1262,25 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                FOLD_1ARG_CELL(neg)
 
                // be very conservative with optimizing $mux cells as we do not want to break mux trees
-               if (cell->type == "$mux") {
-                       RTLIL::SigSpec input = assign_map(cell->getPort("\\S"));
-                       RTLIL::SigSpec inA = assign_map(cell->getPort("\\A"));
-                       RTLIL::SigSpec inB = assign_map(cell->getPort("\\B"));
+               if (cell->type == ID($mux)) {
+                       RTLIL::SigSpec input = assign_map(cell->getPort(ID(S)));
+                       RTLIL::SigSpec inA = assign_map(cell->getPort(ID(A)));
+                       RTLIL::SigSpec inB = assign_map(cell->getPort(ID(B)));
                        if (input.is_fully_const())
-                               ACTION_DO("\\Y", input.as_bool() ? cell->getPort("\\B") : cell->getPort("\\A"));
+                               ACTION_DO(ID(Y), input.as_bool() ? cell->getPort(ID(B)) : cell->getPort(ID(A)));
                        else if (inA == inB)
-                               ACTION_DO("\\Y", cell->getPort("\\A"));
+                               ACTION_DO(ID(Y), cell->getPort(ID(A)));
                }
 
-               if (!keepdc && cell->type == "$mul")
+               if (!keepdc && cell->type == ID($mul))
                {
-                       bool a_signed = cell->parameters["\\A_SIGNED"].as_bool();
-                       bool b_signed = cell->parameters["\\B_SIGNED"].as_bool();
+                       bool a_signed = cell->parameters[ID(A_SIGNED)].as_bool();
+                       bool b_signed = cell->parameters[ID(B_SIGNED)].as_bool();
                        bool swapped_ab = false;
 
-                       RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
-                       RTLIL::SigSpec sig_b = assign_map(cell->getPort("\\B"));
-                       RTLIL::SigSpec sig_y = assign_map(cell->getPort("\\Y"));
+                       RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID(A)));
+                       RTLIL::SigSpec sig_b = assign_map(cell->getPort(ID(B)));
+                       RTLIL::SigSpec sig_y = assign_map(cell->getPort(ID(Y)));
 
                        if (sig_b.is_fully_const() && sig_b.size() <= 32)
                                std::swap(sig_a, sig_b), std::swap(a_signed, b_signed), swapped_ab = true;
@@ -1267,9 +1315,9 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                                                a_val, cell->name.c_str(), module->name.c_str(), i);
 
                                                if (!swapped_ab) {
-                                                       cell->setPort("\\A", cell->getPort("\\B"));
-                                                       cell->parameters.at("\\A_WIDTH") = cell->parameters.at("\\B_WIDTH");
-                                                       cell->parameters.at("\\A_SIGNED") = cell->parameters.at("\\B_SIGNED");
+                                                       cell->setPort(ID(A), cell->getPort(ID(B)));
+                                                       cell->parameters.at(ID(A_WIDTH)) = cell->parameters.at(ID(B_WIDTH));
+                                                       cell->parameters.at(ID(A_SIGNED)) = cell->parameters.at(ID(B_SIGNED));
                                                }
 
                                                std::vector<RTLIL::SigBit> new_b = RTLIL::SigSpec(i, 6);
@@ -1277,10 +1325,10 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                                while (GetSize(new_b) > 1 && new_b.back() == RTLIL::State::S0)
                                                        new_b.pop_back();
 
-                                               cell->type = "$shl";
-                                               cell->parameters["\\B_WIDTH"] = GetSize(new_b);
-                                               cell->parameters["\\B_SIGNED"] = false;
-                                               cell->setPort("\\B", new_b);
+                                               cell->type = ID($shl);
+                                               cell->parameters[ID(B_WIDTH)] = GetSize(new_b);
+                                               cell->parameters[ID(B_SIGNED)] = false;
+                                               cell->setPort(ID(B), new_b);
                                                cell->check();
 
                                                did_something = true;
@@ -1289,11 +1337,11 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        }
                }
 
-               if (!keepdc && cell->type.in("$div", "$mod"))
+               if (!keepdc && cell->type.in(ID($div), ID($mod)))
                {
-                       bool b_signed = cell->parameters["\\B_SIGNED"].as_bool();
-                       SigSpec sig_b = assign_map(cell->getPort("\\B"));
-                       SigSpec sig_y = assign_map(cell->getPort("\\Y"));
+                       bool b_signed = cell->parameters[ID(B_SIGNED)].as_bool();
+                       SigSpec sig_b = assign_map(cell->getPort(ID(B)));
+                       SigSpec sig_y = assign_map(cell->getPort(ID(Y)));
 
                        if (sig_b.is_fully_def() && sig_b.size() <= 32)
                        {
@@ -1316,7 +1364,7 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                for (int i = 1; i < (b_signed ? sig_b.size()-1 : sig_b.size()); i++)
                                        if (b_val == (1 << i))
                                        {
-                                               if (cell->type == "$div")
+                                               if (cell->type == ID($div))
                                                {
                                                        cover("opt.opt_expr.div_shift");
 
@@ -1328,10 +1376,10 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                                        while (GetSize(new_b) > 1 && new_b.back() == RTLIL::State::S0)
                                                                new_b.pop_back();
 
-                                                       cell->type = "$shr";
-                                                       cell->parameters["\\B_WIDTH"] = GetSize(new_b);
-                                                       cell->parameters["\\B_SIGNED"] = false;
-                                                       cell->setPort("\\B", new_b);
+                                                       cell->type = ID($shr);
+                                                       cell->parameters[ID(B_WIDTH)] = GetSize(new_b);
+                                                       cell->parameters[ID(B_SIGNED)] = false;
+                                                       cell->setPort(ID(B), new_b);
                                                        cell->check();
                                                }
                                                else
@@ -1346,9 +1394,9 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                                        if (b_signed)
                                                                new_b.push_back(State::S0);
 
-                                                       cell->type = "$and";
-                                                       cell->parameters["\\B_WIDTH"] = GetSize(new_b);
-                                                       cell->setPort("\\B", new_b);
+                                                       cell->type = ID($and);
+                                                       cell->parameters[ID(B_WIDTH)] = GetSize(new_b);
+                                                       cell->setPort(ID(B), new_b);
                                                        cell->check();
                                                }
 
@@ -1360,7 +1408,7 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
 
                // remove redundant pairs of bits in ==, ===, !=, and !==
                // replace cell with const driver if inputs can't be equal
-               if (do_fine && cell->type.in("$eq", "$ne", "$eqx", "$nex"))
+               if (do_fine && cell->type.in(ID($eq), ID($ne), ID($eqx), ID($nex)))
                {
                        pool<pair<SigBit, SigBit>> redundant_cache;
                        mfp<SigBit> contradiction_cache;
@@ -1368,14 +1416,14 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                        contradiction_cache.promote(State::S0);
                        contradiction_cache.promote(State::S1);
 
-                       int a_width = cell->getParam("\\A_WIDTH").as_int();
-                       int b_width = cell->getParam("\\B_WIDTH").as_int();
+                       int a_width = cell->getParam(ID(A_WIDTH)).as_int();
+                       int b_width = cell->getParam(ID(B_WIDTH)).as_int();
 
-                       bool is_signed = cell->getParam("\\A_SIGNED").as_bool();
+                       bool is_signed = cell->getParam(ID(A_SIGNED)).as_bool();
                        int width = is_signed ? std::min(a_width, b_width) : std::max(a_width, b_width);
 
-                       SigSpec sig_a = cell->getPort("\\A");
-                       SigSpec sig_b = cell->getPort("\\B");
+                       SigSpec sig_a = cell->getPort(ID(A));
+                       SigSpec sig_b = cell->getPort(ID(B));
 
                        int redundant_bits = 0;
 
@@ -1405,8 +1453,8 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
 
                        if (contradiction_cache.find(State::S0) == contradiction_cache.find(State::S1))
                        {
-                               SigSpec y_sig = cell->getPort("\\Y");
-                               Const y_value(cell->type.in("$eq", "$eqx") ? 0 : 1, GetSize(y_sig));
+                               SigSpec y_sig = cell->getPort(ID(Y));
+                               Const y_value(cell->type.in(ID($eq), ID($eqx)) ? 0 : 1, GetSize(y_sig));
 
                                log_debug("Replacing cell `%s' in module `%s' with constant driver %s.\n",
                                        log_id(cell), log_id(module), log_signal(y_value));
@@ -1423,10 +1471,10 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                log_debug("Removed %d redundant input bits from %s cell `%s' in module `%s'.\n",
                                                redundant_bits, log_id(cell->type), log_id(cell), log_id(module));
 
-                               cell->setPort("\\A", sig_a);
-                               cell->setPort("\\B", sig_b);
-                               cell->setParam("\\A_WIDTH", GetSize(sig_a));
-                               cell->setParam("\\B_WIDTH", GetSize(sig_b));
+                               cell->setPort(ID(A), sig_a);
+                               cell->setPort(ID(B), sig_b);
+                               cell->setParam(ID(A_WIDTH), GetSize(sig_a));
+                               cell->setParam(ID(B_WIDTH), GetSize(sig_b));
 
                                did_something = true;
                                goto next_cell;
@@ -1434,57 +1482,57 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                }
 
                // simplify comparisons
-               if (do_fine && cell->type.in("$lt", "$ge", "$gt", "$le"))
+               if (do_fine && cell->type.in(ID($lt), ID($ge), ID($gt), ID($le)))
                {
                        IdString cmp_type = cell->type;
-                       SigSpec var_sig = cell->getPort("\\A");
-                       SigSpec const_sig = cell->getPort("\\B");
-                       int var_width = cell->parameters["\\A_WIDTH"].as_int();
-                       int const_width = cell->parameters["\\B_WIDTH"].as_int();
-                       bool is_signed = cell->getParam("\\A_SIGNED").as_bool();
+                       SigSpec var_sig = cell->getPort(ID(A));
+                       SigSpec const_sig = cell->getPort(ID(B));
+                       int var_width = cell->parameters[ID(A_WIDTH)].as_int();
+                       int const_width = cell->parameters[ID(B_WIDTH)].as_int();
+                       bool is_signed = cell->getParam(ID(A_SIGNED)).as_bool();
 
                        if (!const_sig.is_fully_const())
                        {
                                std::swap(var_sig, const_sig);
                                std::swap(var_width, const_width);
-                               if (cmp_type == "$gt")
-                                       cmp_type = "$lt";
-                               else if (cmp_type == "$lt")
-                                       cmp_type = "$gt";
-                               else if (cmp_type == "$ge")
-                                       cmp_type = "$le";
-                               else if (cmp_type == "$le")
-                                       cmp_type = "$ge";
+                               if (cmp_type == ID($gt))
+                                       cmp_type = ID($lt);
+                               else if (cmp_type == ID($lt))
+                                       cmp_type = ID($gt);
+                               else if (cmp_type == ID($ge))
+                                       cmp_type = ID($le);
+                               else if (cmp_type == ID($le))
+                                       cmp_type = ID($ge);
                        }
 
                        if (const_sig.is_fully_def() && const_sig.is_fully_const())
                        {
                                std::string condition, replacement;
-                               SigSpec replace_sig(State::S0, GetSize(cell->getPort("\\Y")));
+                               SigSpec replace_sig(State::S0, GetSize(cell->getPort(ID(Y))));
                                bool replace = false;
                                bool remove = false;
 
                                if (!is_signed)
                                { /* unsigned */
-                                       if (const_sig.is_fully_zero() && cmp_type == "$lt") {
+                                       if (const_sig.is_fully_zero() && cmp_type == ID($lt)) {
                                                condition   = "unsigned X<0";
                                                replacement = "constant 0";
                                                replace_sig[0] = State::S0;
                                                replace = true;
                                        }
-                                       if (const_sig.is_fully_zero() && cmp_type == "$ge") {
+                                       if (const_sig.is_fully_zero() && cmp_type == ID($ge)) {
                                                condition   = "unsigned X>=0";
                                                replacement = "constant 1";
                                                replace_sig[0] = State::S1;
                                                replace = true;
                                        }
-                                       if (const_width == var_width && const_sig.is_fully_ones() && cmp_type == "$gt") {
+                                       if (const_width == var_width && const_sig.is_fully_ones() && cmp_type == ID($gt)) {
                                                condition   = "unsigned X>~0";
                                                replacement = "constant 0";
                                                replace_sig[0] = State::S0;
                                                replace = true;
                                        }
-                                       if (const_width == var_width && const_sig.is_fully_ones() && cmp_type == "$le") {
+                                       if (const_width == var_width && const_sig.is_fully_ones() && cmp_type == ID($le)) {
                                                condition   = "unsigned X<=~0";
                                                replacement = "constant 1";
                                                replace_sig[0] = State::S1;
@@ -1499,18 +1547,18 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                                        var_high_sig[i - const_bit_hot] = var_sig[i];
                                                }
 
-                                               if (cmp_type == "$lt")
+                                               if (cmp_type == ID($lt))
                                                {
                                                        condition   = stringf("unsigned X<%s", log_signal(const_sig));
                                                        replacement = stringf("!X[%d:%d]", var_width - 1, const_bit_hot);
-                                                       module->addLogicNot(NEW_ID, var_high_sig, cell->getPort("\\Y"));
+                                                       module->addLogicNot(NEW_ID, var_high_sig, cell->getPort(ID(Y)));
                                                        remove = true;
                                                }
-                                               if (cmp_type == "$ge")
+                                               if (cmp_type == ID($ge))
                                                {
                                                        condition   = stringf("unsigned X>=%s", log_signal(const_sig));
                                                        replacement = stringf("|X[%d:%d]", var_width - 1, const_bit_hot);
-                                                       module->addReduceOr(NEW_ID, var_high_sig, cell->getPort("\\Y"));
+                                                       module->addReduceOr(NEW_ID, var_high_sig, cell->getPort(ID(Y)));
                                                        remove = true;
                                                }
                                        }
@@ -1519,19 +1567,19 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        if(const_bit_set >= var_width)
                                        {
                                                string cmp_name;
-                                               if (cmp_type == "$lt" || cmp_type == "$le")
+                                               if (cmp_type == ID($lt) || cmp_type == ID($le))
                                                {
-                                                       if (cmp_type == "$lt") cmp_name = "<";
-                                                       if (cmp_type == "$le") cmp_name = "<=";
+                                                       if (cmp_type == ID($lt)) cmp_name = "<";
+                                                       if (cmp_type == ID($le)) cmp_name = "<=";
                                                        condition   = stringf("unsigned X[%d:0]%s%s", var_width - 1, cmp_name.c_str(), log_signal(const_sig));
                                                        replacement = "constant 1";
                                                        replace_sig[0] = State::S1;
                                                        replace = true;
                                                }
-                                               if (cmp_type == "$gt" || cmp_type == "$ge")
+                                               if (cmp_type == ID($gt) || cmp_type == ID($ge))
                                                {
-                                                       if (cmp_type == "$gt") cmp_name = ">";
-                                                       if (cmp_type == "$ge") cmp_name = ">=";
+                                                       if (cmp_type == ID($gt)) cmp_name = ">";
+                                                       if (cmp_type == ID($ge)) cmp_name = ">=";
                                                        condition   = stringf("unsigned X[%d:0]%s%s", var_width - 1, cmp_name.c_str(), log_signal(const_sig));
                                                        replacement = "constant 0";
                                                        replace_sig[0] = State::S0;
@@ -1541,18 +1589,18 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                }
                                else
                                { /* signed */
-                                       if (const_sig.is_fully_zero() && cmp_type == "$lt")
+                                       if (const_sig.is_fully_zero() && cmp_type == ID($lt))
                                        {
                                                condition   = "signed X<0";
                                                replacement = stringf("X[%d]", var_width - 1);
                                                replace_sig[0] = var_sig[var_width - 1];
                                                replace = true;
                                        }
-                                       if (const_sig.is_fully_zero() && cmp_type == "$ge")
+                                       if (const_sig.is_fully_zero() && cmp_type == ID($ge))
                                        {
                                                condition   = "signed X>=0";
                                                replacement = stringf("X[%d]", var_width - 1);
-                                               module->addNot(NEW_ID, var_sig[var_width - 1], cell->getPort("\\Y"));
+                                               module->addNot(NEW_ID, var_sig[var_width - 1], cell->getPort(ID(Y)));
                                                remove = true;
                                        }
                                }
@@ -1562,7 +1610,7 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
                                        log_debug("Replacing %s cell `%s' (implementing %s) with %s.\n",
                                                        log_id(cell->type), log_id(cell), condition.c_str(), replacement.c_str());
                                        if (replace)
-                                               module->connect(cell->getPort("\\Y"), replace_sig);
+                                               module->connect(cell->getPort(ID(Y)), replace_sig);
                                        module->remove(cell);
                                        did_something = true;
                                        goto next_cell;