[英]C embedded assembly error: ‘asm’ operand has impossible constraints
當我用C語言嵌入匯編時,我遇到了以下錯誤,在ubuntu linux 14.04中使用shell命令編譯這些代碼。
IFR_temp_measure.cpp: In function ‘void BlockTempClc(char*, char*,
int, int, char, int, int, int, int*, int, int*, int)’:
IFR_temp_measure.cpp:1843:6: error: ‘asm’ operand has impossible
constraints);
^
&make: *** [IFR_temp_measure.o] Error 1
或者錯誤代碼行1842,1843的位置響應代碼
:"cc", "memory","q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q10", "q11", "q12", "q13", "q14", "q15","r0", "r1", "r3", "r4", "r5","r6","r8", "r9", "r10", "r12"
);
我試圖解決這個問題,但很少有參考文獻在線提供,有一個鏈接器: Gcc內聯匯編“'asm'操作數有不可能的約束”是什么意思? 和http://www.ethernut.de/en/documents/arm-inline-asm.html但沒有幫助。 我的代碼如下:
void BlockTempClc(char* src1,char* src2,int StrideDist,int height,char temp_comp1,int numofiterations,int temp_comp2,int temp_comp3,int *dstData,int width,int *dstSum,int step)
{
volatile char array1[16] = {0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0};
volatile char array2[16] = {0,0,1,0,2,0,3,0,
4,0,5,0,6,0,7,0};
asm volatile(
"mov r0, %0; " //image[0]
"mov r1, %1; " //image[1]
"mov r12,%11; " //m
"mov r3, %4; " //n
"mov r4, %2; " //store data
"mov r8, %12; " //step down for loading next line of image
"mov r5, %6; " //numofiterations
"mov r6, %3; " //out
"mov.8 r9,%5;"//isp_temp_comp
"mov.8 r10,%7;"//led_temp_comp
"mov.8 r11,%8;"//fac_temp_comp
"vdup.8 d20,r9;"//copy arm register value isp_temp_comp to neon register
"VMOV.S16 q9, d20; " //isp_temp_comp transfer to signed short type
"VLD1.8 {d16,d17}, [%9];"//q8 array1 sum
"VLD1.8 {d6,d7}, [%10];"//q3 array2
"VMOV.S16 q0, #256; "
"VMOV.S16 q1, #2730; " //Assign immediate number 2730 to each 16 bits of d1
".loop:;"
"vdup.8 d21,r10;"//copy arm register value led_temp_comp to neon register
"vdup.8 d22,r11;"//copy arm register value fac_temp_comp to neon register
"VLD1.8 d14, [r1],r8; " // q7 *(image[1] + tmp + n) Load: Load Picture Pixels r6:move step ?
"VLD1.8 d15, [r0],r8 " // *(image[0] + tmp + n) Load: Load Picture Pixels
"PLD [r1]; " //Preload: one line in cache
"PLD [r0]; " //?
"VMOV.S16 q5, d14; " //q5 8*16 transfer to signed short type:*(image[1] + tmp + n)
"VMOV.S16 q6, d15; " //q6 8*16 transfer to signed short type : *(image[0] + tmp + n)
"VADD.S16 q12,q6, q9;"//*(image[0] + tmp + n) + isp_temp_comp
"VMOV.S16 q6, d21; " //led_temp_comp
"VADD.S16 q13,q12, q6;"//*(image[0] + tmp + n) + isp_temp_comp+ + led_temp_comp
"VMOV.S16 q6, d22; " //fac_temp_comp
"VADD.S16 q14,q13, q6;"//*(image[0] + tmp + n) + isp_temp_comp+ + led_temp_comp+ fac_temp_comp
"VSUB.S16 q15,q14, q1;"//*(image[0] + tmp + n) + isp_temp_comp+ + led_temp_comp+ fac_temp_comp-2730
"VMLA.S16 q15, q5, q0;"//img_temp[m][n]=*(image[0] + tmp + n) + isp_temp_comp+ + led_temp_comp+ fac_temp_comp-2730+*(image[1] + tmp + n) *256
"VADD.S16 q2,q15, q8;"//sum
"VMOV.S16 q8, q2; " //q8
"vdup.8 d20,r3;"//n
"vdup.8 d21,r12;"//m
"VMOV.S16 q11, d20; " //n
"VMOV.S16 q10, d21; " //m
"VADD.S16 q4,q3, q11;"//(n,n+1,n+2,n+3,n+4,n+5,n+6,n+7)
"VADD.S16 q7,q3, q10;"//(m,m+1,m+2,m+3,m+4,m+5,m+6,m+7) q7
"VST1.16 {d30[0]}, [r4]!; "//restore img_temp[m][n] to pointer data
"VST1.16 {d14[0]}, [r4]!; "//restore m
"VST1.16 {d8[0]}, [r4]!; " //restore n
"VST1.16 {d30[1]}, [r4]!; "
"VST1.16 {d14[1]}, [r4]!; "
"VST1.16 {d8[1]}, [r4]!; "
"VST1.16 {d30[2]}, [r4]!; "
"VST1.16 {d14[2]}, [r4]!; "
"VST1.16 {d8[2]}, [r4]!; "
"VST1.16 {d30[3]}, [r4]!; "
"VST1.16 {d14[3]}, [r4]!; "
"VST1.16 {d8[3]}, [r4]!; "//response to array
"subs r5, r5, #1; " // decrement: numofinteration -= 1;
"bne .loop; " // Branch If Not Zero; to .loop
"VST1.16 {d4[0]}, [r6]!; "//q2 refer to sum restore the final result to pointer out
"VST1.16 {d4[1]}, [r6]!; "
"VST1.16 {d4[2]}, [r6]!; "
"VST1.16 {d4[3]}, [r6]!; "
"VST1.16 {d5[0]}, [r6]!; "
"VST1.16 {d5[1]}, [r6]!; "
"VST1.16 {d5[2]}, [r6]!; "
"VST1.16 {d5[3]}, [r6]!; "
:"+r"(src1),"+r"(src2),"+r"(dstData),"+r"(dstSum),"+r"(height)
:"r"(temp_comp1),"r"(numofiterations),"r"(temp_comp2),"r"(temp_comp3),
"r"(array1),"r"(array2), "r"(width),"r"(step)
:"cc", "memory","q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q10", "q11", "q12", "q13", "q14", "q15","r0", "r1", "r3", "r4", "r5","r6","r8", "r9", "r10", "r12"
);
}
我想問題可能是輸出操作數列表或輸出操作數列表。 什么導致我的代碼錯誤?以及如何解決它?
你在大多數整數寄存器上聲明了clobbers,但是你要求13個不同的輸入變量。 32位ARM只有16個寄存器,其中2個是PC和SP,最多只剩下14個寄存器。
我們可以測試太多則會覆蓋+操作數是通過刪除所有則會覆蓋問題r0
... r12
; 這讓它編譯(進入錯誤的代碼!!)。 https://godbolt.org/z/Z6x78N這不是解決方案,因為它引入了巨大的錯誤,這就是我如何確認這是問題所在。
每當你的內聯asm模板以mov
開始從輸入寄存器操作數復制到硬編碼寄存器時,你通常都會做錯。 即使你有足夠的寄存器,編譯器也必須發出代碼才能將變量放入寄存器,然后你手寫的asm使用另一個mov
來mov
地復制它。
有關更多指南,請參閱https://stackoverflow.com/tags/inline-assembly/info 。
而是首先使用register int foo asm("r0")
向編譯器請求該寄存器中的輸入,或者更好地讓編譯器通過使用%0
或等效的命名操作數(如%[src1]
而不是你的asm模板里到處都是硬編碼的r0
。 命名操作數的語法是[name] "r" (C_var_name)
。 他們沒有匹配,但他們不必是唯一的要么; 使用相同的asm操作數名稱作為C var名稱通常很方便。
然后你可以刪除大多數GP寄存器上的clobbers。 您需要告訴編譯器您修改的任何輸入寄存器,例如使用"+r"
約束而不是"r"
(然后在asm修改后不使用該C變量)。 或者使用"=r"
輸出約束和匹配輸入約束(如"0" (var)
將該輸入放在與輸出操作數0相同的寄存器中。 "+r"
在包裝函數中更容易,其中C變量是以后沒用過。
如果你使用虛擬輸出操作數來讓編譯器進行寄存器分配,你可以刪除向量寄存器上的clobbers,但如果你只是留下那些硬編碼的話,它基本上沒問題。
asm( // "mov r0, %[src1]; " // remove this and just use %[src1] instead of r0
"... \n\t"
"VST1.16 {d30[0]}, [%[dstData]]! \n\t" //restore img_temp[m][n] to pointer data
"... \n\t"
: [src1]"+&r"(src1), [src2]"+&r"(src2), [dstData]"+&r"(dstData),
[dstSum]"+&r"(dstSum), [height]"+&r"(height)
: [temp_comp1] "r"(temp_comp1), [niter] "r"(numofiterations),
[temp_comp2] "r"(temp_comp2), [temp_comp3] "r"(temp_comp3),
...
: "memory", "cc", all the q and d regs you use. // but not r0..r13
);
您可以查看編譯器的asm輸出,以查看它如何填充您給出的asm模板中的%0
和%[name]
操作數。 使用"instruction \\n\\t"
使這個可讀;
將所有指令放在asm輸出的同一行上。 (C string-literal concatenation不引入換行符)。
讀/寫操作數上的早期clobber聲明確保沒有任何僅輸入操作數與它們共享一個寄存器,即使它們讓編譯器知道temp_comp1 == height
。 因為temp_comp1
的原始值仍然需要從寄存器%[temp_comp1]
讀取,即使在修改了%[height]
。 所以他們不能兩個都是r4
例如。 否則,如果沒有&
在"+&r"
,如果只在讀取所有輸入后寫入輸出,編譯器可以選擇提高效率。 (例如,當包裝單個指令時,如GNU C inline asm旨在有效地執行)。
注意: char array1[16]
和2不需要是volatile
; 即使您只是將指針傳遞給它們,asm語句中的"memory"
clobber就足夠了,而不是將它們用作"m"
輸入操作數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.