| 1 | // StackFrameFSM.cpp: implementation of the StackFrameFSM class. |
| 2 | /* |
| 3 | Pep8-1 is a virtual machine for writing machine language and assembly |
| 4 | language programs. |
| 5 | |
| 6 | Copyright (C) 2009 J. Stanley Warford, Pepperdine University |
| 7 | |
| 8 | This program is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include "stackframefsm.h" |
| 23 | #include "sim.h" |
| 24 | #include "pep.h" |
| 25 | |
| 26 | // #include <QDebug> |
| 27 | |
| 28 | StackFrameFSM::StackFrameFSM() |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | void StackFrameFSM::reset() |
| 34 | { |
| 35 | stackState = EStart; |
| 36 | numCellsFromSubSP = 0; |
| 37 | numCellsFromCall = 0; |
| 38 | } |
| 39 | |
| 40 | // the states: EStart, ESubSP, ECall |
| 41 | |
| 42 | int StackFrameFSM::makeTransition(int numCellsToAdd) |
| 43 | { |
| 44 | Enu::EMnemonic mnemon = Pep::decodeMnemonic[Sim::instructionSpecifier]; |
| 45 | |
| 46 | switch(stackState) |
| 47 | { |
| 48 | case(EStart): |
| 49 | if (mnemon == Enu::SUBSP) { |
| 50 | numCellsFromSubSP = numCellsToAdd; |
| 51 | stackState = ESubSP; |
| 52 | } |
| 53 | else if (mnemon == Enu::CALL) { |
| 54 | numCellsFromSubSP = 0; |
| 55 | numCellsFromCall = 1; // = numCellsToAdd; // ECall = 1 |
| 56 | stackState = ECall; |
| 57 | } |
| 58 | break; |
| 59 | case(ESubSP): |
| 60 | if (mnemon == Enu::CALL) { |
| 61 | numCellsFromCall = 1; // = numCellsToAdd; // ECall = 1 |
| 62 | stackState = ECall; |
| 63 | } |
| 64 | else { // not ECall |
| 65 | stackState = EStart; |
| 66 | return numCellsFromSubSP; // lone subsp |
| 67 | } |
| 68 | break; |
| 69 | case(ECall): |
| 70 | if (mnemon == Enu::SUBSP) { // function with 1 or more locals and 0 or more parameters |
| 71 | stackState = EStart; |
| 72 | return (numCellsFromSubSP + numCellsFromCall + numCellsToAdd); |
| 73 | } |
| 74 | else { // not ESubSP |
| 75 | stackState = EStart; // no locals |
| 76 | return (numCellsFromSubSP + numCellsFromCall); |
| 77 | } |
| 78 | break; |
| 79 | default:; |
| 80 | } |
| 81 | return 0; // don't add a frame yet |
| 82 | } |