Due: before class on Monday 25 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
    
    Expressions:
    
    *   A single character `T` is an expression that
        always evaluates to True.
    
    *   A single character `?` is an expression that
        reads a number from standard in and evalueates to False if
        the input number was 0, and otherwise evaluates to True.
    
    *   Two expressions next to each other (with no spaces between
        them) is an expression
        that evauates to the logical NAND of the two values.
    
    Statements:
    
    *   An expression surrounded by `[]` square brackets
        causes the expression to be evaluated, and then printed as
        `true` or `false`.
    
    A COOLBOOL Program is any sequence of Statements.
    
    1. Write a valid COOLBOOL program that will print true.

    2. Write a valid COOLBOOL program that will 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 numbers) 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?