aboutsummaryrefslogtreecommitdiff
path: root/gcc/go/gofrontend/expressions.cc
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2016-09-22 20:32:16 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2016-09-22 20:32:16 +0000
commit0cb904afbe28927d4efb343558e641e35580d72c (patch)
treec603e6261575dd4da3b11d23174440467f68b4db /gcc/go/gofrontend/expressions.cc
parent1c681c7bf7e28ceeeb50e90444b89f833f7bcc23 (diff)
downloadgcc-0cb904afbe28927d4efb343558e641e35580d72c.zip
gcc-0cb904afbe28927d4efb343558e641e35580d72c.tar.gz
gcc-0cb904afbe28927d4efb343558e641e35580d72c.tar.bz2
compiler: compile runtime.getcaller{pc,sp} into builtin functions
The runtime functions runtime.getcallerpc and runtime.getcallersp are intended to be efficient ways to get the return and frame address of the caller (that is, the caller of runtime.getcallerpc). In the C code that is implemented by simply using C macros: This patch essentially implements those macros in the Go code. It would be nice if we could just use //extern for this, but it doesn't work because the runtime code passes the right argument. Of course we could change the runtime code, but these are common enough that I'd prefer to avoid the difference from the gc version of the runtime code. This patch corrects the existing declaration of __builtin_return_address to use uint32, rather than uint, for the parameter type. The builtin functions take the C type "unsigned int", which for the targets we use corresponds to the Go type uint32. Not that it should matter, really. Reviewed-on: https://go-review.googlesource.com/29653 From-SVN: r240382
Diffstat (limited to 'gcc/go/gofrontend/expressions.cc')
-rw-r--r--gcc/go/gofrontend/expressions.cc61
1 files changed, 61 insertions, 0 deletions
diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc
index b893d2f..84c578d 100644
--- a/gcc/go/gofrontend/expressions.cc
+++ b/gcc/go/gofrontend/expressions.cc
@@ -8897,6 +8897,45 @@ Call_expression::do_lower(Gogo* gogo, Named_object* function,
bme->location());
}
+ // Handle a couple of special runtime functions. In the runtime
+ // package, getcallerpc returns the PC of the caller, and
+ // getcallersp returns the frame pointer of the caller. Implement
+ // these by turning them into calls to GCC builtin functions. We
+ // could implement them in normal code, but then we would have to
+ // explicitly unwind the stack. These functions are intended to be
+ // efficient. Note that this technique obviously only works for
+ // direct calls, but that is the only way they are used. The actual
+ // argument to these functions is always the address of a parameter;
+ // we don't need that for the GCC builtin functions, so we just
+ // ignore it.
+ if (gogo->compiling_runtime()
+ && this->args_ != NULL
+ && this->args_->size() == 1
+ && gogo->package_name() == "runtime")
+ {
+ Func_expression* fe = this->fn_->func_expression();
+ if (fe != NULL
+ && fe->named_object()->is_function_declaration()
+ && fe->named_object()->package() == NULL)
+ {
+ std::string n = Gogo::unpack_hidden_name(fe->named_object()->name());
+ if (n == "getcallerpc")
+ {
+ static Named_object* builtin_return_address;
+ return this->lower_to_builtin(&builtin_return_address,
+ "__builtin_return_address",
+ 0);
+ }
+ else if (n == "getcallersp")
+ {
+ static Named_object* builtin_frame_address;
+ return this->lower_to_builtin(&builtin_frame_address,
+ "__builtin_frame_address",
+ 1);
+ }
+ }
+ }
+
return this;
}
@@ -8997,6 +9036,28 @@ Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
this->varargs_are_lowered_ = true;
}
+// Return a call to __builtin_return_address or __builtin_frame_address.
+
+Expression*
+Call_expression::lower_to_builtin(Named_object** pno, const char* name,
+ int arg)
+{
+ if (*pno == NULL)
+ *pno = Gogo::declare_builtin_rf_address(name);
+
+ Location loc = this->location();
+
+ Expression* fn = Expression::make_func_reference(*pno, NULL, loc);
+ Expression* a = Expression::make_integer_ul(arg, NULL, loc);
+ Expression_list *args = new Expression_list();
+ args->push_back(a);
+ Expression* call = Expression::make_call(fn, args, false, loc);
+
+ // The builtin functions return void*, but the Go functions return uintptr.
+ Type* uintptr_type = Type::lookup_integer_type("uintptr");
+ return Expression::make_cast(uintptr_type, call, loc);
+}
+
// Flatten a call with multiple results into a temporary.
Expression*