-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
285 lines (268 loc) · 12.9 KB
/
Program.cs
File metadata and controls
285 lines (268 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System.Diagnostics;
using System.Text;
using static Epsilon.NodeBinExpr;
using static Epsilon.NodeTermUnaryExpr;
namespace Epsilon
{
internal sealed class Program
{
static StringBuilder Compile(string InputFilePath, bool Optimize)
{
string InputCode = Shartilities.ReadFile(InputFilePath);
List<Token> TokenizedProgram = new Tokenizer(InputCode, InputFilePath, []).TokenizeProg();
NodeProg ParsedProgram = new Parser(TokenizedProgram, InputFilePath).ParseProg();
//StringBuilder Before = Arborist.CutProgram(ParsedProgram);
//Shartilities.WriteFile("./Before.e", Before.ToString(), false);
if (Optimize) Optimizer.OptimizeProgram(ref ParsedProgram);
//StringBuilder After = Arborist.CutProgram(ParsedProgram);
//Shartilities.WriteFile("./After.e", After.ToString(), false);
StringBuilder GeneratedProgram = Generator.GenProgram(ParsedProgram, InputFilePath);
return GeneratedProgram;
}
static void AssembleAndLinkForQemu(string SourceFilePath, string OutputFilePath)
{
Shartilities.Command cmd = new(["riscv64-linux-gnu-gcc","-o", OutputFilePath,SourceFilePath,"-static"]);
Process? p = null;
if (!cmd.RunSyncRealTime(ref p, out string stdout, out string stderr)) Environment.Exit(p!.ExitCode);
}
static void CompileAssembleLinkForQemu(string SourceFilePath, string OutputFilePath, bool Optimize, bool Dump)
{
bool IsAssemblyFile = SourceFilePath.EndsWith(".S");
if (IsAssemblyFile)
{
AssembleAndLinkForQemu(SourceFilePath, OutputFilePath);
}
else
{
string TempAssembly = OutputFilePath + ".S";
{
StringBuilder Assembly = Compile(SourceFilePath, Optimize);
Shartilities.WriteFile(TempAssembly, Assembly.ToString(), false);
}
AssembleAndLinkForQemu(TempAssembly, OutputFilePath);
if (!Dump && File.Exists(TempAssembly))
File.Delete(TempAssembly);
}
}
static void RunOnQemu(string FilePath, List<string> ClArgs)
{
StringBuilder clargs = new();
foreach (string arg in ClArgs) clargs.Append(arg + ' ');
Shartilities.Command cmd = new(["qemu-riscv64", FilePath, clargs.ToString()]);
Process? p = null;
cmd.RunSyncRealTime(ref p, out string stdout, out string stderr);
Environment.Exit(p!.ExitCode);
}
static LibUtils.Program AssembleAndLinkForCAS(string SourceFilePath, string OutputFilePath, uint IM_SIZE, uint DM_SIZE, bool Generate)
{
LibUtils.Program p = Assembler.Assembler.AssembleProgram(SourceFilePath, false);
if (Generate)
{
string IM_INIT_filepath = $"{OutputFilePath}_IM_INIT.INIT";
string DM_INIT_filepath = $"{OutputFilePath}_DM_INIT.INIT";
string MC_filepath = $"{OutputFilePath}_MC.txt";
string DM_filepath = $"{OutputFilePath}_DM.txt";
string IM_MIF_filepath = $"{OutputFilePath}_IM_MIF.mif";
string DM_MIF_filepath = $"{OutputFilePath}_DM_MIF.mif";
StringBuilder IM_INIT = LibUtils.GetIM_INIT(p.MachineCodes, p.Instructions);
Shartilities.WriteFile(IM_INIT_filepath, IM_INIT.ToString(), false, 1);
StringBuilder DM_INIT = LibUtils.GetDM_INIT(p.DataMemoryValues);
Shartilities.WriteFile(DM_INIT_filepath, DM_INIT.ToString(), false, 1);
List<string> IM = LibUtils.GetIM(p.MachineCodes);
File.WriteAllLines(MC_filepath, IM);
List<string> DM = LibUtils.ParseDataMemoryValues(p.DataMemoryValues);
File.WriteAllLines(DM_filepath, DM);
Shartilities.WriteFile(IM_MIF_filepath, LibUtils.GetIMMIF(p.MachineCodes, IM_SIZE).ToString(), false, 1);
Shartilities.WriteFile(DM_MIF_filepath, LibUtils.GetDMMIF(p.DataMemoryValues, DM_SIZE).ToString(), false, 1);
}
return p;
}
static LibUtils.Program CompileAssembleLinkForCAS(string SourceFilePath, string OutputFilePath, bool Optimize, uint IM_SIZE, uint DM_SIZE, bool Dump)
{
LibUtils.Program p = new();
bool IsAssemblyFile = SourceFilePath.EndsWith(".S");
if (IsAssemblyFile)
{
p = AssembleAndLinkForCAS(SourceFilePath, OutputFilePath, IM_SIZE, DM_SIZE, Dump);
}
else
{
string TempAssembly = OutputFilePath + ".S";
{
StringBuilder Assembly = Compile(SourceFilePath, Optimize);
Shartilities.WriteFile(TempAssembly, Assembly.ToString(), false);
}
p = AssembleAndLinkForCAS(TempAssembly, OutputFilePath, IM_SIZE, DM_SIZE, Dump);
if (!Dump && File.Exists(TempAssembly))
File.Delete(TempAssembly);
}
return p;
}
static void SimOnCAS(LibUtils.Program p, List<string> ClArgs, uint IM_SIZE, uint DM_SIZE)
{
List<string> MC = LibUtils.GetIM(p.MachineCodes);
List<string> DM = LibUtils.ParseDataMemoryValues(p.DataMemoryValues);
LibCPU.SingleCycle.Run(MC, DM, IM_SIZE, DM_SIZE, ClArgs, null);
}
static void Usage()
{
Console.WriteLine($"Usage:");
Console.WriteLine($" {Environment.ProcessPath} [options] <input_file>");
Console.WriteLine();
Console.WriteLine($"Options:");
Console.WriteLine($" -o <file> Specify output file path (default: ./a or ./a.S for -S)");
Console.WriteLine($" -S Compile only; generate assembly and exit");
Console.WriteLine($" -run Compile, assemble, link, and run using QEMU");
Console.WriteLine($" -sim Compile, assemble, and simulate using the custom simulator (CAS)");
Console.WriteLine($" -h Display this usage information");
Console.WriteLine($" -dump don't erase any generated files during compilation in general");
Console.WriteLine($" -O enable optimization");
Console.WriteLine($" -imsize <size> set instruction memory size for CAS (default: {1 << 14})");
Console.WriteLine($" -dmsize <size> set data memory size for CAS (default: {1 << 14})");
Console.WriteLine($" -v enable verbose logging");
Console.WriteLine();
Console.WriteLine($"Notes:");
Console.WriteLine($" - Default behavior (no -S/-run/-sim) compiles, assembles, links, and generates the executable, and files needed by the CAS.");
}
static void Main(string[] args)
{
//Compile("../../../main/main.e", false);
// TODO:
// - deal with multiple files
// - if you start with .e file you interpret the rest as epsilon files and you start from the compile step
// - if you start with .S file you interpret the rest as assembly files and you start from the assemble step
// dotnet ../Epsilon/bin/Debug/net8.0/Epsilon.dll -o ./hw/singlecycle/test/Generated/test -dump -sim ./hw/singlecycle/test/Generated/test.S
//string? OutputFilePath = null;
#if false
string? OutputFilePath = "../../../../risc-v-CPUs/hw/singlecycle/test/Generated/test";
List<string> ClArgs = ["../../../../risc-v-CPUs/hw/singlecycle/test/Generated/test.S"];
bool Sim = true; // simulate on my cycle accurate simulator (CAS)
uint IM_SIZE = 1 << 14;
uint DM_SIZE = 1 << 14;
bool Optimize = false;
bool CompileOnly = false;
bool Run = false; // Run on qemu
bool Dump = true;
bool verbose = false;
#else
string? OutputFilePath = null;
uint IM_SIZE = 1 << 14;
uint DM_SIZE = 1 << 14;
List<string> ClArgs = [];
bool Optimize = false;
bool CompileOnly = false;
bool Run = false; // Run on qemu
bool Sim = false; // simulate on my cycle accurate simulator (CAS)
bool Dump = false;
bool verbose = false;
while (Shartilities.ShiftArgs(ref args, out string arg))
{
if (arg == "-o")
{
if (!Shartilities.ShiftArgs(ref args, out string OutputFilePathuser))
Shartilities.Log(Shartilities.LogType.ERROR, $"Expected output file path after -o\n", 1);
OutputFilePath = OutputFilePathuser;
}
else if (arg == "-O")
{
Optimize = true;
}
else if (arg == "-run")
{
Run = true;
while (Shartilities.ShiftArgs(ref args, out string clarg))
ClArgs.Add(clarg);
}
else if (arg == "-sim")
{
Sim = true;
while (Shartilities.ShiftArgs(ref args, out string clarg))
ClArgs.Add(clarg);
}
else if (arg == "-S")
{
CompileOnly = true;
}
else if (arg == "-dump")
{
Dump = true;
}
else if (arg == "-imsize")
{
if (!Shartilities.ShiftArgs(ref args, out string temp_IM_SIZE))
Shartilities.Log(Shartilities.LogType.ERROR, $"Missing argument \n", 1);
if (uint.TryParse(temp_IM_SIZE, out uint temp_size))
{
IM_SIZE = temp_size;
}
else
{
Shartilities.Log(Shartilities.LogType.ERROR, $"could not parse instruction memory size {temp_IM_SIZE}\n", 1);
return;
}
}
else if (arg == "-dmsize")
{
if (!Shartilities.ShiftArgs(ref args, out string temp_DM_SIZE))
Shartilities.Log(Shartilities.LogType.ERROR, $"Missing argument \n", 1);
if (uint.TryParse(temp_DM_SIZE, out uint temp_size))
{
DM_SIZE = temp_size;
}
else
{
Shartilities.Log(Shartilities.LogType.ERROR, $"could not parse instruction memory size {temp_DM_SIZE}\n", 1);
return;
}
}
else if (arg == "-v")
{
verbose = true;
}
else if (arg == "-h" || arg == "--h" || arg == "-help" || arg == "--help")
{
Usage();
Environment.Exit(0);
}
else
{
ClArgs.Add(arg);
}
}
#endif
if (ClArgs.Count == 0)
Shartilities.Logln(Shartilities.LogType.ERROR, $"no input files was provided", 1);
string SourceFilePath = ClArgs[0];
ClArgs.RemoveAt(0);
if (Run)
{
if (verbose) Console.WriteLine("running");
OutputFilePath ??= "./a";
CompileAssembleLinkForQemu(SourceFilePath, OutputFilePath, Optimize, Dump);
RunOnQemu(OutputFilePath, ClArgs);
}
else if (Sim)
{
if (verbose) Console.WriteLine("simulating");
OutputFilePath ??= "./a";
LibUtils.Program p = CompileAssembleLinkForCAS(SourceFilePath, OutputFilePath, Optimize, IM_SIZE, DM_SIZE, Dump);
ClArgs.Insert(0, OutputFilePath);
SimOnCAS(p, ClArgs, IM_SIZE, DM_SIZE);
}
else if (CompileOnly)
{
if (verbose) Console.WriteLine("compile only");
OutputFilePath ??= "./a.S";
StringBuilder Assembly = Compile(SourceFilePath, Optimize);
Shartilities.WriteFile(OutputFilePath, Assembly.ToString(), false);
}
else
{
if (verbose) Console.WriteLine("compiling and assembling");
OutputFilePath ??= "./a";
CompileAssembleLinkForQemu(SourceFilePath, OutputFilePath, Optimize, Dump);
LibUtils.Program p = CompileAssembleLinkForCAS(SourceFilePath, OutputFilePath, Optimize, IM_SIZE, DM_SIZE, true);
}
}
}
}