// Finds heap table entry based on string value. // // Returns: Heap entry with str value, or null if not in heap. public HeapTableEntry GetHeapTableEntry(String str) { // Inits HeapTableEntry entry = null; HeapTableEntry ret = null; bool found = false; // Cycle through entries for (int i = 0; ((i < heapTable.Count) && !found); i++) { // Get entry entry = (HeapTableEntry)heapTable[i]; // Check if str are equal if (entry.StringValue == str) { // Set ret ret = entry; // Set found flag found = true; } } // Return entry or null if not found return(ret); }
// Gens op codes to create string on heap, // and load address in accumulator. // // Returns: Number of bytes generated. public override int GenOpCodes(OpCodeGenParam param) { // Inits HeapTableEntry heapEntry = null; // Check if heap entry exists heapEntry = param.tables.AddHeapTableEntry(param, Value); try { // Load accumulator with heap address //param.opCodes.AppendFormat("A9 H{0} ", heapEntry.HeapID); param.AddBytes(0xA9); param.AddByteForUpdate('H', heapEntry.HeapID); } // Catch over 256 byte error, // and throw up catch (IndexOutOfRangeException ex) { throw ex; } // Increment bytes //param.curByte += 2; // Return cur num bytes return(2); }
// Find heap entry by index. // // Throws: IndexOutOfRangeException // Returns: Entry for given index. public HeapTableEntry GetHeapTableEntryByIndex(int index) { // Inits HeapTableEntry ret = null; // Check for errors if ((index >= heapTable.Count) || index < 0) { throw new IndexOutOfRangeException("OpCodeGen heap table out of range."); } else { ret = (HeapTableEntry)heapTable[index]; } // Return entry return(ret); }
// Gets total heap size. // // Returns: Total heap size. public int TotalHeapSize() { // Inits int heapSize = 0; HeapTableEntry entry = null; // Cycle through heap table for (int i = 0; i < heapTable.Count; i++) { // Get heap entry entry = (HeapTableEntry)heapTable[i]; // Calc total heapSize += entry.Length; } // Return heap size return(heapSize); }
// Adds heap table entry if one doesn't exist with same string. // If one does exits, returns heap table entry. // // Returns: New or existing heap table entry. public HeapTableEntry AddHeapTableEntry(OpCodeGenParam param, String str) { // Inits HeapTableEntry heapEntry = null; // Check if entry exists for that string in heap heapEntry = GetHeapTableEntry(str); // Check if entry doesn't exist if (heapEntry == null) { // Create new heap table entry heapEntry = new HeapTableEntry(param.curHeapID, str); // Incrment cur heap id param.curHeapID++; // Add heap entry to table heapTable.Add(heapEntry); } // Return heap table entry return(heapEntry); }
// 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); }
// Fills in variable locations, heap locations, and jump size for // blocks in one pase. Collected some meta data about program. // // Returns: OpCodeData describing memory of program. private OpCodeData FillInTempOpCodeValues(OpCodeGenParam param) { // Inits int zeros = 0; int num = 0; int localOffSet = 0; int curByte = 0; String[] strings = null; String s = null; String[] offStrings = null; String o = null; int len = 0; VarTableEntry varEntry = null; HeapTableEntry heapEntry = null; BlockSizeTableEntry blockEntry = null; OpCodeData programData = new OpCodeData(); TempByteData byteData = null; int location = 0; // Determin stack size programData.stackSize = param.tables.MaxVarUsage; programData.stackStart = param.curByte; // Determine heap size programData.heapSize = param.tables.TotalHeapSize(); programData.heapStart = param.curByte + programData.stackSize; // Write 00 over stack zeros = programData.stackSize; for (int i = 0; i < zeros; i++) { //param.opCodes.Append("00 "); param.AddBytes(0x00); } // Update size of file //param.curByte += zeros; // Send message SendGeneralMessage("Creating strings in memory..."); // Fill in memory locations for heap for (int i = 0; i < param.tables.HeapTableCount(); i++) { heapEntry = param.tables.GetHeapTableEntryByIndex(i); heapEntry.MemoryLocation = programData.heapStart + curByte; // Write char for (int n = 0; n < heapEntry.StringValue.Length; n++) { //param.opCodes.AppendFormat("{0} ", ((int)heapEntry.StringValue[n]).ToString("X2")); param.AddBytes((byte)heapEntry.StringValue[n]); } //param.opCodes.Append("00 "); param.AddBytes(0x00); //param.curByte += heapEntry.Length + 1; curByte += heapEntry.Length; } // Send message SendGeneralMessage("String creation complete."); // Set program data size programData.totalBytes = param.curByte; // Cycle through codes for (int i = 0; i < param.insertBytes.Count; i++) { // Get byte data byteData = (TempByteData)param.insertBytes[i]; // Get place holder string s = byteData.VarSymbol; // Check for V (var table if (s[0] == 'V') { // Get number off string num = int.Parse(s.Substring(1, s.Length - 1)); // Find entry varEntry = param.tables.GetVarTableEntry(num); // Replace entry with address if (varEntry != null) { // Set location location = programData.stackStart + varEntry.Offset; param.SetByte(byteData.Index, (byte)(location)); if (varEntry.VarEntry != null) { varEntry.VarEntry.MemoryLocation = location; } } } // Check for H (heap) else if (s[0] == 'H') { // Find length to s or end len = s.Length; for (int n = 1; n < s.Length; n++) { if (s[n] == 'S') { len = n; } } // Get number off string num = int.Parse(s.Substring(1, len - 1)); // Find entry heapEntry = param.tables.GetHeapTableEntry(num); // Split of F offStrings = s.Split('S'); // Get offf num if valid if (offStrings.Length > 1) { o = offStrings[1]; localOffSet = int.Parse(o.Substring(0, o.Length)); } else { localOffSet = 0; } // Replace entry with address if (heapEntry != null) { // strings[i] = String.Format("{0}", (heapEntry.MemoryLocation + localOffSet).ToString("X2")); param.SetByte(byteData.Index, (byte)(heapEntry.MemoryLocation + localOffSet)); } } // Else if block parse flag else if (s[0] == 'B') { // Find length to s or end len = s.Length; for (int n = 1; n < s.Length; n++) { if (s[n] == 'S') { len = n; } } // Get number off string num = int.Parse(s.Substring(1, len - 1)); // Find entry ( happens to work out that block id = index ) blockEntry = param.tables.GetBlockSizeTableEntry(num); // Split of F offStrings = s.Split('S'); // Get offf num if valid if (offStrings.Length > 1) { o = offStrings[1]; localOffSet = int.Parse(o.Substring(0, o.Length)); } else { localOffSet = 0; } // Replace entry with address if (blockEntry != null) { //strings[i] = String.Format("{0}", (blockEntry.BlockSize + localOffSet).ToString("X2")); param.SetByte(byteData.Index, (byte)(blockEntry.BlockSize + localOffSet)); } } } // Return program data return(programData); }
// 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); }