protected string[] generatePopFromStackToSegment(vmILParser.vmMemSegments segment, string IndexOperand, InstructionData currentVMinstruction) { var output = new List <String>(); if (segment == vmMemSegments._static) { output.AddRange(generateDecrement(stackPointer_symbol)); output.Add(assembler.CommandType.LOADAATPOINTER.ToString()); output.Add(stackPointer_symbol); output.Add(assembler.CommandType.STOREA.ToString()); output.Add($"STATIC{currentVMinstruction.VMFilePath}.{currentVMinstruction.VMFunction}.{IndexOperand}"); } else { //offset the base address of the symbol by the index output.AddRange(generateIncrement(this.vmSegmentToSymbolName[segment], IndexOperand, updateSymbol: false)); output.AddRange(generateMoveAtoTemp()); output.AddRange(generateDecrement(stackPointer_symbol)); output.Add(assembler.CommandType.LOADAATPOINTER.ToString()); output.Add(stackPointer_symbol); output.Add(assembler.CommandType.STOREAATPOINTER.ToString()); output.Add(temp_symbol); } return(output.ToArray()); }
protected string[] generatePushToStackFromSegment(vmILParser.vmMemSegments segment, string index, InstructionData currentVMinstruction) { //if we are writing from local to stack - we need to read from memory at the base address //pointed to by the local symbol which is stored at LCL //flow is LoadAFromPointer LCL - lets assume LCL holds the number 100 - which is the base address for the LCL segment //then increment A by index //store this in temp var output = new List <String>(); //add LCL and index and store in A //static is different from other segments //we need to read a value from the symbol VMFILE.VMFUNCTION.INDEX if (segment == vmMemSegments._static) { output.Add(assembler.CommandType.LOADA.ToString()); output.Add($"STATIC{currentVMinstruction.VMFilePath}.{currentVMinstruction.VMFunction}.{index}"); } //if constant, then just load the constant specified into A. else if (segment == vmMemSegments.constant) { output.Add(assembler.CommandType.LOADAIMMEDIATE.ToString()); output.Add(index); } else { output.AddRange(generateIncrement(vmSegmentToSymbolName[segment], index, updateSymbol: false)); output.Add(assembler.CommandType.STOREA.ToString()); output.Add(temp_symbol + " + 2"); output.Add(assembler.CommandType.LOADAATPOINTER.ToString()); output.Add(temp_symbol + " + 2"); } //store it on the stack, and increment SP output.Add(assembler.CommandType.STOREAATPOINTER.ToString()); output.Add(stackPointer_symbol); output.AddRange(generateIncrement(stackPointer_symbol)); return(output.ToArray()); }