1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef HC_GEN_BYTECODE_GEN_H
10 #define HC_GEN_BYTECODE_GEN_H
11 
12 #include <fstream>
13 #include "generator.h"
14 
15 namespace OHOS {
16 namespace Hardware {
17 struct OpCode {
OpCodeOpCode18     OpCode() : opCode(0), size(0) {}
19 
OpCodeOpCode20     OpCode(uint8_t code, uint32_t s, const std::string &str) : opCode(code), size(s), opStr(str) {}
21 
22     ~OpCode() = default;
23 
24     uint8_t opCode;
25     uint32_t size;
26     const std::string opStr;
27 };
28 
29 class ByteCodeGen : public Generator {
30 public:
31     explicit ByteCodeGen(std::shared_ptr<Ast> ast);
32 
33     ~ByteCodeGen() override = default;
34 
35     bool Output() override;
36 
37 private:
38     bool Initialize();
39 
40     bool ByteCodeConvert();
41 
42     uint32_t Align(uint32_t size) const;
43 
44     void CalculateSize(const std::shared_ptr<AstObject> &object) const;
45 
46     bool ByteCodeWrite(bool dummy);
47 
48     bool ByteCodeWriteWalk();
49 
50     template <typename T>
51     void Write(T &data);
52 
53     void Write(const char *data, uint32_t size);
54 
55     void Write(const std::string &data);
56 
57     void FsWrite(const char *data, uint32_t size);
58 
59     static const OpCode &ToOpCode(uint32_t objectType);
60 
61     bool WriteBad();
62 
63     bool HexdumpInitialize(FILE *&in, FILE *&out);
64 
65     static bool HexdumpOutput(FILE *in, FILE *out);
66 
67     bool Hexdump();
68 
69     bool needAlign_;
70     std::ofstream ofs_;
71     std::string outFileName_;
72     bool dummyOutput_;
73     uint32_t writeSize_;
74 };
75 } // namespace Hardware
76 } // namespace OHOS
77 #endif // HC_GEN_BYTECODE_GEN_H
78