Compilation Methods SS 2013 - File risc.burg
%{
/* example application of BURG, demonstrates tree cover with patterns */
/* required headers of standard library */
#include <stdio.h>
#include <stdlib.h>
/* datatype of the tree nodes to cover
(state_label is used internally by BURG) */
typedef struct node *NODEPTR_TYPE;
struct node {
int op, state_label;
NODEPTR_TYPE left, right;
};
/* access macros for components of tree nodes as required by BURG */
#define OP_LABEL(p) ((p)->op)
#define STATE_LABEL(p) ((p)->state_label)
#define LEFT_CHILD(p) ((p)->left)
#define RIGHT_CHILD(p) ((p)->right)
#define PANIC printf
%}
/* declare start symbol for root of cover and valid operators at tree nodes */
%start Stmt
%term iconst=1 var=2 iadd=3 deref=4 assign=5
%%
IConst: iconst = 1 (0);
/* use constant as constant, emits no code */
IReg: iconst = 2 (1);
/* load constant into register: move #const, Rn */
IReg: var = 3 (2);
/* load address of variable into register: addr #var, Rn */
IReg: iadd(IReg, IReg) = 4 (2);
/* add contents of two registers: add Ra, Rb, Rsum */
IReg: iadd(IReg, IConst) = 5 (2);
/* add constant to contents of register: add Ra, #const, Rsum */
IReg: deref(IReg) = 6 (3);
/* load value from memory: load Raddr, Rvalue */
Stmt: assign(IReg, IReg) = 7 (2);
/*store value into memory: store Raddr, Rvalue */
/* add further tree patterns HERE:
value descriptor: tree fragment = id number of pattern (cost);
Tree patterns must be numbered continously without gaps: 8, 9, ...
Cost value are taken from a specification of the target processor.
For example, cost values match the expected execution time of the
emitted code in clock cycles. */
%%
#include "utils.c" /* auxiliary functions, not related to patterns */
int main(void)
{
/* construct the tree to be covered:
intermediate language code tree for the C sourcecode fragment
{
int v[8];
struct { int x, y; } p;
p.y = v[7] + 9;
}
assign
/ \
------ ------
/ \
iadd iadd
/ \ / \
var p iconst 4 deref iconst 9
|
iadd
/ \
var v iconst 28
*/
NODEPTR_TYPE tree;
tree = assign(iadd(var, iconst), iadd(deref(iadd(var, iconst)), iconst));
printf("Tree:\n");
printtree(tree); /* output original tree unmodified */
printf("\n\nCover:\n");
burm_label(tree); /* preprocess tree (annotate with relative costs) */
/* output tree cover with patterns used */
printcover(tree, 1, 0);
/* output total cost of all patterns used */
printf("\nCover cost == %d\n\n", treecost(tree, 1, 0));
return 0;
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 13.06.2017


