📄 asmmanager.cpp
字号:
#include "StdAfx.h"
#include "AsmManager.h"
#include "CompilingErrorException.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include <ctype.h>
bool AsmManager::GetWord(istringstream& iss, string &word){
word = "";
if (iss.eof()) return false;
char ch;
while(iss.get(ch) && (ch ==',' || isspace(ch) || ch == '(' || ch == ')' || ch == '#')){
if (ch == '#') while (iss.get(ch) && ch != '\n');
}
if (iss.eof()) return false;
do{
word += ch;
}while(iss.get(ch) && ch != ',' && ch != '(' && ch != ')' && !isspace(ch));
//if (!word.empty() && word[0] == '#') while (iss.get(ch) && ch != '\'n') word += ch;
//cout << word << "\t" << word.size() << "\t" <<word.empty() << endl;
return !word.empty();
}
string AsmManager::ToLower(string s){
string ret;
for (unsigned i = 0; i < s.length(); ++i) ret += tolower(s[i]);
return ret;
}
bool AsmManager::IsRegister(string& s){
return s[0] == '$';
}
void AsmManager::init(string& asmCodes, string labelfilename)
{
ifstream fin(labelfilename.c_str());
string word;
while (fin >> word){
lableInstrument.insert(word);
}
unsigned pos = 0x00400000;
vector<string> ins;
istringstream stin(asmCodes);
while (GetWord(stin, word)){
if (word[0] == '#') continue; //Skip commend
if (*word.rbegin() == ':'){
if (!ins.empty()) throw CompilingErrorException("Lable defined in an instrument.");
word.resize(word.length() - 1);
if (LableTable.find(word) != LableTable.end()) throw CompilingErrorException("Duplation Lables.");
LableTable[word] = pos;
}
else {
if (ins.empty()) {
word = ToLower(word);
ins.push_back(word);
if (InstrumentType::infoTable.find(word) == InstrumentType::infoTable.end())
throw CompilingErrorException("Wrong instrument name or we haven't support it.");
}
else{
ins.push_back(word);
}
if (ins.size() == InstrumentType::ArgumentNumOfInstrument(ins[0]) + 1){
// TODO: If have time, translate the pesudo code.
AsmCodes.push_back(make_pair(ins, pos));
pos += 4;
if (lableInstrument.find(ins[0]) != lableInstrument.end()){
// I don't know whether we need add a noop instrument after a jump instrument;
// ins.clear();
ins.clear();
ins.push_back("noop");
AsmCodes.push_back(make_pair(ins, pos));
pos += 4;
}
ins.clear();
}
}
}
for (vector<pair<vector<string>,int> >::iterator i = AsmCodes.begin(); i < AsmCodes.end(); ++i){
if (lableInstrument.find(i->first[0]) != lableInstrument.end()){
for (vector<string>::iterator j = i->first.begin() + 1; j < i->first.end(); ++j){
if (!IsRegister(*j) && !IsNum(*j)){
if (LableTable.find(*j) == LableTable.end()) throw CompilingErrorException("Undefined Lable.");
unsigned address = LableTable[*j];
ostringstream os;
if (InstrumentType::ArgumentNumOfInstrument(i->first[0]) == 1){
os << ((address & 0x0fffffff) >> 2);
}
else{
os << ((address - i->second - 1) >> 2);
}
*j = os.str();
}
}
}
}
}
bool AsmManager::IsNum(string& s){
for (string::iterator i = s.begin(); i != s.end(); ++i){
if (!isdigit(*i)) return false;
}
return true;
}
void AsmManager::Translate(){
TextCode item;
for (vector<pair<vector<string>,int> >::iterator i = AsmCodes.begin(); i < AsmCodes.end(); ++i){
cpu.instrument.SetAsmCode(i->first);
item.asmCode = cpu.instrument.GetAsmCode();
item.binary = cpu.instrument.GetCodeRegister().UIntV;
item.address = i->second;
textCodes.push_back(item);
}
}
//AsmManager::AsmManager(std::string &asmCodes, std::string labelfilename){
// init(asmCodes, labelfilename);
//}
//AsmManager::AsmManager(const char *filename, std::string labelfilename){
// ifstream fin(filename);
// string asmCodes;
// char ch;
// while(fin.get(ch)) asmCodes += ch;
// init(asmCodes, labelfilename);
//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -