-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathblock_processor.dart
67 lines (59 loc) · 1.62 KB
/
block_processor.dart
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
import '../ast/block.dart';
import '../ast/exp.dart';
import '../ast/stat.dart';
import 'exp_helper.dart';
import 'exp_processor.dart';
import 'funcinfo.dart';
import 'stat_processor.dart';
class BlockProcessor {
static void processBlock(FuncInfo fi, Block node) {
for (Stat stat in node.stats) {
StatProcessor.processStat(fi, stat);
}
if (node.retExps != null) {
processRetStat(fi, node.retExps!, node.lastLine);
}
}
static void processRetStat(FuncInfo fi, List<Exp> exps, int lastLine) {
int nExps = exps.length;
if (nExps == 0) {
fi.emitReturn(lastLine, 0, 0);
return;
}
if (nExps == 1) {
if (exps[0] is NameExp) {
NameExp nameExp = exps[0] as NameExp;
int r = fi.slotOfLocVar(nameExp.name)!;
if (r >= 0) {
fi.emitReturn(lastLine, r, 1);
return;
}
}
if (exps[0] is FuncCallExp) {
FuncCallExp fcExp = exps[0] as FuncCallExp;
int r = fi.allocReg();
ExpProcessor.processTailCallExp(fi, fcExp, r);
fi.freeReg();
fi.emitReturn(lastLine, r, -1);
return;
}
}
bool multRet = ExpHelper.isVarargOrFuncCall(exps[nExps-1]);
for (int i = 0; i < nExps; i++) {
Exp exp = exps[i];
int r = fi.allocReg();
if (i == nExps-1 && multRet) {
ExpProcessor.processExp(fi, exp, r, -1);
} else {
ExpProcessor.processExp(fi, exp, r, 1);
}
}
fi.freeRegs(nExps);
int a = fi.usedRegs; // correct?
if (multRet) {
fi.emitReturn(lastLine, a, -1);
} else {
fi.emitReturn(lastLine, a, nExps);
}
}
}