This is an old revision of the document!


Homework 5 - Web Assembly

Information

Deadline: 22nd 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 file.alf.json file.wat

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

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

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

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

stack allocation

Stack allocated variables are complex and string variables from a function 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

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

Parameters are allocated using Web Assembly parameters.

Steps for writing the homework

  • Compute the allocation for each global variable (variables sizes are described below)
  • Allocate each local variable to a web assembly local variable
  • Transform each AST element into the corresponding Web Assembly instruction(s)
  • Transform each function (and its statements) into the corresponding Web Assembly instructions

ASM in AST

To read and write from and to the screen, the following function, that are declared in Alf will not be transformed in WebAssembly but imported:

  • readint
  • readfloat
  • readchar
  • writeint
  • writefloat
  • writechar

Bonus

Implement the homework so that is may use strings.

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.1588762476.txt.gz · Last modified: 2020/05/06 13:54 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