#include <stdio.h>
#include "error.h"
#include "commonInter.h"
#include "mystdlib.h"
#include "nat.h"
#include "poly.h"
#include "file.h"
#include "control.h"
#include "miniPentium.h"
str toStringOperand (operand o);
void toStringInstr (linkedList l, instruction i);
operand miniPNewOperandNum (int i)
{
operand o = checkedMalloc (sizeof (*o));
o->kind = PNUM;
(o->u).i = i;
return o;
}
operand miniPNewOperandId (str id)
{
operand o = checkedMalloc (sizeof (*o));
o->kind = PID;
(o->u).id = id;
return o;
}
instruction miniPNewInstrMovl (operand src, str dst)
{
instruction i = checkedMalloc (sizeof (*i));
i->kind = MOVL;
i->src = src;
i->dst = dst;
return i;
}
instruction miniPNewInstrAddl (operand src, str dst)
{
instruction i = checkedMalloc (sizeof (*i));
i->kind = ADDL;
i->src = src;
i->dst = dst;
return i;
}
instruction miniPNewInstrSubl (operand src, str dst)
{
instruction i = checkedMalloc (sizeof (*i));
i->kind = SUBL;
i->src = src;
i->dst = dst;
return i;
}
instruction miniPNewInstrMull (operand src, str dst)
{
instruction i = checkedMalloc (sizeof (*i));
i->kind = MULL;
i->src = src;
i->dst = dst;
return i;
}
instruction miniPNewInstrPrint (operand src)
{
instruction i = checkedMalloc (sizeof (*i));
i->kind = PPRINT;
i->src = src;
return i;
}
instruction miniPNewInstrComment (str c)
{
instruction i = checkedMalloc (sizeof (*i));
i->kind = COMMENT;
i->dst = c;
return i;
}
pentiumProg miniPNewProg (linkedList instructions)
{
pentiumProg p = checkedMalloc (sizeof (*p));
p->instructions = instructions;
return p;
}
void miniPPrettyPrintOperand (operand o)
{
switch (o->kind)
{
case PID:
{
strOutput ((o->u).id);
break;
}
case PNUM:
{
printf ("%d", (o->u).i);
break;
}
default: break;
}
return;
}
void miniPPrettyPrintInstruction (instruction s)
{
switch (s->kind)
{
case MOVL:
{
printf ("movl\t");
miniPPrettyPrintOperand (s->src);
printf (", ");
strOutput (s->dst);
printf ("\n");
break;
}
case ADDL:
{
printf ("addl\t");
miniPPrettyPrintOperand (s->src);
printf (", ");
strOutput (s->dst);
printf ("\n");
break;
}
case SUBL:
{
printf ("subl\t");
miniPPrettyPrintOperand (s->src);
printf (", ");
strOutput (s->dst);
printf ("\n");
break;
}
case MULL:
{
printf ("mull\t");
miniPPrettyPrintOperand (s->src);
printf (", ");
strOutput (s->dst);
printf ("\n");
break;
}
case PPRINT:
{
printf ("print\t");
miniPPrettyPrintOperand (s->src);
printf ("\n");
break;
}
case COMMENT:
{
str tmp = strConcat (newStr ("// "), s->dst);
strOutput (tmp);
printf ("\n");
break;
}
default:
{
error ("no this case\n");
break;
}
}
}
typedef void (*f) (poly);
void miniPPrettyPrintProg (pentiumProg p)
{
linkedList l = p->instructions;
linkedListForeach (l, (f)miniPPrettyPrintInstruction);
return;
}
set setOperand (operand o)
{
switch (o->kind)
{
case PID:
{
set s = newSet ();
setInsert2 (s, ((o->u).id));
return s;
}
case PNUM:
{
return newSet ();
}
default:
{
error ("no this case\n");
return newSet ();
}
}
return newSet ();
}
set setOfInstruction (instruction s, set st)
{
switch (s->kind)
{
case MOVL:
{
set temp = setOperand (s->src);
setInsert2 (temp, s->dst);
return setUnion2 (temp, st);
}
case ADDL:
{
set temp = setOperand (s->src);
setInsert2 (temp, s->dst);
return setUnion2 (temp, st);
}
case SUBL:
{
set temp = setOperand (s->src);
setInsert2 (temp, s->dst);
return setUnion2 (temp, st);
}
case MULL:
{
set temp = setOperand (s->src);
setInsert2 (temp, s->dst);
return setUnion2 (temp, st);
}
case PPRINT:
{
set temp = setOperand (s->src);
return setUnion2 (temp, st);
}
case COMMENT:
{
return st;
}
default:
{
error ("no this case\n");
return newSet ();
}
}
return newSet ();
}
set miniPSetOfId (pentiumProg p)
{
linkedList l = linkedListGetFirst (p->instructions);
set s = newSet ();
while (l)
{
s = setOfInstruction (l->data, s);
l = l->next;
}
return s;
}
static void emit (linkedList l, str s)
{
linkedListInsertHead (l, s);
}
str toStringOperand (operand o)
{
switch (o->kind)
{
case PID:
{
return ((o->u).id);
}
case PNUM:
{
nat n = newNat ((o->u).i);
str s = getVft (n)->toString (n);
s = strConcat (newStr ("$"), s);
return s;
}
default: return newStr ("");
}
return newStr ("");
}
void toStringInstr (linkedList l, instruction s)
{
switch (s->kind)
{
case MOVL:
{
str a = newStr ("movl\t");
str b = toStringOperand (s->src);
a = strConcat (a, b);
a = strConcat (a, newStr (", "));
a = strConcat (a, newStr ("%eax\n"));
a = strConcat (a, newStr ("movl\t"));
a = strConcat (a, newStr ("%eax, "));
a = strConcat (a, s->dst);
a = strConcat (a, newStr ("\n"));
emit (l, a);
break;
}
case ADDL:
{
str a = newStr ("movl\t");
str b = toStringOperand (s->src);
a = strConcat (a, b);
a = strConcat (a, newStr (", "));
a = strConcat (a, newStr ("%eax\n"));
a = strConcat (a, newStr ("addl\t%eax, "));
a = strConcat (a, (s->dst));
a = strConcat (a, newStr ("\n"));
emit (l, a);
break;
}
case SUBL:
{
str a = newStr ("movl\t");
str b = toStringOperand (s->src);
a = strConcat (a, b);
a = strConcat (a, newStr (", "));
a = strConcat (a, newStr ("%eax\n"));
a = strConcat (a, newStr ("subl\t%eax, "));
a = strConcat (a, (s->dst));
a = strConcat (a, newStr ("\n"));
emit (l, a);
break;
}
case MULL:
{
str a = newStr ("movl\t");
str b = toStringOperand (s->src);
a = strConcat (a, b);
a = strConcat (a, newStr (", "));
a = strConcat (a, newStr ("%eax\n"));
a = strConcat (a, newStr ("mull\t"));
a = strConcat (a, (s->dst));
a = strConcat (a, newStr ("\n"));
a = strConcat (a, newStr ("movl\t%eax, "));
a = strConcat (a, (s->dst));
a = strConcat (a, newStr ("\n"));
emit (l, a);
break;
}
case PPRINT:
{
str a = toStringOperand (s->src);
str b = newStr ("pushl\t");
a = strConcat (b, a);
a = strConcat (a, newStr ("\n"));
str s1 = newStr ("$LC0");
s1 = strConcat (b, s1);
s1 = strConcat (s1, newStr ("\n"));
a = strConcat (a, s1);
str s2 = newStr ("call\t_printf\n");
a = strConcat (a, s2);
emit (l, a);
break;
}
case COMMENT:
{
str a = newStr ("\n// ");
a = strConcat (a, s->dst);
a = strConcat (a, newStr ("\n"));
emit (l, a);
break;
}
default:
{
error ("no this case\n");
break;
}
}
}
str miniPToStringProg (pentiumProg p)
{
linkedList l = newLinkedList ();
linkedList temp = linkedListGetFirst (p->instructions);
while (temp)
{
toStringInstr (l, temp->data);
temp = temp->next;
}
l = linkedListReverse (l);
str s = newStr ("");
l = l->next;
while (l)
{
s = strConcat (s, l->data);
l = l->nex
c链表 图 树的实现
5星 · 超过95%的资源 需积分: 10 184 浏览量
2008-11-13
00:20:50
上传
评论
收藏 92KB RAR 举报
nlcchina
- 粉丝: 0
- 资源: 2