[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / Support / AutoConvert.cpp
blob8170e553ac6e10d0eaa2cd3f765ac1704b475127
1 //===- AutoConvert.cpp - Auto conversion between ASCII/EBCDIC -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains functions used for auto conversion between
10 // ASCII/EBCDIC codepages specific to z/OS.
12 //===----------------------------------------------------------------------===//
14 #ifdef __MVS__
16 #include "llvm/Support/AutoConvert.h"
17 #include <cassert>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
22 static int savedStdHandleAutoConversionMode[3] = {-1, -1, -1};
24 int disableAutoConversion(int FD) {
25 static const struct f_cnvrt Convert = {
26 SETCVTOFF, // cvtcmd
27 0, // pccsid
28 0, // fccsid
31 return fcntl(FD, F_CONTROL_CVT, &Convert);
34 int restoreStdHandleAutoConversion(int FD) {
35 assert(FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO);
36 if (savedStdHandleAutoConversionMode[FD] == -1)
37 return 0;
38 struct f_cnvrt Cvt = {
39 savedStdHandleAutoConversionMode[FD], // cvtcmd
40 0, // pccsid
41 0, // fccsid
43 return (fcntl(FD, F_CONTROL_CVT, &Cvt));
46 int enableAutoConversion(int FD) {
47 struct f_cnvrt Query = {
48 QUERYCVT, // cvtcmd
49 0, // pccsid
50 0, // fccsid
53 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
54 return -1;
56 // We don't need conversion for UTF-8 tagged files.
57 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
58 // problems related to UTF-8 tagged source files.
59 // When the pccsid is not ISO8859-1, autoconversion is still needed.
60 if (Query.pccsid == CCSID_ISO8859_1 &&
61 (Query.fccsid == CCSID_UTF_8 || Query.fccsid == CCSID_ISO8859_1))
62 return 0;
64 // Save the state of std handles before we make changes to it.
65 if ((FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO) &&
66 savedStdHandleAutoConversionMode[FD] == -1)
67 savedStdHandleAutoConversionMode[FD] = Query.cvtcmd;
69 if (FD == STDOUT_FILENO || FD == STDERR_FILENO)
70 Query.cvtcmd = SETCVTON;
71 else
72 Query.cvtcmd = SETCVTALL;
74 Query.pccsid =
75 (FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO)
76 ? 0
77 : CCSID_UTF_8;
78 // Assume untagged files to be IBM-1047 encoded.
79 Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
80 return fcntl(FD, F_CONTROL_CVT, &Query);
83 std::error_code llvm::disableAutoConversion(int FD) {
84 if (::disableAutoConversion(FD) == -1)
85 return std::error_code(errno, std::generic_category());
87 return std::error_code();
90 std::error_code llvm::enableAutoConversion(int FD) {
91 if (::enableAutoConversion(FD) == -1)
92 return std::error_code(errno, std::generic_category());
94 return std::error_code();
97 std::error_code llvm::restoreStdHandleAutoConversion(int FD) {
98 if (::restoreStdHandleAutoConversion(FD) == -1)
99 return std::error_code(errno, std::generic_category());
101 return std::error_code();
104 std::error_code llvm::setFileTag(int FD, int CCSID, bool Text) {
105 assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
106 "FT_UNTAGGED and FT_BINARY are not allowed for text files");
107 struct file_tag Tag;
108 Tag.ft_ccsid = CCSID;
109 Tag.ft_txtflag = Text;
110 Tag.ft_deferred = 0;
111 Tag.ft_rsvflags = 0;
113 if (fcntl(FD, F_SETTAG, &Tag) == -1)
114 return std::error_code(errno, std::generic_category());
115 return std::error_code();
118 #endif // __MVS__