-
Notifications
You must be signed in to change notification settings - Fork 57
/
GenCl.fu
378 lines (356 loc) · 9.62 KB
/
GenCl.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// GenCl.fu - OpenCL C code generator
//
// Copyright (C) 2020-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 class GenCl : GenC
{
bool StringLength;
bool StringEquals;
bool StringStartsWith;
bool BytesEqualsString;
protected override string GetTargetName() => "OpenCL C";
protected override void IncludeStdBool!()
{
}
protected override void IncludeMath!()
{
}
protected override void WriteNumericType!(FuId id)
{
switch (id) {
case FuId.SByteRange:
Write("char");
break;
case FuId.ByteRange:
Write("uchar");
break;
case FuId.ShortRange:
Write("short");
break;
case FuId.UShortRange:
Write("ushort");
break;
case FuId.IntType:
Write("int");
break;
case FuId.NIntType:
Write("ptrdiff_t");
break;
case FuId.LongType:
Write("long");
break;
default:
assert false;
}
}
protected override void WriteStringPtrType!()
{
Write("constant char *");
}
protected override void WriteClassType!(FuClassType klass, bool space)
{
switch (klass.Class.Id) {
case FuId.None:
if (klass is FuDynamicPtrType)
NotSupported(klass, "Dynamic reference");
else
base.WriteClassType(klass, space);
break;
case FuId.StringClass:
if (klass.Id == FuId.StringStorageType)
NotSupported(klass, "string()");
else
WriteStringPtrType();
break;
default:
NotSupported(klass, klass.Class.Name);
break;
}
}
protected override void WritePrintfLongPrefix!()
{
WriteChar('l');
}
protected override void WriteInterpolatedStringArgBase!(FuExpr expr)
{
expr.Accept(this, FuPriority.Argument);
}
internal override void VisitInterpolatedString!(FuInterpolatedString expr, FuPriority parent)
{
NotSupported(expr, "Interpolated strings");
}
protected override void WriteCamelCaseNotKeyword!(string name)
{
switch (name) {
case "Constant":
case "Global":
case "Kernel":
case "Local":
case "Private":
case "constant":
case "global":
case "kernel":
case "local":
case "private":
WriteCamelCase(name);
WriteChar('_');
break;
default:
base.WriteCamelCaseNotKeyword(name);
break;
}
}
protected override string GetConst(FuArrayStorageType array) => array.PtrTaken ? "const " : "constant ";
protected override void WriteSubstringEqual!(FuCallExpr call, string literal, FuPriority parent, bool not)
{
if (not)
WriteChar('!');
if (IsUTF8GetString(call)) {
this.BytesEqualsString = true;
Write("FuBytes_Equals(");
WriteArrayPtrAdd(call.Arguments[0], call.Arguments[1]);
}
else {
this.StringStartsWith = true;
Write("FuString_StartsWith(");
WriteArrayPtrAdd(call.Method.Left, call.Arguments[0]);
}
Write(", ");
VisitLiteralString(literal);
WriteChar(')');
}
protected override void WriteEqualStringInternal!(FuExpr left, FuExpr right, FuPriority parent, bool not)
{
this.StringEquals = true;
if (not)
WriteChar('!');
WriteCall("FuString_Equals", left, right);
}
protected override void WriteStringLength!(FuExpr expr)
{
this.StringLength = true;
WriteCall("strlen", expr);
}
void WriteConsoleWrite!(List<FuExpr#> args, bool newLine)
{
Write("printf(");
if (args.Count == 0)
Write("\"\\n\")");
else if (args[0] is FuInterpolatedString interpolated)
WritePrintf(interpolated, newLine);
else
WritePrintfNotInterpolated(args, newLine);
}
protected override void WriteCallExpr!(FuExpr? obj, FuMethod method, List<FuExpr#> args, FuPriority parent)
{
switch (method.Id) {
case FuId.None:
case FuId.ClassToString:
WriteCCall(obj, method, args);
break;
case FuId.EnumFromInt:
WriteStaticCast(method.Type, args[0]);
break;
case FuId.EnumHasFlag:
WriteEnumHasFlag(obj, args, parent);
break;
case FuId.StringStartsWith:
int c = GetOneAscii(args[0]);
if (c >= 0) {
if (parent > FuPriority.Equality)
WriteChar('(');
WritePostfix(obj, "[0] == ");
VisitLiteralChar(c);
if (parent > FuPriority.Equality)
WriteChar(')');
}
else {
this.StringStartsWith = true;
WriteCall("FuString_StartsWith", obj, args[0]);
}
break;
case FuId.StringSubstring:
if (args.Count == 1)
WriteStringSubstringStart(obj, args, parent);
else
NotSupported(obj, "Substring");
break;
case FuId.ArrayCopyTo:
Write("for (size_t _i = 0; _i < ");
args[3].Accept(this, FuPriority.Rel); // FIXME: side effect in every iteration
WriteLine("; _i++)");
WriteChar('\t');
args[1].Accept(this, FuPriority.Primary); // FIXME: side effect in every iteration
WriteChar('[');
StartAdd(args[2]); // FIXME: side effect in every iteration
Write("_i] = ");
obj.Accept(this, FuPriority.Primary); // FIXME: side effect in every iteration
WriteChar('[');
StartAdd(args[0]); // FIXME: side effect in every iteration
Write("_i]");
break;
case FuId.ArrayFillAll:
case FuId.ArrayFillPart:
WriteArrayFill(obj, args);
break;
case FuId.ConsoleWrite:
WriteConsoleWrite(args, false);
break;
case FuId.ConsoleWriteLine:
WriteConsoleWrite(args, true);
break;
case FuId.UTF8GetByteCount:
WriteStringLength(args[0]);
break;
case FuId.UTF8GetBytes:
Write("for (size_t _i = 0; ");
args[0].Accept(this, FuPriority.Primary); // FIXME: side effect in every iteration
WriteLine("[_i] != '\\0'; _i++)");
WriteChar('\t');
args[1].Accept(this, FuPriority.Primary); // FIXME: side effect in every iteration
WriteChar('[');
StartAdd(args[2]); // FIXME: side effect in every iteration
Write("_i] = ");
WritePostfix(args[0], "[_i]"); // FIXME: side effect in every iteration
break;
case FuId.MathMethod:
case FuId.MathClamp:
case FuId.MathIsFinite:
case FuId.MathIsNaN:
case FuId.MathLog2:
case FuId.MathRound:
WriteLowercase(method.Name);
WriteInParentheses(args);
break;
case FuId.MathAbs:
if (args[0].Type is FuFloatingType)
WriteChar('f');
WriteCall("abs", args[0]);
break;
case FuId.MathCeiling:
WriteCall("ceil", args[0]);
break;
case FuId.MathFusedMultiplyAdd:
WriteCall("fma", args[0], args[1], args[2]);
break;
case FuId.MathIsInfinity:
WriteCall("isinf", args[0]);
break;
case FuId.MathMax:
if (args[0].Type is FuFloatingType || args[1].Type is FuFloatingType)
WriteChar('f');
WriteCall("max", args[0], args[1]);
break;
case FuId.MathMin:
if (args[0].Type is FuFloatingType || args[1].Type is FuFloatingType)
WriteChar('f');
WriteCall("min", args[0], args[1]);
break;
case FuId.MathTruncate:
WriteCall("trunc", args[0]);
break;
default:
NotSupported(obj, method.Name);
break;
}
}
protected override void WriteAssert!(FuAssert statement)
{
}
protected override void WriteSwitchCaseBody!(List<FuStatement#> statements)
{
if (statements.All(statement => statement is FuAssert))
WriteCharLine(';');
else
base.WriteSwitchCaseBody(statements);
}
void WriteLibrary!()
{
if (this.StringLength) {
WriteNewLine();
WriteLine("static ptrdiff_t strlen(constant char *str)");
OpenBlock();
WriteLine("ptrdiff_t len = 0;");
WriteLine("while (str[len] != '\\0')");
WriteLine("\tlen++;");
WriteLine("return len;");
CloseBlock();
}
if (this.StringEquals) {
WriteNewLine();
WriteLine("static bool FuString_Equals(constant char *str1, constant char *str2)");
OpenBlock();
WriteLine("for (size_t i = 0; str1[i] == str2[i]; i++) {");
WriteLine("\tif (str1[i] == '\\0')");
WriteLine("\t\treturn true;");
WriteCharLine('}');
WriteLine("return false;");
CloseBlock();
}
if (this.StringStartsWith) {
WriteNewLine();
WriteLine("static bool FuString_StartsWith(constant char *str1, constant char *str2)");
OpenBlock();
WriteLine("for (int i = 0; str2[i] != '\\0'; i++) {");
WriteLine("\tif (str1[i] != str2[i])");
WriteLine("\t\treturn false;");
WriteCharLine('}');
WriteLine("return true;");
CloseBlock();
}
if (this.BytesEqualsString) {
WriteNewLine();
WriteLine("static bool FuBytes_Equals(const uchar *mem, constant char *str)");
OpenBlock();
WriteLine("for (size_t i = 0; str[i] != '\\0'; i++) {");
WriteLine("\tif (mem[i] != str[i])");
WriteLine("\t\treturn false;");
WriteCharLine('}');
WriteLine("return true;");
CloseBlock();
}
}
public override void WriteProgram!(FuProgram program)
{
this.WrittenClasses.Clear();
this.StringLength = false;
this.StringEquals = false;
this.StringStartsWith = false;
this.BytesEqualsString = false;
OpenStringWriter();
foreach (FuClass klass in program.Classes) {
this.CurrentClass = klass;
WriteConstructor(klass);
WriteDestructor(klass);
WriteMethods(klass);
}
CreateOutputFile();
WriteTopLevelNatives(program);
WriteRegexOptionsEnum(program);
WriteTypedefs(program, true);
foreach (FuClass klass in program.Classes)
WriteSignatures(klass, true);
WriteTypedefs(program, false);
foreach (FuClass klass in program.Classes)
WriteClass(klass, program);
WriteResources(program.Resources);
WriteLibrary();
CloseStringWriter();
CloseFile();
}
}