Joos1W Compiler Framework
All Classes Functions Typedefs Pages
CompilationUnit.cc
1 #include "tir/CompilationUnit.h"
2 
3 #include <iostream>
4 #include <ostream>
5 
6 #include "tir/Constant.h"
7 
8 namespace tir {
9 
10 CompilationUnit::CompilationUnit(Context& ctx) : ctx_{ctx}, globals_{ctx.alloc()} {
11  // Declare the "ptr* __malloc(i32)" function
12  {
13  auto fnty = FunctionType::get(
14  ctx, Type::getPointerTy(ctx), {Type::getInt32Ty(ctx)});
15  auto fn = CreateFunction(fnty, "__malloc");
16  fn->setExternalLinkage();
17  }
18  // Declare the "void __exception()" function
19  {
20  auto fnty = FunctionType::get(ctx, Type::getVoidTy(ctx), {});
21  auto fn = CreateFunction(fnty, "__exception");
22  fn->setNoReturn();
23  fn->setExternalLinkage();
24  }
25 }
26 
27 std::ostream& CompilationUnit::print(std::ostream& os) const {
28  // Print all the struct types
29  for(auto const* structTy : ctx_.pimpl().structTypes) {
30  structTy->printDetail(os) << "\n";
31  }
32 
33  // Print all the functions
34  for(auto* func : functions()) {
35  func->print(os) << "\n";
36  }
37  return os;
38 }
39 
40 void CompilationUnit::dump() const { print(std::cerr) << "\n"; }
41 
42 } // namespace tir