Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM
This lab gives each student an Ubuntu machine of their own with a real C compiler and the tools around it, and a RISC-V toolchain with an emulator. Students watch a program go through each stage of a build, build a small calculator from a lexer and a parser, and run a RISC-V program although the machine is not RISC-V. Nothing is installed on the student’s computer.
What students learn
The four stages of a build: preprocessing with gcc -E, compiling to assembly with gcc -S, assembling to an object file with gcc -c, and linking. The difference between an error and a warning, from gcc -Wall on a program with a variable used out of its scope. Tokens and grammar, from a calculator built with flex and bison that gets the order of operations right.
Registers, instructions and system calls, from five lines of RISC-V assembly that add two numbers and leave the answer as the exit status. Instructions as numbers, read with objdump. Why a cache matters, from a program that sums a table row by row and column by column and times each. And the program counter, stepping one instruction at a time in gdb.
What is on the machine
gcc, make, binutils, gdb, flex, bison, python3 and file, all from Ubuntu’s archive. A RISC-V cross compiler and assembler, riscv64-linux-gnu-gcc and -as, and qemu-user, whose qemu-riscv64 runs a RISC-V program on the VM’s own processor.
In the compiler folder: hello.c, a small program with a macro and a function; scope.c, with one variable never used and one used where it does not exist; and calc.l, calc.y and a Makefile for the calculator. In the arch folder: add.s, the RISC-V program, and loop.c, the cache timing.
Run it in your browser
VM labs are set as a lab task in a module, and the sample is on the lab’s page. The machine is small: one CPU, 1 GB of memory and 10 GB of disk, which is plenty for programs of a few lines.
- Add the lab. In a module you teach, add a task and choose the Ubuntu VM lab card. The page shows a short description of the machine instead of a compose file.
- Start from the sample. The page offers ready-made labs to start from. Choose Compilers and computer architecture. Using a sample replaces the text, and Undo brings yours back.
- Publish and start your own copy. Publish, then Start. The lab’s setup script installs the compilers and tools from Ubuntu’s archive and leaves the programs in the home folder the first time the VM starts, in a few minutes.
- Open the terminal. Open its terminal and read README.md in the home folder: it lists the programs in the compiler and arch folders. Then open Compilers Basics.
A basic lesson on this lab
There are two short lessons, each worked in this lab: Compilers Basics and Computer Architecture Basics. Each stands alone, starts in its own folder, and has a hook, an I can outcome, key words, a starter, a check after each idea and an exit check.
A Run button types its command into the VM’s terminal and shows what it printed. gdb waits for what is typed next, so it is marked Type this, and the student types it in the terminal.
How it is used in class
The lab marks nothing, so it suits a first practical on compilers or on architecture, a demonstration on the teacher’s screen, or a session before an assignment written in Python.
- Hand it out. Assign the lab and the lessons like any task, with a due date if you want one. Each student gets a VM of their own, kept between sessions.
- Watch them work. The terminal beside a lesson is the student’s own, and you can watch it or take it over from the class list while they type.
- Mark it yourself if you want a mark. The lab has no check script. If you want a hand-in, ask for something small, such as the two times loop printed and a sentence on why they differ.
The definition on GitHub
The file is the whole lab definition, the same text the platform runs: size, setup script and README. Copy it and add a program of your own to make a lab for your own class.
Questions people ask
- Does the lab need a RISC-V processor?
- No. The VM runs on an ordinary processor, and qemu-riscv64 runs a RISC-V program on it one instruction at a time. The program, the assembler and the linker are real RISC-V tools, and file names the program as RISC-V.
- Why does the assembly from gcc -S look different on another machine?
- gcc -S writes assembly for the processor it runs on, which may be x86-64 or Arm. The lesson says so and asks students to find the call to the function, which is call on one and bl on the other.
- Can students harm anything?
- Only their own copy. If a student breaks the machine they can stop the lab and start it again.
- Is the compilers and computer architecture lab marked automatically?
- No. It has no check script, because the lessons are the activity. Set a small hand-in of your own, such as a short Python task with hidden tests that simulates what the lab showed.