PDA

View Full Version : Constant expressions resolved at compile time?



Phil Pilgrim (PhiPi)
03-17-2006, 03:23 PM
Chip,

Do expressions within Spin methods that involve constants get evaluated at compile time or runtime? ('Just wondering how much hand optimization to do.)

Thanks,
Phil

Paul Baker
03-17-2006, 08:42 PM
There is the CONSTANT operator which can direct the compiler that the following expression is to be evaluated as a compile time constant.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1+1=10

cgracey
03-18-2006, 12:55 AM
Phil,

Like Paul said, there is a CONSTANT operator which will tell the compiler to resolve a constant expression down to a single constant, rather than generate the bytecode for run-time solving. Here are examples of each way:

x := 5 * 2 + 3 'Generate byte code to solve 5* 2 + 3 at run-time

x := constant(5 * 2 + 3) 'Generate constant 13, saves 6 bytes, faster


Phil Pilgrim said...
Chip,

Do expressions within Spin methods that involve constants get evaluated at compile time or runtime? ('Just wondering how much hand optimization to do.)

Thanks,
Phil

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Chip Gracey
Parallax, Inc.

Phil Pilgrim (PhiPi)
03-18-2006, 01:57 AM
Thanks, guys.

I guess I missed the constant operator. ('Can't wait for the paper version of the manual to come out, so I can just sit down and read through the whole thing! http://forums.parallax.com/images/smilies/smile.gif )

'Not sure why I'd ever want constant expressions to evaluate at runtime, though, given the choice. Somewhere down the road, this would be a nice thing to have the compiler do either by default or via an option flag. The problem, though (if it matters), is that it becomes hard for the programmer to know which constant expressions get pre-evaluated and which ones get deferred. For example, 3 + 3 + x and x + (3 + 3) would probably get optimized, while x + 3 + 3 would not.

-Phil