您的位置:首页 > 其它

Tiny语言编译器之TM虚拟机接口

2014-02-24 15:04 211 查看
TM虚拟机接口用于生成TM指令,源代码如下:

//code.h

//TM接口
#ifndef _CODE_H_
#define _CODE_H_

#define pc 7 //程序寄存器
#define mp 6 //指向数据区顶部
#define gp 5 //指向数据区底部

#define ac 0
#define ac1 0
//输出寄存器指令
void emitRO(char *op, int r, int s, int t, char* c);
//输出寄存器内存指令
void emitRM(char *op, int r, int d, int s, char *c);
//跳过一些指令标号,返回当前指令标号
int emitSkip(int howmany);
//回到loc处产生指令
void emitBackup(int loc);
//恢复当前位置
void emitRestore();
//将绝对地址指令转化为相对地址指令
void emitRM_Abs(char *op, int r, int a, char* c);
//输出注释
void emitComment(char* c);

//code.c
#endif #include "globals.h"
#include "code.h"
static int emitLoc = 0 ; //当前标号,用于产生当前指令

static int highEmitLoc = 0; //下一条指令的标号,在调用emitBackup后可以用highEmitLoc恢复当前标号

void emitComment( char * c )

{ if (TraceCode) fprintf(code,"* %s\n",c);}

void emitRO( char *op, int r, int s, int t, char *c)

{ fprintf(code,"%3d:  %5s  %d,%d,%d ",emitLoc++,op,r,s,t);

  if (TraceCode) fprintf(code,"\t%s",c) ;

  fprintf(code,"\n") ;

  if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;



void emitRM( char * op, int r, int d, int s, char *c)

{ fprintf(code,"%3d:  %5s  %d,%d(%d) ",emitLoc++,op,r,d,s);

  if (TraceCode) fprintf(code,"\t%s",c) ;

  fprintf(code,"\n") ;

  if (highEmitLoc < emitLoc)  highEmitLoc = emitLoc ;



int emitSkip( int howMany)

{  int i = emitLoc;

   emitLoc += howMany ;

   if (highEmitLoc < emitLoc)  highEmitLoc = emitLoc ;

   return i;



void emitBackup( int loc)

{ if (loc > highEmitLoc) emitComment("BUG in emitBackup");

  emitLoc = loc ;



void emitRestore(void)

{ emitLoc = highEmitLoc;}

void emitRM_Abs( char *op, int r, int a, char * c)

{ fprintf(code,"%3d:  %5s  %d,%d(%d) ",

               emitLoc,op,r,a-(emitLoc+1),pc);

  ++emitLoc ;

  if (TraceCode) fprintf(code,"\t%s",c) ;

  fprintf(code,"\n") ;

  if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: