-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator-func.cpp
More file actions
543 lines (425 loc) 路 18.2 KB
/
Copy pathgenerator-func.cpp
File metadata and controls
543 lines (425 loc) 路 18.2 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// generator-func.cpp - split from generator.cpp
#include <cassert>
#include <sstream>
#include "main.h"
// Function generation ///////////////////////////////////////////////
// X=CINT(<袗袪袚校袦袝袧孝>)
// result is Integer
void Generator::GenerateFuncCint(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateExpression(expr1);
if (expr1.GetExpressionValueType() == ValueTypeInteger)
{
Warning(node.token, "CINT function call has no effect, value already has Integer type.");
return; // already Integer, the value is in R0 already
}
AddRuntimeCall(RuntimeFTOI, "to Integer"); // result in R0
}
// X=FIX(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncFix(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFFIX, "FIX");
}
// X=INT(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncInt(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFINT, "INT");
}
// X=ABS(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Integer if arguments is Integer
// result is Single if arguments is Single
void Generator::GenerateFuncAbs(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
switch (expr1.GetExpressionValueType())
{
case ValueTypeInteger:
GenerateExpression(expr1); // result in R0
AddLine("\tBPL\t.+4");
AddLine("\tNEG\tR0");
return;
case ValueTypeSingle:
GenerateExpression(expr1); // result on stack
AddLine("\tBIC\t#100000, (SP)\t; ABS"); // clear sign
return;
default:
assert(false); // unexpected value type
}
}
// X=RND(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncRnd(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
// Special case for RND(0): return RNDSAV value
//NOTE: The sign of the argument decides what RND does, see 5.1.13, so only the
// exact zero goes here: RND(0.5) has a positive argument, not a zero one.
if (expr1.IsConstExpression() && expr1.GetConstExpressionDValue() == 0.0)
{
AddLine("\tMOV\tRNDSAV+2, -(SP)\t; RND(0)");
AddLine("\tMOV\tRNDSAV, -(SP)");
m_runtimeneeds.insert(RuntimeFRND);
return;
}
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFRND, "random number"); // result on stack
}
// X=PEEK(<袗袪袚校袦袝袧孝>)
// result is Integer
void Generator::GenerateFuncPeek(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 1);
const string comment = "\t; PEEK";
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
if (expr1.IsConstExpression())
{
int ivalue = ConstToInteger(expr1.GetConstExpressionDValue());
AddLine("\tMOV\t@#" + std::to_string(ivalue) + "., R0" + comment);
return;
}
else if (expr1.IsVariableExpression() && expr1.GetExpressionValueType() == ValueTypeInteger)
{
// Integer variable holds the address
AddLine("\tMOV\t@" + expr1.GetVariableExpressionDecoratedName() + ", R0" + comment);
return;
}
// The address parameter, Integer; Single converted to Integer
GenerateOperandAsInteger(expr1); // result in R0
AddLine("\tMOV\t(R0), R0" + comment);
}
// X=INP(<袗袛袪袝小>,<袦袗小袣袗>)
// result is Integer
void Generator::GenerateFuncInp(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 2);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
const ExpressionModel& expr2 = node.args[1];
assert(expr2.GetExpressionValueType() != ValueTypeString);
// Special cases for the const mask
if (expr2.IsConstExpression())
{
//NOTE: The mask is a 16-bit value, so &HFFFF gives -1 here, see ParseDValue.
int imask = ConstToInteger(expr2.GetConstExpressionDValue()) & 0xFFFF;
if (imask == 0) // Nothing left of the value, the result is always 0
{
Warning(node.token, "INP with mask 0 reduced to 0, consider to remove this INP.");
AddLine("\tCLR\tR0\t; INP mask 0");
return;
}
if (imask == 0xFFFF) // The mask keeps all the bits, same as PEEK
{
Warning(node.token, "INP with mask 177777 does the same as PEEK, consider to use PEEK.");
GenerateOperandAsInteger(expr1); // R0 = address
AddLine("\tMOV\t(R0), R0\t; INP");
return;
}
}
//TODO: Special case for const expression and variable expression
GenerateOperandAsInteger(expr1); // R0 = address
AddLine("\tMOV\t(R0), R1\t; INP value"); // R1 = value
GenerateOperandAsInteger(expr2); // R0 = mask
AddLine("\tCOM\tR0"); // invert the mask
AddLine("\tBIC\tR0, R1\t; INP mask"); // apply the mask
AddLine("\tMOV\tR1, R0\t; INP"); // result in R0
}
// X=CSRLIN[(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)]
// result is Integer
void Generator::GenerateFuncCsrlin(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() <= 1);
// If we have non-const expression then calculate it
if (node.args.size() > 0)
{
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
if (!expr1.IsConstExpression() && !expr1.IsVariableExpression())
GenerateExpression(expr1);
Warning(node.token, "CSRLIN argument calculated but value not used; consider to remove the argument");
}
AddRuntimeCall(RuntimeGETCR, "get cursor pos for CSRLIN"); // R1 = column, R2 = row
AddLine("\tMOV\tR2, R0\t; row");
}
// X=POS[(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)]
// result is Integer
void Generator::GenerateFuncPos(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() <= 1);
// If we have non-const expression then calculate it
if (node.args.size() > 0)
{
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
if (!expr1.IsConstExpression() && !expr1.IsVariableExpression())
GenerateExpression(expr1);
Warning(node.token, "POS argument calculated but value not used; consider to remove the argument");
}
AddRuntimeCall(RuntimeGETCR, "get cursor pos for POS"); // R1 = column, R2 = row
AddLine("\tMOV\tR1, R0\t; column");
}
// X=LEN(<小袠袦袙袨袥鞋袧袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Integer
void Generator::GenerateFuncLen(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 1);
//TODO: Special case for const expression and variable expression
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() == ValueTypeString);
GenerateExpression(expr1); // R0 = string address
AddLine("\tMOV\tR0, R1\t");
AddLine("\tCLR\tR0\t");
AddLine("\tBISB\t(R1), R0\t; LEN"); // get byte of the string length
}
// X=SQR(<袗袪袚校袦袝袧孝>)
// result is Single
void Generator::GenerateFuncSqr(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFSQR, "square root"); // result on stack
}
// X=SGN(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncSgn(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFSGN, "SGN");
}
// X=CSNG(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncCsng(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateExpression(expr1);
if (expr1.GetExpressionValueType() == ValueTypeSingle)
{
Warning(node.token, "CSNG function call has no effect, value already has Single type.");
return;
}
AddRuntimeCall(RuntimeITOF, "CSNG"); // result on stack
}
// X=SIN(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncSin(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFSIN, "sin(X)"); // result on stack
}
// X=COS(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncCos(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFCOS, "cos(X)"); // result on stack
}
// X=TAN(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncTan(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFTAN, "tan(X)"); // result on stack
}
// X=ATN(<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncAtn(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFATN, "arctan(X)"); // result on stack
}
// X=EXP(<袗袪袚校袦袝袧孝>)
// result is Single
void Generator::GenerateFuncExp(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFEXP, "exp(X)"); // result on stack
}
// X=LOG(<袗袪袚校袦袝袧孝>)
// result is Single
void Generator::GenerateFuncLog(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateOperandAsSingle(expr1);
AddRuntimeCall(RuntimeFLOG, "log(X)"); // result on stack
}
// X陇=INKEY陇
// result is String
void Generator::GenerateFuncInkey(const ExpressionModel& expr, const ExpressionNode& node)
{
//TODO: Rework to return dynamic String
AddRuntimeCall(RuntimeINKEY); // R0 = string address
}
// X=ASC(<袗袪袚校袦袝袧孝>)
// result is Integer
void Generator::GenerateFuncAsc(const ExpressionModel& expr, const ExpressionNode& node)
{
//TODO: Special case for const expression and variable expression
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() == ValueTypeString);
GenerateExpression(expr1); // R0 = string address
AddLine("\tMOV\tR0, R1\t");
AddLine("\tCLR\tR0\t");
AddLine("\tTSTB\t(R1)+\t"); // check string length
AddLine("\tBEQ\t.+4");
AddLine("\tBISB\t(R1), R0\t; ASC"); // get first byte of the string
}
// X陇=CHR陇(<袗袪袚校袦袝袧孝>)
// result is String
void Generator::GenerateFuncChr(const ExpressionModel& expr, const ExpressionNode& node)
{
//TODO: Special case for const expression and variable expression
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateExpression(expr1);
if (expr1.GetExpressionValueType() == ValueTypeSingle)
AddRuntimeCall(RuntimeFTOI, "to Integer"); // result in R0
//TODO: Allocate dynamic 1-char string
//TODO: String length = 1, MOVB R0 to first char of the string
AddComment("TODO CHR$");
}
// X陇=STRING陇(<袗袪袚校袦袝袧孝1>,<袗袪袚校袦袝袧孝2>)
// result is String
void Generator::GenerateFuncString(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 2);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
const ExpressionModel& expr2 = node.args[1];
if (expr1.IsConstExpression())
{
int ivalue = ConstToInteger(expr1.GetConstExpressionDValue());
assert(ivalue >= 0 && ivalue <= 255);
if (ivalue == 0)
{
Warning(node.token, "STRING$(0, ...) reduced to empty string; consider to replace this expression with \"\".");
AddLine("\tMOV\tST0, R0");
return;
}
GenerateExpression(expr2);
if (expr2.GetExpressionValueType() == ValueTypeSingle)
AddRuntimeCall(RuntimeFTOI, "to Integer"); // result in R0
//TODO: If the string is empty, result is empty string (already in R0)
//TODO: Allocate string with the given length
//TODO: Fill the string
AddComment("TODO STRING$");
return;
}
if (expr2.IsConstExpression())
{
ValueType expr2vtype = expr2.GetExpressionValueType();
if (expr2vtype == ValueTypeString && expr2.GetConstExpressionSValue() == "")
{
Warning(node.token, "STRING$(..., \"\") reduced to empty string; consider to replace this expression with \"\".");
AddLine("\tMOV\tST0, R0");
return;
}
GenerateExpression(expr1);
if (expr1.GetExpressionValueType() == ValueTypeSingle)
AddRuntimeCall(RuntimeFTOI, "to Integer"); // result in R0
//TODO: Allocate string with the given length
//TODO: Fill the string
AddComment("TODO STRING$");
return;
}
GenerateExpression(expr1);
if (expr1.GetExpressionValueType() == ValueTypeSingle)
AddRuntimeCall(RuntimeFTOI, "to Integer"); // result in R0
GenerateExpression(expr2);
if (expr2.GetExpressionValueType() == ValueTypeSingle)
AddRuntimeCall(RuntimeFTOI, "to Integer"); // result in R0
//TODO: If the string is empty, result is empty string (already in R0)
//TODO: Allocate string with the given length
//TODO: Fill the string
AddComment("TODO STRING$");
}
// X=VAL(<小袠袦袙袨袥鞋袧袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single
void Generator::GenerateFuncVal(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() == 1);
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() == ValueTypeString);
GenerateExpression(expr1); // R0 = string address (length byte, then the characters)
AddRuntimeCall(RuntimeVALF, "VAL"); // parse the string to a Single on the stack
}
// X=IIF(<袥袨袚袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>,<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>,<袗袪袠肖袦袝孝袠效袝小袣袨袝 袙蝎袪袗袞袝袧袠袝>)
// result is Single or Integer
void Generator::GenerateFuncIif(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(expr.GetExpressionValueType() != ValueTypeString);
assert(node.args.size() == 3);
string labelfalse = GetNextLocalLabel(); // local label for false expression
string labelend = GetNextLocalLabel(); // local label for end of IIF
const ExpressionModel& expr1 = node.args[0];
assert(expr1.GetExpressionValueType() != ValueTypeString);
GenerateExpression(expr1);
AddLine("\tBEQ\t" + labelfalse + "\t; false =>");
AddComment("IIF true expression");
const ExpressionModel& expr2 = node.args[1];
assert(expr2.GetExpressionValueType() != ValueTypeString);
GenerateExpression(expr2);
if (expr.GetExpressionValueType() == ValueTypeSingle && expr2.GetExpressionValueType() == ValueTypeInteger)
AddRuntimeCall(RuntimeITOF, "to Single"); // result on stack
AddLine("\tBR\t" + labelend);
AddLine(labelfalse + ":\t; IIF false expression");
const ExpressionModel& expr3 = node.args[2];
assert(expr3.GetExpressionValueType() != ValueTypeString);
GenerateExpression(expr3);
if (expr.GetExpressionValueType() == ValueTypeSingle && expr3.GetExpressionValueType() == ValueTypeInteger)
AddRuntimeCall(RuntimeITOF, "to Single"); // result on stack
AddLine(labelend + ":\t; end of IIF");
}
//////////////////////////////////////////////////////////////////////