Skip to content

Commit

Permalink
add TST op code
Browse files Browse the repository at this point in the history
  • Loading branch information
ken committed Jan 13, 2011
1 parent b9fb6dd commit 2a74009
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 140 deletions.
87 changes: 58 additions & 29 deletions src/cmd/5g/cgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ agen(Node *n, Node *res)
p1 = gins(AMOVW, N, &n3);
datastring(nl->val.u.sval->s, nl->val.u.sval->len, &p1->from);
p1->from.type = D_CONST;
} else if(isslice(nl->type) || nl->type->etype == TSTRING) {
} else
if(isslice(nl->type) || nl->type->etype == TSTRING) {
n1 = n3;
n1.op = OINDREG;
n1.type = types[tptr];
Expand Down Expand Up @@ -813,6 +814,28 @@ agenr(Node *n, Node *a, Node *res)
agen(n, a);
}

void
gencmp0(Node *n, Type *t, int o, Prog *to)
{
Node n1, n2, n3;
int a;

regalloc(&n1, t, N);
cgen(n, &n1);
a = optoas(OCMP, t);
if(a != ACMP) {
nodconst(&n2, t, 0);
regalloc(&n3, t, N);
gmove(&n2, &n3);
gcmp(a, &n1, &n3);
regfree(&n3);
} else
gins(ATST, &n1, N);
a = optoas(o, t);
patch(gbranch(optoas(o, t), t), to);
regfree(&n1);
}

/*
* generate:
* if(n == true) goto to;
Expand Down Expand Up @@ -856,18 +879,10 @@ bgen(Node *n, int true, Prog *to)
switch(n->op) {
default:
def:
regalloc(&n1, n->type, N);
cgen(n, &n1);
nodconst(&n2, n->type, 0);
regalloc(&n3, n->type, N);
gmove(&n2, &n3);
gcmp(optoas(OCMP, n->type), &n1, &n3);
a = ABNE;
a = ONE;
if(!true)
a = ABEQ;
patch(gbranch(a, n->type), to);
regfree(&n1);
regfree(&n3);
a = OEQ;
gencmp0(n, n->type, a, to);
goto ret;

case OLITERAL:
Expand All @@ -876,23 +891,6 @@ bgen(Node *n, int true, Prog *to)
patch(gbranch(AB, T), to);
goto ret;

case ONAME:
if(n->addable == 0)
goto def;
nodconst(&n1, n->type, 0);
regalloc(&n2, n->type, N);
regalloc(&n3, n->type, N);
gmove(&n1, &n2);
cgen(n, &n3);
gcmp(optoas(OCMP, n->type), &n2, &n3);
a = ABNE;
if(!true)
a = ABEQ;
patch(gbranch(a, n->type), to);
regfree(&n2);
regfree(&n3);
goto ret;

case OANDAND:
if(!true)
goto caseor;
Expand Down Expand Up @@ -975,6 +973,16 @@ bgen(Node *n, int true, Prog *to)
yyerror("illegal array comparison");
break;
}

regalloc(&n1, types[tptr], N);
agen(nl, &n1);
n2 = n1;
n2.op = OINDREG;
n2.xoffset = Array_array;
gencmp0(&n2, types[tptr], a, to);
regfree(&n1);
break;

a = optoas(a, types[tptr]);
regalloc(&n1, types[tptr], N);
regalloc(&n3, types[tptr], N);
Expand All @@ -1000,6 +1008,16 @@ bgen(Node *n, int true, Prog *to)
yyerror("illegal interface comparison");
break;
}

regalloc(&n1, types[tptr], N);
agen(nl, &n1);
n2 = n1;
n2.op = OINDREG;
n2.xoffset = 0;
gencmp0(&n2, types[tptr], a, to);
regfree(&n1);
break;

a = optoas(a, types[tptr]);
regalloc(&n1, types[tptr], N);
regalloc(&n3, types[tptr], N);
Expand Down Expand Up @@ -1039,6 +1057,17 @@ bgen(Node *n, int true, Prog *to)
break;
}

if(nr->op == OLITERAL) {
if(nr->val.ctype == CTINT && mpgetfix(nr->val.u.xval) == 0) {
gencmp0(nl, nl->type, a, to);
break;
}
if(nr->val.ctype == CTNIL) {
gencmp0(nl, nl->type, a, to);
break;
}
}

a = optoas(a, nr->type);

if(nr->ullman >= UINF) {
Expand Down
12 changes: 4 additions & 8 deletions src/cmd/5g/cgen64.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,15 @@ cgen64(Node *n, Node *res)
// shift is >= 1<<32
split64(r, &cl, &ch);
gmove(&ch, &s);
p1 = gins(AMOVW, &s, &s);
p1->scond |= C_SBIT;
p1 = gins(ATST, &s, N);
p6 = gbranch(ABNE, T);
gmove(&cl, &s);
splitclean();
} else {
gmove(r, &s);
p6 = P;
}
p1 = gins(AMOVW, &s, &s);
p1->scond |= C_SBIT;
p1 = gins(ATST, &s, N);

// shift == 0
p1 = gins(AMOVW, &bl, &al);
Expand Down Expand Up @@ -390,17 +388,15 @@ cgen64(Node *n, Node *res)
// shift is >= 1<<32
split64(r, &cl, &ch);
gmove(&ch, &s);
p1 = gins(AMOVW, &s, &s);
p1->scond |= C_SBIT;
p1 = gins(ATST, &s, N);
p6 = gbranch(ABNE, T);
gmove(&cl, &s);
splitclean();
} else {
gmove(r, &s);
p6 = P;
}
p1 = gins(AMOVW, &s, &s);
p1->scond |= C_SBIT;
p1 = gins(ATST, &s, N);

// shift == 0
p1 = gins(AMOVW, &bl, &al);
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/5g/ggen.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,7 @@ cgen_shift(int op, Node *nl, Node *nr, Node *res)
}

// test for shift being 0
p1 = gins(AMOVW, &n1, &n1);
p1->scond |= C_SBIT;
p1 = gins(ATST, &n1, N);
p3 = gbranch(ABEQ, T);

// test and fix up large shifts
Expand Down
Loading

0 comments on commit 2a74009

Please sign in to comment.