@@ -236,7 +236,7 @@ void PhaseLegalize(std::vector<Node>& mods_in_topo_order, TypeCorpus* tc) {
236236}
237237
238238void 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 ()) {
0 commit comments