UP | HOME

Homework: Toy-Compiler Follow-Along

Table of Contents

Overview

Hand-code the toy compiler demonstrated in class yourself. This exercise is to practice using make, gcc, and git to develop, build, and run software. Since the goal of this is not to design and implement the compilation algorithms for yourself, it is classified as homework (Links to an external site.) rather than a project.

You may use the code developed live in class. You must, however, enter the code yourself by hand, without any digital copying from other student's versions of the code. This will help ensure you have comfort with the style of C and programming tools used for the real programming project that you will design and implementation individually.

This assignment is classified as homework, so it follows the late policy of half-off for a late assignment, rather than the project late policy as described on the [[../index.html][syllabus].

To submit the project, accept the GitHub assignment from the link in webcourses, then submit the GitHub URL as the answer to the webcourses assignment. The URL should look something like this:

https://github.com/cop3402fall20/toy-compiler-USERID

Where USERID is your GitHub username.

Please do not create or copy the repository to your personal GitHub page, since this violates UCF's Golden Rule policies.

Grading scheme

  • 2pt for any submission
  • 5pt for any submission that builds
  • 8pt for any submission that builds and runs with some minor mistakes in the output
  • 10pt for any submission that builds and runs correctly

Resources

Please find the code (some as screenshots), assembly templates, and examples below.

Checking your work

It's important to validate that your compile both builds and runs as expected. From inside of your source code repostiroy directory, run the following:

make  # build the toy compiler
cat template_start.s <(cat example.toy | ./toy) template_end.s > example.s  # run the toy compiler
diff -w example.s.expected example.s  # compare the output
gcc -o example example.s  # assembly the output
./example  # execute the output program

Submitting your work

Submit your project using git to add source, commit changes to them, and push your local repository to the GitHub repo as described in class. Put the Makefile and all source files in the root of the source code repository; there is no need to create a subfolder.

It is important to validate that your source code repository has the necessary files to build and run your project. You can check this by going to a new directory (somewhere besides your current toy compiler source directory) and recloning your repository, i.e.,

# in a directory other than your toy compiler directory
git clone https://github.com/cop3402fall20/toy-compiler-USERID toy-compiler-test-repo
cd toy-compiler-test-repo

(Be sure to substitute USERID with your actual GitHub user name.)

Then try to build and run your project as described above.

Complete example

example.toy

1+5;
7-4;
8*3;
9/4;

Output of cat example.toy | ./toy

movl $1, %eax
movl $5, %ebx
addl %ebx, %eax
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

movl $7, %eax
movl $4, %ebx
subl %ebx, %eax
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

movl $8, %eax
movl $3, %ebx
imull %ebx, %eax
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

movl $9, %eax
movl $4, %ebx
cdq
idiv %ebx
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

Output of cat template_start.s <(cat example.toy | ./toy) template_end.s > example.s

  .file	"toy"
  .text
  .section	.rodata
.LC0:
  .string	"%d\n"
  .text
  .globl	main
  .type	main, @function


main:
.LFB0:
  .cfi_startproc
  endbr64
  pushq	%rbp
  .cfi_def_cfa_offset 16
  .cfi_offset 6, -16
  movq	%rsp, %rbp
  .cfi_def_cfa_register 6

  movl $1, %eax
  movl $5, %ebx
  addl %ebx, %eax
  movl  %eax, %esi
  leaq	.LC0(%rip), %rdi
  movl	$0, %eax
  call	printf@PLT

  movl $7, %eax
  movl $4, %ebx
  subl %ebx, %eax
  movl  %eax, %esi
  leaq	.LC0(%rip), %rdi
  movl	$0, %eax
  call	printf@PLT

  movl $8, %eax
  movl $3, %ebx
  imull %ebx, %eax
  movl  %eax, %esi
  leaq	.LC0(%rip), %rdi
  movl	$0, %eax
  call	printf@PLT

  movl $9, %eax
  movl $4, %ebx
  cdq
  idiv %ebx
  movl  %eax, %esi
  leaq	.LC0(%rip), %rdi
  movl	$0, %eax
  call	printf@PLT
  movl	$0, %eax
  popq	%rbp
  .cfi_def_cfa 7, 8
  ret
  .cfi_endproc


.LFE0:
  .size	main, .-main
  .ident	"GCC: (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008"
  .section	.note.GNU-stack,"",@progbits
  .section	.note.gnu.property,"a"
  .align 8
  .long	 1f - 0f
  .long	 4f - 1f
  .long	 5
0:
  .string	 "GNU"
1:
  .align 8
  .long	 0xc0000002
  .long	 3f - 2f
2:
  .long	 0x3
3:
  .align 8
4:

Author: Paul Gazzillo

Created: 2020-11-04 Wed 20:19