Exemplo n.º 1
0
        public List <Opcode> BuildProgram()
        {
            var program = new List <Opcode>();

            foreach (var objectFile in objectFiles.Values)
            {
                var linkedObject = new CodePart();

                foreach (var part in objectFile.Parts)
                {
                    AddInitializationCode(linkedObject, part);
                    linkedObject.FunctionsCode.AddRange(part.FunctionsCode);
                    linkedObject.MainCode.AddRange(part.MainCode);
                }

                // we assume that the first object is the main program and the rest are subprograms/libraries
                bool isMainProgram = (objectFile == objectFiles.Values.First());
                // add a jump to the entry point so the execution skips the functions code
                if (isMainProgram)
                {
                    AddJumpToEntryPoint(linkedObject);
                }
                // add an instruction to indicate the end of the program
                AddEndOfProgram(linkedObject, isMainProgram);
                // save the entry point of the object
                objectFile.EntryPointLabel = GetEntryPointLabel(linkedObject);
                // add the linked object to the final program
                program.AddRange(linkedObject.MergeSections());
            }

            // replace all the labels references with the corresponding address
            ReplaceLabels(program);

            return(program);
        }
Exemplo n.º 2
0
        public List<Opcode> BuildProgram()
        {
            List<Opcode> program = new List<Opcode>();

            foreach (var objectFile in _objectFiles.Values)
            {
                CodePart linkedObject = new CodePart();

                foreach (var part in objectFile.Parts)
                {
                    AddInitializationCode(linkedObject, part);
                    linkedObject.FunctionsCode.AddRange(part.FunctionsCode);
                    linkedObject.MainCode.AddRange(part.MainCode);
                }

                // we assume that the first object is the main program and the rest are subprograms/libraries
                bool isMainProgram = (objectFile == _objectFiles.Values.First());
                // add a jump to the entry point so the execution skips the functions code
                if (isMainProgram)
                    AddJumpToEntryPoint(linkedObject);
                // add an instruction to indicate the end of the program
                AddEndOfProgram(linkedObject, isMainProgram);
                // save the entry point of the object
                objectFile.EntryPointLabel = GetEntryPointLabel(linkedObject);
                // add the linked object to the final program
                program.AddRange(linkedObject.MergeSections());
            }

            // replace all the labels references with the corresponding address
            ReplaceLabels(program);

            return program;
        }
Exemplo n.º 3
0
 private void AddJumpToEntryPoint(CodePart linkedObject)
 {
     if (linkedObject.MainCode.Count > 0)
     {
         OpcodeBranchJump jumpOpcode = new OpcodeBranchJump();
         jumpOpcode.DestinationLabel = GetEntryPointLabel(linkedObject);
         linkedObject.FunctionsCode.Insert(0, jumpOpcode);
     }
 }
Exemplo n.º 4
0
 protected virtual void AddEndOfProgram(CodePart linkedObject, bool isMainProgram)
 {
     if (isMainProgram)
     {
         linkedObject.MainCode.Add(new OpcodeEOP());
     }
     else
     {
         linkedObject.MainCode.Add(new OpcodeReturn());
     }
 }
Exemplo n.º 5
0
 protected virtual void AddEndOfProgram(CodePart linkedObject, bool isMainProgram)
 {
     if (isMainProgram)
     {
         linkedObject.MainCode.Add(new OpcodeEOP());
     }
     else
     {
         linkedObject.MainCode.Add(new OpcodeReturn());
     }
 }
Exemplo n.º 6
0
        private string GetEntryPointLabel(CodePart linkedObject)
        {
            List <Opcode> codeSection = linkedObject.InitializationCode.Count > 0 ? linkedObject.InitializationCode : linkedObject.MainCode;

            return(codeSection[0].Label);
        }
Exemplo n.º 7
0
 protected virtual void AddInitializationCode(CodePart linkedObject, CodePart part)
 {
     linkedObject.InitializationCode.AddRange(part.InitializationCode);
 }
Exemplo n.º 8
0
 protected override void AddInitializationCode(CodePart linkedObject, CodePart part)
 {
     linkedObject.MainCode.AddRange(part.InitializationCode);
 }
Exemplo n.º 9
0
 protected override void AddEndOfProgram(CodePart linkedObject, bool isMainProgram)
 {
     linkedObject.MainCode.Add(new OpcodeEOF());
 }
Exemplo n.º 10
0
 private string GetEntryPointLabel(CodePart linkedObject)
 {
     List<Opcode> codeSection = linkedObject.InitializationCode.Count > 0 ? linkedObject.InitializationCode : linkedObject.MainCode;
     return codeSection[0].Label;
 }
Exemplo n.º 11
0
 private void AddJumpToEntryPoint(CodePart linkedObject)
 {
     if (linkedObject.MainCode.Count > 0)
     {
         OpcodeBranchJump jumpOpcode = new OpcodeBranchJump();
         jumpOpcode.DestinationLabel = GetEntryPointLabel(linkedObject);
         linkedObject.FunctionsCode.Insert(0, jumpOpcode);
     }
 }
Exemplo n.º 12
0
 protected virtual void AddInitializationCode(CodePart linkedObject, CodePart part)
 {
     linkedObject.InitializationCode.AddRange(part.InitializationCode);
 }
Exemplo n.º 13
0
 protected override void AddInitializationCode(CodePart linkedObject, CodePart part)
 {
     linkedObject.MainCode.AddRange(part.InitializationCode);
 }
Exemplo n.º 14
0
 protected override void AddEndOfProgram(CodePart linkedObject, bool isMainProgram)
 {
     linkedObject.MainCode.Add(new OpcodeEOF());
 }