我的编译器正在优化一些不应该的东西

发布于 2024-10-06 04:47:52 字数 1712 浏览 0 评论 0原文

我的编译器 MPLAB C30 (GCC v3.23) 没有编译此代码:

 if(font_info.flags & FONT_LOWERCASE_ONLY)
  ch = tolower(ch);
 if(font_info.flags & FONT_UPPERCASE_ONLY)
  ch = toupper(ch);

它不产生汇编输出(以某种方式优化它),我不明白为什么。

据我所知,我已经正确定义了所有内容:

#define FONT_LOWERCASE_ONLY  1
#define FONT_UPPERCASE_ONLY  2

struct FontEntry
{
 int id;
 unsigned char width, height;
 const char *name;
 const char *lookup;
 const char *data;
 int flags;
};

struct FontEntry fonts[NUM_FONTS + 1] = {
 { 0, 8, 14, "Outlined8x14", &font_lookup_outlined8x14, &font_data_outlined8x14, 0 },
 { 1, 8, 8, "Outlined8x8", &font_lookup_outlined8x8, &font_data_outlined8x8, FONT_UPPERCASE_ONLY }, 
 { 2, 8, 8, "Tiny5x5", 0, 0, 0 }, // not yet implemented
 { -1 } // ends font table
};

我正在使用的函数是:

void write_char(char ch, unsigned int x, unsigned int y, int flags, int font)
{
 int i, yy, addr_temp, row, row_temp, xshift;
 uint16_t and_mask, or_mask, level_bits;
 struct FontEntry font_info;
 char lookup;
 fetch_font_info(ch, font, &font_info, &lookup);
    // ...
}

fetch_font_info 的定义:

int fetch_font_info(char ch, int font, struct FontEntry *font_info, char *lookup)
{
 // First locate the font struct.
 if(font > SIZEOF_ARRAY(fonts))
  return 0; // font does not exist, exit.
 // Load the font info; IDs are always sequential.
 *font_info = fonts[font];
 // Locate character in font lookup table. (If required.)
 if(lookup != NULL)
 {
  *lookup = font_info->lookup[ch];
  if(lookup == 0xff)
   return 0; // character doesn't exist, don't bother writing it.
 }
 return 1;
}

我做错了什么?

My compiler, MPLAB C30 (GCC v3.23) is not compiling this code:

 if(font_info.flags & FONT_LOWERCASE_ONLY)
  ch = tolower(ch);
 if(font_info.flags & FONT_UPPERCASE_ONLY)
  ch = toupper(ch);

It produces no assembly output (somehow optimising it out) and I can't figure out why.

I have defined everything correctly as far as I can see:

#define FONT_LOWERCASE_ONLY  1
#define FONT_UPPERCASE_ONLY  2

struct FontEntry
{
 int id;
 unsigned char width, height;
 const char *name;
 const char *lookup;
 const char *data;
 int flags;
};

struct FontEntry fonts[NUM_FONTS + 1] = {
 { 0, 8, 14, "Outlined8x14", &font_lookup_outlined8x14, &font_data_outlined8x14, 0 },
 { 1, 8, 8, "Outlined8x8", &font_lookup_outlined8x8, &font_data_outlined8x8, FONT_UPPERCASE_ONLY }, 
 { 2, 8, 8, "Tiny5x5", 0, 0, 0 }, // not yet implemented
 { -1 } // ends font table
};

The function I am using is:

void write_char(char ch, unsigned int x, unsigned int y, int flags, int font)
{
 int i, yy, addr_temp, row, row_temp, xshift;
 uint16_t and_mask, or_mask, level_bits;
 struct FontEntry font_info;
 char lookup;
 fetch_font_info(ch, font, &font_info, &lookup);
    // ...
}

The definition of fetch_font_info:

int fetch_font_info(char ch, int font, struct FontEntry *font_info, char *lookup)
{
 // First locate the font struct.
 if(font > SIZEOF_ARRAY(fonts))
  return 0; // font does not exist, exit.
 // Load the font info; IDs are always sequential.
 *font_info = fonts[font];
 // Locate character in font lookup table. (If required.)
 if(lookup != NULL)
 {
  *lookup = font_info->lookup[ch];
  if(lookup == 0xff)
   return 0; // character doesn't exist, don't bother writing it.
 }
 return 1;
}

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2024-10-13 04:47:52

由于 FONT_LOWERCASE_ONLY 和 FONT_UPPERCASE_ONLY 不为 0,因此 font_info.flags 必须始终为 0(或者低两位中没有任何 1)。编译器可以很聪明地了解如何评估“常量”,即使您没有这样定义它们。

我看到你的字体数组在标志部分有几个 0,所以我打赌你在编译时对这些条目之一有一个硬编码的引用。

Since FONT_LOWERCASE_ONLY and FONT_UPPERCASE_ONLY are non 0 then font_info.flags must always be 0 (or not have any 1s in the lower two bits). Compilers can be clever about how they evaluate "constants" even if you don't define them as such.

I see that your fonts array has a few 0s in the flag section so I am betting you have a hard coded reference to one of those entries at compile time.

戴着白色围巾的女孩 2024-10-13 04:47:52

您是否可能正在对 write_char() 中的 fetch_font_info() 的返回代码进行操作(很难知道,因为 write_char()您发布的内容并不是您真正正在编译的内容;已经进行了一些编辑)?

如果您对 fetch_font_info() 的返回进行操作,那么问题可能是由测试 lookup 指向的字符时的错误引起的:

if(lookup == 0xff) 

应该

if(*lookup == 0xff)

与有错误的测试 有关,如果查找非空,fetch_font_info() 将始终返回 0。

Is it possible you're acting on the return code from fetch_font_info() in write_char() (it's hard to know since the write_char() you've posted isn't what you're really compiling; some editing has happened)?

If you're acting on the return from fetch_font_info() then the problem might be cuased by the bug in testing the character pointed to by lookup:

if(lookup == 0xff) 

should be

if(*lookup == 0xff)

with the bugged test, fetch_font_info() will always return 0 if lookup is non-null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文