1435933ddSDimitry Andric //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
29f2f44ceSEd Maste //
39f2f44ceSEd Maste //                     The LLVM Compiler Infrastructure
49f2f44ceSEd Maste //
59f2f44ceSEd Maste // This file is distributed under the University of Illinois Open Source
69f2f44ceSEd Maste // License. See LICENSE.TXT for details.
79f2f44ceSEd Maste //
89f2f44ceSEd Maste //===----------------------------------------------------------------------===//
99f2f44ceSEd Maste 
109f2f44ceSEd Maste #include <stdio.h>
119f2f44ceSEd Maste #if HAVE_SYS_TYPES_H
129f2f44ceSEd Maste #include <sys/types.h>
139f2f44ceSEd Maste #endif
149f2f44ceSEd Maste 
159f2f44ceSEd Maste 
169f2f44ceSEd Maste #include "lldb/Core/Module.h"
179f2f44ceSEd Maste #include "lldb/Core/StreamFile.h"
184bb0738eSEd Maste #include "lldb/Expression/DiagnosticManager.h"
199f2f44ceSEd Maste #include "lldb/Expression/ExpressionSourceCode.h"
204bb0738eSEd Maste #include "lldb/Expression/FunctionCaller.h"
219f2f44ceSEd Maste #include "lldb/Expression/IRExecutionUnit.h"
224bb0738eSEd Maste #include "lldb/Expression/UtilityFunction.h"
239f2f44ceSEd Maste #include "lldb/Host/Host.h"
249f2f44ceSEd Maste #include "lldb/Target/ExecutionContext.h"
259f2f44ceSEd Maste #include "lldb/Target/Process.h"
269f2f44ceSEd Maste #include "lldb/Target/Target.h"
27f678e45dSDimitry Andric #include "lldb/Utility/ConstString.h"
28f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
29f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
309f2f44ceSEd Maste 
319f2f44ceSEd Maste using namespace lldb_private;
329f2f44ceSEd Maste using namespace lldb;
339f2f44ceSEd Maste 
349f2f44ceSEd Maste //------------------------------------------------------------------
359f2f44ceSEd Maste /// Constructor
369f2f44ceSEd Maste ///
379f2f44ceSEd Maste /// @param[in] text
389f2f44ceSEd Maste ///     The text of the function.  Must be a full translation unit.
399f2f44ceSEd Maste ///
409f2f44ceSEd Maste /// @param[in] name
419f2f44ceSEd Maste ///     The name of the function, as used in the text.
429f2f44ceSEd Maste //------------------------------------------------------------------
UtilityFunction(ExecutionContextScope & exe_scope,const char * text,const char * name)439f2f44ceSEd Maste UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
44435933ddSDimitry Andric                                  const char *text, const char *name)
45435933ddSDimitry Andric     : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
469f2f44ceSEd Maste       m_function_text(ExpressionSourceCode::g_expression_prefix),
47435933ddSDimitry Andric       m_function_name(name) {
489f2f44ceSEd Maste   if (text && text[0])
499f2f44ceSEd Maste     m_function_text.append(text);
509f2f44ceSEd Maste }
519f2f44ceSEd Maste 
~UtilityFunction()52435933ddSDimitry Andric UtilityFunction::~UtilityFunction() {
539f2f44ceSEd Maste   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
54435933ddSDimitry Andric   if (process_sp) {
559f2f44ceSEd Maste     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
569f2f44ceSEd Maste     if (jit_module_sp)
579f2f44ceSEd Maste       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
589f2f44ceSEd Maste   }
599f2f44ceSEd Maste }
609f2f44ceSEd Maste 
61435933ddSDimitry Andric // FIXME: We should check that every time this is called it is called with the
62435933ddSDimitry Andric // same return type & arguments...
639f2f44ceSEd Maste 
MakeFunctionCaller(const CompilerType & return_type,const ValueList & arg_value_list,lldb::ThreadSP thread_to_use_sp,Status & error)64435933ddSDimitry Andric FunctionCaller *UtilityFunction::MakeFunctionCaller(
65435933ddSDimitry Andric     const CompilerType &return_type, const ValueList &arg_value_list,
665517e702SDimitry Andric     lldb::ThreadSP thread_to_use_sp, Status &error) {
679f2f44ceSEd Maste   if (m_caller_up)
689f2f44ceSEd Maste     return m_caller_up.get();
699f2f44ceSEd Maste 
709f2f44ceSEd Maste   ProcessSP process_sp = m_jit_process_wp.lock();
71435933ddSDimitry Andric   if (!process_sp) {
724bb0738eSEd Maste     error.SetErrorString("Can't make a function caller without a process.");
739f2f44ceSEd Maste     return nullptr;
744bb0738eSEd Maste   }
759f2f44ceSEd Maste 
769f2f44ceSEd Maste   Address impl_code_address;
779f2f44ceSEd Maste   impl_code_address.SetOffset(StartAddress());
789f2f44ceSEd Maste   std::string name(m_function_name);
799f2f44ceSEd Maste   name.append("-caller");
809f2f44ceSEd Maste 
81435933ddSDimitry Andric   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
82435933ddSDimitry Andric       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
839f2f44ceSEd Maste       error));
84435933ddSDimitry Andric   if (error.Fail()) {
859f2f44ceSEd Maste 
869f2f44ceSEd Maste     return nullptr;
879f2f44ceSEd Maste   }
88435933ddSDimitry Andric   if (m_caller_up) {
894bb0738eSEd Maste     DiagnosticManager diagnostics;
904bb0738eSEd Maste 
91435933ddSDimitry Andric     unsigned num_errors =
92435933ddSDimitry Andric         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
93435933ddSDimitry Andric     if (num_errors) {
94435933ddSDimitry Andric       error.SetErrorStringWithFormat(
95435933ddSDimitry Andric           "Error compiling %s caller function: \"%s\".",
96435933ddSDimitry Andric           m_function_name.c_str(), diagnostics.GetString().c_str());
979f2f44ceSEd Maste       m_caller_up.reset();
989f2f44ceSEd Maste       return nullptr;
999f2f44ceSEd Maste     }
1009f2f44ceSEd Maste 
1014bb0738eSEd Maste     diagnostics.Clear();
1029f2f44ceSEd Maste     ExecutionContext exe_ctx(process_sp);
1039f2f44ceSEd Maste 
104435933ddSDimitry Andric     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
105435933ddSDimitry Andric       error.SetErrorStringWithFormat(
106435933ddSDimitry Andric           "Error inserting caller function for %s: \"%s\".",
107435933ddSDimitry Andric           m_function_name.c_str(), diagnostics.GetString().c_str());
1089f2f44ceSEd Maste       m_caller_up.reset();
1099f2f44ceSEd Maste       return nullptr;
1109f2f44ceSEd Maste     }
1119f2f44ceSEd Maste   }
1129f2f44ceSEd Maste   return m_caller_up.get();
1139f2f44ceSEd Maste }
114