]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
a68: add import from .m68 file
authorMohammad-Reza Nabipoor <mnabipoor@gnu.org>
Tue, 23 Dec 2025 20:10:51 +0000 (21:10 +0100)
committerJose E. Marchesi <jose.marchesi@oracle.com>
Tue, 23 Dec 2025 22:02:10 +0000 (23:02 +0100)
Signed-off-by: Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
gcc/algol68/ChangeLog

* a68.h (a68_file_size): Changed to use file descriptor.
(a68_file_read): Likewise.
* a68-parser-scanner.cc (a68_file_size): Likewise.
(a68_file_read): Likewise.
(read_source_file): Adapt `a68_file_{size,read}'.
(include_files): Likewise.
* a68-lang.cc (a68_handle_option): Likewise.
* 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.
* algol68/compile/modules/program-m68-lp64-be.a68: Likewise.
* algol68/compile/modules/program-m68-llp64-be.a68: Likewise.

gcc/algol68/a68-imports.cc
gcc/algol68/a68-lang.cc
gcc/algol68/a68-parser-scanner.cc
gcc/algol68/a68.h
gcc/testsuite/algol68/compile/modules/compile.exp
gcc/testsuite/algol68/compile/modules/program-m68-ilp32.a68 [new file with mode: 0644]
gcc/testsuite/algol68/compile/modules/program-m68-llp64-be.a68 [new file with mode: 0644]
gcc/testsuite/algol68/compile/modules/program-m68-llp64.a68 [new file with mode: 0644]
gcc/testsuite/algol68/compile/modules/program-m68-lp64-be.a68 [new file with mode: 0644]
gcc/testsuite/algol68/compile/modules/program-m68-lp64.a68 [new file with mode: 0644]

index d8c1f1f7ba92a648b0a5514d98d4573cc7cf5a80..6c76e921f139dbdbae184b5f6eb74e43d1c6f303 100644 (file)
@@ -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);
+      if (len == -1)
+        {
+          a68_error (NO_NODE, "a68_file_size 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, 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
index adcf12cfaa418c00c4550a27291f75ff53614686..3fa7897d23691f40924e116b377df5e25ebb9403 100644 (file)
@@ -533,14 +533,14 @@ a68_handle_option (size_t scode,
          fatal_error (UNKNOWN_LOCATION,
                       "cannot open modules map file %<%s%>", arg);
        
-       ssize_t ssize = a68_file_size (file);
+       ssize_t ssize = a68_file_size (fileno (file));
        if (ssize < 0)
          fatal_error (UNKNOWN_LOCATION,
                       "cannot determine size of modules map file %<%s%>", arg);
        size_t fsize = ssize;
 
        char *buffer = (char *) xmalloc (fsize + 1);
-       size_t bytes_read = a68_file_read (file, buffer, fsize);
+       size_t bytes_read = a68_file_read (fileno (file), buffer, fsize);
        if (bytes_read != fsize)
          fatal_error (UNKNOWN_LOCATION,
                       "cannot read contents of modules map file %<%s%>", arg);
index 94647d528829c19a1c9ed4b80a94d654a5893b1b..49f8dee3ac113b4d8228ffda8e7677ae00aa59d9 100644 (file)
@@ -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 (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 (int fd, void *buf, size_t n)
 {
-  int fd = fileno (file);
   size_t to_do = n;
   int restarts = 0;
   char *z = (char *) buf;
@@ -344,7 +343,7 @@ read_source_file (const char *filename)
     fatal_error (UNKNOWN_LOCATION, "specified file %s is a directory",
                 filename);
 
-  l = a68_file_size (f);
+  l = a68_file_size (fileno (f));
   if (l < 0)
     error ("could not get size of source file");
   source_file_size = l;
@@ -364,7 +363,7 @@ read_source_file (const char *filename)
   /* Read the file into a single buffer, so we save on system calls.  */
   line_num = 1;
   buffer = (char *) xmalloc (8 + source_file_size);
-  bytes_read = a68_file_read (f, buffer, source_file_size);
+  bytes_read = a68_file_read (fileno (f), buffer, source_file_size);
   gcc_assert (bytes_read == source_file_size);
 
   /* Link all lines into the list.  */
@@ -1078,12 +1077,12 @@ include_files (LINE_T *top)
              fp = fopen (fn, "r");
              SCAN_ERROR (fp == NULL, start_l, start_c,
                          "error opening included file");
-             ssize = a68_file_size (fp);
+             ssize = a68_file_size (fileno (fp));
              SCAN_ERROR (ssize < 0, start_l, start_c,
                          "error getting included file size");
              fsize = ssize;
              fbuf = (char *) xmalloc (8 + fsize);
-             bytes_read = (int) a68_file_read (fp, fbuf, (size_t) fsize);
+             bytes_read = (int) a68_file_read (fileno (fp), fbuf, (size_t) fsize);
              SCAN_ERROR ((size_t) bytes_read != fsize, start_l, start_c,
                          "error while reading file");
 
index 98730973bc77a16b80b38e6dd6f3e7728c4afdde..dc73277038c988117de6f29cd21fd17feaab199a 100644 (file)
@@ -281,8 +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 (FILE *file);
-ssize_t a68_file_read (FILE *file, void *buf, size_t n);
+ssize_t a68_file_size (int fd);
+ssize_t a68_file_read (int fd, void *buf, size_t n);
 
 /* a68-parser.cc  */
 
index a843940169ec8bc94082c0bae50f5302aa85040c..41bd2f7cccde76f51baeafb4c7986e4b45bfbc4a 100644 (file)
 
 load_lib algol68-dg.exp
 
+proc dg-data { args } {
+  global objdir
+
+  if { [llength $args] != 3 } {
+      error "[lindex $args 0]: invalid arguments"
+  }
+
+  set filename $objdir/[lindex $args 1]
+  set bytes [lindex $args 2]
+
+  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 (file)
index 0000000..4b64427
--- /dev/null
@@ -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-be.a68 b/gcc/testsuite/algol68/compile/modules/program-m68-llp64-be.a68
new file mode 100644 (file)
index 0000000..d7e70a0
--- /dev/null
@@ -0,0 +1,14 @@
+{ dg-require-effective-target llp64 }
+{ dg-require-effective-target be }
+{
+  { 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 0x00 0x01 0x00 0x0a 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x00 0x00 0x13 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x72 0x65 0x6c 0x75 0x64 0x65 0x00 0x00 0x14 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x6f 0x73 0x74 0x6c 0x75 0x64 0x65 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x1f 0x02 0x00 0x02 0x01 0x0a 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x43 0x00 0x02 0x69 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x45 0x00 0x02 0x6c 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 0x02 0x00 0x0e 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x42 0x41 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x43 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 0x02 0x00 0x0e 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x46 0x4f 0x4f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x47 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-llp64.a68 b/gcc/testsuite/algol68/compile/modules/program-m68-llp64.a68
new file mode 100644 (file)
index 0000000..0367727
--- /dev/null
@@ -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-be.a68 b/gcc/testsuite/algol68/compile/modules/program-m68-lp64-be.a68
new file mode 100644 (file)
index 0000000..aa3fb73
--- /dev/null
@@ -0,0 +1,14 @@
+{ dg-require-effective-target lp64 }
+{ dg-require-effective-target be }
+{
+  { 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 0x00 0x01 0x00 0x0a 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x00 0x00 0x13 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x72 0x65 0x6c 0x75 0x64 0x65 0x00 0x00 0x14 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x5f 0x70 0x6f 0x73 0x74 0x6c 0x75 0x64 0x65 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x1f 0x02 0x00 0x02 0x01 0x0a 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x43 0x00 0x02 0x69 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x45 0x00 0x02 0x6c 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 0x02 0x00 0x0e 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x42 0x41 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x43 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 0x02 0x00 0x0e 0x4d 0x4f 0x44 0x55 0x4c 0x45 0x4d 0x36 0x38 0x5f 0x46 0x4f 0x4f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x47 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 (file)
index 0000000..dfcfa6f
--- /dev/null
@@ -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)