| 1 | /*
|
---|
| 2 | * File
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #include <sstream>
|
---|
| 6 | #include <cstdio>
|
---|
| 7 | #include <string>
|
---|
| 8 | #include "AssemblerCode.h"
|
---|
| 9 | #include "debug.h"
|
---|
| 10 |
|
---|
| 11 | extern int msglevel;
|
---|
| 12 |
|
---|
| 13 | using namespace std;
|
---|
| 14 |
|
---|
| 15 | AssemblerCode::AssemblerCode() {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | AssemblerCode::~AssemblerCode() {
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | unsigned int AssemblerCode::GetStatementCount() const {
|
---|
| 22 | return (statements.size());
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | MStatement *AssemblerCode::GetStatement(unsigned int i) {
|
---|
| 26 | return (statements.at(i));
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | // Appends a statement
|
---|
| 30 |
|
---|
| 31 | void AssemblerCode::AppendStatement(MStatement *stmt) {
|
---|
| 32 | statements.push_back(stmt);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | // Inserts a statement before the i-th statement
|
---|
| 36 |
|
---|
| 37 | void AssemblerCode::InsertStatement(MStatement *stmt, unsigned int i) {
|
---|
| 38 | statements.insert(statements.begin() + i, stmt);
|
---|
| 39 | }
|
---|
| 40 | // Removes the i-th statement
|
---|
| 41 |
|
---|
| 42 | void AssemblerCode::RemoveStatement(unsigned int i) {
|
---|
| 43 | statements.erase(statements.begin() + i);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | // Dump
|
---|
| 47 |
|
---|
| 48 | void AssemblerCode::Dump(FILE *file) {
|
---|
| 49 | msglevel = 100;
|
---|
| 50 | for (unsigned int i = 0; i < GetStatementCount(); ++i) {
|
---|
| 51 | MStatement *stmt = GetStatement(i);
|
---|
| 52 |
|
---|
| 53 | /* Always valid values */
|
---|
| 54 | const char *opcode = MOperatorToString(stmt->GetOperator());
|
---|
| 55 |
|
---|
| 56 | const char *hash = "\t# ";
|
---|
| 57 | const char *comment = stmt->GetComment();
|
---|
| 58 | if (comment == NULL) {
|
---|
| 59 | comment = "";
|
---|
| 60 | hash = "";
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | string label = stmt->GetLabel();
|
---|
| 64 | if (label.empty()) {
|
---|
| 65 | label = "";
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | if (stmt->GetOperator() == MOP_LABEL) {
|
---|
| 69 | pmesg(100, "===MStatement MOP_LABEL ===\n");
|
---|
| 70 | fprintf(file, "%s:\t%s%s\n", label.c_str(), hash, comment);
|
---|
| 71 | continue;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | pmesg(100, "=== MStatement ===\n");
|
---|
| 75 | if (stmt->GetOperand1() == NULL) {
|
---|
| 76 | fprintf(file, "%s\t%s\t%s%s\n", label.c_str(), opcode, hash, comment);
|
---|
| 77 | continue;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | string op1 = MOperandToString(stmt->GetOperand1());
|
---|
| 81 | if (stmt->GetOperand2() == NULL) {
|
---|
| 82 | fprintf(file, "%s\t%s\t%s\t%s%s\n", label.c_str(), opcode, op1.c_str(),
|
---|
| 83 | hash, comment);
|
---|
| 84 | continue;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | string op2 = MOperandToString(stmt->GetOperand2());
|
---|
| 88 | if (stmt->GetOperand3() == NULL) {
|
---|
| 89 | fprintf(file, "%s\t%s\t%s, %s\t%s%s\n", label.c_str(), opcode, op1.c_str(),
|
---|
| 90 | op2.c_str(), hash, comment);
|
---|
| 91 | continue;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | string op3 = MOperandToString(stmt->GetOperand3());
|
---|
| 95 | fprintf(file, "%s\t%s\t%s, %s, %s\t%s%s\n", label.c_str(), opcode, op1.c_str(),
|
---|
| 96 | op2.c_str(), op3.c_str(), hash, comment);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | string AssemblerCode::MOperandToString(MOperand *operand) {
|
---|
| 101 | stringstream ss;
|
---|
| 102 | pmesg(100, "Operand Type: ");
|
---|
| 103 | switch (operand->GetOperandType()) {
|
---|
| 104 | case MT_INT: // Integer immediate
|
---|
| 105 | {
|
---|
| 106 | pmesg(100, "MT_INT");
|
---|
| 107 | ss << operand->GetIntValue();
|
---|
| 108 | break;
|
---|
| 109 | }
|
---|
| 110 | case MT_REAL: // Real immediate
|
---|
| 111 | {
|
---|
| 112 | pmesg(100, "MT_REAL");
|
---|
| 113 |
|
---|
| 114 | ss << showpoint << operand->GetRealValue();
|
---|
| 115 | break;
|
---|
| 116 | }
|
---|
| 117 | case MT_OFFSET:
|
---|
| 118 | {
|
---|
| 119 | pmesg(100, "MT_OFFSET");
|
---|
| 120 | int offset = operand->GetOffset();
|
---|
| 121 | const char *regstr = MRegisterTypeToString(operand->GetRegisterType());
|
---|
| 122 | ss << offset << '(' << regstr << ')';
|
---|
| 123 | break;
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 | case MT_REGISTER: // Integer Register
|
---|
| 127 | {
|
---|
| 128 | pmesg(100, "MT_REGISTER");
|
---|
| 129 | const char *regstr = MRegisterTypeToString(operand->GetRegisterType());
|
---|
| 130 | ss << regstr;
|
---|
| 131 | break;
|
---|
| 132 | }
|
---|
| 133 | case MT_ADDRESS: // Address operand
|
---|
| 134 | {
|
---|
| 135 | pmesg(100, "MT_REGISTER");
|
---|
| 136 | ss << operand->GetAddress();
|
---|
| 137 | break;
|
---|
| 138 | }
|
---|
| 139 | case MT_LABEL:
|
---|
| 140 | {
|
---|
| 141 | pmesg(100, "MT_LABEL");
|
---|
| 142 |
|
---|
| 143 | ss << operand->GetLabel();
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | default:
|
---|
| 147 | return "?";
|
---|
| 148 | }
|
---|
| 149 | pmesg(100, ": %s\n", ss.str().c_str());
|
---|
| 150 | return (ss.str());
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | const char *AssemblerCode::MRegisterTypeToString(MRegisterType op) {
|
---|
| 154 | const char *registertype_str[] = {
|
---|
| 155 | "$0", // ZERO 0
|
---|
| 156 | "$at",
|
---|
| 157 | "$v0",
|
---|
| 158 | "$v1",
|
---|
| 159 | "$a0",
|
---|
| 160 | "$a1",
|
---|
| 161 | "$a2",
|
---|
| 162 | "$a3",
|
---|
| 163 | "$t0",
|
---|
| 164 | "$t1",
|
---|
| 165 | "$t2",
|
---|
| 166 | "$t3",
|
---|
| 167 | "$t4",
|
---|
| 168 | "$t5",
|
---|
| 169 | "$t6",
|
---|
| 170 | "$t7",
|
---|
| 171 | "$s0",
|
---|
| 172 | "$s1",
|
---|
| 173 | "$s2",
|
---|
| 174 | "$s3",
|
---|
| 175 | "$s4",
|
---|
| 176 | "$s5",
|
---|
| 177 | "$s6",
|
---|
| 178 | "$s7",
|
---|
| 179 | "$t8",
|
---|
| 180 | "$t9",
|
---|
| 181 | "$k0",
|
---|
| 182 | "$k1",
|
---|
| 183 | "$gp",
|
---|
| 184 | "$sp",
|
---|
| 185 | "$fp",
|
---|
| 186 | "$ra",
|
---|
| 187 | "$f0",
|
---|
| 188 | "$f1",
|
---|
| 189 | "$f2",
|
---|
| 190 | "$f3",
|
---|
| 191 | "$f4",
|
---|
| 192 | "$f5",
|
---|
| 193 | "$f6",
|
---|
| 194 | "$f7",
|
---|
| 195 | "$f8",
|
---|
| 196 | "$f9",
|
---|
| 197 | "$f10",
|
---|
| 198 | "$f11",
|
---|
| 199 | "$f12",
|
---|
| 200 | "$f13",
|
---|
| 201 | "$f14",
|
---|
| 202 | "$f15",
|
---|
| 203 | "$f16",
|
---|
| 204 | "$f17",
|
---|
| 205 | "$f18",
|
---|
| 206 | "$f19",
|
---|
| 207 | "$f20",
|
---|
| 208 | "$f21",
|
---|
| 209 | "$f22",
|
---|
| 210 | "$f23",
|
---|
| 211 | "$f24",
|
---|
| 212 | "$f25",
|
---|
| 213 | "$f26",
|
---|
| 214 | "$f27",
|
---|
| 215 | "$f28",
|
---|
| 216 | "$f29",
|
---|
| 217 | "$f30",
|
---|
| 218 | "$f31"
|
---|
| 219 | };
|
---|
| 220 | return registertype_str[op];
|
---|
| 221 | }
|
---|