Page 1 of 1
DJGPP Inline Problem
Posted: Wed Sep 08, 2010 4:06 am
by Mohanty
why it is showing an error "can't find a register in class AREG while reloading asm" ?
extern inline int strlen(const char * s)
{
register int __res __asm__("cx");
__asm__("cld\n\t"
"repne\n\t"
"scasb\n\t"
"notl %0\n\t"
"decl %0"
:"=c" (__res):"D" (s),"a" (0),"0" (0xffffffff):"di");
return __res;
}
Re: DJGPP Inline Problem
Posted: Wed Sep 08, 2010 11:52 am
by tharkun
Try getting rid of __asm__("cx") so your code should look like this (oh and try to use code tags when posting)
Code: Select all
extern inline
int strlen (const char * s)
{
register int __res;
__asm__ ("cld\n\t"
"repne\n\t"
"scasb\n\t"
"notl %0\n\t"
"decl %0"
: "=c" (__res) : "D" (s), "a" (0), "0" (0xffffffff) : "di");
return __res;
}
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 1:45 am
by Mohanty
Thank You JoeDavis, But is is Showing The Same Problem, But Now I have Found The Solution.
Solution:
When -O2 (Optimization) is Enabled Then It is Showing The Problem. Because Optimization Don't Support Inline Assembly.
Eg: gcc -g -W -Wall -O2 -oxyz.o xyz.c
After Removing "-O2" it is working Cool.
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 3:04 am
by Combuster
SatyaNarayan wrote:Because Optimization Don't Support Inline Assembly.
That's utterly Bulls**t. It means you depend on GCC to manage resources in a specific way, which differs between O0 and O2.
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 6:50 am
by Mohanty
Hi Combuster,
Please Compile The Above Code in GCC, with -O2 Option. I am Sure it will show Error Message. Remove That Option It will work, then what is That means.......? may be it don't supports Optimization.
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 7:53 am
by gerryg400
If you have a reg in the input list, it should not also be in the clobber list. So remove the "di". Actually I think it should be "%di", but it must be removed anyway.
BTW, I tried your code with "di" removed at various levels of -O and it made no difference. In all cases it compiled, ran and gave the correct length of the strings I tried.
Also, Combuster is correct I think. You just didn't understand the meaning of his comment. I didn't either until just now. Certainly, gcc inline assembler does support optimisation.
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 3:36 pm
by Owen
gerryg400 wrote:If you have a reg in the input list, it should not also be in the clobber list. So remove the "di". Actually I think it should be "%di", but it must be removed anyway.
BTW, I tried your code with "di" removed at various levels of -O and it made no difference. In all cases it compiled, ran and gave the correct length of the strings I tried.
Also, Combuster is correct I think. You just didn't understand the meaning of his comment. I didn't either until just now. Certainly, gcc inline assembler does support optimisation.
If its modified, it must be listed as clobbered; however, you are generally best specifying it as an output (GCC behaves badly when an input register is clobbered - this may be a bug fixed in latter versions)
As for why the OP's code is failing: You have overly constrained the optimizer and it has ran out of registers. Sounds like you need to upgrade also. I have done significantly more complex things with GCC inline assembly (Including one expression where, of all 15 GPRs, 13 were clobbered and two were inout!)
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 5:00 pm
by gerryg400
gerryg400 wrote:If you have a reg in the input list, it should not also be in the clobber list. So remove the "di". Actually I think it should be "%di", but it must be removed anyway.
BTW, I tried your code with "di" removed at various levels of -O and it made no difference. In all cases it compiled, ran and gave the correct length of the strings I tried.
Also, Combuster is correct I think. You just didn't understand the meaning of his comment. I didn't either until just now. Certainly, gcc inline assembler does support optimisation.
Sorry SatyaNarayan, I mis-wrote. It was actually JoeDavis code that I tried not your code.
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 7:52 pm
by Mohanty
Hi Combuster,
I am Really Really Sorry, That I Misunderstood You.
Re: DJGPP Inline Problem
Posted: Thu Sep 09, 2010 8:31 pm
by gerryg400
Owen wrote:If its modified, it must be listed as clobbered; however, you are generally best specifying it as an output (GCC behaves badly when an input register is clobbered - this may be a bug fixed in latter versions)
In later gcc versions (2.8 and later) gcc doesn't like you to use a clobbered reg as input or output. It seems that the best thing to do is, if an input reg is clobbered list it as a fake output. Older versions of gcc weren't so fussy but 4.51 definitely doesn't always allow input regs in the clobber list. It will complain about running out of regs. And as the OP found this might depend on the optimisation level.
Owen wrote:As for why the OP's code is failing: You have overly constrained the optimizer and it has ran out of registers. Sounds like you need to upgrade also. I have done significantly more complex things with GCC inline assembly (Including one expression where, of all 15 GPRs, 13 were clobbered and two were inout!)
The first part is correct but an upgrade to gcc won't help.
I feel this whole thing is a clear change in the way inline assy works.
The gcc manual wrote:You may not write a clobber description in a way that overlaps with an input or output operand. For example, you may not have an operand describing a register class with one member if you mention that register in the clobber list. Variables declared to live in specific registers (see Section 5.40 [Explicit Reg Vars], page 345), and used as asm input or output operands must have no part mentioned in the clobber description. There is no way for you to specify that an input operand is modified without also specifying it as an output operand. Note that if all the output operands you specify are for this purpose (and hence unused), you will then also need to specify volatile for the asm construct, as described below, to prevent GCC from deleting the asm statement as unused