From 2ff4cd3dc5ceab4532ec779f810d4a8fbc03a867 Mon Sep 17 00:00:00 2001 From: Brian Taylor Date: Sun, 31 Mar 2024 17:03:58 -0700 Subject: [PATCH] Go back to before previous merge. --- src/frontend/logicexp.c | 105 ++++++++++++--------------------- src/frontend/udevices.c | 6 +- src/include/ngspice/logicexp.h | 2 +- 3 files changed, 41 insertions(+), 72 deletions(-) diff --git a/src/frontend/logicexp.c b/src/frontend/logicexp.c index 46b061ac8..3d3df6ea9 100644 --- a/src/frontend/logicexp.c +++ b/src/frontend/logicexp.c @@ -569,22 +569,11 @@ static void move_inputs(struct gate_data *curr, struct gate_data *prev) prev->finished = TRUE; } -static void scan_gates(DSTRING *lhs, int optimize) +static void scan_gates(DSTRING *lhs) { struct gate_data *current = NULL, *previous = NULL, *last_curr = NULL; struct gate_data *prev = NULL; - if (optimize < 1) { - current = last_gate; - if (ds_get_length(lhs) > 0) { - assert(current->finished == FALSE); - tfree(current->outp); - current->outp = TMALLOC(char, ds_get_length(lhs) + 1); - strcpy(current->outp, ds_get_buf(lhs)); - } - return; - } - current = first_gate; while (current) { int is_gate = (current->type == '&' @@ -592,8 +581,7 @@ static void scan_gates(DSTRING *lhs, int optimize) || current->type == '|'); previous = current->prev; if (is_gate && current->is_possible) { - if (previous && previous->type == current->type - && previous->is_not == current->is_not) { + if (previous && previous->type == current->type) { if (eq(current->ins->name, previous->outp)) { move_inputs(current, previous); } @@ -620,7 +608,6 @@ static void scan_gates(DSTRING *lhs, int optimize) prev = current->prev; while (prev) { if (prev->type == current->type - && prev->is_not == current->is_not && prev->finished == FALSE && strncmp(prev->outp, TMP_PREFIX, TMP_LEN) == 0 && eq(current->ins->name, prev->outp)) { @@ -761,44 +748,36 @@ static int infix_to_postfix(char* infix, DSTRING * postfix_p) nlist = add_name_entry("first", NULL); ds_clear(postfix_p); while ( ( ltok = lexer_scan(lx) ) != 0 ) { // start while ltok loop - if (last_tok == -1) { // check starting token - if (!(ltok == LEX_ID || ltok == '~' || ltok == '(')) { - fprintf(stderr, "ERROR (1) invalid starting token\n"); + if (ltok == LEX_ID && last_tok == LEX_ID) { + fprintf(stderr, "ERROR (1) no gate operator between two identifiers\n"); + status = 1; + goto err_return; + } + if (lex_gate_op(ltok)) { + if (lex_gate_op(last_tok)) { + fprintf(stderr, "ERROR (2) two consecutive gate operators\n"); + status = 1; + goto err_return; + } else if (last_tok != LEX_ID && last_tok != ')') { + fprintf(stderr, "ERROR (2a) gate operator not after ID or rparen\n"); status = 1; goto err_return; } - } else { - if (last_tok == LEX_ID) { // check follower of an ID - if (!(lex_gate_op(ltok) || ltok == ')')) { - fprintf(stderr, "ERROR (2) incorrect token after ID\n"); - status = 1; - goto err_return; - } - } else if (lex_gate_op(last_tok)) { // check follower of a gate op - if (!(ltok == LEX_ID || ltok == '~' || ltok == '(')) { - fprintf(stderr, "ERROR (3) incorrect token after gate op\n"); - status = 1; - goto err_return; - } - } else if (last_tok == '~') { // check follower of ~ - if (!(ltok == LEX_ID || ltok == '(')) { - fprintf(stderr, "ERROR (4) incorrect token after \'~\'\n"); - status = 1; - goto err_return; - } - } else if (last_tok == '(') { // check follower of lparen - if (!(ltok == LEX_ID || ltok == '~' || ltok == '(')) { - fprintf(stderr, "ERROR (5) incorrect token after lparen\n"); - status = 1; - goto err_return; - } - } else if (last_tok == ')') { // check follower of rparen - if (!(ltok == ')' || lex_gate_op(ltok))) { - fprintf(stderr, "ERROR (6) incorrect token after rparen\n"); - status = 1; - goto err_return; - } - } + } + if (last_tok == '~' && ltok != LEX_ID && ltok != '(') { + fprintf(stderr, "ERROR (3) \'~\' is not followed by an ID or lparen\n"); + status = 1; + goto err_return; + } + if (ltok == '~' && (last_tok == LEX_ID || last_tok == ')')) { + fprintf(stderr, "ERROR (4) \'~\' follows an ID or rparen\n"); + status = 1; + goto err_return; + } + if (ltok == ')' && (lex_gate_op(last_tok) || last_tok == '~')) { + fprintf(stderr, "ERROR (5) incomplete infix sub-expression\n"); + status = 1; + goto err_return; } last_tok = ltok; @@ -818,11 +797,6 @@ static int infix_to_postfix(char* infix, DSTRING * postfix_p) } } else if (ltok == ')') { rparen_count++; - if (rparen_count > lparen_count) { - fprintf(stderr, "ERROR (9a) mismatched rparen\n"); - status = 1; - goto err_return; - } while ( stack.top != -1 && !eq(stack.array[stack.top], "(") ) { ds_cat_printf(postfix_p, " %s", pop(&stack, &status)); if (status) { @@ -858,19 +832,19 @@ static int infix_to_postfix(char* infix, DSTRING * postfix_p) goto err_return; } } else { - fprintf(stderr, "ERROR (7) unexpected infix token %d \'%s\'\n", + fprintf(stderr, "ERROR (6) unexpected infix token %d \'%s\'\n", ltok, lx->lexer_buf); status = 1; goto err_return; } } // end while ltok loop if (lex_gate_op(last_tok) || last_tok == '~') { - fprintf(stderr, "ERROR (8) incomplete infix expression\n"); + fprintf(stderr, "ERROR (7) incomplete infix expression\n"); status = 1; goto err_return; } if (lparen_count != rparen_count) { - fprintf(stderr, "ERROR (9) mismatched parentheses\n"); + fprintf(stderr, "ERROR (8) mismatched parentheses\n"); status = 1; goto err_return; } @@ -992,7 +966,7 @@ err_return: /* Start of logicexp parser */ static void aerror(char *s); static BOOL amatch(int t); -static BOOL bparse(char *line, BOOL new_lexer, int optimize); +static BOOL bparse(char *line, BOOL new_lexer); static int lookahead = 0; static int number_of_instances = 0; @@ -1087,7 +1061,7 @@ static BOOL amatch(int t) return TRUE; } -static BOOL bstmt_postfix(int optimize) +static BOOL bstmt_postfix(void) { /* A stmt is: output_name_id = '{' expr '}' */ DS_CREATE(lhs, 32); @@ -1139,7 +1113,7 @@ static BOOL bstmt_postfix(int optimize) retval = FALSE; goto bail_out; } - scan_gates(&lhs, optimize); + scan_gates(&lhs); gen_scanned_gates(first_gate); lookahead = lex_scan(); while (lookahead != '}') { @@ -1182,7 +1156,7 @@ static char *get_logicexp_tmodel_delays( return ds_get_buf(mname); } -static BOOL bparse(char *line, BOOL new_lexer, int optimize) +static BOOL bparse(char *line, BOOL new_lexer) { BOOL ret_val = TRUE; DS_CREATE(stmt, LEX_BUF_SZ); @@ -1197,7 +1171,7 @@ static BOOL bparse(char *line, BOOL new_lexer, int optimize) while (lookahead != '\0') { ds_clear(&stmt); ds_cat_str(&stmt, parse_lexer->lexer_buf); - if (!bstmt_postfix(optimize)) { + if (!bstmt_postfix()) { cleanup_parser(); ret_val= FALSE; break; @@ -1259,9 +1233,8 @@ static BOOL expect_token( return TRUE; } -BOOL f_logicexp(char *line, int optimize) +BOOL f_logicexp(char *line) { - /* If optimize > 0 then perform optimizations in scan_gates */ int t, num_ins = 0, num_outs = 0, i; char *endp; BOOL ret_val = TRUE; @@ -1346,7 +1319,7 @@ BOOL f_logicexp(char *line, int optimize) } (void) add_sym_tab_entry(parse_lexer->lexer_buf, SYM_TMODEL, &parse_lexer->lexer_sym_tab); - ret_val = bparse(line, FALSE, optimize); + ret_val = bparse(line, FALSE); current_lexer = NULL; if (!ret_val) { diff --git a/src/frontend/udevices.c b/src/frontend/udevices.c index b7437f45e..dcbe5d054 100644 --- a/src/frontend/udevices.c +++ b/src/frontend/udevices.c @@ -280,7 +280,6 @@ static int ps_udevice_msgs = 0; // Controls the verbosity of U* warnings If ps_udevice_exit is non-zero then exit when u_process_instance fails */ static int ps_udevice_exit = 0; -static int ps_scan_gates_optimize = 1; // Allow optimization in scan_gates static int ps_tpz_delays = 0; // For tristate delays static int ps_with_inverters = 0; // For ff/latch control inputs static int ps_with_tri_inverters = 0; // For inv3/inv3a data inputs @@ -966,9 +965,6 @@ void initialize_udevice(char *subckt_line) if (!cp_getvar("ps_udevice_exit", CP_NUM, &ps_udevice_exit, 0)) { ps_udevice_exit = 0; } - if (!cp_getvar("ps_scan_gates_optimize", CP_NUM, &ps_scan_gates_optimize, 0)) { - ps_scan_gates_optimize = 1; - } /* If non-zero use inverters with ff/latch control inputs */ if (!cp_getvar("ps_with_inverters", CP_NUM, &ps_with_inverters, 0)) { ps_with_inverters = 0; @@ -4322,7 +4318,7 @@ BOOL u_process_instance(char *nline) if (ps_ports_and_pins & 4) { printf("TRANS_IN %s\n", nline); } - behav_ret = f_logicexp(nline, ps_scan_gates_optimize); + behav_ret = f_logicexp(nline); if (!behav_ret && current_subckt) { fprintf(stderr, "ERROR in %s\n", current_subckt); } diff --git a/src/include/ngspice/logicexp.h b/src/include/ngspice/logicexp.h index 8c77e9a09..556835a43 100644 --- a/src/include/ngspice/logicexp.h +++ b/src/include/ngspice/logicexp.h @@ -3,7 +3,7 @@ #ifndef INCLUDED_LOGICEXP_H #define INCLUDED_LOGICEXP_H -BOOL f_logicexp(char *line, int optimize); +BOOL f_logicexp(char *line); BOOL f_pindly(char *line); #endif