Skip to content

SpecificationXIII · The engine inside

13.1

The engine's layers

The engine processes a program in layers: each one receives the previous result, checks or transforms it and hands it to the next.

Markdown version

13.1.1 How it works

Figure 13.1-1.From text to execution. The first three layers analyse; the intermediate representation is where the virtual machine and native code part ways.

A column of layers from top to bottom: text, lexical, syntax, semantics and intermediate representation. Two arrows leave the intermediate representation: virtual machine and native code.

LayerReceivesDelivers
lexicaltexttokens: words, numbers, signs
syntaxtokensthe program tree
semanticstreetree with types and names resolved
intermediate representationchecked treeblocks of simple instructions
virtual machine or native codeintermediate representationthe run

The intermediate representation (IR) is the central piece. Every statement is marked with an entry and an exit, and every decision with its branch: that way execution can account for exactly what happens. Translating to the IR does not change the meaning: running it gives the same outputs, in the same order, the language prescribes.

ProgramA small program

Algorithm Pequeno
  Define n As Integer <- 2;
  If n > 1 Then
    Write "grande";
  EndIf
EndAlgorithm

Output

grande

The program finishes normally.

Intermediate representation

module Pequeno en
func suspendible @main() -> unit {
b0() mass 2:
  stmt.enter @2:3-2:27
  %0 = const.i64 2 @2:26-2:27
  stmt.exit @2:3-2:27
  stmt.enter @3:3-5:8
  %1 = const.i64 1 @3:10-3:11
  %2 = int.cmp gt, %0, %1 @3:8-3:9
  decide.bool %2 @3:3-5:8
  branch %2, b1(), b2()
b1() mass 1:
  stmt.enter @4:5-4:19
  %3 = const.str "grande" @4:11-4:19
  out.write %3, newline @4:5-4:19
  stmt.exit @4:5-4:19
  jump b3()
b2() mass 0:
  jump b3()
b3() mass 0:
  stmt.exit @3:3-5:8
  return @6:1-6:13
}

In short

  • The engine works in layers: lexical, syntax, semantics, IR and execution.
  • The IR marks statements and decisions so execution is observable.
  • Translating to the IR keeps the meaning.