generalregister.h

来自「mini mips 小型mips软件」· C头文件 代码 · 共 127 行

H
127
字号
#pragma once
#include <fstream>
#include <map>
#include <string>
#include "RegisterType.h"
#include "RegistersErrorException.h"
using namespace std;

class GeneralRegister{
       map<string, int> nameToID;
       map<int, string> idToName;
       union RegisterType RegisterTable[32];
   public:
       GeneralRegister						(string ininame = "GeneralRegister.ini");
       inline int				GetID		(string name);
       inline string			GetName		(int id);
       inline int				GetValue	(int id);
       inline int				GetValue	(string name);
       inline unsigned			GetValueU	(int id);
       inline unsigned			GetValueU	(string name);
       inline void				SetValue	(int id, int value);
       inline void				SetValue	(string name, int value);
       inline void				SetValueU	(int id, unsigned value);
       inline void				SetValueU	(string name, unsigned value);
	   inline void				Clear		(void);
	   inline union RegisterType&	operator []	(int id);
	   inline union RegisterType&	operator []	(string name);
};

void GeneralRegister::SetValue(string name, int value){
    if (nameToID.find(name) == nameToID.end()){
        RegistersErrorException rE("Error Name");
		throw rE;
    }else RegisterTable[nameToID[name]].IntV = value;
}


inline int GeneralRegister::GetID(string name){
    if (nameToID.find(name) == nameToID.end()){
        RegistersErrorException rE("Error ID");
		throw rE;
    }else
    return nameToID[name];
}

string GeneralRegister::GetName(int id){
    if (idToName.find(id) == idToName.end()){
        RegistersErrorException rE("Error Name");
		throw rE;
    }else
    return idToName[id];
}

int GeneralRegister::GetValue(int id){
    if (idToName.find(id) == idToName.end()){
        RegistersErrorException rE("Error ID");
		throw rE;
    }else
    return RegisterTable[id].IntV;
}


inline int GeneralRegister::GetValue(string name){
    if (nameToID.find(name) == nameToID.end()){
        RegistersErrorException rE("Error Name");
		throw rE;
    }else
    return RegisterTable[nameToID[name]].IntV;
}

unsigned GeneralRegister::GetValueU(int id){
    if (idToName.find(id) == idToName.end()){
        RegistersErrorException rE("Error ID");
		throw rE;
    }else
    return RegisterTable[id].UIntV;
}

unsigned GeneralRegister::GetValueU(string name){
    if (nameToID.find(name) == nameToID.end()){
        RegistersErrorException rE("Error Name");
		throw rE;
    }else
    return RegisterTable[nameToID[name]].UIntV;
}

void GeneralRegister::SetValue(int id, int value){
    if (idToName.find(id) == idToName.end()){
        RegistersErrorException rE("Error ID");
		throw rE;
    }else RegisterTable[id].IntV = value;
}

void GeneralRegister::SetValueU(int id, unsigned value){
    if (idToName.find(id) == idToName.end()){
        RegistersErrorException rE("Error ID");
		throw rE;
    }else RegisterTable[id].UIntV = value;
}

void GeneralRegister::SetValueU(string name, unsigned value){
    if (nameToID.find(name) == nameToID.end()){
        RegistersErrorException rE("Error Name");
		throw rE;
    }else RegisterTable[nameToID[name]].UIntV = value;
}

void GeneralRegister::Clear(void)
{
	for (int i = 0; i < 32; ++i) RegisterTable[i].UIntV = 0;
	SetValueU("$sp", 0x7ffffffc);
	SetValueU("$gp", 0x10008000);
}

union RegisterType& GeneralRegister::operator[](int id){
	if (idToName.find(id) == idToName.end()){
        RegistersErrorException rE("Error ID");
		throw rE;
    }else return RegisterTable[id];
}

union RegisterType& GeneralRegister::operator[](string name){
	if (nameToID.find(name) == nameToID.end()){
        RegistersErrorException rE("Error Name");
		throw rE;
    }else return RegisterTable[nameToID[name]];
}

⌨️ 快捷键说明

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