⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 x86linux.c

📁 浙江大学编译原理课程设计源代码,高等院校计算机专业
💻 C
📖 第 1 页 / 共 4 页
字号:
        break;    case TYPE_STRING:        fprintf(codfp, "\t\tmovl\t$%d, %%cx\n",                strlen(p->v.s));        fprintf(codfp, "\t\tpopl\tesi\n");        fprintf(codfp, "\t\tpopl\tedi\n");        fprintf(codfp, "\t\tmovl\t%%ds, %%eax\n");        fprintf(codfp, "\t\tmovl\t%%eax, %%es\n");        fprintf(codfp, "\t\tcld\n");        fprintf(codfp, "\t\trep\tmovsb\n");        break;    case TYPE_REAL:        break;    case TYPE_ARRAY:        if (p->type_link->last->type->type_id == TYPE_INTEGER                ||p->type_link->last->type->type_id == TYPE_BOOLEAN )        {			/*            fprintf(codfp, "\t\tpop\tax\n");            fprintf(codfp, "\t\tpop\tbx\n");            fprintf(codfp, "\t\tmov\tword ptr [bx],ax\n");			*/            fprintf(codfp, "\t\tpopl\t%%eax\n");            fprintf(codfp, "\t\tpopl\t%%ebx\n");            fprintf(codfp, "\t\tmovl\t%%eax, (%%ebx)\n");        }        else if (p->type_link->last->type->type_id ==                 TYPE_CHAR)        {			/*            fprintf(codfp, "\t\tpop\tax\n");            fprintf(codfp, "\t\tpop\tbx\n");            fprintf(codfp, "\t\tmov\tbyte ptr [bx],al\n");			*/            fprintf(codfp, "\t\tpopl\t%%eax\n");            fprintf(codfp, "\t\tpopl\t%%ebx\n");            fprintf(codfp, "\t\tmovb\t%%al, (%%ebx)\n");        }        break;    default:        break;    }}static void do_linux_cond_jump(int true_or_false, Symbol label){    fprintf(codfp, "\t\tcmpl\t$1, %%eax\n");    if (true_or_false)        fprintf(codfp, "\t\tjge\t%s\n", label->name);    else        fprintf(codfp, "\t\tjl\t%s\n", label->name);}static void do_linux_jump(Symbol label){    fprintf(codfp, "\t\tjmp %s\n", label->name);}static void do_linux_label(Symbol label){    fprintf(codfp, "%s:\n", label->name);}static void do_linux_incr(Symbol sym){    switch (sym->type->type_id)    {    case TYPE_BOOLEAN:    case TYPE_INTEGER:        fprintf(codfp, "\t\tincl %s\n", sym->rname);        break;    case TYPE_CHAR:        fprintf(codfp, "\t\tincb %s\n", sym->rname);        break;    default:        parse_error("incr instruction only support char, boolean and int.", "");        break;    }}static void do_linux_decr(Symbol sym){    switch (sym->type->type_id)    {    case TYPE_BOOLEAN:    case TYPE_INTEGER:        fprintf(codfp, "decl %s\n", sym->rname);        break;    case TYPE_CHAR:        fprintf(codfp, "decb %s\n", sym->rname);        break;    default:        parse_error("incr instruction only support char, boolean and int.", "");        break;    }}static int jump_index = 0;static void do_linux_expression(Type type, int op){    if (type == NULL)    {        return;    }    if (type->type_id == TYPE_INTEGER            || type->type_id == TYPE_BOOLEAN)    {        fprintf(codfp, "\t\tpopl\t%%edx\n");        fprintf(codfp, "\t\tcmpl\t%%eax, %%edx\n");    }    else if(type->type_id == TYPE_CHAR)    {        fprintf(codfp, "\t\tpopl\t%%edx\n");        fprintf(codfp, "\t\tcmpb\t%%al, %%dl\n");    }    else if (type->type_id == TYPE_STRING)    {        fprintf(codfp,"\t\tpopl\t%%edi\n");        fprintf(codfp, "\t\tpopl\t%%esi\n");        fprintf(codfp, "\t\tmovl\t%%ds, %%eax\n");        fprintf(codfp, "\t\tmovl\t%%eax, %%es\n");        fprintf(codfp, "\t\tcld\n");        fprintf(codfp, "\t\tmovl\t$%d, %%ecx\n",strlen(p->v.s));        fprintf(codfp, "\t\trepe\t\tcmpsb\n");    }    else    {        parse_error("standard type expected.", "");        return;    }    fprintf(codfp, "\t\tmovl\t$1, %%eax\n");    switch(generic(op))    {    case GE:        fprintf(codfp, "\t\tjge\tj_%03d\n",                new_index(jump));        break;    case LE:        fprintf(codfp, "\t\tjle\tj_%03d\n",                new_index(jump));        break;    case GT:        fprintf(codfp, "\t\tjg\tj_%03d\n",                new_index(jump));        break;    case LT:        fprintf(codfp, "\t\tjl\tj_%03d\n",                new_index(jump));        break;    case EQ:        fprintf(codfp, "\t\tje\tj_%03d\n",                new_index(jump));        break;    case NE:        fprintf(codfp, "\t\tjne\tj_%03d\n",                new_index(jump));        break;    }    fprintf(codfp, "\t\txorl\t%%eax, %%eax\n");    fprintf(codfp, "j_%03d:\n", jump_index);}static void do_linux_negate(symbol *p){    if (!p)        return;    if (p->defn != DEF_VAR            && p->defn != DEF_VALPARA            && p->defn != DEF_VARPARA            && p->defn != DEF_CONST)    {        parse_error("variable required.", "");        return;    }    switch(p->type->type_id)    {    case TYPE_INTEGER:        fprintf(codfp, "\t\tnegl\t%%eax\n");        break;    default:        parse_error("operand type do not match operator.", "");        break;    }}static void do_linux_expr(Type type, int op){    if (type == NULL)    {        return;    }    switch(generic(op))    {    case ADD:        if (type->type_id == TYPE_REAL)        {}        else if (type->type_id == TYPE_INTEGER)        {            fprintf(codfp,"\t\tpopl\t%%edx\n");            fprintf(codfp, "\t\taddl\t%%edx, %%eax\n");        }        else            parse_error("integer or real type expected.","");        break;    case SUB:        if (type->type_id == TYPE_REAL)        {}        else if (type->type_id==TYPE_INTEGER)        {            fprintf(codfp, "\t\tpopl\t%%edx\n");            fprintf(codfp, "\t\tsubl\t%%eax, %%edx\n");            fprintf(codfp, "\t\tmovl\t%%edx, %%eax\n");        }        else            parse_error("integer or real type expected.", "");        break;    case OR:        if (type->type_id == TYPE_BOOLEAN)        {            fprintf(codfp, "\t\tpopl\t%%edx\n");            fprintf(codfp, "\t\torl\t%%edx, %%eax\n");        }        else            parse_error("boolean type expected.", "");        break;    default:        break;    }}static void do_linux_term(Type type, int op){    if (type == NULL)    {        return;    }    switch(generic(op))    {    case MUL:        if (type->type_id == TYPE_INTEGER)        {            fprintf(codfp, "\t\tpopl\t%%edx\n");            fprintf(codfp, "\t\timul\t%%edx\n");        }        else if (type->type_id == TYPE_REAL)        {}        else            parse_error("integer or real type expected.", "");        break;    case DIV:        if (type->type_id == TYPE_INTEGER)        {            fprintf(codfp,"\t\tmovl\t%%eax, %%ecx\n");            fprintf(codfp, "\t\tpopl\t%%eax\n");            fprintf(codfp, "\t\tsubl\t%%edx,%%edx\n");            fprintf(codfp, "\t\tidiv\t%%ecx\n");        }        else            parse_error("integer type expected.", "");        break;    case MOD:        if (type->type_id == TYPE_INTEGER)        {            fprintf(codfp, "\t\tmovl\t%%eax, %%ecx\n");            fprintf(codfp, "\t\tpopl\t%%eax\n");            fprintf(codfp, "\t\tsubl\t%%edx,%%edx\n");            fprintf(codfp, "\t\tidiv\t%%ecx\n");            fprintf(codfp, "\t\tmovl\t%%edx, %%eax\n");        }        else            parse_error("integer type expected.","");        break;    case AND:        if (type->type_id != TYPE_BOOLEAN)            parse_error("boolean type expected.","");        else        {            fprintf(codfp, "\t\tpopl\t%%edx\n");            fprintf(codfp, "\t\tandl\t%%edx, %%eax\n");        }        break;    default:        break;    }}static void do_linux_factor(symbol *p){    symtab *ptab;    int i;    int n;    if (!p)        return;    if (p->type->type_id == TYPE_ARRAY)    {        parse_error("array element expected","");        return;    }    if (p->defn == DEF_CONST || p->defn == DEF_ELEMENT)    {        switch(p->type->type_id)        {        case TYPE_BOOLEAN:            fprintf(codfp, "\t\tmovl\t$%d, %%eax\n",p->v.b);            break;        case TYPE_INTEGER:            if (p->defn == DEF_ELEMENT)			{                fprintf(codfp, "\t\tmovl\t$%d, %%eax\n",                        p->v.i);			}            else			{				short tvi = (short)p->v.i;                fprintf(codfp, "\t\tmovl\t$%d, %%eax\n",                        tvi);			}            break;        case TYPE_CHAR:            fprintf(codfp, "\t\tmovb\t$%d,%%al\n",p->v.c);            break;        case TYPE_REAL:            break;        case TYPE_STRING:            fprintf(codfp, "\t\tmovl\t%s, %%eax\n",p->rname);            break;        default:            break;        }    }    else if (p->defn == DEF_VARPARA)    {        fprintf(codfp, "\t\tmovl\t%s, %%ebx\n",                p->rname);        fprintf(codfp, "\t\tmovl\t(%%ebx), %%eax\n");    }    else if (p->defn == DEF_VAR             ||p->defn == DEF_VALPARA)    {        if (p->tab == top_symtab_stack()                || p->tab->level == 0)        {            switch(p->type->type_id)            {            case TYPE_CHAR:                fprintf(codfp, "\t\tsubl\t%%eax,%%eax\n");                fprintf(codfp, "\t\tmovb\t%s, %%al\n",p->rname);                break;            case TYPE_BOOLEAN:            case TYPE_INTEGER:                fprintf(codfp, "\t\tmovl\t%s, %%eax\n",p->rname);                break;            case TYPE_REAL:                break;            }        }        if (p->defn == DEF_VAR)        {            ptab = top_symtab_stack();            n = ptab->level - p->tab->level;            if (n <= 0)                return;            fprintf(codfp, "\t\tmovl\t%%ebx,%%ebp\n");            for(i = 0; i<n; i++)                fprintf(codfp, "\t\tmovl\t%s, %%ebp\n",                        LABEL_SLINK);            switch(p->type->type_id)            {            case TYPE_INTEGER:            case TYPE_BOOLEAN:                fprintf(codfp, "\t\tmovl\t%s, %%eax\n",p->rname);                break;            case TYPE_CHAR:                fprintf(codfp, "\t\tmovb\t%s, %%al\n",p->rname);                break;            default:                break;            }            fprintf(codfp,"\t\tmovl\t%%ebx, %%ebp\n");        }    }}static void do_linux_not_factor(symbol *p){    if (!p)        return;    if (p->type->type_id!= TYPE_BOOLEAN)        parse_error("Boolean type expected. ","");    do_linux_factor(p);    fprintf(codfp, "\t\tandl\t$1, %%eax\n");    fprintf(codfp, "\t\txorl\t$1, %%eax\n");}static void do_linux_array_factor(symbol *p){    if (p->type_link->first->v.i >= 0)        fprintf(codfp, "\t\tsubl\t$%d, %%eax\n",                p->type_link->first->v.i);    else        fprintf(codfp, "\t\tsubl\t$-%d, %%eax\n",                -(p->type_link->first->v.i));    fprintf(codfp, "\t\tmovl\t$%d, %%ecx\n",            get_symbol_size(p->type_link->last));    fprintf(codfp, "\t\timul\t%%ecx\n");    fprintf(codfp, "\t\tpopl\t%%edx\n");    if (p->tab->level)        fprintf(codfp, "\t\tsubl\t%%eax, %%edx\n");    else        fprintf(codfp, "\t\taddl\t%%eax, %%edx\n");    fprintf(codfp, "\t\tpushl\t%%edx\n");}static void do_linux_record_factor(symbol *var, symbol *p){    if (!var || !p || p->defn != DEF_FIELD)        return;    fprintf(codfp, "\t\tpopl\t%%eax\n");    fprintf(codfp, "\t\tmovl\t$%d, %%edx\n",p->offset);    if (var->tab->level)        fprintf(codfp, "\t\tsubl\t%%edx, %%eax\n");    else        fprintf(codfp, "\t\taddl\t%%edx, %%eax\n");    fprintf(codfp, "\t\tpushl\t%%eax\n");}static int programbegin(Env *global){    emit_linux_program_prologue(global->u.program.tab);    return 0;}static int programend(Env *global){    emit_linux_program_epilogue(global->u.program.tab);    return 0;}static int mainbegin(Env *main){    emit_linux_main_prologue(main->u.main.tab);    return 0;}static int mainend(Env *main){    emit_linux_main_epilogue(main->u.main.tab);    return 0;}static int globalvariable(Symbol symbol){    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -