How are consts allocated a type?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

How are consts allocated a type?

Post by SHughes_Fusion » Thu Oct 01, 2015 7:35 am

Is there a 'rule' as to what type / size the compiler allocates to consts where a type is not specifically declared?

By chance in a recent program I found that the compiler was assigning a non-byte type to a const which was given a value well within the range of a byte and which was only used in expressions using byte variables. When I declared it as a byte the code size reduced by about 300 words.

The lesson for future seems to be always declare a type but I'm wondering now how important it will be to go back through all my old projects and do this as well....

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: How are consts allocated a type?

Post by Jerry Messina » Thu Oct 01, 2015 5:24 pm

>Is there a 'rule' as to what type / size the compiler allocates to consts where a type is not specifically declared?

According to the manual:
Swordfish will automatically assign the type of a constant, based on the expression itself. For example,

const MyStr = "Hello World" // a string constant
const ValueA = -100 // a signed constant
const ValueB = 100 // an unsigned constant
const ValueC = 0.4 // a floating point constant
But it doesn't really mention anything about the 'size' used. Usually it's pretty good at recognizing it, but I guess there are cases
where it choses a larger type than is absolutely req'd.

Got an example of what you had vs what you ended up with?

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: How are consts allocated a type?

Post by SHughes_Fusion » Mon Oct 05, 2015 8:39 am

Thanks, Jerry, that is all I could find as well.

I can't easily post the full code and can't seem to replicate it by cutting it down. I think it must just be one of those cases where the compiler does something odd which isn't repeatable.

This was a sort of 'compound' constant. I had one constant I'd defined for the number of 'steps' in a software PWM cycle and I was dividing this by another constant. Both were bytes but what I think has happened is it has created the result as a float. I thought a byte divided by a byte always resulted in a byte but not in this case.

If I'm able to replicate it I'll post an update...

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: How are consts allocated a type?

Post by Jerry Messina » Mon Oct 05, 2015 9:32 am

How do the consts show up in the IDE Code Explorer window when you use 'const as byte' vs without? Does that evaluate them differently?
Maybe that'll shed some light on things. If not, can you take a look at the asm difference between the two in the code where you use the const?

In general I don't usually bother declaring the type so I wouldn't be too worried about going back and changing all your previous code (unless you have a lot of free time on your hands), but you must have stumbled across some sequence that's confusing things.

Post Reply