资源简介
前段时间自己业余抽空花了三个月编了一个c语言编译器,将c语言编译成masm32。总共4个文件,2000多行的代码,其中sca.c是词法,p.c生成语法树,table.c是符号表,g.c是转化成具体masm32代码,只支持char,short,int,double四种类型数据,其他的有兴趣的可以自己修改增加,由于masm32限制,目前该编译器只支持局部变量在函数开头定义,全局变量不能和局部变量同名。不支持变量强制转变,也不支持switch语句,个人感觉swich没用,#include不支持,只支持“ ”,现在打包给大家分享,包括源代码(感兴趣的可以自己修改或者转发学习,不允许商业用途),里面还有一些例子,想学习编译原理或者masm32的同志可以看看(特别是学习masm32的,可以把写好的c语言直接编译成masm32代码)!遇到问题可以跟我联系,尽量跟你们回复!
代码片段和文件信息
#include “table.c“
void error_gencode(TreeNode *);
void printtree(TreeNode *);
void gencode(TreeNode *);
void op_pointer(TreeNode *);
void op_array(TreeNode *);
void op_struct(TreeNode *);
void op_inc(TreeNode *);
void op_para(TreeNode *);
void op_call(TreeNode *);
void cmp_handle(TreeNode *char *int);
void exp_handle(TreeNode *char *int);
void load_var(TreeNode *char *int);
void print_g();
void print_l(TreeNode *);
int sizecal(TreeNode *);
int double_test(TreeNode *);
void getmainarg(TreeNode *);
int get_level(TreeNode *);
void error_gencode(TreeNode *p){
printf(“GENCODE ERROR:file %s line %d errormsg=%s\n“filenamep->linenoerrormsg);
exit(1);
}
int get_level(TreeNode *p){
int level=0;
if(p->t==ARRAY){
level=1;
p=p->child[1];
while(p&&p->t==LBRACE){
level++;
p=p->child[1];
}
}
return level;
}
int sizecal(TreeNode *p){
TreeNode *q*t*k;
int offset=0size=0maxsize=0;
q=p->child[0]->child[0]->child[1];
while(q){
t=q->child[0];
while(t){
k=t->child[0];
if(k->t==POINTER){
size=4;
if(offset%size)offset=(offset/size+1)*size;
offset=offset+4;
}else{
size=size_of_type(q->t);
if(offset%size)offset=(offset/size+1)*size;
if(k->t==ID)offset=offset+size;
if(k->t==ARRAY)offset=offset+size*get_array_num(k);
}
if(size>maxsize)maxsize=size;
t=t->child[1];
}
q=q->child[1];
}
//if(offset%maxsize)offset=(offset/maxsize+1)*maxsize;
//printf(“size=%d\n“offset);
return offset;
}
void assign_struct(TreeNode *p){
TreeNode *pl*pr*temp*t;
pl=p->child[0];
pr=p->child[1];
temp=test(pl);
if(temp->t==ARRAY){printtree(pl);printf(“\timul esitype _%s\n\tlea edi[ebx+esi]\n“temp->temp);}
else printf(“\tlea edi_%s\n“pl->id);
if(pr->t==DOT||pr->t==ARROW){
op_struct(pr);
t=test(pr);
if(t->t==ARRAY){op_array(pr);printf(“\timul esitype _%s\n\tlea esi[ebx+esi]\n“temp->temp);}
else printf(“\tlea esi%s\n“pr->id);
}else if(pr->t==ARRAY){op_array(pr);printf(“\timul esitype _%s\n\tlea esi[ebx+esi]\n“temp->temp);}
else printf(“\tlea esi_%s\n“pr->id);
printf(“\tmov ecxtype _%s\n\trep movsb\n“temp->temp);
}
int double_test(TreeNode *p){
TreeNode *temp;
if(!p){p=NewNode(ERROR);errormsg=“double test 1“;error_gencode(p);}
if(p->t==REAL)return 1;
if(p->t==ID){
temp=test(p);
if(temp->t==VAR&&temp->type==DOUBLE)return 1;
}
if(p->t==ARRAY){temp=test(p);if(temp->type==DOUBLE)return 1;}
if(p->t==DOT||p->t==ARROW){temp=struct_test(p);if(temp->type==DOUBLE)return 1;}
if(p->t==POINTER){
while(p){
if(p->t==ID)break;
p=p->child[0];
}
if(!p){p=NewNode(ERROR);errormsg=“double_test 2“;error_gencode(p);}
temp=test(p);
if(temp->type==DOUBLE)return 1;
}
if(p->t==CAL||p->t==CMP||p->t==ASSIGN){
int flfr;
fl=double_test(p->child[0]);
fr=doub
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 89923 2018-10-15 14:10 bianyi\a.exe
文件 90 2018-09-04 16:23 bianyi\example\beep.c
文件 1584 2018-09-18 09:37 bianyi\example\bm.c
文件 5486 2018-10-11 15:12 bianyi\example\bp.c
文件 3213 2018-09-14 09:07 bianyi\example\cal.c
文件 1563 2018-09-11 18:32 bianyi\example\find.c
文件 1154 2018-10-04 14:41 bianyi\example\kmp.c
文件 2537 2018-10-15 10:07 bianyi\example\maze.c
文件 6124 2018-10-03 17:24 bianyi\example\pe.c
文件 862 2018-09-11 14:08 bianyi\example\q.c
文件 3951 2018-09-10 11:54 bianyi\example\scan.c
文件 1417 2018-09-17 08:43 bianyi\example\t.c
文件 101 2018-10-05 11:46 bianyi\example\win.c
文件 7576 2018-09-17 14:33 bianyi\example\x.c
文件 43870 2018-10-12 09:59 bianyi\g.c
文件 15903 2018-10-12 08:46 bianyi\p.c
文件 7851 2018-10-08 16:04 bianyi\sca.c
文件 3358 2018-10-15 10:41 bianyi\stdio.h
文件 15568 2018-10-12 09:22 bianyi\table.c
目录 0 2018-10-15 14:14 bianyi\example
目录 0 2018-10-15 14:10 bianyi
----------- --------- ---------- ----- ----
212131 21
- 上一篇:多目标进化算法评价
- 下一篇:空间后方、前方交会算法的C++实现
相关资源
- 空间后方、前方交会算法的C++实现
- VC++数字图像处理程序设计 63讲 杨淑莹
- 大学生C语言课程设计——学生学籍信
- 卡尔曼滤波算法及C语言代码.
- c语言,学生成绩管理系统综合设计
- 科锐C语言系列视频教程.txt
- VC 实现窗口状态栏显示鼠标坐标信息
- [郝斌]C语言自学视频教程共180集.txt
- VC gethostname获取计算机名和IP地址.
- VC 邮件发送客户端源码.rar
- vc6显示行号包含注册码VC6LineNumberAdd
- AES 算法实现(linux) 可移植
- C++ 程序设计第二版 钱能 源码
- c++课程设计单词频率统计
- 《C语言程序设计第二版》文字非扫描
- 基于C51的单片机温度控制系统设计
- 亲戚关系计算器C语言源码
- AD7683驱动程序
- C语言程序设计试题库编程题-100道、程
- 51单片机驱动ADC0808电路图C51及汇编程
- C语言实现任何文件的加密解密功能
- C++中读取控制台输出,并将文件指针
- DES 加密解密 C语言实现
- 职工信息管理系统(C语言实现)
- C语言中字符数组操作的函数实现(
- 英汉词典(C语言实现)
- 基于TCP/IP和Sqlite3数据库的网络通讯录
- C++贪心算法实现活动安排问题(代码
- C++实现简单走迷宫的代码
- 基于c++ ege图形库实现五子棋游戏
评论
共有 条评论