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

📄 mips.h

📁 一个面向对像语言的编译器
💻 H
字号:
/* File: mips.h
 * ------------
 * The Mips class defines an object capable of emitting MIPS
 * instructions and managing the allocation and use of registers.
 * It is used by the Tac instruction classes to convert each
 * instruction to the appropriate MIPS equivalent.
 *
 * You can scan this code to see it works and get a sense of what
 * it does.  You are not likely to need to change anything unless
 * you're attempting some machine-specific optimizations. 
 *
 * It comments the emitted assembly but the commenting for the code
 * in the class itself is pretty sparse. The SPIM manual (see link
 * from other materials on our web site) has more detailed documentation
 * on the MIPS architecture, instruction set, calling conventions, etc.
 */

#ifndef _H_mips
#define _H_mips

#include "tac.h"
#include "taclist.h"
class Declaration;


class Mips {
  private:
    typedef enum {zero, at, v0, v1, a0, a1, a2, a3,
			t0, t1, t2, t3, t4, t5, t6, t7,
			s0, s1, s2, s3, s4, s5, s6, s7,
			t8, t9, k0, k1, gp, sp, fp, ra, NumRegs } Register;

    struct RegContents {
	bool isDirty;
	Declaration *decl;
	char *name;
	bool isGeneralPurpose;
    } regs[NumRegs];

    Register lastUsed;

    typedef enum { ForRead, ForWrite } Reason;

    Register GetRegister(Declaration *decl, Reason reason, Register avoid1, Register avoid2);
    Register GetRegister(Declaration *decl, Register avoid1 = zero);
    Register GetRegisterForWrite(Declaration *decl, Register avoid1 = zero, Register avoid2 = zero);
    bool FindRegisterWithContents(Declaration *decl, Register& reg);
    Register SelectRegisterToSpill(Register avoid1, Register avoid2);
    void SpillRegister(Register reg);
    void SpillAllDirtyRegisters();
    void SpillForEndFunction();

    void EmitCallInstr(Declaration *dst, const char *fn, int bytes, bool isL);
    
    static const char *mipsName[Tac::NumOps];
    static const char *NameForTac(Tac::OpCode code);

 public:
    
    Mips();

    static void Emit(const char *fmt, ...);
    
    void EmitLoadConstant(Declaration *dst, int val);
    void EmitLoadStringConstant(Declaration *dst, const char *str);
    void EmitLoadLabel(Declaration *dst, const char *label);

    void EmitLoad(Declaration *dst, Declaration *reference, int offset);
    void EmitStore(Declaration *reference, Declaration *value, int offset);
    void EmitCopy(Declaration *dst, Declaration *src);
    
    void EmitBinaryOp(Tac::OpCode code, Declaration *dst, 
			    Declaration *op1, Declaration *op2);

    void EmitLabel(const char *label);
    void EmitGoto(const char *label);
    void EmitIfZ(Declaration *test, const char*label);
    void EmitReturn(Declaration *returnVal);
    
    void EmitBeginFunction(int frameSize);
    void EmitEndFunction();

    void EmitArg(Declaration *arg);
    void EmitLCall(Declaration *result, const char* label, int bytes);
    void EmitACall(Declaration *result, Declaration *fnAddr, int bytes);
    
    void EmitVTable(const char *label, DeclList *methods);

    void EmitPreamble();
};


#endif
 

⌨️ 快捷键说明

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