Skip to content

Commit e1cc153

Browse files
committed
make frontend write to a file instead of stdout
1 parent bc20bbb commit e1cc153

7 files changed

Lines changed: 573 additions & 540 deletions

File tree

FE/Makefile_cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ PHASE =
112112

113113
$(DIR)/%.cw.assim: %.cw
114114
@echo "[assim $@]"
115-
$(EMIT_IR) -arch x64 $(PHASE) $< > $@.cc_ast
116-
./compiler.py -arch x64 $(PHASE) $< > $@.py_ast
115+
$(EMIT_IR) -arch x64 $(PHASE) $< $@.cc_ast
116+
./compiler.py -arch x64 $(PHASE) $< $@.py_ast
117117
diff $@.py_ast $@.cc_ast
118118

119119
############################################################

FE/compiler.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void PhaseLegalize(std::vector<Node>& mods_in_topo_order, TypeCorpus* tc) {
236236
}
237237

238238
void PhaseEmitCode(const std::vector<Node>& mods_in_topo_order,
239-
const TargetArchConfig& ta) {
239+
const TargetArchConfig& ta, std::ostream* fout) {
240240
std::unordered_set<std::string> sig_names;
241241
for (Node mod : mods_in_topo_order) {
242242
for (Node fun = Node_body_mod(mod); !fun.isnull(); fun = Node_next(fun)) {
@@ -245,7 +245,7 @@ void PhaseEmitCode(const std::vector<Node>& mods_in_topo_order,
245245
if (sig_names.contains(sig_name)) {
246246
continue;
247247
}
248-
EmitFunctionHeader(sig_name, "SIGNATURE", Node_x_type(fun));
248+
EmitFunctionHeader(sig_name, "SIGNATURE", Node_x_type(fun), fout);
249249
sig_names.insert(sig_name);
250250
}
251251
}
@@ -254,13 +254,13 @@ void PhaseEmitCode(const std::vector<Node>& mods_in_topo_order,
254254
for (Node mod : mods_in_topo_order) {
255255
for (Node def = Node_body_mod(mod); !def.isnull(); def = Node_next(def)) {
256256
if (def.kind() == NT::DefGlobal) {
257-
EmitDefGlobal(def, ta);
257+
EmitDefGlobal(def, ta, fout);
258258
}
259259
}
260260
for (Node fun = Node_body_mod(mod); !fun.isnull(); fun = Node_next(fun)) {
261261
if (fun.kind() == NT::DefFun) {
262262
IdGenIR id_gen;
263-
EmitDefFun(fun, ta, &id_gen);
263+
EmitDefFun(fun, ta, &id_gen, fout);
264264
}
265265
}
266266
}
@@ -297,11 +297,20 @@ int main(int argc, const char* argv[]) {
297297
InitParser();
298298
ts.RecordDuration("after_initialization");
299299

300-
std::vector<Path> seed_modules;
301-
for (int i = arg_start; i < argc; ++i) {
302-
seed_modules.push_back(std::filesystem::absolute((argv[i])));
300+
if (arg_start + 2 != argc) {
301+
std::cout << "Expected exactly one input and one output file\n";
302+
return 1;
303+
}
304+
std::ofstream foutFile;
305+
std::ostream* fout = &std::cout;
306+
if (argv[argc - 1] != std::string_view("-")) {
307+
foutFile.open(argv[argc - 1]);
308+
fout = &foutFile;
303309
}
304310

311+
std::vector<Path> seed_modules;
312+
seed_modules.push_back(std::filesystem::absolute((argv[arg_start])));
313+
305314
ModPool mp = ReadModulesRecursively(sw_stdlib.Value(), seed_modules, true);
306315
std::set<NT> eliminated_nodes = {NT::Import, NT::ModParam};
307316
SanityCheckMods("after_parsing", mp.mods_in_topo_order, eliminated_nodes,
@@ -404,7 +413,7 @@ int main(int argc, const char* argv[]) {
404413
SanityCheckMods("after_name_cleanup", mp.mods_in_topo_order, eliminated_nodes,
405414
COMPILE_STAGE::AFTER_DESUGAR, &tc, &ts);
406415

407-
PhaseEmitCode(mp.mods_in_topo_order, *ta);
416+
PhaseEmitCode(mp.mods_in_topo_order, *ta, fout);
408417
ts.RecordDuration("after_emitting_code");
409418

410419
if (sw_dump_stats.Value()) {

FE/compiler.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
import pathlib
88
import os
9+
import sys
910

1011
from typing import Any, Optional
1112

@@ -196,24 +197,24 @@ def PhaseLegalize(mod_topo_order: list[cwast.DefMod], tc: type_corpus.TypeCorpus
196197
canonicalize.FunRewriteComplexAssignments(fun, tc)
197198

198199

199-
def PhaseEmitCode(mod_topo_order: list[cwast.DefMod], ta: type_corpus.TargetArchConfig):
200+
def PhaseEmitCode(mod_topo_order: list[cwast.DefMod], ta: type_corpus.TargetArchConfig, fp):
200201
sig_names: set[str] = set()
201202
for mod in mod_topo_order:
202203
for fun in mod.body_mod:
203204
# This is really only needed for signature used by JSRs
204205
if isinstance(fun, cwast.DefFun):
205206
sn = emit_ir.MakeFunSigName(fun.x_type)
206207
if sn not in sig_names:
207-
emit_ir.EmitFunctionHeader(sn, "SIGNATURE", fun.x_type)
208+
emit_ir.EmitFunctionHeader(sn, "SIGNATURE", fun.x_type, fp)
208209
sig_names.add(sn)
209210

210211
for mod in mod_topo_order:
211212
for node in mod.body_mod:
212213
if isinstance(node, cwast.DefGlobal):
213-
emit_ir.EmitDefGlobal(node, ta)
214+
emit_ir.EmitDefGlobal(node, ta, fp)
214215
for node in mod.body_mod:
215216
if isinstance(node, cwast.DefFun):
216-
emit_ir.EmitDefFun(node, ta, identifier.IdGenIR())
217+
emit_ir.EmitDefFun(node, ta, identifier.IdGenIR(), fp)
217218

218219

219220
def main() -> int:
@@ -241,7 +242,8 @@ def main() -> int:
241242
logging.basicConfig(level=logging.WARN)
242243
# typify.logger.setLevel(logging.INFO)
243244
logger.info("Start Parsing")
244-
assert len(args.files) == 1
245+
assert len(args.files) == 2
246+
fout = sys.stdout if args.files[1] == "-" else open(args.files[1], "w")
245247
fn = args.files[0]
246248
fn, ext = os.path.splitext(fn)
247249
assert ext in (".cw", ".cws")
@@ -386,8 +388,7 @@ def main() -> int:
386388
SanityCheckMods("after_name_cleanup", checker.COMPILE_STAGE.AFTER_DESUGAR,
387389
args,
388390
mod_topo_order, tc, eliminated_nodes)
389-
390-
PhaseEmitCode(mod_topo_order, ta)
391+
PhaseEmitCode(mod_topo_order, ta, fout)
391392

392393
return 0
393394

0 commit comments

Comments
 (0)