blob: d3ede6b4a34ab64ad19b3423fce684f28637a23f [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) :
41 m_function_text(text),
Sean Callananf18d91c2010-09-01 00:58:00 +000042 m_function_name(name),
43 m_jit_begin(LLDB_INVALID_ADDRESS),
44 m_jit_end(LLDB_INVALID_ADDRESS)
Sean Callanan830a9032010-08-27 23:31:21 +000045{
46}
47
Greg Clayton6a5aa8a2010-09-24 23:07:41 +000048ClangUtilityFunction::~ClangUtilityFunction ()
49{
50}
51
Sean Callanan830a9032010-08-27 23:31:21 +000052//------------------------------------------------------------------
53/// Install the utility function into a process
54///
55/// @param[in] error_stream
56/// A stream to print parse errors and warnings to.
57///
58/// @param[in] exe_ctx
59/// The execution context to install the utility function to.
60///
61/// @return
62/// True on success (no errors); false otherwise.
63//------------------------------------------------------------------
64bool
65ClangUtilityFunction::Install (Stream &error_stream,
66 ExecutionContext &exe_ctx)
Sean Callananf18d91c2010-09-01 00:58:00 +000067{
68 if (m_jit_begin != LLDB_INVALID_ADDRESS)
69 {
70 error_stream.PutCString("error: already installed\n");
71 return false;
72 }
73
Sean Callanan830a9032010-08-27 23:31:21 +000074 ////////////////////////////////////
75 // Set up the target and compiler
76 //
77
78 Target *target = exe_ctx.target;
79
80 if (!target)
81 {
82 error_stream.PutCString ("error: invalid target\n");
83 return false;
84 }
85
86 ConstString target_triple;
87
88 target->GetTargetTriple (target_triple);
89
90 if (!target_triple)
91 target_triple = Host::GetTargetTriple ();
92
93 if (!target_triple)
94 {
95 error_stream.PutCString ("error: invalid target triple\n");
96 return false;
97 }
98
99 //////////////////////////
100 // Parse the expression
101 //
Sean Callanane8a59a82010-09-13 21:34:21 +0000102
Sean Callanan6a925532011-01-13 08:53:35 +0000103 bool keep_result_in_memory = false;
104
105 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callananaa301c42010-12-03 01:38:59 +0000106
107 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan830a9032010-08-27 23:31:21 +0000108
Sean Callananc7674af2011-01-17 23:42:46 +0000109 ClangExpressionParser parser(target_triple.GetCString(), exe_ctx.process, *this);
Sean Callanan830a9032010-08-27 23:31:21 +0000110
111 unsigned num_errors = parser.Parse (error_stream);
112
113 if (num_errors)
114 {
115 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanane8a59a82010-09-13 21:34:21 +0000116
117 m_expr_decl_map.reset();
118
Sean Callanan830a9032010-08-27 23:31:21 +0000119 return false;
120 }
121
122 //////////////////////////////////
123 // JIT the output of the parser
124 //
125
126 Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
127
Greg Claytonc71899e2011-01-18 19:36:39 +0000128#if 0
129 // jingham: look here
130 StreamFile logfile ("/tmp/exprs.txt", "a");
131 logfile.Printf ("0x%16.16llx: func = %s, source =\n%s\n",
132 m_jit_begin,
133 m_function_name.c_str(),
134 m_function_text.c_str());
135#endif
136
Sean Callananaa301c42010-12-03 01:38:59 +0000137 m_expr_decl_map->DidParse();
138
Sean Callanane8a59a82010-09-13 21:34:21 +0000139 m_expr_decl_map.reset();
140
Sean Callanan830a9032010-08-27 23:31:21 +0000141 if (jit_error.Success())
142 {
143 return true;
144 }
145 else
146 {
147 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
148 return false;
149 }
150}
151
152