Welcome to the last common assignment for the Amy compiler. At this point, we are finally done with the frontend: we have translated source programs to ASTs and have checked that all correctness conditions hold for our program. We are finally ready to generate final code for our program. What we choose as our target language is WebAssembly.
In our case, for simplicity we will not use a browser, but instead
nodejs, which is essentially the js engine of Chrome distributed as a standalone tool. When you run your complete compiler (or the reference compiler) with no options on program
p, it will generate four different files under the
p.watis the wasm output of the compiler in text format. Use it to debug (or marvel at) your generated code.
p.wasmis the binary output of the compiler. This is what
nodejswill use. To translate to the binary format, we use a translator provided by the wasm developers which we distribute under the
bindirectory in our compiler.
p.jsis the js wrapper which we will run with nodejs.
p.htmlshould be an html wrapper to use in a browser, but is unfortunately currently not functional.
To run the program, simply type
- After you install, run
npm install deasyncat the directory you plan to run
amyc, i.e. the toplevel directory of the compiler.
- Make sure the
wat2wasmexecutable is visible, i.e. it is in the system path or you are at the toplevel of the
WebAssembly and Amy
Look at this presentation for the main concepts of how to translate Amy programs to WebAssembly.
You can find the annotated compiler output to the concat example here.
The assignment code
The code for the assignment is divided into two directories: wasm for the modelling of the WebAssembly framework, and codegen for Amy-specific code generation. There is a lot of code here, but your task is only to implement code generation for Amy expressions within
wasm/Instructions.scalaprovides types that describe a subset of WebAssembly instructions. It also provides a type
Codeto describe sequences of instructions. You can chain multiple instructions or
Codeobjects together to generate a longer
wasm/Function.scaladescribes a wasm function.
LocalsHandleris an object which will create fresh indexes for local variables as needed.
- A Function contains a field called
- The only way to create one is
Function.apply. Its last argument is a function from a
Code. The reason for this unusual choice is to make sure the Function object is instantiated with the number of local variables that will be requested from the LocalsHandler. To see how it is used, you can look in
codegen/Utils.scala(but you won't have to use it directly).
wasm/ModulePrinter.scaladescribe a wasm module, which essentially contains a few functions and the module headers.
codegen/Utils.scalacontains a few utility functions (which you should use!) and implementations of the built-in functions of Amy. Use the built-ins as examples.
codegen/CodeGen.scalais the focus of the assignment. It contains code to translate Amy modules, functions and expressions to wasm code. It is a pipeline and returns a wasm Module.
codegen/CodePrinter.scalais a Pipeline which will print output files from the wasm module.
The cgExpr function
The focus of this assignment is the cgExpr function, which takes an expression and generates a
Code object. It also takes two additional arguments: (1) a LocalsHandler which you can use to get a new slot for a local when we encounter a local variable, or you need a temporary variable for your computation. (2) a map from Identifiers to locals slots in the wasm world. For example, if locals contains a pair
i → 4, we know that
get_local 4 in wasm will push the value of i to the stack. Notice how locals is instantiated with the function parameters in
As usual, merge the stubs of the parser into your main branch by typing
git fetch -
git merge origin/Lab06
origin is the name of your remote repository).
The structure of your project
src directory should be as following:
amyc ├── Main.scala (updated) │ ├── analyzer │ ├── SymbolTable.scala │ ├── NameAnalyzer.scala │ └── TypeChecker.scala │ ├── ast │ ├── Identifier.scala │ ├── Printer.scala │ └── TreeModule.scala │ ├── codegen (new) │ ├── CodeGen.scala │ ├── CodePrinter.scala │ └── Utils.scala │ ├── interpreter │ └── Interpreter.scala │ ├── parsing │ ├── ASTConstructor.scala │ ├── ASTConstructorLL1.scala │ ├── Parser.scala) │ ├── Lexer.scala │ └── Tokens.scala │ ├── utils │ ├── AmycFatalError.scala │ ├── Context.scala │ ├── Document.scala │ ├── Pipeline.scala │ ├── Position.scala │ ├── Reporter.scala │ └── UniqueCounter.scala │ └── wasm (new) ├── Function.scala ├── Instructions.scala ├── ModulePrinter.scala └── Module.scala
You can deliver this assignment after the midterm.
Deadline: Friday 22 December, 23:59
IMPORTANT: Please remember to deliver the commit you want us to grade under https://larasrv13.epfl.ch/clp17/deliverables