40 llvm::Function *function = ctx.module->getFunction(node.
function_name);
42 if (function->arg_size() < node.
arguments.size())
44 throw std::runtime_error(
"Aaaaaahhhhhhh");
47 std::vector<llvm::Value *> argument_values;
48 argument_values.reserve(node.
arguments.size());
51 llvm::Value *arg_value = std::visit(*
this, *argument);
52 argument_values.push_back(arg_value);
55 return this->builder.CreateCall(function, argument_values,
"calltmp");
95 llvm::BasicBlock *parent = this->builder.GetInsertBlock();
98 std::vector<llvm::Type *> argument_types(node.
args.size(), llvm::Type::getDoubleTy(ctx.llvm_ctx));
99 llvm::FunctionType *function_type = llvm::FunctionType::get(llvm::Type::getDoubleTy(ctx.llvm_ctx), argument_types,
false);
100 llvm::Function *function = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, node.
name, *ctx.module);
102 this->ctx.scope.Insert(node.
name, function);
105 for (
auto &arg: function->args())
107 arg.setName(node.
args[idx++]);
111 llvm::BasicBlock *block = llvm::BasicBlock::Create(ctx.llvm_ctx, node.
name, function);
112 this->builder.SetInsertPoint(block);
114 ctx.scope.PushLayer();
115 for (
auto &arg: function->args())
117 ctx.scope.Insert(std::string(arg.getName()), &arg);
120 llvm::Value *return_value = std::visit(*
this, *node.
body);
122 this->builder.CreateRet(return_value);
124 ctx.scope.PopLayer();
126 if (llvm::verifyFunction(*function, &llvm::errs()))
128 throw std::runtime_error(
"function has errors");
132 this->builder.SetInsertPoint(parent);
139 llvm::Value *scene = *this->ctx.scope.Lookup(
"__scene");
140 llvm::Value *name = this->builder.CreateGlobalStringPtr(node.
function_name);
142 auto function = this->ctx.module->getFunction(node.
function_name);
145 throw std::runtime_error(std::format(
"Function \"{}\" could not be found!", node.
function_name));
148 auto scene_add_plot = this->ctx.module->getFunction(
"unlogic_scene_add_plot");
150 std::array<llvm::Value *, 3> args = {scene, name, function};
157 std::array<llvm::Type *, 1> args = {
158 llvm::PointerType::getUnqual(this->ctx.llvm_ctx),
160 llvm::FunctionType *entry_type = llvm::FunctionType::get(llvm::Type::getVoidTy(this->ctx.llvm_ctx), args,
false);
161 llvm::Function *entry = llvm::Function::Create(entry_type, llvm::Function::ExternalLinkage,
"__entry", *this->ctx.module);
163 llvm::BasicBlock *block = llvm::BasicBlock::Create(ctx.llvm_ctx,
"__entry", entry);
165 this->ctx.scope.PushLayer();
167 this->ctx.scope.Insert(
"__scene", entry->getArg(0));
169 this->builder.SetInsertPoint(block);
171 std::visit(*
this, *node.
body);
173 this->builder.CreateRetVoid();
174 this->ctx.scope.PopLayer();
176 if (llvm::verifyFunction(*entry, &llvm::errs()))
178 throw std::runtime_error(
"function has errors");
LibrarySymbol scene_add_plot(runtime, "unlogic_scene_add_plot",(void *) unlogic_scene_add_plot, [](llvm::LLVMContext &ctx, llvm::Module &mod) { std::array< llvm::Type *, 1 > plot_function_args={ llvm::Type::getDoubleTy(ctx), };llvm::FunctionType *plot_function_type=llvm::FunctionType::get(llvm::Type::getDoubleTy(ctx), plot_function_args, false);std::array< llvm::Type *, 3 > args={ llvm::PointerType::getUnqual(ctx), llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(ctx)), llvm::PointerType::getUnqual(plot_function_type), };llvm::Type *ret=llvm::Type::getVoidTy(ctx);llvm::FunctionType *fn=llvm::FunctionType::get(ret, args, false);llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_scene_add_plot", mod);})
LibrarySymbol std_pow(stdlib, "pow",(void *) unlogic_std_pow, [](llvm::LLVMContext &ctx, llvm::Module &mod) { std::array args={ llvm::Type::getDoubleTy(ctx), llvm::Type::getDoubleTy(ctx), };llvm::Type *ret=llvm::Type::getDoubleTy(ctx);llvm::FunctionType *fn=llvm::FunctionType::get(ret, args, false);llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "pow", mod);})
llvm::LLVMContext & llvm_ctx