Q1. Global variables Add support for global variables. The following rules in Decaf are the ones that involve the use of global variables.
*Check image.JPG for details*
Q2. Controlflow and loops: The following fragment of Decaf syntax adds control flow (if statements) and loops (while and for statements) to Decaf.
Your program must implement shortcircuit evaluation for boolean expressions.
*Check image.JPG for details*
Q3. Semantic checks Perform at least the following semantic checks for any syntactically valid input Decaf program:
(a) A method called main has to exist in the Decaf program.
(b) Find all cases where there is a type mismatch between the definition of the type of a variable and a value assigned to that variable. e.g. bool x; x = 10; is an example of a type mismatch.
(c) Find all cases where an expression is wellformed, where binary and unary operators are distinguished from relational and equality operators. e.g. true + false is an example of a mismatch but true != true is not a mismatch.
(d) Check that all variables are defined in the proper scope before they are used as an lvalue or rvalue in a Decaf program (see below for hints on how to do this).
(e) Check that the return statement in a method matches the return type in the method definition. e.g. bool foo() { return(10); } is an example of a mismatch.
Raise a semantic error if the input Decaf program does not pass any of the above semantic checks.
Your program should take a syntactically valid Decaf program as input and perform all the semantic checks listed above. You can optionally include any other semantic checks that seem reasonable based on your analysis of the language. Provide a readme file with a description of any additional semantic checks.
Q4. Code Optimization using LLVM optimization passes:
(a) Convert stack allocation usage (alloca) into register usage (mem2reg)
(b) Simple “peephole” optimization (instruction combining pass)
(c) Reassociate expresssions
(d) Eliminate common subexpressions
(e) Simplify the control flow graph (CFG simplification)
You should modify the source code in your yacc program using the ‘FunctionPassManager LLVM API’ call.
You can even write your own LLVM pass using the documentation in https://llvm.org/docs/WritingAnLLVMPass.html.
Implement at least the following
Q5. The Decaf compiler
Create a yacc/lex program that accepts any syntactically valid Decaf program as defined in the Decaf specification and produces LLVM
assembly language output. Your program should reject any syntactically invalid Decaf program and provide a helpful error message (the quality of the error reporting is up to you – at least report the line and character number where the syntax error is thrown). Your program should also perform the semantic checks defined in the Decaf specification and the code optimizations defined in Q. 4 above. Make sure that make will compile your program. Provide the details of your implementation in the readme file (it is essential for marking this assignment).
Attachment:- hw4.tgz