Homework 5 - Web Assembly

Information

Deadline: 26nd of May, 23:55
Points: 2 points out of the final grade
Upload the homework: vmchecker.cs.pub.ro

What do you have to do

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,]

The AST file is considered to be correct.

You have to write two files:

  • the WAT file (text file)
  • a JSON file with the processed AST so that the symbol table reflects the variable allocation. Tests will only verify the symbol_table. You mad add extra properties to it.

Variable Types

Variables are divided in two types:

  • simple - variables of type int, real, bool and character
  • complex - array and struct
  • strings

We will discuss them in detail. Each variable type from ALF has to be translated into a WebAssembly type following the table.

Simple variable types are allocated as WebAssembly variables, while complex and strings are allocated in the memory space.

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

Variable Values

int

All these values are represented as i32.

real

All these values are represented as f32.

character

All these values are represented as i32, in ascii.

bool

The bool value is represented:

  • true - i32, value 1
  • false - i32 value 0

String

The representation used for strings is Pascal type and has a 256 bytes length:

  • 1 byte for length
  • 255 bytes for symbols

Variable Allocation Rules

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 allocation

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 allocation

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.

You can add a (global $title i32 (i32.const address_of_variable) variable to be easier to read the code.

param allocation

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 allocation

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 allocation

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

You can add a (local $title i32) variable and set its value to (local.set $title address_of_variable) to be easier to read the code.

relocated allocation

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

Main Script

Your main script (script section from the semnatic ast) will be the function in webassembly. The name of the function is up to you.

Imports

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

Strings

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.

Example string.alf string.alf.opt.json string.alf.wat

Steps for writing the homework

  • Compute the allocation for each variable
  • Transform each AST element into the corresponding Web Assembly instruction(s)
  • Transform each function (and its statements) into the corresponding Web Assembly instructions

Bonus Optimization

Implement the homework so that is does the following optimizations:

  • constant folding
  • delete unused variables and functions

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]

[] means that it is optional

Testing

The homework will be tested automatically using a set of public and private tests.

Public 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.

To install nodejs in Linux or Windows Linux Subsystem, do the following:

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

Private Tests

When uploading the homework, we might have some private tests that it needs to pass. vmchecker will run them.

You may always upload the homework as many times you want until the deadline. This will run all the tests for you and display the result.

Upload the homework

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.

Readme

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.

Homework Archive

To upload your homework, please follow the following:

  1. Create a zip (not rar, ace, 7zip or anything else) archive containing:
    • your main file (main.js)
    • your javascript files (*.js)
    • the package.json file
    • the Readme file
  2. sign in with vmchecker
  3. select the Automates et Langages Formelles (FILS) course
  4. select 5. Web Assembly Language
  5. upload the archive

The archive needs to contain the files in its root, not in a folder. DO NOT archive a folder with those file, archive DIRECTLY those files.

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
alf/teme/tema_en_5.txt · Last modified: 2020/05/12 21:01 by alexandru.radovici
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0