blob: 03b15d1413c60e2b2d11c8fd999726335b04e716 [file] [log] [blame]
Sean Callanan830a9032010-08-27 23:31:21 +00001//===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <stdio.h>
12#if HAVE_SYS_TYPES_H
13# include <sys/types.h>
14#endif
15
16// C++ Includes
17
18#include "lldb/Core/ConstString.h"
19#include "lldb/Core/Stream.h"
Greg Claytonc71899e2011-01-18 19:36:39 +000020#include "lldb/Core/StreamFile.h"
Sean Callanane8a59a82010-09-13 21:34:21 +000021#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan830a9032010-08-27 23:31:21 +000022#include "lldb/Expression/ClangExpressionParser.h"
23#include "lldb/Expression/ClangUtilityFunction.h"
24#include "lldb/Host/Host.h"
25#include "lldb/Target/ExecutionContext.h"
26#include "lldb/Target/Target.h"
27
28using namespace lldb_private;
29
30//------------------------------------------------------------------
31/// Constructor
32///
33/// @param[in] text
34/// The text of the function. Must be a full translation unit.
35///
36/// @param[in] name
37/// The name of the function, as used in the text.
38//------------------------------------------------------------------
39ClangUtilityFunction::ClangUtilityFunction (const char *text,
40 const char *name) :
Greg Claytond0882d02011-01-19 23:00:49 +000041 ClangExpression (),
42 m_function_text (text),
43 m_function_name (name)
Sean Callanan830a9032010-08-27 23:31:21 +000044{
45}
46
Greg Clayton6a5aa8a2010-09-24 23:07:41 +000047ClangUtilityFunction::~ClangUtilityFunction ()
48{
49}
50
Sean Callanan830a9032010-08-27 23:31:21 +000051//------------------------------------------------------------------
52/// Install the utility function into a process
53///
54/// @param[in] error_stream
55/// A stream to print parse errors and warnings to.
56///
57/// @param[in] exe_ctx
58/// The execution context to install the utility function to.
59///
60/// @return
61/// True on success (no errors); false otherwise.
62//------------------------------------------------------------------
63bool
64ClangUtilityFunction::Install (Stream &error_stream,
65 ExecutionContext &exe_ctx)
Sean Callananf18d91c2010-09-01 00:58:00 +000066{
Greg Claytond0882d02011-01-19 23:00:49 +000067 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callananf18d91c2010-09-01 00:58:00 +000068 {
69 error_stream.PutCString("error: already installed\n");
70 return false;
71 }
72
Sean Callanan830a9032010-08-27 23:31:21 +000073 ////////////////////////////////////
74 // Set up the target and compiler
75 //
76
77 Target *target = exe_ctx.target;
78
79 if (!target)
80 {
81 error_stream.PutCString ("error: invalid target\n");
82 return false;
83 }
84
85 ConstString target_triple;
86
87 target->GetTargetTriple (target_triple);
88
89 if (!target_triple)
90 target_triple = Host::GetTargetTriple ();
91
92 if (!target_triple)
93 {
94 error_stream.PutCString ("error: invalid target triple\n");
95 return false;
96 }
97
98 //////////////////////////
99 // Parse the expression
100 //
Sean Callanane8a59a82010-09-13 21:34:21 +0000101
Sean Callanan6a925532011-01-13 08:53:35 +0000102 bool keep_result_in_memory = false;
103
104 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callananaa301c42010-12-03 01:38:59 +0000105
106 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan830a9032010-08-27 23:31:21 +0000107
Sean Callananc7674af2011-01-17 23:42:46 +0000108 ClangExpressionParser parser(target_triple.GetCString(), exe_ctx.process, *this);
Sean Callanan830a9032010-08-27 23:31:21 +0000109
110 unsigned num_errors = parser.Parse (error_stream);
111
112 if (num_errors)
113 {
114 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanane8a59a82010-09-13 21:34:21 +0000115
116 m_expr_decl_map.reset();
117
Sean Callanan830a9032010-08-27 23:31:21 +0000118 return false;
119 }
120
121 //////////////////////////////////
122 // JIT the output of the parser
123 //
124
Greg Claytond0882d02011-01-19 23:00:49 +0000125 Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx);
126
127 if (exe_ctx.process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
128 m_jit_process_sp = exe_ctx.process->GetSP();
Sean Callanan830a9032010-08-27 23:31:21 +0000129
Greg Claytonc71899e2011-01-18 19:36:39 +0000130#if 0
131 // jingham: look here
132 StreamFile logfile ("/tmp/exprs.txt", "a");
133 logfile.Printf ("0x%16.16llx: func = %s, source =\n%s\n",
Greg Claytond0882d02011-01-19 23:00:49 +0000134 m_jit_start_addr,
Greg Claytonc71899e2011-01-18 19:36:39 +0000135 m_function_name.c_str(),
136 m_function_text.c_str());
137#endif
138
Sean Callananaa301c42010-12-03 01:38:59 +0000139 m_expr_decl_map->DidParse();
140
Sean Callanane8a59a82010-09-13 21:34:21 +0000141 m_expr_decl_map.reset();
142
Sean Callanan830a9032010-08-27 23:31:21 +0000143 if (jit_error.Success())
144 {
145 return true;
146 }
147 else
148 {
149 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
150 return false;
151 }
152}
153
154