Episode 3 Tami's Suspicious Sum

The Life of Code 04

Momo's Transformation Factory

Can the same code take on a whole new form?

Here is a tree that passed Tami's check.

Now it is on its way to Momo's factory.

Can its meaning stay the same when its shape changes?

Today's odd question

Momo has just been handed the tree for 1 + 2, and it passed its check.

What will Momo do with it?

So that's your guess.

Let's see what Momo does with the tree.

Comic

Scene 1 At the factory door

TamiAll checked. Your turn.

MomoGot it. I'll take good care of it.

Scene 2 The frame on the workbench

LineySo what happens to me now?

MomoI'm changing your shape. Not your meaning.

Scene 3 The card rack

LineyHey, my tree is gone!

MomoFollow the threads.They show which cards this one points to.

Scene 4 Strange words

LineyWhere did my plus go?

MomoIt's right here. Follow the threads from this card.

Scene 5 The shelf

LineySo what shows up on the screen?

MomoWe'll find out with Tinker next time.Want to follow one of these threads first?

Which card did the tree's plus become?

Let's follow the threads.

A little experiment

Follow Momo's Threads

Topaz compiler 5.21.1 record

The same 1 + 2 has gone from a tree to cards.

For now, only the cards for the expression are out.

Which two numbers does the addition card use? Follow its two threads.

Pick a thread to follow

Choose a thread from the list below. The thread and the cards at both ends are highlighted together.

You can read what a card uses by following its threads.

Seen both threads? Open "One more" below.

One more 1 + 2 * 3

What does the addition card add this time? Follow its threads.

Pick a thread to follow

Choose a thread from the list below. The thread and the cards at both ends are highlighted together.

The addition card's second thread leads to the multiplication card. Follow that card's two threads and you'll find 2 and 3. This addition uses 1 and the result of 2 * 3.

Compare with the tree

The multiplication sits under the addition in the tree. On the cards, it sits at the end of the addition's thread.

We'll see why the multiplication ends up under the addition in Episode 8.

Two outer cards for 1 + 2 * 3

Here are all the cards for 1 + 2 * 3. Around the five expression cards are a statement card and a program setup card.

In Episode 2 we looked at the expression part of the tree. These two cards come from outside it.

The threads run from program setup to the statement to the addition.

  1. program setup card

    The program setup card's first thread leads to the statement card.

  2. statement card

    The statement card's first thread leads to the addition card.

  3. add card

    The addition card's first thread leads to the 1 card.

    The addition card's second thread leads to the multiplication card.

  4. 1 card
  5. multiply card

    The multiplication card's first thread leads to the 2 card.

    The multiplication card's second thread leads to the 3 card.

  6. 2 card
  7. 3 card

The threads are part of the story. Which card points to which comes from the real compiler record.

The real inside story

New shape, same meaning

Momo did the second thing. Momo rewrote the tree for 1 + 2 as cards. The addition card still points to the cards for 1 and 2. Next time, we'll run a program with lines that write to the screen and see 3 appear.

We rewrite the code into a form the next stage can use, linking each operation to the values it needs.

A compiler uses intermediate representations, or IRs, to work with a program. Here it takes the code it has checked and puts it into a form the next stage can use. That change is called lowering.

Each card comes from an operation line Topaz wrote to lowered.jsonl, and only the ones for the expression are drawn. 1 + 2 shows three of its five operations, and 1 + 2 * 3 shows five of its seven.

These cards aren't commands the computer carries out one at a time. You can read what a card uses by following its threads.

Does every compiler make cards like these?

No. How many intermediate forms a compiler goes through, and what they look like, differs from compiler to compiler.

What you saw today is the record of Topaz compiler 5.21.1.

In one line

What we found out today

A compiler rewrites code that passed its checks into a form the next stage can use.

The shape changes, but threads still tie what is added to what.

In the first `1 + 2`, which threads would you follow to find the two numbers being added?

Show the answer

Start at the addition card and follow its two threads. The first leads to 1 and the second to 2.

You can find them without memorizing the order of the cards.

  1. add card
  2. 1 card
  3. 2 card

Now you can spot the same meaning wearing a different shape.

Field GuideRevisit these wordsIntermediate Representation

Next story

Episode 5Tinker and the Long-Awaited 3

This time, Tinker has opened the toolbox.

TinkerLet's get ready to run!

When does the actual calculating happen?

Next time, let's meet the moment a program really runs.