Compiler Week 3

Instead of the manual method described below, there is a productivity extension for controlling video playback rate with keyboard shortcuts: Video Speed Controller

To set the YouTube video playback rate to some particular number, e.g.,

π, execute the following command in the browser's console; use Command + Option + J or Control + Shift + J to access Chrome's console.

document.querySelector('video').playbackRate = Math.PI

What this actually does is setting the playback rate of the first <video> tag on the page to

π, which happens to be the main video on any YouTube page.

Many links in this page refer to text fragments which are known to work on Chrome 80 and above. I can't say about other browsers.

Q&A

Classify the following as front-end or back-end components

  • Target code optimizer: back-end
  • Parser: front-end
  • Scanner: front-end
  • IR code generator: front/middle-end
  • Type checker: front-end

Classify 1-3 as (a) or (b)

(a) a common back-end compiling system (b) a retargetable compiler

  1. HP PA-RISC compiler/optimizer: (a)
  2. Intel C++ Compiler Classic (ICC): (a) for C/C++ (b) for x86/x64
    • According to the developer's guide, ICC actually stands for Intel C++ Compiler Classic.
    • x86 refers to IA-32 whereas x64 refers to IA-64
  3. GNU Compiler Collection (GCC): (b)

How to create the first C compiler on the Itanium machine?

  • Write a C compiler in C, compile it on Itanium machines using GCC.
    • This line of reasoning is incoherent. If the first C compiler for Itanium is being developed, GCC wouldn't be able to target Itanium.
  • Write a C compiler in Itanium machine code.
    • One doesn't attempt this feat unless one is Jeff Dean.
  • Develop a cross-compiler on x86 and use it to compile itself into Itanium instructions.
    • A cross compiler is a compiler that can generate instructions for some CPU architecture different from the one it is running on. E.g., GCC running on an Intel machine can compile sources as ARM binaries. A cross compiler targeting Itanium can compile its source code on x86 to produce its Itanium counterpart which can then be executed on Itanium as a native compiler.
    • This illustrates the process.
  • Leave it to Intel/Microsoft, i.e., evil empire.
    • Why not? Why is the professor against this.

How to create the first C compiler on the new Itanium if no cross-compiler is available?

Incremental bootstrapping.

  1. Craft a simple assembler, C1, in Itanium machine code, i.e., directly editing a binary file from scratch.
  2. Craft a more complex assembler, C2, in Itanium assembly with C1.
  3. Craft a simple C compiler, C3, with C2.
  4. Craft a more complex C compiler, C4, with C3.
  5. Continue step 4 for several iterations until a good enough C compiler, C0, is finally produced.

Then, C0 is what we seek for.

Evaluate 1u > -1 in C/C++

Comparing unsigned int with int is defined behavior in C/C++: int is coerced to unsigned int for comparison. Since -1 has all bits being 1 in 2's complement, 1u > -1 evaluates to false in most cases. In fact, C++20 mandates that all values be represented in 2's complement. Here are more C++ specific details.

Which of the following are in the language defined by the AC syntax?

  • f b i a a = 0 b = a + 3.2 p b
    
    
    
    
    
    
    %0
    
    
    
    1
    Dcls
    
    
    
    2
    Dcl
    
    
    
    1->2
    
    
    
    
    
    5
    Dcls
    
    
    
    1->5
    
    
    
    
    
    3
    floatdcl
    f
    
    
    
    2->3
    
    
    
    
    
    4
    id
    b
    
    
    
    2->4
    
    
    
    
    
    6
    Dcl
    
    
    
    5->6
    
    
    
    
    
    9
    Dcls
    
    
    
    5->9
    
    
    
    
    
    7
    intdcl
    i
    
    
    
    6->7
    
    
    
    
    
    8
    id
    a
    
    
    
    6->8
    
    
    
    
    
    10
    
    λ
    
    
    
    9->10
    
    
    
    
    
    11
    Stmts
    
    
    
    12
    Stmt
    
    
    
    11->12
    
    
    
    
    
    19
    Stmts
    
    
    
    11->19
    
    
    
    
    
    13
    id
    a
    
    
    
    12->13
    
    
    
    
    
    14
    assign
    =
    
    
    
    12->14
    
    
    
    
    
    15
    Val
    
    
    
    12->15
    
    
    
    
    
    17
    Expr
    
    
    
    12->17
    
    
    
    
    
    16
    inum
    0
    
    
    
    15->16
    
    
    
    
    
    18
    
    λ
    
    
    
    17->18
    
    
    
    
    
    20
    Stmt
    
    
    
    19->20
    
    
    
    
    
    31
    Stmts
    
    
    
    19->31
    
    
    
    
    
    21
    id
    b
    
    
    
    20->21
    
    
    
    
    
    22
    assign
    =
    
    
    
    20->22
    
    
    
    
    
    23
    Val
    
    
    
    20->23
    
    
    
    
    
    25
    Expr
    
    
    
    20->25
    
    
    
    
    
    24
    id
    a
    
    
    
    23->24
    
    
    
    
    
    26
    plus
    +
    
    
    
    25->26
    
    
    
    
    
    27
    Val
    
    
    
    25->27
    
    
    
    
    
    29
    Expr
    
    
    
    25->29
    
    
    
    
    
    28
    fnum
    3.2
    
    
    
    27->28
    
    
    
    
    
    30
    
    λ
    
    
    
    29->30
    
    
    
    
    
    32
    Stmt
    
    
    
    31->32
    
    
    
    
    
    35
    Stmts
    
    
    
    31->35
    
    
    
    
    
    33
    print
    p
    
    
    
    32->33
    
    
    
    
    
    34
    id
    b
    
    
    
    32->34
    
    
    
    
    
    36
    
    λ
    
    
    
    35->36
    
    
    
    
    
    Prog
    
    Prog
    
    
    
    Prog->1
    
    
    
    
    
    Prog->11
    
    
    
    
    
    $
    
    $
    
    
    
    Prog->$
    
    
    
    
    
    
  • p15
    An id must follow print.
    
    
    
    
    
    
    %0
    
    
    
    Prog
    
    Prog
    
    
    
    1
    Dcls
    
    
    
    Prog->1
    
    
    
    
    
    3
    Stmts
    
    
    
    Prog->3
    
    
    
    
    
    $
    
    $
    
    
    
    Prog->$
    
    
    
    
    
    2
    
    λ
    
    
    
    1->2
    
    
    
    
    
    5
    Stmt
    
    
    
    3->5
    
    
    
    
    
    6
    Stmts
    
    
    
    3->6
    
    
    
    
    
    7
    print
    p
    
    
    
    5->7
    
    
    
    
    
    8
    inum
    15
    
    
    
    5->8
    
    
    expects id
    
    
    
    4
    
    λ
    
    
    
    6->4
    
    
    
    
    
    
  • An empty file
    
    
    
    
    
    
    %0
    
    
    
    Prog
    
    Prog
    
    
    
    1
    Dcls
    
    
    
    Prog->1
    
    
    
    
    
    3
    Stmts
    
    
    
    Prog->3
    
    
    
    
    
    $
    
    $
    
    
    
    Prog->$
    
    
    
    
    
    2
    
    λ
    
    
    
    1->2
    
    
    
    
    
    4
    
    λ
    
    
    
    3->4
    
    
    
    
    
    
  • f b a = 1.0i cc = 0 p a
    Note that a = 1.0 is not a syntax error but a semantics error.
    
    
    
    
    
    
    %0
    
    
    
    1
    Dcls
    
    
    
    2
    Dcl
    
    
    
    1->2
    
    
    
    
    
    3
    floatdcl
    f
    
    
    
    2->3
    
    
    
    
    
    4
    id
    b
    
    
    
    2->4
    
    
    
    
    
    11
    Stmts
    
    
    
    12
    Stmt
    
    
    
    11->12
    
    
    
    
    
    19
    Dcls
    
    
    
    11->19
    
    
    expects Stmts
    
    
    
    13
    id
    a
    
    
    
    12->13
    
    
    
    
    
    14
    assign
    =
    
    
    
    12->14
    
    
    
    
    
    15
    Val
    
    
    
    12->15
    
    
    
    
    
    17
    Expr
    
    
    
    12->17
    
    
    
    
    
    16
    fnum
    1.0
    
    
    
    15->16
    
    
    
    
    
    18
    
    λ
    
    
    
    17->18
    
    
    
    
    
    20
    Dcl
    
    
    
    19->20
    
    
    
    
    
    30
    Stmts
    
    
    
    19->30
    
    
    expects Dcls
    
    
    
    21
    intdcl
    i
    
    
    
    20->21
    
    
    
    
    
    22
    id
    c
    
    
    
    20->22
    
    
    
    
    
    23
    id
    c
    
    
    
    24
    assign
    =
    
    
    
    25
    inum
    0
    
    
    
    26
    Val
    
    
    
    26->25
    
    
    
    
    
    27
    Stmt
    
    
    
    27->23
    
    
    
    
    
    27->24
    
    
    
    
    
    27->26
    
    
    
    
    
    28
    Expr
    
    
    
    27->28
    
    
    
    
    
    29
    
    λ
    
    
    
    28->29
    
    
    
    
    
    30->27
    
    
    
    
    
    31
    Stmts
    
    
    
    30->31
    
    
    
    
    
    32
    Stmt
    
    
    
    31->32
    
    
    
    
    
    35
    Stmts
    
    
    
    31->35
    
    
    
    
    
    33
    print
    p
    
    
    
    32->33
    
    
    
    
    
    34
    id
    a
    
    
    
    32->34
    
    
    
    
    
    36
    
    λ
    
    
    
    35->36
    
    
    
    
    
    Prog
    
    Prog
    
    
    
    Prog->1
    
    
    
    
    
    Prog->11
    
    
    
    
    
    $
    
    $
    
    
    
    Prog->$
    
    
    
    
    
    
  • f b i a a = 0 b =-a - 3.2 p b
    
    
    
    
    
    
    %0
    
    
    
    1
    Dcls
    
    
    
    2
    Dcl
    
    
    
    1->2
    
    
    
    
    
    5
    Dcls
    
    
    
    1->5
    
    
    
    
    
    3
    floatdcl
    f
    
    
    
    2->3
    
    
    
    
    
    4
    id
    b
    
    
    
    2->4
    
    
    
    
    
    6
    Dcl
    
    
    
    5->6
    
    
    
    
    
    9
    Dcls
    
    
    
    5->9
    
    
    
    
    
    7
    intdcl
    i
    
    
    
    6->7
    
    
    
    
    
    8
    id
    a
    
    
    
    6->8
    
    
    
    
    
    10
    
    λ
    
    
    
    9->10
    
    
    
    
    
    11
    Stmts
    
    
    
    12
    Stmt
    
    
    
    11->12
    
    
    
    
    
    19
    Stmts
    
    
    
    11->19
    
    
    
    
    
    13
    id
    a
    
    
    
    12->13
    
    
    
    
    
    14
    assign
    =
    
    
    
    12->14
    
    
    
    
    
    15
    Val
    
    
    
    12->15
    
    
    
    
    
    17
    Expr
    
    
    
    12->17
    
    
    
    
    
    16
    inum
    0
    
    
    
    15->16
    
    
    
    
    
    18
    
    λ
    
    
    
    17->18
    
    
    
    
    
    20
    Stmt
    
    
    
    19->20
    
    
    
    
    
    31
    Stmts
    
    
    
    19->31
    
    
    
    
    
    21
    id
    b
    
    
    
    20->21
    
    
    
    
    
    22
    assign
    =
    
    
    
    20->22
    
    
    
    
    
    23
    ?
    
    
    
    20->23
    
    
    expects Val
    
    
    
    25
    Expr
    
    
    
    20->25
    
    
    
    
    
    37
    minus
    -
    
    
    
    23->37
    
    
    
    
    
    24
    id
    a
    
    
    
    23->24
    
    
    
    
    
    26
    minus
    -
    
    
    
    25->26
    
    
    
    
    
    27
    Val
    
    
    
    25->27
    
    
    
    
    
    29
    Expr
    
    
    
    25->29
    
    
    
    
    
    28
    fnum
    3.2
    
    
    
    27->28
    
    
    
    
    
    30
    
    λ
    
    
    
    29->30
    
    
    
    
    
    32
    Stmt
    
    
    
    31->32
    
    
    
    
    
    35
    Stmts
    
    
    
    31->35
    
    
    
    
    
    33
    print
    p
    
    
    
    32->33
    
    
    
    
    
    34
    id
    b
    
    
    
    32->34
    
    
    
    
    
    36
    
    λ
    
    
    
    35->36
    
    
    
    
    
    Prog
    
    Prog
    
    
    
    Prog->1
    
    
    
    
    
    Prog->11
    
    
    
    
    
    $
    
    $
    
    
    
    Prog->$
    
    
    
    
    
    

What is the associativity of op given the grammar expr -> expr op primary? How can the grammar be re-defined so as to invert the associativity of op?

  • Left associative.
  • expr -> primary op expr

Identify expressions with constant folding potential

  • 3 + 5 * b
  • a + 5- 4
    Left association doesn't make way for folding 5 - 4.
  • a +2 * 5+ c
  • 2 + 5+ c