unlogic
Loading...
Searching...
No Matches
IRGenerator.cpp
Go to the documentation of this file.
1#include "IRGenerator.h"
2#include <format>
3
5{
6 return builder.CreateConstant<double>(node.value);
7}
8
10{
11 return nullptr;
12 // return this->builder.CreateGlobalStringPtr(node.value);
13}
14
16{
17 city::Value *lhs = std::visit(*this, *node.lhs);
18 city::Value *rhs = std::visit(*this, *node.rhs);
19
20 return this->builder.InsertDivInst(lhs, rhs);
21}
22
24{
25 for (auto &statement: node.statements)
26 {
27 std::visit(*this, *statement);
28 }
29
30 return nullptr;
31}
32
34{
35 return *this->ctx.scope.Lookup(node.identifier);
36}
37
39{
40 city::Function *function = (*this->ctx.scope.Lookup(node.function_name))->ToFunction();
41
42 if (function == nullptr)
43 {
44 throw std::runtime_error("function does not exist");
45 }
46
47 std::vector<city::Value *> arguments;
48 arguments.reserve(node.arguments.size());
49 for (auto &argument: node.arguments)
50 {
51 city::Value *arg_value = std::visit(*this, *argument);
52 arguments.push_back(arg_value);
53 }
54
55 return this->builder.InsertCallInst(function, arguments);
56}
57
59{
60 city::Value *lhs = std::visit(*this, *node.lhs);
61 city::Value *rhs = std::visit(*this, *node.rhs);
62
63 return this->builder.InsertAddInst(lhs, rhs);
64}
65
67{
68 city::Value *lhs = std::visit(*this, *node.lhs);
69 city::Value *rhs = std::visit(*this, *node.rhs);
70
71 return this->builder.InsertSubInst(lhs, rhs);
72}
73
75{
76 city::Value *lhs = std::visit(*this, *node.lhs);
77 city::Value *rhs = std::visit(*this, *node.rhs);
78
79 return this->builder.InsertMulInst(lhs, rhs);
80}
81
83{
84 city::Value *lhs = std::visit(*this, *node.lhs);
85 city::Value *rhs = std::visit(*this, *node.rhs);
86
87 city::Function *std_pow = (*this->ctx.scope.Lookup("__pow"))->ToFunction();
88
89 return this->builder.InsertCallInst(std_pow, {lhs, rhs});
90}
91
93{
94 // Save entry
95 city::IRBlock &parent = this->builder.GetInsertPoint();
96
97 // Generate function information
98 std::vector<city::Type> argument_types(node.args.size(), city::Type::Get<double>());
99 city::IRFunction *function = this->builder.CreateFunction(node.name, city::Type::Get<double>(), argument_types);
100
101 this->ctx.scope.Set(node.name, function);
102
103 ctx.scope.PushLayer();
104 for (auto const &[value, name]: std::views::zip(function->GetArgumentValues(), node.args))
105 {
106 ctx.scope.Set(name, value);
107 }
108
109 city::Value *retval = std::visit(*this, *node.body);
110
111 this->builder.InsertRetInst(retval);
112
113 ctx.scope.PopLayer();
114
115 // Return to parent block
116 this->builder.SetInsertPoint(parent);
117
118 return nullptr;
119}
120
122{
123 city::Value *scene = *this->ctx.scope.Lookup("__scene");
124
125 auto function = (*this->ctx.scope.Lookup(node.function_name))->ToFunction();
126 if (!function)
127 {
128 throw std::runtime_error(std::format("Function \"{}\" could not be found!", node.function_name));
129 }
130
131 auto plot_anon = (*this->ctx.scope.Lookup("__plot_anon"))->ToFunction();
132
133 return this->builder.InsertCallInst(plot_anon, {scene, function});
134}
135
137{
138 city::IRFunction *entry = this->builder.CreateFunction("__entry", city::Type::Get<double>(), {city::Type::Get<void *>()});
139
140 this->ctx.scope.PushLayer();
141 this->ctx.scope.Set("__scene", entry->GetArgumentValues()[0]);
142
143 std::visit(*this, *node.body);
144
145 this->builder.InsertRetInst();
146 this->ctx.scope.PopLayer();
147
148 return nullptr;
149}
150
151city::Value *unlogic::IRGenerator::operator()(std::monostate &node)
152{
153 throw std::runtime_error("Invalid Node!");
154}
bf::DefineNonTerminal< G, std::unique_ptr< Node > > statement
Definition Parser.cpp:156
UniqueNode rhs
Definition Node.h:73
UniqueNode lhs
Definition Node.h:73
std::vector< UniqueNode > arguments
Definition Node.h:65
std::string function_name
Definition Node.h:64
std::vector< std::string > args
Definition Node.h:112
city::Value * operator()(std::monostate &node)
city::IRBuilder builder
Definition IRGenerator.h:14
std::string function_name
Definition Node.h:122
std::vector< UniqueNode > statements
Definition Node.h:130
std::string identifier
Definition Node.h:56