// Gens op codes for if statement, and following block. // // Returns: Number of bytes generated. public override int GenOpCodes(OpCodeGenParam param) { // Inits int bytes = 0; int bytes2 = 0; VarTableEntry varEntry = null; try { // Gen op codes for boolean exp ( results in accum ) bytes += expr.GenOpCodes(param); // Create new temp var varEntry = param.tables.CreateTempVarTableEntry(); // Set in use flag varEntry.InUse = true; // Increment in use count param.tables.IncVarIsUseCount(); // Move results into temp memory param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Load 1 into accum param.AddBytes(0xA2, 0x01); // Compare temp (res of expr) to true (1) param.AddBytes(0xEC); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Branch to end of block (using curBlockID, as gen op // will use this id and incremement it. The current // block at any time is really curBlockID - 1 ). param.AddBytes(0xD0); param.AddByteForUpdate('B', param.curBlockID); // Incrmeent bytes bytes += 10; // Set temp var not in use varEntry.InUse = false; // Decremeent in use count param.tables.DecVarInUseCount(); // Gen op codes for block bytes += block.GenOpCodes(param); } // Catch over 256 byte error, // and throw up catch (IndexOutOfRangeException ex) { throw ex; } // Return bytes added return(bytes); }
// Gens opp codes for int operation add. // Results are in the accum. // // Returns: Number of bytes generated. public override int GenOpCodes(OpCodeGenParam param) { // Inits VarTableEntry varEntry = null; int bytes = 0; int bytes2 = 0; try { // Load accummulator with constant param.AddBytes(0xA9, (byte)intVal.Value); // Get avaialbe stack location varEntry = param.tables.CreateTempVarTableEntry(); // Set in use flag varEntry.InUse = true; // Increment var in use flag param.tables.IncVarIsUseCount(); // Store accumulator in stack location param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Increment bytes bytes += 5; // Check if expr is not null if (expr != null) { // Gen op codes for expr bytes2 += expr.GenOpCodes(param); } // Else expr is null else { // Move 0 to accumulator param.AddBytes(0xA9, 0x00); // Increment bytes bytes2 += 2; } // Add temp var to accumulator param.AddBytes(0x6D); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Increment bytes bytes2 += 3; // Set temp var not in use varEntry.InUse = false; // Decrement counter param.tables.DecVarInUseCount(); } // Catch over 256 byte error, // and throw up catch (IndexOutOfRangeException ex) { throw ex; } // Return bytes added return(bytes + bytes2); }
// Gens op codes for while loop. // // Returns: Number of bytes generated. public override int GenOpCodes(OpCodeGenParam param) { // Inits int bytes = 0; int bytes2 = 0; int jumpByte = 0; int startByte = param.curByte; VarTableEntry varEntry = null; VarTableEntry varEntry2 = null; try { // Gen op codes for boolean exp ( results in accum ) bytes += expr.GenOpCodes(param); // Create new temp var varEntry = param.tables.CreateTempVarTableEntry(); // Set in use flag varEntry.InUse = true; // Increment in use count param.tables.IncVarIsUseCount(); // Move results into temp memory //param.opCodes.AppendFormat("8D V{0} 00 ", varEntry.VarID); param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Incrmeent bytes bytes += 3; // Capture current byte (where to jump to at end of loop) jumpByte = param.curByte + bytes; // Load 1 into accum //param.opCodes.Append("A2 01 "); param.AddBytes(0xA2, 0x01); // Compare temp (res of expr) to true (1) //param.opCodes.AppendFormat("EC V{0} 00 ", varEntry.VarID); param.AddBytes(0xEC); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Branch to end of block, and 12 more to skip // forced jump to begining of while //param.opCodes.AppendFormat("D0 B{0}S{1} ", param.curBlockID, 12); param.AddBytes(0xD0); param.AddByteForUpdate('B', param.curBlockID, 12); // Incrmeent bytes bytes += 7; // Set current byte (for calling gen op) //param.curByte += 10; // Set temp var not in use varEntry.InUse = false; // Decremeent in use count param.tables.DecVarInUseCount(); // Gen op codes for block bytes2 += block.GenOpCodes(param); // ### Force NE Jump ### // // Create another new temp var varEntry2 = param.tables.CreateTempVarTableEntry(); // Set in use varEntry2.InUse = true; // Inc var in use count param.tables.IncVarIsUseCount(); // Load 0 into acc //param.opCodes.Append("A9 00 "); param.AddBytes(0xA9, 0x00); // Load acc to temp var //param.opCodes.AppendFormat("8D V{0} 00 ", varEntry2.VarID); param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntry2.VarID); param.AddBytes(0x00); // Load 1 into x reg //param.opCodes.Append("A2 01 "); param.AddBytes(0xA2, 0x01); // Compare temp to x (which will always be not equal ) //param.opCodes.AppendFormat("EC V{0} 00 ", varEntry2.VarID); param.AddBytes(0xEC); param.AddByteForUpdate('V', varEntry2.VarID); param.AddBytes(0x00); // Incremeent bytes bytes2 += 12; // Update bytes ( I do it before next statmement for // caculation of accurate curByte ) //param.curByte += 12; // Branch to top of while loop ( note these two bytes already // added to curByte ) //param.opCodes.AppendFormat("D0 {0} ",(256 - param.curByte + startByte + 1).ToString("X2")); param.AddBytes(0xD0, (byte)(255 - (param.curByte - startByte + 1))); // Set temp var not in use varEntry2.InUse = false; // Decremeent in use count param.tables.DecVarInUseCount(); } // Catch over 256 byte error, // and throw up catch (IndexOutOfRangeException ex) { throw ex; } // Return bytes added return(bytes + bytes2); }
// Gens op codes for print statment. // Calls system to print based on type of expr. // // Returns: Number of bytes generated. public override int GenOpCodes(OpCodeGenParam param) { // Inits int bytes = 0; VarTableEntry varEntry = null; VarTableEntry varEntryTrue = null; VarTableEntry varEntryFalse = null; HeapTableEntry heapEntryTrue = null; HeapTableEntry heapEntryFalse = null; IDASTNode idASTNode = null; try { // Gen op codes for expr. Results in accum bytes += expr.GenOpCodes(param); // Create temp var on stack varEntry = param.tables.CreateTempVarTableEntry(); varEntry.InUse = true; param.tables.IncVarIsUseCount(); // Load accumlator to memory (results from expr ) //param.opCodes.AppendFormat("8D V{0} 00 ", varEntry.VarID); param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); bytes += 3; // Inc cur bytes //param.curByte += 3; // Check if int expr, or var dec of type int if ((expr is IntExprASTNode) || ((expr is IDASTNode) && (((IDASTNode)expr).SymbolTableEntry.DataType == DataType.DT_INT))) { // Load 1 int x reg //param.opCodes.Append("A2 01 "); param.AddBytes(0xA2, 0x01); // Load into y register //param.opCodes.AppendFormat("AC V{0} 00 ",varEntry.VarID); param.AddBytes(0xAC); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Make system call //param.opCodes.Append("FF "); param.AddBytes(0xFF); // Inc bytes bytes += 6; // Inc cur bytes // param.curByte += 6; } // Else check if bool expr, or var dec with boolean type else if ((expr is BooleanExprASTNode) || ((expr is IDASTNode) && (((IDASTNode)expr).SymbolTableEntry.DataType == DataType.DT_BOOLEAN))) { // Create strings for true and false on heap. // If already on their it wont allocate new // ones, but use the same. heapEntryTrue = param.tables.AddHeapTableEntry(param, "true"); heapEntryFalse = param.tables.AddHeapTableEntry(param, "false"); // Create temp vars on stack for true varEntryTrue = param.tables.CreateTempVarTableEntry(); varEntryTrue.InUse = true; param.tables.IncVarIsUseCount(); // Create temp var on stack for false varEntryFalse = param.tables.CreateTempVarTableEntry(); varEntryFalse.InUse = true; param.tables.IncVarIsUseCount(); // Load true string address in acc //param.opCodes.AppendFormat("A9 H{0} ", heapEntryTrue.HeapID); param.AddBytes(0xA9); param.AddByteForUpdate('H', heapEntryTrue.HeapID); // Move to temp var //param.opCodes.AppendFormat("8D V{0} 00 ", varEntryTrue.VarID); param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntryTrue.VarID); param.AddBytes(0x00); // Load true add in acc //param.opCodes.AppendFormat("A9 H{0} ", heapEntryFalse.HeapID); param.AddBytes(0xA9); param.AddByteForUpdate('H', heapEntryFalse.HeapID); // Move to temp var //param.opCodes.AppendFormat("8D V{0} 00 ", varEntryFalse.VarID); param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntryFalse.VarID); param.AddBytes(0x00); // Load false into y reg //param.opCodes.AppendFormat("AC V{0} 00 ", varEntryFalse.VarID); param.AddBytes(0xAC); param.AddByteForUpdate('V', varEntryFalse.VarID); param.AddBytes(0x00); // Load 01 int x reg //param.opCodes.Append("A2 01 "); param.AddBytes(0xA2, 0x01); // Compare results with results from acc //param.opCodes.AppendFormat("EC V{0} 00 ", varEntry.VarID); param.AddBytes(0xEC); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Branch on not equal x bytes //param.opCodes.Append("D0 04 "); param.AddBytes(0xD0, 0x03); // Load true into y reg //param.opCodes.AppendFormat("AC V{0} 00 ", varEntryTrue.VarID); param.AddBytes(0xAC); param.AddByteForUpdate('V', varEntryTrue.VarID); param.AddBytes(0x00); // Load 2 int x reg //param.opCodes.Append("A2 02 "); param.AddBytes(0xA2, 0x02); // Make system call //param.opCodes.Append("FF "); param.AddBytes(0xFF); // Inc bytes bytes += 26; // Inc cur bytes //param.curByte += 26; /* * // Set temp vars not in use * varEntryTrue.InUse = false; * varEntryFalse.InUse = false; * param.tables.DecVarInUseCount(); * param.tables.DecVarInUseCount(); * */ } // Else check if string expr or var of string type else if ((expr is StringExprASTNode) || ((expr is IDASTNode) && (((IDASTNode)expr).SymbolTableEntry.DataType == DataType.DT_STRING))) { // Load into temp var //param.opCodes.AppendFormat("8D // Load expr res into y reg //param.opCodes.AppendFormat("AC V{0} 00 ", varEntry.VarID); param.AddBytes(0xAC); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Load 2 int x reg //param.opCodes.Append("A2 02 "); param.AddBytes(0xA2, 0x02); // Make system call //param.opCodes.Append("FF "); param.AddBytes(0xFF); // Increment bytes bytes += 6; // Inc cur bytes //param.curByte += 6; } // Reset temp var varEntry.InUse = false; param.tables.DecVarInUseCount(); } // Catch over 256 byte error, // and throw up catch (IndexOutOfRangeException ex) { throw ex; } // Return bytes added return(bytes); }
// Gens op codes for comparision of boolean expr == or !=, // and puts results in accum. 1 for true, 0 for false. // // Returns: Number of bytes generated. public override int GenOpCodes(OpCodeGenParam param) { // Inits VarTableEntry varEntryTemp = null; int bytes = 0; int index = 0; // ### Checking Expr 1 ### // // try for error try { // Gen op codes for expr one bytes += exprOne.GenOpCodes(param); // ### Check if not only bool val, as if it was ### // // ### the value is just left in the accumulator ### // // Check if expr 2 is not null if (exprTwo != null) { // ### Add acc results from expr 1 ### // // Add temp var for expr one results varEntryTemp = param.tables.CreateTempVarTableEntry(); // Set in use varEntryTemp.InUse = true; // Increment var in use param.tables.IncVarIsUseCount(); // Move value of accumulator to temp var param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntryTemp.VarID); param.AddBytes(0x00); // ### Generate expr 2 value and put in accumm ### // // Gen op codes for expr two bytes += exprTwo.GenOpCodes(param); // ### Compare values of temp var (expr 1), and accum (expr2) ### // // ### Put results in accumulator. ### // // Load temp value from first expr into reg x param.AddBytes(0xAE); param.AddByteForUpdate('V', varEntryTemp.VarID); param.AddBytes(0x00); // Move accum into same temp val param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntryTemp.VarID); param.AddBytes(0x00); // Increment bytes bytes += 9; // Check if equals if (value == BOOLOP_TYPE.BOOLOP_EQUALS) { // Set false in accum "A9 00 " param.AddBytes(0xA9, 0x00); } else { // Set true in accum param.AddBytes(0xA9, 0x01); } // Compare x to temp var param.AddBytes(0xEC); param.AddByteForUpdate('V', varEntryTemp.VarID); param.AddBytes(0x00); // Branch 2 bytes if equl param.AddBytes(0xD0, 0x02); // Check if equals if (value == BOOLOP_TYPE.BOOLOP_EQUALS) { // Set true in accum (skip in nnot equal) param.AddBytes(0xA9, 0x01); } else { // Set false in accum (skip in nnot equal) param.AddBytes(0xA9, 0x00); } // Increment bytes bytes += 9; // Reset in use flag for temp var varEntryTemp.InUse = false; // Decremeent in use flag param.tables.DecVarInUseCount(); } } // Throw error up catch (IndexOutOfRangeException ex) { throw ex; } // Return number of bytes return(bytes); }
// Generates op codes for expr, which is put in accum. // This value is then loaded into var's value. // // Returns: Number of op code bytes added. public override int GenOpCodes(OpCodeGenParam param) { // Inits VarTableEntry varEntry = null; InitValueEntry initEntry = null; int bytes = 0; int curByte = param.curByte; bool value = false; StringExprASTNode stringNode = null; HeapTableEntry heapEntry = null; bool initVar = false; // Try for over 256 bytes try { // Get init value entry initEntry = param.tables.GetLastInitValueEntry(id.Value, id.SymbolTableEntry.DataType); // Check if value was already init if (initEntry != null) { // Set init var initVar = true; // Check if int value if (expr is IntValASTNode) { // Set byte with value param.SetByte(initEntry.Index, (byte)((IntValASTNode)expr).Value); // Set inital value in symbol table id.SymbolTableEntry.IntValue = ((IntValASTNode)expr).Value; } else if (expr is BoolValASTNode) { // Get bool value if (((BoolValASTNode)expr).Value == BOOLVAL_TYPE.BOOLVAL_TRUE) { value = true; } else { value = false; } // Set byte with value param.SetByte(initEntry.Index, (byte)(value == true ? 1 : 0)); // Set inital value in symbol table id.SymbolTableEntry.IntValue = (value == true ? 1 : 0); } else if (expr is StringExprASTNode) { stringNode = ((StringExprASTNode)expr); heapEntry = param.tables.AddHeapTableEntry(param, stringNode.Value); param.AddInsertByte(initEntry.Index, 'H', heapEntry.HeapID, 0); id.SymbolTableEntry.StringValue = stringNode.Value; } else { initVar = false; } // Remove from table if iniitalized if (initVar) { param.tables.RemoveLastInitValueEntry(initEntry); param.initVars++; } } // Check if initialized optimization if (!initVar) { // Gen op codes for expr , value in accum bytes += expr.GenOpCodes(param); // Retreive temp placehold from var table varEntry = param.tables.GetVarTableEntry(id.SymbolTableEntry.EntryID); // Move accumlator to memory "8D V{0} 00 " param.AddBytes(0x8D); param.AddByteForUpdate('V', varEntry.VarID); param.AddBytes(0x00); // Increment bytes bytes += 3; } } // Catch over 256 bytes catch (IndexOutOfRangeException ex) { throw ex; } // Return bytesa added return(bytes); }