Signed-off-by: Mohammad-Reza Nabipoor <[email protected]>

gcc/algol68/ChangeLog

        * a68.h (a68_file_size_fd): New function declaration.
        (a68_file_read_fd): Likewise.
        * a68-parser-scanner.cc (a68_file_size_fd): New function.
        (a68_file_read_fd): Likewise.
        (a68_file_size): Adapt to use `a68_file_size_fd'.
        (a68_file_read): Adapt to use `a68_file_read_fd'.
        * a68-imports.cc (a68_find_export_data): Implement
        reading from module's .m68 file if available.

gcc/testsuite/ChangeLog

        * algol68/compile/modules/compile.exp (dg-data): New procedure
        for writing binary test data to disk.
        * algol68/compile/modules/program-m68-lp64.a68: New test which
        embeds binary module data.
        * algol68/compile/modules/program-m68-llp64.a68: Likewise.
        * algol68/compile/modules/program-m68-ilp32.a68: Likewise.
---

Hi Jose.

I've added 3 new tests which are basically a single test conditioned on the
target:

  dg-require-effective-target ilp32
  dg-require-effective-target lp64
  dg-require-effective-target llp64

Also all of them have this line, too:

  dg-require-effective-target le

to only run on little-endian machines.
(Right now I'm trying to clone gcc repo on cfarm111 machine as a big-endian
machine to extend these tests, but I don't want to wait for that).

I've also ran
  make check-algol68 RUNTESTFLAGS='compile.exp'
for arm-none-eabi target (which is a 32-bit target) and verified that compiler 
can
find the module correctly.

Also I didn't do anything special to make sure `dg-data' is friendly to `make 
-j'
as with the current form, it's the responsibility of the caller to pass distinct
file names (which IMHO is not too much to ask).

Regards,
Mohammad-Reza


 gcc/algol68/a68-imports.cc                    | 51 +++++++++++++++++--
 gcc/algol68/a68-parser-scanner.cc             | 34 +++++++++----
 gcc/algol68/a68.h                             |  2 +
 .../algol68/compile/modules/compile.exp       | 10 ++++
 .../compile/modules/program-m68-ilp32.a68     | 14 +++++
 .../compile/modules/program-m68-llp64.a68     | 14 +++++
 .../compile/modules/program-m68-lp64.a68      | 14 +++++
 7 files changed, 125 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/algol68/compile/modules/program-m68-ilp32.a68
 create mode 100644 gcc/testsuite/algol68/compile/modules/program-m68-llp64.a68
 create mode 100644 gcc/testsuite/algol68/compile/modules/program-m68-lp64.a68

diff --git a/gcc/algol68/a68-imports.cc b/gcc/algol68/a68-imports.cc
index d8c1f1f7ba9..1405132a3bc 100644
--- a/gcc/algol68/a68-imports.cc
+++ b/gcc/algol68/a68-imports.cc
@@ -271,15 +271,57 @@ a68_find_export_data (const std::string &filename, int 
fd, size_t *psize)
     }
 
   char buf[A68_EXPORT_MAGIC_LEN];
-  ssize_t c = ::read(fd, buf, A68_EXPORT_MAGIC_LEN);
+  ssize_t c = read (fd, buf, A68_EXPORT_MAGIC_LEN);
   if (c < A68_EXPORT_MAGIC_LEN)
     return NULL;
 
+  if (lseek (fd, 0, SEEK_SET) < 0)
+    {
+      a68_error (NO_NODE, "lseek Z failed", filename.c_str ());
+      return NULL;
+    }
+
   /* Check for a file containing nothing but Algol 68 export data.  */
-  if (buf[0] == '\x0a' && buf[1] == '\xad')
+  if (buf[0] == '\x0a' && buf[1] == '\x68')
     {
-      /* XXX read whole file.  */
-      return exports;
+      /* read whole file.  */
+
+      char *buf;
+      ssize_t len, nread;
+
+      len = a68_file_size_fd (fd);
+      if (len == -1)
+        {
+          a68_error (NO_NODE, "a68_file_size_fd failed for Z",
+                     filename.c_str ());
+          return NULL;
+        }
+
+      buf = XNEWVEC (char, len);
+      if (buf == NULL)
+        {
+          a68_error (NO_NODE,
+                     "memory allocation failed while reading export data");
+          return NULL;
+        }
+
+      nread = a68_file_read_fd (fd, buf, len);
+      if (nread < 0)
+        {
+          free (buf);
+          a68_error (NO_NODE, "read failed while reading export data");
+          return NULL;
+        }
+
+      if (nread < len)
+        {
+          free (buf);
+          a68_error (NO_NODE, "short read while reading export data");
+          return NULL;
+        }
+
+      *psize = len;
+      return buf;
     }
 
 #if 0
@@ -289,7 +331,6 @@ a68_find_export_data (const std::string &filename, int fd, 
size_t *psize)
 #endif
 
   return NULL;
-
 }
 
 /* Given *PFILENAME, where *PFILENAME does not exist, try various suffixes.  If
diff --git a/gcc/algol68/a68-parser-scanner.cc 
b/gcc/algol68/a68-parser-scanner.cc
index 94647d52882..ed42a5a642e 100644
--- a/gcc/algol68/a68-parser-scanner.cc
+++ b/gcc/algol68/a68-parser-scanner.cc
@@ -119,37 +119,36 @@ supper_postlude[] = {
     }                                                                  \
   while (0)
 
-/* Get the size of a file given a stream pointer FILE.  In case the size of
+/* Get the size of a file given a file descriptor FD.  In case the size of
    the file cannot be determined then this function returns -1.  */
 
 ssize_t
-a68_file_size (FILE *file)
+a68_file_size_fd (int fd)
 {
   ssize_t fsize;
   off_t off, save;
 
-  save = ftell (file);
-  if (save == -1)
+  save = lseek (fd, 0, SEEK_CUR);
+  if (save == (off_t) -1)
     return -1;
 
-  off = lseek (fileno (file), 0, SEEK_END);
+  off = lseek (fd, 0, SEEK_END);
   if (off == (off_t) -1)
     return -1;
   fsize = (ssize_t) off;
 
-  off = lseek (fileno (file), save, SEEK_SET);
+  off = lseek (fd, save, SEEK_SET);
   if (off == (off_t) -1)
     return -1;
 
   return fsize;
 }
 
-/* Read bytes from file into buffer.  */
+/* Read bytes from file into buffer given a file descriptor.  */
 
 ssize_t
-a68_file_read (FILE *file, void *buf, size_t n)
+a68_file_read_fd (int fd, void *buf, size_t n)
 {
-  int fd = fileno (file);
   size_t to_do = n;
   int restarts = 0;
   char *z = (char *) buf;
@@ -189,6 +188,23 @@ a68_file_read (FILE *file, void *buf, size_t n)
   return (ssize_t) n - (ssize_t) to_do;
 }
 
+/* Get the size of a file given a stream pointer FILE.  In case the size of
+   the file cannot be determined then this function returns -1.  */
+
+ssize_t
+a68_file_size (FILE *file)
+{
+  return a68_file_size_fd (fileno (file));
+}
+
+/* Read bytes from file into buffer given a stream pointer FILE.  */
+
+ssize_t
+a68_file_read (FILE *file, void *buf, size_t n)
+{
+  return a68_file_read_fd (fileno (file), buf, n);
+}
+
 /* Save scanner state, for character look-ahead.  */
 
 static void
diff --git a/gcc/algol68/a68.h b/gcc/algol68/a68.h
index 98730973bc7..e623376749d 100644
--- a/gcc/algol68/a68.h
+++ b/gcc/algol68/a68.h
@@ -281,6 +281,8 @@ void a68_scan_error (LINE_T *u, char *v, const char *txt, 
...);
 /* a68-parser-scanner.cc  */
 
 bool a68_lexical_analyser (const char *filename, bool *empty_file);
+ssize_t a68_file_size_fd (int fd);
+ssize_t a68_file_read_fd (int fd, void *buf, size_t n);
 ssize_t a68_file_size (FILE *file);
 ssize_t a68_file_read (FILE *file, void *buf, size_t n);
 
diff --git a/gcc/testsuite/algol68/compile/modules/compile.exp 
b/gcc/testsuite/algol68/compile/modules/compile.exp
index a843940169e..eab84490fea 100644
--- a/gcc/testsuite/algol68/compile/modules/compile.exp
+++ b/gcc/testsuite/algol68/compile/modules/compile.exp
@@ -20,6 +20,16 @@
 
 load_lib algol68-dg.exp
 
+proc dg-data { nargs filename bytes } {
+  global objdir
+
+  set filename $objdir/$filename
+  set fd [open $filename w]
+  fconfigure $fd -translation binary
+  puts -nonewline $fd [binary format c* $bytes]
+  close $fd
+}
+
 # Initialize `dg'.
 dg-init
 
diff --git a/gcc/testsuite/algol68/compile/modules/program-m68-ilp32.a68 
b/gcc/testsuite/algol68/compile/modules/program-m68-ilp32.a68
new file mode 100644
index 00000000000..4b64427a779
--- /dev/null
+++ b/gcc/testsuite/algol68/compile/modules/program-m68-ilp32.a68
@@ -0,0 +1,14 @@
+{ dg-require-effective-target ilp32 }
+{ dg-require-effective-target le }
+{
+  { The following dg-data is representing this module: }
+  module Module_m68 =
+  def
+      pub mode Foo = struct (int i, long int l);
+      pub mode Bar = int;
+      skip
+  fed
+}
+{ dg-data modulem68.m68 {0x0a 0x68 0x01 0x00 0x0a 0x00 0x4d 0x4f 0x44 0x55 
0x4c 0x45 0x4d 0x36 0x38 0x00 0x13 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 
0x38 0x5f 0x5f 0x70 0x72 0x65 0x6c 0x75 0x64 0x65 0x00 0x14 0x00 0x4d 0x4f 0x44 
0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x6f 0x73 0x74 0x6c 0x75 0x64 0x65 
0x00 0x17 0x00 0x00 0x00 0x02 0x00 0x02 0x01 0x0a 0x02 0x00 0x3f 0x00 0x00 0x00 
0x02 0x00 0x69 0x00 0x41 0x00 0x00 0x00 0x02 0x00 0x6c 0x00 0x3a 0x00 0x00 0x00 
0x19 0x00 0x00 0x00 0x02 0x0e 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 
0x5f 0x42 0x41 0x52 0x00 0x3f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x19 0x00 0x00 
0x00 0x02 0x0e 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x46 0x4f 
0x4f 0x00 0x43 0x00 0x00 0x00 0x00 0x00 0x00 0x00} }
+
+access Module_m68 (Foo foo; i of foo)
diff --git a/gcc/testsuite/algol68/compile/modules/program-m68-llp64.a68 
b/gcc/testsuite/algol68/compile/modules/program-m68-llp64.a68
new file mode 100644
index 00000000000..0367727fa19
--- /dev/null
+++ b/gcc/testsuite/algol68/compile/modules/program-m68-llp64.a68
@@ -0,0 +1,14 @@
+{ dg-require-effective-target llp64 }
+{ dg-require-effective-target le }
+{
+  { The following dg-data is representing this module: }
+  module Module_m68 =
+  def
+      pub mode Foo = struct (int i, long int l);
+      pub mode Bar = int;
+      skip
+  fed
+}
+{ dg-data modulem68.m68 {0x0a 0x68 0x01 0x00 0x0a 0x00 0x4d 0x4f 0x44 0x55 
0x4c 0x45 0x4d 0x36 0x38 0x00 0x13 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 
0x38 0x5f 0x5f 0x70 0x72 0x65 0x6c 0x75 0x64 0x65 0x00 0x14 0x00 0x4d 0x4f 0x44 
0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x6f 0x73 0x74 0x6c 0x75 0x64 0x65 
0x00 0x1f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x02 0x01 0x0a 0x02 0x00 
0x43 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x69 0x00 0x45 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x02 0x00 0x6c 0x00 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x21 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x0e 0x00 0x4d 0x4f 0x44 0x55 0x4c 
0x45 0x4d 0x36 0x38 0x5f 0x42 0x41 0x52 0x00 0x43 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x02 0x0e 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x46 0x4f 
0x4f 0x00 0x47 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00} }
+
+access Module_m68 (Foo foo; i of foo)
diff --git a/gcc/testsuite/algol68/compile/modules/program-m68-lp64.a68 
b/gcc/testsuite/algol68/compile/modules/program-m68-lp64.a68
new file mode 100644
index 00000000000..dfcfa6f60f8
--- /dev/null
+++ b/gcc/testsuite/algol68/compile/modules/program-m68-lp64.a68
@@ -0,0 +1,14 @@
+{ dg-require-effective-target lp64 }
+{ dg-require-effective-target le }
+{
+  { The following dg-data is representing this module: }
+  module Module_m68 =
+  def
+      pub mode Foo = struct (int i, long int l);
+      pub mode Bar = int;
+      skip
+  fed
+}
+{ dg-data modulem68.m68 {0x0a 0x68 0x01 0x00 0x0a 0x00 0x4d 0x4f 0x44 0x55 
0x4c 0x45 0x4d 0x36 0x38 0x00 0x13 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 
0x38 0x5f 0x5f 0x70 0x72 0x65 0x6c 0x75 0x64 0x65 0x00 0x14 0x00 0x4d 0x4f 0x44 
0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x6f 0x73 0x74 0x6c 0x75 0x64 0x65 
0x00 0x1f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x02 0x01 0x0a 0x02 0x00 
0x43 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x69 0x00 0x45 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x02 0x00 0x6c 0x00 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x21 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x0e 0x00 0x4d 0x4f 0x44 0x55 0x4c 
0x45 0x4d 0x36 0x38 0x5f 0x42 0x41 0x52 0x00 0x43 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x02 0x0e 0x00 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x46 0x4f 
0x4f 0x00 0x47 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00} }
+
+access Module_m68 (Foo foo; i of foo)
-- 
2.52.0

Reply via email to