<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>swithc-case question</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/38321/swithc-case-question</link><description> 
When I write a simple switch case program: 
---------- source : 

 
void main()
{
 char e = 0;

 int a=0;
 int b=2;
 int c=3;
 switch(e)
 {
 case 1:
 a = a + b;
 break;
 case 2:
 a = a + c;
 break;
 default:
 break;
 }
}
 

 
 
But,the c51 complier</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/142274?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2009 02:54:20 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:52e5d2a1-cdd5-41ba-ba44-097269bf614a</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Looking at a manual for a totally different compiler for a totally
different architecture probably isn&amp;#39;t too helpful - especially when
the correct manual is readily to hand:&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;&lt;br /&gt;
&lt;a href="http://www.keil.com/support/man/docs/c51/c51_optimize.htm"&gt;http://www.keil.com/support/man/docs/c51/c51_optimize.htm&lt;/a&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;
Note also the Related Knowledgebase Articles linked at the bottom
of the page...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/114837?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2009 02:45:21 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6802324c-be94-49ce-a7de-4febf14e3178</guid><dc:creator>Jack Sprat</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;So,how can i choose the optimization level to satisfy my
require? Is there any principias?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
If your requirement is that the code works properly then don&amp;#39;t
change anything.&lt;/p&gt;

&lt;p&gt;
If your requirement is that the compiler generates the object code
you expected then reduce the optimisation level to zero and define
all the variables as volatile.&lt;/p&gt;

&lt;p&gt;
I assume the latter suggestion is your requirement, but if it is
something else then please explain.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/139124?ContentTypeID=1</link><pubDate>Wed, 15 Apr 2009 22:10:56 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1c34ece0-6ad0-4bc0-8653-f1f91b783929</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;a href="http://www.keil.com/support/man/docs/armcc/armcc_cjaieafa.htm"&gt;http://www.keil.com/support/man/docs/armcc/armcc_cjaieafa.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://www.keil.com/support/man/docs/armccref/armccref_cihgfgfb.htm"&gt;
&lt;a href="http://www.keil.com/support/man/docs/armccref/armccref_cihgfgfb.htm"&gt;www.keil.com/.../armccref_cihgfgfb.htm&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
The above URL is for ARM, not 8051. But it does provide some
hints.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&lt;br /&gt;
3&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;Maximum optimization. -O3 performs the same optimizations as
-O2 however the balance between space and time optimizations in the
generated code is more heavily weighted towards space or time
compared with -O2. That is:&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;* -O3 -Otime aims to produce faster code than -O2 -Otime, at
the risk of increasing your image size&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;* -O3 -Ospace aims to produce smaller code than -O2 -Ospace,
but performance might be degraded.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;In addition, -O3 performs extra optimizations that are more
aggressive, such as:&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;* High-level scalar optimizations, including loop unrolling,
for -O3 -Otime. This can give significant performance benefits at a
small code size cost, but at the risk of a longer build time.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;* More aggressive inlining and automatic inlining for -O3
-Otime.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;* Multifile compilation by default.&lt;br /&gt;&lt;/i&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/135748?ContentTypeID=1</link><pubDate>Wed, 15 Apr 2009 19:31:58 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a7a2ab7f-231e-4964-9a5d-c6adb6573ce5</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hi yu zhou,&lt;/p&gt;

&lt;p&gt;
I don&amp;#39;t know much about this. But as Andy Neil says, it depends on
your requirement.&lt;/p&gt;

&lt;p&gt;
For Study/Debug, it is usually better to choose the lowest
optimization level. (Unless you want to know the behavior of
compiler.)&lt;/p&gt;

&lt;p&gt;
If smaller binary size/better performance is important, it is
usually better to choose the highest optimization level.&lt;/p&gt;

&lt;p&gt;
And if you find something strange, then choose the lowest
optimization level.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/125372?ContentTypeID=1</link><pubDate>Wed, 15 Apr 2009 17:54:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:027b71fa-0ac8-4880-a939-4fb368ab7ce6</guid><dc:creator>yu zhou</dc:creator><description>&lt;p&gt;&lt;p&gt;
?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/114835?ContentTypeID=1</link><pubDate>Wed, 15 Apr 2009 08:08:20 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:287eea89-3811-4d81-95c6-cc2038ef886a</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
What is your requirement?&lt;/p&gt;

&lt;p&gt;
Currently, the source code does nothing useful - and the generated
machine code meets that requirement!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/101785?ContentTypeID=1</link><pubDate>Wed, 15 Apr 2009 04:47:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:af21c689-265e-4393-bdc3-40e23b0fa813</guid><dc:creator>yu zhou</dc:creator><description>&lt;p&gt;&lt;p&gt;
So,how can i choose the optimization level to satisfy my require?
Is there any principias?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/89677?ContentTypeID=1</link><pubDate>Mon, 13 Apr 2009 02:47:46 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f8efcfed-f577-4a07-90e5-a39b40e9dd27</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Try lowering optimization level.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
And while the OP is at it, he could also add an endless loop or
some other measure to prevent main from returning.&lt;/p&gt;

&lt;p&gt;
Else we&amp;#39;ll see the other question that&amp;#39;s been discussed to death,
&amp;quot;why does my program enter an endless loop?&amp;quot;, shortly.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: swithc-case question</title><link>https://community.arm.com/thread/55841?ContentTypeID=1</link><pubDate>Sun, 12 Apr 2009 22:31:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e816662f-14f1-4d66-a7ef-720be8d29dd6</guid><dc:creator>Mike Kleshov</dc:creator><description>&lt;p&gt;&lt;p&gt;
This has been discussed many times.&lt;br /&gt;
The compiler is allowed to emit &lt;b&gt;ANY&lt;/b&gt; code as long as the
emitted code generates the correct output. Your example generates
&lt;b&gt;NO&lt;/b&gt; output whatsoever, and the emitted code does so as well.
That&amp;#39;s optimization at work. In fact, I would expect the emitted code
to consist of a single instruction RET.&lt;br /&gt;
Try lowering optimization level.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>