1 //===- yaml2goff - Convert YAML to a GOFF object file ---------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// The GOFF component of yaml2obj.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ObjectYAML/ObjectYAML.h"
15 #include "llvm/ObjectYAML/yaml2obj.h"
16 #include "llvm/Support/ConvertEBCDIC.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/raw_ostream.h"
24 // Common flag values on records.
26 // Flag: This record is continued.
29 // Flag: This record is a continuation.
30 Rec_Continuation
= 1 << (8 - 6 - 1),
33 template <typename ValueType
> struct BinaryBeImpl
{
35 BinaryBeImpl(ValueType V
) : Value(V
) {}
38 template <typename ValueType
>
39 raw_ostream
&operator<<(raw_ostream
&OS
, const BinaryBeImpl
<ValueType
> &BBE
) {
40 char Buffer
[sizeof(BBE
.Value
)];
41 support::endian::write
<ValueType
, llvm::endianness::big
, support::unaligned
>(
43 OS
.write(Buffer
, sizeof(BBE
.Value
));
47 template <typename ValueType
> BinaryBeImpl
<ValueType
> binaryBe(ValueType V
) {
48 return BinaryBeImpl
<ValueType
>(V
);
55 raw_ostream
&operator<<(raw_ostream
&OS
, const ZerosImpl
&Z
) {
56 OS
.write_zeros(Z
.NumBytes
);
60 ZerosImpl
zeros(const size_t NumBytes
) { return ZerosImpl
{NumBytes
}; }
62 // The GOFFOstream is responsible to write the data into the fixed physical
63 // records of the format. A user of this class announces the start of a new
64 // logical record and the size of its payload. While writing the payload, the
65 // physical records are created for the data. Possible fill bytes at the end of
66 // a physical record are written automatically.
67 class GOFFOstream
: public raw_ostream
{
69 explicit GOFFOstream(raw_ostream
&OS
)
70 : OS(OS
), LogicalRecords(0), RemainingSize(0), NewLogicalRecord(false) {
71 SetBufferSize(GOFF::PayloadLength
);
74 ~GOFFOstream() { finalize(); }
76 void makeNewRecord(GOFF::RecordType Type
, size_t Size
) {
80 if (size_t Gap
= (RemainingSize
% GOFF::PayloadLength
))
81 RemainingSize
+= GOFF::PayloadLength
- Gap
;
82 NewLogicalRecord
= true;
86 void finalize() { fillRecord(); }
88 uint32_t logicalRecords() { return LogicalRecords
; }
91 // The underlying raw_ostream.
94 // The number of logical records emitted so far.
95 uint32_t LogicalRecords
;
97 // The remaining size of this logical record, including fill bytes.
100 // The type of the current (logical) record.
101 GOFF::RecordType CurrentType
;
103 // Signals start of new record.
104 bool NewLogicalRecord
;
106 // Return the number of bytes left to write until next physical record.
107 // Please note that we maintain the total number of bytes left, not the
109 size_t bytesToNextPhysicalRecord() {
110 size_t Bytes
= RemainingSize
% GOFF::PayloadLength
;
111 return Bytes
? Bytes
: GOFF::PayloadLength
;
114 // Write the record prefix of a physical record, using the current record
116 static void writeRecordPrefix(raw_ostream
&OS
, GOFF::RecordType Type
,
117 size_t RemainingSize
,
118 uint8_t Flags
= Rec_Continuation
) {
119 uint8_t TypeAndFlags
= Flags
| (Type
<< 4);
120 if (RemainingSize
> GOFF::RecordLength
)
121 TypeAndFlags
|= Rec_Continued
;
122 OS
<< binaryBe(static_cast<unsigned char>(GOFF::PTVPrefix
))
123 << binaryBe(static_cast<unsigned char>(TypeAndFlags
))
124 << binaryBe(static_cast<unsigned char>(0));
127 // Fill the last physical record of a logical record with zero bytes.
129 assert((GetNumBytesInBuffer() <= RemainingSize
) &&
130 "More bytes in buffer than expected");
131 size_t Remains
= RemainingSize
- GetNumBytesInBuffer();
133 assert((Remains
< GOFF::RecordLength
) &&
134 "Attempting to fill more than one physical record");
135 raw_ostream::write_zeros(Remains
);
138 assert(RemainingSize
== 0 && "Not fully flushed");
139 assert(GetNumBytesInBuffer() == 0 && "Buffer not fully empty");
142 // See raw_ostream::write_impl.
143 void write_impl(const char *Ptr
, size_t Size
) override
{
144 assert((RemainingSize
>= Size
) && "Attempt to write too much data");
145 assert(RemainingSize
&& "Logical record overflow");
146 if (!(RemainingSize
% GOFF::PayloadLength
)) {
147 writeRecordPrefix(OS
, CurrentType
, RemainingSize
,
148 NewLogicalRecord
? 0 : Rec_Continuation
);
149 NewLogicalRecord
= false;
151 assert(!NewLogicalRecord
&&
152 "New logical record not on physical record boundary");
156 size_t BytesToWrite
= bytesToNextPhysicalRecord();
157 if (BytesToWrite
> Size
)
159 OS
.write(Ptr
+ Idx
, BytesToWrite
);
161 Size
-= BytesToWrite
;
162 RemainingSize
-= BytesToWrite
;
164 writeRecordPrefix(OS
, CurrentType
, RemainingSize
);
169 // Return the current position within the stream, not counting the bytes
170 // currently in the buffer.
171 uint64_t current_pos() const override
{ return OS
.tell(); }
175 void writeHeader(GOFFYAML::FileHeader
&FileHdr
);
178 void reportError(const Twine
&Msg
) {
183 GOFFState(raw_ostream
&OS
, GOFFYAML::Object
&Doc
,
184 yaml::ErrorHandler ErrHandler
)
185 : GW(OS
), Doc(Doc
), ErrHandler(ErrHandler
), HasError(false) {}
187 ~GOFFState() { GW
.finalize(); }
192 static bool writeGOFF(raw_ostream
&OS
, GOFFYAML::Object
&Doc
,
193 yaml::ErrorHandler ErrHandler
);
197 GOFFYAML::Object
&Doc
;
198 yaml::ErrorHandler ErrHandler
;
202 void GOFFState::writeHeader(GOFFYAML::FileHeader
&FileHdr
) {
203 SmallString
<16> CCSIDName
;
204 if (std::error_code EC
=
205 ConverterEBCDIC::convertToEBCDIC(FileHdr
.CharacterSetName
, CCSIDName
))
206 reportError("Conversion error on " + FileHdr
.CharacterSetName
);
207 if (CCSIDName
.size() > 16) {
208 reportError("CharacterSetName too long");
209 CCSIDName
.resize(16);
211 SmallString
<16> LangProd
;
212 if (std::error_code EC
= ConverterEBCDIC::convertToEBCDIC(
213 FileHdr
.LanguageProductIdentifier
, LangProd
))
214 reportError("Conversion error on " + FileHdr
.LanguageProductIdentifier
);
215 if (LangProd
.size() > 16) {
216 reportError("LanguageProductIdentifier too long");
220 GW
.makeNewRecord(GOFF::RT_HDR
, GOFF::PayloadLength
);
221 GW
<< binaryBe(FileHdr
.TargetEnvironment
) // TargetEnvironment
222 << binaryBe(FileHdr
.TargetOperatingSystem
) // TargetOperatingSystem
223 << zeros(2) // Reserved
224 << binaryBe(FileHdr
.CCSID
) // CCSID
225 << CCSIDName
// CharacterSetName
226 << zeros(16 - CCSIDName
.size()) // Fill bytes
227 << LangProd
// LanguageProductIdentifier
228 << zeros(16 - LangProd
.size()) // Fill bytes
229 << binaryBe(FileHdr
.ArchitectureLevel
); // ArchitectureLevel
230 // The module propties are optional. Figure out if we need to write them.
231 uint16_t ModPropLen
= 0;
232 if (FileHdr
.TargetSoftwareEnvironment
)
234 else if (FileHdr
.InternalCCSID
)
237 GW
<< binaryBe(ModPropLen
) << zeros(6);
239 GW
<< binaryBe(FileHdr
.InternalCCSID
.value_or(0));
241 GW
<< binaryBe(FileHdr
.TargetSoftwareEnvironment
.value_or(0));
245 void GOFFState::writeEnd() {
246 GW
.makeNewRecord(GOFF::RT_END
, GOFF::PayloadLength
);
247 GW
<< binaryBe(uint8_t(0)) // No entry point
248 << binaryBe(uint8_t(0)) // No AMODE
249 << zeros(3) // Reserved
250 << binaryBe(GW
.logicalRecords());
251 // No entry point yet. Automatically fill remaining space with zero bytes.
255 bool GOFFState::writeObject() {
256 writeHeader(Doc
.Header
);
263 bool GOFFState::writeGOFF(raw_ostream
&OS
, GOFFYAML::Object
&Doc
,
264 yaml::ErrorHandler ErrHandler
) {
265 GOFFState
State(OS
, Doc
, ErrHandler
);
266 return State
.writeObject();
273 bool yaml2goff(llvm::GOFFYAML::Object
&Doc
, raw_ostream
&Out
,
274 ErrorHandler ErrHandler
) {
275 return GOFFState::writeGOFF(Out
, Doc
, ErrHandler
);