⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code.h

📁 一个简单的C语言子集的编译器,一个简单的C语言子集的编译器
💻 H
字号:
/****************************************************/
/* File: code.h                                     */
/* Code emitting utilities for the TINY compiler    */
/* and interface to the TM machine                  */
/* Compiler Construction: Principles and Practice   */
/* Kenneth C. Louden                                */
/****************************************************/

#ifndef _CODE_H_
#define _CODE_H_

#include <string>
#include "global.h"
#include "code.h"
using namespace std;

/* pc = program counter  */
#define  pc 7

/* mp = "memory pointer" points
 * to bottom of memory (for temp storage)
 */
#define  mp 6

/* gp = "global pointer" points
 * to top of memory for (global)
 * variable storage
 */
#define gp 5

#define fp 4
#define zero 3
#define temp 2
/* accumulator */
#define  ac 0

/* 2nd accumulator */
#define  ac1 1

/* TM location number for current instruction emission */
static int emitLoc = 0 ;

/* Highest TM location emitted so far
   For use in conjunction with emitSkip,
   emitBackup, and emitRestore */
static int highEmitLoc = 0;

/* code emitting utilities */

/* Procedure emitComment prints a comment line 
 * with comment c in the code file
 */
void emitComment( string c );

/* Procedure emitRO emits a register-only
 * TM instruction
 * op = the opcode
 * r = target register
 * s = 1st source register
 * t = 2nd source register
 * c = a comment to be printed if TraceCode is TRUE
 */
void emitRO( string op, int r, int s, int t, string c);

/* Procedure emitRM emits a register-to-memory
 * TM instruction
 * op = the opcode
 * r = target register
 * d = the offset
 * s = the base register
 * c = a comment to be printed if TraceCode is TRUE
 */
void emitRM( string op, int r, int d, int s, string c);

/* Function emitSkip skips "howMany" code
 * locations for later backpatch. It also
 * returns the current code position
 */
int emitSkip( int howMany);

/* Procedure emitBackup backs up to 
 * loc = a previously skipped location
 */
void emitBackup( int loc);

/* Procedure emitRestore restores the current 
 * code position to the highest previously
 * unemitted position
 */
void emitRestore(void);

/* Procedure emitRM_Abs converts an absolute reference 
 * to a pc-relative reference when emitting a
 * register-to-memory TM instruction
 * op = the opcode
 * r = target register
 * a = the absolute location in memory
 * c = a comment to be printed if TraceCode is TRUE
 */
void emitRM_Abs( string op, int r, int a, string c);


void emitComment( string c)
{ fcode<<"* "<<c<<endl;}

void emitRO( string op, int r, int s, int t, string c=string(""))
{ fcode<<emitLoc++<<": "<<op<<"   "<<r<<","<<s<<","<<t;
  fcode<<"\t"<<c;
  fcode<<endl;
  if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} /* emitRO */

void emitRM( string op, int r, int d, int s, string c=string(""))
{ fcode<<emitLoc++<<": "<<op<<"   "<<r<<","<<d<<"("<<s<<")";
  fcode<<"\t"<<c;
  fcode<<endl;
  if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} /* emitRM */

int emitSkip( int howMany)
{  int i = emitLoc;
   emitLoc += howMany ;
   if (highEmitLoc < emitLoc)  highEmitLoc = emitLoc ;
   return i;
} /* emitSkip */

void emitBackup( int loc)
{ if (loc > highEmitLoc) emitComment(string("BUG in emitBackup"));
  emitLoc = loc ;
} /* emitBackup */

void emitRestore(void)
{ emitLoc = highEmitLoc;}

void emitRM_Abs( string op, int r, int a, string c=string(""))
{ fcode<<emitLoc<<": "<<op<<"   "<<r<<","<<a-(emitLoc+1)<<"("<<pc<<")";
  ++emitLoc;
  fcode<<c;
  fcode<<endl;
  if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} /* emitRM_Abs */
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -