// 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); }