KarolS / KarolS/millfork

Maths doesn't promote to destination type

Open
#119 6 comments 0 reactions 0 assignees View on GitHub
compiler enhancement
Dominant language
Scala
Stars
279
Forks
21
PR merge metrics
No merged PRs in 30d

Description

So Given

```
word CurrentNumMovesLeft

CurrentNumMovesLeft = 148 << 6
```

You then get
```
LDA #0
STA CurrentNumMovesLeft
LDA #0
STA CurrentNumMovesLeft + 1
```
Which is wrong. You have to do
`; CurrentNumMovesLeft = word(148) << 6`
Which then gets you
```
LDA #0
STA CurrentNumMovesLeft
LDA #$25
STA CurrentNumMovesLeft + 1
```
It should detect that the destination is a word and limit the value to the size of the destination without having everything cast. It should also perform all maths at the highest needed and then only truncate at the end.

Contributor guide

Open the contributing guide

Research direction

Use the issue's word assignment example as the reproduction case, comparing the generated assembly with and without the explicit word cast. Trace the compiler's type promotion and arithmetic evaluation entry points, then verify that destination-width truncation happens only at the end and that the generated code preserves the expected high byte.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.