Merge pull request #4697 from georgerennie/george/zero_width_string

frontends/ast.cc: special-case zero width strings as "\0"
This commit is contained in:
Lofty 2024-11-04 15:40:04 +00:00 committed by GitHub
commit 52c231dd64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 8 deletions

View File

@ -877,16 +877,25 @@ AstNode *AstNode::mkconst_str(const std::vector<RTLIL::State> &v)
// create an AST node for a constant (using a string as value) // create an AST node for a constant (using a string as value)
AstNode *AstNode::mkconst_str(const std::string &str) AstNode *AstNode::mkconst_str(const std::string &str)
{ {
std::vector<RTLIL::State> data; AstNode *node;
data.reserve(str.size() * 8);
for (size_t i = 0; i < str.size(); i++) { // LRM 1364-2005 5.2.3.3 The empty string literal ("") shall be considered
unsigned char ch = str[str.size() - i - 1]; // equivalent to the ASCII NUL ("\0")
for (int j = 0; j < 8; j++) { if (str.empty()) {
data.push_back((ch & 1) ? State::S1 : State::S0); node = AstNode::mkconst_int(0, false, 8);
ch = ch >> 1; } else {
std::vector<RTLIL::State> data;
data.reserve(str.size() * 8);
for (size_t i = 0; i < str.size(); i++) {
unsigned char ch = str[str.size() - i - 1];
for (int j = 0; j < 8; j++) {
data.push_back((ch & 1) ? State::S1 : State::S0);
ch = ch >> 1;
}
} }
node = AstNode::mkconst_bits(data, false);
} }
AstNode *node = AstNode::mkconst_bits(data, false);
node->is_string = true; node->is_string = true;
node->str = str; node->str = str;
return node; return node;