Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
Yihui He authored and Yihui He committed Jul 20, 2016
1 parent 4674e30 commit 350259d
Show file tree
Hide file tree
Showing 114 changed files with 8,732 additions and 0 deletions.
Binary file added Modern Compiler Implementation in C.pdf
Binary file not shown.
Empty file added chap1/main.c
Empty file.
17 changes: 17 additions & 0 deletions chap1/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
a.out: main.o prog1.o slp.o util.o
cc -g main.o prog1.o slp.o util.o

main.o: main.c slp.h util.h
cc -g -c main.c

prog1.o: prog1.c slp.h util.h
cc -g -c prog1.c

slp.o: slp.c slp.h util.h
cc -g -c slp.c

util.o: util.c util.h
cc -g -c util.c

clean:
rm -f a.out util.o prog1.o slp.o main.o
15 changes: 15 additions & 0 deletions chap1/prog1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "util.h"
#include "slp.h"

A_stm prog(void) {

return
A_CompoundStm(A_AssignStm("a",
A_OpExp(A_NumExp(5), A_plus, A_NumExp(3))),
A_CompoundStm(A_AssignStm("b",
A_EseqExp(A_PrintStm(A_PairExpList(A_IdExp("a"),
A_LastExpList(A_OpExp(A_IdExp("a"), A_minus,
A_NumExp(1))))),
A_OpExp(A_NumExp(10), A_times, A_IdExp("a")))),
A_PrintStm(A_LastExpList(A_IdExp("b")))));
}
1 change: 1 addition & 0 deletions chap1/prog1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A_stm prog(void);
60 changes: 60 additions & 0 deletions chap1/slp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "util.h"
#include "slp.h"

A_stm A_CompoundStm(A_stm stm1, A_stm stm2) {
A_stm s = checked_malloc(sizeof *s);
s->kind=A_compoundStm; s->u.compound.stm1=stm1; s->u.compound.stm2=stm2;
return s;
}


A_stm A_AssignStm(string id, A_exp exp) {
A_stm s = checked_malloc(sizeof *s);
s->kind=A_assignStm; s->u.assign.id=id; s->u.assign.exp=exp;
return s;
}

A_stm A_PrintStm(A_expList exps) {
A_stm s = checked_malloc(sizeof *s);
s->kind=A_printStm; s->u.print.exps=exps;
return s;
}

A_exp A_IdExp(string id) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_idExp; e->u.id=id;
return e;
}

A_exp A_NumExp(int num) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_numExp; e->u.num=num;
return e;
}

A_exp A_OpExp(A_exp left, A_binop oper, A_exp right) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_opExp; e->u.op.left=left; e->u.op.oper=oper; e->u.op.right=right;
return e;
}

A_exp A_EseqExp(A_stm stm, A_exp exp) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_eseqExp; e->u.eseq.stm=stm; e->u.eseq.exp=exp;
return e;
}

A_expList A_PairExpList(A_exp head, A_expList tail) {
A_expList e = checked_malloc(sizeof *e);
e->kind=A_pairExpList; e->u.pair.head=head; e->u.pair.tail=tail;
return e;
}

A_expList A_LastExpList(A_exp last) {
A_expList e = checked_malloc(sizeof *e);
e->kind=A_lastExpList; e->u.last=last;
return e;
}



36 changes: 36 additions & 0 deletions chap1/slp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
typedef struct A_stm_ *A_stm;
typedef struct A_exp_ *A_exp;
typedef struct A_expList_ *A_expList;
typedef enum {A_plus,A_minus,A_times,A_div} A_binop;

struct A_stm_ {enum {A_compoundStm, A_assignStm, A_printStm} kind;
union {struct {A_stm stm1, stm2;} compound;
struct {string id; A_exp exp;} assign;
struct {A_expList exps;} print;
} u;
};
A_stm A_CompoundStm(A_stm stm1, A_stm stm2);
A_stm A_AssignStm(string id, A_exp exp);
A_stm A_PrintStm(A_expList exps);

struct A_exp_ {enum {A_idExp, A_numExp, A_opExp, A_eseqExp} kind;
union {string id;
int num;
struct {A_exp left; A_binop oper; A_exp right;} op;
struct {A_stm stm; A_exp exp;} eseq;
} u;
};
A_exp A_IdExp(string id);
A_exp A_NumExp(int num);
A_exp A_OpExp(A_exp left, A_binop oper, A_exp right);
A_exp A_EseqExp(A_stm stm, A_exp exp);

struct A_expList_ {enum {A_pairExpList, A_lastExpList} kind;
union {struct {A_exp head; A_expList tail;} pair;
A_exp last;
} u;
};

A_expList A_PairExpList(A_exp head, A_expList tail);
A_expList A_LastExpList(A_exp last);

29 changes: 29 additions & 0 deletions chap1/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* util.c - commonly used utility functions.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
void *checked_malloc(int len)
{void *p = malloc(len);
if (!p) {
fprintf(stderr,"\nRan out of memory!\n");
exit(1);
}
return p;
}

string String(char *s)
{string p = checked_malloc(strlen(s)+1);
strcpy(p,s);
return p;
}

U_boolList U_BoolList(bool head, U_boolList tail)
{ U_boolList list = checked_malloc(sizeof(*list));
list->head = head;
list->tail = tail;
return list;
}
15 changes: 15 additions & 0 deletions chap1/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <assert.h>

typedef char *string;
typedef char bool;

#define TRUE 1
#define FALSE 0

void *checked_malloc(int);
string String(char *);

typedef struct U_boolList_ *U_boolList;
struct U_boolList_ {bool head; U_boolList tail;};
U_boolList U_BoolList(bool head, U_boolList tail);

8 changes: 8 additions & 0 deletions chap10/flowgraph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* flowgraph.h - Function prototypes to represent control flow graphs.
*/

Temp_tempList FG_def(G_node n);
Temp_tempList FG_use(G_node n);
bool FG_isMove(G_node n);
G_graph FG_AssemFlowGraph(AS_instrList il);
170 changes: 170 additions & 0 deletions chap10/graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* graph.c - Functions to manipulate and create control flow and
* interference graphs.
*/

#include <stdio.h>
#include "util.h"
#include "symbol.h"
#include "temp.h"
#include "tree.h"
#include "absyn.h"
#include "assem.h"
#include "frame.h"
#include "graph.h"
#include "errormsg.h"
#include "table.h"

struct G_graph_ {int nodecount;
G_nodeList mynodes, mylast;
};

struct G_node_ {
G_graph mygraph;
int mykey;
G_nodeList succs;
G_nodeList preds;
void *info;
};

G_graph G_Graph(void)
{G_graph g = (G_graph) checked_malloc(sizeof *g);
g->nodecount = 0;
g->mynodes = NULL;
g->mylast = NULL;
return g;
}

G_nodeList G_NodeList(G_node head, G_nodeList tail)
{G_nodeList n = (G_nodeList) checked_malloc(sizeof *n);
n->head=head;
n->tail=tail;
return n;
}

/* generic creation of G_node */
G_node G_Node(G_graph g, void *info)
{G_node n = (G_node)checked_malloc(sizeof *n);
G_nodeList p = G_NodeList(n, NULL);
assert(g);
n->mygraph=g;
n->mykey=g->nodecount++;

if (g->mylast==NULL)
g->mynodes=g->mylast=p;
else g->mylast = g->mylast->tail = p;

n->succs=NULL;
n->preds=NULL;
n->info=info;
return n;
}

G_nodeList G_nodes(G_graph g)
{
assert(g);
return g->mynodes;
}

/* return true if a is in l list */
bool G_inNodeList(G_node a, G_nodeList l) {
G_nodeList p;
for(p=l; p!=NULL; p=p->tail)
if (p->head==a) return TRUE;
return FALSE;
}

void G_addEdge(G_node from, G_node to) {
assert(from); assert(to);
assert(from->mygraph == to->mygraph);
if (G_goesTo(from, to)) return;
to->preds=G_NodeList(from, to->preds);
from->succs=G_NodeList(to, from->succs);
}

static G_nodeList delete(G_node a, G_nodeList l) {
assert(a && l);
if (a==l->head) return l->tail;
else return G_NodeList(l->head, delete(a, l->tail));
}

void G_rmEdge(G_node from, G_node to) {
assert(from && to);
to->preds=delete(from,to->preds);
from->succs=delete(to,from->succs);
}

/**
* Print a human-readable dump for debugging.
*/
void G_show(FILE *out, G_nodeList p, void showInfo(void *)) {
for (; p!=NULL; p=p->tail) {
G_node n = p->head;
G_nodeList q;
assert(n);
if (showInfo)
showInfo(n->info);
fprintf(out, " (%d): ", n->mykey);
for(q=G_succ(n); q!=NULL; q=q->tail)
fprintf(out, "%d ", q->head->mykey);
fprintf(out, "\n");
}
}

G_nodeList G_succ(G_node n) { assert(n); return n->succs; }

G_nodeList G_pred(G_node n) { assert(n); return n->preds; }

bool G_goesTo(G_node from, G_node n) {
return G_inNodeList(n, G_succ(from));
}

/* return length of predecessor list for node n */
static int inDegree(G_node n)
{ int deg = 0;
G_nodeList p;
for(p=G_pred(n); p!=NULL; p=p->tail) deg++;
return deg;
}

/* return length of successor list for node n */
static int outDegree(G_node n)
{ int deg = 0;
G_nodeList p;
for(p=G_succ(n); p!=NULL; p=p->tail) deg++;
return deg;
}

int G_degree(G_node n) {return inDegree(n)+outDegree(n);}

/* put list b at the back of list a and return the concatenated list */
static G_nodeList cat(G_nodeList a, G_nodeList b) {
if (a==NULL) return b;
else return G_NodeList(a->head, cat(a->tail, b));
}

/* create the adjacency list for node n by combining the successor and
* predecessor lists of node n */
G_nodeList G_adj(G_node n) {return cat(G_succ(n), G_pred(n));}

void *G_nodeInfo(G_node n) {return n->info;}



/* G_node table functions */

G_table G_empty(void) {
return TAB_empty();
}

void G_enter(G_table t, G_node node, void *value)
{
TAB_enter(t, node, value);
}

void *G_look(G_table t, G_node node)
{
return TAB_look(t, node);
}


Loading

0 comments on commit 350259d

Please sign in to comment.