Commit 706cd57f by Roberto Lublinerman Committed by Ian Lance Taylor

gccgo...

gccgo: 	fixes to ast-dump, refactoring of export and ast-dump
        to implement a common interface for writing basic
        type literals and added flags for optimization.

	* lang.opt: Add fgo-optimize-.
	* go-lang.c (go_langhook_handle_option): Handle OPT_fgo_optimize.
	* go-c.h (go_enable_optimize): Declare.
	* Make-lang.in (GO_OBJS): Add go/go-optimize.o.
	(GO_EXPORT_H): Define.
	(GO_IMPORT_H): Add $(GO_EXPORT_H).
	(GO_AST_DUMP_H): Define.
	(go/ast-dump.o, go/statements.o): Use GO_AST_DUMP_H.
	(go/export.o, go/gogo.o, go/import.o): Use GO_EXPORT_H.
	(go/types.o): Likewise.
	(go/expressions.o): Use GO_AST_DUMP_H and GO_EXPORT_H.
	(go/go-optimize.o): New target.

From-SVN: r178046
parent 8020d73e
2011-08-24 Roberto Lublinerman <rluble@gmail.com>
* lang.opt: Add fgo-optimize-.
* go-lang.c (go_langhook_handle_option): Handle OPT_fgo_optimize.
* go-c.h (go_enable_optimize): Declare.
* Make-lang.in (GO_OBJS): Add go/go-optimize.o.
(GO_EXPORT_H): Define.
(GO_IMPORT_H): Add $(GO_EXPORT_H).
(GO_AST_DUMP_H): Define.
(go/ast-dump.o, go/statements.o): Use GO_AST_DUMP_H.
(go/export.o, go/gogo.o, go/import.o): Use GO_EXPORT_H.
(go/types.o): Likewise.
(go/expressions.o): Use GO_AST_DUMP_H and GO_EXPORT_H.
(go/go-optimize.o): New target.
2011-08-24 Joseph Myers <joseph@codesourcery.com>
* Make-lang.in (CFLAGS-go/go-lang.o): New.
......
......@@ -54,6 +54,7 @@ GO_OBJS = \
go/go-dump.o \
go/go-gcc.o \
go/go-lang.o \
go/go-optimize.o \
go/go.o \
go/gogo-tree.o \
go/gogo.o \
......@@ -222,8 +223,10 @@ GO_GOGO_H = go/gofrontend/gogo.h
GO_TYPES_H = go/gofrontend/types.h
GO_STATEMENTS_H = go/gofrontend/statements.h go/gofrontend/operator.h
GO_EXPRESSIONS_H = go/gofrontend/expressions.h go/gofrontend/operator.h
GO_IMPORT_H = go/gofrontend/import.h go/gofrontend/export.h
GO_EXPORT_H = go/gofrontend/export.h go/gofrontend/string-dump.h
GO_IMPORT_H = go/gofrontend/import.h $(GO_EXPORT_H)
GO_RUNTIME_H = go/gofrontend/runtime.h go/gofrontend/runtime.def
GO_AST_DUMP_H = go/gofrontend/ast-dump.h go/gofrontend/string-dump.h
go/go-backend.o: go/go-backend.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
$(TM_H) $(RTL_H) $(TREE_H) $(TM_P_H) output.h $(TARGET_H) \
......@@ -249,22 +252,24 @@ go/%.o: go/gofrontend/%.cc
go/ast-dump.o: go/gofrontend/ast-dump.cc $(GO_SYSTME_H) $(GO_GOGO_H) \
$(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) $(GO_TYPES_H) \
go/gofrontend/ast-dump.h $(GO_C_H) go/gofrontend/go-dump.h
$(GO_AST_DUMP_H) $(GO_C_H) go/gofrontend/go-dump.h
go/dataflow.o: go/gofrontend/dataflow.cc $(GO_SYSTEM_H) $(GO_GOGO_H) \
$(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) go/gofrontend/dataflow.h
go/export.o: go/gofrontend/export.cc $(GO_SYSTEM_H) \
$(srcdir)/../include/sha1.h $(GO_C_H) $(GO_GOGO_H) $(GO_TYPES_H) \
$(GO_STATEMENTS_H) go/gofrontend/export.h
$(GO_STATEMENTS_H) $(GO_EXPORT_H)
go/expressions.o: go/gofrontend/expressions.cc $(GO_SYSTEM_H) $(TOPLEV_H) \
intl.h $(TREE_H) $(GIMPLE_H) tree-iterator.h convert.h $(REAL_H) \
realmpfr.h $(GO_C_H) $(GO_GOGO_H) $(GO_TYPES_H) \
go/gofrontend/export.h $(GO_IMPORT_H) $(GO_STATEMENTS_H) $(GO_LEX_H) \
$(GO_EXPORT_H) $(GO_IMPORT_H) $(GO_STATEMENTS_H) $(GO_LEX_H) \
$(GO_RUNTIME_H) go/gofrontend/backend.h $(GO_EXPRESSIONS_H) \
go/gofrontend/ast-dump.h
$(GO_AST_DUMP_H)
go/go.o: go/gofrontend/go.cc $(GO_SYSTEM_H) $(GO_C_H) $(GO_LEX_H) \
$(GO_PARSE_H) go/gofrontend/backend.h $(GO_GOGO_H)
go/go-dump.o: go/gofrontend/go-dump.cc $(GO_SYSTEM_H) $(GO_C_H) \
go/gofrontend/go-dump.h
go/go-optimize.o: go/gofrontend/go-optimize.cc $(GO_SYSTEM_H) $(GO_C_H) \
go/gofrontend/go-optimize.h
go/gogo-tree.o: go/gofrontend/gogo-tree.cc $(GO_SYSTEM_H) $(TOPLEV_H) \
$(TREE_H) $(GIMPLE_H) tree-iterator.h $(CGRAPH_H) langhooks.h \
convert.h output.h $(DIAGNOSTIC_H) $(GO_TYPES_H) \
......@@ -273,12 +278,10 @@ go/gogo-tree.o: go/gofrontend/gogo-tree.cc $(GO_SYSTEM_H) $(TOPLEV_H) \
go/gogo.o: go/gofrontend/gogo.cc $(GO_SYSTEM_H) $(GO_C_H) \
go/gofrontend/go-dump.h $(GO_LEX_H) $(GO_TYPES_H) $(GO_STATEMENTS_H) \
$(GO_EXPRESSIONS_H) go/gofrontend/dataflow.h $(GO_RUNTIME_H) \
$(GO_IMPORT_H) go/gofrontend/export.h go/gofrontend/backend.h \
$(GO_GOGO_H)
$(GO_IMPORT_H) $(GO_EXPORT_H) go/gofrontend/backend.h $(GO_GOGO_H)
go/import.o: go/gofrontend/import.cc $(GO_SYSTEM_H) \
$(srcdir)/../include/filenames.h $(srcdir)/../include/simple-object.h \
$(GO_C_H) $(GO_GOGO_H) $(GO_TYPES_H) go/gofrontend/export.h \
$(GO_IMPORT_H)
$(GO_C_H) $(GO_GOGO_H) $(GO_TYPES_H) $(GO_EXPORT_H) $(GO_IMPORT_H)
go/import-archive.o: go/gofrontend/import-archive.cc $(GO_SYSTEM_H) \
$(GO_IMPORT_H)
go/lex.o: go/gofrontend/lex.cc $(GO_LEX_H) $(GO_SYSTEM_H)
......@@ -290,11 +293,10 @@ go/runtime.o: go/gofrontend/runtime.cc $(GO_SYSTEM_H) $(GO_GOGO_H) \
go/statements.o: go/gofrontend/statements.cc $(GO_SYSTEM_H) \
$(GO_C_H) $(GO_TYPES_H) $(GO_EXPRESSIONS_H) $(GO_GOGO_H) \
$(GO_RUNTIME_H) go/gofrontend/backend.h $(GO_STATEMENTS_H) \
go/gofrontend/ast-dump.h
$(GO_AST_DUMP_H)
go/types.o: go/gofrontend/types.cc $(GO_SYSTEM_H) $(TOPLEV_H) intl.h $(TREE_H) \
$(GIMPLE_H) $(REAL_H) convert.h $(GO_C_H) $(GO_GOGO_H) \
go/gofrontend/operator.h $(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) \
go/gofrontend/export.h $(GO_IMPORT_H) go/gofrontend/backend.h \
$(GO_TYPES_H)
$(GO_EXPORT_H) $(GO_IMPORT_H) go/gofrontend/backend.h $(GO_TYPES_H)
go/unsafe.o: go/gofrontend/unsafe.cc $(GO_SYSTEM_H) $(GO_C_H) $(GO_TYPES_H) \
$(GO_GOGO_H)
......@@ -37,6 +37,7 @@ extern "C"
interface. */
extern int go_enable_dump (const char*);
extern int go_enable_optimize (const char*);
extern void go_set_prefix (const char*);
extern void go_add_search_path (const char*);
......
......@@ -223,6 +223,10 @@ go_langhook_handle_option (
ret = go_enable_dump (arg) ? true : false;
break;
case OPT_fgo_optimize_:
ret = go_enable_optimize (arg) ? true : false;
break;
case OPT_fgo_prefix_:
go_set_prefix (arg);
break;
......
......@@ -19,9 +19,9 @@
// The -fgo-dump-ast flag to activate AST dumps.
Go_dump ast_dump_context_flag("ast");
Go_dump ast_dump_flag("ast");
// This class is used to traverse the tree to look for blocks and
// This class is used to traverse the tree to look for blocks and
// function headers.
class Ast_dump_traverse_blocks_and_functions : public Traverse
......@@ -33,9 +33,9 @@ class Ast_dump_traverse_blocks_and_functions : public Traverse
{ }
protected:
int
int
block(Block*);
int
function(Named_object*);
......@@ -79,7 +79,7 @@ int Ast_dump_traverse_blocks_and_functions::block(Block * block)
return TRAVERSE_SKIP_COMPONENTS;
}
// Dump each traversed statement.
int
......@@ -87,7 +87,7 @@ Ast_dump_traverse_statements::statement(Block* block, size_t* pindex,
Statement* statement)
{
statement->dump_statement(this->ast_dump_context_);
if (statement->is_block_statement())
{
Ast_dump_traverse_blocks_and_functions adtbf(this->ast_dump_context_);
......@@ -103,49 +103,51 @@ int
Ast_dump_traverse_blocks_and_functions::function(Named_object* no)
{
this->ast_dump_context_->ostream() << no->name();
go_assert(no->is_function());
Function* func = no->func_value();
this->ast_dump_context_->ostream() << "(";
this->ast_dump_context_->ostream() << "(";
this->ast_dump_context_->dump_typed_identifier_list(
func->type()->parameters());
this->ast_dump_context_->ostream() << ")";
this->ast_dump_context_->ostream() << ")";
Function::Results* res = func->result_variables();
if (res != NULL && !res->empty())
{
this->ast_dump_context_->ostream() << " (";
for (Function::Results::const_iterator it = res->begin();
it != res->end();
{
this->ast_dump_context_->ostream() << " (";
for (Function::Results::const_iterator it = res->begin();
it != res->end();
it++)
{
if (it != res->begin())
this->ast_dump_context_->ostream() << ",";
Named_object* no = (*it);
this->ast_dump_context_->ostream() << no->name() << " ";
go_assert(no->is_result_variable());
Result_variable* resvar = no->result_var_value();
this->ast_dump_context_->dump_type(resvar->type());
}
this->ast_dump_context_->ostream() << ")";
}
this->ast_dump_context_->ostream() << " : ";
this->ast_dump_context_->dump_type(func->type());
this->ast_dump_context_->ostream() << std::endl;
return TRAVERSE_CONTINUE;
}
// Class Ast_dump_context.
Ast_dump_context::Ast_dump_context() : ostream_(NULL)
Ast_dump_context::Ast_dump_context(std::ostream* out /* = NULL */,
bool dump_subblocks /* = true */)
: indent_(0), dump_subblocks_(dump_subblocks), ostream_(out), gogo_(NULL)
{
}
......@@ -168,8 +170,7 @@ Ast_dump_context::dump(Gogo* gogo, const char* basename)
error("cannot open %s:%m, -fgo-dump-ast ignored", dumpname.c_str());
return;
}
this->indent_ = 0;
this->gogo_ = gogo;
this->ostream_ = out;
......@@ -188,9 +189,10 @@ Ast_dump_context::dump_type(const Type* t)
if (t == NULL)
this->ostream() << "(nil type)";
else
// FIXME: write a type pretty printer instead of
// FIXME: write a type pretty printer instead of
// using mangled names.
this->ostream() << "(" << t->mangled_name(this->gogo_) << ")";
if (this->gogo_ != NULL)
this->ostream() << "(" << t->mangled_name(this->gogo_) << ")";
}
// Dump a textual representation of a block to the
......@@ -216,18 +218,28 @@ Ast_dump_context::dump_expression(const Expression* e)
// the dump file.
void
Ast_dump_context::dump_expression_list(const Expression_list* el)
Ast_dump_context::dump_expression_list(const Expression_list* el,
bool as_pairs /* = false */)
{
if (el == NULL)
return;
for (std::vector<Expression*>::const_iterator it = el->begin();
for (std::vector<Expression*>::const_iterator it = el->begin();
it != el->end();
it++)
{
if ( it != el->begin())
this->ostream() << ",";
(*it)->dump_expression(this);
if (*it != NULL)
(*it)->dump_expression(this);
else
this->ostream() << "NULL";
if (as_pairs)
{
this->ostream() << ":";
++it;
(*it)->dump_expression(this);
}
}
}
......@@ -246,13 +258,13 @@ Ast_dump_context::dump_typed_identifier(const Typed_identifier* ti)
void
Ast_dump_context::dump_typed_identifier_list(
const Typed_identifier_list* ti_list)
const Typed_identifier_list* ti_list)
{
if (ti_list == NULL)
return;
for (Typed_identifier_list::const_iterator it = ti_list->begin();
it != ti_list->end();
for (Typed_identifier_list::const_iterator it = ti_list->begin();
it != ti_list->end();
it++)
{
if (it != ti_list->begin())
......@@ -300,6 +312,8 @@ op_string(Operator op)
return "!";
case OPERATOR_XOR:
return "^";
case OPERATOR_OR:
return "|";
case OPERATOR_AND:
return "&";
case OPERATOR_MULT:
......@@ -415,9 +429,41 @@ Ast_dump_context::print_indent()
void Gogo::dump_ast(const char* basename)
{
if (ast_dump_context_flag.is_enabled())
if (::ast_dump_flag.is_enabled())
{
Ast_dump_context adc;
adc.dump(this, basename);
}
}
// Implementation of String_dump interface.
void
Ast_dump_context::write_c_string(const char* s)
{
this->ostream() << s;
}
void
Ast_dump_context::write_string(const std::string& s)
{
this->ostream() << s;
}
// Dump statment to stream.
void
Ast_dump_context::dump_to_stream(const Statement* stm, std::ostream* out)
{
Ast_dump_context adc(out, false);
stm->dump_statement(&adc);
}
// Dump expression to stream.
void
Ast_dump_context::dump_to_stream(const Expression* expr, std::ostream* out)
{
Ast_dump_context adc(out, false);
expr->dump_expression(&adc);
}
\ No newline at end of file
......@@ -7,6 +7,8 @@
#ifndef GO_AST_DUMP_H
#define GO_AST_DUMP_H
#include "string-dump.h"
class Expression;
class Expression_list;
class Named_object;
......@@ -16,10 +18,10 @@ class Gogo;
// This class implements fgo-dump-ast. the
// Abstract syntax tree dump of the Go program.
class Ast_dump_context
class Ast_dump_context : public String_dump
{
public:
Ast_dump_context();
Ast_dump_context(std::ostream* out = NULL, bool dump_subblocks = true);
// Initialize the dump context.
void
......@@ -28,38 +30,43 @@ class Ast_dump_context
// Dump spaces to dumpfile as indentation.
void
print_indent();
// Increase current indentation for print_indent().
void
indent()
indent()
{ ++this->indent_;}
// Decrease current indentation for print_indent().
void
unindent()
unindent()
{ --this->indent_;}
// Whether subblocks should be dumped or not.
bool
dump_subblocks()
{ return this->dump_subblocks_; }
// Get dump output stream.
std::ostream&
ostream()
std::ostream&
ostream()
{ return *this->ostream_;}
// Dump a Block to dump file.
void
void
dump_block(Block*);
// Dump a type to dump file.
void
void
dump_type(const Type*);
// Dump an expression to dump file.
void
void
dump_expression(const Expression*);
// Dump an expression list to dump file.
void
dump_expression_list(const Expression_list*);
void
dump_expression_list(const Expression_list*, bool as_pairs = false);
// Dump a typed identifier to dump file.
void
dump_typed_identifier(const Typed_identifier*);
......@@ -67,7 +74,7 @@ class Ast_dump_context
// Dump a typed identifier list to dump file.
void
dump_typed_identifier_list(const Typed_identifier_list*);
// Dump temporary variable name to dump file.
void
dump_temp_variable_name(const Statement*);
......@@ -79,17 +86,36 @@ class Ast_dump_context
// Dump operator symbol to dump file.
void
dump_operator(Operator);
// Implementation of String_dump interface.
void
write_c_string(const char*);
// Implements the String_dump interface.
void
write_string(const std::string& s);
// Dump statement to stream.
static void
dump_to_stream(const Statement*, std::ostream*);
// Dump expression to stream.
static void
dump_to_stream(const Expression* expr, std::ostream* out);
private:
// Current indent level.
int indent_;
// Indentation offset.
static const int offset_;
// Whether subblocks of composite statements should be dumped or not.
bool dump_subblocks_;
// Stream on output dump file.
std::ostream* ostream_;
Gogo* gogo_;
};
......
......@@ -7,6 +7,8 @@
#ifndef GO_EXPORT_H
#define GO_EXPORT_H
#include "string-dump.h"
struct sha1_ctx;
class Gogo;
class Import_init;
......@@ -45,7 +47,7 @@ enum Builtin_code
// loop of exporting. A pointer to this class is also passed to the
// various specific export implementations.
class Export
class Export : public String_dump
{
public:
// The Stream class is an interface used to output the exported
......@@ -57,12 +59,12 @@ class Export
Stream();
virtual ~Stream();
// Write a string.
// Write a string. Implements the String_dump interface.
void
write_string(const std::string& s)
{ this->write_and_sum_bytes(s.data(), s.length()); }
// Write a nul terminated string.
// Write a nul terminated string. Implements the String_dump interface.
void
write_c_string(const char* s)
{ this->write_and_sum_bytes(s, strlen(s)); }
......
......@@ -43,6 +43,7 @@ class Import;
class Temporary_statement;
class Label;
class Ast_dump_context;
class String_dump;
// The base class for all expressions.
......@@ -1043,6 +1044,10 @@ class String_expression : public Expression
tree
do_get_tree(Translate_context*);
// Write string literal to a string dump.
static void
export_string(String_dump* exp, const String_expression* str);
void
do_export(Export*) const;
......
// go-optimize.cc -- Go frontend optimizer flags.
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include "go-c.h"
#include "go-optimize.h"
namespace {
// The list of optimizations.
Go_optimize* optimizations;
} // End empty namespace.
// Create a new optimization.
Go_optimize::Go_optimize(const char* name)
: next_(optimizations), name_(name), is_enabled_(false)
{
optimizations = this;
}
// Enable an optimization by name.
bool
Go_optimize::enable_by_name(const char* name)
{
bool is_all = strcmp(name, "all") == 0;
bool found = false;
for (Go_optimize* p = optimizations; p != NULL; p = p->next_)
{
if (is_all || strcmp(name, p->name_) == 0)
{
p->is_enabled_ = true;
found = true;
}
}
return found;
}
// Enable an optimization. Return 1 if this is a real name, 0 if not.
GO_EXTERN_C
int
go_enable_optimize(const char* name)
{
return Go_optimize::enable_by_name(name) ? 1 : 0;
}
// go-optimize.h -- Go frontend optimizer flags. -*- C++ -*-
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#ifndef GO_OPTIMIZE_H
#define GO_OPTIMIZE_H
// This class manages different arguments to -fgo-optimize-XXX. If you
// want to create a new optimization, create a variable of this type with the
// name to use for XXX. You can then use is_enabled to see whether
// the -fgo-optimize-XXX option was used on the command line.
class Go_optimize
{
public:
Go_optimize(const char* name);
// Whether this optimizaiton was enabled.
bool
is_enabled() const
{ return this->is_enabled_; }
// Enable an optimization by name. Return true if found.
static bool
enable_by_name(const char*);
private:
// The next optimize flag. These are not in any order.
Go_optimize* next_;
// The name of this optimization pass.
const char* name_;
// Whether this dump was enabled.
bool is_enabled_;
};
#endif // !defined(GO_OPTIMIZE_H)
// string-dump.h -- Abstract base class for dumping strings. -*- C++ -*-
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#ifndef GO_STRING_DUMP_H
#define GO_STRING_DUMP_H
// This abstract class provides an interface strings for whatever purpose.
// Used for example for exporting and dumping objects.
class String_dump
{
public:
// Write a string. Implements the String_dump interface.
virtual void
write_string(const std::string& s) = 0;
// Implementors should override this member, to dump a formatted c string.
virtual void
write_c_string(const char*) = 0;
};
#endif // GO_STRING_DUMP_H
; lang.opt -- Options for the gcc Go front end.
; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
;
; This file is part of GCC.
;
......@@ -41,6 +41,10 @@ fgo-dump-
Go Joined RejectNegative
-fgo-dump-<type> Dump Go frontend internal information
fgo-optimize-
Go Joined RejectNegative
-fgo-optimize-<type> Turn on optimization passes in the frontend
fgo-prefix=
Go Joined RejectNegative
-fgo-prefix=<string> Set package-specific prefix for exported Go names
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment