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

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

问题描述

我有一个奇怪的。我工作在嵌入式系统上,使用供应商的头文件。我编译使用GCC 4.6.3的文件。我想用C ++为我的code,我的错误,我想不通。我运行一个供应商的示例程序,而我所做的是改变main.c文件到main.cpp中的名称。因此,我认为,头文件正在由C ++编译器PTED间$ P $。其中一个包含以下行:

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\n" \
         "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 又名 SWI 的ARM / Thumb 软件中断的指令。只需要常量,但它们是从其他寄存器的常数不同。也就是说, MOV R0,#4096 。你需要,如果你想指定立即使用preprocessor和标记粘贴。 数量不能是变量注册

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" \
     ); \
  }

将工作。 注意:C的preprocessors字符串化。另外请注意,它是在高效看看 SVC 号,因为它是在 I-CACHE 和检查要求 D-CACHE 。一般来说它始终是功能的数字在寄存器传递更快的系统调用的。

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的手册说,

- 整数这是作为一个数据立即数有效
           处理指令。即,在范围为0的整数
           到255 2的倍数旋转

'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公司的部分A5.1.3。在 SVC 操作数是固定的拇指模式的8位或固定于ARM模式24位。它也许可能与我不知道的一些其他约束,但至少preprocessor的字串化将只要一个数字常数传递给宏工作

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.

编辑:您的code似乎与<​​code> GCC-4.7.2 ,但若干工作 const int的在我的情况下,使用若干也许问题本地。也许它已经从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 ++ code。使用C头文件。错误包括内联汇编:在“汇编”不可能约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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