程序代写案例-IN2009

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
1
IN2009: Language Processors
Coursework Part 3







Introduction
This is the 3rd and final part of the coursework. In Part 1 you created a parser for the LPL
grammar which, given a syntactically correct LPL program as input, builds an AST
representation of the program. In Part 3 you develop a compiler that processes the AST to
generate IR code.

For this part of the coursework we provide functional code for parsing and type checking.

Module marks
This coursework is worth either 70% or 60% of the coursework marks for the module,
whichever gives you the highest overall mark. This coursework is marked out of 100.

Plagiarism
If you copy the work of others (either that of fellow students or of a third party), with or
without their permission, you will score no marks and further disciplinary action will be taken
against you.

Pair-working
Pair-working is permitted only if you officially registered as a pair. If working as a pair, both
members must submit identical files and both members must contribute equally to the work.
Other stuff you need to know
See Other stuff you need to know at the end of this document.


Submission: Submit two files:
1. A zip archive (not a rar file) of all your source code. If you have
developed in an IDE do not submit the entire project structure, only the
source code, and before you submit check that your source code can be
compiled and executed using command line tools alone (javac and
java).
2. Either an MS Word or PDF file containing your answers to part d.

Note: Compilers which do not compile (see above) will get zero marks.
2
Getting started

Download LPL-compiler.zip from Moodle and extract all files. Key contents to be
aware of:
1. Source code:
• An LPL parser (in package lpl.parser).
• An LPL type checker (in package lpl.staticanalysis).
• A prototype compiler (lpl.compiler.Compiler).
• A top-level program lpl.Compile for running your compiler: this creates
an .ir file containing the IR code generated by your compiler.
2. A jar of compiled library code Backend.jar . This provides the IR AST classes
(package ir.ast) an abstract machine (package tac) an IR parser (package
ir.parser) an IR compiler (package ir.compiler) and top-level programs:
• ir.Compile: takes an .ir file as input and creates two new files: binary
machine code (with extension .tac) and a human-readable assembly version
of the same code (with extension .tacass).
• tac.Exec: for running tac binaries.
Study the code for the prototype compiler in package lpl.compiler. You will find the
following:
1. Class Compiler which implements the Visitor interface (by extending VisitorAdapter)
and contains a compile method which takes an LPL program as a parameter and
returns an IR program as its result. You need to complete the Visitor implementation
as well as the top-level compile method.
2. Support classes FreshLabelGenerator and IRUtils. The class IRUtils provides a
number of convenience static factory methods for building IR ASTs; you will be
writing code which uses these factory methods to build an IR program (you don't
strictly need to use the factory methods, since you could use IR AST constructors
directly, but it will make your life much easier if you do).

Your compiler should generate code which implements all assignable values as integers, as
follows:
• int values: implement in the obvious way.
• bool values: use 0 for false and 1 for true.
• unit: the unit type has only one value; implement as 0.
• array values: like Java, LPL uses reference semantics for arrays; implement as an
integer which is a memory address within the heap where the array data resides. Null
references are implemented as 0.

The parts below should be attempted in sequence. When you have completed one part you
should make a back-up copy of the work and keep it safe, in case you break it in your attempt
at the next part. Be sure to test the old functionality as well as the new (regression testing).
We will not assess multiple versions so, if a later attempt breaks previously working code,
you may gain a better mark by submitting the earlier version for assessment.


3
a) [30 marks] The Basic Compiler: partially complete the implementation of
lpl.compiler.Compiler. To get started without becoming lost in the detail, don't
initially implement support for function definitions, function calls, return statements, or
arrays. The prototype code assumes that a program consists of a single initial “main” function
(the actual function name does not matter) which has zero parameters; it just compiles the
body of this function. A proper treatment of variables is not possible in this version: compile
variables to TEMP expressions for now. Much of the code that you need to write can be found
in the Session 9 slides.
b) [20 marks] Functions: add support for function definitions and function calls. Variables
now must be implemented as MEM expressions using offsets from TEMP FP. You should no
longer assume that the initial procedure always has zero parameters. Instead, your generated
IR code should start with code which loads command-line argument values from the stack and
calls the top-level procedure using these as the actual parameters. Note: the call to the initial
procedure must be followed by a JUMP to the label _END, otherwise execution will fall
through to the following code (the symptom is likely to be an infinite looping behaviour).
Label _END is pre-defined and added by the IR compiler (do not add your own).
You should generate code under the assumption that all command-line arguments have been
pushed on the stack, followed by an argument count. For example, suppose you execute a
compiled program as follows:
java -cp Backend.jar tac.Exec prog.tac 78 29

Before executing prog.tac, the Exec program will initialise the machine
state so that the stack looks like the picture on the left.

Note: initially, FP points to an address just before the base of the stack. Your
generated code can use negative offsets from FP to access the command-line
arguments.



c) [20 marks] Arrays: add support for arrays. Your generated code will need to call the pre-
defined _malloc function to allocate heap memory for array creation. You are not required
to generate code for bounds-checking.

d) [30 Marks] LPL language extension. Design an extension for the LPL language. There are
many features present in languages like Java that are missing from LPL, so you have a wide
range of options. More ambitious choices will have wider scope for marks than trivial ones.
Your extension must be backwards compatible with the existing grammar (the language
generated by your new grammar must include the language generated by the existing
grammar). You should provide:
• A short informal description of your language extension.
• The grammar for your extension (you do not need to repeat all the existing rules, only
new rules and modified versions of any rules which have changed). Use the same
notation as in the existing grammar.
• A detailed description of any necessary code changes or additions to:
o the set of AST classes
o the type-checker
o the compiler
You may use a mixture of natural language and pseudocode or Java.

FP →
78
29
SP → 2

4
Testing
When you have a partially working compiler you can test it by compiling LPL programs to IR
code, then compiling the IR code to a tac executable, then executing the tac code. For
example, you might try:

javac -cp .:Backend.jar lpl/Compile.java
java -cp .:Backend.jar lpl.Compile counter.lpl
java -cp Backend.jar ir.Compile counter.ir
java -cp Backend.jar tac.Exec counter.tac 11
(assuming that counter.lpl is the test input provided for Part 1, the expected output in
this case would be: 11 10 9 8 7 6 5 4 3 2 1 0).
You can use the test inputs from Part 1 but note that these do not comprise a comprehensive
test-suite. You should invent and run your own tests. The document LPL compared with Java
gives a concise summary of how LPL programs are supposed to behave.
If the IR code generated by your LPL compiler is rejected by the IR compiler, or doesn't
execute as you expect, then you should study the .ir file to see why. (If it is accepted by the
IR compiler you can also look at the assembly code in the .tacass file, but this is less
likely to be useful for debugging your LPL compiler.) As always, test incrementally
throughout development, and design test inputs which are as simple as possible for the
behaviour that you want to test.


Note: Windows users
should use semi-colon (;)
as the classpath separator
instead of colon (:).
5
Other stuff you need to know
To compile to correct IR code, you need to know a few things about what the IR compiler
will do with it:
1. TEMP names. TEMP's are the IR counterpart to machine registers (and will in fact be
compiled to registers by the backend compiler). Some names are treated specially by
the IR compiler:
FP is the frame pointer
SP is the stack pointer
RV this where your compiled function bodies must leave their return values
Apart from these, you are free to invent any names you like for TEMP nodes but care
is needed to avoid name clashes, so you are advised to use the
FreshLabelGenerator.freshLab methods.
You will probably notice that in some cases it would be possible to be more
economical and reuse the same TEMP name in different parts of your code: DON'T
be tempted to do this. Firstly, it is easy to get this wrong, leading to some very subtle
bugs in your compiled code (IR code is deliberately designed to allow an unbounded
number of “registers” so that you can avoid these issues). Secondly, even if you
manage to get it right, it is actually likely to result in less efficient executable code
because of the register-allocation algorithm used by the backend compiler.
2. LABEL names. For the most part, you should use freshLab to create label names,
since label names must be unique (but see the remarks below about compiling function
definitions). Don't create any labels with names that start with an underscore: these
are reserved as label names for use by the backend IR compiler.
3. Pre-defined labels. The IR compiler provides the following routines which you can
call in your generated IR code, as required. Each of them takes a single parameter.
_printchar : the parameter is an integer which will be interpreted as a 16-bit
Unicode Plane 0 code point of a character to be printed (the 16 higher-order bits of
the integer are ignored). Note that the first 128 code points coincide with ASCII.
_printint : the parameter is an integer which will be printed as text (with no
newline).
_printstr : the parameter is a memory address for a null-terminated string
constant; any valid memory address can be used but in practice you will always
specify the parameter as NAME lab, where lab is a label name defined in the
(optional) strings section at the start of your generated IR code.
_malloc : the parameter is the number of words of memory to allocate; the start
address of the allocated block is returned. Note that _malloc will allocate memory
which is not currently in use but makes no guarantees about the contents of the
allocated memory (it may contain arbitrary junk).
The IR compiler also adds a label _END to the very end of the compiled code.
4. Function definitions: You will compile an LPL function definition by compiling its
body into a sequence of IR statements, starting with LABEL foo, where foo is the LPL
function name; your code should ensure that the return value is stored in TEMP RV.
To compile the IR sequence into machine code which can be called and returned from,
the backend IR compiler needs to top-and-tail the code that it generates with
instructions for pushing and popping a stack frame; to enable this, your generated code
must include a PROLOGUE at the start (immediately after LABEL foo) and an
EPILOGUE at the end.

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468