Skip to content

Commit 1691b35

Browse files
committed
assimilate so BE code
1 parent 3aaa176 commit 1691b35

8 files changed

Lines changed: 75 additions & 108 deletions

File tree

BE/CodeGenA32/codegen.cc

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
#include "BE/CodeGenA32/codegen.h"
44

5-
#include "BE/Base/cfg.h"
65
#include "BE/Base/ir.h"
76
#include "BE/Base/optimize.h"
8-
#include "BE/Base/sanity.h"
97
#include "BE/Base/serialize.h"
108
#include "BE/CodeGenA32/isel_gen.h"
119
#include "BE/CodeGenA32/legalize.h"
@@ -220,42 +218,8 @@ a32::A32Unit EmitUnitAsBinary(base::Unit unit) {
220218
return out;
221219
}
222220

223-
std::vector<Fun> GetSeeds(Unit unit) {
224-
std::vector<Fun> seeds;
225-
Fun fun = UnitFunFind(unit, StrNew("main"));
226-
if (!fun.isnull()) seeds.push_back(fun);
227-
fun = UnitFunFind(unit, StrNew("_start"));
228-
if (!fun.isnull()) seeds.push_back(fun);
229-
return seeds;
230-
}
231221

232-
void LegalizeAll(Unit unit, bool verbose, std::ostream* fout) {
233-
std::vector<Fun> seeds = GetSeeds(unit);
234-
if (!seeds.empty()) UnitRemoveUnreachableCode(unit, seeds);
235-
//
236-
for (Fun fun : UnitFunIter(unit)) {
237-
FunCheck(fun, false, true, false);
238-
if (FunKind(fun) == FUN_KIND::NORMAL) {
239-
FunCfgInit(fun);
240-
FunOptBasic(fun, true);
241-
}
242-
}
243-
for (Fun fun : UnitFunIter(unit)) {
244-
PhaseLegalization(fun, unit, fout);
245-
}
246-
}
247222

248-
void RegAllocGlobal(Unit unit, bool verbose, std::ostream* fout) {
249-
for (Fun fun : UnitFunIter(unit)) {
250-
FunCheck(fun, false, false, false);
251-
PhaseGlobalRegAlloc(fun, unit, fout);
252-
}
253-
}
254223

255-
void RegAllocLocal(Unit unit, bool verbose, std::ostream* fout) {
256-
for (Fun fun : UnitFunIter(unit)) {
257-
PhaseFinalizeStackAndLocalRegAlloc(fun, unit, fout);
258-
}
259-
}
260224

261225
} // namespace cwerg::code_gen_a32

BE/CodeGenA32/codegen.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
import stat
1010
import collections
1111

12-
from BE.Base import cfg
1312
from BE.Base import ir
1413
from IR import opcode_tab as o
15-
from BE.Base import sanity
1614
from BE.Base import serialize
1715

1816
from BE.CpuA32 import opcode_tab as a32
@@ -27,36 +25,7 @@
2725
from BE.Elf import elf_unit
2826

2927

30-
def LegalizeAll(unit: ir.Unit, opt_stats, verbose=False):
31-
seeds = [f for f in [unit.fun_syms.get("_start"),
32-
unit.fun_syms.get("main")] if f]
33-
if seeds:
34-
cfg.UnitRemoveUnreachableCode(unit, seeds)
35-
for fun in unit.funs:
36-
sanity.FunCheck(fun, unit, check_cfg=False,
37-
check_push_pop=True, check_fallthroughs=False)
38-
39-
if fun.kind is o.FUN_KIND.NORMAL:
40-
legalize.PhaseOptimize(fun, unit, opt_stats)
41-
42-
for fun in unit.funs:
43-
legalize.PhaseLegalization(fun, unit, opt_stats)
44-
4528

46-
def RegAllocGlobal(unit: ir.Unit, opt_stats, fout, verbose=False):
47-
for fun in unit.funs:
48-
sanity.FunCheck(fun, unit, check_cfg=False,
49-
check_push_pop=False, check_fallthroughs=False)
50-
legalize.PhaseGlobalRegAlloc(fun, opt_stats, fout)
51-
if verbose:
52-
legalize.DumpFun("after global_reg_alloc", fun)
53-
54-
55-
def RegAllocLocal(unit: ir.Unit, opt_stats, verbose=False):
56-
for fun in unit.funs:
57-
legalize.PhaseFinalizeStackAndLocalRegAlloc(fun, opt_stats)
58-
if verbose:
59-
legalize.DumpFun("after stack finalization", fun)
6029

6130

6231
############################################################
@@ -272,9 +241,9 @@ def main():
272241
if args.mode == "binary":
273242
# we need to legalize all functions first as this may change the signature
274243
# and fills in cpu reg usage which is used by subsequent interprocedural opts.
275-
LegalizeAll(unit, opt_stats, None)
276-
RegAllocGlobal(unit, opt_stats, None)
277-
RegAllocLocal(unit, opt_stats, None)
244+
legalize.LegalizeAll(unit, opt_stats, None)
245+
legalize.RegAllocGlobal(unit, opt_stats, None)
246+
legalize.RegAllocLocal(unit, opt_stats, None)
278247
armunit = EmitUnitAsBinary(unit)
279248
exe = assembler.Assemble(armunit, True)
280249
exe.save(open(args.output, "wb"))
@@ -285,17 +254,17 @@ def main():
285254

286255
# we need to legalize all functions first as this may change the signature
287256
# and fills in cpu reg usage which is used by subsequent interprocedural opts.
288-
LegalizeAll(unit, opt_stats)
257+
legalize.LegalizeAll(unit, opt_stats)
289258
if args.mode == "legalize":
290259
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
291260
return
292261

293-
RegAllocGlobal(unit, opt_stats, fout)
262+
legalize.RegAllocGlobal(unit, opt_stats, fout)
294263
if args.mode == "reg_alloc_global":
295264
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
296265
return
297266

298-
RegAllocLocal(unit, opt_stats)
267+
legalize.RegAllocLocal(unit, opt_stats)
299268
if args.mode == "reg_alloc_local":
300269
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
301270
return

BE/CodeGenA32/legalize.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include <algorithm>
44
#include <iomanip>
55

6+
#include "BE/Base/cfg.h"
67
#include "BE/Base/canonicalize.h"
78
#include "BE/Base/liveness.h"
89
#include "BE/Base/lowering.h"
910
#include "BE/Base/optimize.h"
1011
#include "BE/Base/reg_alloc.h"
12+
#include "BE/Base/sanity.h"
1113
#include "BE/Base/serialize.h"
1214
#include "BE/CodeGenA32/isel_gen.h"
1315
#include "BE/CodeGenA32/regs.h"
@@ -402,4 +404,33 @@ void PhaseFinalizeStackAndLocalRegAlloc(Fun fun, Unit unit,
402404
FunMoveEliminationCpu(fun, &inss);
403405
}
404406

407+
void LegalizeAll(Unit unit, bool verbose, std::ostream* fout) {
408+
std::vector<Fun> seeds = UnitGetEntryPoints(unit);
409+
if (!seeds.empty()) UnitRemoveUnreachableCode(unit, seeds);
410+
//
411+
for (Fun fun : UnitFunIter(unit)) {
412+
FunCheck(fun, false, true, false);
413+
if (FunKind(fun) == FUN_KIND::NORMAL) {
414+
FunCfgInit(fun);
415+
FunOptBasic(fun, true);
416+
}
417+
}
418+
for (Fun fun : UnitFunIter(unit)) {
419+
PhaseLegalization(fun, unit, fout);
420+
}
421+
}
422+
423+
void RegAllocGlobal(Unit unit, bool verbose, std::ostream* fout) {
424+
for (Fun fun : UnitFunIter(unit)) {
425+
FunCheck(fun, false, false, false);
426+
PhaseGlobalRegAlloc(fun, unit, fout);
427+
}
428+
}
429+
430+
void RegAllocLocal(Unit unit, bool verbose, std::ostream* fout) {
431+
for (Fun fun : UnitFunIter(unit)) {
432+
PhaseFinalizeStackAndLocalRegAlloc(fun, unit, fout);
433+
}
434+
}
435+
405436
} // namespace cwerg::code_gen_a32

BE/CodeGenA32/legalize.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
#pragma once
22
// (c) Robert Muth - see LICENSE for more info
33

4+
#include <iostream>
5+
46
#include "BE/Base/ir.h"
57
#include "BE/CodeGenA32/regs.h"
68

7-
#include <iostream>
8-
99
namespace cwerg::code_gen_a32 {
1010
using namespace cwerg;
1111

12-
extern void PhaseLegalization(base::Fun fun,
13-
base::Unit unit,
14-
std::ostream* fout);
12+
extern void LegalizeAll(base::Unit unit, bool verbose, std::ostream* fout);
1513

16-
extern void PhaseGlobalRegAlloc(base::Fun fun,
17-
base::Unit unit,
18-
std::ostream* fout);
14+
extern void RegAllocGlobal(base::Unit unit, bool verbose, std::ostream* fout);
1915

20-
extern void PhaseFinalizeStackAndLocalRegAlloc(base::Fun fun,
21-
base::Unit unit,
22-
std::ostream* fout);
16+
extern void RegAllocLocal(base::Unit unit, bool verbose, std::ostream* fout);
2317

2418
} // namespace cwerg::code_gen_a32

BE/CodeGenA32/legalize.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import dataclasses
33
from typing import List, Dict, Optional, Tuple, Set
44

5+
from BE.Base import cfg
56
from BE.Base import canonicalize
67
from BE.Base import reg_alloc
78
from BE.Base import ir
@@ -431,3 +432,33 @@ def PhaseFinalizeStackAndLocalRegAlloc(fun: ir.Fun,
431432
fun.FinalizeStackSlots()
432433
# cleanup
433434
FunMoveEliminationCpu(fun)
435+
436+
def LegalizeAll(unit: ir.Unit, opt_stats, verbose=False):
437+
seeds = cfg.UnitGetEntryPoints(unit)
438+
if seeds:
439+
cfg.UnitRemoveUnreachableCode(unit, seeds)
440+
for fun in unit.funs:
441+
sanity.FunCheck(fun, unit, check_cfg=False,
442+
check_push_pop=True, check_fallthroughs=False)
443+
444+
if fun.kind is o.FUN_KIND.NORMAL:
445+
PhaseOptimize(fun, unit, opt_stats)
446+
447+
for fun in unit.funs:
448+
PhaseLegalization(fun, unit, opt_stats)
449+
450+
451+
def RegAllocGlobal(unit: ir.Unit, opt_stats, fout, verbose=False):
452+
for fun in unit.funs:
453+
sanity.FunCheck(fun, unit, check_cfg=False,
454+
check_push_pop=False, check_fallthroughs=False)
455+
PhaseGlobalRegAlloc(fun, opt_stats, fout)
456+
if verbose:
457+
DumpFun("after global_reg_alloc", fun)
458+
459+
460+
def RegAllocLocal(unit: ir.Unit, opt_stats, verbose=False):
461+
for fun in unit.funs:
462+
PhaseFinalizeStackAndLocalRegAlloc(fun, opt_stats)
463+
if verbose:
464+
DumpFun("after stack finalization", fun)

BE/CodeGenA64/legalize.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@
99
namespace cwerg::code_gen_a64 {
1010
using namespace cwerg;
1111

12-
extern void PhaseLegalizationStep1(base::Fun fun, base::Unit unit,
13-
std::ostream* fout);
14-
extern void PhaseLegalizationStep2(base::Fun fun, base::Unit unit,
15-
std::ostream* fout);
16-
17-
extern void PhaseGlobalRegAlloc(base::Fun fun, base::Unit unit,
18-
std::ostream* fout);
19-
20-
extern void PhaseFinalizeStackAndLocalRegAlloc(base::Fun fun, base::Unit unit,
21-
std::ostream* fout);
22-
2312
void LegalizeAll(base::Unit unit, bool verbose, std::ostream* fout);
2413

2514
void RegAllocGlobal(base::Unit unit, bool verbose, std::ostream* fout);

BE/CodeGenX64/legalize.cc

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -532,18 +532,8 @@ void PhaseFinalizeStackAndLocalRegAlloc(Fun fun, Unit unit,
532532
FunMoveEliminationCpu(fun, &inss);
533533
}
534534

535-
std::vector<Fun> GetSeeds(Unit unit) {
536-
std::vector<Fun> seeds;
537-
Fun fun = UnitFunFind(unit, StrNew("main"));
538-
if (!fun.isnull()) seeds.push_back(fun);
539-
fun = UnitFunFind(unit, StrNew("_start"));
540-
if (!fun.isnull()) seeds.push_back(fun);
541-
return seeds;
542-
}
543-
544-
545535
void OptimizeAll(Unit unit, bool verbose, std::ostream* fout) {
546-
std::vector<Fun> seeds = GetSeeds(unit);
536+
std::vector<Fun> seeds = UnitGetEntryPoints(unit);
547537
if (!seeds.empty()) UnitRemoveUnreachableCode(unit, seeds);
548538
for (Fun fun : UnitFunIter(unit)) {
549539
FunCheck(fun, false, true, false);

BE/CodeGenX64/legalize.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ def PhaseLegalization(fun: ir.Fun, unit: ir.Unit, _opt_stats: Dict[str, int]):
268268

269269

270270
def OptimizeAll(unit, opt_stats):
271-
seeds = [f for f in [unit.fun_syms.get("_start"),
272-
unit.fun_syms.get("main")] if f]
271+
seeds = cfg.UnitGetEntryPoints(unit)
273272
if seeds:
274273
cfg.UnitRemoveUnreachableCode(unit, seeds)
275274
for fun in unit.funs:

0 commit comments

Comments
 (0)