Ejemplo n.º 1
0
        private void ReplaceLabels(List<Opcode> program)
        {
            var labels = new Dictionary<string, int>();

            // get the index of every label
            for (int index = 0; index < program.Count; index++)
            {
                if (program[index].Label != string.Empty)
                {
                    if (labels.ContainsKey(program[index].Label))
                    {
                        if (program[index].Label.EndsWith("-default"))
                            continue;
                        // This is one of those "should never happen" errors that if it happens
                        // it means kOS devs screwed up - so dump the partially relabeled program
                        // to the log just to help in diagnosing the bug report that may happen:
                        //
                        Utilities.SafeHouse.Logger.LogError("=====Relabeled Program so far is: =========");
                        Utilities.SafeHouse.Logger.LogError(Utilities.Debug.GetCodeFragment(program));

                        throw new Exceptions.KOSCompileException(LineCol.Unknown(), string.Format(
                            "ProgramBuilder.ReplaceLabels: Cannot add label {0}, label already exists.  Opcode: {1}", program[index].Label, program[index].ToString()));
                    }
                    labels.Add(program[index].Label, index);
                }
            }

            // replace destination labels with the corresponding index
            for (int index = 0; index < program.Count; index++)
            {
                Opcode opcode = program[index];
                if (string.IsNullOrEmpty(opcode.DestinationLabel)) continue;

                if (!labels.ContainsKey(opcode.DestinationLabel))
                {
                    Utilities.SafeHouse.Logger.LogError("=====Relabeled Program so far is: =========");
                    Utilities.SafeHouse.Logger.LogError(Utilities.Debug.GetCodeFragment(program));

                    throw new Exceptions.KOSCompileException(LineCol.Unknown(), string.Format(
                        "ProgramBuilder.ReplaceLabels: Cannot find label {0}.  Opcode: {1}", opcode.DestinationLabel, opcode.ToString()));

                }
                int destinationIndex = labels[opcode.DestinationLabel];
                if (opcode is BranchOpcode)
                {
                    ((BranchOpcode)opcode).Distance = destinationIndex - index;
                }
                else if (opcode is OpcodePushRelocateLater)
                {
                    // Replace the OpcodePushRelocateLater with the proper OpcodePush:
                    Opcode newOp;
                    if (opcode is OpcodePushDelegateRelocateLater)
                    {
                        newOp = new OpcodePushDelegate(destinationIndex, ((OpcodePushDelegateRelocateLater)opcode).WithClosure);
                    }
                    else
                        newOp = new OpcodePush(destinationIndex);
                    newOp.SourcePath = opcode.SourcePath;
                    newOp.SourceLine = opcode.SourceLine;
                    newOp.SourceColumn = opcode.SourceColumn;
                    newOp.Label = opcode.Label;
                    program[index] = newOp;
                }
                else if (opcode is OpcodeCall)
                {
                    ((OpcodeCall)opcode).Destination = destinationIndex;
                }
            }

            // complete the entry point address of all the objects
            foreach (var objectFile in objectFiles.Values)
            {
                if (objectFile.EntryPointLabel != string.Empty)
                    objectFile.EntryPointAddress = labels[objectFile.EntryPointLabel];
            }
        }
Ejemplo n.º 2
0
        private void ReplaceLabels(List<Opcode> program)
        {
            var labels = new Dictionary<string, int>();

            // get the index of every label
            for (int index = 0; index < program.Count; index++)
            {
                if (program[index].Label != string.Empty)
                {
                    labels.Add(program[index].Label, index);
                }
            }

            // replace destination labels with the corresponding index
            for (int index = 0; index < program.Count; index++)
            {
                Opcode opcode = program[index];
                if (string.IsNullOrEmpty(opcode.DestinationLabel)) continue;

                int destinationIndex = labels[opcode.DestinationLabel];
                if (opcode is BranchOpcode)
                {
                    ((BranchOpcode)opcode).Distance = destinationIndex - index;
                }
                else if (opcode is OpcodePushRelocateLater)
                {
                    // Replace the OpcodePushRelocateLater with the proper OpcodePush:
                    Opcode newOp;
                    if (opcode is OpcodePushDelegateRelocateLater)
                    {
                        newOp = new OpcodePushDelegate(destinationIndex, ((OpcodePushDelegateRelocateLater)opcode).WithClosure);
                    }
                    else
                        newOp = new OpcodePush(destinationIndex);
                    newOp.SourceName = opcode.SourceName;
                    newOp.SourceLine = opcode.SourceLine;
                    newOp.SourceColumn = opcode.SourceColumn;
                    newOp.Label = opcode.Label;
                    program[index] = newOp;
                }
                else if (opcode is OpcodeCall)
                {
                    ((OpcodeCall)opcode).Destination = destinationIndex;
                }
            }

            // complete the entry point address of all the objects
            foreach (var objectFile in objectFiles.Values)
            {
                if (objectFile.EntryPointLabel != string.Empty)
                    objectFile.EntryPointAddress = labels[objectFile.EntryPointLabel];
            }
        }