#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语言经典经典例子---快速成为C中高手
共93个文件
h:31个
o:30个
c:30个
需积分: 9 90 下载量 95 浏览量
2008-10-29
15:59:26
上传
评论
收藏 72KB RAR 举报
温馨提示
C语言经典经典例子---快速成为C中高手 快速掌握C语言,让你的基础更加牢固
资源推荐
资源详情
资源评论
收起资源包目录
c.rar (93个子文件)
c
plist.o 1KB
pvm.c 2KB
property.c 1KB
triple.c 957B
property.o 1KB
tuple.o 2KB
complex.h 209B
miniPentium.o 7KB
pvm.o 2KB
control.c 1KB
control.o 1KB
queue.o 902B
cvm.h 84B
miniPentium.c 9KB
nat.o 1KB
file.o 1KB
lib.a 67KB
commonInter.h 487B
queue.c 477B
miniVC.h 122B
miniPentium.h 1KB
graph.o 2KB
set.c 7KB
bitArray.h 423B
nat.h 382B
hash.h 442B
file.h 282B
arrayList.h 581B
graph.h 246B
file.c 946B
control.h 142B
tag.c 718B
linkedList.o 3KB
plist.h 274B
tag.h 159B
stack.h 229B
cvm.o 2KB
myint.h 139B
arrayList.o 2KB
myint.o 1KB
hash.c 5KB
tuple.h 428B
complex.o 923B
property.h 261B
linkedList.h 1KB
dot.o 3KB
store.o 677B
dot.h 209B
triple.h 274B
makefile 3KB
nat.c 1KB
store.c 398B
mystdlib.h 215B
commonInter.c 92B
bitArray.o 2KB
arrayList.c 2KB
dot.c 3KB
error.o 676B
str.c 3KB
miniVC.c 4KB
store.h 170B
pvm.h 92B
mystdlib.c 900B
miniC.h 1KB
graph.c 2KB
triple.o 1KB
mystdlib.o 1KB
myint.c 1KB
error.c 222B
bitArray.c 2KB
miniC.c 5KB
stack.o 923B
complex.c 681B
plist.c 1KB
miniC.o 4KB
str.h 482B
set.h 596B
linkedList.c 4KB
stack.c 504B
str.o 2KB
queue.h 218B
tuple.c 2KB
cvm.c 2KB
btree.c 3KB
poly.h 68B
hash.o 4KB
error.h 128B
btree.o 3KB
set.o 6KB
btree.h 478B
miniVC.o 4KB
tag.o 1KB
commonInter.o 358B
共 93 条
- 1
资源评论
wzl_long
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功