Creating
PIC compiler in

Tallinn 2016
Hannes Kinks
Agenda
- Tutorial on creating a simple PIC compiler
- Task - compiler for PIC counter program
- Consultation about homeworks
PIC compiler



Compilation
Flashing PIC with binary instructions
Compilation stages
Compilation of C code
- Preprocessing
- Compilation
- Assembly
- Linking
#include <stdio.h>
#define MESSAGE "Hello world"
int main(void) {
printf(MESSAGE);
return 0;
} Preprocessing
- Process the lines starting with #
- Join multi line statements (ending with \)
- To see the results of preprocessing
-
cc -E hello_world.c
-
Compilation
- Preprocessed code is translated into assembly instructions
- Specific to the target processor architecture
- Some compilators translate directly to machine code
- To see the result of this stage:
-
cc -S hello_world.c
-
Assembly
- Assembly instructions are translated to machine code (object code)
- We get binary instructions that can be executed by the processor
- To see the result of this stage:
-
cc -c hello_world.c
-
hexdump hello_world.o
-
Linking
- Inserts the missing object code from included libraries
- To produce an executable program, the existing pieces have to be rearranged and the missing ones filled in
- The result of this stage is the final executable program.
- To see the result of this stage:
-
cc hello_world.c
-
Task (4p)
LED = 0x00;
i = 0;
WHILE(TRUE) {
LED = i;
DELAY();
i = i + 1;
IF(i==16) {
i = 0;
}
}
- Come up with a programming language
- no requirements on syntax
- Write a compiler for PIC
- Should be able to count on LEDs
- Example code:
- You will need to implement:
- assignment
- addition
- if statement
- comparison
0 1 2 3 ...
Getting started
-
IDEA skeleton project for practice task
-
git clone https://bitbucket.org/hkinks/iag0450-antlr
- data - input, output files
- lib - libraries necessary to include in IDEA
- doc - documentation, IDEA plugin
-

- Start idea, with
- cad
- idea&
- Go through the initial setup, until you reach the welcome screen
- if root password is asked, just skip by closing it
- Add ANTLR4 plugin
- Configure -> Plugins


- Add ANTLR4 plugin
- Click on "Install plugin from disk ..."
- Find and select
- iag0450-antlr/doc/plugin.zip

- Import the cloned project
- click on "Import Project"
- select iag0450-antlr directory
- Click "Next" until JDK installation
is required - JDK location is:
- /usr/lib64/jvm/java-1.7.0-openjdk-1.7.0
- Finish the import
- Don't forget to generate ANTLR sources
- Right click on *.g4 grammar file
- Generate ANTLR recognizer
- Add the folder as source
- Right click on gen folder
- Mark Directory as
- Generated Sources Root
- Try if everything works
- Right click on Main
- Run Main.main()
- Start developing the features
Blinking tutorial
Tutorial for blinking lights program compilation is included in the README.md file, down below.
Visitor pattern


opExpr: left=expr op=OP right:expr ;
PIC compiler in ANTLR4
By Hannes Kinks
PIC compiler in ANTLR4
- 1,022