void VariableEntry::memAlloc(int reset_AR) { if (varKind() == VariableEntry::GLOBAL_VAR) { offSet(memAllocUtil(type(), EntryKind::GLOBAL, 0)); LOG("Allocation of GLOBAL name: " << name() << ", offSet: " << offSet()); } else if (varKind() == VariableEntry::LOCAL_VAR) { offSet(memAllocUtil(type(), EntryKind::LOCAL, reset_AR)); LOG("Allocation of LOCAL, name: " << name() << ", offSet:" << offSet()); } else if (varKind() == VariableEntry::PARAM_VAR) { offSet(memAllocUtil(type(), EntryKind::FUNCTION_PARAMETER, reset_AR)); LOG("Allocation of PARAM, name: " << name() << ", offSet: " << offSet()); } else { LOG("Allocation failed for, name: " << name() << ", VarKind not set! Cannot know where to allocate this."); } return; }
CodeBlock* VariableEntry::codeGen(){ CodeBlock* var_block = new CodeBlock(); if(initVal() != NULL){ var_block->append(initVal()->codeGen()); ICode::ICodeType store_type = ICode::ICodeType::STI; ICode get_addr; if(varKind() == VariableEntry::VarKind::GLOBAL_VAR){ ICode::ICodeType mov_type = ICode::ICodeType::MOVI; get_addr = ICode(mov_type,new Value(offSet(),Type::TypeTag::INT),&mem_register_); } else { ICode::ICodeType add_type = ICode::ICodeType::ADD; get_addr = ICode(add_type,&MemoryMgr::basePointerRegister(),new Value(offSet(),Type::TypeTag::INT),&mem_register_); } if(type()->tag() == Type::TypeTag::BOOL){ CodeBlock* true_block = new CodeBlock(); ICode store_true_val(store_type,new Value(1,Type::TypeTag::UINT),&mem_register_); ICode jmp_end(ICode::ICodeType::JMP,endLabel()); true_block->setStartLabel(trueLabel()); true_block->append(get_addr); true_block->append(store_true_val); true_block->append(jmp_end); CodeBlock* false_block = new CodeBlock(); ICode store_false_val(store_type,new Value(0,Type::TypeTag::UINT),&mem_register_); false_block->setStartLabel(falseLabel()); false_block->append(get_addr); false_block->append(store_false_val); false_block->setEndLabel(endLabel()); var_block->append(true_block); var_block->append(false_block); } else { if(initVal()->reg().isFloat()){ store_type = ICode::ICodeType::STF; } ICode store_val(store_type,&initVal()->reg(),&mem_register_); var_block->append(get_addr); var_block->append(store_val); } } ICode incr_stack(ICode::ICodeType::ADD,&MemoryMgr::stackPointerRegister(),new Value(1,Type::TypeTag::INT),&MemoryMgr::stackPointerRegister()); var_block->append(incr_stack); return var_block; }
void VariableEntry::codeGen(IntermediateCodeGen * list) { LOG(""); int isInt, regPtr = -1, hackyReg = -1; Instruction *instr = NULL; Instruction *getParam = NULL; Instruction *hackyinstr = NULL; Instruction *instrAddOffset = NULL; Value *immediate = NULL; Value *immediate1 = NULL; if (type()->isIntegral(type()->tag()) || type()->isBool(type()->tag())) { setReg(get_vreg_int(), VREG_INT); isInt = 1; } else { setReg(get_vreg_float(), VREG_FLOAT); isInt = 0; } if (varKind() == VariableEntry::VarKind::PARAM_VAR) { immediate1 = new Value(1, Type::TypeTag::INT); instr = new Instruction(Instruction::Mnemonic::ADD); instr->operand_src1(get_vreg_temp_stack(), NULL, VREG_INT); instr->operand_src2(-1, immediate1, Instruction::OpType::IMM); instr->operand_dest(get_vreg_temp_stack(), NULL, VREG_INT); list->addInstruction(instr); getParam = new Instruction(Instruction::typedMnemonic(isInt, Instruction::Mnemonic::LDI)); getParam->operand_src1(get_vreg_temp_stack(), NULL, VREG_INT); getParam->operand_dest(getReg(), NULL, reg_type()); list->addInstruction(getParam); } else if (varKind() == VariableEntry::VarKind::LOCAL_VAR) { if (initVal()) { initVal()->codeGen(list); if (!isInt && initVal()->reg_type() == VREG_INT) { hackyinstr = new Instruction(Instruction::Mnemonic::MOVIF); hackyinstr->operand_src1(initVal()->getReg(), NULL, initVal()->reg_type()); hackyReg = get_vreg_float(); hackyinstr->operand_dest(hackyReg, NULL, VREG_FLOAT); list->addInstruction(hackyinstr); getParam = new Instruction(Instruction::typedMnemonic(isInt, Instruction::Mnemonic::STI)); getParam->operand_src1(hackyReg, NULL, VREG_FLOAT); getParam->operand_dest(get_vreg_sp(), NULL, VREG_INT); list->addInstruction(getParam); } else { getParam = new Instruction(Instruction::typedMnemonic(isInt, Instruction::Mnemonic::STI)); getParam->operand_src1(initVal()->getReg(), NULL, initVal()->reg_type()); getParam->operand_dest(get_vreg_sp(), NULL, VREG_INT); list->addInstruction(getParam); } } immediate1 = new Value(1, Type::TypeTag::INT); instr = new Instruction(Instruction::Mnemonic::SUB); instr->operand_src1(get_vreg_sp(), NULL, VREG_INT); instr->operand_src2(-1, immediate1, Instruction::OpType::IMM); instr->operand_dest(get_vreg_sp(), NULL, VREG_INT); list->addInstruction(instr); } else if (varKind() == VariableEntry::VarKind::GLOBAL_VAR) { instrAddOffset = new Instruction(); instrAddOffset->opcode(Instruction::Mnemonic::ADD); instrAddOffset->operand_src1(get_vreg_global(), NULL, VREG_INT); immediate = new Value(offSet(), Type::TypeTag::INT); instrAddOffset->operand_src2(-1, immediate, Instruction::OpType::IMM); regPtr = get_vreg_int(); instrAddOffset->operand_dest(regPtr, NULL, VREG_INT); list->addInstruction(instrAddOffset); if (initVal()) { initVal()->codeGen(list); instr = new Instruction(Instruction::typedMnemonic(isInt, Instruction::Mnemonic::STI)); instr->operand_dest(regPtr, NULL, VREG_INT); instr->operand_src1(initVal()->getReg(), NULL, initVal()->reg_type()); list->addInstruction(instr); } } return; }