Shop OBEX P1 Docs P2 Docs Learn Events
Constant folding — Parallax Forums

Constant folding

BradCBradC Posts: 2,601
edited 2009-06-17 03:40 in Propeller 1
I've just seen the Parallax compiler fold a constant and I can't figure out why it would do it here and never previously.

The code in question is in the Parallax Examples :
(Ch3-Ex12)-RealNumbers

Its the last line in the file and it's entirely constants.

Term.Dec(trunc(B*K)*trunc(C*K)/trunc(D*K))

The code generated is this :
0:1 : 007A :       39 09 34  : Constant 2 Bytes - 09 34 
0:1 : 007D :       06 02 05  : Call Obj.Sub 2 5
0:1 : 0080 :             32  : Return




Yet this code further up

Term.Dec(iB*iC/iD)

Generates the following :

0:1 : 0031 :             01  : Drop Anchor 
0:1 : 0032 :       39 02 F8  : Constant 2 Bytes - 02 F8 
0:1 : 0035 :       39 0F 23  : Constant 2 Bytes - 0F 23 
0:1 : 0038 :             F4  : Math Op *
0:1 : 0039 :       39 04 E2  : Constant 2 Bytes - 04 E2 
0:1 : 003C :             F6  : Math Op /
0:1 : 003D :       06 02 05  : Call Obj.Sub 2 5




Each piece of code is working on constants defined in the CON section.

To say I'm baffled is putting it very mildly! This is the first time I've ever seen it fold a constant. (scratches head)

On another note, this example has exposed a gross hole in the bst[noparse][[/noparse]c] compilers with regard to the ROUND() and TRUNC() operators that none of my previous tests have exposed.
This is the only file in all Parallax supplied spin that triggers the bug. None of the Hydra or Obex source triggers it either, but please be aware.

CON
  A = 123.456
  B = 234.567

PUB ABCD | X

 X := trunc(A*B)



Will not work with any published versions of the compiler. (It does not emit bytecode for the value generated by TRUNC() - Obviously not a common construct as I've never hit it before)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Eccles : How do I open the door?
Bluebottle : You turn the knob on your side
Eccles : I haven't got a knob on my side!
Sign In or Register to comment.