leaf *build(char *expr, hashtab *h) { lstack s; char opr; int i = -1, num; char str[MAX_STR]; leaf *l = NULL, *r = NULL, *tmp = NULL; lstack_init(&s, 1024); while (expr[++i]) { if (is_digit(expr[i])) { num = get_number(expr, &i); l = new_num_leaf(num, INT, NULL, NULL, -1); lpush(&s, l); } else if (is_opr(expr[i])) { lpop(&s, &r); lpop(&s, &l); //Optimization : reducing the amount of registers !! if ((expr[i] == '+' || expr[i] == '*') && l->typ == INT && r->typ != INT) tmp = l, l = r, r = tmp; l = new_num_leaf(expr[i], CHAR, l, r, get_reg_num(l, r)); lpush(&s, l); } else if (is_alpha(expr[i])) { get_string(expr, str, &i); if (search(h, str)) { lpop(&s, &r); r->reg = 1; //Premice to register allocation !!! l = new_str_leaf(str, FUNC, NULL, r, 1); lpush(&s, l); } else //If not a function then a variable identifier ! [extend the grammar] { l = new_str_leaf(str, VAR, NULL, NULL, -1); lpush(&s, l); } } } lstack_free(&s); return l; }
int main(int argc, char **argv) { //No checks, make sure the parameters are OK, otherwise SEGFAULT! if (argc < 6) return printf("%s [outfile.dot] [graph name] [reg file] [x86 file] [stack file] [postfix expression]\n", argv[0]), 0; hashtab h; hashtab_init(&h, hash_2, 20); insert(&h, "ln"); insert(&h, "log"); insert(&h, "exp"); insert(&h, "cos"); insert(&h, "sin"); insert(&h, "tan"); insert(&h, "cosh"); insert(&h, "sinh"); insert(&h, "acos"); insert(&h, "asin"); insert(&h, "cotan"); tree t = build(argv[6], &h); printf("Expr : "); print_expr(t); printf("\n\n"); printf("Tree :\n"); print_tree(t); //Fictional register machine code generation gen_reg_asm_code(argv[3], t); //x86 machine code generation gen_x86_asm_code(argv[4], t); //Stack machine code generation gen_stack_code(argv[5], t); printf("\nParallel sub expressions : \n"); leaf *l; lstack *s = get_par_subtrees(t); //Write the AST with some nice colors write_tree_dot(argv[1], argv[2], t, s); while (lpop(s, &l)) print_tree(l), printf("\n"); lstack_free(s); hashtab_free(&h); return 0; }
void log_exit() { pthread_mutex_unlock(&mxq); pthread_join(log_thread, NULL); lstack_free(&log_stack); fclose(log_stream); }