Compiler Construction 2013 
Computer Laboratory 
University of Cambridge 
Timothy G. Griffin (tgg22@cam.ac.uk) 

=============================================================
     Slang.1 Programming Excersises (in SML) 
=============================================================

These are recommended programing tasks, listed from easiest to 
more difficult. 

0) Write, compile, and run more Slang.1 programs. 

1) Extend Slang.0 with the following boolean operators 
     || 
     && 

   Push this extension all the way through the "front-end" (lexing, 
   parsing, type checking). 

2) Push the extension (1) all the way through to code generation 
   for the VSM. 

   We evaluate "e1 op e2" from left-to-right.  However, 
   these operations are "short-circuited". This 
   means that  and if e1 evalutes to false and 
   op = &&, then we do not evaluate e2, and if e1 evalutes to true and 
   op = ||, then we do not evaluate e2. 

3) Push the extension (1) all the way through to code generation 
   for the VRM.0.  (see (2) for note on "short-circuited") 

4) Can you improve the code generated by the compiler by lifting 
   "constant expressions" out of loop bodies?  (For either VRM, VSM
   or both).  Part of this problem is to come up with a good definition 
   of a "constant expression". 

5) Can you improve the VRM.0 code generated by the compiler by 
   reducing the number of temporary locations generated? 
   When are "temporary locations" really needed? 

6) Generate JVM or CLI code from VSM assembler. 

7) Generate x86, MIPS, or Dalvik code from VRM assembler. 

8) Currently, we compile this way: 

   to VRM : expr -> normal_expr -> vrm_assembler ... 
   to VSM : expr ----------------> vsm_assembler ... 

   Could we instead invent an intermediate langauge IL so that 
   for both virtual machines we first do 

    to IL  : expr -> IL 

   and then 

   to VRM : IL -> vrm_assembler ... 
   to VSM : IL -> vsm_assembler ... 
   
   Here is a proposed Intermediate Language (inspired by 
   Andrew Appel's TREE langauge): 

-----------------------

datatype il_expr = 
         IL_Skip
       | IL_Integer of int
       | IL_Boolean of bool
       | IL_UnaryOp of unary_oper * il_expr
       | IL_Op of oper * il_expr * il_expr
       | IL_Deref of loc
       | IL_Print of (type_expr option) * il_expr

datatype il_stm = 
         IL_Expr of il_expr 
       | IL_Assign of loc * (type_expr option) * il_expr
       | IL_Seq of il_expr list 
       | IL_If il_expr * code_loc code_loc 
       | IL_Jmp code_loc 

datatype il_code = 
         IL_Code of il_stm
       | IL_Labelled of il_code_loc * il_stm

type il_program = il_code list 


-----------------------

Can you make it work? 
      
