Description
Part 1: Reading Compilers – Principles, Techniques, & Tools, 2nd Edition, Sections 8.5 to 8.9, 9.1 to 9.2. Homework Exercises – laboratory assignment and answer the questions at the end of the assignment.
Introduction In the last assignment you optimize the code generation by improving QUAD generation and/or optimized the QUAD linked list. In this assignment you modify the calculator program to generate PIC 16F1827 assembler code. You support QUAD to TUPLE generation for expressions and IFELSE statements and add support for indirect array quads. You test your code generator with the previous test files. You update the compiler and implement the
For extra credit (10) you update the code generator or write your own version. For additional extra credit (5) you download MPLAB IDE software, compile and simulate your output code. Part 2: Laboratory From a console window, make a directory on your computer in your EECS337 directory under your Case ID and call it hw12. mkdir ~/EECS337/caseid/hw12/ ; where caseid is YOUR Case ID, enter all in lower case
Change directory to the hw12 directory. cd ~/EECS337/caseid/hw12/
Download a copy of: hw12_caseid.tar file to the hw12 directory from https://blackboard.case.edu/ in the EECS337 homework assignment area. To untar the tar file type the command: tar xvf hw12_caseid.tar
The following files will be created in the current working directory. codegen.c codegen2.c Makefile hw12_test.sh
Copy the following files from the assignment 10 directory to this directory with the commands: cp ../hw11/lex.l . cp ../hw11/main.c . cp ../hw11/quad.c . cp ../hw11/symbol_table.c . cp ../hw11/yacc.y . cp ../hw11/yystype.h .
Optional: If you did not complete the last assignment you can download the hw11solutions.tar file from the assignment directory and use that as your starting code. Use the command “tar xvf hw11solutions.tar”. The hw11/ directory will be created with the files. In that case edit all the files and change “caseid” to your Case ID.
You are now ready to solve the laboratory assignment.
Part 3: Laboratory Assignment Edit the yystype.h file and add to the top of the file the PIC instructions and the TUPLE structure definition. /* * define pic instructions */ #define I_LABEL 0 #define I_MOV 1 #define I_ADD 2 #define I_AND 3 #define I_IOR 4 #define I_SUB 5 #define I_XOR 6 #define I_COMF 7 #define I_DECF 8 #define I_DECFSZ 9 #define I_INCF 10 #define I_INCFSZ 11 #define I_RLF 12 #define I_RRF 13 #define I_SWAPF 14 #define I_BCF 15 #define I_BSF 16 #define I_BTFSC 17 #define I_BTFSS 18 #define I_CALL 19 #define I_GOTO 20 #define I_TRIS 21 #define I_CLR 22 #define I_RETLW 23 #define I_CLRWDT 24 #define I_NOP 25 #define I_OPTION 26 #define I_RETFIE 27 #define I_RETURN 28 #define I_SLEEP 29 /* * define a tuple structure * supports: CONSTANT STRING_LITERAL IDENTIFIER types */ #define TUPLE struct tuple TUPLE { TUPLE *next; int token; unsigned char value; int address; #define MASK_VALUE 0x0001 #define MASK_ADDRESS 0x0002 #define MASK_LABEL 0x0004 #define MASK_W_REG 0x0008 #define MASK_F_REG 0x0010 #define MASK_INSTR 0x0020 int mask; char *buffer; int length; int level; };
Add to the bottom of the symbol table data structure the field to hold the PIC physical address. int address;
Add to the bottom of the data structure the field to hold the PIC physical address and define the values for the maximum and minimum PIC 16F1827 address ranges. #define TOP_MEMORY 0x0020 #define BOTTOM_MEMORY 0x007f int address; Before the bottom of the file (#endif) add the external declarations below for the code generator. /* * external variables and functions from codegen.c */ extern void code_generator_pic_prefix( void); extern void code_generator_pic_postfix( void); extern void code_generator_operand_postfix( TUPLE *tuple); extern void code_generator_operand( TUPLE *tuple); extern void code_generator_instr_postfix( TUPLE *tuple); extern void code_generator_instr( TUPLE *tuple); extern void code_generator_pic16f1827( TUPLE *tuple_list); extern void code_generator_instr_test( void); /* * external variables and functions from codegen2.c */ extern void code_generator_pic_address( void); extern TUPLE *generate_quad_instruction( int instruction, int type, int index); extern TUPLE *generate_quad_operand( int type, int index); extern TUPLE *generate_quad_destination( int type, int index); extern TUPLE *generate_quad( QUAD *quad); extern TUPLE *generate_quad_2_tuple( QUAD *quad_list); Save the yystype.h file
Edit the main.c and add to the ‘+’ flags section the code below. This flag prints out examples of the PIC code generator instructions. else if( !strncmp( command, “+test”, strlen( command))) { code_generator_instr_test(); exit( 0); } Save the main.c file.
Edit the symbol_table.c file and add the code to allocate the PIC physical address. In the new_symbol function add one line of code to each CHAR, SHORT, INT and LONG specifiers. /* * allocate the PIC physical address */ data.st[ identifier].address = get_address( 1); //char data.st[ identifier].address = get_address( 2); //short data.st[ identifier].address = get_address( 4); // int data.st[ identifier].address = get_address( 8); //long Save the symbol_table.c file
Edit the lex.l file and delete the line that has [\n] and add the new line character to the white space regular expression [ \n\t]. From now on we want to generate the whole file or until we type $ on the command line. Save the lex.l file. [ \t\n] { /* ignore white characters */ } Edit the yacc.y file and at the top change the include files, the global TUPLE variable and the new %start file for the start symbol.
#include

