4

我有一个奇怪的。我正在使用供应商的头文件开发嵌入式系统。我正在使用 GCC 4.6.3 编译文件。我想在我的代码中使用 C++,我有错误我无法弄清楚。我正在运行一个供应商示例程序,我所做的只是将 main.c 文件的名称更改为 main.cpp。因此,我假设 C++ 编译器正在解释头文件。其中之一包含以下几行:

__attribute__((naked)) static return_type signature \
  { \
     __asm( \
         "svc %0\n" \
         "bx r14" : : "I" (number) : "r0" \
     ); \
}

如果文件的名称是 main.c,则文件可以正确编译,我认为这是因为 C 编译器正在处理该文件。如果我使用 C++,我得到的错误是

error: impossible constraint in 'asm'

But again, I have no problem with the C compiler. I need to call functions that use this define in C++ files. I've considered writing wrapper functions that stay on the c side and linking to them, but it would be a real pain, and less efficient. Any suggestions?

4

2 回答 2

1

svc also known as swi is the ARM/Thumb software interrupt instruction. It only takes constants, but they are different from other register constants. Ie, mov r0, #4096. You need to use the preprocessor and token pasting if you wish to specify an immediate. number can not be a variable or register.

#define syscall(number) __attribute__((naked)) static return_type signature \
  { \
     __asm( \
         "svc " #number "\n" \
         "bx r14" : : : "r0" \
     ); \
  }

will work. Note: The # is the 'C' preprocessors stringify. Also note that it is in-efficient to look at the SVC number as it is in the I-CACHE and inspecting requires D-CACHE. Generally it is always constant and the function number is passed in a register for faster syscall's.

The gcc manual says,

'I'- Integer that is valid as an immediate operand in a data processing instruction. That is, an integer in the range 0 to 255 rotated by a multiple of 2

This is typical of Data-processing operands - immediate, section A5.1.3 of the ARM ARM. The SVC operands are either fixed 8-bits in thumb mode or fixed 24-bits in ARM mode. It maybe possible with some other constraint that I am unaware of, but at least the preprocessor's stringification will work as long as a numeric constant is passed to the macro.

I guess it is lucky that this worked from gcc and unlucky that g++ did not. You can get further insight by using -S and looking at (and posting) the output using both tools.

Edit: Your code does seem to work with gcc-4.7.2, but number is a const int local in my case, the use of number maybe the issue. Perhaps it has a subtle semantic change from 'C' to 'C++'.

于 2013-03-31T18:04:42.193 回答
0

Check the GCC manual (inline assembler) for the exact meaning of the constraints for your machine. Older GCC versions have been notoriously sloppy in the checking of constraints, perhaps you are being bitten by this. It is strange that gcc and g++ (same version?) handle the code differently, it just might be a compiler bug, but I'd consider that only after all other explanations have been exhausted.

于 2013-03-30T03:22:51.640 回答