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

📄 analysis.c

📁 非常好的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
    case GT:
        Printf2("if (",0);
        if (OptimizeFlag && optype(p->op) == I && CheckBooleanAsgn(p)) {
            break;
        }
        FindSymbols(p->kids[0],level+1);
        FindSymbols(p->kids[1],level+1);
        Printf2("%s ) goto ",opname(p->op));
        hasConditionals = 1;
        if (p->syms[0]) {
            n = equated(p->syms[0]);
            Printf2("_$%d ",n->u.l.label);
            if (succIdx >= MAXSUCCESORS) {
                printf(StrTab[407]);// <Overflow in number of successors\n>
            }
            else {
                successorsTable[succIdx] = n->u.l.label;
                succIdx++;
            }
        }
        AddJumpNode(p);
        p->x.unsafe = unsafe;
        return result;
    case ASGN:
        if (p->op == ASGNB) {
            unsafe = 1;
            hasDangerousOp = 1;
            p->x.unsafe = 1;
        }
		FindSymbols(p->kids[0],level+1);
        FindSymbols(p->kids[1],level+1);
        break;
    case AND:
        return 0;
    case CALL:
        if (p->x.intrinsic == 0) {
            hasCalls = 1;
            unsafe = 1;
            p->x.unsafe = 1;
        }
        if (p->kids[0]->syms[0]) {
            Printf2("%s() ",p->kids[0]->syms[0]->name);
            FindSymbols(p->kids[0],level+1);
            FindSymbols(p->kids[1],level+1);
            return 0;
        }
        else {
            FindSymbols(p->kids[0],level+1);
            FindSymbols(p->kids[1],level+1);
        }
        break;
    case ARG:
//        if (p->x.intrinsicArg == 0) {
//            unsafe = 1;
//            p->x.unsafe = 1;
//        }
        FindSymbols(p->kids[0],level+1);
        FindSymbols(p->kids[1],level+1);
        if (p->op == ARGB) {
            hasDangerousOp = 1;
            unsafe = 1;
            p->x.unsafe = 1;
        }
        return 0;
    case LOAD:
    case INDIR:
        FindSymbols(p->kids[0],level+1);
        FindSymbols(p->kids[1],level+1);
        p->x.unsafe = unsafe;
        return 0;
    case ADDRG:
    case ADDRF:
    case ADDRL:
        p->x.unsafe = unsafe;
        FindSymbols(p->kids[0],level+1);
        FindSymbols(p->kids[1],level+1);
        return 0;
    default:
        if (op == DIV ||  op == RSH || op == LSH || op == MOD || p->op == CVDI) {
            if (p->op != DIVD) {
                unsafe = 1;
                if (p->op == DIVI || p->op == MODI)
                    hasDangerousOp = 1;
                p->x.unsafe = 1;
                p->kids[0]->x.unsafe = 1;
                if (p->kids[1])
                    p->kids[1]->x.unsafe = 1;
            }
        }
        FindSymbols(p->kids[0],level+1);
        FindSymbols(p->kids[1],level+1);

    }
    p->x.unsafe = unsafe;
    return result;
}
static int SymbolSortFn(const void *f1, const void *f2)
{
    Symbol s1, s2;
    s1 = *(Symbol *) f1;
    s2 = *(Symbol *) f2;
    if (s1->ref > s2->ref)
        return (-1);
    else if (s1->ref < s2->ref)
        return (1);
    else
        return (0);
}

static Symbol RegisterVars[5];
static int maxRegisterVars;
void SetupRegisterVariables(void)
{
    int nrOfSym,i,k,l,RegistersAllocated;
    Symbol *FunctionLocals,n;

    SetRegistersMasks();
    RegisterVariablesMask = maxRegisterVars = 0;
    if (vmask[0] == 0)
        return;
    nrOfSym = 0;
    if (cfunc->u.f.callee) {
        Printf2(StrTab[415],0);// <Function arguments:\n>
        i = 0;
        while (cfunc->u.f.callee[i]) {
            n = cfunc->u.f.callee[i];
            Printf3("[%2d] %s ",i,n->name);
            if (n->addressed) Printf2(StrTab[416],0);// <addressed >
//			printf("%s Usage %f\n",n->name,n->ref);
            i++;
            nrOfSym++;
        }
    }
    FunctionLocals = FunctionInfo.cp->u.block.locals;
    if (OptimizeFlag == 0 && FunctionLocals) {
        i = 0;
        while (FunctionLocals[i]) {
            n = FunctionLocals[i];
            if (n->sclass == REGISTER)
                n->sclass = AUTO;
            i++;
        }
        return;
    }
    if (FunctionLocals) {
        Printf2(StrTab[417],0);// <Local variables:\n>
        i = 0;
        while (FunctionLocals[i]) {
            n = FunctionLocals[i];
            Printf3("[%2d] %s ",i,n->name);
            if (n->addressed) Printf2(StrTab[418],0);// <addressed >
//			printf("%s Usage %f\n",n->name,n->ref);
            i++;
            nrOfSym++;
        }

    }
    if (nrOfSym) {
//		printf("Function %s:\n",cfunc->name);
        FunctionLocals = (Symbol *)allocate((1+nrOfSym)*sizeof(Symbol),FUNC);
        memset(FunctionLocals,0,(1+nrOfSym)*sizeof(Symbol));
        k = 0;
        if (cfunc->u.f.callee) {
            i = 0;
            while (cfunc->u.f.callee[i]) {
                n = cfunc->u.f.callee[i];
                if (n->addressed == 0 &&
                    n->type->size == 4 &&
                    isscalar(n->type) &&
                    unqual(n->type)->op > DOUBLE) {
                    FunctionLocals[k++] = n;
                }
                i++;
            }
        }
        if (FunctionInfo.cp->u.block.locals) {
            i = 0;
            while (FunctionInfo.cp->u.block.locals[i]) {
                n = FunctionInfo.cp->u.block.locals[i];
                if (n->addressed == 0 &&
                    isscalar(n->type) &&
                    n->type->size == 4 &&
                    unqual(n->type)->op > DOUBLE) {
                    FunctionLocals[k++] = n;
                }
                i++;
            }
        }
        if (k) {
            if (k > 1)
                qsort(FunctionLocals,k,sizeof(Symbol),SymbolSortFn);
            Printf2(StrTab[419],0);// <Best three symbols to register: >
#if 0
            for (i=0; i<k;i++) {
                int j,last,first;

                last = FunctionLocals[i]->lastuse;
                first = FunctionLocals[i]->firstuse;
                for (j=i; j<k; j++) {
                    if (FunctionLocals[j]->firstuse > last ||
                        FunctionLocals[j]->lastuse < first) {
                        printf(StrTab[420],// <Disjoint variables %s [%4d] (%d %d) %s [%4d] (%d %d)\n>

                            FunctionLocals[j]->name,
                            FunctionLocals[j]->src.y,
                            FunctionLocals[j]->firstuse,
                            FunctionLocals[j]->lastuse,
                            FunctionLocals[i]->name,
                            FunctionLocals[i]->src.y,
                            FunctionLocals[i]->firstuse,
                            FunctionLocals[i]->lastuse);
                    }
                }
            }
#endif
            RegistersAllocated = 0;
            for (i=0; RegistersAllocated<3 && i < k; i++) {
                n = FunctionLocals[i];
                if (n->x.aliased)
                    continue;
                if (n->ref < 2.0) break;
				else if (n->ref < 3.0 && i > 0 &&
					(FunctionInfo.NestedCalls || FunctionInfo.hasDiv ||FunctionInfo.hasBlockMove))
					break;
                l = n->sclass;
                n->sclass = REGISTER;
                Printf2("%s ",FunctionLocals[i]->name);
                if (!askregvar(n,rmap[ttob(n->type)])) {
                    n->sclass = l;
                    break;
                }
                else {
                    int z,last,first;
                    FunctionInfo.hasRegisterVars = 1;
                    RegisterVars[maxRegisterVars++] = n;
                    RegistersAllocated++;
                    if (IntermediateLanguageFile) {
                        fprintf(ilFile,"; %s --> %s\n",n->name,n->x.name);
                    }
                    last = n->lastuse;
                    first = n->firstuse;
                    if (last && first
                        && first != last
                        && !FunctionInfo.hasgotos)
                    for (z=i+1; z<k;z++) {
                        Symbol s = FunctionLocals[z];
                        if (s->firstuse > last ||
                            s->lastuse < first) {
                            if (s->firstuse != s->lastuse &&
                                s->x.isArgument == 0  &&
                                n->x.isArgument == 0  &&
                                s->sclass != REGISTER) {
#if 0
                                printf("[%4d] aliasing %s to %s [%d]",
                                    s->src.y,
                                    s->name,n->name,z);
                                printf(" reg %s\n",n->x.name);
#endif
                                s->x.regnode = n->x.regnode;
                                s->x.regnode->vbl = s;
                                s->x.name = n->x.name;
                                s->x.aliased = 1;
                                s->sclass = REGISTER;
                                RegisterVars[maxRegisterVars++] = s;
                                if (IntermediateLanguageFile) {
                                    fprintf(ilFile,"; %s aliased to %s --> %s\n",s->name,n->name,n->x.name);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        Printf2("\n",0);
    }
	RegisterVariablesMask = usedmask[0];
}

void SaturateUnsafe(Node p)
{
    if (p == NULL) return;
    p->x.unsafe = 1;
    p->x.dangerousChildren = 1;
    SaturateUnsafe(p->kids[0]);
    SaturateUnsafe(p->kids[1]);
}

void ReplaceReg(Node p,Symbol oldreg,Symbol newreg)
{
    Symbol s;
    int opt;
    if (p == NULL) return;
    s = p->syms[RX];
    if (s) {
        if (s->x.name == oldreg->x.name)
            p->syms[RX] = newreg;
        else if (s->x.regnode) {
            int oldnum = oldreg->x.regnode->number;
            int nodenum = s->x.regnode->number;

            if (oldnum == nodenum) {
                if (s->x.name[2] == 'i' || s->x.name[2] == 'x')
                    p->syms[RX] = shortreg[newreg->x.regnode->number];
                else if (s->x.name[2] == 'l')
                    p->syms[RX] = charreg[newreg->x.regnode->number];
                else {
                    opt = optype(p->op);
                    if (opt != F && opt != D && opt != L)
                        fatal("ReplaceReg",StrTab[421],0);// <Invalid register number>
                }
            }
        }
    }
    ReplaceReg(p->kids[0],oldreg,newreg);
    ReplaceReg(p->kids[1],oldreg,newreg);
}

typedef struct {
    Node conditional;
    Node Jump;
    Code cpJump;
}jmpInfo;

#define relink(a, b) ((b)->x.prev = (a), (a)->x.next = (b))

int BuildBasicBlocks(void)
{
    int i,l,showline;
    Code cp,previous;
    Node p,last,watchout,forest,root;
    Symbol n;
    jmpInfo jInfo;
    struct node sentinel;
    int result = 1;

    endswithjump = hasCalls = currentLabel = succIdx = 0;
    i = 1;
    showline = 0;
    memset(&StartBlock,0,sizeof(basicCodeBlock));
    memset(&jInfo,0,sizeof(jmpInfo));
    ClearAliasTable();
    BlockNodes = NULL;
    aliasIndex = 0;
    JumpNodeIndex = 0;
    memset(JumpNodeTable,0,sizeof(JumpNodeTable));
    previous = NULL;
    watchout = last = NULL;
    AssignmentsList = NULL;
    SymbolsHead = NULL;
    unsafe = 0;
    for (cp = codehead.next; cp; cp = cp->next) {
        switch (cp->kind) {
        case Defpoint:
            src = cp->u.point.src;
            showline = 1;
            break;
        case Gen:
        case Jump:
            watchout = NULL;
        case Label:
            if (cp->kind == Label)
                unsafe = 0;
            if (showline) {
                Printf2("\nLine %d\n",src.y);

⌨️ 快捷键说明

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