Skip to content

Commit

Permalink
fix Issue 15538 - [REG 2.064] wrong code with switch
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Oct 7, 2017
1 parent 2f98e66 commit ef56ed9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ddmd/backend/cod3.c
Expand Up @@ -1303,6 +1303,7 @@ void doswitch(CodeBuilder& cdb, block *b)
bool csseg = false;
#endif

//printf("doswitch(%d)\n", b->BC);
elem *e = b->Belem;
elem_debug(e);
docommas(cdb,&e);
Expand Down Expand Up @@ -1463,15 +1464,16 @@ void doswitch(CodeBuilder& cdb, block *b)
if (vmax - vmin != REGMASK) // if there is a maximum
{ // CMP reg,vmax-vmin
cdb.genc2(0x81,modregrm(3,7,reg),vmax-vmin);
if (I64)
if (I64 && sz == 8)
code_orrex(cdb.last(), REX_W);
genjmp(cdb,JA,FLblock,b->nthSucc(0)); // JA default
}
if (I64)
{
if (!vmin)
{ // Need to clear out high 32 bits of reg
genmovreg(cdb,reg,reg); // MOV reg,reg
// Use 8B instead of 89, as 89 will be optimized away as a NOP
genregs(cdb,0x8B,reg,reg); // MOV reg,reg
}
if (config.flags3 & CFG3pic || config.exe == EX_WIN64)
{
Expand Down
29 changes: 29 additions & 0 deletions test/runnable/testswitch.d
Expand Up @@ -703,6 +703,34 @@ void test15396()

/*****************************************/

// https://issues.dlang.org/show_bug.cgi?id=15538

struct S15538
{
int a = 0;
int b = 1;
}

int f15538(S15538 s)
{
switch (s.a)
{
case 0: return 10;
case 1: return 20;
case 2: return 30;
case 3: return 40;
default: return 99;
}
}

void test15538()
{
S15538 s;
assert(f15538(s) == 10); /* fails */
}

/*****************************************/

int main()
{
test1();
Expand Down Expand Up @@ -732,6 +760,7 @@ int main()
test14352();
test14587();
test15396();
test15538();

printf("Success\n");
return 0;
Expand Down

0 comments on commit ef56ed9

Please sign in to comment.