-
Notifications
You must be signed in to change notification settings - Fork 55
/
GenCCpp.fu
293 lines (264 loc) · 7.09 KB
/
GenCCpp.fu
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
286
287
288
289
290
291
292
293
// GenCCpp.fu - C/C++ code generator
//
// Copyright (C) 2011-2024 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
//
// Fusion Transpiler is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fusion Transpiler is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fusion Transpiler. If not, see http://www.gnu.org/licenses/
public abstract class GenCCpp : GenCCppD
{
protected override void WriteDocCode!(string s)
{
if (s == "null")
VisitLiteralNull();
else
WriteXmlDoc(s);
}
protected abstract void IncludeStdInt!();
protected abstract void IncludeStdDef!();
protected abstract void IncludeAssert!();
protected abstract void IncludeMath!();
void WriteCIncludes!()
{
WriteIncludes("#include <", ">");
}
protected override int GetLiteralChars() => 127;
protected virtual void WriteNumericType!(FuId id)
{
switch (id) {
case FuId.SByteRange:
IncludeStdInt();
Write("int8_t");
break;
case FuId.ByteRange:
IncludeStdInt();
Write("uint8_t");
break;
case FuId.ShortRange:
IncludeStdInt();
Write("int16_t");
break;
case FuId.UShortRange:
IncludeStdInt();
Write("uint16_t");
break;
case FuId.IntType:
Write("int");
break;
case FuId.NIntType:
IncludeStdDef();
Write("ptrdiff_t");
break;
case FuId.LongType:
IncludeStdInt();
Write("int64_t");
break;
case FuId.FloatType:
Write("float");
break;
case FuId.DoubleType:
Write("double");
break;
default:
assert false;
}
}
protected override void WriteArrayLength!(FuExpr expr, FuPriority parent)
{
if (parent > FuPriority.Add)
WriteChar('(');
Write("argc - 1");
if (parent > FuPriority.Add)
WriteChar(')');
}
protected void WriteArgsIndexing!(FuExpr index)
{
Write("argv[");
if (index is FuLiteralLong literal)
VisitLiteralLong(1 + literal.Value);
else {
Write("1 + ");
index.Accept(this, FuPriority.Add);
}
WriteChar(']');
}
internal override void VisitSymbolReference!(FuSymbolReference expr, FuPriority parent)
{
switch (expr.Symbol.Id) {
case FuId.MathNaN:
IncludeMath();
Write("NAN");
break;
case FuId.MathNegativeInfinity:
IncludeMath();
Write("-INFINITY");
break;
case FuId.MathPositiveInfinity:
IncludeMath();
Write("INFINITY");
break;
default:
base.VisitSymbolReference(expr, parent);
break;
}
}
protected static FuExpr? IsStringEmpty(FuBinaryExpr expr)
{
if (expr.Left is FuSymbolReference symbol && symbol.Symbol.Id == FuId.StringLength
&& expr.Right.IsLiteralZero())
return symbol.Left;
return null;
}
protected abstract void WriteArrayPtr!(FuExpr expr, FuPriority parent);
protected void WriteArrayPtrAdd!(FuExpr array, FuExpr index)
{
if (index.IsLiteralZero())
WriteArrayPtr(array, FuPriority.Argument);
else {
WriteArrayPtr(array, FuPriority.Add);
Write(" + ");
index.Accept(this, FuPriority.Mul);
}
}
protected static FuCallExpr? IsStringSubstring(FuExpr expr)
{
if (expr is FuCallExpr call) {
FuId id = call.Method.Symbol.Id;
if ((id == FuId.StringSubstring && call.Arguments.Count == 2)
|| id == FuId.UTF8GetString)
return call;
}
return null;
}
protected static bool IsUTF8GetString(FuCallExpr call) => call.Method.Symbol.Id == FuId.UTF8GetString;
protected static FuExpr? IsTrimSubstring(FuBinaryExpr expr)
{
FuCallExpr? call = IsStringSubstring(expr.Right);
if (call != null
&& !IsUTF8GetString(call)
&& expr.Left is FuSymbolReference leftSymbol && call.Method.Left.IsReferenceTo(leftSymbol.Symbol) // TODO: more complex expr
&& call.Arguments[0].IsLiteralZero())
return call.Arguments[1];
return null;
}
protected void WriteStringLiteralWithNewLine!(string s)
{
WriteChar('"');
Write(s);
Write("\\n\"");
}
protected virtual void WriteUnreachable!(FuAssert statement)
{
// TODO: C23, C++23: unreachable()
Write("abort();");
if (statement.Message != null) {
Write(" // ");
statement.Message.Accept(this, FuPriority.Argument);
}
WriteNewLine();
}
protected override void WriteAssert!(FuAssert statement)
{
if (statement.CompletesNormally()) {
WriteTemporaries(statement.Cond);
IncludeAssert();
Write("assert(");
if (statement.Message == null)
statement.Cond.Accept(this, FuPriority.Argument);
else {
statement.Cond.Accept(this, FuPriority.CondAnd);
Write(" && ");
statement.Message.Accept(this, FuPriority.Argument);
}
WriteLine(");");
}
else
WriteUnreachable(statement);
}
internal override void VisitReturn!(FuReturn statement)
{
if (statement.Value == null && this.CurrentMethod.Id == FuId.Main)
WriteLine("return 0;");
else
base.VisitReturn(statement);
}
internal override void VisitSwitch!(FuSwitch statement)
{
if (statement.Value.Type is FuStringType || statement.HasWhen())
WriteSwitchAsIfsWithGoto(statement);
else
base.VisitSwitch(statement);
}
protected void WriteMethods!(FuClass klass)
{
for (FuSymbol? symbol = klass.First; symbol != null; symbol = symbol.Next) {
if (symbol is FuMethod method) {
WriteMethod(method);
this.CurrentTemporaries.Clear();
}
}
}
protected abstract void WriteClassInternal!(FuClass klass);
protected override void WriteClass!(FuClass klass, FuProgram program)
{
// topological sorting of class hierarchy and class storage fields
if (!WriteBaseClass(klass, program))
return;
for (FuSymbol? symbol = klass.First; symbol != null; symbol = symbol.Next) {
if (symbol is FuField field && field.Type.GetBaseType() is FuStorageType storage && storage.Class.Id == FuId.None)
WriteClass(storage.Class, program);
}
WriteClassInternal(klass);
}
static string() ChangeExtension(string path, string ext)
{
int extIndex = path.Length;
for (int i = extIndex; --i >= 0 && path[i] != '/' && path[i] != '\\'; ) {
if (path[i] == '.') {
extIndex = i;
break;
}
}
return path.Substring(0, extIndex) + ext;
}
protected void CreateHeaderFile!(string headerExt)
{
CreateFile(null, ChangeExtension(this.OutputFile, headerExt));
WriteLine("#pragma once");
WriteCIncludes();
}
static string() GetFilenameWithoutExtension(string path)
{
int pathLength = path.Length;
int extIndex = pathLength;
int i = pathLength;
while (--i >= 0 && path[i] != '/' && path[i] != '\\') {
if (path[i] == '.' && extIndex == pathLength)
extIndex = i;
}
i++;
return path.Substring(i, extIndex - i);
}
protected void CreateImplementationFile!(FuProgram program, string headerExt)
{
CreateOutputFile();
WriteTopLevelNatives(program);
WriteCIncludes();
Write("#include \"");
Write(GetFilenameWithoutExtension(this.OutputFile));
Write(headerExt);
WriteCharLine('"');
}
}