The purpose of the homework is to translate an Alf Langauge provided as an AST file with semantic data into Web Assembly. Your program will receive two parameters: source file (the file from the previous homework, with the symbol table, the AST and the error list) and the output file. You don't need a working version of homework 4! The test files have already been generated and you can find them on github. The programs will have any variable type. String concatenation is not provided (except for a few tests test).
node main.js fisier.alf.json fisier.alf.opt.json fisier.wat [optimisations,]
You have to write two files:
Variables are divided in two types:
We will discuss them in detail. Each variable type from ALF has to be translated into a WebAssembly type following the table.
Complex and Strings variables are handled as a pointer value (i32) to the position in memory.
ALF Type | WebAssembly Type |
---|---|
int | i32 |
real | i32 |
character | i32 |
bool | i32 |
string | a 256 bytes memory space |
array (type:[from – to]) | a bytes * length memory space |
struct | a sum of the struct's properties bytes memory space |
All these values are represented as i32.
All these values are represented as f32.
All these values are represented as i32, in ascii.
The bool value is represented:
The representation used for strings is Pascal type and has a 256 bytes length:
Variables may be allocated differently based on their type and their position. Allocation types are global
, data
, param
, local
, stack
and relocated
.
Variable Type | Variable Location | Allocation Type |
---|---|---|
simple | script | global |
complex | script | data |
string | script | data |
simple | function parameter | param |
complex | function parameter | param (sent as pointer, see note) |
string | function parameter | param (sent as pointer, see note) |
simple | function | local |
complex | function | stack |
string | function | stack |
simple | if, for, loop | relocated |
complex | if, for, loop | relocated |
string | if, for, loop | relocated |
Global allocated variables are simple variables from the main script. These will be allocated as (global $title type)
statements.
Example: simple.alf simple.wat
Access to these variables is done using the global.set
and global.get
instructions.
Data allocated variables are complex and string variables from the main script. These will be allocated as in the memory space. Each variable of this type will have an address
parameter with the position of the variable in memory.
Example: simple.alf simple.wat
Access to these variables is done using the i32.load
and i32.store
instructions to load and store the variable address to the data stack.
(global $title i32 (i32.const address_of_variable)
variable to be easier to read the code.
Parameter allocation is used for simple type function parameters. These will be allocated as (param $title type)
statements in the function definition.
Example: definition_variables.alf definition_variables.wat
Access to these variables is done using the local.set
and local.get
instructions.
Local allocated variables are simple variables from a function. These will be allocated as (local $title type)
statements in the function definition.
Example: definition_variables.alf definition_variables.wat
Access to these variables is done using the local.set
and local.get
instructions.
Stack allocated variables are complex and string variables from a function. These will be allocated as in the memory space in a simulated stack. Each variable of this type will have an address
parameter with the position of the variable in memory related to a stack base pointer.
To simulate a stack, you have to declare a global variable (in our example $stack_pointer
) and a local variable in each function (in our example $base_pointer
). Whenever you enter a function, store the value of $stack_pointer
in $base_pointer
. Whenever you exit a function (return or function end), return the original $stack_pointer
from $base_pointer
.
Variable addresses are computed relative to the $base_pointer
. For example, if you have a variable with address 100, the actual variable address will be $base_pointer
+100.
Example: definition_variables_stack.alf definition_variables_stack.alf.wat
(local $title i32)
variable and set its value to (local.set $title address_of_variable)
to be easier to read the code.
Relocated allocation variables are all variables declared in an if, loop or for. These will be relocated as global variables if the statement is in a script or as local variables if the statement is in a function. The variable will be renamed to $(node.symbol_table)+_+variable.title
.
Example: local_variable.alf local_variable.alf.wat
Your main script (script
section from the semnatic ast) will be the function in webassembly. The name of the function is up to you.
Some of the functions in the symbol table have a parameter use
. This means that these functions are imported from the
module declared in the use
property.
Example: 2_simple/real_number.alf real_number.alf.json real_number.alf.wat
All the string constants (values) are stored in the main memory right after the global variables. You can use (data (i32.const address) “\SSvalue”)
where SS is the base16 value of the length. The first byte in the string represents the string's length.
Implement the homework so that is does the following optimizations:
For the bonus, there are two extra parameters in the command line
node main.js fisier.alf.json fisier.alf.opt.json fisier.wat [fold_constants] [unused]
The homework will be tested automatically using a set of public and private tests.
You can download the public tests from the GitHub repository.
To run the tests, download the contents of the repository in the folder with the homework. Enter the verify folder and run ./run_all.sh.
cd verify ./run_all.sh
You will need bash for that. You can use either Linux or Windows Linux Subsystem.
wget https://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.xz tar xvfJ node-v6.10.0-linux-x64.tar.xz cd node-v6.10.0-linux-x64 sudo cp -R * /usr
When uploading the homework, we might have some private tests that it needs to pass. vmchecker will run them.
The homework needs to be uploaded to vmchecker. Login with your moodle user name, select the Automates et Langages Formelles (FILS) course and upload the homework archive.
The readme file has the following format:
Your full name Group An explanation how you wrote your homework, what did you use, what are the main ideas.
To upload your homework, please follow the following:
DO NOT include node_modules.
When the archive is uploaded, vmchecker will run:
unzip archive.zip homework cd homework # if the file yarn.lock exists yarn # else npm install echo '{ "node":true, "esnext":true }' > .jshintrc jshint *.js jison grammar.jison