1b1994897Sopenharmony_ci/** 2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License. 5b1994897Sopenharmony_ci * You may obtain a copy of the License at 6b1994897Sopenharmony_ci * 7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1994897Sopenharmony_ci * 9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and 13b1994897Sopenharmony_ci * limitations under the License. 14b1994897Sopenharmony_ci */ 15b1994897Sopenharmony_ci 16b1994897Sopenharmony_ci#include <string_view> 17b1994897Sopenharmony_ci#include "assembly-parser.h" 18b1994897Sopenharmony_ci#include "utils/number-utils.h" 19b1994897Sopenharmony_ci 20b1994897Sopenharmony_cinamespace panda::pandasm { 21b1994897Sopenharmony_ci 22b1994897Sopenharmony_civoid Context::Make(const std::vector<panda::pandasm::Token> &t) 23b1994897Sopenharmony_ci{ 24b1994897Sopenharmony_ci err = {}; 25b1994897Sopenharmony_ci 26b1994897Sopenharmony_ci ins_number = 0; 27b1994897Sopenharmony_ci 28b1994897Sopenharmony_ci tokens = t; 29b1994897Sopenharmony_ci 30b1994897Sopenharmony_ci number = 1; 31b1994897Sopenharmony_ci 32b1994897Sopenharmony_ci end = false; 33b1994897Sopenharmony_ci 34b1994897Sopenharmony_ci token = std::string_view(&*(tokens[number - 1].whole_line.begin() + tokens[number - 1].bound_left), 35b1994897Sopenharmony_ci tokens[number - 1].bound_right - tokens[number - 1].bound_left); 36b1994897Sopenharmony_ci 37b1994897Sopenharmony_ci id = this->tokens[number - 1].type; 38b1994897Sopenharmony_ci} 39b1994897Sopenharmony_ci 40b1994897Sopenharmony_cisize_t Context::Len() const 41b1994897Sopenharmony_ci{ 42b1994897Sopenharmony_ci return token.size(); 43b1994897Sopenharmony_ci} 44b1994897Sopenharmony_ci 45b1994897Sopenharmony_cibool Context::ValidateRegisterName(char c, size_t n) const 46b1994897Sopenharmony_ci{ 47b1994897Sopenharmony_ci if (token[0] == c) { 48b1994897Sopenharmony_ci std::string_view p = token; 49b1994897Sopenharmony_ci 50b1994897Sopenharmony_ci p.remove_prefix(1); 51b1994897Sopenharmony_ci 52b1994897Sopenharmony_ci if (p.empty() || (p.size() > 1 && p[0] == '0')) { 53b1994897Sopenharmony_ci return false; 54b1994897Sopenharmony_ci } 55b1994897Sopenharmony_ci 56b1994897Sopenharmony_ci if (c != 'a') { 57b1994897Sopenharmony_ci for (const auto &ch : p) { 58b1994897Sopenharmony_ci if (std::isdigit(ch) == 0) { 59b1994897Sopenharmony_ci return false; 60b1994897Sopenharmony_ci } 61b1994897Sopenharmony_ci } 62b1994897Sopenharmony_ci } else { 63b1994897Sopenharmony_ci if (ToNumber(p) > n) { 64b1994897Sopenharmony_ci return false; 65b1994897Sopenharmony_ci } 66b1994897Sopenharmony_ci } 67b1994897Sopenharmony_ci 68b1994897Sopenharmony_ci return true; 69b1994897Sopenharmony_ci } 70b1994897Sopenharmony_ci 71b1994897Sopenharmony_ci return false; 72b1994897Sopenharmony_ci} 73b1994897Sopenharmony_ci 74b1994897Sopenharmony_cibool Context::ValidateParameterName(size_t number_of_params_already_is) const 75b1994897Sopenharmony_ci{ 76b1994897Sopenharmony_ci if (number_of_params_already_is >= MAX_DWORD) { 77b1994897Sopenharmony_ci return false; 78b1994897Sopenharmony_ci } 79b1994897Sopenharmony_ci 80b1994897Sopenharmony_ci if (token[0] == 'a') { 81b1994897Sopenharmony_ci std::string_view p = token; 82b1994897Sopenharmony_ci 83b1994897Sopenharmony_ci p.remove_prefix(1); 84b1994897Sopenharmony_ci 85b1994897Sopenharmony_ci if (ToNumber(p) == number_of_params_already_is) { 86b1994897Sopenharmony_ci return true; 87b1994897Sopenharmony_ci } 88b1994897Sopenharmony_ci } 89b1994897Sopenharmony_ci 90b1994897Sopenharmony_ci return false; 91b1994897Sopenharmony_ci} 92b1994897Sopenharmony_ci 93b1994897Sopenharmony_cistd::string_view Context::GiveToken() 94b1994897Sopenharmony_ci{ 95b1994897Sopenharmony_ci return token; 96b1994897Sopenharmony_ci} 97b1994897Sopenharmony_ci 98b1994897Sopenharmony_ciToken::Type Context::Next() 99b1994897Sopenharmony_ci{ 100b1994897Sopenharmony_ci if (this->tokens.size() > number) { 101b1994897Sopenharmony_ci return this->tokens[number].type; 102b1994897Sopenharmony_ci } 103b1994897Sopenharmony_ci 104b1994897Sopenharmony_ci return this->tokens[number - 1].type; 105b1994897Sopenharmony_ci} 106b1994897Sopenharmony_ci 107b1994897Sopenharmony_civoid Context::UpSignOperation() 108b1994897Sopenharmony_ci{ 109b1994897Sopenharmony_ci signop = id; 110b1994897Sopenharmony_ci} 111b1994897Sopenharmony_ci 112b1994897Sopenharmony_ciToken::Type Context::WaitFor() 113b1994897Sopenharmony_ci{ 114b1994897Sopenharmony_ci return signop; 115b1994897Sopenharmony_ci} 116b1994897Sopenharmony_ci 117b1994897Sopenharmony_cibool Context::Mask() 118b1994897Sopenharmony_ci{ 119b1994897Sopenharmony_ci return end; 120b1994897Sopenharmony_ci} 121b1994897Sopenharmony_ci 122b1994897Sopenharmony_cibool Context::NextMask() 123b1994897Sopenharmony_ci{ 124b1994897Sopenharmony_ci if (end) { 125b1994897Sopenharmony_ci return true; 126b1994897Sopenharmony_ci } 127b1994897Sopenharmony_ci 128b1994897Sopenharmony_ci return this->tokens.size() < number + 1; 129b1994897Sopenharmony_ci} 130b1994897Sopenharmony_ci 131b1994897Sopenharmony_ci// NOLINTNEXTLINE(cert-dcl21-cpp) 132b1994897Sopenharmony_ciToken::Type Context::operator++(int) 133b1994897Sopenharmony_ci{ 134b1994897Sopenharmony_ci Token::Type last_id = id; 135b1994897Sopenharmony_ci 136b1994897Sopenharmony_ci if (this->tokens.size() > number) { 137b1994897Sopenharmony_ci ++number; 138b1994897Sopenharmony_ci 139b1994897Sopenharmony_ci id = this->tokens[number - 1].type; 140b1994897Sopenharmony_ci 141b1994897Sopenharmony_ci token = std::string_view(&*(tokens[number - 1].whole_line.begin() + tokens[number - 1].bound_left), 142b1994897Sopenharmony_ci tokens[number - 1].bound_right - tokens[number - 1].bound_left); 143b1994897Sopenharmony_ci } else { 144b1994897Sopenharmony_ci end = true; 145b1994897Sopenharmony_ci } 146b1994897Sopenharmony_ci 147b1994897Sopenharmony_ci return last_id; 148b1994897Sopenharmony_ci} 149b1994897Sopenharmony_ci 150b1994897Sopenharmony_ciToken::Type Context::operator++() 151b1994897Sopenharmony_ci{ 152b1994897Sopenharmony_ci if (this->tokens.size() > number) { 153b1994897Sopenharmony_ci ++number; 154b1994897Sopenharmony_ci 155b1994897Sopenharmony_ci id = this->tokens[number - 1].type; 156b1994897Sopenharmony_ci 157b1994897Sopenharmony_ci token = std::string_view(&*(tokens[number - 1].whole_line.begin() + tokens[number - 1].bound_left), 158b1994897Sopenharmony_ci tokens[number - 1].bound_right - tokens[number - 1].bound_left); 159b1994897Sopenharmony_ci } else { 160b1994897Sopenharmony_ci end = true; 161b1994897Sopenharmony_ci } 162b1994897Sopenharmony_ci 163b1994897Sopenharmony_ci return id; 164b1994897Sopenharmony_ci} 165b1994897Sopenharmony_ci 166b1994897Sopenharmony_ci// NOLINTNEXTLINE(cert-dcl21-cpp) 167b1994897Sopenharmony_ciToken::Type Context::operator--(int) 168b1994897Sopenharmony_ci{ 169b1994897Sopenharmony_ci Token::Type last_id = id; 170b1994897Sopenharmony_ci 171b1994897Sopenharmony_ci if (1 < number) { 172b1994897Sopenharmony_ci end = false; 173b1994897Sopenharmony_ci 174b1994897Sopenharmony_ci --number; 175b1994897Sopenharmony_ci 176b1994897Sopenharmony_ci id = this->tokens[number - 1].type; 177b1994897Sopenharmony_ci 178b1994897Sopenharmony_ci token = std::string_view(&*(tokens[number - 1].whole_line.begin() + tokens[number - 1].bound_left), 179b1994897Sopenharmony_ci tokens[number - 1].bound_right - tokens[number - 1].bound_left); 180b1994897Sopenharmony_ci } else { 181b1994897Sopenharmony_ci end = false; 182b1994897Sopenharmony_ci } 183b1994897Sopenharmony_ci 184b1994897Sopenharmony_ci return last_id; 185b1994897Sopenharmony_ci} 186b1994897Sopenharmony_ci 187b1994897Sopenharmony_ciToken::Type Context::operator--() 188b1994897Sopenharmony_ci{ 189b1994897Sopenharmony_ci if (1 < number) { 190b1994897Sopenharmony_ci end = false; 191b1994897Sopenharmony_ci 192b1994897Sopenharmony_ci --number; 193b1994897Sopenharmony_ci 194b1994897Sopenharmony_ci id = this->tokens[number - 1].type; 195b1994897Sopenharmony_ci 196b1994897Sopenharmony_ci token = std::string_view(&*(tokens[number - 1].whole_line.begin() + tokens[number - 1].bound_left), 197b1994897Sopenharmony_ci tokens[number - 1].bound_right - tokens[number - 1].bound_left); 198b1994897Sopenharmony_ci } else { 199b1994897Sopenharmony_ci end = false; 200b1994897Sopenharmony_ci } 201b1994897Sopenharmony_ci 202b1994897Sopenharmony_ci return id; 203b1994897Sopenharmony_ci} 204b1994897Sopenharmony_ci 205b1994897Sopenharmony_ciToken::Type Context::operator*() 206b1994897Sopenharmony_ci{ 207b1994897Sopenharmony_ci return id; 208b1994897Sopenharmony_ci} 209b1994897Sopenharmony_ci 210b1994897Sopenharmony_ci} // namespace panda::pandasm 211