• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 struct OpCode {
OpCodeOpCode19     OpCode() : opCode(0), size(0) {}
20 
OpCodeOpCode21     OpCode(uint8_t code, uint32_t s, std::string str) : opCode(code), size(s), opStr(std::move(str)) {}
22 
23     ~OpCode() = default;
24 
25     uint8_t opCode;
26     uint32_t size;
27     const std::string opStr;
28 };
29 
30 class ByteCodeGen : public Generator {
31 public:
32     explicit ByteCodeGen(std::shared_ptr<Ast> ast);
33 
34     ~ByteCodeGen() override = default;
35 
36     bool Output() override;
37 
38 private:
39     bool Initialize();
40 
41     bool ByteCodeConvert();
42 
43     uint32_t Align(uint32_t size) const;
44 
45     void CalculateSize(const std::shared_ptr<AstObject> &object);
46 
47     bool ByteCodeWrite(bool dummy);
48 
49     bool ByteCodeWriteWalk();
50 
51     template <typename T>
52     void Write(T &data);
53 
54     void Write(const char *data, uint32_t size);
55 
56     void Write(const std::string &data);
57 
58     void FsWrite(const char *data, uint32_t size);
59 
60     static const OpCode &ToOpCode(uint32_t objectType);
61 
62     bool WriteBad();
63 
64     bool HexdumpInitialize(FILE *&in, FILE *&out);
65 
66     static bool HexdumpOutput(FILE *in, FILE *out);
67 
68     bool Hexdump();
69 
70     bool needAlign_;
71     std::ofstream ofs_;
72     std::string outFileName_;
73     bool dummyOutput_;
74     uint32_t writeSize_;
75 };
76 } // namespace Hardware
77 } // namespace OHOS
78 #endif // HC_GEN_BYTECODE_GEN_H
79