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