This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[gcjx] Patch: FYI: resource compilation
- From: Tom Tromey <tromey at redhat dot com>
- To: Java Patch List <java-patches at gcc dot gnu dot org>
- Date: 04 Oct 2005 15:47:32 -0600
- Subject: [gcjx] Patch: FYI: resource compilation
- Reply-to: tromey at redhat dot com
I'm checking this in on the gcjx branch.
This patch is a bit weird since I thought I had been able to compile
all of libgcj to a .so before, but clearly that couldn't be possible
without resource compilation. So, weird. The code in resource.cc is
from the current gcj.
Tom
Index: ChangeLog
from Tom Tromey <tromey@redhat.com>
* Make-lang.in (JAVA_OBJS): Added resource.o.
* resource.cc: New file.
* treegen.cc (compile_resource): New method.
* hooks.hh (resource_list): Declare.
(compile_resource_file, compile_resource_data): Likewise.
* decl.cc (resource_list): New global.
* driver.cc (arg_info::resource_name): New field.
(arg_info): Initialize it.
(handle_option): Set resource_name.
(process_one_file): Handle resource_name.
* treegen.hh (tree_code_generator::compile_resource): Declare.
Index: Make-lang.in
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Make-lang.in,v
retrieving revision 1.151.2.8
diff -u -r1.151.2.8 Make-lang.in
--- Make-lang.in 4 Oct 2005 01:45:18 -0000 1.151.2.8
+++ Make-lang.in 4 Oct 2005 21:52:53 -0000
@@ -78,7 +78,8 @@
# Executables built by this Makefile:
JAVA_OBJS = java/abi.o java/builtins.o java/classobj.o java/decl.o \
-java/driver.o java/langhooks.o java/lower.o java/tree.o java/treegen.o
+java/driver.o java/langhooks.o java/lower.o java/tree.o java/treegen.o \
+java/resource.o
# later:
# java/boehm.o java/mangle_name.o
Index: decl.cc
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Attic/decl.cc,v
retrieving revision 1.1.2.23
diff -u -r1.1.2.23 decl.cc
--- decl.cc 4 Oct 2005 02:08:31 -0000 1.1.2.23
+++ decl.cc 4 Oct 2005 21:52:53 -0000
@@ -200,6 +200,10 @@
// a TREE_LIST.
tree class_list;
+// This holds the constructors for all the resources we've compiled,
+// as a TREE_LIST.
+tree resource_list;
+
// In gcjx this is used only to protect the tree from garbage
Index: driver.cc
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Attic/driver.cc,v
retrieving revision 1.1.2.8
diff -u -r1.1.2.8 driver.cc
--- driver.cc 12 Sep 2005 01:49:03 -0000 1.1.2.8
+++ driver.cc 4 Oct 2005 21:52:53 -0000
@@ -48,6 +48,10 @@
// Input file name.
const char *filename;
+ // If --resource was given, the resource name. If this is NULL then
+ // we are not compiling a resource file.
+ const char *resource_name;
+
// Dependency tracking structure.
struct deps *dependencies;
// True if we should print phony targets.
@@ -69,6 +73,7 @@
encoding (NULL),
output (NULL),
filename (NULL),
+ resource_name (NULL),
dependencies (NULL),
print_phonies (false),
include_system_files (false),
@@ -335,7 +340,7 @@
break;
case OPT_fcompile_resource_:
- // resource_name = arg;
+ arguments->resource_name = arg;
break;
case OPT_fdump_:
@@ -618,9 +623,10 @@
static void
process_one_file (const std::string &filename)
{
- // FIXME: need special handling for resources, class files, jar
- // files.
- our_compiler->load_source_file (filename);
+ if (arguments->resource_name)
+ compile_resource_file (arguments->resource_name, filename.c_str ());
+ else
+ our_compiler->load_source_file (filename);
}
void
Index: glue.hh
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Attic/glue.hh,v
retrieving revision 1.1.2.15
diff -u -r1.1.2.15 glue.hh
--- glue.hh 4 Oct 2005 01:30:59 -0000 1.1.2.15
+++ glue.hh 4 Oct 2005 21:52:53 -0000
@@ -45,6 +45,9 @@
// libstdc++'s use of those same identifiers.
#include <iostream>
+#include <fcntl.h>
+#include <unistd.h>
+
#define IN_GCC
extern "C"
Index: hooks.hh
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Attic/hooks.hh,v
retrieving revision 1.1.2.12
diff -u -r1.1.2.12 hooks.hh
--- hooks.hh 11 Jul 2005 16:25:46 -0000 1.1.2.12
+++ hooks.hh 4 Oct 2005 21:52:54 -0000
@@ -102,12 +102,16 @@
extern GTY (()) tree field_slot_o;
extern GTY (()) tree gcj_abi_version;
extern GTY (()) tree class_list;
+extern GTY (()) tree resource_list;
extern tree build_address_of (tree);
extern void push_field (tree record, tree &field, const char *name,
tree field_type, bool is_private = false);
extern void pushdecl (tree);
+extern void compile_resource_data (const char *, const char *, int);
+extern void compile_resource_file (const char *, const char *);
+
namespace gcjx
{
Index: resource.cc
===================================================================
RCS file: resource.cc
diff -N resource.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ resource.cc 4 Oct 2005 21:52:54 -0000
@@ -0,0 +1,141 @@
+// Resource file compilation.
+
+// Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+// Free Software Foundation, Inc.
+//
+// This file is part of GCC.
+//
+// GCC is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// GCC is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING. If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+
+#include "java/glue.hh"
+
+/* DOS brain-damage */
+#ifndef O_BINARY
+#define O_BINARY 0 /* MS-DOS brain-damage */
+#endif
+
+// Count of all the resources compiled in this invocation.
+static int Jr_count = 0;
+
+void
+compile_resource_data (const char *name, const char *buffer, int length)
+{
+ tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
+ char buf[60];
+
+ int len = strlen (name) + length;
+ tree idx_type = build_index_type (build_int_cst (sizetype, len - 1));
+ data_type = build_array_type (type_jubyte, idx_type);
+
+ rtype = make_node (RECORD_TYPE);
+ push_field (rtype, field, "name_length", type_juint);
+ push_field (rtype, field, "resource_length", type_juint);
+ push_field (rtype, field, "data", data_type);
+ layout_type (rtype);
+
+ record_creator creator (rtype);
+ creator.set_field ("name_length",
+ build_int_cst (NULL_TREE, strlen (name)));
+ creator.set_field ("resource_length",
+ build_int_cst (NULL_TREE, length));
+ data = build_string (strlen(name) + length, buffer);
+ TREE_TYPE (data) = data_type;
+ creator.set_field ("data", data);
+
+ rinit = creator.finish_record ();
+ TREE_CONSTANT (rinit) = 1;
+ TREE_INVARIANT (rinit) = 1;
+
+ /* Generate a unique-enough identifier. */
+ sprintf (buf, "_Jr%d", ++Jr_count);
+
+ decl = build_decl (VAR_DECL, get_identifier (buf), rtype);
+ TREE_STATIC (decl) = 1;
+ DECL_ARTIFICIAL (decl) = 1;
+ DECL_IGNORED_P (decl) = 1;
+ TREE_READONLY (decl) = 1;
+ TREE_THIS_VOLATILE (decl) = 0;
+ DECL_INITIAL (decl) = rinit;
+ layout_decl (decl, 0);
+ pushdecl (decl);
+ rest_of_decl_compilation (decl, true, 0);
+ cgraph_varpool_finalize_decl (decl);
+
+ resource_list = tree_cons (NULL_TREE, decl, resource_list);
+}
+
+void
+write_resource_constructor ()
+{
+ tree iter, t, register_resource_fn;
+
+ if (resource_list == NULL_TREE)
+ return;
+
+ t = build_function_type_list (void_type_node, ptr_type_node, NULL);
+ t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterResource"), t);
+ TREE_PUBLIC (t) = 1;
+ DECL_EXTERNAL (t) = 1;
+ register_resource_fn = t;
+
+ tree body = NULL_TREE;
+
+ /* Write out entries in the same order in which they were defined. */
+ for (iter = nreverse (resource_list); iter ; iter = TREE_CHAIN (iter))
+ {
+ t = build_fold_addr_expr (TREE_VALUE (iter));
+ t = tree_cons (NULL, t, NULL);
+ t = build_function_call_expr (register_resource_fn, t);
+ append_to_statement_list (t, &body);
+ }
+
+ // FIXME: we should share this with the commented-out case in
+ // register_classes().
+ cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
+}
+
+/* Generate a byte array representing the contents of FILENAME. The
+ array is assigned a unique local symbol. The array represents a
+ compiled Java resource, which is accessed by the runtime using
+ NAME. */
+void
+compile_resource_file (const char *name, const char *filename)
+{
+ struct stat stat_buf;
+ int fd;
+ char *buffer;
+
+ fd = open (filename, O_RDONLY | O_BINARY);
+ if (fd < 0)
+ {
+ perror ("Failed to read resource file");
+ return;
+ }
+ if (fstat (fd, &stat_buf) != 0
+ || ! S_ISREG (stat_buf.st_mode))
+ {
+ perror ("Could not figure length of resource file");
+ return;
+ }
+
+ // FIXME: we seem to just leak BUFFER here.
+ buffer = new char[strlen (name) + stat_buf.st_size];
+ strcpy (buffer, name);
+ read (fd, buffer + strlen (name), stat_buf.st_size);
+ close (fd);
+
+ compile_resource_data (name, buffer, stat_buf.st_size);
+}
Index: treegen.cc
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Attic/treegen.cc,v
retrieving revision 1.1.2.10
diff -u -r1.1.2.10 treegen.cc
--- treegen.cc 27 Apr 2005 17:38:08 -0000 1.1.2.10
+++ treegen.cc 4 Oct 2005 21:52:54 -0000
@@ -24,6 +24,7 @@
#include "codegen.hh"
#include "java/treegen.hh"
#include "java/tree.hh"
+#include "buffer.hh"
tree_code_generator::tree_code_generator (compiler *, directory_cache &dirs)
: code_generator (dirs),
@@ -38,6 +39,18 @@
}
void
+tree_code_generator::compile_resource (const std::string &name,
+ byte_buffer *contents)
+{
+ char *buf = new char[name.length () + contents->get_length ()];
+ strcpy (buf, name.c_str ());
+ // We intentionally copy over the \0 byte here.
+ memcpy (buf + name.length (), contents->get (), contents->get_length ());
+ compile_resource_data (name.c_str (), buf, (int) contents->get_length ());
+ // FIXME: can we delete BUF here? For now just leak.
+}
+
+void
tree_code_generator::generate (model_class *the_class)
{
// The first time we're called to compile a class, we ask the
Index: treegen.hh
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/Attic/treegen.hh,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 treegen.hh
--- treegen.hh 4 Apr 2005 04:30:32 -0000 1.1.2.3
+++ treegen.hh 4 Oct 2005 21:52:54 -0000
@@ -39,6 +39,8 @@
void generate (model_class *);
+ void compile_resource (const std::string &, byte_buffer *);
+
bool handles_class_p () const
{
return true;