• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MAPLEBE_INCLUDE_CG_X86_64_EMITTER_H
17 #define MAPLEBE_INCLUDE_CG_X86_64_EMITTER_H
18 
19 #include "emit.h"
20 #include "assembler/asm_assembler.h"
21 #include "assembler/elf_assembler.h"
22 
23 namespace maplebe {
24 class X64Emitter : public Emitter {
25 public:
X64Emitter(CG & cg,assembler::Assembler & newAssembler)26     X64Emitter(CG &cg, assembler::Assembler &newAssembler) : Emitter(cg, ""), assmbler(newAssembler) {}
27     ~X64Emitter() = default;
28 
GetAssembler()29     assembler::Assembler &GetAssembler() const
30     {
31         return assmbler;
32     }
33 
34     assembler::Reg TransferReg(Operand *opnd) const;
35     std::pair<int64, bool> TransferImm(Operand *opnd);
36     assembler::Mem TransferMem(Operand *opnd, uint32 funcUniqueId);
37     int64 TransferLabel(Operand *opnd, uint32 funcUniqueId);
38     uint32 TransferFuncName(Operand *opnd);
39 
40     void EmitFunctionHeader(maplebe::CGFunc &cgFunc);
41     void EmitBBHeaderLabel(maplebe::CGFunc &cgFunc, LabelIdx labIdx, uint32 freq);
42     void EmitInsn(Insn &insn, uint32 funcUniqueId);
43     void EmitJmpTable(const maplebe::CGFunc &cgFunc);
44     void EmitFunctionFoot(maplebe::CGFunc &cgFunc);
45     uint8 GetSymbolAlign(const maple::MIRSymbol &mirSymbol, bool isComm = false);
46     uint64 GetSymbolSize(maple::TyIdx typeIndex);
47     void EmitLocalVariable(maplebe::CGFunc &cgFunc);
48     void EmitGlobalVariable(maplebe::CG &cg);
49     uint64 EmitStructure(maple::MIRConst &mirConst, maplebe::CG &cg, bool belongsToDataSec = true);
50     uint64 EmitStructure(maple::MIRConst &mirConst, maplebe::CG &cg, uint32 &subStructFieldCounts,
51                          bool belongsToDataSec = true);
52     uint64 EmitVector(maple::MIRConst &mirConst, bool belongsToDataSec = true);
53     uint64 EmitArray(maple::MIRConst &mirConst, maplebe::CG &cg, bool belongsToDataSec = true);
54     void EmitAddrofElement(MIRConst &mirConst, bool belongsToDataSec);
55     uint32 EmitSingleElement(maple::MIRConst &mirConst, bool belongsToDataSec = true, bool isIndirect = false);
56     void EmitBitField(maplebe::StructEmitInfo &structEmitInfo, maple::MIRConst &mirConst,
57                       const maple::MIRType *nextType, uint64 fieldOffset, bool belongsToDataSec = true);
58     void EmitCombineBfldValue(maplebe::StructEmitInfo &structEmitInfo, bool belongsToDataSec = true);
59     void EmitStringPointers();
60     void Run(maplebe::CGFunc &cgFunc);
61 
62     /* Dwarf debug info */
63     void EmitDIHeaderFileInfo();
64     void UpdateAttrAndEmit(const std::string &sfile, DebugInfo &mirdi, DBGAbbrevEntry &diae, DBGDie &die,
65                            const std::string &spath);
66     void EmitDIDebugInfoSection(maplebe::DebugInfo &mirdi);
67     void EmitDwFormAddr(const DBGDie &die, const DBGDieAttr &attr, DwAt attrName, DwTag tagName, DebugInfo &di);
68     void EmitDwFormRef4(DBGDie &die, const DBGDieAttr &attr, DwAt attrName, DwTag tagName, DebugInfo &di);
69     void EmitDwFormData8(const DBGDieAttr &attr, DwAt attrName, DwTag tagName, DebugInfo &di,
70                          MapleVector<DBGDieAttr *> &attrvec);
71     void EmitDIAttrValue(DBGDie &die, DBGDieAttr &attr, DwAt attrName, DwTag tagName, DebugInfo &di);
72     void EmitDIDebugAbbrevSection(maplebe::DebugInfo &mirdi);
73     void EmitDIDebugStrSection();
74     void EmitDebugInfo(maplebe::CG &cg);
75 
76 private:
77     assembler::Assembler &assmbler;
78     std::vector<uint32> stringPtr;
79     const MapleString *currDebugComment { nullptr };
80 };
81 } /* namespace maplebe */
82 
83 #endif /* MAPLEBE_INCLUDE_CG_X86_64_EMITTER_H */
84