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

来源:互联网 发布:linux压缩文件命令 zip 编辑:程序博客网 时间:2024/05/16 12:06

TM虚拟机接口用于生成TM指令,源代码如下:

//code.h

//TM接口
#ifndef _CODE_H_
#define _CODE_H_

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

#define ac 0
#define ac1 0
//输出寄存器指令
void emitRO(char *op, int r, int s, int t, charc);
//输出寄存器内存指令
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, charc);
//输出注释
void emitComment(charc);

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

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

void emitComment( char )
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 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 ;

0 0