[flang] Support GNU extensions IARGC and GETARG in runtime
authorPeixin-Qiao <qiaopeixin@huawei.com>
Tue, 11 Oct 2022 02:29:23 +0000 (10:29 +0800)
committerPeixin-Qiao <qiaopeixin@huawei.com>
Tue, 11 Oct 2022 02:29:23 +0000 (10:29 +0800)
The GNU extension intrinsic IARGC is equivalent to
COMMAND_ARGUMENT_COUNT, and the GETARG is similar to
GET_COMMAND_ARGUMENT, but with less arguments. Reuse the runtime of
COMMAND_ARGUMENT_COUNT and GET_COMMAND_ARGUMENT for IARGC and GETARG.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D133558

flang/include/flang/Runtime/extensions.h
flang/runtime/extensions.cpp

index c0edfd9..ad59281 100644 (file)
 #ifndef FORTRAN_RUNTIME_EXTENSIONS_H_
 #define FORTRAN_RUNTIME_EXTENSIONS_H_
 
-#define FORTRAN_SUBROUTINE_NAME(name) name##_
+#define FORTRAN_PROCEDURE_NAME(name) name##_
+
+#include <cstdint>
 
 extern "C" {
 
 // CALL FLUSH(n) antedates the Fortran 2003 FLUSH statement.
-void FORTRAN_SUBROUTINE_NAME(flush)(const int &unit);
+void FORTRAN_PROCEDURE_NAME(flush)(const int &unit);
+
+// GNU Fortran 77 compatibility function IARGC.
+std::int32_t FORTRAN_PROCEDURE_NAME(iargc)();
+
+// GNU Fortran 77 compatibility subroutine GETARG(N, ARG).
+void FORTRAN_PROCEDURE_NAME(getarg)(
+    std::int32_t &n, std::int8_t *arg, std::int64_t length);
 
 } // extern "C"
 #endif // FORTRAN_RUNTIME_EXTENSIONS_H_
index 2769c7c..b8e9b6e 100644 (file)
 // extensions that will eventually be implemented in Fortran.
 
 #include "flang/Runtime/extensions.h"
+#include "flang/Runtime/command.h"
+#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/io-api.h"
 
 extern "C" {
 
+namespace Fortran::runtime {
+namespace io {
 // SUBROUTINE FLUSH(N)
 //   FLUSH N
 // END
-namespace Fortran::runtime::io {
-void FORTRAN_SUBROUTINE_NAME(flush)(const int &unit) {
+void FORTRAN_PROCEDURE_NAME(flush)(const int &unit) {
   Cookie cookie{IONAME(BeginFlush)(unit, __FILE__, __LINE__)};
   IONAME(EndIoStatement)(cookie);
 }
-} // namespace Fortran::runtime::io
+} // namespace io
+
+// RESULT = IARGC()
+std::int32_t FORTRAN_PROCEDURE_NAME(iargc)() { return RTNAME(ArgumentCount)(); }
+
+// CALL GETARG(N, ARG)
+void FORTRAN_PROCEDURE_NAME(getarg)(
+    std::int32_t &n, std::int8_t *arg, std::int64_t length) {
+  Descriptor value{*Descriptor::Create(1, length, arg, 0)};
+  (void)RTNAME(GetCommandArgument)(
+      n, &value, nullptr, nullptr, __FILE__, __LINE__);
+}
+} // namespace Fortran::runtime
 } // extern "C"