• 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_LEXER_H
17 #define MAPLE_IR_INCLUDE_LEXER_H
18 #include "cstdio"
19 #include <fstream>
20 #include "types_def.h"
21 #include "tokens.h"
22 #include "mempool_allocator.h"
23 #include "mir_module.h"
24 
25 namespace maple {
26 class MIRParser;  // circular dependency exists, no other choice
27 class MIRLexer {
28     friend MIRParser;
29 
30 public:
31     explicit MIRLexer(MIRModule &mod);
~MIRLexer()32     ~MIRLexer()
33     {
34         airFile = nullptr;
35         if (airFileInternal.is_open()) {
36             airFileInternal.close();
37         }
38     }
39 
40     void PrepareForFile(const std::string &filename);
41     void PrepareForString(const std::string &src);
42     TokenKind NextToken();
43     TokenKind LexToken();
GetTokenKind()44     TokenKind GetTokenKind() const
45     {
46         return kind;
47     }
48 
GetLineNum()49     uint32 GetLineNum() const
50     {
51         return lineNum;
52     }
53 
GetCurIdx()54     uint32 GetCurIdx() const
55     {
56         return curIdx;
57     }
58 
59     // get the identifier name after the % or $ prefix
GetName()60     const std::string &GetName() const
61     {
62         return name;
63     }
64 
GetTheIntVal()65     uint64 GetTheIntVal() const
66     {
67         return theIntVal;
68     }
69 
GetTheFloatVal()70     float GetTheFloatVal() const
71     {
72         return theFloatVal;
73     }
74 
GetTheDoubleVal()75     double GetTheDoubleVal() const
76     {
77         return theDoubleVal;
78     }
79 
80     std::string GetTokenString() const;  // for error reporting purpose
81 
82 private:
83     MIRModule &module;
84     // for storing the different types of constant values
85     uint64 theIntVal = 0;  // also indicates preg number under TK_preg
86     float theFloatVal = 0.0;
87     double theDoubleVal = 0.0;
88     MapleVector<std::string> seenComments;
89     std::ifstream *airFile = nullptr;
90     std::ifstream airFileInternal;
91     std::string line;
92     size_t lineBufSize = 0;  // the allocated size of line(buffer).
93     uint32 currentLineSize = 0;
94     uint32 curIdx = 0;
95     uint32 lineNum = 0;
96     TokenKind kind = TK_invalid;
97     std::string name = "";  // store the name token without the % or $ prefix
98     MapleUnorderedMap<std::string, TokenKind> keywordMap;
99     std::queue<std::string> mirQueue;
100     bool needFile = true;
RemoveReturnInline(std::string & line)101     void RemoveReturnInline(std::string &line)
102     {
103         if (line.empty()) {
104             return;
105         }
106         if (line.back() == '\n') {
107             line.pop_back();
108         }
109         if (line.back() == '\r') {
110             line.pop_back();
111         }
112     }
113 
114     int ReadALine();            // read a line from MIR (text) file.
115     int ReadALineByMirQueue();  // read a line from MIR Queue.
116     void GenName();
117     TokenKind GetConstVal();
118     TokenKind GetSpecialFloatConst();
119     TokenKind GetHexConst(uint32 valStart, bool negative);
120     TokenKind GetIntConst(uint32 valStart, bool negative);
121     TokenKind GetFloatConst(uint32 valStart, uint32 startIdx, bool negative);
122     TokenKind GetSpecialTokenUsingOneCharacter(char c);
123     TokenKind GetTokenWithPrefixDollar();
124     TokenKind GetTokenWithPrefixPercent();
125     TokenKind GetTokenWithPrefixAmpersand();
126     TokenKind GetTokenWithPrefixAtOrCircumflex(char prefix);
127     TokenKind GetTokenWithPrefixExclamation();
128     TokenKind GetTokenWithPrefixQuotation();
129     TokenKind GetTokenWithPrefixDoubleQuotation();
130     TokenKind GetTokenSpecial();
131 
GetCharAt(uint32 idx)132     char GetCharAt(uint32 idx) const
133     {
134         return line[idx];
135     }
136 
GetCharAtWithUpperCheck(uint32 idx)137     char GetCharAtWithUpperCheck(uint32 idx) const
138     {
139         return idx < currentLineSize ? line[idx] : 0;
140     }
141 
GetCharAtWithLowerCheck(uint32 idx)142     char GetCharAtWithLowerCheck(uint32 idx) const
143     {
144         return idx >= 0 ? line[idx] : 0;
145     }
146 
GetCurrentCharWithUpperCheck()147     char GetCurrentCharWithUpperCheck()
148     {
149         return curIdx < currentLineSize ? line[curIdx] : 0;
150     }
151 
GetNextCurrentCharWithUpperCheck()152     char GetNextCurrentCharWithUpperCheck()
153     {
154         ++curIdx;
155         return curIdx < currentLineSize ? line[curIdx] : 0;
156     }
157 
SetFile(std::ifstream & file)158     void SetFile(std::ifstream &file)
159     {
160         airFile = &file;
161     }
162 
GetFile()163     std::ifstream *GetFile() const
164     {
165         return airFile;
166     }
167 
SetMirQueue(const std::string & fileText)168     void SetMirQueue(const std::string &fileText)
169     {
170         StringUtils::Split(fileText, mirQueue, '\n');
171         needFile = false;
172     }
173 };
174 
IsPrimitiveType(TokenKind tk)175 inline bool IsPrimitiveType(TokenKind tk)
176 {
177     return (tk >= TK_void) && (tk < TK_unknown);
178 }
179 
IsVarName(TokenKind tk)180 inline bool IsVarName(TokenKind tk)
181 {
182     return (tk == TK_lname) || (tk == TK_gname);
183 }
184 
IsExprBinary(TokenKind tk)185 inline bool IsExprBinary(TokenKind tk)
186 {
187     return (tk >= TK_add) && (tk <= TK_sub);
188 }
189 
IsConstValue(TokenKind tk)190 inline bool IsConstValue(TokenKind tk)
191 {
192     return (tk >= TK_intconst) && (tk <= TK_doubleconst);
193 }
194 
IsConstAddrExpr(TokenKind tk)195 inline bool IsConstAddrExpr(TokenKind tk)
196 {
197     return (tk == TK_addrof) || (tk == TK_addroffunc) || (tk == TK_addroflabel) || (tk == TK_conststr) ||
198            (tk == TK_conststr16);
199 }
200 }  // namespace maple
201 #endif  // MAPLE_IR_INCLUDE_LEXER_H
202