MCPcopy Create free account
hub / github.com/chigraph/chigraph / CFuncNode

Class CFuncNode

libchigraph/src/GraphModule.cpp:176–316  ·  view source on GitHub ↗

The NodeType for calling C functions

Source from the content-addressed store, hash-verified

174
175/// The NodeType for calling C functions
176struct CFuncNode : NodeType {
177 CFuncNode(GraphModule& mod, std::string cCode, std::string functionName,
178 std::vector<std::string> extraArgs, std::vector<NamedDataType> inputs,
179 DataType output)
180 : NodeType{mod, "c-call", "call C code"},
181 mFunctionName{std::move(functionName)},
182 mCCode(std::move(cCode)),
183 mExtraArguments{std::move(extraArgs)},
184 mInputs{std::move(inputs)},
185 mOutput{std::move(output)},
186 mGraphModule{&mod} {
187 setExecInputs({""});
188 setExecOutputs({""});
189
190 setDataInputs(mInputs);
191 if (mOutput.valid()) { setDataOutputs({{"", mOutput}}); }
192 }
193
194 Result codegen(
195 size_t /*inID*/, const llvm::DebugLoc& nodeLocation, const std::vector<llvm::Value*>& io,
196 llvm::BasicBlock* codegenInto, const std::vector<llvm::BasicBlock*>& outputBlocks,
197 std::unordered_map<std::string, std::shared_ptr<void>>& /*compileCache*/) override {
198 assert(io.size() == dataInputs().size() + dataOutputs().size() && codegenInto != nullptr &&
199 outputBlocks.size() == 1);
200
201 Result res;
202
203 // compile the c code if it hasn't already been compiled
204 if (llcompiledmod == nullptr) {
205 auto args = mExtraArguments;
206
207 // add -I for the .c dir
208 args.push_back("-I");
209 args.push_back(mGraphModule->pathToCSources().string());
210
211 llcompiledmod = compileCCode(llvm::sys::fs::getMainExecutable(nullptr, nullptr).c_str(),
212 mCCode, args, context().llvmContext(), res);
213
214 if (!res) { return res; }
215 }
216
217 // create a copy of the module
218 auto copymod = llvm::CloneModule(llcompiledmod.get());
219
220 // link it in
221
222 auto parentModule = codegenInto
223 ->
224#if LLVM_VERSION_LESS_EQUAL(3, 6)
225 getParent()
226 ->getParent()
227#else
228 getModule()
229#endif
230 ;
231
232#if LLVM_VERSION_LESS_EQUAL(3, 7)
233 llvm::Linker::LinkModules(parentModule, copymod

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected