• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 #include <string_view>
17 #include "assembly-parser.h"
18 #include "utils/number-utils.h"
19 
20 namespace ark::pandasm {
21 
Make(const std::vector<ark::pandasm::Token> & t)22 void Context::Make(const std::vector<ark::pandasm::Token> &t)
23 {
24     err = {};
25 
26     insNumber = 0;
27 
28     tokens = t;
29 
30     number = 1;
31 
32     end = false;
33 
34     token = std::string_view(&*(tokens[number - 1].wholeLine.begin() + tokens[number - 1].boundLeft),
35                              tokens[number - 1].boundRight - tokens[number - 1].boundLeft);
36 
37     id = this->tokens[number - 1].type;
38 }
39 
Len() const40 size_t Context::Len() const
41 {
42     return token.size();
43 }
44 
ValidateFoundedRegisterName(char c,size_t n) const45 bool Context::ValidateFoundedRegisterName(char c, size_t n) const
46 {
47     std::string_view p = token;
48 
49     p.remove_prefix(1);
50 
51     if (p.empty() || (p.size() > 1 && p[0] == '0')) {
52         return false;
53     }
54 
55     if (c != 'a') {
56         for (const auto &ch : p) {
57             if (std::isdigit(ch) == 0) {
58                 return false;
59             }
60         }
61     } else {
62         if (ToNumber(p) > n) {
63             return false;
64         }
65     }
66 
67     return true;
68 }
69 
ValidateRegisterName(char c,size_t n) const70 bool Context::ValidateRegisterName(char c, size_t n) const
71 {
72     if (token[0] == c) {
73         return ValidateFoundedRegisterName(c, n);
74     }
75 
76     return false;
77 }
78 
ValidateParameterName(size_t numberOfParamsAlreadyIs) const79 bool Context::ValidateParameterName(size_t numberOfParamsAlreadyIs) const
80 {
81     if (numberOfParamsAlreadyIs >= MAX_DWORD) {
82         return false;
83     }
84 
85     if (token[0] == 'a') {
86         std::string_view p = token;
87 
88         p.remove_prefix(1);
89 
90         if (ToNumber(p) == numberOfParamsAlreadyIs) {
91             return true;
92         }
93     }
94 
95     return false;
96 }
97 
GiveToken()98 std::string_view Context::GiveToken()
99 {
100     return token;
101 }
102 
Next()103 Token::Type Context::Next()
104 {
105     if (this->tokens.size() > number) {
106         return this->tokens[number].type;
107     }
108 
109     return this->tokens[number - 1].type;
110 }
111 
UpSignOperation()112 void Context::UpSignOperation()
113 {
114     signop = id;
115 }
116 
WaitFor()117 Token::Type Context::WaitFor()
118 {
119     return signop;
120 }
121 
Mask()122 bool Context::Mask()
123 {
124     return end;
125 }
126 
NextMask()127 bool Context::NextMask()
128 {
129     if (end) {
130         return true;
131     }
132 
133     return this->tokens.size() < number + 1;
134 }
135 
136 // NOLINTNEXTLINE(cert-dcl21-cpp)
operator ++(int)137 Token::Type Context::operator++(int)
138 {
139     Token::Type lastId = id;
140 
141     if (this->tokens.size() > number) {
142         ++number;
143 
144         id = this->tokens[number - 1].type;
145 
146         token = std::string_view(&*(tokens[number - 1].wholeLine.begin() + tokens[number - 1].boundLeft),
147                                  tokens[number - 1].boundRight - tokens[number - 1].boundLeft);
148     } else {
149         end = true;
150     }
151 
152     return lastId;
153 }
154 
operator ++()155 Token::Type Context::operator++()
156 {
157     if (this->tokens.size() > number) {
158         ++number;
159 
160         id = this->tokens[number - 1].type;
161 
162         token = std::string_view(&*(tokens[number - 1].wholeLine.begin() + tokens[number - 1].boundLeft),
163                                  tokens[number - 1].boundRight - tokens[number - 1].boundLeft);
164     } else {
165         end = true;
166     }
167 
168     return id;
169 }
170 
171 // NOLINTNEXTLINE(cert-dcl21-cpp)
operator --(int)172 Token::Type Context::operator--(int)
173 {
174     Token::Type lastId = id;
175 
176     if (number > 1) {
177         end = false;
178 
179         --number;
180 
181         id = this->tokens[number - 1].type;
182 
183         token = std::string_view(&*(tokens[number - 1].wholeLine.begin() + tokens[number - 1].boundLeft),
184                                  tokens[number - 1].boundRight - tokens[number - 1].boundLeft);
185     } else {
186         end = false;
187     }
188 
189     return lastId;
190 }
191 
operator --()192 Token::Type Context::operator--()
193 {
194     if (number > 1) {
195         end = false;
196 
197         --number;
198 
199         id = this->tokens[number - 1].type;
200 
201         token = std::string_view(&*(tokens[number - 1].wholeLine.begin() + tokens[number - 1].boundLeft),
202                                  tokens[number - 1].boundRight - tokens[number - 1].boundLeft);
203     } else {
204         end = false;
205     }
206 
207     return id;
208 }
209 
operator *()210 Token::Type Context::operator*()
211 {
212     return id;
213 }
214 
215 }  // namespace ark::pandasm
216