• 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 MAPLE_IR_INCLUDE_MIR_PARSER_H
17 #define MAPLE_IR_INCLUDE_MIR_PARSER_H
18 #include "mir_module.h"
19 #include "lexer.h"
20 #include "mir_nodes.h"
21 #include "mir_preg.h"
22 #include "mir_scope.h"
23 #include "parser_opt.h"
24 
25 namespace maple {
26 using BaseNodePtr = BaseNode *;
27 using StmtNodePtr = StmtNode *;
28 using BlockNodePtr = BlockNode *;
29 
30 class FormalDef;
31 
32 class MIRParser {
33 public:
MIRParser(MIRModule & md)34     explicit MIRParser(MIRModule &md) : lexer(md), mod(md), definedLabels(mod.GetMPAllocator().Adapter())
35     {
36         safeRegionFlag.push(false);
37     }
38 
39     ~MIRParser() = default;
40 
41     MIRFunction *CreateDummyFunction();
ResetCurrentFunction()42     void ResetCurrentFunction()
43     {
44         mod.SetCurFunction(dummyFunction);
45     }
46 
47     bool ParseLoc();
48     bool ParsePosition(SrcPosition &pos);
49     bool ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar);
50     uint8 *ParseWordsInfo(uint32 size);
51     bool ParseSwitchCase(int64 &, LabelIdx &);
52     bool ParseExprOneOperand(BaseNodePtr &expr);
53     bool ParseExprTwoOperand(BaseNodePtr &opnd0, BaseNodePtr &opnd1);
54     bool ParseExprNaryOperand(MapleVector<BaseNode *> &opndVec);
55     bool IsDelimitationTK(TokenKind tk) const;
56     Opcode GetOpFromToken(TokenKind tk) const;
57     bool IsStatement(TokenKind tk) const;
58     PrimType GetPrimitiveType(TokenKind tk) const;
59     MIRIntrinsicID GetIntrinsicID(TokenKind tk) const;
60     bool ParseScalarValue(MIRConstPtr &stype, MIRType &type);
61     bool ParseDeclaredSt(StIdx &stidx);
62     void CreateFuncMIRSymbol(PUIdx &puidx, GStrIdx strIdx);
63     bool ParseDeclaredFunc(PUIdx &puidx);
64     bool ParseTypeAttrs(TypeAttrs &attrs);
65     bool ParseVarTypeAttrs(MIRSymbol &st);
66     bool CheckAlignTk();
67     bool ParseAlignAttrs(TypeAttrs &tA);
68     bool ParsePackAttrs();
69     bool ParseFieldAttrs(FieldAttrs &attrs);
70     bool ParseFuncAttrs(FuncAttrs &attrs);
71     void SetAttrContent(FuncAttrs &attrs, FuncAttrKind x, const MIRLexer &lexer);
72     bool CheckPrimAndDerivedType(TokenKind tokenKind, TyIdx &tyIdx);
73     bool ParsePrimType(TyIdx &tyIdx);
74     bool ParseFarrayType(TyIdx &arrayTyIdx);
75     bool ParseArrayType(TyIdx &arrayTyIdx);
76     bool ParseBitFieldType(TyIdx &fieldTyIdx);
77     bool ParsePragmaElementForArray(MIRPragmaElement &elem);
78     bool ParseClassType(TyIdx &styidx, const GStrIdx &strIdx = GStrIdx(0));
79     bool ParseInterfaceType(TyIdx &sTyIdx, const GStrIdx &strIdx = GStrIdx(0));
80     bool ParseTypeParam(TyIdx &definedTyIdx);
81     bool ParseGenericInstantVector(MIRInstantVectorType &insVecType);
82     bool ParseType(TyIdx &tyIdx);
83     bool ParseSpecialReg(PregIdx &pRegIdx);
84     bool ParsePseudoReg(PrimType primType, PregIdx &pRegIdx);
85     bool ParseStorageClass(MIRSymbol &symbol) const;
86     bool ParseDeclareVarInitValue(MIRSymbol &symbol);
87     bool ParseDeclareReg(MIRSymbol &symbol, const MIRFunction &func);
88     bool ParseDeclareFormal(FormalDef &formalDef);
89     bool ParsePrototypeRemaining(MIRFunction &func, std::vector<TyIdx> &vecTyIdx, std::vector<TypeAttrs> &vecAttrs,
90                                  bool &varArgs);
91 
92     // Stmt Parser
93     PUIdx EnterUndeclaredFunction(bool isMcount = false);  // for -pg in order to add "void _mcount()"
94     bool ParseStmtCallMcount(StmtNodePtr &stmt);  // for -pg in order to add "void _mcount()" to all the functions
95     bool ParseCallReturns(CallReturnVector &retsvec);
96     bool ParseBinaryStmt(StmtNodePtr &stmt, Opcode op);
97 
98     // Expression Parser
99     bool ParseExprIreadIaddrof(IreadNode &expr);
100     bool ParseExprSTACKJarray(BaseNodePtr &expr);
101     bool ParseNaryExpr(NaryStmtNode &stmtNode);
102 
103     // funcName and paramIndex is out parameter
104     bool ParseCallAssertInfo(std::string &funcName, int *paramIndex, std::string &stmtFuncName);
105     bool ParseAssertInfo(std::string &funcName);
106     bool ParseIntrinsicId(IntrinsicopNode &intrnOpNode);
107     void Error(const std::string &str);
108     void Warning(const std::string &str);
109 
110     const std::string &GetError();
111     const std::string &GetWarning() const;
112     bool ParseFuncInfo(void);
113     void PrepareParsingMIR();
114     void PrepareParsingMplt();
115     bool ParseSrcLang(MIRSrcLang &srcLang);
116     bool ParseMPLT(std::ifstream &mpltFile, const std::string &importFileName);
117     bool ParseTypeFromString(const std::string &src, TyIdx &tyIdx);
118     void EmitError(const std::string &fileName);
119     void EmitWarning(const std::string &fileName);
GetOptions()120     uint32 GetOptions() const
121     {
122         return options;
123     }
124 
125 private:
126     // func ptr map for ParseMIR()
127 
128     bool TypeCompatible(TyIdx typeIdx1, TyIdx typeIdx2);
129     bool IsTypeIncomplete(MIRType *type);
130 
131     // func for ParseMIR
132     bool ParseMIRForFunc();
133     bool ParseMIRForFlavor();
134     bool ParseMIRForSrcLang();
135     bool ParseMIRForGlobalMemSize();
136     bool ParseMIRForGlobalMemMap();
137     bool ParseMIRForGlobalWordsTypeTagged();
138     bool ParseMIRForGlobalWordsRefCounted();
139     bool ParseMIRForID();
140     bool ParseMIRForNumFuncs();
141     bool ParseMIRForEntryFunc();
142     bool ParseMIRForFileInfo();
143     bool ParseMIRForFileData();
144     bool ParseMIRForSrcFileInfo();
145     bool ParseMIRForImportPath();
146     bool ParseMIRForAsmdecl();
147 
148     // func and param for ParseStmtBlock
149     void ParseStmtBlockForSeenComment(BlockNodePtr blk, uint32 mplNum);
150     bool ParseStmtBlockForVar(TokenKind stmtTK);
151     bool ParseStmtBlockForVar();
152     bool ParseStmtBlockForTempVar();
153     bool ParseStmtBlockForReg();
154     bool ParseStmtBlockForType();
155     bool ParseStmtBlockForFrameSize();
156     bool ParseStmtBlockForUpformalSize();
157     bool ParseStmtBlockForModuleID();
158     bool ParseStmtBlockForFuncSize();
159     bool ParseStmtBlockForFuncID();
160     bool ParseStmtBlockForFormalWordsTypeTagged();
161     bool ParseStmtBlockForLocalWordsTypeTagged();
162     bool ParseStmtBlockForFormalWordsRefCounted();
163     bool ParseStmtBlockForLocalWordsRefCounted();
164     bool ParseStmtBlockForFuncInfo();
165 
166     // common func
167     void SetSrcPos(SrcPosition &srcPosition, uint32 mplNum);
168 
169     // func for ParseExpr
170     Opcode paramOpForStmt = OP_undef;
171     TokenKind paramTokenKindForStmt = TK_invalid;
172     // func and param for ParseStmtBlock
173     MIRFunction *paramCurrFuncForParseStmtBlock = nullptr;
174     MIRLexer lexer;
175     MIRModule &mod;
176     std::string message;
177     std::string warningMessage;
178     uint32 options = kKeepFirst;
179     MapleVector<bool> definedLabels;  // true if label at labidx is defined
180     MIRFunction *dummyFunction = nullptr;
181     MIRFunction *curFunc = nullptr;
182     uint16 lastFileNum = 0;                // to remember first number after LOC
183     uint32 lastLineNum = 0;                // to remember second number after LOC
184     uint16 lastColumnNum = 0;              // to remember third number after LOC
185     uint32 firstLineNum = 0;               // to track function starting line
186     std::map<TyIdx, TyIdx> typeDefIdxMap;  // map previous declared tyIdx
187     bool firstImport = true;               // Mark the first imported mplt file
188     bool paramParseLocalType = false;      // param for ParseTypedef
189     uint32 paramFileIdx = 0;               // param for ParseMIR()
190     bool paramIsIPA = false;
191     bool paramIsComb = false;
192     TokenKind paramTokenKind = TK_invalid;
193     std::vector<std::string> paramImportFileList;
194     std::stack<bool> safeRegionFlag;
195 };
196 }  // namespace maple
197 #endif  // MAPLE_IR_INCLUDE_MIR_PARSER_H
198