-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBFCompiler.cs
176 lines (150 loc) · 7.37 KB
/
BFCompiler.cs
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
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Reflection;
namespace bfnetc
{
public static class BFCompiler
{
private readonly static Stack<System.Reflection.Emit.Label> _bracketStack = new Stack<System.Reflection.Emit.Label>();
public static void Compile(string assemblyName, string outputFileName, string sourceCode)
{
_bracketStack.Clear();
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName),
AssemblyBuilderAccess.Save);
var mod = asm.DefineDynamicModule(assemblyName, outputFileName);
var mainClassTypeName = assemblyName + ".Program"; // e.g., hello.Program
var type = mod.DefineType(mainClassTypeName, TypeAttributes.Class | TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.Public);
var pointerField = type.DefineField("pointer", typeof(Int16), FieldAttributes.Static | FieldAttributes.Private);
var memoryField = type.DefineField("memory", typeof(Byte[]), FieldAttributes.Static | FieldAttributes.Private);
var constructor = type.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, null);
var cctorIlGen = constructor.GetILGenerator();
GenerateConstructorBody(cctorIlGen, pointerField, memoryField);
var mainMethod = type.DefineMethod("Execute", MethodAttributes.Public | MethodAttributes.Static);
var ilGen = mainMethod.GetILGenerator();
foreach (char c in sourceCode)
{
switch (c)
{
case '>':
GenerateMovePointerForwardInstruction(ilGen, pointerField, memoryField);
break;
case '<':
GenerateMovePointerBackwardsInstruction(ilGen, pointerField, memoryField);
break;
case '+':
GenerateIncrementInstruction(ilGen, pointerField, memoryField);
break;
case '-':
GenerateDecrementInstruction(ilGen, pointerField, memoryField);
break;
case '.':
GenerateWriteInstruction(ilGen, pointerField, memoryField);
break;
case ',':
GenerateReadInstruction(ilGen, pointerField, memoryField);
break;
case '[':
GenerateOpenBracketInstruction(ilGen, pointerField, memoryField);
break;
case ']':
GenerateCloseBracketInstruction(ilGen, pointerField, memoryField);
break;
}
}
ilGen.Emit(OpCodes.Ret);
type.CreateType();
asm.SetEntryPoint(mainMethod);
asm.Save(outputFileName);
}
private static void GenerateConstructorBody(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
// construct the memory as byte[short.MaxValue]
ilGen.Emit(OpCodes.Ldc_I4, 0x7fff);
ilGen.Emit(OpCodes.Newarr, typeof(Byte));
ilGen.Emit(OpCodes.Stsfld, memoryField);
// Construct the pointer as short = 0
ilGen.Emit(OpCodes.Ldc_I4_0);
ilGen.Emit(OpCodes.Stsfld, pointerField);
ilGen.Emit(OpCodes.Ret);
}
private static void GenerateMovePointerForwardInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldc_I4_1);
ilGen.Emit(OpCodes.Add);
ilGen.Emit(OpCodes.Conv_I2);
ilGen.Emit(OpCodes.Stsfld, pointerField);
}
private static void GenerateMovePointerBackwardsInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldc_I4_1);
ilGen.Emit(OpCodes.Sub);
ilGen.Emit(OpCodes.Conv_I2);
ilGen.Emit(OpCodes.Stsfld, pointerField);
}
private static void GenerateIncrementInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
ilGen.Emit(OpCodes.Ldsfld, memoryField);
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldelema, typeof(Byte));
ilGen.Emit(OpCodes.Dup);
ilGen.Emit(OpCodes.Ldobj, typeof(Byte));
ilGen.Emit(OpCodes.Ldc_I4_1);
ilGen.Emit(OpCodes.Add);
ilGen.Emit(OpCodes.Conv_U1);
ilGen.Emit(OpCodes.Stobj, typeof(Byte));
}
private static void GenerateDecrementInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
ilGen.Emit(OpCodes.Ldsfld, memoryField);
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldelema, typeof(Byte));
ilGen.Emit(OpCodes.Dup);
ilGen.Emit(OpCodes.Ldobj, typeof(Byte));
ilGen.Emit(OpCodes.Ldc_I4_1);
ilGen.Emit(OpCodes.Sub);
ilGen.Emit(OpCodes.Conv_U1);
ilGen.Emit(OpCodes.Stobj, typeof(Byte));
}
private static void GenerateWriteInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
ilGen.Emit(OpCodes.Ldsfld, memoryField);
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldelem_U1);
ilGen.Emit(OpCodes.Call,
typeof (Console).GetMethod("Write", BindingFlags.Public | BindingFlags.Static, null,
new Type[] {typeof (Char)}, null));
}
private static void GenerateReadInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
ilGen.Emit(OpCodes.Ldsfld, memoryField);
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldelem_U1);
ilGen.Emit(OpCodes.Call, typeof(Console).GetMethod("Read", BindingFlags.Public | BindingFlags.Static));
ilGen.Emit(OpCodes.Conv_U1);
ilGen.Emit(OpCodes.Stelem_I1);
}
private static void GenerateOpenBracketInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
var firstLabel = ilGen.DefineLabel();
var secondLabel = ilGen.DefineLabel();
ilGen.Emit(OpCodes.Br, secondLabel);
ilGen.MarkLabel(firstLabel);
_bracketStack.Push(firstLabel);
_bracketStack.Push(secondLabel);
}
private static void GenerateCloseBracketInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
var secondLabel = _bracketStack.Pop();
var firstLabel = _bracketStack.Pop();
ilGen.MarkLabel(secondLabel);
ilGen.Emit(OpCodes.Ldsfld, memoryField);
ilGen.Emit(OpCodes.Ldsfld, pointerField);
ilGen.Emit(OpCodes.Ldelem_U1);
ilGen.Emit(OpCodes.Ldc_I4_0);
ilGen.Emit(OpCodes.Bgt, firstLabel);
}
}
}