added a roadmap
[ozulis.git] / src / compiler.cc
blob3f708ec2651b8a88396ee57e424298cf4f1e83aa
1 #include <iostream>
2 #include <fstream>
3 #include <algorithm>
4 #include <boost/foreach.hpp>
6 #include <core/assert.hh>
7 #include <core/string.hh>
8 #include <core/process.hh>
9 #include <ast/ast.hh>
10 #include <ast/ascii-visitor.hh>
11 #include <ast/llvm-generator-visitor.hh>
12 #include <ast/type-checker-visitor.hh>
13 #include <ast/simplify-visitor.hh>
14 #include <ast/scope-builder-visitor.hh>
15 #include <lang/parser-factory.hh>
16 #include <compiler.hh>
17 #include <task.hh>
18 #include <parse-task.hh>
20 Compiler::Compiler()
21 : tasks_(),
22 astDumpParse_(false),
23 astDumpTypeCheck_(false),
24 astDumpSimplify_(false)
28 Compiler::~Compiler()
32 void
33 Compiler::addTask(Task * task)
35 assert(task);
36 std::vector<Task *> * deps = task->dependancies();
37 if (deps)
39 BOOST_FOREACH(Task * dep, *deps)
40 addTask(dep);
41 delete deps;
43 tasks_.push_back(task);
46 void
47 Compiler::compile()
49 std::sort(tasks_.begin(), tasks_.end());
50 std::unique(tasks_.begin(), tasks_.end());
51 BOOST_FOREACH(Task * task, tasks_)
53 assert(task);
54 task->accept(*this);
58 void
59 Compiler::work(Task & /*task*/)
61 assert(false);
64 void
65 Compiler::work(ParseTask & task)
67 ast::File * file = lang::ParserFactory::parseFile(task.filename);
68 file->path = task.filename;
69 if (astDumpParse_) {
70 std::cout << "Parsed AST" << std::endl;
71 ast::AsciiVisitor v;
72 file->accept(v);
75 ast::ScopeBuilderVisitor sbv;
76 file->accept(sbv);
78 ast::TypeCheckerVisitor tcv;
79 file->accept(tcv);
80 if (astDumpTypeCheck_) {
81 std::cout << "TypeChecked AST" << std::endl;
82 ast::AsciiVisitor v;
83 file->accept(v);
86 ast::SimplifyVisitor sv;
87 file->accept(sv);
88 if (astDumpSimplify_) {
89 std::cout << "Simplified AST" << std::endl;
90 ast::AsciiVisitor v;
91 file->accept(v);
94 std::string asm_filename(task.filename);
95 core::replace_extention(asm_filename, "ll");
96 std::ofstream asm_stream(asm_filename.c_str(),
97 std::ios_base::out | std::ios_base::trunc);
98 ast::LLVMGeneratorVisitor lgv(asm_stream);
99 file->accept(lgv);
100 asm_stream.close();
102 core::Process llvm_as;
103 llvm_as.setBinary("llvm-as");
104 llvm_as.addArg("-f");
105 llvm_as.addArg(asm_filename);
106 llvm_as.run();
107 assert(!llvm_as.wait());
109 std::string bc_filename(asm_filename);
110 core::replace_extention(bc_filename, "bc");
111 std::string bin_filename(asm_filename);
112 core::replace_extention(bin_filename, "");
113 core::Process llvm_ld;
114 llvm_ld.setBinary("llvm-ld");
115 llvm_ld.addArg("-native");
116 llvm_ld.addArg("-o");
117 llvm_ld.addArg(bin_filename);
118 llvm_ld.addArg(bc_filename);
119 llvm_ld.run();
120 assert(!llvm_ld.wait());