DEV Community

Natalia
Natalia

Posted on

Compiler with WebAssembly Target

My Final Project

One of the final projects that I did during my 4-year Electrical/Computer Engineering degree was to write a compiler with a WebAssembly target. The compiler is written in C, and was implemented using compiler construction tools (Flex, Bison). The scanner, parser, semantic checker and code generation were all developed as part of this compiler. The source language is J-- (subset of Java), and the compiler targets WebAssembly by outputting WebAssembly Text Format (WAT).

Link to Code

GitHub logo NataliaPavlovic / CPSC411_Compiler

A J-- (subset of Java) compiler written in C, that compiles J-- into WebAssembly Text Format (WAT).

J-- Compiler

This project is a J-- (subset of Java) compiler written in C, that compiles J-- into WebAssembly Text Format (WAT).

See the full J-- specification here: https://pages.cpsc.ucalgary.ca/~aycock/411/spec.html

There are 4 parts to the compiler:

  1. Scanner (Flex)
  2. Parser (Bison)
  3. Semantic Checker
  4. Code Generation

When the compiler is run, it generates WAT files from J-- files. WASM files are then generated from the WAT files, and then the WASM files are run.

All the provided J-- test cases can found here: J-- Files

All the generated WAT files can be found here: WAT Files

Test Cases:

gen.t1

This test case makes use of the prints run-time function, and outputs a string.

Provided J-- File: gen.t1

Generated WAT File: gen_t1.wat

gen_t1.wasm output:
Hello, world
gen.t5

Add Description.

Provided J-- File: gen.t5

Generated WAT File: gen_t5.wat

gen_t5.wasm output:
42
42
42

gen.t10

Add Description.

Provided J-- File: gen.t10

Generated WAT File: gen_t10.wat




The compiler was implemented over the course of 4 months. The biggest challenge in this project was integrating all the parts of the compiler together in order to be able to do code generation. I learned the importance of doing thorough testing of each project milestone, and I also learned how to write useful test cases.

Overall, the experience of implementing a compiler that was able to generate correct WAT code was very rewarding. I really enjoyed learning about various parts of a compiler and how those parts work.

Discussion (0)