blob: 17ba7dba33117533c2b434df9f98fb16c1ad001f [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"
20#include "lldb/Expression/ClangExpressionParser.h"
21#include "lldb/Expression/ClangUtilityFunction.h"
22#include "lldb/Host/Host.h"
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Target.h"
25
26using namespace lldb_private;
27
28//------------------------------------------------------------------
29/// Constructor
30///
31/// @param[in] text
32/// The text of the function. Must be a full translation unit.
33///
34/// @param[in] name
35/// The name of the function, as used in the text.
36//------------------------------------------------------------------
37ClangUtilityFunction::ClangUtilityFunction (const char *text,
38 const char *name) :
39 m_function_text(text),
Sean Callananf18d91c2010-09-01 00:58:00 +000040 m_function_name(name),
41 m_jit_begin(LLDB_INVALID_ADDRESS),
42 m_jit_end(LLDB_INVALID_ADDRESS)
Sean Callanan830a9032010-08-27 23:31:21 +000043{
44}
45
46//------------------------------------------------------------------
47/// Install the utility function into a process
48///
49/// @param[in] error_stream
50/// A stream to print parse errors and warnings to.
51///
52/// @param[in] exe_ctx
53/// The execution context to install the utility function to.
54///
55/// @return
56/// True on success (no errors); false otherwise.
57//------------------------------------------------------------------
58bool
59ClangUtilityFunction::Install (Stream &error_stream,
60 ExecutionContext &exe_ctx)
Sean Callananf18d91c2010-09-01 00:58:00 +000061{
62 if (m_jit_begin != LLDB_INVALID_ADDRESS)
63 {
64 error_stream.PutCString("error: already installed\n");
65 return false;
66 }
67
Sean Callanan830a9032010-08-27 23:31:21 +000068 ////////////////////////////////////
69 // Set up the target and compiler
70 //
71
72 Target *target = exe_ctx.target;
73
74 if (!target)
75 {
76 error_stream.PutCString ("error: invalid target\n");
77 return false;
78 }
79
80 ConstString target_triple;
81
82 target->GetTargetTriple (target_triple);
83
84 if (!target_triple)
85 target_triple = Host::GetTargetTriple ();
86
87 if (!target_triple)
88 {
89 error_stream.PutCString ("error: invalid target triple\n");
90 return false;
91 }
92
93 //////////////////////////
94 // Parse the expression
95 //
96
97 ClangExpressionParser parser(target_triple.GetCString(), *this);
98
99 unsigned num_errors = parser.Parse (error_stream);
100
101 if (num_errors)
102 {
103 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
104 return false;
105 }
106
107 //////////////////////////////////
108 // JIT the output of the parser
109 //
110
111 Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
112
113 if (jit_error.Success())
114 {
115 return true;
116 }
117 else
118 {
119 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
120 return false;
121 }
122}
123
124