blob: 9d9b3d1d849f5b22ab488668d81a0ccdec45571f [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 }
Greg Clayton395fc332011-02-15 21:59:32 +000084
Sean Callanan830a9032010-08-27 23:31:21 +000085 //////////////////////////
86 // Parse the expression
87 //
Sean Callanane8a59a82010-09-13 21:34:21 +000088
Sean Callanan6a925532011-01-13 08:53:35 +000089 bool keep_result_in_memory = false;
90
91 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callananaa301c42010-12-03 01:38:59 +000092
93 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan830a9032010-08-27 23:31:21 +000094
Greg Clayton395fc332011-02-15 21:59:32 +000095 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this);
Sean Callanan830a9032010-08-27 23:31:21 +000096
97 unsigned num_errors = parser.Parse (error_stream);
98
99 if (num_errors)
100 {
101 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanane8a59a82010-09-13 21:34:21 +0000102
103 m_expr_decl_map.reset();
104
Sean Callanan830a9032010-08-27 23:31:21 +0000105 return false;
106 }
107
108 //////////////////////////////////
109 // JIT the output of the parser
110 //
111
Greg Claytond0882d02011-01-19 23:00:49 +0000112 Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx);
113
114 if (exe_ctx.process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
115 m_jit_process_sp = exe_ctx.process->GetSP();
Sean Callanan830a9032010-08-27 23:31:21 +0000116
Greg Claytonc71899e2011-01-18 19:36:39 +0000117#if 0
118 // jingham: look here
119 StreamFile logfile ("/tmp/exprs.txt", "a");
120 logfile.Printf ("0x%16.16llx: func = %s, source =\n%s\n",
Greg Claytond0882d02011-01-19 23:00:49 +0000121 m_jit_start_addr,
Greg Claytonc71899e2011-01-18 19:36:39 +0000122 m_function_name.c_str(),
123 m_function_text.c_str());
124#endif
125
Sean Callananaa301c42010-12-03 01:38:59 +0000126 m_expr_decl_map->DidParse();
127
Sean Callanane8a59a82010-09-13 21:34:21 +0000128 m_expr_decl_map.reset();
129
Sean Callanan830a9032010-08-27 23:31:21 +0000130 if (jit_error.Success())
131 {
132 return true;
133 }
134 else
135 {
136 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
137 return false;
138 }
139}
140
141