#331 Constant expression

open
nobody
None
5
2011-08-11
2011-08-10
Arnost Vecerka
No

It is not quite a bug but I have found some interesting problem with constant expression which is demonstrated in the following program:

#include "8052.h"

#define U 5u

const unsigned char V = U%3;

void main()
{
P1 = U%V;
}

The listing of generated object code (SDCC --std-sdcc99 --opt-code-speed) is:

2 ; File Created by SDCC : free open source ANSI-C Compiler
3 ; Version 3.0.4 #6726 (Aug 9 2011) (MINGW32)
371 ; Test.c:9: P1 = U%V;
0000 90s00r00 372 mov dptr,#_V
0003 E4 373 clr a
0004 93 374 movc a,@a+dptr
0005 FF 375 mov r7,a
0006 8F*00 376 mov __moduint_PARM_2,r7
0008 75*01 00 377 mov (__moduint_PARM_2 + 1),#0x00
000B 90 00 05 378 mov dptr,#0x0005
000E 12s00r00 379 lcall __moduint
0011 AE 82 380 mov r6,dpl
0013 8E 90 381 mov _P1,r6
0015 22 382 ret
383 .area CSEG (CODE)
384 .area CONST (CODE)
0000 385 _V:
0000 02 386 .db #0x02 ; 2

The compiler generates code for computation of the expression U%V . I think that this expression is a constant expression and it can be evaluated during compilation.

Arnost Vecerka

Discussion

  • Arnost Vecerka
    Arnost Vecerka
    2011-08-10

     
    Attachments
  • Maarten Brock
    Maarten Brock
    2011-08-11

    • labels: 101552 -->
    • milestone: 100455 -->
     
  • Maarten Brock
    Maarten Brock
    2011-08-11

    This is a feature request for more optimal code, not a bug.