ScriptStack 1.0.4
Loading...
Searching...
No Matches
Executable.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4using System.Text;
5
7
9{
10
14 public class Executable
15 {
16
17 #region Private Variables
18
19 private Script script;
20 private List<Instruction> instructions;
21 private Dictionary<String, Function> functions;
22 private Memory scriptMemory;
23
24 #endregion
25
26 #region Private Methods
27
28 private void Clean(Operand operand)
29 {
30
31 if (operand == null)
32 return;
33
34 if (operand.Type != OperandType.InstructionPointer)
35 return;
36
37 Instruction instruction = operand.InstructionPointer;
38
39 if (instruction.OpCode != OpCode.NOP && instruction.OpCode != OpCode.DBG)
40 return;
41
42 Instruction nextInstruction = instruction;
43
44 while (nextInstruction.OpCode == OpCode.NOP || nextInstruction.OpCode == OpCode.DBG)
45 nextInstruction = instructions[(int)nextInstruction.Address + 1];
46
47 operand.InstructionPointer = nextInstruction;
48
49 }
50
51 #endregion
52
53 #region Internal Methods
54
55 internal void Clean()
56 {
57
58 Sort();
59
60 foreach (Instruction instruction in instructions)
61 {
62 Clean(instruction.First);
63 Clean(instruction.Second);
64 }
65
66 foreach (Function function in functions.Values)
67 {
68
69 Instruction instruction = function.EntryPoint;
70
71 Instruction nextInstruction = instruction;
72
73 while (nextInstruction.OpCode == OpCode.NOP || nextInstruction.OpCode == OpCode.DBG)
74 nextInstruction = instructions[(int)nextInstruction.Address + 1];
75
76 function.EntryPoint = nextInstruction;
77
78 }
79
80 for (int i = instructions.Count - 1; i >= 0; i--)
81 if (instructions[i].OpCode == OpCode.NOP)
82 instructions.RemoveAt(i);
83
84 Sort();
85
86 }
87
88 internal void Sort()
89 {
90 for (int i = 0; i < instructions.Count; i++)
91 instructions[i].Address = (uint)i;
92 }
93
94 internal List<Instruction> InstructionsInternal
95 {
96 get { return instructions; }
97 }
98
99 #endregion
100
101 #region Public Methods
102
103 public Executable(Script script) {
104
105 this.script = script;
106
107 instructions = new List<Instruction>();
108
109 functions = new Dictionary<string, Function>();
110
111 scriptMemory = Memory.AllocateScriptMemory(script.Manager.SharedMemory);
112
113 }
114
115 public bool FunctionExists(string function)
116 {
117
118 return functions.ContainsKey(function);
119
120 }
121
122 #endregion
123
124 #region Public Properties
125
130 {
131 get { return script; }
132 }
133
134 public ReadOnlyCollection<Instruction> Runnable
135 {
136 get { return instructions.AsReadOnly(); }
137 }
138
139 public Dictionary<String, Function> Functions
140 {
141 get { return functions; }
142 }
143
145 {
146
147 get
148 {
149
150 if (!functions.ContainsKey("main"))
151 throw new ParserException("Das Script hat keinen Einstiegspunkt.");
152
153 return functions["main"];
154
155 }
156
157 }
158
160 {
161 get { return scriptMemory; }
162 }
163
164 #endregion
165
166 }
167
168}
Script Script
Access the script from which the executable was created.
bool FunctionExists(string function)
Dictionary< String, Function > Functions
ReadOnlyCollection< Instruction > Runnable
A function, forward declared in a script.
Definition Function.cs:15
An instruction in a virtual intermediate language.
static Memory AllocateScriptMemory(Memory sharedMemory)
Definition Memory.cs:60
Instruction InstructionPointer
Definition Operand.cs:242