Shop OBEX P1 Docs P2 Docs Learn Events
Constant expressions resolved at compile time? — Parallax Forums

Constant expressions resolved at compile time?

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2006-03-17 17:57 in Propeller 1
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

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-03-17 12:42
    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
  • cgraceycgracey Posts: 14,133
    edited 2006-03-17 16:55
    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)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-03-17 17:57
    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! 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
Sign In or Register to comment.