string CBackendx86::Operand(const CTac *op) { string operand; // TODO // return a string representing op // hint: take special care of references (op of type CTacReference) std::stringstream buffer ; buffer << op ; CScope *cs = GetScope(); CSymtab *st = cs->GetSymbolTable(); vector<CSymbol*> slist = st->GetSymbols(); vector< CSymbol * > symbols = st->GetSymbols() ; //CTacName if (st->FindSymbol(buffer.str()) != NULL){ operand = buffer.str() ; } //CTacConstant else if (buffer.str()[0] > 47 && buffer.str()[0] < 58){ operand = Imm(atoi(buffer.str().c_str())) ; } //CTacReference else { } return operand ; }
void CBackendx86::EmitLocalData(CScope *scope) // initializes array's meta-data { assert(scope != NULL); CSymtab* symtab = scope->GetSymbolTable(); vector<CSymbol*> slist = symtab->GetSymbols(); vector<CSymbol*>::iterator it; for(it = slist.begin(); it != slist.end(); it++) { const CArrayType* typ = dynamic_cast<const CArrayType*>((*it)->GetDataType()); if(typ != NULL && (*it)->GetSymbolType() == stLocal && (dynamic_cast<CSymParam*>(*it) == NULL)) { // just initialize the arrays that are local and are not param int offset = (*it)->GetOffset(); string base = (*it)->GetBaseRegister(); int dim = typ->GetNDim(); stringstream ss, ss2; ss << "$" << dim << "," << offset << "(" << base << ")"; ss2 << "local array '" << (*it)->GetName() << "': " << dim << " dimensions"; EmitInstruction("movl", ss.str(), ss2.str()); // store the dimension meta-data int dimcount = 1; while(typ != NULL) { stringstream ss, ss2; offset += 4; int n = typ->GetNElem(); ss << "$" << n << "," << offset << "(" << base << ")"; ss2 << " dimension " << dimcount << ": " << n << " elements"; EmitInstruction("movl", ss.str(), ss2.str()); // store the meta-data of length of each dimension typ = dynamic_cast<const CArrayType*>(typ->GetInnerType()); dim++; dimcount++; } } } }
void CBackendx86::EmitGlobalData(CScope *scope) { assert(scope != NULL); // emit the globals for the current scope CSymtab *st = scope->GetSymbolTable(); assert(st != NULL); bool header = false; vector<CSymbol*> slist = st->GetSymbols(); _out << dec; size_t size = 0; for (size_t i=0; i<slist.size(); i++) { CSymbol *s = slist[i]; const CType *t = s->GetDataType(); if (s->GetSymbolType() == stGlobal) { if (!header) { _out << _ind << "# scope: " << scope->GetName() << endl; header = true; } // insert alignment only when necessary if ((t->GetAlign() > 1) && (size % t->GetAlign() != 0)) { size += t->GetAlign() - size % t->GetAlign(); _out << setw(4) << " " << ".align " << right << setw(3) << t->GetAlign() << endl; } _out << left << setw(36) << s->GetName() + ":" << "# " << t << endl; if (t->IsArray()) { const CArrayType *a = dynamic_cast<const CArrayType*>(t); assert(a != NULL); int dim = a->GetNDim(); _out << setw(4) << " " << ".long " << right << setw(4) << dim << endl; for (int d=0; d<dim; d++) { assert(a != NULL); _out << setw(4) << " " << ".long " << right << setw(4) << a->GetNElem() << endl; a = dynamic_cast<const CArrayType*>(a->GetInnerType()); } } const CDataInitializer *di = s->GetData(); if (di != NULL) { const CDataInitString *sdi = dynamic_cast<const CDataInitString*>(di); assert(sdi != NULL); // only support string data initializers for now _out << left << setw(4) << " " << ".asciz " << '"' << sdi->GetData() << '"' << endl; } else { _out << left << setw(4) << " " << ".skip " << dec << right << setw(4) << t->GetDataSize() << endl; } size += t->GetSize(); } } _out << endl; // emit globals in subscopes (necessary if we support static local variables) vector<CScope*>::const_iterator sit = scope->GetSubscopes().begin(); while (sit != scope->GetSubscopes().end()) EmitGlobalData(*sit++); }