This shows you the differences between two versions of the page.
alf:teme:tema_en_5 [2020/05/06 13:30] alexandru.radovici |
alf:teme:tema_en_5 [2020/05/12 21:01] (current) alexandru.radovici |
||
---|---|---|---|
Line 4: | Line 4: | ||
<note important> | <note important> | ||
- | Deadline: **22nd of May, 23:55**\\ | + | Deadline: **26nd of May, 23:55**\\ |
Points: **2 points** out of the final grade\\ | Points: **2 points** out of the final grade\\ | ||
Upload the homework: [[https://vmchecker.cs.pub.ro|vmchecker.cs.pub.ro]]\\ | Upload the homework: [[https://vmchecker.cs.pub.ro|vmchecker.cs.pub.ro]]\\ | ||
Line 11: | Line 11: | ||
===== What do you have to do ===== | ===== 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 [[https://github.com/alexandruradovici/alf/tree/master/Devoirs/assembly/verify/assembly|github]]. | + | 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 [[https://github.com/upb-fils/alf/tree/master/Devoirs/assembly/verify/assembly|github]]. |
- | The programs will have any variable type. String concatenation is not privided (except for a few tests test). | + | The programs will have any variable type. String concatenation is not provided (except for a few tests test). |
<code bash> | <code bash> | ||
- | node main.js file.alf.json file.wat | + | node main.js fisier.alf.json fisier.alf.opt.json fisier.wat [optimisations,] |
</code> | </code> | ||
Line 27: | Line 27: | ||
===== Variable Types ===== | ===== 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. | Each variable type from ALF has to be translated into a WebAssembly type following the table. | ||
Line 43: | Line 49: | ||
| struct | a sum of the struct's properties bytes memory space | | | struct | a sum of the struct's properties bytes memory space | | ||
- | ===== 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`. | ||
- | Variables are divided in two types: | + | ==== Variable Values ==== |
- | * simple - variables of type int, real, bool and character | + | |
- | * complex - array and struct | + | === int === |
- | * strings | + | 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 | ||
+ | |||
+ | {{ :alf:teme:strings.png?nolink&300 |}} | ||
+ | |||
+ | |||
+ | ===== 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''. | ||
- | We will discuss them in detail. | ||
^ Variable Type ^ Variable Location ^ Allocation Type ^ | ^ Variable Type ^ Variable Location ^ Allocation Type ^ | ||
- | | simple | script | local | | + | | simple | script | global | |
| complex | script | data | | | complex | script | data | | ||
| string | script | data | | | string | script | data | | ||
Line 69: | Line 95: | ||
==== global allocation ==== | ==== global allocation ==== | ||
- | Global allocated variables are simple variables from the main script. These will be allocated as `(global $title type)` statements. | + | Global allocated variables are simple variables from the main script. These will be allocated as ''(global $title type)'' statements. |
+ | Example: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/simple.alf|simple.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/simple.alf.wat|simple.wat]] | ||
+ | Access to these variables is done using the ''global.set'' and ''global.get'' instructions. | ||
- | Parameters are allocated using Web Assembly parameters. | ||
- | {{section>:alf:res:webassembly&nofooter}} | + | ==== 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: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/simple.alf|simple.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/simple.alf.wat|simple.wat]] | ||
- | ==== Variable Size ==== | + | 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. |
- | ^ Type ^ Size ^ Representation | | + | <note>You can add a ''(global $title i32 (i32.const address_of_variable)'' variable to be easier to read the code.</note> |
- | | int | 4 | 32 bit signed integer | | + | |
- | | real | 4 | 32 bit float | | + | |
- | | symbol | 1 | 8 bit unsigned integer | | + | |
- | | string | 256 | 255 bytes array of symbol | | + | |
- | | logic | 1 | 8 bit unsigned integer | | + | |
- | === Logic === | ||
- | The logic value is represented: | ||
- | * true - 8 bit unsigned 1 (00000001) | ||
- | * false - 8 bit unsigned 0 (00000000) | ||
- | === String === | + | ==== param allocation ==== |
- | The representation used for strings is Pascal: | + | Parameter allocation is used for simple type function parameters. These will be allocated as ''(param $title type)'' statements in the function definition. |
- | * 1 byte for length | + | Example: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/6_function/definition_variables.alf|definition_variables.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/6_function/definition_variables.alf.wat|definition_variables.wat]] |
- | * 255 bytes for symbols | + | |
+ | 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: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/6_function/definition_variables.alf|definition_variables.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/6_function/definition_variables.alf.wat|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: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/6_function/definition_variables_stack.alf|definition_variables_stack.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/6_function/definition_variables_stack.alf.wat|definition_variables_stack.alf.wat]] | ||
+ | |||
+ | <note>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.</note> | ||
+ | |||
+ | ==== 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: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/4_branch/local_variable.alf|local_variable.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/4_branch/local_variable.alf.wat|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: [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/2_simple/real_number.alf|2_simple/real_number.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/2_simple/real_number.alf.json|real_number.alf.json]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/2_simple/real_number.alf.wat|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 [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/string.alf|string.alf]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/string.alf.opt.json|string.alf.opt.json]] [[https://github.com/UPB-FILS/alf/blob/master/Devoir/assembly/verify/assembly/1_variable_definition/string.alf.wat|string.alf.wat]] | ||
===== Steps for writing the homework ===== | ===== Steps for writing the homework ===== | ||
- | * Compute the allocation for each global variable (variables sizes are described below) | + | * Compute the allocation for each variable |
- | * Allocate each local variable to a web assembly local variable | + | |
* Transform each AST element into the corresponding Web Assembly instruction(s) | * Transform each AST element into the corresponding Web Assembly instruction(s) | ||
* Transform each function (and its statements) into the corresponding Web Assembly instructions | * Transform each function (and its statements) into the corresponding Web Assembly instructions | ||
- | ===== ASM in AST ===== | + | ===== Bonus Optimization ===== |
- | 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: | + | Implement the homework so that is does the following optimizations: |
+ | * constant folding | ||
+ | * delete unused variables and functions | ||
- | * readint | + | For the bonus, there are two extra parameters in the command line |
- | * readfloat | + | |
- | * readchar | + | <code bash> |
- | * writeint | + | node main.js fisier.alf.json fisier.alf.opt.json fisier.wat [fold_constants] [unused] |
- | * writefloat | + | </code> |
- | * writechar | + | |
- | ===== Bonus ===== | + | <note>[] means that it is optional</note> |
- | Implement the homework so that is may use strings. | + | |
===== Testing ===== | ===== Testing ===== |