Jump to content
  • entries
    338
  • comments
    905
  • views
    260,812

Z3 compiler frustration


EricBall

418 views

What do you think the following C should do?

 

unsigned byte zmem[128*1024];

int read_word( int a, x )
{
 a = zmem[a++]<<8 + zmem[a];
...
}

There are three (!) errors in the above code:

  1. precedence error : addition is done before shift
  2. order of operations : is a incremented before the second array reference?
  3. compiler bug : a contains a++ after the operation

All three can be worked around by using a temporary variable. Here's another gotcha I ran into:

int PC;

void run( void )
{
...
 op2code( op & 31, zmem[PC++], zmem[PC++] );
...
}

Looks reasonable, provide the next two bytes in the instruction stream as operands. Again, there are several potential bugs lurking:

  1. is PC incremented before or after the function call (tested - it's before, at least with my compiler)
  2. order of operations : which operand gets which byte?
  3. is actually PC incremented before the second array reference?

#3 turned out to be not true - the same value was passed in for both operands. Again, recoding with temporary variables worked around the problem.

 

In both of these cases, the code looks sane. The compiler doesn't flag them as errors. But the results aren't what you'd expect.

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...