在 GNU 的 C++ 代码中使用 C 头文件.包括内联汇编的错误:“asm"中的不可能约束 [英] Using C headers in C++ code in GNU. Error including inline assembly: impossible constraint in 'asm'

查看:29
本文介绍了在 GNU 的 C++ 代码中使用 C 头文件.包括内联汇编的错误:“asm"中的不可能约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a weird one. I'm working on an embedded system, using the vendors header files. I'm compiling the files using GCC 4.6.3. I want to use C++ for my code, I have error I can't figure out. I'm running a vendor example program, and all I've done is changed the name of the main.c file to main.cpp. As a result, I assume, the header files are being interpreted by the C++ compiler. One of them contains the following lines:

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

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

The files compile properly if the name of the file is main.c, I assume this is because the file is being processed by the C compiler. The error I get if I use C++ is

error: impossible constraint in 'asm'

但同样,我对 C 编译器没有任何问题.我需要在 C++ 文件中调用使用此定义的函数.我考虑过编写保留在 c 端并链接到它们的包装函数,但这将是一个真正的痛苦,而且效率较低.有什么建议?

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?

推荐答案

svc 也称为 swiARM/Thumb 软件中断 指令.它只需要常量,但它们与其他寄存器常量不同.即,mov r0, #4096.如果您希望指定立即数,则需要使用预处理器和令牌粘贴.数字不能是变量寄存器.

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 "
" 
         "bx r14" : : : "r0" 
     ); 
  }

会起作用.注意: #'C' 预处理器字符串化.另请注意,查看 I-CACHE 中的 SVC 编号效率低下,并且检查需要 D-CACHE.通常它总是 constant 并且 function 编号在寄存器中传递以加快系统调用.

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.

gcc 手册说,

'I'- 在数据中作为立即操作数有效的整数处理指令.即0范围内的整数以 2 的倍数旋转到 255

'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

这是典型的数据处理操作数 - 立即,ARM ARM 的 A5.1.3 节.SVC 操作数在 thumb 模式下是固定的 8 位,在 ARM 模式下是固定的 24 位.可能有一些我不知道的其他约束,但只要将数字常量传递给宏,至少预处理器的字符串化将起作用.

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.

我想这是幸运的,这在 gcc 中起作用,不幸的是 g++ 没有.您可以通过使用 -S 并使用这两种工具查看(并发布)输出来获得更深入的了解.

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.

您的代码似乎确实适用于 gcc-4.7.2,但 numberconst int 在我的情况下是本地的,使用 number 可能是问题所在.也许它有从C"到C++"的微妙语义变化.

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++'.

这篇关于在 GNU 的 C++ 代码中使用 C 头文件.包括内联汇编的错误:“asm"中的不可能约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆