Due: before class on Monday 24 August. Bring your completed homework, printed out, with you to class.

Note: In our homeworks, we will generally have some “looking back” questions (designed to help you for exam prep), and some “looking ahead” questions (designed to prepare you for next week’s classes).

Remember that you are expected to work through these problems on your own and in their entirety. Try hard to solve them on your own and write down your best attempt. Then use your neighbors, your notes, AI tools, the sample solutions provided, or anything else to understand where you went wrong, so you can get it right next time!

Looking back

  1. For each of the Java programs below, indicate whether it has an error in syntax, an error in program semantics, or no error at all.

    (Assume each code snippet appears inside a valid main() method, with no other definitions.)

    1. String s;
      System.out.println(s);
    2. String s = "unicorn"s";
    3. int x = (1 + 2)) * 3);
    4. int x = 10;
      System.out.println(x[20]);
  2. Here is a specification for a very simple language that just lets us do some logic operations.

    (If you don’t remember it from your computer architecture class, the NAND operation on two boolean values means “not (X and Y)”.)

    COOLBOOL language
    
    SYNTAX
    
    There are three kinds of Expressions:
    
    *   A single character `T`
    
    *   A single character `?`
    
    *   Two Expressions next to each other with no space in between
    
    A Statement is a single Expression surrounded by square brackets
    `[]`.
    
    A Program is a sequence of one or more statements.
    
    SEMANTICS
    
    *   `T` always evaluates to True.
    
    *   Evaluating `?` causes a single character to be read in from standard in.
        If that character is `Y`, then the result of the evaluation
        is True; otherwise, the result of the evaluation is False.
    
    *   Two expressions next to each other evaluate to the logical
        NAND of the two values.
    
    A Statement with square brackets evaluates the contained
    expression then prints either `true` or `false`.
    
    A Program executes each Statement in order.
    
    1. Write a valid COOLBOOL program that will always print true.

    2. Write a valid COOLBOOL program that will always print false.

    3. Consider the following COOLBOOL program:

      [?T][TT?]
      

      There are two user inputs (?) in this program. Come up with a valid user input (two characters) that would definitely cause this program to print the following:

      true
      true
      
    4. Demonstrate that the COOLBOOL language allows Underspecified Behavior (UB) by writing a program which follows the spec but could be interpreted in two different ways. Explain how the two possible results could happen.

    5. Fix the COOLBOOL spec so that it no longer has UB.

Looking ahead

  1. Watch this YouTube video which very enthusiastically gives an introduction to compilation with LLVM and the LLVM IR language which we will be using.

    The whole video is interesting and relevant for us, but you only need to watch up to 17:30 and can skip the rest where he gets into optimizations and other languages if you don’t have time.

    https://www.youtube.com/watch?v=IR_L1xf4PrU

    Answer these questions to check you were paying attention:

    1. Besides C, what is another language that uses LLVM in its compiler?

    2. Is IR generated before, after, or instead of the compiler producing assembly code?

    3. What command-line flags do we have to pass to clang so that it outputs LLVM IR code?

    4. What does something like %2 represent in LLVM IR code?