void BinOpNode::generateLvalue(AsmCode& code){ string value = token->Value; if (value == "+"){ SymbolType *type = left->getType(); if (dynamic_cast<ArraySymbol*>(type)) left->generateLvalue(code); else if (dynamic_cast<PointerSymbol*>(type)) left->generate(code); SymbolType *t = type->upType(); right->generate(code); code.add(_POP, _EAX) .add(_MOV, _EBX, to_string(t->byteSize())) .add(_IMUL, _EAX, _EBX) .add(_POP, _EBX) .add(_ADD, _EAX, _EBX) .add(_PUSH, _EAX); t = t->upType(); } else if (isAssing(token)){ generate(code); code.add(_POP, _EAX); left->generateLvalue(code); } else throw MyException("Compiler error"); }
void ArrNode::generateLvalue(AsmCode &code){ SymbolType *type = name->getType(); if(dynamic_cast<ArraySymbol*>(type)) name->generateLvalue(code); else if(dynamic_cast<PointerSymbol*>(type)) name->generate(code); SymbolType *t = type->upType(); for(int i = 0; i < args.size(); i++){ args[i]->generate(code); code.add(_POP, _EAX) .add(_MOV, _EBX, to_string(t->byteSize())) .add(_IMUL, _EAX, _EBX) .add(_POP, _EBX) .add(_ADD, _EAX, _EBX) .add(_PUSH, _EAX); t = t->upType(); } }
SymbolType *ArrNode::getType() { if(nodeType) return nodeType; ArraySymbol *sym = dynamic_cast<ArraySymbol*>(name->getType()); if(!sym) throw MyException("Expression must be a pointer type", token); SymbolType *type = sym; for(int i = 0; i < args.size(); i++){ type = type->upType(); if(type == 0) throw MyException("Expression must be a pointer type", args[i]->token); if(!args[i]->getType()->canConvertTo(IntType)) throw MyException("Expression must have integral type", args[i]->token); args[i] = makeTypeCoerce(args[i], args[i]->getType(), IntType); } nodeType = type; return type; }
bool ArrNode::isModifiableLvalue() const { SymbolType *type = name->getType(); for(int i = 0; i < args.size(); i++) type = type->upType(); return type->isModifiableLvalue(); }